Commit 6cc6ec01 by Kien Truong Committed by Stéphane Graber

Sort the cgroup memory settings before applying.

Add a function to sort the cgroup settings before applying. Currently, the function will put memory.memsw.limit_in_bytes after memory.limit_in_bytes setting so the container will start regardless of the order specified in the input. Fix #453 Signed-off-by: 's avatarKien Truong <duckientruong@gmail.com>
parent 72453c4c
...@@ -1888,12 +1888,15 @@ static int do_setup_cgroup_limits(struct cgfs_data *d, ...@@ -1888,12 +1888,15 @@ static int do_setup_cgroup_limits(struct cgfs_data *d,
{ {
struct lxc_list *iterator; struct lxc_list *iterator;
struct lxc_cgroup *cg; struct lxc_cgroup *cg;
struct lxc_list *sorted_cgroup_settings;
int ret = -1; int ret = -1;
if (lxc_list_empty(cgroup_settings)) if (lxc_list_empty(cgroup_settings))
return 0; return 0;
lxc_list_for_each(iterator, cgroup_settings) { sorted_cgroup_settings = sort_cgroup_settings(cgroup_settings);
lxc_list_for_each(iterator, sorted_cgroup_settings) {
cg = iterator->elem; cg = iterator->elem;
if (do_devices == !strncmp("devices", cg->subsystem, 7)) { if (do_devices == !strncmp("devices", cg->subsystem, 7)) {
...@@ -1916,6 +1919,10 @@ static int do_setup_cgroup_limits(struct cgfs_data *d, ...@@ -1916,6 +1919,10 @@ static int do_setup_cgroup_limits(struct cgfs_data *d,
ret = 0; ret = 0;
INFO("cgroup has been setup"); INFO("cgroup has been setup");
out: out:
lxc_list_for_each(iterator, sorted_cgroup_settings) {
lxc_list_del(iterator);
free(iterator);
}
return ret; return ret;
} }
......
...@@ -1221,6 +1221,7 @@ static bool cgm_setup_limits(void *hdata, struct lxc_list *cgroup_settings, bool ...@@ -1221,6 +1221,7 @@ static bool cgm_setup_limits(void *hdata, struct lxc_list *cgroup_settings, bool
struct lxc_list *iterator; struct lxc_list *iterator;
struct lxc_cgroup *cg; struct lxc_cgroup *cg;
bool ret = false; bool ret = false;
struct lxc_list *sorted_cgroup_settings;
if (lxc_list_empty(cgroup_settings)) if (lxc_list_empty(cgroup_settings))
return true; return true;
...@@ -1233,7 +1234,9 @@ static bool cgm_setup_limits(void *hdata, struct lxc_list *cgroup_settings, bool ...@@ -1233,7 +1234,9 @@ static bool cgm_setup_limits(void *hdata, struct lxc_list *cgroup_settings, bool
return false; return false;
} }
lxc_list_for_each(iterator, cgroup_settings) { sorted_cgroup_settings = sort_cgroup_settings(cgroup_settings);
lxc_list_for_each(iterator, sorted_cgroup_settings) {
char controller[100], *p; char controller[100], *p;
cg = iterator->elem; cg = iterator->elem;
if (do_devices != !strncmp("devices", cg->subsystem, 7)) if (do_devices != !strncmp("devices", cg->subsystem, 7))
...@@ -1261,6 +1264,10 @@ static bool cgm_setup_limits(void *hdata, struct lxc_list *cgroup_settings, bool ...@@ -1261,6 +1264,10 @@ static bool cgm_setup_limits(void *hdata, struct lxc_list *cgroup_settings, bool
ret = true; ret = true;
INFO("cgroup limits have been setup"); INFO("cgroup limits have been setup");
out: out:
lxc_list_for_each(iterator, sorted_cgroup_settings) {
lxc_list_del(iterator);
free(iterator);
}
cgm_dbus_disconnect(); cgm_dbus_disconnect();
return ret; return ret;
} }
......
...@@ -4557,3 +4557,38 @@ void suggest_default_idmap(void) ...@@ -4557,3 +4557,38 @@ void suggest_default_idmap(void)
free(gname); free(gname);
free(uname); free(uname);
} }
/*
* Return the list of cgroup_settings sorted according to the following rules
* 1. Put memory.limit_in_bytes before memory.memsw.limit_in_bytes
*/
struct lxc_list *sort_cgroup_settings(struct lxc_list* cgroup_settings)
{
struct lxc_list *result;
struct lxc_list *memsw_limit = NULL;
struct lxc_list *it = NULL;
struct lxc_cgroup *cg = NULL;
struct lxc_list *item = NULL;
result = malloc(sizeof(*result));
lxc_list_init(result);
/*Iterate over the cgroup settings and copy them to the output list*/
lxc_list_for_each(it, cgroup_settings) {
item = malloc(sizeof(*item));
item->elem = it->elem;
cg = it->elem;
if (strcmp(cg->subsystem, "memory.memsw.limit_in_bytes") == 0) {
/* Store the memsw_limit location */
memsw_limit = item;
} else if (strcmp(cg->subsystem, "memory.limit_in_bytes") == 0 && memsw_limit != NULL) {
/* lxc.cgroup.memory.memsw.limit_in_bytes is found before
* lxc.cgroup.memory.limit_in_bytes, swap these two items */
item->elem = memsw_limit->elem;
memsw_limit->elem = it->elem;
}
lxc_list_add_tail(result, item);
}
return result;
}
\ No newline at end of file
...@@ -430,4 +430,5 @@ extern void tmp_proc_unmount(struct lxc_conf *lxc_conf); ...@@ -430,4 +430,5 @@ extern void tmp_proc_unmount(struct lxc_conf *lxc_conf);
void remount_all_slave(void); void remount_all_slave(void);
extern void suggest_default_idmap(void); extern void suggest_default_idmap(void);
FILE *write_mount_file(struct lxc_list *mount); FILE *write_mount_file(struct lxc_list *mount);
struct lxc_list *sort_cgroup_settings(struct lxc_list* cgroup_settings);
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment