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 } 50 51 /// Inserts a delay based on microseconds with busy waiting. 52 /// 53 /// Equivalent to the C side [`udelay()`], which delays in microseconds. 54 /// 55 /// `delta` must be within `[0, MAX_UDELAY_MS]` in milliseconds; 56 /// otherwise, it is erroneous behavior. That is, it is considered a bug to 57 /// call this function with an out-of-range value. 58 /// 59 /// The behavior above differs from the C side [`udelay()`] for which out-of-range 60 /// values could lead to an overflow and unexpected behavior. 61 /// 62 /// [`udelay()`]: https://docs.kernel.org/timers/delay_sleep_functions.html#c.udelay 63 pub fn udelay(delta: Delta) { 64 const MAX_UDELAY_DELTA: Delta = Delta::from_millis(bindings::MAX_UDELAY_MS as i64); 65 66 debug_assert!(delta.as_nanos() >= 0); 67 debug_assert!(delta <= MAX_UDELAY_DELTA); 68 69 let delta = if (Delta::ZERO..=MAX_UDELAY_DELTA).contains(&delta) { 70 delta 71 } else { 72 MAX_UDELAY_DELTA 73 }; 74 75 // SAFETY: It is always safe to call `udelay()` with any duration. 76 // Note that the kernel is compiled with `-fno-strict-overflow` 77 // so any out-of-range value could lead to unexpected behavior 78 // but won't lead to undefined behavior. 79 unsafe { 80 // Convert the duration to microseconds and round up to preserve 81 // the guarantee; `udelay()` inserts a delay for at least 82 // the provided duration, but that it may delay for longer 83 // under some circumstances. 84 bindings::udelay(delta.as_micros_ceil() as c_ulong) 85 } 86 } 87