History

rust: driver: move 'static bounds to constructor

With the ForeignOwnable lifetime change, the 'static bound is no longer necessary on the drvdata methods or bus adapter impls. Move it to the Registration constructor instead.

Gary Guo committed 5 months ago

Commit c8a43661 parent e9df9187 files · +26 −23Browse files

Unified · Split

7 files
  1. rust/kernel/auxiliary.rs +3 −3
  2. rust/kernel/device.rs +4 −4
  3. rust/kernel/driver.rs +5 −2
  4. rust/kernel/i2c.rs +4 −4
  5. rust/kernel/pci.rs +3 −3
  6. rust/kernel/platform.rs +4 −4
  7. rust/kernel/usb.rs +3 −3
rust/kernel/auxiliary.rs+3−3
@@ -44,7 +44,7 @@ pub struct Adapter<T: Driver>(T); · 43 unmodified lines
44// - `T` is the type of the driver's device private data.44// - `T` is the type of the driver's device private data.
45// - `struct auxiliary_driver` embeds a `struct device_driver`.45// - `struct auxiliary_driver` embeds a `struct device_driver`.
46// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.46// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
47unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {47unsafe impl<T: Driver> driver::DriverLayout for Adapter<T> {
48 type DriverType = bindings::auxiliary_driver;48 type DriverType = bindings::auxiliary_driver;
49 type DriverData = T;49 type DriverData = T;
50 const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);50 const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
@@ -52,7 +52,7 @@ unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> { · 1 unmodified line
52 52
53// SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if53// SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
54// a preceding call to `register` has been successful.54// a preceding call to `register` has been successful.
55unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {55unsafe impl<T: Driver> driver::RegistrationOps for Adapter<T> {
56 unsafe fn register(56 unsafe fn register(
57 adrv: &Opaque<Self::DriverType>,57 adrv: &Opaque<Self::DriverType>,
58 name: &'static CStr,58 name: &'static CStr,
@@ -78,7 +78,7 @@ unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> { · 19 unmodified lines
78 }78 }
79}79}
80 80
81impl<T: Driver + 'static> Adapter<T> {81impl<T: Driver> Adapter<T> {
82 extern "C" fn probe_callback(82 extern "C" fn probe_callback(
83 adev: *mut bindings::auxiliary_device,83 adev: *mut bindings::auxiliary_device,
84 id: *const bindings::auxiliary_device_id,84 id: *const bindings::auxiliary_device_id,
rust/kernel/device.rs+4−4
@@ -203,7 +203,7 @@ impl Device { · 202 unmodified lines
203 203
204impl Device<CoreInternal> {204impl Device<CoreInternal> {
205 /// Store a pointer to the bound driver's private data.205 /// Store a pointer to the bound driver's private data.
206 pub fn set_drvdata<T: 'static>(&self, data: impl PinInit<T, Error>) -> Result {206 pub fn set_drvdata<T>(&self, data: impl PinInit<T, Error>) -> Result {
207 let data = KBox::pin_init(data, GFP_KERNEL)?;207 let data = KBox::pin_init(data, GFP_KERNEL)?;
208 208
209 // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.209 // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.
@@ -218,7 +218,7 @@ impl Device<CoreInternal> { · 8 unmodified lines
218 ///218 ///
219 /// - The type `T` must match the type of the `ForeignOwnable` previously stored by219 /// - The type `T` must match the type of the `ForeignOwnable` previously stored by
220 /// [`Device::set_drvdata`].220 /// [`Device::set_drvdata`].
221 pub(crate) unsafe fn drvdata_obtain<T: 'static>(&self) -> Option<Pin<KBox<T>>> {221 pub(crate) unsafe fn drvdata_obtain<T>(&self) -> Option<Pin<KBox<T>>> {
222 // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.222 // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.
223 let ptr = unsafe { bindings::dev_get_drvdata(self.as_raw()) };223 let ptr = unsafe { bindings::dev_get_drvdata(self.as_raw()) };
224 224
@@ -244,7 +244,7 @@ impl Device<CoreInternal> { · 19 unmodified lines
244 /// device is fully unbound.244 /// device is fully unbound.
245 /// - The type `T` must match the type of the `ForeignOwnable` previously stored by245 /// - The type `T` must match the type of the `ForeignOwnable` previously stored by
246 /// [`Device::set_drvdata`].246 /// [`Device::set_drvdata`].
247 pub unsafe fn drvdata_borrow<T: 'static>(&self) -> Pin<&T> {247 pub unsafe fn drvdata_borrow<T>(&self) -> Pin<&T> {
248 // SAFETY: `drvdata_unchecked()` has the exact same safety requirements as the ones248 // SAFETY: `drvdata_unchecked()` has the exact same safety requirements as the ones
249 // required by this method.249 // required by this method.
250 unsafe { self.drvdata_unchecked() }250 unsafe { self.drvdata_unchecked() }
@@ -260,7 +260,7 @@ impl Device<Bound> { · 9 unmodified lines
260 /// the device is fully unbound.260 /// the device is fully unbound.
261 /// - The type `T` must match the type of the `ForeignOwnable` previously stored by261 /// - The type `T` must match the type of the `ForeignOwnable` previously stored by
262 /// [`Device::set_drvdata`].262 /// [`Device::set_drvdata`].
263 unsafe fn drvdata_unchecked<T: 'static>(&self) -> Pin<&T> {263 unsafe fn drvdata_unchecked<T>(&self) -> Pin<&T> {
264 // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.264 // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.
265 let ptr = unsafe { bindings::dev_get_drvdata(self.as_raw()) };265 let ptr = unsafe { bindings::dev_get_drvdata(self.as_raw()) };
266 266
rust/kernel/driver.rs+5−2
@@ -181,7 +181,7 @@ unsafe impl<T: RegistrationOps> Sync for Registration<T> {} · 180 unmodified lines
181// any thread, so `Registration` is `Send`.181// any thread, so `Registration` is `Send`.
182unsafe impl<T: RegistrationOps> Send for Registration<T> {}182unsafe impl<T: RegistrationOps> Send for Registration<T> {}
183 183
184impl<T: RegistrationOps + 'static> Registration<T> {184impl<T: RegistrationOps> Registration<T> {
185 extern "C" fn post_unbind_callback(dev: *mut bindings::device) {185 extern "C" fn post_unbind_callback(dev: *mut bindings::device) {
186 // SAFETY: The driver core only ever calls the post unbind callback with a valid pointer to186 // SAFETY: The driver core only ever calls the post unbind callback with a valid pointer to
187 // a `struct device`.187 // a `struct device`.
@@ -215,7 +215,10 @@ impl<T: RegistrationOps + 'static> Registration<T> { · 27 unmodified lines
215 }215 }
216 216
217 /// Creates a new instance of the registration object.217 /// Creates a new instance of the registration object.
218 pub fn new(name: &'static CStr, module: &'static ThisModule) -> impl PinInit<Self, Error> {218 pub fn new(name: &'static CStr, module: &'static ThisModule) -> impl PinInit<Self, Error>
219 where
220 T: 'static,
221 {
219 try_pin_init!(Self {222 try_pin_init!(Self {
220 reg <- Opaque::try_ffi_init(|ptr: *mut T::DriverType| {223 reg <- Opaque::try_ffi_init(|ptr: *mut T::DriverType| {
221 // SAFETY: `try_ffi_init` guarantees that `ptr` is valid for write.224 // SAFETY: `try_ffi_init` guarantees that `ptr` is valid for write.
rust/kernel/i2c.rs+4−4
@@ -96,7 +96,7 @@ pub struct Adapter<T: Driver>(T); · 95 unmodified lines
96// - `T` is the type of the driver's device private data.96// - `T` is the type of the driver's device private data.
97// - `struct i2c_driver` embeds a `struct device_driver`.97// - `struct i2c_driver` embeds a `struct device_driver`.
98// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.98// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
99unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {99unsafe impl<T: Driver> driver::DriverLayout for Adapter<T> {
100 type DriverType = bindings::i2c_driver;100 type DriverType = bindings::i2c_driver;
101 type DriverData = T;101 type DriverData = T;
102 const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);102 const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
@@ -104,7 +104,7 @@ unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> { · 1 unmodified line
104 104
105// SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if105// SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
106// a preceding call to `register` has been successful.106// a preceding call to `register` has been successful.
107unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {107unsafe impl<T: Driver> driver::RegistrationOps for Adapter<T> {
108 unsafe fn register(108 unsafe fn register(
109 idrv: &Opaque<Self::DriverType>,109 idrv: &Opaque<Self::DriverType>,
110 name: &'static CStr,110 name: &'static CStr,
@@ -151,7 +151,7 @@ unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> { · 40 unmodified lines
151 }151 }
152}152}
153 153
154impl<T: Driver + 'static> Adapter<T> {154impl<T: Driver> Adapter<T> {
155 extern "C" fn probe_callback(idev: *mut bindings::i2c_client) -> kernel::ffi::c_int {155 extern "C" fn probe_callback(idev: *mut bindings::i2c_client) -> kernel::ffi::c_int {
156 // SAFETY: The I2C bus only ever calls the probe callback with a valid pointer to a156 // SAFETY: The I2C bus only ever calls the probe callback with a valid pointer to a
157 // `struct i2c_client`.157 // `struct i2c_client`.
@@ -222,7 +222,7 @@ impl<T: Driver + 'static> Adapter<T> { · 64 unmodified lines
222 }222 }
223}223}
224 224
225impl<T: Driver + 'static> driver::Adapter for Adapter<T> {225impl<T: Driver> driver::Adapter for Adapter<T> {
226 type IdInfo = T::IdInfo;226 type IdInfo = T::IdInfo;
227 227
228 fn of_id_table() -> Option<of::IdTable<Self::IdInfo>> {228 fn of_id_table() -> Option<of::IdTable<Self::IdInfo>> {
rust/kernel/pci.rs+3−3
@@ -62,7 +62,7 @@ pub struct Adapter<T: Driver>(T); · 61 unmodified lines
62// - `T` is the type of the driver's device private data.62// - `T` is the type of the driver's device private data.
63// - `struct pci_driver` embeds a `struct device_driver`.63// - `struct pci_driver` embeds a `struct device_driver`.
64// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.64// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
65unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {65unsafe impl<T: Driver> driver::DriverLayout for Adapter<T> {
66 type DriverType = bindings::pci_driver;66 type DriverType = bindings::pci_driver;
67 type DriverData = T;67 type DriverData = T;
68 const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);68 const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
@@ -70,7 +70,7 @@ unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> { · 1 unmodified line
70 70
71// SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if71// SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
72// a preceding call to `register` has been successful.72// a preceding call to `register` has been successful.
73unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {73unsafe impl<T: Driver> driver::RegistrationOps for Adapter<T> {
74 unsafe fn register(74 unsafe fn register(
75 pdrv: &Opaque<Self::DriverType>,75 pdrv: &Opaque<Self::DriverType>,
76 name: &'static CStr,76 name: &'static CStr,
@@ -96,7 +96,7 @@ unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> { · 19 unmodified lines
96 }96 }
97}97}
98 98
99impl<T: Driver + 'static> Adapter<T> {99impl<T: Driver> Adapter<T> {
100 extern "C" fn probe_callback(100 extern "C" fn probe_callback(
101 pdev: *mut bindings::pci_dev,101 pdev: *mut bindings::pci_dev,
102 id: *const bindings::pci_device_id,102 id: *const bindings::pci_device_id,
rust/kernel/platform.rs+4−4
@@ -48,7 +48,7 @@ pub struct Adapter<T: Driver>(T); · 47 unmodified lines
48// - `T` is the type of the driver's device private data.48// - `T` is the type of the driver's device private data.
49// - `struct platform_driver` embeds a `struct device_driver`.49// - `struct platform_driver` embeds a `struct device_driver`.
50// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.50// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
51unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {51unsafe impl<T: Driver> driver::DriverLayout for Adapter<T> {
52 type DriverType = bindings::platform_driver;52 type DriverType = bindings::platform_driver;
53 type DriverData = T;53 type DriverData = T;
54 const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);54 const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
@@ -56,7 +56,7 @@ unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> { · 1 unmodified line
56 56
57// SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if57// SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
58// a preceding call to `register` has been successful.58// a preceding call to `register` has been successful.
59unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {59unsafe impl<T: Driver> driver::RegistrationOps for Adapter<T> {
60 unsafe fn register(60 unsafe fn register(
61 pdrv: &Opaque<Self::DriverType>,61 pdrv: &Opaque<Self::DriverType>,
62 name: &'static CStr,62 name: &'static CStr,
@@ -91,7 +91,7 @@ unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> { · 28 unmodified lines
91 }91 }
92}92}
93 93
94impl<T: Driver + 'static> Adapter<T> {94impl<T: Driver> Adapter<T> {
95 extern "C" fn probe_callback(pdev: *mut bindings::platform_device) -> kernel::ffi::c_int {95 extern "C" fn probe_callback(pdev: *mut bindings::platform_device) -> kernel::ffi::c_int {
96 // SAFETY: The platform bus only ever calls the probe callback with a valid pointer to a96 // SAFETY: The platform bus only ever calls the probe callback with a valid pointer to a
97 // `struct platform_device`.97 // `struct platform_device`.
@@ -124,7 +124,7 @@ impl<T: Driver + 'static> Adapter<T> { · 26 unmodified lines
124 }124 }
125}125}
126 126
127impl<T: Driver + 'static> driver::Adapter for Adapter<T> {127impl<T: Driver> driver::Adapter for Adapter<T> {
128 type IdInfo = T::IdInfo;128 type IdInfo = T::IdInfo;
129 129
130 fn of_id_table() -> Option<of::IdTable<Self::IdInfo>> {130 fn of_id_table() -> Option<of::IdTable<Self::IdInfo>> {
rust/kernel/usb.rs+3−3
@@ -39,7 +39,7 @@ pub struct Adapter<T: Driver>(T); · 38 unmodified lines
39// - `T` is the type of the driver's device private data.39// - `T` is the type of the driver's device private data.
40// - `struct usb_driver` embeds a `struct device_driver`.40// - `struct usb_driver` embeds a `struct device_driver`.
41// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.41// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
42unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {42unsafe impl<T: Driver> driver::DriverLayout for Adapter<T> {
43 type DriverType = bindings::usb_driver;43 type DriverType = bindings::usb_driver;
44 type DriverData = T;44 type DriverData = T;
45 const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);45 const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
@@ -47,7 +47,7 @@ unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> { · 1 unmodified line
47 47
48// SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if48// SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
49// a preceding call to `register` has been successful.49// a preceding call to `register` has been successful.
50unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {50unsafe impl<T: Driver> driver::RegistrationOps for Adapter<T> {
51 unsafe fn register(51 unsafe fn register(
52 udrv: &Opaque<Self::DriverType>,52 udrv: &Opaque<Self::DriverType>,
53 name: &'static CStr,53 name: &'static CStr,
@@ -73,7 +73,7 @@ unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> { · 19 unmodified lines
73 }73 }
74}74}
75 75
76impl<T: Driver + 'static> Adapter<T> {76impl<T: Driver> Adapter<T> {
77 extern "C" fn probe_callback(77 extern "C" fn probe_callback(
78 intf: *mut bindings::usb_interface,78 intf: *mut bindings::usb_interface,
79 id: *const bindings::usb_device_id,79 id: *const bindings::usb_device_id,