Skip to contentSkip to conversation

rust: bug: add warn_once macro

PATCH In review FUJITA Tomonori 2 participants 4 messages Oct 3 – Oct 4

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover rust: bug: add warn_once macro

From: FUJITA Tomonori <[email protected]>

This series adds warn_once!, the Rust version of C WARN_ONCE().

Patch 1 documents when to use the WARN macros, based on the comment in include/asm-generic/bug.h. Patch 2 adds warn_once!.

Miguel, patch 2 moves the UML warn_slowpath_fmt() call into warn_slowpath() and updates its SAFETY comment, so [1] can be dropped if this series is applied.

[1] https://lore.kernel.org/rust-for-linux/[email protected]/

FUJITA Tomonori (2):
rust: bug: document when to use the WARN macros
rust: bug: add warn_once macro

 rust/helpers/bug.c |  11 ++++
 rust/kernel/bug.rs | 134 +++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 135 insertions(+), 10 deletions(-)

base-commit: d52549881e6777fdcc79a349028413b182a641d2

Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 rust: bug: document when to use the WARN macros

From: FUJITA Tomonori <[email protected]>

Add a note to the module documentation, based on the comment in include/asm-generic/bug.h: use the WARN macros only for kernel bugs, not for invalid inputs from user space, the network or devices.

Also mention panic_on_warn, since a warning can then crash the machine.

---
 rust/kernel/bug.rs | 5 +++++
 1 file changed, 5 insertions(+)
rust/kernel/bug.rs +5 -0
index 6abed2c055b1..569dc05d75ad 100644
@@ -4,6 +4,11 @@
 
 //! Support for BUG and WARN functionality.
 //!
+//! Use the WARN macros only for kernel bugs that the kernel can recover
+//! from. Do not use them for invalid inputs from user space, the network or
+//! devices, or for transient conditions like `ENOMEM` or `EAGAIN`. A warning
+//! can crash the machine when `panic_on_warn` is enabled.
+//!
 //! C header: [`include/asm-generic/bug.h`](srctree/include/asm-generic/bug.h)
 
 #[macro_export]
Signature · 1 line
2.43.0
reviewer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH v1 1/2] rust: bug: document when to use the WARN macros

Quoted 10 lines · FUJITA Tomonori

From: FUJITA Tomonori <[email protected]>

Add a note to the module documentation, based on the comment in include/asm-generic/bug.h: use the WARN macros only for kernel bugs, not for invalid inputs from user space, the network or devices.

Also mention panic_on_warn, since a warning can then crash the machine.

Best regards,
Andreas Hindborg

#3 lore raw

PATCH 2/2 rust: bug: add warn_once macro

In reply to FUJITA Tomonori

From: FUJITA Tomonori <[email protected]>

Add warn_once!, the Rust version of C WARN_ONCE(). It prints a message and reports a warning the first time the condition is true, and returns the condition every time.

Like the generic C WARN_ONCE(), use do_once_lite! for the once state. Add warn_printf!, the Rust version of C __WARN_printf():

- On architectures where Rust implements WARN with inline asm (x86_64, arm64, riscv and s390), print the message with __warn_printk(), then trap, as the generic C __WARN_printf() does. x86_64 and s390 have an optimized C version, but Rust uses the generic way for now.

- On UML, pass the format to warn_slowpath_fmt(), as C does. warn_flags! now shares the same call.

- On ARM and LoongArch, add a WARN() helper, as warn_flags! uses the WARN_ON() helper there.

WARN() is a macro, so Rust calls it through the helper. A C function cannot pass its variadic arguments on to WARN(), so the helper takes the format and one argument. Rust passes "%pA" and a pointer to one fmt::Arguments, as the pr_*! macros do.

---
 rust/helpers/bug.c |  11 ++++
 rust/kernel/bug.rs | 129 +++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 130 insertions(+), 10 deletions(-)
rust/helpers/bug.c +11 -0
index b51e60772578..e5973e3c8aaa 100644
@@ -11,3 +11,14 @@ __rust_helper bool rust_helper_WARN_ON(bool cond)
 {
 	return WARN_ON(cond);
 }
+
+/*
+ * Rust uses this only on ARM and LoongArch. On x86_64 and s390, WARN()
+ * needs a string literal format, so it cannot be built there.
+ */
+#if defined(CONFIG_ARM) || defined(CONFIG_LOONGARCH)
+__rust_helper bool rust_helper_WARN(bool cond, const char *fmt, const void *arg)
+{
+	return WARN(cond, fmt, arg);
+}
+#endif
rust/kernel/bug.rs +119 -10

159 lines not on this page · raw message

Signature · 1 line
2.43.0