From: Yu Kuai <yukuai@kernel.org>
To: axboe@kernel.dk, tj@kernel.org, linux-block@vger.kernel.org,
cgroups@vger.kernel.org, nilay@linux.ibm.com, bvanassche@acm.org,
ming.lei@redhat.com, hch@lst.de
Cc: linux-kernel@vger.kernel.org, Yu Kuai <yukuai3@huawei.com>
Subject: [PATCH v2 11/19] blk-cgroup: remove preallocate blkg for blkg_create()
Date: Fri, 10 Oct 2025 17:14:36 +0800 [thread overview]
Message-ID: <20251010091446.3048529-12-yukuai@kernel.org> (raw)
In-Reply-To: <20251010091446.3048529-1-yukuai@kernel.org>
From: Yu Kuai <yukuai3@huawei.com>
Now that blkg_create is protected with blkcg_mutex, there is no need to
preallocate blkg, remove related code.
Signed-off-by: Yu Kuai <yukuai3@huawei.com>
---
block/blk-cgroup.c | 91 +++++++++++++++++-----------------------------
1 file changed, 33 insertions(+), 58 deletions(-)
diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c
index 030499d70543..3c23d2d1e237 100644
--- a/block/blk-cgroup.c
+++ b/block/blk-cgroup.c
@@ -364,10 +364,9 @@ static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct gendisk *disk,
* If @new_blkg is %NULL, this function tries to allocate a new one as
* necessary using %GFP_NOWAIT. @new_blkg is always consumed on return.
*/
-static struct blkcg_gq *blkg_create(struct blkcg *blkcg, struct gendisk *disk,
- struct blkcg_gq *new_blkg)
+static struct blkcg_gq *blkg_create(struct blkcg *blkcg, struct gendisk *disk)
{
- struct blkcg_gq *blkg;
+ struct blkcg_gq *blkg = NULL;
int i, ret;
lockdep_assert_held(&disk->queue->blkcg_mutex);
@@ -384,15 +383,11 @@ static struct blkcg_gq *blkg_create(struct blkcg *blkcg, struct gendisk *disk,
goto err_free_blkg;
}
- /* allocate */
- if (!new_blkg) {
- new_blkg = blkg_alloc(blkcg, disk, GFP_NOWAIT);
- if (unlikely(!new_blkg)) {
- ret = -ENOMEM;
- goto err_put_css;
- }
+ blkg = blkg_alloc(blkcg, disk, GFP_NOIO);
+ if (unlikely(!blkg)) {
+ ret = -ENOMEM;
+ goto err_put_css;
}
- blkg = new_blkg;
/* link parent */
if (blkcg_parent(blkcg)) {
@@ -415,35 +410,34 @@ static struct blkcg_gq *blkg_create(struct blkcg *blkcg, struct gendisk *disk,
/* insert */
spin_lock(&blkcg->lock);
ret = radix_tree_insert(&blkcg->blkg_tree, disk->queue->id, blkg);
- if (likely(!ret)) {
- hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
- list_add(&blkg->q_node, &disk->queue->blkg_list);
+ if (unlikely(ret)) {
+ spin_unlock(&blkcg->lock);
+ blkg_put(blkg);
+ return ERR_PTR(ret);
+ }
- for (i = 0; i < BLKCG_MAX_POLS; i++) {
- struct blkcg_policy *pol = blkcg_policy[i];
+ hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
+ list_add(&blkg->q_node, &disk->queue->blkg_list);
- if (blkg->pd[i]) {
- if (pol->pd_online_fn)
- pol->pd_online_fn(blkg->pd[i]);
- blkg->pd[i]->online = true;
- }
+ for (i = 0; i < BLKCG_MAX_POLS; i++) {
+ struct blkcg_policy *pol = blkcg_policy[i];
+
+ if (blkg->pd[i]) {
+ if (pol->pd_online_fn)
+ pol->pd_online_fn(blkg->pd[i]);
+ blkg->pd[i]->online = true;
}
}
+
blkg->online = true;
spin_unlock(&blkcg->lock);
-
- if (!ret)
- return blkg;
-
- /* @blkg failed fully initialized, use the usual release path */
- blkg_put(blkg);
- return ERR_PTR(ret);
+ return blkg;
err_put_css:
css_put(&blkcg->css);
err_free_blkg:
- if (new_blkg)
- blkg_free(new_blkg);
+ if (blkg)
+ blkg_free(blkg);
return ERR_PTR(ret);
}
@@ -498,7 +492,7 @@ static struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
parent = blkcg_parent(parent);
}
- blkg = blkg_create(pos, disk, NULL);
+ blkg = blkg_create(pos, disk);
if (IS_ERR(blkg)) {
blkg = ret_blkg;
break;
@@ -880,7 +874,6 @@ int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
while (true) {
struct blkcg *pos = blkcg;
struct blkcg *parent;
- struct blkcg_gq *new_blkg;
parent = blkcg_parent(blkcg);
while (parent && !blkg_lookup(parent, q)) {
@@ -888,23 +881,14 @@ int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
parent = blkcg_parent(parent);
}
- new_blkg = blkg_alloc(pos, disk, GFP_NOIO);
- if (unlikely(!new_blkg)) {
- ret = -ENOMEM;
- goto fail_unlock;
- }
-
if (!blkcg_policy_enabled(q, pol)) {
- blkg_free(new_blkg);
ret = -EOPNOTSUPP;
goto fail_unlock;
}
blkg = blkg_lookup(pos, q);
- if (blkg) {
- blkg_free(new_blkg);
- } else {
- blkg = blkg_create(pos, disk, new_blkg);
+ if (!blkg) {
+ blkg = blkg_create(pos, disk);
if (IS_ERR(blkg)) {
ret = PTR_ERR(blkg);
goto fail_unlock;
@@ -1469,27 +1453,18 @@ void blkg_init_queue(struct request_queue *q)
int blkcg_init_disk(struct gendisk *disk)
{
struct request_queue *q = disk->queue;
- struct blkcg_gq *new_blkg, *blkg;
-
- new_blkg = blkg_alloc(&blkcg_root, disk, GFP_KERNEL);
- if (!new_blkg)
- return -ENOMEM;
+ struct blkcg_gq *blkg;
+ /* Make sure the root blkg exists. */
mutex_lock(&q->blkcg_mutex);
+ blkg = blkg_create(&blkcg_root, disk);
+ mutex_unlock(&q->blkcg_mutex);
- /* Make sure the root blkg exists. */
- blkg = blkg_create(&blkcg_root, disk, new_blkg);
if (IS_ERR(blkg))
- goto err_unlock;
- q->root_blkg = blkg;
-
- mutex_unlock(&q->blkcg_mutex);
+ return PTR_ERR(blkg);
+ q->root_blkg = blkg;
return 0;
-
-err_unlock:
- mutex_unlock(&q->blkcg_mutex);
- return PTR_ERR(blkg);
}
void blkcg_exit_disk(struct gendisk *disk)
--
2.51.0
next prev parent reply other threads:[~2025-10-10 9:15 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-10 9:14 [PATCH v2 00/19] blk-cgroup: convert to use blkcg_mutex for protection Yu Kuai
2025-10-10 9:14 ` [PATCH v2 01/19] blk-cgroup: protect iterating blkgs with blkcg->lock in blkcg_print_stat() Yu Kuai
2025-10-10 9:14 ` [PATCH v2 02/19] blk-cgroup: delay freeing policy data after rcu grace period Yu Kuai
2025-10-10 9:14 ` [PATCH v2 03/19] blk-cgroup: don't nest queue_lock under rcu in blkcg_print_blkgs() Yu Kuai
2025-10-10 9:14 ` [PATCH v2 04/19] blk-cgroup: don't nest queue_lock under rcu in blkg_lookup_create() Yu Kuai
2025-10-10 9:14 ` [PATCH v2 05/19] blk-cgroup: don't nest queu_lock under rcu in bio_associate_blkg() Yu Kuai
2025-10-10 9:14 ` [PATCH v2 06/19] blk-cgroup: don't nest queue_lock under blkcg->lock in blkcg_destroy_blkgs() Yu Kuai
2025-10-10 9:14 ` [PATCH v2 07/19] mm/page_io: don't nest queue_lock under rcu in bio_associate_blkg_from_page() Yu Kuai
2025-10-10 9:14 ` [PATCH v2 08/19] block, bfq: don't grab queue_lock to initialize bfq Yu Kuai
2025-10-10 9:14 ` [PATCH v2 09/19] blk-cgroup: convert to protect blkgs with blkcg_mutex Yu Kuai
2025-10-10 9:14 ` [PATCH v2 10/19] blk-cgroup: remove radix_tree_preload() Yu Kuai
2025-10-10 9:14 ` Yu Kuai [this message]
2025-10-10 9:14 ` [PATCH v2 12/19] blk-throttle: fix possible deadlock due to queue_lock in timer Yu Kuai
2025-10-10 9:14 ` [PATCH v2 13/19] blk-cgroup: add new blkg configuration helpers Yu Kuai
2025-10-10 9:14 ` [PATCH v2 14/19] blk-cgroup: factor out a helper __blkg_activate_policy() Yu Kuai
2025-10-10 9:14 ` [PATCH v2 15/19] blk-throttle: convert to use blkg_conf_{start, end} Yu Kuai
2025-10-10 9:14 ` [PATCH v2 16/19] block, bfq: " Yu Kuai
2025-10-10 9:14 ` [PATCH v2 17/19] blk-iolatency: " Yu Kuai
2025-10-10 9:14 ` [PATCH v2 18/19] blk-iocost: " Yu Kuai
2025-10-10 9:14 ` [PATCH v2 19/19] blk-cgroup: remove unsed blkg configuration helpers 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=20251010091446.3048529-12-yukuai@kernel.org \
--to=yukuai@kernel.org \
--cc=axboe@kernel.dk \
--cc=bvanassche@acm.org \
--cc=cgroups@vger.kernel.org \
--cc=hch@lst.de \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ming.lei@redhat.com \
--cc=nilay@linux.ibm.com \
--cc=tj@kernel.org \
--cc=yukuai3@huawei.com \
/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
all inboxes | Powered by JetHome®