mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 04/19] blk-cgroup: don't nest queue_lock under rcu in blkg_lookup_create()
Date: Fri, 10 Oct 2025 17:14:29 +0800	[thread overview]
Message-ID: <20251010091446.3048529-5-yukuai@kernel.org> (raw)
In-Reply-To: <20251010091446.3048529-1-yukuai@kernel.org>

From: Yu Kuai <yukuai3@huawei.com>

Changes to two step:

1) hold rcu lock and do blkg_lookup() from fast path;
2) hold queue_lock directly from slow path, and don't nest it under rcu
   lock;

Prepare to convert protecting blkcg with blkcg_mutex instead of
queue_lock;

Signed-off-by: Yu Kuai <yukuai3@huawei.com>
---
 block/blk-cgroup.c | 57 +++++++++++++++++++++++++++++-----------------
 1 file changed, 36 insertions(+), 21 deletions(-)

diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c
index fb40262971c9..3363d2476fed 100644
--- a/block/blk-cgroup.c
+++ b/block/blk-cgroup.c
@@ -467,22 +467,17 @@ static struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
 {
 	struct request_queue *q = disk->queue;
 	struct blkcg_gq *blkg;
-	unsigned long flags;
-
-	WARN_ON_ONCE(!rcu_read_lock_held());
 
-	blkg = blkg_lookup(blkcg, q);
-	if (blkg)
-		return blkg;
-
-	spin_lock_irqsave(&q->queue_lock, flags);
+	rcu_read_lock();
 	blkg = blkg_lookup(blkcg, q);
 	if (blkg) {
 		if (blkcg != &blkcg_root &&
 		    blkg != rcu_dereference(blkcg->blkg_hint))
 			rcu_assign_pointer(blkcg->blkg_hint, blkg);
-		goto found;
+		rcu_read_unlock();
+		return blkg;
 	}
+	rcu_read_unlock();
 
 	/*
 	 * Create blkgs walking down from blkcg_root to @blkcg, so that all
@@ -514,8 +509,6 @@ static struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
 			break;
 	}
 
-found:
-	spin_unlock_irqrestore(&q->queue_lock, flags);
 	return blkg;
 }
 
@@ -2078,6 +2071,18 @@ void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta)
 	atomic64_add(delta, &blkg->delay_nsec);
 }
 
+static inline struct blkcg_gq *blkg_lookup_tryget(struct blkcg_gq *blkg)
+{
+retry:
+	if (blkg_tryget(blkg))
+		return blkg;
+
+	blkg = blkg->parent;
+	if (blkg)
+		goto retry;
+
+	return NULL;
+}
 /**
  * blkg_tryget_closest - try and get a blkg ref on the closet blkg
  * @bio: target bio
@@ -2090,20 +2095,30 @@ void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta)
 static inline struct blkcg_gq *blkg_tryget_closest(struct bio *bio,
 		struct cgroup_subsys_state *css)
 {
-	struct blkcg_gq *blkg, *ret_blkg = NULL;
+	struct request_queue *q = bio->bi_bdev->bd_queue;
+	struct blkcg *blkcg = css_to_blkcg(css);
+	struct blkcg_gq *blkg;
 
 	rcu_read_lock();
-	blkg = blkg_lookup_create(css_to_blkcg(css), bio->bi_bdev->bd_disk);
-	while (blkg) {
-		if (blkg_tryget(blkg)) {
-			ret_blkg = blkg;
-			break;
-		}
-		blkg = blkg->parent;
-	}
+	blkg = blkg_lookup(blkcg, q);
+	if (likely(blkg))
+		blkg = blkg_lookup_tryget(blkg);
 	rcu_read_unlock();
 
-	return ret_blkg;
+	if (blkg)
+		return blkg;
+
+	/*
+	 * Fast path failed, we're probably issuing IO in this cgroup the first
+	 * time, hold lock to create new blkg.
+	 */
+	spin_lock_irq(&q->queue_lock);
+	blkg = blkg_lookup_create(blkcg, bio->bi_bdev->bd_disk);
+	if (blkg)
+		blkg = blkg_lookup_tryget(blkg);
+	spin_unlock_irq(&q->queue_lock);
+
+	return blkg;
 }
 
 /**
-- 
2.51.0


  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 ` Yu Kuai [this message]
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 ` [PATCH v2 11/19] blk-cgroup: remove preallocate blkg for blkg_create() Yu Kuai
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-5-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®