From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mout02.posteo.de (mout02.posteo.de [185.67.36.66]) (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 5C30831D75B for ; Mon, 27 Oct 2025 20:06:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.67.36.66 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761595575; cv=none; b=eV0le3PwOcfK1UwBP3OEz5NgRr2Wgdd9jvqQcjvsGcGWr/7Q7ZsQt/eS3CSbP7AovXiDF1efe5ASj5jGlMiJVY6f3DzMlQcm+07NfGb1VQ/PsqpC7o/tAEscdhIoKD2DKSW0zzZh5jFMIzS9z/Z5wrwIdIsyfDoVZsJKDRZaUa8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761595575; c=relaxed/simple; bh=Garq7VO1PdQbR41KtGI+fTjLqRCsro5auWy0YdckOkA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LXaB1ObQkLRDKDyhZCG2y4iZULWiBD9aQQBIn6//HqHOz3QI4g90/68zDdwY0ogu64aD3TXHWqmL0vLTWmVOrv9ihW17yHnI4zBxAoOgqMNTF1j2+db6a9ZI0AHO+cL1d4IfN8PysmdElayc+mCTJV1iEqzW8tLU3pU8zgPA+W8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=posteo.de; spf=pass smtp.mailfrom=posteo.de; dkim=pass (2048-bit key) header.d=posteo.de header.i=@posteo.de header.b=rw3RPwDt; arc=none smtp.client-ip=185.67.36.66 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=posteo.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=posteo.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=posteo.de header.i=@posteo.de header.b="rw3RPwDt" Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id BE1DF240103 for ; Mon, 27 Oct 2025 21:06:04 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=posteo.de; s=2017; t=1761595564; bh=qm2cnUKzVLRN3dgzzMumdrxbcUv5Pqw48DSmrrD/IkA=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version: Content-Transfer-Encoding:Autocrypt:OpenPGP:From; b=rw3RPwDt/YxYgLLxXZ2ONVrAStx5KO+CBvD9+W0to1J8a+hxm23dYa922qw0SGodV lyKMxv3UIPmQ5PArWvOyh9v1saLrH5a+czSZD9bCVtoN5PzOsNcSJwhVGGsaVFKoZ+ mCgHLCWhOYE5op5WTENznHLGWaNt3ouVz/95ocibAtiLXuOo67VNNgT3mPPUstwVUd bgyKv3yW9ukRc5GZWFOvigM8YJQTmJ735g6F3AteI1DVxb5hdOB3QJA3XIAE3oBHB2 JPBS0rJIUnUIitumVipuFsQBEn0MiGWdC8DmZNBJs63kpEAxdKjeezSpEJOjGDNWl+ YCaAPtCnzqnkw== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4cwPdj0nD4z9rxK; Mon, 27 Oct 2025 21:06:01 +0100 (CET) From: Markus Probst To: Greg Kroah-Hartman , Miguel Ojeda , Alex Gaynor , Danilo Krummrich , "Rafael J. Wysocki" , Lee Jones , Pavel Machek Cc: Dave Ertman , Ira Weiny , Leon Romanovsky , Boqun Feng , Gary Guo , bjorn3_gh@protonmail.com, Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Bjorn Helgaas , =?UTF-8?q?Krzysztof=20Wilczy=C5=84ski?= , Markus Probst , rust-for-linux@vger.kernel.org, linux-leds@vger.kernel.org, linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v7 1/2] rust: Add trait to convert a device reference to a bus device reference Date: Mon, 27 Oct 2025 20:06:03 +0000 Message-ID: <20251027200547.1038967-2-markus.probst@posteo.de> In-Reply-To: <20251027200547.1038967-1-markus.probst@posteo.de> References: <20251027200547.1038967-1-markus.probst@posteo.de> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=5573; i=markus.probst@posteo.de; h=from:subject; bh=Garq7VO1PdQbR41KtGI+fTjLqRCsro5auWy0YdckOkA=; b=owEBbQKS/ZANAwAIATR2H/jnrUPSAcsmYgBo/9B7gUvu7Zhh52kQWYQjztQ4KWXF/nHONfwKs xNpc2Z3Qi+JAjMEAAEIAB0WIQSCdBjE9KxY53IwxHM0dh/4561D0gUCaP/QewAKCRA0dh/4561D 0luND/4tPX7HQmtzU6jLks8vsqblaucYlVLZWnkZpTZbv7qGBd5//essk+V+y3d3M7gu/I/v5LU GXOyDzARcd5Bcl0zAx3uMZPvMf1mOsUQYxQxeMJmcJO8cvw1KObGuowwNHlLHI0U9ddEawkYuhW CRz3koY7rFnh1XdodHyEPR+ziHK925qLZNpVDWYLiMayGiX/PppZVXshbVGiBApvoe59y5Rd9R5 FFqqzwoXfHNpDtHbNQtePB7icdfxWHTu4jNvznMZ9LtNe5zNBymNVQQxdFdstcxeHEb4Rtnk8Mw cWdi1fa2J3SpWISg2/pFJJhhzAgsrIMCu4eobWB3JXkRf6moLJZcx3JZ9it6t/lH/Qx+vgVghUg KTUh4MEPPLwN1epxGsObGizRVZ/M5e+Cqcprp0VMskXV6nefuX5QRoCxIxP+uheGfxQmmMR9KDB zZjRrb7LE4IJidBRXjZo2F4rddebWnfpS816ELbKBst/cWNJysXAQW+DCmvi0yNBeej90kHHMwy Z8UucOJ4JrBUW3OY1gwavEpud1pnaJna3+k+xYfx+do934cO7xum/ggyUoq04YEKnjqunx2Wad9 SEJygnZU55re+Pps77Zw2ntiW5RbUko4HlsFkTwCSd9YYVMTmtCfErEaiq6Kw3Yy83E80d6KoCM Db2oMjDQ3WCDE0A= = X-Developer-Key: i=markus.probst@posteo.de; a=openpgp; fpr=827418C4F4AC58E77230C47334761FF8E7AD43D2 Content-Transfer-Encoding: 8bit Autocrypt: addr=markus.probst@posteo.de; prefer-encrypt=mutual; keydata=xsFNBGiDvXgBEADAXUceKafpl46S35UmDh2wRvvx+UfZbcTjeQOlSwKP7YVJ4JOZrVs93qReNLkO WguIqPBxR9blQ4nyYrqSCV+MMw/3ifyXIm6Pw2YRUDg+WTEOjTixRCoWDgUj1nOsvJ9tVAm76Ww+ /pAnepVRafMID0rqEfD9oGv1YrfpeFJhyE2zUw3SyyNLIKWD6QeLRhKQRbSnsXhGLFBXCqt9k5JA RhgQof9zvztcCVlT5KVvuyfC4H+HzeGmu9201BVyihJwKdcKPq+n/aY5FUVxNTgtI9f8wIbmfAja oT1pjXSp+dszakA98fhONM98pOq723o/1ZGMZukyXFfsDGtA3BB79HoopHKujLGWAGskzClwTjRQ xBqxh/U/lL1pc+0xPWikTNCmtziCOvv0KA0arDOMQlyFvImzX6oGVgE4ksKQYbMZ3Ikw6L1Rv1J+ FvN0aNwOKgL2ztBRYscUGcQvA0Zo1fGCAn/BLEJvQYShWKeKqjyncVGoXFsz2AcuFKe1pwETSsN6 OZncjy32e4ktgs07cWBfx0v62b8md36jau+B6RVnnodaA8++oXl3FRwiEW8XfXWIjy4umIv93tb8 8ekYsfOfWkTSewZYXGoqe4RtK80ulMHb/dh2FZQIFyRdN4HOmB4FYO5sEYFr9YjHLmDkrUgNodJC XCeMe4BO4iaxUQARAQABzRdtYXJrdXMucHJvYnN0QHBvc3Rlby5kZcLBkQQTAQgAOxYhBIJ0GMT0 rFjncjDEczR2H/jnrUPSBQJog714AhsDBQsJCAcCAiICBhUKCQgLAgQWAgMBAh4HAheAAAoJEDR2 H/jnrUPSgdkQAISaTk2D345ehXEkn5z2yUEjaVjHIE7ziqRaOgn/QanCgeTUinIv6L6QXUFvvIfH 1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2+XRx0Pk4e2Lj8IVy9yEYyrywEOC5XRW37KJjsiOAsumi1rkvM7QREWgUDe Xs0+RpxI3QrrANh71fLMRo7LKRF3Gvw13NVCCC9ea20P4PwhgWKStkwO2NO+YJsAoS1QycMi/vKu 0EHhknYXamaSV50oZzHKmX56vEeJHTcngrM8R1SwJCYopCx9gkz90bTVYlitJa5hloWTYeMD7FNj Y6jfVSzgM/K4gMgUNDW/PPGeMwARAQABwsF2BBgBCAAgFiEEgnQYxPSsWOdyMMRzNHYf+OetQ9IF AmiDvXgCGwwACgkQNHYf+OetQ9LHDBAAhk+ab8+WrbS/b1/gYW3q1KDiXU719nCtfkUVXKidW5Ec Idlr5HGt8ilLoxSWT2Zi368iHCXS0WenGgPwlv8ifvB7TOZiiTDZROZkXjEBmU4nYjJ7GymawpWv oQwjMsPuq6ysbzWtOZ7eILx7cI0FjQeJ/Q2baRJub0uAZNwBOxCkAS6lpk5Fntd2u8CWmDQo4SYp xeuQ+pwkp0yEP30RhN2BO2DXiBEGSZSYh+ioGbCHQPIV3iVj0h6lcCPOqopZqyeCfigeacBI0nvN jHWz/spzF3+4OS+3RJvoHtAQmProxyGib8iVsTxgZO3UUi4TSODeEt0i0kHSPY4sCciOyXfAyYoD DFqhRjOEwBBxhr+scU4C1T2AflozvDwq3VSONjrKJUkhd8+WsdXxMdPFgBQuiKKwUy11mz6KQfcR wmDehF3UaUoxa+YIhWPbKmycxuX/D8SvnqavzAeAL1OcRbEI/HsoroVlEFbBRNBZLJUlnTPs8ZcU 4+8rq5YX1GUrJL3jf6SAfSgO7UdkEET3PdcKFYtS+ruV1Cp5V0q4kCfI5jk25iiz8grM2wOzVSsc l1mEkhiEPH87HP0whhb544iioSnumd3HJKL7dzhRegsMizatupp8D65A2JziW0WKopa1iw9fti3A aBeNN4ijKZchBXHPgVx+YtWRHfcm4l8= OpenPGP: url=https://posteo.de/keys/markus.probst@posteo.de.asc; preference=encrypt Implement the `AsBusDevice` trait for converting a `Device` reference to a bus device reference for all bus devices. The `AsBusDevice` trait allows abstractions to provide the bus device in class device callbacks. It must not be used by drivers and is intended for bus and class device abstractions only. Signed-off-by: Markus Probst --- rust/kernel/auxiliary.rs | 7 +++++++ rust/kernel/device.rs | 33 +++++++++++++++++++++++++++++++++ rust/kernel/pci.rs | 7 +++++++ rust/kernel/platform.rs | 7 +++++++ rust/kernel/usb.rs | 6 ++++++ 5 files changed, 60 insertions(+) diff --git a/rust/kernel/auxiliary.rs b/rust/kernel/auxiliary.rs index 7a3b0b9c418e..db52062a4355 100644 --- a/rust/kernel/auxiliary.rs +++ b/rust/kernel/auxiliary.rs @@ -15,6 +15,7 @@ }; use core::{ marker::PhantomData, + mem::offset_of, ptr::{addr_of_mut, NonNull}, }; @@ -233,6 +234,12 @@ extern "C" fn release(dev: *mut bindings::device) { } } +// SAFETY: `auxiliary::Device` is a transparent wrapper of `struct auxiliary_device`. +// The offset is guaranteed to point to a valid device field inside `auxiliary::Device`. +unsafe impl device::AsBusDevice for Device { + const OFFSET: usize = offset_of!(bindings::auxiliary_device, dev); +} + // SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic // argument. kernel::impl_device_context_deref!(unsafe { Device }); diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index a849b7dde2fd..bf09fd0b0199 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -511,6 +511,39 @@ impl DeviceContext for Core {} impl DeviceContext for CoreInternal {} impl DeviceContext for Normal {} +/// Convert device references to bus device references. +/// +/// Bus devices can implement this trait to allow abstractions to provide the bus device in +/// class device callbacks. +/// +/// This must not be used by drivers and is intended for bus and class device abstractions only. +/// +/// # Safety +/// +/// `AsBusDevice::OFFSET` must be the offset of the embedded base `struct device` field within a +/// bus device structure. +pub(crate) unsafe trait AsBusDevice: AsRef> { + /// The relative offset to the device field. + /// + /// Use `offset_of!(bindings, field)` macro to avoid breakage. + const OFFSET: usize; + + /// Convert a reference to [`Device`] into `Self`. + /// + /// # Safety + /// + /// `dev` must be contained in `Self`. + unsafe fn from_device(dev: &Device) -> &Self + where + Self: Sized, + { + let raw = dev.as_raw(); + // SAFETY: `raw - Self::OFFSET` is guaranteed by the safety requirements + // to be a valid pointer to `Self`. + unsafe { &*raw.byte_sub(Self::OFFSET).cast::() } + } +} + /// # Safety /// /// The type given as `$device` must be a transparent wrapper of a type that doesn't depend on the diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index 7fcc5f6022c1..d78f6feeaae9 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -19,6 +19,7 @@ }; use core::{ marker::PhantomData, + mem::offset_of, ops::Deref, ptr::{addr_of_mut, NonNull}, }; @@ -593,6 +594,12 @@ pub fn set_master(&self) { } } +// SAFETY: `pci::Device` is a transparent wrapper of `struct pci_dev`. +// The offset is guaranteed to point to a valid device field inside `pci::Device`. +unsafe impl device::AsBusDevice for Device { + const OFFSET: usize = offset_of!(bindings::pci_dev, dev); +} + // SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic // argument. kernel::impl_device_context_deref!(unsafe { Device }); diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs index 7205fe3416d3..7197264b5a53 100644 --- a/rust/kernel/platform.rs +++ b/rust/kernel/platform.rs @@ -19,6 +19,7 @@ use core::{ marker::PhantomData, + mem::offset_of, ptr::{addr_of_mut, NonNull}, }; @@ -285,6 +286,12 @@ pub fn io_request_by_name(&self, name: &CStr) -> Option> { } } +// SAFETY: `platform::Device` is a transparent wrapper of `struct platform_device`. +// The offset is guaranteed to point to a valid device field inside `platform::Device`. +unsafe impl device::AsBusDevice for Device { + const OFFSET: usize = offset_of!(bindings::platform_device, dev); +} + macro_rules! define_irq_accessor_by_index { ( $(#[$meta:meta])* $fn_name:ident, diff --git a/rust/kernel/usb.rs b/rust/kernel/usb.rs index 14ddb711bab3..ec21261baf6a 100644 --- a/rust/kernel/usb.rs +++ b/rust/kernel/usb.rs @@ -324,6 +324,12 @@ fn as_raw(&self) -> *mut bindings::usb_interface { } } +// SAFETY: `usb::Interface` is a transparent wrapper of `struct usb_interface`. +// The offset is guaranteed to point to a valid device field inside `usb::Interface`. +unsafe impl device::AsBusDevice for Interface { + const OFFSET: usize = offset_of!(bindings::usb_interface, dev); +} + // SAFETY: `Interface` is a transparent wrapper of a type that doesn't depend on // `Interface`'s generic argument. kernel::impl_device_context_deref!(unsafe { Interface }); -- 2.51.0