[PATCH v4 0/4] rust: Add bug/warn abstractions
From: FUJITA Tomonori <fujita.tomonori@gmail.com>
Date: 2025-03-05 11:10:08
Also in:
linux-riscv, lkml, loongarch, rust-for-linux
This patchset adds warn_on macro with the bug/warn abstraction that utilizes the kernel's BUG/WARN feature via assembly. Currently, Rust code simply wraps BUG() macro; however the approach doesn't provide the proper debug information. For example, I added bindings::BUG() to rnull's init method and got the following output: # insmod /root/rnull_mod.ko rnull_mod: Rust null_blk loaded ------------[ cut here ]------------ kernel BUG at rust/helpers/bug.c:7! Oops: invalid opcode: 0000 [#1] SMP CPU: 0 UID: 0 PID: 31 Comm: insmod Not tainted 6.14.0-rc1+ #103 RIP: 0010:rust_helper_BUG+0x8/0x10 (snip) The kernel's BUG/WARN feature (lib/bug.c) can only be used from assembly. Rust code needs to directly execute the same assembly code used on the C side. 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. The 1st to 3th patches export the BUG/WARN assembly code for Rust on x86, RISC-V, and ARM64 architecture, with no functional changes on the C side. They have already been acked by the maintainers of their respective architectures. The change for LoongArch assembly hasn't been acked yet so the v4 doesnt' include it. LoongArch still uses the current approach; just wrapping C's macro. UML doesn't use the assembly BUG/WARN feature; just wrapping generic BUG/WARN functions implemented in C works. The last patch adds warn_on implementation on the top of the abstraction. To make the patchset easier to review, the remaining features such as bug() are not included in this patchset. These features will be added after this patchset is merged. This has been tested on x86, ARM64, and RISC-V (QEMU), with only a compile test performed for LoongArch and UML. v4: - added Acked-by tag to the RISC-V and ARM64 asm change - simplify the asm code - use the cfgs on the macro rather in its expansion - use a const fn for bugflag_taint over macro - dropped LoongArch assembly change - dropped warn_on_once; make the patch easier to review v3: https://lore.kernel.org/rust-for-linux/20250213135759.190006-1-fujita.tomonori@gmail.com/ (local) - rebased on rust-next - use ANNOTATE_REACHABLE macro (replaced ASM_REACHABLE) - added Acked-by tag to the x86 change v2: https://lore.kernel.org/linux-arm-kernel/20241218062009.2402650-1-fujita.tomonori@gmail.com/ (local) - remove target_arch cfg by using asm comment - clean up the changes to loongarch asm v1: https://lore.kernel.org/linux-arm-kernel/20241210001802.228725-1-fujita.tomonori@gmail.com/ (local) FUJITA Tomonori (4): x86/bug: Add ARCH_WARN_ASM macro for BUG/WARN asm code sharing with Rust riscv/bug: Add ARCH_WARN_ASM macro for BUG/WARN asm code sharing with Rust arm64/bug: Add ARCH_WARN_ASM macro for BUG/WARN asm code sharing with Rust rust: Add warn_on macro arch/arm64/include/asm/asm-bug.h | 33 ++++- arch/riscv/include/asm/bug.h | 37 +++--- arch/x86/include/asm/bug.h | 56 ++++----- rust/Makefile | 8 ++ rust/helpers/bug.c | 5 + rust/kernel/.gitignore | 2 + rust/kernel/bug.rs | 113 ++++++++++++++++++ rust/kernel/generated_arch_reachable_asm.rs.S | 7 ++ rust/kernel/generated_arch_warn_asm.rs.S | 7 ++ rust/kernel/lib.rs | 1 + 10 files changed, 222 insertions(+), 47 deletions(-) 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 base-commit: beeb78d46249cab8b2b8359a2ce8fa5376b5ad2d -- 2.43.0