* [PATCH v2] rust: bug: Add bug macro
@ 2026-07-09 0:01 FUJITA Tomonori
2026-07-09 7:16 ` kernel test robot
0 siblings, 1 reply; 2+ messages in thread
From: FUJITA Tomonori @ 2026-07-09 0:01 UTC (permalink / raw)
To: ojeda
Cc: gregkh, a.hindborg, acourbot, aliceryhl, bjorn3_gh, boqun, dakr,
daniel.almeida, gary, lossin, tamird, tmgross, work,
rust-for-linux, FUJITA Tomonori
From: FUJITA Tomonori <fujita.tomonori@gmail.com>
Add bug!(), the Rust equivalent of C's BUG(). Like warn_on!(), it
emits the BUG/WARN metadata (lib/bug.c) directly via inline assembly
on x86_64/arm64/riscv/s390. This differs from bindings::BUG(), which
reaches the same C BUG() macro indirectly through a C helper
(rust/helpers/bug.c).
bindings::BUG() is already in use today, indirectly, from the kernel
crate itself and from drivers such as binder and nova-core, via
panic!()/.expect()/.unwrap() on invariants that "can't happen". All of
those funnel through panic() in kernel/lib.rs (#[panic_handler]),
which itself calls bindings::BUG().
The motivation for this patch is narrow: replace that bindings::BUG()
call in the panic() function in lib.rs with bug!(), for a somewhat
better trace. It isn't adding a new decision to crash the system; that
decision is already made at each of the existing panic!()/.expect()
call sites.
The bug!() macro is currently for internal use only, so it is marked
`#[doc(hidden)]`.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
v2
- Replace bindings::BUG in lib.rs with bug!() to make the motivation clear
- Add #[doc(hidden)] to make clear that bug!() is for internal use only
- Add the bug!() macro, rather than bug_on!(), as the straightforward
choice for replacing bindings::BUG.
v1: https://lore.kernel.org/rust-for-linux/20260707143026.2774023-1-tomo@aliasing.net/
---
rust/kernel/bug.rs | 98 ++++++++++++++++++++++++++++++++--------------
rust/kernel/lib.rs | 3 +-
2 files changed, 70 insertions(+), 31 deletions(-)
diff --git a/rust/kernel/bug.rs b/rust/kernel/bug.rs
index ed943960f851..c7bd019508f7 100644
--- a/rust/kernel/bug.rs
+++ b/rust/kernel/bug.rs
@@ -11,9 +11,13 @@
#[cfg(all(CONFIG_BUG, not(CONFIG_UML), not(CONFIG_LOONGARCH), not(CONFIG_ARM)))]
#[cfg(CONFIG_DEBUG_BUGVERBOSE)]
macro_rules! warn_flags {
- ($file:expr, $flags:expr) => {
- const FLAGS: u32 = $crate::bindings::BUGFLAG_WARNING | $flags;
- const _FILE: &[u8] = $file.as_bytes();
+ ($cond:expr, $flags:expr, $asm_tail:expr) => {{
+ #[cfg(CONFIG_DEBUG_BUGVERBOSE_DETAILED)]
+ const _COND_STR: &str = concat!("[", stringify!($cond), "] ", file!());
+ #[cfg(not(CONFIG_DEBUG_BUGVERBOSE_DETAILED))]
+ const _COND_STR: &str = file!();
+
+ const _FILE: &[u8] = _COND_STR.as_bytes();
// Plus one for null-terminator.
static FILE: [u8; _FILE.len() + 1] = {
let mut bytes = [0; _FILE.len() + 1];
@@ -24,6 +28,7 @@ macro_rules! warn_flags {
}
bytes
};
+ const FLAGS: u32 = $flags;
// SAFETY:
// - `file`, `line`, `flags`, and `size` are all compile-time constants or
@@ -35,14 +40,14 @@ macro_rules! warn_flags {
concat!(
"/* {size} */",
include!(concat!(env!("OBJTREE"), "/rust/kernel/generated_arch_warn_asm.rs")),
- include!(concat!(env!("OBJTREE"), "/rust/kernel/generated_arch_reachable_asm.rs")));
+ $asm_tail);
file = sym FILE,
line = const line!(),
flags = const FLAGS,
size = const ::core::mem::size_of::<$crate::bindings::bug_entry>(),
);
}
- }
+ }};
}
#[macro_export]
@@ -50,8 +55,8 @@ macro_rules! warn_flags {
#[cfg(all(CONFIG_BUG, not(CONFIG_UML), not(CONFIG_LOONGARCH), not(CONFIG_ARM)))]
#[cfg(not(CONFIG_DEBUG_BUGVERBOSE))]
macro_rules! warn_flags {
- ($file:expr, $flags:expr) => {
- const FLAGS: u32 = $crate::bindings::BUGFLAG_WARNING | $flags;
+ ($cond:expr, $flags:expr, $asm_tail:expr) => {
+ const FLAGS: u32 = $flags;
// SAFETY:
// - `flags` and `size` are all compile-time constants, preventing
@@ -63,7 +68,7 @@ macro_rules! warn_flags {
concat!(
"/* {size} */",
include!(concat!(env!("OBJTREE"), "/rust/kernel/generated_arch_warn_asm.rs")),
- include!(concat!(env!("OBJTREE"), "/rust/kernel/generated_arch_reachable_asm.rs")));
+ $asm_tail);
flags = const FLAGS,
size = const ::core::mem::size_of::<$crate::bindings::bug_entry>(),
);
@@ -75,16 +80,23 @@ macro_rules! warn_flags {
#[doc(hidden)]
#[cfg(all(CONFIG_BUG, CONFIG_UML))]
macro_rules! warn_flags {
- ($file:expr, $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_char_ptr(),
- line!() as $crate::ffi::c_int,
- $flags as $crate::ffi::c_uint,
- ::core::ptr::null(),
- );
+ ($cond:expr, $flags:expr, $asm_tail:expr) => {
+ // bug_on!() sets $flags to 0, so we can use that to determine
+ // whether to call BUG() or WARN_ON().
+ if $flags == 0 {
+ // SAFETY: It is always safe to call `BUG()`.
+ unsafe { $crate::bindings::BUG() }
+ } else {
+ // 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_char_ptr(),
+ line!() as $crate::ffi::c_int,
+ $flags as $crate::ffi::c_uint,
+ ::core::ptr::null(),
+ );
+ }
}
};
}
@@ -93,9 +105,16 @@ macro_rules! warn_flags {
#[doc(hidden)]
#[cfg(all(CONFIG_BUG, any(CONFIG_LOONGARCH, CONFIG_ARM)))]
macro_rules! warn_flags {
- ($file:expr, $flags:expr) => {
- // SAFETY: It is always safe to call `WARN_ON()`.
- unsafe { $crate::bindings::WARN_ON(true) }
+ ($cond:expr, $flags:expr, $asm_tail:expr) => {
+ // bug_on!() sets $flags to 0, so we can use that to determine
+ // whether to call BUG() or WARN_ON().
+ if $flags == 0 {
+ // SAFETY: It is always safe to call `BUG()`.
+ unsafe { $crate::bindings::BUG() }
+ } else {
+ // SAFETY: It is always safe to call `WARN_ON()`.
+ unsafe { $crate::bindings::WARN_ON(true) }
+ }
};
}
@@ -103,7 +122,11 @@ macro_rules! warn_flags {
#[doc(hidden)]
#[cfg(not(CONFIG_BUG))]
macro_rules! warn_flags {
- ($file:expr, $flags:expr) => {};
+ ($cond:expr, $flags:expr, $asm_tail:expr) => {
+ if $flags == 0 {
+ loop {}
+ }
+ };
}
#[doc(hidden)]
@@ -117,16 +140,33 @@ macro_rules! warn_on {
($cond:expr) => {{
let cond = $cond;
- #[cfg(CONFIG_DEBUG_BUGVERBOSE_DETAILED)]
- const _COND_STR: &str = concat!("[", stringify!($cond), "] ", file!());
- #[cfg(not(CONFIG_DEBUG_BUGVERBOSE_DETAILED))]
- const _COND_STR: &str = file!();
-
if cond {
- const WARN_ON_FLAGS: u32 = $crate::bug::bugflag_taint($crate::bindings::TAINT_WARN);
+ #[cfg(any(not(CONFIG_BUG), CONFIG_UML))]
+ const WARN_ON_FLAGS: u32 = $crate::bindings::TAINT_WARN;
+ #[cfg(all(CONFIG_BUG, not(CONFIG_UML)))]
+ const WARN_ON_FLAGS: u32 = $crate::bindings::BUGFLAG_WARNING
+ | $crate::bug::bugflag_taint($crate::bindings::TAINT_WARN);
- $crate::warn_flags!(_COND_STR, WARN_ON_FLAGS);
+ $crate::warn_flags!(
+ $cond,
+ WARN_ON_FLAGS,
+ include!(concat!(
+ env!("OBJTREE"),
+ "/rust/kernel/generated_arch_reachable_asm.rs"
+ ))
+ );
}
cond
}};
}
+
+/// Reports an unrecoverable kernel bug.
+#[macro_export]
+#[doc(hidden)]
+macro_rules! bug {
+ () => {
+ $crate::warn_flags!(true, 0, "");
+ // SAFETY: We call `BUG()` above so this block will never be reached.
+ unsafe { core::hint::unreachable_unchecked() }
+ };
+}
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index 9512af7156df..41bfe5a20287 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -217,8 +217,7 @@ pub const fn as_ptr(&self) -> *mut bindings::module {
#[panic_handler]
fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
pr_emerg!("{}\n", info);
- // SAFETY: FFI call.
- unsafe { bindings::BUG() };
+ crate::bug!();
}
/// Produces a pointer to an object from a pointer to one of its fields.
base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v2] rust: bug: Add bug macro
2026-07-09 0:01 [PATCH v2] rust: bug: Add bug macro FUJITA Tomonori
@ 2026-07-09 7:16 ` kernel test robot
0 siblings, 0 replies; 2+ messages in thread
From: kernel test robot @ 2026-07-09 7:16 UTC (permalink / raw)
To: FUJITA Tomonori, ojeda
Cc: llvm, oe-kbuild-all, gregkh, a.hindborg, acourbot, aliceryhl,
bjorn3_gh, boqun, dakr, daniel.almeida, gary, lossin, tamird,
tmgross, work, rust-for-linux, FUJITA Tomonori
Hi FUJITA,
kernel test robot noticed the following build errors:
[auto build test ERROR on dc59e4fea9d83f03bad6bddf3fa2e52491777482]
url: https://github.com/intel-lab-lkp/linux/commits/FUJITA-Tomonori/rust-bug-Add-bug-macro/20260709-080347
base: dc59e4fea9d83f03bad6bddf3fa2e52491777482
patch link: https://lore.kernel.org/r/20260709000103.2912159-1-tomo%40aliasing.net
patch subject: [PATCH v2] rust: bug: Add bug macro
config: um-allmodconfig (https://download.01.org/0day-ci/archive/20260709/202607091512.wQmfg8U1-lkp@intel.com/config)
compiler: clang version 17.0.6 (https://github.com/llvm/llvm-project 6009708b4367171ccdbf4b5905cb6a803753fe18)
rustc: rustc 1.96.0 (ac68faa20 2026-05-25)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260709/202607091512.wQmfg8U1-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202607091512.wQmfg8U1-lkp@intel.com/
All errors (new ones prefixed by >>):
In file included from kernel/sched/rq-offsets.c:5:
In file included from kernel/sched/sched.h:28:
In file included from include/linux/cgroup_api.h:1:
In file included from include/linux/cgroup.h:27:
In file included from include/linux/kernel_stat.h:8:
In file included from include/linux/interrupt.h:11:
In file included from include/linux/hardirq.h:11:
In file included from arch/um/include/asm/hardirq.h:24:
In file included from include/linux/irq.h:20:
In file included from include/linux/io.h:12:
In file included from arch/um/include/asm/io.h:24:
include/asm-generic/io.h:1209:55: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
1209 | return (port > MMIO_UPPER_LIMIT) ? NULL : PCI_IOBASE + port;
| ~~~~~~~~~~ ^
1 warning generated.
In file included from rust/helpers/helpers.c:49:
In file included from rust/helpers/blk.c:3:
In file included from include/linux/blk-mq.h:5:
In file included from include/linux/blkdev.h:9:
In file included from include/linux/blk_types.h:10:
In file included from include/linux/bvec.h:10:
In file included from include/linux/highmem.h:12:
In file included from include/linux/hardirq.h:11:
In file included from arch/um/include/asm/hardirq.h:24:
In file included from include/linux/irq.h:20:
In file included from include/linux/io.h:12:
In file included from arch/um/include/asm/io.h:24:
include/asm-generic/io.h:1209:55: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
1209 | return (port > MMIO_UPPER_LIMIT) ? NULL : PCI_IOBASE + port;
| ~~~~~~~~~~ ^
clang diag: include/asm-generic/io.h:1209:55: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
clang diag: include/asm-generic/io.h:1209:55: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
clang diag: include/asm-generic/io.h:1209:55: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
1 warning generated.
>> error[E0599]: no method named `as_char_ptr` found for reference `&ffi::CStr` in the current scope
--> rust/kernel/bug.rs:94:53
|
94 | $crate::c_str!(::core::file!()).as_char_ptr(),
| ^^^^^^^^^^^
|
::: rust/kernel/lib.rs:220:5
|
220 | crate::bug!();
| ------------- in this macro invocation
|
= help: items from traits can only be used if the trait is in scope
= note: this error originates in the macro `$crate::warn_flags` which comes from the expansion of the macro `crate::bug` (in Nightly builds, run with -Z macro-backtrace for more info)
help: trait `CStrExt` which provides `as_char_ptr` is implemented but not in scope; perhaps you want to import it
--> rust/kernel/lib.rs:35:1
|
35 + use crate::str::CStrExt;
|
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-07-09 7:17 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-09 0:01 [PATCH v2] rust: bug: Add bug macro FUJITA Tomonori
2026-07-09 7:16 ` kernel test robot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox