xe_bo_pci_dev_remove_all() drops all DMA mappings on device removal:
non-exported bos are purged and exported dma-bufs are moved out to
system. However, nothing prevents an exported dma-buf from being moved
back to TT afterwards, e.g. via DMA_BUF_IOCTL_SYNC ->
xe_dma_buf_begin_cpu_access() -> xe_bo_migrate(bo, XE_PL_TT), which
re-establishes a DMA mapping for a device that is going away. Once the
pci device has been removed from its IOMMU group, unmapping that tt on
dma-buf release dereferences a NULL iommu group.
Refuse to DMA map tt pages once the device is unplugged. drm_dev_unplug()
completes before xe_bo_pci_dev_remove_all() evicts bos under their dma-resv
lock and the map runs under the same lock, so a mapping created before
the unplug is evicted, and any later map attempt sees the unplugged
device. begin_cpu_access() already ignores migration failures, leaving
CPU access in the system placement.
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, 10 insertions(+)
diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c
index 41092e2810b4..8ff92b2c9176 100644
--- a/drivers/gpu/drm/xe/xe_bo.c
+++ b/drivers/gpu/drm/xe/xe_bo.c
@@ -499,6 +499,16 @@ static int xe_tt_map_sg(struct xe_device *xe, struct
ttm_tt *tt)
if (xe_tt->sg)
return 0;
+ /*
+ * Once the device is unplugged, xe_bo_pci_dev_remove_all() has dropped
+ * (or is about to drop, under each bo's lock) all DMA mappings. Don't
+ * create new ones, e.g. when an exported dma-buf moved out to system
+ * is migrated back to TT by begin_cpu_access(): such a mapping would
+ * outlive the device's IOMMU group and crash on unmap.
+ */
+ if (drm_dev_is_unplugged(&xe->drm))
+ return -ENODEV;
+
ret = sg_alloc_table_from_pages_segment(&xe_tt->sgt, tt->pages,
num_pages, 0,
(u64)num_pages << PAGE_SHIFT,
--
2.34.1