Linux block layer
 help / color / mirror / Atom feed
* [PATCH] blk-cgroup: publish delay before enabling throttling
@ 2026-11-30 13:34 Gui-Dong Han
  0 siblings, 0 replies; only message in thread
From: Gui-Dong Han @ 2026-11-30 13:34 UTC (permalink / raw)
  To: Tejun Heo, Josef Bacik, Jens Axboe
  Cc: cgroups, linux-block, Jia-Ju Bai, Gui-Dong Han

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



^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-30 13:34 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 13:34 [PATCH] blk-cgroup: publish delay before enabling throttling Gui-Dong Han

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox