naccyde / rpms / systemd

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