rust: time: add Delta::to_jiffies_timeout() for timeout conversion
Subsystems (2)
PATCH v2 1/1 rust: time: add Delta::to_jiffies_timeout() for timeout conversion
From: FUJITA Tomonori <[email protected]>
The boundaries of __msecs_to_jiffies() and nsecs_to_jiffies64() depend on which HZ branch is compiled. With CONFIG_HZ_300 nsecs_to_jiffies64() overflows after 64.99 years, which is less than the 292 years a Delta can hold. With HZ=1000 a jiffy is a millisecond, so __msecs_to_jiffies() returns its argument unchanged and never caps it at MAX_JIFFY_OFFSET.
Compute the conversion in Rust with mul_u64_add_u64_div_u64() instead. It returns (a * b + c) / d, computing a * b internally in 128 bits, so ceil(nanos * HZ / NSEC_PER_SEC) needs no input clamp and rounds once. The bound then follows from the arithmetic: with HZ <= NSEC_PER_SEC, which a static_assert() checks, the result is at most the nanosecond count.
Unless the result saturates, the value is rounded up, so the timeout is never shorter than the requested span. It saturates at zero jiffies for a negative span, i.e. an immediate timeout, and at MAX_JIFFY_OFFSET, the upper bound the kernel uses for a jiffies span. Since MAX_JIFFY_OFFSET is derived from long, only 32 bit can reach it, and a saturated timeout there is finite, so it can be shorter than the requested span.
---
v2:
- Use mul_u64_add_u64_div_u64() instead of __msecs_to_jiffies().
- Dropped the as_millis_ceil() example and KUnit test patch; the conversion no longer uses as_millis_ceil(). Will send it separately.
v1: https://lore.kernel.org/rust-for-linux/[email protected]/
--- rust/helpers/helpers.c | 1 + rust/helpers/math.c | 8 +++ rust/kernel/Kconfig.test | 10 ++++ rust/kernel/time.rs | 109 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 128 insertions(+) create mode 100644 rust/helpers/math.c
rust/helpers/helpers.c +1 -0
@@ -73,6 +73,7 @@#include "kunit.c" #include "list.c" #include "maple_tree.c" +#include "math.c" #include "mm.c" #include "mutex.c" #include "net/genetlink.c"
rust/helpers/math.c new file +8 -0
@@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/math64.h> + +__rust_helper u64 rust_helper_mul_u64_add_u64_div_u64(u64 a, u64 b, u64 c, u64 d) +{ +return mul_u64_add_u64_div_u64 (a, b, c, d); +}
rust/kernel/Kconfig.test +10 -0
@@ -83,4 +83,14 @@ config RUST_BITFIELD_KUNIT_TEST If unsure, say N. +config RUST_TIME_KUNIT_TEST + bool "KUnit tests for the Rust time API" if !KUNIT_ALL_TESTS + default KUNIT_ALL_TESTS + help + This option enables KUnit tests for the Rust time API. + These are only for development and testing, not for regular + kernel use cases. + + If unsure, say N. + endif
rust/kernel/time.rs +109 -0
124 lines not on this page · raw message
base-commit: df2908090cda368b01ff43709f51890076c56157
Signature · 1 line
2.43.0
Alice Ryhl
reviewerQuoted 6 lines · FUJITA Tomonori
rust/helpers/math.c new file +0
-0
rust/helpers/math.c line 8
Quoted 8 lines
rust/helpers/math.c new file +8
-0@@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/math64.h> + +__rust_helper u64 rust_helper_mul_u64_add_u64_div_u64(u64 a, u64 b, u64 c, u64 d) +{ +return mul_u64_add_u64_div_u64 (a, b, c, d); +}
This looks like it's never an inline function, so I don't think a helper is needed.
rust/kernel/time.rs line 44
Quoted 11 lines
rust/kernel/time.rs +3
-0@@ -39,6 +39,10 @@/// The number of nanoseconds per second. pub const NSEC_PER_SEC :i64 = bindings::NSEC_PER_SEC as i64 ; +/// The C side `MAX_JIFFY_OFFSET`, i.e. `((LONG_MAX >> 1) - 1)`. It is the upper +/// bound the kernel uses for a jiffies span, not a wait-forever value. +const MAX_JIFFY_OFFSET :isize = (isize::MAX >>1 ) -1 ;
Should this be:
const MAX_JIFFY_OFFSET: Jiffies = (isize::MAX >> 1) - 1;
The Jiffies type alias is always the same as isize, but it may be more clear for readers to use Jiffies for the constant.
rust/kernel/time.rs pub fn as_millis_ceil(self) -> i64 { · line 593
Quoted 4 lines
rust/kernel/time.rs +4
-0+// `bindings::HZ` cannot be used here. bindgen keeps the first definition of a +// macro and ignores a later `#undef`, so `HZ` resolves to `__USER_HZ`, i.e. +// 100, from `include/uapi/asm-generic/param.h` instead of `CONFIG_HZ`. +const HZ :u64 = bindings::CONFIG_HZ as u64 ;
Sounds very error-prone. Consider fixing this in rust/bindings/lib.rs similar to compat_ptr_ioctl and then use bindings::HZ here. This will reduce the chance of others using bindings::HZ and getting the wrong value.
Alice
Quoted 10 lines · On Tue, 8 Sep 2026 12:45:58 +0000 Alice Ryhl <[email protected]> wrote:
Quoted 9 lines · FUJITA Tomonori
rust/helpers/math.c new file +3
-0@@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/math64.h>
Quoted 8 lines
rust/helpers/math.c line 8
Quoted 5 lines
rust/helpers/math.c new file +5
-0+ +__rust_helper u64 rust_helper_mul_u64_add_u64_div_u64(u64 a, u64 b, u64 c, u64 d) +{ +return mul_u64_add_u64_div_u64 (a, b, c, d); +}This looks like it's never an inline function, so I don't think a helper is needed.
It's an inline function on x86; arch/x86/include/asm/div64.h.
Quoted 14 lines
rust/kernel/time.rs line 44
Quoted 11 lines
rust/kernel/time.rs +3
-0@@ -39,6 +39,10 @@/// The number of nanoseconds per second. pub const NSEC_PER_SEC :i64 = bindings::NSEC_PER_SEC as i64 ; +/// The C side `MAX_JIFFY_OFFSET`, i.e. `((LONG_MAX >> 1) - 1)`. It is the upper +/// bound the kernel uses for a jiffies span, not a wait-forever value. +const MAX_JIFFY_OFFSET :isize = (isize::MAX >>1 ) -1 ;Should this be:
Quoted 4 lines
const
MAX_JIFFY_OFFSET: Jiffies = (isize::MAX>> 1) - 1;The Jiffies type alias is always the same as isize, but it may be more clear for readers to use Jiffies for the constant.
Jiffies is crate::ffi::c_ulong, i.e. usize, not isize. And it will be removed; replacing it with Delta<Jiffy> is what the series is for
https://lore.kernel.org/rust-for-linux/[email protected]/
Quoted 9 lines
rust/kernel/time.rs pub fn as_millis_ceil(self) -> i64 { · line 593
Quoted 4 lines
rust/kernel/time.rs +4
-0+// `bindings::HZ` cannot be used here. bindgen keeps the first definition of a +// macro and ignores a later `#undef`, so `HZ` resolves to `__USER_HZ`, i.e. +// 100, from `include/uapi/asm-generic/param.h` instead of `CONFIG_HZ`. +const HZ :u64 = bindings::CONFIG_HZ as u64 ;Sounds very error-prone. Consider fixing this in
rust/bindings/lib.rssimilar tocompat_ptr_ioctland then usebindings::HZhere. This will reduce the chance of others usingbindings::HZand getting the wrong value.
Good idea, thanks. I'll do that in v3: patch 1 fixes bindings::HZ in lib.rs, and this patch then uses it.
Alice Ryhl
reviewerQuoted 23 lines · FUJITA Tomonori
Quoted 10 lines · On Tue, 8 Sep 2026 12:45:58 +0000 Alice Ryhl <[email protected]> wrote:
Quoted 9 lines · FUJITA Tomonori
rust/helpers/math.c new file +3
-0@@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/math64.h> Quoted 8 lines
rust/helpers/math.c line 8
Quoted 5 lines
rust/helpers/math.c new file +5
-0+ +__rust_helper u64 rust_helper_mul_u64_add_u64_div_u64(u64 a, u64 b, u64 c, u64 d) +{ +return mul_u64_add_u64_div_u64 (a, b, c, d); +}This looks like it's never an inline function, so I don't think a helper is needed.
Quoted 1 line
It's an inline function on x86;
arch/x86/include/asm/div64.h.
ok
Quoted 16 lines
Quoted 16 lines
rust/kernel/time.rs line 44
Quoted 11 lines
rust/kernel/time.rs +3
-0@@ -39,6 +39,10 @@/// The number of nanoseconds per second. pub const NSEC_PER_SEC :i64 = bindings::NSEC_PER_SEC as i64 ; +/// The C side `MAX_JIFFY_OFFSET`, i.e. `((LONG_MAX >> 1) - 1)`. It is the upper +/// bound the kernel uses for a jiffies span, not a wait-forever value. +const MAX_JIFFY_OFFSET :isize = (isize::MAX >>1 ) -1 ;Should this be:
const
MAX_JIFFY_OFFSET: Jiffies = (isize::MAX>> 1) - 1;
Quoted 7 lines
Quoted 2 lines
The Jiffies type alias is always the same as isize, but it may be more clear for readers to use Jiffies for the constant.
Jiffies is
crate::ffi::c_ulong, i.e. usize, not isize. And it will be removed; replacing it with Delta<Jiffy> is what the series is for
Fair enough
Quoted 13 lines
Quoted 9 lines
rust/kernel/time.rs pub fn as_millis_ceil(self) -> i64 { · line 593
Quoted 4 lines
rust/kernel/time.rs +4
-0+// `bindings::HZ` cannot be used here. bindgen keeps the first definition of a +// macro and ignores a later `#undef`, so `HZ` resolves to `__USER_HZ`, i.e. +// 100, from `include/uapi/asm-generic/param.h` instead of `CONFIG_HZ`. +const HZ :u64 = bindings::CONFIG_HZ as u64 ;Sounds very error-prone. Consider fixing this in
rust/bindings/lib.rssimilar tocompat_ptr_ioctland then usebindings::HZhere. This will reduce the chance of others usingbindings::HZand getting the wrong value.Good idea, thanks. I'll do that in v3: patch 1 fixes
bindings::HZinlib.rs, and this patch then uses it.