Re: [PATCH v2] rust: time: make Delta division and remainder fail consistently

From: Andreas Hindborg

Date: Thu Oct 01 2026 - 04:16:18 EST


"chenhan" <chenhan0017.work@xxxxxxxxx> writes:

> Delta's division and rem_nanos() use Rust operators on 64-bit and C
> helpers on 32-bit. The report shows that rem_nanos(0) can return 10 for
> a 10 ns dividend on ARMv7, while the same call panics on x86-64. The
> i64::MIN / -1 case also differs, and neither API documents these inputs.
>
> Use Rust's signed division and remainder semantics on both architectures,
> as discussed in the report. Div should behave like i64 division, and
> rem_nanos() supplies the corresponding remainder operation with a 32-bit
> divisor. Both reject zero divisors and i64::MIN with -1, even when Rust
> overflow checks are disabled.
>
> Check these inputs before entering the architecture-specific code and
> document the panic conditions. This preserves the API signatures and
> valid-input results, but intentionally makes invalid inputs panic on
> 32-bit too. No in-tree callers of either API were found at the base
> commit. Correct the rem_nanos() parameter name to divisor as well.
>
> Tested on x86-64 and ARMv7 QEMU with a built-in Rust module that passes
> runtime operands to both APIs. Separate boots verify each panic case;
> valid-input tests cover mixed signs, signed extrema and full-width
> division operands.
>
> Fixes: 4521438fb076 ("rust: time: Implement basic arithmetic operations for Delta")
> Reported-by: Georgios Androutsopoulos <georgeandrout13@xxxxxxxxx>
> Closes: https://github.com/Rust-for-Linux/linux/issues/1254
> Link: https://github.com/Rust-for-Linux/linux/issues/1254#issuecomment-5869573842
> Link: https://github.com/Rust-for-Linux/linux/issues/1254#issuecomment-5869925737
> Cc: stable@xxxxxxxxxxxxxxx
> Assisted-by: LLM
> Signed-off-by: chenhan <chenhan0017.work@xxxxxxxxx>
> ---
> Changes in v2:
> - Put the checks directly in div() and rem_nanos(), following Tomonori's
> suggestion. Each proposed helper had only one caller, and no other
> concrete users were found to justify a math module.

Even so, I think we should get the math module started and move the
division helpers there. This discussion is not the first on this
problem. Having the helpers available will help everyone, and create
precedence for further helpers down the line.

Best regards,
Andreas Hindborg