All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] block/rq_qos: Use atomic_try_cmpxchg in atomic_inc_below
@ 2022-07-12 15:05 Uros Bizjak
  2022-07-12 20:39 ` Jens Axboe
  0 siblings, 1 reply; 2+ messages in thread
From: Uros Bizjak @ 2022-07-12 15:05 UTC (permalink / raw)
  To: linux-block, linux-kernel; +Cc: Uros Bizjak, Jens Axboe

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 <ubizjak@gmail.com>
Cc: Jens Axboe <axboe@kernel.dk>
---
v2: Split patch from the original big patch
---
 block/blk-rq-qos.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)

diff --git a/block/blk-rq-qos.c b/block/blk-rq-qos.c
index d3a75693adbf..88f0fe7dcf54 100644
--- a/block/blk-rq-qos.c
+++ b/block/blk-rq-qos.c
@@ -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;
 }
-- 
2.35.3


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] block/rq_qos: Use atomic_try_cmpxchg in atomic_inc_below
  2022-07-12 15:05 [PATCH v2] block/rq_qos: Use atomic_try_cmpxchg in atomic_inc_below Uros Bizjak
@ 2022-07-12 20:39 ` Jens Axboe
  0 siblings, 0 replies; 2+ messages in thread
From: Jens Axboe @ 2022-07-12 20:39 UTC (permalink / raw)
  To: ubizjak, linux-kernel, linux-block

On Tue, 12 Jul 2022 17:05:47 +0200, Uros Bizjak wrote:
> 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.
> 
> [...]

Applied, thanks!

[1/1] block/rq_qos: Use atomic_try_cmpxchg in atomic_inc_below
      commit: f4b1e27db49c8b985b116aa99481b4c6a4342ed4

Best regards,
-- 
Jens Axboe



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-07-12 20:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-12 15:05 [PATCH v2] block/rq_qos: Use atomic_try_cmpxchg in atomic_inc_below Uros Bizjak
2022-07-12 20:39 ` Jens Axboe

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.