Skip to contentSkip to conversation

rust: time: keep Instant invariant when adding/subtracting Delta

PATCH Superseded Franklin-Qi 3 participants 8 messages Dec 1 – Dec 3

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: time: keep Instant invariant when adding/subtracting Delta

Instant documents inner in 0..=KTIME_MAX, but Add/Sub<Delta> only asserted non-negativity under CONFIG_RUST_OVERFLOW_CHECKS. With checks disabled, a negative result can be returned and later Instant-Instant subtraction may no longer be overflow-safe.

Always assert the result is non-negative, and document the panic in the operators' rustdoc.

Fixes 22b65a40574e ("rust: time: Implement Add<Delta>/Sub<Delta> for Instant")

---
Note: Per discussion on the GitHub issue with Miguel Ojeda, this uses an unconditional assert to preserve the Instant invariant (not clamp + warn_once), and documents the panic condition in the operators' rustdoc.

 rust/kernel/time.rs | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
rust/kernel/time.rs +12 -6
index b6a2aab12a50..f58f55e440af 100644
@@ -258,14 +258,17 @@ fn sub(self, other: Instant<C>) -> Delta {
 impl<T: ClockId> ops::Add<Delta> for Instant<T> {
     type Output = Self;
 
+    /// # Panics
+    ///
+    /// Panics if the result of the addition is negative.
     #[inline]
     fn add(self, rhs: Delta) -> Self::Output {
         // INVARIANT: With arithmetic over/underflow checks enabled, this will panic if we overflow
-        // (e.g. go above `KTIME_MAX`)
+        // (e.g. go above `KTIME_MAX`). The assert below also rejects a wrapped negative result when
+        // overflow checks are disabled.
         let res = self.inner + rhs.value;
 
-        // INVARIANT: With overflow checks enabled, we verify here that the value is >= 0
-        #[cfg(CONFIG_RUST_OVERFLOW_CHECKS)]
+        // INVARIANT: The result must be non-negative (`0..=KTIME_MAX`).
         assert!(res >= 0);
 
         Self {
@@ -278,14 +281,17 @@ fn add(self, rhs: Delta) -> Self::Output {
 impl<T: ClockId> ops::Sub<Delta> for Instant<T> {
     type Output = Self;
 
+    /// # Panics
+    ///
+    /// Panics if the result of the subtraction is negative.
     #[inline]
     fn sub(self, rhs: Delta) -> Self::Output {
         // INVARIANT: With arithmetic over/underflow checks enabled, this will panic if we overflow
-        // (e.g. go above `KTIME_MAX`)
+        // (e.g. go above `KTIME_MAX`). The assert below also rejects a wrapped negative result when
+        // overflow checks are disabled.
         let res = self.inner - rhs.value;
 
-        // INVARIANT: With overflow checks enabled, we verify here that the value is >= 0
-        #[cfg(CONFIG_RUST_OVERFLOW_CHECKS)]
+        // INVARIANT: The result must be non-negative (`0..=KTIME_MAX`).
         assert!(res >= 0);
 
         Self {
Signature · 1 line
2.55.0
maintainer
#1 lore raw
Quoted 13 lines · Franklin-Qi

Instant documents inner in 0..=KTIME_MAX, but Add/Sub<Delta> only asserted non-negativity under CONFIG_RUST_OVERFLOW_CHECKS. With checks disabled, a negative result can be returned and later Instant-Instant subtraction may no longer be overflow-safe.

Always assert the result is non-negative, and document the panic in the operators' rustdoc.

Fixes 22b65a40574e ("rust: time: Implement Add<Delta>/Sub<Delta> for Instant")
Quoted 4 lines

---
Note: Per discussion on the GitHub issue with Miguel Ojeda, this uses an unconditional assert to preserve the Instant invariant (not clamp + warn_once), and documents the panic condition in the operators' rustdoc.

I agree that allowing the invariant to be broken is wrong, but could you elaborate pros/cons wrt. clamping instead of panicking here?

I am more in favor of clamp + warn_once, since it does not crash the kernel.

We could also consider panic if overflow checks enabled and clamp + warn if not. But this may be too confusing?

Best regards,
Andreas Hindborg

#2 lore raw
Quoted 3 lines · Andreas Hindborg

I agree that allowing the invariant to be broken is wrong, but could you elaborate pros/cons wrt. clamping instead of panicking here?

+1, the key here is that discussion, which started in the issue.

Quoted 2 lines

We could also consider panic if overflow checks enabled and clamp + warn if not. But this may be too confusing?

I think that also makes sense -- it is essentially what we do with EB but under OVERFLOW_CHECKS instead of the other one.

That assumes we are OK with panicking here under a default configuration, but that is already the case before this patch...

Cheers,
Miguel

#3 lore raw
In reply to Andreas Hindborg

Hi Andreas, Miguel,

Thanks for the review.

Quoted 1 line · Andreas Hindborg

could you elaborate pros/cons wrt. clamping instead of panicking here?

Unconditional panic keeps the invariant simple to reason about, but it turns an out-of-range Delta into a kernel crash even in configurations where plain integer arithmetic would not panic. Clamping + warn_once keeps the kernel running and the invariant intact, at the cost of silently changing the value (hence the warning).

Quoted 2 lines

We could also consider panic if overflow checks enabled and clamp + warn if not. But this may be too confusing?

I agree with Miguel that this hybrid is consistent with the existing behaviour (the panic under CONFIG_RUST_OVERFLOW_CHECKS is not new), so I went with it in v2, which I'm sending in a separate thread shortly:

- CONFIG_RUST_OVERFLOW_CHECKS=y: panic when the result overflows or is negative, as before.
- Otherwise: saturating_add()/saturating_sub(), a negative result is clamped to 0 with a pr_warn_once!(), and a result past KTIME_MAX saturates to KTIME_MAX (instead of wrapping and then being clamped to 0).
- The panic/clamp conditions are documented in the rustdoc.

I also checked it in QEMU with a small test module: with overflow checks disabled the out-of-range cases are clamped/saturated and in-range arithmetic is unchanged (without the patch the same cases yield an out-of-range Instant); with overflow checks enabled they panic as before.

Best regards,
Franklin

#4 lore raw
Quoted 6 lines · Franklin-Qi

I also checked it in QEMU with a small test module: with overflow checks disabled the out-of-range cases are clamped/saturated and in-range arithmetic is unchanged (without the patch the same cases yield an out-of-range Instant); with overflow checks enabled they panic as before.

Whichever way the maintainers pick, I think it is a good opportunity to add some tests to check for that. Ideally as examples (doctests), and if we want to cover a lot of extra cases for some reason, then those can go as unittests (the file already has a mod tests, so it is a matter of adding them there).

If you do that, it can be in a second patch in the series.

Thanks!

Cheers,
Miguel

maintainer
#5 lore raw
In reply to Franklin-Qi
Quoted 28 lines · Franklin-Qi

Hi Andreas, Miguel,

Thanks for the review.

Quoted 1 line · Andreas Hindborg

could you elaborate pros/cons wrt. clamping instead of panicking here?

Unconditional panic keeps the invariant simple to reason about, but it turns an out-of-range Delta into a kernel crash even in configurations where plain integer arithmetic would not panic. Clamping + warn_once keeps the kernel running and the invariant intact, at the cost of silently changing the value (hence the warning).

Quoted 2 lines

We could also consider panic if overflow checks enabled and clamp + warn if not. But this may be too confusing?

I agree with Miguel that this hybrid is consistent with the existing behaviour (the panic under CONFIG_RUST_OVERFLOW_CHECKS is not new), so I went with it in v2, which I'm sending in a separate thread shortly:

- CONFIG_RUST_OVERFLOW_CHECKS=y: panic when the result overflows or is negative, as before.
- Otherwise: saturating_add()/saturating_sub(), a negative result is clamped to 0 with a pr_warn_once!(), and a result past KTIME_MAX saturates to KTIME_MAX (instead of wrapping and then being clamped to 0).
- The panic/clamp conditions are documented in the rustdoc.

Quoted 5 lines

I also checked it in QEMU with a small test module: with overflow checks disabled the out-of-range cases are clamped/saturated and in-range arithmetic is unchanged (without the patch the same cases yield an out-of-range Instant); with overflow checks enabled they panic as before.

I think you can add test for the clamping. Probably not for the panic though.

Best regards,
Andreas Hindborg

#6 lore raw
Quoted 3 lines · Andreas Hindborg

I think you can add test for the clamping. Probably not for the panic though.

We will have should_panic support soon, but yeah, we can test the rest for now.

Cheers,
Miguel

#7 lore raw
In reply to Andreas Hindborg

Hi Andreas, Miguel,

Thanks for the suggestion to add tests.

In v3 I am sending a second patch with KUnit tests for the in-range cases and for the clamping paths under CONFIG_RUST_OVERFLOW_CHECKS=n. Panic coverage can wait for should_panic support.

Best regards,
Franklin