From nobody Fri Nov 29 18:40:16 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 72129178363; Tue, 17 Sep 2024 22:28: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=1726612132; cv=none; b=ISz9D5FFPh0BTSv9d/OJGJaPxowMBlZOl1465hsFtyn4SVf44iagvjSVEADhSuJqPEW8u03qsEwDOoY86vRDCWPXnY/CjGnc9K4efdXFsC9+1TP766zbLOGOw3niM9yoiBUMNQpq4oGfqQcTGpLaRUGroft4vTcN5JuGNEGrlik= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726612132; c=relaxed/simple; bh=BkB2xvYeoQrGfgSamMPfNUGbcq5vvBqTCceqHEixtuQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jxmX2zhRIFJqHj64KOhGDlEABNuQEHJcXR5Gmgh9G25NyshLNWB2Ulpg2+Vd6lAQW3uQO38FzyYkeXoaqZhjpVoV5UPttYnmj6mw9yV3gqHw3c7+bjS1ZFcyD1fw5004+ZkeEC+1eawOMp/sML+t8/iu6ywnTkjp1o/lysVb9Ug= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=n7Frz93C; 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="n7Frz93C" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7E34CC4CEC5; Tue, 17 Sep 2024 22:28:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1726612132; bh=BkB2xvYeoQrGfgSamMPfNUGbcq5vvBqTCceqHEixtuQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=n7Frz93Cekm1lxLTVeggTlHMKB3veKPOhzSifoEG8vRWNYoR3zL437JgtCROg9tL0 wu3+hVtrk0tPN6ybFg9S+TyThheFZrTCtxQuHCAvsWwca1qAQMiUL/nzGZGlEOUQBI f9Sfe7cIyFHKCVGTO1hkrb7j2WikzbLyTDRHCLMTXxQO8lMTHwUPrGDC68N8T/D9s+ a/gZCIDrmpeFmli9eJRHpGrz+wQWLqX1i6cFjoAHRb/Ph6aXNKSdH1WkT1Tvnyi/bX X6qkzs4uWgtJoCQFxQ7Naa5Ub1Mci490nJ+leXWgTYljznY24mr6DwOJjvXVDe48+E 55Y8QaGa+kHYQ== From: Andreas Hindborg To: Miguel Ojeda , Alex Gaynor , Anna-Maria Behnsen , Frederic Weisbecker , Thomas Gleixner Cc: Andreas Hindborg , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Alice Ryhl , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 08/14] rust: hrtimer: implement `UnsafeTimerPointer` for `Pin<&T>` Date: Wed, 18 Sep 2024 00:27:32 +0200 Message-ID: <20240917222739.1298275-9-a.hindborg@kernel.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240917222739.1298275-1-a.hindborg@kernel.org> References: <20240917222739.1298275-1-a.hindborg@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3873; i=a.hindborg@kernel.org; h=from:subject; bh=BkB2xvYeoQrGfgSamMPfNUGbcq5vvBqTCceqHEixtuQ=; b=LS0tLS1CRUdJTiBQR1AgTUVTU0FHRS0tLS0tCgpvd0o0bkFGdEFw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0MG1Wa2IyZ1ZYV2JVejJ kbit3RysrdnBrcWFoemZ5bG1CMS95eWRVSmMyUU5RCll4eTRHNVFZcDZVd3N3PT0KPUxJRnoKLS 0tLS1FTkQgUEdQIE1FU1NBR0UtLS0tLQo= X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Allow pinned references to structs that contain a `Timer` node to be scheduled with the `hrtimer` subsystem. Signed-off-by: Andreas Hindborg --- rust/kernel/hrtimer.rs | 1 + rust/kernel/hrtimer/pin.rs | 96 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 rust/kernel/hrtimer/pin.rs diff --git a/rust/kernel/hrtimer.rs b/rust/kernel/hrtimer.rs index bb6349f440e2..25d3702d0d05 100644 --- a/rust/kernel/hrtimer.rs +++ b/rust/kernel/hrtimer.rs @@ -396,3 +396,4 @@ unsafe fn raw_get_timer(ptr: *const Self) -> } =20 mod arc; +mod pin; diff --git a/rust/kernel/hrtimer/pin.rs b/rust/kernel/hrtimer/pin.rs new file mode 100644 index 000000000000..f9ce0498a0d2 --- /dev/null +++ b/rust/kernel/hrtimer/pin.rs @@ -0,0 +1,96 @@ +// SPDX-License-Identifier: GPL-2.0 + +use super::HasTimer; +use super::RawTimerCallback; +use super::Timer; +use super::TimerCallback; +use super::TimerHandle; +use super::UnsafeTimerPointer; +use crate::time::Ktime; +use core::pin::Pin; + +/// A handle for a `Pin<&HasTimer>`. When the handle exists, the timer mig= ht be +/// armed. +pub struct PinTimerHandle<'a, U> +where + U: HasTimer, +{ + pub(crate) inner: Pin<&'a U>, +} + +// SAFETY: We cancel the timer when the handle is dropped. The implementat= ion of +// the `cancel` method will block if the timer handler is running. +unsafe impl<'a, U> TimerHandle for PinTimerHandle<'a, U> +where + U: HasTimer, +{ + fn cancel(&mut self) -> bool { + let self_ptr =3D self.inner.get_ref() as *const U; + + // SAFETY: As we got `self_ptr` from a reference above, it must po= int to + // a valid `U`. + let timer_ptr =3D unsafe { >::raw_get_timer(self_= ptr) }; + + // SAFETY: As `timer_ptr` is derived from a reference, it must poi= nt to + // a valid and initialized `Timer`. + unsafe { Timer::::raw_cancel(timer_ptr) } + } +} + +impl<'a, U> Drop for PinTimerHandle<'a, U> +where + U: HasTimer, +{ + fn drop(&mut self) { + self.cancel(); + } +} + +// SAFETY: We capture the lifetime of `Self` when we create a `PinTimerHan= dle`, +// so `Self` will outlive the handle. +unsafe impl<'a, U> UnsafeTimerPointer for Pin<&'a U> +where + U: Send + Sync, + U: HasTimer, + U: TimerCallback =3D Self>, +{ + type TimerHandle =3D PinTimerHandle<'a, U>; + + unsafe fn schedule(self, expires: Ktime) -> Self::TimerHandle { + use core::ops::Deref; + + // Cast to pointer + let self_ptr =3D self.deref() as *const U; + + // SAFETY: As we derive `self_ptr` from a reference above, it must= point + // to a valid `U`. + unsafe { U::schedule(self_ptr, expires) }; + + PinTimerHandle { inner: self } + } +} + +impl<'a, U> RawTimerCallback for Pin<&'a U> +where + U: HasTimer, + U: TimerCallback =3D Self>, +{ + unsafe extern "C" fn run(ptr: *mut bindings::hrtimer) -> bindings::hrt= imer_restart { + // `Timer` is `repr(transparent)` + let timer_ptr =3D ptr as *mut Timer; + + // SAFETY: By the safety requirement of this function, `timer_ptr` + // points to a `Timer` contained in an `U`. + let receiver_ptr =3D unsafe { U::timer_container_of(timer_ptr) }; + + // SAFETY: By the safety requirement of this function, `timer_ptr` + // points to a `Timer` contained in an `U`. + let receiver_ref =3D unsafe { &*receiver_ptr }; + + // SAFETY: `receiver_ref` only exists as pinned, so it is safe to = pin it + // here. + let receiver_pin =3D unsafe { Pin::new_unchecked(receiver_ref) }; + + U::run(receiver_pin).into() + } +} --=20 2.46.0