lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Tue, 26 Mar 2024 17:13:38 +0000
From: Benno Lossin <benno.lossin@...ton.me>
To: Boqun Feng <boqun.feng@...il.com>
Cc: rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org, Thomas Gleixner <tglx@...utronix.de>, Alice Ryhl <aliceryhl@...gle.com>, Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>, Wedson Almeida Filho <wedsonaf@...il.com>, Gary Guo <gary@...yguo.net>, Björn Roy Baron <bjorn3_gh@...tonmail.com>, Andreas Hindborg <a.hindborg@...sung.com>, John Stultz <jstultz@...gle.com>, Stephen Boyd <sboyd@...nel.org>, Valentin Obst <kernel@...entinobst.de>, Heghedus Razvan <heghedus.razvan@...tonmail.com>, Asahi Lina <lina@...hilina.net>
Subject: Re: [PATCH 5/5] rust: time: Add Instant::elapsed() for monotonic clocks

On 24.03.24 23:33, Boqun Feng wrote:
> This is a convenient way to do:
> 
> 	t1 = Clock::now();
> 	...
> 	delta =  Clock::now() - t1;
> 
> Hence add it.
> 
> Co-developed-by: Heghedus Razvan <heghedus.razvan@...tonmail.com>
> Signed-off-by: Heghedus Razvan <heghedus.razvan@...tonmail.com>
> Co-developed-by: Asahi Lina <lina@...hilina.net>
> Signed-off-by: Asahi Lina <lina@...hilina.net>
> Signed-off-by: Boqun Feng <boqun.feng@...il.com>
> ---
>  rust/kernel/time.rs | 25 +++++++++++++++++++++++++
>  1 file changed, 25 insertions(+)
> 
> diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
> index 5cd669cbea01..cd1e45169517 100644
> --- a/rust/kernel/time.rs
> +++ b/rust/kernel/time.rs
> @@ -114,6 +114,31 @@ fn sub(self, other: Self) -> Self::Output {
>      }
>  }
> 
> +impl<T: Clock + Monotonic> Instant<T> {
> +    /// Returns the time elapsed since this [`Instant`].
> +    ///
> +    /// This provides a convenient way to calculate time elapsed since a previous [`Clock::now`].
> +    /// Note even though the function only exists for monotonic clocks, it could still return
> +    /// negative [`Duration`] if the current time is earlier than the time of `&self`, and this
> +    /// could happen if `&self` is a timestamp generated by a [`Instant`] + [`Duration`].

But there currently is no way to add an `Instant<T>` to a `Duration`.

> +    ///
> +    /// But for typical usages, it should always return non-negative [`Duration`]:
> +    ///
> +    /// # Examples
> +    ///
> +    /// ```
> +    /// use kernel::time::{Clock, clock::KernelTime};
> +    ///
> +    /// let ts = KernelTime::now();
> +    ///
> +    /// // `KernelTime` is monotonic.
> +    /// assert!(ts.elapsed().to_ns() >= 0);

Now that I thought a bit more about the design, I think allowing
negative durations is a bad idea.
Do you disagree?

If there is a case where you have a non-monotonic clock, or you are not
sure if two timestamps are in the correct relation, we could have a
function that returns a `Option<Duration>` or `Result<Duration>`.

-- 
Cheers,
Benno

> +    /// ```
> +    pub fn elapsed(&self) -> Duration {
> +        T::now() - *self
> +    }
> +}
> +
>  /// Contains the various clock source types available to the kernel.
>  pub mod clock {
>      use super::*;
> --
> 2.44.0
> 

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ