* [PATCH v2 1/2] drm/xe: Snapshot inline DMA addresses before staging page tables
2026-12-04 6:53 [PATCH v2 0/2] Fix invalidated inline DMA mappings in GPUSVM Arvind Yadav
@ 2026-12-04 6:53 ` Arvind Yadav
2026-12-04 6:53 ` [PATCH v2 2/2] drm/gpusvm: Clear inline DMA mapping state on unmap Arvind Yadav
1 sibling, 0 replies; 3+ messages in thread
From: Arvind Yadav @ 2026-12-04 6:53 UTC (permalink / raw)
To: dri-devel, intel-xe, linux-kernel
Cc: matthew.brost, thomas.hellstrom, maarten.lankhorst, mripard,
tzimmermann, airlied, simona, himal.prasad.ghimiray
Clearing inline DMA storage during unmap can race with
xe_pt_stage_bind(). The userptr path reads it without notifier_lock,
and the SVM path keeps a pointer after releasing the lock.
Protect the userptr lookup with notifier_lock and copy inline entries
for both paths. Use the copy throughout page-table staging.
For invalidated userptrs, return -EAGAIN in fault mode. Otherwise,
stage cleared PTEs and keep those tiles marked invalidated at commit
until exec or the rebind worker repins and rebinds them.
This prepares Xe for clearing inline DMA storage during unmap.
v2:
- Add a preparatory Xe patch to protect DMA lookup and snapshot
inline entries for userptr and SVM binds.
Cc: Matthew Brost <matthew.brost@intel.com>
Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Cc: Himal Prasad Ghimiray <himal.prasad.ghimiray@intel.com>
Signed-off-by: Arvind Yadav <arvind.yadav@intel.com>
---
drivers/gpu/drm/xe/xe_pt.c | 82 ++++++++++++++++++++++++++++----
drivers/gpu/drm/xe/xe_pt_types.h | 2 +
2 files changed, 76 insertions(+), 8 deletions(-)
diff --git a/drivers/gpu/drm/xe/xe_pt.c b/drivers/gpu/drm/xe/xe_pt.c
index 0e0bf428df0f..4a6135ab2c8d 100644
--- a/drivers/gpu/drm/xe/xe_pt.c
+++ b/drivers/gpu/drm/xe/xe_pt.c
@@ -770,6 +770,7 @@ static u32 xe_pt_target_leaf_level_from_bo(struct xe_device *xe,
* the main tree at commit time.
* @num_entries: On output contains the number of @entries used.
* @clear_pt: Clear the page table entries.
+ * @pt_cleared: Whether the staged page table entries were cleared.
*
* This function builds a disconnected page-table tree for a given address
* range. The tree is connected to the main vm tree for the gpu using
@@ -784,11 +785,12 @@ static int
xe_pt_stage_bind(struct xe_tile *tile, struct xe_vma *vma,
struct xe_svm_range *range,
struct xe_vm_pgtable_update *entries,
- u32 *num_entries, bool clear_pt)
+ u32 *num_entries, bool clear_pt, bool *pt_cleared)
{
struct xe_device *xe = tile_to_xe(tile);
struct xe_bo *bo = xe_vma_bo(vma);
struct xe_res_cursor curs = {};
+ struct drm_pagemap_addr inline_addr;
struct xe_vm *vm = xe_vma_vm(vma);
struct xe_pt_stage_bind_walk xe_walk = {
.base = {
@@ -842,6 +844,11 @@ xe_pt_stage_bind(struct xe_tile *tile, struct xe_vma *vma,
bool contiguous;
addr = xe_svm_range_first_dma(range, &contiguous);
+ /* Keep inline storage stable after dropping the notifier lock. */
+ if (contiguous) {
+ inline_addr = *addr;
+ addr = &inline_addr;
+ }
xe_res_first_dma(addr, 0, xe_svm_range_size(range),
contiguous, &curs);
xe_svm_range_debug(range, "BIND PREPARE - MIXED");
@@ -876,13 +883,38 @@ xe_pt_stage_bind(struct xe_tile *tile, struct xe_vma *vma,
if (!xe_vma_is_null(vma) && !range && !is_purged) {
if (xe_vma_is_userptr(vma)) {
+ struct xe_userptr_vma *uvma = to_userptr_vma(vma);
const struct drm_pagemap_addr *addr;
bool contiguous;
- addr = drm_gpusvm_pages_first_dma(&to_userptr_vma(vma)->userptr.pages,
- &contiguous);
+ xe_svm_notifier_lock(vm);
+ if (xe_vma_userptr_check_repin(uvma))
+ addr = NULL;
+ else
+ addr = drm_gpusvm_pages_first_dma(&uvma->userptr.pages,
+ &contiguous);
+ if (!addr) {
+ xe_svm_notifier_unlock(vm);
+ if (xe_vm_in_fault_mode(vm))
+ return -EAGAIN;
+
+ /* Exec or the rebind worker will repin invalidated userptrs. */
+ xe_walk.clear_pt = true;
+ curs.size = xe_vma_size(vma);
+ goto walk_pt;
+ }
+ if (contiguous) {
+ inline_addr = *addr;
+ addr = &inline_addr;
+ }
xe_res_first_dma(addr, 0, xe_vma_size(vma), contiguous,
&curs);
+ /*
+ * The snapshot protects cursor storage, not the DMA mapping.
+ * Fault-mode binds recheck validity at pre-commit. Other VMs
+ * repin and rebind invalidated userptrs before execution.
+ */
+ xe_svm_notifier_unlock(vm);
} else if (xe_bo_is_vram(bo) || xe_bo_is_stolen(bo))
xe_res_first(bo->ttm.resource, xe_vma_bo_offset(vma),
xe_vma_size(vma), &curs);
@@ -900,6 +932,7 @@ xe_pt_stage_bind(struct xe_tile *tile, struct xe_vma *vma,
&xe_walk.base);
*num_entries = xe_walk.wupd.num_used_entries;
+ *pt_cleared = xe_walk.clear_pt;
return ret;
}
@@ -1291,13 +1324,14 @@ static int
xe_pt_prepare_bind(struct xe_tile *tile, struct xe_vma *vma,
struct xe_svm_range *range,
struct xe_vm_pgtable_update *entries,
- u32 *num_entries, bool invalidate_on_bind)
+ u32 *num_entries, bool invalidate_on_bind, bool *pt_cleared)
{
int err;
*num_entries = 0;
+ *pt_cleared = false;
err = xe_pt_stage_bind(tile, vma, range, entries, num_entries,
- invalidate_on_bind);
+ invalidate_on_bind, pt_cleared);
if (!err)
xe_tile_assert(tile, *num_entries);
@@ -2166,7 +2200,8 @@ static int bind_op_prepare(struct xe_vm *vm, struct xe_tile *tile,
return err;
err = xe_pt_prepare_bind(tile, vma, NULL, pt_op->entries,
- &pt_op->num_entries, invalidate_on_bind);
+ &pt_op->num_entries, invalidate_on_bind,
+ &pt_op->pt_cleared);
if (!err) {
xe_tile_assert(tile, pt_op->num_entries <=
ARRAY_SIZE(pt_op->entries));
@@ -2234,7 +2269,7 @@ static int bind_range_prepare(struct xe_vm *vm, struct xe_tile *tile,
pt_op->rebind = BIT(tile->id) & range->tile_present;
err = xe_pt_prepare_bind(tile, vma, range, pt_op->entries,
- &pt_op->num_entries, false);
+ &pt_op->num_entries, false, &pt_op->pt_cleared);
if (!err) {
xe_tile_assert(tile, pt_op->num_entries <=
ARRAY_SIZE(pt_op->entries));
@@ -2869,7 +2904,6 @@ xe_pt_update_ops_run(struct xe_device *xe, struct xe_vma_ops *vops)
xe_pt_commit(pt_op->vma, pt_op->entries,
pt_op->num_entries,
&pt_update_ops->pt_job_ops->deferred);
- pt_op->vma = NULL; /* skip in xe_pt_update_ops_abort */
tile_mask |= BIT(tile->id);
}
}
@@ -2905,6 +2939,38 @@ xe_pt_update_ops_run(struct xe_device *xe, struct xe_vma_ops *vops)
tile_mask);
}
+ /*
+ * op_commit() assumes a normal bind installed valid PTEs. Restore the
+ * actual userptr state from the staged operations, in operation order,
+ * before releasing the notifier lock. With shared page tables, tile 0
+ * determines the state for all tiles.
+ */
+ for_each_tile(tile, xe, j) {
+ struct xe_vm_pgtable_update_ops *pt_update_ops =
+ &vops->pt_update_ops[j];
+ u8 mask = xe->info.has_pt_mirror ? BIT(tile->id) : tile_mask;
+
+ if (pt_mirroring_disabled_for_tile(vm, j))
+ continue;
+
+ for (i = 0; i < get_current_op(pt_update_ops); ++i) {
+ struct xe_vm_pgtable_update_op *pt_op =
+ to_pt_op(pt_update_ops, i);
+ struct xe_vma *vma = pt_op->vma;
+
+ if (pt_op->bind && xe_vma_is_userptr(vma)) {
+ /* Pairs with READ_ONCE in xe_vm_has_valid_gpu_mapping(). */
+ if (pt_op->pt_cleared)
+ WRITE_ONCE(vma->tile_invalidated,
+ vma->tile_invalidated | mask);
+ else
+ WRITE_ONCE(vma->tile_invalidated,
+ vma->tile_invalidated & ~mask);
+ }
+ pt_op->vma = NULL; /* skip in xe_pt_update_ops_abort */
+ }
+ }
+
if (needs_svm_lock)
xe_pt_svm_userptr_notifier_unlock(vm);
diff --git a/drivers/gpu/drm/xe/xe_pt_types.h b/drivers/gpu/drm/xe/xe_pt_types.h
index 0d4bac22ee6c..56f8c2a00762 100644
--- a/drivers/gpu/drm/xe/xe_pt_types.h
+++ b/drivers/gpu/drm/xe/xe_pt_types.h
@@ -95,6 +95,8 @@ struct xe_vm_pgtable_update_op {
bool bind;
/** @rebind: is a rebind */
bool rebind;
+ /** @pt_cleared: bind staged cleared PTEs instead of DMA mappings */
+ bool pt_cleared;
};
/**
--
2.43.0
^ permalink raw reply [flat|nested] 3+ messages in thread* [PATCH v2 2/2] drm/gpusvm: Clear inline DMA mapping state on unmap
2026-12-04 6:53 [PATCH v2 0/2] Fix invalidated inline DMA mappings in GPUSVM Arvind Yadav
2026-12-04 6:53 ` [PATCH v2 1/2] drm/xe: Snapshot inline DMA addresses before staging page tables Arvind Yadav
@ 2026-12-04 6:53 ` Arvind Yadav
1 sibling, 0 replies; 3+ messages in thread
From: Arvind Yadav @ 2026-12-04 6:53 UTC (permalink / raw)
To: dri-devel, intel-xe, linux-kernel
Cc: matthew.brost, thomas.hellstrom, maarten.lankhorst, mripard,
tzimmermann, airlied, simona, himal.prasad.ghimiray
Unmap clears has_dma_mapping but leaves the inline DMA address and
inline_dma_mapping flag intact.
When a devmem callback is present, get_pages() continues through HMM
even with a valid DMA mapping. Invalidation during HMM can leave stale
inline state while HMM refreshes notifier_seq. The later sequence
check can then pass, but DMA array allocation is skipped. The mapper
treats the stored DMA address as an array pointer.
BUG: KASAN: user-memory-access in drm_gpusvm_get_pages+0xe12/0x1380 [drm_gpusvm_helper]
Write of size 16 at addr 000000027abfd000 by task kworker/u64:71/693678
Workqueue: xe_prefetch_work_queue prefetch_work_func [xe]
Clear the inline address and flag during unmap so the allocation pass
after HMM can allocate a DMA array. Update the helper documentation
to describe the inline entry's lifetime.
v2:
- Clear inline DMA mapping state in __drm_gpusvm_unmap_pages()
instead of adding a retry in drm_gpusvm_get_pages() (Matt).
- Update the inline DMA helper's lifetime documentation. (Sashiko)
Fixes: 8d242e452ed7 ("drm/gpusvm: keep a single DMA mapping inline for THP")
Cc: Matthew Brost <matthew.brost@intel.com>
Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Cc: Himal Prasad Ghimiray <himal.prasad.ghimiray@intel.com>
Suggested-by: Matthew Brost <matthew.brost@intel.com>
Signed-off-by: Arvind Yadav <arvind.yadav@intel.com>
---
drivers/gpu/drm/drm_gpusvm.c | 5 +++++
include/drm/drm_gpusvm.h | 14 ++++++++------
2 files changed, 13 insertions(+), 6 deletions(-)
diff --git a/drivers/gpu/drm/drm_gpusvm.c b/drivers/gpu/drm/drm_gpusvm.c
index de2fa2684e84..1a61b13384a0 100644
--- a/drivers/gpu/drm/drm_gpusvm.c
+++ b/drivers/gpu/drm/drm_gpusvm.c
@@ -1286,6 +1286,11 @@ static void __drm_gpusvm_unmap_pages(struct drm_gpusvm *gpusvm,
i += 1 << addr->order;
}
+ if (flags.inline_dma_mapping) {
+ svm_pages->inline_addr = (struct drm_pagemap_addr){};
+ flags.inline_dma_mapping = false;
+ }
+
/* WRITE_ONCE pairs with READ_ONCE for opportunistic checks */
flags.has_devmem_pages = false;
flags.has_dma_mapping = false;
diff --git a/include/drm/drm_gpusvm.h b/include/drm/drm_gpusvm.h
index b43432af5637..9d55c34ffbb8 100644
--- a/include/drm/drm_gpusvm.h
+++ b/include/drm/drm_gpusvm.h
@@ -428,12 +428,14 @@ drm_gpusvm_pages_read_retry(struct drm_gpusvm *gpusvm,
* from the order. The order still states what one PTE may cover: the range
* length for a huge page, PAGE_SIZE for an IOVA mapped range of single pages.
*
- * Only get_pages() and the free path switch between the two union members.
- * Both hold the notifier lock for read, so taking that lock does not stop
- * them; callers need the driver lock that does, which every reader of the
- * addresses holds anyway. The notifier never touches the union, so the
- * pointer returned here stays good and can then be used under the notifier
- * lock.
+ * Callers must hold the driver lock that serializes get_pages() and the
+ * free path. Invalidation can clear inline storage under the notifier
+ * lock, so callers must also hold that lock while resolving and copying
+ * an inline entry.
+ *
+ * To use an inline entry after releasing the notifier lock, copy it while
+ * holding the lock and use the copy. The copy does not keep the DMA mapping
+ * valid. Callers must still validate it before committing GPU access.
*
* Return: Pointer to the first device address, NULL if none is populated.
*/
--
2.43.0
^ permalink raw reply [flat|nested] 3+ messages in thread