Rust for Linux List
 help / color / mirror / Atom feed
From: FUJITA Tomonori <tomo@flapping.org>
To: a.hindborg@kernel.org, boqun@kernel.org, ojeda@kernel.org
Cc: acourbot@nvidia.com, aliceryhl@google.com,
	anna-maria@linutronix.de, bjorn3_gh@protonmail.com,
	dakr@kernel.org, daniel.almeida@collabora.com,
	frederic@kernel.org, gary@garyguo.net, jstultz@google.com,
	lossin@kernel.org, lyude@redhat.com, mike@fireburn.co.uk,
	sboyd@kernel.org, tamird@kernel.org, tglx@kernel.org,
	tmgross@umich.edu, work@onurozkan.dev,
	rust-for-linux@vger.kernel.org,
	FUJITA Tomonori <fujita.tomonori@gmail.com>
Subject: [PATCH v1 2/2] rust: time: add Instant in seconds
Date: Sat, 31 Oct 2026 09:33:34 +0900	[thread overview]
Message-ID: <20261031003334.4161188-3-tomo@flapping.org> (raw)
In-Reply-To: <20261031003334.4161188-1-tomo@flapping.org>

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


  parent reply	other threads:[~2026-10-31  0:34 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2026-10-31  3:56 ` [PATCH v1 0/2] rust: time: seconds based Instant Boqun Feng

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261031003334.4161188-3-tomo@flapping.org \
    --to=tomo@flapping.org \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=anna-maria@linutronix.de \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=frederic@kernel.org \
    --cc=fujita.tomonori@gmail.com \
    --cc=gary@garyguo.net \
    --cc=jstultz@google.com \
    --cc=lossin@kernel.org \
    --cc=lyude@redhat.com \
    --cc=mike@fireburn.co.uk \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=sboyd@kernel.org \
    --cc=tamird@kernel.org \
    --cc=tglx@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=work@onurozkan.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox