Skip to contentSkip to conversation

block/rq_qos: Use atomic_try_cmpxchg in atomic_inc_below

PATCH Applied Uros Bizjak 2 participants 2 messages Jul 12, 2022 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 block/rq_qos: Use atomic_try_cmpxchg in atomic_inc_below

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.

---
v2: Split patch from the original big patch

---
 block/blk-rq-qos.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
block/blk-rq-qos.c +2 -8
index d3a75693adbf..88f0fe7dcf54 100644
@@ -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;
 }
Signature · 1 line
2.35.3
maintainer Applied
#1 lore raw
Quoted 9 lines · Uros Bizjak

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,

Signature · 1 line
Jens Axboe