summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanilo Krummrich <dakr@kernel.org>2026-01-07 13:35:03 +0300
committerDanilo Krummrich <dakr@kernel.org>2026-01-16 03:17:29 +0300
commitc1d4519e1c36ffa01973e23af4502e69dcd84f39 (patch)
tree26e7f83683b1feefa4a9606cdde88143bffc770f
parent0af1a9e4629a85964a7eebe58ebd2ca37c8c21fc (diff)
downloadlinux-c1d4519e1c36ffa01973e23af4502e69dcd84f39.tar.xz
rust: driver: add DEVICE_DRIVER_OFFSET to the DriverLayout trait
Add an associated const DEVICE_DRIVER_OFFSET to the DriverLayout 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. Acked-by: Alice Ryhl <aliceryhl@google.com> Acked-by: Igor Korotin <igor.korotin.linux@gmail.com> Link: https://patch.msgid.link/20260107103511.570525-5-dakr@kernel.org Signed-off-by: Danilo Krummrich <dakr@kernel.org>
-rw-r--r--rust/kernel/auxiliary.rs3
-rw-r--r--rust/kernel/driver.rs8
-rw-r--r--rust/kernel/i2c.rs3
-rw-r--r--rust/kernel/pci.rs3
-rw-r--r--rust/kernel/platform.rs3
-rw-r--r--rust/kernel/usb.rs3
6 files changed, 22 insertions, 1 deletions
diff --git a/rust/kernel/auxiliary.rs b/rust/kernel/auxiliary.rs
index 9922b9158d16..9b25af331ad5 100644
--- a/rust/kernel/auxiliary.rs
+++ b/rust/kernel/auxiliary.rs
@@ -25,8 +25,11 @@ pub struct Adapter<T: Driver>(T);
// 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::DriverLayout 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 73968b13d7dc..4a96a07905d1 100644
--- a/rust/kernel/driver.rs
+++ b/rust/kernel/driver.rs
@@ -107,10 +107,16 @@ use pin_init::{pin_data, pinned_drop, PinInit};
/// # 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 DriverLayout {
/// 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 8e80d8572e1a..d97e73282003 100644
--- a/rust/kernel/i2c.rs
+++ b/rust/kernel/i2c.rs
@@ -94,8 +94,11 @@ pub struct Adapter<T: Driver>(T);
// 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::DriverLayout 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 703ce5709f0c..fe6f508b0cac 100644
--- a/rust/kernel/pci.rs
+++ b/rust/kernel/pci.rs
@@ -52,8 +52,11 @@ pub struct Adapter<T: Driver>(T);
// 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::DriverLayout 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 93a64cf86b76..716c9cc25aea 100644
--- a/rust/kernel/platform.rs
+++ b/rust/kernel/platform.rs
@@ -28,8 +28,11 @@ pub struct Adapter<T: Driver>(T);
// 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::DriverLayout 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 60b761c06fbd..eb1c9b9ef228 100644
--- a/rust/kernel/usb.rs
+++ b/rust/kernel/usb.rs
@@ -29,8 +29,11 @@ pub struct Adapter<T: Driver>(T);
// 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::DriverLayout 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