Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rust: drm: dispatch work items to the private data

This implementation dispatches any work enqueued on ARef<drm::Device<T>> to
its driver-provided handler. It does so by building upon the newly-added
ARef<T> support in workqueue.rs in order to call into the driver
implementations for work_container_of and raw_get_work.

This is notably important for work items that need access to the drm
device, as it was not possible to enqueue work on a ARef<drm::Device<T>>
previously without failing the orphan rule.

The current implementation needs T::Data to live inline with drm::Device in
order for work_container_of to function. This restriction is already
captured by the trait bounds. Drivers that need to share their ownership of
T::Data may trivially get around this:

// Lives inline in drm::Device
struct DataWrapper {
work: ...,
// Heap-allocated, shared ownership.
data: Arc<DriverData>,
}

Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Acked-by: Danilo Krummrich <dakr@kernel.org>
Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
Link: https://lore.kernel.org/r/20260323-aref-workitem-v3-2-f59729b812aa@collabora.com
Signed-off-by: Alice Ryhl <aliceryhl@google.com>

authored by

Daniel Almeida and committed by
Alice Ryhl
72a723df f5e841e4

+53 -3
+53 -3
rust/kernel/drm/device.rs
··· 6 6 7 7 use crate::{ 8 8 alloc::allocator::Kmalloc, 9 - bindings, 10 - device, 9 + bindings, device, 11 10 drm::{ 12 11 self, 13 12 driver::AllocImpl, // ··· 17 18 ARef, 18 19 AlwaysRefCounted, // 19 20 }, 20 - types::Opaque, // 21 + types::Opaque, 22 + workqueue::{ 23 + HasWork, 24 + Work, 25 + WorkItem, // 26 + }, 21 27 }; 22 28 use core::{ 23 29 alloc::Layout, ··· 245 241 // SAFETY: A `drm::Device` can be shared among threads because all immutable methods are protected 246 242 // by the synchronization in `struct drm_device`. 247 243 unsafe impl<T: drm::Driver> Sync for Device<T> {} 244 + 245 + impl<T, const ID: u64> WorkItem<ID> for Device<T> 246 + where 247 + T: drm::Driver, 248 + T::Data: WorkItem<ID, Pointer = ARef<Device<T>>>, 249 + T::Data: HasWork<Device<T>, ID>, 250 + { 251 + type Pointer = ARef<Device<T>>; 252 + 253 + fn run(ptr: ARef<Device<T>>) { 254 + T::Data::run(ptr); 255 + } 256 + } 257 + 258 + // SAFETY: 259 + // 260 + // - `raw_get_work` and `work_container_of` return valid pointers by relying on 261 + // `T::Data::raw_get_work` and `container_of`. In particular, `T::Data` is 262 + // stored inline in `drm::Device`, so the `container_of` call is valid. 263 + // 264 + // - The two methods are true inverses of each other: given `ptr: *mut 265 + // Device<T>`, `raw_get_work` will return a `*mut Work<Device<T>, ID>` through 266 + // `T::Data::raw_get_work` and given a `ptr: *mut Work<Device<T>, ID>`, 267 + // `work_container_of` will return a `*mut Device<T>` through `container_of`. 268 + unsafe impl<T, const ID: u64> HasWork<Device<T>, ID> for Device<T> 269 + where 270 + T: drm::Driver, 271 + T::Data: HasWork<Device<T>, ID>, 272 + { 273 + unsafe fn raw_get_work(ptr: *mut Self) -> *mut Work<Device<T>, ID> { 274 + // SAFETY: The caller promises that `ptr` points to a valid `Device<T>`. 275 + let data_ptr = unsafe { &raw mut (*ptr).data }; 276 + 277 + // SAFETY: `data_ptr` is a valid pointer to `T::Data`. 278 + unsafe { T::Data::raw_get_work(data_ptr) } 279 + } 280 + 281 + unsafe fn work_container_of(ptr: *mut Work<Device<T>, ID>) -> *mut Self { 282 + // SAFETY: The caller promises that `ptr` points at a `Work` field in 283 + // `T::Data`. 284 + let data_ptr = unsafe { T::Data::work_container_of(ptr) }; 285 + 286 + // SAFETY: `T::Data` is stored as the `data` field in `Device<T>`. 287 + unsafe { crate::container_of!(data_ptr, Self, data) } 288 + } 289 + }