Re: [PATCH v1 2/2] rust: task: add Rust version of might_sleep()

From: FUJITA Tomonori
Date: Thu Apr 10 2025 - 09:52:29 EST


On Wed, 9 Apr 2025 08:51:44 +0000
Alice Ryhl <aliceryhl@xxxxxxxxxx> wrote:

>> diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs
>> index 9e6f6854948d..1f0156b38ab5 100644
>> --- a/rust/kernel/task.rs
>> +++ b/rust/kernel/task.rs
>> @@ -380,3 +380,29 @@ 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();
>> + // SAFETY: FFI call.
>
> Overall this looks okay to me, but this safety comment could be
> improved. This being an FFI call is not the reason *why* it is safe to
> make this call.

Undertood.

> // SAFETY: `file.as_ptr()` is valid for reading for `file.len()` bytes.
>
> And I might separate the file into a separate variable for clarity:
> let loc = core::panic::Location::caller();
> let file = loc.file();

Fixed.

>> + unsafe {
>> + crate::bindings::__might_sleep_precision(
>> + loc.file().as_ptr().cast(),
>> + loc.file().len() as i32,
>> + loc.line() as i32,
>> + )
>> + }
>> + }
>> +
>> + // SAFETY: FFI call.
>> + unsafe { crate::bindings::might_resched() }
>
> And here you can say
> // SAFETY: Always safe to call.

Fixed.

Thanks a lot!