f8452f
From 04f21709ff081a5f1a2b5ca746582a9c5c03db7f Mon Sep 17 00:00:00 2001
f8452f
From: Tejun Heo <tj@kernel.org>
f8452f
Date: Fri, 8 Jun 2018 17:33:14 -0700
f8452f
Subject: [PATCH] core: add MemoryMin
f8452f
f8452f
The kernel added support for a new cgroup memory controller knob memory.min in
f8452f
bf8d5d52ffe8 ("memcg: introduce memory.min") which was merged during v4.18
f8452f
merge window.
f8452f
f8452f
Add MemoryMin to support memory.min.
f8452f
f8452f
(cherry picked from commit 484226357789991de0b3363beb69258be06b4c92)
f8452f
f8452f
Resolves: #1763435
f8452f
---
f8452f
 doc/TRANSIENT-SETTINGS.md             |  1 +
f8452f
 man/systemd.resource-control.xml      | 21 +++++++++++++++++++++
f8452f
 src/core/cgroup.c                     |  5 ++++-
f8452f
 src/core/cgroup.h                     |  1 +
f8452f
 src/core/dbus-cgroup.c                |  7 +++++++
f8452f
 src/core/load-fragment-gperf.gperf.m4 |  1 +
f8452f
 src/core/load-fragment.c              |  4 +++-
f8452f
 src/shared/bus-unit-util.c            |  2 +-
f8452f
 src/systemctl/systemctl.c             | 11 +++++++++--
f8452f
 9 files changed, 48 insertions(+), 5 deletions(-)
f8452f
f8452f
diff --git a/doc/TRANSIENT-SETTINGS.md b/doc/TRANSIENT-SETTINGS.md
f8452f
index 0d2d3e9065..93865c0333 100644
f8452f
--- a/doc/TRANSIENT-SETTINGS.md
f8452f
+++ b/doc/TRANSIENT-SETTINGS.md
f8452f
@@ -222,6 +222,7 @@ All cgroup/resource control settings are available for transient units
f8452f
 ✓ AllowedCPUs=
f8452f
 ✓ AllowedMemoryNodes=
f8452f
 ✓ MemoryAccounting=
f8452f
+✓ MemoryMin=
f8452f
 ✓ MemoryLow=
f8452f
 ✓ MemoryHigh=
f8452f
 ✓ MemoryMax=
f8452f
diff --git a/man/systemd.resource-control.xml b/man/systemd.resource-control.xml
f8452f
index cfe19a6574..63a0c87565 100644
f8452f
--- a/man/systemd.resource-control.xml
f8452f
+++ b/man/systemd.resource-control.xml
f8452f
@@ -265,6 +265,27 @@
f8452f
         </listitem>
f8452f
       </varlistentry>
f8452f
 
f8452f
+      <varlistentry>
f8452f
+        <term><varname>MemoryMin=<replaceable>bytes</replaceable></varname></term>
f8452f
+
f8452f
+        <listitem>
f8452f
+          <para>Specify the memory usage protection of the executed processes in this unit. If the memory usages of
f8452f
+          this unit and all its ancestors are below their minimum boundaries, this unit's memory won't be reclaimed.</para>
f8452f
+
f8452f
+          <para>Takes a memory size in bytes. If the value is suffixed with K, M, G or T, the specified memory size is
f8452f
+          parsed as Kilobytes, Megabytes, Gigabytes, or Terabytes (with the base 1024), respectively. Alternatively, a
f8452f
+          percentage value may be specified, which is taken relative to the installed physical memory on the
f8452f
+          system. This controls the <literal>memory.min</literal> control group attribute. For details about this
f8452f
+          control group attribute, see 
f8452f
+          url="https://www.kernel.org/doc/Documentation/cgroup-v2.txt">cgroup-v2.txt</ulink>.</para>
f8452f
+
f8452f
+          <para>Implies <literal>MemoryAccounting=true</literal>.</para>
f8452f
+
f8452f
+          <para>This setting is supported only if the unified control group hierarchy is used and disables
f8452f
+          <varname>MemoryLimit=</varname>.</para>
f8452f
+        </listitem>
f8452f
+      </varlistentry>
f8452f
+
f8452f
       <varlistentry>
f8452f
         <term><varname>MemoryLow=<replaceable>bytes</replaceable></varname></term>
f8452f
 
f8452f
diff --git a/src/core/cgroup.c b/src/core/cgroup.c
f8452f
index f8b351a65d..9d09c65453 100644
f8452f
--- a/src/core/cgroup.c
f8452f
+++ b/src/core/cgroup.c
f8452f
@@ -220,6 +220,7 @@ void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix) {
f8452f
                 "%sStartupIOWeight=%" PRIu64 "\n"
f8452f
                 "%sBlockIOWeight=%" PRIu64 "\n"
f8452f
                 "%sStartupBlockIOWeight=%" PRIu64 "\n"
f8452f
+                "%sMemoryMin=%" PRIu64 "\n"
f8452f
                 "%sMemoryLow=%" PRIu64 "\n"
f8452f
                 "%sMemoryHigh=%" PRIu64 "\n"
f8452f
                 "%sMemoryMax=%" PRIu64 "\n"
f8452f
@@ -246,6 +247,7 @@ void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix) {
f8452f
                 prefix, c->startup_io_weight,
f8452f
                 prefix, c->blockio_weight,
f8452f
                 prefix, c->startup_blockio_weight,
f8452f
+                prefix, c->memory_min,
f8452f
                 prefix, c->memory_low,
f8452f
                 prefix, c->memory_high,
f8452f
                 prefix, c->memory_max,
f8452f
@@ -777,7 +779,7 @@ static void cgroup_apply_blkio_device_limit(Unit *u, const char *dev_path, uint6
f8452f
 }
f8452f
 
f8452f
 static bool cgroup_context_has_unified_memory_config(CGroupContext *c) {
f8452f
-        return c->memory_low > 0 || c->memory_high != CGROUP_LIMIT_MAX || c->memory_max != CGROUP_LIMIT_MAX || c->memory_swap_max != CGROUP_LIMIT_MAX;
f8452f
+        return c->memory_min > 0 || c->memory_low > 0 || c->memory_high != CGROUP_LIMIT_MAX || c->memory_max != CGROUP_LIMIT_MAX || c->memory_swap_max != CGROUP_LIMIT_MAX;
f8452f
 }
f8452f
 
f8452f
 static void cgroup_apply_unified_memory_limit(Unit *u, const char *file, uint64_t v) {
f8452f
@@ -1035,6 +1037,7 @@ static void cgroup_context_apply(
f8452f
                                         log_cgroup_compat(u, "Applying MemoryLimit %" PRIu64 " as MemoryMax", max);
f8452f
                         }
f8452f
 
f8452f
+                        cgroup_apply_unified_memory_limit(u, "memory.min", c->memory_min);
f8452f
                         cgroup_apply_unified_memory_limit(u, "memory.low", c->memory_low);
f8452f
                         cgroup_apply_unified_memory_limit(u, "memory.high", c->memory_high);
f8452f
                         cgroup_apply_unified_memory_limit(u, "memory.max", max);
f8452f
diff --git a/src/core/cgroup.h b/src/core/cgroup.h
f8452f
index 2ba57d3ded..5e1be87b20 100644
f8452f
--- a/src/core/cgroup.h
f8452f
+++ b/src/core/cgroup.h
f8452f
@@ -95,6 +95,7 @@ struct CGroupContext {
f8452f
         LIST_HEAD(CGroupIODeviceLimit, io_device_limits);
f8452f
         LIST_HEAD(CGroupIODeviceLatency, io_device_latencies);
f8452f
 
f8452f
+        uint64_t memory_min;
f8452f
         uint64_t memory_low;
f8452f
         uint64_t memory_high;
f8452f
         uint64_t memory_max;
f8452f
diff --git a/src/core/dbus-cgroup.c b/src/core/dbus-cgroup.c
f8452f
index 4555b33b1f..6ce5984a02 100644
f8452f
--- a/src/core/dbus-cgroup.c
f8452f
+++ b/src/core/dbus-cgroup.c
f8452f
@@ -353,6 +353,7 @@ const sd_bus_vtable bus_cgroup_vtable[] = {
f8452f
         SD_BUS_PROPERTY("BlockIOReadBandwidth", "a(st)", property_get_blockio_device_bandwidths, 0, 0),
f8452f
         SD_BUS_PROPERTY("BlockIOWriteBandwidth", "a(st)", property_get_blockio_device_bandwidths, 0, 0),
f8452f
         SD_BUS_PROPERTY("MemoryAccounting", "b", bus_property_get_bool, offsetof(CGroupContext, memory_accounting), 0),
f8452f
+        SD_BUS_PROPERTY("MemoryMin", "t", NULL, offsetof(CGroupContext, memory_min), 0),
f8452f
         SD_BUS_PROPERTY("MemoryLow", "t", NULL, offsetof(CGroupContext, memory_low), 0),
f8452f
         SD_BUS_PROPERTY("MemoryHigh", "t", NULL, offsetof(CGroupContext, memory_high), 0),
f8452f
         SD_BUS_PROPERTY("MemoryMax", "t", NULL, offsetof(CGroupContext, memory_max), 0),
f8452f
@@ -661,6 +662,9 @@ int bus_cgroup_set_property(
f8452f
         if (streq(name, "MemoryAccounting"))
f8452f
                 return bus_cgroup_set_boolean(u, name, &c->memory_accounting, CGROUP_MASK_MEMORY, message, flags, error);
f8452f
 
f8452f
+        if (streq(name, "MemoryMin"))
f8452f
+                return bus_cgroup_set_memory(u, name, &c->memory_min, message, flags, error);
f8452f
+
f8452f
         if (streq(name, "MemoryLow"))
f8452f
                 return bus_cgroup_set_memory(u, name, &c->memory_low, message, flags, error);
f8452f
 
f8452f
@@ -676,6 +680,9 @@ int bus_cgroup_set_property(
f8452f
         if (streq(name, "MemoryLimit"))
f8452f
                 return bus_cgroup_set_memory(u, name, &c->memory_limit, message, flags, error);
f8452f
 
f8452f
+        if (streq(name, "MemoryMinScale"))
f8452f
+                return bus_cgroup_set_memory_scale(u, name, &c->memory_min, message, flags, error);
f8452f
+
f8452f
         if (streq(name, "MemoryLowScale"))
f8452f
                 return bus_cgroup_set_memory_scale(u, name, &c->memory_low, message, flags, error);
f8452f
 
f8452f
diff --git a/src/core/load-fragment-gperf.gperf.m4 b/src/core/load-fragment-gperf.gperf.m4
f8452f
index 4defa82ac1..1c6e539f30 100644
f8452f
--- a/src/core/load-fragment-gperf.gperf.m4
f8452f
+++ b/src/core/load-fragment-gperf.gperf.m4
f8452f
@@ -171,6 +171,7 @@ $1.StartupCPUShares,             config_parse_cpu_shares,            0,
f8452f
 $1.CPUQuota,                     config_parse_cpu_quota,             0,                             offsetof($1, cgroup_context)
f8452f
 $1.CPUQuotaPeriodSec,            config_parse_sec_def_infinity,      0,                             offsetof($1, cgroup_context.cpu_quota_period_usec)
f8452f
 $1.MemoryAccounting,             config_parse_bool,                  0,                             offsetof($1, cgroup_context.memory_accounting)
f8452f
+$1.MemoryMin,                    config_parse_memory_limit,          0,                             offsetof($1, cgroup_context)
f8452f
 $1.MemoryLow,                    config_parse_memory_limit,          0,                             offsetof($1, cgroup_context)
f8452f
 $1.MemoryHigh,                   config_parse_memory_limit,          0,                             offsetof($1, cgroup_context)
f8452f
 $1.MemoryMax,                    config_parse_memory_limit,          0,                             offsetof($1, cgroup_context)
f8452f
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
f8452f
index 762b106007..d43b0f08f9 100644
f8452f
--- a/src/core/load-fragment.c
f8452f
+++ b/src/core/load-fragment.c
f8452f
@@ -3096,7 +3096,9 @@ int config_parse_memory_limit(
f8452f
                 }
f8452f
         }
f8452f
 
f8452f
-        if (streq(lvalue, "MemoryLow"))
f8452f
+        if (streq(lvalue, "MemoryMin"))
f8452f
+                c->memory_min = bytes;
f8452f
+        else if (streq(lvalue, "MemoryLow"))
f8452f
                 c->memory_low = bytes;
f8452f
         else if (streq(lvalue, "MemoryHigh"))
f8452f
                 c->memory_high = bytes;
f8452f
diff --git a/src/shared/bus-unit-util.c b/src/shared/bus-unit-util.c
f8452f
index ec45d6f86d..203c068d02 100644
f8452f
--- a/src/shared/bus-unit-util.c
f8452f
+++ b/src/shared/bus-unit-util.c
f8452f
@@ -429,7 +429,7 @@ static int bus_append_cgroup_property(sd_bus_message *m, const char *field, cons
f8452f
                 return 1;
f8452f
         }
f8452f
 
f8452f
-        if (STR_IN_SET(field, "MemoryLow", "MemoryHigh", "MemoryMax", "MemorySwapMax", "MemoryLimit", "TasksMax")) {
f8452f
+        if (STR_IN_SET(field, "MemoryMin", "MemoryLow", "MemoryHigh", "MemoryMax", "MemorySwapMax", "MemoryLimit", "TasksMax")) {
f8452f
 
f8452f
                 if (isempty(eq) || streq(eq, "infinity")) {
f8452f
                         r = sd_bus_message_append(m, "(sv)", field, "t", CGROUP_LIMIT_MAX);
f8452f
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
f8452f
index 559e49f104..35ad20f510 100644
f8452f
--- a/src/systemctl/systemctl.c
f8452f
+++ b/src/systemctl/systemctl.c
f8452f
@@ -3905,6 +3905,7 @@ typedef struct UnitStatusInfo {
f8452f
 
f8452f
         /* CGroup */
f8452f
         uint64_t memory_current;
f8452f
+        uint64_t memory_min;
f8452f
         uint64_t memory_low;
f8452f
         uint64_t memory_high;
f8452f
         uint64_t memory_max;
f8452f
@@ -4284,12 +4285,17 @@ static void print_status_info(
f8452f
 
f8452f
                 printf("   Memory: %s", format_bytes(buf, sizeof(buf), i->memory_current));
f8452f
 
f8452f
-                if (i->memory_low > 0 || i->memory_high != CGROUP_LIMIT_MAX ||
f8452f
-                    i->memory_max != CGROUP_LIMIT_MAX || i->memory_swap_max != CGROUP_LIMIT_MAX ||
f8452f
+                if (i->memory_min > 0 || i->memory_low > 0 ||
f8452f
+                    i->memory_high != CGROUP_LIMIT_MAX || i->memory_max != CGROUP_LIMIT_MAX ||
f8452f
+                    i->memory_swap_max != CGROUP_LIMIT_MAX ||
f8452f
                     i->memory_limit != CGROUP_LIMIT_MAX) {
f8452f
                         const char *prefix = "";
f8452f
 
f8452f
                         printf(" (");
f8452f
+                        if (i->memory_min > 0) {
f8452f
+                                printf("%smin: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_min));
f8452f
+                                prefix = " ";
f8452f
+                        }
f8452f
                         if (i->memory_low > 0) {
f8452f
                                 printf("%slow: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_low));
f8452f
                                 prefix = " ";
f8452f
@@ -5022,6 +5028,7 @@ static int show_one(
f8452f
                 { "Where",                          "s",              NULL,           offsetof(UnitStatusInfo, where)                             },
f8452f
                 { "What",                           "s",              NULL,           offsetof(UnitStatusInfo, what)                              },
f8452f
                 { "MemoryCurrent",                  "t",              NULL,           offsetof(UnitStatusInfo, memory_current)                    },
f8452f
+                { "MemoryMin",                      "t",              NULL,           offsetof(UnitStatusInfo, memory_min)                        },
f8452f
                 { "MemoryLow",                      "t",              NULL,           offsetof(UnitStatusInfo, memory_low)                        },
f8452f
                 { "MemoryHigh",                     "t",              NULL,           offsetof(UnitStatusInfo, memory_high)                       },
f8452f
                 { "MemoryMax",                      "t",              NULL,           offsetof(UnitStatusInfo, memory_max)                        },