History

cgroup: Use separate src/dst nodes when preloading css_sets for migration

Each cset (css_set) is pinned by its tasks. When we're moving tasks around across csets for a migration, we need to hold the source and destination csets to ensure that they don't go away while we're moving tasks about. This is done by linking cset->mg_preload_node on either the mgctx->preloaded_src_csets or mgctx->preloaded_dst_csets list. Using the same cset->mg_preload_node for both the src and dst lists was deemed okay as a cset can't be both the source and destination at the same time. Unfortunately, this overloading becomes problematic when multiple tasks are involved in a migration and some of them are identity noop migrations while others are actually moving across cgroups. For example, this can happen with the following sequence on cgroup1: #1> mkdir -p /sys/fs/cgroup/misc/a/b #2> echo $$ > /sys/fs/cgroup/misc/a/cgroup.procs #3> RUN_A_COMMAND_WHICH_CREATES_MULTIPLE_THREADS & #4> PID=$! #5> echo $PID > /sys/fs/cgroup/misc/a/b/tasks #6> echo $PID > /sys/fs/cgroup/misc/a/cgroup.procs the process including the group leader back into a. In this final migration, non-leader threads would be doing identity migration while the group leader is doing an actual one. After #3, let's say the whole process was in cset A, and that after #4, the leader moves to cset B. Then, during #6, the following happens: 1. cgroup_migrate_add_src() is called on B for the leader. 2. cgroup_migrate_add_src() is called on A for the other threads. 3. cgroup_migrate_prepare_dst() is called. It scans the src list. 4. It notices that B wants to migrate to A, so it tries to A to the dst list but realizes that its ->mg_preload_node is already busy. 5. and then it notices A wants to migrate to A as it's an identity migration, it culls it by list_del_init()'ing its ->mg_preload_node and putting references accordingly. 6. The rest of migration takes place with B on the src list but nothing on the dst list. This means that A isn't held while migration is in progress. If all tasks leave A before the migration finishes and the incoming task pins it, the cset will be destroyed leading to use-after-free. This is caused by overloading cset->mg_preload_node for both src and dst preload lists. We wanted to exclude the cset from the src list but ended up inadvertently excluding it from the dst list too. This patch fixes the issue by separating out cset->mg_preload_node into ->mg_src_preload_node and ->mg_dst_preload_node, so that the src and dst preloadings don't interfere with each other.

Tejun Heo committed 4 years ago

Commit 07fd5b61 parent 48a23ec2 files · +25 −15Browse files

Unified · Split

2 files
  1. include/linux/cgroup-defs.h +2 −1
  2. kernel/cgroup/cgroup.c +23 −14
include/linux/cgroup-defs.h+2−1
@@ -264,7 +264,8 @@ struct css_set { · 263 unmodified lines
264 * List of csets participating in the on-going migration either as264 * List of csets participating in the on-going migration either as
265 * source or destination. Protected by cgroup_mutex.265 * source or destination. Protected by cgroup_mutex.
266 */266 */
267 struct list_head mg_preload_node;267 struct list_head mg_src_preload_node;
268 struct list_head mg_dst_preload_node;
268 struct list_head mg_node;269 struct list_head mg_node;
269 270
270 /*271 /*
kernel/cgroup/cgroup.c+23−14
@@ -765,7 +765,8 @@ struct css_set init_css_set = { · 764 unmodified lines
765 .task_iters = LIST_HEAD_INIT(init_css_set.task_iters),765 .task_iters = LIST_HEAD_INIT(init_css_set.task_iters),
766 .threaded_csets = LIST_HEAD_INIT(init_css_set.threaded_csets),766 .threaded_csets = LIST_HEAD_INIT(init_css_set.threaded_csets),
767 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),767 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
768 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),768 .mg_src_preload_node = LIST_HEAD_INIT(init_css_set.mg_src_preload_node),
769 .mg_dst_preload_node = LIST_HEAD_INIT(init_css_set.mg_dst_preload_node),
769 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),770 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
770 771
771 /*772 /*
@@ -1240,7 +1241,8 @@ static struct css_set *find_css_set(struct css_set *old_cset, · 468 unmodified lines
1240 INIT_LIST_HEAD(&cset->threaded_csets);1241 INIT_LIST_HEAD(&cset->threaded_csets);
1241 INIT_HLIST_NODE(&cset->hlist);1242 INIT_HLIST_NODE(&cset->hlist);
1242 INIT_LIST_HEAD(&cset->cgrp_links);1243 INIT_LIST_HEAD(&cset->cgrp_links);
1243 INIT_LIST_HEAD(&cset->mg_preload_node);1244 INIT_LIST_HEAD(&cset->mg_src_preload_node);
1245 INIT_LIST_HEAD(&cset->mg_dst_preload_node);
1244 INIT_LIST_HEAD(&cset->mg_node);1246 INIT_LIST_HEAD(&cset->mg_node);
1245 1247
1246 /* Copy the set of subsystem state objects generated in1248 /* Copy the set of subsystem state objects generated in
@@ -2597,21 +2599,27 @@ int cgroup_migrate_vet_dst(struct cgroup *dst_cgrp) · 1350 unmodified lines
2597 */2599 */
2598void cgroup_migrate_finish(struct cgroup_mgctx *mgctx)2600void cgroup_migrate_finish(struct cgroup_mgctx *mgctx)
2599{2601{
2600 LIST_HEAD(preloaded);
2601 struct css_set *cset, *tmp_cset;2602 struct css_set *cset, *tmp_cset;
2602 2603
2603 lockdep_assert_held(&cgroup_mutex);2604 lockdep_assert_held(&cgroup_mutex);
2604 2605
2605 spin_lock_irq(&css_set_lock);2606 spin_lock_irq(&css_set_lock);
2606 2607
2607 list_splice_tail_init(&mgctx->preloaded_src_csets, &preloaded);2608 list_for_each_entry_safe(cset, tmp_cset, &mgctx->preloaded_src_csets,
2608 list_splice_tail_init(&mgctx->preloaded_dst_csets, &preloaded);2609 mg_src_preload_node) {
2610 cset->mg_src_cgrp = NULL;
2611 cset->mg_dst_cgrp = NULL;
2612 cset->mg_dst_cset = NULL;
2613 list_del_init(&cset->mg_src_preload_node);
2614 put_css_set_locked(cset);
2615 }
2609 2616
2610 list_for_each_entry_safe(cset, tmp_cset, &preloaded, mg_preload_node) {2617 list_for_each_entry_safe(cset, tmp_cset, &mgctx->preloaded_dst_csets,
2618 mg_dst_preload_node) {
2611 cset->mg_src_cgrp = NULL;2619 cset->mg_src_cgrp = NULL;
2612 cset->mg_dst_cgrp = NULL;2620 cset->mg_dst_cgrp = NULL;
2613 cset->mg_dst_cset = NULL;2621 cset->mg_dst_cset = NULL;
2614 list_del_init(&cset->mg_preload_node);2622 list_del_init(&cset->mg_dst_preload_node);
2615 put_css_set_locked(cset);2623 put_css_set_locked(cset);
2616 }2624 }
2617 2625
@@ -2651,7 +2659,7 @@ void cgroup_migrate_add_src(struct css_set *src_cset, · 33 unmodified lines
2651 if (src_cset->dead)2659 if (src_cset->dead)
2652 return;2660 return;
2653 2661
2654 if (!list_empty(&src_cset->mg_preload_node))2662 if (!list_empty(&src_cset->mg_src_preload_node))
2655 return;2663 return;
2656 2664
2657 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);2665 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
@@ -2664,7 +2672,7 @@ void cgroup_migrate_add_src(struct css_set *src_cset, · 6 unmodified lines
2664 src_cset->mg_src_cgrp = src_cgrp;2672 src_cset->mg_src_cgrp = src_cgrp;
2665 src_cset->mg_dst_cgrp = dst_cgrp;2673 src_cset->mg_dst_cgrp = dst_cgrp;
2666 get_css_set(src_cset);2674 get_css_set(src_cset);
2667 list_add_tail(&src_cset->mg_preload_node, &mgctx->preloaded_src_csets);2675 list_add_tail(&src_cset->mg_src_preload_node, &mgctx->preloaded_src_csets);
2668}2676}
2669 2677
2670/**2678/**
@@ -2689,7 +2697,7 @@ int cgroup_migrate_prepare_dst(struct cgroup_mgctx *mgctx) · 18 unmodified lines
2689 2697
2690 /* look up the dst cset for each src cset and link it to src */2698 /* look up the dst cset for each src cset and link it to src */
2691 list_for_each_entry_safe(src_cset, tmp_cset, &mgctx->preloaded_src_csets,2699 list_for_each_entry_safe(src_cset, tmp_cset, &mgctx->preloaded_src_csets,
2692 mg_preload_node) {2700 mg_src_preload_node) {
2693 struct css_set *dst_cset;2701 struct css_set *dst_cset;
2694 struct cgroup_subsys *ss;2702 struct cgroup_subsys *ss;
2695 int ssid;2703 int ssid;
@@ -2708,7 +2716,7 @@ int cgroup_migrate_prepare_dst(struct cgroup_mgctx *mgctx) · 12 unmodified lines
2708 if (src_cset == dst_cset) {2716 if (src_cset == dst_cset) {
2709 src_cset->mg_src_cgrp = NULL;2717 src_cset->mg_src_cgrp = NULL;
2710 src_cset->mg_dst_cgrp = NULL;2718 src_cset->mg_dst_cgrp = NULL;
2711 list_del_init(&src_cset->mg_preload_node);2719 list_del_init(&src_cset->mg_src_preload_node);
2712 put_css_set(src_cset);2720 put_css_set(src_cset);
2713 put_css_set(dst_cset);2721 put_css_set(dst_cset);
2714 continue;2722 continue;
@@ -2716,8 +2724,8 @@ int cgroup_migrate_prepare_dst(struct cgroup_mgctx *mgctx) · 1 unmodified line
2716 2724
2717 src_cset->mg_dst_cset = dst_cset;2725 src_cset->mg_dst_cset = dst_cset;
2718 2726
2719 if (list_empty(&dst_cset->mg_preload_node))2727 if (list_empty(&dst_cset->mg_dst_preload_node))
2720 list_add_tail(&dst_cset->mg_preload_node,2728 list_add_tail(&dst_cset->mg_dst_preload_node,
2721 &mgctx->preloaded_dst_csets);2729 &mgctx->preloaded_dst_csets);
2722 else2730 else
2723 put_css_set(dst_cset);2731 put_css_set(dst_cset);
@@ -2963,7 +2971,8 @@ static int cgroup_update_dfl_csses(struct cgroup *cgrp) · 239 unmodified lines
2963 goto out_finish;2971 goto out_finish;
2964 2972
2965 spin_lock_irq(&css_set_lock);2973 spin_lock_irq(&css_set_lock);
2966 list_for_each_entry(src_cset, &mgctx.preloaded_src_csets, mg_preload_node) {2974 list_for_each_entry(src_cset, &mgctx.preloaded_src_csets,
2975 mg_src_preload_node) {
2967 struct task_struct *task, *ntask;2976 struct task_struct *task, *ntask;
2968 2977
2969 /* all tasks in src_csets need to be migrated */2978 /* all tasks in src_csets need to be migrated */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t