Add Rust version of might_sleep()
v4 cover Add Rust version of might_sleep()
Hi,
This is a v4 of Tomo's previous version:
v3: https://lore.kernel.org/rust-for-linux/[email protected]/
v2: https://lore.kernel.org/rust-for-linux/[email protected]/
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/[email protected]/
[2]: https://github.com/rust-lang/rust/issues/141727
[3]: https://lore.kernel.org/lkml/[email protected]/
[4]: https://lore.kernel.org/rust-for-linux/[email protected]/
[5]: https://lore.kernel.org/rust-for-linux/[email protected]/
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(+)
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH v4 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() [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.
--- init/Kconfig | 3 +++ rust/kernel/lib.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+)
init/Kconfig +3 -0
@@ -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))
rust/kernel/lib.rs +43 -0
52 lines not on this page · raw message
Signature · 1 line
2.39.5 (Apple Git-154)
Re: [PATCH v4 1/2] rust: Introduce file_from_location()
Quoted 2 lines · Boqun Feng
+/// // - A path like "
rust/kernel/example.rs" iffile_with_nul()available.
"is available."
rust/kernel/lib.rs macro_rules! asm { · line 319
Quoted 7 lines
rust/kernel/lib.rs +7
-0+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:
The new version looks great, thanks!
Cheers,
Miguel
Re: [PATCH v4 1/2] rust: Introduce file_from_location()
Quoted 6 lines · Miguel Ojeda
Quoted 2 lines · Boqun Feng
+/// // - A path like "
rust/kernel/example.rs" iffile_with_nul()available."is available."
Fixed.
Quoted 10 lines
rust/kernel/lib.rs macro_rules! asm { · line 319
Quoted 7 lines
rust/kernel/lib.rs +7
-0+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:
Quoted 2 lines
Make sense, applied.
Quoted 2 lines
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
Quoted 2 lines
Cheers,
Miguel
[tip: sched/core] rust: Introduce file_from_location()
In reply to Boqun FengThe following commit has been merged into the sched/core branch of tip:
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 <[email protected]> AuthorDate: Sun, 15 Jun 2025 15:14:00 -07:00 Committer: Boqun Feng <[email protected]> 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.
--- init/Kconfig | 3 +++- rust/kernel/lib.rs | 48 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+)
init/Kconfig +3 -0
@@ -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))
rust/kernel/lib.rs +48 -0
57 lines not on this page · raw message
PATCH v4 2/2 rust: task: Add Rust version of might_sleep()
In reply to Boqun FengFrom: FUJITA Tomonori <[email protected]>
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()]
--- rust/helpers/task.c | 6 ++++++ rust/kernel/task.rs | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+)
rust/helpers/task.c +6 -0
@@ -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;
rust/kernel/task.rs +24 -0
@@ -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 () } +}
Signature · 1 line
2.39.5 (Apple Git-154)
[tip: sched/core] rust: task: Add Rust version of might_sleep()
The following commit has been merged into the sched/core branch of tip:
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 <[email protected]> AuthorDate: Fri, 11 Apr 2025 07:56:23 +09:00 Committer: Boqun Feng <[email protected]> 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()]
--- rust/helpers/task.c | 6 ++++++ rust/kernel/task.rs | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+)
rust/helpers/task.c +6 -0
@@ -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;
rust/kernel/task.rs +24 -0
@@ -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 () } +}