Skip to contentSkip to conversation

rust: time: add debug assertions for out-of-range input in fsleep()

PATCH In review Sang-Heon Jeon 4 participants 11 messages May 8 – Sep 28

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

PATCH 1/1 rust: time: add debug assertions for out-of-range input in fsleep()

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.

---
Hello,

I found this small inconsistency while reading the code. The same check was applied to udelay() [1] but does not seem to have been extended to fsleep(). Please let me know if I have misunderstood anything.

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

Best Regards,
Sang-Heon Jeon

---
 rust/kernel/time/delay.rs | 3 +++
 1 file changed, 3 insertions(+)
rust/kernel/time/delay.rs +3 -0
index b5b1b42797a0..1e699fe647ff 100644
@@ -32,6 +32,9 @@ 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 {
Signature · 1 line
2.43.0
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 38 lines · On Sat, 09 May 2026 02:38:27 +0900 Sang-Heon Jeon <[email protected]> wrote:

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.

---
Hello,

I found this small inconsistency while reading the code. The same check was applied to udelay() [1] but does not seem to have been extended to fsleep(). Please let me know if I have misunderstood anything.

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

Best Regards,
Sang-Heon Jeon

---
 rust/kernel/time/delay.rs | 3 +++
 1 file changed, 3 insertions(+)
rust/kernel/time/delay.rs +3 -0
index b5b1b42797a0..1e699fe647ff 100644
@@ -32,6 +32,9 @@ 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 {
Signature · 1 line
2.43.0
maintainer
#2 lore raw
In reply to Sang-Heon Jeon
Quoted 13 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.

---
Hello,

I found this small inconsistency while reading the code. The same check was applied to udelay() [1] but does not seem to have been extended to fsleep(). Please let me know if I have misunderstood anything.

Quoted 4 lines

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

Best Regards,
Sang-Heon Jeon

Looks good to me. Could you add the warn_once and remove the todo as well? And add the warn_once to udelay as well, so both functions have same shape.

Thanks,
Andreas

reviewer
#3 lore raw
Quoted 15 lines · Andreas Hindborg
Quoted 13 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.

---
Hello,

I found this small inconsistency while reading the code. The same check was applied to udelay() [1] but does not seem to have been extended to fsleep(). Please let me know if I have misunderstood anything.

Quoted 8 lines
Quoted 4 lines

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

Best Regards,
Sang-Heon Jeon

Looks good to me. Could you add the warn_once and remove the todo as well? And add the warn_once to udelay as well, so both functions have same shape.

If warn_once is added then the debug assert would be unnecessary.

Best,
Gary

maintainer
#4 lore raw
Quoted 18 lines · Gary Guo
Quoted 17 lines · Andreas Hindborg
Quoted 15 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.

---
Hello,

I found this small inconsistency while reading the code. The same check was applied to udelay() [1] but does not seem to have been extended to fsleep(). Please let me know if I have misunderstood anything.

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

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Best Regards,
Sang-Heon Jeon

Looks good to me. Could you add the warn_once and remove the todo as well? And add the warn_once to udelay as well, so both functions have same shape.

If warn_once is added then the debug assert would be unnecessary.

It is different mechanisms in different situations, right? pr_warn_once prints a warning once (and a stack trace depending on kernel configuration?). It would be enabled in production environments, and it allows the thread to continue executing after triggering.

debug_assert panics when debug assertions are enabled, otherwise is compiled out.

I don't think one makes the other unnecessary.

Best regards,
Andreas Hindborg

reviewer
#5 lore raw
Quoted 27 lines · Andreas Hindborg
Quoted 25 lines · Gary Guo
Quoted 23 lines · Andreas Hindborg
Quoted 13 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.

---
Hello,

I found this small inconsistency while reading the code. The same check was applied to udelay() [1] but does not seem to have been extended to fsleep(). Please let me know if I have misunderstood anything.

Quoted 4 lines

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

Best Regards,
Sang-Heon Jeon

Looks good to me. Could you add the warn_once and remove the todo as well? And add the warn_once to udelay as well, so both functions have same shape.

Quoted 6 lines
Quoted 1 line

If warn_once is added then the debug assert would be unnecessary.

It is different mechanisms in different situations, right? pr_warn_once prints a warning once (and a stack trace depending on kernel configuration?). It would be enabled in production environments, and it allows the thread to continue executing after triggering.

The existing comment and your message says warn_once, not pr_warn_once.

Quoted 5 lines

debug_assert panics when debug assertions are enabled, otherwise is compiled out.

I don't think one makes the other unnecessary.

I don't think there should be two checks of the same thing. Even if we want different behaviour depending on if debug assertions are enabled or not, it should be a single macro that switches behaviour based on that config.

Best,
Gary

maintainer
#6 lore raw
Quoted 28 lines · Gary Guo
Quoted 27 lines · Andreas Hindborg
Quoted 25 lines · Gary Guo
Quoted 23 lines · Andreas Hindborg
Quoted 13 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.

---
Hello,

I found this small inconsistency while reading the code. The same check was applied to udelay() [1] but does not seem to have been extended to fsleep(). Please let me know if I have misunderstood anything.

Quoted 4 lines

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

Best Regards,
Sang-Heon Jeon

Looks good to me. Could you add the warn_once and remove the todo as well? And add the warn_once to udelay as well, so both functions have same shape.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

If warn_once is added then the debug assert would be unnecessary.

It is different mechanisms in different situations, right? pr_warn_once prints a warning once (and a stack trace depending on kernel configuration?). It would be enabled in production environments, and it allows the thread to continue executing after triggering.

The existing comment and your message says warn_once, not pr_warn_once.

I actually mixed these up, thanks for pointing that out. We do not have warn_once! yet.

Quoted 10 lines
Quoted 6 lines

debug_assert panics when debug assertions are enabled, otherwise is compiled out.

I don't think one makes the other unnecessary.

I don't think there should be two checks of the same thing. Even if we want different behaviour depending on if debug assertions are enabled or not, it should be a single macro that switches behaviour based on that config.

Apparently this was discussed in start of April [1] (I somehow missed it), and as far as I can tell, consensus is that we do not use WARN_ONCE. Instead we use debug_assert with pr_warn_once. That sounds reasonable to me.

This can be a combined macro or open coded, either way is fine with me.

I think we should add the debug_assert as suggested by this patch, add the pr_warn_once in the not taken branch, and remove the TODO about WARN_ONCE. We can bike-shed a combined macro on the side.

Sounds reasonable @Gary?

Best regards,
Andreas Hindborg

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

#7 lore raw
Quoted 50 lines · Andreas Hindborg
Quoted 28 lines · Gary Guo
Quoted 27 lines · Andreas Hindborg
Quoted 25 lines · Gary Guo
Quoted 23 lines · Andreas Hindborg
Quoted 13 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.

---
Hello,

I found this small inconsistency while reading the code. The same check was applied to udelay() [1] but does not seem to have been extended to fsleep(). Please let me know if I have misunderstood anything.

Quoted 4 lines

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

Best Regards,
Sang-Heon Jeon

Looks good to me. Could you add the warn_once and remove the todo as well? And add the warn_once to udelay as well, so both functions have same shape.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

If warn_once is added then the debug assert would be unnecessary.

It is different mechanisms in different situations, right? pr_warn_once prints a warning once (and a stack trace depending on kernel configuration?). It would be enabled in production environments, and it allows the thread to continue executing after triggering.

The existing comment and your message says warn_once, not pr_warn_once.

I actually mixed these up, thanks for pointing that out. We do not have warn_once! yet.

Quoted 7 lines
Quoted 6 lines

debug_assert panics when debug assertions are enabled, otherwise is compiled out.

I don't think one makes the other unnecessary.

Quoted 8 lines
Quoted 3 lines

I don't think there should be two checks of the same thing. Even if we want different behaviour depending on if debug assertions are enabled or not, it should be a single macro that switches behaviour based on that config.

Apparently this was discussed in start of April [1] (I somehow missed it), and as far as I can tell, consensus is that we do not use WARN_ONCE. Instead we use debug_assert with pr_warn_once. That sounds reasonable to me.

Above explanation is reasonable to me. So I'll add pr_warn_once to fsleep() and udelay() both.

Quoted 1 line

This can be a combined macro or open coded, either way is fine with me.

I don't have a strong preference about this either. Maybe we can handle it in a separate patch.

Quoted 5 lines

I think we should add the debug_assert as suggested by this patch, add the pr_warn_once in the not taken branch, and remove the TODO about WARN_ONCE. We can bike-shed a combined macro on the side.

Sounds reasonable @Gary?

Any other opinions are welcome including Gary.

Quoted 6 lines

Best regards,
Andreas Hindborg

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

Best Regards,
Sang-Heon Jeon

maintainer
#8 lore raw
Quoted 68 lines · Sang-Heon Jeon
Quoted 50 lines · Andreas Hindborg
Quoted 28 lines · Gary Guo
Quoted 27 lines · Andreas Hindborg
Quoted 25 lines · Gary Guo
Quoted 23 lines · Andreas Hindborg
Quoted 13 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.

---
Hello,

I found this small inconsistency while reading the code. The same check was applied to udelay() [1] but does not seem to have been extended to fsleep(). Please let me know if I have misunderstood anything.

Quoted 4 lines

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

Best Regards,
Sang-Heon Jeon

Looks good to me. Could you add the warn_once and remove the todo as well? And add the warn_once to udelay as well, so both functions have same shape.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

If warn_once is added then the debug assert would be unnecessary.

It is different mechanisms in different situations, right? pr_warn_once prints a warning once (and a stack trace depending on kernel configuration?). It would be enabled in production environments, and it allows the thread to continue executing after triggering.

The existing comment and your message says warn_once, not pr_warn_once.

I actually mixed these up, thanks for pointing that out. We do not have warn_once! yet.

Quoted 7 lines
Quoted 6 lines

debug_assert panics when debug assertions are enabled, otherwise is compiled out.

I don't think one makes the other unnecessary.

Quoted 8 lines
Quoted 3 lines

I don't think there should be two checks of the same thing. Even if we want different behaviour depending on if debug assertions are enabled or not, it should be a single macro that switches behaviour based on that config.

Apparently this was discussed in start of April [1] (I somehow missed it), and as far as I can tell, consensus is that we do not use WARN_ONCE. Instead we use debug_assert with pr_warn_once. That sounds reasonable to me.

Above explanation is reasonable to me. So I'll add pr_warn_once to fsleep() and udelay() both.

Quoted 1 line

This can be a combined macro or open coded, either way is fine with me.

I don't have a strong preference about this either. Maybe we can handle it in a separate patch.

Quoted 7 lines
Quoted 5 lines

I think we should add the debug_assert as suggested by this patch, add the pr_warn_once in the not taken branch, and remove the TODO about WARN_ONCE. We can bike-shed a combined macro on the side.

Sounds reasonable @Gary?

Any other opinions are welcome including Gary.

Still waiting for a new version of this patch. Let me know if I missed it somehow.

Best regards,
Andreas Hindborg

#9 lore raw

Hi, Andreas

2026년 9월 23일 (수) 오후 5:25, Andreas Hindborg <[email protected]>님이 작성:

Quoted 75 lines
Quoted 72 lines · Sang-Heon Jeon
Quoted 50 lines · Andreas Hindborg
Quoted 28 lines · Gary Guo
Quoted 27 lines · Andreas Hindborg
Quoted 25 lines · Gary Guo
Quoted 23 lines · Andreas Hindborg
Quoted 13 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.

---
Hello,

I found this small inconsistency while reading the code. The same check was applied to udelay() [1] but does not seem to have been extended to fsleep(). Please let me know if I have misunderstood anything.

Quoted 4 lines

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

Best Regards,
Sang-Heon Jeon

Looks good to me. Could you add the warn_once and remove the todo as well? And add the warn_once to udelay as well, so both functions have same shape.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

If warn_once is added then the debug assert would be unnecessary.

It is different mechanisms in different situations, right? pr_warn_once prints a warning once (and a stack trace depending on kernel configuration?). It would be enabled in production environments, and it allows the thread to continue executing after triggering.

The existing comment and your message says warn_once, not pr_warn_once.

I actually mixed these up, thanks for pointing that out. We do not have warn_once! yet.

Quoted 7 lines
Quoted 6 lines

debug_assert panics when debug assertions are enabled, otherwise is compiled out.

I don't think one makes the other unnecessary.

Quoted 8 lines
Quoted 3 lines

I don't think there should be two checks of the same thing. Even if we want different behaviour depending on if debug assertions are enabled or not, it should be a single macro that switches behaviour based on that config.

Apparently this was discussed in start of April [1] (I somehow missed it), and as far as I can tell, consensus is that we do not use WARN_ONCE. Instead we use debug_assert with pr_warn_once. That sounds reasonable to me.

Above explanation is reasonable to me. So I'll add pr_warn_once to fsleep() and udelay() both.

Quoted 1 line

This can be a combined macro or open coded, either way is fine with me.

I don't have a strong preference about this either. Maybe we can handle it in a separate patch.

Quoted 4 lines

I think we should add the debug_assert as suggested by this patch, add the pr_warn_once in the not taken branch, and remove the TODO about WARN_ONCE. We can bike-shed a combined macro on the side.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Sounds reasonable @Gary?

Any other opinions are welcome including Gary.

Still waiting for a new version of this patch. Let me know if I missed it somehow.

You already replied to v3 [1] :) Let me know if I missed it somehow as well.

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

Quoted 6 lines

Best regards,
Andreas Hindborg

Best regards,
Sang-Heon Jeon

maintainer
#10 lore raw
Quoted 80 lines · Sang-Heon Jeon

Hi, Andreas

2026년 9월 23일 (수) 오후 5:25, Andreas Hindborg <[email protected]>님이 작성:

Quoted 77 lines
Quoted 74 lines · Sang-Heon Jeon
Quoted 50 lines · Andreas Hindborg
Quoted 28 lines · Gary Guo
Quoted 27 lines · Andreas Hindborg
Quoted 25 lines · Gary Guo
Quoted 23 lines · Andreas Hindborg
Quoted 13 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.

---
Hello,

I found this small inconsistency while reading the code. The same check was applied to udelay() [1] but does not seem to have been extended to fsleep(). Please let me know if I have misunderstood anything.

Quoted 4 lines

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

Best Regards,
Sang-Heon Jeon

Looks good to me. Could you add the warn_once and remove the todo as well? And add the warn_once to udelay as well, so both functions have same shape.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

If warn_once is added then the debug assert would be unnecessary.

It is different mechanisms in different situations, right? pr_warn_once prints a warning once (and a stack trace depending on kernel configuration?). It would be enabled in production environments, and it allows the thread to continue executing after triggering.

The existing comment and your message says warn_once, not pr_warn_once.

I actually mixed these up, thanks for pointing that out. We do not have warn_once! yet.

Quoted 7 lines
Quoted 6 lines

debug_assert panics when debug assertions are enabled, otherwise is compiled out.

I don't think one makes the other unnecessary.

Quoted 8 lines
Quoted 3 lines

I don't think there should be two checks of the same thing. Even if we want different behaviour depending on if debug assertions are enabled or not, it should be a single macro that switches behaviour based on that config.

Apparently this was discussed in start of April [1] (I somehow missed it), and as far as I can tell, consensus is that we do not use WARN_ONCE. Instead we use debug_assert with pr_warn_once. That sounds reasonable to me.

Above explanation is reasonable to me. So I'll add pr_warn_once to fsleep() and udelay() both.

Quoted 1 line

This can be a combined macro or open coded, either way is fine with me.

I don't have a strong preference about this either. Maybe we can handle it in a separate patch.

Quoted 5 lines

I think we should add the debug_assert as suggested by this patch, add the pr_warn_once in the not taken branch, and remove the TODO about WARN_ONCE. We can bike-shed a combined macro on the side.

Sounds reasonable @Gary?

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Any other opinions are welcome including Gary.

Still waiting for a new version of this patch. Let me know if I missed it somehow.

You already replied to v3 [1] :) Let me know if I missed it somehow as well.

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

Thanks! It was a spreadsheet update error on my end.

Best regards,
Andreas Hindborg