Skip to contentSkip to conversation

xfs: Use try_cmpxchg() in xlog_cil_insert_pcp_aggregate()

PATCH Superseded Uros Bizjak 2 participants 3 messages Sep 22, 2024 – Sep 23, 2024 XFS filesystem

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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.

Cc Chandan Babu R <[email protected]>
Cc "Darrick J. Wong" <[email protected]>
Cc Christoph Hellwig <[email protected]>

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

---
 fs/xfs/xfs_log_cil.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
fs/xfs/xfs_log_cil.c +6 -9
index 391a938d690c..d4e06e6f050f 100644
@@ -156,9 +156,8 @@ xlog_cil_insert_pcp_aggregate(
 	struct xfs_cil		*cil,
 	struct xfs_cil_ctx	*ctx)
 {
-	struct xlog_cil_pcp	*cilpcp;
-	int			cpu;
-	int			count = 0;
+	int	cpu;
+	int	count = 0;
 
 	/* Trigger atomic updates then aggregate only for the first caller */
 	if (!test_and_clear_bit(XLOG_CIL_PCP_SPACE, &cil->xc_flags))
@@ -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.42.0
Reviewed-by on v2 1/1
#1 lore raw

fs/xfs/xfs_log_cil.c xlog_cil_insert_pcp_aggregate( · line 160

Quoted 6 lines

{

fs/xfs/xfs_log_cil.c +2 -3
-	struct xlog_cil_pcp	*cilpcp;
-	int			cpu;
-	int			count = 0;
+	int	cpu;
+	int	count = 0;

This should not be reformatted, but maye Carlos can fix it up when applying the patch. Otherwise looks good:

#2 lore raw
Quoted 10 lines · Christoph Hellwig

fs/xfs/xfs_log_cil.c xlog_cil_insert_pcp_aggregate( · line 160

Quoted 7 lines

{

fs/xfs/xfs_log_cil.c +2 -3
-     struct xlog_cil_pcp     *cilpcp;
-     int                     cpu;
-     int                     count = 0;
+     int     cpu;
+     int     count = 0;

This should not be reformatted, but maye Carlos can fix it up when applying the patch. Otherwise looks good:

I'll just send a v3 with your R-b tag.

Thanks,
Uros.