Skip to content

Commit

Permalink
block/rq_qos: Use atomic_try_cmpxchg in atomic_inc_below
Browse files Browse the repository at this point in the history
Use atomic_try_cmpxchg instead of atomic_cmpxchg (*ptr, old, new) == old in
atomic_inc_below. 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 <[email protected]>
Cc: Jens Axboe <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Jens Axboe <[email protected]>
  • Loading branch information
ubizjak authored and axboe committed Jul 12, 2022
1 parent f3ec5d1 commit f4b1e27
Showing 1 changed file with 2 additions and 8 deletions.
10 changes: 2 additions & 8 deletions block/blk-rq-qos.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,10 @@ static bool atomic_inc_below(atomic_t *v, unsigned int below)
{
unsigned int cur = atomic_read(v);

for (;;) {
unsigned int old;

do {
if (cur >= below)
return false;
old = atomic_cmpxchg(v, cur, cur + 1);
if (old == cur)
break;
cur = old;
}
} while (!atomic_try_cmpxchg(v, &cur, cur + 1));

return true;
}
Expand Down

0 comments on commit f4b1e27

Please sign in to comment.