Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH v5 1/3] rust: time: rename the rem_nanos() divisor
       [not found] <20261031103206.24051-1-chenhan0017.work@gmail.com>
@ 2026-10-31 10:32 ` chenhan
  2026-11-02 14:36   ` Andreas Hindborg
  2026-11-12 12:47   ` Andreas Hindborg
  2026-10-31 10:32 ` [PATCH v5 2/3] rust: time: make Delta division and remainder fail consistently chenhan
  1 sibling, 2 replies; 7+ messages in thread
From: chenhan @ 2026-10-31 10:32 UTC (permalink / raw)
  To: rust-for-linux
  Cc: a.hindborg, boqun, fujita.tomonori, frederic, lyude, tglx,
	anna-maria, jstultz, sboyd, ojeda, gary, bjorn3_gh, lossin,
	aliceryhl, tmgross, dakr, daniel.almeida, tamird, acourbot, work,
	miguel.ojeda.sandonis, tomo, georgeandrout13, linux-kernel,
	stable

The argument to rem_nanos() is the divisor, while self holds the
dividend. Rename the argument and correct the documentation. Drop the
architecture-specific implementation explanation from the public API
documentation; the argument type already states the divisor's width.

This does not change the function's type or behavior. It is a preparation
for documenting and checking the invalid divisors in the next patch.

Cc: stable@vger.kernel.org # prerequisite for the Delta division/remainder fix
Reviewed-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
Assisted-by: LLM
Signed-off-by: chenhan <chenhan0017.work@gmail.com>
---
Changes since v4: none.

 rust/kernel/time.rs | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index 5ae377e70dd8..16b44e82d2ac 100644
--- a/rust/kernel/time.rs
+++ b/rust/kernel/time.rs
@@ -614,16 +614,13 @@ pub fn to_jiffies_timeout(self) -> Delta<Jiffy> {
         Delta::<Jiffy>::from_jiffies(jiffies)
     }
 
-    /// Return `self % dividend` where `dividend` is in nanoseconds.
-    ///
-    /// The kernel doesn't have any emulation for `s64 % s64` on 32 bit platforms, so this is
-    /// limited to 32 bit dividends.
+    /// Return `self % divisor` where `divisor` is in nanoseconds.
     #[inline]
-    pub fn rem_nanos(self, dividend: i32) -> Self {
+    pub fn rem_nanos(self, divisor: i32) -> Self {
         #[cfg(CONFIG_64BIT)]
         {
             Self {
-                value: self.as_nanos() % i64::from(dividend),
+                value: self.as_nanos() % i64::from(divisor),
             }
         }
 
@@ -632,7 +629,7 @@ pub fn rem_nanos(self, dividend: i32) -> Self {
             let mut rem = 0;
 
             // SAFETY: `rem` is in the stack, so we can always provide a valid pointer to it.
-            unsafe { bindings::div_s64_rem(self.as_nanos(), dividend, &mut rem) };
+            unsafe { bindings::div_s64_rem(self.as_nanos(), divisor, &mut rem) };
 
             Self {
                 value: i64::from(rem),
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v5 2/3] rust: time: make Delta division and remainder fail consistently
       [not found] <20261031103206.24051-1-chenhan0017.work@gmail.com>
  2026-10-31 10:32 ` [PATCH v5 1/3] rust: time: rename the rem_nanos() divisor chenhan
@ 2026-10-31 10:32 ` chenhan
  2026-10-31 11:44   ` FUJITA Tomonori
                     ` (2 more replies)
  1 sibling, 3 replies; 7+ messages in thread
From: chenhan @ 2026-10-31 10:32 UTC (permalink / raw)
  To: rust-for-linux
  Cc: a.hindborg, boqun, fujita.tomonori, frederic, lyude, tglx,
	anna-maria, jstultz, sboyd, ojeda, gary, bjorn3_gh, lossin,
	aliceryhl, tmgross, dakr, daniel.almeida, tamird, acourbot, work,
	miguel.ojeda.sandonis, tomo, georgeandrout13, linux-kernel,
	stable

Delta's division and rem_nanos() use Rust operators on 64-bit and C
helpers on 32-bit. The report shows that rem_nanos(0) can return 10 for
a 10 ns dividend on ARMv7, while the same call panics on x86-64. The
i64::MIN / -1 case also differs, and neither API documents these inputs.

Check for zero divisors and i64::MIN with -1 before the architecture-
specific code, and document the panic conditions on each method. This
matches Rust's signed division and remainder operators, including when
overflow checks are disabled. Valid-input results and API signatures
are unchanged; invalid inputs now panic on 32-bit too.

No in-tree calls to either API were found at the base commit. The checks
stay in the existing methods so the fix does not depend on introducing
a math module.

Fixes: 4521438fb076 ("rust: time: Implement basic arithmetic operations for Delta")
Reported-by: Georgios Androutsopoulos <georgeandrout13@gmail.com>
Closes: https://github.com/Rust-for-Linux/linux/issues/1254
Cc: stable@vger.kernel.org
Assisted-by: LLM
Signed-off-by: chenhan <chenhan0017.work@gmail.com>
---
Changes since v4:
- Explain that the preceding zero-divisor check establishes the C call
  precondition; keep MIN/-1 checks for Rust panic semantics.
- Retain the output-pointer validity explanation for the remainder call.

 rust/kernel/time.rs | 30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)

diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index 16b44e82d2ac..c9a2dc50ff4c 100644
--- a/rust/kernel/time.rs
+++ b/rust/kernel/time.rs
@@ -412,8 +412,20 @@ fn mul_assign(&mut self, rhs: i64) {
 impl ops::Div for Delta {
     type Output = i64;
 
+    /// # Panics
+    ///
+    /// Panics if `rhs` is zero, or if `self` represents [`i64::MIN`] nanoseconds
+    /// and `rhs` represents `-1` nanosecond.
     #[inline]
     fn div(self, rhs: Self) -> Self::Output {
+        if rhs.value == 0 {
+            panic!("attempt to divide by zero");
+        }
+
+        if self.value == i64::MIN && rhs.value == -1 {
+            panic!("attempt to divide with overflow");
+        }
+
         #[cfg(CONFIG_64BIT)]
         {
             self.value / rhs.value
@@ -421,7 +433,7 @@ fn div(self, rhs: Self) -> Self::Output {
 
         #[cfg(not(CONFIG_64BIT))]
         {
-            // SAFETY: This function is always safe to call regardless of the input values
+            // SAFETY: `rhs.value` is non-zero due to the check above.
             unsafe { bindings::div64_s64(self.value, rhs.value) }
         }
     }
@@ -615,8 +627,21 @@ pub fn to_jiffies_timeout(self) -> Delta<Jiffy> {
     }
 
     /// Return `self % divisor` where `divisor` is in nanoseconds.
+    ///
+    /// # Panics
+    ///
+    /// Panics if `divisor` is zero, or if `self` represents [`i64::MIN`]
+    /// nanoseconds and `divisor` is `-1`.
     #[inline]
     pub fn rem_nanos(self, divisor: i32) -> Self {
+        if divisor == 0 {
+            panic!("attempt to calculate the remainder with a divisor of zero");
+        }
+
+        if self.value == i64::MIN && divisor == -1 {
+            panic!("attempt to calculate the remainder with overflow");
+        }
+
         #[cfg(CONFIG_64BIT)]
         {
             Self {
@@ -628,7 +653,8 @@ pub fn rem_nanos(self, divisor: i32) -> Self {
         {
             let mut rem = 0;
 
-            // SAFETY: `rem` is in the stack, so we can always provide a valid pointer to it.
+            // SAFETY: `divisor` is non-zero due to the check above.
+            // `&mut rem` provides a valid, aligned, writable pointer for this call.
             unsafe { bindings::div_s64_rem(self.as_nanos(), divisor, &mut rem) };
 
             Self {
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v5 2/3] rust: time: make Delta division and remainder fail consistently
  2026-10-31 10:32 ` [PATCH v5 2/3] rust: time: make Delta division and remainder fail consistently chenhan
@ 2026-10-31 11:44   ` FUJITA Tomonori
  2026-11-02 14:51   ` Andreas Hindborg
  2026-11-12 12:47   ` Andreas Hindborg
  2 siblings, 0 replies; 7+ messages in thread
From: FUJITA Tomonori @ 2026-10-31 11:44 UTC (permalink / raw)
  To: chenhan0017.work
  Cc: rust-for-linux, a.hindborg, boqun, fujita.tomonori, frederic,
	lyude, tglx, anna-maria, jstultz, sboyd, ojeda, gary, bjorn3_gh,
	lossin, aliceryhl, tmgross, dakr, daniel.almeida, tamird,
	acourbot, work, miguel.ojeda.sandonis, tomo, georgeandrout13,
	linux-kernel, stable

On Sat, 31 Oct 2026 18:32:05 +0800
chenhan <chenhan0017.work@gmail.com> wrote:

> Delta's division and rem_nanos() use Rust operators on 64-bit and C
> helpers on 32-bit. The report shows that rem_nanos(0) can return 10 for
> a 10 ns dividend on ARMv7, while the same call panics on x86-64. The
> i64::MIN / -1 case also differs, and neither API documents these inputs.
> 
> Check for zero divisors and i64::MIN with -1 before the architecture-
> specific code, and document the panic conditions on each method. This
> matches Rust's signed division and remainder operators, including when
> overflow checks are disabled. Valid-input results and API signatures
> are unchanged; invalid inputs now panic on 32-bit too.
> 
> No in-tree calls to either API were found at the base commit. The checks
> stay in the existing methods so the fix does not depend on introducing
> a math module.
> 
> Fixes: 4521438fb076 ("rust: time: Implement basic arithmetic operations for Delta")
> Reported-by: Georgios Androutsopoulos <georgeandrout13@gmail.com>
> Closes: https://github.com/Rust-for-Linux/linux/issues/1254
> Cc: stable@vger.kernel.org
> Assisted-by: LLM
> Signed-off-by: chenhan <chenhan0017.work@gmail.com>

Reviewed-by: FUJITA Tomonori <fujita.tomonori@gmail.com>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v5 1/3] rust: time: rename the rem_nanos() divisor
  2026-10-31 10:32 ` [PATCH v5 1/3] rust: time: rename the rem_nanos() divisor chenhan
@ 2026-11-02 14:36   ` Andreas Hindborg
  2026-11-12 12:47   ` Andreas Hindborg
  1 sibling, 0 replies; 7+ messages in thread
From: Andreas Hindborg @ 2026-11-02 14:36 UTC (permalink / raw)
  To: chenhan, rust-for-linux
  Cc: boqun, fujita.tomonori, frederic, lyude, tglx, anna-maria,
	jstultz, sboyd, ojeda, gary, bjorn3_gh, lossin, aliceryhl,
	tmgross, dakr, daniel.almeida, tamird, acourbot, work,
	miguel.ojeda.sandonis, tomo, georgeandrout13, linux-kernel,
	stable

"chenhan" <chenhan0017.work@gmail.com> writes:

> The argument to rem_nanos() is the divisor, while self holds the
> dividend. Rename the argument and correct the documentation. Drop the
> architecture-specific implementation explanation from the public API
> documentation; the argument type already states the divisor's width.
>
> This does not change the function's type or behavior. It is a preparation
> for documenting and checking the invalid divisors in the next patch.
>
> Cc: stable@vger.kernel.org # prerequisite for the Delta division/remainder fix
> Reviewed-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
> Assisted-by: LLM
> Signed-off-by: chenhan <chenhan0017.work@gmail.com>

Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org>

Best regards,
Andreas Hindborg



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v5 2/3] rust: time: make Delta division and remainder fail consistently
  2026-10-31 10:32 ` [PATCH v5 2/3] rust: time: make Delta division and remainder fail consistently chenhan
  2026-10-31 11:44   ` FUJITA Tomonori
@ 2026-11-02 14:51   ` Andreas Hindborg
  2026-11-12 12:47   ` Andreas Hindborg
  2 siblings, 0 replies; 7+ messages in thread
From: Andreas Hindborg @ 2026-11-02 14:51 UTC (permalink / raw)
  To: chenhan, rust-for-linux
  Cc: boqun, fujita.tomonori, frederic, lyude, tglx, anna-maria,
	jstultz, sboyd, ojeda, gary, bjorn3_gh, lossin, aliceryhl,
	tmgross, dakr, daniel.almeida, tamird, acourbot, work,
	miguel.ojeda.sandonis, tomo, georgeandrout13, linux-kernel,
	stable

"chenhan" <chenhan0017.work@gmail.com> writes:

> Delta's division and rem_nanos() use Rust operators on 64-bit and C
> helpers on 32-bit. The report shows that rem_nanos(0) can return 10 for
> a 10 ns dividend on ARMv7, while the same call panics on x86-64. The
> i64::MIN / -1 case also differs, and neither API documents these inputs.
>
> Check for zero divisors and i64::MIN with -1 before the architecture-
> specific code, and document the panic conditions on each method. This
> matches Rust's signed division and remainder operators, including when
> overflow checks are disabled. Valid-input results and API signatures
> are unchanged; invalid inputs now panic on 32-bit too.
>
> No in-tree calls to either API were found at the base commit. The checks
> stay in the existing methods so the fix does not depend on introducing
> a math module.
>
> Fixes: 4521438fb076 ("rust: time: Implement basic arithmetic operations for Delta")
> Reported-by: Georgios Androutsopoulos <georgeandrout13@gmail.com>
> Closes: https://github.com/Rust-for-Linux/linux/issues/1254
> Cc: stable@vger.kernel.org
> Assisted-by: LLM
> Signed-off-by: chenhan <chenhan0017.work@gmail.com>
> ---
> Changes since v4:
> - Explain that the preceding zero-divisor check establishes the C call
>   precondition; keep MIN/-1 checks for Rust panic semantics.
> - Retain the output-pointer validity explanation for the remainder call.
>
>  rust/kernel/time.rs | 30 ++++++++++++++++++++++++++++--
>  1 file changed, 28 insertions(+), 2 deletions(-)
>
> diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
> index 16b44e82d2ac..c9a2dc50ff4c 100644
> --- a/rust/kernel/time.rs
> +++ b/rust/kernel/time.rs
> @@ -412,8 +412,20 @@ fn mul_assign(&mut self, rhs: i64) {
>  impl ops::Div for Delta {
>      type Output = i64;
>
> +    /// # Panics
> +    ///
> +    /// Panics if `rhs` is zero, or if `self` represents [`i64::MIN`] nanoseconds
> +    /// and `rhs` represents `-1` nanosecond.
>      #[inline]
>      fn div(self, rhs: Self) -> Self::Output {
> +        if rhs.value == 0 {
> +            panic!("attempt to divide by zero");
> +        }
> +
> +        if self.value == i64::MIN && rhs.value == -1 {
> +            panic!("attempt to divide with overflow");
> +        }
> +
>          #[cfg(CONFIG_64BIT)]
>          {
>              self.value / rhs.value
> @@ -421,7 +433,7 @@ fn div(self, rhs: Self) -> Self::Output {
>
>          #[cfg(not(CONFIG_64BIT))]
>          {
> -            // SAFETY: This function is always safe to call regardless of the input values
> +            // SAFETY: `rhs.value` is non-zero due to the check above.

Please add to the commit message that this safety requirement was wrong,
and the commit fixes it.

>              unsafe { bindings::div64_s64(self.value, rhs.value) }
>          }
>      }
> @@ -615,8 +627,21 @@ pub fn to_jiffies_timeout(self) -> Delta<Jiffy> {
>      }
>
>      /// Return `self % divisor` where `divisor` is in nanoseconds.
> +    ///
> +    /// # Panics
> +    ///
> +    /// Panics if `divisor` is zero, or if `self` represents [`i64::MIN`]
> +    /// nanoseconds and `divisor` is `-1`.
>      #[inline]
>      pub fn rem_nanos(self, divisor: i32) -> Self {
> +        if divisor == 0 {
> +            panic!("attempt to calculate the remainder with a divisor of zero");
> +        }
> +
> +        if self.value == i64::MIN && divisor == -1 {
> +            panic!("attempt to calculate the remainder with overflow");
> +        }
> +
>          #[cfg(CONFIG_64BIT)]
>          {
>              Self {
> @@ -628,7 +653,8 @@ pub fn rem_nanos(self, divisor: i32) -> Self {
>          {
>              let mut rem = 0;
>
> -            // SAFETY: `rem` is in the stack, so we can always provide a valid pointer to it.
> +            // SAFETY: `divisor` is non-zero due to the check above.
> +            // `&mut rem` provides a valid, aligned, writable pointer for this call.

Same.

Why did you change the wording of the original line?

Best regards,
Andreas Hindborg



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v5 2/3] rust: time: make Delta division and remainder fail consistently
  2026-10-31 10:32 ` [PATCH v5 2/3] rust: time: make Delta division and remainder fail consistently chenhan
  2026-10-31 11:44   ` FUJITA Tomonori
  2026-11-02 14:51   ` Andreas Hindborg
@ 2026-11-12 12:47   ` Andreas Hindborg
  2 siblings, 0 replies; 7+ messages in thread
From: Andreas Hindborg @ 2026-11-12 12:47 UTC (permalink / raw)
  To: rust-for-linux, chenhan
  Cc: boqun, fujita.tomonori, frederic, lyude, tglx, anna-maria,
	jstultz, sboyd, ojeda, gary, bjorn3_gh, lossin, aliceryhl,
	tmgross, dakr, daniel.almeida, tamird, acourbot, work, tomo,
	georgeandrout13, linux-kernel, stable, Miguel Ojeda


On Sat, 31 Oct 2026 18:32:05 +0800, chenhan wrote:
> Delta's division and rem_nanos() use Rust operators on 64-bit and C
> helpers on 32-bit. The report shows that rem_nanos(0) can return 10 for
> a 10 ns dividend on ARMv7, while the same call panics on x86-64. The
> i64::MIN / -1 case also differs, and neither API documents these inputs.
> 
> Check for zero divisors and i64::MIN with -1 before the architecture-
> specific code, and document the panic conditions on each method. This
> matches Rust's signed division and remainder operators, including when
> overflow checks are disabled. Valid-input results and API signatures
> are unchanged; invalid inputs now panic on 32-bit too.
> 
> [...]

Applied, thanks!

[2/3] rust: time: make Delta division and remainder fail consistently
      commit: 9687257afe8c2c2c257e2168bc6c44e5145dc73a

Best regards,
-- 
Andreas Hindborg <a.hindborg@kernel.org>



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v5 1/3] rust: time: rename the rem_nanos() divisor
  2026-10-31 10:32 ` [PATCH v5 1/3] rust: time: rename the rem_nanos() divisor chenhan
  2026-11-02 14:36   ` Andreas Hindborg
@ 2026-11-12 12:47   ` Andreas Hindborg
  1 sibling, 0 replies; 7+ messages in thread
From: Andreas Hindborg @ 2026-11-12 12:47 UTC (permalink / raw)
  To: rust-for-linux, chenhan
  Cc: boqun, fujita.tomonori, frederic, lyude, tglx, anna-maria,
	jstultz, sboyd, ojeda, gary, bjorn3_gh, lossin, aliceryhl,
	tmgross, dakr, daniel.almeida, tamird, acourbot, work, tomo,
	georgeandrout13, linux-kernel, stable, Miguel Ojeda


On Sat, 31 Oct 2026 18:32:04 +0800, chenhan wrote:
> The argument to rem_nanos() is the divisor, while self holds the
> dividend. Rename the argument and correct the documentation. Drop the
> architecture-specific implementation explanation from the public API
> documentation; the argument type already states the divisor's width.
> 
> This does not change the function's type or behavior. It is a preparation
> for documenting and checking the invalid divisors in the next patch.
> 
> [...]

Applied, thanks!

[1/3] rust: time: rename the rem_nanos() divisor
      commit: c6dce610f733d16849e5a8d491ddf080b839237c

Best regards,
-- 
Andreas Hindborg <a.hindborg@kernel.org>



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-12 12:49 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20261031103206.24051-1-chenhan0017.work@gmail.com>
2026-10-31 10:32 ` [PATCH v5 1/3] rust: time: rename the rem_nanos() divisor chenhan
2026-11-02 14:36   ` Andreas Hindborg
2026-11-12 12:47   ` Andreas Hindborg
2026-10-31 10:32 ` [PATCH v5 2/3] rust: time: make Delta division and remainder fail consistently chenhan
2026-10-31 11:44   ` FUJITA Tomonori
2026-11-02 14:51   ` Andreas Hindborg
2026-11-12 12:47   ` Andreas Hindborg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox