Skip to contentSkip to conversation

s390/irqflags: add out-of-line definitions of arch_local_irq_*() for KMSAN

PATCH Superseded Ilya Leoshkevich 3 participants 5 messages Aug 6 s390

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 s390/irqflags: add out-of-line definitions of arch_local_irq_*() for KMSAN

Inline KMSAN arch_local_irq_*() definitions run afoul of -Wstatic-in-inline. Move them out-of-line. Decompressor is not instrumented, so make sure it keeps using inline definitions. Make sure non-GPL modules see the out-of-line definitions.

Cc Boqun Feng <[email protected]>
Fixes 1b301f5f28ba ("s390/irqflags: do not instrument arch_local_irq_*() with KMSAN")
---
 arch/s390/include/asm/irqflags.h | 20 +++++++++++++-------
 arch/s390/kernel/Makefile        |  1 +
 arch/s390/kernel/irqflags.c      | 27 +++++++++++++++++++++++++++
 3 files changed, 41 insertions(+), 7 deletions(-)
 create mode 100644 arch/s390/kernel/irqflags.c
arch/s390/include/asm/irqflags.h +13 -7
index bcab456dfb803..44d158f7831e6 100644
@@ -37,18 +37,24 @@ static __always_inline void __arch_local_irq_ssm(unsigned long flags)
 	asm volatile("ssm   %0" : : "Q" (flags) : "memory");
 }
 
-#ifdef CONFIG_KMSAN
-#define arch_local_irq_attributes noinline notrace __no_sanitize_memory __maybe_unused
+#if defined(CONFIG_KMSAN) && !defined(__DECOMPRESSOR)
+extern unsigned long arch_local_save_flags(void);
+extern unsigned long arch_local_irq_save(void);
+extern void arch_local_irq_enable_external(void);
+extern void arch_local_irq_enable(void);
 #else
-#define arch_local_irq_attributes __always_inline
+#define arch_local_save_flags __arch_local_save_flags
+#define arch_local_irq_save __arch_local_irq_save
+#define arch_local_irq_enable_external __arch_local_irq_enable_external
+#define arch_local_irq_enable __arch_local_irq_enable
 #endif
 
-static arch_local_irq_attributes unsigned long arch_local_save_flags(void)
+static __always_inline unsigned long __arch_local_save_flags(void)
 {
 	return __arch_local_irq_stnsm(0xff);
 }
 
-static arch_local_irq_attributes unsigned long arch_local_irq_save(void)
+static __always_inline unsigned long __arch_local_irq_save(void)
 {
 	return __arch_local_irq_stnsm(0xfc);
 }
@@ -58,12 +64,12 @@ static __always_inline void arch_local_irq_disable(void)
 	arch_local_irq_save();
 }
 
-static arch_local_irq_attributes void arch_local_irq_enable_external(void)
+static __always_inline void __arch_local_irq_enable_external(void)
 {
 	__arch_local_irq_stosm(0x01);
 }
 
-static arch_local_irq_attributes void arch_local_irq_enable(void)
+static __always_inline void __arch_local_irq_enable(void)
 {
 	__arch_local_irq_stosm(0x03);
 }
arch/s390/kernel/Makefile +1 -0
index 6c88476d79a31..14ef03cb2f72a 100644
@@ -72,6 +72,7 @@ obj-$(CONFIG_STACKPROTECTOR)	+= stackprotector.o
 obj-$(CONFIG_KEXEC_FILE)	+= machine_kexec_file.o kexec_image.o
 obj-$(CONFIG_KEXEC_FILE)	+= kexec_elf.o
 obj-$(CONFIG_CERT_STORE)	+= cert_store.o
+obj-$(CONFIG_KMSAN)		+= irqflags.o
 
 obj-$(CONFIG_PERF_EVENTS)	+= perf_event.o
 obj-$(CONFIG_PERF_EVENTS)	+= perf_cpum_cf.o perf_cpum_sf.o
arch/s390/kernel/irqflags.c new file +27 -0
new file mode 100644
index 0000000000000..98299c4758c6e
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <asm/irqflags.h>
+#include <linux/export.h>
+
+noinstr unsigned long arch_local_save_flags(void)
+{
+	return __arch_local_save_flags();
+}
+EXPORT_SYMBOL(arch_local_save_flags);
+
+noinstr unsigned long arch_local_irq_save(void)
+{
+	return __arch_local_irq_save();
+}
+EXPORT_SYMBOL(arch_local_irq_save);
+
+noinstr void arch_local_irq_enable_external(void)
+{
+	__arch_local_irq_enable_external();
+}
+EXPORT_SYMBOL(arch_local_irq_enable_external);
+
+noinstr void arch_local_irq_enable(void)
+{
+	__arch_local_irq_enable();
+}
+EXPORT_SYMBOL(arch_local_irq_enable);
Signature · 1 line
2.55.0
maintainer
#1 lore raw
Quoted 11 lines · Ilya Leoshkevich

Inline KMSAN arch_local_irq_*() definitions run afoul of -Wstatic-in-inline. Move them out-of-line. Decompressor is not instrumented, so make sure it keeps using inline definitions. Make sure non-GPL modules see the out-of-line definitions.

Cc Boqun Feng <[email protected]>
Fixes 1b301f5f28ba ("s390/irqflags: do not instrument arch_local_irq_*() with KMSAN")
Quoted 6 lines
---
 arch/s390/include/asm/irqflags.h | 20 +++++++++++++-------
 arch/s390/kernel/Makefile        |  1 +
 arch/s390/kernel/irqflags.c      | 27 +++++++++++++++++++++++++++
 3 files changed, 41 insertions(+), 7 deletions(-)
 create mode 100644 arch/s390/kernel/irqflags.c

Don't we have the very same problem in arch/s390/include/asm/uaccess.h with uaccess_kmsan_or_inline too?

arch/s390/include/asm/irqflags.h static __always_inline void __arch_local_irq_ssm(unsigned long flags) · line 44

Quoted 7 lines
arch/s390/include/asm/irqflags.h +5 -2
-#ifdef CONFIG_KMSAN
-#define arch_local_irq_attributes noinline notrace __no_sanitize_memory __maybe_unused
+#if defined(CONFIG_KMSAN) && !defined(__DECOMPRESSOR)
+extern unsigned long arch_local_save_flags(void);
+extern unsigned long arch_local_irq_save(void);
+extern void arch_local_irq_enable_external(void);
+extern void arch_local_irq_enable(void);

FWIW, we go without "extern" for function prototypes.

arch/s390/include/asm/irqflags.h static __always_inline void __arch_local_irq_ssm(unsigned long flags) · line 50

Quoted 7 lines

#else

arch/s390/include/asm/irqflags.h +4 -1
-#define arch_local_irq_attributes __always_inline
+#define arch_local_save_flags __arch_local_save_flags
+#define arch_local_irq_save __arch_local_irq_save
+#define arch_local_irq_enable_external __arch_local_irq_enable_external
+#define arch_local_irq_enable __arch_local_irq_enable
 #endif

For the sake of readability: could you adjust all of them with tabs? Like e.g.:

#define arch_local_save_flags __arch_local_save_flags
#define arch_local_irq_save __arch_local_irq_save
#define arch_local_irq_enable_external __arch_local_irq_enable_external
#define arch_local_irq_enable __arch_local_irq_enable

And just to annoy you: the patch subject needs to be adjusted to:

s390/irqflags: add out-of-line definitions of arch_local_irq_*() for KMSAN

               ^

In the meantime we require always an upper case letter following the component.

arch/s390/kernel/irqflags.c line 3

Quoted 5 lines

+++ b/arch/s390/kernel/irqflags.c

arch/s390/kernel/irqflags.c new file +3 -0
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <asm/irqflags.h>
+#include <linux/export.h>

Please add an empty line before the first include, and also change the order of includes - "linux" should always come before "asm".

#2 lore raw
Quoted 19 lines · Heiko Carstens
Quoted 11 lines · Ilya Leoshkevich

Inline KMSAN arch_local_irq_*() definitions run afoul of -Wstatic-in-inline. Move them out-of-line. Decompressor is not instrumented, so make sure it keeps using inline definitions. Make sure non-GPL modules see the out-of-line definitions.

Cc Boqun Feng <[email protected]>
Fixes 1b301f5f28ba ("s390/irqflags: do not instrument arch_local_irq_*() with KMSAN")
Quoted 6 lines
---
  arch/s390/include/asm/irqflags.h | 20 +++++++++++++-------
  arch/s390/kernel/Makefile        |  1 +
  arch/s390/kernel/irqflags.c      | 27 +++++++++++++++++++++++++++
  3 files changed, 41 insertions(+), 7 deletions(-)

create mode 100644 arch/s390/kernel/irqflags.c

Quoted 2 lines

Don't we have the very same problem in arch/s390/include/asm/uaccess.h with uaccess_kmsan_or_inline too?

Apparently yes. I will send a separate patch.

And I'll fix the rest of your findings in v2.

#3 lore raw
In reply to Ilya Leoshkevich
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Missing KMSAN attributes on the `extern` declarations of `arch_local_save_flags` and…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Missing KMSAN attributes on the extern declarations of arch_local_save_flags and arch_local_irq_save will cause spurious use-of-uninitialized-value reports. --

--- Patch [1]: [PATCH] s390/irqflags: add out-of-line definitions of arch_local_irq_*() for KMSAN --- commit 75ac5ab83800e65155075aeee91b3fd23aa6274d
Author: Ilya Leoshkevich <[email protected]>

s390/irqflags: add out-of-line definitions of arch_local_irq_*() for KMSAN

This commit moves KMSAN arch_local_irq_*() definitions out-of-line to avoid warnings about static functions in inline functions. Uninstrumented decompressor code keeps the inline definitions, while non-GPL modules see the out-of-line versions.

arch/s390/include/asm/irqflags.h static __always_inline void __arch_local_irq_ssm(unsigned long flags) · line 42

Quoted 13 lines
arch/s390/include/asm/irqflags.h +3 -2
index bcab456dfb803..44d158f7831e6 100644
@@ -37,18 +37,24 @@ static __always_inline void __arch_local_irq_ssm(unsigned long flags)
 	asm volatile("ssm   %0" : : "Q" (flags) : "memory");
 }
 
-#ifdef CONFIG_KMSAN
-#define arch_local_irq_attributes noinline notrace __no_sanitize_memory __maybe_unused
+#if defined(CONFIG_KMSAN) && !defined(__DECOMPRESSOR)
+extern unsigned long arch_local_save_flags(void);
+extern unsigned long arch_local_irq_save(void);

[Severity: High]
Since these extern declarations lack KMSAN attributes, will instrumented callers expect a populated return value shadow in __msan_retval_tls?

When arch_local_save_flags() and arch_local_irq_save() are called from instrumented code, the caller reads __msan_retval_tls. However, because the corresponding definitions are marked noinstr:

arch/s390/kernel/irqflags.c:
noinstr unsigned long arch_local_save_flags(void)

{
	return __arch_local_save_flags();
}

instrumentation is skipped, and __msan_retval_tls is never populated before returning to the caller.

Could this result in spurious use-of-uninitialized-value reports on every IRQ state check or lock operation (like spin_lock_irqsave) when the kernel is built with CONFIG_KMSAN?

Quoted 6 lines
arch/s390/include/asm/irqflags.h +4 -1
+extern void arch_local_irq_enable_external(void);
+extern void arch_local_irq_enable(void);
 #else
-#define arch_local_irq_attributes __always_inline
+#define arch_local_save_flags __arch_local_save_flags
+#define arch_local_irq_save __arch_local_irq_save

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#4 lore raw
Quoted 2 lines · On 8/6/26 12:19, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Missing KMSAN attributes on the extern declarations of arch_local_save_flags and arch_local_irq_save will cause spurious use-of-uninitialized-value reports.

[...]

Quoted 20 lines

arch/s390/include/asm/irqflags.h static __always_inline void __arch_local_irq_ssm(unsigned long flags) · line 42

Quoted 5 lines
arch/s390/include/asm/irqflags.h +3 -2
-#ifdef CONFIG_KMSAN
-#define arch_local_irq_attributes noinline notrace __no_sanitize_memory __maybe_unused
+#if defined(CONFIG_KMSAN) && !defined(__DECOMPRESSOR)
+extern unsigned long arch_local_save_flags(void);
+extern unsigned long arch_local_irq_save(void);

[Severity: High]
Since these extern declarations lack KMSAN attributes, will instrumented callers expect a populated return value shadow in __msan_retval_tls?

When arch_local_save_flags() and arch_local_irq_save() are called from instrumented code, the caller reads __msan_retval_tls. However, because the corresponding definitions are marked noinstr:

arch/s390/kernel/irqflags.c:
noinstr unsigned long arch_local_save_flags(void)

{
	return __arch_local_save_flags();
}
Quoted 6 lines

instrumentation is skipped, and __msan_retval_tls is never populated before returning to the caller.

Could this result in spurious use-of-uninitialized-value reports on every IRQ state check or lock operation (like spin_lock_irqsave) when the kernel is built with CONFIG_KMSAN?

This is a known issue that affects many other places. It can be resolved by compiling with KMSAN_CHECK_PARAM_RETVAL:

lib/Kconfig.kmsan:
------------------
Disabling KMSAN_CHECK_PARAM_RETVAL will result in tracking shadow for function parameters and return values across function borders. This is a more relaxed mode, but it generates more instrumentation code and may potentially report errors in corner cases when non-instrumented functions call instrumented ones.

LLVM refers to this as "EagerChecks" internally.

[...]