All of lore.kernel.org
 help / color / mirror / Atom feed
From: Bill Wendling <morbo@google.com>
To: linux-kernel@vger.kernel.org
Cc: Bill Wendling <morbo@google.com>,
	Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
	 Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	 "H. Peter Anvin" <hpa@zytor.com>, Arnd Bergmann <arnd@arndb.de>,
	Paul Walmsley <pjw@kernel.org>,
	 Palmer Dabbelt <palmer@dabbelt.com>,
	Albert Ou <aou@eecs.berkeley.edu>,
	 Alexandre Ghiti <alex@ghiti.fr>, Kees Cook <kees@kernel.org>,
	 Catalin Marinas <catalin.marinas@arm.com>,
	Peter Collingbourne <peter@pcc.me.uk>,
	linux-arch@vger.kernel.org,  linux-riscv@lists.infradead.org,
	x86@kernel.org
Subject: [PATCH 2/7] x86/word-at-a-time: Instrument load_unaligned_zeropad()
Date: Fri, 11 Sep 2026 07:03:40 +0000	[thread overview]
Message-ID: <20260911070346.801948-3-morbo@google.com> (raw)
In-Reply-To: <20260911070346.801948-1-morbo@google.com>

load_unaligned_zeropad() is implemented as inline asm plus an
exception table entry on every architecture, so the compiler never
sees the actual memory access and cannot emit KASAN/KCSAN
instrumentation for it.

Resurrect the instrumentation that was lost when migrating from
read_word_at_a_time() by renaming load_unaligned_zeropad() to
arch_load_unaligned_zeropad() and adding a common instrumented
load_unaligned_zeropad() in the new
asm-generic/word-at-a-time-instrumented.h, pulled in from the bottom
of asm/word-at-a-time.h. This mirrors the existing
arch_atomic_*()/atomic_*() split in linux/atomic/atomic-instrumented.h,
where the arch_* version is the raw, uninstrumented primitive and the
plain name is the instrumented one everyone should call.

The wrapper is __always_inline, so it compiles down to exactly the
same inline asm as before plus instrument_read() -- confirmed via
objdump that no calls or relocations to load_unaligned_zeropad(),
instrument_read(), or the KASAN/KCSAN check functions remain when
KASAN and KCSAN are both disabled.

Convert x86 to the new split here. Follow-up patches convert the
remaining architectures (arm64, arm, riscv, s390, powerpc) that
implement load_unaligned_zeropad().

Fixes: d94c12bd97d5 ("string: Add load_unaligned_zeropad() code path to sized_strscpy()")
Assisted-by: Claude:claude-sonnet-5
Signed-off-by: Bill Wendling <morbo@google.com>
---
Cc: Thomas Gleixner <tglx@kernel.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Paul Walmsley <pjw@kernel.org>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: Alexandre Ghiti <alex@ghiti.fr>
Cc: Kees Cook <kees@kernel.org>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Peter Collingbourne <peter@pcc.me.uk>
Cc: linux-kernel@vger.kernel.org
Cc: linux-arch@vger.kernel.org
Cc: linux-riscv@lists.infradead.org
Cc: x86@kernel.org
---
 arch/x86/include/asm/word-at-a-time.h         |  4 ++-
 .../asm-generic/word-at-a-time-instrumented.h | 25 +++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)
 create mode 100644 include/asm-generic/word-at-a-time-instrumented.h

diff --git a/arch/x86/include/asm/word-at-a-time.h b/arch/x86/include/asm/word-at-a-time.h
index 422a47746657..483d27dc96b2 100644
--- a/arch/x86/include/asm/word-at-a-time.h
+++ b/arch/x86/include/asm/word-at-a-time.h
@@ -67,7 +67,7 @@ static inline unsigned long find_zero(unsigned long mask)
  * and the next page not being mapped, take the exception and
  * return zeroes in the non-existing part.
  */
-static inline unsigned long load_unaligned_zeropad(const void *addr)
+static inline unsigned long arch_load_unaligned_zeropad(const void *addr)
 {
 	unsigned long ret;
 
@@ -81,4 +81,6 @@ static inline unsigned long load_unaligned_zeropad(const void *addr)
 	return ret;
 }
 
+#include <asm-generic/word-at-a-time-instrumented.h>
+
 #endif /* _ASM_WORD_AT_A_TIME_H */
diff --git a/include/asm-generic/word-at-a-time-instrumented.h b/include/asm-generic/word-at-a-time-instrumented.h
new file mode 100644
index 000000000000..8a66d53bf538
--- /dev/null
+++ b/include/asm-generic/word-at-a-time-instrumented.h
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_GENERIC_WORD_AT_A_TIME_INSTRUMENTED_H
+#define _ASM_GENERIC_WORD_AT_A_TIME_INSTRUMENTED_H
+
+#include <linux/instrumented.h>
+
+/*
+ * Each arch's asm/word-at-a-time.h defines arch_load_unaligned_zeropad()
+ * as inline asm plus an exception table entry, so the compiler never sees
+ * the actual memory access and cannot emit KASAN/KCSAN instrumentation
+ * for it. Provide the common, instrumented load_unaligned_zeropad() that
+ * every caller uses by wrapping the arch version with instrument_read().
+ *
+ * This is marked __always_inline, and arch_load_unaligned_zeropad() is
+ * itself a tiny inline-asm-only function, so this compiles down to the
+ * same inline asm as before plus instrument_read() (which itself compiles
+ * to nothing when KASAN and KCSAN are both disabled) -- no actual call.
+ */
+static __always_inline unsigned long load_unaligned_zeropad(const void *addr)
+{
+	instrument_read(addr, 1);
+	return arch_load_unaligned_zeropad(addr);
+}
+
+#endif /* _ASM_GENERIC_WORD_AT_A_TIME_INSTRUMENTED_H */
-- 
2.47.3


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

WARNING: multiple messages have this Message-ID (diff)
From: Bill Wendling <morbo@google.com>
To: linux-kernel@vger.kernel.org
Cc: Bill Wendling <morbo@google.com>,
	Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
	 Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	 "H. Peter Anvin" <hpa@zytor.com>, Arnd Bergmann <arnd@arndb.de>,
	Paul Walmsley <pjw@kernel.org>,
	 Palmer Dabbelt <palmer@dabbelt.com>,
	Albert Ou <aou@eecs.berkeley.edu>,
	 Alexandre Ghiti <alex@ghiti.fr>, Kees Cook <kees@kernel.org>,
	 Catalin Marinas <catalin.marinas@arm.com>,
	Peter Collingbourne <peter@pcc.me.uk>,
	linux-arch@vger.kernel.org,  linux-riscv@lists.infradead.org,
	x86@kernel.org
Subject: [PATCH 2/7] x86/word-at-a-time: Instrument load_unaligned_zeropad()
Date: Fri, 11 Sep 2026 07:03:40 +0000	[thread overview]
Message-ID: <20260911070346.801948-3-morbo@google.com> (raw)
In-Reply-To: <20260911070346.801948-1-morbo@google.com>

load_unaligned_zeropad() is implemented as inline asm plus an
exception table entry on every architecture, so the compiler never
sees the actual memory access and cannot emit KASAN/KCSAN
instrumentation for it.

Resurrect the instrumentation that was lost when migrating from
read_word_at_a_time() by renaming load_unaligned_zeropad() to
arch_load_unaligned_zeropad() and adding a common instrumented
load_unaligned_zeropad() in the new
asm-generic/word-at-a-time-instrumented.h, pulled in from the bottom
of asm/word-at-a-time.h. This mirrors the existing
arch_atomic_*()/atomic_*() split in linux/atomic/atomic-instrumented.h,
where the arch_* version is the raw, uninstrumented primitive and the
plain name is the instrumented one everyone should call.

The wrapper is __always_inline, so it compiles down to exactly the
same inline asm as before plus instrument_read() -- confirmed via
objdump that no calls or relocations to load_unaligned_zeropad(),
instrument_read(), or the KASAN/KCSAN check functions remain when
KASAN and KCSAN are both disabled.

Convert x86 to the new split here. Follow-up patches convert the
remaining architectures (arm64, arm, riscv, s390, powerpc) that
implement load_unaligned_zeropad().

Fixes: d94c12bd97d5 ("string: Add load_unaligned_zeropad() code path to sized_strscpy()")
Assisted-by: Claude:claude-sonnet-5
Signed-off-by: Bill Wendling <morbo@google.com>
---
Cc: Thomas Gleixner <tglx@kernel.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Paul Walmsley <pjw@kernel.org>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: Alexandre Ghiti <alex@ghiti.fr>
Cc: Kees Cook <kees@kernel.org>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Peter Collingbourne <peter@pcc.me.uk>
Cc: linux-kernel@vger.kernel.org
Cc: linux-arch@vger.kernel.org
Cc: linux-riscv@lists.infradead.org
Cc: x86@kernel.org
---
 arch/x86/include/asm/word-at-a-time.h         |  4 ++-
 .../asm-generic/word-at-a-time-instrumented.h | 25 +++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)
 create mode 100644 include/asm-generic/word-at-a-time-instrumented.h

diff --git a/arch/x86/include/asm/word-at-a-time.h b/arch/x86/include/asm/word-at-a-time.h
index 422a47746657..483d27dc96b2 100644
--- a/arch/x86/include/asm/word-at-a-time.h
+++ b/arch/x86/include/asm/word-at-a-time.h
@@ -67,7 +67,7 @@ static inline unsigned long find_zero(unsigned long mask)
  * and the next page not being mapped, take the exception and
  * return zeroes in the non-existing part.
  */
-static inline unsigned long load_unaligned_zeropad(const void *addr)
+static inline unsigned long arch_load_unaligned_zeropad(const void *addr)
 {
 	unsigned long ret;
 
@@ -81,4 +81,6 @@ static inline unsigned long load_unaligned_zeropad(const void *addr)
 	return ret;
 }
 
+#include <asm-generic/word-at-a-time-instrumented.h>
+
 #endif /* _ASM_WORD_AT_A_TIME_H */
diff --git a/include/asm-generic/word-at-a-time-instrumented.h b/include/asm-generic/word-at-a-time-instrumented.h
new file mode 100644
index 000000000000..8a66d53bf538
--- /dev/null
+++ b/include/asm-generic/word-at-a-time-instrumented.h
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_GENERIC_WORD_AT_A_TIME_INSTRUMENTED_H
+#define _ASM_GENERIC_WORD_AT_A_TIME_INSTRUMENTED_H
+
+#include <linux/instrumented.h>
+
+/*
+ * Each arch's asm/word-at-a-time.h defines arch_load_unaligned_zeropad()
+ * as inline asm plus an exception table entry, so the compiler never sees
+ * the actual memory access and cannot emit KASAN/KCSAN instrumentation
+ * for it. Provide the common, instrumented load_unaligned_zeropad() that
+ * every caller uses by wrapping the arch version with instrument_read().
+ *
+ * This is marked __always_inline, and arch_load_unaligned_zeropad() is
+ * itself a tiny inline-asm-only function, so this compiles down to the
+ * same inline asm as before plus instrument_read() (which itself compiles
+ * to nothing when KASAN and KCSAN are both disabled) -- no actual call.
+ */
+static __always_inline unsigned long load_unaligned_zeropad(const void *addr)
+{
+	instrument_read(addr, 1);
+	return arch_load_unaligned_zeropad(addr);
+}
+
+#endif /* _ASM_GENERIC_WORD_AT_A_TIME_INSTRUMENTED_H */
-- 
2.47.3


  parent reply	other threads:[~2026-09-11  7:04 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20260911070346.801948-1-morbo@google.com>
2026-09-11  7:03 ` [PATCH 1/7] kasan: Add .kunitconfig for KUnit tests Bill Wendling
2026-09-11  7:03 ` Bill Wendling [this message]
2026-09-11  7:03   ` [PATCH 2/7] x86/word-at-a-time: Instrument load_unaligned_zeropad() Bill Wendling
2026-09-11  7:03 ` [PATCH 3/7] arm64/word-at-a-time: " Bill Wendling
2026-09-11  7:03 ` [PATCH 4/7] arm/word-at-a-time: " Bill Wendling
2026-09-11  7:03 ` [PATCH 5/7] riscv/word-at-a-time: " Bill Wendling
2026-09-11  7:03 ` [PATCH 6/7] s390/word-at-a-time: " Bill Wendling
2026-09-11  7:09   ` sashiko-bot
2026-09-11  7:03 ` [PATCH 7/7] powerpc/word-at-a-time: " Bill Wendling

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=20260911070346.801948-3-morbo@google.com \
    --to=morbo@google.com \
    --cc=alex@ghiti.fr \
    --cc=aou@eecs.berkeley.edu \
    --cc=arnd@arndb.de \
    --cc=bp@alien8.de \
    --cc=catalin.marinas@arm.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=kees@kernel.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=mingo@redhat.com \
    --cc=palmer@dabbelt.com \
    --cc=peter@pcc.me.uk \
    --cc=pjw@kernel.org \
    --cc=tglx@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.