From nobody Wed Dec 17 21:27:10 2025 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 F3B0D21B1B5; Fri, 6 Dec 2024 19:35:52 +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=1733513753; cv=none; b=HkAve07f9W7hgcVOy+d2feOHRb2F5q4xlafA6ztfkvpYaKA0On9hQX3HYrTKl9+uBGuA1Ly62EPHBZzPzVb/SpjBHhuJGxOlNRL5wYv3vZG/2XnNNxWjh0ARKkyq6xyf5Pn7Ahku/MKv+T6V4fuXaPZHuH59toDty2N8IuvxJmk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733513753; c=relaxed/simple; bh=nooMkUxQMi3qxanUzKuu/XUmI2mf+pjc89vZvmK0u20=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ZUckQReScLte+F/i8Wg8TtbeuKw5m60pWZq3n7MM+ERMv9C0kjimF/bke6/kOKaVixZ/X0/Cd3cQzPedfe2aXZloJjmuVrt1y33k0HdIeZ1514CH9mW4c/XhN5dAs2cRmB976QZc5hLRwlqZ0cWmrK8C8h8lMqw/WbMhbGy+aII= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=TVMZKJPs; 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="TVMZKJPs" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 46CC4C4CED1; Fri, 6 Dec 2024 19:35:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733513752; bh=nooMkUxQMi3qxanUzKuu/XUmI2mf+pjc89vZvmK0u20=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=TVMZKJPsxx+xhC3VweVMEUOIq3ssEjVlcDbYDvOei+z0zZInytwsv6iiuYAwslovS 0XFIVFOPo8lpUebNGLkdDVp0X6W+3b31ldLEHsVmfAua8t2ScEmYVdj5KCNbACvKs+ ZchfG8wo94I91xfhMyXAHkChiaUWlQ1G570PRqJHp+qH06vxSoNJrXJmbsSxJIXTni jFq4QByQhRDrSNCcQs2nw0Oivff/ETu7U/9vhkp5pt9NvX9GXpgI+a1hF/0pHmWb4O E5HZgF1G07kjbfxuwrRHBNet5kK6o89AVqaM+2ql2bbm4t2rS5dXx2TQaY7j3rkRbY DftEpc6skrlHw== From: Andreas Hindborg Date: Fri, 06 Dec 2024 20:33:04 +0100 Subject: [PATCH v4 12/14] 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: <20241206-hrtimer-v3-v6-12-rc2-v4-12-6cb8c3673682@kernel.org> References: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@kernel.org> In-Reply-To: <20241206-hrtimer-v3-v6-12-rc2-v4-0-6cb8c3673682@kernel.org> To: Miguel Ojeda , Anna-Maria Behnsen , Frederic Weisbecker , Thomas Gleixner , Danilo Krummrich 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 X-Developer-Signature: v=1; a=openpgp-sha256; l=5693; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=n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d0ZzYlJSR3cwcDZhL0pncTJkUGhxWUJCelNIeE1aL2JzQlc3 SlBBMWo1N09nY1oxdUVvQXVNTEVFNwpzTWN5YVROOFFaV0I4bjA5OXRVTjB2SU40cGZ0Uy9XeWR ocGYvTWloM040R1NJWk5FTjV3cmk2MDRSRE1wR3dJCm9HL2dnenhEOUhvZ2RnPT0KPXVnU1AKLS 0tLS1FTkQgUEdQIE1FU1NBR0UtLS0tLQo= X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Allow selection of timer mode by passing a `TimerMode` variant to `Timer::new`. Signed-off-by: Andreas Hindborg --- rust/kernel/time/hrtimer.rs | 87 +++++++++++++++++++++++++++++++++++++++++= ++-- 1 file changed, 84 insertions(+), 3 deletions(-) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index 976a7350d6bee0cebe3dcca04ccbe48fef31e430..3bcc45dae130448173be157ce60= 395e2b9d46a10 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -43,6 +43,8 @@ pub struct Timer { #[pin] timer: Opaque, + // This field goes away when `bindings::hrtimer_setup` is added. + mode: TimerMode, _t: PhantomData, } =20 @@ -55,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,10 +72,11 @@ pub fn new() -> impl PinInit place, Some(T::CallbackTarget::run), bindings::CLOCK_MONOTONIC as i32, - bindings::hrtimer_mode_HRTIMER_MODE_REL, + mode.into(), ); } }), + mode: mode, _t: PhantomData, }) } @@ -322,7 +325,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(), ); } } @@ -363,6 +366,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