On Mon Mar 16, 2026 at 10:16 PM CET, Lyude Paul wrote:
- /// Creates (if necessary) and returns an immutable reference to a scatter-gather table of DMA
- /// pages for this object.
- ///
- /// This will pin the object in memory.
- #[inline]
- pub fn sg_table(&self) -> Result<&scatterlist::SGTable> {
// SAFETY:// - drm_gem_shmem_get_pages_sgt is thread-safe.// - drm_gem_shmem_get_pages_sgt returns either a valid pointer to a scatterlist, or an// error pointer.let sgt =from_err_ptr(unsafe { bindings::drm_gem_shmem_get_pages_sgt(self.as_raw_shmem()) })?;
This is unsound as nothing guarantees that the device used by drm_gem_shmem_get_pages_sgt() is actually bound to the calling driver. It is also not guaranteed that the DMA mapping within the returned &SGTable does not out-live driver unbind.
There are two possible solutions.
(1) Change drm_gem_shmem_get_pages_sgt() to provide this guarantee.
(2) Don't use drm_gem_shmem_get_pages_sgt() in the first place and instead use SGTable::new(), which guarantees to destroy the backing DMA mapping on driver unbind.
In any case, this function needs to take a &Device<Bound> argument that matches the bus devices stored in the backing GEM object.
// SAFETY: We checked above that `sgt` is not an error pointer, so it must be a valid// pointer to a scatterlistOk(unsafe { scatterlist::SGTable::from_raw(sgt) })- }
+}