* [PATCH] x86/atomic: Add arch_atomic_not()
@ 2026-07-14 5:42 Ziran Zhang
2026-07-15 11:49 ` Gary Guo
0 siblings, 1 reply; 3+ messages in thread
From: Ziran Zhang @ 2026-07-14 5:42 UTC (permalink / raw)
To: Will Deacon, Peter Zijlstra, Boqun Feng, Thomas Gleixner,
Ingo Molnar, Borislav Petkov, Dave Hansen, x86, H . Peter Anvin
Cc: Mark Rutland, Gary Guo, linux-kernel, 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.
Signed-off-by: Ziran Zhang <zhangcoder@yeah.net>
---
arch/x86/include/asm/atomic.h | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/arch/x86/include/asm/atomic.h b/arch/x86/include/asm/atomic.h
index 75743f1df..d7e543194 100644
--- a/arch/x86/include/asm/atomic.h
+++ b/arch/x86/include/asm/atomic.h
@@ -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.51.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] x86/atomic: Add arch_atomic_not()
2026-07-14 5:42 [PATCH] x86/atomic: Add arch_atomic_not() Ziran Zhang
@ 2026-07-15 11:49 ` Gary Guo
2026-07-15 14:30 ` Ziran Zhang
0 siblings, 1 reply; 3+ messages in thread
From: Gary Guo @ 2026-07-15 11:49 UTC (permalink / raw)
To: Ziran Zhang, Will Deacon, Peter Zijlstra, Boqun Feng,
Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H . Peter Anvin
Cc: Mark Rutland, Gary Guo, linux-kernel
On Tue Jul 14, 2026 at 6:42 AM BST, Ziran Zhang wrote:
> 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
>
> Signed-off-by: Ziran Zhang <zhangcoder@yeah.net>
> ---
> arch/x86/include/asm/atomic.h | 7 +++++++
> 1 file changed, 7 insertions(+)
>
> diff --git a/arch/x86/include/asm/atomic.h b/arch/x86/include/asm/atomic.h
> index 75743f1df..d7e543194 100644
> --- a/arch/x86/include/asm/atomic.h
> +++ b/arch/x86/include/asm/atomic.h
> @@ -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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] x86/atomic: Add arch_atomic_not()
2026-07-15 11:49 ` Gary Guo
@ 2026-07-15 14:30 ` Ziran Zhang
0 siblings, 0 replies; 3+ messages in thread
From: Ziran Zhang @ 2026-07-15 14:30 UTC (permalink / raw)
To: Gary Guo
Cc: Will Deacon, Peter Zijlstra, Boqun Feng, Thomas Gleixner,
Ingo Molnar, Borislav Petkov, Dave Hansen, H . Peter Anvin, x86,
Mark Rutland, linux-kernel, Ziran Zhang
On Wed, 15 Jul 2026 12:49:20 +0100, Gary Guo wrote:
> > 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
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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-07-15 14:32 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-14 5:42 [PATCH] x86/atomic: Add arch_atomic_not() Ziran Zhang
2026-07-15 11:49 ` Gary Guo
2026-07-15 14:30 ` Ziran Zhang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox