public inbox for linux-xfs@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] xfs: Use try_cmpxchg() in xlog_cil_insert_pcp_aggregate()
@ 2024-09-19  8:14 Uros Bizjak
  2024-09-20 14:16 ` Christoph Hellwig
  0 siblings, 1 reply; 3+ messages in thread
From: Uros Bizjak @ 2024-09-19  8:14 UTC (permalink / raw)
  To: linux-xfs, linux-kernel; +Cc: Uros Bizjak, Chandan Babu R, Darrick J. Wong

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.

Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
Cc: Chandan Babu R <chandan.babu@oracle.com>
Cc: "Darrick J. Wong" <djwong@kernel.org>
---
 fs/xfs/xfs_log_cil.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/fs/xfs/xfs_log_cil.c b/fs/xfs/xfs_log_cil.c
index 391a938d690c..e7a9fcd6935b 100644
--- a/fs/xfs/xfs_log_cil.c
+++ b/fs/xfs/xfs_log_cil.c
@@ -171,13 +171,12 @@ xlog_cil_insert_pcp_aggregate(
 	 * structures that could have a nonzero space_used.
 	 */
 	for_each_cpu(cpu, &ctx->cil_pcpmask) {
-		int	old, prev;
+		int	old;
 
 		cilpcp = per_cpu_ptr(cil->xc_pcp, cpu);
+		old = READ_ONCE(cilpcp->space_used);
 		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);
-- 
2.46.1


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

end of thread, other threads:[~2024-09-22 16:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-19  8:14 [PATCH] xfs: Use try_cmpxchg() in xlog_cil_insert_pcp_aggregate() Uros Bizjak
2024-09-20 14:16 ` Christoph Hellwig
2024-09-22 16:51   ` Uros Bizjak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox