rust: time: warn and assert out-of-range input in fsleep() and udelay()
Subsystems (2)
PATCH v2 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
[1] https://lore.kernel.org/all/[email protected]/
--- rust/kernel/time/delay.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
rust/kernel/time/delay.rs +9 -4
@@ -8,6 +8,7 @@//! C header: [`include/linux/delay.h`](srctree/include/linux/delay.h). use super ::Delta ; +use crate ::pr_warn_once;use crate ::prelude::*;/// Sleeps for a given duration at least.
@@ -18,8 +19,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 +32,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 +57,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 +73,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
Hello,
Quoted 7 lines · Sang-Heon Jeon
fsleep()documents out-of-range input as a bug but does not check it, unlikeudelay(). Adddebug_assert!calls to catch it in debug builds.Also add
pr_warn_once!calls in bothfsleep()andudelay()so the clamped path is observable at runtime.
We're now in a new merge window.
Gentle ping. Any feedback is welcome :)
Thanks.
Quoted 68 lines
Reviewed-by Onur Özkan <[email protected]>Signed-off-by Sang-Heon Jeon <[email protected]>---
Changes from v1 [1]
- Addpr_warn_oncetofsleep()andudelay()
- Add reviewed-by tag by Onur[1] https://lore.kernel.org/all/[email protected]/
--- rust/kernel/time/delay.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)rust/kernel/time/delay.rs +9
-5@@ -8,6 +8,7 @@//! C header: [`include/linux/delay.h`](srctree/include/linux/delay.h). use super ::Delta ; +use crate ::pr_warn_once;use crate ::prelude::*;/// Sleeps for a given duration at least. @@ -18,8 +19,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 +32,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 +57,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 +73,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 }; --
Quoted 2 lines
2.43.0
Best Regards,
Sang-Heon Jeon
Quoted 3 lines · Sang-Heon Jeon
We're now in a new merge window.
Gentle ping. Any feedback is welcome :)
In case it clarifies: the merge window is for PRs, i.e. it is when Linus merges everything from maintainers, rather than when maintainers apply the patches (generally speaking -- there are exceptions of course), which is a "previous step" if that makes sense.
This means that, in general, your patch may be applied after the merge window closes in a couple weeks, and then it would be sent to Linus in the next merge window in a couple months.
As for a bit of feedback below, in case it helps... :)
Quoted 1 line
Quoted 1 line
+/// will sleep for at least the maximum value in the range and warns once.
Not sure if they timekeeping maintainers will want to promise that it warns (and once) or not (i.e. it is EB, after all), but it sounds fine to me.
Quoted 1 line
Quoted 1 line
+ pr_warn_once!("attempted to
udelay()with out of range delta\n");
Perhaps:
pr_warn_once!("Attempted to call udelay() with out of range delta\n");
But it is up to the maintainers.
You could also consider splitting the patch into two, but it is not major.
Finally, perhaps you could take the chance to adopt the "kernel vertical" style since we are adding an import.
I hope this helps & thanks!
Cheers,
Miguel
Hello,
Quoted 6 lines · Miguel Ojeda
Quoted 3 lines · Sang-Heon Jeon
We're now in a new merge window.
Gentle ping. Any feedback is welcome :)
Quoted 8 lines
In case it clarifies: the merge window is for PRs, i.e. it is when Linus merges everything from maintainers, rather than when maintainers apply the patches (generally speaking -- there are exceptions of course), which is a "previous step" if that makes sense.
This means that, in general, your patch may be applied after the merge window closes in a couple weeks, and then it would be sent to Linus in the next merge window in a couple months.
You're right. Some subsystems have their own tree policies, but that's not my main point.
It was just a friendly nudge to keep the clock ticking :)
Quoted 8 lines
As for a bit of feedback below, in case it helps... :)
Quoted 1 line
Quoted 1 line
+/// will sleep for at least the maximum value in the range and warns once.
Not sure if they timekeeping maintainers will want to promise that it warns (and once) or not (i.e. it is EB, after all), but it sounds fine to me.
Quoted 7 lines
Quoted 1 line
Quoted 1 line
+ pr_warn_once!("attempted to
udelay()with out of range delta\n");Perhaps:
pr_warn_once!("Attempted to call `udelay()` with out of range delta\n");But it is up to the maintainers.
Both are fine with me.
Quoted 1 line
You could also consider splitting the patch into two, but it is not major.
IMHO, it's a bit too small to split, but I can do it if you'd prefer.
Quoted 2 lines
Finally, perhaps you could take the chance to adopt the "kernel vertical" style since we are adding an import.
Ah, I missed it. Thanks for pointing this out. I'll wait for the timekeeping maintainers' review and apply it together :)
Quoted 1 line
I hope this helps & thanks!
Thanks for the kind review. It really helps a lot.
Quoted 2 lines
Cheers,
Miguel
Best Regards,
Sang-Heon Jeon
Quoted 4 lines · Sang-Heon Jeon
You're right. Some subsystems have their own tree policies, but that's not my main point.
It was just a friendly nudge to keep the clock ticking :)
What I meant is that it is a global thing, i.e. in general when the merge window is open, it is usually already late to land patches that are not "urgent" for some reason.
Therefore, sending a ping may have a bigger impact (and thus better for your patch! :) if it is after the merge window closes, because that is usually when maintainers will take a look at a patch like this.
Quoted 1 line
IMHO, it's a bit too small to split, but I can do it if you'd prefer.
Yeah, in this particular case it is arguable whether they are truly independent things, given this is essentially about handling the Erroneous Behavior properly. Having said that, it is generally about whether things are independent, not so much small.
Now, on the other hand, for instance, the "kernel vertical" style cleanups are something we typically do in a preparatory commit. Here, though, the import block is quite trivial in this case, so it doesn't necessarily need a new commit, but it is a possibility that wouldn't hurt either.
Quoted 1 line
Thanks for the kind review. It really helps a lot.
You're welcome!
Cheers,
Miguel
Andreas Hindborg
maintainerQuoted 8 lines · Sang-Heon Jeon
Hello,
Quoted 5 lines · Sang-Heon Jeon
fsleep()documents out-of-range input as a bug but does not check it, unlikeudelay(). Adddebug_assert!calls to catch it in debug builds.
Quoted 5 lines
Quoted 2 lines
Also add
pr_warn_once!calls in bothfsleep()andudelay()so the clamped path is observable at runtime.We're now in a new merge window.
Gentle ping. Any feedback is welcome :)
It's on my list, I'll take another look as soon as possible.
Best regards,
Andreas Hindborg
Quoted 11 lines · Andreas Hindborg
Quoted 8 lines · Sang-Heon Jeon
Hello,
Quoted 5 lines · Sang-Heon Jeon
fsleep()documents out-of-range input as a bug but does not check it, unlikeudelay(). Adddebug_assert!calls to catch it in debug builds.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Also add
pr_warn_once!calls in bothfsleep()andudelay()so the clamped path is observable at runtime.We're now in a new merge window.
Gentle ping. Any feedback is welcome :)It's on my list, I'll take another look as soon as possible.
No rush. I really appreciate your time.
Quoted 3 lines
Best regards,
Andreas Hindborg
Best Regards,
Sang-Heon Jeon
Andreas Hindborg
maintainerQuoted 9 lines · Sang-Heon Jeon
fsleep()documents out-of-range input as a bug but does not check it, unlikeudelay(). Adddebug_assert!calls to catch it in debug builds.Also add
pr_warn_once!calls in bothfsleep()andudelay()so the clamped path is observable at runtime.Reviewed-by Onur Özkan <[email protected]>Signed-off-by Sang-Heon Jeon <[email protected]>
Looks good to me, but please follow code guidelines for imports. That is, combine the use statements for crate:: and add the empty comment on the last one [1].
With that: Reviewed-by: Andreas Hindborg <[email protected]>
Best regards,
Andreas Hindborg