rust: drm: gpuvm: implement Send and Sync for GpuVaAlloc and GpuVmBo
Subsystems (4)
PATCH 1/1 rust: drm: gpuvm: implement Send and Sync for GpuVaAlloc and GpuVmBo
A GpuVaAlloc holds only uninitialised memory with no live VaData and hands out no reference to its contents, so it is Send and Sync unconditionally. A GpuVmBo is refcounted atomically and its deferred put is sound from any thread.
Implement the markers in the abstraction so drivers can move these objects between threads without an unsafe impl of their own. Send for GpuVmBo requires VmBoData: Send because the data is dropped during deferred cleanup, on whichever thread drains the queue. Sync requires both VmBoData: Sync and Object: Sync because the &self data() and obj() accessors hand out &VmBoData and &Object.
--- rust/kernel/drm/gpuvm/va.rs | 8 ++++++++ rust/kernel/drm/gpuvm/vm_bo.rs | 15 +++++++++++++++ 2 files changed, 23 insertions(+)
rust/kernel/drm/gpuvm/va.rs +8 -0
@@ -104,6 +104,14 @@ pub fn vm_bo(&self) -> &GpuVmBo<T> {/// The memory is zeroed. pub struct GpuVaAlloc <T :DriverGpuVm >(KBox <MaybeUninit <GpuVa <T >>>); +// SAFETY: A [`GpuVaAlloc`] is an owned, uninitialised allocation with no live `T::VaData` and no +// thread-bound state. +unsafe impl <T :DriverGpuVm >Send for GpuVaAlloc <T > {} + +// SAFETY: A [`GpuVaAlloc`] has no `&self` method that reaches its contents, so a shared +// `&GpuVaAlloc` cannot access the allocation. +unsafe impl <T :DriverGpuVm >Sync for GpuVaAlloc <T > {} +impl <T :DriverGpuVm >GpuVaAlloc <T > {/// Pre-allocate a [`GpuVa`] object. pub fn new (flags:AllocFlags ) ->Result <GpuVaAlloc <T >,AllocError > {
rust/kernel/drm/gpuvm/vm_bo.rs +15 -0
@@ -19,6 +19,21 @@ pub struct GpuVmBo<T: DriverGpuVm> { data: T::VmBoData, } +// SAFETY: The refcount in `self.inner` is atomic, so `dec_ref`'s deferred put is sound from any +// thread. `data` is dropped later by `drm_gpuvm_bo_deferred_cleanup`, on whichever thread drains +// the queue, hence the `T::VmBoData: Send` bound. +unsafe impl <T :DriverGpuVm >Send for GpuVmBo <T >where T ::VmBoData :Send {} + +// SAFETY: The fields of `inner` read by shared-reference methods are immutable after construction. +// [`Self::data`] hands out `&T::VmBoData` and [`Self::obj`] hands out `&T::Object`, so sharing +// `&Self` across threads requires both to be `Sync`. +unsafe impl <T :DriverGpuVm >Sync for GpuVmBo <T > +where +T ::VmBoData :Sync , +T ::Object :Sync , +{ +} +// SAFETY: By type invariants, the allocation is managed by the refcount in `self.inner`. unsafe impl <T :DriverGpuVm >AlwaysRefCounted for GpuVmBo <T > {fn inc_ref (&self) {
base-commit: fea3a2dd7d3fc1936211ced5f84420e610435730
Signature · 1 line
2.54.0.1032.g2f8565e1d1-goog
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The `Send` implementation for `GpuVmBo` is missing `T::Object: Sync` and `T::Object: Send` bounds.…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The Send implementation for GpuVmBo is missing T::Object: Sync and T::Object: Send bounds. --
commit 0b3e6c9417da8e3bad6e8397aefb1b71a9755123
Author: Sami Tolvanen <[email protected]>
rust: drm: gpuvm: implement Send and Sync for GpuVaAlloc and GpuVmBo
This commit implements Send and Sync traits for GpuVaAlloc and GpuVmBo. It asserts that these objects can be safely moved between threads by providing unsafe impls with bounds for the underlying data types.
Quoted 4 lines
rust/kernel/drm/gpuvm/vm_bo.rs +0
-0
[ ... ]
rust/kernel/drm/gpuvm/vm_bo.rs pub struct GpuVmBo<T: DriverGpuVm> { · line 25
Quoted 8 lines
rust/kernel/drm/gpuvm/vm_bo.rs +4
-0@@ -19,6 +19,21 @@ pub struct GpuVmBo<T: DriverGpuVm> { data: T::VmBoData, } +// SAFETY: The refcount in `self.inner` is atomic, so `dec_ref`'s deferred put is sound from any +// thread. `data` is dropped later by `drm_gpuvm_bo_deferred_cleanup`, on whichever thread drains +// the queue, hence the `T::VmBoData: Send` bound. +unsafe impl <T :DriverGpuVm >Send for GpuVmBo <T >where T ::VmBoData :Send {}
[Severity: High]
Is this implementation missing bounds for T::Object: Sync and T::Object: Send?
Since GpuVmBo acts as a refcounted handle to a GEM object and hands out shared references to it via the obj() method, sending a GpuVmBo to another thread allows that thread to obtain a shared reference concurrently. This requires T::Object: Sync.
Additionally, when the deferred cleanup queue is drained, dropping the C-side refcount could result in the final drop of the GEM object occurring on that thread. This requires T::Object: Send.
Could omitting these bounds allow safe Rust code to bypass thread-safety guarantees, potentially leading to data races when non-thread-safe GEM objects are shared?
Quoted 10 lines
rust/kernel/drm/gpuvm/vm_bo.rs +10
-0+ +// SAFETY: The fields of `inner` read by shared-reference methods are immutable after construction. +// [`Self::data`] hands out `&T::VmBoData` and [`Self::obj`] hands out `&T::Object`, so sharing +// `&Self` across threads requires both to be `Sync`. +unsafe impl <T :DriverGpuVm >Sync for GpuVmBo <T > +where +T ::VmBoData :Sync , +T ::Object :Sync , +{ +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1