All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ziran Zhang <zhangcoder@yeah.net>
To: Will Deacon <will@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Boqun Feng <boqun@kernel.org>, Thomas Gleixner <tglx@kernel.org>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	x86@kernel.org, "H . Peter Anvin" <hpa@zytor.com>
Cc: Mark Rutland <mark.rutland@arm.com>, Gary Guo <gary@garyguo.net>,
	linux-kernel@vger.kernel.org, Ziran Zhang <zhangcoder@yeah.net>
Subject: [PATCH] x86/atomic: Add arch_atomic_not()
Date: Tue, 14 Jul 2026 13:42:18 +0800	[thread overview]
Message-ID: <20260714054219.39382-1-zhangcoder@yeah.net> (raw)

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


                 reply	other threads:[~2026-07-14  5:57 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260714054219.39382-1-zhangcoder@yeah.net \
    --to=zhangcoder@yeah.net \
    --cc=boqun@kernel.org \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=gary@garyguo.net \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=tglx@kernel.org \
    --cc=will@kernel.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.