Re: [PATCH net-next v3 4/8] rust: time: Implement addition of Ktime and Delta

From: Alice Ryhl
Date: Thu Oct 17 2024 - 05:33:37 EST


On Thu, Oct 17, 2024 at 11:31 AM FUJITA Tomonori
<fujita.tomonori@xxxxxxxxx> wrote:
>
> On Wed, 16 Oct 2024 12:54:07 -0700
> Boqun Feng <boqun.feng@xxxxxxxxx> wrote:
>
> >> 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?
>
> Yes, I'll add a comment.
>
> > I asked because we have ktime_add_safe() which saturate at
> > KTIME_SEC_MAX.
>
> We could add the Rust version of add_safe method. But looks like
> ktime_add_safe() is used by only some core systems so we don't need to
> add it now?

I think it makes sense to follow the standard Rust addition
conventions here. Rust normally treats + as addition that BUGs on
overflow (with the appropriate configs set), and then there's a
saturating_add function for when you want it to saturate.

Alice