Skip to content

Commit 3dbaacf

Browse files
Ming Leiaxboe
authored andcommitted
blk-cgroup: wait for blkcg cleanup before initializing new disk
When a queue is shared across disk rebind (e.g., SCSI unbind/bind), the previous disk's blkcg state is cleaned up asynchronously via disk_release() -> blkcg_exit_disk(). If the new disk's blkcg_init_disk() runs before that cleanup finishes, we may overwrite q->root_blkg while the old one is still alive, and radix_tree_insert() in blkg_create() fails with -EEXIST because the old blkg entries still occupy the same queue id slot in blkcg->blkg_tree. This causes the sd probe to fail with -ENOMEM. Fix it by waiting in blkcg_init_disk() for root_blkg to become NULL, which indicates the previous disk's blkcg cleanup has completed. Fixes: 1059699 ("block: move blkcg initialization/destroy into disk allocation/release handler") Cc: Yi Zhang <yi.zhang@redhat.com> Signed-off-by: Ming Lei <ming.lei@redhat.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Link: https://patch.msgid.link/20260311032837.2368714-1-ming.lei@redhat.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent daa6c79 commit 3dbaacf

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

block/blk-cgroup.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <linux/backing-dev.h>
2525
#include <linux/slab.h>
2626
#include <linux/delay.h>
27+
#include <linux/wait_bit.h>
2728
#include <linux/atomic.h>
2829
#include <linux/ctype.h>
2930
#include <linux/resume_user_mode.h>
@@ -611,6 +612,8 @@ static void blkg_destroy_all(struct gendisk *disk)
611612

612613
q->root_blkg = NULL;
613614
spin_unlock_irq(&q->queue_lock);
615+
616+
wake_up_var(&q->root_blkg);
614617
}
615618

616619
static void blkg_iostat_set(struct blkg_iostat *dst, struct blkg_iostat *src)
@@ -1498,6 +1501,18 @@ int blkcg_init_disk(struct gendisk *disk)
14981501
struct blkcg_gq *new_blkg, *blkg;
14991502
bool preloaded;
15001503

1504+
/*
1505+
* If the queue is shared across disk rebind (e.g., SCSI), the
1506+
* previous disk's blkcg state is cleaned up asynchronously via
1507+
* disk_release() -> blkcg_exit_disk(). Wait for that cleanup to
1508+
* finish (indicated by root_blkg becoming NULL) before setting up
1509+
* new blkcg state. Otherwise, we may overwrite q->root_blkg while
1510+
* the old one is still alive, and radix_tree_insert() in
1511+
* blkg_create() will fail with -EEXIST because the old entries
1512+
* still occupy the same queue id slot in blkcg->blkg_tree.
1513+
*/
1514+
wait_var_event(&q->root_blkg, !READ_ONCE(q->root_blkg));
1515+
15011516
new_blkg = blkg_alloc(&blkcg_root, disk, GFP_KERNEL);
15021517
if (!new_blkg)
15031518
return -ENOMEM;

0 commit comments

Comments
 (0)