[PATCH v2 1/2] drm/xe: Snapshot inline DMA addresses before staging page tables
From: Arvind Yadav
Date: Fri Dec 04 2026 - 01:53:44 EST
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@xxxxxxxxx>
Cc: Thomas Hellström <thomas.hellstrom@xxxxxxxxxxxxxxx>
Cc: Himal Prasad Ghimiray <himal.prasad.ghimiray@xxxxxxxxx>
Signed-off-by: Arvind Yadav <arvind.yadav@xxxxxxxxx>
---
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