Re: [PATCH v3 2/2] rust: task: Add Rust version of might_sleep()
From: FUJITA Tomonori
Date: Mon Jun 16 2025 - 23:05:20 EST
On Mon, 16 Jun 2025 23:28:31 +0200
Miguel Ojeda <miguel.ojeda.sandonis@xxxxxxxxx> wrote:
> On Mon, Jun 16, 2025 at 5:36 PM Boqun Feng <boqun.feng@xxxxxxxxx> wrote:
>>
>> +/// 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?
Ah, you're 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?
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() }
}