Skip to contentSkip to conversation

rust: drm: remove DRM device private data

PATCH Applied Danilo Krummrich 3 participants 8 messages Nov 18 – Nov 21

Subsystems (6)
lore
Share & tools
lore

Danilo Krummrich

author maintainer
#0 lore raw

PATCH 1/1 rust: drm: remove DRM device private data

Driver::Data is owned by the reference-counted drm::Device and can outlive device unregistration and driver unbind. Driver state belongs in RegistrationData, whose lifetime ends with registration teardown.

The intended model is that, after drm_dev_unplug(), surviving DRM objects (e.g. through remaining userspace handles) no longer require access to the driver state. Thus Driver::RegistrationData is available (and sufficient) for all DRM callbacks into the driver.

Remove Driver::Data, its storage and Deref implementation, and the unused workqueue implementations that depend on it. Drop the data initializer argument from UnregisteredDevice::new() and update its callers.

---
 drivers/gpu/drm/nova/driver.rs |   3 +-
 drivers/gpu/drm/tyr/driver.rs  |   3 +-
 rust/kernel/drm/device.rs      | 128 ++-------------------------------
 rust/kernel/drm/driver.rs      |   3 -
 rust/kernel/drm/gem/shmem.rs   |   6 +-
 5 files changed, 9 insertions(+), 134 deletions(-)
drivers/gpu/drm/nova/driver.rs +1 -2
index 44a52e60b3c7..df3f37d06861 100644
@@ -71,7 +71,7 @@ fn probe<'bound>(
     ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
         try_pin_init!(Self::Data {
             reg <- {
-                let drm = drm::UnregisteredDevice::<Self>::new(adev, Ok(()))?;
+                let drm = drm::UnregisteredDevice::<Self>::new(adev)?;
 
                 let reg_data = DrmRegData {
                     api: NovaCoreApi::of(adev)?,
@@ -88,7 +88,6 @@ fn probe<'bound>(
 
 #[vtable]
 impl drm::Driver for NovaDriver {
-    type Data = ();
     type RegistrationData<'a> = DrmRegData<'a>;
     type File = File;
     type Object = gem::Object<NovaObject>;
drivers/gpu/drm/tyr/driver.rs +1 -2
index c2b99d67369f..9caedd0fd130 100644
@@ -148,7 +148,7 @@ fn probe<'bound>(
             // other threads of execution.
             unsafe { pdev.dma_set_mask_and_coherent(DmaMask::try_new(pa_bits)?)? };
 
-            let unreg_dev = drm::UnregisteredDevice::<TyrDrmDriver>::new(pdev, Ok(()))?;
+            let unreg_dev = drm::UnregisteredDevice::<TyrDrmDriver>::new(pdev)?;
 
             let mmu = Mmu::new(pdev.as_ref(), iomem.as_arc_borrow(), &gpu_info)?;
 
@@ -207,7 +207,6 @@ fn drop(self: Pin<&mut Self>) {}
 
 #[vtable]
 impl drm::Driver for TyrDrmDriver {
-    type Data = ();
     type RegistrationData<'drm> = TyrDrmRegistrationData<'drm>;
     type File = TyrDrmFileData;
     type Object = Bo;
rust/kernel/drm/device.rs +6 -122

187 lines not on this page · raw message

rust/kernel/drm/driver.rs +0 -3
index f6db91e81e79..7b76e6abed9f 100644
@@ -106,9 +106,6 @@ pub trait AllocImpl: super::private::Sealed + drm::gem::IntoGEMObject {
 /// drm_driver` to be registered in the DRM subsystem.
 #[vtable]
 pub trait Driver {
-    /// Context data associated with the DRM driver
-    type Data: Sync + Send;
-
     /// Data owned by the [`Registration`] and accessible within a
     /// [`RegistrationGuard`](drm::RegistrationGuard) critical section via
     /// [`Device::registration_data_with()`](drm::Device::registration_data_with).
rust/kernel/drm/gem/shmem.rs +1 -5
index ed13e21d06cf..3a72fb21eeaa 100644
@@ -589,8 +589,6 @@ mod tests {
 
     // The bare minimum needed to create a fake drm driver for kunit
 
-    #[pin_data]
-    struct KunitData {}
     struct KunitDriver;
     struct KunitFile;
     #[pin_data]
@@ -627,7 +625,6 @@ fn new(
 
     #[vtable]
     impl drm::Driver for KunitDriver {
-        type Data = KunitData;
         type RegistrationData<'a> = ();
         type File = KunitFile;
         type Object = Object<KunitObject>;
@@ -639,10 +636,9 @@ impl drm::Driver for KunitDriver {
 
     fn create_drm_dev() -> Result<(faux::Registration, UnregisteredDevice<KunitDriver>)> {
         // Create a faux DRM device so we can test gem object creation.
-        let data = try_pin_init!(KunitData {});
         let reg = faux::Registration::new(c"Kunit", None)?;
         let fdev = reg.as_ref();
-        let drm = UnregisteredDevice::new(fdev, data)?;
+        let drm = UnregisteredDevice::new(fdev)?;
 
         Ok((reg, drm))
     }

base-commit: e55a991e09e3b44f0b0d8206bb523dc57c086341

Signature · 1 line
2.56.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Danilo Krummrich

Driver::Data is owned by the reference-counted drm::Device and can outlive device unregistration and driver unbind. Driver state belongs in RegistrationData, whose lifetime ends with registration teardown.

The intended model is that, after drm_dev_unplug(), surviving DRM objects (e.g. through remaining userspace handles) no longer require access to the driver state. Thus Driver::RegistrationData is available (and sufficient) for all DRM callbacks into the driver.

Quoted 6 lines

Remove Driver::Data, its storage and Deref implementation, and the unused workqueue implementations that depend on it. Drop the data initializer argument from UnregisteredDevice::new() and update its callers.

Makes sense to me. Both in-tree users had type Data = () anyway, and nothing uses the WorkItem/HasWork impls on drm::Device, so this is a pure simplification.

One small suggestion below.

Quoted 2 lines
rust/kernel/drm/device.rs +1 -1
-        release: Some(Device::<T>::release),
+        release: None,

[...]

Quoted 7 lines

#[repr(C)]
pub struct Device<T: drm::Driver, C: DeviceContext = Normal> {
dev: Opaque<bindings::drm_device>,
- data: T::Data,
pub(super) registration_data: UnsafeCell<NonNull<T::RegistrationData<'static>>>, _ctx: PhantomData<C>,
}

With release gone, nothing ever runs drop_in_place() on the Rust side of Device anymore. That's fine today since all remaining fields are trivially droppable, but it is now an implicit requirement on the
struct: anyone adding a field with a Drop impl (or a pinned field that needs a destructor) would silently leak it.

Could you add a short comment on the struct along the lines of "no release callback is installed, so no field of this struct may require dropping"? That way the next person adding a field knows they have to bring back the release callback.

(For what it's worth, I checked the lifetime-parameterized File series [1] against this: open_count/open_count_wq are both droppable without a destructor, and the two merge cleanly, so no ordering constraint there as far as I can tell.)

[1] https://lore.kernel.org/r/[email protected]

Alice

reviewer
#2 lore raw
Quoted 49 lines · Alice Ryhl
Quoted 9 lines · Danilo Krummrich

Driver::Data is owned by the reference-counted drm::Device and can outlive device unregistration and driver unbind. Driver state belongs in RegistrationData, whose lifetime ends with registration teardown.

The intended model is that, after drm_dev_unplug(), surviving DRM objects (e.g. through remaining userspace handles) no longer require access to the driver state. Thus Driver::RegistrationData is available (and sufficient) for all DRM callbacks into the driver.

Quoted 6 lines

Remove Driver::Data, its storage and Deref implementation, and the unused workqueue implementations that depend on it. Drop the data initializer argument from UnregisteredDevice::new() and update its callers.

Makes sense to me. Both in-tree users had type Data = () anyway, and nothing uses the WorkItem/HasWork impls on drm::Device, so this is a pure simplification.

One small suggestion below.

Quoted 2 lines
rust/kernel/drm/device.rs +1 -1
-        release: Some(Device::<T>::release),
+        release: None,

[...]

Quoted 7 lines

#[repr(C)]
pub struct Device<T: drm::Driver, C: DeviceContext = Normal> {
dev: Opaque<bindings::drm_device>,
- data: T::Data,
pub(super) registration_data: UnsafeCell<NonNull<T::RegistrationData<'static>>>, _ctx: PhantomData<C>,
}

With release gone, nothing ever runs drop_in_place() on the Rust side of Device anymore. That's fine today since all remaining fields are trivially droppable, but it is now an implicit requirement on the
struct: anyone adding a field with a Drop impl (or a pinned field that needs a destructor) would silently leak it.

Could you add a short comment on the struct along the lines of "no release callback is installed, so no field of this struct may require dropping"? That way the next person adding a field knows they have to bring back the release callback.

Quoted 4 lines

(For what it's worth, I checked the lifetime-parameterized File series [1] against this: open_count/open_count_wq are both droppable without a destructor, and the two merge cleanly, so no ordering constraint there as far as I can tell.)

Instead of a comment, a const assertion of !needs_drop would be better.

Best,
Gary

maintainer
#3 lore raw
Quoted 51 lines · Gary Guo
Quoted 49 lines · Alice Ryhl
Quoted 9 lines · Danilo Krummrich

Driver::Data is owned by the reference-counted drm::Device and can outlive device unregistration and driver unbind. Driver state belongs in RegistrationData, whose lifetime ends with registration teardown.

The intended model is that, after drm_dev_unplug(), surviving DRM objects (e.g. through remaining userspace handles) no longer require access to the driver state. Thus Driver::RegistrationData is available (and sufficient) for all DRM callbacks into the driver.

Quoted 6 lines

Remove Driver::Data, its storage and Deref implementation, and the unused workqueue implementations that depend on it. Drop the data initializer argument from UnregisteredDevice::new() and update its callers.

Makes sense to me. Both in-tree users had type Data = () anyway, and nothing uses the WorkItem/HasWork impls on drm::Device, so this is a pure simplification.

One small suggestion below.

Quoted 2 lines
rust/kernel/drm/device.rs +1 -1
-        release: Some(Device::<T>::release),
+        release: None,

[...]

Quoted 7 lines

#[repr(C)]
pub struct Device<T: drm::Driver, C: DeviceContext = Normal> {
dev: Opaque<bindings::drm_device>,
- data: T::Data,
pub(super) registration_data: UnsafeCell<NonNull<T::RegistrationData<'static>>>, _ctx: PhantomData<C>,
}

With release gone, nothing ever runs drop_in_place() on the Rust side of Device anymore. That's fine today since all remaining fields are trivially droppable, but it is now an implicit requirement on the
struct: anyone adding a field with a Drop impl (or a pinned field that needs a destructor) would silently leak it.

Could you add a short comment on the struct along the lines of "no release callback is installed, so no field of this struct may require dropping"? That way the next person adding a field knows they have to bring back the release callback.

Quoted 6 lines
Quoted 4 lines

(For what it's worth, I checked the lifetime-parameterized File series [1] against this: open_count/open_count_wq are both droppable without a destructor, and the two merge cleanly, so no ordering constraint there as far as I can tell.)

Instead of a comment, a const assertion of !needs_drop would be better.

Good idea

Alice

author maintainer
#4 lore raw
Quoted 1 line · Alice Ryhl

Good idea

Indeed, what do you think about this?

rust/kernel/drm/device.rs +5 -1
index 774db3acb15a..8c48dcfdecec 100644
@@ -158,7 +158,11 @@ const fn compute_features() -> u32 {
         open: Some(drm::File::<T::File>::open_callback),
         postclose: Some(drm::File::<T::File>::postclose_callback),
         unload: None,
-        release: None,
+        release: {
+            // The DRM core frees the device without running a Rust destructor.
+            const_assert!(!mem::needs_drop::<Device<T, Normal>>());
+            None
+        },
         master_set: None,
         master_drop: None,
         debugfs_init: None,
maintainer
#5 lore raw
Quoted 12 lines · Danilo Krummrich
Quoted 1 line · Alice Ryhl

Good idea

Indeed, what do you think about this?

rust/kernel/drm/device.rs +0 -0
index 774db3acb15a..8c48dcfdecec 100644
@@ -158,7 +158,11 @@ const fn compute_features() -> u32 {
         open: Some(drm::File::<T::File>::open_callback),
         postclose: Some(drm::File::<T::File>::postclose_callback),

rust/kernel/drm/device.rs const fn compute_features() -> u32 { · line 168

Quoted 10 lines
rust/kernel/drm/device.rs +5 -1
         unload: None,
-        release: None,
+        release: {
+            // The DRM core frees the device without running a Rust destructor.
+            const_assert!(!mem::needs_drop::<Device<T, Normal>>());
+            None
+        },
         master_set: None,
         master_drop: None,
         debugfs_init: None,

Looks good to me.

Alice

reviewer Reviewed-by on v1 1/1
#6 lore raw
In reply to Danilo Krummrich
Quoted 9 lines · Danilo Krummrich

Driver::Data is owned by the reference-counted drm::Device and can outlive device unregistration and driver unbind. Driver state belongs in RegistrationData, whose lifetime ends with registration teardown.

The intended model is that, after drm_dev_unplug(), surviving DRM objects (e.g. through remaining userspace handles) no longer require access to the driver state. Thus Driver::RegistrationData is available (and sufficient) for all DRM callbacks into the driver.

Quoted 6 lines

Remove Driver::Data, its storage and Deref implementation, and the unused workqueue implementations that depend on it. Drop the data initializer argument from UnregisteredDevice::new() and update its callers.

With the const assert added:

Quoted 7 lines
---
 drivers/gpu/drm/nova/driver.rs |   3 +-
 drivers/gpu/drm/tyr/driver.rs  |   3 +-
 rust/kernel/drm/device.rs      | 128 ++-------------------------------
 rust/kernel/drm/driver.rs      |   3 -
 rust/kernel/drm/gem/shmem.rs   |   6 +-
 5 files changed, 9 insertions(+), 134 deletions(-)
author maintainer Applied
#7 lore raw
In reply to Danilo Krummrich
Quoted 1 line · Danilo Krummrich

[PATCH] rust: drm: remove DRM device private data

Applied, thanks!

Branch: drm-rust-next
Tree: https://gitlab.freedesktop.org/drm/rust/kernel.git

[1/1] rust: drm: remove DRM device private data
commit: 0694e2390c5b

[ Assert !needs_drop() in drm_driver::release(). - Danilo ]

The patch will appear in the next linux-next integration (typically within 24 hours on weekdays).

The patch is queued up for the upcoming merge window for the next major kernel release.