From nobody Tue Nov 26 17:46:24 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CF7441DE2A2; Thu, 17 Oct 2024 13:09:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729170572; cv=none; b=HI1beb668qvHoRe7gWNdnxcg42rMMJYBdpeovlMENkJX/AZIovSxsbKb2vvPiIwiMdQ6aikFMLTPf83z1olOOzIqXi07Xkcw5LZ56bkYLiHbanc6DM0XpstsNbORUno8krgnfuiqIt/8t0z8m5Pz9t7G1S5BuzTUReH3ud+ZkKE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729170572; c=relaxed/simple; bh=6GsQsOXBkEeRY13Q8KtQu/O5wn3Kj6oFTmpBitQUMoI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=N9lQsdJ5N14e8M/vj6dHnj2ASRkV4jfsiiRliqL7zUbXAj7bcaJ4e6yi1VzsWwrdRa00lG0H2SEko4Vcc1SQxn0M6QDH3nklR6S0bgjnNSlkGNNjQqQ55hHRug2bnoexBk/5bCGfc6y6RTomSsdBbp2BymJeDqw4idLFqnSO5uY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BVpJiZeN; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="BVpJiZeN" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5FB57C4CEC5; Thu, 17 Oct 2024 13:09:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1729170572; bh=6GsQsOXBkEeRY13Q8KtQu/O5wn3Kj6oFTmpBitQUMoI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=BVpJiZeNfa+o7sXwgZhU/9TajP1Ho6+dkU72SJ9Piji006YANwQIRbWBaeWOLaRMu eq71CQszVoXWUZF5Iw75MkzMhCNJItO3oPq1L+7OONkcUG+apb2vZOz/wystMrPIO/ TV0GVq7U9pcWfzPdvgH64zNviZ7nnT5QzimwtiJMDCO4jHUa5mcDNAaNO+1vgC19Va ZZXrcPCBgoWlpBVFg/8/ukpIgo0hXOAZQLZmnlQo8SqSAJgWrjkVt/iyDU3YmYsGCe LJrg/cgc1qbgBqkliGMfS1fVlolYIv3suvkoQeCw32grzHWRx1GXcCuy3ThM+vl1DV RbVtf6Xb2oHOg== From: Andreas Hindborg Date: Thu, 17 Oct 2024 15:04:38 +0200 Subject: [PATCH v3 11/13] rust: hrtimer: add `TimerMode` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241017-hrtimer-v3-v6-12-rc2-v3-11-59a75cbb44da@kernel.org> References: <20241017-hrtimer-v3-v6-12-rc2-v3-0-59a75cbb44da@kernel.org> In-Reply-To: <20241017-hrtimer-v3-v6-12-rc2-v3-0-59a75cbb44da@kernel.org> To: Miguel Ojeda , Anna-Maria Behnsen , Frederic Weisbecker , Thomas Gleixner Cc: Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Lyude Paul , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.14.2 Allow selection of timer mode by passing a `TimerMode` variant to `Timer::new`. Signed-off-by: Andreas Hindborg --- rust/kernel/hrtimer.rs | 88 ++++++++++++++++++++++++++++++++++++++++++++++= +--- 1 file changed, 84 insertions(+), 4 deletions(-) diff --git a/rust/kernel/hrtimer.rs b/rust/kernel/hrtimer.rs index 2c1573e19576de93afc959d71e94173e2c1ed715..1674d1dcba39cc7ab82e1f18900= 2afa365ee9341 100644 --- a/rust/kernel/hrtimer.rs +++ b/rust/kernel/hrtimer.rs @@ -38,12 +38,13 @@ /// # Invariants /// /// * `self.timer` is initialized by `bindings::hrtimer_init`. -#[repr(transparent)] #[pin_data] #[repr(C)] pub struct Timer { #[pin] timer: Opaque, + // This field goes away when `bindings::hrtimer_setup` is added. + mode: TimerMode, _t: PhantomData, } =20 @@ -56,7 +57,7 @@ unsafe impl Sync for Timer {} =20 impl Timer { /// Return an initializer for a new timer instance. - pub fn new() -> impl PinInit + pub fn new(mode: TimerMode) -> impl PinInit where T: TimerCallback, { @@ -70,7 +71,7 @@ pub fn new() -> impl PinInit bindings::hrtimer_init( place, bindings::CLOCK_MONOTONIC as i32, - bindings::hrtimer_mode_HRTIMER_MODE_REL, + mode.into(), ); } =20 @@ -83,6 +84,7 @@ pub fn new() -> impl PinInit // exclusive access. unsafe { core::ptr::write(function, Some(T::CallbackTarget= ::run)) }; }), + mode: mode, _t: PhantomData, }) } @@ -330,7 +332,7 @@ unsafe fn start(self_ptr: *const Self, expires: Ktime) { Self::c_timer_ptr(self_ptr).cast_mut(), expires.to_ns(), 0, - bindings::hrtimer_mode_HRTIMER_MODE_REL, + (*Self::raw_get_timer(self_ptr)).mode.into(), ); } } @@ -362,6 +364,84 @@ fn from(value: TimerRestart) -> Self { } } =20 +/// Operational mode of [`Timer`]. +#[derive(Clone, Copy)] +pub enum TimerMode { + /// Timer expires at the given expiration time. + Absolute, + /// Timer expires after the given expiration time interpreted as a dur= ation from now. + Relative, + /// Timer does not move between CPU cores. + Pinned, + /// Timer handler is executed in soft irq context. + Soft, + /// Timer handler is executed in hard irq context. + Hard, + /// Timer expires at the given expiration time. + /// Timer does not move between CPU cores. + AbsolutePinned, + /// Timer expires after the given expiration time interpreted as a dur= ation from now. + /// Timer does not move between CPU cores. + RelativePinned, + /// Timer expires at the given expiration time. + /// Timer handler is executed in soft irq context. + AbsoluteSoft, + /// Timer expires after the given expiration time interpreted as a dur= ation from now. + /// Timer handler is executed in soft irq context. + RelativeSoft, + /// Timer expires at the given expiration time. + /// Timer does not move between CPU cores. + /// Timer handler is executed in soft irq context. + AbsolutePinnedSoft, + /// Timer expires after the given expiration time interpreted as a dur= ation from now. + /// Timer does not move between CPU cores. + /// Timer handler is executed in soft irq context. + RelativePinnedSoft, + /// Timer expires at the given expiration time. + /// Timer handler is executed in hard irq context. + AbsoluteHard, + /// Timer expires after the given expiration time interpreted as a dur= ation from now. + /// Timer handler is executed in hard irq context. + RelativeHard, + /// Timer expires at the given expiration time. + /// Timer does not move between CPU cores. + /// Timer handler is executed in hard irq context. + AbsolutePinnedHard, + /// Timer expires after the given expiration time interpreted as a dur= ation from now. + /// Timer does not move between CPU cores. + /// Timer handler is executed in hard irq context. + RelativePinnedHard, +} + +impl From for bindings::hrtimer_mode { + fn from(value: TimerMode) -> Self { + use bindings::*; + match value { + TimerMode::Absolute =3D> hrtimer_mode_HRTIMER_MODE_ABS, + TimerMode::Relative =3D> hrtimer_mode_HRTIMER_MODE_REL, + TimerMode::Pinned =3D> hrtimer_mode_HRTIMER_MODE_PINNED, + TimerMode::Soft =3D> hrtimer_mode_HRTIMER_MODE_SOFT, + TimerMode::Hard =3D> hrtimer_mode_HRTIMER_MODE_HARD, + TimerMode::AbsolutePinned =3D> hrtimer_mode_HRTIMER_MODE_ABS_P= INNED, + TimerMode::RelativePinned =3D> hrtimer_mode_HRTIMER_MODE_REL_P= INNED, + TimerMode::AbsoluteSoft =3D> hrtimer_mode_HRTIMER_MODE_ABS_SOF= T, + TimerMode::RelativeSoft =3D> hrtimer_mode_HRTIMER_MODE_REL_SOF= T, + TimerMode::AbsolutePinnedSoft =3D> hrtimer_mode_HRTIMER_MODE_A= BS_PINNED_SOFT, + TimerMode::RelativePinnedSoft =3D> hrtimer_mode_HRTIMER_MODE_R= EL_PINNED_SOFT, + TimerMode::AbsoluteHard =3D> hrtimer_mode_HRTIMER_MODE_ABS_HAR= D, + TimerMode::RelativeHard =3D> hrtimer_mode_HRTIMER_MODE_REL_HAR= D, + TimerMode::AbsolutePinnedHard =3D> hrtimer_mode_HRTIMER_MODE_A= BS_PINNED_HARD, + TimerMode::RelativePinnedHard =3D> hrtimer_mode_HRTIMER_MODE_R= EL_PINNED_HARD, + } + } +} + +impl From for u64 { + fn from(value: TimerMode) -> Self { + Into::::into(value) as u64 + } +} + /// Use to implement the [`HasTimer`] trait. /// /// See [`module`] documentation for an example. --=20 2.46.0