xe_bo_apply_to_pinned() drops xe->pinned.lock before invoking the
callback with only a bo reference held. Unlike the other pinned
callbacks, xe_bo_dma_unmap_pinned() does not take the bo's dma-resv lock,
so on device removal it can race with an importer unpinning an exported
bo (xe_bo_unpin_external()) followed by an eviction that unmaps the same
tt, resulting in a double DMA unmap and sg table free. The imported-bo
branch also calls dma_buf_unmap_sgt(), which asserts that the dma-resv
lock is held.

Take the bo lock around the unmap.

Fixes: 3cbb651117ff ("drm/xe/bo: Add a bo remove callback")
Cc: Thomas Hellström <[email protected]>
Cc: Matthew Auld <[email protected]>
Cc: [email protected] # v6.16+
Signed-off-by: Matthew Brost <[email protected]>
---
 drivers/gpu/drm/xe/xe_bo.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c
index 8ff92b2c9176..878a1b0e7dac 100644
--- a/drivers/gpu/drm/xe/xe_bo.c
+++ b/drivers/gpu/drm/xe/xe_bo.c
@@ -2585,8 +2585,15 @@ int xe_bo_restore_pinned(struct xe_bo *bo)
 int xe_bo_dma_unmap_pinned(struct xe_bo *bo)
 {
        struct ttm_buffer_object *ttm_bo = &bo->ttm;
-       struct ttm_tt *tt = ttm_bo->ttm;
+       struct ttm_tt *tt;
 
+       /*
+        * The caller only holds a reference: without the lock, a concurrent
+        * unpin followed by eviction or destruction may unmap, or free, the
+        * same tt.
+        */
+       xe_bo_lock(bo, false);
+       tt = ttm_bo->ttm;
        if (tt) {
                struct xe_ttm_tt *xe_tt = container_of(tt, typeof(*xe_tt), ttm);
 
@@ -2601,6 +2608,7 @@ int xe_bo_dma_unmap_pinned(struct xe_bo *bo)
                                       &xe_tt->ttm);
                }
        }
+       xe_bo_unlock(bo);
 
        return 0;
 }
-- 
2.34.1

Reply via email to