[systemd-devel] [PATCH] cgroup: fix incorrectly setting memory cgroup

Gao feng gaofeng at cn.fujitsu.com
Thu Sep 12 23:43:04 PDT 2013


If the memory_limit of unit is -1, we should write "-1"
to the file memory.limit_in_bytes. not the (unit64_t) -1.

otherwise the memory.limit_in_bytes will be set to zero.
---
 src/core/cgroup.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/core/cgroup.c b/src/core/cgroup.c
index aee93ba..244baff 100644
--- a/src/core/cgroup.c
+++ b/src/core/cgroup.c
@@ -257,14 +257,21 @@ void cgroup_context_apply(CGroupContext *c, CGroupControllerMask mask, const cha
 
         if (mask & CGROUP_MEMORY) {
                 char buf[DECIMAL_STR_MAX(uint64_t) + 1];
+                if (c->memory_limit != (uint64_t) -1) {
+                        sprintf(buf, "%" PRIu64 "\n", c->memory_limit);
+                        r = cg_set_attribute("memory", path, "memory.limit_in_bytes", buf);
+                } else
+                        r = cg_set_attribute("memory", path, "memory.limit_in_bytes", "-1");
 
-                sprintf(buf, "%" PRIu64 "\n", c->memory_limit);
-                r = cg_set_attribute("memory", path, "memory.limit_in_bytes", buf);
                 if (r < 0)
                         log_error("Failed to set memory.limit_in_bytes on %s: %s", path, strerror(-r));
 
-                sprintf(buf, "%" PRIu64 "\n", c->memory_soft_limit);
-                r = cg_set_attribute("memory", path, "memory.soft_limit_in_bytes", buf);
+                if (c->memory_soft_limit != (uint64_t) -1) {
+                        sprintf(buf, "%" PRIu64 "\n", c->memory_soft_limit);
+                        r = cg_set_attribute("memory", path, "memory.soft_limit_in_bytes", buf);
+                } else
+                        r = cg_set_attribute("memory", path, "memory.soft_limit_in_bytes", "-1");
+
                 if (r < 0)
                         log_error("Failed to set memory.soft_limit_in_bytes on %s: %s", path, strerror(-r));
         }
-- 
1.8.3.1



More information about the systemd-devel mailing list