Skip to contentSkip to conversation

x86/atomic: Add arch_atomic_not()

PATCH In review Ziran Zhang 2 participants 3 messages Jul 14 – Jul 15

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/atomic: Add arch_atomic_not()

The kernel already has non-atomic bitmap_complement(), but no direct atomic implementation.

According to Intel and AMD manuals, the NOT instruction supports a LOCK prefix.

Currently the only way to achieve this is arch_atomic_xor(-1, v), which is non-obvious and hurts readability, so add arch_atomic_not() to provide an explicit atomic complement operation.

---
 arch/x86/include/asm/atomic.h | 7 +++++++
 1 file changed, 7 insertions(+)
arch/x86/include/asm/atomic.h +7 -0
index 75743f1df..d7e543194 100644
@@ -168,6 +168,13 @@ static __always_inline int arch_atomic_fetch_xor(int i, atomic_t *v)
 }
 #define arch_atomic_fetch_xor arch_atomic_fetch_xor
 
+static __always_inline void arch_atomic_not(atomic_t *v)
+{
+	asm_inline volatile(LOCK_PREFIX "notl %0"
+			: "+m" (v->counter) :: "memory");
+}
+#define arch_atomic_not arch_atomic_not
+
 #ifdef CONFIG_X86_32
 # include <asm/atomic64_32.h>
 #else
Signature · 1 line
2.51.0
reviewer
#1 lore raw
Quoted 9 lines · Ziran Zhang

The kernel already has non-atomic bitmap_complement(), but no direct atomic implementation.

According to Intel and AMD manuals, the NOT instruction supports a LOCK prefix.

Currently the only way to achieve this is arch_atomic_xor(-1, v), which is non-obvious and hurts readability, so add arch_atomic_not() to provide an explicit atomic complement operation.

What do you need this for?

Best,
Gary

Quoted 24 lines
---
 arch/x86/include/asm/atomic.h | 7 +++++++
 1 file changed, 7 insertions(+)
arch/x86/include/asm/atomic.h +7 -0
index 75743f1df..d7e543194 100644
@@ -168,6 +168,13 @@ static __always_inline int arch_atomic_fetch_xor(int i, atomic_t *v)
 }
 #define arch_atomic_fetch_xor arch_atomic_fetch_xor
 
+static __always_inline void arch_atomic_not(atomic_t *v)
+{
+	asm_inline volatile(LOCK_PREFIX "notl %0"
+			: "+m" (v->counter) :: "memory");
+}
+#define arch_atomic_not arch_atomic_not
+
 #ifdef CONFIG_X86_32
 # include <asm/atomic64_32.h>
 #else
#2 lore raw
Quoted 6 lines · Gary Guo
Quoted 6 lines

The kernel already has non-atomic bitmap_complement(), but no direct atomic implementation.

According to Intel and AMD manuals, the NOT instruction supports a LOCK prefix.

Quoted 8 lines
Quoted 3 lines

Currently the only way to achieve this is arch_atomic_xor(-1, v), which is non-obvious and hurts readability, so add arch_atomic_not() to provide an explicit atomic complement operation.

What do you need this for?

Best,
Gary

Hi Gary,

At present there is no code in kernel that would call arch_atomic_not() or arch_atomic_xor(~0,v). I'll drop this patch and revisit if a real use case shows up in the future.

Thanks for the review!

Regards,
Ziran Zhang