pwm: th1520: Fix 32-bit build with COMPILE_TEST
Subsystems (5)
v3 cover pwm: th1520: Fix 32-bit build with COMPILE_TEST
From: FUJITA Tomonori <[email protected]>
This series fixes the 32-bit build of the TH1520 PWM driver with COMPILE_TEST. v2 limited COMPILE_TEST to 64BIT. Uwe asked to use the div helpers instead, so v3 does that.
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().
Uwe, are you OK with this series going through the Rust tree?
chenhan's series [1] also adds rust/kernel/math.rs, with signed division helpers. Both series 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]/
v3:
- 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 | 71 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 rust/kernel/math.rs
base-commit: c339f4010dea8b0d754f84ace70cd1b089878ef8
Signature · 1 line
2.43.0
PATCH v3 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 | 71 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 rust/kernel/math.rs
rust/kernel/lib.rs +1 -0
@@ -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 +71 -0
71 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v3 2/2 pwm: th1520: Fix 32-bit build with COMPILE_TEST
In reply to FUJITA TomonoriFrom: 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
@@ -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
Hello,
Quoted 45 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()anddiv64_u64_round_up()for these divisions, in the same way as C drivers usediv64_u64()andDIV64_U64_ROUND_UP(). The results do not change.Fixes 3c180003dffb ("pwm: th1520: Fix missing Kconfig dependencies")Reported-by Andreas Hindborg <[email protected]>Suggested-by Uwe Kleine-König <[email protected]>Assisted-by LLMSigned-off-by FUJITA Tomonori <[email protected]>--- drivers/pwm/pwm_th1520.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)drivers/pwm/pwm_th1520.rs +5
-1@@ -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 )
drivers/pwm/pwm_th1520.rs const fn th1520_pwm_fp(n: u32) -> usize { · line 84
Quoted 10 lines
drivers/pwm/pwm_th1520.rs +1
-1}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.
For me (being at most half-literate in Rust) it looks ok, and I'm fine to let this patch go in together with the first patch of this series via the Rust tree.
@Michal: If you have objections speak up quick!
Best regards
Uwe
Hi Tomonori,
Thanks for CC'ing me.
Quoted 2 lines
Both series use the same module header, so whichever is applied later needs only a small rebase.
Agreed. My div64_s64() and div_s64_rem() can share math.rs with your div64_u64() and div64_u64_round_up(). If your series lands first, I will rebase patch 3 onto that module, preserving your unsigned helpers and the common module header.
I have now posted v5 [1], which only changes the SAFETY comments relative to v4. The first two patches still fix Delta independently of the math module, so the behavior fix can be taken separately from the extraction.
[1] https://lore.kernel.org/rust-for-linux/[email protected]/
Best regards,
chenhan
Hi Uwe,
Quoted 37 lines · On Fri, 30 Oct 2026 11:02:07 +0900 FUJITA Tomonori <[email protected]> wrote:
From: FUJITA Tomonori <[email protected]>
This series fixes the 32-bit build of the TH1520 PWM driver with
COMPILE_TEST. v2 limitedCOMPILE_TESTto 64BIT. Uwe asked to use the div helpers instead, so v3 does that.Patch 1 adds a math module to the kernel crate with Rust versions of
div64_u64()andDIV64_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().Uwe, are you OK with this series going through the Rust tree?
chenhan's series [1] also adds
rust/kernel/math.rs, with signed division helpers. Both series use the same module header, so whichever is applied later needs only a small rebase.Tested with the doctests on
x86_64and 32-bit ARM QEMU.[1] https://lore.kernel.org/rust-for-linux/[email protected]/
v3:
- Use div helpers instead of limitingCOMPILE_TESTto 64BIT (Uwe)
- Add a math module withdiv64_u64()anddiv64_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: adddiv64_u64()anddiv64_u64_round_up()
pwm: th1520: Fix 32-bit build withCOMPILE_TEST
Quoted 5 lines
drivers/pwm/pwm_th1520.rs | 8 +++-- rust/kernel/lib.rs | 1 + rust/kernel/math.rs | 71 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 rust/kernel/math.rs
Gentle ping.
Could you take a look at patch 2? Are you OK with this series going through the Rust tree?