This patch allows timer handlers to report that they want a timer to be
restarted after the timer handler has finished executing.
Also update the `hrtimer` documentation to showcase the new feature.
Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
rust/kernel/time/hrtimer.rs | 37 ++++++++++++++++++++++++++++++++++++-
rust/kernel/time/hrtimer/arc.rs | 4 +---
2 files changed, 37 insertions(+), 4 deletions(-)
diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs
index d0842c7c4c6ddffeef9a79cbf9727819060e4333..50e8c23578b5cf7196893ac88d9547fc027f1f04 100644
--- a/rust/kernel/time/hrtimer.rs
+++ b/rust/kernel/time/hrtimer.rs
@@ -167,7 +167,7 @@ pub trait HrTimerCallback {
type CallbackTargetParameter<'a>;
/// Called by the timer logic when the timer fires.
- fn run(this: Self::CallbackTargetParameter<'_>)
+ fn run(this: Self::CallbackTargetParameter<'_>) -> HrTimerRestart
where
Self: Sized;
}
@@ -262,6 +262,41 @@ unsafe fn start(self_ptr: *const Self, expires: Ktime) {
}
}
+/// Restart policy for timers.
+pub enum HrTimerRestart {
+ /// Timer should not be restarted.
+ NoRestart,
+ /// Timer should be restarted.
+ Restart,
+}
+
+impl From<u32> for HrTimerRestart {
+ fn from(value: u32) -> Self {
+ match value {
+ 0 => Self::NoRestart,
+ _ => Self::Restart,
+ }
+ }
+}
+
+impl From<i32> for HrTimerRestart {
+ fn from(value: i32) -> Self {
+ match value {
+ 0 => Self::NoRestart,
+ _ => Self::Restart,
+ }
+ }
+}
+
+impl From<HrTimerRestart> for bindings::hrtimer_restart {
+ fn from(value: HrTimerRestart) -> Self {
+ match value {
+ HrTimerRestart::NoRestart => bindings::hrtimer_restart_HRTIMER_NORESTART,
+ HrTimerRestart::Restart => bindings::hrtimer_restart_HRTIMER_RESTART,
+ }
+ }
+}
+
/// Use to implement the [`HasHrTimer<T>`] trait.
///
/// See [`module`] documentation for an example.
diff --git a/rust/kernel/time/hrtimer/arc.rs b/rust/kernel/time/hrtimer/arc.rs
index 4c1812f190bc7a9e0b2fcd5ea2e320f45ba584bc..cee143b04aa21fa6c60c3363cd95f7a67360cbf8 100644
--- a/rust/kernel/time/hrtimer/arc.rs
+++ b/rust/kernel/time/hrtimer/arc.rs
@@ -82,8 +82,6 @@ impl<U> RawHrTimerCallback for Arc<U>
// timer. This `U` is contained in an `Arc`.
let receiver = unsafe { ArcBorrow::from_raw(data_ptr) };
- U::run(receiver);
-
- bindings::hrtimer_restart_HRTIMER_NORESTART
+ U::run(receiver).into()
}
}
--
2.47.0
On Fri, Jan 10, 2025 at 3:17 PM Andreas Hindborg <a.hindborg@kernel.org> wrote:
>
> This patch allows timer handlers to report that they want a timer to be
> restarted after the timer handler has finished executing.
>
> Also update the `hrtimer` documentation to showcase the new feature.
>
> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
> ---
> rust/kernel/time/hrtimer.rs | 37 ++++++++++++++++++++++++++++++++++++-
> rust/kernel/time/hrtimer/arc.rs | 4 +---
> 2 files changed, 37 insertions(+), 4 deletions(-)
>
> diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs
> index d0842c7c4c6ddffeef9a79cbf9727819060e4333..50e8c23578b5cf7196893ac88d9547fc027f1f04 100644
> --- a/rust/kernel/time/hrtimer.rs
> +++ b/rust/kernel/time/hrtimer.rs
> @@ -167,7 +167,7 @@ pub trait HrTimerCallback {
> type CallbackTargetParameter<'a>;
>
> /// Called by the timer logic when the timer fires.
> - fn run(this: Self::CallbackTargetParameter<'_>)
> + fn run(this: Self::CallbackTargetParameter<'_>) -> HrTimerRestart
> where
> Self: Sized;
> }
> @@ -262,6 +262,41 @@ unsafe fn start(self_ptr: *const Self, expires: Ktime) {
> }
> }
>
> +/// Restart policy for timers.
> +pub enum HrTimerRestart {
> + /// Timer should not be restarted.
> + NoRestart,
> + /// Timer should be restarted.
> + Restart,
> +}
> +
> +impl From<u32> for HrTimerRestart {
> + fn from(value: u32) -> Self {
> + match value {
> + 0 => Self::NoRestart,
> + _ => Self::Restart,
> + }
> + }
> +}
> +
> +impl From<i32> for HrTimerRestart {
> + fn from(value: i32) -> Self {
> + match value {
> + 0 => Self::NoRestart,
> + _ => Self::Restart,
> + }
> + }
> +}
These are for converting from bindings to our enum, right? Why do we
need both signed and unsigned, and why do we hardcode 0 rather than
using the generated constants?
> +
> +impl From<HrTimerRestart> for bindings::hrtimer_restart {
> + fn from(value: HrTimerRestart) -> Self {
> + match value {
> + HrTimerRestart::NoRestart => bindings::hrtimer_restart_HRTIMER_NORESTART,
> + HrTimerRestart::Restart => bindings::hrtimer_restart_HRTIMER_RESTART,
> + }
> + }
> +}
> +
> /// Use to implement the [`HasHrTimer<T>`] trait.
> ///
> /// See [`module`] documentation for an example.
> diff --git a/rust/kernel/time/hrtimer/arc.rs b/rust/kernel/time/hrtimer/arc.rs
> index 4c1812f190bc7a9e0b2fcd5ea2e320f45ba584bc..cee143b04aa21fa6c60c3363cd95f7a67360cbf8 100644
> --- a/rust/kernel/time/hrtimer/arc.rs
> +++ b/rust/kernel/time/hrtimer/arc.rs
> @@ -82,8 +82,6 @@ impl<U> RawHrTimerCallback for Arc<U>
> // timer. This `U` is contained in an `Arc`.
> let receiver = unsafe { ArcBorrow::from_raw(data_ptr) };
>
> - U::run(receiver);
> -
> - bindings::hrtimer_restart_HRTIMER_NORESTART
> + U::run(receiver).into()
> }
> }
>
> --
> 2.47.0
>
>
>
"Tamir Duberstein" <tamird@gmail.com> writes:
> On Fri, Jan 10, 2025 at 3:17 PM Andreas Hindborg <a.hindborg@kernel.org> wrote:
>>
>> This patch allows timer handlers to report that they want a timer to be
>> restarted after the timer handler has finished executing.
>>
>> Also update the `hrtimer` documentation to showcase the new feature.
>>
>> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
>> ---
>> rust/kernel/time/hrtimer.rs | 37 ++++++++++++++++++++++++++++++++++++-
>> rust/kernel/time/hrtimer/arc.rs | 4 +---
>> 2 files changed, 37 insertions(+), 4 deletions(-)
>>
>> diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs
>> index d0842c7c4c6ddffeef9a79cbf9727819060e4333..50e8c23578b5cf7196893ac88d9547fc027f1f04 100644
>> --- a/rust/kernel/time/hrtimer.rs
>> +++ b/rust/kernel/time/hrtimer.rs
>> @@ -167,7 +167,7 @@ pub trait HrTimerCallback {
>> type CallbackTargetParameter<'a>;
>>
>> /// Called by the timer logic when the timer fires.
>> - fn run(this: Self::CallbackTargetParameter<'_>)
>> + fn run(this: Self::CallbackTargetParameter<'_>) -> HrTimerRestart
>> where
>> Self: Sized;
>> }
>> @@ -262,6 +262,41 @@ unsafe fn start(self_ptr: *const Self, expires: Ktime) {
>> }
>> }
>>
>> +/// Restart policy for timers.
>> +pub enum HrTimerRestart {
>> + /// Timer should not be restarted.
>> + NoRestart,
>> + /// Timer should be restarted.
>> + Restart,
>> +}
>> +
>> +impl From<u32> for HrTimerRestart {
>> + fn from(value: u32) -> Self {
>> + match value {
>> + 0 => Self::NoRestart,
>> + _ => Self::Restart,
>> + }
>> + }
>> +}
>> +
>> +impl From<i32> for HrTimerRestart {
>> + fn from(value: i32) -> Self {
>> + match value {
>> + 0 => Self::NoRestart,
>> + _ => Self::Restart,
>> + }
>> + }
>> +}
>
> These are for converting from bindings to our enum, right? Why do we
> need both signed and unsigned,
Depending on kernel (target arch) configuration, the enum type will be
either signed or unsigned on C side.
I guess I could try to figure out what kernel configs effect the change
and then do conditional compilation on Rust side, but it seems overkill.
> and why do we hardcode 0 rather than
> using the generated constants?
We should use the constants - thanks.
Best regards,
Andreas Hindborg
On Wed, Jan 15, 2025 at 6:21 AM Andreas Hindborg <a.hindborg@kernel.org> wrote:
>
> "Tamir Duberstein" <tamird@gmail.com> writes:
>
> > On Fri, Jan 10, 2025 at 3:17 PM Andreas Hindborg <a.hindborg@kernel.org> wrote:
> >>
> >> This patch allows timer handlers to report that they want a timer to be
> >> restarted after the timer handler has finished executing.
> >>
> >> Also update the `hrtimer` documentation to showcase the new feature.
> >>
> >> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
> >> ---
> >> rust/kernel/time/hrtimer.rs | 37 ++++++++++++++++++++++++++++++++++++-
> >> rust/kernel/time/hrtimer/arc.rs | 4 +---
> >> 2 files changed, 37 insertions(+), 4 deletions(-)
> >>
> >> diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs
> >> index d0842c7c4c6ddffeef9a79cbf9727819060e4333..50e8c23578b5cf7196893ac88d9547fc027f1f04 100644
> >> --- a/rust/kernel/time/hrtimer.rs
> >> +++ b/rust/kernel/time/hrtimer.rs
> >> @@ -167,7 +167,7 @@ pub trait HrTimerCallback {
> >> type CallbackTargetParameter<'a>;
> >>
> >> /// Called by the timer logic when the timer fires.
> >> - fn run(this: Self::CallbackTargetParameter<'_>)
> >> + fn run(this: Self::CallbackTargetParameter<'_>) -> HrTimerRestart
> >> where
> >> Self: Sized;
> >> }
> >> @@ -262,6 +262,41 @@ unsafe fn start(self_ptr: *const Self, expires: Ktime) {
> >> }
> >> }
> >>
> >> +/// Restart policy for timers.
> >> +pub enum HrTimerRestart {
> >> + /// Timer should not be restarted.
> >> + NoRestart,
> >> + /// Timer should be restarted.
> >> + Restart,
> >> +}
> >> +
> >> +impl From<u32> for HrTimerRestart {
> >> + fn from(value: u32) -> Self {
> >> + match value {
> >> + 0 => Self::NoRestart,
> >> + _ => Self::Restart,
> >> + }
> >> + }
> >> +}
> >> +
> >> +impl From<i32> for HrTimerRestart {
> >> + fn from(value: i32) -> Self {
> >> + match value {
> >> + 0 => Self::NoRestart,
> >> + _ => Self::Restart,
> >> + }
> >> + }
> >> +}
> >
> > These are for converting from bindings to our enum, right? Why do we
> > need both signed and unsigned,
>
> Depending on kernel (target arch) configuration, the enum type will be
> either signed or unsigned on C side.
>
> I guess I could try to figure out what kernel configs effect the change
> and then do conditional compilation on Rust side, but it seems overkill.
The bindings seem to contain a type alias:
pub const hrtimer_restart_HRTIMER_NORESTART: hrtimer_restart = 0;
pub const hrtimer_restart_HRTIMER_RESTART: hrtimer_restart = 1;
pub type hrtimer_restart = ffi::c_uint;
Perhaps you could impl `From<bindings:: hrtimer_restart>` instead?
Tamir
"Tamir Duberstein" <tamird@gmail.com> writes:
> On Wed, Jan 15, 2025 at 6:21 AM Andreas Hindborg <a.hindborg@kernel.org> wrote:
>>
>> "Tamir Duberstein" <tamird@gmail.com> writes:
>>
>> > On Fri, Jan 10, 2025 at 3:17 PM Andreas Hindborg <a.hindborg@kernel.org> wrote:
>> >>
>> >> This patch allows timer handlers to report that they want a timer to be
>> >> restarted after the timer handler has finished executing.
>> >>
>> >> Also update the `hrtimer` documentation to showcase the new feature.
>> >>
>> >> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
>> >> ---
>> >> rust/kernel/time/hrtimer.rs | 37 ++++++++++++++++++++++++++++++++++++-
>> >> rust/kernel/time/hrtimer/arc.rs | 4 +---
>> >> 2 files changed, 37 insertions(+), 4 deletions(-)
>> >>
>> >> diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs
>> >> index d0842c7c4c6ddffeef9a79cbf9727819060e4333..50e8c23578b5cf7196893ac88d9547fc027f1f04 100644
>> >> --- a/rust/kernel/time/hrtimer.rs
>> >> +++ b/rust/kernel/time/hrtimer.rs
>> >> @@ -167,7 +167,7 @@ pub trait HrTimerCallback {
>> >> type CallbackTargetParameter<'a>;
>> >>
>> >> /// Called by the timer logic when the timer fires.
>> >> - fn run(this: Self::CallbackTargetParameter<'_>)
>> >> + fn run(this: Self::CallbackTargetParameter<'_>) -> HrTimerRestart
>> >> where
>> >> Self: Sized;
>> >> }
>> >> @@ -262,6 +262,41 @@ unsafe fn start(self_ptr: *const Self, expires: Ktime) {
>> >> }
>> >> }
>> >>
>> >> +/// Restart policy for timers.
>> >> +pub enum HrTimerRestart {
>> >> + /// Timer should not be restarted.
>> >> + NoRestart,
>> >> + /// Timer should be restarted.
>> >> + Restart,
>> >> +}
>> >> +
>> >> +impl From<u32> for HrTimerRestart {
>> >> + fn from(value: u32) -> Self {
>> >> + match value {
>> >> + 0 => Self::NoRestart,
>> >> + _ => Self::Restart,
>> >> + }
>> >> + }
>> >> +}
>> >> +
>> >> +impl From<i32> for HrTimerRestart {
>> >> + fn from(value: i32) -> Self {
>> >> + match value {
>> >> + 0 => Self::NoRestart,
>> >> + _ => Self::Restart,
>> >> + }
>> >> + }
>> >> +}
>> >
>> > These are for converting from bindings to our enum, right? Why do we
>> > need both signed and unsigned,
>>
>> Depending on kernel (target arch) configuration, the enum type will be
>> either signed or unsigned on C side.
>>
>> I guess I could try to figure out what kernel configs effect the change
>> and then do conditional compilation on Rust side, but it seems overkill.
>
> The bindings seem to contain a type alias:
>
> pub const hrtimer_restart_HRTIMER_NORESTART: hrtimer_restart = 0;
> pub const hrtimer_restart_HRTIMER_RESTART: hrtimer_restart = 1;
> pub type hrtimer_restart = ffi::c_uint;
>
> Perhaps you could impl `From<bindings:: hrtimer_restart>` instead?
Yes - good idea!
Best regards,
Andreas Hindborg
© 2016 - 2025 Red Hat, Inc.