]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
api.c: Fix order of memory subsystem parameters generated by cgsnapshot
authorNikola Forró <nforro@redhat.com>
Thu, 16 Jan 2020 17:43:49 +0000 (18:43 +0100)
committerTom Hromatka <tom.hromatka@oracle.com>
Fri, 31 Jan 2020 20:59:20 +0000 (20:59 +0000)
Order of parameters usually doesn't matter, but that's not the case with
memory.limit_in_bytes and memory.memsw.limit_in_bytes. When the latter
is first in the list of parameters, the resulting configuration is not
loadable with cgconfigparser.

This happens because when a cgroup is created, both memory.limit_in_bytes
and memory.memsw.limit_in_bytes parameters are initialized to highest
value possible (RESOURCE_MAX). And because memory.memsw.limit_in_bytes
must be always higher or equal to memory.limit_in_bytes, it's impossible
to change its value first.

Make sure that after constructing parameter list of memory subsystem,
the mentioned parameters are in correct order.

Signed-off-by: Nikola Forró <nforro@redhat.com>
Reviewed-by: Tom Hromatka <tom.hromatka@oracle.com>
src/api.c

index c37855f3ac9c5d05808446ab73a85cd40218e14d..92730e6a435587573c6a254a03badf82c457dfb5 100644 (file)
--- a/src/api.c
+++ b/src/api.c
@@ -2715,6 +2715,30 @@ int cgroup_get_cgroup(struct cgroup *cgroup)
                        }
                }
                closedir(dir);
+
+               if (! strcmp(cgc->name, "memory")) {
+                       /*
+                        * Make sure that memory.limit_in_bytes is placed before
+                        * memory.memsw.limit_in_bytes in the list of values
+                        */
+                       int memsw_limit = -1;
+                       int mem_limit = -1;
+
+                       for (j = 0; j < cgc->index; j++) {
+                               if (! strcmp(cgc->values[j]->name,
+                                                               "memory.memsw.limit_in_bytes"))
+                                       memsw_limit = j;
+                               else if (! strcmp(cgc->values[j]->name,
+                                                                       "memory.limit_in_bytes"))
+                                       mem_limit = j;
+                       }
+
+                       if (memsw_limit >= 0 && memsw_limit < mem_limit) {
+                               struct control_value *val = cgc->values[memsw_limit];
+                               cgc->values[memsw_limit] = cgc->values[mem_limit];
+                               cgc->values[mem_limit] = val;
+                       }
+               }
        }
 
        /* Check if the group really exists or not */