From nobody Tue Nov 26 17:21:52 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 3A6571DE893; Thu, 17 Oct 2024 13:09:36 +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=1729170577; cv=none; b=GptpORyyMeVyl052m5tNVO6vPEgp8CHCZQi5rnnAypvhONsLYswpUE2OGQquupG01JaucycCh84MhQi6RIhMVpcHtiImfhgI9ScHMQZJyrO+1jaKOrLA784NeiRHNM7JJlKdroZtnBKPPaVmpv25MY/9YrQAWmiRDV+eh+majTA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729170577; c=relaxed/simple; bh=vK66/laxxraODLHeUfQy6EIVcT/80ojUgOWDCBSGJcs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=CvTW+EexXtwulsZ142icJXRyiM1B6srLxkOA3/P+KiNePCN+vVDkrRI7Chl0RZITLDl0B8tJSiJnNiB/jLr/DwTrMSigPUOWeCoS5jTtXuM8alZ7bUq5oROUcQ2ttAruBL1wFerljo/cDmBieYTfFb9CuEv2HXz2hxErjccyiz0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nXikI7Q/; 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="nXikI7Q/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6A1E1C4CEC3; Thu, 17 Oct 2024 13:09:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1729170576; bh=vK66/laxxraODLHeUfQy6EIVcT/80ojUgOWDCBSGJcs=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=nXikI7Q/iXtSUChiZjg5VM2V6SbDL1EWfPtcYI9l3xuc0MD5mIqkK7Dew/Tt11qDI KQJDa7RnGN9o6Df22UyGYTYza654ur8duVzUBEgGuxtg2Lwxug3Rc/wwn/F0Lj7A/0 HhpBxgRPon2vt/trsLXM8KFDoJYV+/rwIomWS6YdgoHUf2K4x5vBkeVJ1pt7bUjyP1 yEF4JpaLN9962Y3XWYnygBQGoaLydooyRDTjM+QSxzRe+DKd/H2Fyz8tcTmKVgIWA2 HhbGPy5fjcEjeQIzB7igFcQrseMiQNVDI+QEpLahT9Yut7fzYS2JZThTpC/2TnXMWw cw8y1fNyZ/sqQ== From: Andreas Hindborg Date: Thu, 17 Oct 2024 15:04:35 +0200 Subject: [PATCH v3 08/13] rust: hrtimer: implement `UnsafeTimerPointer` for `Pin<&mut T>` 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-8-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 pinned mutable 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_mut.rs | 99 ++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 100 insertions(+) diff --git a/rust/kernel/hrtimer.rs b/rust/kernel/hrtimer.rs index ceedf330a803ec2db7ff6c25713ae48e2fd1f4ca..940390aa2a6c6a222534b545d8d= 2fbd639a19f64 100644 --- a/rust/kernel/hrtimer.rs +++ b/rust/kernel/hrtimer.rs @@ -363,3 +363,4 @@ unsafe fn raw_get_timer(ptr: *const Self) -> =20 mod arc; mod pin; +mod pin_mut; diff --git a/rust/kernel/hrtimer/pin_mut.rs b/rust/kernel/hrtimer/pin_mut.rs new file mode 100644 index 0000000000000000000000000000000000000000..d6c85647a762066776cf292620a= 8b19c12721876 --- /dev/null +++ b/rust/kernel/hrtimer/pin_mut.rs @@ -0,0 +1,99 @@ +// 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<&mut HasTimer>`. When the handle exists, the timer= might +/// be running. +pub struct PinMutTimerHandle<'a, U> +where + U: HasTimer, +{ + pub(crate) inner: Pin<&'a mut 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 PinMutTimerHandle<'a, U> +where + U: HasTimer, +{ + fn cancel(&mut self) -> bool { + // SAFETY: We are not moving out of `self` or handing out mutable + // references to `self`. + let self_ptr =3D unsafe { self.inner.as_mut().get_unchecked_mut() = as *mut 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 PinMutTimerHandle<'a, U> +where + U: HasTimer, +{ + fn drop(&mut self) { + self.cancel(); + } +} + +// SAFETY: We capture the lifetime of `Self` when we create a +// `PinMutTimerHandle`, so `Self` will outlive the handle. +unsafe impl<'a, U> UnsafeTimerPointer for Pin<&'a mut U> +where + U: Send + Sync, + U: HasTimer, + U: TimerCallback =3D Self>, +{ + type TimerHandle =3D PinMutTimerHandle<'a, U>; + + unsafe fn start(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::start(self_ptr, expires) }; + + PinMutTimerHandle { inner: self } + } +} + +impl<'a, U> RawTimerCallback for Pin<&'a mut U> +where + U: HasTimer, + U: TimerCallback =3D Self>, + U: TimerCallback =3D Self>, +{ + unsafe extern "C" fn run(ptr: *mut bindings::hrtimer) -> bindings::hrt= imer_restart { + // `Timer` is `repr(C)` + 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 { &mut *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