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>

+22 -1
+3
rust/kernel/auxiliary.rs
··· 25 26 // SAFETY: 27 // - `bindings::auxiliary_driver` is a C type declared as `repr(C)`. 28 unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> { 29 type DriverType = bindings::auxiliary_driver; 30 } 31 32 // SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
··· 25 26 // SAFETY: 27 // - `bindings::auxiliary_driver` is a C type declared as `repr(C)`. 28 + // - `struct auxiliary_driver` embeds a `struct device_driver`. 29 + // - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`. 30 unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> { 31 type DriverType = bindings::auxiliary_driver; 32 + const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver); 33 } 34 35 // SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
+7 -1
rust/kernel/driver.rs
··· 107 /// # Safety 108 /// 109 /// Implementors must guarantee that: 110 - /// - `DriverType` is `repr(C)`. 111 pub unsafe trait DriverLayout { 112 /// The specific driver type embedding a `struct device_driver`. 113 type DriverType: Default; 114 } 115 116 /// The [`RegistrationOps`] trait serves as generic interface for subsystems (e.g., PCI, Platform,
··· 107 /// # Safety 108 /// 109 /// Implementors must guarantee that: 110 + /// - `DriverType` is `repr(C)`, 111 + /// - `DriverType` embeds a valid `struct device_driver` at byte offset `DEVICE_DRIVER_OFFSET`. 112 pub unsafe trait DriverLayout { 113 /// The specific driver type embedding a `struct device_driver`. 114 type DriverType: Default; 115 + 116 + /// Byte offset of the embedded `struct device_driver` within `DriverType`. 117 + /// 118 + /// This must correspond exactly to the location of the embedded `struct device_driver` field. 119 + const DEVICE_DRIVER_OFFSET: usize; 120 } 121 122 /// The [`RegistrationOps`] trait serves as generic interface for subsystems (e.g., PCI, Platform,
+3
rust/kernel/i2c.rs
··· 94 95 // SAFETY: 96 // - `bindings::i2c_driver` is a C type declared as `repr(C)`. 97 unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> { 98 type DriverType = bindings::i2c_driver; 99 } 100 101 // SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
··· 94 95 // SAFETY: 96 // - `bindings::i2c_driver` is a C type declared as `repr(C)`. 97 + // - `struct i2c_driver` embeds a `struct device_driver`. 98 + // - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`. 99 unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> { 100 type DriverType = bindings::i2c_driver; 101 + const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver); 102 } 103 104 // SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
+3
rust/kernel/pci.rs
··· 52 53 // SAFETY: 54 // - `bindings::pci_driver` is a C type declared as `repr(C)`. 55 unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> { 56 type DriverType = bindings::pci_driver; 57 } 58 59 // SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
··· 52 53 // SAFETY: 54 // - `bindings::pci_driver` is a C type declared as `repr(C)`. 55 + // - `struct pci_driver` embeds a `struct device_driver`. 56 + // - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`. 57 unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> { 58 type DriverType = bindings::pci_driver; 59 + const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver); 60 } 61 62 // SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
+3
rust/kernel/platform.rs
··· 28 29 // SAFETY: 30 // - `bindings::platform_driver` is a C type declared as `repr(C)`. 31 unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> { 32 type DriverType = bindings::platform_driver; 33 } 34 35 // SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
··· 28 29 // SAFETY: 30 // - `bindings::platform_driver` is a C type declared as `repr(C)`. 31 + // - `struct platform_driver` embeds a `struct device_driver`. 32 + // - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`. 33 unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> { 34 type DriverType = bindings::platform_driver; 35 + const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver); 36 } 37 38 // SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
+3
rust/kernel/usb.rs
··· 29 30 // SAFETY: 31 // - `bindings::usb_driver` is a C type declared as `repr(C)`. 32 unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> { 33 type DriverType = bindings::usb_driver; 34 } 35 36 // SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
··· 29 30 // SAFETY: 31 // - `bindings::usb_driver` is a C type declared as `repr(C)`. 32 + // - `struct usb_driver` embeds a `struct device_driver`. 33 + // - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`. 34 unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> { 35 type DriverType = bindings::usb_driver; 36 + const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver); 37 } 38 39 // SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if