* [PATCH v4] rust: bug: Add bug macro
@ 2026-07-13 23:29 FUJITA Tomonori
0 siblings, 0 replies; only message in thread
From: FUJITA Tomonori @ 2026-07-13 23:29 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 macro(), the Rust equivalent of the C's BUG(), which uses
the BUG/WARN feature (lib/bug.c) via assembly for
x86_64/arm64/riscv/s390 like warn_on() macro.
bindings::BUG() is already in use today, indirectly, from the kernel
crate itself and from drivers such as binder and nova-core, via
panic!()/.exepct()/.unwrap() on invariants that "can't happen". All of
those funnel through panic() in kernel/lib.rs (#[panic_handler]),
which itself calls BUG().
The motivation for this patch is narrow: replace that bindings::BUG()
call in lib.rs's panic() 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>
---
v4
- Fix objtool warning with CONFIG_RUST_DEBUG_ASSERTIONS enabled
v3: https://lore.kernel.org/rust-for-linux/20260712102802.3013600-1-tomo@flapping.org/
- Make some macro branches wrapped in {{ ... }}
- Fix typos
- Fix UML compile failure
v2: https://lore.kernel.org/rust-for-linux/20260709000103.2912159-1-tomo@aliasing.net/
- 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 | 89 ++++++++++++++++++++++++++++++++++------------
rust/kernel/lib.rs | 3 +-
2 files changed, 68 insertions(+), 24 deletions(-)
diff --git a/rust/kernel/bug.rs b/rust/kernel/bug.rs
index ed943960f851..7f4a83554c26 100644
--- a/rust/kernel/bug.rs
+++ b/rust/kernel/bug.rs
@@ -11,9 +11,19 @@
#[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, 0, $asm_tail:expr) => {
+ $crate::warn_flags!(@inner $cond, 0, $asm_tail, options(noreturn))
+ };
+ ($cond:expr, $flags:expr, $asm_tail:expr) => {
+ $crate::warn_flags!(@inner $cond, $flags, $asm_tail,)
+ };
+ (@inner $cond:expr, $flags:expr, $asm_tail:expr, $($opts:tt)*) => {{
+ #[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 +34,7 @@ macro_rules! warn_flags {
}
bytes
};
+ const FLAGS: u32 = $flags;
// SAFETY:
// - `file`, `line`, `flags`, and `size` are all compile-time constants or
@@ -35,14 +46,15 @@ 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>(),
- );
+ $($opts)*
+ )
}
- }
+ }};
}
#[macro_export]
@@ -50,8 +62,14 @@ 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, 0, $asm_tail:expr) => {
+ $crate::warn_flags!(@inner $cond, 0, $asm_tail, options(noreturn))
+ };
+ ($cond:expr, $flags:expr, $asm_tail:expr) => {
+ $crate::warn_flags!(@inner $cond, $flags, $asm_tail,)
+ };
+ (@inner $cond:expr, $flags:expr, $asm_tail:expr, $($opts:tt)*) => {{
+ const FLAGS: u32 = $flags;
// SAFETY:
// - `flags` and `size` are all compile-time constants, preventing
@@ -63,24 +81,29 @@ 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>(),
- );
+ $($opts)*
+ )
}
- }
+ }};
}
#[macro_export]
#[doc(hidden)]
#[cfg(all(CONFIG_BUG, CONFIG_UML))]
macro_rules! warn_flags {
- ($file:expr, $flags:expr) => {
+ ($cond:expr, 0, $asm_tail:expr) => {
+ // SAFETY: It is always safe to call `BUG()`.
+ unsafe { $crate::bindings::BUG() }
+ };
+ ($cond:expr, $flags:expr, $asm_tail: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(),
+ $crate::str::CStrExt::as_char_ptr($crate::c_str!(::core::file!())),
line!() as $crate::ffi::c_int,
$flags as $crate::ffi::c_uint,
::core::ptr::null(),
@@ -93,7 +116,11 @@ macro_rules! warn_flags {
#[doc(hidden)]
#[cfg(all(CONFIG_BUG, any(CONFIG_LOONGARCH, CONFIG_ARM)))]
macro_rules! warn_flags {
- ($file:expr, $flags:expr) => {
+ ($cond:expr, 0, $asm_tail:expr) => {
+ // SAFETY: It is always safe to call `BUG()`.
+ unsafe { $crate::bindings::BUG() }
+ };
+ ($cond:expr, $flags:expr, $asm_tail:expr) => {
// SAFETY: It is always safe to call `WARN_ON()`.
unsafe { $crate::bindings::WARN_ON(true) }
};
@@ -103,7 +130,10 @@ macro_rules! warn_flags {
#[doc(hidden)]
#[cfg(not(CONFIG_BUG))]
macro_rules! warn_flags {
- ($file:expr, $flags:expr) => {};
+ ($cond:expr, 0, $asm_tail:expr) => {
+ loop {}
+ };
+ ($cond:expr, $flags:expr, $asm_tail:expr) => {};
}
#[doc(hidden)]
@@ -117,16 +147,31 @@ 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, "")
+ }};
+}
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] only message in thread
only message in thread, other threads:[~2026-07-13 23:29 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-13 23:29 [PATCH v4] rust: bug: Add bug macro FUJITA Tomonori
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox