* [PATCH v5 26/36] drm/xe: Don't DMA map tt pages after device unplug
[not found] <20261202233323.3743190-1-matthew.brost@intel.com>
@ 2026-12-02 23:33 ` Matthew Brost
2026-12-02 23:33 ` [PATCH v5 27/36] drm/xe/display: Flush cleanup work on unregister Matthew Brost
2026-12-02 23:33 ` [PATCH v5 28/36] drm/xe: Lock the bo in xe_bo_dma_unmap_pinned() Matthew Brost
2 siblings, 0 replies; 4+ messages in thread
From: Matthew Brost @ 2026-12-02 23:33 UTC (permalink / raw)
To: intel-xe, dri-devel; +Cc: Thomas Hellström, Matthew Auld, stable
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 <thomas.hellstrom@linux.intel.com>
Cc: Matthew Auld <matthew.auld@intel.com>
Cc: stable@vger.kernel.org # v6.16+
Signed-off-by: Matthew Brost <matthew.brost@intel.com>
---
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
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH v5 27/36] drm/xe/display: Flush cleanup work on unregister
[not found] <20261202233323.3743190-1-matthew.brost@intel.com>
2026-12-02 23:33 ` [PATCH v5 26/36] drm/xe: Don't DMA map tt pages after device unplug Matthew Brost
@ 2026-12-02 23:33 ` Matthew Brost
2026-12-02 23:33 ` [PATCH v5 28/36] drm/xe: Lock the bo in xe_bo_dma_unmap_pinned() Matthew Brost
2 siblings, 0 replies; 4+ messages in thread
From: Matthew Brost @ 2026-12-02 23:33 UTC (permalink / raw)
To: intel-xe, dri-devel
Cc: Thomas Hellström, Matthew Auld, Jani Nikula, stable
xe_device_remove() calls xe_display_unregister() and then
xe_bo_pci_dev_remove_all(), which drops all DMA mappings by evicting
every bo. Framebuffers pinned for scanout via ttm_bo_pin() are on the
unevictable list and are not on the xe pinned lists, so they rely on
drm_atomic_helper_shutdown() in intel_display_driver_unregister() to be
unpinned first.
However, the shutdown commit unpins the old plane state from the
asynchronous cleanup work (intel_atomic_cleanup_work()), which is not
flushed until intel_display_driver_remove() runs from a devm action,
after xe_device_remove(). A scanout bo can therefore still be pinned
when xe_bo_pci_dev_remove_all() runs and keep its DMA mapping past
device removal. Unmapping it on final release, after the pci device has
left its IOMMU group, dereferences a NULL iommu group.
Flush the cleanup work in xe_display_unregister() so all framebuffers are
unpinned before the bos are evicted.
Fixes: 3cbb651117ff ("drm/xe/bo: Add a bo remove callback")
Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Cc: Matthew Auld <matthew.auld@intel.com>
Cc: Jani Nikula <jani.nikula@intel.com>
Cc: stable@vger.kernel.org # v6.16+
Signed-off-by: Matthew Brost <matthew.brost@intel.com>
---
drivers/gpu/drm/xe/display/xe_display.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/drivers/gpu/drm/xe/display/xe_display.c b/drivers/gpu/drm/xe/display/xe_display.c
index b1a6008bf15c..68d29d78dc8b 100644
--- a/drivers/gpu/drm/xe/display/xe_display.c
+++ b/drivers/gpu/drm/xe/display/xe_display.c
@@ -155,6 +155,14 @@ void xe_display_unregister(struct xe_device *xe)
intel_display_driver_runtime_pm_disable(display);
intel_display_driver_unregister(display);
+
+ /*
+ * The shutdown commit unpins the framebuffers from the asynchronous
+ * cleanup work. Flush it so they are unpinned, and thus visible to
+ * the eviction in xe_bo_pci_dev_remove_all(), before the device
+ * is removed.
+ */
+ intel_display_flush_cleanup_work(display);
}
void xe_display_shutdown(struct xe_device *xe)
--
2.34.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH v5 28/36] drm/xe: Lock the bo in xe_bo_dma_unmap_pinned()
[not found] <20261202233323.3743190-1-matthew.brost@intel.com>
2026-12-02 23:33 ` [PATCH v5 26/36] drm/xe: Don't DMA map tt pages after device unplug Matthew Brost
2026-12-02 23:33 ` [PATCH v5 27/36] drm/xe/display: Flush cleanup work on unregister Matthew Brost
@ 2026-12-02 23:33 ` Matthew Brost
2026-12-03 16:18 ` Matthew Auld
2 siblings, 1 reply; 4+ messages in thread
From: Matthew Brost @ 2026-12-02 23:33 UTC (permalink / raw)
To: intel-xe, dri-devel; +Cc: Thomas Hellström, Matthew Auld, stable
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 <thomas.hellstrom@linux.intel.com>
Cc: Matthew Auld <matthew.auld@intel.com>
Cc: stable@vger.kernel.org # v6.16+
Signed-off-by: Matthew Brost <matthew.brost@intel.com>
---
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
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH v5 28/36] drm/xe: Lock the bo in xe_bo_dma_unmap_pinned()
2026-12-02 23:33 ` [PATCH v5 28/36] drm/xe: Lock the bo in xe_bo_dma_unmap_pinned() Matthew Brost
@ 2026-12-03 16:18 ` Matthew Auld
0 siblings, 0 replies; 4+ messages in thread
From: Matthew Auld @ 2026-12-03 16:18 UTC (permalink / raw)
To: Matthew Brost, intel-xe, dri-devel; +Cc: Thomas Hellström, stable
On 02/12/2026 23:33, Matthew Brost wrote:
> 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 <thomas.hellstrom@linux.intel.com>
> Cc: Matthew Auld <matthew.auld@intel.com>
> Cc: stable@vger.kernel.org # v6.16+
> Signed-off-by: Matthew Brost <matthew.brost@intel.com>
Reviewed-by: Matthew Auld <matthew.auld@intel.com>
^ permalink raw reply [flat|nested] 4+ messages in thread