Add Delta::to_jiffies_timeout()
Subsystems (2)
v4 cover Add Delta::to_jiffies_timeout()
From: FUJITA Tomonori <[email protected]>
The first patch makes bindings::HZ resolve to CONFIG_HZ.
The second patch was previously posted as patch 4 of [1]; patches 1-3 of that series have been applied. It has been reworked.
[1] https://lore.kernel.org/rust-for-linux/[email protected]/
---
v4:
- Use i64::max() and u64::min() instead of the method call syntax (Gary)
- Add Reviewed-by from Gary
v3: https://lore.kernel.org/rust-for-linux/[email protected]/
- Add the first patch to set bindings::HZ to CONFIG_HZ.
- Drop the CONFIG_HZ workaround.
v2: https://lore.kernel.org/rust-for-linux/[email protected]/
- 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]/
FUJITA Tomonori (2):
rust: bindings: set HZ to CONFIG_HZ
rust: time: add Delta::to_jiffies_timeout() for timeout conversion
rust/bindings/lib.rs | 5 ++ rust/helpers/helpers.c | 1 + rust/helpers/math.c | 8 +++ rust/kernel/Kconfig.test | 10 ++++ rust/kernel/time.rs | 105 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 129 insertions(+) create mode 100644 rust/helpers/math.c
base-commit: 93f51579e7df248780214094418f205253383cc5
Signature · 1 line
2.43.0
PATCH v4 1/2 rust: bindings: set HZ to CONFIG_HZ
From: FUJITA Tomonori <[email protected]>
include/uapi/asm-generic/param.h defines HZ as __USER_HZ and include/asm-generic/param.h then sets it to CONFIG_HZ. bindgen keeps the first definition of a macro and ignores a later #undef, so the generated binding is __USER_HZ.
Redefine HZ in rust/bindings/lib.rs, the same way compat_ptr_ioctl is redefined there. Nothing in the tree reads bindings::HZ today, so this prevents accidental misuse rather than fixing a bug.
--- rust/bindings/lib.rs | 5 +++++ 1 file changed, 5 insertions(+)
rust/bindings/lib.rs +5 -0
@@ -83,3 +83,8 @@ mod bindings_helper {None } }; + +// `bindgen` keeps the first definition of a macro and ignores a later `#undef`. +// The generated `HZ` is therefore `__USER_HZ` from +// `include/uapi/asm-generic/param.h`, not `CONFIG_HZ`. +pub const HZ :u32 =CONFIG_HZ ;
Signature · 1 line
2.43.0
PATCH v4 2/2 rust: time: add Delta::to_jiffies_timeout() for timeout conversion
In reply to FUJITA TomonoriFrom: 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.
--- rust/helpers/helpers.c | 1 + rust/helpers/math.c | 8 +++ rust/kernel/Kconfig.test | 10 ++++ rust/kernel/time.rs | 105 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 124 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 +105 -0
120 lines not on this page · raw message
Signature · 1 line
2.43.0
Andreas Hindborg
maintainerRe: [PATCH v4 2/2] rust: time: add Delta::to_jiffies_timeout() for timeout conversion
Quoted 65 lines · FUJITA Tomonori
From: FUJITA Tomonori <[email protected]>
The boundaries of
__msecs_to_jiffies()andnsecs_to_jiffies64()depend on which HZ branch is compiled. WithCONFIG_HZ_300nsecs_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 atMAX_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, soceil(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 astatic_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. SinceMAX_JIFFY_OFFSETis 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.Reviewed-by Gary Guo <[email protected]>Signed-off-by FUJITA Tomonori <[email protected]>--- rust/helpers/helpers.c | 1 + rust/helpers/math.c | 8 +++ rust/kernel/Kconfig.test | 10 ++++ rust/kernel/time.rs | 105 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 rust/helpers/math.crust/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 +0
-0@@ -83,4 +83,14 @@ config RUST_BITFIELD_KUNIT_TEST If unsure, say N.
rust/kernel/Kconfig.test config RUST_BITFIELD_KUNIT_TEST · line 94
Quoted 10 lines
rust/kernel/Kconfig.test +9
-0+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.
Why would you gate out the tests? They do not look like they take long to run or anything.
Best regards,
Andreas Hindborg
Re: [PATCH v4 2/2] rust: time: add Delta::to_jiffies_timeout() for timeout conversion
Quoted 8 lines · On Fri, 25 Sep 2026 10:00:19 +0200 Andreas Hindborg <[email protected]>…
Quoted 8 lines
rust/kernel/Kconfig.test +0
-0@@ -83,4 +83,14 @@ config RUST_BITFIELD_KUNIT_TEST If unsure, say N.
Quoted 12 lines
rust/kernel/Kconfig.test config RUST_BITFIELD_KUNIT_TEST · line 94
Quoted 9 lines
rust/kernel/Kconfig.test +9
-0+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.Why would you gate out the tests? They do not look like they take long to run or anything.
Documentation/rust/testing.rst says:
Each test suite should be guarded by a Kconfig option in ``rust/kernel/Kconfig.test``.
I think that the reason is the kernel image size. From commit e74b7a3f5aee ("rust: tests: add Kconfig for KUnit test"):
All the tests are compiled unconditionally now, which adds ~200 kB to the kernel image for me on x86_64. As Rust matures, this bloating will inevitably grow.
Re: [PATCH v4 2/2] rust: time: add Delta::to_jiffies_timeout() for timeout conversion
Quoted 7 lines · FUJITA Tomonori
I think that the reason is the kernel image size. From commit
e74b7a3f5aee("rust: tests: addKconfigfor KUnit test"):All the tests are compiled unconditionally now, which adds ~200 kB to the kernel image for me on
x86_64. As Rust matures, this bloating will inevitably grow.
Quick note on that commit: it wasn't really "unconditional", i.e. one could still disable KUnit or make it m (I should have probably added a note to that commit message...). But some maintainers prefer fine-grained options, which is reasonable (though overdoing it may mean a lot of Kconfig options that most users do not care about).
Cheers,
Miguel
Andreas Hindborg
maintainerRe: [PATCH v4 2/2] rust: time: add Delta::to_jiffies_timeout() for timeout conversion
Quoted 9 lines · Miguel Ojeda
Quoted 7 lines · FUJITA Tomonori
I think that the reason is the kernel image size. From commit
e74b7a3f5aee("rust: tests: addKconfigfor KUnit test"):All the tests are compiled unconditionally now, which adds ~200 kB to the kernel image for me on
x86_64. As Rust matures, this bloating will inevitably grow.
Quoted 5 lines
Quick note on that commit: it wasn't really "unconditional", i.e. one could still disable KUnit or make it
m(I should have probably added a note to that commit message...). But some maintainers prefer fine-grained options, which is reasonable (though overdoing it may mean a lot ofKconfigoptions that most users do not care about).
I would prefer to just enable the tests as soon as kunit is enabled if that is OK.
Best regards,
Andreas Hindborg
Re: [PATCH v4 2/2] rust: time: add Delta::to_jiffies_timeout() for timeout conversion
Quoted 3 lines · Andreas Hindborg
I would prefer to just enable the tests as soon as kunit is enabled if that is OK.
Let me Cc David (and Yury) to see what he thinks -- the kernel probably wants to have at least a Kconfig for each test rather than having a default set that gets always enabled as soon as KUnit test, I think the question is mostly about how "wide" to make those Kconfigs.
Cheers,
Miguel
Andreas Hindborg
maintainerRe: [PATCH v4 2/2] rust: time: add Delta::to_jiffies_timeout() for timeout conversion
Quoted 9 lines · Miguel Ojeda
Quoted 3 lines · Andreas Hindborg
I would prefer to just enable the tests as soon as kunit is enabled if that is OK.
Let me Cc David (and Yury) to see what he thinks -- the kernel probably wants to have at least a
Kconfigfor each test rather than having a default set that gets always enabled as soon as KUnit test, I think the question is mostly about how "wide" to make those Kconfigs.
I'm used to having all the rust tests enabled when I hack on rust stuff. I would rather not have to go flick a ton of switches to enable all the stuff.
I think that most users that enable rust+kunit will be fine with enabling all rust kunit tests. I understand that there may be environments like embedded platforms where this is not feasible. Could we invert the situation so that we have one toggle to enable all our tests but make it possible to opt out of this behavior?
RUST=y
KUNIT=y
RUST_ALL_KUNIT_TESTS=y
That would be great.
Best regards,
Andreas Hindborg
Quoted 8 lines · FUJITA Tomonori
From: FUJITA Tomonori <[email protected]>
The first patch makes
bindings::HZresolve toCONFIG_HZ.The second patch was previously posted as patch 4 of [1]; patches 1-3 of that series have been applied. It has been reworked.
[...]
Applied, thanks!
[1/2] rust: bindings: set HZ to CONFIG_HZ
commit: 4ebfdb2c8b28eb8ef85b4e4ce65e55bd5a4df8d5
[2/2] rust: time: add Delta::to_jiffies_timeout() for timeout conversion
commit: a9aba476d42df2537403f97f185a2b1979d0dd97Best regards,
Signature · 1 line
Andreas Hindborg <[email protected]>