From nobody Wed Dec 17 10:10:44 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 D10A71E0DD1; Fri, 21 Mar 2025 14:59:16 +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=1742569156; cv=none; b=HP3cIVjEylUquJ1UA0CPsdfKe+OcxaIMn4IOZ9kDeZYpFvq3DCxHH2b9jyhJ7yCeL93H4tvDsjNFFB2U5UJ/8RQ3OulB6Vd2gtgDmoPw7tBKJyD/+WWLMh2vRpiuByFZjMUUZKx6tMgoTLzsu7CVtj8cMeHsvZZGUzuH87jtqvo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742569156; c=relaxed/simple; bh=MKwm/fHil0GDCiiaBpnQB8YlsfUYdX6rYlCKMcY4+DY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FNNX2Jway4EIRSbr9cFOsY13JTIrsNnsqkUvJDc95SFwy+KJ9iawFDDkpaoyOBtvZzrK4RX6RnA0CjcndmFB8IxuDRA4PqVhZNq0IxCPOFnndNaUCNy3bflYfYY6BhZ3SJ58HDNxhU6wiK5+k2K+rA3QuSdYRrXvqGq9NDG28IY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=eGYxR+2G; 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="eGYxR+2G" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1C64AC4CEEC; Fri, 21 Mar 2025 14:59:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1742569156; bh=MKwm/fHil0GDCiiaBpnQB8YlsfUYdX6rYlCKMcY4+DY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eGYxR+2GO3Lf2zTwHPHZP2hb+9yCggNXQghPGTGWHb/0YFawnsGhwpfT9NTKKmaa4 XNjQ6f2j6xs2eTjc0i+2My5QobvhXAfrtMxMvZ2qCMPxskQIYOJaBlUcK3aXFo31F2 CUqlMabLKWHaYgitBAu+1g/PcK4PgRI6BKgy08zX1YmV4dBF2pBqWtoV3EuGTZVgC5 xEJzxv/dmQCpcasUP+CV9lR6XF4UPSKFw2JOigYc+ANPKpy+BN1YNXSxHE5L7yD9de OsoQJLVf5thfBq9wouiwgbzqLvk5Sc5lG51R7itCWRyLuX1ZikzRYJHiOgu0Vns53f +1rJ5/wUZPC7Q== From: Danilo Krummrich To: bhelgaas@google.com, 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 Cc: linux-pci@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH v3 1/3] rust: device: implement bus_type_raw() Date: Fri, 21 Mar 2025 15:57:56 +0100 Message-ID: <20250321145906.3163-2-dakr@kernel.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250321145906.3163-1-dakr@kernel.org> References: <20250321145906.3163-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 bus_type_raw(), which returns a raw pointer to the device' struct bus_type. This is useful for bus devices, to implement the following trait. impl TryFrom<&Device> for &pci::Device With this a caller can try to get the bus specific device from a generic device in a safe way. try_from() will only succeed if the generic device' bus type pointer matches the pointer of the bus' type. Reviewed-by: Alice Ryhl Reviewed-by: Benno Lossin Signed-off-by: Danilo Krummrich --- rust/kernel/device.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index 21b343a1dc4d..67a2fc46cf4c 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -65,6 +65,16 @@ pub(crate) fn as_raw(&self) -> *mut bindings::device { self.0.get() } =20 + /// Returns a raw pointer to the device' bus type. + #[expect(unused)] + pub(crate) fn bus_type_raw(&self) -> *const bindings::bus_type { + // SAFETY: + // - By the type invariants, `self.as_raw()` is a valid pointer to= a `struct device`. + // - `dev->bus` is a pointer to a `const struct bus_type`, which i= s only ever set at device + // creation. + unsafe { (*self.as_raw()).bus } + } + /// Convert a raw C `struct device` pointer to a `&'a Device`. /// /// # Safety --=20 2.48.1 From nobody Wed Dec 17 10:10:44 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 97FCF1D7E26; Fri, 21 Mar 2025 14:59:20 +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=1742569160; cv=none; b=kFuL0wPjX8iZ0RTXcT0NT6WuAOTWZjhtFU8Alxil1M8cOCxtYEb9JPRQVGMXtDyMHsxBgkyQGCbpBPqgxN6LcTYN0zMZmehh/Wk4oigPyV6ff9k+gkwfs8XkUWcRT8j7BdFAAQixNQ2Tp1cWKZUdQLqBgxrBaBImQQJF3c+Z2mM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742569160; c=relaxed/simple; bh=iqf+tTdt2jGaWlTQ8KsyRMl+ITOHzZS2Gb8CPaDPkn0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RzYx+wY/ehSjyJ403xFtnU/379VjVWJAszx9PwYFi3OZHrMu+u/8yk10Kl4UqvO0/AWCXdzZihZ3kx9sxAUvmnHSin4Q8TmK9FIdUH9b01ZpAxqv9A0gzKwg0o6pJKb+AF3a5ymd6AgwDqZoUR45UbPTg+ISCUrFkpo0xfgYJHY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Ru6hFph/; 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="Ru6hFph/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C9A3AC4CEE9; Fri, 21 Mar 2025 14:59:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1742569160; bh=iqf+tTdt2jGaWlTQ8KsyRMl+ITOHzZS2Gb8CPaDPkn0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ru6hFph/FKAqYte1+tuQpXsDEh3t7xuLslBTTqmN0N0LTAPHsJ+euP2vne/O8Up03 CGKsTOfp1aY1AoHyyVH6sXKWw45c36353613tXgbjXkyo0ky/vogtA6Yvl6IIeDBXd DeAPj/c6u0Mq4SNME84YSTm8acr6F9AzU4mp9uALgq7rMwWKk9X5JzmWgNmjduLcKt 1J69SenBI/8GehEMnjIqom+VMzbU/SKc+bx6q3w78EOqP8M8VmXWSIqpTkvziWP3ll BxD4eP8S+KdvD6oGR6S5oGQ+1bXume9RppnAXx5ss3J237JwyuVg3YWRZP4aPyVQOc MhXEVo9IqXUFQ== From: Danilo Krummrich To: bhelgaas@google.com, 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 Cc: linux-pci@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH v3 2/3] rust: pci: impl TryFrom<&Device> for &pci::Device Date: Fri, 21 Mar 2025 15:57:57 +0100 Message-ID: <20250321145906.3163-3-dakr@kernel.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250321145906.3163-1-dakr@kernel.org> References: <20250321145906.3163-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 TryFrom<&device::Device> for &Device. This allows us to get a &pci::Device from a generic &Device in a safe way; the conversion fails if the device' bus type does not match with the PCI bus type. Reviewed-by: Alice Ryhl Reviewed-by: Benno Lossin Signed-off-by: Danilo Krummrich --- rust/kernel/device.rs | 1 - rust/kernel/pci.rs | 22 ++++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index 67a2fc46cf4c..0bbc67edd38d 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -66,7 +66,6 @@ pub(crate) fn as_raw(&self) -> *mut bindings::device { } =20 /// Returns a raw pointer to the device' bus type. - #[expect(unused)] pub(crate) fn bus_type_raw(&self) -> *const bindings::bus_type { // SAFETY: // - By the type invariants, `self.as_raw()` is a valid pointer to= a `struct device`. diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index 22a32172b108..c563129d29b6 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -6,7 +6,7 @@ =20 use crate::{ alloc::flags::*, - bindings, device, + bindings, container_of, device, device_id::RawDeviceId, devres::Devres, driver, @@ -20,7 +20,7 @@ use core::{ marker::PhantomData, ops::Deref, - ptr::{addr_of_mut, NonNull}, + ptr::{addr_of, addr_of_mut, NonNull}, }; use kernel::prelude::*; =20 @@ -466,6 +466,24 @@ fn as_ref(&self) -> &device::Device { } } =20 +impl TryFrom<&device::Device> for &Device { + type Error =3D kernel::error::Error; + + fn try_from(dev: &device::Device) -> Result { + if dev.bus_type_raw() !=3D addr_of!(bindings::pci_bus_type) { + return Err(EINVAL); + } + + // SAFETY: We've just verified that the bus type of `dev` equals `= bindings::pci_bus_type`, + // hence `dev` must be embedded in a valid `struct pci_dev` as gua= ranteed by the + // corresponding C code. + let pdev =3D unsafe { container_of!(dev.as_raw(), bindings::pci_de= v, dev) }; + + // SAFETY: `pdev` is a valid pointer to a `struct pci_dev`. + Ok(unsafe { &*pdev.cast() }) + } +} + // SAFETY: A `Device` is always reference-counted and can be released from= any thread. unsafe impl Send for Device {} =20 --=20 2.48.1 From nobody Wed Dec 17 10:10:44 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 E815F1D7E26; Fri, 21 Mar 2025 14:59:23 +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=1742569164; cv=none; b=PnScb89NWQ/CRlEnLU96/4azOSsPHjOLgKo3b7X9OAhap4a5mrbkmtFiSIQPIb2OFMb7UynXSmTelwP/4sG7/WulVhu3DvxDV++uWAnIt1y+/QeRFhleqDlQujoS/ajehePheRCs+lN2z4uHegrgtOY/DP4BzjrVgrVMIFJaUL4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742569164; c=relaxed/simple; bh=XK3ss92XB9PpaMbmmBVABAPs+6dJW9sFNmtLOGmVRwA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=G8R8ECgxZejJF3xvXrHuMxfEVvv7lxNbUyrp0YQ0aPwTorkyLPsgMUZLGVrglL1z5xogoKHTIDquVcSa0UUUEAYGADTAkiIzuBpOnTs5dQrvYA8hml7HAQQYjK42bw6qwV+z6+8IJ6QBza7bU5yFZHGk31Ed3I9Qg5BJ+B4D7Us= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Umk6rLD+; 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="Umk6rLD+" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 80B28C4CEEC; Fri, 21 Mar 2025 14:59:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1742569163; bh=XK3ss92XB9PpaMbmmBVABAPs+6dJW9sFNmtLOGmVRwA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Umk6rLD+QQhysD2ySV+lZiJVTixCw6GXeItNx/BQVMzrYqlS6zGQlYsu/8Dp2d7L3 LgQeMj1DpTM0F1aW54X+lI3tBFcq9eZ8kX/F4yFiqm/Woc2H+pb9CfxDZVah0+uwiv eZN8e7aZoUw+DuLDGrfqOybrt6PwWzTXfWx9oXnZLarnapKtIyK8+wRt1mexhr2/3O /8JsqroJ5ime7A3ZmpkHcW9JOK6i3BCdlkERm9wJmY1AgezTbnT9pUQhsPtk80jlLg QICtwkbfoJZsitlOYXWhBFISHbmjvcLFpar/vG4sJxHw7rEzULNRajXhJCwqPRWuBA UmCbncsRiTtnw== From: Danilo Krummrich To: bhelgaas@google.com, 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 Cc: linux-pci@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH v3 3/3] rust: platform: impl TryFrom<&Device> for &platform::Device Date: Fri, 21 Mar 2025 15:57:58 +0100 Message-ID: <20250321145906.3163-4-dakr@kernel.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250321145906.3163-1-dakr@kernel.org> References: <20250321145906.3163-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 TryFrom<&device::Device> for &Device. This allows us to get a &platform::Device from a generic &Device in a safe way; the conversion fails if the device' bus type does not match with the platform bus type. Reviewed-by: Alice Ryhl Reviewed-by: Benno Lossin Signed-off-by: Danilo Krummrich --- rust/kernel/platform.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs index e37531bae8e9..d7796967cffa 100644 --- a/rust/kernel/platform.rs +++ b/rust/kernel/platform.rs @@ -5,7 +5,7 @@ //! C header: [`include/linux/platform_device.h`](srctree/include/linux/pl= atform_device.h) =20 use crate::{ - bindings, device, driver, + bindings, container_of, device, driver, error::{to_result, Result}, of, prelude::*, @@ -17,7 +17,7 @@ use core::{ marker::PhantomData, ops::Deref, - ptr::{addr_of_mut, NonNull}, + ptr::{addr_of, addr_of_mut, NonNull}, }; =20 /// An adapter for the registration of platform drivers. @@ -234,6 +234,24 @@ fn as_ref(&self) -> &device::Device { } } =20 +impl TryFrom<&device::Device> for &Device { + type Error =3D kernel::error::Error; + + fn try_from(dev: &device::Device) -> Result { + if dev.bus_type_raw() !=3D addr_of!(bindings::platform_bus_type) { + return Err(EINVAL); + } + + // SAFETY: We've just verified that the bus type of `dev` equals + // `bindings::platform_bus_type`, hence `dev` must be embedded in = a valid + // `struct platform_device` as guaranteed by the corresponding C c= ode. + let pdev =3D unsafe { container_of!(dev.as_raw(), bindings::platfo= rm_device, dev) }; + + // SAFETY: `pdev` is a valid pointer to a `struct platform_device`. + Ok(unsafe { &*pdev.cast() }) + } +} + // SAFETY: A `Device` is always reference-counted and can be released from= any thread. unsafe impl Send for Device {} =20 --=20 2.48.1