From: Yu Kuai <yukuai@kernel.org>
To: Jens Axboe <axboe@kernel.dk>, Tejun Heo <tj@kernel.org>,
Josef Bacik <josef@toxicpanda.com>,
Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
Clark Williams <clrkwllms@kernel.org>,
Steven Rostedt <rostedt@goodmis.org>
Cc: Yu Kuai <yukuai@fygo.io>, Christoph Hellwig <hch@lst.de>,
Nilay Shroff <nilay@linux.ibm.com>, Tao Cui <cui.tao@linux.dev>,
Hannes Reinecke <hare@suse.de>,
linux-block@vger.kernel.org, cgroups@vger.kernel.org,
linux-rt-devel@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: [RFC PATCH v3 5/6] blk-cgroup: share blkg creation between lookup and config prep
Date: Sun, 23 Aug 2026 23:29:24 +0800 [thread overview]
Message-ID: <20260823152926.1043863-6-yukuai@kernel.org> (raw)
In-Reply-To: <20260823152926.1043863-1-yukuai@kernel.org>
From: Yu Kuai <yukuai@fygo.io>
blkg_conf_prep() open-codes the same parent walk and blkg creation that
blkg_lookup_create() now performs. Give blkg_lookup_create() an out
parameter for the created/found blkg and have it report whether the target
blkg was created or found (returning the closest existing blkg in the out
parameter on failure), then have blkg_conf_prep() use the helper and treat
errors as config failures.
This keeps the bio association path's closest-blkg fallback and removes the
duplicate config path loop.
Signed-off-by: Yu Kuai <yukuai@fygo.io>
---
block/blk-cgroup.c | 77 +++++++++++++---------------------------------
1 file changed, 21 insertions(+), 56 deletions(-)
diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c
index 33fba781017b..31afb433ab18 100644
--- a/block/blk-cgroup.c
+++ b/block/blk-cgroup.c
@@ -473,34 +473,36 @@ static struct blkcg_gq *blkg_lookup_tryget(struct blkcg_gq *blkg)
/**
* blkg_lookup_create - lookup blkg, try to create one if not there
* @blkcg: blkcg of interest
* @disk: gendisk of interest
+ * @gfp_mask: allocation mask to use
+ * @blkgp: out parameter for the target blkg, or closest blkg on failure
*
* Lookup blkg for the @blkcg - @disk pair. If it doesn't exist, try to
* create one. blkg creation is performed recursively from blkcg_root such
* that all non-root blkg's have access to the parent blkg. This function
* must be called with @disk->queue->blkcg_mutex held.
*
- * Returns the closest blkg with an extra reference acquired. If
- * blkg_create() fails while walking down from root, the returned blkg may
- * belong to an ancestor of @blkcg. This function never returns %NULL.
+ * On success, *@blkgp points to the target blkg and 0 is returned. On
+ * failure, *@blkgp points to the closest blkg and the errno is returned.
+ * The returned blkg does not have an extra reference acquired.
*/
-static struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
- struct gendisk *disk)
+static int blkg_lookup_create(struct blkcg *blkcg, struct gendisk *disk,
+ gfp_t gfp_mask, struct blkcg_gq **blkgp)
{
struct request_queue *q = disk->queue;
struct blkcg_gq *blkg;
lockdep_assert_held(&q->blkcg_mutex);
rcu_read_lock();
blkg = blkg_lookup(blkcg, q);
if (blkg) {
- blkg = blkg_lookup_tryget(blkg);
+ *blkgp = blkg;
rcu_read_unlock();
- return blkg;
+ return 0;
}
rcu_read_unlock();
/*
* Create blkgs walking down from blkcg_root to @blkcg, so that all
@@ -523,20 +525,20 @@ static struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
pos = parent;
parent = blkcg_parent(parent);
}
rcu_read_unlock();
- blkg = blkg_create(pos, disk, GFP_NOIO);
+ blkg = blkg_create(pos, disk, gfp_mask);
if (IS_ERR(blkg)) {
- blkg = ret_blkg;
- break;
+ *blkgp = ret_blkg;
+ return PTR_ERR(blkg);
+ }
+ if (pos == blkcg) {
+ *blkgp = blkg;
+ return 0;
}
- if (pos == blkcg)
- break;
}
-
- return blkg_lookup_tryget(blkg);
}
static void blkg_destroy(struct blkcg_gq *blkg)
{
struct blkcg *blkcg = blkg->blkcg;
@@ -844,52 +846,14 @@ int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
if (!blkcg_policy_enabled(q, pol)) {
ret = -EOPNOTSUPP;
goto fail_unlock;
}
- rcu_read_lock();
- blkg = blkg_lookup(blkcg, q);
- rcu_read_unlock();
- if (blkg)
- goto success;
-
- /*
- * Create blkgs walking down from blkcg_root to @blkcg, so that all
- * non-root blkgs have access to their parents.
- */
- while (true) {
- struct blkcg *pos = blkcg;
- struct blkcg *parent;
-
- parent = blkcg_parent(blkcg);
- rcu_read_lock();
- while (parent && !blkg_lookup(parent, q)) {
- pos = parent;
- parent = blkcg_parent(parent);
- }
- rcu_read_unlock();
-
- if (!blkcg_policy_enabled(q, pol)) {
- ret = -EOPNOTSUPP;
- goto fail_unlock;
- }
-
- rcu_read_lock();
- blkg = blkg_lookup(pos, q);
- rcu_read_unlock();
- if (!blkg) {
- blkg = blkg_create(pos, disk, GFP_NOIO);
- if (IS_ERR(blkg)) {
- ret = PTR_ERR(blkg);
- goto fail_unlock;
- }
- }
+ ret = blkg_lookup_create(blkcg, disk, GFP_NOIO, &blkg);
+ if (ret)
+ goto fail_unlock;
- if (pos == blkcg)
- goto success;
- }
-success:
ctx->blkg = blkg;
return 0;
fail_unlock:
mutex_unlock(&q->blkcg_mutex);
@@ -2124,11 +2088,12 @@ struct blkcg_gq *bio_blkg(struct bio *bio)
bio_set_blkg_ref(bio, blkg);
return blkg;
}
mutex_lock(&q->blkcg_mutex);
- blkg = blkg_lookup_create(blkcg, disk);
+ blkg_lookup_create(blkcg, disk, GFP_NOIO, &blkg);
+ blkg = blkg_lookup_tryget(blkg);
mutex_unlock(&q->blkcg_mutex);
bio_set_blkg_ref(bio, blkg);
return blkg;
}
--
2.51.0
next prev parent reply other threads:[~2026-08-23 15:30 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-23 15:29 [RFC PATCH v3 0/6] blk-cgroup: protect blkgs with blkcg_mutex Yu Kuai
2026-08-23 15:29 ` [RFC PATCH v3 1/6] blk-cgroup: call pd_free_fn() outside spinlocks Yu Kuai
2026-08-23 15:29 ` [RFC PATCH v3 2/6] blk-throttle: protect throttle state with td lock Yu Kuai
2026-08-23 15:29 ` [RFC PATCH v3 3/6] blk-cgroup: protect blkgs with blkcg_mutex Yu Kuai
2026-08-23 15:29 ` [RFC PATCH v3 4/6] blk-cgroup: allocate blkgs in blkg_create Yu Kuai
2026-08-25 1:35 ` Tao Cui
2026-08-25 2:33 ` yu kuai
2026-08-23 15:29 ` Yu Kuai [this message]
2026-08-23 15:29 ` [RFC PATCH v3 6/6] blk-cgroup: make policy blkg creation nowait-safe Yu Kuai
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=20260823152926.1043863-6-yukuai@kernel.org \
--to=yukuai@kernel.org \
--cc=axboe@kernel.dk \
--cc=bigeasy@linutronix.de \
--cc=cgroups@vger.kernel.org \
--cc=clrkwllms@kernel.org \
--cc=cui.tao@linux.dev \
--cc=hare@suse.de \
--cc=hch@lst.de \
--cc=josef@toxicpanda.com \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-rt-devel@lists.linux.dev \
--cc=nilay@linux.ibm.com \
--cc=rostedt@goodmis.org \
--cc=tj@kernel.org \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox