From: chenhan <chenhan0017.work@gmail.com>
To: rust-for-linux@vger.kernel.org
Cc: a.hindborg@kernel.org, boqun@kernel.org,
fujita.tomonori@gmail.com, frederic@kernel.org, lyude@redhat.com,
tglx@kernel.org, anna-maria@linutronix.de, jstultz@google.com,
sboyd@kernel.org, ojeda@kernel.org, gary@garyguo.net,
bjorn3_gh@protonmail.com, lossin@kernel.org,
aliceryhl@google.com, tmgross@umich.edu, dakr@kernel.org,
daniel.almeida@collabora.com, tamird@kernel.org,
acourbot@nvidia.com, work@onurozkan.dev,
miguel.ojeda.sandonis@gmail.com, tomo@flapping.org,
georgeandrout13@gmail.com, linux-kernel@vger.kernel.org
Subject: [PATCH v4 3/3] rust: math: add signed division and remainder helpers
Date: Fri, 23 Oct 2026 21:04:13 +0800 [thread overview]
Message-ID: <20261023130413.178487-4-chenhan0017.work@gmail.com> (raw)
In-Reply-To: <20261023130413.178487-1-chenhan0017.work@gmail.com>
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() for an i64 dividend and divisor, and div_s64_rem()
for an i64 dividend and an i32 divisor. The latter returns an i64 quotient
and an i32 remainder, matching the results of the C helper without
exposing its output pointer. Delta's rem_nanos() uses the remainder and
converts it to i64. Both helpers retain Rust's signed operator semantics
and unconditional panic checks, using the existing C helpers on 32-bit.
Keep the Delta API and its panic conditions unchanged. Document the
quotient and remainder semantics and link to the corresponding C header.
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@gmail.com>
---
Changes since v3:
- Link the module documentation to include/linux/math64.h.
- Replace rem_s64() with div_s64_rem(), returning (i64, i32).
- Document quotient/remainder semantics and explain the i32 cast.
- Adapt Delta and directly test both public helpers.
FUJITA provided a Reviewed-by for v3. It is not carried here because
this revision changes the public API to return both results; a fresh
review of that change is requested.
rust/kernel/lib.rs | 1 +
rust/kernel/math.rs | 71 +++++++++++++++++++++++++++++++++++++++++++++
rust/kernel/time.rs | 50 ++++---------------------------
3 files changed, 78 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..aeb61d5427a7
--- /dev/null
+++ b/rust/kernel/math.rs
@@ -0,0 +1,71 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Integer arithmetic helpers.
+//!
+//! C header: [`include/linux/math64.h`](srctree/include/linux/math64.h).
+
+/// 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) }
+ }
+}
+
+/// Divides an [`i64`] dividend by an [`i32`] divisor, returning `(quotient, remainder)`.
+///
+/// The quotient is rounded towards zero. 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 div_s64_rem(dividend: i64, divisor: i32) -> (i64, i32) {
+ if divisor == 0 {
+ panic!("attempt to divide by zero");
+ }
+
+ if dividend == i64::MIN && divisor == -1 {
+ panic!("attempt to divide with overflow");
+ }
+
+ #[cfg(CONFIG_64BIT)]
+ {
+ let divisor = i64::from(divisor);
+ let quotient = dividend / divisor;
+ let remainder = dividend % divisor;
+
+ // CAST: |remainder| < |divisor| <= 2^31, so the remainder fits in i32.
+ (quotient, remainder as i32)
+ }
+
+ #[cfg(not(CONFIG_64BIT))]
+ {
+ let mut remainder = 0;
+
+ // SAFETY: The checks above exclude a zero divisor and `i64::MIN / -1`.
+ // `&mut remainder` provides a valid, aligned, writable pointer for this call.
+ let quotient = unsafe { bindings::div_s64_rem(dividend, divisor, &mut remainder) };
+
+ (quotient, remainder)
+ }
+}
diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index 4415e91a5227..f38e2fbd2670 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,9 @@ 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),
- }
+ let (_, remainder) = math::div_s64_rem(self.as_nanos(), divisor);
+ Self {
+ value: i64::from(remainder),
}
}
}
--
2.34.1
prev parent reply other threads:[~2026-10-23 13:05 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-23 13:04 [PATCH v4 0/3] rust: time: fix Delta arithmetic and add math helpers chenhan
2026-10-23 13:04 ` [PATCH v4 1/3] rust: time: rename the rem_nanos() divisor chenhan
2026-10-23 13:04 ` [PATCH v4 2/3] rust: time: make Delta division and remainder fail consistently chenhan
2026-10-23 13:04 ` chenhan [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261023130413.178487-4-chenhan0017.work@gmail.com \
--to=chenhan0017.work@gmail.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=anna-maria@linutronix.de \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=frederic@kernel.org \
--cc=fujita.tomonori@gmail.com \
--cc=gary@garyguo.net \
--cc=georgeandrout13@gmail.com \
--cc=jstultz@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=lyude@redhat.com \
--cc=miguel.ojeda.sandonis@gmail.com \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=sboyd@kernel.org \
--cc=tamird@kernel.org \
--cc=tglx@kernel.org \
--cc=tmgross@umich.edu \
--cc=tomo@flapping.org \
--cc=work@onurozkan.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®