Re: [PATCH v9 5/8] rust: time: Add wrapper for fsleep() function

From: FUJITA Tomonori
Date: Wed Jan 29 2025 - 00:04:51 EST


On Tue, 28 Jan 2025 11:37:41 +0100
Fiona Behrens <me@xxxxxxxxxx> wrote:

>> Add a wrapper for fsleep(), flexible sleep functions in
>> include/linux/delay.h which typically deals with hardware delays.
>>
>> The kernel supports several sleep functions to handle various lengths
>> of delay. This adds fsleep(), automatically chooses the best sleep
>> method based on a duration.
>>
>> sleep functions including fsleep() belongs to TIMERS, not
>> TIMEKEEPING. They are maintained separately. rust/kernel/time.rs is an
>> abstraction for TIMEKEEPING. To make Rust abstractions match the C
>> side, add rust/kernel/time/delay.rs for this wrapper.
>>
>> fsleep() can only be used in a nonatomic context. This requirement is
>> not checked by these abstractions, but it is intended that klint [1]
>> or a similar tool will be used to check it in the future.
>>
>> Link: https://rust-for-linux.com/klint [1]
>> Signed-off-by: FUJITA Tomonori <fujita.tomonori@xxxxxxxxx>
>
> One question below, but fine with this as well
>
> Reviewed-by: Fiona Behrens <me@xxxxxxxxxx>

Thanks!

>> +pub fn fsleep(delta: Delta) {
>> + // The maximum value is set to `i32::MAX` microseconds to prevent integer
>> + // overflow inside fsleep, which could lead to unintentional infinite sleep.
>> + const MAX_DELTA: Delta = Delta::from_micros(i32::MAX as i64);
>> +
>> + let delta = if (Delta::ZERO..=MAX_DELTA).contains(&delta) {
>> + delta
>> + } else {
>> + // TODO: Add WARN_ONCE() when it's supported.
>> + MAX_DELTA
>> + };
>
> Did you try that with std::cmp::Ord you derived on Delta? This `.contains` looks a bit weird, maybe it also works with `delta <= MAX_DELTA`?

Do you mean using either if or match?

The link to the discussion that led to the current implementation is:

https://lore.kernel.org/lkml/CANiq72nNsmuQz1mEx2ov8SXj_UAEURDZFtLotf4qP2pf+r97eQ@xxxxxxxxxxxxxx/#t