linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 0/2] Add Rust version of might_sleep()
@ 2025-06-19 15:10 Boqun Feng
  2025-06-19 15:10 ` [PATCH v4 1/2] rust: Introduce file_from_location() Boqun Feng
  2025-06-19 15:10 ` [PATCH v4 2/2] rust: task: Add Rust version of might_sleep() Boqun Feng
  0 siblings, 2 replies; 7+ messages in thread
From: Boqun Feng @ 2025-06-19 15:10 UTC (permalink / raw)
  To: linux-kernel, rust-for-linux
  Cc: boqun.feng, ojeda, alex.gaynor, gary, bjorn3_gh, benno.lossin,
	a.hindborg, aliceryhl, tmgross, dakr, mingo, peterz, juri.lelli,
	vincent.guittot, dietmar.eggemann, rostedt, bsegall, mgorman,
	vschneid, pmladek, fujita.tomonori, mingo, tamird

Hi,

This is a v4 of Tomo's previous version:

v3: https://lore.kernel.org/rust-for-linux/20250616153604.49418-1-boqun.feng@gmail.com/
v2: https://lore.kernel.org/rust-for-linux/20250410225623.152616-1-fujita.tomonori@gmail.com/

Given Ingo's feedback [1], and the ongoing work at Rust side [2] (thanks
Alice!), I think it's better that we just start to use
Location::file_with_nul() if available, otherwise use a "this is not
supported" as file names for might_sleep() debug output, this should be
sufficient since might_sleep() only uses the file names for debug
output, and it will also trigger a stack trace when error, so some
callsite information can be gather from there too.

Having might_sleep() will unblock the support of read_poll_timeout()
[3], and then wait_gfw_boot_completion() [4] in nova. So hopefully this
will allow use moving forward.

I will a PR to tip including this updated version if no one objects.
Thanks.

Changes since v3:

* Changed the name of Kconfig per Miguel's feedback.
* Added comments on the unstable feature `file_with_nul` per Miguel's
  feedback.
* Added an example for `file_from_location()` per Miguel's feedback.
* Changed the return type from `kernel::str::CStr` to `core::ffi::CStr`
  to align with Tamir's effort [5].
* Applied the documentation change for `might_sleep()` from Tomo.

[1]: https://lore.kernel.org/rust-for-linux/aB2aAEELa3253nBh@gmail.com/
[2]: https://github.com/rust-lang/rust/issues/141727
[3]: https://lore.kernel.org/lkml/20250220070611.214262-8-fujita.tomonori@gmail.com/
[4]: https://lore.kernel.org/rust-for-linux/20250612-nova-frts-v5-12-14ba7eaf166b@nvidia.com/
[5]: https://lore.kernel.org/rust-for-linux/20250619-cstr-core-v12-0-80c9c7b45900@gmail.com/

Regards,
Boqun

Boqun Feng (1):
  rust: Introduce file_from_location()

FUJITA Tomonori (1):
  rust: task: Add Rust version of might_sleep()

 init/Kconfig        |  3 +++
 rust/helpers/task.c |  6 ++++++
 rust/kernel/lib.rs  | 43 +++++++++++++++++++++++++++++++++++++++++++
 rust/kernel/task.rs | 24 ++++++++++++++++++++++++
 4 files changed, 76 insertions(+)

-- 
2.39.5 (Apple Git-154)


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v4 1/2] rust: Introduce file_from_location()
  2025-06-19 15:10 [PATCH v4 0/2] Add Rust version of might_sleep() Boqun Feng
@ 2025-06-19 15:10 ` Boqun Feng
  2025-06-19 20:42   ` Miguel Ojeda
  2025-07-03  8:36   ` [tip: sched/core] " tip-bot2 for Boqun Feng
  2025-06-19 15:10 ` [PATCH v4 2/2] rust: task: Add Rust version of might_sleep() Boqun Feng
  1 sibling, 2 replies; 7+ messages in thread
From: Boqun Feng @ 2025-06-19 15:10 UTC (permalink / raw)
  To: linux-kernel, rust-for-linux
  Cc: boqun.feng, ojeda, alex.gaynor, gary, bjorn3_gh, benno.lossin,
	a.hindborg, aliceryhl, tmgross, dakr, mingo, peterz, juri.lelli,
	vincent.guittot, dietmar.eggemann, rostedt, bsegall, mgorman,
	vschneid, pmladek, fujita.tomonori, mingo, tamird

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() [1], 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 returns 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@gmail.com>
---
 init/Kconfig       |  3 +++
 rust/kernel/lib.rs | 43 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/init/Kconfig b/init/Kconfig
index af4c2f085455..6f4ec5633ffa 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_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..57cff1ca8b90 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -40,6 +40,10 @@
 #![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))]
+//
+// `feature(file_with_nul)` is expected to become stable. Before Rust 1.89.0, it did not exist, so
+// enable it conditionally.
+#![cfg_attr(CONFIG_RUSTC_HAS_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 +278,42 @@ 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
+///
+/// # Examples
+///
+/// ```
+/// # use kernel::file_from_location;
+///
+/// #[track_caller]
+/// fn foo() {
+///     let caller = core::panic::Location::caller();
+///
+///     // Output:
+///     // - A path like "rust/kernel/example.rs" if file_with_nul() available.
+///     // - "<Location::file_with_nul() not supported>" otherwise.
+///     let caller_file = file_from_location(caller);
+///
+///     // Prints out the message with caller's file name.
+///     pr_info!("foo() called in file {caller_file:?}\n");
+///
+///     # if cfg!(CONFIG_RUSTC_HAS_FILE_WITH_NUL) {
+///     #     assert_eq!(Ok(caller.file()), caller_file.to_str());
+///     # }
+/// }
+///
+/// # foo();
+/// ```
+#[inline]
+pub fn file_from_location<'a>(_loc: &'a core::panic::Location<'a>) -> &'a core::ffi::CStr {
+    #[cfg(CONFIG_RUSTC_HAS_FILE_WITH_NUL)]
+    { _loc.file_with_nul() }
+
+    #[cfg(not(CONFIG_RUSTC_HAS_FILE_WITH_NUL))]
+    { c"<Location::file_with_nul() not supported>" }
+}
-- 
2.39.5 (Apple Git-154)


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v4 2/2] rust: task: Add Rust version of might_sleep()
  2025-06-19 15:10 [PATCH v4 0/2] Add Rust version of might_sleep() Boqun Feng
  2025-06-19 15:10 ` [PATCH v4 1/2] rust: Introduce file_from_location() Boqun Feng
@ 2025-06-19 15:10 ` Boqun Feng
  2025-07-03  8:36   ` [tip: sched/core] " tip-bot2 for FUJITA Tomonori
  1 sibling, 1 reply; 7+ messages in thread
From: Boqun Feng @ 2025-06-19 15:10 UTC (permalink / raw)
  To: linux-kernel, rust-for-linux
  Cc: boqun.feng, ojeda, alex.gaynor, gary, bjorn3_gh, benno.lossin,
	a.hindborg, aliceryhl, tmgross, dakr, mingo, peterz, juri.lelli,
	vincent.guittot, dietmar.eggemann, rostedt, bsegall, mgorman,
	vschneid, pmladek, fujita.tomonori, mingo, tamird

From: FUJITA Tomonori <fujita.tomonori@gmail.com>

Add a helper function equivalent to the C's might_sleep(), which
serves as a debugging aid and a potential scheduling point.

Note that this function can only be used in a nonatomic context.

This will be used by Rust version of read_poll_timeout().

[boqun: Use file_from_location() to get a C string instead of changing
__might_sleep()]

Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
---
 rust/helpers/task.c |  6 ++++++
 rust/kernel/task.rs | 24 ++++++++++++++++++++++++
 2 files changed, 30 insertions(+)

diff --git a/rust/helpers/task.c b/rust/helpers/task.c
index 31c33ea2dce6..2c85bbc2727e 100644
--- a/rust/helpers/task.c
+++ b/rust/helpers/task.c
@@ -1,7 +1,13 @@
 // SPDX-License-Identifier: GPL-2.0
 
+#include <linux/kernel.h>
 #include <linux/sched/task.h>
 
+void rust_helper_might_resched(void)
+{
+	might_resched();
+}
+
 struct task_struct *rust_helper_get_current(void)
 {
 	return current;
diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs
index 834368313088..7d0935bc325c 100644
--- a/rust/kernel/task.rs
+++ b/rust/kernel/task.rs
@@ -400,3 +400,27 @@ fn eq(&self, other: &Kuid) -> bool {
 }
 
 impl Eq for Kuid {}
+
+/// Annotation for functions that can sleep.
+///
+/// Equivalent to the C side [`might_sleep()`], this function serves as
+/// a debugging aid and a potential scheduling point.
+///
+/// This function can only be used in a nonatomic context.
+///
+/// [`might_sleep()`]: https://docs.kernel.org/driver-api/basics.html#c.might_sleep
+#[track_caller]
+#[inline]
+pub fn might_sleep() {
+    #[cfg(CONFIG_DEBUG_ATOMIC_SLEEP)]
+    {
+        let loc = core::panic::Location::caller();
+        let file = kernel::file_from_location(loc);
+
+        // SAFETY: `file.as_ptr()` is valid for reading and guaranteed to be nul-terminated.
+        unsafe { crate::bindings::__might_sleep(file.as_ptr().cast(), loc.line() as i32) }
+    }
+
+    // SAFETY: Always safe to call.
+    unsafe { crate::bindings::might_resched() }
+}
-- 
2.39.5 (Apple Git-154)


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v4 1/2] rust: Introduce file_from_location()
  2025-06-19 15:10 ` [PATCH v4 1/2] rust: Introduce file_from_location() Boqun Feng
@ 2025-06-19 20:42   ` Miguel Ojeda
  2025-06-19 20:51     ` Boqun Feng
  2025-07-03  8:36   ` [tip: sched/core] " tip-bot2 for Boqun Feng
  1 sibling, 1 reply; 7+ messages in thread
From: Miguel Ojeda @ 2025-06-19 20:42 UTC (permalink / raw)
  To: Boqun Feng
  Cc: linux-kernel, rust-for-linux, ojeda, alex.gaynor, gary, bjorn3_gh,
	benno.lossin, a.hindborg, aliceryhl, tmgross, dakr, mingo, peterz,
	juri.lelli, vincent.guittot, dietmar.eggemann, rostedt, bsegall,
	mgorman, vschneid, pmladek, fujita.tomonori, mingo, tamird

On Thu, Jun 19, 2025 at 5:10 PM Boqun Feng <boqun.feng@gmail.com> wrote:
>
> +///     // - A path like "rust/kernel/example.rs" if file_with_nul() available.

"is available."

> +pub fn file_from_location<'a>(_loc: &'a core::panic::Location<'a>) -> &'a core::ffi::CStr {
> +    #[cfg(CONFIG_RUSTC_HAS_FILE_WITH_NUL)]
> +    { _loc.file_with_nul() }
> +
> +    #[cfg(not(CONFIG_RUSTC_HAS_FILE_WITH_NUL))]
> +    { c"<Location::file_with_nul() not supported>" }
> +}

If a `let _` works, then it may be better -- please see a similar case at:

    https://lore.kernel.org/rust-for-linux/CANiq72=ph_XG0TtkdsNJCUZMiAMYBo11eJaCTLCTpTAFXtcNMA@mail.gmail.com/

The new version looks great, thanks!

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v4 1/2] rust: Introduce file_from_location()
  2025-06-19 20:42   ` Miguel Ojeda
@ 2025-06-19 20:51     ` Boqun Feng
  0 siblings, 0 replies; 7+ messages in thread
From: Boqun Feng @ 2025-06-19 20:51 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: linux-kernel, rust-for-linux, ojeda, alex.gaynor, gary, bjorn3_gh,
	benno.lossin, a.hindborg, aliceryhl, tmgross, dakr, mingo, peterz,
	juri.lelli, vincent.guittot, dietmar.eggemann, rostedt, bsegall,
	mgorman, vschneid, pmladek, fujita.tomonori, mingo, tamird

On Thu, Jun 19, 2025 at 10:42:44PM +0200, Miguel Ojeda wrote:
> On Thu, Jun 19, 2025 at 5:10 PM Boqun Feng <boqun.feng@gmail.com> wrote:
> >
> > +///     // - A path like "rust/kernel/example.rs" if file_with_nul() available.
> 
> "is available."
> 

Fixed.

> > +pub fn file_from_location<'a>(_loc: &'a core::panic::Location<'a>) -> &'a core::ffi::CStr {
> > +    #[cfg(CONFIG_RUSTC_HAS_FILE_WITH_NUL)]
> > +    { _loc.file_with_nul() }
> > +
> > +    #[cfg(not(CONFIG_RUSTC_HAS_FILE_WITH_NUL))]
> > +    { c"<Location::file_with_nul() not supported>" }
> > +}
> 
> If a `let _` works, then it may be better -- please see a similar case at:
> 
>     https://lore.kernel.org/rust-for-linux/CANiq72=ph_XG0TtkdsNJCUZMiAMYBo11eJaCTLCTpTAFXtcNMA@mail.gmail.com/
> 

Make sense, applied.

> The new version looks great, thanks!
> 

Thanks! I pushed the updated version at:

	https://git.kernel.org/pub/scm/linux/kernel/git/boqun/linux.git/log/?h=rust-sched-v2

"-v2" because it's going to be a v2 of the pull request.

Regards,
Boqun

> Cheers,
> Miguel

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [tip: sched/core] rust: task: Add Rust version of might_sleep()
  2025-06-19 15:10 ` [PATCH v4 2/2] rust: task: Add Rust version of might_sleep() Boqun Feng
@ 2025-07-03  8:36   ` tip-bot2 for FUJITA Tomonori
  0 siblings, 0 replies; 7+ messages in thread
From: tip-bot2 for FUJITA Tomonori @ 2025-07-03  8:36 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: FUJITA Tomonori, Alice Ryhl, Boqun Feng, x86, linux-kernel

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     7e611710acf966df1e14bcf4e067385e38e549a1
Gitweb:        https://git.kernel.org/tip/7e611710acf966df1e14bcf4e067385e38e549a1
Author:        FUJITA Tomonori <fujita.tomonori@gmail.com>
AuthorDate:    Fri, 11 Apr 2025 07:56:23 +09:00
Committer:     Boqun Feng <boqun.feng@gmail.com>
CommitterDate: Tue, 24 Jun 2025 15:53:50 -07:00

rust: task: Add Rust version of might_sleep()

Add a helper function equivalent to the C's might_sleep(), which
serves as a debugging aid and a potential scheduling point.

Note that this function can only be used in a nonatomic context.

This will be used by Rust version of read_poll_timeout().

[boqun: Use file_from_location() to get a C string instead of changing
__might_sleep()]

Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
Link: https://lore.kernel.org/r/20250619151007.61767-3-boqun.feng@gmail.com
---
 rust/helpers/task.c |  6 ++++++
 rust/kernel/task.rs | 24 ++++++++++++++++++++++++
 2 files changed, 30 insertions(+)

diff --git a/rust/helpers/task.c b/rust/helpers/task.c
index 31c33ea..2c85bbc 100644
--- a/rust/helpers/task.c
+++ b/rust/helpers/task.c
@@ -1,7 +1,13 @@
 // SPDX-License-Identifier: GPL-2.0
 
+#include <linux/kernel.h>
 #include <linux/sched/task.h>
 
+void rust_helper_might_resched(void)
+{
+	might_resched();
+}
+
 struct task_struct *rust_helper_get_current(void)
 {
 	return current;
diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs
index 8343683..7d0935b 100644
--- a/rust/kernel/task.rs
+++ b/rust/kernel/task.rs
@@ -400,3 +400,27 @@ impl PartialEq for Kuid {
 }
 
 impl Eq for Kuid {}
+
+/// Annotation for functions that can sleep.
+///
+/// Equivalent to the C side [`might_sleep()`], this function serves as
+/// a debugging aid and a potential scheduling point.
+///
+/// This function can only be used in a nonatomic context.
+///
+/// [`might_sleep()`]: https://docs.kernel.org/driver-api/basics.html#c.might_sleep
+#[track_caller]
+#[inline]
+pub fn might_sleep() {
+    #[cfg(CONFIG_DEBUG_ATOMIC_SLEEP)]
+    {
+        let loc = core::panic::Location::caller();
+        let file = kernel::file_from_location(loc);
+
+        // SAFETY: `file.as_ptr()` is valid for reading and guaranteed to be nul-terminated.
+        unsafe { crate::bindings::__might_sleep(file.as_ptr().cast(), loc.line() as i32) }
+    }
+
+    // SAFETY: Always safe to call.
+    unsafe { crate::bindings::might_resched() }
+}

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [tip: sched/core] rust: Introduce file_from_location()
  2025-06-19 15:10 ` [PATCH v4 1/2] rust: Introduce file_from_location() Boqun Feng
  2025-06-19 20:42   ` Miguel Ojeda
@ 2025-07-03  8:36   ` tip-bot2 for Boqun Feng
  1 sibling, 0 replies; 7+ messages in thread
From: tip-bot2 for Boqun Feng @ 2025-07-03  8:36 UTC (permalink / raw)
  To: linux-tip-commits; +Cc: Boqun Feng, x86, linux-kernel

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     0aa2b78ce5a9eac8f3332192ea77755d63a831cd
Gitweb:        https://git.kernel.org/tip/0aa2b78ce5a9eac8f3332192ea77755d63a831cd
Author:        Boqun Feng <boqun.feng@gmail.com>
AuthorDate:    Sun, 15 Jun 2025 15:14:00 -07:00
Committer:     Boqun Feng <boqun.feng@gmail.com>
CommitterDate: Tue, 24 Jun 2025 15:53:46 -07:00

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() [1], 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 returns 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@gmail.com>
Link: https://lore.kernel.org/r/20250619151007.61767-2-boqun.feng@gmail.com
---
 init/Kconfig       |  3 +++-
 rust/kernel/lib.rs | 48 +++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 51 insertions(+)

diff --git a/init/Kconfig b/init/Kconfig
index af4c2f0..6f4ec56 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_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 6b4774b..717a5b6 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -40,6 +40,10 @@
 #![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))]
+//
+// `feature(file_with_nul)` is expected to become stable. Before Rust 1.89.0, it did not exist, so
+// enable it conditionally.
+#![cfg_attr(CONFIG_RUSTC_HAS_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 +278,47 @@ 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
+///
+/// # Examples
+///
+/// ```
+/// # use kernel::file_from_location;
+///
+/// #[track_caller]
+/// fn foo() {
+///     let caller = core::panic::Location::caller();
+///
+///     // Output:
+///     // - A path like "rust/kernel/example.rs" if file_with_nul() is available.
+///     // - "<Location::file_with_nul() not supported>" otherwise.
+///     let caller_file = file_from_location(caller);
+///
+///     // Prints out the message with caller's file name.
+///     pr_info!("foo() called in file {caller_file:?}\n");
+///
+///     # if cfg!(CONFIG_RUSTC_HAS_FILE_WITH_NUL) {
+///     #     assert_eq!(Ok(caller.file()), caller_file.to_str());
+///     # }
+/// }
+///
+/// # foo();
+/// ```
+#[inline]
+pub fn file_from_location<'a>(loc: &'a core::panic::Location<'a>) -> &'a core::ffi::CStr {
+    #[cfg(CONFIG_RUSTC_HAS_FILE_WITH_NUL)]
+    {
+        loc.file_with_nul()
+    }
+
+    #[cfg(not(CONFIG_RUSTC_HAS_FILE_WITH_NUL))]
+    {
+        let _ = loc;
+        c"<Location::file_with_nul() not supported>"
+    }
+}

^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2025-07-03  8:36 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-19 15:10 [PATCH v4 0/2] Add Rust version of might_sleep() Boqun Feng
2025-06-19 15:10 ` [PATCH v4 1/2] rust: Introduce file_from_location() Boqun Feng
2025-06-19 20:42   ` Miguel Ojeda
2025-06-19 20:51     ` Boqun Feng
2025-07-03  8:36   ` [tip: sched/core] " tip-bot2 for Boqun Feng
2025-06-19 15:10 ` [PATCH v4 2/2] rust: task: Add Rust version of might_sleep() Boqun Feng
2025-07-03  8:36   ` [tip: sched/core] " tip-bot2 for FUJITA Tomonori

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).