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 --- 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