diff mbox series

[net-next,v2,3/6] rust: time: Implement addition of Ktime and Delta

Message ID 20241005122531.20298-4-fujita.tomonori@gmail.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series rust: Add IO polling | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 5 this patch: 5
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers warning 1 maintainers not CCed: boqun.feng@gmail.com
netdev/build_clang success Errors and warnings before: 5 this patch: 5
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn fail Errors and warnings before: 36 this patch: 37
netdev/checkpatch warning WARNING: line length of 87 exceeds 80 columns
netdev/build_clang_rust success Errors and warnings before: 4 this patch: 4
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

FUJITA Tomonori Oct. 5, 2024, 12:25 p.m. UTC
Implement Add<Delta> for Ktime to support the operation:

Ktime = Ktime + Delta

This is used to calculate the future time when the timeout will occur.

Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
 rust/helpers/time.c |  5 +++++
 rust/kernel/time.rs | 11 +++++++++++
 2 files changed, 16 insertions(+)

Comments

Andrew Lunn Oct. 5, 2024, 6:07 p.m. UTC | #1
On Sat, Oct 05, 2024 at 09:25:28PM +0900, FUJITA Tomonori wrote:
> Implement Add<Delta> for Ktime to support the operation:
> 
> Ktime = Ktime + Delta
> 
> This is used to calculate the future time when the timeout will occur.

Since Delta can be negative, it could also be a passed time. For a
timeout, that does not make much sense.

> +impl core::ops::Add<Delta> for Ktime {
> +    type Output = Ktime;
> +
> +    #[inline]
> +    fn add(self, delta: Delta) -> Ktime {
> +        // SAFETY: FFI call.
> +        let t = unsafe { bindings::ktime_add_ns(self.inner, delta.as_nanos() as u64) };

So you are throwing away the sign bit. What does Rust in the kernel do
if it was a negative delta?

I think the types being used here need more consideration.

	Andrew
Miguel Ojeda Oct. 5, 2024, 6:36 p.m. UTC | #2
On Sat, Oct 5, 2024 at 2:26 PM FUJITA Tomonori
<fujita.tomonori@gmail.com> wrote:
>
> +    fn add(self, delta: Delta) -> Ktime {
> +        // SAFETY: FFI call.
> +        let t = unsafe { bindings::ktime_add_ns(self.inner, delta.as_nanos() as u64) };
> +        Ktime::from_raw(t)
> +    }

I wonder if we want to use the `ktime` macros/operations for this type
or not (even if we still promise it is the same type underneath). It
means having to define helpers, adding `unsafe` code and `SAFETY`
comments, a call penalty in non-LTO, losing overflow checking (if we
want it for these types), and so on.

(And at least C is `-fno-strict-overflow`, otherwise it would be even subtler).

Cheers,
Miguel
Fiona Behrens Oct. 6, 2024, 10:45 a.m. UTC | #3
On 5 Oct 2024, at 20:07, Andrew Lunn wrote:

> On Sat, Oct 05, 2024 at 09:25:28PM +0900, FUJITA Tomonori wrote:
>> Implement Add<Delta> for Ktime to support the operation:
>>
>> Ktime = Ktime + Delta
>>
>> This is used to calculate the future time when the timeout will occur.
>
> Since Delta can be negative, it could also be a passed time. For a
> timeout, that does not make much sense.
>

Are there more usecases than Delta? Would it make sense in that case to also implement Sub as well?

Thanks,
Fiona

>> +impl core::ops::Add<Delta> for Ktime {
>> +    type Output = Ktime;
>> +
>> +    #[inline]
>> +    fn add(self, delta: Delta) -> Ktime {
>> +        // SAFETY: FFI call.
>> +        let t = unsafe { bindings::ktime_add_ns(self.inner, delta.as_nanos() as u64) };
>
> So you are throwing away the sign bit. What does Rust in the kernel do
> if it was a negative delta?
>
> I think the types being used here need more consideration.
>
>     Andrew
FUJITA Tomonori Oct. 7, 2024, 6:06 a.m. UTC | #4
On Sun, 06 Oct 2024 12:45:06 +0200
Fiona Behrens <me@kloenk.dev> wrote:

>> On Sat, Oct 05, 2024 at 09:25:28PM +0900, FUJITA Tomonori wrote:
>>> Implement Add<Delta> for Ktime to support the operation:
>>>
>>> Ktime = Ktime + Delta
>>>
>>> This is used to calculate the future time when the timeout will occur.
>>
>> Since Delta can be negative, it could also be a passed time. For a
>> timeout, that does not make much sense.
>>
> 
> Are there more usecases than Delta? Would it make sense in that case to also implement Sub as well?

We might add the api to calculate the elapsed time when it becomes
necessary:

Delta = Ktime - Ktime
FUJITA Tomonori Oct. 7, 2024, 6:17 a.m. UTC | #5
On Sat, 5 Oct 2024 20:36:44 +0200
Miguel Ojeda <miguel.ojeda.sandonis@gmail.com> wrote:

> On Sat, Oct 5, 2024 at 2:26 PM FUJITA Tomonori
> <fujita.tomonori@gmail.com> wrote:
>>
>> +    fn add(self, delta: Delta) -> Ktime {
>> +        // SAFETY: FFI call.
>> +        let t = unsafe { bindings::ktime_add_ns(self.inner, delta.as_nanos() as u64) };
>> +        Ktime::from_raw(t)
>> +    }
> 
> I wonder if we want to use the `ktime` macros/operations for this type
> or not (even if we still promise it is the same type underneath). It
> means having to define helpers, adding `unsafe` code and `SAFETY`
> comments, a call penalty in non-LTO, losing overflow checking (if we
> want it for these types), and so on.

Yeah, if we are allowed to touch ktime_t directly instead of using the
accessors, it's great for the rust side.

The timers maintainers, what do you think?
Alice Ryhl Oct. 7, 2024, 2:24 p.m. UTC | #6
On Mon, Oct 7, 2024 at 8:17 AM FUJITA Tomonori
<fujita.tomonori@gmail.com> wrote:
>
> On Sat, 5 Oct 2024 20:36:44 +0200
> Miguel Ojeda <miguel.ojeda.sandonis@gmail.com> wrote:
>
> > On Sat, Oct 5, 2024 at 2:26 PM FUJITA Tomonori
> > <fujita.tomonori@gmail.com> wrote:
> >>
> >> +    fn add(self, delta: Delta) -> Ktime {
> >> +        // SAFETY: FFI call.
> >> +        let t = unsafe { bindings::ktime_add_ns(self.inner, delta.as_nanos() as u64) };
> >> +        Ktime::from_raw(t)
> >> +    }
> >
> > I wonder if we want to use the `ktime` macros/operations for this type
> > or not (even if we still promise it is the same type underneath). It
> > means having to define helpers, adding `unsafe` code and `SAFETY`
> > comments, a call penalty in non-LTO, losing overflow checking (if we
> > want it for these types), and so on.
>
> Yeah, if we are allowed to touch ktime_t directly instead of using the
> accessors, it's great for the rust side.
>
> The timers maintainers, what do you think?

We already do that in the existing code. The Ktime::sub method touches
the ktime_t directly and performs a subtraction using the - operator
rather than call a ktime_ method for it.

Alice
FUJITA Tomonori Oct. 9, 2024, 12:50 p.m. UTC | #7
On Mon, 7 Oct 2024 16:24:28 +0200
Alice Ryhl <aliceryhl@google.com> wrote:

>> > or not (even if we still promise it is the same type underneath). It
>> > means having to define helpers, adding `unsafe` code and `SAFETY`
>> > comments, a call penalty in non-LTO, losing overflow checking (if we
>> > want it for these types), and so on.
>>
>> Yeah, if we are allowed to touch ktime_t directly instead of using the
>> accessors, it's great for the rust side.
>>
>> The timers maintainers, what do you think?
> 
> We already do that in the existing code. The Ktime::sub method touches
> the ktime_t directly and performs a subtraction using the - operator
> rather than call a ktime_ method for it.

I'll touch ktime_t directly in the next version.
diff mbox series

Patch

diff --git a/rust/helpers/time.c b/rust/helpers/time.c
index d6f61affb2c3..60dee69f4efc 100644
--- a/rust/helpers/time.c
+++ b/rust/helpers/time.c
@@ -2,6 +2,11 @@ 
 
 #include <linux/ktime.h>
 
+ktime_t rust_helper_ktime_add_ns(const ktime_t kt, const u64 nsec)
+{
+	return ktime_add_ns(kt, nsec);
+}
+
 int rust_helper_ktime_compare(const ktime_t cmp1, const ktime_t cmp2)
 {
 	return ktime_compare(cmp1, cmp2);
diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index 6c5a1c50c5f1..3e00ad22ed89 100644
--- a/rust/kernel/time.rs
+++ b/rust/kernel/time.rs
@@ -167,3 +167,14 @@  pub fn as_micros(self) -> i64 {
         self.nanos / NSEC_PER_USEC
     }
 }
+
+impl core::ops::Add<Delta> for Ktime {
+    type Output = Ktime;
+
+    #[inline]
+    fn add(self, delta: Delta) -> Ktime {
+        // SAFETY: FFI call.
+        let t = unsafe { bindings::ktime_add_ns(self.inner, delta.as_nanos() as u64) };
+        Ktime::from_raw(t)
+    }
+}