From nobody Thu Oct 9 01:11:38 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 62A0A270EB2; Sat, 21 Jun 2025 19:51: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=1750535492; cv=none; b=A8hCNS1j/p4vYRSkjNE8gyRT+vAQHdb3pdxopaYJ9HknZUxifnLvMdOJobadMjJ7/4liq5MtgIRfl8yDzsxH4oLlRDb4jWlNAU3GmfEJlBORaWO7VyAq10cwm8EeOBgcyAMgn4Bxx1ndBeJu94nSj2FqH0gLoMEQhesl6G2P2/Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750535492; c=relaxed/simple; bh=pjAS50S4PI0FmiSCTxI6FL6s36Lw3uJn25OAPQ7R6MI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ndM+R8GSOZ0d443Le52f6Nu7od/nVhe3DTTHk7Z3DLIQMPlkaBMVgc9ODcBH0EN+16dT4bFUXCsA/mdkGuujw47FEHtM5Yg1oWJb6WDvxZP4CeJba6pYQreHe5WZUbgwPc+fWVtNilhoM5EosjJdXGdLMdq7w2FdcrvUs0Bh9JQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=JAd2ESdF; 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="JAd2ESdF" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 62AF0C4CEF1; Sat, 21 Jun 2025 19:51:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1750535492; bh=pjAS50S4PI0FmiSCTxI6FL6s36Lw3uJn25OAPQ7R6MI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JAd2ESdFeOHFtJQFC/QSF+YDUBhemjZHjCp9UfZcZ+OUfJr2ofRUL885/rtHSVT1e v2Q3XmVM39n5uICtfLZWJQxrCJjBJuCI/o5OqI/BFyt5TtcpT33rV5TUPT9T+rBYJN K56ScRu2tCqkTIs4dIJXA1hHBthcSWSOVae0OLAgxGOHsyxBp0Bvcz7Dyic5Ek891+ 2gR/U6qAMfNF76Wt/8qGuTacGX3hVO7W0UcSkLFmd0EKaGXG7Csu6OBYMkTS09oOfO Bo1aLbzPTXHszpk50Kclml26EnskhEWgZczcYbNfdZvZeSbD5wwqcTNfQaK0AimhHY Wk2l3lik6od6g== From: Danilo Krummrich To: gregkh@linuxfoundation.org, rafael@kernel.org, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, david.m.ertman@intel.com, ira.weiny@intel.com, leon@kernel.org, kwilczynski@kernel.org, bhelgaas@google.com Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Danilo Krummrich Subject: [PATCH 1/8] rust: device: introduce device::Internal Date: Sat, 21 Jun 2025 21:43:27 +0200 Message-ID: <20250621195118.124245-2-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250621195118.124245-1-dakr@kernel.org> References: <20250621195118.124245-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce an internal device context, which is semantically equivalent to the Core device context, but reserved for bus abstractions. This allows implementing methods for the Device type, which are limited to be used within the core context of bus abstractions, i.e. restrict the availability for drivers. Signed-off-by: Danilo Krummrich --- rust/kernel/device.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index 665f5ceadecc..e9094d8322d5 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -261,6 +261,10 @@ pub trait DeviceContext: private::Sealed {} /// any of the bus callbacks, such as `probe()`. pub struct Core; =20 +/// Semantically the same as [`Core`] but reserved for internal usage of t= he corresponding bus +/// abstraction. +pub struct Internal; + /// The [`Bound`] context is the context of a bus specific device referenc= e when it is guaranteed to /// be bound for the duration of its lifetime. pub struct Bound; @@ -270,11 +274,13 @@ pub trait Sealed {} =20 impl Sealed for super::Bound {} impl Sealed for super::Core {} + impl Sealed for super::Internal {} impl Sealed for super::Normal {} } =20 impl DeviceContext for Bound {} impl DeviceContext for Core {} +impl DeviceContext for Internal {} impl DeviceContext for Normal {} =20 /// # Safety @@ -312,6 +318,13 @@ fn deref(&self) -> &Self::Target { #[macro_export] macro_rules! impl_device_context_deref { (unsafe { $device:ident }) =3D> { + // SAFETY: This macro has the exact same safety requirement as + // `__impl_device_context_deref!`. + ::kernel::__impl_device_context_deref!(unsafe { + $device, + $crate::device::Internal =3D> $crate::device::Core + }); + // SAFETY: This macro has the exact same safety requirement as // `__impl_device_context_deref!`. ::kernel::__impl_device_context_deref!(unsafe { @@ -345,6 +358,7 @@ fn from(dev: &$device<$src>) -> Self { #[macro_export] macro_rules! impl_device_context_into_aref { ($device:tt) =3D> { + ::kernel::__impl_device_context_into_aref!($crate::device::Interna= l, $device); ::kernel::__impl_device_context_into_aref!($crate::device::Core, $= device); ::kernel::__impl_device_context_into_aref!($crate::device::Bound, = $device); }; --=20 2.49.0 From nobody Thu Oct 9 01:11:38 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 0F5D6253F2C; Sat, 21 Jun 2025 19:51: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=1750535497; cv=none; b=NC5VxYhPgHE3A6HmjPfuiH0q8hM3W206Xthb58nPSifrCZtixReM+1tmg2fKL2zDv5u0VAaqCMC7DBZq1Q58UUsPdTwJohEFHozT0A5XZhMfcvt0EyehOhvqIIVyJ23lRSk8Y2j+Ww5E4XwJchGv0Q0AHus9J89cgj4NEyWNw6k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750535497; c=relaxed/simple; bh=YHLg1VTNOxaRSyhsBjrCYNRgQ+pK9YBYD8hTsccsNsw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rgEHlPkecYBEpUk8KXPoMbZTsN7shsb4+RywwCY7MT6DLmHhhGxPaePYnox+mYNcHh3RMpzO9/tyiXmYGBUiyc5JYsLRUVkB7YO4jBfC+rpBL79hvW3T4lXY/IMWkTjSfnO7YppCxxcG+hRg9lngh2bEi6EB5qdwXP/IMCHfSK0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qY7VFCX/; 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="qY7VFCX/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id AD986C4CEE7; Sat, 21 Jun 2025 19:51:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1750535496; bh=YHLg1VTNOxaRSyhsBjrCYNRgQ+pK9YBYD8hTsccsNsw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qY7VFCX/W4HfmbJ5ws7CF3ynLLx/ICx0MNP4LhvXZiPAj9UzLiwDi8atnPGnGiUsP 4Vi230AzK6ORztPoMWwVIVBHPUl7EIf4tA2tZVBaoWSSryj08fyrcSev/ArWtZaNKh UimiXPhz7Zx9S5len76VVUbvwQt17SyJpEStoo7CykRI6D1DN3P8RWfmBFZZuVygGC A4IoM8B7rSW14Axb2MYo1qG6BglEYCjn3edZGNT3y6zf6XEJXrRNIBKKDEWdE6xj8T MB/DJ6+0SCgKbwai+L3Dma59wZRrDbsUcXM0yhQFmuiWna9v1GUFS/1OT3uW1thIKQ yjLEMtdKQFMxw== From: Danilo Krummrich To: gregkh@linuxfoundation.org, rafael@kernel.org, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, david.m.ertman@intel.com, ira.weiny@intel.com, leon@kernel.org, kwilczynski@kernel.org, bhelgaas@google.com Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Danilo Krummrich Subject: [PATCH 2/8] rust: device: add drvdata accessors Date: Sat, 21 Jun 2025 21:43:28 +0200 Message-ID: <20250621195118.124245-3-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250621195118.124245-1-dakr@kernel.org> References: <20250621195118.124245-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Implement generic accessors for the private data of a driver bound to a device. Those accessors should be used by bus abstractions from their corresponding core callbacks, such as probe(), remove(), etc. Implementing them for device::Internal guarantees that driver's can't interfere with the logic implemented by the bus abstraction. Signed-off-by: Danilo Krummrich Acked-by: Benno Lossin --- rust/helpers/device.c | 10 ++++++++++ rust/kernel/device.rs | 43 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/rust/helpers/device.c b/rust/helpers/device.c index b2135c6686b0..9bf252649c75 100644 --- a/rust/helpers/device.c +++ b/rust/helpers/device.c @@ -8,3 +8,13 @@ int rust_helper_devm_add_action(struct device *dev, { return devm_add_action(dev, action, data); } + +void *rust_helper_dev_get_drvdata(const struct device *dev) +{ + return dev_get_drvdata(dev); +} + +void rust_helper_dev_set_drvdata(struct device *dev, void *data) +{ + dev_set_drvdata(dev, data); +} diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index e9094d8322d5..146eba147d2f 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -6,7 +6,7 @@ =20 use crate::{ bindings, - types::{ARef, Opaque}, + types::{ARef, ForeignOwnable, Opaque}, }; use core::{fmt, marker::PhantomData, ptr}; =20 @@ -62,6 +62,47 @@ pub unsafe fn get_device(ptr: *mut bindings::device) -> = ARef { } } =20 +impl Device { + /// Store a pointer to the bound driver's private data. + pub fn set_drvdata(&self, data: impl ForeignOwnable) { + // SAFETY: By the type invariants, `self.as_raw()` is a valid poin= ter to a `struct device`. + unsafe { bindings::dev_set_drvdata(self.as_raw(), data.into_foreig= n().cast()) } + } + + /// Take ownership of the private data stored in this [`Device`]. + /// + /// # Safety + /// + /// - Must only be called once after a preceding call to [`Device::set= _drvdata`]. + /// - The type `T` must match the type of the `ForeignOwnable` previou= sly stored by + /// [`Device::set_drvdata`]. + pub unsafe fn drvdata_obtain(&self) -> T { + // SAFETY: By the type invariants, `self.as_raw()` is a valid poin= ter to a `struct device`. + let ptr =3D unsafe { bindings::dev_get_drvdata(self.as_raw()) }; + + // SAFETY: By the safety requirements of this function, `ptr` come= s from a previous call to + // `into_foreign()`. + unsafe { T::from_foreign(ptr.cast()) } + } + + /// Borrow the driver's private data bound to this [`Device`]. + /// + /// # Safety + /// + /// - Must only be called after a preceding call to [`Device::set_drvd= ata`] and before + /// [`Device::drvdata_obtain`]. + /// - The type `T` must match the type of the `ForeignOwnable` previou= sly stored by + /// [`Device::set_drvdata`]. + pub unsafe fn drvdata_borrow(&self) -> T::Borrowed<= '_> { + // SAFETY: By the type invariants, `self.as_raw()` is a valid poin= ter to a `struct device`. + let ptr =3D unsafe { bindings::dev_get_drvdata(self.as_raw()) }; + + // SAFETY: By the safety requirements of this function, `ptr` come= s from a previous call to + // `into_foreign()`. + unsafe { T::borrow(ptr.cast()) } + } +} + impl Device { /// Obtain the raw `struct device *`. pub(crate) fn as_raw(&self) -> *mut bindings::device { --=20 2.49.0 From nobody Thu Oct 9 01:11:38 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 515252701C8; Sat, 21 Jun 2025 19:51:40 +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=1750535501; cv=none; b=vEIhOqR4MBpz5Aol7Yp8LaVHsSK+sRjKK1Fj9JiBo8W4WXok/FMLkkctk7KRrZ3Yj3wLyfQ6gjq69iuVxeuE3t3rjId6h4hoCEH2zj3JjzBFs9lB9uIlrca3wOpPRiClEEg4UU31Ok8OPr9xiX2WWyvEjwtscAolaD9QhDQQvD0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750535501; c=relaxed/simple; bh=64LA9BPdINZxDweXi3XrPvDZBhIiC67sjGS0F3HdOPg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qbEg1qQaM3BwOu53FHbqekIZElx5fuJ1Sw40LTbR/FKccdOadyASym86tRYqh0iHoCcbSkga0DqhpHbUqb+rEQs/9kQ9/EKuUKKatgBVCIwp/7DNw/yEmb84T7D5H1hQy2i6NkUAQHO5x2B4KUYBgLUnh/fSE6fRvRyE2S0bIdY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hPIx4PHB; 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="hPIx4PHB" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0374FC4CEF0; Sat, 21 Jun 2025 19:51:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1750535500; bh=64LA9BPdINZxDweXi3XrPvDZBhIiC67sjGS0F3HdOPg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hPIx4PHBQ9WQfZht+Ufuepc4qS5Roc/26MRLN02aPZzGbw9qYrodi3BjLb0StRPzC NJlj77yt+o9HTMKe2buOjP+U1Wb5ABZFKlND4Cw19YlOjjWfK89jjRy+joD78uzsCt DyeQkN5ol//HC7ZY0lGkOwA7cGG35e8/q38RNQJHOTA46Rclm7crVRvKvFmfMnW58m 25lS9K9dxiHVzIG24n4EnSRb/hY4JFm67eif6rRTuJvfgwt9Npcoc8isoBlNri952h ddimMd8GSOGONMB2AkAHvxbkOOcmiqEVdQtr8mfS5BCD+3ImtUrlPEzeRHsYo3AKBJ 1pmPkPBy10fEQ== From: Danilo Krummrich To: gregkh@linuxfoundation.org, rafael@kernel.org, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, david.m.ertman@intel.com, ira.weiny@intel.com, leon@kernel.org, kwilczynski@kernel.org, bhelgaas@google.com Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Danilo Krummrich Subject: [PATCH 3/8] rust: platform: use generic device drvdata accessors Date: Sat, 21 Jun 2025 21:43:29 +0200 Message-ID: <20250621195118.124245-4-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250621195118.124245-1-dakr@kernel.org> References: <20250621195118.124245-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Take advantage of the generic drvdata accessors of the generic Device type. While at it, use from_result() instead of match. Signed-off-by: Danilo Krummrich --- rust/helpers/platform.c | 10 ---------- rust/kernel/platform.rs | 36 +++++++++++++++++------------------- 2 files changed, 17 insertions(+), 29 deletions(-) diff --git a/rust/helpers/platform.c b/rust/helpers/platform.c index 82171233d12f..1ce89c1a36f7 100644 --- a/rust/helpers/platform.c +++ b/rust/helpers/platform.c @@ -2,16 +2,6 @@ =20 #include =20 -void *rust_helper_platform_get_drvdata(const struct platform_device *pdev) -{ - return platform_get_drvdata(pdev); -} - -void rust_helper_platform_set_drvdata(struct platform_device *pdev, void *= data) -{ - platform_set_drvdata(pdev, data); -} - bool rust_helper_dev_is_platform(const struct device *dev) { return dev_is_platform(dev); diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs index 5b21fa517e55..dc0c36d70963 100644 --- a/rust/kernel/platform.rs +++ b/rust/kernel/platform.rs @@ -6,11 +6,11 @@ =20 use crate::{ bindings, container_of, device, driver, - error::{to_result, Result}, + error::{from_result, to_result, Result}, of, prelude::*, str::CStr, - types::{ForeignOwnable, Opaque}, + types::Opaque, ThisModule, }; =20 @@ -61,30 +61,28 @@ extern "C" fn probe_callback(pdev: *mut bindings::platf= orm_device) -> kernel::ff // `struct platform_device`. // // INVARIANT: `pdev` is valid for the duration of `probe_callback(= )`. - let pdev =3D unsafe { &*pdev.cast::>() }; - + let pdev =3D unsafe { &*pdev.cast::>() }; let info =3D ::id_info(pdev.as_ref()); - match T::probe(pdev, info) { - Ok(data) =3D> { - // Let the `struct platform_device` own a reference of the= driver's private data. - // SAFETY: By the type invariant `pdev.as_raw` returns a v= alid pointer to a - // `struct platform_device`. - unsafe { bindings::platform_set_drvdata(pdev.as_raw(), dat= a.into_foreign() as _) }; - } - Err(err) =3D> return Error::to_errno(err), - } =20 - 0 + from_result(|| { + let data =3D T::probe(pdev, info)?; + + pdev.as_ref().set_drvdata(data); + Ok(0) + }) } =20 extern "C" fn remove_callback(pdev: *mut bindings::platform_device) { - // SAFETY: `pdev` is a valid pointer to a `struct platform_device`. - let ptr =3D unsafe { bindings::platform_get_drvdata(pdev) }.cast(); + // SAFETY: The platform bus only ever calls the remove callback wi= th a valid pointer to a + // `struct platform_device`. + // + // INVARIANT: `pdev` is valid for the duration of `probe_callback(= )`. + let pdev =3D unsafe { &*pdev.cast::>() }; =20 // SAFETY: `remove_callback` is only ever called after a successfu= l call to - // `probe_callback`, hence it's guaranteed that `ptr` points to a = valid and initialized - // `KBox` pointer created through `KBox::into_foreign`. - let _ =3D unsafe { KBox::::from_foreign(ptr) }; + // `probe_callback`, hence it's guaranteed that `Device::set_drvda= ta()` has been called + // and stored a `Pin>`. + let _ =3D unsafe { pdev.as_ref().drvdata_obtain::>>() = }; } } =20 --=20 2.49.0 From nobody Thu Oct 9 01:11:38 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 82CE427056D; Sat, 21 Jun 2025 19:51:45 +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=1750535505; cv=none; b=REB8w9pMzVZpJzboZ2q94IvPGaXGpFY2CQS9i9aEZud8h2FiH4Flx40QqbIelOWCeX8PPUYfem/xFQ75c/xsh57V2MK/4u5jqNVqqfzqZLI11fxn8q7GALllFex1PBnUb5SOX22PkyNTbgV+Vv97xjf6AYq4OijQHHrYEGBAbmg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750535505; c=relaxed/simple; bh=o1NrCTqKOLaLyCyMx28sXtEauIeDFKCQj+3OLoj5FnM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=T3GVO8Oh5x9JSZMic1KSmBOyIim/wnka1ePucBwQu+5OQMfFzGm1VHtINYXgLWqlVR3GVtkS3mdsdfS6YZe35RSDZqorx55+OcDBs4etiotsfe0Gd3upk8VyXbfm7Q/zCuxbVMo6KGSCHeFAfvvWtK9wKeYwOMDShKyrreO4I5Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZLJNMWFh; 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="ZLJNMWFh" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4DA9BC4CEE7; Sat, 21 Jun 2025 19:51:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1750535505; bh=o1NrCTqKOLaLyCyMx28sXtEauIeDFKCQj+3OLoj5FnM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZLJNMWFhH1IC/AZMKYFp/cjcYO5RYN+pG5sN4ZnyCHyUyHLP+7OYy0ynXNyt8uSmZ wfMoIKlomph/K0PwwyqGGPNa1EkR9nPWMvvKsoGMlzxWZ5H4xp+thExhCLrTf5/iaS 8vok5bSb92Nn3jqEV7turYM6K0OAa7Z1vk6oE+PNb3Ml3sUMk15UB+MqR7+6w+I0pt Sj/PQOzSqd06euSR/ZlxZ9eiVUttELyk5OgUgKTbnD19Z3F9w7C0gvuk8GCUbyi/4S N9u1FHAnxdSfBAz7kEvKWILV5qUQ8WHY7PzVc6k1VoQQWu/QtorbDkbUp4FtxO0neR VPXgwT01t5nxA== From: Danilo Krummrich To: gregkh@linuxfoundation.org, rafael@kernel.org, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, david.m.ertman@intel.com, ira.weiny@intel.com, leon@kernel.org, kwilczynski@kernel.org, bhelgaas@google.com Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Danilo Krummrich Subject: [PATCH 4/8] rust: pci: use generic device drvdata accessors Date: Sat, 21 Jun 2025 21:43:30 +0200 Message-ID: <20250621195118.124245-5-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250621195118.124245-1-dakr@kernel.org> References: <20250621195118.124245-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Take advantage of the generic drvdata accessors of the generic Device type. While at it, use from_result() instead of match. Signed-off-by: Danilo Krummrich --- rust/helpers/pci.c | 10 ---------- rust/kernel/pci.rs | 31 ++++++++++++++----------------- 2 files changed, 14 insertions(+), 27 deletions(-) diff --git a/rust/helpers/pci.c b/rust/helpers/pci.c index cd0e6bf2cc4d..ef9cb38c81a6 100644 --- a/rust/helpers/pci.c +++ b/rust/helpers/pci.c @@ -2,16 +2,6 @@ =20 #include =20 -void rust_helper_pci_set_drvdata(struct pci_dev *pdev, void *data) -{ - pci_set_drvdata(pdev, data); -} - -void *rust_helper_pci_get_drvdata(struct pci_dev *pdev) -{ - return pci_get_drvdata(pdev); -} - resource_size_t rust_helper_pci_resource_len(struct pci_dev *pdev, int bar) { return pci_resource_len(pdev, bar); diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index 8435f8132e38..064e74a90904 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -10,11 +10,11 @@ device_id::RawDeviceId, devres::Devres, driver, - error::{to_result, Result}, + error::{from_result, to_result, Result}, io::Io, io::IoRaw, str::CStr, - types::{ARef, ForeignOwnable, Opaque}, + types::{ARef, Opaque}, ThisModule, }; use core::{ @@ -66,35 +66,32 @@ extern "C" fn probe_callback( // `struct pci_dev`. // // INVARIANT: `pdev` is valid for the duration of `probe_callback(= )`. - let pdev =3D unsafe { &*pdev.cast::>() }; + let pdev =3D unsafe { &*pdev.cast::>() }; =20 // SAFETY: `DeviceId` is a `#[repr(transparent)` wrapper of `struc= t pci_device_id` and // does not add additional invariants, so it's safe to transmute. let id =3D unsafe { &*id.cast::() }; let info =3D T::ID_TABLE.info(id.index()); =20 - match T::probe(pdev, info) { - Ok(data) =3D> { - // Let the `struct pci_dev` own a reference of the driver'= s private data. - // SAFETY: By the type invariant `pdev.as_raw` returns a v= alid pointer to a - // `struct pci_dev`. - unsafe { bindings::pci_set_drvdata(pdev.as_raw(), data.int= o_foreign() as _) }; - } - Err(err) =3D> return Error::to_errno(err), - } + from_result(|| { + let data =3D T::probe(pdev, info)?; =20 - 0 + pdev.as_ref().set_drvdata(data); + Ok(0) + }) } =20 extern "C" fn remove_callback(pdev: *mut bindings::pci_dev) { // SAFETY: The PCI bus only ever calls the remove callback with a = valid pointer to a // `struct pci_dev`. - let ptr =3D unsafe { bindings::pci_get_drvdata(pdev) }.cast(); + // + // INVARIANT: `pdev` is valid for the duration of `remove_callback= ()`. + let pdev =3D unsafe { &*pdev.cast::>() }; =20 // SAFETY: `remove_callback` is only ever called after a successfu= l call to - // `probe_callback`, hence it's guaranteed that `ptr` points to a = valid and initialized - // `KBox` pointer created through `KBox::into_foreign`. - let _ =3D unsafe { KBox::::from_foreign(ptr) }; + // `probe_callback`, hence it's guaranteed that `Device::set_drvda= ta()` has been called + // and stored a `Pin>`. + let _ =3D unsafe { pdev.as_ref().drvdata_obtain::>>() = }; } } =20 --=20 2.49.0 From nobody Thu Oct 9 01:11:38 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 951E6270EAA; Sat, 21 Jun 2025 19:51:49 +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=1750535509; cv=none; b=Fh5uARy8Iv//2U+qJUADVeQnpIDIQyrHF4/b4Arf8FZvsl4hYi/ULCu7PnxeGP4/dDnjJ63hTlLStbT7p05JGHzRdexTe/5u97+4C3zEZ599oUHksnl83293bqw4zp/TcQ+iyTMxC+Pjkj4ksS3/30IMdcfGWdB2942xb8uTCLc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750535509; c=relaxed/simple; bh=mM3PxOtUthJ3GpHeqpRPkpyqXw2y9hSUkmakMzIVsuk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Qm85ZbvcKId8AA7bgtkm35luBwyqO8zH5YWeSxWgERZcEm4f3OdcPeBoQTULk1XQr0oWdS2FrKbforX7d3G/C0tv3mMrxV4x729md0BHnc4yR8hZ7GQiKYI43ms7IpeY9pca6hdymZjlrwFLjahEZk/1vASiD7SsucWWEMTRbU0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=I8Gce29k; 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="I8Gce29k" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 984C3C4CEEF; Sat, 21 Jun 2025 19:51:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1750535509; bh=mM3PxOtUthJ3GpHeqpRPkpyqXw2y9hSUkmakMzIVsuk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=I8Gce29k2Xe7ufysDGxaGpD+is6WimD/iLkCKTOTxiMyt4ctsXcx4KdhchiUDebi+ UhsS/QHqveQqmWR6qMwRO7bOsLtA0BOyUZ/fGi8sa+g9egext0skh2LtYrwCl23eWE tDX9HcmpSutp4mwCclbVQoLoN8nRlKqA1EsNKnSdy+uea+yh5Zwg04NzcLpLwCLvqs +R2GY/ClxnMkKCXcax4oB5uq6nUo9SQqRrTxw5vboN4dGyIACur1LXtsv+zboGX9+B YXznmm5CpsAQlEiPpT/FJIFnWy/t0YIejZa0CSRa3gpd58bUYCdgnYz2yJgvxufXd7 b9DVIOBnuEGWg== From: Danilo Krummrich To: gregkh@linuxfoundation.org, rafael@kernel.org, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, david.m.ertman@intel.com, ira.weiny@intel.com, leon@kernel.org, kwilczynski@kernel.org, bhelgaas@google.com Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Danilo Krummrich Subject: [PATCH 5/8] rust: auxiliary: use generic device drvdata accessors Date: Sat, 21 Jun 2025 21:43:31 +0200 Message-ID: <20250621195118.124245-6-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250621195118.124245-1-dakr@kernel.org> References: <20250621195118.124245-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Take advantage of the generic drvdata accessors of the generic Device type. While at it, use from_result() instead of match. Signed-off-by: Danilo Krummrich --- rust/helpers/auxiliary.c | 10 ---------- rust/kernel/auxiliary.rs | 35 +++++++++++++++-------------------- 2 files changed, 15 insertions(+), 30 deletions(-) diff --git a/rust/helpers/auxiliary.c b/rust/helpers/auxiliary.c index 0db3860d774e..8b5e0fea4493 100644 --- a/rust/helpers/auxiliary.c +++ b/rust/helpers/auxiliary.c @@ -2,16 +2,6 @@ =20 #include =20 -void rust_helper_auxiliary_set_drvdata(struct auxiliary_device *adev, void= *data) -{ - auxiliary_set_drvdata(adev, data); -} - -void *rust_helper_auxiliary_get_drvdata(struct auxiliary_device *adev) -{ - return auxiliary_get_drvdata(adev); -} - void rust_helper_auxiliary_device_uninit(struct auxiliary_device *adev) { return auxiliary_device_uninit(adev); diff --git a/rust/kernel/auxiliary.rs b/rust/kernel/auxiliary.rs index d2cfe1eeefb6..250d3178c334 100644 --- a/rust/kernel/auxiliary.rs +++ b/rust/kernel/auxiliary.rs @@ -8,10 +8,10 @@ bindings, container_of, device, device_id::RawDeviceId, driver, - error::{to_result, Result}, + error::{from_result, to_result, Result}, prelude::*, str::CStr, - types::{ForeignOwnable, Opaque}, + types::Opaque, ThisModule, }; use core::{ @@ -61,37 +61,32 @@ extern "C" fn probe_callback( // `struct auxiliary_device`. // // INVARIANT: `adev` is valid for the duration of `probe_callback(= )`. - let adev =3D unsafe { &*adev.cast::>() }; + let adev =3D unsafe { &*adev.cast::>() }; =20 // SAFETY: `DeviceId` is a `#[repr(transparent)`] wrapper of `stru= ct auxiliary_device_id` // and does not add additional invariants, so it's safe to transmu= te. let id =3D unsafe { &*id.cast::() }; let info =3D T::ID_TABLE.info(id.index()); =20 - match T::probe(adev, info) { - Ok(data) =3D> { - // Let the `struct auxiliary_device` own a reference of th= e driver's private data. - // SAFETY: By the type invariant `adev.as_raw` returns a v= alid pointer to a - // `struct auxiliary_device`. - unsafe { - bindings::auxiliary_set_drvdata(adev.as_raw(), data.in= to_foreign().cast()) - }; - } - Err(err) =3D> return Error::to_errno(err), - } + from_result(|| { + let data =3D T::probe(adev, info)?; =20 - 0 + adev.as_ref().set_drvdata(data); + Ok(0) + }) } =20 extern "C" fn remove_callback(adev: *mut bindings::auxiliary_device) { - // SAFETY: The auxiliary bus only ever calls the remove callback w= ith a valid pointer to a + // SAFETY: The auxiliary bus only ever calls the probe callback wi= th a valid pointer to a // `struct auxiliary_device`. - let ptr =3D unsafe { bindings::auxiliary_get_drvdata(adev) }; + // + // INVARIANT: `adev` is valid for the duration of `probe_callback(= )`. + let adev =3D unsafe { &*adev.cast::>() }; =20 // SAFETY: `remove_callback` is only ever called after a successfu= l call to - // `probe_callback`, hence it's guaranteed that `ptr` points to a = valid and initialized - // `KBox` pointer created through `KBox::into_foreign`. - drop(unsafe { KBox::::from_foreign(ptr.cast()) }); + // `probe_callback`, hence it's guaranteed that `Device::set_drvda= ta()` has been called + // and stored a `Pin>`. + drop(unsafe { adev.as_ref().drvdata_obtain::>>() }); } } =20 --=20 2.49.0 From nobody Thu Oct 9 01:11:38 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 3ADB7270ED7; Sat, 21 Jun 2025 19:51:53 +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=1750535514; cv=none; b=TB/v5GvF7p7wTU1BMkmekZVjIn4gfW2RE9zpAD5VUMvTEZXkWAEYqxZCNTzfBC4BWtxuCIdtjc45RV6kNOcDp892ay8FRvQ0omdUcmOdgiVnCVd1R5w925yz4AAFqm5yBSObXwYfEKKrPApXWpm41Pr+Eb0OAFh1/QHnFcjVi74= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750535514; c=relaxed/simple; bh=7QAcOvOYh5asCbN/n+PaQTUogUpYg0tZ05+enhoXBgA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=b/dCmSuEfIAHUPlV4/ccznmGWUIiXr/BOe/HY8G0fk8e/bg16R3A/Bz+R6ms7E+dE1IMbGWKzP1/hE3MJNiZXXWPhn3IhjEc1Meq55qCystQZAKLLEPSDc6gfeDsUMiAvGrXjm7t6ERMUBpcqMxTq+NJw4kDlVr5uxOdNr9wnNM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gtEBEW6d; 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="gtEBEW6d" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E3284C4CEE7; Sat, 21 Jun 2025 19:51:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1750535513; bh=7QAcOvOYh5asCbN/n+PaQTUogUpYg0tZ05+enhoXBgA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gtEBEW6dXTKMhVbZ+EIsCX7ZIs8DLZHY5nq8jzo2BAE18hd6c/Xlx4wr82vMDaywp zX7hhNi/qL1jW95tNcEPmNpHB9Anrk624xT3ycPZoWAPUfjrkgB7bPRm4RAcqLvgml iqEFLOo9JEFSoYFREmYU/rt98GJlJAJe4cfB4Ben0rCSDmhK7XVdtqD0KLH9+3kkE2 AarnwwUYA0aNZg20PNsdQvhQLicwDqh0XmCKXyL6y/errLeEY23MwRBFRSoI5m+deL LUgEMjISLrper9B4+irYBui1DZrry1quSWBZ2b1fiB6GytyWtOXAK3mOsQlQWUFHxJ y42eipGMJrgfA== From: Danilo Krummrich To: gregkh@linuxfoundation.org, rafael@kernel.org, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, david.m.ertman@intel.com, ira.weiny@intel.com, leon@kernel.org, kwilczynski@kernel.org, bhelgaas@google.com Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Danilo Krummrich Subject: [PATCH 6/8] rust: platform: implement Driver::unbind() Date: Sat, 21 Jun 2025 21:43:32 +0200 Message-ID: <20250621195118.124245-7-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250621195118.124245-1-dakr@kernel.org> References: <20250621195118.124245-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently, there's really only one core callback for drivers, which is probe(). Now, this isn't entirely true, since there is also the drop() callback of the driver type (serving as the driver's private data), which is returned by probe() and is dropped in remove(). On the C side remove() mainly serves two purposes: (1) Tear down the device that is operated by the driver, e.g. call bus specific functions, write I/O memory to reset the device, etc. (2) Free the resources that have been allocated by a driver for a specific device. The drop() callback mentioned above is intended to cover (2) as the Rust idiomatic way. However, it is partially insufficient and inefficient to cover (1) properly, since drop() can't be called with additional arguments, such as the reference to the corresponding device that has the correct device context, i.e. the Core device context. This makes it inefficient (but not impossible) to access device resources, e.g. to write device registers, and impossible to call device methods, which are only accessible under the Core device context. In order to solve this, add an additional callback for (1), which we call unbind(). The reason for calling it unbind() is that, unlike remove(), it is *only* meant to be used to perform teardown operations on the device (1), but *not* to release resources (2). Signed-off-by: Danilo Krummrich --- rust/kernel/platform.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs index dc0c36d70963..9b3de89dc2f9 100644 --- a/rust/kernel/platform.rs +++ b/rust/kernel/platform.rs @@ -82,7 +82,9 @@ extern "C" fn remove_callback(pdev: *mut bindings::platfo= rm_device) { // SAFETY: `remove_callback` is only ever called after a successfu= l call to // `probe_callback`, hence it's guaranteed that `Device::set_drvda= ta()` has been called // and stored a `Pin>`. - let _ =3D unsafe { pdev.as_ref().drvdata_obtain::>>() = }; + let data =3D unsafe { pdev.as_ref().drvdata_obtain::>>= () }; + + T::unbind(pdev, data.as_ref()); } } =20 @@ -164,6 +166,20 @@ pub trait Driver: Send { /// Implementers should attempt to initialize the device here. fn probe(dev: &Device, id_info: Option<&Self::IdInfo>) -> Result>>; + + /// Platform driver unbind. + /// + /// Called when a [`Device`] is unbound from its bound [`Driver`]. Imp= lementing this callback + /// is optional. + /// + /// This callback serves as a place for drivers to perform teardown op= erations that require a + /// `&Device` or `&Device` reference. For instance, drive= rs may try to perform I/O + /// operations to gracefully tear down the device. + /// + /// Otherwise, release operations for driver resources should be perfo= rmed in `Self::drop`. + fn unbind(dev: &Device, this: Pin<&Self>) { + let _ =3D (dev, this); + } } =20 /// The platform device representation. --=20 2.49.0 From nobody Thu Oct 9 01:11:38 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 8C8F1271448; Sat, 21 Jun 2025 19:51:58 +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=1750535518; cv=none; b=TA3Yjm++u4aNvJPbBKf4c5ff0laCi3yF11sSYvYAZYCQgqDegDuvyC2kJma3BqX8EJ5JhkFhLNo/CVeonGbasJUwxBMsKFHWl/Rj4X6DmHyW13CH02IPbay3FZS+cfM5lbJAwgoYVNKGlnsCMdQaoNAJH8hd2sjD81AvXoyasPs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750535518; c=relaxed/simple; bh=2JkK5rZrOgiP/B+Tmh5/VUxDYFEElGEgx5i/EiKwWQg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jqQ0+Yk6hA6UH8e+PPsLCUEJKkSMl+zUHWt/TgLbu1Jq78PySq4MZg1cetHBKAJ27zqSnvHGtMSwX4PqPbjP1LZrsMKFNjBqcybIb0qqV+59kCmaj1ov6FTtVJa3IGel68CxzvmAw7FS5EXTtz3+0PgYIEAzzKJiqjD7oevNd6U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=YeB4POrs; 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="YeB4POrs" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 38BADC4CEEF; Sat, 21 Jun 2025 19:51:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1750535518; bh=2JkK5rZrOgiP/B+Tmh5/VUxDYFEElGEgx5i/EiKwWQg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YeB4POrsRlpzYE4d6RgxCMyvx9S+IlY9SQFzm4rSItiFg4IClwpdySqmKIdh+rwhG rlyl/b226EkNfPBG0wJ1R+Go6/Z2boxyg1xoCv3pZmxkBOMbH+Yqw8Q+m4wrDV6VfF ta+7TvJA9ezUEMeh7C8RmhWKvBzu/lsZ8T5zbkSAINse5IXS/TCXNMOPDbg3yfre/y Sj7011Ji8uCJnD2urE+psEEBE3Svz/nzTjWjh9pcMN5w7FC8JyMkRj5MhCdnH9NhxQ ZwUQTSphV4A0yhZyC0fB41BpVs9PPUc8lSeAFcdrUCHLq9QCjscvLF1kpT/nfB1QOd rAM5Y9jiOVOgg== From: Danilo Krummrich To: gregkh@linuxfoundation.org, rafael@kernel.org, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, david.m.ertman@intel.com, ira.weiny@intel.com, leon@kernel.org, kwilczynski@kernel.org, bhelgaas@google.com Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Danilo Krummrich Subject: [PATCH 7/8] rust: pci: implement Driver::unbind() Date: Sat, 21 Jun 2025 21:43:33 +0200 Message-ID: <20250621195118.124245-8-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250621195118.124245-1-dakr@kernel.org> References: <20250621195118.124245-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently, there's really only one core callback for drivers, which is probe(). Now, this isn't entirely true, since there is also the drop() callback of the driver type (serving as the driver's private data), which is returned by probe() and is dropped in remove(). On the C side remove() mainly serves two purposes: (1) Tear down the device that is operated by the driver, e.g. call bus specific functions, write I/O memory to reset the device, etc. (2) Free the resources that have been allocated by a driver for a specific device. The drop() callback mentioned above is intended to cover (2) as the Rust idiomatic way. However, it is partially insufficient and inefficient to cover (1) properly, since drop() can't be called with additional arguments, such as the reference to the corresponding device that has the correct device context, i.e. the Core device context. This makes it inefficient (but not impossible) to access device resources, e.g. to write device registers, and impossible to call device methods, which are only accessible under the Core device context. In order to solve this, add an additional callback for (1), which we call unbind(). The reason for calling it unbind() is that, unlike remove(), it is *only* meant to be used to perform teardown operations on the device (1), but *not* to release resources (2). Signed-off-by: Danilo Krummrich --- rust/kernel/pci.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index 064e74a90904..6bdd3ab23f17 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -91,7 +91,9 @@ extern "C" fn remove_callback(pdev: *mut bindings::pci_de= v) { // SAFETY: `remove_callback` is only ever called after a successfu= l call to // `probe_callback`, hence it's guaranteed that `Device::set_drvda= ta()` has been called // and stored a `Pin>`. - let _ =3D unsafe { pdev.as_ref().drvdata_obtain::>>() = }; + let data =3D unsafe { pdev.as_ref().drvdata_obtain::>>= () }; + + T::unbind(pdev, data.as_ref()); } } =20 @@ -238,6 +240,20 @@ pub trait Driver: Send { /// Called when a new platform device is added or discovered. /// Implementers should attempt to initialize the device here. fn probe(dev: &Device, id_info: &Self::IdInfo) -> Result= >>; + + /// Platform driver unbind. + /// + /// Called when a [`Device`] is unbound from its bound [`Driver`]. Imp= lementing this callback + /// is optional. + /// + /// This callback serves as a place for drivers to perform teardown op= erations that require a + /// `&Device` or `&Device` reference. For instance, drive= rs may try to perform I/O + /// operations to gracefully tear down the device. + /// + /// Otherwise, release operations for driver resources should be perfo= rmed in `Self::drop`. + fn unbind(dev: &Device, this: Pin<&Self>) { + let _ =3D (dev, this); + } } =20 /// The PCI device representation. --=20 2.49.0 From nobody Thu Oct 9 01:11:38 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 B37A4271456; Sat, 21 Jun 2025 19:52:02 +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=1750535522; cv=none; b=qCo9nT9oayIhc01GCbvHKuDo1ecOLrMz2BmxHiDZaEclmKq/klfTiK227iY1pZYaP+3txLsIqNZdpnLE9VDfCeQCXOej5MAnpv+GR7SAnrmG9IIFFU2Rh0Ao9JCrN5aXpYh0W8U0uwNbmcq9fSD3yaTTwbcDapn5mNqvivkrkpo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750535522; c=relaxed/simple; bh=LVDt14mBWg0lELVOCK+Hfq1+W2xSKm8S17ORApwzmbE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ky36cVnmpry2As+G4BvBJs2tHSG2OBl0DWez+nNOdSD1eGkYhc2XU8ZAmmbU9JHKNCGK/fbQUX8sZfB9VlIK9Bj1sNb9nCuwowsmRFjhsxaPSZP9mPyl13pEZsqMMuTlw+z3qTmknLsOpYY5ixhw72GrlvLxM5bUs9yQxYalvDw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=SEE+9IDW; 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="SEE+9IDW" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8240FC4CEF0; Sat, 21 Jun 2025 19:51:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1750535522; bh=LVDt14mBWg0lELVOCK+Hfq1+W2xSKm8S17ORApwzmbE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SEE+9IDWJ72u5ssBPS95FTKmADMfXy0b452Dg/nJqpTdPRg/21rvdMVni0ofQJb1q RptHe1Wa2D6vKl8ibFPgTC0YILGokbANMno5oiRmTcjPqWuMe9mXnIZBfiB8Xhlim7 /ijPOUCS+gUYGuxM3rioSfnzGNO6gJF/Q2ppl8YhS9zBnbISw/E2y2hPkrUEz7ly3E w9vG4X36rgeTb/04aZrwnjR2QPOp5iEBaA9Udz2eSRujxxr+vlxFruffJh8mYbWo/e 7QzWhrEcgGlCz3CEYzMGPQ3YNyuMuwX20rofpLmRwJrBLEeY4NDburiMx1funvqq6N x6iftko7HcjhA== From: Danilo Krummrich To: gregkh@linuxfoundation.org, rafael@kernel.org, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, david.m.ertman@intel.com, ira.weiny@intel.com, leon@kernel.org, kwilczynski@kernel.org, bhelgaas@google.com Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Danilo Krummrich Subject: [PATCH 8/8] samples: rust: pci: reset pci-testdev in unbind() Date: Sat, 21 Jun 2025 21:43:34 +0200 Message-ID: <20250621195118.124245-9-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250621195118.124245-1-dakr@kernel.org> References: <20250621195118.124245-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Reset the pci-testdev when the driver is unbound from its device. Signed-off-by: Danilo Krummrich --- samples/rust/rust_driver_pci.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/samples/rust/rust_driver_pci.rs b/samples/rust/rust_driver_pci= .rs index 15147e4401b2..062a242f8874 100644 --- a/samples/rust/rust_driver_pci.rs +++ b/samples/rust/rust_driver_pci.rs @@ -18,7 +18,7 @@ impl Regs { =20 type Bar0 =3D pci::Bar<{ Regs::END }>; =20 -#[derive(Debug)] +#[derive(Copy, Clone, Debug)] struct TestIndex(u8); =20 impl TestIndex { @@ -28,6 +28,7 @@ impl TestIndex { struct SampleDriver { pdev: ARef, bar: Devres, + index: TestIndex, } =20 kernel::pci_device_table!( @@ -79,6 +80,7 @@ fn probe(pdev: &pci::Device, info: &Self::IdInfo) -= > Result Self { pdev: pdev.into(), bar, + index: *info, }, GFP_KERNEL, )?; @@ -92,6 +94,13 @@ fn probe(pdev: &pci::Device, info: &Self::IdInfo) = -> Result =20 Ok(drvdata.into()) } + + fn unbind(pdev: &pci::Device, this: Pin<&Self>) { + if let Ok(bar) =3D this.bar.access(pdev.as_ref()) { + // Reset pci-testdev by writing a new test index. + bar.write8(this.index.0, Regs::TEST); + } + } } =20 impl Drop for SampleDriver { --=20 2.49.0