rust: drm: remove DRM device private data
Subsystems (6)
Danilo Krummrich
maintainerPATCH 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
@@ -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
@@ -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
@@ -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
@@ -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
Quoted 9 lines · Danilo Krummrich
Driver::Datais owned by the reference-counteddrm::Deviceand 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. ThusDriver::RegistrationDatais 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 fromUnregisteredDevice::new()and update its callers.Signed-off-by Danilo Krummrich <[email protected]>
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
Gary Guo
reviewerQuoted 49 lines · Alice Ryhl
Quoted 9 lines · Danilo Krummrich
Driver::Datais owned by the reference-counteddrm::Deviceand 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. ThusDriver::RegistrationDatais 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 fromUnregisteredDevice::new()and update its callers.Signed-off-by Danilo Krummrich <[email protected]>Makes sense to me. Both in-tree users had
type Data = ()anyway, and nothing uses theWorkItem/HasWorkimpls ondrm::Device, so this is a pure simplification.Reviewed-by Alice Ryhl <[email protected]>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
releasegone, nothing ever runsdrop_in_place()on the Rust side ofDeviceanymore. 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 aDropimpl (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
releasecallback 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 thereleasecallback.
Quoted 4 lines
(For what it's worth, I checked the lifetime-parameterized
Fileseries [1] against this:open_count/open_count_wqare 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
Alice Ryhl
maintainerQuoted 51 lines · Gary Guo
Quoted 49 lines · Alice Ryhl
Quoted 9 lines · Danilo Krummrich
Driver::Datais owned by the reference-counteddrm::Deviceand 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. ThusDriver::RegistrationDatais 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 fromUnregisteredDevice::new()and update its callers.Signed-off-by Danilo Krummrich <[email protected]>Makes sense to me. Both in-tree users had
type Data = ()anyway, and nothing uses theWorkItem/HasWorkimpls ondrm::Device, so this is a pure simplification.Reviewed-by Alice Ryhl <[email protected]>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
releasegone, nothing ever runsdrop_in_place()on the Rust side ofDeviceanymore. 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 aDropimpl (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
releasecallback 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 thereleasecallback.
Quoted 6 lines
Quoted 4 lines
(For what it's worth, I checked the lifetime-parameterized
Fileseries [1] against this:open_count/open_count_wqare 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
Danilo Krummrich
maintainerQuoted 1 line · Alice Ryhl
Good idea
Indeed, what do you think about this?
rust/kernel/drm/device.rs +5 -1
@@ -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,
Alice Ryhl
maintainerQuoted 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@@ -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
-1unload: 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
Quoted 9 lines · Danilo Krummrich
Driver::Datais owned by the reference-counteddrm::Deviceand 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. ThusDriver::RegistrationDatais 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 fromUnregisteredDevice::new()and update its callers.Signed-off-by Danilo Krummrich <[email protected]>
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(-)
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.