Skip to content

Commit 4d137b8

Browse files
valeriyvdovinavagin
authored andcommitted
cgroup/restore: split prepare_task_cgroup code into two separate functions
This does cgroup namespace creation separately from joining task cgroups. This makes the code more logical, because creating cgroup namespace also involves joining cgroups but these cgroups can be different to task's cgroups as they are cgroup namespace roots (cgns_prefix), and mixing all of them together may lead to misunderstanding. Another positive thing is that we consolidate !item->parent checks in one place in restore_task_with_children. Signed-off-by: Valeriy Vdovin <[email protected]> Signed-off-by: Pavel Tikhomirov <[email protected]>
1 parent 104a828 commit 4d137b8

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

criu/cgroup.c

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,17 +1202,12 @@ static int prepare_cgns(CgSetEntry *se)
12021202
return 0;
12031203
}
12041204

1205-
static int move_in_cgroup(CgSetEntry *se, bool setup_cgns)
1205+
static int move_in_cgroup(CgSetEntry *se)
12061206
{
12071207
int i;
12081208

12091209
pr_info("Move into %d\n", se->id);
12101210

1211-
if (setup_cgns && prepare_cgns(se) < 0) {
1212-
pr_err("failed preparing cgns\n");
1213-
return -1;
1214-
}
1215-
12161211
for (i = 0; i < se->n_ctls; i++) {
12171212
char aux[PATH_MAX];
12181213
int fd = -1, err, j, aux_off;
@@ -1252,7 +1247,44 @@ static int move_in_cgroup(CgSetEntry *se, bool setup_cgns)
12521247
return 0;
12531248
}
12541249

1255-
int prepare_task_cgroup(struct pstree_item *me)
1250+
int prepare_cgroup_namespace(struct pstree_item *root_task)
1251+
{
1252+
CgSetEntry *se;
1253+
1254+
if (opts.manage_cgroups == CG_MODE_IGNORE)
1255+
return 0;
1256+
1257+
if (root_task->parent) {
1258+
pr_err("Expecting root_task to restore cgroup namespace\n");
1259+
return -1;
1260+
}
1261+
1262+
/*
1263+
* If on dump all dumped tasks are in same cgset with criu we don't
1264+
* dump cgsets and thus cgroup namespaces and rely that on restore
1265+
* criu caller would prepare proper cgset/cgns for us. Also in case
1266+
* of --unprivileged we don't even have the root cgset here.
1267+
*/
1268+
if (!rsti(root_task)->cg_set || rsti(root_task)->cg_set == root_cg_set) {
1269+
pr_info("Cgroup namespace inherited from parent\n");
1270+
return 0;
1271+
}
1272+
1273+
se = find_rst_set_by_id(rsti(root_task)->cg_set);
1274+
if (!se) {
1275+
pr_err("No set %d found\n", rsti(root_task)->cg_set);
1276+
return -1;
1277+
}
1278+
1279+
if (prepare_cgns(se) < 0) {
1280+
pr_err("failed preparing cgns\n");
1281+
return -1;
1282+
}
1283+
1284+
return 0;
1285+
}
1286+
1287+
int restore_task_cgroup(struct pstree_item *me)
12561288
{
12571289
struct pstree_item *parent = me->parent;
12581290
CgSetEntry *se;
@@ -1284,13 +1316,7 @@ int prepare_task_cgroup(struct pstree_item *me)
12841316
return -1;
12851317
}
12861318

1287-
/* Since don't support nesting of cgroup namespaces, let's only set up
1288-
* the cgns (if it exists) in the init task. In the future, we should
1289-
* just check that the cgns prefix string matches for all the entries
1290-
* in the cgset, and only unshare if that's true.
1291-
*/
1292-
1293-
return move_in_cgroup(se, !me->parent);
1319+
return move_in_cgroup(se);
12941320
}
12951321

12961322
void fini_cgroup(void)

criu/cr-restore.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1827,6 +1827,13 @@ static int restore_task_with_children(void *_arg)
18271827
/* Wait prepare_userns */
18281828
if (restore_finish_ns_stage(CR_STATE_ROOT_TASK, CR_STATE_PREPARE_NAMESPACES) < 0)
18291829
goto err;
1830+
1831+
/*
1832+
* Since we don't support nesting of cgroup namespaces, let's
1833+
* only set up the cgns (if it exists) in the init task.
1834+
*/
1835+
if (prepare_cgroup_namespace(current) < 0)
1836+
goto err;
18301837
}
18311838

18321839
if (needs_prep_creds(current) && (prepare_userns_creds()))
@@ -1838,7 +1845,7 @@ static int restore_task_with_children(void *_arg)
18381845
* we will only move the root one there, others will
18391846
* just have it inherited.
18401847
*/
1841-
if (prepare_task_cgroup(current) < 0)
1848+
if (restore_task_cgroup(current) < 0)
18421849
goto err;
18431850

18441851
/* Restore root task */

criu/include/cgroup.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ struct parasite_dump_cgroup_args;
99
extern u32 root_cg_set;
1010
int dump_thread_cgroup(const struct pstree_item *, u32 *, struct parasite_dump_cgroup_args *args, int id);
1111
int dump_cgroups(void);
12-
int prepare_task_cgroup(struct pstree_item *);
12+
int restore_task_cgroup(struct pstree_item *);
13+
int prepare_cgroup_namespace(struct pstree_item *);
1314
int prepare_cgroup(void);
1415
/* Restore things like cpu_limit in known cgroups. */
1516
int prepare_cgroup_properties(void);

0 commit comments

Comments
 (0)