blk-cgroup: fix blkg list and policy data races
Subsystems (3)
v3 cover blk-cgroup: fix blkg list and policy data races
From: Yu Kuai <[email protected]>
Hi,
This series fixes races around q->blkg_list and blkg policy data lifetime.
Patch 1 protects blkg_destroy_all()'s q->blkg_list walk with blkcg_mutex.
Patches 2-3 fix races between blkcg_activate_policy() and concurrent blkg destruction.
Patch 4 factors the policy data teardown loop into a helper after the race fixes.
Changes since v2:
- Rebase on the latest block-7.2 branch.
Changes since v1:
- Drop the BFQ q->blkg_list patch because the current block tree already has a stronger fix in commit 17b2d950a3c0 ("block, bfq: protect async queue reset with blkcg locks").
- Add Reviewed-by tags from Tang Yizhou.
Yu Kuai (1):
blk-cgroup: protect q->blkg_list iteration in blkg_destroy_all() with
blkcg_mutex
Zheng Qixing (3):
blk-cgroup: fix race between policy activation and blkg destruction
blk-cgroup: skip dying blkg in blkcg_activate_policy()
blk-cgroup: factor policy pd teardown loop into helper
block/blk-cgroup.c | 65 +++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 30 deletions(-)
Signature · 1 line
2.51.0
PATCH v3 1/4 blk-cgroup: protect q->blkg_list iteration in blkg_destroy_all() with blkcg_mutex
From: Yu Kuai <[email protected]>
blkg_destroy_all() iterates q->blkg_list without holding blkcg_mutex, which can race with blkg_free_workfn() that removes blkgs from the list while holding blkcg_mutex.
Add blkcg_mutex protection around the q->blkg_list iteration to prevent potential list corruption or use-after-free issues.
--- block/blk-cgroup.c | 3 +++ 1 file changed, 3 insertions(+)
block/blk-cgroup.c +3 -0
@@ -567,10 +567,11 @@ static void blkg_destroy_all(struct gendisk *disk)struct blkcg_gq *blkg;int count =BLKG_DESTROY_BATCH_SIZE ;int i; restart: +mutex_lock (&q->blkcg_mutex);spin_lock_irq (&q->queue_lock);list_for_each_entry (blkg, &q->blkg_list, q_node) {struct blkcg *blkcg = blkg->blkcg;if (hlist_unhashed (&blkg->blkcg_node))
@@ -585,10 +586,11 @@ static void blkg_destroy_all(struct gendisk *disk)* it when a batch of blkgs are destroyed. */ if (!(--count)) { count =BLKG_DESTROY_BATCH_SIZE ;spin_unlock_irq (&q->queue_lock); +mutex_unlock (&q->blkcg_mutex);cond_resched (); goto restart; } }
@@ -604,10 +606,11 @@ static void blkg_destroy_all(struct gendisk *disk)__clear_bit (pol->plid, q->blkcg_pols); } q->root_blkg =NULL ;spin_unlock_irq (&q->queue_lock); +mutex_unlock (&q->blkcg_mutex);wake_up_var (&q->root_blkg); }static void blkg_iostat_set (struct blkg_iostat *dst,struct blkg_iostat *src)
Signature · 1 line
2.51.0
PATCH v3 2/4 blk-cgroup: fix race between policy activation and blkg destruction
In reply to Yu KuaiFrom: Zheng Qixing <[email protected]>
When switching an IO scheduler on a block device, blkcg_activate_policy() allocates blkg_policy_data (pd) for all blkgs attached to the queue. However, blkcg_activate_policy() may race with concurrent blkcg deletion, leading to use-after-free and memory leak issues.
The use-after-free occurs in the following race:
T1 (blkcg_activate_policy):
- Successfully allocates pd for blkg1 (loop0->queue, blkcgA)
- Fails to allocate pd for blkg2 (loop0->queue, blkcgB)
- Enters the enomem rollback path to release blkg1 resources
T2 (blkcg deletion):
- blkcgA is deleted concurrently
- blkg1 is freed via blkg_free_workfn()
- blkg1->pd is freed
T1 (continued):
- Rollback path accesses blkg1->pd->online after pd is freed
- Triggers use-after-free
In addition, blkg_free_workfn() frees pd before removing the blkg from q->blkg_list. This allows blkcg_activate_policy() to allocate a new pd for a blkg that is being destroyed, leaving the newly allocated pd unreachable when the blkg is finally freed.
Fix these races by extending blkcg_mutex coverage to serialize blkcg_activate_policy() rollback and blkg destruction, ensuring pd lifecycle is synchronized with blkg list visibility.
--- block/blk-cgroup.c | 3 +++ 1 file changed, 3 insertions(+)
block/blk-cgroup.c +3 -0
@@ -1564,10 +1564,12 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol)if (WARN_ON_ONCE (!pol->pd_alloc_fn || !pol->pd_free_fn))return -EINVAL ;if (queue_is_mq (q)) memflags =blk_mq_freeze_queue (q); + +mutex_lock (&q->blkcg_mutex); retry:spin_lock_irq (&q->queue_lock);/* blkg_list is pushed at the head, reverse walk to initialize parents first */ list_for_each_entry_reverse (blkg, &q->blkg_list, q_node) {
@@ -1626,10 +1628,11 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol)__set_bit (pol->plid, q->blkcg_pols); ret =0 ;spin_unlock_irq (&q->queue_lock); out: +mutex_unlock (&q->blkcg_mutex);if (queue_is_mq (q))blk_mq_unfreeze_queue (q, memflags);if (pinned_blkg)blkg_put (pinned_blkg);if (pd_prealloc)
Signature · 1 line
2.51.0
PATCH v3 3/4 blk-cgroup: skip dying blkg in blkcg_activate_policy()
In reply to Yu KuaiFrom: Zheng Qixing <[email protected]>
When switching IO schedulers on a block device, blkcg_activate_policy() can race with concurrent blkcg deletion, leading to a use-after-free in rcu_accelerate_cbs.
T1: T2:
blkg_destroy
kill(&blkg->refcnt) // blkg->refcnt=1->0
blkg_release // call_rcu(__blkg_release)
...
blkg_free_workfn
->pd_free_fn(pd)elv_iosched_storeelevator_switch
...
iterate blkg listblkg_get(blkg) // blkg->refcnt=0->1
list_del_init(&blkg->q_node)
blkg_put(pinned_blkg) // blkg->refcnt=1->0blkg_release // call_rcu againrcu_accelerate_cbs // uaf
Fix this by checking hlist_unhashed(&blkg->blkcg_node) before getting a reference to the blkg. This is the same check used in blkg_destroy() to detect if a blkg has already been destroyed. If the blkg is already unhashed, skip processing it since it's being destroyed.
--- block/blk-cgroup.c | 2 ++ 1 file changed, 2 insertions(+)
block/blk-cgroup.c +2 -0
@@ -1575,10 +1575,12 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol)list_for_each_entry_reverse (blkg, &q->blkg_list, q_node) {struct blkg_policy_data *pd;if (blkg->pd[pol->plid])continue ; +if (hlist_unhashed (&blkg->blkcg_node)) +continue ;/* If prealloc matches, use it; otherwise try GFP_NOWAIT */ if (blkg == pinned_blkg) { pd = pd_prealloc; pd_prealloc =NULL ;
Signature · 1 line
2.51.0
PATCH v3 4/4 blk-cgroup: factor policy pd teardown loop into helper
In reply to Yu KuaiFrom: Zheng Qixing <[email protected]>
Move the teardown sequence which offlines and frees per-policy blkg_policy_data (pd) into a helper for readability.
No functional change intended.
--- block/blk-cgroup.c | 57 ++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 30 deletions(-)
block/blk-cgroup.c +27 -30
@@ -1527,10 +1527,35 @@ struct cgroup_subsys io_cgrp_subsys = { .depends_on = 1 << memory_cgrp_id,#endif };EXPORT_SYMBOL_GPL (io_cgrp_subsys); +/* +* Tear down per-blkg policy data for @pol on @q. +*/ +static void blkcg_policy_teardown_pds (struct request_queue *q, +const struct blkcg_policy *pol) +{ +struct blkcg_gq *blkg; + +list_for_each_entry (blkg, &q->blkg_list, q_node) { +struct blkcg *blkcg = blkg->blkcg; +struct blkg_policy_data *pd; + +spin_lock (&blkcg->lock); + pd = blkg->pd[pol->plid]; +if (pd) { +if (pd->online && pol->pd_offline_fn) + pol->pd_offline_fn (pd); + pd->online = false; + pol->pd_free_fn (pd); +WRITE_ONCE (blkg->pd[pol->plid],NULL ); + } +spin_unlock (&blkcg->lock); + } +} +/** * blkcg_activate_policy - activate a blkcg policy on a gendisk * @disk: gendisk of interest * @pol: blkcg policy to activate *
@@ -1642,25 +1667,11 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol)return ret; enomem:/* alloc failed, take down everything */ spin_lock_irq (&q->queue_lock); -list_for_each_entry (blkg, &q->blkg_list, q_node) { -struct blkcg *blkcg = blkg->blkcg; -struct blkg_policy_data *pd; - -spin_lock (&blkcg->lock); - pd = blkg->pd[pol->plid]; -if (pd) { -if (pd->online && pol->pd_offline_fn) - pol->pd_offline_fn (pd); - pd->online = false; - pol->pd_free_fn (pd); -WRITE_ONCE (blkg->pd[pol->plid],NULL ); - } -spin_unlock (&blkcg->lock); - } +blkcg_policy_teardown_pds (q, pol);spin_unlock_irq (&q->queue_lock); ret = -ENOMEM ; goto out; }EXPORT_SYMBOL_GPL (blkcg_activate_policy);
@@ -1675,11 +1686,10 @@ EXPORT_SYMBOL_GPL(blkcg_activate_policy);*/ void blkcg_deactivate_policy (struct gendisk *disk,const struct blkcg_policy *pol) {struct request_queue *q = disk->queue; -struct blkcg_gq *blkg;unsigned int memflags;if (!blkcg_policy_enabled (q, pol))return ;
@@ -1688,24 +1698,11 @@ void blkcg_deactivate_policy(struct gendisk *disk,mutex_lock (&q->blkcg_mutex);spin_lock_irq (&q->queue_lock);__clear_bit (pol->plid, q->blkcg_pols); - -list_for_each_entry (blkg, &q->blkg_list, q_node) { -struct blkcg *blkcg = blkg->blkcg; - -spin_lock (&blkcg->lock); -if (blkg->pd[pol->plid]) { -if (blkg->pd[pol->plid]->online && pol->pd_offline_fn) - pol->pd_offline_fn (blkg->pd[pol->plid]); - pol->pd_free_fn (blkg->pd[pol->plid]); - blkg->pd[pol->plid] =NULL ; - } -spin_unlock (&blkcg->lock); - } - +blkcg_policy_teardown_pds (q, pol);spin_unlock_irq (&q->queue_lock);mutex_unlock (&q->blkcg_mutex);if (queue_is_mq (q))blk_mq_unfreeze_queue (q, memflags);
Signature · 1 line
2.51.0