[systemd-devel] [PATCH 2/4] cgroups: support for KernelMemoryLimit= setting

Mika Eloranta mel at ohmu.fi
Wed Oct 9 16:18:35 PDT 2013


Add a KernelMemoryLimit setting that behaves the same way
as MemoryLimit, except that it controls the
memory.kmem.limit_in_bytes cgroup attribute.
---
 man/systemd.resource-control.xml      |  5 ++++-
 src/core/cgroup.c                     | 18 +++++++++++++++++-
 src/core/cgroup.h                     |  1 +
 src/core/dbus-cgroup.c                |  1 +
 src/core/load-fragment-gperf.gperf.m4 |  1 +
 src/core/load-fragment.c              | 34 ++++++++++++++++++++++++++++++++++
 src/core/load-fragment.h              |  1 +
 src/systemctl/systemctl.c             |  3 ++-
 8 files changed, 61 insertions(+), 3 deletions(-)

diff --git a/man/systemd.resource-control.xml b/man/systemd.resource-control.xml
index 606e078..dca7294 100644
--- a/man/systemd.resource-control.xml
+++ b/man/systemd.resource-control.xml
@@ -138,7 +138,7 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>.
       </varlistentry>
 
       <varlistentry>
-        <term><varname>MemoryLimit=, MemoryAndSwapLimit=<replaceable>bytes</replaceable></varname></term>
+        <term><varname>MemoryLimit=, MemoryAndSwapLimit=, KernelMemoryLimit=<replaceable>bytes</replaceable></varname></term>
 
         <listitem>
           <para>Specify the limit on maximum memory usage of the
@@ -154,6 +154,9 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>.
           <literal>memory.limit_in_bytes</literal> control group
           attribute, which sets the limit for the sum of the used
           memory and used swap space.
+          <literal>KernelMemoryLimit</literal> controls the
+          <literal>memory.kmem.limit_in_bytes</literal> control group
+          attribute, which limits the amound of kernel memory used.
           For details about these control group attributes,
           see <ulink
           url="https://www.kernel.org/doc/Documentation/cgroups/memory.txt">memory.txt</ulink>.</para>
diff --git a/src/core/cgroup.c b/src/core/cgroup.c
index 3b465cc..41a09b0 100644
--- a/src/core/cgroup.c
+++ b/src/core/cgroup.c
@@ -35,6 +35,7 @@ void cgroup_context_init(CGroupContext *c) {
         c->cpu_shares = 1024;
         c->memory_limit = (uint64_t) -1;
         c->memory_and_swap_limit = (uint64_t) -1;
+        c->kernel_memory_limit = (uint64_t) -1;
         c->blockio_weight = 1000;
 }
 
@@ -96,6 +97,7 @@ void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix) {
                 "%sBlockIOWeight=%lu\n"
                 "%sMemoryLimit=%" PRIu64 "\n"
                 "%sMemoryAndSwapLimit=%" PRIu64 "\n"
+                "%sKernelMemoryLimit=%" PRIu64 "\n"
                 "%sDevicePolicy=%s\n",
                 prefix, yes_no(c->cpu_accounting),
                 prefix, yes_no(c->blockio_accounting),
@@ -104,6 +106,7 @@ void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix) {
                 prefix, c->blockio_weight,
                 prefix, c->memory_limit,
                 prefix, c->memory_and_swap_limit,
+                prefix, c->kernel_memory_limit,
                 prefix, cgroup_device_policy_to_string(c->device_policy));
 
         LIST_FOREACH(device_allow, a, c->device_allow)
@@ -278,6 +281,18 @@ void cgroup_context_apply(CGroupContext *c, CGroupControllerMask mask, const cha
 
                 if (r < 0)
                         log_error("Failed to set memory.memsw.limit_in_bytes on %s: %s", path, strerror(-r));
+
+                if (c->kernel_memory_limit != (uint64_t) -1) {
+                        sprintf(buf, "%" PRIu64 "\n", c->kernel_memory_limit);
+                        r = cg_set_attribute("memory", path, "memory.kmem.limit_in_bytes", buf);
+                } else {
+                        r = cg_set_attribute("memory", path, "memory.kmem.limit_in_bytes", "-1");
+                        if (r == -EACCES) /* CONFIG_MEMCG_KMEM not built into kernel, ignore */
+                                r = 0;
+                }
+
+                if (r < 0)
+                        log_error("Failed to set memory.kmem.limit_in_bytes on %s: %s", path, strerror(-r));
         }
 
         if (mask & CGROUP_DEVICE) {
@@ -341,7 +356,8 @@ CGroupControllerMask cgroup_context_get_mask(CGroupContext *c) {
 
         if (c->memory_accounting ||
             c->memory_limit != (uint64_t) -1 ||
-            c->memory_and_swap_limit != (uint64_t) -1)
+            c->memory_and_swap_limit != (uint64_t) -1 ||
+            c->kernel_memory_limit != (uint64_t) -1)
                 mask |= CGROUP_MEMORY;
 
         if (c->device_allow || c->device_policy != CGROUP_AUTO)
diff --git a/src/core/cgroup.h b/src/core/cgroup.h
index c84eed2..644eafc 100644
--- a/src/core/cgroup.h
+++ b/src/core/cgroup.h
@@ -78,6 +78,7 @@ struct CGroupContext {
 
         uint64_t memory_limit;
         uint64_t memory_and_swap_limit;
+        uint64_t kernel_memory_limit;
 
         CGroupDevicePolicy device_policy;
         LIST_HEAD(CGroupDeviceAllow, device_allow);
diff --git a/src/core/dbus-cgroup.c b/src/core/dbus-cgroup.c
index d86a03a..d398e7c 100644
--- a/src/core/dbus-cgroup.c
+++ b/src/core/dbus-cgroup.c
@@ -134,6 +134,7 @@ const BusProperty bus_cgroup_context_properties[] = {
         { "MemoryAccounting",        bus_property_append_bool,            "b",     offsetof(CGroupContext, memory_accounting)  },
         { "MemoryLimit",             bus_property_append_uint64,          "t",     offsetof(CGroupContext, memory_limit)       },
         { "MemoryAndSwapLimit",      bus_property_append_uint64,          "t",     offsetof(CGroupContext, memory_and_swap_limit) },
+        { "KernelMemoryLimit",       bus_property_append_uint64,          "t",     offsetof(CGroupContext, kernel_memory_limit) },
         { "DevicePolicy",            bus_cgroup_append_device_policy,     "s",     offsetof(CGroupContext, device_policy)      },
         { "DeviceAllow",             bus_cgroup_append_device_allow,      "a(ss)", 0                                           },
         {}
diff --git a/src/core/load-fragment-gperf.gperf.m4 b/src/core/load-fragment-gperf.gperf.m4
index f86352e..e60d014 100644
--- a/src/core/load-fragment-gperf.gperf.m4
+++ b/src/core/load-fragment-gperf.gperf.m4
@@ -90,6 +90,7 @@ $1.CPUShares,                    config_parse_cpu_shares,            0,
 $1.MemoryAccounting,             config_parse_bool,                  0,                             offsetof($1, cgroup_context.memory_accounting)
 $1.MemoryLimit,                  config_parse_memory_limit,          0,                             offsetof($1, cgroup_context)
 $1.MemoryAndSwapLimit,           config_parse_memory_and_swap_limit, 0,                             offsetof($1, cgroup_context)
+$1.KernelMemoryLimit,            config_parse_kernel_memory_limit,   0,                             offsetof($1, cgroup_context)
 $1.DeviceAllow,                  config_parse_device_allow,          0,                             offsetof($1, cgroup_context)
 $1.DevicePolicy,                 config_parse_device_policy,         0,                             offsetof($1, cgroup_context.device_policy)
 $1.BlockIOAccounting,            config_parse_bool,                  0,                             offsetof($1, cgroup_context.blockio_accounting)
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
index 6dabf4c..91a9971 100644
--- a/src/core/load-fragment.c
+++ b/src/core/load-fragment.c
@@ -2105,6 +2105,39 @@ int config_parse_memory_and_swap_limit(
         return 0;
 }
 
+int config_parse_kernel_memory_limit(
+                const char *unit,
+                const char *filename,
+                unsigned line,
+                const char *section,
+                const char *lvalue,
+                int ltype,
+                const char *rvalue,
+                void *data,
+                void *userdata) {
+
+        CGroupContext *c = data;
+        off_t bytes;
+        int r;
+
+        if (isempty(rvalue)) {
+                c->kernel_memory_limit = (uint64_t) -1;
+                return 0;
+        }
+
+        assert_cc(sizeof(uint64_t) == sizeof(off_t));
+
+        r = parse_bytes(rvalue, &bytes);
+        if (r < 0) {
+                log_syntax(unit, LOG_ERR, filename, line, EINVAL,
+                           "Kernel memory limit '%s' invalid. Ignoring.", rvalue);
+                return 0;
+        }
+
+        c->kernel_memory_limit = (uint64_t) bytes;
+        return 0;
+}
+
 int config_parse_device_allow(
                 const char *unit,
                 const char *filename,
@@ -2746,6 +2779,7 @@ void unit_dump_config_items(FILE *f) {
                 { config_parse_cpu_shares,            "SHARES" },
                 { config_parse_memory_limit,          "LIMIT" },
                 { config_parse_memory_and_swap_limit, "LIMIT" },
+                { config_parse_kernel_memory_limit,   "LIMIT" },
                 { config_parse_device_allow,          "DEVICE" },
                 { config_parse_device_policy,         "POLICY" },
                 { config_parse_blockio_bandwidth,     "BANDWIDTH" },
diff --git a/src/core/load-fragment.h b/src/core/load-fragment.h
index 846825c..c79c7f2 100644
--- a/src/core/load-fragment.h
+++ b/src/core/load-fragment.h
@@ -79,6 +79,7 @@ int config_parse_unit_slice(const char *unit, const char *filename, unsigned lin
 int config_parse_cpu_shares(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 int config_parse_memory_limit(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 int config_parse_memory_and_swap_limit(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
+int config_parse_kernel_memory_limit(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 int config_parse_device_policy(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 int config_parse_device_allow(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 int config_parse_blockio_weight(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index 9e6bb06..cf00334 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -3658,7 +3658,8 @@ static int append_assignment(DBusMessageIter *iter, const char *assignment) {
                     !dbus_message_iter_append_basic(&sub, DBUS_TYPE_BOOLEAN, &b))
                         return log_oom();
 
-        } else if (streq(field, "MemoryLimit") || streq(field, "MemoryAndSwapLimit")) {
+        } else if (streq(field, "MemoryLimit") || streq(field, "MemoryAndSwapLimit")
+                   || streq(field, "KernelMemoryLimit")) {
                 off_t bytes;
                 uint64_t u;
 
-- 
1.8.3.2



More information about the systemd-devel mailing list