History

Merge tag 'drm-xe-next-2026-03-26-1' of https://gitlab.freedesktop.org/drm/xe/kernel into drm-next

Hi Dave and Sima, Here goes our late, final drm-xe-next PR towards 7.1. We just purgeable BO uAPI in today, hence the late pull. In the big things we have: - Add support for purgeable buffer objects Thanks, Matt UAPI Changes: - Add support for purgeable buffer objects (Arvind, Himal) Driver Changes: - Remove useless comment (Maarten) - Issue GGTT invalidation under lock in ggtt_node_remove (Brost, Fixes) - Fix mismatched include guards in header files (Shuicheng)

Dave Airlie committed 7 months ago

Commit a51973c2 parents 0d270f0 05c8b1c22 files · +814 −68Browse files

Unified · Split

22 files
  1. drivers/gpu/drm/xe/xe_bo.c +181 −13
  2. drivers/gpu/drm/xe/xe_bo.h +58 −0
  3. drivers/gpu/drm/xe/xe_bo_types.h +6 −0
  4. drivers/gpu/drm/xe/xe_device.c +0 −3
  5. drivers/gpu/drm/xe/xe_dma_buf.c +24 −0
  6. drivers/gpu/drm/xe/xe_ggtt.c +2 −7
  7. drivers/gpu/drm/xe/xe_gt_idle_types.h +3 −3
  8. drivers/gpu/drm/xe/xe_guc_exec_queue_types.h +2 −2
  9. drivers/gpu/drm/xe/xe_heci_gsc.h +3 −3
  10. drivers/gpu/drm/xe/xe_hw_engine_class_sysfs.h +2 −2
  11. drivers/gpu/drm/xe/xe_late_bind_fw_types.h +2 −2
  12. drivers/gpu/drm/xe/xe_pagefault.c +14 −1
  13. drivers/gpu/drm/xe/xe_platform_types.h +2 −2
  14. drivers/gpu/drm/xe/xe_pt.c +33 −7
  15. drivers/gpu/drm/xe/xe_query.c +2 −0
  16. drivers/gpu/drm/xe/xe_svm.c +1 −0
  17. drivers/gpu/drm/xe/xe_tile_printk.h +2 −2
  18. drivers/gpu/drm/xe/xe_vm.c +95 −17
  19. drivers/gpu/drm/xe/xe_vm_madvise.c +299 −4
  20. drivers/gpu/drm/xe/xe_vm_madvise.h +3 −0
  21. drivers/gpu/drm/xe/xe_vm_types.h +11 −0
  22. include/uapi/drm/xe_drm.h +69 −0
drivers/gpu/drm/xe/xe_bo.c+181−13
@@ -838,6 +838,122 @@ static int xe_bo_move_notify(struct xe_bo *bo, · 837 unmodified lines
838838 return 0;
839839 }
840840
841+/**
842+ * xe_bo_set_purgeable_shrinker() - Update shrinker accounting for purgeable state
843+ * @bo: Buffer object
844+ * @new_state: New purgeable state being set
845+ *
846+ * Transfers pages between shrinkable and purgeable buckets when the BO
847+ * purgeable state changes. Called automatically from xe_bo_set_purgeable_state().
848+ */
849+static void xe_bo_set_purgeable_shrinker(struct xe_bo *bo,
850+ enum xe_madv_purgeable_state new_state)
851+{
852+ struct ttm_buffer_object *ttm_bo = &bo->ttm;
853+ struct ttm_tt *tt = ttm_bo->ttm;
854+ struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
855+ struct xe_ttm_tt *xe_tt;
856+ long tt_pages;
857+
858+ xe_bo_assert_held(bo);
859+
860+ if (!tt || !ttm_tt_is_populated(tt))
861+ return;
862+
863+ xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
864+ tt_pages = tt->num_pages;
865+
866+ if (!xe_tt->purgeable && new_state == XE_MADV_PURGEABLE_DONTNEED) {
867+ xe_tt->purgeable = true;
868+ /* Transfer pages from shrinkable to purgeable count */
869+ xe_shrinker_mod_pages(xe->mem.shrinker, -tt_pages, tt_pages);
870+ } else if (xe_tt->purgeable && new_state == XE_MADV_PURGEABLE_WILLNEED) {
871+ xe_tt->purgeable = false;
872+ /* Transfer pages from purgeable to shrinkable count */
873+ xe_shrinker_mod_pages(xe->mem.shrinker, tt_pages, -tt_pages);
874+ }
875+}
876+
877+/**
878+ * xe_bo_set_purgeable_state() - Set BO purgeable state with validation
879+ * @bo: Buffer object
880+ * @new_state: New purgeable state
881+ *
882+ * Sets the purgeable state with lockdep assertions and validates state
883+ * transitions. Once a BO is PURGED, it cannot transition to any other state.
884+ * Invalid transitions are caught with xe_assert(). Shrinker page accounting
885+ * is updated automatically.
886+ */
887+void xe_bo_set_purgeable_state(struct xe_bo *bo,
888+ enum xe_madv_purgeable_state new_state)
889+{
890+ struct xe_device *xe = xe_bo_device(bo);
891+
892+ xe_bo_assert_held(bo);
893+
894+ /* Validate state is one of the known values */
895+ xe_assert(xe, new_state == XE_MADV_PURGEABLE_WILLNEED ||
896+ new_state == XE_MADV_PURGEABLE_DONTNEED ||
897+ new_state == XE_MADV_PURGEABLE_PURGED);
898+
899+ /* Once purged, always purged - cannot transition out */
900+ xe_assert(xe, !(bo->madv_purgeable == XE_MADV_PURGEABLE_PURGED &&
901+ new_state != XE_MADV_PURGEABLE_PURGED));
902+
903+ bo->madv_purgeable = new_state;
904+ xe_bo_set_purgeable_shrinker(bo, new_state);
905+}
906+
907+/**
908+ * xe_ttm_bo_purge() - Purge buffer object backing store
909+ * @ttm_bo: The TTM buffer object to purge
910+ * @ctx: TTM operation context
911+ *
912+ * This function purges the backing store of a BO marked as DONTNEED and
913+ * triggers rebind to invalidate stale GPU mappings. For fault-mode VMs,
914+ * this zaps the PTEs. The next GPU access will trigger a page fault and
915+ * perform NULL rebind (scratch pages or clear PTEs based on VM config).
916+ *
917+ * Return: 0 on success, negative error code on failure
918+ */
919+static int xe_ttm_bo_purge(struct ttm_buffer_object *ttm_bo, struct ttm_operation_ctx *ctx)
920+{
921+ struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
922+ struct ttm_placement place = {};
923+ int ret;
924+
925+ xe_bo_assert_held(bo);
926+
927+ if (!ttm_bo->ttm)
928+ return 0;
929+
930+ if (!xe_bo_madv_is_dontneed(bo))
931+ return 0;
932+
933+ /*
934+ * Use the standard pre-move hook so we share the same cleanup/invalidate
935+ * path as migrations: drop any CPU vmap and schedule the necessary GPU
936+ * unbind/rebind work.
937+ *
938+ * This must be called before ttm_bo_validate() frees the pages.
939+ * May fail in no-wait contexts (fault/shrinker) or if the BO is
940+ * pinned. Keep state unchanged on failure so we don't end up "PURGED"
941+ * with stale mappings.
942+ */
943+ ret = xe_bo_move_notify(bo, ctx);
944+ if (ret)
945+ return ret;
946+
947+ ret = ttm_bo_validate(ttm_bo, &place, ctx);
948+ if (ret)
949+ return ret;
950+
951+ /* Commit the state transition only once invalidation was queued */
952+ xe_bo_set_purgeable_state(bo, XE_MADV_PURGEABLE_PURGED);
953+
954+ return 0;
955+}
956+
841957 static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
842958 struct ttm_operation_ctx *ctx,
843959 struct ttm_resource *new_mem,
@@ -857,6 +973,20 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict, · 13 unmodified lines
857973 ttm && ttm_tt_is_populated(ttm)) ? true : false;
858974 int ret = 0;
859975
976+ /*
977+ * Purge only non-shared BOs explicitly marked DONTNEED by userspace.
978+ * The move_notify callback will handle invalidation asynchronously.
979+ */
980+ if (evict && xe_bo_madv_is_dontneed(bo)) {
981+ ret = xe_ttm_bo_purge(ttm_bo, ctx);
982+ if (ret)
983+ return ret;
984+
985+ /* Free the unused eviction destination resource */
986+ ttm_resource_free(ttm_bo, &new_mem);
987+ return 0;
988+ }
989+
860990 /* Bo creation path, moving to system or TT. */
861991 if ((!old_mem && ttm) && !handle_system_ccs) {
862992 if (new_mem->mem_type == XE_PL_TT)
@@ -1154,6 +1284,9 @@ long xe_bo_shrink(struct ttm_operation_ctx *ctx, struct ttm_buffer_object *bo, · 291 unmodified lines
11541284 lret = xe_bo_move_notify(xe_bo, ctx);
11551285 if (!lret)
11561286 lret = xe_bo_shrink_purge(ctx, bo, scanned);
1287+ if (lret > 0 && xe_bo_madv_is_dontneed(xe_bo))
1288+ xe_bo_set_purgeable_state(xe_bo,
1289+ XE_MADV_PURGEABLE_PURGED);
11571290 goto out_unref;
11581291 }
11591292
@@ -1606,18 +1739,6 @@ static void xe_ttm_bo_delete_mem_notify(struct ttm_buffer_object *ttm_bo) · 446 unmodified lines
16061739 }
16071740 }
16081741
1609−static void xe_ttm_bo_purge(struct ttm_buffer_object *ttm_bo, struct ttm_operation_ctx *ctx)
1610−{
1611− struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
1612−
1613− if (ttm_bo->ttm) {
1614− struct ttm_placement place = {};
1615− int ret = ttm_bo_validate(ttm_bo, &place, ctx);
1616−
1617− drm_WARN_ON(&xe->drm, ret);
1618− }
1619−}
1620−
16211742 static void xe_ttm_bo_swap_notify(struct ttm_buffer_object *ttm_bo)
16221743 {
16231744 struct ttm_operation_ctx ctx = {
@@ -1902,6 +2023,16 @@ static vm_fault_t xe_bo_cpu_fault_fastpath(struct vm_fault *vmf, struct xe_devic · 278 unmodified lines
19022023 if (!dma_resv_trylock(tbo->base.resv))
19032024 goto out_validation;
19042025
2026+ /*
2027+ * Reject CPU faults to purgeable BOs. DONTNEED BOs can be purged
2028+ * at any time, and purged BOs have no backing store. Either case
2029+ * is undefined behavior for CPU access.
2030+ */
2031+ if (xe_bo_madv_is_dontneed(bo) || xe_bo_is_purged(bo)) {
2032+ ret = VM_FAULT_SIGBUS;
2033+ goto out_unlock;
2034+ }
2035+
19052036 if (xe_ttm_bo_is_imported(tbo)) {
19062037 ret = VM_FAULT_SIGBUS;
19072038 drm_dbg(&xe->drm, "CPU trying to access an imported buffer object.\n");
@@ -1992,6 +2123,15 @@ static vm_fault_t xe_bo_cpu_fault(struct vm_fault *vmf) · 84 unmodified lines
19922123 if (err)
19932124 break;
19942125
2126+ /*
2127+ * Reject CPU faults to purgeable BOs. DONTNEED BOs can be
2128+ * purged at any time, and purged BOs have no backing store.
2129+ */
2130+ if (xe_bo_madv_is_dontneed(bo) || xe_bo_is_purged(bo)) {
2131+ err = -EFAULT;
2132+ break;
2133+ }
2134+
19952135 if (xe_ttm_bo_is_imported(tbo)) {
19962136 err = -EFAULT;
19972137 drm_dbg(&xe->drm, "CPU trying to access an imported buffer object.\n");
@@ -2069,10 +2209,35 @@ static const struct vm_operations_struct xe_gem_vm_ops = { · 71 unmodified lines
20692209 .access = xe_bo_vm_access,
20702210 };
20712211
2212+static int xe_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
2213+{
2214+ struct xe_bo *bo = gem_to_xe_bo(obj);
2215+ int err = 0;
2216+
2217+ /*
2218+ * Reject mmap of purgeable BOs. DONTNEED BOs can be purged
2219+ * at any time, making CPU access undefined behavior. Purged BOs have
2220+ * no backing store and are permanently invalid.
2221+ */
2222+ err = xe_bo_lock(bo, true);
2223+ if (err)
2224+ return err;
2225+
2226+ if (xe_bo_madv_is_dontneed(bo))
2227+ err = -EBUSY;
2228+ else if (xe_bo_is_purged(bo))
2229+ err = -EINVAL;
2230+ xe_bo_unlock(bo);
2231+ if (err)
2232+ return err;
2233+
2234+ return drm_gem_ttm_mmap(obj, vma);
2235+}
2236+
20722237 static const struct drm_gem_object_funcs xe_gem_object_funcs = {
20732238 .free = xe_gem_object_free,
20742239 .close = xe_gem_object_close,
2075− .mmap = drm_gem_ttm_mmap,
2240+ .mmap = xe_gem_object_mmap,
20762241 .export = xe_gem_prime_export,
20772242 .vm_ops = &xe_gem_vm_ops,
20782243 };
@@ -2198,6 +2363,9 @@ struct xe_bo *xe_bo_init_locked(struct xe_device *xe, struct xe_bo *bo, · 119 unmodified lines
21982363 #endif
21992364 INIT_LIST_HEAD(&bo->vram_userfault_link);
22002365
2366+ /* Initialize purge advisory state */
2367+ bo->madv_purgeable = XE_MADV_PURGEABLE_WILLNEED;
2368+
22012369 drm_gem_private_object_init(&xe->drm, &bo->ttm.base, size);
22022370
22032371 if (resv) {
drivers/gpu/drm/xe/xe_bo.h+58−0
@@ -87,6 +87,28 @@ · 86 unmodified lines
8787
8888 #define XE_PCI_BARRIER_MMAP_OFFSET (0x50 << XE_PTE_SHIFT)
8989
90+/**
91+ * enum xe_madv_purgeable_state - Buffer object purgeable state enumeration
92+ *
93+ * This enum defines the possible purgeable states for a buffer object,
94+ * allowing userspace to provide memory usage hints to the kernel for
95+ * better memory management under pressure.
96+ *
97+ * @XE_MADV_PURGEABLE_WILLNEED: The buffer object is needed and should not be purged.
98+ * This is the default state.
99+ * @XE_MADV_PURGEABLE_DONTNEED: The buffer object is not currently needed and can be
100+ * purged by the kernel under memory pressure.
101+ * @XE_MADV_PURGEABLE_PURGED: The buffer object has been purged by the kernel.
102+ *
103+ * Accessing a purged buffer will result in an error. Per i915 semantics,
104+ * once purged, a BO remains permanently invalid and must be destroyed and recreated.
105+ */
106+enum xe_madv_purgeable_state {
107+ XE_MADV_PURGEABLE_WILLNEED,
108+ XE_MADV_PURGEABLE_DONTNEED,
109+ XE_MADV_PURGEABLE_PURGED,
110+};
111+
90112 struct sg_table;
91113
92114 struct xe_bo *xe_bo_alloc(void);
@@ -215,6 +237,42 @@ static inline bool xe_bo_is_protected(const struct xe_bo *bo) · 122 unmodified lines
215237 return bo->pxp_key_instance;
216238 }
217239
240+/**
241+ * xe_bo_is_purged() - Check if buffer object has been purged
242+ * @bo: The buffer object to check
243+ *
244+ * Checks if the buffer object's backing store has been discarded by the
245+ * kernel due to memory pressure after being marked as purgeable (DONTNEED).
246+ * Once purged, the BO cannot be restored and any attempt to use it will fail.
247+ *
248+ * Context: Caller must hold the BO's dma-resv lock
249+ * Return: true if the BO has been purged, false otherwise
250+ */
251+static inline bool xe_bo_is_purged(struct xe_bo *bo)
252+{
253+ xe_bo_assert_held(bo);
254+ return bo->madv_purgeable == XE_MADV_PURGEABLE_PURGED;
255+}
256+
257+/**
258+ * xe_bo_madv_is_dontneed() - Check if BO is marked as DONTNEED
259+ * @bo: The buffer object to check
260+ *
261+ * Checks if userspace has marked this BO as DONTNEED (i.e., its contents
262+ * are not currently needed and can be discarded under memory pressure).
263+ * This is used internally to decide whether a BO is eligible for purging.
264+ *
265+ * Context: Caller must hold the BO's dma-resv lock
266+ * Return: true if the BO is marked DONTNEED, false otherwise
267+ */
268+static inline bool xe_bo_madv_is_dontneed(struct xe_bo *bo)
269+{
270+ xe_bo_assert_held(bo);
271+ return bo->madv_purgeable == XE_MADV_PURGEABLE_DONTNEED;
272+}
273+
274+void xe_bo_set_purgeable_state(struct xe_bo *bo, enum xe_madv_purgeable_state new_state);
275+
218276 static inline void xe_bo_unpin_map_no_vm(struct xe_bo *bo)
219277 {
220278 if (likely(bo)) {
drivers/gpu/drm/xe/xe_bo_types.h+6−0
@@ -108,6 +108,12 @@ struct xe_bo { · 107 unmodified lines
108108 * from default
109109 */
110110 u64 min_align;
111+
112+ /**
113+ * @madv_purgeable: user space advise on BO purgeability, protected
114+ * by BO's dma-resv lock.
115+ */
116+ u32 madv_purgeable;
111117 };
112118
113119 #endif
drivers/gpu/drm/xe/xe_device.c+0−3
@@ -390,9 +390,6 @@ bool xe_is_xe_file(const struct file *file) · 389 unmodified lines
390390 }
391391
392392 static struct drm_driver driver = {
393− /* Don't use MTRRs here; the Xserver or userspace app should
394− * deal with them for Intel hardware.
395− */
396393 .driver_features =
397394 DRIVER_GEM |
398395 DRIVER_RENDER | DRIVER_SYNCOBJ |
drivers/gpu/drm/xe/xe_dma_buf.c+24−0
@@ -223,6 +223,26 @@ struct dma_buf *xe_gem_prime_export(struct drm_gem_object *obj, int flags) · 222 unmodified lines
223223 if (bo->vm)
224224 return ERR_PTR(-EPERM);
225225
226+ /*
227+ * Reject exporting purgeable BOs. DONTNEED BOs can be purged
228+ * at any time, making the exported dma-buf unusable. Purged BOs
229+ * have no backing store and are permanently invalid.
230+ */
231+ ret = xe_bo_lock(bo, true);
232+ if (ret)
233+ return ERR_PTR(ret);
234+
235+ if (xe_bo_madv_is_dontneed(bo)) {
236+ ret = -EBUSY;
237+ goto out_unlock;
238+ }
239+
240+ if (xe_bo_is_purged(bo)) {
241+ ret = -EINVAL;
242+ goto out_unlock;
243+ }
244+ xe_bo_unlock(bo);
245+
226246 ret = ttm_bo_setup_export(&bo->ttm, &ctx);
227247 if (ret)
228248 return ERR_PTR(ret);
@@ -232,6 +252,10 @@ struct dma_buf *xe_gem_prime_export(struct drm_gem_object *obj, int flags) · 3 unmodified lines
232252 buf->ops = &xe_dmabuf_ops;
233253
234254 return buf;
255+
256+out_unlock:
257+ xe_bo_unlock(bo);
258+ return ERR_PTR(ret);
235259 }
236260
237261 static struct drm_gem_object *
drivers/gpu/drm/xe/xe_ggtt.c+2−7
@@ -481,15 +481,10 @@ static void ggtt_node_remove(struct xe_ggtt_node *node) · 480 unmodified lines
481481 xe_ggtt_clear(ggtt, xe_ggtt_node_addr(node), xe_ggtt_node_size(node));
482482 drm_mm_remove_node(&node->base);
483483 node->base.size = 0;
484− mutex_unlock(&ggtt->lock);
485−
486− if (!bound)
487− goto free_node;
488−
489− if (node->invalidate_on_remove)
484+ if (bound && node->invalidate_on_remove)
490485 xe_ggtt_invalidate(ggtt);
486+ mutex_unlock(&ggtt->lock);
491487
492−free_node:
493488 ggtt_node_fini(node);
494489 }
495490
drivers/gpu/drm/xe/xe_gt_idle_types.h+3−3
@@ -3,8 +3,8 @@ · 2 unmodified lines
33 * Copyright © 2023 Intel Corporation
44 */
55
6−#ifndef _XE_GT_IDLE_SYSFS_TYPES_H_
7−#define _XE_GT_IDLE_SYSFS_TYPES_H_
6+#ifndef _XE_GT_IDLE_TYPES_H_
7+#define _XE_GT_IDLE_TYPES_H_
88
99 #include <linux/spinlock.h>
1010 #include <linux/types.h>
@@ -40,4 +40,4 @@ struct xe_gt_idle { · 29 unmodified lines
4040 u64 (*idle_residency)(struct xe_guc_pc *pc);
4141 };
4242
43−#endif /* _XE_GT_IDLE_SYSFS_TYPES_H_ */
43+#endif /* _XE_GT_IDLE_TYPES_H_ */
drivers/gpu/drm/xe/xe_guc_exec_queue_types.h+2−2
@@ -3,8 +3,8 @@ · 2 unmodified lines
33 * Copyright © 2022 Intel Corporation
44 */
55
6−#ifndef _XE_GUC_ENGINE_TYPES_H_
7−#define _XE_GUC_ENGINE_TYPES_H_
6+#ifndef _XE_GUC_EXEC_QUEUE_TYPES_H_
7+#define _XE_GUC_EXEC_QUEUE_TYPES_H_
88
99 #include <linux/spinlock.h>
1010 #include <linux/workqueue.h>
drivers/gpu/drm/xe/xe_heci_gsc.h+3−3
@@ -2,8 +2,8 @@ · 1 unmodified line
22 /*
33 * Copyright(c) 2023, Intel Corporation. All rights reserved.
44 */
5−#ifndef __XE_HECI_GSC_DEV_H__
6−#define __XE_HECI_GSC_DEV_H__
5+#ifndef _XE_HECI_GSC_H_
6+#define _XE_HECI_GSC_H_
77
88 #include <linux/types.h>
99
@@ -37,4 +37,4 @@ int xe_heci_gsc_init(struct xe_device *xe); · 27 unmodified lines
3737 void xe_heci_gsc_irq_handler(struct xe_device *xe, u32 iir);
3838 void xe_heci_csc_irq_handler(struct xe_device *xe, u32 iir);
3939
40−#endif /* __XE_HECI_GSC_DEV_H__ */
40+#endif /* _XE_HECI_GSC_H_ */
drivers/gpu/drm/xe/xe_hw_engine_class_sysfs.h+2−2
@@ -3,8 +3,8 @@ · 2 unmodified lines
33 * Copyright © 2023 Intel Corporation
44 */
55
6−#ifndef _XE_ENGINE_CLASS_SYSFS_H_
7−#define _XE_ENGINE_CLASS_SYSFS_H_
6+#ifndef _XE_HW_ENGINE_CLASS_SYSFS_H_
7+#define _XE_HW_ENGINE_CLASS_SYSFS_H_
88
99 #include <linux/kobject.h>
1010
drivers/gpu/drm/xe/xe_late_bind_fw_types.h+2−2
@@ -3,8 +3,8 @@ · 2 unmodified lines
33 * Copyright © 2025 Intel Corporation
44 */
55
6−#ifndef _XE_LATE_BIND_TYPES_H_
7−#define _XE_LATE_BIND_TYPES_H_
6+#ifndef _XE_LATE_BIND_FW_TYPES_H_
7+#define _XE_LATE_BIND_FW_TYPES_H_
88
99 #include <linux/iosys-map.h>
1010 #include <linux/mutex.h>
drivers/gpu/drm/xe/xe_pagefault.c+14−1
@@ -59,6 +59,19 @@ static int xe_pagefault_begin(struct drm_exec *exec, struct xe_vma *vma, · 58 unmodified lines
5959 if (!bo)
6060 return 0;
6161
62+ /*
63+ * Skip validate/migrate for DONTNEED/purged BOs - repopulating
64+ * their pages would prevent the shrinker from reclaiming them.
65+ * For non-scratch VMs there is no safe fallback so fail the fault.
66+ * For scratch VMs let xe_vma_rebind() run normally; it will install
67+ * scratch PTEs so the GPU gets safe zero reads instead of faulting.
68+ */
69+ if (unlikely(xe_bo_madv_is_dontneed(bo) || xe_bo_is_purged(bo))) {
70+ if (!xe_vm_has_scratch(vm))
71+ return -EACCES;
72+ return 0;
73+ }
74+
6275 return need_vram_move ? xe_bo_migrate(bo, vram->placement, NULL, exec) :
6376 xe_bo_validate(bo, vm, true, exec);
6477 }
@@ -145,7 +158,7 @@ static struct xe_vm *xe_pagefault_asid_to_vm(struct xe_device *xe, u32 asid) · 80 unmodified lines
145158
146159 down_read(&xe->usm.lock);
147160 vm = xa_load(&xe->usm.asid_to_vm, asid);
148− if (vm && xe_vm_in_fault_mode(vm))
161+ if (vm && (xe_vm_in_fault_mode(vm) || xe_vm_has_scratch(vm)))
149162 xe_vm_get(vm);
150163 else
151164 vm = ERR_PTR(-EINVAL);
drivers/gpu/drm/xe/xe_platform_types.h+2−2
@@ -3,8 +3,8 @@ · 2 unmodified lines
33 * Copyright © 2022 Intel Corporation
44 */
55
6−#ifndef _XE_PLATFORM_INFO_TYPES_H_
7−#define _XE_PLATFORM_INFO_TYPES_H_
6+#ifndef _XE_PLATFORM_TYPES_H_
7+#define _XE_PLATFORM_TYPES_H_
88
99 /*
1010 * Keep this in graphics version based order and chronological order within a
drivers/gpu/drm/xe/xe_pt.c+33−7
@@ -531,20 +531,26 @@ xe_pt_stage_bind_entry(struct xe_ptw *parent, pgoff_t offset, · 530 unmodified lines
531531 /* Is this a leaf entry ?*/
532532 if (level == 0 || xe_pt_hugepte_possible(addr, next, level, xe_walk)) {
533533 struct xe_res_cursor *curs = xe_walk->curs;
534− bool is_null = xe_vma_is_null(xe_walk->vma);
535− bool is_vram = is_null ? false : xe_res_is_vram(curs);
534+ struct xe_bo *bo = xe_vma_bo(xe_walk->vma);
535+ bool is_null_or_purged = xe_vma_is_null(xe_walk->vma) ||
536+ (bo && xe_bo_is_purged(bo));
537+ bool is_vram = is_null_or_purged ? false : xe_res_is_vram(curs);
536538
537539 XE_WARN_ON(xe_walk->va_curs_start != addr);
538540
539541 if (xe_walk->clear_pt) {
540542 pte = 0;
541543 } else {
542− pte = vm->pt_ops->pte_encode_vma(is_null ? 0 :
544+ /*
545+ * For purged BOs, treat like null VMAs - pass address 0.
546+ * The pte_encode_vma will set XE_PTE_NULL flag for scratch mapping.
547+ */
548+ pte = vm->pt_ops->pte_encode_vma(is_null_or_purged ? 0 :
543549 xe_res_dma(curs) +
544550 xe_walk->dma_offset,
545551 xe_walk->vma,
546552 pat_index, level);
547− if (!is_null)
553+ if (!is_null_or_purged)
548554 pte |= is_vram ? xe_walk->default_vram_pte :
549555 xe_walk->default_system_pte;
550556
@@ -568,7 +574,7 @@ xe_pt_stage_bind_entry(struct xe_ptw *parent, pgoff_t offset, · 17 unmodified lines
568574 if (unlikely(ret))
569575 return ret;
570576
571− if (!is_null && !xe_walk->clear_pt)
577+ if (!is_null_or_purged && !xe_walk->clear_pt)
572578 xe_res_next(curs, next - addr);
573579 xe_walk->va_curs_start = next;
574580 xe_walk->vma->gpuva.flags |= (XE_VMA_PTE_4K << level);
@@ -721,6 +727,26 @@ xe_pt_stage_bind(struct xe_tile *tile, struct xe_vma *vma, · 146 unmodified lines
721727 };
722728 struct xe_pt *pt = vm->pt_root[tile->id];
723729 int ret;
730+ bool is_purged = false;
731+
732+ /*
733+ * Check if BO is purged:
734+ * - Scratch VMs: Use scratch PTEs (XE_PTE_NULL) for safe zero reads
735+ * - Non-scratch VMs: Clear PTEs to zero (non-present) to avoid mapping to phys addr 0
736+ *
737+ * For non-scratch VMs, we force clear_pt=true so leaf PTEs become completely
738+ * zero instead of creating a PRESENT mapping to physical address 0.
739+ */
740+ if (bo && xe_bo_is_purged(bo)) {
741+ is_purged = true;
742+
743+ /*
744+ * For non-scratch VMs, a NULL rebind should use zero PTEs
745+ * (non-present), not a present PTE to phys 0.
746+ */
747+ if (!xe_vm_has_scratch(vm))
748+ xe_walk.clear_pt = true;
749+ }
724750
725751 if (range) {
726752 /* Move this entire thing to xe_svm.c? */
@@ -756,11 +782,11 @@ xe_pt_stage_bind(struct xe_tile *tile, struct xe_vma *vma, · 29 unmodified lines
756782 }
757783
758784 xe_walk.default_vram_pte |= XE_PPGTT_PTE_DM;
759− xe_walk.dma_offset = bo ? vram_region_gpu_offset(bo->ttm.resource) : 0;
785+ xe_walk.dma_offset = (bo && !is_purged) ? vram_region_gpu_offset(bo->ttm.resource) : 0;
760786 if (!range)
761787 xe_bo_assert_held(bo);
762788
763− if (!xe_vma_is_null(vma) && !range) {
789+ if (!xe_vma_is_null(vma) && !range && !is_purged) {
764790 if (xe_vma_is_userptr(vma))
765791 xe_res_first_dma(to_userptr_vma(vma)->userptr.pages.dma_addr, 0,
766792 xe_vma_size(vma), &curs);
drivers/gpu/drm/xe/xe_query.c+2−0
@@ -342,6 +342,8 @@ static int query_config(struct xe_device *xe, struct drm_xe_device_query *query) · 341 unmodified lines
342342 DRM_XE_QUERY_CONFIG_FLAG_HAS_LOW_LATENCY;
343343 config->info[DRM_XE_QUERY_CONFIG_FLAGS] |=
344344 DRM_XE_QUERY_CONFIG_FLAG_HAS_DISABLE_STATE_CACHE_PERF_FIX;
345+ config->info[DRM_XE_QUERY_CONFIG_FLAGS] |=
346+ DRM_XE_QUERY_CONFIG_FLAG_HAS_PURGING_SUPPORT;
345347 config->info[DRM_XE_QUERY_CONFIG_MIN_ALIGNMENT] =
346348 xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K ? SZ_64K : SZ_4K;
347349 config->info[DRM_XE_QUERY_CONFIG_VA_BITS] = xe->info.va_bits;
drivers/gpu/drm/xe/xe_svm.c+1−0
@@ -322,6 +322,7 @@ static void xe_vma_set_default_attributes(struct xe_vma *vma) · 321 unmodified lines
322322 .preferred_loc.migration_policy = DRM_XE_MIGRATE_ALL_PAGES,
323323 .pat_index = vma->attr.default_pat_index,
324324 .atomic_access = DRM_XE_ATOMIC_UNDEFINED,
325+ .purgeable_state = XE_MADV_PURGEABLE_WILLNEED,
325326 };
326327
327328 xe_vma_mem_attr_copy(&vma->attr, &default_attr);
drivers/gpu/drm/xe/xe_tile_printk.h+2−2
@@ -3,8 +3,8 @@ · 2 unmodified lines
33 * Copyright © 2025 Intel Corporation
44 */
55
6−#ifndef _xe_tile_printk_H_
7−#define _xe_tile_printk_H_
6+#ifndef _XE_TILE_PRINTK_H_
7+#define _XE_TILE_PRINTK_H_
88
99 #include "xe_printk.h"
1010
drivers/gpu/drm/xe/xe_vm.c+95−17
@@ -40,6 +40,7 @@ · 39 unmodified lines
4040 #include "xe_tile.h"
4141 #include "xe_tlb_inval.h"
4242 #include "xe_trace_bo.h"
43+#include "xe_vm_madvise.h"
4344 #include "xe_wa.h"
4445
4546 static struct drm_gem_object *xe_vm_obj(struct xe_vm *vm)
@@ -327,6 +328,7 @@ void xe_vm_kill(struct xe_vm *vm, bool unlocked) · 281 unmodified lines
327328 static int xe_gpuvm_validate(struct drm_gpuvm_bo *vm_bo, struct drm_exec *exec)
328329 {
329330 struct xe_vm *vm = gpuvm_to_vm(vm_bo->vm);
331+ struct xe_bo *bo = gem_to_xe_bo(vm_bo->obj);
330332 struct drm_gpuva *gpuva;
331333 int ret;
332334
@@ -335,10 +337,16 @@ static int xe_gpuvm_validate(struct drm_gpuvm_bo *vm_bo, struct drm_exec *exec) · 2 unmodified lines
335337 list_move_tail(&gpuva_to_vma(gpuva)->combined_links.rebind,
336338 &vm->rebind_list);
337339
340+ /* Skip re-populating purged BOs, rebind maps scratch pages. */
341+ if (xe_bo_is_purged(bo)) {
342+ vm_bo->evicted = false;
343+ return 0;
344+ }
345+
338346 if (!try_wait_for_completion(&vm->xe->pm_block))
339347 return -EAGAIN;
340348
341− ret = xe_bo_validate(gem_to_xe_bo(vm_bo->obj), vm, false, exec);
349+ ret = xe_bo_validate(bo, vm, false, exec);
342350 if (ret)
343351 return ret;
344352
@@ -1147,6 +1155,7 @@ static struct xe_vma *xe_vma_create(struct xe_vm *vm, · 802 unmodified lines
11471155 static void xe_vma_destroy_late(struct xe_vma *vma)
11481156 {
11491157 struct xe_vm *vm = xe_vma_vm(vma);
1158+ struct xe_bo *bo = xe_vma_bo(vma);
11501159
11511160 if (vma->ufence) {
11521161 xe_sync_ufence_put(vma->ufence);
@@ -1161,7 +1170,7 @@ static void xe_vma_destroy_late(struct xe_vma *vma) · 8 unmodified lines
11611170 } else if (xe_vma_is_null(vma) || xe_vma_is_cpu_addr_mirror(vma)) {
11621171 xe_vm_put(vm);
11631172 } else {
1164− xe_bo_put(xe_vma_bo(vma));
1173+ xe_bo_put(bo);
11651174 }
11661175
11671176 xe_vma_free(vma);
@@ -1187,6 +1196,7 @@ static void vma_destroy_cb(struct dma_fence *fence, · 19 unmodified lines
11871196 static void xe_vma_destroy(struct xe_vma *vma, struct dma_fence *fence)
11881197 {
11891198 struct xe_vm *vm = xe_vma_vm(vma);
1199+ struct xe_bo *bo = xe_vma_bo(vma);
11901200
11911201 lockdep_assert_held_write(&vm->lock);
11921202 xe_assert(vm->xe, list_empty(&vma->combined_links.destroy));
@@ -1195,9 +1205,10 @@ static void xe_vma_destroy(struct xe_vma *vma, struct dma_fence *fence) · 2 unmodified lines
11951205 xe_assert(vm->xe, vma->gpuva.flags & XE_VMA_DESTROYED);
11961206 xe_userptr_destroy(to_userptr_vma(vma));
11971207 } else if (!xe_vma_is_null(vma) && !xe_vma_is_cpu_addr_mirror(vma)) {
1198− xe_bo_assert_held(xe_vma_bo(vma));
1208+ xe_bo_assert_held(bo);
11991209
12001210 drm_gpuva_unlink(&vma->gpuva);
1211+ xe_bo_recompute_purgeable_state(bo);
12011212 }
12021213
12031214 xe_vm_assert_held(vm);
@@ -1427,6 +1438,9 @@ static u64 xelp_pte_encode_bo(struct xe_bo *bo, u64 bo_offset, · 223 unmodified lines
14271438 static u64 xelp_pte_encode_vma(u64 pte, struct xe_vma *vma,
14281439 u16 pat_index, u32 pt_level)
14291440 {
1441+ struct xe_bo *bo = xe_vma_bo(vma);
1442+ struct xe_vm *vm = xe_vma_vm(vma);
1443+
14301444 pte |= XE_PAGE_PRESENT;
14311445
14321446 if (likely(!xe_vma_read_only(vma)))
@@ -1435,7 +1449,13 @@ static u64 xelp_pte_encode_vma(u64 pte, struct xe_vma *vma, · 2 unmodified lines
14351449 pte |= pte_encode_pat_index(pat_index, pt_level);
14361450 pte |= pte_encode_ps(pt_level);
14371451
1438− if (unlikely(xe_vma_is_null(vma)))
1452+ /*
1453+ * NULL PTEs redirect to scratch page (return zeros on read).
1454+ * Set for: 1) explicit null VMAs, 2) purged BOs on scratch VMs.
1455+ * Never set NULL flag without scratch page - causes undefined behavior.
1456+ */
1457+ if (unlikely(xe_vma_is_null(vma) ||
1458+ (bo && xe_bo_is_purged(bo) && xe_vm_has_scratch(vm))))
14391459 pte |= XE_PTE_NULL;
14401460
14411461 return pte;
@@ -2751,6 +2771,7 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm *vm, struct drm_gpuva_ops *ops, · 1309 unmodified lines
27512771 .atomic_access = DRM_XE_ATOMIC_UNDEFINED,
27522772 .default_pat_index = op->map.pat_index,
27532773 .pat_index = op->map.pat_index,
2774+ .purgeable_state = XE_MADV_PURGEABLE_WILLNEED,
27542775 };
27552776
27562777 flags |= op->map.vma_flags & XE_VMA_CREATE_MASK;
@@ -2990,8 +3011,22 @@ static void vm_bind_ioctl_ops_unwind(struct xe_vm *vm, · 233 unmodified lines
29903011 }
29913012 }
29923013
3014+/**
3015+ * struct xe_vma_lock_and_validate_flags - Flags for vma_lock_and_validate()
3016+ * @res_evict: Allow evicting resources during validation
3017+ * @validate: Perform BO validation
3018+ * @request_decompress: Request BO decompression
3019+ * @check_purged: Reject operation if BO is purged
3020+ */
3021+struct xe_vma_lock_and_validate_flags {
3022+ u32 res_evict : 1;
3023+ u32 validate : 1;
3024+ u32 request_decompress : 1;
3025+ u32 check_purged : 1;
3026+};
3027+
29933028 static int vma_lock_and_validate(struct drm_exec *exec, struct xe_vma *vma,
2994− bool res_evict, bool validate, bool request_decompress)
3029+ struct xe_vma_lock_and_validate_flags flags)
29953030 {
29963031 struct xe_bo *bo = xe_vma_bo(vma);
29973032 struct xe_vm *vm = xe_vma_vm(vma);
@@ -3000,15 +3035,24 @@ static int vma_lock_and_validate(struct drm_exec *exec, struct xe_vma *vma, · 2 unmodified lines
30003035 if (bo) {
30013036 if (!bo->vm)
30023037 err = drm_exec_lock_obj(exec, &bo->ttm.base);
3003− if (!err && validate)
3038+
3039+ /* Reject new mappings to DONTNEED/purged BOs; allow cleanup operations */
3040+ if (!err && flags.check_purged) {
3041+ if (xe_bo_madv_is_dontneed(bo))
3042+ err = -EBUSY; /* BO marked purgeable */
3043+ else if (xe_bo_is_purged(bo))
3044+ err = -EINVAL; /* BO already purged */
3045+ }
3046+
3047+ if (!err && flags.validate)
30043048 err = xe_bo_validate(bo, vm,
30053049 xe_vm_allow_vm_eviction(vm) &&
3006− res_evict, exec);
3050+ flags.res_evict, exec);
30073051
30083052 if (err)
30093053 return err;
30103054
3011− if (request_decompress)
3055+ if (flags.request_decompress)
30123056 err = xe_bo_decompress(bo);
30133057 }
30143058
@@ -3102,10 +3146,14 @@ static int op_lock_and_prep(struct drm_exec *exec, struct xe_vm *vm, · 87 unmodified lines
31023146 case DRM_GPUVA_OP_MAP:
31033147 if (!op->map.invalidate_on_bind)
31043148 err = vma_lock_and_validate(exec, op->map.vma,
3105− res_evict,
3106− !xe_vm_in_fault_mode(vm) ||
3107− op->map.immediate,
3108− op->map.request_decompress);
3149+ (struct xe_vma_lock_and_validate_flags) {
3150+ .res_evict = res_evict,
3151+ .validate = !xe_vm_in_fault_mode(vm) ||
3152+ op->map.immediate,
3153+ .request_decompress =
3154+ op->map.request_decompress,
3155+ .check_purged = true,
3156+ });
31093157 break;
31103158 case DRM_GPUVA_OP_REMAP:
31113159 err = check_ufence(gpuva_to_vma(op->base.remap.unmap->va));
@@ -3114,13 +3162,28 @@ static int op_lock_and_prep(struct drm_exec *exec, struct xe_vm *vm, · 2 unmodified lines
31143162
31153163 err = vma_lock_and_validate(exec,
31163164 gpuva_to_vma(op->base.remap.unmap->va),
3117− res_evict, false, false);
3165+ (struct xe_vma_lock_and_validate_flags) {
3166+ .res_evict = res_evict,
3167+ .validate = false,
3168+ .request_decompress = false,
3169+ .check_purged = false,
3170+ });
31183171 if (!err && op->remap.prev)
31193172 err = vma_lock_and_validate(exec, op->remap.prev,
3120− res_evict, true, false);
3173+ (struct xe_vma_lock_and_validate_flags) {
3174+ .res_evict = res_evict,
3175+ .validate = true,
3176+ .request_decompress = false,
3177+ .check_purged = true,
3178+ });
31213179 if (!err && op->remap.next)
31223180 err = vma_lock_and_validate(exec, op->remap.next,
3123− res_evict, true, false);
3181+ (struct xe_vma_lock_and_validate_flags) {
3182+ .res_evict = res_evict,
3183+ .validate = true,
3184+ .request_decompress = false,
3185+ .check_purged = true,
3186+ });
31243187 break;
31253188 case DRM_GPUVA_OP_UNMAP:
31263189 err = check_ufence(gpuva_to_vma(op->base.unmap.va));
@@ -3129,7 +3192,12 @@ static int op_lock_and_prep(struct drm_exec *exec, struct xe_vm *vm, · 2 unmodified lines
31293192
31303193 err = vma_lock_and_validate(exec,
31313194 gpuva_to_vma(op->base.unmap.va),
3132− res_evict, false, false);
3195+ (struct xe_vma_lock_and_validate_flags) {
3196+ .res_evict = res_evict,
3197+ .validate = false,
3198+ .request_decompress = false,
3199+ .check_purged = false,
3200+ });
31333201 break;
31343202 case DRM_GPUVA_OP_PREFETCH:
31353203 {
@@ -3142,9 +3210,19 @@ static int op_lock_and_prep(struct drm_exec *exec, struct xe_vm *vm, · 6 unmodified lines
31423210 region <= ARRAY_SIZE(region_to_mem_type));
31433211 }
31443212
3213+ /*
3214+ * Prefetch attempts to migrate BO's backing store without
3215+ * repopulating it first. Purged BOs have no backing store
3216+ * to migrate, so reject the operation.
3217+ */
31453218 err = vma_lock_and_validate(exec,
31463219 gpuva_to_vma(op->base.prefetch.va),
3147− res_evict, false, false);
3220+ (struct xe_vma_lock_and_validate_flags) {
3221+ .res_evict = res_evict,
3222+ .validate = false,
3223+ .request_decompress = false,
3224+ .check_purged = true,
3225+ });
31483226 if (!err && !xe_vma_has_no_bo(vma))
31493227 err = xe_bo_migrate(xe_vma_bo(vma),
31503228 region_to_mem_type[region],
drivers/gpu/drm/xe/xe_vm_madvise.c+299−4
@@ -13,6 +13,7 @@ · 12 unmodified lines
1313 #include "xe_pt.h"
1414 #include "xe_svm.h"
1515 #include "xe_tlb_inval.h"
16+#include "xe_vm.h"
1617
1718 struct xe_vmas_in_madvise_range {
1819 u64 addr;
@@ -26,6 +27,8 @@ struct xe_vmas_in_madvise_range { · 7 unmodified lines
2627 /**
2728 * struct xe_madvise_details - Argument to madvise_funcs
2829 * @dpagemap: Reference-counted pointer to a struct drm_pagemap.
30+ * @has_purged_bo: Track if any BO was purged (for purgeable state)
31+ * @retained_ptr: User pointer for retained value (for purgeable state)
2932 *
3033 * The madvise IOCTL handler may, in addition to the user-space
3134 * args, have additional info to pass into the madvise_func that
@@ -34,6 +37,8 @@ struct xe_vmas_in_madvise_range { · 2 unmodified lines
3437 */
3538 struct xe_madvise_details {
3639 struct drm_pagemap *dpagemap;
40+ bool has_purged_bo;
41+ u64 retained_ptr;
3742 };
3843
3944 static int get_vmas(struct xe_vm *vm, struct xe_vmas_in_madvise_range *madvise_range)
@@ -180,6 +185,222 @@ static void madvise_pat_index(struct xe_device *xe, struct xe_vm *vm, · 140 unmodified lines
180185 }
181186 }
182187
188+/**
189+ * xe_bo_is_dmabuf_shared() - Check if BO is shared via dma-buf
190+ * @bo: Buffer object
191+ *
192+ * Prevent marking imported or exported dma-bufs as purgeable.
193+ * For imported BOs, Xe doesn't own the backing store and cannot
194+ * safely reclaim pages (exporter or other devices may still be
195+ * using them). For exported BOs, external devices may have active
196+ * mappings we cannot track.
197+ *
198+ * Return: true if BO is imported or exported, false otherwise
199+ */
200+static bool xe_bo_is_dmabuf_shared(struct xe_bo *bo)
201+{
202+ struct drm_gem_object *obj = &bo->ttm.base;
203+
204+ /* Imported: exporter owns backing store */
205+ if (drm_gem_is_imported(obj))
206+ return true;
207+
208+ /* Exported: external devices may be accessing */
209+ if (obj->dma_buf)
210+ return true;
211+
212+ return false;
213+}
214+
215+/**
216+ * enum xe_bo_vmas_purge_state - VMA purgeable state aggregation
217+ *
218+ * Distinguishes whether a BO's VMAs are all DONTNEED, have at least
219+ * one WILLNEED, or have no VMAs at all.
220+ *
221+ * Enum values align with XE_MADV_PURGEABLE_* states for consistency.
222+ */
223+enum xe_bo_vmas_purge_state {
224+ /** @XE_BO_VMAS_STATE_WILLNEED: At least one VMA is WILLNEED */
225+ XE_BO_VMAS_STATE_WILLNEED = 0,
226+ /** @XE_BO_VMAS_STATE_DONTNEED: All VMAs are DONTNEED */
227+ XE_BO_VMAS_STATE_DONTNEED = 1,
228+ /** @XE_BO_VMAS_STATE_NO_VMAS: BO has no VMAs */
229+ XE_BO_VMAS_STATE_NO_VMAS = 2,
230+};
231+
232+/*
233+ * xe_bo_recompute_purgeable_state() casts between xe_bo_vmas_purge_state and
234+ * xe_madv_purgeable_state. Enforce that WILLNEED=0 and DONTNEED=1 match across
235+ * both enums so the single-line cast is always valid.
236+ */
237+static_assert(XE_BO_VMAS_STATE_WILLNEED == (int)XE_MADV_PURGEABLE_WILLNEED,
238+ "VMA purge state WILLNEED must equal madv purgeable WILLNEED");
239+static_assert(XE_BO_VMAS_STATE_DONTNEED == (int)XE_MADV_PURGEABLE_DONTNEED,
240+ "VMA purge state DONTNEED must equal madv purgeable DONTNEED");
241+
242+/**
243+ * xe_bo_all_vmas_dontneed() - Determine BO VMA purgeable state
244+ * @bo: Buffer object
245+ *
246+ * Check all VMAs across all VMs to determine aggregate purgeable state.
247+ * Shared BOs require unanimous DONTNEED state from all mappings.
248+ *
249+ * Caller must hold BO dma-resv lock.
250+ *
251+ * Return: XE_BO_VMAS_STATE_DONTNEED if all VMAs are DONTNEED,
252+ * XE_BO_VMAS_STATE_WILLNEED if at least one VMA is not DONTNEED,
253+ * XE_BO_VMAS_STATE_NO_VMAS if BO has no VMAs
254+ */
255+static enum xe_bo_vmas_purge_state xe_bo_all_vmas_dontneed(struct xe_bo *bo)
256+{
257+ struct drm_gpuvm_bo *vm_bo;
258+ struct drm_gpuva *gpuva;
259+ struct drm_gem_object *obj = &bo->ttm.base;
260+ bool has_vmas = false;
261+
262+ xe_bo_assert_held(bo);
263+
264+ /* Shared dma-bufs cannot be purgeable */
265+ if (xe_bo_is_dmabuf_shared(bo))
266+ return XE_BO_VMAS_STATE_WILLNEED;
267+
268+ drm_gem_for_each_gpuvm_bo(vm_bo, obj) {
269+ drm_gpuvm_bo_for_each_va(gpuva, vm_bo) {
270+ struct xe_vma *vma = gpuva_to_vma(gpuva);
271+
272+ has_vmas = true;
273+
274+ /* Any non-DONTNEED VMA prevents purging */
275+ if (vma->attr.purgeable_state != XE_MADV_PURGEABLE_DONTNEED)
276+ return XE_BO_VMAS_STATE_WILLNEED;
277+ }
278+ }
279+
280+ /*
281+ * No VMAs => preserve existing BO purgeable state.
282+ * Avoids incorrectly flipping DONTNEED -> WILLNEED when last VMA unmapped.
283+ */
284+ if (!has_vmas)
285+ return XE_BO_VMAS_STATE_NO_VMAS;
286+
287+ return XE_BO_VMAS_STATE_DONTNEED;
288+}
289+
290+/**
291+ * xe_bo_recompute_purgeable_state() - Recompute BO purgeable state from VMAs
292+ * @bo: Buffer object
293+ *
294+ * Walk all VMAs to determine if BO should be purgeable or not.
295+ * Shared BOs require unanimous DONTNEED state from all mappings.
296+ * If the BO has no VMAs the existing state is preserved.
297+ *
298+ * Locking: Caller must hold BO dma-resv lock. When iterating GPUVM lists,
299+ * VM lock must also be held (write) to prevent concurrent VMA modifications.
300+ * This is satisfied at both call sites:
301+ * - xe_vma_destroy(): holds vm->lock write
302+ * - madvise_purgeable(): holds vm->lock write (from madvise ioctl path)
303+ *
304+ * Return: nothing
305+ */
306+void xe_bo_recompute_purgeable_state(struct xe_bo *bo)
307+{
308+ enum xe_bo_vmas_purge_state vma_state;
309+
310+ if (!bo)
311+ return;
312+
313+ xe_bo_assert_held(bo);
314+
315+ /*
316+ * Once purged, always purged. Cannot transition back to WILLNEED.
317+ * This matches i915 semantics where purged BOs are permanently invalid.
318+ */
319+ if (bo->madv_purgeable == XE_MADV_PURGEABLE_PURGED)
320+ return;
321+
322+ vma_state = xe_bo_all_vmas_dontneed(bo);
323+
324+ if (vma_state != (enum xe_bo_vmas_purge_state)bo->madv_purgeable &&
325+ vma_state != XE_BO_VMAS_STATE_NO_VMAS)
326+ xe_bo_set_purgeable_state(bo, (enum xe_madv_purgeable_state)vma_state);
327+}
328+
329+/**
330+ * madvise_purgeable - Handle purgeable buffer object advice
331+ * @xe: XE device
332+ * @vm: VM
333+ * @vmas: Array of VMAs
334+ * @num_vmas: Number of VMAs
335+ * @op: Madvise operation
336+ * @details: Madvise details for return values
337+ *
338+ * Handles DONTNEED/WILLNEED/PURGED states. Tracks if any BO was purged
339+ * in details->has_purged_bo for later copy to userspace.
340+ */
341+static void madvise_purgeable(struct xe_device *xe, struct xe_vm *vm,
342+ struct xe_vma **vmas, int num_vmas,
343+ struct drm_xe_madvise *op,
344+ struct xe_madvise_details *details)
345+{
346+ int i;
347+
348+ xe_assert(vm->xe, op->type == DRM_XE_VMA_ATTR_PURGEABLE_STATE);
349+
350+ for (i = 0; i < num_vmas; i++) {
351+ struct xe_bo *bo = xe_vma_bo(vmas[i]);
352+
353+ if (!bo) {
354+ /* Purgeable state applies to BOs only, skip non-BO VMAs */
355+ vmas[i]->skip_invalidation = true;
356+ continue;
357+ }
358+
359+ /* BO must be locked before modifying madv state */
360+ xe_bo_assert_held(bo);
361+
362+ /* Skip shared dma-bufs - no PTEs to zap */
363+ if (xe_bo_is_dmabuf_shared(bo)) {
364+ vmas[i]->skip_invalidation = true;
365+ continue;
366+ }
367+
368+ /*
369+ * Once purged, always purged. Cannot transition back to WILLNEED.
370+ * This matches i915 semantics where purged BOs are permanently invalid.
371+ */
372+ if (xe_bo_is_purged(bo)) {
373+ details->has_purged_bo = true;
374+ vmas[i]->skip_invalidation = true;
375+ continue;
376+ }
377+
378+ switch (op->purge_state_val.val) {
379+ case DRM_XE_VMA_PURGEABLE_STATE_WILLNEED:
380+ vmas[i]->attr.purgeable_state = XE_MADV_PURGEABLE_WILLNEED;
381+ vmas[i]->skip_invalidation = true;
382+
383+ xe_bo_recompute_purgeable_state(bo);
384+ break;
385+ case DRM_XE_VMA_PURGEABLE_STATE_DONTNEED:
386+ vmas[i]->attr.purgeable_state = XE_MADV_PURGEABLE_DONTNEED;
387+ /*
388+ * Don't zap PTEs at DONTNEED time -- pages are still
389+ * alive. The zap happens in xe_bo_move_notify() right
390+ * before the shrinker frees them.
391+ */
392+ vmas[i]->skip_invalidation = true;
393+
394+ xe_bo_recompute_purgeable_state(bo);
395+ break;
396+ default:
397+ /* Should never hit - values validated in madvise_args_are_sane() */
398+ xe_assert(vm->xe, 0);
399+ return;
400+ }
401+ }
402+}
403+
183404 typedef void (*madvise_func)(struct xe_device *xe, struct xe_vm *vm,
184405 struct xe_vma **vmas, int num_vmas,
185406 struct drm_xe_madvise *op,
@@ -189,6 +410,7 @@ static const madvise_func madvise_funcs[] = { · 3 unmodified lines
189410 [DRM_XE_MEM_RANGE_ATTR_PREFERRED_LOC] = madvise_preferred_mem_loc,
190411 [DRM_XE_MEM_RANGE_ATTR_ATOMIC] = madvise_atomic,
191412 [DRM_XE_MEM_RANGE_ATTR_PAT] = madvise_pat_index,
413+ [DRM_XE_VMA_ATTR_PURGEABLE_STATE] = madvise_purgeable,
192414 };
193415
194416 static u8 xe_zap_ptes_in_madvise_range(struct xe_vm *vm, u64 start, u64 end)
@@ -319,6 +541,19 @@ static bool madvise_args_are_sane(struct xe_device *xe, const struct drm_xe_madv · 124 unmodified lines
319541 return false;
320542 break;
321543 }
544+ case DRM_XE_VMA_ATTR_PURGEABLE_STATE:
545+ {
546+ u32 val = args->purge_state_val.val;
547+
548+ if (XE_IOCTL_DBG(xe, !(val == DRM_XE_VMA_PURGEABLE_STATE_WILLNEED ||
549+ val == DRM_XE_VMA_PURGEABLE_STATE_DONTNEED)))
550+ return false;
551+
552+ if (XE_IOCTL_DBG(xe, args->purge_state_val.pad))
553+ return false;
554+
555+ break;
556+ }
322557 default:
323558 if (XE_IOCTL_DBG(xe, 1))
324559 return false;
@@ -337,6 +572,12 @@ static int xe_madvise_details_init(struct xe_vm *vm, const struct drm_xe_madvise · 12 unmodified lines
337572
338573 memset(details, 0, sizeof(*details));
339574
575+ /* Store retained pointer for purgeable state */
576+ if (args->type == DRM_XE_VMA_ATTR_PURGEABLE_STATE) {
577+ details->retained_ptr = args->purge_state_val.retained_ptr;
578+ return 0;
579+ }
580+
340581 if (args->type == DRM_XE_MEM_RANGE_ATTR_PREFERRED_LOC) {
341582 int fd = args->preferred_mem_loc.devmem_fd;
342583 struct drm_pagemap *dpagemap;
@@ -365,6 +606,21 @@ static void xe_madvise_details_fini(struct xe_madvise_details *details) · 22 unmodified lines
365606 drm_pagemap_put(details->dpagemap);
366607 }
367608
609+static int xe_madvise_purgeable_retained_to_user(const struct xe_madvise_details *details)
610+{
611+ u32 retained;
612+
613+ if (!details->retained_ptr)
614+ return 0;
615+
616+ retained = !details->has_purged_bo;
617+
618+ if (put_user(retained, (u32 __user *)u64_to_user_ptr(details->retained_ptr)))
619+ return -EFAULT;
620+
621+ return 0;
622+}
623+
368624 static bool check_bo_args_are_sane(struct xe_vm *vm, struct xe_vma **vmas,
369625 int num_vmas, u32 atomic_val)
370626 {
@@ -416,13 +672,21 @@ int xe_vm_madvise_ioctl(struct drm_device *dev, void *data, struct drm_file *fil · 45 unmodified lines
416672 struct xe_device *xe = to_xe_device(dev);
417673 struct xe_file *xef = to_xe_file(file);
418674 struct drm_xe_madvise *args = data;
419− struct xe_vmas_in_madvise_range madvise_range = {.addr = args->start,
420− .range = args->range, };
675+ struct xe_vmas_in_madvise_range madvise_range = {
676+ /*
677+ * Userspace may pass canonical (sign-extended) addresses.
678+ * Strip the sign extension to get the internal non-canonical
679+ * form used by the GPUVM, matching xe_vm_bind_ioctl() behavior.
680+ */
681+ .addr = xe_device_uncanonicalize_addr(xe, args->start),
682+ .range = args->range,
683+ };
421684 struct xe_madvise_details details;
422685 u16 pat_index, coh_mode;
423686 struct xe_vm *vm;
424687 struct drm_exec exec;
425688 int err, attr_type;
689+ bool do_retained;
426690
427691 vm = xe_vm_lookup(xef, args->vm_id);
428692 if (XE_IOCTL_DBG(xe, !vm))
@@ -433,6 +697,25 @@ int xe_vm_madvise_ioctl(struct drm_device *dev, void *data, struct drm_file *fil · 4 unmodified lines
433697 goto put_vm;
434698 }
435699
700+ /* Cache whether we need to write retained, and validate it's initialized to 0 */
701+ do_retained = args->type == DRM_XE_VMA_ATTR_PURGEABLE_STATE &&
702+ args->purge_state_val.retained_ptr;
703+ if (do_retained) {
704+ u32 retained;
705+ u32 __user *retained_ptr;
706+
707+ retained_ptr = u64_to_user_ptr(args->purge_state_val.retained_ptr);
708+ if (get_user(retained, retained_ptr)) {
709+ err = -EFAULT;
710+ goto put_vm;
711+ }
712+
713+ if (XE_IOCTL_DBG(xe, retained != 0)) {
714+ err = -EINVAL;
715+ goto put_vm;
716+ }
717+ }
718+
436719 xe_svm_flush(vm);
437720
438721 err = down_write_killable(&vm->lock);
@@ -448,7 +731,7 @@ int xe_vm_madvise_ioctl(struct drm_device *dev, void *data, struct drm_file *fil · 9 unmodified lines
448731 if (err)
449732 goto unlock_vm;
450733
451− err = xe_vm_alloc_madvise_vma(vm, args->start, args->range);
734+ err = xe_vm_alloc_madvise_vma(vm, madvise_range.addr, args->range);
452735 if (err)
453736 goto madv_fini;
454737
@@ -510,10 +793,18 @@ int xe_vm_madvise_ioctl(struct drm_device *dev, void *data, struct drm_file *fil · 55 unmodified lines
510793 }
511794
512795 attr_type = array_index_nospec(args->type, ARRAY_SIZE(madvise_funcs));
796+
797+ /* Ensure the madvise function exists for this type */
798+ if (!madvise_funcs[attr_type]) {
799+ err = -EINVAL;
800+ goto err_fini;
801+ }
802+
513803 madvise_funcs[attr_type](xe, vm, madvise_range.vmas, madvise_range.num_vmas, args,
514804 &details);
515805
516− err = xe_vm_invalidate_madvise_range(vm, args->start, args->start + args->range);
806+ err = xe_vm_invalidate_madvise_range(vm, madvise_range.addr,
807+ madvise_range.addr + args->range);
517808
518809 if (madvise_range.has_svm_userptr_vmas)
519810 xe_svm_notifier_unlock(vm);
@@ -528,6 +819,10 @@ madv_fini: · 8 unmodified lines
528819 xe_madvise_details_fini(&details);
529820 unlock_vm:
530821 up_write(&vm->lock);
822+
823+ /* Write retained value to user after releasing all locks */
824+ if (!err && do_retained)
825+ err = xe_madvise_purgeable_retained_to_user(&details);
531826 put_vm:
532827 xe_vm_put(vm);
533828 return err;
drivers/gpu/drm/xe/xe_vm_madvise.h+3−0
@@ -8,8 +8,11 @@ · 7 unmodified lines
88
99 struct drm_device;
1010 struct drm_file;
11+struct xe_bo;
1112
1213 int xe_vm_madvise_ioctl(struct drm_device *dev, void *data,
1314 struct drm_file *file);
1415
16+void xe_bo_recompute_purgeable_state(struct xe_bo *bo);
17+
1518 #endif
drivers/gpu/drm/xe/xe_vm_types.h+11−0
@@ -96,6 +96,17 @@ struct xe_vma_mem_attr { · 95 unmodified lines
9696 * same as default_pat_index unless overwritten by madvise.
9797 */
9898 u16 pat_index;
99+
100+ /**
101+ * @purgeable_state: Purgeable hint for this VMA mapping
102+ *
103+ * Per-VMA purgeable state from madvise. Valid states are WILLNEED (0)
104+ * or DONTNEED (1). Shared BOs require all VMAs to be DONTNEED before
105+ * the BO can be purged. PURGED state exists only at BO level.
106+ *
107+ * Protected by BO dma-resv lock. Set via DRM_IOCTL_XE_MADVISE.
108+ */
109+ u32 purgeable_state;
99110 };
100111
101112 struct xe_vma {
include/uapi/drm/xe_drm.h+69−0
@@ -432,6 +432,7 @@ struct drm_xe_query_config { · 431 unmodified lines
432432 #define DRM_XE_QUERY_CONFIG_FLAG_HAS_CPU_ADDR_MIRROR (1 << 2)
433433 #define DRM_XE_QUERY_CONFIG_FLAG_HAS_NO_COMPRESSION_HINT (1 << 3)
434434 #define DRM_XE_QUERY_CONFIG_FLAG_HAS_DISABLE_STATE_CACHE_PERF_FIX (1 << 4)
435+ #define DRM_XE_QUERY_CONFIG_FLAG_HAS_PURGING_SUPPORT (1 << 5)
435436 #define DRM_XE_QUERY_CONFIG_MIN_ALIGNMENT 2
436437 #define DRM_XE_QUERY_CONFIG_VA_BITS 3
437438 #define DRM_XE_QUERY_CONFIG_MAX_EXEC_QUEUE_PRIORITY 4
@@ -2171,6 +2172,7 @@ struct drm_xe_query_eu_stall { · 1733 unmodified lines
21712172 * - DRM_XE_MEM_RANGE_ATTR_PREFERRED_LOC: Set preferred memory location.
21722173 * - DRM_XE_MEM_RANGE_ATTR_ATOMIC: Set atomic access policy.
21732174 * - DRM_XE_MEM_RANGE_ATTR_PAT: Set page attribute table index.
2175+ * - DRM_XE_VMA_ATTR_PURGEABLE_STATE: Set purgeable state for BOs.
21742176 *
21752177 * Example:
21762178 *
@@ -2203,6 +2205,7 @@ struct drm_xe_madvise { · 26 unmodified lines
22032205 #define DRM_XE_MEM_RANGE_ATTR_PREFERRED_LOC 0
22042206 #define DRM_XE_MEM_RANGE_ATTR_ATOMIC 1
22052207 #define DRM_XE_MEM_RANGE_ATTR_PAT 2
2208+#define DRM_XE_VMA_ATTR_PURGEABLE_STATE 3
22062209 /** @type: type of attribute */
22072210 __u32 type;
22082211
@@ -2293,6 +2296,72 @@ struct drm_xe_madvise { · 84 unmodified lines
22932296 /** @pat_index.reserved: Reserved */
22942297 __u64 reserved;
22952298 } pat_index;
2299+
2300+ /**
2301+ * @purge_state_val: Purgeable state configuration
2302+ *
2303+ * Used when @type == DRM_XE_VMA_ATTR_PURGEABLE_STATE.
2304+ *
2305+ * Configures the purgeable state of buffer objects in the specified
2306+ * virtual address range. This allows applications to hint to the kernel
2307+ * about bo's usage patterns for better memory management.
2308+ *
2309+ * By default all VMAs are in WILLNEED state.
2310+ *
2311+ * Supported values for @purge_state_val.val:
2312+ * - DRM_XE_VMA_PURGEABLE_STATE_WILLNEED (0): Marks BO as needed.
2313+ * If the BO was previously purged, the kernel sets the __u32 at
2314+ * @retained_ptr to 0 (backing store lost) so the application knows
2315+ * it must recreate the BO.
2316+ *
2317+ * - DRM_XE_VMA_PURGEABLE_STATE_DONTNEED (1): Marks BO as not currently
2318+ * needed. Kernel may purge it under memory pressure to reclaim memory.
2319+ * Only applies to non-shared BOs. The kernel sets the __u32 at
2320+ * @retained_ptr to 1 if the backing store still exists (not yet purged),
2321+ * or 0 if it was already purged.
2322+ *
2323+ * Important: Once marked as DONTNEED, touching the BO's memory
2324+ * is undefined behavior. It may succeed temporarily (before the
2325+ * kernel purges the backing store) but will suddenly fail once
2326+ * the BO transitions to PURGED state.
2327+ *
2328+ * To transition back: use WILLNEED and check @retained_ptr —
2329+ * if 0, backing store was lost and the BO must be recreated.
2330+ *
2331+ * The following operations are blocked in DONTNEED state to
2332+ * prevent the BO from being re-mapped after madvise:
2333+ * - New mmap() calls: Fail with -EBUSY
2334+ * - VM_BIND operations: Fail with -EBUSY
2335+ * - New dma-buf exports: Fail with -EBUSY
2336+ * - CPU page faults (existing mmap): Fail with SIGBUS
2337+ * - GPU page faults (fault-mode VMs): Fail with -EACCES
2338+ */
2339+ struct {
2340+#define DRM_XE_VMA_PURGEABLE_STATE_WILLNEED 0
2341+#define DRM_XE_VMA_PURGEABLE_STATE_DONTNEED 1
2342+ /** @purge_state_val.val: value for DRM_XE_VMA_ATTR_PURGEABLE_STATE */
2343+ __u32 val;
2344+
2345+ /** @purge_state_val.pad: MBZ */
2346+ __u32 pad;
2347+ /**
2348+ * @purge_state_val.retained_ptr: Pointer to a __u32 output
2349+ * field for backing store status.
2350+ *
2351+ * Userspace must initialize the __u32 value at this address
2352+ * to 0 before the ioctl. Kernel writes a __u32 after the
2353+ * operation:
2354+ * - 1 if backing store exists (not purged)
2355+ * - 0 if backing store was purged
2356+ *
2357+ * If userspace fails to initialize to 0, ioctl returns -EINVAL.
2358+ * This ensures a safe default (0 = assume purged) if kernel
2359+ * cannot write the result.
2360+ *
2361+ * Similar to i915's drm_i915_gem_madvise.retained field.
2362+ */
2363+ __u64 retained_ptr;
2364+ } purge_state_val;
22962365 };
22972366
22982367 /** @reserved: Reserved */