Skip to contentSkip to conversation

rust: time: warn and assert out-of-range input in fsleep() and udelay()

PATCH Applied Sang-Heon Jeon 2 participants 4 messages Jun 21 – Sep 29

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 rust: time: warn and assert out-of-range input in fsleep() and udelay()

fsleep() documents out-of-range input as a bug but does not check it, unlike udelay(). Add debug_assert! calls to catch it in debug builds.

Also add pr_warn_once! calls in both fsleep() and udelay() so the clamped path is observable at runtime.

---
Changes from v1 [1]
- Add pr_warn_once to fsleep() and udelay()
- Add reviewed-by tag by Onur

Changes from v2 [2]
- Fix import to follow kernel code guidelines
- Add reviewed-by tag by Andreas

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

---
 rust/kernel/time/delay.rs | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
rust/kernel/time/delay.rs +12 -5
index b5b1b42797a0..4debe23731bf 100644
@@ -8,7 +8,10 @@
 //! C header: [`include/linux/delay.h`](srctree/include/linux/delay.h).
 
 use super::Delta;
-use crate::prelude::*;
+use crate::{
+    pr_warn_once,
+    prelude::*, //
+};
 
 /// Sleeps for a given duration at least.
 ///
@@ -18,8 +21,7 @@
 /// `delta` must be within `[0, i32::MAX]` microseconds;
 /// otherwise, it is erroneous behavior. That is, it is considered a bug
 /// to call this function with an out-of-range value, in which case the function
-/// will sleep for at least the maximum value in the range and may warn
-/// in the future.
+/// will sleep for at least the maximum value in the range and warns once.
 ///
 /// The behavior above differs from the C side [`fsleep()`] for which out-of-range
 /// values mean "infinite timeout" instead.
@@ -32,10 +34,13 @@ pub fn fsleep(delta: Delta) {
     // overflow inside fsleep, which could lead to unintentional infinite sleep.
     const MAX_DELTA: Delta = Delta::from_micros(i32::MAX as i64);
 
+    debug_assert!(delta.as_nanos() >= 0);
+    debug_assert!(delta <= MAX_DELTA);
+
     let delta = if (Delta::ZERO..=MAX_DELTA).contains(&delta) {
         delta
     } else {
-        // TODO: Add WARN_ONCE() when it's supported.
+        pr_warn_once!("attempted to fsleep() with out of range delta\n");
         MAX_DELTA
     };
 
@@ -54,7 +59,8 @@ pub fn fsleep(delta: Delta) {
 ///
 /// `delta` must be within `[0, MAX_UDELAY_MS]` in milliseconds;
 /// otherwise, it is erroneous behavior. That is, it is considered a bug to
-/// call this function with an out-of-range value.
+/// call this function with an out-of-range value, in which case the function
+/// will delay for at least the maximum value in the range and warns once.
 ///
 /// The behavior above differs from the C side [`udelay()`] for which out-of-range
 /// values could lead to an overflow and unexpected behavior.
@@ -69,6 +75,7 @@ pub fn udelay(delta: Delta) {
     let delta = if (Delta::ZERO..=MAX_UDELAY_DELTA).contains(&delta) {
         delta
     } else {
+        pr_warn_once!("attempted to udelay() with out of range delta\n");
         MAX_UDELAY_DELTA
     };
 
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 10 lines · Sang-Heon Jeon

fsleep() documents out-of-range input as a bug but does not check it, unlike udelay(). Add debug_assert! calls to catch it in debug builds.

Also add pr_warn_once! calls in both fsleep() and udelay() so the clamped path is observable at runtime.

Just FYI, I plan to queue this after the next merge window (for v6.4).

Best regards,
Andreas Hindborg

maintainer
#2 lore raw
Quoted 6 lines · Andreas Hindborg
Quoted 4 lines · Sang-Heon Jeon

fsleep() documents out-of-range input as a bug but does not check it, unlike udelay(). Add debug_assert! calls to catch it in debug builds.

Quoted 8 lines
Quoted 6 lines

Also add pr_warn_once! calls in both fsleep() and udelay() so the clamped path is observable at runtime.

Just FYI, I plan to queue this after the next merge window (for v6.4).

v7.4 of course.

Best regards,
Andreas Hindborg

maintainer Applied
#3 lore raw
In reply to Sang-Heon Jeon
Quoted 8 lines · Sang-Heon Jeon

fsleep() documents out-of-range input as a bug but does not check it, unlike udelay(). Add debug_assert! calls to catch it in debug builds.

Also add pr_warn_once! calls in both fsleep() and udelay() so the clamped path is observable at runtime.

[...]

Applied, thanks!

[1/1] rust: time: warn and assert out-of-range input in fsleep() and udelay()

      commit: 125a931f9d7f0fd93037f1c8c8154ab5b19678c8

Best regards,

Signature · 1 line
Andreas Hindborg <[email protected]>