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
4444 // - `T` is the type of the driver's device private data.
4545 // - `struct auxiliary_driver` embeds a `struct device_driver`.
4646 // - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
47−unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {
47+unsafe impl<T: Driver> driver::DriverLayout for Adapter<T> {
4848 type DriverType = bindings::auxiliary_driver;
4949 type DriverData = T;
5050 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
5252
5353 // SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
5454 // a preceding call to `register` has been successful.
55−unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {
55+unsafe impl<T: Driver> driver::RegistrationOps for Adapter<T> {
5656 unsafe fn register(
5757 adrv: &Opaque<Self::DriverType>,
5858 name: &'static CStr,
@@ -78,7 +78,7 @@ unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> { · 19 unmodified lines
7878 }
7979 }
8080
81−impl<T: Driver + 'static> Adapter<T> {
81+impl<T: Driver> Adapter<T> {
8282 extern "C" fn probe_callback(
8383 adev: *mut bindings::auxiliary_device,
8484 id: *const bindings::auxiliary_device_id,
rust/kernel/device.rs+4−4
@@ -203,7 +203,7 @@ impl Device { · 202 unmodified lines
203203
204204 impl Device<CoreInternal> {
205205 /// 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 {
207207 let data = KBox::pin_init(data, GFP_KERNEL)?;
208208
209209 // 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
218218 ///
219219 /// - The type `T` must match the type of the `ForeignOwnable` previously stored by
220220 /// [`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>>> {
222222 // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.
223223 let ptr = unsafe { bindings::dev_get_drvdata(self.as_raw()) };
224224
@@ -244,7 +244,7 @@ impl Device<CoreInternal> { · 19 unmodified lines
244244 /// device is fully unbound.
245245 /// - The type `T` must match the type of the `ForeignOwnable` previously stored by
246246 /// [`Device::set_drvdata`].
247− pub unsafe fn drvdata_borrow<T: 'static>(&self) -> Pin<&T> {
247+ pub unsafe fn drvdata_borrow<T>(&self) -> Pin<&T> {
248248 // SAFETY: `drvdata_unchecked()` has the exact same safety requirements as the ones
249249 // required by this method.
250250 unsafe { self.drvdata_unchecked() }
@@ -260,7 +260,7 @@ impl Device<Bound> { · 9 unmodified lines
260260 /// the device is fully unbound.
261261 /// - The type `T` must match the type of the `ForeignOwnable` previously stored by
262262 /// [`Device::set_drvdata`].
263− unsafe fn drvdata_unchecked<T: 'static>(&self) -> Pin<&T> {
263+ unsafe fn drvdata_unchecked<T>(&self) -> Pin<&T> {
264264 // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.
265265 let ptr = unsafe { bindings::dev_get_drvdata(self.as_raw()) };
266266
rust/kernel/driver.rs+5−2
@@ -181,7 +181,7 @@ unsafe impl<T: RegistrationOps> Sync for Registration<T> {} · 180 unmodified lines
181181 // any thread, so `Registration` is `Send`.
182182 unsafe impl<T: RegistrationOps> Send for Registration<T> {}
183183
184−impl<T: RegistrationOps + 'static> Registration<T> {
184+impl<T: RegistrationOps> Registration<T> {
185185 extern "C" fn post_unbind_callback(dev: *mut bindings::device) {
186186 // SAFETY: The driver core only ever calls the post unbind callback with a valid pointer to
187187 // a `struct device`.
@@ -215,7 +215,10 @@ impl<T: RegistrationOps + 'static> Registration<T> { · 27 unmodified lines
215215 }
216216
217217 /// 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+ {
219222 try_pin_init!(Self {
220223 reg <- Opaque::try_ffi_init(|ptr: *mut T::DriverType| {
221224 // 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
9696 // - `T` is the type of the driver's device private data.
9797 // - `struct i2c_driver` embeds a `struct device_driver`.
9898 // - `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> {
99+unsafe impl<T: Driver> driver::DriverLayout for Adapter<T> {
100100 type DriverType = bindings::i2c_driver;
101101 type DriverData = T;
102102 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
104104
105105 // SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
106106 // a preceding call to `register` has been successful.
107−unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {
107+unsafe impl<T: Driver> driver::RegistrationOps for Adapter<T> {
108108 unsafe fn register(
109109 idrv: &Opaque<Self::DriverType>,
110110 name: &'static CStr,
@@ -151,7 +151,7 @@ unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> { · 40 unmodified lines
151151 }
152152 }
153153
154−impl<T: Driver + 'static> Adapter<T> {
154+impl<T: Driver> Adapter<T> {
155155 extern "C" fn probe_callback(idev: *mut bindings::i2c_client) -> kernel::ffi::c_int {
156156 // SAFETY: The I2C bus only ever calls the probe callback with a valid pointer to a
157157 // `struct i2c_client`.
@@ -222,7 +222,7 @@ impl<T: Driver + 'static> Adapter<T> { · 64 unmodified lines
222222 }
223223 }
224224
225−impl<T: Driver + 'static> driver::Adapter for Adapter<T> {
225+impl<T: Driver> driver::Adapter for Adapter<T> {
226226 type IdInfo = T::IdInfo;
227227
228228 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
6262 // - `T` is the type of the driver's device private data.
6363 // - `struct pci_driver` embeds a `struct device_driver`.
6464 // - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
65−unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {
65+unsafe impl<T: Driver> driver::DriverLayout for Adapter<T> {
6666 type DriverType = bindings::pci_driver;
6767 type DriverData = T;
6868 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
7070
7171 // SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
7272 // a preceding call to `register` has been successful.
73−unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {
73+unsafe impl<T: Driver> driver::RegistrationOps for Adapter<T> {
7474 unsafe fn register(
7575 pdrv: &Opaque<Self::DriverType>,
7676 name: &'static CStr,
@@ -96,7 +96,7 @@ unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> { · 19 unmodified lines
9696 }
9797 }
9898
99−impl<T: Driver + 'static> Adapter<T> {
99+impl<T: Driver> Adapter<T> {
100100 extern "C" fn probe_callback(
101101 pdev: *mut bindings::pci_dev,
102102 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
4848 // - `T` is the type of the driver's device private data.
4949 // - `struct platform_driver` embeds a `struct device_driver`.
5050 // - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
51−unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {
51+unsafe impl<T: Driver> driver::DriverLayout for Adapter<T> {
5252 type DriverType = bindings::platform_driver;
5353 type DriverData = T;
5454 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
5656
5757 // SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
5858 // a preceding call to `register` has been successful.
59−unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {
59+unsafe impl<T: Driver> driver::RegistrationOps for Adapter<T> {
6060 unsafe fn register(
6161 pdrv: &Opaque<Self::DriverType>,
6262 name: &'static CStr,
@@ -91,7 +91,7 @@ unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> { · 28 unmodified lines
9191 }
9292 }
9393
94−impl<T: Driver + 'static> Adapter<T> {
94+impl<T: Driver> Adapter<T> {
9595 extern "C" fn probe_callback(pdev: *mut bindings::platform_device) -> kernel::ffi::c_int {
9696 // SAFETY: The platform bus only ever calls the probe callback with a valid pointer to a
9797 // `struct platform_device`.
@@ -124,7 +124,7 @@ impl<T: Driver + 'static> Adapter<T> { · 26 unmodified lines
124124 }
125125 }
126126
127−impl<T: Driver + 'static> driver::Adapter for Adapter<T> {
127+impl<T: Driver> driver::Adapter for Adapter<T> {
128128 type IdInfo = T::IdInfo;
129129
130130 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
3939 // - `T` is the type of the driver's device private data.
4040 // - `struct usb_driver` embeds a `struct device_driver`.
4141 // - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
42−unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {
42+unsafe impl<T: Driver> driver::DriverLayout for Adapter<T> {
4343 type DriverType = bindings::usb_driver;
4444 type DriverData = T;
4545 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
4747
4848 // SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
4949 // a preceding call to `register` has been successful.
50−unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {
50+unsafe impl<T: Driver> driver::RegistrationOps for Adapter<T> {
5151 unsafe fn register(
5252 udrv: &Opaque<Self::DriverType>,
5353 name: &'static CStr,
@@ -73,7 +73,7 @@ unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> { · 19 unmodified lines
7373 }
7474 }
7575
76−impl<T: Driver + 'static> Adapter<T> {
76+impl<T: Driver> Adapter<T> {
7777 extern "C" fn probe_callback(
7878 intf: *mut bindings::usb_interface,
7979 id: *const bindings::usb_device_id,