tests/xe: Add purgeable memory madvise tests for system allocator
v7 cover tests/xe: Add purgeable memory madvise tests for system allocator
This series adds IGT tests for the purgeable memory madvise functionality in the XE driver's system allocator path.
Purgeable memory allows userspace to mark buffer objects as DONTNEED, making them eligible for kernel reclamation under memory pressure. This is critical for mobile and memory-constrained platforms to prevent OOM conditions while managing temporary or regeneratable GPU data.
The test suite validates:
- Basic purgeable lifecycle (WILLNEED -> DONTNEED -> PURGED)
- "Once purged, always purged" semantics (i915 compatibility)
- Per-VMA purgeable state tracking for shared buffers
- CPU fault handling on purged BOs (SIGBUS/SIGSEGV)
- GPU execution with purged memory and scratch page protection
- Proper state transitions across multiple VMs
Purgeable Memory States:-
- **WILLNEED (0)**: Memory is actively needed, kernel should not reclaim it.
- **DONTNEED (1)**: Application doesn't need this memory right now, kernel can reclaim it if needed
Retained Value
When querying purgeable state, the kernel returns:
- retained = 1: Memory is still present (not purged)
- retained = 0: Memory was purged/reclaimed
Kernel dependency: https://patchwork.freedesktop.org/series/156651/
Test Cases :
1. dontneed-before-mmap
Purpose: Validate that mmap fails on dontneed BO
2. purged-mmap-blocked
Purpose: Validate that mmap fails on already-purged
3. dontneed-after-mmap
Purpose: Validate that accessing an existing mapping of purged memory
triggers SIGBUS/SIGSEGV.
4. dontneed-before-exec
Purpose: Validate GPU execution on purgeable BO (before it's used).
5. dontneed-after-exec
Purpose: Validate that previously-used BO can be purged and becomes
inaccessible.
6. per-vma-tracking
Purpose: Validate per-VMA purgeable state tracking
7. per-vma-protection
Purpose: Validate that WILLNEED VMA protects BO from purging during
GPU operations.
v2:
- Move tests from xe_exec_system_allocator.c to dedicated xe_madvise.c (Thomas Hellström).
- Fix trigger_memory_pressure to use scalable overpressure
(25% of VRAM, minimum 64MB instead of fixed 64MB). (Pravalika)
- Add MAP_FAILED check in trigger_memory_pressure.
- Touch all pages in allocated chunks, not just first 4KB. (Pravalika)
- Add 100ms sleep before freeing BOs to allow shrinker time
to process memory pressure. (Pravalika)
- Rename 'bo2' to 'handle' for clarity in trigger_memory_pressure.(Pravalika)
- Add NEEDS_VISIBLE_VRAM flag to purgeable_setup_simple_bo
for consistent CPU mapping support on discrete GPUs. (Pravalika)
- Add proper NULL mmap handling in test_dontneed_before_mmap with cleanup and early return. (Pravalika)
v3:
- Added separate commits for each individual test case. (Pravalika)
v4:
- Move unmap outside the block. (Pravalika)
- Added proper resource cleanup before calling igt_skip(). (Nishit)
- Added assertion for xe_bo_map. (Nishit)
- Now using sync[0] consistently. (Nishit)
- Added clarifying comment. (Nishit)
v5:
- Document DONTNEED BO access blocking behavior to prevent undefined behavior and clarify uAPI contract (Thomas, Matt)
- Add query flag DRM_XE_QUERY_CONFIG_FLAG_HAS_PURGING_SUPPORT for feature detection. (Jose)
- For DONTNEED BO's mmap offset ioctl blocked with -EBUSY.
- Rename retained to retained_ptr. (Jose)
- Add new subtest purged-mmap-blocked.
v6:
- Support iGPU in trigger_memory_pressure() by using total system RAM as pressure baseline; raise overpressure to 50% to force shrinker.
- DONTNEED mmap blocking now enforced at mmap() time (xe_gem_object_mmap), not at DRM_IOCTL_XE_GEM_MMAP_OFFSET. Update dontneed-before-mmap and purged-mmap-blocked accordingly.
- Fix graceful skip (instead of fail) in per-vma-tracking and per-vma-protection when purge cannot be induced. (Nishit)
v7:
- Commit message updated with kernel UAPI details (Nishit)
- Moved trigger_memory_pressure(), purgeable_mark_and_verify_purged() and sigtrap() in 4/9 and other patch. (Nishit)
- Use xe_has_vram() instead of checking xe_visible_vram_size() > 0 for clearer dGPU/iGPU detection. (Nishit)
- Fix purgeable_mark_and_verify_purged(): handle retained == 0 from DONTNEED (BO already purged) as success instead of incorrectly returning false. (Nishit)
- Drop unused vm parameter from trigger_memory_pressure(). (Nishit)
- Mentioned this new ioctl instead of __xe_vm_madvise(). (Nishit)
Arvind Yadav (8):
lib/xe: Add purgeable memory ioctl support tests/intel/xe_madvise: Add dontneed-before-mmap subtest tests/intel/xe_madvise: Add purged-mmap-blocked subtest tests/intel/xe_madvise: Add dontneed-after-mmap subtest tests/intel/xe_madvise: Add dontneed-before-exec subtest tests/intel/xe_madvise: Add dontneed-after-exec subtest tests/intel/xe_madvise: Add per-vma-tracking subtest tests/intel/xe_madvise: Add per-vma-protection subtest
Himal Prasad Ghimiray (1):
drm-uapi/xe_drm: Sync with Add UAPI support for purgeable buffer
objects
include/drm-uapi/xe_drm.h | 69 ++++ lib/xe/xe_ioctl.c | 33 ++ lib/xe/xe_ioctl.h | 2 + tests/intel/xe_madvise.c | 826 ++++++++++++++++++++++++++++++++++++++ tests/meson.build | 1 + 5 files changed, 931 insertions(+) create mode 100644 tests/intel/xe_madvise.c
Signature · 1 line
2.43.0
PATCH v7 1/9 drm-uapi/xe_drm: Sync with Add UAPI support for purgeable buffer objects
From: Himal Prasad Ghimiray <[email protected]>
Extend the DRM_XE_MADVISE ioctl to support purgeable buffer object management by adding DRM_XE_VMA_ATTR_PURGEABLE_STATE attribute type.
This allows userspace applications to provide memory usage hints to the kernel for better memory management under pressure:
- WILLNEED: Buffer is needed and should not be purged. If the BO was previously purged, retained field returns 0 indicating backing store was lost (once purged, always purged semantics matching i915).
- DONTNEED: Buffer is not currently needed and may be purged by the kernel under memory pressure to free resources. Only applies to non-shared BOs.
To prevent undefined behavior, the following operations are blocked while a BO is in DONTNEED state:
- New mmap() operations return -EBUSY
- VM_BIND operations return -EBUSY
- New dma-buf exports return -EBUSY
- CPU/GPU page faults return SIGBUS
This ensures applications cannot use a BO while marked as DONTNEED, preventing erratic behavior when the kernel purges the backing store.
The implementation includes a 'retained' output field (matching i915's drm_i915_gem_madvise.retained) that indicates whether the BO's backing store still exists (1) or has been purged (0).
Added DRM_XE_QUERY_CONFIG_FLAG_HAS_PURGING_SUPPORT flag to allow userspace to detect kernel support for purgeable buffer objects before attempting to use the feature.
Align with kernel 'commit f99a05cc0b85 ("drm/xe/uapi: Add UAPI support for purgeable buffer objects")'
v2:
- Update UAPI documentation to clarify retained must be initialized to 0(Thomas)
v5:
- Document DONTNEED BO access blocking behavior to prevent undefined behavior and clarify uAPI contract (Thomas, Matt)
- Add query flag DRM_XE_QUERY_CONFIG_FLAG_HAS_PURGING_SUPPORT for feature detection. (Jose)
- Renamed retained to retained_ptr. (Jose)
v6:
- Updated UAPI documentation as suggested. (Jose)
v7:
- Commit message updated with kernel UAPI details. (Nishit)
--- include/drm-uapi/xe_drm.h | 69 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+)
include/drm-uapi/xe_drm.h +69 -0
@@ -431,6 +431,7 @@ struct drm_xe_query_config {#define DRM_XE_QUERY_CONFIG_FLAG_HAS_LOW_LATENCY (1 << 1)#define DRM_XE_QUERY_CONFIG_FLAG_HAS_CPU_ADDR_MIRROR (1 << 2)#define DRM_XE_QUERY_CONFIG_FLAG_HAS_NO_COMPRESSION_HINT (1 << 3) +#define DRM_XE_QUERY_CONFIG_FLAG_HAS_PURGING_SUPPORT (1 << 5)#define DRM_XE_QUERY_CONFIG_MIN_ALIGNMENT 2#define DRM_XE_QUERY_CONFIG_VA_BITS 3#define DRM_XE_QUERY_CONFIG_MAX_EXEC_QUEUE_PRIORITY 4
@@ -2067,6 +2068,7 @@ struct drm_xe_query_eu_stall {* - DRM_XE_MEM_RANGE_ATTR_PREFERRED_LOC: Set preferred memory location. * - DRM_XE_MEM_RANGE_ATTR_ATOMIC: Set atomic access policy. * - DRM_XE_MEM_RANGE_ATTR_PAT: Set page attribute table index. +* - DRM_XE_VMA_ATTR_PURGEABLE_STATE: Set purgeable state for BOs. * * Example: *
@@ -2099,6 +2101,7 @@ struct drm_xe_madvise {#define DRM_XE_MEM_RANGE_ATTR_PREFERRED_LOC 0#define DRM_XE_MEM_RANGE_ATTR_ATOMIC 1#define DRM_XE_MEM_RANGE_ATTR_PAT 2 +#define DRM_XE_VMA_ATTR_PURGEABLE_STATE 3/** @type: type of attribute */ __u32 type;
@@ -2189,6 +2192,72 @@ struct drm_xe_madvise {/** @pat_index.reserved: Reserved */ __u64 reserved; } pat_index; + +/** +* @purge_state_val: Purgeable state configuration +* +* Used when @type == DRM_XE_VMA_ATTR_PURGEABLE_STATE. +* +* Configures the purgeable state of buffer objects in the specified +* virtual address range. This allows applications to hint to the kernel +* about bo's usage patterns for better memory management. +* +* By default all VMAs are in WILLNEED state. +* +* Supported values for @purge_state_val.val: +* - DRM_XE_VMA_PURGEABLE_STATE_WILLNEED (0): Marks BO as needed. +* If the BO was previously purged, the kernel sets the __u32 at +* @retained_ptr to 0 (backing store lost) so the application knows +* it must recreate the BO. +* +* - DRM_XE_VMA_PURGEABLE_STATE_DONTNEED (1): Marks BO as not currently +* needed. Kernel may purge it under memory pressure to reclaim memory. +* Only applies to non-shared BOs. The kernel sets the __u32 at +* @retained_ptr to 1 if the backing store still exists (not yet purged), +* or 0 if it was already purged. +* +* Important: Once marked as DONTNEED, touching the BO's memory +* is undefined behavior. It may succeed temporarily (before the +* kernel purges the backing store) but will suddenly fail once +* the BO transitions to PURGED state. +* +* To transition back: use WILLNEED and check @retained_ptr — +* if 0, backing store was lost and the BO must be recreated. +* +* The following operations are blocked in DONTNEED state to +* prevent the BO from being re-mapped after madvise: +* - New mmap() calls: Fail with -EBUSY +* - VM_BIND operations: Fail with -EBUSY +* - New dma-buf exports: Fail with -EBUSY +* - CPU page faults (existing mmap): Fail with SIGBUS +* - GPU page faults (fault-mode VMs): Fail with -EACCES +*/ +struct { +#define DRM_XE_VMA_PURGEABLE_STATE_WILLNEED 0 +#define DRM_XE_VMA_PURGEABLE_STATE_DONTNEED 1 +/** @purge_state_val.val: value for DRM_XE_VMA_ATTR_PURGEABLE_STATE */ +__u32 val; + +/** @purge_state_val.pad: MBZ */ +__u32 pad; +/** +* @purge_state_val.retained_ptr: Pointer to a __u32 output +* field for backing store status. +* +* Userspace must initialize the __u32 value at this address +* to 0 before the ioctl. Kernel writes a __u32 after the +* operation: +* - 1 if backing store exists (not purged) +* - 0 if backing store was purged +* +* If userspace fails to initialize to 0, ioctl returns -EINVAL. +* This ensures a safe default (0 = assume purged) if kernel +* cannot write the result. +* +* Similar to i915's drm_i915_gem_madvise.retained field. +*/ +__u64 retained_ptr; + } purge_state_val; };/** @reserved: Reserved */
Signature · 1 line
2.43.0
Re: [PATCH i-g-t v7 1/9] drm-uapi/xe_drm: Sync with Add UAPI support for purgeable buffer objects
Hi Arvind,
Quoted 54 lines · Arvind Yadav
From: Himal Prasad Ghimiray <[email protected]>
Extend the
DRM_XE_MADVISEioctl to support purgeable buffer object management by addingDRM_XE_VMA_ATTR_PURGEABLE_STATEattribute type.This allows userspace applications to provide memory usage hints to the kernel for better memory management under pressure:
- WILLNEED: Buffer is needed and should not be purged. If the BO was previously purged, retained field returns 0 indicating backing store was lost (once purged, always purged semantics matching i915).
- DONTNEED: Buffer is not currently needed and may be purged by the kernel under memory pressure to free resources. Only applies to non-shared BOs.
To prevent undefined behavior, the following operations are blocked while a BO is in DONTNEED state:
- Newmmap()operations return-EBUSY
-VM_BINDoperations return-EBUSY
- New dma-buf exports return-EBUSY
- CPU/GPU page faults return SIGBUSThis ensures applications cannot use a BO while marked as DONTNEED, preventing erratic behavior when the kernel purges the backing store.
The implementation includes a 'retained' output field (matching i915's drm_i915_gem_madvise.retained) that indicates whether the BO's backing store still exists (1) or has been purged (0).
Added
DRM_XE_QUERY_CONFIG_FLAG_HAS_PURGING_SUPPORTflag to allow userspace to detect kernel support for purgeable buffer objects before attempting to use the feature.Align with kernel 'commit
f99a05cc0b85("drm/xe/uapi: Add UAPI support for purgeable buffer objects")'v2:
- Update UAPI documentation to clarify retained must be initialized to 0(Thomas)v5:
- Document DONTNEED BO access blocking behavior to prevent undefined behavior and clarify uAPI contract (Thomas, Matt)
- Add query flagDRM_XE_QUERY_CONFIG_FLAG_HAS_PURGING_SUPPORTfor feature detection. (Jose)
- Renamed retained toretained_ptr. (Jose)v6:
- Updated UAPI documentation as suggested. (Jose)v7:
- Commit message updated with kernel UAPI details. (Nishit)
Quoted 6 lines
Cc Nishit Sharma <[email protected]>Cc Matthew Brost <[email protected]>Cc Thomas Hellström <[email protected]>Cc Pravalika Gurram <[email protected]>Signed-off-by Himal Prasad Ghimiray <[email protected]>Signed-off-by Arvind Yadav <[email protected]>
Looks like this commit is already in igt so you could drop first patch and resend. For example:
grep DRM_XE_VMA_ATTR_PURGEABLE_STATE include/drm-uapi/xe_drm.h
* - DRM_XE_VMA_ATTR_PURGEABLE_STATE: Set purgeable state for BOs.
#define DRM_XE_VMA_ATTR_PURGEABLE_STATE 3
* Used when @type == DRM_XE_VMA_ATTR_PURGEABLE_STATE.
/** @purge_state_val.val: value for DRM_XE_VMA_ATTR_PURGEABLE_STATE */
grep purge_state_val include/drm-uapi/xe_drm.h
* @purge_state_val: Purgeable state configuration
* Supported values for @purge_state_val.val:
/** @purge_state_val.val: value for DRM_XE_VMA_ATTR_PURGEABLE_STATE */ /** @purge_state_val.pad: MBZ */
* @purge_state_val.retained_ptr: Pointer to a __u32 output } purge_state_val;
Regards,
Kamil
Quoted 108 lines
--- include/drm-uapi/xe_drm.h | 69 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+)include/drm-uapi/xe_drm.h +69
-0@@ -431,6 +431,7 @@ struct drm_xe_query_config {#define DRM_XE_QUERY_CONFIG_FLAG_HAS_LOW_LATENCY (1 << 1)#define DRM_XE_QUERY_CONFIG_FLAG_HAS_CPU_ADDR_MIRROR (1 << 2)#define DRM_XE_QUERY_CONFIG_FLAG_HAS_NO_COMPRESSION_HINT (1 << 3) +#define DRM_XE_QUERY_CONFIG_FLAG_HAS_PURGING_SUPPORT (1 << 5)#define DRM_XE_QUERY_CONFIG_MIN_ALIGNMENT 2#define DRM_XE_QUERY_CONFIG_VA_BITS 3#define DRM_XE_QUERY_CONFIG_MAX_EXEC_QUEUE_PRIORITY 4@@ -2067,6 +2068,7 @@ struct drm_xe_query_eu_stall {* - DRM_XE_MEM_RANGE_ATTR_PREFERRED_LOC: Set preferred memory location. * - DRM_XE_MEM_RANGE_ATTR_ATOMIC: Set atomic access policy. * - DRM_XE_MEM_RANGE_ATTR_PAT: Set page attribute table index. +* - DRM_XE_VMA_ATTR_PURGEABLE_STATE: Set purgeable state for BOs. * * Example: * @@ -2099,6 +2101,7 @@ struct drm_xe_madvise {#define DRM_XE_MEM_RANGE_ATTR_PREFERRED_LOC 0#define DRM_XE_MEM_RANGE_ATTR_ATOMIC 1#define DRM_XE_MEM_RANGE_ATTR_PAT 2 +#define DRM_XE_VMA_ATTR_PURGEABLE_STATE 3/** @type: type of attribute */ __u32 type;@@ -2189,6 +2192,72 @@ struct drm_xe_madvise {/** @pat_index.reserved: Reserved */ __u64 reserved; } pat_index; + +/** +* @purge_state_val: Purgeable state configuration +* +* Used when @type == DRM_XE_VMA_ATTR_PURGEABLE_STATE. +* +* Configures the purgeable state of buffer objects in the specified +* virtual address range. This allows applications to hint to the kernel +* about bo's usage patterns for better memory management. +* +* By default all VMAs are in WILLNEED state. +* +* Supported values for @purge_state_val.val: +* - DRM_XE_VMA_PURGEABLE_STATE_WILLNEED (0): Marks BO as needed. +* If the BO was previously purged, the kernel sets the __u32 at +* @retained_ptr to 0 (backing store lost) so the application knows +* it must recreate the BO. +* +* - DRM_XE_VMA_PURGEABLE_STATE_DONTNEED (1): Marks BO as not currently +* needed. Kernel may purge it under memory pressure to reclaim memory. +* Only applies to non-shared BOs. The kernel sets the __u32 at +* @retained_ptr to 1 if the backing store still exists (not yet purged), +* or 0 if it was already purged. +* +* Important: Once marked as DONTNEED, touching the BO's memory +* is undefined behavior. It may succeed temporarily (before the +* kernel purges the backing store) but will suddenly fail once +* the BO transitions to PURGED state. +* +* To transition back: use WILLNEED and check @retained_ptr — +* if 0, backing store was lost and the BO must be recreated. +* +* The following operations are blocked in DONTNEED state to +* prevent the BO from being re-mapped after madvise: +* - New mmap() calls: Fail with -EBUSY +* - VM_BIND operations: Fail with -EBUSY +* - New dma-buf exports: Fail with -EBUSY +* - CPU page faults (existing mmap): Fail with SIGBUS +* - GPU page faults (fault-mode VMs): Fail with -EACCES +*/ +struct { +#define DRM_XE_VMA_PURGEABLE_STATE_WILLNEED 0 +#define DRM_XE_VMA_PURGEABLE_STATE_DONTNEED 1 +/** @purge_state_val.val: value for DRM_XE_VMA_ATTR_PURGEABLE_STATE */ +__u32 val; + +/** @purge_state_val.pad: MBZ */ +__u32 pad; +/** +* @purge_state_val.retained_ptr: Pointer to a __u32 output +* field for backing store status. +* +* Userspace must initialize the __u32 value at this address +* to 0 before the ioctl. Kernel writes a __u32 after the +* operation: +* - 1 if backing store exists (not purged) +* - 0 if backing store was purged +* +* If userspace fails to initialize to 0, ioctl returns -EINVAL. +* This ensures a safe default (0 = assume purged) if kernel +* cannot write the result. +* +* Similar to i915's drm_i915_gem_madvise.retained field. +*/ +__u64 retained_ptr; + } purge_state_val; };/** @reserved: Reserved */ Signature · 1 line
2.43.0
Re: [PATCH i-g-t v7 1/9] drm-uapi/xe_drm: Sync with Add UAPI support for purgeable buffer objects
Quoted 72 lines · Kamil Konieczny
Hi Arvind,
Quoted 54 lines · Arvind Yadav
From: Himal Prasad Ghimiray <[email protected]>
Extend the
DRM_XE_MADVISEioctl to support purgeable buffer object management by addingDRM_XE_VMA_ATTR_PURGEABLE_STATEattribute type.This allows userspace applications to provide memory usage hints to the kernel for better memory management under pressure:
- WILLNEED: Buffer is needed and should not be purged. If the BO was previously purged, retained field returns 0 indicating backing store was lost (once purged, always purged semantics matching i915).
- DONTNEED: Buffer is not currently needed and may be purged by the kernel under memory pressure to free resources. Only applies to non-shared BOs.
To prevent undefined behavior, the following operations are blocked while a BO is in DONTNEED state:
- Newmmap()operations return-EBUSY
-VM_BINDoperations return-EBUSY
- New dma-buf exports return-EBUSY
- CPU/GPU page faults return SIGBUSThis ensures applications cannot use a BO while marked as DONTNEED, preventing erratic behavior when the kernel purges the backing store.
The implementation includes a 'retained' output field (matching i915's drm_i915_gem_madvise.retained) that indicates whether the BO's backing store still exists (1) or has been purged (0).
Added
DRM_XE_QUERY_CONFIG_FLAG_HAS_PURGING_SUPPORTflag to allow userspace to detect kernel support for purgeable buffer objects before attempting to use the feature.Align with kernel 'commit
f99a05cc0b85("drm/xe/uapi: Add UAPI support for purgeable buffer objects")'v2:
- Update UAPI documentation to clarify retained must be initialized to 0(Thomas)v5:
- Document DONTNEED BO access blocking behavior to prevent undefined behavior and clarify uAPI contract (Thomas, Matt)
- Add query flagDRM_XE_QUERY_CONFIG_FLAG_HAS_PURGING_SUPPORTfor feature detection. (Jose)
- Renamed retained toretained_ptr. (Jose)v6:
- Updated UAPI documentation as suggested. (Jose)v7:
- Commit message updated with kernel UAPI details. (Nishit)Quoted 6 lines
Cc Nishit Sharma <[email protected]>Cc Matthew Brost <[email protected]>Cc Thomas Hellström <[email protected]>Cc Pravalika Gurram <[email protected]>Signed-off-by Himal Prasad Ghimiray <[email protected]>Signed-off-by Arvind Yadav <[email protected]>Looks like this commit is already in igt so you could drop first patch and resend. For example:
grep
DRM_XE_VMA_ATTR_PURGEABLE_STATEinclude/drm-uapi/xe_drm.h* -
DRM_XE_VMA_ATTR_PURGEABLE_STATE: Set purgeable state for BOs.
#defineDRM_XE_VMA_ATTR_PURGEABLE_STATE3
* Used when @type ==DRM_XE_VMA_ATTR_PURGEABLE_STATE.
/** @purge_state_val.val: value forDRM_XE_VMA_ATTR_PURGEABLE_STATE*/
Quoted 8 lines
grep
purge_state_valinclude/drm-uapi/xe_drm.h* @purge_state_val: Purgeable state configuration
* Supported values for @purge_state_val.val:
/** @purge_state_val.val: value forDRM_XE_VMA_ATTR_PURGEABLE_STATE*/ /** @purge_state_val.pad: MBZ */
* @purge_state_val.retained_ptr: Pointer to a__u32output }purge_state_val;
Noted, I will drop this patch.
Thanks,
Arvind
Quoted 112 lines
Regards,
KamilQuoted 108 lines
--- include/drm-uapi/xe_drm.h | 69 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+)include/drm-uapi/xe_drm.h +69
-0@@ -431,6 +431,7 @@ struct drm_xe_query_config {#define DRM_XE_QUERY_CONFIG_FLAG_HAS_LOW_LATENCY (1 << 1)#define DRM_XE_QUERY_CONFIG_FLAG_HAS_CPU_ADDR_MIRROR (1 << 2)#define DRM_XE_QUERY_CONFIG_FLAG_HAS_NO_COMPRESSION_HINT (1 << 3) +#define DRM_XE_QUERY_CONFIG_FLAG_HAS_PURGING_SUPPORT (1 << 5)#define DRM_XE_QUERY_CONFIG_MIN_ALIGNMENT 2#define DRM_XE_QUERY_CONFIG_VA_BITS 3#define DRM_XE_QUERY_CONFIG_MAX_EXEC_QUEUE_PRIORITY 4@@ -2067,6 +2068,7 @@ struct drm_xe_query_eu_stall {* - DRM_XE_MEM_RANGE_ATTR_PREFERRED_LOC: Set preferred memory location. * - DRM_XE_MEM_RANGE_ATTR_ATOMIC: Set atomic access policy. * - DRM_XE_MEM_RANGE_ATTR_PAT: Set page attribute table index. +* - DRM_XE_VMA_ATTR_PURGEABLE_STATE: Set purgeable state for BOs. * * Example: * @@ -2099,6 +2101,7 @@ struct drm_xe_madvise {#define DRM_XE_MEM_RANGE_ATTR_PREFERRED_LOC 0#define DRM_XE_MEM_RANGE_ATTR_ATOMIC 1#define DRM_XE_MEM_RANGE_ATTR_PAT 2 +#define DRM_XE_VMA_ATTR_PURGEABLE_STATE 3/** @type: type of attribute */ __u32 type;@@ -2189,6 +2192,72 @@ struct drm_xe_madvise {/** @pat_index.reserved: Reserved */ __u64 reserved; } pat_index; + +/** +* @purge_state_val: Purgeable state configuration +* +* Used when @type == DRM_XE_VMA_ATTR_PURGEABLE_STATE. +* +* Configures the purgeable state of buffer objects in the specified +* virtual address range. This allows applications to hint to the kernel +* about bo's usage patterns for better memory management. +* +* By default all VMAs are in WILLNEED state. +* +* Supported values for @purge_state_val.val: +* - DRM_XE_VMA_PURGEABLE_STATE_WILLNEED (0): Marks BO as needed. +* If the BO was previously purged, the kernel sets the __u32 at +* @retained_ptr to 0 (backing store lost) so the application knows +* it must recreate the BO. +* +* - DRM_XE_VMA_PURGEABLE_STATE_DONTNEED (1): Marks BO as not currently +* needed. Kernel may purge it under memory pressure to reclaim memory. +* Only applies to non-shared BOs. The kernel sets the __u32 at +* @retained_ptr to 1 if the backing store still exists (not yet purged), +* or 0 if it was already purged. +* +* Important: Once marked as DONTNEED, touching the BO's memory +* is undefined behavior. It may succeed temporarily (before the +* kernel purges the backing store) but will suddenly fail once +* the BO transitions to PURGED state. +* +* To transition back: use WILLNEED and check @retained_ptr — +* if 0, backing store was lost and the BO must be recreated. +* +* The following operations are blocked in DONTNEED state to +* prevent the BO from being re-mapped after madvise: +* - New mmap() calls: Fail with -EBUSY +* - VM_BIND operations: Fail with -EBUSY +* - New dma-buf exports: Fail with -EBUSY +* - CPU page faults (existing mmap): Fail with SIGBUS +* - GPU page faults (fault-mode VMs): Fail with -EACCES +*/ +struct { +#define DRM_XE_VMA_PURGEABLE_STATE_WILLNEED 0 +#define DRM_XE_VMA_PURGEABLE_STATE_DONTNEED 1 +/** @purge_state_val.val: value for DRM_XE_VMA_ATTR_PURGEABLE_STATE */ +__u32 val; + +/** @purge_state_val.pad: MBZ */ +__u32 pad; +/** +* @purge_state_val.retained_ptr: Pointer to a __u32 output +* field for backing store status. +* +* Userspace must initialize the __u32 value at this address +* to 0 before the ioctl. Kernel writes a __u32 after the +* operation: +* - 1 if backing store exists (not purged) +* - 0 if backing store was purged +* +* If userspace fails to initialize to 0, ioctl returns -EINVAL. +* This ensures a safe default (0 = assume purged) if kernel +* cannot write the result. +* +* Similar to i915's drm_i915_gem_madvise.retained field. +*/ +__u64 retained_ptr; + } purge_state_val; };/** @reserved: Reserved */ Signature · 1 line
2.43.0
PATCH v7 2/9 lib/xe: Add purgeable memory ioctl support
In reply to Arvind YadavAdd xe_vm_madvise_purgeable() helper function to support purgeable memory management through the XE madvise ioctl. This allows applications to hint to the kernel about buffer object usage patterns for better memory management under pressure.
The function provides a clean interface to:
- Mark buffer objects as DONTNEED (purgeable)
- Mark buffer objects as WILLNEED (not purgeable)
Returns the retained value directly (1 if backing store exists, 0 if purged).
Also update __xe_vm_madvise() to reject purgeable state operations and direct users to the dedicated helper. Here purgeable state requires an output (retained_ptr) field, which does not fit the generic type/op_val model of __xe_vm_madvise(). Add a dedicated helper that populates the purgeable-specific ioctl fields and returns the retained value directly.
v2:
- retained must be initialized to 0(Thomas)
v5:
- Rename retained to retained_ptr. (Jose)
v7:
- Mention this new ioctl instead of __xe_vm_madvise(). (Nishit)
--- lib/xe/xe_ioctl.c | 33 +++++++++++++++++++++++++++++++++ lib/xe/xe_ioctl.h | 2 ++ 2 files changed, 35 insertions(+)
lib/xe/xe_ioctl.c +33 -0
@@ -785,6 +785,9 @@ int __xe_vm_madvise(int fd, uint32_t vm, uint64_t addr, uint64_t range,case DRM_XE_MEM_RANGE_ATTR_PAT : madvise.pat_index.val = op_val;break ; +case DRM_XE_VMA_ATTR_PURGEABLE_STATE : +/* Purgeable state handled by xe_vm_madvise_purgeable */ +return -EINVAL ;default :igt_warn ("Unknown attribute\n" );return -EINVAL ;
@@ -821,6 +824,36 @@ void xe_vm_madvise(int fd, uint32_t vm, uint64_t addr, uint64_t range, instance), 0); } +/** +* xe_vm_madvise_purgeable: +* @fd: xe device fd +* @vm_id: vm_id of the virtual range +* @start: start of the virtual address range +* @range: size of the virtual address range +* @state: purgeable state (DRM_XE_VMA_PURGEABLE_STATE_WILLNEED or DONTNEED) +* +* Sets the purgeable state for a virtual memory range. This allows applications +* to hint to the kernel about buffer object usage patterns for better memory management. +* +* Returns: retained value (1 if backing store exists, 0 if purged) +*/ +uint32_t xe_vm_madvise_purgeable (int fd,uint32_t vm_id,uint64_t start, +uint64_t range,uint32_t state) +{ +uint32_t retained_val =0 ; +struct drm_xe_madvise madvise = { + .vm_id = vm_id, + .start = start, + .range = range, + .type =DRM_XE_VMA_ATTR_PURGEABLE_STATE , + .purge_state_val.val = state, + .purge_state_val.retained_ptr = (uint64_t )(uintptr_t )&retained_val, + }; + +igt_assert_eq (igt_ioctl (fd,DRM_IOCTL_XE_MADVISE , &madvise),0 ); +return retained_val; +} +#define BIND_SYNC_VAL 0x686868void xe_vm_bind_lr_sync (int fd,uint32_t vm,uint32_t bo,uint64_t offset,uint64_t addr,uint64_t size,uint32_t flags)
lib/xe/xe_ioctl.h +2 -0
@@ -108,6 +108,8 @@ int __xe_vm_madvise(int fd, uint32_t vm, uint64_t addr, uint64_t range, uint64_t uint32_t type, uint32_t op_val, uint16_t policy, uint16_t instance);void xe_vm_madvise (int fd,uint32_t vm,uint64_t addr,uint64_t range,uint64_t ext,uint32_t type,uint32_t op_val,uint16_t policy,uint16_t instance); +uint32_t xe_vm_madvise_purgeable (int fd,uint32_t vm_id,uint64_t start, +uint64_t range,uint32_t state);int xe_vm_number_vmas_in_range (int fd,struct drm_xe_vm_query_mem_range_attr *vmas_attr);int xe_vm_vma_attrs (int fd,struct drm_xe_vm_query_mem_range_attr *vmas_attr,struct drm_xe_mem_range_attr *mem_attr);
Signature · 1 line
2.43.0
PATCH v7 3/9 tests/intel/xe_madvise: Add dontneed-before-mmap subtest
In reply to Arvind YadavThis test validates that mmap() fails with -EBUSY when attempting to map a BO marked DONTNEED. The mmap offset ioctl succeeds (it just returns the pre-allocated offset); the purgeable check happens in xe_gem_object_mmap() at mmap() time.
- DONTNEED BOs: return -EBUSY (temporary purgeable state, BO still has backing store but can be purged at any time)
- Purged BOs: return -EINVAL (permanent, backing store discarded)
v4:
- Move unmap outside the block. (Pravalika)
- Added proper resource cleanup before calling igt_skip(). (Nishit)
- Added assertion for xe_bo_map. (Nishit)
v5:
- Add kernel capability check *_FLAG_HAS_PURGING_SUPPORT for purgeable support. (Jose)
- Drop memory pressure trigger path; mark DONTNEED directly and assert -EBUSY from mmap offset ioctl; restore WILLNEED before cleanup.
v6:
- Support iGPU by using total system RAM as the pressure baseline instead of VRAM size (which is 0 on iGPU).
- Raise overpressure from 25% to 50% of the baseline to ensure the kernel shrinker is forced to reclaim on systems with large free RAM.
- The DONTNEED enforcement point is mmap() itself, not the DRM_IOCTL_XE_GEM_MMAP_OFFSET ioctl. Update the test to mark DONTNEED first, then verify DRM_IOCTL_XE_GEM_MMAP_OFFSET still succeeds, and finally verify that mmap() fails with -EBUSY
v7:
- Move unused functions: trigger_memory_pressure(), sigtrap and purgeable_mark_and_verify_purged. (Nishit)
--- tests/intel/xe_madvise.c | 122 +++++++++++++++++++++++++++++++++++++++ tests/meson.build | 1 + 2 files changed, 123 insertions(+) create mode 100644 tests/intel/xe_madvise.c
tests/intel/xe_madvise.c new file +122 -0
@@ -0,0 +1,122 @@ +// SPDX-License-Identifier: MIT +/* +* Copyright © 2025 Intel Corporation +*/ + +/** +* TEST: Validate purgeable BO madvise functionality +* Category: Core +* Mega feature: General Core features +* Sub-category: Memory management tests +* Functionality: madvise, purgeable +*/ + +#include "igt.h" +#include "xe_drm.h" + +#include "xe/xe_ioctl.h" +#include "xe/xe_query.h" + +/* Purgeable test constants */ +#define PURGEABLE_ADDR 0x1a0000 +#define PURGEABLE_BO_SIZE 4096 + +static bool xe_has_purgeable_support (int fd) +{ +struct drm_xe_query_config *config =xe_config (fd); + +return config->info[DRM_XE_QUERY_CONFIG_FLAGS ] & +DRM_XE_QUERY_CONFIG_FLAG_HAS_PURGING_SUPPORT ; +} + +/** +* purgeable_setup_simple_bo - Setup VM and bind a single BO +* @fd: DRM file descriptor +* @vm: Output VM handle +* @bo: Output BO handle +* @addr: Virtual address to bind at +* @size: Size of the BO +* @use_scratch: Whether to use scratch page flag +* +* Helper to create VM, BO, and bind it at the specified address. +*/ +static void purgeable_setup_simple_bo (int fd,uint32_t *vm,uint32_t *bo, +uint64_t addr,size_t size,bool use_scratch) +{ +struct drm_xe_sync sync = { + .type =DRM_XE_SYNC_TYPE_USER_FENCE , + .flags =DRM_XE_SYNC_FLAG_SIGNAL , + .timeline_value =1 , + }; +uint64_t sync_val =0 ; + + *vm =xe_vm_create (fd, use_scratch ?DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE :0 ,0 ); + *bo =xe_bo_create (fd, *vm, size,vram_if_possible (fd,0 ), +DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM ); + + sync.addr =to_user_pointer (&sync_val); +xe_vm_bind_async (fd, *vm,0 , *bo,0 , addr, size, &sync,1 ); +xe_wait_ufence (fd, &sync_val,1 ,0 ,NSEC_PER_SEC ); +} + +/** +* SUBTEST: dontneed-before-mmap +* Description: Mark BO as DONTNEED before mmap, verify mmap() fails with -EBUSY +* Test category: functionality test +*/ +static void test_dontneed_before_mmap (int fd) +{ +uint32_t bo, vm; +uint64_t addr =PURGEABLE_ADDR ; +size_t bo_size =PURGEABLE_BO_SIZE ; +struct drm_xe_gem_mmap_offset mmo = {}; +uint32_t retained; +void *ptr; + +purgeable_setup_simple_bo (fd, &vm, &bo, addr, bo_size, false); + +/* Mark BO as DONTNEED - new mmap operations must be blocked */ + retained =xe_vm_madvise_purgeable (fd, vm, addr, bo_size, +DRM_XE_VMA_PURGEABLE_STATE_DONTNEED ); +igt_assert_eq (retained,1 ); + +/* Ioctl succeeds even for DONTNEED BO; blocking happens at mmap() time. */ + mmo.handle = bo; +igt_assert_eq (igt_ioctl (fd,DRM_IOCTL_XE_GEM_MMAP_OFFSET , &mmo),0 ); + +/* mmap() on a DONTNEED BO must fail with EBUSY. */ + ptr =mmap (NULL , bo_size,PROT_READ |PROT_WRITE ,MAP_SHARED , fd, mmo.offset); +igt_assert_eq_u64 ((uint64_t )ptr, (uint64_t )MAP_FAILED ); +igt_assert_eq (errno,EBUSY ); + +/* Restore to WILLNEED before cleanup */ +xe_vm_madvise_purgeable (fd, vm, addr, bo_size, +DRM_XE_VMA_PURGEABLE_STATE_WILLNEED ); + +gem_close (fd, bo); +xe_vm_destroy (fd, vm); +} + +int igt_main () +{ +struct drm_xe_engine_class_instance *hwe; +int fd; + + igt_fixture() { + fd =drm_open_driver (DRIVER_XE ); +xe_device_get (fd); +igt_require_f (xe_has_purgeable_support (fd), +"Kernel does not support purgeable buffer objects\n" ); + } + + igt_subtest("dontneed-before-mmap") +xe_for_each_engine (fd, hwe) { +test_dontneed_before_mmap (fd); +break ; + } + + igt_fixture() { +xe_device_put (fd); +drm_close_driver (fd); + } +}
tests/meson.build +1 -0
@@ -314,6 +314,7 @@ intel_xe_progs = [ 'xe_huc_copy', 'xe_intel_bb', 'xe_live_ktest', + 'xe_madvise', 'xe_media_fill', 'xe_mmap', 'xe_module_load',
Signature · 1 line
2.43.0
Re: [PATCH i-g-t v7 3/9] tests/intel/xe_madvise: Add dontneed-before-mmap subtest
Quoted 180 lines · Arvind Yadav
This test validates that
mmap()fails with-EBUSYwhen attempting to map a BO marked DONTNEED. The mmap offset ioctl succeeds (it just returns the pre-allocated offset); the purgeable check happens inxe_gem_object_mmap()atmmap()time.- DONTNEED BOs: return
-EBUSY(temporary purgeable state, BO still has backing store but can be purged at any time)
- Purged BOs: return-EINVAL(permanent, backing store discarded)v4:
- Move unmap outside the block. (Pravalika)
- Added proper resource cleanup before callingigt_skip(). (Nishit)
- Added assertion forxe_bo_map. (Nishit)v5:
- Add kernel capability check*_FLAG_HAS_PURGING_SUPPORTfor purgeable support. (Jose)
- Drop memory pressure trigger path; mark DONTNEED directly and assert-EBUSYfrom mmap offset ioctl; restore WILLNEED before cleanup.v6:
- Support iGPU by using total system RAM as the pressure baseline instead of VRAM size (which is 0 on iGPU).
- Raise overpressure from 25% to 50% of the baseline to ensure the kernel shrinker is forced to reclaim on systems with large free RAM.
- The DONTNEED enforcement point ismmap()itself, not theDRM_IOCTL_XE_GEM_MMAP_OFFSETioctl. Update the test to mark DONTNEED first, then verifyDRM_IOCTL_XE_GEM_MMAP_OFFSETstill succeeds, and finally verify thatmmap()fails with-EBUSYv7:
- Move unused functions:trigger_memory_pressure(), sigtrap andpurgeable_mark_and_verify_purged. (Nishit)Cc Nishit Sharma <[email protected]>Cc Matthew Brost <[email protected]>Cc Thomas Hellström <[email protected]>Cc Himal Prasad Ghimiray <[email protected]>Cc Pravalika Gurram <[email protected]>Signed-off-by Arvind Yadav <[email protected]>--- tests/intel/xe_madvise.c | 122 +++++++++++++++++++++++++++++++++++++++ tests/meson.build | 1 + 2 files changed, 123 insertions(+)create mode 100644
tests/intel/xe_madvise.ctests/intel/xe_madvise.c new file +122
-0@@ -0,0 +1,122 @@ +// SPDX-License-Identifier: MIT +/* +* Copyright © 2025 Intel Corporation +*/ + +/** +* TEST: Validate purgeable BO madvise functionality +* Category: Core +* Mega feature: General Core features +* Sub-category: Memory management tests +* Functionality: madvise, purgeable +*/ + +#include "igt.h" +#include "xe_drm.h" + +#include "xe/xe_ioctl.h" +#include "xe/xe_query.h" + +/* Purgeable test constants */ +#define PURGEABLE_ADDR 0x1a0000 +#define PURGEABLE_BO_SIZE 4096 + +static bool xe_has_purgeable_support (int fd) +{ +struct drm_xe_query_config *config =xe_config (fd); + +return config->info[DRM_XE_QUERY_CONFIG_FLAGS ] & +DRM_XE_QUERY_CONFIG_FLAG_HAS_PURGING_SUPPORT ; +} + +/** +* purgeable_setup_simple_bo - Setup VM and bind a single BO +* @fd: DRM file descriptor +* @vm: Output VM handle +* @bo: Output BO handle +* @addr: Virtual address to bind at +* @size: Size of the BO +* @use_scratch: Whether to use scratch page flag +* +* Helper to create VM, BO, and bind it at the specified address. +*/ +static void purgeable_setup_simple_bo (int fd,uint32_t *vm,uint32_t *bo, +uint64_t addr,size_t size,bool use_scratch) +{ +struct drm_xe_sync sync = { + .type =DRM_XE_SYNC_TYPE_USER_FENCE , + .flags =DRM_XE_SYNC_FLAG_SIGNAL , + .timeline_value =1 , + }; +uint64_t sync_val =0 ; + + *vm =xe_vm_create (fd, use_scratch ?DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE :0 ,0 ); + *bo =xe_bo_create (fd, *vm, size,vram_if_possible (fd,0 ), +DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM ); + + sync.addr =to_user_pointer (&sync_val); +xe_vm_bind_async (fd, *vm,0 , *bo,0 , addr, size, &sync,1 ); +xe_wait_ufence (fd, &sync_val,1 ,0 ,NSEC_PER_SEC ); +} + +/** +* SUBTEST: dontneed-before-mmap +* Description: Mark BO as DONTNEED before mmap, verify mmap() fails with -EBUSY +* Test category: functionality test +*/ +static void test_dontneed_before_mmap (int fd) +{ +uint32_t bo, vm; +uint64_t addr =PURGEABLE_ADDR ; +size_t bo_size =PURGEABLE_BO_SIZE ; +struct drm_xe_gem_mmap_offset mmo = {}; +uint32_t retained; +void *ptr; + +purgeable_setup_simple_bo (fd, &vm, &bo, addr, bo_size, false); + +/* Mark BO as DONTNEED - new mmap operations must be blocked */ + retained =xe_vm_madvise_purgeable (fd, vm, addr, bo_size, +DRM_XE_VMA_PURGEABLE_STATE_DONTNEED ); +igt_assert_eq (retained,1 ); + +/* Ioctl succeeds even for DONTNEED BO; blocking happens at mmap() time. */ + mmo.handle = bo; +igt_assert_eq (igt_ioctl (fd,DRM_IOCTL_XE_GEM_MMAP_OFFSET , &mmo),0 ); + +/* mmap() on a DONTNEED BO must fail with EBUSY. */ + ptr =mmap (NULL , bo_size,PROT_READ |PROT_WRITE ,MAP_SHARED , fd, mmo.offset); +igt_assert_eq_u64 ((uint64_t )ptr, (uint64_t )MAP_FAILED ); +igt_assert_eq (errno,EBUSY ); + +/* Restore to WILLNEED before cleanup */ +xe_vm_madvise_purgeable (fd, vm, addr, bo_size, +DRM_XE_VMA_PURGEABLE_STATE_WILLNEED ); + +gem_close (fd, bo); +xe_vm_destroy (fd, vm); +} + +int igt_main () +{ +struct drm_xe_engine_class_instance *hwe; +int fd; + + igt_fixture() { + fd =drm_open_driver (DRIVER_XE ); +xe_device_get (fd); +igt_require_f (xe_has_purgeable_support (fd), +"Kernel does not support purgeable buffer objects\n" ); + } + + igt_subtest("dontneed-before-mmap") +xe_for_each_engine (fd, hwe) { +test_dontneed_before_mmap (fd); +break ; + } + + igt_fixture() { +xe_device_put (fd); +drm_close_driver (fd); + } +}tests/meson.build +0
-0
tests/meson.build intel_xe_progs = [ · line 320
Quoted 7 lines
tests/meson.build +1
-0@@ -314,6 +314,7 @@ intel_xe_progs = [ 'xe_huc_copy', 'xe_intel_bb', 'xe_live_ktest', + 'xe_madvise', 'xe_media_fill', 'xe_mmap', 'xe_module_load',
LGTM:
PATCH v7 4/9 tests/intel/xe_madvise: Add purged-mmap-blocked subtest
In reply to Arvind YadavAdd a new purged-mmap-blocked subtest that triggers an actual purge via memory pressure and verifies that mmap() fails with -EINVAL once the BO backing store has been permanently discarded.
The purgeable check moved from xe_gem_mmap_offset_ioctl() into a new xe_gem_object_mmap() callback, so the blocking point is now mmap() itself rather than the mmap offset ioctl:
- DRM_IOCTL_XE_GEM_MMAP_OFFSET: always succeeds regardless of purgeable state (just returns the pre-allocated offset)
- mmap() on DONTNEED BO: fails with -EBUSY (temporary state)
- mmap() on purged BO: fails with -EINVAL (permanent, no backing store)
v5:
- Add purged-mmap-blocked subtest to verify mmap is blocked after BO backing store is permanently purged.
v6:
- DRM_IOCTL_XE_GEM_MMAP_OFFSET always succeeds; the purgeable check now happens in xe_gem_object_mmap() at mmap() time. For purged BOs, assert mmap() fails with -EINVAL.
v7:
- Moved trigger_memory_pressure() and purgeable_mark_and_verify_purged() here. (Nishit)
- Use xe_has_vram() instead of checking xe_visible_vram_size() > 0 for clearer dGPU/iGPU detection. (Nishit)
- Fix purgeable_mark_and_verify_purged(): handle retained == 0 from DONTNEED (BO already purged) as success instead of incorrectly returning false. (Nishit)
- Drop unused vm parameter from trigger_memory_pressure(). (Nishit)
--- tests/intel/xe_madvise.c | 136 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+)
tests/intel/xe_madvise.c +136 -0
@@ -59,6 +59,136 @@ static void purgeable_setup_simple_bo(int fd, uint32_t *vm, uint32_t *bo,xe_wait_ufence (fd, &sync_val,1 ,0 ,NSEC_PER_SEC ); } +/** +* trigger_memory_pressure - Fill VRAM/RAM + 50% to force purgeable reclaim +* @fd: DRM file descriptor +* +* Allocates BOs in a temporary VM until memory is overcommitted by 50%, +* forcing the kernel to purge DONTNEED-marked BOs. +*/ +static void trigger_memory_pressure (int fd) +{ +uint64_t mem_size, overpressure; +const uint64_t chunk =8ull <<20 ;/* 8 MiB */ +int max_objs, n =0 ; +uint32_t *handles; +uint64_t total; +void *p; +uint32_t handle, vm; + +/* Use a separate VM so pressure BOs don't affect the test VM */ + vm =xe_vm_create (fd,0 ,0 ); + +/* Purgeable BOs reside in VRAM (dGPU) or system memory (iGPU) */ + mem_size =xe_has_vram (fd) ?xe_visible_vram_size (fd,0 ) : +igt_get_total_ram_mb () <<20 ; + +/* Scale overpressure to 50% of memory, minimum 64MB */ + overpressure = mem_size /2 ; +if (overpressure < (64 <<20 )) + overpressure =64 <<20 ; + + max_objs = (mem_size + overpressure) / chunk +1 ; + handles =malloc (max_objs *sizeof (*handles)); +igt_assert (handles); + + total =0 ; +while (total < mem_size + overpressure && n < max_objs) { +uint32_t err; + + err =__xe_bo_create (fd, vm, chunk, +vram_if_possible (fd,0 ), +DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM , +NULL , &handle); +if (err)/* Out of memory — sufficient pressure achieved */ +break ; + + handles[n++] = handle; + total += chunk; + + p =xe_bo_map (fd, handle, chunk); +igt_assert (p !=MAP_FAILED ); + +/* Fault in all pages so they actually consume memory */ +memset (p,0xCD , chunk); +munmap (p, chunk); + } + +/* Allow shrinker time to process pressure */ +usleep (100000 ); + +for (int i =0 ; i < n; i++) +gem_close (fd, handles[i]); + +free (handles); + +xe_vm_destroy (fd, vm); +} + +/** +* purgeable_mark_and_verify_purged - Mark DONTNEED, pressure, check purged +* @fd: DRM file descriptor +* @vm: VM handle +* @addr: Virtual address of the BO +* @size: Size of the BO +* +* Returns true if the BO was purged under memory pressure. +*/ +static bool purgeable_mark_and_verify_purged (int fd,uint32_t vm,uint64_t addr,size_t size) +{ +uint32_t retained; + +/* Mark as DONTNEED */ + retained =xe_vm_madvise_purgeable (fd, vm, addr, size, +DRM_XE_VMA_PURGEABLE_STATE_DONTNEED ); +if (retained ==0 ) +return true;/* Already purged */ + +/* Trigger memory pressure */ +trigger_memory_pressure (fd); + +/* Verify purged */ + retained =xe_vm_madvise_purgeable (fd, vm, addr, size, +DRM_XE_VMA_PURGEABLE_STATE_WILLNEED ); +return retained ==0 ; +} + +/** +* SUBTEST: purged-mmap-blocked +* Description: After BO is purged, verify mmap() fails with -EINVAL +* Test category: functionality test +*/ +static void test_purged_mmap_blocked (int fd) +{ +uint32_t bo, vm; +uint64_t addr =PURGEABLE_ADDR ; +size_t bo_size =PURGEABLE_BO_SIZE ; +struct drm_xe_gem_mmap_offset mmo = {}; +void *ptr; + +purgeable_setup_simple_bo (fd, &vm, &bo, addr, bo_size, false); +if (!purgeable_mark_and_verify_purged (fd, vm, addr, bo_size)) { +gem_close (fd, bo); +xe_vm_destroy (fd, vm); +igt_skip ("Unable to induce purge on this platform/config" ); + } + +/* +* Getting the mmap offset is always allowed regardless of purgeable +* state - the blocking happens at mmap() time (xe_gem_object_mmap). +* For a purged BO, mmap() must fail with -EINVAL (no backing store). +*/ + mmo.handle = bo; +igt_assert_eq (igt_ioctl (fd,DRM_IOCTL_XE_GEM_MMAP_OFFSET , &mmo),0 ); + + ptr =mmap (NULL , bo_size,PROT_READ |PROT_WRITE ,MAP_SHARED , fd, mmo.offset); +igt_assert_eq_u64 ((uint64_t )ptr, (uint64_t )MAP_FAILED ); +igt_assert_eq (errno,EINVAL ); + +gem_close (fd, bo); +xe_vm_destroy (fd, vm); +} +/** * SUBTEST: dontneed-before-mmap * Description: Mark BO as DONTNEED before mmap, verify mmap() fails with -EBUSY
@@ -115,6 +245,12 @@ int igt_main()break ; } + igt_subtest("purged-mmap-blocked") +xe_for_each_engine (fd, hwe) { +test_purged_mmap_blocked (fd); +break ; + } + igt_fixture() {xe_device_put (fd);drm_close_driver (fd);
Signature · 1 line
2.43.0
Re: [PATCH i-g-t v7 4/9] tests/intel/xe_madvise: Add purged-mmap-blocked subtest
Quoted 152 lines · Arvind Yadav
Add a new purged-mmap-blocked subtest that triggers an actual purge via memory pressure and verifies that
mmap()fails with-EINVALonce the BO backing store has been permanently discarded.The purgeable check moved from
xe_gem_mmap_offset_ioctl()into a newxe_gem_object_mmap()callback, so the blocking point is nowmmap()itself rather than the mmap offset ioctl:-
DRM_IOCTL_XE_GEM_MMAP_OFFSET: always succeeds regardless of purgeable state (just returns the pre-allocated offset)
-mmap()on DONTNEED BO: fails with-EBUSY(temporary state)
-mmap()on purged BO: fails with-EINVAL(permanent, no backing store)v5:
- Add purged-mmap-blocked subtest to verify mmap is blocked after BO backing store is permanently purged.v6:
-DRM_IOCTL_XE_GEM_MMAP_OFFSETalways succeeds; the purgeable check now happens inxe_gem_object_mmap()atmmap()time. For purged BOs, assertmmap()fails with-EINVAL.v7:
- Movedtrigger_memory_pressure()andpurgeable_mark_and_verify_purged()here. (Nishit)
- Usexe_has_vram()instead of checkingxe_visible_vram_size()> 0 for clearer dGPU/iGPU detection. (Nishit)
- Fixpurgeable_mark_and_verify_purged(): handle retained == 0 from DONTNEED (BO already purged) as success instead of incorrectly returning false. (Nishit)
- Drop unused vm parameter fromtrigger_memory_pressure(). (Nishit)Cc Nishit Sharma <[email protected]>Cc Matthew Brost <[email protected]>Cc Thomas Hellström <[email protected]>Cc Himal Prasad Ghimiray <[email protected]>Cc Pravalika Gurram <[email protected]>Signed-off-by Arvind Yadav <[email protected]>--- tests/intel/xe_madvise.c | 136 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+)tests/intel/xe_madvise.c +102
-0@@ -59,6 +59,136 @@ static void purgeable_setup_simple_bo(int fd, uint32_t *vm, uint32_t *bo,xe_wait_ufence (fd, &sync_val,1 ,0 ,NSEC_PER_SEC ); } +/** +* trigger_memory_pressure - Fill VRAM/RAM + 50% to force purgeable reclaim +* @fd: DRM file descriptor +* +* Allocates BOs in a temporary VM until memory is overcommitted by 50%, +* forcing the kernel to purge DONTNEED-marked BOs. +*/ +static void trigger_memory_pressure (int fd) +{ +uint64_t mem_size, overpressure; +const uint64_t chunk =8ull <<20 ;/* 8 MiB */ +int max_objs, n =0 ; +uint32_t *handles; +uint64_t total; +void *p; +uint32_t handle, vm; + +/* Use a separate VM so pressure BOs don't affect the test VM */ + vm =xe_vm_create (fd,0 ,0 ); + +/* Purgeable BOs reside in VRAM (dGPU) or system memory (iGPU) */ + mem_size =xe_has_vram (fd) ?xe_visible_vram_size (fd,0 ) : +igt_get_total_ram_mb () <<20 ; + +/* Scale overpressure to 50% of memory, minimum 64MB */ + overpressure = mem_size /2 ; +if (overpressure < (64 <<20 )) + overpressure =64 <<20 ; + + max_objs = (mem_size + overpressure) / chunk +1 ; + handles =malloc (max_objs *sizeof (*handles)); +igt_assert (handles); + + total =0 ; +while (total < mem_size + overpressure && n < max_objs) { +uint32_t err; + + err =__xe_bo_create (fd, vm, chunk, +vram_if_possible (fd,0 ), +DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM , +NULL , &handle); +if (err)/* Out of memory — sufficient pressure achieved */ +break ; + + handles[n++] = handle; + total += chunk; + + p =xe_bo_map (fd, handle, chunk); +igt_assert (p !=MAP_FAILED ); + +/* Fault in all pages so they actually consume memory */ +memset (p,0xCD , chunk); +munmap (p, chunk); + } + +/* Allow shrinker time to process pressure */ +usleep (100000 ); + +for (int i =0 ; i < n; i++) +gem_close (fd, handles[i]); + +free (handles); + +xe_vm_destroy (fd, vm); +} + +/** +* purgeable_mark_and_verify_purged - Mark DONTNEED, pressure, check purged +* @fd: DRM file descriptor +* @vm: VM handle +* @addr: Virtual address of the BO +* @size: Size of the BO +* +* Returns true if the BO was purged under memory pressure. +*/ +static bool purgeable_mark_and_verify_purged (int fd,uint32_t vm,uint64_t addr,size_t size) +{ +uint32_t retained; + +/* Mark as DONTNEED */ + retained =xe_vm_madvise_purgeable (fd, vm, addr, size, +DRM_XE_VMA_PURGEABLE_STATE_DONTNEED ); +if (retained ==0 ) +return true;/* Already purged */ + +/* Trigger memory pressure */ +trigger_memory_pressure (fd); + +/* Verify purged */ + retained =xe_vm_madvise_purgeable (fd, vm, addr, size, +DRM_XE_VMA_PURGEABLE_STATE_WILLNEED ); +return retained ==0 ; +} + +/** +* SUBTEST: purged-mmap-blocked +* Description: After BO is purged, verify mmap() fails with -EINVAL +* Test category: functionality test +*/ +static void test_purged_mmap_blocked (int fd) +{ +uint32_t bo, vm;
tests/intel/xe_madvise.c static void purgeable_setup_simple_bo(int fd, uint32_t *vm, uint32_t *bo, · line 173
Quoted 10 lines
tests/intel/xe_madvise.c +10
-0+uint64_t addr =PURGEABLE_ADDR ; +size_t bo_size =PURGEABLE_BO_SIZE ; +struct drm_xe_gem_mmap_offset mmo = {}; +void *ptr; + +purgeable_setup_simple_bo (fd, &vm, &bo, addr, bo_size, false); +if (!purgeable_mark_and_verify_purged (fd, vm, addr, bo_size)) { +gem_close (fd, bo); +xe_vm_destroy (fd, vm); +igt_skip ("Unable to induce purge on this platform/config" );
small nit: here skip message reflecting purge support unavailability in platform/config, Simple mention Unable to induce purge.
Quoted 24 lines
tests/intel/xe_madvise.c +18
-0+ } + +/* +* Getting the mmap offset is always allowed regardless of purgeable +* state - the blocking happens at mmap() time (xe_gem_object_mmap). +* For a purged BO, mmap() must fail with -EINVAL (no backing store). +*/ + mmo.handle = bo; +igt_assert_eq (igt_ioctl (fd,DRM_IOCTL_XE_GEM_MMAP_OFFSET , &mmo),0 ); + + ptr =mmap (NULL , bo_size,PROT_READ |PROT_WRITE ,MAP_SHARED , fd, mmo.offset); +igt_assert_eq_u64 ((uint64_t )ptr, (uint64_t )MAP_FAILED ); +igt_assert_eq (errno,EINVAL ); + +gem_close (fd, bo); +xe_vm_destroy (fd, vm); +} +/** * SUBTEST: dontneed-before-mmap * Description: Mark BO as DONTNEED before mmap, verify mmap() fails with -EBUSY @@ -115,6 +245,12 @@ int igt_main()break ; }
tests/intel/xe_madvise.c int igt_main() · line 256
Quoted 10 lines
tests/intel/xe_madvise.c +6
-0+ igt_subtest("purged-mmap-blocked") +xe_for_each_engine (fd, hwe) { +test_purged_mmap_blocked (fd); +break ; + } + igt_fixture() {xe_device_put (fd);drm_close_driver (fd);
With above Nit change LGTM:
PATCH v7 5/9 tests/intel/xe_madvise: Add dontneed-after-mmap subtest
In reply to Arvind YadavThis test verifies that an existing mmap becomes invalid after the BO is marked as purgeable and purged. The test creates a BO, maps it, writes data, then marks it DONTNEED and triggers memory pressure. Accessing the previously valid mapping should now trigger SIGBUS or SIGSEGV, confirming that existing mappings are correctly invalidated when the backing store is purged.
v4:
- Added proper resource cleanup before calling igt_skip(). (Nishit)
- Added assertion for xe_bo_map. (Nishit)
v6:
- Fix sigsetjmp(jmp, SIGBUS | SIGSEGV) to sigsetjmp(jmp, 1). The second argument is a plain boolean savemask, not a signal set.
v7:
- Move sigtrap/jmp_buf in this patch. (Nishit)
--- tests/intel/xe_madvise.c | 71 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+)
tests/intel/xe_madvise.c +71 -0
@@ -153,6 +153,13 @@ static bool purgeable_mark_and_verify_purged(int fd, uint32_t vm, uint64_t addr,return retained ==0 ; } +static jmp_buf jmp; + +__noreturn static void sigtrap (int sig) +{ +siglongjmp (jmp, sig); +} +/** * SUBTEST: purged-mmap-blocked * Description: After BO is purged, verify mmap() fails with -EINVAL
@@ -227,6 +234,64 @@ static void test_dontneed_before_mmap(int fd)xe_vm_destroy (fd, vm); } +/** +* SUBTEST: dontneed-after-mmap +* Description: Mark BO as DONTNEED after mmap, verify SIGBUS on accessing purged mapping +* Test category: functionality test +*/ +static void test_dontneed_after_mmap (int fd) +{ +uint32_t bo, vm; +uint64_t addr =PURGEABLE_ADDR ; +size_t bo_size =PURGEABLE_BO_SIZE ; +void *map; + +purgeable_setup_simple_bo (fd, &vm, &bo, addr, bo_size, true); + + map =xe_bo_map (fd, bo, bo_size); +igt_assert (map !=MAP_FAILED ); +memset (map,0xAB , bo_size); + +if (!purgeable_mark_and_verify_purged (fd, vm, addr, bo_size)) { +munmap (map, bo_size); +gem_close (fd, bo); +xe_vm_destroy (fd, vm); +igt_skip ("Unable to induce purge on this platform/config" ); + } + +/* Access purged mapping - should trigger SIGBUS/SIGSEGV */ + { +sighandler_t old_sigsegv, old_sigbus; +char *ptr = (char *)map; +int sig; + + old_sigsegv =signal (SIGSEGV , (__sighandler_t )sigtrap); + old_sigbus =signal (SIGBUS , (__sighandler_t )sigtrap); + + sig =sigsetjmp (jmp,1 );/* savemask=1: save/restore signal mask */ +switch (sig) { +case SIGBUS : +case SIGSEGV : +/* Expected - purged mapping access failed */ +break ; +case 0 : + *ptr =0 ; +default : +igt_assert_f (false, +"Access to purged mapping should trigger SIGBUS, got sig=%d\n" , + sig); +break ; + } + +signal (SIGBUS , old_sigbus); +signal (SIGSEGV , old_sigsegv); + } + +munmap (map, bo_size); +gem_close (fd, bo); +xe_vm_destroy (fd, vm); +} +int igt_main () {struct drm_xe_engine_class_instance *hwe;
@@ -251,6 +316,12 @@ int igt_main()break ; } + igt_subtest("dontneed-after-mmap") +xe_for_each_engine (fd, hwe) { +test_dontneed_after_mmap (fd); +break ; + } + igt_fixture() {xe_device_put (fd);drm_close_driver (fd);
Signature · 1 line
2.43.0
Re: [PATCH i-g-t v7 5/9] tests/intel/xe_madvise: Add dontneed-after-mmap subtest
Quoted 63 lines · Arvind Yadav
This test verifies that an existing mmap becomes invalid after the BO is marked as purgeable and purged. The test creates a BO, maps it, writes data, then marks it DONTNEED and triggers memory pressure. Accessing the previously valid mapping should now trigger SIGBUS or SIGSEGV, confirming that existing mappings are correctly invalidated when the backing store is purged.
v4:
- Added proper resource cleanup before callingigt_skip(). (Nishit)
- Added assertion forxe_bo_map. (Nishit)v6:
- Fixsigsetjmp(jmp, SIGBUS | SIGSEGV)tosigsetjmp(jmp, 1). The second argument is a plain boolean savemask, not a signal set.v7:
- Move sigtrap/jmp_buf in this patch. (Nishit)Cc Nishit Sharma <[email protected]>Cc Matthew Brost <[email protected]>Cc Thomas Hellström <[email protected]>Cc Himal Prasad Ghimiray <[email protected]>Cc Pravalika Gurram <[email protected]>Signed-off-by Arvind Yadav <[email protected]>--- tests/intel/xe_madvise.c | 71 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+)tests/intel/xe_madvise.c +20
-0@@ -153,6 +153,13 @@ static bool purgeable_mark_and_verify_purged(int fd, uint32_t vm, uint64_t addr,return retained ==0 ; } +static jmp_buf jmp; + +__noreturn static void sigtrap (int sig) +{ +siglongjmp (jmp, sig); +} +/** * SUBTEST: purged-mmap-blocked * Description: After BO is purged, verify mmap() fails with -EINVAL @@ -227,6 +234,64 @@ static void test_dontneed_before_mmap(int fd)xe_vm_destroy (fd, vm); } +/** +* SUBTEST: dontneed-after-mmap +* Description: Mark BO as DONTNEED after mmap, verify SIGBUS on accessing purged mapping +* Test category: functionality test +*/ +static void test_dontneed_after_mmap (int fd) +{ +uint32_t bo, vm; +uint64_t addr =PURGEABLE_ADDR ; +size_t bo_size =PURGEABLE_BO_SIZE ; +void *map; + +purgeable_setup_simple_bo (fd, &vm, &bo, addr, bo_size, true);
tests/intel/xe_madvise.c static void test_dontneed_before_mmap(int fd) · line 259
Quoted 10 lines
tests/intel/xe_madvise.c +10
-0+ + map =xe_bo_map (fd, bo, bo_size); +igt_assert (map !=MAP_FAILED ); +memset (map,0xAB , bo_size); + +if (!purgeable_mark_and_verify_purged (fd, vm, addr, bo_size)) { +munmap (map, bo_size); +gem_close (fd, bo); +xe_vm_destroy (fd, vm); +igt_skip ("Unable to induce purge on this platform/config" );
Same Nit as in Patch-4/9.
Quoted 9 lines
tests/intel/xe_madvise.c +9
-0+ } + +/* Access purged mapping - should trigger SIGBUS/SIGSEGV */ + { +sighandler_t old_sigsegv, old_sigbus; +char *ptr = (char *)map; +int sig; + + old_sigsegv =signal (SIGSEGV , (__sighandler_t )sigtrap);
tests/intel/xe_madvise.c static void test_dontneed_before_mmap(int fd) · line 278
Quoted 10 lines
tests/intel/xe_madvise.c +10
-0+ old_sigbus =signal (SIGBUS , (__sighandler_t )sigtrap); + + sig =sigsetjmp (jmp,1 );/* savemask=1: save/restore signal mask */ +switch (sig) { +case SIGBUS : +case SIGSEGV : +/* Expected - purged mapping access failed */ +break ; +case 0 : + *ptr =0 ;
Here if suppose SIGBUS/SIGSEGV is not hit but mapping is invalidated, here you'll get segfault. Remove *ptr = 0 and club case 0: and default and
let it assert as available
Quoted 22 lines
tests/intel/xe_madvise.c +16
-0+default : +igt_assert_f (false, +"Access to purged mapping should trigger SIGBUS, got sig=%d\n" , + sig); +break ; + } + +signal (SIGBUS , old_sigbus); +signal (SIGSEGV , old_sigsegv); + } + +munmap (map, bo_size); +gem_close (fd, bo); +xe_vm_destroy (fd, vm); +} +int igt_main () {struct drm_xe_engine_class_instance *hwe;@@ -251,6 +316,12 @@ int igt_main()break ; }
tests/intel/xe_madvise.c int igt_main() · line 327
Quoted 10 lines
tests/intel/xe_madvise.c +6
-0+ igt_subtest("dontneed-after-mmap") +xe_for_each_engine (fd, hwe) { +test_dontneed_after_mmap (fd); +break ; + } + igt_fixture() {xe_device_put (fd);drm_close_driver (fd);
With above changes LGTM:
Re: [PATCH i-g-t v7 5/9] tests/intel/xe_madvise: Add dontneed-after-mmap subtest
Quoted 99 lines · Nishit
Quoted 63 lines · Arvind Yadav
This test verifies that an existing mmap becomes invalid after the BO is marked as purgeable and purged. The test creates a BO, maps it, writes data, then marks it DONTNEED and triggers memory pressure. Accessing the previously valid mapping should now trigger SIGBUS or SIGSEGV, confirming that existing mappings are correctly invalidated when the backing store is purged.
v4:
- Added proper resource cleanup before callingigt_skip(). (Nishit)
- Added assertion forxe_bo_map. (Nishit)v6:
- Fixsigsetjmp(jmp, SIGBUS | SIGSEGV)tosigsetjmp(jmp, 1). The second argument is a plain boolean savemask, not a signal set.v7:
- Move sigtrap/jmp_buf in this patch. (Nishit)Cc Nishit Sharma <[email protected]>Cc Matthew Brost <[email protected]>Cc Thomas Hellström <[email protected]>Cc Himal Prasad Ghimiray <[email protected]>Cc Pravalika Gurram <[email protected]>Signed-off-by Arvind Yadav <[email protected]>---
tests/intel/xe_madvise.c| 71 ++++++++++++++++++++++++++++++++++++++++1 file changed, 71 insertions(+)tests/intel/xe_madvise.c +6
-0@@ -153,6 +153,13 @@ static bool purgeable_mark_and_verify_purged(int fd, uint32_t vm, uint64_t addr, return retained == 0; } +static jmp_buf jmp; + +__noreturn static void sigtrap (int sig) +{ + siglongjmp(jmp, sig); +} + /*** SUBTEST: purged-mmap-blocked * Description: After BO is purged, verify
mmap()fails with-EINVALtests/intel/xe_madvise.c +0
-0@@ -227,6 +234,64 @@ static void test_dontneed_before_mmap(int fd) xe_vm_destroy(fd, vm); }+/**
+ * SUBTEST: dontneed-after-mmap
+ * Description: Mark BO as DONTNEED after mmap, verify SIGBUS on accessing purged mapping
+ * Test category: functionality test
+ */tests/intel/xe_madvise.c +8
-0+static void test_dontneed_after_mmap (int fd) +{ + uint32_t bo, vm; + uint64_t addr = PURGEABLE_ADDR; + size_t bo_size = PURGEABLE_BO_SIZE; + void *map; + + purgeable_setup_simple_bo(fd, &vm, &bo, addr, bo_size, true);tests/intel/xe_madvise.c static void test_dontneed_before_mmap(int fd) · line 259
Quoted 10 lines
tests/intel/xe_madvise.c +10
-0+ + map = xe_bo_map(fd, bo, bo_size); + igt_assert(map != MAP_FAILED); + memset(map, 0xAB, bo_size); + + if (!purgeable_mark_and_verify_purged(fd, vm, addr, bo_size)) { + munmap(map, bo_size); + gem_close(fd, bo); + xe_vm_destroy(fd, vm); + igt_skip("Unable to induce purge on this platform/config");Same Nit as in Patch-4/9.
Quoted 12 lines
+ }
tests/intel/xe_madvise.c +11
-0+ + /* Access purged mapping - should trigger SIGBUS/SIGSEGV */ + { + sighandler_t old_sigsegv, old_sigbus; + char *ptr = (char *)map; + int sig; + + old_sigsegv = signal(SIGSEGV, (__sighandler_t)sigtrap); + old_sigbus = signal(SIGBUS, (__sighandler_t)sigtrap); + + sig = sigsetjmp(jmp, 1); /* savemask=1: save/restore signalQuoted 8 lines
mask */
+ switch (sig) {
+ case SIGBUS:
+ case SIGSEGV:
+ /* Expected - purged mapping access failed */ + break;
+ case 0:
+ *ptr = 0;Here if suppose SIGBUS/SIGSEGV is not hit but mapping is invalidated, here you'll get segfault. Remove *ptr = 0 and club case 0: and default and
Quoted 1 line
let it assert as available
If we remove *ptr = 0, we never actually access the purged mapping, so SIGBUS is never triggered and the test would always fail on the assert - defeating its purpose. we can say without *ptr = 0, we never touch the purged mapping at all, so no signal would fire and the test would always hit the assert.
Thanks,
Arvind
Quoted 38 lines
Quoted 23 lines
+ default:
+ igt_assert_f(false,
+ "Access to purged mapping should trigger SIGBUS, got sig=%d\n",
+ sig);
+ break;
+ }tests/intel/xe_madvise.c +10
-0+ + signal(SIGBUS, old_sigbus); + signal(SIGSEGV, old_sigsegv); + } + + munmap(map, bo_size); + gem_close(fd, bo); + xe_vm_destroy(fd, vm); +} +int
igt_main()
{
structdrm_xe_engine_class_instance*hwe;tests/intel/xe_madvise.c +0
-0@@ -251,6 +316,12 @@ int igt_main() break;tests/intel/xe_madvise.c int igt_main() · line 326
Quoted 10 lines
tests/intel/xe_madvise.c +5
-0} + igt_subtest("dontneed-after-mmap") + xe_for_each_engine(fd, hwe) { + test_dontneed_after_mmap(fd); + break; + } + igt_fixture() { xe_device_put(fd); drm_close_driver(fd);With above changes LGTM:
Reviewed-by Nishit Sharma <[email protected]>
PATCH v7 6/9 tests/intel/xe_madvise: Add dontneed-before-exec subtest
In reply to Arvind YadavThis test validates GPU execution behavior when a data BO is purged before submission. The test creates a batch that writes to a data BO, purges the data BO (while keeping the batch BO valid to avoid GPU reset), then submits for execution. With VM_CREATE_FLAG_SCRATCH_PAGE, the GPU write may succeed by landing on scratch memory instead of the purged BO, demonstrating graceful handling of purged memory during GPU operations.
v4:
- Added proper resource cleanup before calling igt_skip(). (Nishit)
--- tests/intel/xe_madvise.c | 148 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+)
tests/intel/xe_madvise.c +148 -0
@@ -19,7 +19,11 @@/* Purgeable test constants */ #define PURGEABLE_ADDR 0x1a0000 +#define PURGEABLE_BATCH_ADDR 0x3c0000#define PURGEABLE_BO_SIZE 4096 +#define PURGEABLE_FENCE_VAL 0xbeef +#define PURGEABLE_TEST_PATTERN 0xc0ffee +#define PURGEABLE_DEAD_PATTERN 0xdeadstatic bool xe_has_purgeable_support (int fd) {
@@ -196,6 +200,62 @@ static void test_purged_mmap_blocked(int fd)xe_vm_destroy (fd, vm); } +/** +* purgeable_setup_batch_and_data - Setup VM with batch and data BOs for GPU exec +* @fd: DRM file descriptor +* @vm: Output VM handle +* @bind_engine: Output bind engine handle +* @batch_bo: Output batch BO handle +* @data_bo: Output data BO handle +* @batch: Output batch buffer pointer +* @data: Output data buffer pointer +* @batch_addr: Batch virtual address +* @data_addr: Data virtual address +* @batch_size: Batch buffer size +* @data_size: Data buffer size +* +* Helper to create VM, bind engine, batch and data BOs, and bind them. +*/ +static void purgeable_setup_batch_and_data (int fd,uint32_t *vm, +uint32_t *bind_engine, +uint32_t *batch_bo, +uint32_t *data_bo, +uint32_t **batch, +uint32_t **data, +uint64_t batch_addr, +uint64_t data_addr, +size_t batch_size, +size_t data_size) +{ +struct drm_xe_sync sync = { + .type =DRM_XE_SYNC_TYPE_USER_FENCE , + .flags =DRM_XE_SYNC_FLAG_SIGNAL , + .timeline_value =PURGEABLE_FENCE_VAL , + }; +uint64_t vm_sync =0 ; + + *vm =xe_vm_create (fd,DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE ,0 ); + *bind_engine =xe_bind_exec_queue_create (fd, *vm,0 ); + +/* Create and bind batch BO */ + *batch_bo =xe_bo_create (fd, *vm, batch_size,vram_if_possible (fd,0 ), +DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM ); + *batch =xe_bo_map (fd, *batch_bo, batch_size); + + sync.addr =to_user_pointer (&vm_sync); +xe_vm_bind_async (fd, *vm, *bind_engine, *batch_bo,0 , batch_addr, batch_size, &sync,1 ); +xe_wait_ufence (fd, &vm_sync,PURGEABLE_FENCE_VAL ,0 ,NSEC_PER_SEC ); + +/* Create and bind data BO */ + *data_bo =xe_bo_create (fd, *vm, data_size,vram_if_possible (fd,0 ), +DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM ); + *data =xe_bo_map (fd, *data_bo, data_size); + + vm_sync =0 ; +xe_vm_bind_async (fd, *vm, *bind_engine, *data_bo,0 , data_addr, data_size, &sync,1 ); +xe_wait_ufence (fd, &vm_sync,PURGEABLE_FENCE_VAL ,0 ,NSEC_PER_SEC ); +} +/** * SUBTEST: dontneed-before-mmap * Description: Mark BO as DONTNEED before mmap, verify mmap() fails with -EBUSY
@@ -292,6 +352,88 @@ static void test_dontneed_after_mmap(int fd)xe_vm_destroy (fd, vm); } +/** +* SUBTEST: dontneed-before-exec +* Description: Mark BO as DONTNEED before GPU exec, verify GPU behavior with SCRATCH_PAGE +* Test category: functionality test +*/ +static void test_dontneed_before_exec (int fd,struct drm_xe_engine_class_instance *hwe) +{ +uint32_t vm, exec_queue, bo, batch_bo, bind_engine; +uint64_t data_addr =PURGEABLE_ADDR ; +uint64_t batch_addr =PURGEABLE_BATCH_ADDR ; +size_t data_size =PURGEABLE_BO_SIZE ; +size_t batch_size =PURGEABLE_BO_SIZE ; +struct drm_xe_sync sync[1 ] = { + { .type =DRM_XE_SYNC_TYPE_USER_FENCE , + .flags =DRM_XE_SYNC_FLAG_SIGNAL , + .timeline_value =PURGEABLE_FENCE_VAL }, + }; +struct drm_xe_exec exec = { + .num_batch_buffer =1 , + .num_syncs =1 , + .syncs =to_user_pointer (sync), + }; +uint32_t *data, *batch; +uint64_t vm_sync =0 ; +int b, ret; + +purgeable_setup_batch_and_data (fd, &vm, &bind_engine, &batch_bo, + &bo, &batch, &data, batch_addr, + data_addr, batch_size, data_size); + +/* Prepare batch */ + b =0 ; + batch[b++] =MI_STORE_DWORD_IMM_GEN4 ; + batch[b++] = data_addr; + batch[b++] = data_addr >>32 ; + batch[b++] =PURGEABLE_DEAD_PATTERN ; + batch[b++] =MI_BATCH_BUFFER_END ; + +/* Phase 1: Purge data BO, batch BO still valid */ +if (!purgeable_mark_and_verify_purged (fd, vm, data_addr, data_size)) { +munmap (data, data_size); +munmap (batch, batch_size); +gem_close (fd, bo); +gem_close (fd, batch_bo); +xe_exec_queue_destroy (fd, bind_engine); +xe_vm_destroy (fd, vm); +igt_skip ("Unable to induce purge on this platform/config" ); + } + + exec_queue =xe_exec_queue_create (fd, vm, hwe,0 ); + exec.exec_queue_id = exec_queue; + exec.address = batch_addr; + + vm_sync =0 ; + sync[0 ].addr =to_user_pointer (&vm_sync); + +/* +* VM has SCRATCH_PAGE — exec may succeed with the GPU write +* landing on scratch instead of the purged data BO. +*/ + ret =__xe_exec (fd, &exec); +if (ret ==0 ) { +int64_t timeout =NSEC_PER_SEC ; + +__xe_wait_ufence (fd, &vm_sync,PURGEABLE_FENCE_VAL , + exec_queue, &timeout); + } + +/* +* Don't purge the batch BO — GPU would fetch zeroed scratch +* instructions and trigger an engine reset. +*/ + +munmap (data, data_size); +munmap (batch, batch_size); +gem_close (fd, bo); +gem_close (fd, batch_bo); +xe_exec_queue_destroy (fd, bind_engine); +xe_exec_queue_destroy (fd, exec_queue); +xe_vm_destroy (fd, vm); +} +int igt_main () {struct drm_xe_engine_class_instance *hwe;
@@ -322,6 +464,12 @@ int igt_main()break ; } + igt_subtest("dontneed-before-exec") +xe_for_each_engine (fd, hwe) { +test_dontneed_before_exec (fd, hwe); +break ; + } + igt_fixture() {xe_device_put (fd);drm_close_driver (fd);
Signature · 1 line
2.43.0
Re: [PATCH i-g-t v7 6/9] tests/intel/xe_madvise: Add dontneed-before-exec subtest
Quoted 192 lines · Arvind Yadav
This test validates GPU execution behavior when a data BO is purged before submission. The test creates a batch that writes to a data BO, purges the data BO (while keeping the batch BO valid to avoid GPU reset), then submits for execution. With
VM_CREATE_FLAG_SCRATCH_PAGE, the GPU write may succeed by landing on scratch memory instead of the purged BO, demonstrating graceful handling of purged memory during GPU operations.v4:
- Added proper resource cleanup before callingigt_skip(). (Nishit)Cc Nishit Sharma <[email protected]>Cc Matthew Brost <[email protected]>Cc Thomas Hellström <[email protected]>Cc Himal Prasad Ghimiray <[email protected]>Reviewed-by Pravalika Gurram <[email protected]>Signed-off-by Arvind Yadav <[email protected]>--- tests/intel/xe_madvise.c | 148 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+)tests/intel/xe_madvise.c +142
-0@@ -19,7 +19,11 @@/* Purgeable test constants */ #define PURGEABLE_ADDR 0x1a0000 +#define PURGEABLE_BATCH_ADDR 0x3c0000#define PURGEABLE_BO_SIZE 4096 +#define PURGEABLE_FENCE_VAL 0xbeef +#define PURGEABLE_TEST_PATTERN 0xc0ffee +#define PURGEABLE_DEAD_PATTERN 0xdeadstatic bool xe_has_purgeable_support (int fd) {@@ -196,6 +200,62 @@ static void test_purged_mmap_blocked(int fd)xe_vm_destroy (fd, vm); } +/** +* purgeable_setup_batch_and_data - Setup VM with batch and data BOs for GPU exec +* @fd: DRM file descriptor +* @vm: Output VM handle +* @bind_engine: Output bind engine handle +* @batch_bo: Output batch BO handle +* @data_bo: Output data BO handle +* @batch: Output batch buffer pointer +* @data: Output data buffer pointer +* @batch_addr: Batch virtual address +* @data_addr: Data virtual address +* @batch_size: Batch buffer size +* @data_size: Data buffer size +* +* Helper to create VM, bind engine, batch and data BOs, and bind them. +*/ +static void purgeable_setup_batch_and_data (int fd,uint32_t *vm, +uint32_t *bind_engine, +uint32_t *batch_bo, +uint32_t *data_bo, +uint32_t **batch, +uint32_t **data, +uint64_t batch_addr, +uint64_t data_addr, +size_t batch_size, +size_t data_size) +{ +struct drm_xe_sync sync = { + .type =DRM_XE_SYNC_TYPE_USER_FENCE , + .flags =DRM_XE_SYNC_FLAG_SIGNAL , + .timeline_value =PURGEABLE_FENCE_VAL , + }; +uint64_t vm_sync =0 ; + + *vm =xe_vm_create (fd,DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE ,0 ); + *bind_engine =xe_bind_exec_queue_create (fd, *vm,0 ); + +/* Create and bind batch BO */ + *batch_bo =xe_bo_create (fd, *vm, batch_size,vram_if_possible (fd,0 ), +DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM ); + *batch =xe_bo_map (fd, *batch_bo, batch_size); + + sync.addr =to_user_pointer (&vm_sync); +xe_vm_bind_async (fd, *vm, *bind_engine, *batch_bo,0 , batch_addr, batch_size, &sync,1 ); +xe_wait_ufence (fd, &vm_sync,PURGEABLE_FENCE_VAL ,0 ,NSEC_PER_SEC ); + +/* Create and bind data BO */ + *data_bo =xe_bo_create (fd, *vm, data_size,vram_if_possible (fd,0 ), +DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM ); + *data =xe_bo_map (fd, *data_bo, data_size); + + vm_sync =0 ; +xe_vm_bind_async (fd, *vm, *bind_engine, *data_bo,0 , data_addr, data_size, &sync,1 ); +xe_wait_ufence (fd, &vm_sync,PURGEABLE_FENCE_VAL ,0 ,NSEC_PER_SEC ); +} +/** * SUBTEST: dontneed-before-mmap * Description: Mark BO as DONTNEED before mmap, verify mmap() fails with -EBUSY @@ -292,6 +352,88 @@ static void test_dontneed_after_mmap(int fd)xe_vm_destroy (fd, vm); } +/** +* SUBTEST: dontneed-before-exec +* Description: Mark BO as DONTNEED before GPU exec, verify GPU behavior with SCRATCH_PAGE +* Test category: functionality test +*/ +static void test_dontneed_before_exec (int fd,struct drm_xe_engine_class_instance *hwe) +{ +uint32_t vm, exec_queue, bo, batch_bo, bind_engine; +uint64_t data_addr =PURGEABLE_ADDR ; +uint64_t batch_addr =PURGEABLE_BATCH_ADDR ; +size_t data_size =PURGEABLE_BO_SIZE ; +size_t batch_size =PURGEABLE_BO_SIZE ; +struct drm_xe_sync sync[1 ] = { + { .type =DRM_XE_SYNC_TYPE_USER_FENCE , + .flags =DRM_XE_SYNC_FLAG_SIGNAL , + .timeline_value =PURGEABLE_FENCE_VAL }, + }; +struct drm_xe_exec exec = { + .num_batch_buffer =1 , + .num_syncs =1 , + .syncs =to_user_pointer (sync), + }; +uint32_t *data, *batch; +uint64_t vm_sync =0 ; +int b, ret; + +purgeable_setup_batch_and_data (fd, &vm, &bind_engine, &batch_bo, + &bo, &batch, &data, batch_addr, + data_addr, batch_size, data_size); + +/* Prepare batch */ + b =0 ; + batch[b++] =MI_STORE_DWORD_IMM_GEN4 ; + batch[b++] = data_addr; + batch[b++] = data_addr >>32 ; + batch[b++] =PURGEABLE_DEAD_PATTERN ; + batch[b++] =MI_BATCH_BUFFER_END ; + +/* Phase 1: Purge data BO, batch BO still valid */ +if (!purgeable_mark_and_verify_purged (fd, vm, data_addr, data_size)) { +munmap (data, data_size); +munmap (batch, batch_size); +gem_close (fd, bo); +gem_close (fd, batch_bo); +xe_exec_queue_destroy (fd, bind_engine); +xe_vm_destroy (fd, vm); +igt_skip ("Unable to induce purge on this platform/config" ); + } + + exec_queue =xe_exec_queue_create (fd, vm, hwe,0 ); + exec.exec_queue_id = exec_queue; + exec.address = batch_addr; + + vm_sync =0 ; + sync[0 ].addr =to_user_pointer (&vm_sync); + +/* +* VM has SCRATCH_PAGE — exec may succeed with the GPU write +* landing on scratch instead of the purged data BO. +*/ + ret =__xe_exec (fd, &exec); +if (ret ==0 ) { +int64_t timeout =NSEC_PER_SEC ; + +__xe_wait_ufence (fd, &vm_sync,PURGEABLE_FENCE_VAL , + exec_queue, &timeout); + } + +/* +* Don't purge the batch BO — GPU would fetch zeroed scratch +* instructions and trigger an engine reset. +*/ + +munmap (data, data_size); +munmap (batch, batch_size); +gem_close (fd, bo); +gem_close (fd, batch_bo); +xe_exec_queue_destroy (fd, bind_engine); +xe_exec_queue_destroy (fd, exec_queue); +xe_vm_destroy (fd, vm); +} +int igt_main () {struct drm_xe_engine_class_instance *hwe;@@ -322,6 +464,12 @@ int igt_main()break ; }
tests/intel/xe_madvise.c int igt_main() · line 475
Quoted 10 lines
tests/intel/xe_madvise.c +6
-0+ igt_subtest("dontneed-before-exec") +xe_for_each_engine (fd, hwe) { +test_dontneed_before_exec (fd, hwe); +break ; + } + igt_fixture() {xe_device_put (fd);drm_close_driver (fd);
LGTM:
PATCH v7 7/9 tests/intel/xe_madvise: Add dontneed-after-exec subtest
In reply to Arvind YadavThis test verifies that memory can be marked purgeable and reclaimed after successful GPU execution. The test first executes a batch that writes to a data BO and verifies the result. It then marks the BO as DONTNEED, triggers memory pressure to purge it, and attempts a second execution. The second execution may fail or succeed with scratch rebind, validating that the kernel correctly handles purged BOs in GPU submissions.
v4:
- Added proper resource cleanup before calling igt_skip(). (Nishit)
- Added assertion for xe_bo_map. (Nishit)
- Now using sync[0] consistently. (Nishit)
- Added clarifying comment. (Nishit)
--- tests/intel/xe_madvise.c | 108 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+)
tests/intel/xe_madvise.c +108 -0
@@ -16,6 +16,7 @@#include "xe/xe_ioctl.h" #include "xe/xe_query.h" +#include "lib/igt_syncobj.h" /* Purgeable test constants */ #define PURGEABLE_ADDR 0x1a0000
@@ -434,6 +435,107 @@ static void test_dontneed_before_exec(int fd, struct drm_xe_engine_class_instancxe_vm_destroy (fd, vm); } +/** +* SUBTEST: dontneed-after-exec +* Description: Mark BO as DONTNEED after GPU exec, verify memory becomes inaccessible +* Test category: functionality test +*/ +static void test_dontneed_after_exec (int fd,struct drm_xe_engine_class_instance *hwe) +{ +uint32_t vm, exec_queue, bo, batch_bo, bind_engine; +uint64_t data_addr =PURGEABLE_ADDR ; +uint64_t batch_addr =PURGEABLE_BATCH_ADDR ; +size_t data_size =PURGEABLE_BO_SIZE ; +size_t batch_size =PURGEABLE_BO_SIZE ; +struct drm_xe_sync sync[2 ] = { + { .type =DRM_XE_SYNC_TYPE_USER_FENCE , + .flags =DRM_XE_SYNC_FLAG_SIGNAL , + .timeline_value =PURGEABLE_FENCE_VAL }, + { .type =DRM_XE_SYNC_TYPE_SYNCOBJ , + .flags =DRM_XE_SYNC_FLAG_SIGNAL }, + }; +struct drm_xe_exec exec = { + .num_batch_buffer =1 , + .num_syncs =2 , + .syncs =to_user_pointer (sync), + }; +uint32_t *data, *batch; +uint32_t syncobj; +int b, ret; + +purgeable_setup_batch_and_data (fd, &vm, &bind_engine, &batch_bo, + &bo, &batch, &data, batch_addr, + data_addr, batch_size, data_size); +memset (data,0 , data_size); + + syncobj =syncobj_create (fd,0 ); + +/* Prepare batch to write to data BO */ + b =0 ; + batch[b++] =MI_STORE_DWORD_IMM_GEN4 ; + batch[b++] = data_addr; + batch[b++] = data_addr >>32 ; + batch[b++] =0xfeed0001 ; + batch[b++] =MI_BATCH_BUFFER_END ; + + exec_queue =xe_exec_queue_create (fd, vm, hwe,0 ); + exec.exec_queue_id = exec_queue; + exec.address = batch_addr; + +/* Use only syncobj for exec (not USER_FENCE) */ + sync[0 ].type =DRM_XE_SYNC_TYPE_SYNCOBJ ; + sync[0 ].flags =DRM_XE_SYNC_FLAG_SIGNAL ; + sync[0 ].handle = syncobj; + exec.num_syncs =1 ; + exec.syncs =to_user_pointer (&sync[0 ]); + + ret =__xe_exec (fd, &exec); +igt_assert_eq (ret,0 ); + +igt_assert (syncobj_wait (fd, &syncobj,1 ,INT64_MAX ,0 ,NULL )); +munmap (data, data_size); + data =xe_bo_map (fd, bo, data_size); +igt_assert (data !=MAP_FAILED ); +igt_assert_eq (data[0 ],0xfeed0001 ); + +if (!purgeable_mark_and_verify_purged (fd, vm, data_addr, data_size)) { +munmap (data, data_size); +munmap (batch, batch_size); +gem_close (fd, bo); +gem_close (fd, batch_bo); +syncobj_destroy (fd, syncobj); +xe_exec_queue_destroy (fd, bind_engine); +xe_exec_queue_destroy (fd, exec_queue); +xe_vm_destroy (fd, vm); +igt_skip ("Unable to induce purge on this platform/config" ); + } + +/* Prepare second batch (different value) */ + b =0 ; + batch[b++] =MI_STORE_DWORD_IMM_GEN4 ; + batch[b++] = data_addr; + batch[b++] = data_addr >>32 ; + batch[b++] =0xfeed0002 ; + batch[b++] =MI_BATCH_BUFFER_END ; + +/* +* Second exec with purged BO - may succeed (scratch rebind) or fail. +* Either is valid, so don't check results. +*/ + ret =__xe_exec (fd, &exec); +if (ret ==0 ) +syncobj_wait (fd, &syncobj,1 ,INT64_MAX ,0 ,NULL ); + +munmap (data, data_size); +munmap (batch, batch_size); +gem_close (fd, bo); +gem_close (fd, batch_bo); +syncobj_destroy (fd, syncobj); +xe_exec_queue_destroy (fd, bind_engine); +xe_exec_queue_destroy (fd, exec_queue); +xe_vm_destroy (fd, vm); +} +int igt_main () {struct drm_xe_engine_class_instance *hwe;
@@ -470,6 +572,12 @@ int igt_main()break ; } + igt_subtest("dontneed-after-exec") +xe_for_each_engine (fd, hwe) { +test_dontneed_after_exec (fd, hwe); +break ; + } + igt_fixture() {xe_device_put (fd);drm_close_driver (fd);
Signature · 1 line
2.43.0
PATCH v7 8/9 tests/intel/xe_madvise: Add per-vma-tracking subtest
In reply to Arvind YadavThis test validates that purgeable state is tracked per-VMA when a single BO is bound in multiple VMs. The test creates one BO shared across two VMs at different virtual addresses. It verifies that marking only one VMA as DONTNEED does not make the BO purgeable, but marking both VMAs as DONTNEED allows the kernel to purge the shared BO. This ensures proper per-VMA tracking for shared memory.
v4:
- The comment now clarifies that triggering pressure. (Nishit)
v6:
- Move resource cleanup before igt_skip() to avoid leaking VM and BO handles on platforms where memory pressure cannot be induced; replace igt_assert_eq(retained, 0) with a graceful skip. (Nishit)
v7:
- Removed unused 'hwe' parameter.
--- tests/intel/xe_madvise.c | 114 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+)
tests/intel/xe_madvise.c +114 -0
@@ -20,6 +20,7 @@/* Purgeable test constants */ #define PURGEABLE_ADDR 0x1a0000 +#define PURGEABLE_ADDR2 0x2b0000#define PURGEABLE_BATCH_ADDR 0x3c0000#define PURGEABLE_BO_SIZE 4096#define PURGEABLE_FENCE_VAL 0xbeef
@@ -257,6 +258,58 @@ static void purgeable_setup_batch_and_data(int fd, uint32_t *vm,xe_wait_ufence (fd, &vm_sync,PURGEABLE_FENCE_VAL ,0 ,NSEC_PER_SEC ); } +/** +* purgeable_setup_two_vms_shared_bo - Setup two VMs with one shared BO +* @fd: DRM file descriptor +* @vm1: Output first VM handle +* @vm2: Output second VM handle +* @bo: Output shared BO handle +* @addr1: Virtual address in VM1 +* @addr2: Virtual address in VM2 +* @size: Size of the BO +* @use_scratch: Whether to use scratch page flag for VMs +* +* Helper to create two VMs and bind one shared BO in both VMs. +* Returns mapped pointer to the BO. +*/ +static void *purgeable_setup_two_vms_shared_bo (int fd,uint32_t *vm1,uint32_t *vm2, +uint32_t *bo,uint64_t addr1, +uint64_t addr2,size_t size, +bool use_scratch) +{ +struct drm_xe_sync sync = { + .type =DRM_XE_SYNC_TYPE_USER_FENCE , + .flags =DRM_XE_SYNC_FLAG_SIGNAL , + .timeline_value =1 , + }; +uint64_t sync_val =0 ; +void *map; + +/* Create two VMs */ + *vm1 =xe_vm_create (fd, use_scratch ?DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE :0 ,0 ); + *vm2 =xe_vm_create (fd, use_scratch ?DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE :0 ,0 ); + +/* Create shared BO */ + *bo =xe_bo_create (fd,0 , size,vram_if_possible (fd,0 ), +DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM ); + + map =xe_bo_map (fd, *bo, size); +memset (map,0xAB , size); + +/* Bind BO in VM1 */ + sync.addr =to_user_pointer (&sync_val); + sync_val =0 ; +xe_vm_bind_async (fd, *vm1,0 , *bo,0 , addr1, size, &sync,1 ); +xe_wait_ufence (fd, &sync_val,1 ,0 ,NSEC_PER_SEC ); + +/* Bind BO in VM2 */ + sync_val =0 ; +xe_vm_bind_async (fd, *vm2,0 , *bo,0 , addr2, size, &sync,1 ); +xe_wait_ufence (fd, &sync_val,1 ,0 ,NSEC_PER_SEC ); + +return map; +} +/** * SUBTEST: dontneed-before-mmap * Description: Mark BO as DONTNEED before mmap, verify mmap() fails with -EBUSY
@@ -536,6 +589,61 @@ static void test_dontneed_after_exec(int fd, struct drm_xe_engine_class_instancexe_vm_destroy (fd, vm); } +/** +* SUBTEST: per-vma-tracking +* Description: One BO in two VMs becomes purgeable only when both VMAs are DONTNEED +* Test category: functionality test +*/ +static void test_per_vma_tracking (int fd) +{ +uint32_t bo, vm1, vm2; +uint64_t addr1 =PURGEABLE_ADDR ; +uint64_t addr2 =PURGEABLE_ADDR2 ; +size_t bo_size =PURGEABLE_BO_SIZE ; +uint32_t retained; +void *map; + + map =purgeable_setup_two_vms_shared_bo (fd, &vm1, &vm2, &bo, + addr1, addr2, + bo_size, false); + +/* Mark VMA1 as DONTNEED */ + retained =xe_vm_madvise_purgeable (fd, vm1, addr1, bo_size, +DRM_XE_VMA_PURGEABLE_STATE_DONTNEED ); +igt_assert_eq (retained,1 ); + +/* Verify BO NOT purgeable (VMA2 still WILLNEED) */ + retained =xe_vm_madvise_purgeable (fd, vm1, addr1, bo_size, +DRM_XE_VMA_PURGEABLE_STATE_WILLNEED ); +igt_assert_eq (retained,1 ); + +/* Mark both VMAs as DONTNEED */ + retained =xe_vm_madvise_purgeable (fd, vm1, addr1, bo_size, +DRM_XE_VMA_PURGEABLE_STATE_DONTNEED ); +igt_assert_eq (retained,1 ); + + retained =xe_vm_madvise_purgeable (fd, vm2, addr2, bo_size, +DRM_XE_VMA_PURGEABLE_STATE_DONTNEED ); +igt_assert_eq (retained,1 ); + +/* +* Trigger pressure and verify BO was purged. +* Using vm1 is sufficient since both VMAs are DONTNEED - kernel can purge the BO. +*/ +trigger_memory_pressure (fd); + + retained =xe_vm_madvise_purgeable (fd, vm1, addr1, bo_size, +DRM_XE_VMA_PURGEABLE_STATE_WILLNEED ); +munmap (map, bo_size); +gem_close (fd, bo); +xe_vm_destroy (fd, vm1); +xe_vm_destroy (fd, vm2); + +if (retained !=0 ) +igt_skip ("Unable to induce purge on this platform/config" ); + +} +int igt_main () {struct drm_xe_engine_class_instance *hwe;
@@ -578,6 +686,12 @@ int igt_main()break ; } + igt_subtest("per-vma-tracking") +xe_for_each_engine (fd, hwe) { +test_per_vma_tracking (fd); +break ; + } + igt_fixture() {xe_device_put (fd);drm_close_driver (fd);
Signature · 1 line
2.43.0
Re: [PATCH i-g-t v7 8/9] tests/intel/xe_madvise: Add per-vma-tracking subtest
Quoted 163 lines · Arvind Yadav
This test validates that purgeable state is tracked per-VMA when a single BO is bound in multiple VMs. The test creates one BO shared across two VMs at different virtual addresses. It verifies that marking only one VMA as DONTNEED does not make the BO purgeable, but marking both VMAs as DONTNEED allows the kernel to purge the shared BO. This ensures proper per-VMA tracking for shared memory.
v4:
- The comment now clarifies that triggering pressure. (Nishit)v6:
- Move resource cleanup beforeigt_skip()to avoid leaking VM and BO handles on platforms where memory pressure cannot be induced; replaceigt_assert_eq(retained, 0)with a graceful skip. (Nishit)
v7:
- Removed unused 'hwe' parameter.Cc Matthew Brost <[email protected]>Cc Thomas Hellström <[email protected]>Cc Himal Prasad Ghimiray <[email protected]>Cc Nishit Sharma <[email protected]>Cc Pravalika Gurram <[email protected]>Signed-off-by Arvind Yadav <[email protected]>--- tests/intel/xe_madvise.c | 114 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+)tests/intel/xe_madvise.c +108
-0@@ -20,6 +20,7 @@/* Purgeable test constants */ #define PURGEABLE_ADDR 0x1a0000 +#define PURGEABLE_ADDR2 0x2b0000#define PURGEABLE_BATCH_ADDR 0x3c0000#define PURGEABLE_BO_SIZE 4096#define PURGEABLE_FENCE_VAL 0xbeef@@ -257,6 +258,58 @@ static void purgeable_setup_batch_and_data(int fd, uint32_t *vm,xe_wait_ufence (fd, &vm_sync,PURGEABLE_FENCE_VAL ,0 ,NSEC_PER_SEC ); } +/** +* purgeable_setup_two_vms_shared_bo - Setup two VMs with one shared BO +* @fd: DRM file descriptor +* @vm1: Output first VM handle +* @vm2: Output second VM handle +* @bo: Output shared BO handle +* @addr1: Virtual address in VM1 +* @addr2: Virtual address in VM2 +* @size: Size of the BO +* @use_scratch: Whether to use scratch page flag for VMs +* +* Helper to create two VMs and bind one shared BO in both VMs. +* Returns mapped pointer to the BO. +*/ +static void *purgeable_setup_two_vms_shared_bo (int fd,uint32_t *vm1,uint32_t *vm2, +uint32_t *bo,uint64_t addr1, +uint64_t addr2,size_t size, +bool use_scratch) +{ +struct drm_xe_sync sync = { + .type =DRM_XE_SYNC_TYPE_USER_FENCE , + .flags =DRM_XE_SYNC_FLAG_SIGNAL , + .timeline_value =1 , + }; +uint64_t sync_val =0 ; +void *map; + +/* Create two VMs */ + *vm1 =xe_vm_create (fd, use_scratch ?DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE :0 ,0 ); + *vm2 =xe_vm_create (fd, use_scratch ?DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE :0 ,0 ); + +/* Create shared BO */ + *bo =xe_bo_create (fd,0 , size,vram_if_possible (fd,0 ), +DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM ); + + map =xe_bo_map (fd, *bo, size); +memset (map,0xAB , size); + +/* Bind BO in VM1 */ + sync.addr =to_user_pointer (&sync_val); + sync_val =0 ; +xe_vm_bind_async (fd, *vm1,0 , *bo,0 , addr1, size, &sync,1 ); +xe_wait_ufence (fd, &sync_val,1 ,0 ,NSEC_PER_SEC ); + +/* Bind BO in VM2 */ + sync_val =0 ; +xe_vm_bind_async (fd, *vm2,0 , *bo,0 , addr2, size, &sync,1 ); +xe_wait_ufence (fd, &sync_val,1 ,0 ,NSEC_PER_SEC ); + +return map; +} +/** * SUBTEST: dontneed-before-mmap * Description: Mark BO as DONTNEED before mmap, verify mmap() fails with -EBUSY @@ -536,6 +589,61 @@ static void test_dontneed_after_exec(int fd, struct drm_xe_engine_class_instancexe_vm_destroy (fd, vm); } +/** +* SUBTEST: per-vma-tracking +* Description: One BO in two VMs becomes purgeable only when both VMAs are DONTNEED +* Test category: functionality test +*/ +static void test_per_vma_tracking (int fd) +{ +uint32_t bo, vm1, vm2; +uint64_t addr1 =PURGEABLE_ADDR ; +uint64_t addr2 =PURGEABLE_ADDR2 ; +size_t bo_size =PURGEABLE_BO_SIZE ; +uint32_t retained; +void *map; + + map =purgeable_setup_two_vms_shared_bo (fd, &vm1, &vm2, &bo, + addr1, addr2, + bo_size, false); + +/* Mark VMA1 as DONTNEED */ + retained =xe_vm_madvise_purgeable (fd, vm1, addr1, bo_size, +DRM_XE_VMA_PURGEABLE_STATE_DONTNEED ); +igt_assert_eq (retained,1 ); + +/* Verify BO NOT purgeable (VMA2 still WILLNEED) */ + retained =xe_vm_madvise_purgeable (fd, vm1, addr1, bo_size, +DRM_XE_VMA_PURGEABLE_STATE_WILLNEED ); +igt_assert_eq (retained,1 ); + +/* Mark both VMAs as DONTNEED */ + retained =xe_vm_madvise_purgeable (fd, vm1, addr1, bo_size, +DRM_XE_VMA_PURGEABLE_STATE_DONTNEED ); +igt_assert_eq (retained,1 ); + + retained =xe_vm_madvise_purgeable (fd, vm2, addr2, bo_size, +DRM_XE_VMA_PURGEABLE_STATE_DONTNEED ); +igt_assert_eq (retained,1 ); + +/* +* Trigger pressure and verify BO was purged. +* Using vm1 is sufficient since both VMAs are DONTNEED - kernel can purge the BO. +*/ +trigger_memory_pressure (fd); + + retained =xe_vm_madvise_purgeable (fd, vm1, addr1, bo_size, +DRM_XE_VMA_PURGEABLE_STATE_WILLNEED ); +munmap (map, bo_size); +gem_close (fd, bo); +xe_vm_destroy (fd, vm1); +xe_vm_destroy (fd, vm2); + +if (retained !=0 ) +igt_skip ("Unable to induce purge on this platform/config" ); + +} +int igt_main () {struct drm_xe_engine_class_instance *hwe;@@ -578,6 +686,12 @@ int igt_main()break ; }
tests/intel/xe_madvise.c int igt_main() · line 697
Quoted 10 lines
tests/intel/xe_madvise.c +6
-0+ igt_subtest("per-vma-tracking") +xe_for_each_engine (fd, hwe) { +test_per_vma_tracking (fd); +break ; + } + igt_fixture() {xe_device_put (fd);drm_close_driver (fd);
LGTM:
PATCH v7 9/9 tests/intel/xe_madvise: Add per-vma-protection subtest
In reply to Arvind YadavThis test validates that a WILLNEED VMA protects a shared BO from being purged even when other VMAs are marked DONTNEED. The test creates a BO shared across two VMs, marks VMA1 as DONTNEED while keeping VMA2 as WILLNEED, then triggers memory pressure. The BO should survive and GPU execution should succeed. After marking both VMAs as DONTNEED and triggering pressure again, the BO should be purged, demonstrating that all VMAs must be DONTNEED for the BO to be purgeable.
v4:
- Added syncobj_wait() after the second exec. (Nishit)
v6:
- Move resource cleanup before igt_skip() to avoid leaking VM and BO handles on platforms where memory pressure cannot be induced; replace igt_assert_eq(retained, 0) with a graceful skip. (Nishit)
--- tests/intel/xe_madvise.c | 127 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+)
tests/intel/xe_madvise.c +127 -0
@@ -644,6 +644,127 @@ static void test_per_vma_tracking(int fd) } +/** +* SUBTEST: per-vma-protection +* Description: WILLNEED VMA protects BO from purging; both DONTNEED makes BO purgeable +* Test category: functionality test +*/ +static void test_per_vma_protection (int fd,struct drm_xe_engine_class_instance *hwe) +{ +uint32_t vm1, vm2, exec_queue, bo, batch_bo, bind_engine; +uint64_t data_addr1 =PURGEABLE_ADDR ; +uint64_t data_addr2 =PURGEABLE_ADDR2 ; +uint64_t batch_addr =PURGEABLE_BATCH_ADDR ; +size_t data_size =PURGEABLE_BO_SIZE ; +size_t batch_size =PURGEABLE_BO_SIZE ; +struct drm_xe_sync sync[2 ] = { + { .type =DRM_XE_SYNC_TYPE_USER_FENCE , + .flags =DRM_XE_SYNC_FLAG_SIGNAL , + .timeline_value =PURGEABLE_FENCE_VAL }, + { .type =DRM_XE_SYNC_TYPE_SYNCOBJ , + .flags =DRM_XE_SYNC_FLAG_SIGNAL }, + }; +struct drm_xe_exec exec = { + .num_batch_buffer =1 , + .num_syncs =1 , + .syncs =to_user_pointer (&sync[1 ]), + }; +uint32_t *data, *batch; +uint64_t vm_sync =0 ; +uint32_t retained, syncobj; +int b, ret; + +/* Create two VMs and bind shared data BO */ + data =purgeable_setup_two_vms_shared_bo (fd, &vm1, &vm2, &bo, + data_addr1, data_addr2, + data_size, true); +memset (data,0 , data_size); + bind_engine =xe_bind_exec_queue_create (fd, vm2,0 ); + +/* Create and bind batch BO in VM2 */ + batch_bo =xe_bo_create (fd, vm2, batch_size,vram_if_possible (fd,0 ), +DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM ); + batch =xe_bo_map (fd, batch_bo, batch_size); +igt_assert (batch !=MAP_FAILED ); + + sync[0 ].addr =to_user_pointer (&vm_sync); + vm_sync =0 ; +xe_vm_bind_async (fd, vm2, bind_engine, batch_bo,0 , batch_addr, batch_size, sync,1 ); +xe_wait_ufence (fd, &vm_sync,PURGEABLE_FENCE_VAL ,0 ,NSEC_PER_SEC ); + +/* Mark VMA1 as DONTNEED, VMA2 stays WILLNEED */ + retained =xe_vm_madvise_purgeable (fd, vm1, data_addr1, data_size, +DRM_XE_VMA_PURGEABLE_STATE_DONTNEED ); +igt_assert_eq (retained,1 ); + +/* Trigger pressure - BO should survive */ +trigger_memory_pressure (fd); + + retained =xe_vm_madvise_purgeable (fd, vm2, data_addr2, data_size, +DRM_XE_VMA_PURGEABLE_STATE_WILLNEED ); +igt_assert_eq (retained,1 ); + +/* GPU workload - should succeed */ + b =0 ; + batch[b++] =MI_STORE_DWORD_IMM_GEN4 ; + batch[b++] = data_addr2; + batch[b++] = data_addr2 >>32 ; + batch[b++] =PURGEABLE_TEST_PATTERN ; + batch[b++] =MI_BATCH_BUFFER_END ; + + syncobj =syncobj_create (fd,0 ); + sync[1 ].handle = syncobj; + exec_queue =xe_exec_queue_create (fd, vm2, hwe,0 ); + exec.exec_queue_id = exec_queue; + exec.address = batch_addr; + + ret =__xe_exec (fd, &exec); +igt_assert_eq (ret,0 ); +igt_assert (syncobj_wait (fd, &syncobj,1 ,INT64_MAX ,0 ,NULL )); + +munmap (data, data_size); + data =xe_bo_map (fd, bo, data_size); +igt_assert (data !=MAP_FAILED ); +igt_assert_eq (data[0 ],PURGEABLE_TEST_PATTERN ); + +/* Mark both VMAs DONTNEED */ + retained =xe_vm_madvise_purgeable (fd, vm2, data_addr2, data_size, +DRM_XE_VMA_PURGEABLE_STATE_DONTNEED ); +igt_assert_eq (retained,1 ); + +/* Trigger pressure - BO should be purged */ +trigger_memory_pressure (fd); + + retained =xe_vm_madvise_purgeable (fd, vm2, data_addr2, data_size, +DRM_XE_VMA_PURGEABLE_STATE_WILLNEED ); + +if (retained !=0 ) + goto out; + +/* GPU workload - should fail or succeed with NULL rebind */ + batch[3 ] =PURGEABLE_DEAD_PATTERN ; + + ret =__xe_exec (fd, &exec); +if (ret ==0 ) { +/* Exec succeeded, wait for completion before cleanup */ +syncobj_wait (fd, &syncobj,1 ,INT64_MAX ,0 ,NULL ); + } + +out: +munmap (data, data_size); +munmap (batch, batch_size); +gem_close (fd, bo); +gem_close (fd, batch_bo); +syncobj_destroy (fd, syncobj); +xe_exec_queue_destroy (fd, bind_engine); +xe_exec_queue_destroy (fd, exec_queue); +xe_vm_destroy (fd, vm1); +xe_vm_destroy (fd, vm2); + +if (retained !=0 ) +igt_skip ("Unable to induce purge on this platform/config" ); +} +int igt_main () {struct drm_xe_engine_class_instance *hwe;
@@ -692,6 +813,12 @@ int igt_main()break ; } + igt_subtest("per-vma-protection") +xe_for_each_engine (fd, hwe) { +test_per_vma_protection (fd, hwe); +break ; + } + igt_fixture() {xe_device_put (fd);drm_close_driver (fd);
Signature · 1 line
2.43.0
Patchwork
CI✗ Fi.CI.BUILD: failure for tests/xe: Add purgeable memory madvise tests for system allocator (rev7)
In reply to Arvind Yadav== Series Details ==
== Series Details ==
Series: tests/xe: Add purgeable memory madvise tests for system allocator (rev7)
URL : https://patchwork.freedesktop.org/series/160333/
State : failure
== Summary ==
Applying: drm-uapi/xe_drm: Sync with Add UAPI support for purgeable buffer objects Using index info to reconstruct a base tree...
M include/drm-uapi/xe_drm.h
Falling back to patching base and 3-way merge...
Auto-merging include/drm-uapi/xe_drm.h
CONFLICT (content): Merge conflict in include/drm-uapi/xe_drm.h
Patch failed at 0001 drm-uapi/xe_drm: Sync with Add UAPI support for purgeable buffer objects When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".