* [PATCH v1 1/2] rust: time: make Instant generic over the time unit
2026-10-31 0:33 [PATCH v1 0/2] rust: time: seconds based Instant FUJITA Tomonori
@ 2026-10-31 0:33 ` FUJITA Tomonori
2026-10-31 0:33 ` [PATCH v1 2/2] rust: time: add Instant in seconds FUJITA Tomonori
2026-10-31 3:56 ` [PATCH v1 0/2] rust: time: seconds based Instant Boqun Feng
2 siblings, 0 replies; 4+ messages in thread
From: FUJITA Tomonori @ 2026-10-31 0:33 UTC (permalink / raw)
To: a.hindborg, boqun, ojeda
Cc: acourbot, aliceryhl, anna-maria, bjorn3_gh, dakr, daniel.almeida,
frederic, gary, jstultz, lossin, lyude, mike, sboyd, tamird, tglx,
tmgross, work, rust-for-linux, FUJITA Tomonori
From: FUJITA Tomonori <fujita.tomonori@gmail.com>
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.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
rust/kernel/time.rs | 44 ++++++++++++++++++++++++++++++++------------
1 file changed, 32 insertions(+), 12 deletions(-)
diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index b6a2aab12a50..8232880bffe7 100644
--- a/rust/kernel/time.rs
+++ b/rust/kernel/time.rs
@@ -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
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH v1 2/2] rust: time: add Instant in seconds
2026-10-31 0:33 [PATCH v1 0/2] rust: time: seconds based Instant FUJITA Tomonori
2026-10-31 0:33 ` [PATCH v1 1/2] rust: time: make Instant generic over the time unit FUJITA Tomonori
@ 2026-10-31 0:33 ` FUJITA Tomonori
2026-10-31 3:56 ` [PATCH v1 0/2] rust: time: seconds based Instant Boqun Feng
2 siblings, 0 replies; 4+ messages in thread
From: FUJITA Tomonori @ 2026-10-31 0:33 UTC (permalink / raw)
To: a.hindborg, boqun, ojeda
Cc: acourbot, aliceryhl, anna-maria, bjorn3_gh, dakr, daniel.almeida,
frederic, gary, jstultz, lossin, lyude, mike, sboyd, tamird, tglx,
tmgross, work, rust-for-linux, FUJITA Tomonori
From: FUJITA Tomonori <fujita.tomonori@gmail.com>
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.
Link: https://lore.kernel.org/rust-for-linux/20260826162851.2497-10-mike@fireburn.co.uk/
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
rust/helpers/time.c | 10 ++++++
rust/kernel/time.rs | 83 ++++++++++++++++++++++++++++++++++++++++-----
2 files changed, 85 insertions(+), 8 deletions(-)
diff --git a/rust/helpers/time.c b/rust/helpers/time.c
index 32f495970493..47b7b2e111b9 100644
--- a/rust/helpers/time.c
+++ b/rust/helpers/time.c
@@ -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();
diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index 8232880bffe7..f7f6a674fec6 100644
--- a/rust/kernel/time.rs
+++ b/rust/kernel/time.rs
@@ -69,7 +69,9 @@ pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies {
///
/// Implementers must ensure that `ktime_get()` returns a value in the inclusive range
/// `0..=KTIME_MAX` (i.e., greater than or equal to 0 and less than or equal to
-/// `KTIME_MAX`, where `KTIME_MAX` equals `i64::MAX`).
+/// `KTIME_MAX`, where `KTIME_MAX` equals `i64::MAX`), and that `ktime_get_seconds()`
+/// returns a value in the inclusive range `0..=KTIME_SEC_MAX` (where `KTIME_SEC_MAX`
+/// equals `KTIME_MAX / NSEC_PER_SEC`).
pub unsafe trait ClockId {
/// The kernel clock ID associated with this clock.
///
@@ -80,6 +82,11 @@ pub unsafe trait ClockId {
///
/// The function must return a value in the range `0..=KTIME_MAX`.
fn ktime_get() -> bindings::ktime_t;
+
+ /// Get the current time from the clock in seconds.
+ ///
+ /// The function must return a value in the range `0..=KTIME_SEC_MAX`.
+ fn ktime_get_seconds() -> bindings::time64_t;
}
/// A monotonically increasing clock.
@@ -96,7 +103,7 @@ pub unsafe trait ClockId {
pub struct Monotonic;
// SAFETY: The kernel's `ktime_get()` is guaranteed to return a value
-// in `0..=KTIME_MAX`.
+// in `0..=KTIME_MAX`, and `ktime_get_seconds()` a value in `0..=KTIME_SEC_MAX`.
unsafe impl ClockId for Monotonic {
const ID: bindings::clockid_t = bindings::CLOCK_MONOTONIC as bindings::clockid_t;
@@ -104,6 +111,11 @@ fn ktime_get() -> bindings::ktime_t {
// SAFETY: It is always safe to call `ktime_get()` outside of NMI context.
unsafe { bindings::ktime_get() }
}
+
+ fn ktime_get_seconds() -> bindings::time64_t {
+ // SAFETY: It is always safe to call `ktime_get_seconds()` outside of NMI context.
+ unsafe { bindings::ktime_get_seconds() }
+ }
}
/// A settable system-wide clock that measures real (i.e., wall-clock) time.
@@ -123,7 +135,7 @@ fn ktime_get() -> bindings::ktime_t {
pub struct RealTime;
// SAFETY: The kernel's `ktime_get_real()` is guaranteed to return a value
-// in `0..=KTIME_MAX`.
+// in `0..=KTIME_MAX`, and `ktime_get_real_seconds()` a value in `0..=KTIME_SEC_MAX`.
unsafe impl ClockId for RealTime {
const ID: bindings::clockid_t = bindings::CLOCK_REALTIME as bindings::clockid_t;
@@ -131,6 +143,11 @@ fn ktime_get() -> bindings::ktime_t {
// SAFETY: It is always safe to call `ktime_get_real()` outside of NMI context.
unsafe { bindings::ktime_get_real() }
}
+
+ fn ktime_get_seconds() -> bindings::time64_t {
+ // SAFETY: It is always safe to call `ktime_get_real_seconds()` outside of NMI context.
+ unsafe { bindings::ktime_get_real_seconds() }
+ }
}
/// A monotonic that ticks while system is suspended.
@@ -143,7 +160,7 @@ fn ktime_get() -> bindings::ktime_t {
pub struct BootTime;
// SAFETY: The kernel's `ktime_get_boottime()` is guaranteed to return a value
-// in `0..=KTIME_MAX`.
+// in `0..=KTIME_MAX`, and `ktime_get_boottime_seconds()` a value in `0..=KTIME_SEC_MAX`.
unsafe impl ClockId for BootTime {
const ID: bindings::clockid_t = bindings::CLOCK_BOOTTIME as bindings::clockid_t;
@@ -151,6 +168,11 @@ fn ktime_get() -> bindings::ktime_t {
// SAFETY: It is always safe to call `ktime_get_boottime()` outside of NMI context.
unsafe { bindings::ktime_get_boottime() }
}
+
+ fn ktime_get_seconds() -> bindings::time64_t {
+ // SAFETY: It is always safe to call `ktime_get_boottime_seconds()` outside of NMI context.
+ unsafe { bindings::ktime_get_boottime_seconds() }
+ }
}
/// International Atomic Time.
@@ -167,7 +189,7 @@ fn ktime_get() -> bindings::ktime_t {
pub struct Tai;
// SAFETY: The kernel's `ktime_get_clocktai()` is guaranteed to return a value
-// in `0..=KTIME_MAX`.
+// in `0..=KTIME_MAX`, and `ktime_get_clocktai_seconds()` a value in `0..=KTIME_SEC_MAX`.
unsafe impl ClockId for Tai {
const ID: bindings::clockid_t = bindings::CLOCK_TAI as bindings::clockid_t;
@@ -175,6 +197,11 @@ fn ktime_get() -> bindings::ktime_t {
// SAFETY: It is always safe to call `ktime_get_tai()` outside of NMI context.
unsafe { bindings::ktime_get_clocktai() }
}
+
+ fn ktime_get_seconds() -> bindings::time64_t {
+ // SAFETY: It is always safe to call `ktime_get_clocktai_seconds()` outside of NMI context.
+ unsafe { bindings::ktime_get_clocktai_seconds() }
+ }
}
/// A specific point in time.
@@ -186,7 +213,8 @@ fn ktime_get() -> bindings::ktime_t {
///
/// # Invariants
///
-/// The `inner` value is in the range from 0 to `KTIME_MAX`.
+/// * For `U = Nsec`, the `inner` value is in the range from 0 to `KTIME_MAX`.
+/// * For `U = Sec`, the `inner` value is in the range from 0 to `KTIME_SEC_MAX`.
#[repr(transparent)]
#[derive(PartialEq, PartialOrd, Eq, Ord)]
pub struct Instant<C: ClockId, U: InstantUnit = Nsec> {
@@ -206,8 +234,10 @@ impl<C: ClockId, U: InstantUnit> Instant<C, U> {
/// Get the current time from the clock `C`.
#[inline]
pub fn now() -> Self {
- // 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`.
+ // INVARIANT: `InstantUnit` is sealed. `Nsec` calls `ClockId::ktime_get()`, which
+ // returns a value in the range from 0 to `KTIME_MAX`, and `Sec` calls
+ // `ClockId::ktime_get_seconds()`, which returns a value in the range from 0 to
+ // `KTIME_SEC_MAX`.
Self {
inner: U::now::<C>(),
_c: PhantomData,
@@ -302,10 +332,28 @@ fn sub(self, rhs: Delta) -> Self::Output {
}
}
+impl<C: ClockId> Instant<C, Sec> {
+ /// Return the number of whole seconds in the [`Instant`].
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use kernel::time::{Instant, RealTime, Sec};
+ ///
+ /// let secs: i64 = Instant::<RealTime, Sec>::now().as_secs();
+ /// assert!(secs >= 0);
+ /// ```
+ #[inline]
+ pub fn as_secs(&self) -> i64 {
+ self.inner
+ }
+}
+
mod private {
pub trait Sealed {}
impl Sealed for super::Nsec {}
+ impl Sealed for super::Sec {}
impl Sealed for super::Jiffy {}
}
@@ -330,6 +378,18 @@ impl TimeUnit for Nsec {
const ZERO: Self::Repr = 0;
}
+/// A time unit of seconds.
+///
+/// An [`Instant<C, Sec>`] stores its value as [`i64`] seconds. [`Sec`] does not
+/// implement the comparison traits, so two such instants cannot be compared.
+#[derive(Copy, Clone, Debug)]
+pub enum Sec {}
+
+impl TimeUnit for Sec {
+ type Repr = i64;
+ const ZERO: Self::Repr = 0;
+}
+
/// A time unit of jiffies.
///
/// A [`Delta<Jiffy>`] stores its value as [`isize`] jiffies and can represent
@@ -355,6 +415,13 @@ fn now<C: ClockId>() -> i64 {
}
}
+impl InstantUnit for Sec {
+ #[inline]
+ fn now<C: ClockId>() -> i64 {
+ C::ktime_get_seconds()
+ }
+}
+
/// A span of time.
///
/// The span is stored in the unit given by the type parameter `U` (see
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH v1 0/2] rust: time: seconds based Instant
2026-10-31 0:33 [PATCH v1 0/2] rust: time: seconds based Instant FUJITA Tomonori
2026-10-31 0:33 ` [PATCH v1 1/2] rust: time: make Instant generic over the time unit FUJITA Tomonori
2026-10-31 0:33 ` [PATCH v1 2/2] rust: time: add Instant in seconds FUJITA Tomonori
@ 2026-10-31 3:56 ` Boqun Feng
2 siblings, 0 replies; 4+ messages in thread
From: Boqun Feng @ 2026-10-31 3:56 UTC (permalink / raw)
To: FUJITA Tomonori
Cc: a.hindborg, ojeda, acourbot, aliceryhl, anna-maria, bjorn3_gh,
dakr, daniel.almeida, frederic, gary, jstultz, lossin, lyude,
mike, sboyd, tamird, tglx, tmgross, work, rust-for-linux,
FUJITA Tomonori
On Sat, Oct 31, 2026 at 09:33:32AM +0900, FUJITA Tomonori wrote:
> From: FUJITA Tomonori <fujita.tomonori@gmail.com>
>
> 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/20260826162851.2497-10-mike@fireburn.co.uk/
> [2] https://lore.kernel.org/rust-for-linux/20261030044803.3863267-1-tomo@flapping.org/
>
Acked-by: Boqun Feng <boqun@kernel.org>
Regards,
Boqun
> 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
> --
> 2.43.0
>
^ permalink raw reply [flat|nested] 4+ messages in thread