Re: [PATCH net-next v3 4/8] rust: time: Implement addition of Ktime and Delta
From: Boqun Feng
Date: Wed Oct 16 2024 - 15:54:24 EST
On Wed, Oct 16, 2024 at 12:52:09PM +0900, FUJITA Tomonori wrote:
> Implement Add<Delta> for Ktime to support the operation:
>
> Ktime = Ktime + Delta
>
> This is typically used to calculate the future time when the timeout
> will occur.
>
> timeout Ktime = current Ktime (via ktime_get()) + Delta;
> // do something
> if (ktime_get() > timeout Ktime) {
> // timed out
> }
>
> Signed-off-by: FUJITA Tomonori <fujita.tomonori@xxxxxxxxx>
> ---
> rust/kernel/time.rs | 11 +++++++++++
> 1 file changed, 11 insertions(+)
>
> diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
> index 8c00854db58c..9b0537b63cf7 100644
> --- a/rust/kernel/time.rs
> +++ b/rust/kernel/time.rs
> @@ -155,3 +155,14 @@ pub fn as_secs(self) -> i64 {
> self.nanos / NSEC_PER_SEC
> }
> }
> +
> +impl core::ops::Add<Delta> for Ktime {
> + type Output = Ktime;
> +
> + #[inline]
> + fn add(self, delta: Delta) -> Ktime {
> + Ktime {
> + inner: self.inner + delta.as_nanos(),
What if overflow happens in this addition? Is the expectation that user
should avoid overflows? I asked because we have ktime_add_safe() which
saturate at KTIME_SEC_MAX.
Regards,
Boqun
> + }
> + }
> +}
> --
> 2.43.0
>
>