Skip to contentSkip to conversation

rust: add u64 division helpers and fix the pwm_th1520 32-bit build

PATCH In review FUJITA Tomonori 3 participants 6 messages Nov 18

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v4 cover rust: add u64 division helpers and fix the pwm_th1520 32-bit build

From: FUJITA Tomonori <[email protected]>

This series fixes the 32-bit build of the TH1520 PWM driver with COMPILE_TEST.

Patch 1 adds a math module to the kernel crate with Rust versions of div64_u64() and DIV64_U64_ROUND_UP(). Patch 2 uses them in the driver.

The driver only needs u64 by u64 division. It does not need a 128-bit intermediate value like mul_u64_u64_div_u64().

Miguel, Uwe acked patch 2 and is fine with this series going through the Rust tree.

chenhan's patch [1] also adds rust/kernel/math.rs, with signed division helpers. Both use the same module header, so whichever is applied later needs only a small rebase.

Tested with the doctests on x86_64 and 32-bit ARM QEMU.

[1] https://lore.kernel.org/rust-for-linux/[email protected]/

v4:
- Add #[track_caller] to both helpers (Alice)
- Check for a zero divisor only on 32-bit, and use the panic of / on 64-bit (Alice)
- Add Uwe's Acked-by to patch 2
- Rebase on v7.4-rc3
v3: https://lore.kernel.org/rust-for-linux/[email protected]/
- Use div helpers instead of limiting COMPILE_TEST to 64BIT (Uwe)
- Add a math module with div64_u64() and div64_u64_round_up()
- Drop Cc: stable and the Reviewed-by tags, since the change is different
v2: https://lore.kernel.org/rust-for-linux/[email protected]/
- Fix a typo in the commit message
- Add Cc: stable, Reported-by and Closes tags
- Add Reviewed-by tags from Andreas and Gary
- Rebase on v7.3-rc5
v1: https://lore.kernel.org/all/[email protected]/

FUJITA Tomonori (2):
rust: math: add div64_u64() and div64_u64_round_up()
pwm: th1520: Fix 32-bit build with COMPILE_TEST

 drivers/pwm/pwm_th1520.rs |  8 +++--
 rust/kernel/lib.rs        |  1 +
 rust/kernel/math.rs       | 73 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 80 insertions(+), 2 deletions(-)
 create mode 100644 rust/kernel/math.rs

base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3

Signature · 1 line
2.43.0
#1 lore raw

PATCH v4 1/2 rust: math: add div64_u64() and div64_u64_round_up()

From: FUJITA Tomonori <[email protected]>

On 32-bit architectures, dividing a u64 by a u64 with the / operator makes the compiler emit a call to a compiler runtime function, such as __aeabi_uldivmod() on arm. The kernel does not provide these functions, so the build fails when Rust code that is also built for 32-bit architectures uses / on u64.

C code uses the helpers in include/linux/math64.h instead. Add a math module with Rust versions of two of them:

- div64_u64(), the same as div64_u64() in C.
- div64_u64_round_up(), the same as DIV64_U64_ROUND_UP() in C.

On 64-bit architectures, both use /. On 32-bit architectures, div64_u64() calls the C function. Both panic if the divisor is zero, as / does.

---
 rust/kernel/lib.rs  |  1 +
 rust/kernel/math.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 74 insertions(+)
 create mode 100644 rust/kernel/math.rs
rust/kernel/lib.rs +1 -0
index 1ab41129d557..482fef8be2e8 100644
@@ -100,6 +100,7 @@
 pub mod kunit;
 pub mod list;
 pub mod maple_tree;
+pub mod math;
 pub mod mem;
 pub mod miscdevice;
 pub mod mm;
rust/kernel/math.rs new file +73 -0

73 lines not on this page · raw message

Signature · 1 line
2.43.0
reviewer Reviewed-by on v4 1/2
#2 lore raw

Re: [PATCH v4 1/2] rust: math: add div64_u64() and div64_u64_round_up()

Quoted 11 lines · FUJITA Tomonori

From: FUJITA Tomonori <[email protected]>

On 32-bit architectures, dividing a u64 by a u64 with the / operator makes the compiler emit a call to a compiler runtime function, such as __aeabi_uldivmod() on arm. The kernel does not provide these functions, so the build fails when Rust code that is also built for 32-bit architectures uses / on u64.

C code uses the helpers in include/linux/math64.h instead. Add a math module with Rust versions of two of them:

Quoted 8 lines

- div64_u64(), the same as div64_u64() in C.
- div64_u64_round_up(), the same as DIV64_U64_ROUND_UP() in C.

On 64-bit architectures, both use /. On 32-bit architectures, div64_u64() calls the C function. Both panic if the divisor is zero, as / does.

reviewer Reviewed-by on v4 1/2
#3 lore raw

Re: [PATCH v4 1/2] rust: math: add div64_u64() and div64_u64_round_up()

In reply to FUJITA Tomonori
Quoted 11 lines · FUJITA Tomonori

From: FUJITA Tomonori <[email protected]>

On 32-bit architectures, dividing a u64 by a u64 with the / operator makes the compiler emit a call to a compiler runtime function, such as __aeabi_uldivmod() on arm. The kernel does not provide these functions, so the build fails when Rust code that is also built for 32-bit architectures uses / on u64.

C code uses the helpers in include/linux/math64.h instead. Add a math module with Rust versions of two of them:

Quoted 8 lines

- div64_u64(), the same as div64_u64() in C.
- div64_u64_round_up(), the same as DIV64_U64_ROUND_UP() in C.

On 64-bit architectures, both use /. On 32-bit architectures, div64_u64() calls the C function. Both panic if the divisor is zero, as / does.

Alice

#4 lore raw

PATCH v4 2/2 pwm: th1520: Fix 32-bit build with COMPILE_TEST

In reply to FUJITA Tomonori

From: FUJITA Tomonori <[email protected]>

Fix the following build error on 32-bit ARM with COMPILE_TEST:

ERROR: modpost: drivers/pwm/pwm_th1520.ko: symbol '__aeabi_uldivmod' undefined!

The driver divides u64 values when it converts between nanoseconds and clock cycles. On 32-bit ARM, these divisions are compiled to calls to __aeabi_uldivmod, which the kernel does not provide.

Use div64_u64() and div64_u64_round_up() for these divisions, in the same way as C drivers use div64_u64() and DIV64_U64_ROUND_UP(). The results do not change.

---
 drivers/pwm/pwm_th1520.rs | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/pwm/pwm_th1520.rs +6 -2
index 728993998446..bd05fe49650e 100644
@@ -31,6 +31,10 @@
         mem::DevresIoMem,
         Io, //
     },
+    math::{
+        div64_u64,
+        div64_u64_round_up, //
+    },
     of,
     platform,
     prelude::*,
@@ -68,13 +72,13 @@ const fn th1520_pwm_fp(n: u32) -> usize {
 fn ns_to_cycles(ns: u64, rate_hz: u64) -> u64 {
     const NSEC_PER_SEC_U64: u64 = time::NSEC_PER_SEC as u64;
 
-    ns.saturating_mul(rate_hz) / NSEC_PER_SEC_U64
+    div64_u64(ns.saturating_mul(rate_hz), NSEC_PER_SEC_U64)
 }
 
 fn cycles_to_ns(cycles: u32, rate_hz: u64) -> u64 {
     const NSEC_PER_SEC_U64: u64 = time::NSEC_PER_SEC as u64;
 
-    (u64::from(cycles) * NSEC_PER_SEC_U64).div_ceil(rate_hz)
+    div64_u64_round_up(u64::from(cycles) * NSEC_PER_SEC_U64, rate_hz)
 }
 
 /// Hardware-specific waveform representation for TH1520.
Signature · 1 line
2.43.0
reviewer Reviewed-by on v4 2/2
#5 lore raw

Re: [PATCH v4 2/2] pwm: th1520: Fix 32-bit build with COMPILE_TEST

Quoted 14 lines · FUJITA Tomonori

From: FUJITA Tomonori <[email protected]>

Fix the following build error on 32-bit ARM with COMPILE_TEST:

ERROR: modpost: drivers/pwm/pwm_th1520.ko: symbol '__aeabi_uldivmod' undefined!

The driver divides u64 values when it converts between nanoseconds and clock cycles. On 32-bit ARM, these divisions are compiled to calls to __aeabi_uldivmod, which the kernel does not provide.

Use div64_u64() and div64_u64_round_up() for these divisions, in the same way as C drivers use div64_u64() and DIV64_U64_ROUND_UP(). The results do not change.

Quoted 6 lines

Alice