c2dfb7
From 614b43fcad3a16dfde5ad606b43c3aa1adacc30a Mon Sep 17 00:00:00 2001
c2dfb7
From: Chris Down <chris@chrisdown.name>
c2dfb7
Date: Fri, 3 May 2019 08:32:41 -0400
c2dfb7
Subject: [PATCH] cgroup: Support 0-value for memory protection directives
c2dfb7
c2dfb7
These make sense to be explicitly set at 0 (which has a different effect
c2dfb7
than the default, since it can affect processing of `DefaultMemoryXXX`).
c2dfb7
c2dfb7
Without this, it's not easily possible to relinquish memory protection
c2dfb7
for a subtree, which is not great.
c2dfb7
c2dfb7
(cherry picked from commit 22bf131be278b95a4a204514d37a4344cf6365c6)
c2dfb7
c2dfb7
Related: #1763435
c2dfb7
---
c2dfb7
 src/core/dbus-cgroup.c   | 17 +++++++++--------
c2dfb7
 src/core/load-fragment.c |  2 +-
c2dfb7
 2 files changed, 10 insertions(+), 9 deletions(-)
c2dfb7
c2dfb7
diff --git a/src/core/dbus-cgroup.c b/src/core/dbus-cgroup.c
c2dfb7
index e34ff3a016..39778a8dd4 100644
c2dfb7
--- a/src/core/dbus-cgroup.c
c2dfb7
+++ b/src/core/dbus-cgroup.c
c2dfb7
@@ -606,6 +606,7 @@ BUS_DEFINE_SET_CGROUP_WEIGHT(cpu_shares, CGROUP_MASK_CPU, CGROUP_CPU_SHARES_IS_O
c2dfb7
 BUS_DEFINE_SET_CGROUP_WEIGHT(io_weight, CGROUP_MASK_IO, CGROUP_WEIGHT_IS_OK, CGROUP_WEIGHT_INVALID);
c2dfb7
 BUS_DEFINE_SET_CGROUP_WEIGHT(blockio_weight, CGROUP_MASK_BLKIO, CGROUP_BLKIO_WEIGHT_IS_OK, CGROUP_BLKIO_WEIGHT_INVALID);
c2dfb7
 BUS_DEFINE_SET_CGROUP_LIMIT(memory, CGROUP_MASK_MEMORY, physical_memory_scale, 1);
c2dfb7
+BUS_DEFINE_SET_CGROUP_LIMIT(memory_protection, CGROUP_MASK_MEMORY, physical_memory_scale, 0);
c2dfb7
 BUS_DEFINE_SET_CGROUP_LIMIT(swap, CGROUP_MASK_MEMORY, physical_memory_scale, 0);
c2dfb7
 BUS_DEFINE_SET_CGROUP_LIMIT(tasks_max, CGROUP_MASK_PIDS, system_tasks_max_scale, 1);
c2dfb7
 #pragma GCC diagnostic pop
c2dfb7
@@ -665,16 +666,16 @@ int bus_cgroup_set_property(
c2dfb7
                 return bus_cgroup_set_boolean(u, name, &c->memory_accounting, CGROUP_MASK_MEMORY, message, flags, error);
c2dfb7
 
c2dfb7
         if (streq(name, "MemoryMin"))
c2dfb7
-                return bus_cgroup_set_memory(u, name, &c->memory_min, message, flags, error);
c2dfb7
+                return bus_cgroup_set_memory_protection(u, name, &c->memory_min, message, flags, error);
c2dfb7
 
c2dfb7
         if (streq(name, "MemoryLow"))
c2dfb7
-                return bus_cgroup_set_memory(u, name, &c->memory_low, message, flags, error);
c2dfb7
+                return bus_cgroup_set_memory_protection(u, name, &c->memory_low, message, flags, error);
c2dfb7
 
c2dfb7
         if (streq(name, "DefaultMemoryMin"))
c2dfb7
-                return bus_cgroup_set_memory(u, name, &c->default_memory_min, message, flags, error);
c2dfb7
+                return bus_cgroup_set_memory_protection(u, name, &c->default_memory_min, message, flags, error);
c2dfb7
 
c2dfb7
         if (streq(name, "DefaultMemoryLow"))
c2dfb7
-                return bus_cgroup_set_memory(u, name, &c->default_memory_low, message, flags, error);
c2dfb7
+                return bus_cgroup_set_memory_protection(u, name, &c->default_memory_low, message, flags, error);
c2dfb7
 
c2dfb7
         if (streq(name, "MemoryHigh"))
c2dfb7
                 return bus_cgroup_set_memory(u, name, &c->memory_high, message, flags, error);
c2dfb7
@@ -689,16 +690,16 @@ int bus_cgroup_set_property(
c2dfb7
                 return bus_cgroup_set_memory(u, name, &c->memory_limit, message, flags, error);
c2dfb7
 
c2dfb7
         if (streq(name, "MemoryMinScale"))
c2dfb7
-                return bus_cgroup_set_memory_scale(u, name, &c->memory_min, message, flags, error);
c2dfb7
+                return bus_cgroup_set_memory_protection_scale(u, name, &c->memory_min, message, flags, error);
c2dfb7
 
c2dfb7
         if (streq(name, "MemoryLowScale"))
c2dfb7
-                return bus_cgroup_set_memory_scale(u, name, &c->memory_low, message, flags, error);
c2dfb7
+                return bus_cgroup_set_memory_protection_scale(u, name, &c->memory_low, message, flags, error);
c2dfb7
 
c2dfb7
         if (streq(name, "DefaultMemoryMinScale"))
c2dfb7
-                return bus_cgroup_set_memory_scale(u, name, &c->default_memory_min, message, flags, error);
c2dfb7
+                return bus_cgroup_set_memory_protection_scale(u, name, &c->default_memory_min, message, flags, error);
c2dfb7
 
c2dfb7
         if (streq(name, "DefaultMemoryLowScale"))
c2dfb7
-                return bus_cgroup_set_memory_scale(u, name, &c->default_memory_low, message, flags, error);
c2dfb7
+                return bus_cgroup_set_memory_protection_scale(u, name, &c->default_memory_low, message, flags, error);
c2dfb7
 
c2dfb7
         if (streq(name, "MemoryHighScale"))
c2dfb7
                 return bus_cgroup_set_memory_scale(u, name, &c->memory_high, message, flags, error);
c2dfb7
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
c2dfb7
index 3b8ee6b124..33fdb82754 100644
c2dfb7
--- a/src/core/load-fragment.c
c2dfb7
+++ b/src/core/load-fragment.c
c2dfb7
@@ -3090,7 +3090,7 @@ int config_parse_memory_limit(
c2dfb7
                         bytes = physical_memory_scale(r, 100U);
c2dfb7
 
c2dfb7
                 if (bytes >= UINT64_MAX ||
c2dfb7
-                    (bytes <= 0 && !streq(lvalue, "MemorySwapMax"))) {
c2dfb7
+                    (bytes <= 0 && !STR_IN_SET(lvalue, "MemorySwapMax", "MemoryLow", "MemoryMin", "DefaultMemoryLow", "DefaultMemoryMin"))) {
c2dfb7
                         log_syntax(unit, LOG_ERR, filename, line, 0, "Memory limit '%s' out of range, ignoring.", rvalue);
c2dfb7
                         return 0;
c2dfb7
                 }