Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] rust: drm: shmem: narrowed provenance in prime_import_sg_table_callback() - [Low] rust: drm: shmem: no INVARIANT comment in prime_import_sg_table_callback() --
commit 93fbf9fffcbe36057059749efd20c7a4b1a389f5 Author: Janne Grunau <[email protected]> rust: drm: gem: shmem: Implement prime_import_sg_table callback This commit implements a Rust gem_prime_import_sg_table callback for shmem GEM objects to avoid C-side object allocation and teardown, and adds a create_imported() constructor to DriverObject for Nova, Tyr, and the KUnit test driver. Link: https://lore.kernel.org/rust-for-linux/[email protected]/ > diff --git a/rust/kernel/drm/gem/shmem.rs b/rust/kernel/drm/gem/shmem.rs > index 41f31adb23717..e0af54eb36564 100644 > --- a/rust/kernel/drm/gem/shmem.rs > +++ b/rust/kernel/drm/gem/shmem.rs [ ... ] > @@ -346,6 +347,67 @@ pub fn new( > Ok(obj) > } > > + /// Create a new shmem GEM object from another driver's scatter/gather > table of pinned pages. > + /// > + /// This is a Rust re-implemetation of > `drm_gem_shmem_prime_import_sg_table()` to avoid > + /// creating and destroying `Self` in C code. > + /// This function imports a scatter/gather table exported via DMA-BUF by > another driver. > + /// It should be only called via the > &drm_driver.gem_prime_import_sg_table callback. > + extern "C" fn prime_import_sg_table_callback( > + raw_dev: *mut bindings::drm_device, > + raw_attach: *mut bindings::dma_buf_attachment, > + raw_sgt: *mut bindings::sg_table, > + ) -> *mut bindings::drm_gem_object { [ ... ] > + let new: Pin<KBox<Self>> = match KBox::try_pin_init( > + try_pin_init!(Self { > + obj <- Opaque::init_zeroed(), > + parent_resv_obj: None, > + sgt_res: ManuallyDrop::new(SetOnce::new()), > + sgt_lock <- new_mutex!(()), > + inner <- T::create_imported(dev, size), > + }), > + GFP_KERNEL, > + ) { > + Ok(new) => new, > + Err(e) => return e.to_ptr(), > + }; [Severity: Low] Object<T> documents an "# Invariants" section covering the initialized drm_gem_shmem_object and the imported sgt field. Per the kernel Rust coding guidelines, should constructing Self here carry an // INVARIANT: comment explaining how those invariants are satisfied? [ ... ] > + // SAFETY: We never move out of `self`. Ownership of the allocation > is transferred to the > + // C side. > + let new = KBox::into_raw(unsafe { Pin::into_inner_unchecked(new) }); > + > + // SAFETY: `new` was just produced by `KBox::into_raw` and so is a > valid pointer to an > + // initialized `Self`. `obj.base` is the embedded `drm_gem_object` > the caller expects. > + unsafe { &raw mut (*(*new).obj.get()).base } > + } [Severity: Medium] Does (*new).obj.get() narrow pointer provenance to the obj field here? Opaque::get() takes &self, so (*new).obj.get() creates a temporary shared reference restricted to the obj subobject. When C later passes the returned pointer to Object::free_callback(), container_of! accesses sibling fields and frees the full Object<T> via KBox::from_raw(). Under Rust's aliasing models that is undefined behavior. Using &raw mut (*Opaque::cast_into(&raw const (*new).obj)).base avoids the intermediate reference and preserves the full allocation provenance. -- Sashiko AI review ยท https://sashiko.dev/#/patchset/20261205-rust-drm-gem-shmem-imported-sgt-v4-0-f6ccbdafb...@jannau.net?part=5
