[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20260107103511.570525-5-dakr@kernel.org>
Date: Wed, 7 Jan 2026 11:35:03 +0100
From: Danilo Krummrich <dakr@...nel.org>
To: gregkh@...uxfoundation.org,
rafael@...nel.org,
igor.korotin.linux@...il.com,
ojeda@...nel.org,
boqun.feng@...il.com,
gary@...yguo.net,
bjorn3_gh@...tonmail.com,
lossin@...nel.org,
a.hindborg@...nel.org,
aliceryhl@...gle.com,
tmgross@...ch.edu,
david.m.ertman@...el.com,
ira.weiny@...el.com,
leon@...nel.org,
bhelgaas@...gle.com,
kwilczynski@...nel.org,
wsa+renesas@...g-engineering.com
Cc: linux-kernel@...r.kernel.org,
rust-for-linux@...r.kernel.org,
linux-pci@...r.kernel.org,
linux-usb@...r.kernel.org,
linux-i2c@...r.kernel.org,
Danilo Krummrich <dakr@...nel.org>
Subject: [PATCH 4/6] rust: driver: add DEVICE_DRIVER_OFFSET to the Driver trait
Add an associated const DEVICE_DRIVER_OFFSET to the Driver trait
indicating the offset of the embedded struct device_driver within
Self::DriverType, i.e. the specific driver structs, such as
struct pci_driver or struct platform_driver.
Signed-off-by: Danilo Krummrich <dakr@...nel.org>
---
rust/kernel/auxiliary.rs | 3 +++
rust/kernel/driver.rs | 8 +++++++-
rust/kernel/i2c.rs | 3 +++
rust/kernel/pci.rs | 3 +++
rust/kernel/platform.rs | 3 +++
rust/kernel/usb.rs | 3 +++
6 files changed, 22 insertions(+), 1 deletion(-)
diff --git a/rust/kernel/auxiliary.rs b/rust/kernel/auxiliary.rs
index 4636b6f41195..e712d1b89dc3 100644
--- a/rust/kernel/auxiliary.rs
+++ b/rust/kernel/auxiliary.rs
@@ -25,8 +25,11 @@
// SAFETY:
// - `bindings::auxiliary_driver` is a C type declared as `repr(C)`.
+// - `struct auxiliary_driver` embeds a `struct device_driver`.
+// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
unsafe impl<T: Driver + 'static> driver::Driver for Adapter<T> {
type DriverType = bindings::auxiliary_driver;
+ const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
}
// SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
diff --git a/rust/kernel/driver.rs b/rust/kernel/driver.rs
index cd1d36c313e1..4b0c53b7d22a 100644
--- a/rust/kernel/driver.rs
+++ b/rust/kernel/driver.rs
@@ -107,10 +107,16 @@
/// # Safety
///
/// Implementors must guarantee that:
-/// - `DriverType` is `repr(C)`.
+/// - `DriverType` is `repr(C)`,
+/// - `DriverType` embeds a valid `struct device_driver` at byte offset `DEVICE_DRIVER_OFFSET`.
pub unsafe trait Driver {
/// The specific driver type embedding a `struct device_driver`.
type DriverType: Default;
+
+ /// Byte offset of the embedded `struct device_driver` within `DriverType`.
+ ///
+ /// This must correspond exactly to the location of the embedded `struct device_driver` field.
+ const DEVICE_DRIVER_OFFSET: usize;
}
/// The [`RegistrationOps`] trait serves as generic interface for subsystems (e.g., PCI, Platform,
diff --git a/rust/kernel/i2c.rs b/rust/kernel/i2c.rs
index de35961c6903..56f1ed8163a0 100644
--- a/rust/kernel/i2c.rs
+++ b/rust/kernel/i2c.rs
@@ -94,8 +94,11 @@ macro_rules! i2c_device_table {
// SAFETY:
// - `bindings::i2c_driver` is a C type declared as `repr(C)`.
+// - `struct i2c_driver` embeds a `struct device_driver`.
+// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
unsafe impl<T: Driver + 'static> driver::Driver for Adapter<T> {
type DriverType = bindings::i2c_driver;
+ const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
}
// SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs
index f58ce35d9c60..68466150ef20 100644
--- a/rust/kernel/pci.rs
+++ b/rust/kernel/pci.rs
@@ -52,8 +52,11 @@
// SAFETY:
// - `bindings::pci_driver` is a C type declared as `repr(C)`.
+// - `struct pci_driver` embeds a `struct device_driver`.
+// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
unsafe impl<T: Driver + 'static> driver::Driver for Adapter<T> {
type DriverType = bindings::pci_driver;
+ const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
}
// SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs
index e48d055fdc8a..56d9e968634e 100644
--- a/rust/kernel/platform.rs
+++ b/rust/kernel/platform.rs
@@ -28,8 +28,11 @@
// SAFETY:
// - `bindings::platform_driver` is a C type declared as `repr(C)`.
+// - `struct platform_driver` embeds a `struct device_driver`.
+// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
unsafe impl<T: Driver + 'static> driver::Driver for Adapter<T> {
type DriverType = bindings::platform_driver;
+ const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
}
// SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
diff --git a/rust/kernel/usb.rs b/rust/kernel/usb.rs
index 32f4b2d55dfb..a9a9d2298d87 100644
--- a/rust/kernel/usb.rs
+++ b/rust/kernel/usb.rs
@@ -29,8 +29,11 @@
// SAFETY:
// - `bindings::usb_driver` is a C type declared as `repr(C)`.
+// - `struct usb_driver` embeds a `struct device_driver`.
+// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
unsafe impl<T: Driver + 'static> driver::Driver for Adapter<T> {
type DriverType = bindings::usb_driver;
+ const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
}
// SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
--
2.52.0
Powered by blists - more mailing lists