[RFC 5/7] drm/xe/vm: Deactivate madvise notifier on GPU touch

Arvind Yadav
2026-02-19T09:13:36Z

MADVISE_AUTORESET notifier is only needed while VMA is CPU-only.
After GPU touch, the existing SVM notifier handles munmap.

Add 'active' flag to xe_madvise_notifier, cleared on first GPU touch.
The callback checks this flag and returns early when inactive.

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_svm.c | 5 +++-
drivers/gpu/drm/xe/xe_vm.h | 6 ++--
drivers/gpu/drm/xe/xe_vm_madvise.c | 46 ++++++++++++++++++++++++++++++
drivers/gpu/drm/xe/xe_vm_madvise.h | 2 ++
drivers/gpu/drm/xe/xe_vm_types.h | 2 ++
5 files changed, 58 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_svm.c b/drivers/gpu/drm/xe/xe_svm.c
index b9dbbb245779..3f09f5f6481f 100644
--- a/drivers/gpu/drm/xe/xe_svm.c
+++ b/drivers/gpu/drm/xe/xe_svm.c
@@ -21,6 +21,7 @@
#include "xe_tile.h"
#include "xe_ttm_vram_mgr.h"
#include "xe_vm.h"
+#include "xe_vm_madvise.h"
#include "xe_vm_types.h"
#include "xe_vram_types.h"

@@ -1367,8 +1368,10 @@ int xe_svm_handle_pagefault(struct xe_vm *vm, struct xe_vma *vma,
lockdep_assert_held_write(&vm->lock);

/* Transition CPU-only -> GPU-touched before installing PTEs. */
- if (xe_vma_has_cpu_autoreset_active(vma))
+ if (xe_vma_has_cpu_autoreset_active(vma)) {
xe_vma_gpu_touch(vma);
+ xe_vm_madvise_gpu_touch(vm, vma);
+ }

retry:
need_vram = xe_vma_need_vram_for_atomic(vm->xe, vma, atomic);
diff --git a/drivers/gpu/drm/xe/xe_vm.h b/drivers/gpu/drm/xe/xe_vm.h
index 3dc549550c91..f353ab928e4c 100644
--- a/drivers/gpu/drm/xe/xe_vm.h
+++ b/drivers/gpu/drm/xe/xe_vm.h
@@ -426,12 +426,14 @@ void xe_vma_mem_attr_copy(struct xe_vma_mem_attr *to, struct xe_vma_mem_attr *fr

/**
* xe_vma_gpu_touch() - Mark VMA as GPU-touched
- * @vma: VMA to mark
+ * @vma: VMA to transition
*
- * Clear XE_VMA_CPU_AUTORESET_ACTIVE. Must be done before first GPU PTE install.
+ * Clears CPU_AUTORESET_ACTIVE flag. Call xe_vm_madvise_gpu_touch() separately
+ * to deactivate the madvise notifier.
*/
static inline void xe_vma_gpu_touch(struct xe_vma *vma)
{
vma->gpuva.flags &= ~XE_VMA_CPU_AUTORESET_ACTIVE;
}
+
#endif
diff --git a/drivers/gpu/drm/xe/xe_vm_madvise.c b/drivers/gpu/drm/xe/xe_vm_madvise.c
index 4c0ffb100bcc..98663707d039 100644
--- a/drivers/gpu/drm/xe/xe_vm_madvise.c
+++ b/drivers/gpu/drm/xe/xe_vm_madvise.c
@@ -657,6 +657,9 @@ static bool xe_madvise_notifier_callback(struct mmu_interval_notifier *mni,
if (range->event != MMU_NOTIFY_UNMAP)
return true;

+ if (!atomic_read(&notifier->active))
+ return true;
+
/*
* Best-effort: skip in non-blockable contexts to avoid building up work.
* Correctness does not rely on this notifier - CPU_AUTORESET_ACTIVE flag
@@ -857,6 +860,7 @@ int xe_vm_madvise_register_notifier_range(struct xe_vm *vm, u64 start, u64 end)
notifier->vm = xe_vm_get(vm);
notifier->vma_start = start;
notifier->vma_end = end;
+ atomic_set(&notifier->active, 1);
INIT_LIST_HEAD(&notifier->list);

err = mmu_interval_notifier_insert(&notifier->mmu_notifier,
@@ -894,3 +898,45 @@ int xe_vm_madvise_register_notifier_range(struct xe_vm *vm, u64 start, u64 end)

return 0;
}
+
+/**
+ * xe_vm_deactivate_madvise_notifier_for_range - Deactivate notifier for a range
+ * @vm: VM
+ * @start: Start address (page-aligned)
+ * @end: End address (page-aligned)
+ *
+ * Called when GPU touches a VMA - disables munmap processing for this range.
+ * Notifier remains registered but callback becomes a no-op until VM teardown.
+ */
+void xe_vm_deactivate_madvise_notifier_for_range(struct xe_vm *vm, u64 start, u64 end)
+{
+ struct xe_madvise_notifier *notifier;
+
+ /* Skip if madvise infrastructure not initialized */
+ if (!READ_ONCE(vm->svm.madvise_work.wq))
+ return;
+
+ mutex_lock(&vm->svm.madvise_notifiers.lock);
+ /* Deactivate overlapping notifiers (VMA splits may create multiple) */
+ list_for_each_entry(notifier, &vm->svm.madvise_notifiers.list, list) {
+ if (notifier->vma_start < end && notifier->vma_end > start)
+ atomic_set(&notifier->active, 0);
+ }
+ mutex_unlock(&vm->svm.madvise_notifiers.lock);
+}
+
+/**
+ * xe_vm_madvise_gpu_touch() - Deactivate madvise notifier on GPU touch
+ * @vm: VM
+ * @vma: VMA that was GPU-touched
+ *
+ * Deactivates the madvise notifier for this VMA's range after GPU touch.
+ * Call after xe_vma_gpu_touch() clears the CPU_AUTORESET_ACTIVE flag.
+ */
+void xe_vm_madvise_gpu_touch(struct xe_vm *vm, struct xe_vma *vma)
+{
+ if (vma->gpuva.flags & XE_VMA_MADV_AUTORESET)
+ xe_vm_deactivate_madvise_notifier_for_range(vm,
+ xe_vma_start(vma),
+ xe_vma_end(vma));
+}
diff --git a/drivers/gpu/drm/xe/xe_vm_madvise.h b/drivers/gpu/drm/xe/xe_vm_madvise.h
index ba9cd7912113..91417062a33e 100644
--- a/drivers/gpu/drm/xe/xe_vm_madvise.h
+++ b/drivers/gpu/drm/xe/xe_vm_madvise.h
@@ -19,5 +19,7 @@ int xe_vm_madvise_ioctl(struct drm_device *dev, void *data,
int xe_vm_madvise_init(struct xe_vm *vm);
void xe_vm_madvise_fini(struct xe_vm *vm);
int xe_vm_madvise_register_notifier_range(struct xe_vm *vm, u64 start, u64 end);
+void xe_vm_deactivate_madvise_notifier_for_range(struct xe_vm *vm, u64 start, u64 end);
+void xe_vm_madvise_gpu_touch(struct xe_vm *vm, struct xe_vma *vma);

#endif
diff --git a/drivers/gpu/drm/xe/xe_vm_types.h b/drivers/gpu/drm/xe/xe_vm_types.h
index eb978995000c..9cdae3492472 100644
--- a/drivers/gpu/drm/xe/xe_vm_types.h
+++ b/drivers/gpu/drm/xe/xe_vm_types.h
@@ -48,6 +48,8 @@ struct xe_madvise_notifier {
u64 vma_end;
/** @list: Link in vm->svm.madvise_notifiers.list */
struct list_head list;
+ /** @active: Cleared when GPU touches VMA to avoid callback overhead */
+ atomic_t active;
};

#if IS_ENABLED(CONFIG_DRM_XE_DEBUG)
--
2.43.0