blk-cgroup: publish delay before enabling throttling
Subsystems (3)
PATCH 1/1 blk-cgroup: publish delay before enabling throttling
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.
---
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(-)
block/blk-cgroup.c +2 -1
@@ -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;
block/blk-cgroup.h +2 -2
@@ -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
Signature · 1 line
2.34.1
Tejun Heo
maintainerHello, Gui-Dong.
Quoted 3 lines · Gui-Dong Han
blkcg_set_delay()setsblkg->use_delayto -1 before storingblkg->delay_nsec, so another CPU can see throttling enabled with a zero or stale delay:
...
Quoted 4 lines
Store
delay_nsecbefore enablinguse_delay, and pairatomic_try_cmpxchg()withatomic_read_acquire(&blkg->use_delay)inblkcg_maybe_throttle_blkg()so thedelay_nsecload is ordered after theuse_delaycheck.
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