* [PATCH] blk-cgroup: publish delay before enabling throttling
@ 2026-11-30 13:34 Gui-Dong Han
2026-11-30 17:00 ` Tejun Heo
0 siblings, 1 reply; 2+ messages 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] 2+ messages in thread* Re: [PATCH] blk-cgroup: publish delay before enabling throttling
2026-11-30 13:34 [PATCH] blk-cgroup: publish delay before enabling throttling Gui-Dong Han
@ 2026-11-30 17:00 ` Tejun Heo
0 siblings, 0 replies; 2+ messages in thread
From: Tejun Heo @ 2026-11-30 17:00 UTC (permalink / raw)
To: Gui-Dong Han; +Cc: Josef Bacik, Jens Axboe, cgroups, linux-block, Jia-Ju Bai
Hello, Gui-Dong.
On Mon, Nov 30, 2026 at 09:34:03PM +0800, Gui-Dong Han wrote:
> 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:
...
> 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.
The race window is tiny and the effect is transient. AFAICS, the worst that
can happen is a task skipping one throttle or sleeping once on the previous
delay, and the delay mechanism is approximate to begin with. I don't think
this matters in practice and wouldn't call the current code broken. Maybe
this just needs a comment noting that the reader can see use_delay and
delay_nsec out of sync and that it's harmless?
Thanks.
--
tejun
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-30 17:00 UTC | newest]
Thread overview: 2+ messages (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
2026-11-30 17:00 ` Tejun Heo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox