Add Rust version of might_sleep()
v3 cover Add Rust version of might_sleep()
Hi,
This is a quick v3 of Tomo's previous version:
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.
[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]/
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 | 21 +++++++++++++++++++++ rust/kernel/task.rs | 22 ++++++++++++++++++++++ 4 files changed, 52 insertions(+)
Signature · 1 line
2.39.5 (Apple Git-154)
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.
--- init/Kconfig | 3 +++ rust/kernel/lib.rs | 21 +++++++++++++++++++++ 2 files changed, 24 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_LOCATION_FILE_WITH_NUL + def_bool RUSTC_VERSION >= 108900 + config PAHOLE_VERSION int default $(shell,$(srctree)/scripts/pahole-version.sh $(PAHOLE))
rust/kernel/lib.rs +21 -0
@@ -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>) -> &'acrate ::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>" ) + } +}
Signature · 1 line
2.39.5 (Apple Git-154)
Re: [PATCH v3 1/2] rust: Introduce file_from_location()
Quoted 2 lines · Boqun Feng
introduce a
file_from_location()function, which return a warning string
returns
Quoted 4 lines
#![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))]
I would change the config name to CONFIG_RUSTC_HAS_FILE_WITH_NUL since that is the actual name, i.e. without "location".
By the way, please add a comment on top, like the others, i.e. something like:
//
// `feature(file_with_nul)` is expected to become stable. Before Rust
// 1.89.0, it did not exist, so enable it conditionally.Alice: the tracking issue uses the wrong name, i.e. with the location_* prefix.
Quoted 1 line
+/// If
file_with_nul()is not available, returns a string that warns about it.
Could we give a couple examples, i.e. of each case? (No need to assert anything)
Quoted 1 line
+ use
crate::c_str;
Do we need the use?
Thanks!
Cheers,
Miguel
Alice Ryhl
reviewerRe: [PATCH v3 1/2] rust: Introduce file_from_location()
Quoted 6 lines · Miguel Ojeda
Quoted 2 lines · Boqun Feng
introduce a
file_from_location()function, which return a warning stringreturns
Quoted 7 lines
Quoted 4 lines
#![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))]I would change the config name to
CONFIG_RUSTC_HAS_FILE_WITH_NULsince that is the actual name, i.e. without "location".
We will need to coordinate with
https://github.com/rust-lang/rust/pull/142579
Quoted 8 lines
By the way, please add a comment on top, like the others, i.e. something like:
// // `feature(file_with_nul)` is expected to become stable. Before Rust // 1.89.0, it did not exist, so enable it conditionally.Alice: the tracking issue uses the wrong name, i.e. with the
location_*prefix.
Fixed.
Alice
Re: [PATCH v3 1/2] rust: Introduce file_from_location()
Quoted 3 lines · Alice Ryhl
We will need to coordinate with
https://github.com/rust-lang/rust/pull/142579
Upstream Rust just closed it rejecting the change, so no change (so far) needed for this patch.
It would be nice to know if they feel the signature will stay reasonably stable now -- we can ask them tomorrow.
Cheers,
Miguel
Re: [PATCH v3 1/2] rust: Introduce file_from_location()
In reply to Miguel OjedaQuoted 6 lines · Miguel Ojeda
Quoted 2 lines · Boqun Feng
introduce a
file_from_location()function, which return a warning stringreturns
Fixed.
Quoted 8 lines
Quoted 4 lines
#![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))]I would change the config name to
CONFIG_RUSTC_HAS_FILE_WITH_NULsince that is the actual name, i.e. without "location".
Sounds good to me
Quoted 6 lines
By the way, please add a comment on top, like the others, i.e. something like:
// // `feature(file_with_nul)` is expected to become stable. Before Rust // 1.89.0, it did not exist, so enable it conditionally.
Will do, one thing though: the comment lines seem to wrap at 78 or 80 chars, so do other lines for conditional features in rust/kernel/lib.rs. However I believe in Rust code we use 100 chars text width, any particular reason that I should keep these new lines the same (wrapping at 80 characters)? Otherwise I will make the new lines wrap at 100.
Quoted 7 lines
Alice: the tracking issue uses the wrong name, i.e. with the
location_*prefix.Quoted 1 line
+/// If
file_with_nul()is not available, returns a string that warns about it.Could we give a couple examples, i.e. of each case? (No need to assert anything)
Sure, will do, but I'm afraid there is only case, unless I misunderstood you:
/// # Examples
///
/// ```
/// use kernel::file_from_location;
///
/// #[track_caller]
/// fn foo() {
/// let caller = core::panic::Location::caller();
///
/// pr_info!("{}\n", file_from_location(caller));
/// }
///
/// foo();
Quoted 4 lines
Quoted 1 line
+ use
crate::c_str;Do we need the
use?
No need for that, and thanks for catching it!
Regards,
Boqun
Quoted 4 lines
Thanks!
Cheers,
Miguel
Re: [PATCH v3 1/2] rust: Introduce file_from_location()
Quoted 6 lines · Boqun Feng
Will do, one thing though: the comment lines seem to wrap at 78 or 80 chars, so do other lines for conditional features in
rust/kernel/lib.rs. However I believe in Rust code we use 100 chars text width, any particular reason that I should keep these new lines the same (wrapping at 80 characters)? Otherwise I will make the new lines wrap at 100.
We have both styles, so up to you.
It would have been nice to at least know already if rustfmt would eventually land on 80 or 100 for this, even if the automatically wrapping is not stable :(
Quoted 2 lines
Sure, will do, but I'm afraid there is only case, unless I misunderstood you:
I meant the "If file_with_nul() is not available" vs. the available one (since it is mentioned in the docs already).
Quoted 1 line
/// use
kernel::file_from_location;
I would hide this line, since it is a single import of the item itself.
Quoted 1 line
/// pr_info!("{}\n",
file_from_location(caller));
I would suggest adding a comment on top of this line mentioning the output it could potentially show, e.g.
// Output: ...
Thanks for this!
Cheers,
Miguel
Re: [PATCH v3 1/2] rust: Introduce file_from_location()
Quoted 10 lines · Miguel Ojeda
Quoted 6 lines · Boqun Feng
Will do, one thing though: the comment lines seem to wrap at 78 or 80 chars, so do other lines for conditional features in
rust/kernel/lib.rs. However I believe in Rust code we use 100 chars text width, any particular reason that I should keep these new lines the same (wrapping at 80 characters)? Otherwise I will make the new lines wrap at 100.We have both styles, so up to you.
I will use 100 characters then.
Quoted 10 lines
It would have been nice to at least know already if
rustfmtwould eventually land on 80 or 100 for this, even if the automatically wrapping is not stable :(Quoted 2 lines
Sure, will do, but I'm afraid there is only case, unless I misunderstood you:
I meant the "If
file_with_nul()is not available" vs. the available one (since it is mentioned in the docs already).
Yes, but the example would be one, just the output would be different, hence I said the "only case", but see below:
Quoted 11 lines
Quoted 1 line
/// use
kernel::file_from_location;I would hide this line, since it is a single import of the item itself.
Quoted 1 line
/// pr_info!("{}\n",
file_from_location(caller));I would suggest adding a comment on top of this line mentioning the output it could potentially show, e.g.
// Output: ...
This actually helped me find a bug in the current implementation: I should use core::ffi::CStr::to_bytes_with_nul() instead of to_bytes(). Please see below for the update "Examples" section:
/// # Examples
///
/// ```
/// # use kernel::file_from_location;
///
/// #[track_caller]
/// fn foo() {
/// let caller = core::panic::Location::caller();
///
/// // Output:
/// // - If file_with_nul() available: "rust/doctests_kernel_generated.rs"
/// // - otherwise: "<Location::file_with_nul() not supported>"
/// pr_info!("{}\n", file_from_location(caller));/// }
///
/// # foo();
Quoted 2 lines
Thanks for this!
Thank you!
Regards,
Boqun
Quoted 2 lines
Cheers,
Miguel
Re: [PATCH v3 1/2] rust: Introduce file_from_location()
Quoted 4 lines · Boqun Feng
This actually helped me find a bug in the current implementation: I should use
core::ffi::CStr::to_bytes_with_nul()instead ofto_bytes(). Please see below for the update "Examples" section:
Yeah, writing examples can force us to find issues :)
I guess we could conditionally (cfg) assert in the "otherwise" case, since we already had one case, but I didn't suggest it earlier because it is a bit heavy, and the interesting case is the other one anyway so it wouldn't have caught the issue. I guess we could assert it ends with .rs for the interesting one.
By the way, I would avoid the actual filename, i.e. I would give a more "normal" example instead of the doctests_kernel_generated one of the example itself. Something like:
// Output:
// - A path like `rust/kernel/example.rs` if `file_with_nul()` is available.
// - `<Location::file_with_nul() not supported>` otherwise.It could make sense to have an intermediate variable (especially if you end up asserting anything), then you could put the comment on top of that instead. Then the pr_* call could then perhaps show a "realistic" example, and could inline the variable name, e.g. something like {caller_file}: my message\n.
Anyway, no big deal either way, what you had is also OK.
By the way, I noticed a typo in "with a NUL terminated." above.
Cheers,
Miguel
Re: [PATCH v3 1/2] rust: Introduce file_from_location()
Quoted 6 lines · Miguel Ojeda
Quoted 4 lines · Boqun Feng
This actually helped me find a bug in the current implementation: I should use
core::ffi::CStr::to_bytes_with_nul()instead ofto_bytes(). Please see below for the update "Examples" section:
Quoted 8 lines
Yeah, writing examples can force us to find issues :)
I guess we could conditionally (
cfg) assert in the "otherwise" case, since we already had one case, but I didn't suggest it earlier because it is a bit heavy, and the interesting case is the other one anyway so it wouldn't have caught the issue. I guess we could assert it ends with.rsfor the interesting one.
We already know the full function name from Location::file() ;-) so the assertion is easy:
assert_eq!(Ok(caller.file()), file_from_location(caller).to_str());
I didn't add the assertion of the otherwise case because that would involve either writing another string "<Location ... not supported>" or define it as a const, both are a bit overkilling to me.
Quoted 2 lines
By the way, I would avoid the actual filename, i.e. I would give a more "normal" example instead of the
doctests_kernel_generatedone
Good point.
Quoted 6 lines
of the example itself. Something like:
// Output: // - A path like `rust/kernel/example.rs` if `file_with_nul()` is available. // - `<Location::file_with_nul() not supported>` otherwise.
Quoted 6 lines
It could make sense to have an intermediate variable (especially if you end up asserting anything), then you could put the comment on top of that instead. Then the
pr_*call could then perhaps show a "realistic" example, and could inline the variable name, e.g. something like{caller_file}: my message\n.
How aobut something below? (I use "" instead of `` on purpose because the output variable there is a string)
/// 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: &kernel::str::CStr = 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();
/// ```
Quoted 4 lines
Anyway, no big deal either way, what you had is also OK.
By the way, I noticed a typo in "with a NUL terminated." above.
You mean it should be "with an NUL terminated"? Or it should be "with a NUL byte terminated"?
Regards,
Boqun
Quoted 2 lines
Cheers,
Miguel
Re: [PATCH v3 1/2] rust: Introduce file_from_location()
Quoted 3 lines · Boqun Feng
How aobut something below? (I use "" instead of `` on purpose because the output variable there is a string)
Looks much better, thanks!
Quoted 2 lines
You mean it should be "with an NUL terminated"? Or it should be "with a
NULbyte terminated"?
Ah, I meant that "terminated" sounded strange to me, i.e. it sounds as if the NUL is what is terminated. But I am not a native speaker.
I would have expected e.g. "a NUL terminated string" or variations like "a string terminated with a NUL" or"a string with a NUL termination byte", if that makes sense.
Cheers,
Miguel
Re: [PATCH v3 1/2] rust: Introduce file_from_location()
Quoted 10 lines · Miguel Ojeda
Quoted 3 lines · Boqun Feng
How aobut something below? (I use "" instead of `` on purpose because the output variable there is a string)
Looks much better, thanks!
Quoted 2 lines
You mean it should be "with an NUL terminated"? Or it should be "with a
NULbyte terminated"?
Quoted 6 lines
Ah, I meant that "terminated" sounded strange to me, i.e. it sounds as if the NUL is what is terminated. But I am not a native speaker.
I would have expected e.g. "a NUL terminated string" or variations like "a string terminated with a NUL" or"a string with a NUL termination byte", if that makes sense.
Yes, instead of this:
"guarantees to return a string with a NUL terminated."
...you will want this:
"guarantees to return a NUL-terminated string."
And depending on whether you prefer to speak Kernel, or R4L here, note that the kernel normally spells this as NULL (two L's).
thanks,
John Hubbard
Re: [PATCH v3 1/2] rust: Introduce file_from_location()
In reply to Miguel OjedaQuoted 10 lines · Miguel Ojeda
Quoted 3 lines · Boqun Feng
How aobut something below? (I use "" instead of `` on purpose because the output variable there is a string)
Looks much better, thanks!
Quoted 2 lines
You mean it should be "with an NUL terminated"? Or it should be "with a
NULbyte terminated"?
Quoted 7 lines
Ah, I meant that "terminated" sounded strange to me, i.e. it sounds as if the NUL is what is terminated. But I am not a native speaker.
I would have expected e.g. "a NUL terminated string" or variations like "a string terminated with a NUL" or"a string with a NUL termination byte", if that makes sense.
I made it "a NUL-terminated string" to align with other parts of our documentation in kernel::str.
Regards,
Boqun
Quoted 2 lines
Cheers,
Miguel
PATCH v3 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 | 22 ++++++++++++++++++++++ 2 files changed, 28 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 +22 -0
@@ -400,3 +400,25 @@ 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. +#[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)
Re: [PATCH v3 2/2] rust: task: Add Rust version of might_sleep()
Quoted 2 lines · Boqun Feng
+/// Equivalent to the C side [
might_sleep()], this function serves as
I am always pleased to see intra-doc links, but wouldn't this one point to the Rust item? :) i.e. we need to add the link, right?
I would normally ask for an example of typical usage, but do we have some good C docs on that already that we could link otherwise?
Thanks!
Cheers,
Miguel
Re: [PATCH v3 2/2] rust: task: Add Rust version of might_sleep()
Quoted 6 lines · On Mon, 16 Jun 2025 23:28:31 +0200 Miguel Ojeda…
Quoted 2 lines · Boqun Feng
+/// Equivalent to the C side [
might_sleep()], this function serves asI am always pleased to see intra-doc links, but wouldn't this one point to the Rust item? :) i.e. we need to add the link, right?
Ah, you're right.
Quoted 2 lines
I would normally ask for an example of typical usage, but do we have some good C docs on that already that we could link otherwise?
I can't find C docs on might_sleep(). Device driver developers probably don't need to use might_sleep() directly. I can't think of a good example.
How about adding a link to the header file where the might_sleep macro is defined, like this?
/// Annotation for functions that can sleep.
///
/// Equivalent to the C side [might_sleep] macro, this function serves as /// a debugging aid and a potential scheduling point.
///
/// This function can only be used in a nonatomic context.
///
/// [might_sleep]: srctree/include/linux/kernel.h #[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() }
}Re: [PATCH v3 2/2] rust: task: Add Rust version of might_sleep()
Quoted 3 lines · FUJITA Tomonori
How about adding a link to the header file where the
might_sleepmacro is defined, like this?
Sounds good to me. Though, for APIs that have rendered docs, we typically point to those instead:
https://docs.kernel.org/driver-api/basics.html#c.might_sleep
(Ideally those kernel.org docs would then have a source view themselves, but that is a different problem... :)
Cheers,
Miguel
Re: [PATCH v3 2/2] rust: task: Add Rust version of might_sleep()
Quoted 10 lines · On Tue, 17 Jun 2025 10:02:22 +0200 Miguel Ojeda…
Quoted 3 lines · FUJITA Tomonori
How about adding a link to the header file where the
might_sleepmacro is defined, like this?Sounds good to me. Though, for APIs that have rendered docs, we typically point to those instead:
https://docs.kernel.org/driver-api/basics.html#c.might_sleep
Ah, somehow, I overlooked that. Thanks!
Boqun, here is the revised version.
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`] macro, 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 () } +}
Boqun Feng
AppliedRe: [PATCH v3 2/2] rust: task: Add Rust version of might_sleep()
Quoted 9 lines · FUJITA Tomonori
Quoted 6 lines · On Tue, 17 Jun 2025 10:02:22 +0200 Miguel Ojeda…
Quoted 3 lines · FUJITA Tomonori
How about adding a link to the header file where the
might_sleepmacro is defined, like this?
Quoted 9 lines
Quoted 4 lines
Sounds good to me. Though, for APIs that have rendered docs, we typically point to those instead:
https://docs.kernel.org/driver-api/basics.html#c.might_sleep
Ah, somehow, I overlooked that. Thanks!
Boqun, here is the revised version.
Thanks, applied. Although I kept the parentheses because might_sleep() is a C macro, in the same spirit of [1].
[1]: https://docs.kernel.org/process/maintainer-tip.html#function-references-in-changelogs
Regards,
Boqun
Quoted 50 lines
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`] macro, 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 () } +}