[PATCH v2] rust: time: Avoid 64-bit integer division on 32-bit architectures

FUJITA Tomonori posted 1 patch 9 months, 1 week ago
rust/helpers/helpers.c |  1 +
rust/helpers/time.c    | 13 +++++++++++++
rust/kernel/time.rs    | 26 ++++++++++++++++++++++----
3 files changed, 36 insertions(+), 4 deletions(-)
create mode 100644 rust/helpers/time.c
[PATCH v2] rust: time: Avoid 64-bit integer division on 32-bit architectures
Posted by FUJITA Tomonori 9 months, 1 week ago
Avoid 64-bit integer division that 32-bit architectures don't
implement generally. This uses ktime_to_us() and ktime_to_ms()
instead.

The time abstraction needs i64 / u32 division so C's div_s64() can be
used but ktime_to_us() and ktime_to_ms() provide a simpler solution
for this time abstraction problem on 32-bit architectures.

32-bit ARM is the only 32-bit architecture currently supported by
Rust. Using the cfg attribute, only 32-bit architectures will call
ktime_to_us() and ktime_to_ms(), while the other 64-bit architectures
will continue to use the current code as-is to avoid the overhead.

One downside of calling the C's functions is that the as_micros/millis
methods can no longer be const fn. We stick with the simpler approach
unless there's a compelling need for a const fn.

Suggested-by: Arnd Bergmann <arnd@arndb.de>
Suggested-by: Boqun Feng <boqun.feng@gmail.com>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
v2:
- fix the helper to call ktime_to_[us|ms]
- use cfg to avoid calling ktime_to_[us|ms] on 64-bit archs
- update the commit message
v1: https://lore.kernel.org/lkml/20250501015818.226376-1-fujita.tomonori@gmail.com/
---
 rust/helpers/helpers.c |  1 +
 rust/helpers/time.c    | 13 +++++++++++++
 rust/kernel/time.rs    | 26 ++++++++++++++++++++++----
 3 files changed, 36 insertions(+), 4 deletions(-)
 create mode 100644 rust/helpers/time.c

diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c
index 1e7c84df7252..2ac088de050f 100644
--- a/rust/helpers/helpers.c
+++ b/rust/helpers/helpers.c
@@ -34,6 +34,7 @@
 #include "spinlock.c"
 #include "sync.c"
 #include "task.c"
+#include "time.c"
 #include "uaccess.c"
 #include "vmalloc.c"
 #include "wait.c"
diff --git a/rust/helpers/time.c b/rust/helpers/time.c
new file mode 100644
index 000000000000..3d31473bce08
--- /dev/null
+++ b/rust/helpers/time.c
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/ktime.h>
+
+s64 rust_helper_ktime_to_us(const ktime_t kt)
+{
+	return ktime_to_us(kt);
+}
+
+s64 rust_helper_ktime_to_ms(const ktime_t kt)
+{
+	return ktime_to_ms(kt);
+}
diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index a8089a98da9e..b0a8f3c0ba49 100644
--- a/rust/kernel/time.rs
+++ b/rust/kernel/time.rs
@@ -228,13 +228,31 @@ pub const fn as_nanos(self) -> i64 {
     /// Return the smallest number of microseconds greater than or equal
     /// to the value in the [`Delta`].
     #[inline]
-    pub const fn as_micros_ceil(self) -> i64 {
-        self.as_nanos().saturating_add(NSEC_PER_USEC - 1) / NSEC_PER_USEC
+    pub fn as_micros_ceil(self) -> i64 {
+        #[cfg(CONFIG_64BIT)]
+        {
+            self.as_nanos().saturating_add(NSEC_PER_USEC - 1) / NSEC_PER_USEC
+        }
+
+        #[cfg(not(CONFIG_64BIT))]
+        // SAFETY: It is always safe to call `ktime_to_us()` with any value.
+        unsafe {
+            bindings::ktime_to_us(self.as_nanos().saturating_add(NSEC_PER_USEC - 1))
+        }
     }
 
     /// Return the number of milliseconds in the [`Delta`].
     #[inline]
-    pub const fn as_millis(self) -> i64 {
-        self.as_nanos() / NSEC_PER_MSEC
+    pub fn as_millis(self) -> i64 {
+        #[cfg(CONFIG_64BIT)]
+        {
+            self.as_nanos() / NSEC_PER_MSEC
+        }
+
+        #[cfg(not(CONFIG_64BIT))]
+        // SAFETY: It is always safe to call `ktime_to_ms()` with any value.
+        unsafe {
+            bindings::ktime_to_ms(self.as_nanos())
+        }
     }
 }

base-commit: 679185904972421c570a1c337a8266835045012d
-- 
2.43.0
Re: [PATCH v2] rust: time: Avoid 64-bit integer division on 32-bit architectures
Posted by Andreas Hindborg 7 months, 3 weeks ago
On Fri, 02 May 2025 09:45:24 +0900, FUJITA Tomonori wrote:
> Avoid 64-bit integer division that 32-bit architectures don't
> implement generally. This uses ktime_to_us() and ktime_to_ms()
> instead.
> 
> The time abstraction needs i64 / u32 division so C's div_s64() can be
> used but ktime_to_us() and ktime_to_ms() provide a simpler solution
> for this time abstraction problem on 32-bit architectures.
> 
> [...]

Applied, thanks!

[1/1] rust: time: Avoid 64-bit integer division on 32-bit architectures
      commit: 1b7bbd5975279a1cf8d907fbc719f350031194c2

Best regards,
-- 
Andreas Hindborg <a.hindborg@kernel.org>
Re: [PATCH v2] rust: time: Avoid 64-bit integer division on 32-bit architectures
Posted by Andreas Hindborg 8 months, 2 weeks ago
FUJITA Tomonori <fujita.tomonori@gmail.com> writes:

> Avoid 64-bit integer division that 32-bit architectures don't
> implement generally. This uses ktime_to_us() and ktime_to_ms()
> instead.
>
> The time abstraction needs i64 / u32 division so C's div_s64() can be
> used but ktime_to_us() and ktime_to_ms() provide a simpler solution
> for this time abstraction problem on 32-bit architectures.
>
> 32-bit ARM is the only 32-bit architecture currently supported by
> Rust. Using the cfg attribute, only 32-bit architectures will call
> ktime_to_us() and ktime_to_ms(), while the other 64-bit architectures
> will continue to use the current code as-is to avoid the overhead.
>
> One downside of calling the C's functions is that the as_micros/millis
> methods can no longer be const fn. We stick with the simpler approach
> unless there's a compelling need for a const fn.
>
> Suggested-by: Arnd Bergmann <arnd@arndb.de>
> Suggested-by: Boqun Feng <boqun.feng@gmail.com>
> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>

Looks good to me.


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


Best regards,
Andreas Hindborg