rust: time: seconds based Instant
Subsystems (2)
cover rust: time: seconds based Instant
From: FUJITA Tomonori <[email protected]>
Mike's series added a free function ktime_get_real_seconds() [1]. Andreas and Boqun preferred a seconds based Instant instead. This series does that.
1/2 adds a unit type parameter to Instant, with no functional change. 2/2 adds the Sec unit and ClockId::ktime_get_seconds(). Instant<C, Sec> has only now() and as_secs(); there is no Delta<Sec>.
The now() doc comment in 1/2 changes the same line as [2]. Whichever is applied first, the other conflicts on that one line only.
Based on rust-next.
[1] https://lore.kernel.org/rust-for-linux/[email protected]/
[2] https://lore.kernel.org/rust-for-linux/[email protected]/
FUJITA Tomonori (2):
rust: time: make Instant generic over the time unit
rust: time: add Instant in seconds
rust/helpers/time.c | 10 ++++ rust/kernel/time.rs | 123 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 115 insertions(+), 18 deletions(-)
base-commit: d89e1d186ec9271bf78e62771779e9fc03bf0fc3
Signature · 1 line
2.43.0
PATCH 1/2 rust: time: make Instant generic over the time unit
From: FUJITA Tomonori <[email protected]>
Add a type parameter U to Instant, with Nsec as the default, so that an Instant in seconds can be added later. The value is stored as U::Repr, like Delta<U>.
U is bound by a new trait InstantUnit rather than TimeUnit. It reads the clock in its unit, and jiffies is not a clock, so Instant<C, Jiffy> must not exist.
now() stays one function on Instant<C, U>. With two inherent now(), one per unit, the existing Instant::now() calls do not compile (E0034, multiple applicable items). The other methods and operators are implemented for Instant<C, Nsec> only. Instant<C> still means Instant<C, Nsec>, so no user changes.
No functional change.
--- rust/kernel/time.rs | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-)
rust/kernel/time.rs +32 -12
@@ -179,36 +179,43 @@ fn ktime_get() -> bindings::ktime_t {/// A specific point in time. /// +/// The time is read from the clock given by the type parameter `C` (see +/// [`ClockId`]) and stored in the unit given by `U` (see [`InstantUnit`]); its +/// value has type `U::Repr`. `U` defaults to [`Nsec`], so a plain +/// [`Instant<C>`] is a point in time in nanoseconds. +/// /// # Invariants /// /// The `inner` value is in the range from 0 to `KTIME_MAX`. #[repr(transparent)] #[derive( PartialEq , PartialOrd , Eq , Ord )] -pub struct Instant <C :ClockId > { - inner: bindings::ktime_t , +pub struct Instant <C :ClockId ,U :InstantUnit =Nsec > { + inner:U ::Repr , _c:PhantomData <C >, } -impl <C :ClockId >Clone for Instant <C > { +impl <C :ClockId ,U :InstantUnit >Clone for Instant <C ,U > {fn clone (&self) ->Self { *self } } -impl <C :ClockId >Copy for Instant <C > {} +impl <C :ClockId ,U :InstantUnit >Copy for Instant <C ,U > {} -impl <C :ClockId >Instant <C > { -/// Get the current time from the clock source. +impl <C :ClockId ,U :InstantUnit >Instant <C ,U > { +/// Get the current time from the clock `C`. #[inline] pub fn now () ->Self { -// INVARIANT: The `ClockId::ktime_get()` function returns a value in the range -// from 0 to `KTIME_MAX`. +// INVARIANT: `InstantUnit` is sealed and its only impl, `Nsec`, calls +// `ClockId::ktime_get()`, which returns a value in the range from 0 to `KTIME_MAX`. Self { - inner:C ::ktime_get (), + inner:U ::now ::<C >(), _c:PhantomData , } } +} +impl <C :ClockId >Instant <C ,Nsec > {/// Return the amount of time elapsed since the [`Instant`]. #[inline] pub fn elapsed (&self) ->Delta {
@@ -243,7 +250,7 @@ pub(crate) unsafe fn from_ktime(ktime: bindings::ktime_t) -> Self { } } -impl <C :ClockId > ops::Sub for Instant <C > { +impl <C :ClockId > ops::Sub for Instant <C ,Nsec > {type Output =Delta ;// By the type invariant, it never overflows.
@@ -255,7 +262,7 @@ fn sub(self, other: Instant<C>) -> Delta { } } -impl <T :ClockId > ops::Add <Delta >for Instant <T > { +impl <T :ClockId > ops::Add <Delta >for Instant <T ,Nsec > {type Output =Self ;#[inline]
@@ -275,7 +282,7 @@ fn add(self, rhs: Delta) -> Self::Output { } } -impl <T :ClockId > ops::Sub <Delta >for Instant <T > { +impl <T :ClockId > ops::Sub <Delta >for Instant <T ,Nsec > {type Output =Self ;#[inline]
@@ -335,6 +342,19 @@ impl TimeUnit for Jiffy {const ZERO :Self ::Repr =0 ; } +/// A trait for time units that can be used with [`Instant`]. +pub trait InstantUnit :TimeUnit { +/// Return the current time of the clock `C` in this unit. +fn now <C :ClockId >() ->Self ::Repr ; +} + +impl InstantUnit for Nsec { +#[inline] +fn now <C :ClockId >() ->i64 { +C ::ktime_get () + } +} +/// A span of time. /// /// The span is stored in the unit given by the type parameter `U` (see
Signature · 1 line
2.43.0
PATCH 2/2 rust: time: add Instant in seconds
In reply to FUJITA TomonoriFrom: FUJITA Tomonori <[email protected]>
Add Sec as a time unit for Instant. Instant<C, Sec>::now() reads the clock in seconds through a new ClockId::ktime_get_seconds(), which wraps ktime_get_seconds(), ktime_get_real_seconds(), ktime_get_boottime_seconds() and ktime_get_clocktai_seconds(). as_secs() returns the value.
Instant<C, Sec> has only now() and as_secs(), and it cannot be compared. There is no Delta<Sec>. One can be added when a user needs it.
The first user is the drm/vino driver, which passes the wall clock time in seconds to the firmware of a DisplayLink dock.
--- rust/helpers/time.c | 10 ++++++ rust/kernel/time.rs | 83 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 85 insertions(+), 8 deletions(-)
rust/helpers/time.c +10 -0
@@ -19,6 +19,16 @@ __rust_helper ktime_t rust_helper_ktime_get_boottime(void)return ktime_get_boottime (); } +__rust_helper time64_t rust_helper_ktime_get_boottime_seconds(void) +{ +return ktime_get_boottime_seconds (); +} + +__rust_helper time64_t rust_helper_ktime_get_clocktai_seconds(void) +{ +return ktime_get_clocktai_seconds (); +} + __rust_helper ktime_t rust_helper_ktime_get_clocktai(void) {return ktime_get_clocktai ();
rust/kernel/time.rs +75 -8
177 lines not on this page · raw message
Signature · 1 line
2.43.0
Quoted 11 lines · FUJITA Tomonori
From: FUJITA Tomonori <[email protected]>
Mike's series added a free function
ktime_get_real_seconds()[1]. Andreas and Boqun preferred a seconds basedInstantinstead. This series does that.1/2 adds a unit type parameter to
Instant, with no functional change. 2/2 adds theSecunit andClockId::ktime_get_seconds().Instant<C, Sec>has onlynow()andas_secs(); there is noDelta<Sec>.
Quoted 8 lines
The
now()doc comment in 1/2 changes the same line as [2]. Whichever is applied first, the other conflicts on that one line only.Based on rust-next.
[1] https://lore.kernel.org/rust-for-linux/[email protected]/
[2] https://lore.kernel.org/rust-for-linux/[email protected]/
Regards,
Boqun
Quoted 13 lines
FUJITA Tomonori (2):
rust: time: make Instant generic over the time unit
rust: time: add Instant in secondsrust/helpers/time.c | 10 ++++ rust/kernel/time.rs | 123 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 115 insertions(+), 18 deletions(-)base-commit:
d89e1d186ec9271bf78e62771779e9fc03bf0fc3Signature · 1 line
2.43.0
Quoted 11 lines · FUJITA Tomonori
From: FUJITA Tomonori <[email protected]>
Mike's series added a free function
ktime_get_real_seconds()[1]. Andreas and Boqun preferred a seconds basedInstantinstead. This series does that.1/2 adds a unit type parameter to
Instant, with no functional change. 2/2 adds theSecunit andClockId::ktime_get_seconds().Instant<C, Sec>has onlynow()andas_secs(); there is noDelta<Sec>.
Quoted 5 lines
The
now()doc comment in 1/2 changes the same line as [2]. Whichever is applied first, the other conflicts on that one line only.Based on rust-next.
Orthogonal, but I dislike the shorthand Sec and Nsec. I would much prefer Seconds and NanoSeconds. Opinions?
Best regards,
Andreas Hindborg
Quoted 3 lines · Andreas Hindborg
Orthogonal, but I dislike the shorthand
SecandNsec. I would much preferSecondsandNanoSeconds. Opinions?
I guess it matches things like NSEC_PER_USEC? i.e. C.
(Personally, I don't mind longer identifiers and I tend to prefer them where reasonable, but sometimes there are other factors to take into account.)
Another point is whether we should align also our methods too (e.g. "nanos", which comes from upstream Rust), i.e. with your suggestion, we would have all NSEC, nanos and NanoSeconds.
Cheers,
Miguel
Andreas Hindborg
maintainerQuoted 7 lines · Miguel Ojeda
Quoted 3 lines · Andreas Hindborg
Orthogonal, but I dislike the shorthand
SecandNsec. I would much preferSecondsandNanoSeconds. Opinions?I guess it matches things like
NSEC_PER_USEC? i.e. C.
Quoted 7 lines
(Personally, I don't mind longer identifiers and I tend to prefer them where reasonable, but sometimes there are other factors to take into account.)
Another point is whether we should align also our methods too (e.g. "nanos", which comes from upstream Rust), i.e. with your suggestion, we would have all
NSEC,nanosandNanoSeconds.
In general I prefer that we match std on naming in general, which is also why we have as_nanos. However, chrono is using spelled out names, and I do prefer that to the short hand used in std. Worth a discussion I think.
Best regards,
Andreas Hindborg
Quoted 8 lines · On Mon, 02 Nov 2026 19:34:07 +0100 Andreas Hindborg <[email protected]>…
Quoted 3 lines
Another point is whether we should align also our methods too (e.g. "nanos", which comes from upstream Rust), i.e. with your suggestion, we would have all
NSEC,nanosandNanoSeconds.In general I prefer that we match std on naming in general, which is also why we have
as_nanos. However, chrono is using spelled out names, and I do prefer that to the short hand used in std. Worth a discussion I think.
I don't have a strong opinion, but I slightly prefer the short names. Our method names follow std Duration (from_secs(), as_nanos()), and short type names fit them better.
Either way, I would like to keep this series as is. Nsec is already in mainline, so renaming only Sec here would make the names inconsistent. If we decide to rename, I will send a separate series that renames all the unit types in the tree. It would be easier to do that after the pending Delta<Jiffy> users (condvar, workqueue and serdev) are merged.
Andreas Hindborg
maintainerQuoted 16 lines · FUJITA Tomonori
Quoted 8 lines · On Mon, 02 Nov 2026 19:34:07 +0100 Andreas Hindborg <[email protected]>…
Quoted 3 lines
Another point is whether we should align also our methods too (e.g. "nanos", which comes from upstream Rust), i.e. with your suggestion, we would have all
NSEC,nanosandNanoSeconds.In general I prefer that we match std on naming in general, which is also why we have
as_nanos. However, chrono is using spelled out names, and I do prefer that to the short hand used in std. Worth a discussion I think.I don't have a strong opinion, but I slightly prefer the short names. Our method names follow std Duration (
from_secs(),as_nanos()), and short type names fit them better.
Quoted 6 lines
Either way, I would like to keep this series as is. Nsec is already in mainline, so renaming only Sec here would make the names inconsistent. If we decide to rename, I will send a separate series that renames all the unit types in the tree. It would be easier to do that after the pending Delta<Jiffy> users (condvar, workqueue and serdev) are merged.
For sure, the naming is orthogonal and will not delay this series.
Best regards,
Andreas Hindborg
Quoted 5 lines · On Mon, 02 Nov 2026 16:07:49 +0100 Andreas Hindborg <[email protected]>…
Quoted 5 lines
From: FUJITA Tomonori <[email protected]>
Mike's series added a free function
ktime_get_real_seconds()[1]. Andreas and Boqun preferred a seconds basedInstantinstead. This series does that.
[...]
Quoted 1 line
Reviewed-by Andreas Hindborg <[email protected]>
Thanks for the review.
Mike's drm/vino series will use Instant<C, Sec>, and that series is based on drm-rust-next. There are other time patches on the list, so it might be simpler to take these two patches through rust-next or timekeeping-next, and provide a tag that drm-rust-next can also merge. Andreas, Miguel, would that work for you?
Quoted 6 lines · FUJITA Tomonori
Mike's drm/vino series will use Instant<C, Sec>, and that series is based on drm-rust-next. There are other time patches on the list, so it might be simpler to take these two patches through rust-next or timekeeping-next, and provide a tag that drm-rust-next can also merge. Andreas, Miguel, would that work for you?
Up to Andreas and what his plans are for timekeeping-next this cycle, but whatever is easiest I would say -- thanks!
Cheers,
Miguel
Andreas Hindborg
maintainerQuoted 10 lines · Miguel Ojeda
Quoted 6 lines · FUJITA Tomonori
Mike's drm/vino series will use Instant<C, Sec>, and that series is based on drm-rust-next. There are other time patches on the list, so it might be simpler to take these two patches through rust-next or timekeeping-next, and provide a tag that drm-rust-next can also merge. Andreas, Miguel, would that work for you?
Up to Andreas and what his plans are for timekeeping-next this cycle, but whatever is easiest I would say -- thanks!
Yes, these will go through the rust timekeeping tree. I plan to apply them around rc2, does that work?
Best regards,
Andreas Hindborg
Quoted 3 lines · Andreas Hindborg
Yes, these will go through the rust timekeeping tree. I plan to apply them around rc2, does that work?
For me, of course, but I guess you are asking DRM.
Thanks!
Cheers,
Miguel
Quoted 6 lines · FUJITA Tomonori
From: FUJITA Tomonori <[email protected]>
Mike's series added a free function
ktime_get_real_seconds()[1]. Andreas and Boqun preferred a seconds basedInstantinstead. This series does that.
Quoted 6 lines
1/2 adds a unit type parameter to
Instant, with no functional change. 2/2 adds theSecunit andClockId::ktime_get_seconds().Instant<C, Sec>has onlynow()andas_secs(); there is noDelta<Sec>.[...]
Applied, thanks!
[1/2] rust: time: make Instant generic over the time unit
commit: 52e953331adb68ea0f8173eacd421f8f88843c36
[2/2] rust: time: add Instant in seconds
commit: 093d14cf49d4f33466ec41f6e7b28f365c75624aBest regards,
Signature · 1 line
Andreas Hindborg <[email protected]>