Skip to content

rust: drm: Higher-Ranked Lifetime private data

Danilo Krummrich dri-devel driver-core lkml nova-gpu rust-for-linux stable 5 versions – Applied

Status from this message: Applied, thanks!

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v5 Latest19 patches 25 repliesApplied
  2. v416 patches 11 repliesSuperseded
  3. v313 patches 1 replySuperseded
Earlier versions · 2
VersionPostedPatchesRepliesStatus
v27 patches9 repliesSuperseded
v16 patches17 repliesSuperseded

Changes in v5 from the cover letter

- Replace __call_ioctl() helper with a simple for<'a> fn(...) coercion
- Rebase onto latest drm-rust-next, which requires a faux::Device type with AsBusDevice impl and motivates the removal of the DeviceContext generic from shmem::Object entirely
- Replace ForLt with a GAT type RegistrationData<'a>
- Fix drm_dev_register() error path race on the C side
- Add a few missing #[inline] annotations

Patches in v5 Applied

#Subject and filesSizeRepliesApplied
1/19rust: drm: ioctl: fix unbounded lifetimes in ioctl handler argumentsrust/kernel/drm/ioctl.rs+6 -02 replies
2/19rust: drm: rename Uninit DeviceContext to Normalrust/kernel/drm/device.rs rust/kernel/drm/mod.rs+28 -661 reply
3/19rust: faux: add Device type with AsBusDevice supportrust/kernel/drm/gem/shmem.rs rust/kernel/faux.rs samples/rust/rust_driver_faux.rs+68 -151 reply
4/19rust: drm: Add Driver::ParentDevice associated typedrivers/gpu/drm/nova/driver.rs drivers/gpu/drm/tyr/driver.rs rust/kernel/drm/device.rs +2 more+20 -81 reply
5/19rust: drm: change default DeviceContext to Normaldrivers/gpu/drm/nova/driver.rs drivers/gpu/drm/tyr/driver.rs rust/kernel/drm/device.rs +2 more+12 -111 reply
6/19rust: drm: restrict AlwaysRefCounted to Normal Device contextrust/kernel/drm/device.rs+5 -41 reply
7/19rust: drm: restrict AlwaysRefCounted to Normal GEM Object contextdrivers/gpu/drm/nova/driver.rs drivers/gpu/drm/nova/gem.rs drivers/gpu/drm/tyr/driver.rs +5 more+130 -1451 reply
8/19rust: drm/gem: remove DeviceContext from shmem::Objectrust/kernel/drm/gem/shmem.rs+51 -702 replies
9/19rust: drm: split Deref for Device context typestatesrust/kernel/drm/device.rs+15 -11 reply
10/19rust: drm: pin ioctl Device reference to Normal contextrust/kernel/drm/ioctl.rs+2 -11 reply
11/19rust: drm: add Ioctl device context typestaterust/kernel/drm/device.rs rust/kernel/drm/mod.rs+32 -31 reply
12/19rust: drm: Add RegistrationGuard for drm_dev_enter/exit critical sectionsrust/kernel/drm/device.rs rust/kernel/drm/driver.rs rust/kernel/drm/mod.rs+83 -132 replies
13/19rust: drm: Wrap ioctl dispatch in RegistrationGuarddrivers/gpu/drm/nova/file.rs drivers/gpu/drm/tyr/file.rs rust/kernel/drm/ioctl.rs+55 -91 reply
14/19rust: drm: return ParentDevice from Device AsRefrust/kernel/drm/device.rs rust/kernel/drm/driver.rs rust/kernel/drm/gem/shmem.rs+11 -51 reply
15/19rust: drm: add AsRef<ParentDevice<Bound>> for Device<Registered>rust/kernel/drm/device.rs+14 -01 reply
16/19drm: fix race between partial drm_dev_register() failure and ioctldrivers/gpu/drm/drm_drv.c+25 -92 replies
17/19rust: drm: Add RegistrationData to drm::Driverdrivers/gpu/drm/nova/driver.rs drivers/gpu/drm/tyr/driver.rs rust/kernel/drm/device.rs +2 more+125 -511 reply
18/19rust: drm: Pass registration data to ioctl handlersdrivers/gpu/drm/nova/file.rs drivers/gpu/drm/tyr/file.rs rust/kernel/drm/ioctl.rs+10 -21 reply
19/19drm: nova: Use drm::Device<Registered> to access the parent bus devicedrivers/gpu/drm/nova/driver.rs drivers/gpu/drm/nova/file.rs+6 -121 reply

Reviews trailers per patch

All 19 patches have a Reviewed-by or Acked-by.

PatchLyude PaulAlexandre CourbotDeborah Brouwer
SeriesT-b
1/19R-b R-b
2/19R-b R-b
3/19R-b
4/19R-b R-b
5/19R-b R-b
6/19R-b R-b
7/19R-b R-b
8/19R-b R-b
9/19R-b R-b
10/19R-b R-b
11/19R-b R-b
12/19R-b R-b
13/19R-b R-b
14/19R-b R-b
15/19R-b R-b
16/19R-b R-b
17/19R-b
18/19R-b R-b
19/19R-b R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 3 reviewers.

Read the v5 thread

Cover letter [PATCH v5 00/19] rust: drm: Higher-Ranked Lifetime private data

DRM ioctls run in process context without any guarantee that the parent bus device is still bound. This series solves the problem by introducing RegistrationGuard -- a guard representing a drm_dev_enter/exit SRCU critical section that proves the parent bus device is bound for the lifetime of the guard.

As initial plumbing for this, the DRM DeviceContext typestates are
reworked: Uninit is renamed to Normal, defaults are adjusted, AlwaysRefCounted is restricted to Normal, and a Deref chain from Device<T, Registered> to Device<T, Normal> is established. This gives Device<T, Registered> the semantic that the device is currently registered and the parent bus device is bound, which makes the RegistrationGuard and ioctl dispatch much cleaner. An Ioctl context restricts registration_guard() to ioctl dispatch, where the DRM core guarantees prior registration.

On top of that, add RegistrationData as a GAT (Generic Associated Type) on drm::Driver, allowing drivers to store data whose lifetime is tied to the parent bus device binding scope. The data is allocated in Registration::new(), lifetime-erased to 'static for storage, and made accessible through Device<T, Registered>::registration_data_with(). The closure's HRTB ties the lifetime to the closure scope; internally the 'static pointer is cast back to the closure-scoped lifetime. The reference is valid for the duration of the drm_dev_enter/exit critical section held by RegistrationGuard.

Also update the ioctl dispatch macro to wrap every handler in a RegistrationGuard, returning ENODEV if the device has been unplugged, and pass the registration data to handlers.

A branch with all patches can be found in [1].

[1] https://git.kernel.org/pub/scm/linux/kernel/git/dakr/linux.git/log/?h=drm-lifetime

Changes in v5:
- Replace __call_ioctl() helper with a simple for<'a> fn(...) coercion
- Rebase onto latest drm-rust-next, which requires a faux::Device type with AsBusDevice impl and motivates the removal of the DeviceContext generic from shmem::Object entirely
- Replace ForLt with a GAT type RegistrationData<'a>
- Fix drm_dev_register() error path race on the C side
- Add a few missing #[inline] annotations

Changes in v4:
- Fix pre-existing unbounded lifetimes in ioctl handler arguments.
- Fix registration_guard() being callable on unregistered devices by introducing an Ioctl device context typestate; registration_guard() is now only available on Device<T, Ioctl>, which is exclusively constructed in ioctl dispatch context where the DRM core guarantees prior registration.
- Fix type inference allowing handlers to obtain Device<Registered> before RegistrationGuard is acquired.
- Make RegistrationGuard !Send via NotThreadSafe to prevent potential lockdep splats from cross-thread SRCU unlock.
- Store &Device<T, Registered> directly in RegistrationGuard instead of calling assume_ctx() in Deref.

Changes in v3:
- Rename UnbindGuard to RegistrationGuard
- RegistrationGuard no longer dereferences to &Device<Bound>; it dereferences to &drm::Device<T, Registered> instead
- Drop Registration::new() and rename Registration::new_with_lt() to Registration::new()
- Rework DeviceContext typestates: rename Uninit to Normal, restrict AlwaysRefCounted to Normal, establish Deref chain from Registered to Normal
- Add AsRef<ParentDevice<Bound>> on Device<T, Registered> for parent device access
- Move registration_data_with() from RegistrationGuard to drm::Device<T, Registered>
- Ioctl handlers no longer receive &Device<Bound>, only registration data and drm::Device<T, Registered>
- Use Device<Registered>::as_ref() to access parent device in nova-drm

Changes in v2:
- Replace unsafe direct registration data access in ioctl dispatch with safe UnbindGuard::registration_data_with() closure
- Eliminate unbind_guard() free function; use type-inference anchor to enable direct dev.unbind_guard() method call in the ioctl macro
- UnbindGuard::registration_data_with() provides both parent device and registration data to the closure
- Add nova-drm conversion patch demonstrating lifetime-aware registration data with &'bound auxiliary::Device<Bound>
- Various safety comment and documentation improvements

Danilo Krummrich (19):
rust: drm: ioctl: fix unbounded lifetimes in ioctl handler arguments
rust: drm: rename Uninit DeviceContext to Normal
rust: faux: add Device type with AsBusDevice support
rust: drm: Add Driver::ParentDevice associated type
rust: drm: change default DeviceContext to Normal
rust: drm: restrict AlwaysRefCounted to Normal Device context
rust: drm: restrict AlwaysRefCounted to Normal GEM Object context
rust: drm/gem: remove DeviceContext from shmem::Object
rust: drm: split Deref for Device context typestates
rust: drm: pin ioctl Device reference to Normal context
rust: drm: add Ioctl device context typestate
rust: drm: Add RegistrationGuard for drm_dev_enter/exit critical
sections
rust: drm: Wrap ioctl dispatch in RegistrationGuard
rust: drm: return ParentDevice from Device AsRef
rust: drm: add AsRef<ParentDevice<Bound>> for Device<Registered>
drm: fix race between partial drm_dev_register() failure and ioctl
rust: drm: Add RegistrationData to drm::Driver
rust: drm: Pass registration data to ioctl handlers
drm: nova: Use drm::Device<Registered> to access the parent bus device

 drivers/gpu/drm/drm_drv.c        |  34 +++-
 drivers/gpu/drm/nova/driver.rs   |  38 ++--
 drivers/gpu/drm/nova/file.rs     |  22 ++-
 drivers/gpu/drm/nova/gem.rs      |  18 +-
 drivers/gpu/drm/tyr/driver.rs    |  25 ++-
 drivers/gpu/drm/tyr/file.rs      |   8 +-
 drivers/gpu/drm/tyr/gem.rs       |  11 +-
 rust/kernel/drm/device.rs        | 293 +++++++++++++++++++++++--------
 rust/kernel/drm/driver.rs        | 111 +++++++-----
 rust/kernel/drm/gem/mod.rs       |  98 +++++------
 rust/kernel/drm/gem/shmem.rs     | 257 +++++++++++++--------------
 rust/kernel/drm/ioctl.rs         |  56 +++++-
 rust/kernel/drm/mod.rs           |   4 +-
 rust/kernel/faux.rs              |  69 ++++++--
 samples/rust/rust_driver_faux.rs |   3 +-
 15 files changed, 660 insertions(+), 387 deletions(-)

base-commit: fa8cc4e3067f958ea2057f37a8a6f9c6b10a9c03

Signature · 1 line
2.54.0