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::{ 12 pr_warn_once, 13 prelude::*, // 14 }; 15 16 /// Sleeps for a given duration at least. 17 /// 18 /// Equivalent to the C side [`fsleep()`], flexible sleep function, 19 /// which automatically chooses the best sleep method based on a duration. 20 /// 21 /// `delta` must be within `[0, i32::MAX]` microseconds; 22 /// otherwise, it is erroneous behavior. That is, it is considered a bug 23 /// to call this function with an out-of-range value, in which case the function 24 /// will sleep for at least the maximum value in the range and warns once. 25 /// 26 /// The behavior above differs from the C side [`fsleep()`] for which out-of-range 27 /// values mean "infinite timeout" instead. 28 /// 29 /// This function can only be used in a nonatomic context. 30 /// 31 /// [`fsleep()`]: https://docs.kernel.org/timers/delay_sleep_functions.html#c.fsleep 32 pub fn fsleep(delta: Delta) { 33 // The maximum value is set to `i32::MAX` microseconds to prevent integer 34 // overflow inside fsleep, which could lead to unintentional infinite sleep. 35 const MAX_DELTA: Delta = Delta::from_micros(i32::MAX as i64); 36 37 debug_assert!(delta.as_nanos() >= 0); 38 debug_assert!(delta <= MAX_DELTA); 39 40 let delta = if (Delta::ZERO..=MAX_DELTA).contains(&delta) { 41 delta 42 } else { 43 pr_warn_once!("attempted to fsleep() with out of range delta\n"); 44 MAX_DELTA 45 }; 46 47 // SAFETY: It is always safe to call `fsleep()` with any duration. 48 unsafe { 49 // Convert the duration to microseconds and round up to preserve 50 // the guarantee; `fsleep()` sleeps for at least the provided duration, 51 // but that it may sleep for longer under some circumstances. 52 bindings::fsleep(delta.as_micros_ceil() as c_ulong) 53 } 54 } 55 56 /// Inserts a delay based on microseconds with busy waiting. 57 /// 58 /// Equivalent to the C side [`udelay()`], which delays in microseconds. 59 /// 60 /// `delta` must be within `[0, MAX_UDELAY_MS]` in milliseconds; 61 /// otherwise, it is erroneous behavior. That is, it is considered a bug to 62 /// call this function with an out-of-range value, in which case the function 63 /// will delay for at least the maximum value in the range and warns once. 64 /// 65 /// The behavior above differs from the C side [`udelay()`] for which out-of-range 66 /// values could lead to an overflow and unexpected behavior. 67 /// 68 /// [`udelay()`]: https://docs.kernel.org/timers/delay_sleep_functions.html#c.udelay 69 pub fn udelay(delta: Delta) { 70 const MAX_UDELAY_DELTA: Delta = Delta::from_millis(bindings::MAX_UDELAY_MS as i64); 71 72 debug_assert!(delta.as_nanos() >= 0); 73 debug_assert!(delta <= MAX_UDELAY_DELTA); 74 75 let delta = if (Delta::ZERO..=MAX_UDELAY_DELTA).contains(&delta) { 76 delta 77 } else { 78 pr_warn_once!("attempted to udelay() with out of range delta\n"); 79 MAX_UDELAY_DELTA 80 }; 81 82 // SAFETY: It is always safe to call `udelay()` with any duration. 83 // Note that the kernel is compiled with `-fno-strict-overflow` 84 // so any out-of-range value could lead to unexpected behavior 85 // but won't lead to undefined behavior. 86 unsafe { 87 // Convert the duration to microseconds and round up to preserve 88 // the guarantee; `udelay()` inserts a delay for at least 89 // the provided duration, but that it may delay for longer 90 // under some circumstances. 91 bindings::udelay(delta.as_micros_ceil() as c_ulong) 92 } 93 } 94