Skip to content

Commit bfa3a41

Browse files
author
Andreas Hindborg
committed
rust: hrtimer: add HrTimerMode
Allow selection of timer mode by passing a `HrTimerMode` variant to `HrTimer::new`. Acked-by: Frederic Weisbecker <frederic@kernel.org> Acked-by: Thomas Gleixner <tglx@linutronix.de> Reviewed-by: Lyude Paul <lyude@redhat.com> Reviewed-by: Benno Lossin <benno.lossin@proton.me> Link: https://lore.kernel.org/r/20250309-hrtimer-v3-v6-12-rc2-v12-11-73586e2bd5f1@kernel.org Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
1 parent 374b60a commit bfa3a41

File tree

1 file changed

+79
-3
lines changed

1 file changed

+79
-3
lines changed

rust/kernel/time/hrtimer.rs

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ use core::marker::PhantomData;
8080
pub struct HrTimer<T> {
8181
#[pin]
8282
timer: Opaque<bindings::hrtimer>,
83+
mode: HrTimerMode,
8384
_t: PhantomData<T>,
8485
}
8586

@@ -93,7 +94,7 @@ unsafe impl<T> Sync for HrTimer<T> {}
9394

9495
impl<T> HrTimer<T> {
9596
/// Return an initializer for a new timer instance.
96-
pub fn new() -> impl PinInit<Self>
97+
pub fn new(mode: HrTimerMode) -> impl PinInit<Self>
9798
where
9899
T: HrTimerCallback,
99100
{
@@ -108,10 +109,11 @@ impl<T> HrTimer<T> {
108109
place,
109110
Some(T::Pointer::run),
110111
bindings::CLOCK_MONOTONIC as i32,
111-
bindings::hrtimer_mode_HRTIMER_MODE_REL,
112+
mode.into_c(),
112113
);
113114
}
114115
}),
116+
mode: mode,
115117
_t: PhantomData,
116118
})
117119
}
@@ -369,7 +371,7 @@ pub unsafe trait HasHrTimer<T> {
369371
Self::c_timer_ptr(this).cast_mut(),
370372
expires.to_ns(),
371373
0,
372-
bindings::hrtimer_mode_HRTIMER_MODE_REL,
374+
(*Self::raw_get_timer(this)).mode.into_c(),
373375
);
374376
}
375377
}
@@ -393,6 +395,80 @@ impl HrTimerRestart {
393395
}
394396
}
395397

398+
/// Operational mode of [`HrTimer`].
399+
// NOTE: Some of these have the same encoding on the C side, so we keep
400+
// `repr(Rust)` and convert elsewhere.
401+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
402+
pub enum HrTimerMode {
403+
/// Timer expires at the given expiration time.
404+
Absolute,
405+
/// Timer expires after the given expiration time interpreted as a duration from now.
406+
Relative,
407+
/// Timer does not move between CPU cores.
408+
Pinned,
409+
/// Timer handler is executed in soft irq context.
410+
Soft,
411+
/// Timer handler is executed in hard irq context.
412+
Hard,
413+
/// Timer expires at the given expiration time.
414+
/// Timer does not move between CPU cores.
415+
AbsolutePinned,
416+
/// Timer expires after the given expiration time interpreted as a duration from now.
417+
/// Timer does not move between CPU cores.
418+
RelativePinned,
419+
/// Timer expires at the given expiration time.
420+
/// Timer handler is executed in soft irq context.
421+
AbsoluteSoft,
422+
/// Timer expires after the given expiration time interpreted as a duration from now.
423+
/// Timer handler is executed in soft irq context.
424+
RelativeSoft,
425+
/// Timer expires at the given expiration time.
426+
/// Timer does not move between CPU cores.
427+
/// Timer handler is executed in soft irq context.
428+
AbsolutePinnedSoft,
429+
/// Timer expires after the given expiration time interpreted as a duration from now.
430+
/// Timer does not move between CPU cores.
431+
/// Timer handler is executed in soft irq context.
432+
RelativePinnedSoft,
433+
/// Timer expires at the given expiration time.
434+
/// Timer handler is executed in hard irq context.
435+
AbsoluteHard,
436+
/// Timer expires after the given expiration time interpreted as a duration from now.
437+
/// Timer handler is executed in hard irq context.
438+
RelativeHard,
439+
/// Timer expires at the given expiration time.
440+
/// Timer does not move between CPU cores.
441+
/// Timer handler is executed in hard irq context.
442+
AbsolutePinnedHard,
443+
/// Timer expires after the given expiration time interpreted as a duration from now.
444+
/// Timer does not move between CPU cores.
445+
/// Timer handler is executed in hard irq context.
446+
RelativePinnedHard,
447+
}
448+
449+
impl HrTimerMode {
450+
fn into_c(self) -> bindings::hrtimer_mode {
451+
use bindings::*;
452+
match self {
453+
HrTimerMode::Absolute => hrtimer_mode_HRTIMER_MODE_ABS,
454+
HrTimerMode::Relative => hrtimer_mode_HRTIMER_MODE_REL,
455+
HrTimerMode::Pinned => hrtimer_mode_HRTIMER_MODE_PINNED,
456+
HrTimerMode::Soft => hrtimer_mode_HRTIMER_MODE_SOFT,
457+
HrTimerMode::Hard => hrtimer_mode_HRTIMER_MODE_HARD,
458+
HrTimerMode::AbsolutePinned => hrtimer_mode_HRTIMER_MODE_ABS_PINNED,
459+
HrTimerMode::RelativePinned => hrtimer_mode_HRTIMER_MODE_REL_PINNED,
460+
HrTimerMode::AbsoluteSoft => hrtimer_mode_HRTIMER_MODE_ABS_SOFT,
461+
HrTimerMode::RelativeSoft => hrtimer_mode_HRTIMER_MODE_REL_SOFT,
462+
HrTimerMode::AbsolutePinnedSoft => hrtimer_mode_HRTIMER_MODE_ABS_PINNED_SOFT,
463+
HrTimerMode::RelativePinnedSoft => hrtimer_mode_HRTIMER_MODE_REL_PINNED_SOFT,
464+
HrTimerMode::AbsoluteHard => hrtimer_mode_HRTIMER_MODE_ABS_HARD,
465+
HrTimerMode::RelativeHard => hrtimer_mode_HRTIMER_MODE_REL_HARD,
466+
HrTimerMode::AbsolutePinnedHard => hrtimer_mode_HRTIMER_MODE_ABS_PINNED_HARD,
467+
HrTimerMode::RelativePinnedHard => hrtimer_mode_HRTIMER_MODE_REL_PINNED_HARD,
468+
}
469+
}
470+
}
471+
396472
/// Use to implement the [`HasHrTimer<T>`] trait.
397473
///
398474
/// See [`module`] documentation for an example.

0 commit comments

Comments
 (0)