Linux block layer
 help / color / mirror / Atom feed
From: Gui-Dong Han <hanguidong02@gmail.com>
To: Tejun Heo <tj@kernel.org>, Josef Bacik <josef@toxicpanda.com>,
	Jens Axboe <axboe@kernel.dk>
Cc: cgroups@vger.kernel.org, linux-block@vger.kernel.org,
	Jia-Ju Bai <baijiaju1990@gmail.com>,
	Gui-Dong Han <hanguidong02@gmail.com>
Subject: [PATCH] blk-cgroup: publish delay before enabling throttling
Date: Mon, 30 Nov 2026 21:34:03 +0800	[thread overview]
Message-ID: <20261130133403.3789095-1-hanguidong02@gmail.com> (raw)

blkcg_set_delay() sets blkg->use_delay to -1 before storing
blkg->delay_nsec, so another CPU can see throttling enabled with a zero
or stale delay:

  // Initially: use_delay == 0, delay_nsec == 0

  CPU 0                               CPU 1
  -----                               -----
  // blkcg_set_delay()                // blkcg_maybe_throttle_blkg()
  atomic_try_cmpxchg(&blkg->use_delay,
                     &old, -1);
                                      use_delay =
                                        atomic_read(&blkg->use_delay);
                                        // sees -1
                                      this_delay =
                                        atomic64_read(&blkg->delay_nsec);
                                        // sees 0, skips throttling
  atomic64_set(&blkg->delay_nsec,
               delay);

Store delay_nsec before enabling use_delay, and pair
atomic_try_cmpxchg() with atomic_read_acquire(&blkg->use_delay) in
blkcg_maybe_throttle_blkg() so the delay_nsec load is ordered after the
use_delay check.

Fixes: 54c52e10dc9b ("blk-iocost: switch to fixed non-auto-decaying use_delay")
Signed-off-by: Gui-Dong Han <hanguidong02@gmail.com>
---
Found by auditing atomic operations used for synchronization.
A similar fix can be found in 6df8e84aa6b5.
---
 block/blk-cgroup.c | 3 ++-
 block/blk-cgroup.h | 4 ++--
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c
index 5b7a32293dc6..cf0a85a821d7 100644
--- a/block/blk-cgroup.c
+++ b/block/blk-cgroup.c
@@ -1944,7 +1944,8 @@ static void blkcg_maybe_throttle_blkg(struct blkcg_gq *blkg, bool use_memdelay)
 	int tok;
 
 	while (blkg->parent) {
-		int use_delay = atomic_read(&blkg->use_delay);
+		/* Pairs with atomic_try_cmpxchg() in blkcg_set_delay(). */
+		int use_delay = atomic_read_acquire(&blkg->use_delay);
 
 		if (use_delay) {
 			u64 this_delay;
diff --git a/block/blk-cgroup.h b/block/blk-cgroup.h
index 1fd60cb4157c..a0b092cdd2fd 100644
--- a/block/blk-cgroup.h
+++ b/block/blk-cgroup.h
@@ -437,11 +437,11 @@ static inline void blkcg_set_delay(struct blkcg_gq *blkg, u64 delay)
 {
 	int old = atomic_read(&blkg->use_delay);
 
+	atomic64_set(&blkg->delay_nsec, delay);
+
 	/* We only want 1 person setting the congestion count for this blkg. */
 	if (!old && atomic_try_cmpxchg(&blkg->use_delay, &old, -1))
 		blkcg_inc_congestion_count(blkg->blkcg);
-
-	atomic64_set(&blkg->delay_nsec, delay);
 }
 
 /**

base-commit: 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc
-- 
2.34.1



                 reply	other threads:[~2026-11-30 13:34 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261130133403.3789095-1-hanguidong02@gmail.com \
    --to=hanguidong02@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=baijiaju1990@gmail.com \
    --cc=cgroups@vger.kernel.org \
    --cc=josef@toxicpanda.com \
    --cc=linux-block@vger.kernel.org \
    --cc=tj@kernel.org \
    /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