Skip to contentSkip to conversation

percpu: Add __this_cpu_try_cmpxchg()

PATCH Superseded Uros Bizjak 2 participants 3 messages May 23, 2024 – May 28, 2024

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 percpu: Add __this_cpu_try_cmpxchg()

Add __this_cpu_try_cmpxchg() version of the percpu op.

Cc Andrew Morton <[email protected]>
Cc Uladzislau Rezki <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Dennis Zhou <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Christoph Lameter <[email protected]>
---
 include/linux/percpu-defs.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/linux/percpu-defs.h +6 -0
index ec3573119923..8efce7414fad 100644
@@ -475,6 +475,12 @@ do {									\
 	raw_cpu_cmpxchg(pcp, oval, nval);				\
 })
 
+#define __this_cpu_try_cmpxchg(pcp, ovalp, nval)			\
+({									\
+	__this_cpu_preempt_check("try_cmpxchg");			\
+	raw_cpu_try_cmpxchg(pcp, ovalp, nval);				\
+})
+
 #define __this_cpu_sub(pcp, val)	__this_cpu_add(pcp, -(typeof(pcp))(val))
 #define __this_cpu_inc(pcp)		__this_cpu_add(pcp, 1)
 #define __this_cpu_dec(pcp)		__this_cpu_sub(pcp, 1)
Signature · 1 line
2.45.1
#1 lore raw

PATCH 2/2 mm/vmalloc: Use __this_cpu_try_cmpxchg() in preload_this_cpu_lock()

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

No functional change intended.

Cc Andrew Morton <[email protected]>
Cc Uladzislau Rezki <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Dennis Zhou <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Christoph Lameter <[email protected]>
---
 mm/vmalloc.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
mm/vmalloc.c +3 -2
index 6641be0ca80b..0120faee5471 100644
@@ -1816,7 +1816,7 @@ static void free_vmap_area(struct vmap_area *va)
 static inline void
 preload_this_cpu_lock(spinlock_t *lock, gfp_t gfp_mask, int node)
 {
-	struct vmap_area *va = NULL;
+	struct vmap_area *va = NULL, *tmp;
 
 	/*
 	 * Preload this CPU with one extra vmap_area object. It is used
@@ -1832,7 +1832,8 @@ preload_this_cpu_lock(spinlock_t *lock, gfp_t gfp_mask, int node)
 
 	spin_lock(lock);
 
-	if (va && __this_cpu_cmpxchg(ne_fit_preload_node, NULL, va))
+	tmp = NULL;
+	if (va && !__this_cpu_try_cmpxchg(ne_fit_preload_node, &tmp, va))
 		kmem_cache_free(vmap_area_cachep, va);
 }
 
Signature · 1 line
2.45.1
maintainer
#2 lore raw

Re: [PATCH 2/2] mm/vmalloc: Use __this_cpu_try_cmpxchg() in preload_this_cpu_lock()

Quoted 5 lines

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

Just a small note. Can you also add a tiny difference in assembly language where it is visible? I mean update a commit message.

--
Uladzislau Rezki