Skip to contentSkip to conversation

xfs: Use try_cmpxchg() in xlog_cil_insert_pcp_aggregate()

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

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 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]>
---
 fs/xfs/xfs_log_cil.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
fs/xfs/xfs_log_cil.c +3 -4
index 391a938d690c..e7a9fcd6935b 100644
@@ -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);
Signature · 1 line
2.46.1
#1 lore raw

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

Quoted 11 lines · Uros Bizjak
fs/xfs/xfs_log_cil.c +2 -1
@@ -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);

Maybe it is just me, but this would probably look nicer if the cilpcp variable moved into the loop scope, and both were initialized at declaration time:

		struct xlog_cil_pcp	*cilpcp = per_cpu_ptr(cil->xc_pcp, cpu);
		int			old = READ_ONCE(cilpcp->space_used);

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

Quoted 2 lines
fs/xfs/xfs_log_cil.c +1 -0
 		do {
+		} while (!try_cmpxchg(&cilpcp->space_used, &old, 0));

And this also looks a bit odd. Again, probably preference, but a:

		while (!try_cmpxchg(&cilpcp->space_used, &old, 0))
			;

looks somewhat more normal (although still not pretty).

Sorry for not having anything more substantial to see, but the diff just looked a bit odd..

#2 lore raw
Quoted 18 lines · Christoph Hellwig

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

Quoted 11 lines · Uros Bizjak
fs/xfs/xfs_log_cil.c +2 -1
@@ -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);

Maybe it is just me, but this would probably look nicer if the cilpcp variable moved into the loop scope, and both were initialized at declaration time:

Quoted 2 lines
                struct xlog_cil_pcp     *cilpcp = per_cpu_ptr(cil->xc_pcp, cpu);
                int                     old = READ_ONCE(cilpcp->space_used);

No problem, I just tried to keep the number of changed lines as low as possible. Some maintainers don't like functional and cosmetic changes mixed together.

Quoted 10 lines

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

Quoted 3 lines
fs/xfs/xfs_log_cil.c +1 -0
              do {
+             } while (!try_cmpxchg(&cilpcp->space_used, &old, 0));

And this also looks a bit odd. Again, probably preference, but a:

                while (!try_cmpxchg(&cilpcp->space_used, &old, 0))
                        ;

looks somewhat more normal (although still not pretty).

Yes, the alternative form is what some maintainers prefer.

Quoted 2 lines

Sorry for not having anything more substantial to see, but the diff just looked a bit odd..

I'll prepare a v2 patch with the suggested changes.

Thanks,
Uros.