Skip to content

Commit cc4b74f

Browse files
fujitajannau
authored andcommitted
rust: time: Add wrapper for fsleep() function
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. 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] Reviewed-by: Gary Guo <gary@garyguo.net> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Fiona Behrens <me@kloenk.dev> Tested-by: Daniel Almeida <daniel.almeida@collabora.com> Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com> Link: https://lore.kernel.org/r/20250617144155.3903431-3-fujita.tomonori@gmail.com Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
1 parent c9572d1 commit cc4b74f

3 files changed

Lines changed: 56 additions & 0 deletions

File tree

rust/helpers/time.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
// SPDX-License-Identifier: GPL-2.0
22

3+
#include <linux/delay.h>
34
#include <linux/ktime.h>
45

6+
void rust_helper_fsleep(unsigned long usecs)
7+
{
8+
fsleep(usecs);
9+
}
10+
511
s64 rust_helper_ktime_to_us(const ktime_t kt)
612
{
713
return ktime_to_us(kt);

rust/kernel/time.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
//! C header: [`include/linux/jiffies.h`](srctree/include/linux/jiffies.h).
2525
//! C header: [`include/linux/ktime.h`](srctree/include/linux/ktime.h).
2626
27+
pub mod delay;
2728
pub mod hrtimer;
2829

2930
/// The number of nanoseconds per microsecond.

rust/kernel/time/delay.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Delay and sleep primitives.
4+
//!
5+
//! This module contains the kernel APIs related to delay and sleep that
6+
//! have been ported or wrapped for usage by Rust code in the kernel.
7+
//!
8+
//! C header: [`include/linux/delay.h`](srctree/include/linux/delay.h).
9+
10+
use super::Delta;
11+
use crate::prelude::*;
12+
13+
/// Sleeps for a given duration at least.
14+
///
15+
/// Equivalent to the C side [`fsleep()`], flexible sleep function,
16+
/// which automatically chooses the best sleep method based on a duration.
17+
///
18+
/// `delta` must be within `[0, i32::MAX]` microseconds;
19+
/// otherwise, it is erroneous behavior. That is, it is considered a bug
20+
/// to call this function with an out-of-range value, in which case the function
21+
/// will sleep for at least the maximum value in the range and may warn
22+
/// in the future.
23+
///
24+
/// The behavior above differs from the C side [`fsleep()`] for which out-of-range
25+
/// values mean "infinite timeout" instead.
26+
///
27+
/// This function can only be used in a nonatomic context.
28+
///
29+
/// [`fsleep()`]: https://docs.kernel.org/timers/delay_sleep_functions.html#c.fsleep
30+
pub fn fsleep(delta: Delta) {
31+
// The maximum value is set to `i32::MAX` microseconds to prevent integer
32+
// overflow inside fsleep, which could lead to unintentional infinite sleep.
33+
const MAX_DELTA: Delta = Delta::from_micros(i32::MAX as i64);
34+
35+
let delta = if (Delta::ZERO..=MAX_DELTA).contains(&delta) {
36+
delta
37+
} else {
38+
// TODO: Add WARN_ONCE() when it's supported.
39+
MAX_DELTA
40+
};
41+
42+
// SAFETY: It is always safe to call `fsleep()` with any duration.
43+
unsafe {
44+
// Convert the duration to microseconds and round up to preserve
45+
// the guarantee; `fsleep()` sleeps for at least the provided duration,
46+
// but that it may sleep for longer under some circumstances.
47+
bindings::fsleep(delta.as_micros_ceil() as c_ulong)
48+
}
49+
}

0 commit comments

Comments
 (0)