Skip to content

Commit 144232b

Browse files
htejunaxboe
authored andcommitted
blkcg: blkcg_css_alloc() should grab blkcg_pol_mutex while iterating blkcg_policy[]
An entry in blkcg_policy[] is stable while there are non-bypassing in-flight IOs on a request_queue which has the policy activated. This is why most derefs of blkcg_policy[] don't need explicit locking; however, blkcg_css_alloc() isn't invoked from IO path and thus doesn't have this protection and may race policies being added and removed. Fix it by adding explicit blkcg_pol_mutex protection around blkcg_policy[] iteration in blkcg_css_alloc(). Signed-off-by: Tejun Heo <tj@kernel.org> Fixes: e48453c ("block, cgroup: implement policy-specific per-blkcg data") Cc: Vivek Goyal <vgoyal@redhat.com> Cc: Arianna Avanzini <avanzini.arianna@gmail.com> Signed-off-by: Jens Axboe <axboe@fb.com>
1 parent 838f13b commit 144232b

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

block/blk-cgroup.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,8 @@ blkcg_css_alloc(struct cgroup_subsys_state *parent_css)
844844
goto free_blkcg;
845845
}
846846

847+
mutex_lock(&blkcg_pol_mutex);
848+
847849
for (i = 0; i < BLKCG_MAX_POLS ; i++) {
848850
struct blkcg_policy *pol = blkcg_policy[i];
849851
struct blkcg_policy_data *cpd;
@@ -860,6 +862,7 @@ blkcg_css_alloc(struct cgroup_subsys_state *parent_css)
860862
BUG_ON(blkcg->pd[i]);
861863
cpd = kzalloc(pol->cpd_size, GFP_KERNEL);
862864
if (!cpd) {
865+
mutex_unlock(&blkcg_pol_mutex);
863866
ret = ERR_PTR(-ENOMEM);
864867
goto free_pd_blkcg;
865868
}
@@ -868,6 +871,7 @@ blkcg_css_alloc(struct cgroup_subsys_state *parent_css)
868871
pol->cpd_init_fn(blkcg);
869872
}
870873

874+
mutex_unlock(&blkcg_pol_mutex);
871875
done:
872876
spin_lock_init(&blkcg->lock);
873877
INIT_RADIX_TREE(&blkcg->blkg_tree, GFP_ATOMIC);

0 commit comments

Comments
 (0)