* [PATCH v6 1/4] x86/bug: Add ARCH_WARN_ASM macro for BUG/WARN asm code sharing with Rust
2025-05-02 9:45 [PATCH v6 0/4] rust: Add bug/warn abstractions FUJITA Tomonori
@ 2025-05-02 9:45 ` FUJITA Tomonori
2025-05-02 9:45 ` [PATCH v6 2/4] riscv/bug: " FUJITA Tomonori
` (4 subsequent siblings)
5 siblings, 0 replies; 12+ messages in thread
From: FUJITA Tomonori @ 2025-05-02 9:45 UTC (permalink / raw)
To: linux-kernel, rust-for-linux
Cc: Peter Zijlstra, x86, linux-riscv, linux-arm-kernel, loongarch,
tglx, mingo, bp, dave.hansen, hpa, paul.walmsley, palmer, aou,
catalin.marinas, will, chenhuacai, kernel, tangyouling, hejinyang,
yangtiezhu, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh,
benno.lossin, a.hindborg, aliceryhl, tmgross, dakr
Add new ARCH_WARN_ASM macro for BUG/WARN assembly code sharing with
Rust to avoid the duplication.
No functional changes.
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
arch/x86/include/asm/bug.h | 56 +++++++++++++++++++-------------------
1 file changed, 28 insertions(+), 28 deletions(-)
diff --git a/arch/x86/include/asm/bug.h b/arch/x86/include/asm/bug.h
index f0e9acf72547..be5aef1aff9e 100644
--- a/arch/x86/include/asm/bug.h
+++ b/arch/x86/include/asm/bug.h
@@ -32,45 +32,42 @@
#ifdef CONFIG_GENERIC_BUG
#ifdef CONFIG_X86_32
-# define __BUG_REL(val) ".long " __stringify(val)
+# define __BUG_REL(val) ".long " val
#else
-# define __BUG_REL(val) ".long " __stringify(val) " - ."
+# define __BUG_REL(val) ".long " val " - ."
#endif
#ifdef CONFIG_DEBUG_BUGVERBOSE
+#define __BUG_ENTRY(file, line, flags) \
+ "2:\t" __BUG_REL("1b") "\t# bug_entry::bug_addr\n" \
+ "\t" __BUG_REL(file) "\t# bug_entry::file\n" \
+ "\t.word " line "\t# bug_entry::line\n" \
+ "\t.word " flags "\t# bug_entry::flags\n"
+#else
+#define __BUG_ENTRY(file, ine, flags) \
+ "2:\t" __BUG_REL("1b") "\t# bug_entry::bug_addr\n" \
+ "\t.word " flags "\t# bug_entry::flags\n"
+#endif
+
+#define _BUG_FLAGS_ASM(ins, file, line, flags, size, extra) \
+ "1:\t" ins "\n" \
+ ".pushsection __bug_table,\"aw\"\n" \
+ __BUG_ENTRY(file, line, flags) \
+ "\t.org 2b + " size "\n" \
+ ".popsection\n" \
+ extra
#define _BUG_FLAGS(ins, flags, extra) \
do { \
- asm_inline volatile("1:\t" ins "\n" \
- ".pushsection __bug_table,\"aw\"\n" \
- "2:\t" __BUG_REL(1b) "\t# bug_entry::bug_addr\n" \
- "\t" __BUG_REL(%c0) "\t# bug_entry::file\n" \
- "\t.word %c1" "\t# bug_entry::line\n" \
- "\t.word %c2" "\t# bug_entry::flags\n" \
- "\t.org 2b+%c3\n" \
- ".popsection\n" \
- extra \
+ asm_inline volatile(_BUG_FLAGS_ASM(ins, "%c0", \
+ "%c1", "%c2", "%c3", extra) \
: : "i" (__FILE__), "i" (__LINE__), \
"i" (flags), \
"i" (sizeof(struct bug_entry))); \
} while (0)
-#else /* !CONFIG_DEBUG_BUGVERBOSE */
-
-#define _BUG_FLAGS(ins, flags, extra) \
-do { \
- asm_inline volatile("1:\t" ins "\n" \
- ".pushsection __bug_table,\"aw\"\n" \
- "2:\t" __BUG_REL(1b) "\t# bug_entry::bug_addr\n" \
- "\t.word %c0" "\t# bug_entry::flags\n" \
- "\t.org 2b+%c1\n" \
- ".popsection\n" \
- extra \
- : : "i" (flags), \
- "i" (sizeof(struct bug_entry))); \
-} while (0)
-
-#endif /* CONFIG_DEBUG_BUGVERBOSE */
+#define ARCH_WARN_ASM(file, line, flags, size) \
+ _BUG_FLAGS_ASM(ASM_UD2, file, line, flags, size, "")
#else
@@ -92,11 +89,14 @@ do { \
* were to trigger, we'd rather wreck the machine in an attempt to get the
* message out than not know about it.
*/
+
+#define ARCH_WARN_REACHABLE ANNOTATE_REACHABLE(1b)
+
#define __WARN_FLAGS(flags) \
do { \
__auto_type __flags = BUGFLAG_WARNING|(flags); \
instrumentation_begin(); \
- _BUG_FLAGS(ASM_UD2, __flags, ANNOTATE_REACHABLE(1b)); \
+ _BUG_FLAGS(ASM_UD2, __flags, ARCH_WARN_REACHABLE); \
instrumentation_end(); \
} while (0)
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH v6 2/4] riscv/bug: Add ARCH_WARN_ASM macro for BUG/WARN asm code sharing with Rust
2025-05-02 9:45 [PATCH v6 0/4] rust: Add bug/warn abstractions FUJITA Tomonori
2025-05-02 9:45 ` [PATCH v6 1/4] x86/bug: Add ARCH_WARN_ASM macro for BUG/WARN asm code sharing with Rust FUJITA Tomonori
@ 2025-05-02 9:45 ` FUJITA Tomonori
2025-05-02 9:45 ` [PATCH v6 3/4] arm64/bug: " FUJITA Tomonori
` (3 subsequent siblings)
5 siblings, 0 replies; 12+ messages in thread
From: FUJITA Tomonori @ 2025-05-02 9:45 UTC (permalink / raw)
To: linux-kernel, rust-for-linux
Cc: Alexandre Ghiti, x86, linux-riscv, linux-arm-kernel, loongarch,
tglx, mingo, bp, dave.hansen, peterz, hpa, paul.walmsley, palmer,
aou, catalin.marinas, will, chenhuacai, kernel, tangyouling,
hejinyang, yangtiezhu, ojeda, alex.gaynor, boqun.feng, gary,
bjorn3_gh, benno.lossin, a.hindborg, aliceryhl, tmgross, dakr
Add new ARCH_WARN_ASM macro for BUG/WARN assembly code sharing with
Rust to avoid the duplication.
No functional changes.
Acked-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
arch/riscv/include/asm/bug.h | 37 +++++++++++++++++++++---------------
1 file changed, 22 insertions(+), 15 deletions(-)
diff --git a/arch/riscv/include/asm/bug.h b/arch/riscv/include/asm/bug.h
index 1aaea81fb141..6ab13b56feb0 100644
--- a/arch/riscv/include/asm/bug.h
+++ b/arch/riscv/include/asm/bug.h
@@ -31,40 +31,45 @@ typedef u32 bug_insn_t;
#ifdef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
#define __BUG_ENTRY_ADDR RISCV_INT " 1b - ."
-#define __BUG_ENTRY_FILE RISCV_INT " %0 - ."
+#define __BUG_ENTRY_FILE(file) RISCV_INT " " file " - ."
#else
#define __BUG_ENTRY_ADDR RISCV_PTR " 1b"
-#define __BUG_ENTRY_FILE RISCV_PTR " %0"
+#define __BUG_ENTRY_FILE(file) RISCV_PTR " " file
#endif
#ifdef CONFIG_DEBUG_BUGVERBOSE
-#define __BUG_ENTRY \
+#define __BUG_ENTRY(file, line, flags) \
__BUG_ENTRY_ADDR "\n\t" \
- __BUG_ENTRY_FILE "\n\t" \
- RISCV_SHORT " %1\n\t" \
- RISCV_SHORT " %2"
+ __BUG_ENTRY_FILE(file) "\n\t" \
+ RISCV_SHORT " " line "\n\t" \
+ RISCV_SHORT " " flags
#else
-#define __BUG_ENTRY \
- __BUG_ENTRY_ADDR "\n\t" \
- RISCV_SHORT " %2"
+#define __BUG_ENTRY(file, line, flags) \
+ __BUG_ENTRY_ADDR "\n\t" \
+ RISCV_SHORT " " flags
#endif
#ifdef CONFIG_GENERIC_BUG
-#define __BUG_FLAGS(flags) \
-do { \
- __asm__ __volatile__ ( \
+
+#define ARCH_WARN_ASM(file, line, flags, size) \
"1:\n\t" \
"ebreak\n" \
".pushsection __bug_table,\"aw\"\n\t" \
"2:\n\t" \
- __BUG_ENTRY "\n\t" \
- ".org 2b + %3\n\t" \
- ".popsection" \
+ __BUG_ENTRY(file, line, flags) "\n\t" \
+ ".org 2b + " size "\n\t" \
+ ".popsection\n" \
+
+#define __BUG_FLAGS(flags) \
+do { \
+ __asm__ __volatile__ ( \
+ ARCH_WARN_ASM("%0", "%1", "%2", "%3") \
: \
: "i" (__FILE__), "i" (__LINE__), \
"i" (flags), \
"i" (sizeof(struct bug_entry))); \
} while (0)
+
#else /* CONFIG_GENERIC_BUG */
#define __BUG_FLAGS(flags) do { \
__asm__ __volatile__ ("ebreak\n"); \
@@ -78,6 +83,8 @@ do { \
#define __WARN_FLAGS(flags) __BUG_FLAGS(BUGFLAG_WARNING|(flags))
+#define ARCH_WARN_REACHABLE
+
#define HAVE_ARCH_BUG
#include <asm-generic/bug.h>
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH v6 3/4] arm64/bug: Add ARCH_WARN_ASM macro for BUG/WARN asm code sharing with Rust
2025-05-02 9:45 [PATCH v6 0/4] rust: Add bug/warn abstractions FUJITA Tomonori
2025-05-02 9:45 ` [PATCH v6 1/4] x86/bug: Add ARCH_WARN_ASM macro for BUG/WARN asm code sharing with Rust FUJITA Tomonori
2025-05-02 9:45 ` [PATCH v6 2/4] riscv/bug: " FUJITA Tomonori
@ 2025-05-02 9:45 ` FUJITA Tomonori
2025-05-02 9:45 ` [PATCH v6 4/4] rust: Add warn_on macro FUJITA Tomonori
` (2 subsequent siblings)
5 siblings, 0 replies; 12+ messages in thread
From: FUJITA Tomonori @ 2025-05-02 9:45 UTC (permalink / raw)
To: linux-kernel, rust-for-linux
Cc: Catalin Marinas, x86, linux-riscv, linux-arm-kernel, loongarch,
tglx, mingo, bp, dave.hansen, peterz, hpa, paul.walmsley, palmer,
aou, will, chenhuacai, kernel, tangyouling, hejinyang, yangtiezhu,
ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, benno.lossin,
a.hindborg, aliceryhl, tmgross, dakr
Add new ARCH_WARN_ASM macro for BUG/WARN assembly code sharing with
Rust to avoid the duplication.
No functional changes.
Acked-by: Catalin Marinas <catalin.marinas@arm.com>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
arch/arm64/include/asm/asm-bug.h | 33 ++++++++++++++++++++++++++++----
1 file changed, 29 insertions(+), 4 deletions(-)
diff --git a/arch/arm64/include/asm/asm-bug.h b/arch/arm64/include/asm/asm-bug.h
index 6e73809f6492..a5f13801b784 100644
--- a/arch/arm64/include/asm/asm-bug.h
+++ b/arch/arm64/include/asm/asm-bug.h
@@ -21,16 +21,21 @@
#endif
#ifdef CONFIG_GENERIC_BUG
-
-#define __BUG_ENTRY(flags) \
+#define __BUG_ENTRY_START \
.pushsection __bug_table,"aw"; \
.align 2; \
14470: .long 14471f - .; \
-_BUGVERBOSE_LOCATION(__FILE__, __LINE__) \
- .short flags; \
+
+#define __BUG_ENTRY_END \
.align 2; \
.popsection; \
14471:
+
+#define __BUG_ENTRY(flags) \
+ __BUG_ENTRY_START \
+_BUGVERBOSE_LOCATION(__FILE__, __LINE__) \
+ .short flags; \
+ __BUG_ENTRY_END
#else
#define __BUG_ENTRY(flags)
#endif
@@ -41,4 +46,24 @@ _BUGVERBOSE_LOCATION(__FILE__, __LINE__) \
#define ASM_BUG() ASM_BUG_FLAGS(0)
+#ifdef CONFIG_DEBUG_BUGVERBOSE
+#define __BUG_LOCATION_STRING(file, line) \
+ ".long " file "- .;" \
+ ".short " line ";"
+#else
+#define __BUG_LOCATION_STRING(file, line)
+#endif
+
+#define __BUG_ENTRY_STRING(file, line, flags) \
+ __stringify(__BUG_ENTRY_START) \
+ __BUG_LOCATION_STRING(file, line) \
+ ".short " flags ";" \
+ __stringify(__BUG_ENTRY_END)
+
+#define ARCH_WARN_ASM(file, line, flags, size) \
+ __BUG_ENTRY_STRING(file, line, flags) \
+ __stringify(brk BUG_BRK_IMM)
+
+#define ARCH_WARN_REACHABLE
+
#endif /* __ASM_ASM_BUG_H */
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH v6 4/4] rust: Add warn_on macro
2025-05-02 9:45 [PATCH v6 0/4] rust: Add bug/warn abstractions FUJITA Tomonori
` (2 preceding siblings ...)
2025-05-02 9:45 ` [PATCH v6 3/4] arm64/bug: " FUJITA Tomonori
@ 2025-05-02 9:45 ` FUJITA Tomonori
2025-05-02 11:17 ` Alice Ryhl
2025-06-05 1:56 ` [PATCH v6 0/4] rust: Add bug/warn abstractions Miguel Ojeda
2025-07-23 0:48 ` Miguel Ojeda
5 siblings, 1 reply; 12+ messages in thread
From: FUJITA Tomonori @ 2025-05-02 9:45 UTC (permalink / raw)
To: linux-kernel, rust-for-linux
Cc: x86, linux-riscv, linux-arm-kernel, loongarch, tglx, mingo, bp,
dave.hansen, peterz, hpa, paul.walmsley, palmer, aou,
catalin.marinas, will, chenhuacai, kernel, tangyouling, hejinyang,
yangtiezhu, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh,
benno.lossin, a.hindborg, aliceryhl, tmgross, dakr
Add warn_on macro, uses the BUG/WARN feature (lib/bug.c) via assembly
for x86_64/arm64/riscv.
The current Rust code simply wraps BUG() macro but doesn't provide the
proper debug information. The BUG/WARN feature can only be used from
assembly.
This uses the assembly code exported by the C side via ARCH_WARN_ASM
macro. To avoid duplicating the assembly code, this approach follows
the same strategy as the static branch code: it generates the assembly
code for Rust using the C preprocessor at compile time.
Similarly, ARCH_WARN_REACHABLE is also used at compile time to
generate the assembly code; objtool's reachable annotation code. It's
used for only architectures that use objtool.
For now, Loongarch and arm just use a wrapper for WARN macro.
UML doesn't use the assembly BUG/WARN feature; just wrapping generic
BUG/WARN functions implemented in C works.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
rust/Makefile | 8 ++
rust/helpers/bug.c | 5 +
rust/kernel/.gitignore | 2 +
rust/kernel/bug.rs | 125 ++++++++++++++++++
rust/kernel/generated_arch_reachable_asm.rs.S | 7 +
rust/kernel/generated_arch_warn_asm.rs.S | 7 +
rust/kernel/lib.rs | 1 +
7 files changed, 155 insertions(+)
create mode 100644 rust/kernel/bug.rs
create mode 100644 rust/kernel/generated_arch_reachable_asm.rs.S
create mode 100644 rust/kernel/generated_arch_warn_asm.rs.S
diff --git a/rust/Makefile b/rust/Makefile
index 3aca903a7d08..0b795cf05a34 100644
--- a/rust/Makefile
+++ b/rust/Makefile
@@ -34,6 +34,9 @@ obj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.o
obj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.o
always-$(subst y,$(CONFIG_RUST),$(CONFIG_JUMP_LABEL)) += kernel/generated_arch_static_branch_asm.rs
+ifndef CONFIG_UML
+always-$(subst y,$(CONFIG_RUST),$(CONFIG_BUG)) += kernel/generated_arch_warn_asm.rs kernel/generated_arch_reachable_asm.rs
+endif
# Avoids running `$(RUSTC)` when it may not be available.
ifdef CONFIG_RUST
@@ -536,5 +539,10 @@ $(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/build_error.o $(obj)/pin_init.o \
ifdef CONFIG_JUMP_LABEL
$(obj)/kernel.o: $(obj)/kernel/generated_arch_static_branch_asm.rs
endif
+ifndef CONFIG_UML
+ifdef CONFIG_BUG
+$(obj)/kernel.o: $(obj)/kernel/generated_arch_warn_asm.rs $(obj)/kernel/generated_arch_reachable_asm.rs
+endif
+endif
endif # CONFIG_RUST
diff --git a/rust/helpers/bug.c b/rust/helpers/bug.c
index e2d13babc737..a62c96f507d1 100644
--- a/rust/helpers/bug.c
+++ b/rust/helpers/bug.c
@@ -6,3 +6,8 @@ __noreturn void rust_helper_BUG(void)
{
BUG();
}
+
+bool rust_helper_WARN_ON(bool cond)
+{
+ return WARN_ON(cond);
+}
diff --git a/rust/kernel/.gitignore b/rust/kernel/.gitignore
index 6ba39a178f30..f636ad95aaf3 100644
--- a/rust/kernel/.gitignore
+++ b/rust/kernel/.gitignore
@@ -1,3 +1,5 @@
# SPDX-License-Identifier: GPL-2.0
/generated_arch_static_branch_asm.rs
+/generated_arch_warn_asm.rs
+/generated_arch_reachable_asm.rs
diff --git a/rust/kernel/bug.rs b/rust/kernel/bug.rs
new file mode 100644
index 000000000000..1191391eb2cb
--- /dev/null
+++ b/rust/kernel/bug.rs
@@ -0,0 +1,125 @@
+// SPDX-License-Identifier: GPL-2.0
+
+// Copyright (C) 2024, 2025 FUJITA Tomonori <fujita.tomonori@gmail.com>
+
+//! Support for BUG and WARN functionality.
+//!
+//! C header: [`include/asm-generic/bug.h`](srctree/include/asm-generic/bug.h)
+
+#[macro_export]
+#[doc(hidden)]
+#[cfg(all(CONFIG_BUG, not(CONFIG_UML), not(CONFIG_LOONGARCH), not(CONFIG_ARM)))]
+#[cfg(CONFIG_DEBUG_BUGVERBOSE)]
+macro_rules! warn_flags {
+ ($flags:expr) => {
+ const FLAGS: u32 = $crate::bindings::BUGFLAG_WARNING | $flags;
+ const _FILE: &[u8] = file!().as_bytes();
+ // Plus one for null-terminator.
+ static FILE: [u8; _FILE.len() + 1] = {
+ let mut bytes = [0; _FILE.len() + 1];
+ let mut i = 0;
+ while i < _FILE.len() {
+ bytes[i] = _FILE[i];
+ i += 1;
+ }
+ bytes
+ };
+
+ // SAFETY:
+ // - `file`, `line`, `flags`, and `size` are all compile-time constants or
+ // symbols, preventing any invalid memory access.
+ // - The asm block has no side effects and does not modify any registers
+ // or memory. It is purely for embedding metadata into the ELF section.
+ unsafe {
+ $crate::asm!(
+ concat!(
+ "/* {size} */",
+ include!(concat!(env!("OBJTREE"), "/rust/kernel/generated_arch_warn_asm.rs")),
+ include!(concat!(env!("OBJTREE"), "/rust/kernel/generated_arch_reachable_asm.rs")));
+ file = sym FILE,
+ line = const line!(),
+ flags = const FLAGS,
+ size = const ::core::mem::size_of::<$crate::bindings::bug_entry>(),
+ );
+ }
+ }
+}
+
+#[macro_export]
+#[doc(hidden)]
+#[cfg(all(CONFIG_BUG, not(CONFIG_UML), not(CONFIG_LOONGARCH), not(CONFIG_ARM)))]
+#[cfg(not(CONFIG_DEBUG_BUGVERBOSE))]
+macro_rules! warn_flags {
+ ($flags:expr) => {
+ const FLAGS: u32 = $crate::bindings::BUGFLAG_WARNING | $flags;
+
+ // SAFETY:
+ // - `flags` and `size` are all compile-time constants, preventing
+ // any invalid memory access.
+ // - The asm block has no side effects and does not modify any registers
+ // or memory. It is purely for embedding metadata into the ELF section.
+ unsafe {
+ $crate::asm!(
+ concat!(
+ "/* {size} */",
+ include!(concat!(env!("OBJTREE"), "/rust/kernel/generated_arch_warn_asm.rs")),
+ include!(concat!(env!("OBJTREE"), "/rust/kernel/generated_arch_reachable_asm.rs")));
+ flags = const FLAGS,
+ size = const ::core::mem::size_of::<$crate::bindings::bug_entry>(),
+ );
+ }
+ }
+}
+
+#[macro_export]
+#[doc(hidden)]
+#[cfg(all(CONFIG_BUG, CONFIG_UML))]
+macro_rules! warn_flags {
+ ($flags:expr) => {
+ // SAFETY: It is always safe to call `warn_slowpath_fmt()`
+ // with a valid null-terminated string.
+ unsafe {
+ $crate::bindings::warn_slowpath_fmt(
+ $crate::c_str!(::core::file!()).as_ptr() as *const $crate::ffi::c_char,
+ line!() as i32,
+ $flags as u32,
+ ::core::ptr::null() as *const $crate::ffi::c_char,
+ );
+ }
+ };
+}
+
+#[macro_export]
+#[doc(hidden)]
+#[cfg(all(CONFIG_BUG, any(CONFIG_LOONGARCH, CONFIG_ARM)))]
+macro_rules! warn_flags {
+ ($flags:expr) => {
+ // SAFETY: It is always safe to call `WARN_ON()`.
+ unsafe { $crate::bindings::WARN_ON(true) }
+ };
+}
+
+#[macro_export]
+#[doc(hidden)]
+#[cfg(not(CONFIG_BUG))]
+macro_rules! warn_flags {
+ ($flags:expr) => {};
+}
+
+#[doc(hidden)]
+pub const fn bugflag_taint(value: u32) -> u32 {
+ value << 8
+}
+
+/// Report a warning if `cond` is true and return the condition's evaluation result.
+#[macro_export]
+macro_rules! warn_on {
+ ($cond:expr) => {{
+ if $cond {
+ const WARN_ON_FLAGS: u32 = $crate::bug::bugflag_taint($crate::bindings::TAINT_WARN);
+
+ $crate::warn_flags!(WARN_ON_FLAGS);
+ }
+ $cond
+ }};
+}
diff --git a/rust/kernel/generated_arch_reachable_asm.rs.S b/rust/kernel/generated_arch_reachable_asm.rs.S
new file mode 100644
index 000000000000..3886a9ad3a99
--- /dev/null
+++ b/rust/kernel/generated_arch_reachable_asm.rs.S
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#include <linux/bug.h>
+
+// Cut here.
+
+::kernel::concat_literals!(ARCH_WARN_REACHABLE)
diff --git a/rust/kernel/generated_arch_warn_asm.rs.S b/rust/kernel/generated_arch_warn_asm.rs.S
new file mode 100644
index 000000000000..409eb4c2d3a1
--- /dev/null
+++ b/rust/kernel/generated_arch_warn_asm.rs.S
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#include <linux/bug.h>
+
+// Cut here.
+
+::kernel::concat_literals!(ARCH_WARN_ASM("{file}", "{line}", "{flags}", "{size}"))
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index de07aadd1ff5..bc13e92bdb1e 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -40,6 +40,7 @@
pub mod alloc;
#[cfg(CONFIG_BLOCK)]
pub mod block;
+pub mod bug;
#[doc(hidden)]
pub mod build_assert;
pub mod cred;
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH v6 4/4] rust: Add warn_on macro
2025-05-02 9:45 ` [PATCH v6 4/4] rust: Add warn_on macro FUJITA Tomonori
@ 2025-05-02 11:17 ` Alice Ryhl
0 siblings, 0 replies; 12+ messages in thread
From: Alice Ryhl @ 2025-05-02 11:17 UTC (permalink / raw)
To: FUJITA Tomonori
Cc: linux-kernel, rust-for-linux, x86, linux-riscv, linux-arm-kernel,
loongarch, tglx, mingo, bp, dave.hansen, peterz, hpa,
paul.walmsley, palmer, aou, catalin.marinas, will, chenhuacai,
kernel, tangyouling, hejinyang, yangtiezhu, ojeda, alex.gaynor,
boqun.feng, gary, bjorn3_gh, benno.lossin, a.hindborg, tmgross,
dakr
On Fri, May 02, 2025 at 06:45:36PM +0900, FUJITA Tomonori wrote:
> Add warn_on macro, uses the BUG/WARN feature (lib/bug.c) via assembly
> for x86_64/arm64/riscv.
>
> The current Rust code simply wraps BUG() macro but doesn't provide the
> proper debug information. The BUG/WARN feature can only be used from
> assembly.
>
> This uses the assembly code exported by the C side via ARCH_WARN_ASM
> macro. To avoid duplicating the assembly code, this approach follows
> the same strategy as the static branch code: it generates the assembly
> code for Rust using the C preprocessor at compile time.
>
> Similarly, ARCH_WARN_REACHABLE is also used at compile time to
> generate the assembly code; objtool's reachable annotation code. It's
> used for only architectures that use objtool.
>
> For now, Loongarch and arm just use a wrapper for WARN macro.
>
> UML doesn't use the assembly BUG/WARN feature; just wrapping generic
> BUG/WARN functions implemented in C works.
>
> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v6 0/4] rust: Add bug/warn abstractions
2025-05-02 9:45 [PATCH v6 0/4] rust: Add bug/warn abstractions FUJITA Tomonori
` (3 preceding siblings ...)
2025-05-02 9:45 ` [PATCH v6 4/4] rust: Add warn_on macro FUJITA Tomonori
@ 2025-06-05 1:56 ` Miguel Ojeda
2025-06-05 7:20 ` FUJITA Tomonori
2025-07-23 0:48 ` Miguel Ojeda
5 siblings, 1 reply; 12+ messages in thread
From: Miguel Ojeda @ 2025-06-05 1:56 UTC (permalink / raw)
To: FUJITA Tomonori
Cc: linux-kernel, rust-for-linux, x86, linux-riscv, linux-arm-kernel,
loongarch, tglx, mingo, bp, dave.hansen, peterz, hpa,
paul.walmsley, palmer, aou, catalin.marinas, will, chenhuacai,
kernel, tangyouling, hejinyang, yangtiezhu, ojeda, alex.gaynor,
boqun.feng, gary, bjorn3_gh, benno.lossin, a.hindborg, aliceryhl,
tmgross, dakr
On Fri, May 2, 2025 at 11:46 AM FUJITA Tomonori
<fujita.tomonori@gmail.com> wrote:
>
> This patchset adds warn_on macro with the bug/warn abstraction that
> utilizes the kernel's BUG/WARN feature via assembly.
This passed my usual tests -- I will take a better look and pick it
early next cycle.
Cheers,
Miguel
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v6 0/4] rust: Add bug/warn abstractions
2025-06-05 1:56 ` [PATCH v6 0/4] rust: Add bug/warn abstractions Miguel Ojeda
@ 2025-06-05 7:20 ` FUJITA Tomonori
0 siblings, 0 replies; 12+ messages in thread
From: FUJITA Tomonori @ 2025-06-05 7:20 UTC (permalink / raw)
To: miguel.ojeda.sandonis
Cc: fujita.tomonori, linux-kernel, rust-for-linux, x86, linux-riscv,
linux-arm-kernel, loongarch, tglx, mingo, bp, dave.hansen, peterz,
hpa, paul.walmsley, palmer, aou, catalin.marinas, will,
chenhuacai, kernel, tangyouling, hejinyang, yangtiezhu, ojeda,
alex.gaynor, boqun.feng, gary, bjorn3_gh, benno.lossin,
a.hindborg, aliceryhl, tmgross, dakr
On Thu, 5 Jun 2025 03:56:31 +0200
Miguel Ojeda <miguel.ojeda.sandonis@gmail.com> wrote:
> On Fri, May 2, 2025 at 11:46 AM FUJITA Tomonori
> <fujita.tomonori@gmail.com> wrote:
>>
>> This patchset adds warn_on macro with the bug/warn abstraction that
>> utilizes the kernel's BUG/WARN feature via assembly.
>
> This passed my usual tests -- I will take a better look and pick it
> early next cycle.
Perfect, thank you so much!
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v6 0/4] rust: Add bug/warn abstractions
2025-05-02 9:45 [PATCH v6 0/4] rust: Add bug/warn abstractions FUJITA Tomonori
` (4 preceding siblings ...)
2025-06-05 1:56 ` [PATCH v6 0/4] rust: Add bug/warn abstractions Miguel Ojeda
@ 2025-07-23 0:48 ` Miguel Ojeda
2025-07-23 6:20 ` FUJITA Tomonori
5 siblings, 1 reply; 12+ messages in thread
From: Miguel Ojeda @ 2025-07-23 0:48 UTC (permalink / raw)
To: FUJITA Tomonori
Cc: linux-kernel, rust-for-linux, x86, linux-riscv, linux-arm-kernel,
loongarch, tglx, mingo, bp, dave.hansen, peterz, hpa,
paul.walmsley, palmer, aou, catalin.marinas, will, chenhuacai,
kernel, tangyouling, hejinyang, yangtiezhu, ojeda, alex.gaynor,
boqun.feng, gary, bjorn3_gh, benno.lossin, a.hindborg, aliceryhl,
tmgross, dakr
On Fri, May 2, 2025 at 11:46 AM FUJITA Tomonori
<fujita.tomonori@gmail.com> wrote:
>
> This patchset adds warn_on macro with the bug/warn abstraction that
> utilizes the kernel's BUG/WARN feature via assembly.
Ok, let's move forward with this.
I took a look at the first three patches and compared the expanded
output for the existing macros, to make sure we (hopefully) don't
break anything else.
It seems OK, I only noticed a removed newline. Was that intentional?
There was also a bad parameter name, but that was not hurting anything
since it was unused.
(For the x86 one could be closer removing a couple spaces, but it
should not matter and other x86 files format it that way, so I didn't
change it. I also noticed unexpected spaces used for aligning the
macro, but it turned out it was in the original already, so I left it
also unchanged.)
Then I found a few more bits on the last patch.
Tomo, could you please double-check you are OK with all the changes,
and please run the tests you did back then on `rust-next` again for
all arches, given it has been a while since you posted it (plus I did
a few changes on top, after all)? I would appreciate it, thanks in
advance!
To be clear, I didn't re-check every single thing/combination, but
hopefully what I caught helps. Since there are no users anyway (of the
last patch) right now, it should be fairly safe.
Applied to `rust-next` -- thanks everyone!
[ Fixed typo in macro parameter name. - Miguel ]
[ Remove ending newline in `ARCH_WARN_ASM` content to be closer to the
original. - Miguel ]
[ Avoid evaluating the condition twice (a good idea in general,
but it also matches the C side). Simplify with `as_char_ptr()`
to avoid a cast. Cast to `ffi` integer types for
`warn_slowpath_fmt`. Avoid cast for `null()`. - Miguel ]
Cheers,
Miguel
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v6 0/4] rust: Add bug/warn abstractions
2025-07-23 0:48 ` Miguel Ojeda
@ 2025-07-23 6:20 ` FUJITA Tomonori
2025-07-23 9:52 ` Miguel Ojeda
0 siblings, 1 reply; 12+ messages in thread
From: FUJITA Tomonori @ 2025-07-23 6:20 UTC (permalink / raw)
To: miguel.ojeda.sandonis
Cc: fujita.tomonori, linux-kernel, rust-for-linux, x86, linux-riscv,
linux-arm-kernel, loongarch, tglx, mingo, bp, dave.hansen, peterz,
hpa, paul.walmsley, palmer, aou, catalin.marinas, will,
chenhuacai, kernel, tangyouling, hejinyang, yangtiezhu, ojeda,
alex.gaynor, boqun.feng, gary, bjorn3_gh, benno.lossin,
a.hindborg, aliceryhl, tmgross, dakr
On Wed, 23 Jul 2025 02:48:20 +0200
Miguel Ojeda <miguel.ojeda.sandonis@gmail.com> wrote:
>> This patchset adds warn_on macro with the bug/warn abstraction that
>> utilizes the kernel's BUG/WARN feature via assembly.
>
> Ok, let's move forward with this.
>
> I took a look at the first three patches and compared the expanded
> output for the existing macros, to make sure we (hopefully) don't
> break anything else.
Thanks a lot!
> It seems OK, I only noticed a removed newline. Was that intentional?
The patch for arm64? I tried to minimize changes to the original code
so not intentional.
> There was also a bad parameter name, but that was not hurting anything
> since it was unused.
>
> (For the x86 one could be closer removing a couple spaces, but it
> should not matter and other x86 files format it that way, so I didn't
> change it. I also noticed unexpected spaces used for aligning the
> macro, but it turned out it was in the original already, so I left it
> also unchanged.)
Yeah, I followed the original code style.
> Then I found a few more bits on the last patch.
>
> Tomo, could you please double-check you are OK with all the changes,
> and please run the tests you did back then on `rust-next` again for
> all arches, given it has been a while since you posted it (plus I did
> a few changes on top, after all)? I would appreciate it, thanks in
> advance!
I've just tested rust-next. Looks like all arches (x86, riscv, and
arm64) works as expected.
> To be clear, I didn't re-check every single thing/combination, but
> hopefully what I caught helps. Since there are no users anyway (of the
> last patch) right now, it should be fairly safe.
>
> Applied to `rust-next` -- thanks everyone!
>
> [ Fixed typo in macro parameter name. - Miguel ]
>
> [ Remove ending newline in `ARCH_WARN_ASM` content to be closer to the
> original. - Miguel ]
>
> [ Avoid evaluating the condition twice (a good idea in general,
> but it also matches the C side). Simplify with `as_char_ptr()`
> to avoid a cast. Cast to `ffi` integer types for
> `warn_slowpath_fmt`. Avoid cast for `null()`. - Miguel ]
>
All the changes look good.
Thanks!
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v6 0/4] rust: Add bug/warn abstractions
2025-07-23 6:20 ` FUJITA Tomonori
@ 2025-07-23 9:52 ` Miguel Ojeda
2025-07-23 13:23 ` FUJITA Tomonori
0 siblings, 1 reply; 12+ messages in thread
From: Miguel Ojeda @ 2025-07-23 9:52 UTC (permalink / raw)
To: FUJITA Tomonori
Cc: linux-kernel, rust-for-linux, x86, linux-riscv, linux-arm-kernel,
loongarch, tglx, mingo, bp, dave.hansen, peterz, hpa,
paul.walmsley, palmer, aou, catalin.marinas, will, chenhuacai,
kernel, tangyouling, hejinyang, yangtiezhu, ojeda, alex.gaynor,
boqun.feng, gary, bjorn3_gh, benno.lossin, a.hindborg, aliceryhl,
tmgross, dakr
On Wed, Jul 23, 2025 at 8:21 AM FUJITA Tomonori
<fujita.tomonori@gmail.com> wrote:
>
> The patch for arm64? I tried to minimize changes to the original code
> so not intentional.
The riscv one, i.e. from the range diff:
@@ arch/riscv/include/asm/bug.h: typedef u32 bug_insn_t;
"2:\n\t" \
- __BUG_ENTRY "\n\t" \
- ".org 2b + %3\n\t" \
-- ".popsection" \
+ __BUG_ENTRY(file, line, flags) "\n\t" \
+ ".org 2b + " size "\n\t" \
-+ ".popsection\n" \
+ ".popsection" \
+
+#define __BUG_FLAGS(flags) \
+do { \
If you look into the `.popsection` line, your patch adds a newline,
but I guess it was not intentional. In x86 there is a newline after
the directive, but there we have `extra` afterwards.
(I noticed since I went to expand the macros in a dummy file, given it
can be something tricky to spot differences in the normal diff, but it
is visible in the normal diff.)
> Yeah, I followed the original code style.
And here I meant (i.e. the first part of that paragraph) that:
- "\t.org 2b+%c1\n" \
now generated those two spaces around it:
+ "\t.org 2b + " size "\n" \
> I've just tested rust-next. Looks like all arches (x86, riscv, and
> arm64) works as expected.
Thanks a lot!
Cheers,
Miguel
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v6 0/4] rust: Add bug/warn abstractions
2025-07-23 9:52 ` Miguel Ojeda
@ 2025-07-23 13:23 ` FUJITA Tomonori
0 siblings, 0 replies; 12+ messages in thread
From: FUJITA Tomonori @ 2025-07-23 13:23 UTC (permalink / raw)
To: miguel.ojeda.sandonis
Cc: fujita.tomonori, linux-kernel, rust-for-linux, x86, linux-riscv,
linux-arm-kernel, loongarch, tglx, mingo, bp, dave.hansen, peterz,
hpa, paul.walmsley, palmer, aou, catalin.marinas, will,
chenhuacai, kernel, tangyouling, hejinyang, yangtiezhu, ojeda,
alex.gaynor, boqun.feng, gary, bjorn3_gh, benno.lossin,
a.hindborg, aliceryhl, tmgross, dakr
On Wed, 23 Jul 2025 11:52:04 +0200
Miguel Ojeda <miguel.ojeda.sandonis@gmail.com> wrote:
>> The patch for arm64? I tried to minimize changes to the original code
>> so not intentional.
>
> The riscv one, i.e. from the range diff:
>
> @@ arch/riscv/include/asm/bug.h: typedef u32 bug_insn_t;
> "2:\n\t" \
> - __BUG_ENTRY "\n\t" \
> - ".org 2b + %3\n\t" \
> -- ".popsection" \
> + __BUG_ENTRY(file, line, flags) "\n\t" \
> + ".org 2b + " size "\n\t" \
> -+ ".popsection\n" \
> + ".popsection" \
> +
> +#define __BUG_FLAGS(flags) \
> +do { \
>
> If you look into the `.popsection` line, your patch adds a newline,
> but I guess it was not intentional. In x86 there is a newline after
> the directive, but there we have `extra` afterwards.
Oops, indeed it's not intentional.
> (I noticed since I went to expand the macros in a dummy file, given it
> can be something tricky to spot differences in the normal diff, but it
> is visible in the normal diff.)
>
>> Yeah, I followed the original code style.
>
> And here I meant (i.e. the first part of that paragraph) that:
>
> - "\t.org 2b+%c1\n" \
>
> now generated those two spaces around it:
>
> + "\t.org 2b + " size "\n" \
>
Ah, not intentional again.
^ permalink raw reply [flat|nested] 12+ messages in thread