Skip to contentSkip to conversation

xfs: Use try_cmpxchg() in xlog_cil_insert_pcp_aggregate()

PATCH Applied Uros Bizjak 3 participants 3 messages Sep 23, 2024 – Sep 27, 2024 XFS filesystem

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 xfs: Use try_cmpxchg() in xlog_cil_insert_pcp_aggregate()

Use !try_cmpxchg instead of cmpxchg (*ptr, old, new) != old in xlog_cil_insert_pcp_aggregate(). x86 CMPXCHG instruction returns success in ZF flag, so this change saves a compare after cmpxchg.

Also, try_cmpxchg implicitly assigns old *ptr value to "old" when cmpxchg fails. There is no need to re-read the value in the loop.

Note that the value from *ptr should be read using READ_ONCE to prevent the compiler from merging, refetching or reordering the read.

No functional change intended.

---
v2: Move cilpcp variable into the loop scope. Initialize cilcpc and
old variables at the declaration time. Use alternative form of the while loop.
v3: Undo reformatting of variable declarations

---
 fs/xfs/xfs_log_cil.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
fs/xfs/xfs_log_cil.c +4 -7
index 391a938d690c..80da0cf87d7a 100644
@@ -156,7 +156,6 @@ xlog_cil_insert_pcp_aggregate(
 	struct xfs_cil		*cil,
 	struct xfs_cil_ctx	*ctx)
 {
-	struct xlog_cil_pcp	*cilpcp;
 	int			cpu;
 	int			count = 0;
 
@@ -171,13 +170,11 @@ xlog_cil_insert_pcp_aggregate(
 	 * structures that could have a nonzero space_used.
 	 */
 	for_each_cpu(cpu, &ctx->cil_pcpmask) {
-		int	old, prev;
+		struct xlog_cil_pcp	*cilpcp = per_cpu_ptr(cil->xc_pcp, cpu);
+		int			old = READ_ONCE(cilpcp->space_used);
 
-		cilpcp = per_cpu_ptr(cil->xc_pcp, cpu);
-		do {
-			old = cilpcp->space_used;
-			prev = cmpxchg(&cilpcp->space_used, old, 0);
-		} while (old != prev);
+		while (!try_cmpxchg(&cilpcp->space_used, &old, 0))
+			;
 		count += old;
 	}
 	atomic_add(count, &ctx->space_used);
Signature · 1 line
2.46.1
Reviewed-by on v3 1/1
#1 lore raw
Quoted 10 lines · Uros Bizjak

Use !try_cmpxchg instead of cmpxchg (*ptr, old, new) != old in xlog_cil_insert_pcp_aggregate(). x86 CMPXCHG instruction returns success in ZF flag, so this change saves a compare after cmpxchg.

Also, try_cmpxchg implicitly assigns old *ptr value to "old" when cmpxchg fails. There is no need to re-read the value in the loop.

Note that the value from *ptr should be read using READ_ONCE to prevent the compiler from merging, refetching or reordering the read.

Quoted 6 lines

No functional change intended.

Looks fine.

Signature · 2 lines
Dave Chinner
[email protected]
maintainer Applied
#2 lore raw
In reply to Uros Bizjak
Quoted 8 lines · Uros Bizjak

Use !try_cmpxchg instead of cmpxchg (*ptr, old, new) != old in xlog_cil_insert_pcp_aggregate(). x86 CMPXCHG instruction returns success in ZF flag, so this change saves a compare after cmpxchg.

Also, try_cmpxchg implicitly assigns old *ptr value to "old" when cmpxchg fails. There is no need to re-read the value in the loop.

[...]

Applied to xfs-6.12-rc2, thanks!

[1/1] xfs: Use try_cmpxchg() in xlog_cil_insert_pcp_aggregate()

      commit: 6e19fd40d755a64f271687a21cd304ecba41a571

Best regards,

Signature · 1 line
Carlos Maiolino <[email protected]>