[PATCH v3 3/3] rust: math: add signed division and remainder helpers

From: chenhan

Date: Sat Oct 17 2026 - 07:12:36 EST


Delta's division and remainder implementations operate on integers and
do not depend on time units. Move them into a math module to provide a
common home for arithmetic helpers, with Delta as the initial user.

Expose div64_s64() and rem_s64() so driver crates can use these operations
without constructing time values. The division helper takes two i64
operands; the remainder helper takes an i64 dividend and an i32 divisor
and returns an i64. Both keep Rust's signed operator semantics, including
the unconditional panic checks, and use the existing C helpers on 32-bit.

Keep the Delta API and its panic documentation unchanged. This is separate
from the preceding behavior fix so that introducing the math module is
not required for a backport.

Assisted-by: LLM
Signed-off-by: chenhan <chenhan0017.work@xxxxxxxxx>
---
rust/kernel/lib.rs | 1 +
rust/kernel/math.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++
rust/kernel/time.rs | 49 ++++------------------------------
3 files changed, 70 insertions(+), 44 deletions(-)
create mode 100644 rust/kernel/math.rs

diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index 4d5c96ddc49c..60c2af705b47 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -98,6 +98,7 @@
pub mod kunit;
pub mod list;
pub mod maple_tree;
+pub mod math;
pub mod miscdevice;
pub mod mm;
pub mod module;
diff --git a/rust/kernel/math.rs b/rust/kernel/math.rs
new file mode 100644
index 000000000000..34ca097caac9
--- /dev/null
+++ b/rust/kernel/math.rs
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Integer arithmetic helpers.
+
+/// Divides an [`i64`] dividend by an [`i64`] divisor, rounding towards zero.
+///
+/// # Panics
+///
+/// Panics if `divisor` is zero, or if `dividend` is [`i64::MIN`] and `divisor` is `-1`.
+#[inline]
+pub fn div64_s64(dividend: i64, divisor: i64) -> i64 {
+ if divisor == 0 {
+ panic!("attempt to divide by zero");
+ }
+
+ if dividend == i64::MIN && divisor == -1 {
+ panic!("attempt to divide with overflow");
+ }
+
+ #[cfg(CONFIG_64BIT)]
+ {
+ dividend / divisor
+ }
+
+ #[cfg(not(CONFIG_64BIT))]
+ {
+ // SAFETY: The checks above exclude a zero divisor and `i64::MIN / -1`.
+ unsafe { bindings::div64_s64(dividend, divisor) }
+ }
+}
+
+/// Returns the remainder of an [`i64`] dividend divided by an [`i32`] divisor.
+///
+/// A non-zero remainder has the sign of `dividend`.
+///
+/// # Panics
+///
+/// Panics if `divisor` is zero, or if `dividend` is [`i64::MIN`] and `divisor` is `-1`.
+#[inline]
+pub fn rem_s64(dividend: i64, divisor: i32) -> i64 {
+ if divisor == 0 {
+ panic!("attempt to calculate the remainder with a divisor of zero");
+ }
+
+ if dividend == i64::MIN && divisor == -1 {
+ panic!("attempt to calculate the remainder with overflow");
+ }
+
+ #[cfg(CONFIG_64BIT)]
+ {
+ dividend % i64::from(divisor)
+ }
+
+ #[cfg(not(CONFIG_64BIT))]
+ {
+ let mut rem = 0;
+
+ // SAFETY: The checks above exclude a zero divisor and `i64::MIN / -1`.
+ // `&mut rem` provides a valid, aligned, writable pointer for this call.
+ unsafe { bindings::div_s64_rem(dividend, divisor, &mut rem) };
+
+ i64::from(rem)
+ }
+}
diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index 4415e91a5227..e297d327953f 100644
--- a/rust/kernel/time.rs
+++ b/rust/kernel/time.rs
@@ -27,6 +27,8 @@
use core::marker::PhantomData;
use core::ops;

+use crate::math;
+
pub mod delay;
pub mod hrtimer;

@@ -418,24 +420,7 @@ impl ops::Div for Delta {
/// 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
- }
-
- #[cfg(not(CONFIG_64BIT))]
- {
- // SAFETY: The checks above exclude a zero divisor and `i64::MIN / -1`.
- unsafe { bindings::div64_s64(self.value, rhs.value) }
- }
+ math::div64_s64(self.value, rhs.value)
}
}

@@ -634,32 +619,8 @@ pub fn to_jiffies_timeout(self) -> Delta<Jiffy> {
/// 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 {
- value: self.as_nanos() % i64::from(divisor),
- }
- }
-
- #[cfg(not(CONFIG_64BIT))]
- {
- let mut rem = 0;
-
- // SAFETY: The checks above exclude a zero divisor and `i64::MIN / -1`.
- // `&mut rem` provides a valid, aligned, writable pointer for this call.
- unsafe { bindings::div_s64_rem(self.as_nanos(), divisor, &mut rem) };
-
- Self {
- value: i64::from(rem),
- }
+ Self {
+ value: math::rem_s64(self.as_nanos(), divisor),
}
}
}
--
2.34.1