From: Yu Kuai <yukuai@kernel.org>
To: "Jens Axboe" <axboe@kernel.dk>, "Tejun Heo" <tj@kernel.org>,
"Josef Bacik" <josef@toxicpanda.com>,
"Johannes Weiner" <hannes@cmpxchg.org>,
"Michal Koutný" <mkoutny@suse.com>
Cc: Yu Kuai <yukuai@fygo.io>, Christoph Hellwig <hch@lst.de>,
Tao Cui <cui.tao@linux.dev>, Jan Kara <jack@suse.cz>,
Ming Lei <ming.lei@redhat.com>, Jonathan Corbet <corbet@lwn.net>,
Shuah Khan <skhan@linuxfoundation.org>, Coly Li <colyli@fygo.io>,
Kent Overstreet <kent.overstreet@linux.dev>,
Alasdair Kergon <agk@redhat.com>,
Mike Snitzer <snitzer@kernel.org>,
Mikulas Patocka <mpatocka@redhat.com>,
Benjamin Marzinski <bmarzins@redhat.com>,
Song Liu <song@kernel.org>, Li Nan <magiclinan@didiglobal.com>,
Xiao Ni <xiao@kernel.org>,
Pankaj Gupta <pankaj.gupta.linux@gmail.com>,
Dan Williams <djbw@kernel.org>,
Vishal Verma <vishal.l.verma@intel.com>,
Dave Jiang <dave.jiang@intel.com>,
Alison Schofield <alison.schofield@intel.com>,
Ira Weiny <iweiny@kernel.org>,
Andreas Gruenbacher <agruenba@redhat.com>,
Matthew Wilcox <willy@infradead.org>,
Andrew Morton <akpm@linux-foundation.org>,
Chris Li <chrisl@kernel.org>, Kairui Song <kasong@tencent.com>,
Kemeng Shi <shikemeng@huaweicloud.com>,
Nhat Pham <nphamcs@gmail.com>, Baoquan He <baoquan.he@linux.dev>,
Barry Song <baohua@kernel.org>,
Youngjun Park <youngjun.park@lge.com>,
cgroups@vger.kernel.org, linux-doc@vger.kernel.org,
linux-kernel@vger.kernel.org, linux-block@vger.kernel.org,
linux-bcache@vger.kernel.org, dm-devel@lists.linux.dev,
linux-raid@vger.kernel.org, nvdimm@lists.linux.dev,
virtualization@lists.linux.dev, gfs2@lists.linux.dev,
linux-fsdevel@vger.kernel.org, linux-mm@kvack.org
Subject: [RFC PATCH v2 1/4] blk-cgroup: wait for old blkgs to leave queue before disk rebind
Date: Tue, 11 Aug 2026 14:47:41 +0800 [thread overview]
Message-ID: <20260811064744.1139446-2-yukuai@kernel.org> (raw)
In-Reply-To: <20260811064744.1139446-1-yukuai@kernel.org>
From: Yu Kuai <yukuai@fygo.io>
blkcg_init_disk() currently waits for q->root_blkg to become NULL before
initializing blkcg state for a rebound disk. However, blkg_destroy_all()
clears q->root_blkg after calling blkg_destroy() for each blkg. At that
point the initial references have only been killed, and the blkgs remain
on q->blkg_list until the remaining references drain and
blkg_free_workfn() removes them.
A rebound disk can therefore install new blkcg state while old blkgs are
still attached to the request queue. Wait for q->blkg_list to become empty
instead, and wake the waiter when the final blkg is removed. This covers
the complete queue-side blkg lifetime without adding separate state.
Fixes: 3dbaacf6ab68 ("blk-cgroup: wait for blkcg cleanup before initializing new disk")
Signed-off-by: Yu Kuai <yukuai@fygo.io>
---
block/blk-cgroup.c | 14 +++++---------
1 file changed, 5 insertions(+), 9 deletions(-)
diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c
index 354637f3b158..229348273437 100644
--- a/block/blk-cgroup.c
+++ b/block/blk-cgroup.c
@@ -131,10 +131,12 @@ static void blkg_free_workfn(struct work_struct *work)
blkcg_policy[i]->pd_free_fn(blkg->pd[i]);
if (blkg->parent)
blkg_put(blkg->parent);
spin_lock_irq(&q->queue_lock);
list_del_init(&blkg->q_node);
+ if (list_empty(&q->blkg_list))
+ wake_up_var(&q->blkg_list);
spin_unlock_irq(&q->queue_lock);
mutex_unlock(&q->blkcg_mutex);
/*
* Release blkcg css ref only after blkg is removed from q->blkg_list,
@@ -607,12 +609,10 @@ static void blkg_destroy_all(struct gendisk *disk)
}
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)
{
int i;
@@ -1457,18 +1457,14 @@ int blkcg_init_disk(struct gendisk *disk)
bool preloaded;
/*
* If the queue is shared across disk rebind (e.g., SCSI), the
* previous disk's blkcg state is cleaned up asynchronously via
- * disk_release() -> blkcg_exit_disk(). Wait for that cleanup to
- * finish (indicated by root_blkg becoming NULL) before setting up
- * new blkcg state. Otherwise, we may overwrite q->root_blkg while
- * the old one is still alive, and radix_tree_insert() in
- * blkg_create() will fail with -EEXIST because the old entries
- * still occupy the same queue id slot in blkcg->blkg_tree.
+ * disk_release() -> blkcg_exit_disk(). Wait for all old blkgs to be
+ * removed from the queue list before setting up new blkcg state.
*/
- wait_var_event(&q->root_blkg, !READ_ONCE(q->root_blkg));
+ wait_var_event(&q->blkg_list, list_empty_careful(&q->blkg_list));
new_blkg = blkg_alloc(&blkcg_root, disk, GFP_KERNEL);
if (!new_blkg)
return -ENOMEM;
--
2.51.0
next prev parent reply other threads:[~2026-08-11 6:50 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-11 6:47 [RFC PATCH v2 0/4] blk-cgroup: store blkcg in bio before blkcg_mutex conversion Yu Kuai
2026-08-11 6:47 ` Yu Kuai [this message]
2026-08-14 6:50 ` [RFC PATCH v2 1/4] blk-cgroup: wait for old blkgs to leave queue before disk rebind Christoph Hellwig
2026-08-11 6:47 ` [RFC PATCH v2 2/4] blk-cgroup: use a request_queue rhashtable for blkg lookup Yu Kuai
2026-08-14 7:12 ` Christoph Hellwig
2026-08-14 7:50 ` Yu Kuai
2026-08-14 8:15 ` Christoph Hellwig
2026-08-11 6:47 ` [RFC PATCH v2 3/4] blk-cgroup: store blkcg in bio instead of blkg Yu Kuai
2026-08-11 6:47 ` [RFC PATCH v2 4/4] blk-cgroup: move async bio punt state to blkcg Yu Kuai
2026-08-14 6:45 ` [RFC PATCH v2 0/4] blk-cgroup: store blkcg in bio before blkcg_mutex conversion Christoph Hellwig
2026-08-14 7:53 ` yu kuai
2026-08-14 8:16 ` Christoph Hellwig
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260811064744.1139446-2-yukuai@kernel.org \
--to=yukuai@kernel.org \
--cc=agk@redhat.com \
--cc=agruenba@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=alison.schofield@intel.com \
--cc=axboe@kernel.dk \
--cc=baohua@kernel.org \
--cc=baoquan.he@linux.dev \
--cc=bmarzins@redhat.com \
--cc=cgroups@vger.kernel.org \
--cc=chrisl@kernel.org \
--cc=colyli@fygo.io \
--cc=corbet@lwn.net \
--cc=cui.tao@linux.dev \
--cc=dave.jiang@intel.com \
--cc=djbw@kernel.org \
--cc=dm-devel@lists.linux.dev \
--cc=gfs2@lists.linux.dev \
--cc=hannes@cmpxchg.org \
--cc=hch@lst.de \
--cc=iweiny@kernel.org \
--cc=jack@suse.cz \
--cc=josef@toxicpanda.com \
--cc=kasong@tencent.com \
--cc=kent.overstreet@linux.dev \
--cc=linux-bcache@vger.kernel.org \
--cc=linux-block@vger.kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-raid@vger.kernel.org \
--cc=magiclinan@didiglobal.com \
--cc=ming.lei@redhat.com \
--cc=mkoutny@suse.com \
--cc=mpatocka@redhat.com \
--cc=nphamcs@gmail.com \
--cc=nvdimm@lists.linux.dev \
--cc=pankaj.gupta.linux@gmail.com \
--cc=shikemeng@huaweicloud.com \
--cc=skhan@linuxfoundation.org \
--cc=snitzer@kernel.org \
--cc=song@kernel.org \
--cc=tj@kernel.org \
--cc=virtualization@lists.linux.dev \
--cc=vishal.l.verma@intel.com \
--cc=willy@infradead.org \
--cc=xiao@kernel.org \
--cc=youngjun.park@lge.com \
--cc=yukuai@fygo.io \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.