[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250616153604.49418-2-boqun.feng@gmail.com>
Date: Mon, 16 Jun 2025 08:36:03 -0700
From: Boqun Feng <boqun.feng@...il.com>
To: linux-kernel@...r.kernel.org,
rust-for-linux@...r.kernel.org
Cc: boqun.feng@...il.com,
ojeda@...nel.org,
alex.gaynor@...il.com,
gary@...yguo.net,
bjorn3_gh@...tonmail.com,
benno.lossin@...ton.me,
a.hindborg@...sung.com,
aliceryhl@...gle.com,
tmgross@...ch.edu,
dakr@...nel.org,
mingo@...hat.com,
peterz@...radead.org,
juri.lelli@...hat.com,
vincent.guittot@...aro.org,
dietmar.eggemann@....com,
rostedt@...dmis.org,
bsegall@...gle.com,
mgorman@...e.de,
vschneid@...hat.com,
pmladek@...e.com,
fujita.tomonori@...il.com,
mingo@...nel.org
Subject: [PATCH v3 1/2] rust: Introduce file_from_location()
Most of kernel debugging facilities take a nul-terminated string for
file names for a callsite (generated from __FILE__), however the Rust
courterpart, Location, would return a Rust string (not nul-terminated)
from method .file(). And such a string cannot be passed to C debugging
function directly.
There is ongoing work to support a Location::file_with_nul(), which
returns a nul-terminated string from a Location. Since it's still
working in progress, and it will take some time before the feature
finally gets stabilized and the kernel's minimal rustc version might
also take a while to bump to a version that at least has that feature,
introduce a file_from_location() function, which return a warning string
if Location::file_with_nul() is not available.
This should work in most cases because as for now the known usage of
Location::file_with_nul() is only in debugging code (e.g. might_sleep())
and there might be other information reported by the debugging code that
could help locate the problematic function, so missing the file name is
fine at the moment.
Link: https://github.com/rust-lang/rust/issues/141727 [1]
Signed-off-by: Boqun Feng <boqun.feng@...il.com>
---
init/Kconfig | 3 +++
rust/kernel/lib.rs | 21 +++++++++++++++++++++
2 files changed, 24 insertions(+)
diff --git a/init/Kconfig b/init/Kconfig
index af4c2f085455..16e84f80bcd1 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -142,6 +142,9 @@ config RUSTC_HAS_SPAN_FILE
config RUSTC_HAS_UNNECESSARY_TRANSMUTES
def_bool RUSTC_VERSION >= 108800
+config RUSTC_HAS_LOCATION_FILE_WITH_NUL
+ def_bool RUSTC_VERSION >= 108900
+
config PAHOLE_VERSION
int
default $(shell,$(srctree)/scripts/pahole-version.sh $(PAHOLE))
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index 6b4774b2b1c3..3ea2391c7f91 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -40,6 +40,7 @@
#![cfg_attr(not(CONFIG_RUSTC_HAS_COERCE_POINTEE), feature(coerce_unsized))]
#![cfg_attr(not(CONFIG_RUSTC_HAS_COERCE_POINTEE), feature(dispatch_from_dyn))]
#![cfg_attr(not(CONFIG_RUSTC_HAS_COERCE_POINTEE), feature(unsize))]
+#![cfg_attr(CONFIG_RUSTC_HAS_LOCATION_FILE_WITH_NUL, feature(file_with_nul))]
// Ensure conditional compilation based on the kernel configuration works;
// otherwise we may silently break things like initcall handling.
@@ -274,3 +275,23 @@ macro_rules! asm {
::core::arch::asm!( $($asm)*, $($rest)* )
};
}
+
+/// Gets the C string file name of a [`Location`].
+///
+/// If `file_with_nul()` is not available, returns a string that warns about it.
+///
+/// [`Location`]: core::panic::Location
+pub fn file_from_location<'a>(_loc: &'a core::panic::Location<'a>) -> &'a crate::str::CStr {
+ // SAFETY: `Location::file_with_nul()` guarantees to return a string with a NUL terminated.
+ #[cfg(CONFIG_RUSTC_HAS_LOCATION_FILE_WITH_NUL)]
+ unsafe {
+ crate::str::CStr::from_bytes_with_nul_unchecked(_loc.file_with_nul().to_bytes())
+ }
+
+ #[cfg(not(CONFIG_RUSTC_HAS_LOCATION_FILE_WITH_NUL))]
+ {
+ use crate::c_str;
+
+ c_str!("<Location::file_with_nul() not supported>")
+ }
+}
--
2.39.5 (Apple Git-154)
Powered by blists - more mailing lists