lifetime-parameterized DRM File private data
Subsystems (6)
Danilo Krummrich
maintainerv2 cover lifetime-parameterized DRM File private data
DRM file private data is currently 'static, preventing drivers from borrowing registration-scoped resources in their per-file state. This series makes Driver::File lifetime-parameterized, so file data can borrow from RegistrationData.
File data is guaranteed to be dropped before registration data, either when the file is closed normally or when the device is unregistered, whichever comes first. To enforce this during unbind, Registration::drop() revokes all open file data and waits for its destruction before dropping registration data.
fops.open is wrapped in a RegistrationGuard (drm_dev_enter/exit) so that after drm_dev_unplug() all successfully opened files are accounted for. GEM open/close callbacks are similarly gated to prevent access to stale file data after unbind.
The series is based on drm-rust-next with [1] applied; a branch with the patches is available in [2].
[1] https://lore.kernel.org/all/[email protected]/
[2] https://git.kernel.org/pub/scm/linux/kernel/git/dakr/linux.git/log/?h=drm/file
Changes in v2:
- Replace ForLt with Driver::File<'a>; use inner_with() for file-data access and allow returning borrowed fields.
- Use Opaque<bindings::drm_file> during teardown.
- Drop file data outside filelist_mutex; also clean up file data on registration failure.
- Reject cross-device GEM handles.
- Clarify safety documentation and add inline annotations.
Danilo Krummrich (7):
rust: drm: rename Ioctl device context to Userspace
rust: drm: gem: gate open/close callbacks with RegistrationGuard
rust: drm: move file_operations from gem to device
rust: fs: add iminor() helper
rust: drm: wrap fops open with RegistrationGuard
rust: drm: return impl PinInit from DriverFile::open()
rust: drm: make Driver::File lifetime-parameterized
drivers/gpu/drm/nova/driver.rs | 2 +- drivers/gpu/drm/nova/file.rs | 16 +-- drivers/gpu/drm/nova/gem.rs | 8 +- drivers/gpu/drm/tyr/driver.rs | 2 +- drivers/gpu/drm/tyr/file.rs | 13 ++- rust/helpers/fs.c | 5 + rust/kernel/drm/device.rs | 141 ++++++++++++++++++++++---- rust/kernel/drm/driver.rs | 113 +++++++++++++++++++-- rust/kernel/drm/file.rs | 179 ++++++++++++++++++++++++--------- rust/kernel/drm/gem/mod.rs | 80 +++++++++------ rust/kernel/drm/gem/shmem.rs | 11 +- rust/kernel/drm/ioctl.rs | 18 ++-- rust/kernel/drm/mod.rs | 2 +- 13 files changed, 454 insertions(+), 136 deletions(-)
base-commit: cef0cd809e564cca9ac824a369c995ab7e0d16f3
Signature · 1 line
2.56.0
Danilo Krummrich
maintainerPATCH v2 1/7 rust: drm: rename Ioctl device context to Userspace
The Ioctl DeviceContext typestate represents a device that has been registered with userspace at some point. This context is not specific to ioctl dispatch; it applies equally to GEM handle callbacks, mmap, fdinfo, and any other operation triggered by userspace on a registered device.
Rename it to Userspace to accurately reflect its semantics.
--- rust/kernel/drm/device.rs | 21 ++++++++++++--------- rust/kernel/drm/ioctl.rs | 12 ++++++------ rust/kernel/drm/mod.rs | 2 +- 3 files changed, 19 insertions(+), 16 deletions(-)
rust/kernel/drm/device.rs +12 -9
@@ -79,12 +79,12 @@ macro_rules! drm_legacy_fields {/// /// - [`Normal`]: The general-purpose, reference-counted context. A [`Device`] in this context may /// or may not be registered with userspace. -/// - [`Ioctl`]: The device has been registered with userspace at some point; used in ioctl -/// dispatch context. +/// - [`Userspace`]: The device has been registered with userspace at some point; used in +/// callbacks triggered by userspace operations. /// - [`Registered`]: The device is currently registered with userspace and the parent bus device /// is bound. /// -/// Both `Device<T, Ioctl>` and `Device<T, Registered>` dereference to `Device<T>` ([`Normal`]), +/// Both `Device<T, Userspace>` and `Device<T, Registered>` dereference to `Device<T>` ([`Normal`]), /// so any method available on a [`Normal`] device is also available in the other contexts. pub trait DeviceContext :Sealed +Send +Sync + 'static {}
@@ -120,14 +120,17 @@ impl DeviceContext for Registered {}/// unregistering or already unregistered. `drm_dev_enter()` can guard against this, ensuring the /// device remains registered for the duration of the critical section. /// +/// This context is used for all callbacks triggered by userspace operations: ioctls, GEM handle +/// management, mmap, fdinfo, etc. +/// /// # Invariants /// /// A [`Device`] in this context has been registered with userspace via `drm_dev_register()` at /// some point. -pub struct Ioctl ; +pub struct Userspace ; -impl Sealed for Ioctl {} -impl DeviceContext for Ioctl {} +impl Sealed for Userspace {} +impl DeviceContext for Userspace {}/// A [`Device`] which is known at compile-time to be unregistered with userspace. ///
@@ -344,7 +347,7 @@ pub(crate) unsafe fn assume_ctx<NewCtx: DeviceContext>(&self) -> &Device<T, NewC } } -impl <T : drm::Driver >Device <T ,Ioctl > { +impl <T : drm::Driver >Device <T ,Userspace > {/// Guard against the parent bus device being unbound. /// /// Returns a [`RegistrationGuard`] if the device has not been unplugged, [`None`] otherwise.
@@ -467,12 +470,12 @@ fn deref(&self) -> &Self::Target { } } -impl <T : drm::Driver >Deref for Device <T ,Ioctl > { +impl <T : drm::Driver >Deref for Device <T ,Userspace > {type Target =Device <T >;#[inline] fn deref (&self) -> &Self ::Target { -// SAFETY: The caller holds a `Device<T, Ioctl>`, which guarantees all invariants +// SAFETY: The caller holds a `Device<T, Userspace>`, which guarantees all invariants // of the weaker `Normal` context. unsafe { self.assume_ctx () } }
rust/kernel/drm/ioctl.rs +6 -6
@@ -71,14 +71,14 @@ pub mod internal {pub use bindings::drm_file;pub use bindings::drm_ioctl_desc; -/// Cast an [`Ioctl`] DRM device pointer to [`Registered`], preserving the driver type +/// Cast a [`Userspace`] DRM device pointer to [`Registered`], preserving the driver type /// parameter `T`. /// /// Used by [`declare_drm_ioctls!`] to anchor type inference. #[doc(hidden)] #[inline] pub const fn __dev_ctx_cast <T :crate ::drm::Driver >( - ptr: *const crate ::drm::Device <T ,crate ::drm::Ioctl >, + ptr: *const crate ::drm::Device <T ,crate ::drm::Userspace >, ) -> *const crate ::drm::Device <T ,crate ::drm::Registered > { ptr.cast () }
@@ -144,14 +144,14 @@ macro_rules! declare_drm_ioctls {// - The DRM device must have been registered when we're called through // an IOCTL. // -// INVARIANT: The `Ioctl` context requires that the device has been -// registered via `drm_dev_register()` at some point; the DRM core -// guarantees this for ioctl dispatch callbacks. +// INVARIANT: The `Userspace` context requires that the device has +// been registered via `drm_dev_register()` at some point; the DRM +// core guarantees this for ioctl dispatch callbacks. // // FIXME: Currently there is nothing enforcing that the types of the // dev/file match the current driver these ioctls are being declared // for, and it's not clear how to enforce this within the type system. -let dev: &$crate::drm::device::Device <_ , $crate::drm::Ioctl > = +let dev: &$crate::drm::device::Device <_ , $crate::drm::Userspace > = $crate::drm::device::Device ::from_raw (raw_dev);// Type-inference anchor: the closure is never called but ties `dev`'s
rust/kernel/drm/mod.rs +1 -1
@@ -11,11 +11,11 @@pub use self::device::Device ;pub use self::device::DeviceContext ; -pub use self::device::Ioctl ;pub use self::device::Normal ;pub use self::device::Registered ;pub use self::device::RegistrationGuard ;pub use self::device::UnregisteredDevice ; +pub use self::device::Userspace ;pub use self::driver::Driver ;pub use self::driver::DriverInfo ;pub use self::driver::Registration ;
Signature · 1 line
2.56.0
Danilo Krummrich
maintainerPATCH v2 2/7 rust: drm: gem: gate open/close callbacks with RegistrationGuard
In reply to Danilo KrummrichWrap the GEM object open and close callbacks with a RegistrationGuard (drm_dev_enter / drm_dev_exit) to ensure the driver callbacks only run while the parent bus device is bound.
If the device has been unbound, open returns -ENODEV and close silently returns. This prevents driver code from accessing device resources after unbind and is a prerequisite for making drm::Driver::File lifetime-parameterized, since GEM callbacks receive a &drm::File that could otherwise be used to access invalidated file private data.
--- rust/kernel/drm/gem/mod.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
rust/kernel/drm/gem/mod.rs +16 -0
@@ -128,6 +128,14 @@ extern "C" fn open_callback<T: DriverObject>( raw_obj: *mut bindings::drm_gem_object, raw_file: *mut bindings::drm_file, ) -> core::ffi::c_int { +// SAFETY: `raw_obj` is a valid pointer to a `struct drm_gem_object` with a valid `dev`. + let dev: &drm::Device<T::Driver, drm::Userspace> = + unsafe { drm::Device::from_raw((*raw_obj).dev) }; + +let Some (_guard) = dev.registration_guard ()else { +return ENODEV .to_errno (); + }; +// SAFETY: `open_callback` is only ever called with a valid pointer to a `struct drm_file`. let file =unsafe {DriverFile ::<T >::from_raw (raw_file) };
@@ -148,6 +156,14 @@ extern "C" fn close_callback<T: DriverObject>( raw_obj: *mut bindings::drm_gem_object, raw_file: *mut bindings::drm_file, ) { +// SAFETY: `raw_obj` is a valid pointer to a `struct drm_gem_object` with a valid `dev`. +let dev: &drm::Device <T ::Driver , drm::Userspace > = +unsafe { drm::Device ::from_raw ((*raw_obj).dev) }; + +let Some (_guard) = dev.registration_guard ()else { +return ; + }; +// SAFETY: `open_callback` is only ever called with a valid pointer to a `struct drm_file`. let file =unsafe {DriverFile ::<T >::from_raw (raw_file) };
Signature · 1 line
2.56.0
Danilo Krummrich
maintainerPATCH v2 3/7 rust: drm: move file_operations from gem to device
In reply to Danilo KrummrichMove the file_operations definition from drm::gem::create_fops() into UnregisteredDevice in drm::device. This is the file_operations of the DRM minor device, set through the drm_driver struct. It is not specific to GEM.
--- rust/kernel/drm/device.rs | 23 ++++++++++++++++++++--- rust/kernel/drm/gem/mod.rs | 20 -------------------- 2 files changed, 20 insertions(+), 23 deletions(-)
rust/kernel/drm/device.rs +20 -3
@@ -195,11 +195,28 @@ const fn compute_features() -> u32 { driver_features: Self::compute_features(), ioctls: T::IOCTLS.as_ptr(), num_ioctls: T::IOCTLS.len() as i32, - fops: &Self::GEM_FOPS, + fops: &Self::FOPS, }; -const GEM_FOPS : bindings::file_operations = - drm::gem::create_fops (crate ::module::this_module ::<T ::OwnerModule >().as_ptr ()); +const FOPS : bindings::file_operations = { +let mut fops: bindings::file_operations = pin_init::zeroed (); + + fops.owner =crate ::module::this_module ::<T ::OwnerModule >().as_ptr (); + fops.open =Some (bindings::drm_open); + fops.release =Some (bindings::drm_release); + fops.unlocked_ioctl =Some (bindings::drm_ioctl); +#[cfg( CONFIG_COMPAT )] + { + fops.compat_ioctl =Some (bindings::drm_compat_ioctl); + } + fops.poll =Some (bindings::drm_poll); + fops.read =Some (bindings::drm_read); + fops.llseek =Some (bindings::noop_llseek); + fops.mmap =Some (bindings::drm_gem_mmap); + fops.fop_flags = bindings::FOP_UNSIGNED_OFFSET ; + + fops + };/// Create a new `UnregisteredDevice` for a `drm::Driver`. ///
rust/kernel/drm/gem/mod.rs +0 -20
@@ -388,23 +388,3 @@ impl<T: DriverObject, Ctx: DeviceContext> AllocImpl for Object<T, Ctx> { dumb_map_offset: None, }; } - -pub (super )const fn create_fops (owner: *mut bindings::module ) -> bindings::file_operations { -let mut fops: bindings::file_operations = pin_init::zeroed (); - - fops.owner = owner; - fops.open =Some (bindings::drm_open); - fops.release =Some (bindings::drm_release); - fops.unlocked_ioctl =Some (bindings::drm_ioctl); -#[cfg( CONFIG_COMPAT )] - { - fops.compat_ioctl =Some (bindings::drm_compat_ioctl); - } - fops.poll =Some (bindings::drm_poll); - fops.read =Some (bindings::drm_read); - fops.llseek =Some (bindings::noop_llseek); - fops.mmap =Some (bindings::drm_gem_mmap); - fops.fop_flags = bindings::FOP_UNSIGNED_OFFSET ; - - fops -}
Signature · 1 line
2.56.0
Danilo Krummrich
maintainerPATCH v2 4/7 rust: fs: add iminor() helper
In reply to Danilo KrummrichAdd a Rust helper for iminor(), which extracts the minor number from a struct inode. This is a static inline function in the C headers, so bindgen does not generate a binding for it.
--- rust/helpers/fs.c | 5 +++++ 1 file changed, 5 insertions(+)
rust/helpers/fs.c +5 -0
@@ -10,3 +10,8 @@ __rust_helper struct file *rust_helper_get_file(struct file *f) {return get_file (f); } + +__rust_helper unsigned int rust_helper_iminor(const struct inode *inode) +{ +return iminor (inode); +}
Signature · 1 line
2.56.0
Danilo Krummrich
maintainerPATCH v2 5/7 rust: drm: wrap fops open with RegistrationGuard
In reply to Danilo KrummrichAdd a Minor abstraction with RAII release and a fops_open() wrapper that holds a RegistrationGuard (drm_dev_enter / drm_dev_exit) across the entire drm_open() call.
This guarantees that drm_dev_unplug() in Registration::drop() waits for the full open sequence to complete, so all files are visible in the filelist when iterating for cleanup.
--- rust/kernel/drm/device.rs | 63 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-)
rust/kernel/drm/device.rs +62 -1
@@ -42,6 +42,42 @@ }, }; +/// A reference to a `struct drm_minor` with RAII release. +struct Minor (NonNull <bindings::drm_minor >); + +// Methods use `#[inline(never)]` to prevent the unexported `drm_minor_acquire()` / +// `drm_minor_release()` symbols from being inlined into driver modules. +impl Minor { +/// Acquire a minor by ID. Increments the underlying device's refcount. +#[inline(never)] +fn acquire (minor_id:u32 ) ->Result <Self > { +// SAFETY: `drm_minors_xa` is a valid global xarray; any `minor_id` is safe to +// look up (returns ERR_PTR on failure). +let ptr = +unsafe { bindings::drm_minor_acquire (&raw mut bindings::drm_minors_xa, minor_id) }; +Ok (Self (NonNull ::new (from_err_ptr (ptr)?).ok_or (ENODEV )?)) + } + +/// Returns a reference to the DRM device for this minor. +/// +/// # Safety +/// +/// The caller must ensure that the minor belongs to a `Device<T>`. +unsafe fn device <T : drm::Driver >(&self) -> &Device <T ,Userspace > { +// SAFETY: The minor is valid (from `drm_minor_acquire()`) and `minor->dev` +// is a valid `drm_device`. The caller guarantees it is a `Device<T>`. +unsafe {Device ::from_raw ((*self.0 .as_ptr ()).dev) } + } +} + +impl Drop for Minor { +#[inline(never)] +fn drop (&mut self) { +// SAFETY: `self.0` came from `drm_minor_acquire()` and has not been released yet. +unsafe { bindings::drm_minor_release (self.0 .as_ptr ()) } + } +} +#[cfg( CONFIG_DRM_LEGACY )] macro_rules! drm_legacy_fields { ( $($field:ident: $val:expr),* $(,)? ) => {
@@ -198,11 +234,36 @@ const fn compute_features() -> u32 { fops: &Self::FOPS, }; +/// Wrapper for `fops.open` that holds a [`RegistrationGuard`] across the entire `drm_open()` +/// call. This guarantees that `drm_dev_unplug()` in `Registration::drop()` waits for the full +/// open sequence. +extern "C" fn fops_open (inode: *mut bindings::inode , filp: *mut bindings::file ) ->c_int { +let f = || ->Result <c_int > { +// SAFETY: `inode` is valid. +let minor_id =unsafe { bindings::iminor (inode) }; +let minor =Minor ::acquire (minor_id)?; + +// SAFETY: `fops_open` is only installed for devices of type `T` (via `FOPS`). +let _guard = (unsafe { minor.device ::<T >() }) + .registration_guard () + .ok_or (ENODEV )?; + +// SAFETY: `inode` and `filp` are valid. The RegistrationGuard ensures the entire +// `drm_open()` runs within the SRCU critical section. +Ok (unsafe { bindings::drm_open (inode, filp) }) + }; + +match f () { +Ok (ret) => ret, +Err (e) => e.to_errno (), + } + } +const FOPS : bindings::file_operations = {let mut fops: bindings::file_operations = pin_init::zeroed (); fops.owner =crate ::module::this_module ::<T ::OwnerModule >().as_ptr (); - fops.open =Some (bindings::drm_open); + fops.open =Some (Self ::fops_open); fops.release =Some (bindings::drm_release); fops.unlocked_ioctl =Some (bindings::drm_ioctl);#[cfg( CONFIG_COMPAT )]
Signature · 1 line
2.56.0
Danilo Krummrich
maintainerPATCH v2 6/7 rust: drm: return impl PinInit from DriverFile::open()
In reply to Danilo KrummrichChange DriverFile::open() to return impl PinInit<Self, Error> instead of Result<Pin<KBox<Self>>>, consistent with how bus device private data works.
Drivers no longer need to allocate a Pin<KBox<_>> themselves; they just return an initializer and the subsystem takes care of the allocation.
--- drivers/gpu/drm/nova/file.rs | 5 ++--- drivers/gpu/drm/tyr/file.rs | 4 ++-- rust/kernel/drm/file.rs | 5 +++-- rust/kernel/drm/gem/shmem.rs | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-)
drivers/gpu/drm/nova/file.rs +2 -3
@@ -7,7 +7,6 @@ };use crate ::gem::NovaObject ;use kernel::{ - alloc::flags::*, drm::{ self, gem::BaseObject ,
@@ -103,8 +102,8 @@ fn write_info<T: AsBytes>(info: &mut uapi::drm_nova_info, value: T) -> Result {impl drm::file::DriverFile for File {type Driver =NovaDriver ; -fn open (_dev: &NovaDevice ) ->Result <Pin <KBox <Self >>> { -Ok (KBox ::new (Self ,GFP_KERNEL )?.into ()) +fn open (_dev: &NovaDevice ) ->impl PinInit <Self ,Error > { +Ok (Self ) } }
drivers/gpu/drm/tyr/file.rs +2 -2
@@ -25,8 +25,8 @@ pub(crate) struct TyrDrmFileData {}impl drm::file::DriverFile for TyrDrmFileData {type Driver =TyrDrmDriver ; -fn open (_dev: &drm::Device <Self ::Driver >) ->Result <Pin <KBox <Self >>> { -KBox ::try_pin_init (try_pin_init! (Self {}),GFP_KERNEL ) +fn open (_dev: &drm::Device <Self ::Driver >) ->impl PinInit <Self ,Error > { +Ok (Self {}) } }
rust/kernel/drm/file.rs +3 -2
@@ -18,7 +18,7 @@ pub trait DriverFile {type Driver : drm::Driver ;/// Open a new file (called when a client opens the DRM device). -fn open (device: &drm::Device <Self ::Driver >) ->Result <Pin <KBox <Self >>>; +fn open (device: &drm::Device <Self ::Driver >) ->impl PinInit <Self ,Error >; }/// An open DRM File.
@@ -70,7 +70,8 @@ pub(crate) extern "C" fn open_callback(// SAFETY: `raw_file` is a valid pointer to a `struct drm_file`. let file =unsafe {File ::<T >::from_raw (raw_file) }; - let inner = match T::open(drm) { +let init =T ::open (drm); + let inner = match KBox::try_pin_init(init, GFP_KERNEL) { Err(e) => { return e.to_errno(); }
rust/kernel/drm/gem/shmem.rs +2 -2
@@ -604,8 +604,8 @@ struct KunitObject {}impl drm::file::DriverFile for KunitFile {type Driver =KunitDriver ; -fn open (_dev: &drm::Device <KunitDriver >) ->Result <Pin <KBox <Self >>> { -Ok (KBox ::new (Self ,GFP_KERNEL )?.into ()) +fn open (_dev: &drm::Device <KunitDriver >) ->impl PinInit <Self ,Error > { +Ok (Self ) } }
Signature · 1 line
2.56.0
Danilo Krummrich
maintainerPATCH v2 7/7 rust: drm: make Driver::File lifetime-parameterized
In reply to Danilo KrummrichMake the DRM file private data lifetime-parameterized, allowing drivers to borrow from RegistrationData in their per-file data.
Introduce DriverFile<'a> as a lifetime-parameterized trait that receives both the device and registration data in open(). Parametrize File<D> on the driver type rather than the file data type, deriving the concrete file type through Driver::File<'a>.
Add inner_with() to access the driver file data from a File<D> reference.
Ensure file data is always dropped before registration data:
- In Registration::drop(), detach driver_priv for all open files under filelist_mutex, then drop the detached data after releasing the mutex and wait for file-data destruction in concurrent postclose_callback() calls via an open_count / WaitQueue pair on drm::Device.
- In postclose_callback(), skip the drop if driver_priv has already been NULLed by the filelist iteration. The NULL write is visible through the filelist_mutex acquire/release chain in drm_close_helper().
--- drivers/gpu/drm/nova/driver.rs | 2 +- drivers/gpu/drm/nova/file.rs | 13 ++- drivers/gpu/drm/nova/gem.rs | 8 +- drivers/gpu/drm/tyr/driver.rs | 2 +- drivers/gpu/drm/tyr/file.rs | 11 +- rust/kernel/drm/device.rs | 36 +++++-- rust/kernel/drm/driver.rs | 113 +++++++++++++++++++-- rust/kernel/drm/file.rs | 180 ++++++++++++++++++++++++--------- rust/kernel/drm/gem/mod.rs | 44 ++++++-- rust/kernel/drm/gem/shmem.rs | 9 +- rust/kernel/drm/ioctl.rs | 6 +- 11 files changed, 330 insertions(+), 94 deletions(-)
drivers/gpu/drm/nova/driver.rs +1 -1
@@ -90,7 +90,7 @@ fn probe<'bound>(impl drm::Driver for NovaDriver {type Data = ();type RegistrationData <'a> =DrmRegData <'a>; -type File =File ; +type File <'a> =File ;type Object = gem::Object <NovaObject >;type ParentDevice <Ctx :DeviceContext > = auxiliary::Device <Ctx >;
drivers/gpu/drm/nova/file.rs +8 -5
@@ -99,10 +99,13 @@ fn write_info<T: AsBytes>(info: &mut uapi::drm_nova_info, value: T) -> Result {Ok (()) } -impl drm::file::DriverFile for File { +impl drm::file::DriverFile <'_>for File {type Driver =NovaDriver ; -fn open (_dev: &NovaDevice ) ->impl PinInit <Self ,Error > { +fn open ( + _device: &NovaDevice <Registered >, + _reg_data: &DrmRegData <'_>, + ) ->impl PinInit <Self ,Error > {Ok (Self ) } }
@@ -113,7 +116,7 @@ pub(crate) fn gem_create( dev: &NovaDevice<Registered>, _reg_data: &DrmRegData<'_>, req: &mut uapi::drm_nova_gem_create, - file: &drm::File<File>, + file: &drm::File<NovaDriver>, ) -> Result<u32> {let obj =NovaObject ::new (dev, req.size.try_into ()?)?;
@@ -127,7 +130,7 @@ pub(crate) fn gem_info( _dev: &NovaDevice<Registered>, _reg_data: &DrmRegData<'_>, req: &mut uapi::drm_nova_gem_info, - file: &drm::File<File>, + file: &drm::File<NovaDriver>, ) -> Result<u32> {let bo =NovaObject ::lookup_handle (file, req.handle)?;
@@ -141,7 +144,7 @@ pub(crate) fn info( _dev: &NovaDevice<Registered>, reg_data: &DrmRegData<'_>, info: &mut uapi::drm_nova_info, - _file: &drm::File<File>, + _file: &drm::File<NovaDriver>, ) -> Result<u32> {match info.id { uapi::DRM_NOVA_INFO_GPU =>write_info (info,GpuInfo ::new (reg_data)?)?,
drivers/gpu/drm/nova/gem.rs +4 -4
@@ -11,9 +11,9 @@ sync::aref::ARef, }; -use crate ::{ - driver::{NovaDevice ,NovaDriver }, - file::File , +use crate ::driver::{ +NovaDevice , +NovaDriver ,// };/// GEM Object inner driver data
@@ -43,7 +43,7 @@ pub(crate) fn new(dev: &NovaDevice, size: usize) -> Result<ARef<gem::Object<Self/// Look up a GEM object handle for a `File` and return an `ObjectRef` for it. #[inline] pub (crate )fn lookup_handle ( - file: &drm::File <File >, + file: &drm::File <NovaDriver >, handle:u32 , ) ->Result <ARef <gem::Object <Self >>> { gem::Object ::lookup_handle (file, handle)
drivers/gpu/drm/tyr/driver.rs +1 -1
@@ -209,7 +209,7 @@ fn drop(self: Pin<&mut Self>) {}impl drm::Driver for TyrDrmDriver {type Data = ();type RegistrationData <'drm> =TyrDrmRegistrationData <'drm>; -type File =TyrDrmFileData ; +type File <'a> =TyrDrmFileData ;type Object =Bo ;type ParentDevice <Ctx :DeviceContext > = platform::Device <Ctx >;
drivers/gpu/drm/tyr/file.rs +7 -4
@@ -19,13 +19,16 @@#[pin_data] pub (crate )struct TyrDrmFileData {} -/// Convenience type alias for our DRM `File` type -pub (crate )type TyrDrmFile = drm::file::File <TyrDrmFileData >; +/// Convenience type alias for our DRM `File` type. +pub (crate )type TyrDrmFile = drm::file::File <TyrDrmDriver >; -impl drm::file::DriverFile for TyrDrmFileData { +impl drm::file::DriverFile <'_>for TyrDrmFileData {type Driver =TyrDrmDriver ; -fn open (_dev: &drm::Device <Self ::Driver >) ->impl PinInit <Self ,Error > { +fn open ( + _device: &TyrDrmDevice <Registered >, + _reg_data: &TyrDrmRegistrationData <'_>, + ) ->impl PinInit <Self ,Error > {Ok (Self {}) } }
rust/kernel/drm/device.rs +30 -6
@@ -15,9 +15,13 @@ }, error::from_err_ptr, prelude::*, - sync::aref::{ - ARef, - AlwaysRefCounted, // + sync::{ + aref::{ + ARef, + AlwaysRefCounted, // + }, + atomic::Atomic, + WaitQueue, }, types::{NotThreadSafe ,
@@ -190,7 +194,10 @@ fn deref(&self) -> &Self::Target { } } -impl <T : drm::Driver >UnregisteredDevice <T > { +impl <T : drm::Driver >UnregisteredDevice <T > +where +for <'a>T ::File <'a>: drm::file::DriverFile <'a,Driver =T >, +{const fn compute_features () ->u32 {let mut features = drm::driver::FEAT_GEM ;
@@ -203,8 +210,8 @@ const fn compute_features() -> u32 {const VTABLE : bindings::drm_driver =drm_legacy_fields! { load:None , - open:Some (drm::File ::<T ::File >::open_callback), - postclose:Some (drm::File ::<T ::File >::postclose_callback), + open:Some (drm::File ::<T >::open_callback), + postclose:Some (drm::File ::<T >::postclose_callback), unload:None , release:Some (Device ::<T >::release), master_set:None ,
@@ -333,6 +340,21 @@ pub fn new(// SAFETY: `raw_drm` is valid; no concurrent access before registration. unsafe { (*raw_drm.as_ptr ()).registration_data =UnsafeCell ::new (NonNull ::dangling ()) }; +// SAFETY: `raw_drm` is valid; no concurrent access before registration. +unsafe { (*raw_drm.as_ptr ()).open_count =Atomic ::new (0 ) }; + +// SAFETY: +// - `raw_drm` is valid and `open_count_wq` is uninitialized; no concurrent access +// before registration. +// - The field is pinned because the Device is pinned (refcounted, allocated by +// `__drm_dev_alloc()`, never moved). +unsafe { + pin_init::raw_init ( + &raw mut (*raw_drm.as_ptr ()).open_count_wq, +crate ::new_waitqueue!("drm_open_count" ), + ) + }; +// SAFETY: The reference count is one, and now we take ownership of that reference as a // `drm::Device`. // INVARIANT: We just created the device above, but have yet to call `drm_dev_register`.
@@ -357,6 +379,8 @@ 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>>>, + pub(super) open_count: Atomic<i32>, + pub(super) open_count_wq: WaitQueue, _ctx: PhantomData<C>, }
rust/kernel/drm/driver.rs +106 -7
144 lines not on this page · raw message
rust/kernel/drm/file.rs +130 -50
226 lines not on this page · raw message
rust/kernel/drm/gem/mod.rs +33 -11
86 lines not on this page · raw message
rust/kernel/drm/gem/shmem.rs +6 -3
@@ -601,10 +601,13 @@ struct KunitObject {} desc: c"Kunit", }; -impl drm::file::DriverFile for KunitFile { +impl drm::file::DriverFile <'_>for KunitFile {type Driver =KunitDriver ; -fn open (_dev: &drm::Device <KunitDriver >) ->impl PinInit <Self ,Error > { +fn open ( + _device: &drm::Device <KunitDriver , drm::Registered >, + _reg_data: &(), + ) ->impl PinInit <Self ,Error > {Ok (Self ) } }
@@ -626,7 +629,7 @@ fn new(impl drm::Driver for KunitDriver {type Data =KunitData ;type RegistrationData <'a> = (); -type File =KunitFile ; +type File <'a> =KunitFile ;type Object =Object <KunitObject >;type ParentDevice <Ctx : device::DeviceContext > = faux::Device <Ctx >;
rust/kernel/drm/ioctl.rs +4 -2
@@ -97,7 +97,7 @@ pub const fn __dev_ctx_cast<T: crate::drm::Driver>(/// fn foo(device: &kernel::drm::Device<Self, kernel::drm::Registered>, /// reg_data: &Self::RegistrationData<'_>, /// data: &mut uapi::argument_type, -/// file: &kernel::drm::File<Self::File>, +/// file: &kernel::drm::File<Self>, /// ) -> Result<u32> /// ``` /// where `Self` is the drm::drv::Driver implementation these ioctls are being declared within.
@@ -192,7 +192,9 @@ macro_rules! declare_drm_ioctls {// There is no concurrent access or aliasing; no other references // to this object exist during this call. let data = unsafe { &mut *(raw_data.cast::<$crate::uapi::$struct>()) }; -// SAFETY: This is just the DRM file structure +// SAFETY: +// - The DRM core provides a valid file for this device. +// - `guard` is held for the entire use of the file reference. let file =unsafe { $crate::drm::File ::from_raw (raw_file) }; match guard.registration_data_with(|reg_data| {
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v2 7/7] rust: drm: make Driver::File lifetime-parameterized
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] rust: drm: missing Send + Sync bounds on Driver::File and DriverFile --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] rust: drm: missing Send + Sync bounds on Driver::File and DriverFile
--
commit 229740f5d6f5f9093208d976d2ea98edc170b6f8
Author: Danilo Krummrich <[email protected]>
rust: drm: make Driver::File lifetime-parameterized
This commit lifetime-parameterizes Driver::File and DriverFile so per-file driver data can borrow from RegistrationData, and drops open file data before registration teardown completes.
Quoted 4 lines
rust/kernel/drm/driver.rs +0
-0
[ ... ]
Quoted 17 lines
rust/kernel/drm/driver.rs +8
-2@@ -120,8 +132,14 @@ pub trait Driver {/// The type used to manage memory for this driver. type Object :AllocImpl ; -/// The type used to represent a DRM File (client) -type File : drm::file::DriverFile ; +/// The type used to represent a DRM File (client). +/// +/// File data may borrow from [`RegistrationData`](Driver::RegistrationData). File data is +/// guaranteed to be dropped before registration data, either when the file is closed or +/// when the device is unregistered, whichever comes first. +/// +/// Drivers implement [`DriverFile`](drm::file::DriverFile) for their file data type. +type File <'a>: 'a;/// The bus device type of the parent device that the DRM device is associated with. type ParentDevice <Ctx : device::DeviceContext >: device::AsBusDevice <Ctx >;
[ ... ]
Quoted 5 lines
rust/kernel/drm/file.rs +0
-0@@ -8,36 +8,66 @@
[ ... ]
Quoted 8 lines
/// Trait that must be implemented by DRM drivers to represent a DRM File (a client instance).
rust/kernel/drm/file.rs +4
-1-pub trait DriverFile { +/// +/// The lifetime `'a` allows the file data to borrow from +/// [`RegistrationData`](drm::Driver::RegistrationData). +pub trait DriverFile <'a>:Sized {/// The parent `Driver` implementation for this `DriverFile`. type Driver : drm::Driver ;
[ ... ]
Quoted 4 lines
rust/kernel/drm/file.rs +0
-0@@ -45,60 +75,110 @@ pub(super) fn as_raw(&self) -> *mut bindings::drm_file { self.0 .get () }
[ ... ]
Quoted 13 lines
rust/kernel/drm/file.rs +13
-0+/// Access the driver file data through a closure. +/// +/// The closure receives a pinned reference to the file data. +/// +/// The outer reference carries the `&self` lifetime while the inner type carries the HRTB +/// lifetime `'a`, implying `'a` outlives `&self`. This allows the closure to coerce covariant +/// sub-fields (e.g. `&'a T` to the caller's lifetime) and return them directly in `R`. +#[inline] +pub fn inner_with <'this,R ,F >(&'this self, f:F ) ->R +where +F :for <'a>FnOnce (Pin <&'thisD ::File <'a>>) ->R , + { +// SAFETY: By the type invariants, `self.as_raw()` points to a valid `drm_file`.
rust/kernel/drm/file.rs pub(super) fn as_raw(&self) -> *mut bindings::drm_file {
Quoted 10 lines
rust/kernel/drm/file.rs +10
-0+let ptr: *const FileData <_ > =unsafe { (*self.as_raw ()).driver_priv.cast_const ().cast () }; + +// SAFETY: +// - The type invariants keep this file's pinned `FileData` allocation valid for `'this`. +// - The `data` field is structurally pinned. +// - The HRTB `for<'a>` prevents the caller from choosing a concrete lifetime, so shortening +// is sound regardless of variance. +let data:Pin <&'thisD ::File <'this>> =unsafe {Pin ::new_unchecked (&(*ptr).data) }; +f (data) + }
[Severity: Medium]
Should Driver::File<'a> and DriverFile<'a> require Send + Sync, like Driver::Data and Driver::RegistrationData<'a>?
Without those bounds, a driver can store non-Sync or non-Send types such as Cell or RefCell in its per-file data in safe Rust. Multiple threads sharing a file descriptor can invoke ioctls or GEM callbacks concurrently on the same struct drm_file and obtain simultaneous shared references to D::File<'_> through File::inner_with() (requiring Sync), while File::postclose_callback() or Registration::drop_file_data() can drop the FileData allocation on a different thread than the one that opened the file (requiring Send), causing a data race.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Alice Ryhl
maintainerRe: [PATCH v2 7/7] rust: drm: make Driver::File lifetime-parameterized
In reply to Danilo Krummrichrust/kernel/drm/driver.rs pub unsafe fn new<E>( · line 299
Quoted 7 lines · Danilo Krummrich
rust/kernel/drm/driver.rs +7
-0+// Run driver destructors without `filelist_mutex`. Take each link before dropping its +// allocation to avoid recursively dropping the entire chain. +while let Some (mut data) = files { + files = data.as_mut ().project ().next.take (); +drop (data); + drm.open_count.fetch_sub (1 ,Release ); + }
Instead of transferring all the files to another linked list, how about taking them off the file list one-by-one? If you release the mutex in between dropping each file, then we still avoid deadlocks if the file private data destructor takes the lock.
Alice
Danilo Krummrich
maintainerRe: [PATCH v2 7/7] rust: drm: make Driver::File lifetime-parameterized
Quoted 9 lines · Alice Ryhl
rust/kernel/drm/driver.rs pub unsafe fn new<E>( · line 299
Quoted 7 lines · Danilo Krummrich
rust/kernel/drm/driver.rs +7
-0+// Run driver destructors without `filelist_mutex`. Take each link before dropping its +// allocation to avoid recursively dropping the entire chain. +while let Some (mut data) = files { + files = data.as_mut ().project ().next.take (); +drop (data); + drm.open_count.fetch_sub (1 ,Release ); + }
Quoted 4 lines
Instead of transferring all the files to another linked list, how about taking them off the file list one-by-one? If you release the mutex in between dropping each file, then we still avoid deadlocks if the file private data destructor takes the lock.
I did consider this, and technically it would (currently) work with list_del_init(), but I did not like it because it would be a very subtle change in semantics from the perspective of the C code. (I.e. for C drivers the list entry would remain intact after this callback, whereas for Rust drivers it would be removed.)
Actually, I did hesitate to move drop() out of the filelist_mutex protected path at all because I don't think there's a way this could actually ever produce a lock inversion, but the "move to a separate list" approach was pretty simple and very few code so I thought it's worth ruling it out anyway.
(In any case, I'll soon start working on the C code to implement a common lifecycle scheme. Once we have that, the Rust code becomes even more straight foward.)
Re: [PATCH v2 7/7] rust: drm: make Driver::File lifetime-parameterized
Quoted 22 lines · Danilo Krummrich
Quoted 9 lines · Alice Ryhl
rust/kernel/drm/driver.rs pub unsafe fn new<E>( · line 299
Quoted 7 lines · Danilo Krummrich
rust/kernel/drm/driver.rs +7
-0+// Run driver destructors without `filelist_mutex`. Take each link before dropping its +// allocation to avoid recursively dropping the entire chain. +while let Some (mut data) = files { + files = data.as_mut ().project ().next.take (); +drop (data); + drm.open_count.fetch_sub (1 ,Release ); + }Quoted 4 lines
Instead of transferring all the files to another linked list, how about taking them off the file list one-by-one? If you release the mutex in between dropping each file, then we still avoid deadlocks if the file private data destructor takes the lock.
I did consider this, and technically it would (currently) work with
list_del_init(), but I did not like it because it would be a very subtle change in semantics from the perspective of the C code. (I.e. for C drivers the list entry would remain intact after this callback, whereas for Rust drivers it would be removed.)
Quoted 8 lines
Actually, I did hesitate to move
drop()out of thefilelist_mutexprotected path at all because I don't think there's a way this could actually ever produce a lock inversion, but the "move to a separate list" approach was pretty simple and very few code so I thought it's worth ruling it out anyway.(In any case, I'll soon start working on the C code to implement a common lifecycle scheme. Once we have that, the Rust code becomes even more straight foward.)
As per discussion on chat, my suggestion doesn't quite work. The point of moving to a separate list is that we don't want to modify the existing file list.
Alice
Re: [PATCH v2 1/7] rust: drm: rename Ioctl device context to Userspace
In reply to Danilo KrummrichQuoted 11 lines · Danilo Krummrich
The Ioctl DeviceContext typestate represents a device that has been registered with userspace at some point. This context is not specific to ioctl dispatch; it applies equally to GEM handle callbacks, mmap, fdinfo, and any other operation triggered by userspace on a registered device.
Rename it to Userspace to accurately reflect its semantics.
Reviewed-by Alice Ryhl <[email protected]>Reviewed-by Daniel Almeida <[email protected]>Signed-off-by Danilo Krummrich <[email protected]>
Quoted 5 lines
--- rust/kernel/drm/device.rs | 21 ++++++++++++--------- rust/kernel/drm/ioctl.rs | 12 ++++++------ rust/kernel/drm/mod.rs | 2 +- 3 files changed, 19 insertions(+), 16 deletions(-)
Re: [PATCH v2 2/7] rust: drm: gem: gate open/close callbacks with RegistrationGuard
In reply to Danilo KrummrichQuoted 10 lines · Danilo Krummrich
Wrap the GEM object open and close callbacks with a RegistrationGuard (
drm_dev_enter/drm_dev_exit) to ensure the driver callbacks only run while the parent bus device is bound.If the device has been unbound, open returns
-ENODEVand close silently returns. This prevents driver code from accessing device resources after unbind and is a prerequisite for makingdrm::Driver::Filelifetime-parameterized, since GEM callbacks receive a&drm::Filethat could otherwise be used to access invalidated file private data.
Quoted 3 lines
Reviewed-by Alice Ryhl <[email protected]>Reviewed-by Daniel Almeida <[email protected]>Signed-off-by Danilo Krummrich <[email protected]>
Quoted 3 lines
--- rust/kernel/drm/gem/mod.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
Re: [PATCH v2 3/7] rust: drm: move file_operations from gem to device
In reply to Danilo KrummrichQuoted 8 lines · Danilo Krummrich
Move the
file_operationsdefinition fromdrm::gem::create_fops()into UnregisteredDevice indrm::device. This is thefile_operationsof the DRM minor device, set through thedrm_driverstruct. It is not specific to GEM.Reviewed-by Alice Ryhl <[email protected]>Reviewed-by Daniel Almeida <[email protected]>Signed-off-by Danilo Krummrich <[email protected]>
Quoted 4 lines
--- rust/kernel/drm/device.rs | 23 ++++++++++++++++++++--- rust/kernel/drm/gem/mod.rs | 20 -------------------- 2 files changed, 20 insertions(+), 23 deletions(-)
Re: [PATCH v2 4/7] rust: fs: add iminor() helper
In reply to Danilo KrummrichQuoted 7 lines · Danilo Krummrich
Add a Rust helper for
iminor(), which extracts the minor number from a struct inode. This is a static inline function in the C headers, so bindgen does not generate a binding for it.Reviewed-by Alice Ryhl <[email protected]>Reviewed-by Daniel Almeida <[email protected]>Signed-off-by Danilo Krummrich <[email protected]>
Quoted 3 lines
--- rust/helpers/fs.c | 5 +++++ 1 file changed, 5 insertions(+)
Re: [PATCH v2 5/7] rust: drm: wrap fops open with RegistrationGuard
In reply to Danilo KrummrichQuoted 11 lines · Danilo Krummrich
Add a Minor abstraction with RAII release and a
fops_open()wrapper that holds a RegistrationGuard (drm_dev_enter/drm_dev_exit) across the entiredrm_open()call.This guarantees that
drm_dev_unplug()inRegistration::drop()waits for the full open sequence to complete, so all files are visible in the filelist when iterating for cleanup.Reviewed-by Alice Ryhl <[email protected]>Reviewed-by Daniel Almeida <[email protected]>Signed-off-by Danilo Krummrich <[email protected]>
I wonder if the logic can be implemented in drm_open instead?
Regardless:
Quoted 3 lines
--- rust/kernel/drm/device.rs | 63 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-)
Re: [PATCH v2 6/7] rust: drm: return impl PinInit from DriverFile::open()
In reply to Danilo KrummrichQuoted 9 lines · Danilo Krummrich
Change
DriverFile::open()to return impl PinInit<Self, Error> instead of Result<Pin<KBox<Self>>>, consistent with how bus device private data works.Drivers no longer need to allocate a Pin<KBox<_>> themselves; they just return an initializer and the subsystem takes care of the allocation.
Reviewed-by Alice Ryhl <[email protected]>Signed-off-by Danilo Krummrich <[email protected]>
Quoted 6 lines
--- drivers/gpu/drm/nova/file.rs | 5 ++--- drivers/gpu/drm/tyr/file.rs | 4 ++-- rust/kernel/drm/file.rs | 5 +++-- rust/kernel/drm/gem/shmem.rs | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-)
Gary Guo
reviewerRe: [PATCH v2 7/7] rust: drm: make Driver::File lifetime-parameterized
In reply to Danilo KrummrichQuoted 123 lines · Danilo Krummrich
Make the DRM file private data lifetime-parameterized, allowing drivers to borrow from RegistrationData in their per-file data.
Introduce DriverFile<'a> as a lifetime-parameterized trait that receives both the device and registration data in
open(). Parametrize File<D> on the driver type rather than the file data type, deriving the concrete file type through Driver::File<'a>.Add
inner_with()to access the driver file data from a File<D> reference.Ensure file data is always dropped before registration data:
- In
Registration::drop(), detachdriver_privfor all open files underfilelist_mutex, then drop the detached data after releasing the mutex and wait for file-data destruction in concurrentpostclose_callback()calls via anopen_count/ WaitQueue pair ondrm::Device.- In
postclose_callback(), skip the drop ifdriver_privhas already been NULLed by the filelist iteration. The NULL write is visible through thefilelist_mutexacquire/release chain indrm_close_helper().Signed-off-by Danilo Krummrich <[email protected]>--- drivers/gpu/drm/nova/driver.rs | 2 +- drivers/gpu/drm/nova/file.rs | 13 ++- drivers/gpu/drm/nova/gem.rs | 8 +- drivers/gpu/drm/tyr/driver.rs | 2 +- drivers/gpu/drm/tyr/file.rs | 11 +- rust/kernel/drm/device.rs | 36 +++++-- rust/kernel/drm/driver.rs | 113 +++++++++++++++++++-- rust/kernel/drm/file.rs | 180 ++++++++++++++++++++++++--------- rust/kernel/drm/gem/mod.rs | 44 ++++++-- rust/kernel/drm/gem/shmem.rs | 9 +- rust/kernel/drm/ioctl.rs | 6 +- 11 files changed, 330 insertions(+), 94 deletions(-)drivers/gpu/drm/nova/gem.rs +4
-4@@ -11,9 +11,9 @@ sync::aref::ARef, }; -use crate ::{ - driver::{NovaDevice ,NovaDriver }, - file::File , +use crate ::driver::{ +NovaDevice , +NovaDriver ,// };/// GEM Object inner driver data @@ -43,7 +43,7 @@ pub(crate) fn new(dev: &NovaDevice, size: usize) -> Result<ARef<gem::Object<Self/// Look up a GEM object handle for a `File` and return an `ObjectRef` for it. #[inline] pub (crate )fn lookup_handle ( - file: &drm::File <File >, + file: &drm::File <NovaDriver >, handle:u32 , ) ->Result <ARef <gem::Object <Self >>> { gem::Object ::lookup_handle (file, handle)drivers/gpu/drm/tyr/driver.rs +1
-1@@ -209,7 +209,7 @@ fn drop(self: Pin<&mut Self>) {}impl drm::Driver for TyrDrmDriver {type Data = ();type RegistrationData <'drm> =TyrDrmRegistrationData <'drm>; -type File =TyrDrmFileData ; +type File <'a> =TyrDrmFileData ;type Object =Bo ;type ParentDevice <Ctx :DeviceContext > = platform::Device <Ctx >;drivers/gpu/drm/tyr/file.rs +7
-4@@ -19,13 +19,16 @@#[pin_data] pub (crate )struct TyrDrmFileData {} -/// Convenience type alias for our DRM `File` type -pub (crate )type TyrDrmFile = drm::file::File <TyrDrmFileData >; +/// Convenience type alias for our DRM `File` type. +pub (crate )type TyrDrmFile = drm::file::File <TyrDrmDriver >; -impl drm::file::DriverFile for TyrDrmFileData { +impl drm::file::DriverFile <'_>for TyrDrmFileData {type Driver =TyrDrmDriver ; -fn open (_dev: &drm::Device <Self ::Driver >) ->impl PinInit <Self ,Error > { +fn open ( + _device: &TyrDrmDevice <Registered >, + _reg_data: &TyrDrmRegistrationData <'_>, + ) ->impl PinInit <Self ,Error > {Ok (Self {}) } }rust/kernel/drm/device.rs +7
-3@@ -15,9 +15,13 @@ }, error::from_err_ptr, prelude::*, - sync::aref::{ - ARef, - AlwaysRefCounted, // + sync::{ + aref::{ + ARef, + AlwaysRefCounted, // + }, + atomic::Atomic, + WaitQueue, }, types::{NotThreadSafe ,
rust/kernel/drm/device.rs fn deref(&self) -> &Self::Target { · line 199
Quoted 7 lines
rust/kernel/drm/device.rs +3
-1@@ -190,7 +194,10 @@ fn deref(&self) -> &Self::Target { } } -impl <T : drm::Driver >UnregisteredDevice <T > { +impl <T : drm::Driver >UnregisteredDevice <T > +where +for <'a>T ::File <'a>: drm::file::DriverFile <'a,Driver =T >,
Hmm, shouldn't this bound be on drm::Driver?
Does
type File<'a>: DriverFile<'a, Driver = Self> + 'a;
not work?
Quoted 38 lines
rust/kernel/drm/device.rs +18
-2+{const fn compute_features () ->u32 {let mut features = drm::driver::FEAT_GEM ;@@ -203,8 +210,8 @@ const fn compute_features() -> u32 {const VTABLE : bindings::drm_driver =drm_legacy_fields! { load:None , - open:Some (drm::File ::<T ::File >::open_callback), - postclose:Some (drm::File ::<T ::File >::postclose_callback), + open:Some (drm::File ::<T >::open_callback), + postclose:Some (drm::File ::<T >::postclose_callback), unload:None , release:Some (Device ::<T >::release), master_set:None ,@@ -333,6 +340,21 @@ pub fn new(// SAFETY: `raw_drm` is valid; no concurrent access before registration. unsafe { (*raw_drm.as_ptr ()).registration_data =UnsafeCell ::new (NonNull ::dangling ()) }; +// SAFETY: `raw_drm` is valid; no concurrent access before registration. +unsafe { (*raw_drm.as_ptr ()).open_count =Atomic ::new (0 ) }; + +// SAFETY: +// - `raw_drm` is valid and `open_count_wq` is uninitialized; no concurrent access +// before registration. +// - The field is pinned because the Device is pinned (refcounted, allocated by +// `__drm_dev_alloc()`, never moved). +unsafe { + pin_init::raw_init ( + &raw mut (*raw_drm.as_ptr ()).open_count_wq, +crate ::new_waitqueue!("drm_open_count" ), + ) + }; +// SAFETY: The reference count is one, and now we take ownership of that reference as a // `drm::Device`. // INVARIANT: We just created the device above, but have yet to call `drm_dev_register`.
rust/kernel/drm/device.rs pub struct Device<T: drm::Driver, C: DeviceContext = Normal> { · line 382
Quoted 4 lines
rust/kernel/drm/device.rs +1
-0@@ -357,6 +379,8 @@ 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>>>, + pub(super) open_count: Atomic<i32>,
32-bit open count is enough if you want to skip the overflow checks. Consider change to Atomic<i64> or Atomic<u64> instead.
Or is DRM somehow enforcing that no more than 2**31 files can be open against a device?
Quoted 281 lines
rust/kernel/drm/device.rs +1
-0+ pub(super) open_count_wq: WaitQueue, _ctx: PhantomData<C>, }rust/kernel/drm/driver.rs +106
-7@@ -7,10 +7,22 @@use crate ::{ bindings, device, - drm, + drm::{ + self, + file::FileData ,// + }, error::to_result, + interop::list::clist_create, prelude::*, - sync::aref::ARef ,// + sync::{ + aref::ARef , + atomic::{ +Acquire , +Release ,// + }, +Mutex ,// + }, + types::Opaque ,// };use core::{ marker::PhantomPinned ,@@ -120,8 +132,14 @@ pub trait Driver {/// The type used to manage memory for this driver. type Object :AllocImpl ; -/// The type used to represent a DRM File (client) -type File : drm::file::DriverFile ; +/// The type used to represent a DRM File (client). +/// +/// File data may borrow from [`RegistrationData`](Driver::RegistrationData). File data is +/// guaranteed to be dropped before registration data, either when the file is closed or +/// when the device is unregistered, whichever comes first. +/// +/// Drivers implement [`DriverFile`](drm::file::DriverFile) for their file data type. +type File <'a>: 'a;/// The bus device type of the parent device that the DRM device is associated with. type ParentDevice <Ctx : device::DeviceContext >: device::AsBusDevice <Ctx >;@@ -197,6 +215,13 @@ pub unsafe fn new<E>(// SAFETY: `drm` is a valid, initialized but not yet registered DRM device. let ret =unsafe { bindings::drm_dev_register (drm.as_raw (),0 ) };to_result (ret).inspect_err (|_| { +// SAFETY: +// - Failed registration unplugs the device and drains registration guards. +// - `data` remains initialized until this closure returns. +// - Failed initialization does not run `Registration::drop()`, so this is +// the only file-data cleanup for this device. +unsafe {Self ::drop_file_data (drm) }; +// SAFETY: `drm_dev_register()` synchronizes SRCU on failure, so no // concurrent access to `registration_data` is possible at this point. unsafe { *drm.registration_data.get () =NonNull ::dangling () };@@ -209,6 +234,75 @@ pub unsafe fn new<E>(pub fn device (&self) -> &drm::Device <T > { &self.drm } + +/// Detaches and drops file data, then waits for concurrent postclose destruction. +/// +/// # Safety +/// +/// - `drm` must be unplugged with all `RegistrationGuard` sections drained. +/// - Its registration data must remain alive until this function returns. +/// - This must be the only call for this device. +unsafe fn drop_file_data (drm: &drm::Device <T >) { +let raw = drm.as_raw (); +let mut files =None ; + +// SAFETY: `filelist_mutex` is initialized by `drm_dev_init()` and remains valid for +// the lifetime of the `struct drm_device`. +let filelist_mutex =unsafe {Mutex ::from_raw (&raw mut (*raw).filelist_mutex) }; + { +let _guard = filelist_mutex.lock (); + +// SAFETY: +// - `filelist` is initialized and protected by the held mutex. +// - Its entries are `drm_file`s linked through `lhead`; `Opaque` has the same layout. +let filelist =unsafe { +clist_create! ( + &raw mut (*raw).filelist, +Opaque <bindings::drm_file>, + bindings::drm_file, + lhead + ) + }; + +// Detach the allocations so `postclose()` sees NULL and leaves their destruction to us. +// After `postclose()` returns, the DRM core can free the C file, so the detached list +// must not depend on it. All `RegistrationGuard` sections have already been drained. +for filein filelist.iter () { +// SAFETY: `file` is a valid `drm_file` on this device's filelist. +let priv_ptr =unsafe { (*file.get ()).driver_priv }; + +// SAFETY: Setting `driver_priv` to NULL is visible to `postclose_callback()` +// through the `filelist_mutex` acquire/release chain in `drm_close_helper()`. +unsafe { (*file.get ()).driver_priv = core::ptr::null_mut () }; + +// SAFETY: +// - `priv_ptr` comes from `KBox::into_raw()` in `open_callback()`. +// - The file is still in the list, so `postclose_callback()` has not run. Clearing +// `driver_priv` transfers ownership here and makes a later postclose skip it. +// - Only the owning pointer is moved; the pinned allocation is dropped in place. +let mut data =unsafe { +Pin ::new_unchecked (KBox ::from_raw ( + priv_ptr.cast ::<FileData <T ::File <'static>>>(), + )) + }; + *data.as_mut ().project ().next = files.take (); + files =Some (data); + } + } + +// Run driver destructors without `filelist_mutex`. Take each link before dropping its +// allocation to avoid recursively dropping the entire chain. +while let Some (mut data) = files { + files = data.as_mut ().project ().next.take (); +drop (data); + drm.open_count.fetch_sub (1 ,Release ); + } + +// Wait for file-data destruction in concurrent `postclose_callback()` calls. No new opens +// can succeed after unplug, so `open_count` is monotonically decreasing. + drm.open_count_wq + .wait_event (|| drm.open_count.load (Acquire ) ==0 ); + } }// SAFETY: `Registration` doesn't offer any methods or access to fields when shared between @@ -230,8 +324,13 @@ fn drop(self: Pin<&mut Self>) {// SAFETY: Safe by the invariant of `ARef<drm::Device<T>>`. The existence of this // `Registration` also guarantees that this `drm::Device` is actually registered. unsafe { bindings::drm_dev_unplug (self.drm.as_raw ()) }; -// After drm_dev_unplug(), the SRCU barrier guarantees that all RegistrationGuard critical -// sections have completed, so no one holds a reference to reg_data anymore. -// reg_data is dropped here automatically. + +// SAFETY: +// - `drm_dev_unplug()` has unplugged the device and drained registration guards. +// - Registration data remains alive until this destructor returns. +// - Registration succeeded, so this destructor performs the only file-data cleanup. +unsafe {Self ::drop_file_data (&self.drm) }; + +// Registration data is dropped here automatically, after all file data has been dropped. } }rust/kernel/drm/file.rs +67
-36@@ -45,60 +75,110 @@ pub(super) fn as_raw(&self) -> *mut bindings::drm_file { self.0 .get () } -fn driver_priv (&self) -> *mut T { -// SAFETY: By the type invariants of `Self`, `self.as_raw()` is always valid. -unsafe { (*self.as_raw ()).driver_priv }.cast () +/// Returns the registered DRM device this file was opened on. +#[inline] +pub fn device (&self) -> &drm::Device <D , drm::Registered > { +// SAFETY: +// - The open file keeps its minor and device alive. +// - By the type invariants, the device uses driver `D`. +// - The type invariants require a `RegistrationGuard` for the device throughout this +// borrow, satisfying the `Registered` context. +unsafe { drm::Device ::from_raw ((*(*self.as_raw ()).minor).dev) } } -/// Return a pinned reference to the driver file structure. -pub fn inner (&self) ->Pin <&T > { -// SAFETY: By the type invariant the pointer `self.as_raw()` points to a valid and opened -// `struct drm_file`, hence `driver_priv` has been properly initialized by `open_callback`. -unsafe {Pin ::new_unchecked (&*(self.driver_priv ())) } +/// Access the driver file data through a closure. +/// +/// The closure receives a pinned reference to the file data. +/// +/// The outer reference carries the `&self` lifetime while the inner type carries the HRTB +/// lifetime `'a`, implying `'a` outlives `&self`. This allows the closure to coerce covariant +/// sub-fields (e.g. `&'a T` to the caller's lifetime) and return them directly in `R`. +#[inline] +pub fn inner_with <'this,R ,F >(&'this self, f:F ) ->R +where +F :for <'a>FnOnce (Pin <&'thisD ::File <'a>>) ->R , + { +// SAFETY: By the type invariants, `self.as_raw()` points to a valid `drm_file`. +let ptr: *const FileData <_ > =unsafe { (*self.as_raw ()).driver_priv.cast_const ().cast () }; + +// SAFETY: +// - The type invariants keep this file's pinned `FileData` allocation valid for `'this`. +// - The `data` field is structurally pinned. +// - The HRTB `for<'a>` prevents the caller from choosing a concrete lifetime, so shortening +// is sound regardless of variance. +let data:Pin <&'thisD ::File <'this>> =unsafe {Pin ::new_unchecked (&(*ptr).data) }; +f (data) }/// The open callback of a `struct drm_file`. +/// +/// Called from `drm_open()`, which is itself called from `fops_open()`. The latter holds a +/// `RegistrationGuard`, so the device is guaranteed to be registered for the duration of this +/// callback. pub (crate )extern "C" fn open_callback ( raw_dev: *mut bindings::drm_device , raw_file: *mut bindings::drm_file , - ) -> core::ffi::c_int { -// SAFETY: A callback from `struct drm_driver::open` guarantees that -// - `raw_dev` is valid pointer to a `struct drm_device`, -// - the corresponding `struct drm_device` has been registered. -let drm =unsafe { drm::Device ::from_raw (raw_dev) }; - -// SAFETY: `raw_file` is a valid pointer to a `struct drm_file`. -let file =unsafe {File ::<T >::from_raw (raw_file) }; - -let init =T ::open (drm); -let inner =match KBox ::try_pin_init (init,GFP_KERNEL ) { -Err (e) => { -return e.to_errno (); - } -Ok (i) => i, - }; - -// SAFETY: This pointer is treated as pinned, and the Drop guarantee is upheld in -// `postclose_callback()`. -let driver_priv =KBox ::into_raw (unsafe {Pin ::into_inner_unchecked (inner) }); - -// SAFETY: By the type invariants of `Self`, `self.as_raw()` is always valid. -unsafe { (*file.as_raw ()).driver_priv = driver_priv.cast () }; - -0 + ) -> core::ffi::c_int +where +for <'a>D ::File <'a>:DriverFile <'a,Driver =D >, + { +// SAFETY: +// - The DRM core guarantees that `raw_dev` is valid. +// - `fops_open()` holds a `RegistrationGuard`, satisfying the `Registered` context. +let dev: &drm::device::Device <D , drm::Registered > = +unsafe { drm::device::Device ::from_raw (raw_dev) }; + + dev.registration_data_with (|reg_data| { +let init =try_pin_init! (FileData { + next:None , + data <- <D ::File <'_>as DriverFile <'_>>::open (dev, reg_data), + }); +let inner =match KBox ::try_pin_init (init,GFP_KERNEL ) { +Err (e) =>return e.to_errno (), +Ok (i) => i, + }; + +// SAFETY: The allocation stays pinned until `postclose_callback()` or +// `Registration::drop_file_data()` drops it in place. +let driver_priv =KBox ::into_raw (unsafe {Pin ::into_inner_unchecked (inner) }); + + dev.open_count.fetch_add (1 ,Relaxed ); + +// SAFETY: `raw_file` is a valid pointer to a `struct drm_file`. +unsafe { (*raw_file).driver_priv = driver_priv.cast () }; + +0 + }) }/// The postclose callback of a `struct drm_file`. pub (crate )extern "C" fn postclose_callback ( - _raw_dev: *mut bindings::drm_device , + raw_dev: *mut bindings::drm_device , raw_file: *mut bindings::drm_file, - ) { -// SAFETY: This reference won't escape this function
rust/kernel/drm/file.rs pub(super) fn as_raw(&self) -> *mut bindings::drm_file { · line 163
Quoted 10 lines
rust/kernel/drm/file.rs +6
-4-let file =unsafe {File ::<T >::from_raw (raw_file) }; - -// SAFETY: `file.driver_priv` has been created in `open_callback` through `KBox::into_raw`. -let _ =unsafe {KBox ::from_raw (file.driver_priv ()) }; + ) where +for <'a>D ::File <'a>:DriverFile <'a,Driver =D >, + { +// SAFETY: `raw_file` is a valid pointer to a `struct drm_file`. +let driver_priv =unsafe { (*raw_file).driver_priv }; +
I think it's non-trivial on why this code here is correct, so might worth adding a sentence or two about the case when this happens (because drop_file_data runs first) and why the access to driver_priv is sychronized (because the pointer is protected by filelist_mutex, and postclose_callback runs after the file has been detached from the list).
Best,
Gary
Quoted 20 lines
rust/kernel/drm/file.rs +17
-0+if driver_priv.is_null () { +return ; + } + +// SAFETY: +// - `driver_priv` comes from `KBox::into_raw()` in `open_callback()`. +// - The file is not on the filelist, and the NULL check rules out ownership having been +// transferred to `Registration::drop_file_data()`. +// - The pinned data is dropped in place while registration data is still alive, since +// registration cleanup waits for `open_count` to reach zero. +drop (unsafe {KBox ::from_raw (driver_priv.cast ::<FileData <D ::File <'static>>>()) }); + +// SAFETY: `raw_dev` is valid for the lifetime of the `struct drm_file`. +let dev: &drm::device::Device <D > =unsafe { drm::device::Device ::from_raw (raw_dev) }; +if dev.open_count.fetch_sub (1 ,Release ) ==1 { + dev.open_count_wq.wake_up (); + } } }
Re: [PATCH v2 6/7] rust: drm: return impl PinInit from DriverFile::open()
In reply to Danilo KrummrichRe: [PATCH v2 7/7] rust: drm: make Driver::File lifetime-parameterized
In reply to Danilo Krummrich