diff mbox series

[v2,5/8] rust: time: Add Instant::from_nanos()

Message ID 20250415195020.413478-6-lyude@redhat.com (mailing list archive)
State New
Headers show
Series rust/hrtimer: Various hrtimer + time additions | expand

Commit Message

Lyude Paul April 15, 2025, 7:48 p.m. UTC
For implementing Rust bindings which can return a point in time.

Signed-off-by: Lyude Paul <lyude@redhat.com>
---
 rust/kernel/time.rs | 8 ++++++++
 1 file changed, 8 insertions(+)

Comments

FUJITA Tomonori April 16, 2025, 9:10 a.m. UTC | #1
On Tue, 15 Apr 2025 15:48:26 -0400
Lyude Paul <lyude@redhat.com> wrote:

> For implementing Rust bindings which can return a point in time.
> 
> Signed-off-by: Lyude Paul <lyude@redhat.com>
> ---
>  rust/kernel/time.rs | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
> index 8d6aa88724ad8..545963140f180 100644
> --- a/rust/kernel/time.rs
> +++ b/rust/kernel/time.rs
> @@ -83,6 +83,14 @@ pub fn elapsed(&self) -> Delta {
>      pub(crate) fn as_nanos(self) -> i64 {
>          self.inner
>      }
> +
> +    #[expect(unused)]
> +    #[inline]
> +    pub(crate) fn from_nanos(nanos: i64) -> Self {
> +        Self {
> +            inner: nanos as bindings::ktime_t,
> +        }
> +    }
>  }

We need to guarantee the following Invariants.

/// A specific point in time.
///
/// # Invariants
///
/// The `inner` value is in the range from 0 to `KTIME_MAX`.
#[repr(transparent)]
#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord)]
pub struct Instant {
    inner: bindings::ktime_t,
}

Otherwise, The method of the Sub trait may cause an overflow

By the way, what are some use cases for creating an Instant from
driver's input?
Lyude Paul April 16, 2025, 6:41 p.m. UTC | #2
On Wed, 2025-04-16 at 18:10 +0900, FUJITA Tomonori wrote:
> On Tue, 15 Apr 2025 15:48:26 -0400
> Lyude Paul <lyude@redhat.com> wrote:
> 
> > For implementing Rust bindings which can return a point in time.
> > 
> > Signed-off-by: Lyude Paul <lyude@redhat.com>
> > ---
> >   rust/kernel/time.rs | 8 ++++++++
> >   1 file changed, 8 insertions(+)
> > 
> > diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
> > index 8d6aa88724ad8..545963140f180 100644
> > --- a/rust/kernel/time.rs
> > +++ b/rust/kernel/time.rs
> > @@ -83,6 +83,14 @@ pub fn elapsed(&self) -> Delta {
> >       pub(crate) fn as_nanos(self) -> i64 {
> >           self.inner
> >       }
> > +
> > +    #[expect(unused)]
> > +    #[inline]
> > +    pub(crate) fn from_nanos(nanos: i64) -> Self {
> > +        Self {
> > +            inner: nanos as bindings::ktime_t,
> > +        }
> > +    }
> >   }
> 
> We need to guarantee the following Invariants.
> 
> /// A specific point in time.
> ///
> /// # Invariants
> ///
> /// The `inner` value is in the range from 0 to `KTIME_MAX`.
> #[repr(transparent)]
> #[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord)]
> pub struct Instant {
>     inner: bindings::ktime_t,
> }
> 
> Otherwise, The method of the Sub trait may cause an overflow

Gotcha, I'll turn it into u64 then.

> 
> By the way, what are some use cases for creating an Instant from
> driver's input?

For rvkms, the main usecase is that we emulate vblank events from the driver
using an hrtimer, and the hrtimer's expiry value is what we use for generating
the timestamp included in vblank events. Real hardware would likely be doing
this as well but from the actual hardware rather than a hrtimer, since quite a
number of display drivers read a literal monotonic vblank timestamp from the
hardware.

>
diff mbox series

Patch

diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index 8d6aa88724ad8..545963140f180 100644
--- a/rust/kernel/time.rs
+++ b/rust/kernel/time.rs
@@ -83,6 +83,14 @@  pub fn elapsed(&self) -> Delta {
     pub(crate) fn as_nanos(self) -> i64 {
         self.inner
     }
+
+    #[expect(unused)]
+    #[inline]
+    pub(crate) fn from_nanos(nanos: i64) -> Self {
+        Self {
+            inner: nanos as bindings::ktime_t,
+        }
+    }
 }
 
 impl core::ops::Sub for Instant {