Skip to content

Commit 96388f5

Browse files
ubizjakaxboe
authored andcommitted
blk-cgroup: Use atomic{,64}_try_cmpxchg
Use atomic_try_cmpxchg instead of atomic_cmpxchg (*ptr, old, new) == old in blkcg_unuse_delay, blkcg_set_delay and blkcg_clear_delay and atomic64_try_cmpxchg in blkcg_scale_delay. x86 CMPXCHG instruction returns success in ZF flag, so this change saves a compare after cmpxchg (and related move instruction in front of cmpxchg). Also, atomic_try_cmpxchg implicitly assigns old *ptr value to "old" when cmpxchg fails, enabling further code simplifications. No functional change intended. Signed-off-by: Uros Bizjak <ubizjak@gmail.com> Cc: Jens Axboe <axboe@kernel.dk> Link: https://lore.kernel.org/r/20220712154455.66868-1-ubizjak@gmail.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent aee8960 commit 96388f5

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

block/blk-cgroup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1700,7 +1700,7 @@ static void blkcg_scale_delay(struct blkcg_gq *blkg, u64 now)
17001700
* everybody is happy with their IO latencies.
17011701
*/
17021702
if (time_before64(old + NSEC_PER_SEC, now) &&
1703-
atomic64_cmpxchg(&blkg->delay_start, old, now) == old) {
1703+
atomic64_try_cmpxchg(&blkg->delay_start, &old, now)) {
17041704
u64 cur = atomic64_read(&blkg->delay_nsec);
17051705
u64 sub = min_t(u64, blkg->last_delay, now - old);
17061706
int cur_use = atomic_read(&blkg->use_delay);

block/blk-cgroup.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -430,12 +430,8 @@ static inline int blkcg_unuse_delay(struct blkcg_gq *blkg)
430430
* then check to see if we were the last delay so we can drop the
431431
* congestion count on the cgroup.
432432
*/
433-
while (old) {
434-
int cur = atomic_cmpxchg(&blkg->use_delay, old, old - 1);
435-
if (cur == old)
436-
break;
437-
old = cur;
438-
}
433+
while (old && !atomic_try_cmpxchg(&blkg->use_delay, &old, old - 1))
434+
;
439435

440436
if (old == 0)
441437
return 0;
@@ -458,7 +454,7 @@ static inline void blkcg_set_delay(struct blkcg_gq *blkg, u64 delay)
458454
int old = atomic_read(&blkg->use_delay);
459455

460456
/* We only want 1 person setting the congestion count for this blkg. */
461-
if (!old && atomic_cmpxchg(&blkg->use_delay, old, -1) == old)
457+
if (!old && atomic_try_cmpxchg(&blkg->use_delay, &old, -1))
462458
atomic_inc(&blkg->blkcg->css.cgroup->congestion_count);
463459

464460
atomic64_set(&blkg->delay_nsec, delay);
@@ -475,7 +471,7 @@ static inline void blkcg_clear_delay(struct blkcg_gq *blkg)
475471
int old = atomic_read(&blkg->use_delay);
476472

477473
/* We only want 1 person clearing the congestion count for this blkg. */
478-
if (old && atomic_cmpxchg(&blkg->use_delay, old, 0) == old)
474+
if (old && atomic_try_cmpxchg(&blkg->use_delay, &old, 0))
479475
atomic_dec(&blkg->blkcg->css.cgroup->congestion_count);
480476
}
481477

0 commit comments

Comments
 (0)