Skip to content

Commit e485798

Browse files
committed
cgroup: use css_set->mg_dst_cgrp for the migration target cgroup
Migration can be multi-target on the default hierarchy when a controller is enabled - processes belonging to each child cgroup have to be moved to the child cgroup itself to refresh css association. This isn't a problem for cgroup_migrate_add_src() as each source css_set still maps to single source and target cgroups; however, cgroup_migrate_prepare_dst() is called once after all source css_sets are added and thus might not have a single destination cgroup. This is currently worked around by specifying NULL for @dst_cgrp and using the source's default cgroup as destination as the only multi-target migration in use is self-targetting. While this works, it's subtle and clunky. As all taget cgroups are already specified while preparing the source css_sets, this clunkiness can easily be removed by recording the target cgroup in each source css_set. This patch adds css_set->mg_dst_cgrp which is recorded on cgroup_migrate_src() and used by cgroup_migrate_prepare_dst(). This also makes migration code ready for arbitrary multi-target migration. Signed-off-by: Tejun Heo <tj@kernel.org>
1 parent 37ff9f8 commit e485798

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

include/linux/cgroup-defs.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,13 @@ struct css_set {
191191

192192
/*
193193
* If this cset is acting as the source of migration the following
194-
* two fields are set. mg_src_cgrp is the source cgroup of the
195-
* on-going migration and mg_dst_cset is the destination cset the
196-
* target tasks on this cset should be migrated to. Protected by
197-
* cgroup_mutex.
194+
* two fields are set. mg_src_cgrp and mg_dst_cgrp are
195+
* respectively the source and destination cgroups of the on-going
196+
* migration. mg_dst_cset is the destination cset the target tasks
197+
* on this cset should be migrated to. Protected by cgroup_mutex.
198198
*/
199199
struct cgroup *mg_src_cgrp;
200+
struct cgroup *mg_dst_cgrp;
200201
struct css_set *mg_dst_cset;
201202

202203
/*

kernel/cgroup.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,6 +2473,7 @@ static void cgroup_migrate_finish(struct list_head *preloaded_csets)
24732473
spin_lock_bh(&css_set_lock);
24742474
list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
24752475
cset->mg_src_cgrp = NULL;
2476+
cset->mg_dst_cgrp = NULL;
24762477
cset->mg_dst_cset = NULL;
24772478
list_del_init(&cset->mg_preload_node);
24782479
put_css_set_locked(cset);
@@ -2511,32 +2512,31 @@ static void cgroup_migrate_add_src(struct css_set *src_cset,
25112512
return;
25122513

25132514
WARN_ON(src_cset->mg_src_cgrp);
2515+
WARN_ON(src_cset->mg_dst_cgrp);
25142516
WARN_ON(!list_empty(&src_cset->mg_tasks));
25152517
WARN_ON(!list_empty(&src_cset->mg_node));
25162518

25172519
src_cset->mg_src_cgrp = src_cgrp;
2520+
src_cset->mg_dst_cgrp = dst_cgrp;
25182521
get_css_set(src_cset);
25192522
list_add(&src_cset->mg_preload_node, preloaded_csets);
25202523
}
25212524

25222525
/**
25232526
* cgroup_migrate_prepare_dst - prepare destination css_sets for migration
2524-
* @dst_cgrp: the destination cgroup (may be %NULL)
25252527
* @preloaded_csets: list of preloaded source css_sets
25262528
*
2527-
* Tasks are about to be moved to @dst_cgrp and all the source css_sets
2528-
* have been preloaded to @preloaded_csets. This function looks up and
2529-
* pins all destination css_sets, links each to its source, and append them
2530-
* to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
2531-
* source css_set is assumed to be its cgroup on the default hierarchy.
2529+
* Tasks are about to be moved and all the source css_sets have been
2530+
* preloaded to @preloaded_csets. This function looks up and pins all
2531+
* destination css_sets, links each to its source, and append them to
2532+
* @preloaded_csets.
25322533
*
25332534
* This function must be called after cgroup_migrate_add_src() has been
25342535
* called on each migration source css_set. After migration is performed
25352536
* using cgroup_migrate(), cgroup_migrate_finish() must be called on
25362537
* @preloaded_csets.
25372538
*/
2538-
static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
2539-
struct list_head *preloaded_csets)
2539+
static int cgroup_migrate_prepare_dst(struct list_head *preloaded_csets)
25402540
{
25412541
LIST_HEAD(csets);
25422542
struct css_set *src_cset, *tmp_cset;
@@ -2547,8 +2547,7 @@ static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
25472547
list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
25482548
struct css_set *dst_cset;
25492549

2550-
dst_cset = find_css_set(src_cset,
2551-
dst_cgrp ?: src_cset->dfl_cgrp);
2550+
dst_cset = find_css_set(src_cset, src_cset->mg_dst_cgrp);
25522551
if (!dst_cset)
25532552
goto err;
25542553

@@ -2561,6 +2560,7 @@ static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
25612560
*/
25622561
if (src_cset == dst_cset) {
25632562
src_cset->mg_src_cgrp = NULL;
2563+
src_cset->mg_dst_cgrp = NULL;
25642564
list_del_init(&src_cset->mg_preload_node);
25652565
put_css_set(src_cset);
25662566
put_css_set(dst_cset);
@@ -2657,7 +2657,7 @@ static int cgroup_attach_task(struct cgroup *dst_cgrp,
26572657
spin_unlock_bh(&css_set_lock);
26582658

26592659
/* prepare dst csets and commit */
2660-
ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
2660+
ret = cgroup_migrate_prepare_dst(&preloaded_csets);
26612661
if (!ret)
26622662
ret = cgroup_migrate(leader, threadgroup, dst_cgrp->root);
26632663

@@ -2916,7 +2916,7 @@ static int cgroup_update_dfl_csses(struct cgroup *cgrp)
29162916
spin_unlock_bh(&css_set_lock);
29172917

29182918
/* NULL dst indicates self on default hierarchy */
2919-
ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
2919+
ret = cgroup_migrate_prepare_dst(&preloaded_csets);
29202920
if (ret)
29212921
goto out_finish;
29222922

@@ -4156,7 +4156,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
41564156
cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
41574157
spin_unlock_bh(&css_set_lock);
41584158

4159-
ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
4159+
ret = cgroup_migrate_prepare_dst(&preloaded_csets);
41604160
if (ret)
41614161
goto out_err;
41624162

0 commit comments

Comments
 (0)