Skip to contentSkip to conversation

Madvise Tests in IGT

PATCH Superseded [email protected] 2 participants 10 messages Sep 3, 2025

v9 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v9 cover Madvise Tests in IGT

From: Nishit Sharma <[email protected]>

Revision 1:
Added madvise tests in IGT which validate different features related to attributes passed. Madvise tests related to Atomic operation, Preferred Loc have been added and validated. Madvise tests are called as part of different struct section and available as madvise-<test-name> in list of subtests.

ver2:
- added back subtest which was deleted due to rebasing

ver3:
- added variable deleted during rebase.

ver4:
- Removed redundant loop for multi-vma test. Instead added multi-vma check in which is manipulating address, batch addreess only and the remaining execution is done as per default flow.
- Passed region DRM_XE_CONSULT_MEM_ADVISE_PREF_LOC in prefetch tests.

ver5:
- Incorporated review comments
- Removed section from test which was not required
- Added subtests description
- Tests executed on latest drm tip

ver6:
- Incorporated review comments
- Removed dead code checked-in due to rebasing
- Added new subtests in section
- Modified madvise subtests by adding flags
- Added description of new sub-tests
- Called helper functions from test_exec which performs

ver7:
- Fixed code which was calling madvise op only for device mem
- Fixed warnings

ver8:
- Done Code cleanup

ver9:
- Moved madvise-swizzle section in main loop. Each pass can toggle between device/system memory
- Removed redundant code for writing data at location
- called helper in test_exec() for handling madvise-swizzle

Nishit Sharma (5):
DO-NOT-MERGE: include/drm-uapi: Add drm_xe_madvise structure lib/xe: Add xe_vm_madvise ioctl support
lib/xe: Add Helper to get memory attributes tests/intel/xe_exec_system_allocator: Add madvise-swizzle test tests/intel/xe_exec_system_allocator: Add atomic_batch test in IGT

 include/drm-uapi/xe_drm.h              | 289 ++++++++++++++++-
 lib/xe/xe_ioctl.c                      | 149 +++++++++
 lib/xe/xe_ioctl.h                      |   9 +-
 tests/intel/xe_exec_system_allocator.c | 433 +++++++++++++++++++++++--
 4 files changed, 835 insertions(+), 45 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v9 1/5 DO-NOT-MERGE: include/drm-uapi: Add drm_xe_madvise structure

From: Nishit Sharma <[email protected]>

Defined IOCTL number for madvise operation. Added drm_xe_madvise which is passed as Input to MADVISE IOCTL.

---
 include/drm-uapi/xe_drm.h | 289 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 281 insertions(+), 8 deletions(-)
include/drm-uapi/xe_drm.h +281 -8
index a52f95593..e9a27a844 100644
@@ -3,8 +3,8 @@
  * Copyright © 2023 Intel Corporation
  */
 
-#ifndef _XE_DRM_H_
-#define _XE_DRM_H_
+#ifndef _UAPI_XE_DRM_H_
+#define _UAPI_XE_DRM_H_
 
 #include "drm.h"
 
@@ -81,6 +81,8 @@ extern "C" {
  *  - &DRM_IOCTL_XE_EXEC
  *  - &DRM_IOCTL_XE_WAIT_USER_FENCE
  *  - &DRM_IOCTL_XE_OBSERVATION
+ *  - &DRM_IOCTL_XE_MADVISE
+ *  - &DRM_IOCTL_XE_VM_QUERY_MEM_RANGE_ATTRS
  */
 
 /*
@@ -102,6 +104,8 @@ extern "C" {
 #define DRM_XE_EXEC			0x09
 #define DRM_XE_WAIT_USER_FENCE		0x0a
 #define DRM_XE_OBSERVATION		0x0b
+#define DRM_XE_MADVISE			0x0c
+#define DRM_XE_VM_QUERY_MEM_REGION_ATTRS	0x0d
 
 /* Must be kept compact -- no holes */
 
@@ -117,6 +121,8 @@ extern "C" {
 #define DRM_IOCTL_XE_EXEC			DRM_IOW(DRM_COMMAND_BASE + DRM_XE_EXEC, struct drm_xe_exec)
 #define DRM_IOCTL_XE_WAIT_USER_FENCE		DRM_IOWR(DRM_COMMAND_BASE + DRM_XE_WAIT_USER_FENCE, struct drm_xe_wait_user_fence)
 #define DRM_IOCTL_XE_OBSERVATION		DRM_IOW(DRM_COMMAND_BASE + DRM_XE_OBSERVATION, struct drm_xe_observation_param)
+#define DRM_IOCTL_XE_MADVISE			DRM_IOW(DRM_COMMAND_BASE + DRM_XE_MADVISE, struct drm_xe_madvise)
+#define DRM_IOCTL_XE_VM_QUERY_MEM_RANGE_ATTRS	DRM_IOWR(DRM_COMMAND_BASE + DRM_XE_VM_QUERY_MEM_REGION_ATTRS, struct drm_xe_vm_query_mem_range_attr)
 
 /**
  * DOC: Xe IOCTL Extensions
@@ -134,7 +140,7 @@ extern "C" {
  * redefine the interface more easily than an ever growing struct of
  * increasing complexity, and for large parts of that interface to be
  * entirely optional. The downside is more pointer chasing; chasing across
- * the boundary with pointers encapsulated inside u64.
+ * the __user boundary with pointers encapsulated inside u64.
  *
  * Example chaining:
  *
@@ -925,9 +931,9 @@ struct drm_xe_gem_mmap_offset {
  *  - %DRM_XE_VM_CREATE_FLAG_LR_MODE - An LR, or Long Running VM accepts
  *    exec submissions to its exec_queues that don't have an upper time
  *    limit on the job execution time. But exec submissions to these
- *    don't allow any of the flags DRM_XE_SYNC_FLAG_SYNCOBJ,
- *    DRM_XE_SYNC_FLAG_TIMELINE_SYNCOBJ, DRM_XE_SYNC_FLAG_DMA_BUF,
- *    used as out-syncobjs, that is, together with DRM_XE_SYNC_FLAG_SIGNAL.
+ *    don't allow any of the sync types DRM_XE_SYNC_TYPE_SYNCOBJ,
+ *    DRM_XE_SYNC_TYPE_TIMELINE_SYNCOBJ, used as out-syncobjs, that is,
+ *    together with sync flag DRM_XE_SYNC_FLAG_SIGNAL.
  *    LR VMs can be created in recoverable page-fault mode using
  *    DRM_XE_VM_CREATE_FLAG_FAULT_MODE, if the device supports it.
  *    If that flag is omitted, the UMD can not rely on the slightly
@@ -1003,6 +1009,10 @@ struct drm_xe_vm_destroy {
  *    valid on VMs with DRM_XE_VM_CREATE_FLAG_FAULT_MODE set. The CPU address
  *    mirror flag are only valid for DRM_XE_VM_BIND_OP_MAP operations, the BO
  *    handle MBZ, and the BO offset MBZ.
+ *
+ * The @prefetch_mem_region_instance for %DRM_XE_VM_BIND_OP_PREFETCH can also be:
+ *  - %DRM_XE_CONSULT_MEM_ADVISE_PREF_LOC, which ensures prefetching occurs in
+ *    the memory region advised by madvise.
  */
 struct drm_xe_vm_bind_op {
 	/** @extensions: Pointer to the first extension struct, if any */
@@ -1108,6 +1118,7 @@ struct drm_xe_vm_bind_op {
 	/** @flags: Bind flags */
 	__u32 flags;
 
+#define DRM_XE_CONSULT_MEM_ADVISE_PREF_LOC	-1
 	/**
 	 * @prefetch_mem_region_instance: Memory region to prefetch VMA to.
 	 * It is a region instance, not a mask.
@@ -1394,7 +1405,7 @@ struct drm_xe_sync {
 
 	/**
 	 * @timeline_value: Input for the timeline sync object. Needs to be
-	 * different than 0 when used with %DRM_XE_SYNC_FLAG_TIMELINE_SYNCOBJ.
+	 * different than 0 when used with %DRM_XE_SYNC_TYPE_TIMELINE_SYNCOBJ.
 	 */
 	__u64 timeline_value;
 
@@ -1974,8 +1985,270 @@ struct drm_xe_query_eu_stall {
 	__u64 sampling_rates[];
 };
 
+/**
+ * struct drm_xe_madvise - Input of &DRM_IOCTL_XE_MADVISE
+ *
+ * This structure is used to set memory attributes for a virtual address range
+ * in a VM. The type of attribute is specified by @type, and the corresponding
+ * union member is used to provide additional parameters for @type.
+ *
+ * Supported attribute types:
+ * - 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.
+ *
+ * Example:
+ *
+ * .. code-block:: C
+ *
+ * struct drm_xe_madvise madvise = {
+ *          .vm_id = vm_id,
+ *          .start = 0x100000,
+ *          .range = 0x2000,
+ *          .type = DRM_XE_MEM_RANGE_ATTR_ATOMIC,
+ *          .atomic_val = DRM_XE_ATOMIC_DEVICE,
+ *          .pad = 0,
+ *         };
+ *
+ * ioctl(fd, DRM_IOCTL_XE_MADVISE, &madvise);
+ *
+ */
+struct drm_xe_madvise {
+	/** @extensions: Pointer to the first extension struct, if any */
+	__u64 extensions;
+
+	/** @start: start of the virtual address range */
+	__u64 start;
+
+	/** @range: size of the virtual address range */
+	__u64 range;
+
+	/** @vm_id: vm_id of the virtual range */
+	__u32 vm_id;
+
+#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
+	/** @type: type of attribute */
+	__u32 type;
+
+	union {
+		/**
+		 * @preferred_mem_loc: preferred memory location
+		 *
+		 * Used when @type == DRM_XE_MEM_RANGE_ATTR_PREFERRED_LOC
+		 *
+		 * Supported values for @preferred_mem_loc.devmem_fd:
+		 * - DRM_XE_PREFERRED_LOC_DEFAULT_DEVICE: set vram of faulting tile as preferred loc
+		 * - DRM_XE_PREFERRED_LOC_DEFAULT_SYSTEM: set smem as preferred loc
+		 *
+		 * Supported values for @preferred_mem_loc.migration_policy:
+		 * - DRM_XE_MIGRATE_ALL_PAGES
+		 * - DRM_XE_MIGRATE_ONLY_SYSTEM_PAGES
+		 */
+		struct {
+#define DRM_XE_PREFERRED_LOC_DEFAULT_DEVICE	0
+#define DRM_XE_PREFERRED_LOC_DEFAULT_SYSTEM	-1
+			/** @preferred_mem_loc.devmem_fd: fd for preferred loc */
+			__u32 devmem_fd;
+
+#define DRM_XE_MIGRATE_ALL_PAGES		0
+#define DRM_XE_MIGRATE_ONLY_SYSTEM_PAGES	1
+			/** @preferred_mem_loc.migration_policy: Page migration policy */
+			__u16 migration_policy;
+
+			/** @preferred_mem_loc.pad : MBZ */
+			__u16 pad;
+
+			/** @preferred_mem_loc.reserved : Reserved */
+			__u64 reserved;
+		} preferred_mem_loc;
+
+		/**
+		 * @atomic: Atomic access policy
+		 *
+		 * Used when @type == DRM_XE_MEM_RANGE_ATTR_ATOMIC.
+		 *
+		 * Supported values for @atomic.val:
+		 * - DRM_XE_ATOMIC_UNDEFINED: Undefined or default behaviour
+		 *   Support both GPU and CPU atomic operations for system allocator
+		 *   Support GPU atomic operations for normal(bo) allocator
+		 * - DRM_XE_ATOMIC_DEVICE: Support GPU atomic operations
+		 * - DRM_XE_ATOMIC_GLOBAL: Support both GPU and CPU atomic operations
+		 * - DRM_XE_ATOMIC_CPU: Support CPU atomic
+		 */
+		struct {
+#define DRM_XE_ATOMIC_UNDEFINED	0
+#define DRM_XE_ATOMIC_DEVICE	1
+#define DRM_XE_ATOMIC_GLOBAL	2
+#define DRM_XE_ATOMIC_CPU	3
+			/** @atomic.val: value of atomic operation */
+			__u32 val;
+
+			/** @atomic.pad: MBZ */
+			__u32 pad;
+
+			/** @atomic.reserved: Reserved */
+			__u64 reserved;
+		} atomic;
+
+		/**
+		 * @pat_index: Page attribute table index
+		 *
+		 * Used when @type == DRM_XE_MEM_RANGE_ATTR_PAT.
+		 */
+		struct {
+			/** @pat_index.val: PAT index value */
+			__u32 val;
+
+			/** @pat_index.pad: MBZ */
+			__u32 pad;
+
+			/** @pat_index.reserved: Reserved */
+			__u64 reserved;
+		} pat_index;
+	};
+
+	/** @reserved: Reserved */
+	__u64 reserved[2];
+};
+
+/**
+ * struct drm_xe_mem_range_attr - Output of &DRM_IOCTL_XE_VM_QUERY_MEM_RANGES_ATTRS
+ *
+ * This structure is provided by userspace and filled by KMD in response to the
+ * DRM_IOCTL_XE_VM_QUERY_MEM_RANGES_ATTRS ioctl. It describes memory attributes of
+ * a memory ranges within a user specified address range in a VM.
+ *
+ * The structure includes information such as atomic access policy,
+ * page attribute table (PAT) index, and preferred memory location.
+ * Userspace allocates an array of these structures and passes a pointer to the
+ * ioctl to retrieve attributes for each memory ranges
+ *
+ * @extensions: Pointer to the first extension struct, if any
+ * @start: Start address of the memory range
+ * @end: End address of the virtual memory range
+ *
+ */
+struct drm_xe_mem_range_attr {
+	 /** @extensions: Pointer to the first extension struct, if any */
+	__u64 extensions;
+
+	/** @start: start of the memory range */
+	__u64 start;
+
+	/** @end: end of the memory range */
+	__u64 end;
+
+	/** @preferred_mem_loc: preferred memory location */
+	struct {
+		/** @preferred_mem_loc.devmem_fd: fd for preferred loc */
+		__u32 devmem_fd;
+
+		/** @preferred_mem_loc.migration_policy: Page migration policy */
+		__u32 migration_policy;
+	} preferred_mem_loc;
+
+	struct {
+		/** @atomic.val: atomic attribute */
+		__u32 val;
+
+		/** @atomic.reserved: Reserved */
+		__u32 reserved;
+	} atomic;
+
+	struct {
+		/** @pat_index.val: PAT index */
+		__u32 val;
+
+		/** @pat_index.reserved: Reserved */
+		__u32 reserved;
+	} pat_index;
+
+	/** @reserved: Reserved */
+	__u64 reserved[2];
+};
+
+/**
+ * struct drm_xe_vm_query_mem_range_attr - Input of &DRM_IOCTL_XE_VM_QUERY_MEM_ATTRIBUTES
+ *
+ * This structure is used to query memory attributes of memory regions
+ * within a user specified address range in a VM. It provides detailed
+ * information about each memory range, including atomic access policy,
+ * page attribute table (PAT) index, and preferred memory location.
+ *
+ * Userspace first calls the ioctl with @num_mem_ranges = 0,
+ * @sizeof_mem_ranges_attr = 0 and @vector_of_vma_mem_attr = NULL to retrieve
+ * the number of memory regions and size of each memory range attribute.
+ * Then, it allocates a buffer of that size and calls the ioctl again to fill
+ * the buffer with memory range attributes.
+ *
+ * If second call fails with -ENOSPC, it means memory ranges changed between
+ * first call and now, retry IOCTL again with @num_mem_ranges = 0,
+ * @sizeof_mem_ranges_attr = 0 and @vector_of_vma_mem_attr = NULL followed by
+ * Second ioctl call.
+ *
+ * Example:
+ *
+ * .. code-block:: C
+ *    struct drm_xe_vm_query_mem_range_attr query = {
+ *         .vm_id = vm_id,
+ *         .start = 0x100000,
+ *         .range = 0x2000,
+ *     };
+ *
+ *    // First ioctl call to get num of mem regions and sizeof each attribute
+ *    ioctl(fd, DRM_IOCTL_XE_VM_QUERY_MEM_RANGE_ATTRS, &query);
+ *
+ *    // Allocate buffer for the memory region attributes
+ *    void *ptr = malloc(query.num_mem_ranges * query.sizeof_mem_range_attr);
+ *
+ *    query.vector_of_mem_attr = (uintptr_t)ptr;
+ *
+ *    // Second ioctl call to actually fill the memory attributes
+ *    ioctl(fd, DRM_IOCTL_XE_VM_QUERY_MEM_RANGE_ATTRS, &query);
+ *
+ *    // Iterate over the returned memory region attributes
+ *    for (unsigned int i = 0; i < query.num_mem_ranges; ++i) {
+ *       struct drm_xe_mem_range_attr *attr = (struct drm_xe_mem_range_attr *)ptr;
+ *
+ *       // Do something with attr
+ *
+ *       // Move pointer by one entry
+ *       ptr += query.sizeof_mem_range_attr;
+ *     }
+ *
+ *    free(ptr);
+ */
+struct drm_xe_vm_query_mem_range_attr {
+	/** @extensions: Pointer to the first extension struct, if any */
+	__u64 extensions;
+
+	/** @vm_id: vm_id of the virtual range */
+	__u32 vm_id;
+
+	/** @num_mem_ranges: number of mem_ranges in range */
+	__u32 num_mem_ranges;
+
+	/** @start: start of the virtual address range */
+	__u64 start;
+
+	/** @range: size of the virtual address range */
+	__u64 range;
+
+	/** @sizeof_mem_range_attr: size of struct drm_xe_mem_range_attr */
+	__u64 sizeof_mem_range_attr;
+
+	/** @vector_of_ops: userptr to array of struct drm_xe_mem_range_attr */
+	__u64 vector_of_mem_attr;
+
+	/** @reserved: Reserved */
+	__u64 reserved[2];
+
+};
+
 #if defined(__cplusplus)
 }
 #endif
 
-#endif /* _XE_DRM_H_ */
+#endif /* _UAPI_XE_DRM_H_ */
Signature · 1 line
2.43.0
#2 lore raw

PATCH v9 2/5 lib/xe: Add xe_vm_madvise ioctl support

In reply to [email protected]

From: Nishit Sharma <[email protected]>

xe_vm_madvise() defined which issues madvise ioctl DRM_IOCTL_XE_MADVISE for VM region advising the driver about expected usage or memory policy for specified address range. MADVISE ioctl requires pointer to drm_xe_madvise structure as one of the inputs. Depending upon type of madvise operation like Atomic, Preferred LOC or PAT required members of drm_xe_madvise structure are initialized and passed in MADVISE ioctl.

---
 lib/xe/xe_ioctl.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/xe/xe_ioctl.h |  5 ++++-
 2 files changed, 61 insertions(+), 1 deletion(-)
lib/xe/xe_ioctl.c +57 -0
index 1e95af409..5608c8780 100644
@@ -585,3 +585,60 @@ int64_t xe_wait_ufence(int fd, uint64_t *addr, uint64_t value,
 	igt_assert_eq(__xe_wait_ufence(fd, addr, value, exec_queue, &timeout), 0);
 	return timeout;
 }
+
+int __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)
+{
+	struct drm_xe_madvise madvise = {
+		.type = type,
+		.extensions = ext,
+		.vm_id = vm,
+		.start = addr,
+		.range = range,
+	};
+
+	switch (type) {
+	case DRM_XE_MEM_RANGE_ATTR_ATOMIC:
+		madvise.atomic.val = op_val;
+		break;
+	case DRM_XE_MEM_RANGE_ATTR_PREFERRED_LOC:
+		madvise.preferred_mem_loc.devmem_fd = op_val;
+		madvise.preferred_mem_loc.migration_policy = policy;
+		igt_debug("madvise.preferred_mem_loc.devmem_fd = %d\n",
+			  madvise.preferred_mem_loc.devmem_fd);
+		break;
+	case DRM_XE_MEM_RANGE_ATTR_PAT:
+		madvise.pat_index.val = op_val;
+		break;
+	default:
+		igt_warn("Unknown attribute\n");
+		return -EINVAL;
+	}
+
+	if (igt_ioctl(fd, DRM_IOCTL_XE_MADVISE, &madvise))
+		return -errno;
+
+	return 0;
+}
+
+/**
+ * xe_vm_madvise:
+ * @fd: xe device fd
+ * @vm: vm_id of the virtual range
+ * @addr: start of the virtual address range
+ * @range: size of the virtual address range
+ * @ext: Pointer to the first extension struct, if any
+ * @type: type of attribute
+ * @op_val: fd/atomic value/pat index, depending upon type of operation
+ * @policy: Page migration policy
+ *
+ * Function initializes different members of struct drm_xe_madvise and calls
+ * MADVISE IOCTL .
+ *
+ * Asserts in case of error returned by DRM_IOCTL_XE_MADVISE.
+ */
+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)
+{
+	igt_assert_eq(__xe_vm_madvise(fd, vm, addr, range, ext, type, op_val, policy), 0);
+}
lib/xe/xe_ioctl.h +4 -1
index 6302d1a7d..8e13ffe65 100644
@@ -99,5 +99,8 @@ int __xe_wait_ufence(int fd, uint64_t *addr, uint64_t value,
 		     uint32_t exec_queue, int64_t *timeout);
 int64_t xe_wait_ufence(int fd, uint64_t *addr, uint64_t value,
 		       uint32_t exec_queue, int64_t timeout);
-
+int __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);
+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);
 #endif /* XE_IOCTL_H */
Signature · 1 line
2.43.0
Reviewed-by on v9 2/5
#3 lore raw

RE: [PATCH i-g-t v9 2/5] lib/xe: Add xe_vm_madvise ioctl support

Quoted 107 lines

-----Original Message-----
From: Sharma, Nishit <[email protected]>
Sent: Wednesday, September 3, 2025 4:07 PM
To: [email protected]; Gurram, Pravalika <[email protected]>; Ghimiray, Himal Prasad <[email protected]>; Brost, Matthew <[email protected]>; Sharma, Nishit <[email protected]>
Subject: [PATCH i-g-t v9 2/5] lib/xe: Add xe_vm_madvise ioctl support

From: Nishit Sharma <[email protected]>

xe_vm_madvise() defined which issues madvise ioctl DRM_IOCTL_XE_MADVISE for VM region advising the driver about expected usage or memory policy for specified address range. MADVISE ioctl requires pointer to drm_xe_madvise structure as one of the inputs. Depending upon type of madvise operation like Atomic, Preferred LOC or PAT required members of drm_xe_madvise structure are initialized and passed in MADVISE ioctl.

---
 lib/xe/xe_ioctl.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/xe/xe_ioctl.h |  5 ++++-
 2 files changed, 61 insertions(+), 1 deletion(-)
lib/xe/xe_ioctl.c index 1e95af409..5608c8780 renamed from lib/xe/xe_ioctl.c +0 -0

100644

lib/xe/xe_ioctl.c +56 -0
@@ -585,3 +585,60 @@ int64_t xe_wait_ufence(int fd, uint64_t *addr, uint64_t
 value,
 	igt_assert_eq(__xe_wait_ufence(fd, addr, value, exec_queue,
 &timeout), 0);
 	return timeout;
 }
+
+int __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) {
+	struct drm_xe_madvise madvise = {
+		.type = type,
+		.extensions = ext,
+		.vm_id = vm,
+		.start = addr,
+		.range = range,
+	};
+
+	switch (type) {
+	case DRM_XE_MEM_RANGE_ATTR_ATOMIC:
+		madvise.atomic.val = op_val;
+		break;
+	case DRM_XE_MEM_RANGE_ATTR_PREFERRED_LOC:
+		madvise.preferred_mem_loc.devmem_fd = op_val;
+		madvise.preferred_mem_loc.migration_policy = policy;
+		igt_debug("madvise.preferred_mem_loc.devmem_fd = %d\n",
+			  madvise.preferred_mem_loc.devmem_fd);
+		break;
+	case DRM_XE_MEM_RANGE_ATTR_PAT:
+		madvise.pat_index.val = op_val;
+		break;
+	default:
+		igt_warn("Unknown attribute\n");
+		return -EINVAL;
+	}
+
+	if (igt_ioctl(fd, DRM_IOCTL_XE_MADVISE, &madvise))
+		return -errno;
+
+	return 0;
+}
+
+/**
+ * xe_vm_madvise:
+ * @fd: xe device fd
+ * @vm: vm_id of the virtual range
+ * @addr: start of the virtual address range
+ * @range: size of the virtual address range
+ * @ext: Pointer to the first extension struct, if any
+ * @type: type of attribute
+ * @op_val: fd/atomic value/pat index, depending upon type of operation
+ * @policy: Page migration policy
+ *
+ * Function initializes different members of struct drm_xe_madvise and
+calls
+ * MADVISE IOCTL .
+ *
+ * Asserts in case of error returned by DRM_IOCTL_XE_MADVISE.
+ */
+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) {
+	igt_assert_eq(__xe_vm_madvise(fd, vm, addr, range, ext, type, op_val,
+policy), 0); }
lib/xe/xe_ioctl.h index 6302d1a7d..8e13ffe65 renamed from lib/xe/xe_ioctl.h +0 -0

100644

lib/xe/xe_ioctl.h +1 -1
@@ -99,5 +99,8 @@ int __xe_wait_ufence(int fd, uint64_t *addr, uint64_t
 value,
 		     uint32_t exec_queue, int64_t *timeout);  int64_t
 xe_wait_ufence(int fd, uint64_t *addr, uint64_t value,
 		       uint32_t exec_queue, int64_t timeout);
-
+int __xe_vm_madvise(int fd, uint32_t vm, uint64_t addr, uint64_t range,

uint64_t ext,

lib/xe/xe_ioctl.h +2 -0
+		    uint32_t type, uint32_t op_val, uint16_t policy); void
+xe_vm_madvise(int fd, uint32_t vm, uint64_t addr, uint64_t range, uint64_t

ext,

lib/xe/xe_ioctl.h

Quoted 1 line
lib/xe/xe_ioctl.h +1 -0
+		  uint32_t type, uint32_t op_val, uint16_t policy);
Quoted 3 lines

#endif /* XE_IOCTL_H */
--
2.43.0

#4 lore raw

PATCH v9 3/5 lib/xe: Add Helper to get memory attributes

In reply to [email protected]

From: Nishit Sharma <[email protected]>

xe_vm_print_mem_attr_values_in_range() function added which calls QUERY_MEM_RANGES_ATTRS ioctl to get different memory attributes from KMD and then prints memory attributes returned by KMD for different access policies like atomic access, preferred loc and pat index.

---
 lib/xe/xe_ioctl.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/xe/xe_ioctl.h |  4 +++
 2 files changed, 96 insertions(+)
lib/xe/xe_ioctl.c +92 -0
index 5608c8780..28b7d5bec 100644
@@ -57,6 +57,98 @@ uint64_t xe_bb_size(int fd, uint64_t reqsize)
 	             xe_get_default_alignment(fd));
 }
 
+int xe_vm_number_vmas_in_range(int fd, struct drm_xe_vm_query_mem_range_attr *vmas_attr)
+{
+	if (igt_ioctl(fd, DRM_IOCTL_XE_VM_QUERY_MEM_RANGE_ATTRS, vmas_attr))
+		return -errno;
+	return 0;
+}
+
+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)
+{
+	if (!mem_attr)
+		return -EINVAL;
+
+	vmas_attr->vector_of_mem_attr = (uintptr_t)mem_attr;
+
+	if (igt_ioctl(fd, DRM_IOCTL_XE_VM_QUERY_MEM_RANGE_ATTRS, vmas_attr))
+		return -errno;
+
+	return 0;
+}
+
+/**
+ * xe_vm_print_mem_attr_values_in_range:
+ * @fd: xe device fd
+ * @vm: vm_id of the virtual range
+ * @start: start of the virtual address range
+ * @range: size of the virtual address range
+ *
+ * Calls QUERY_MEM_RANGES_ATTRS ioctl to get memory attributes for different
+ * memory ranges from KMD. prints memory attributes as returned by KMD for
+ * atomic, prefrred loc and pat index types.
+ *
+ * Returns 0 for success or error for failure
+ */
+
+int xe_vm_print_mem_attr_values_in_range(int fd, uint32_t vm, uint64_t start, uint64_t range)
+{
+
+	void *ptr_start, *ptr;
+	int err;
+	struct drm_xe_vm_query_mem_range_attr query = {
+		.vm_id = vm,
+		.start = start,
+		.range = range,
+		.num_mem_ranges = 0,
+		.sizeof_mem_range_attr = 0,
+		.vector_of_mem_attr = (uintptr_t)NULL,
+	};
+
+	igt_debug("mem_attr_values_in_range called start = %"PRIu64"\n range = %"PRIu64"\n",
+		  start, range);
+
+	err  = xe_vm_number_vmas_in_range(fd, &query);
+	if (err || !query.num_mem_ranges || !query.sizeof_mem_range_attr) {
+		igt_warn("ioctl failed for xe_vm_number_vmas_in_range\n");
+		igt_debug("vmas_in_range err = %d query.num_mem_ranges = %u query.sizeof_mem_range_attr=%lld\n",
+			 err, query.num_mem_ranges, query.sizeof_mem_range_attr);
+		return err;
+	}
+
+	/* Allocate buffer for the memory region attributes */
+	ptr = malloc(query.num_mem_ranges * query.sizeof_mem_range_attr);
+	ptr_start = ptr;
+
+	if (!ptr)
+		return -ENOMEM;
+
+	err = xe_vm_vma_attrs(fd, &query, ptr);
+	if (err) {
+		igt_warn("ioctl failed for vma_attrs err = %d\n", err);
+		return err;
+	}
+
+	/* Iterate over the returned memory region attributes */
+	for (unsigned int i = 0; i < query.num_mem_ranges; ++i) {
+		struct drm_xe_mem_range_attr *mem_attrs = (struct drm_xe_mem_range_attr *)ptr;
+
+		igt_debug("vma_id = %d\nvma_start = 0x%016llx\nvma_end = 0x%016llx\n"
+				"vma:atomic = %d\nvma:pat_index = %d\nvma:preferred_loc_region = %d\n"
+				"vma:preferred_loc_devmem_fd = %d\n\n\n", i, mem_attrs->start,
+				mem_attrs->end,
+				mem_attrs->atomic.val, mem_attrs->pat_index.val,
+				mem_attrs->preferred_mem_loc.migration_policy,
+				mem_attrs->preferred_mem_loc.devmem_fd);
+
+		ptr += query.sizeof_mem_range_attr;
+	}
+
+	free(ptr_start);
+	return 0;
+}
+
 uint32_t xe_vm_create(int fd, uint32_t flags, uint64_t ext)
 {
 	struct drm_xe_vm_create create = {
lib/xe/xe_ioctl.h +4 -0
index 8e13ffe65..14245eeec 100644
@@ -103,4 +103,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);
 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);
+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);
+int xe_vm_print_mem_attr_values_in_range(int fd, uint32_t vm, uint64_t start, uint64_t range);
 #endif /* XE_IOCTL_H */
Signature · 1 line
2.43.0
Reviewed-by on v9 3/5
#5 lore raw

RE: [PATCH i-g-t v9 3/5] lib/xe: Add Helper to get memory attributes

Quoted 149 lines

-----Original Message-----
From: Sharma, Nishit <[email protected]>
Sent: Wednesday, September 3, 2025 4:07 PM
To: [email protected]; Gurram, Pravalika <[email protected]>; Ghimiray, Himal Prasad <[email protected]>; Brost, Matthew <[email protected]>; Sharma, Nishit <[email protected]>
Subject: [PATCH i-g-t v9 3/5] lib/xe: Add Helper to get memory attributes

From: Nishit Sharma <[email protected]>

xe_vm_print_mem_attr_values_in_range() function added which calls QUERY_MEM_RANGES_ATTRS ioctl to get different memory attributes from KMD and then prints memory attributes returned by KMD for different access policies like atomic access, preferred loc and pat index.

---
 lib/xe/xe_ioctl.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/xe/xe_ioctl.h |  4 +++
 2 files changed, 96 insertions(+)
lib/xe/xe_ioctl.c index 5608c8780..28b7d5bec renamed from lib/xe/xe_ioctl.c +0 -0

100644

lib/xe/xe_ioctl.c +56 -0
@@ -57,6 +57,98 @@ uint64_t xe_bb_size(int fd, uint64_t reqsize)
 	             xe_get_default_alignment(fd));  }
 
+int xe_vm_number_vmas_in_range(int fd, struct
+drm_xe_vm_query_mem_range_attr *vmas_attr) {
+	if (igt_ioctl(fd, DRM_IOCTL_XE_VM_QUERY_MEM_RANGE_ATTRS,
 vmas_attr))
+		return -errno;
+	return 0;
+}
+
+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) {
+	if (!mem_attr)
+		return -EINVAL;
+
+	vmas_attr->vector_of_mem_attr = (uintptr_t)mem_attr;
+
+	if (igt_ioctl(fd, DRM_IOCTL_XE_VM_QUERY_MEM_RANGE_ATTRS,
 vmas_attr))
+		return -errno;
+
+	return 0;
+}
+
+/**
+ * xe_vm_print_mem_attr_values_in_range:
+ * @fd: xe device fd
+ * @vm: vm_id of the virtual range
+ * @start: start of the virtual address range
+ * @range: size of the virtual address range
+ *
+ * Calls QUERY_MEM_RANGES_ATTRS ioctl to get memory attributes for
+different
+ * memory ranges from KMD. prints memory attributes as returned by KMD
+for
+ * atomic, prefrred loc and pat index types.
+ *
+ * Returns 0 for success or error for failure  */
+
+int xe_vm_print_mem_attr_values_in_range(int fd, uint32_t vm, uint64_t
+start, uint64_t range) {
+
+	void *ptr_start, *ptr;
+	int err;
+	struct drm_xe_vm_query_mem_range_attr query = {
+		.vm_id = vm,
+		.start = start,
+		.range = range,
+		.num_mem_ranges = 0,
+		.sizeof_mem_range_attr = 0,
+		.vector_of_mem_attr = (uintptr_t)NULL,
+	};
+
+	igt_debug("mem_attr_values_in_range called start = %"PRIu64"\n range
 = %"PRIu64"\n",
+		  start, range);
+
+	err  = xe_vm_number_vmas_in_range(fd, &query);
+	if (err || !query.num_mem_ranges || !query.sizeof_mem_range_attr) {
+		igt_warn("ioctl failed for xe_vm_number_vmas_in_range\n");
+		igt_debug("vmas_in_range err = %d query.num_mem_ranges =

%u query.sizeof_mem_range_attr=%lld\n",

lib/xe/xe_ioctl.c +1 -0
+			 err, query.num_mem_ranges,

query.sizeof_mem_range_attr);

lib/xe/xe_ioctl.c +22 -0
+		return err;
+	}
+
+	/* Allocate buffer for the memory region attributes */
+	ptr = malloc(query.num_mem_ranges * query.sizeof_mem_range_attr);
+	ptr_start = ptr;
+
+	if (!ptr)
+		return -ENOMEM;
+
+	err = xe_vm_vma_attrs(fd, &query, ptr);
+	if (err) {
+		igt_warn("ioctl failed for vma_attrs err = %d\n", err);
+		return err;
+	}
+
+	/* Iterate over the returned memory region attributes */
+	for (unsigned int i = 0; i < query.num_mem_ranges; ++i) {
+		struct drm_xe_mem_range_attr *mem_attrs = (struct
+drm_xe_mem_range_attr *)ptr;
+
+		igt_debug("vma_id = %d\nvma_start = 0x%016llx\nvma_end =

0x%016llx\n"

lib/xe/xe_ioctl.c +1 -0
+				"vma:atomic = %d\nvma:pat_index =

%d\nvma:preferred_loc_region = %d\n"

lib/xe/xe_ioctl.c +1 -0
+				"vma:preferred_loc_devmem_fd = %d\n\n\n",

i, mem_attrs->start,

lib/xe/xe_ioctl.c +2 -0
+				mem_attrs->end,
+				mem_attrs->atomic.val, mem_attrs-
Quoted 1 line

pat_index.val,

lib/xe/xe_ioctl.c +1 -0
+				mem_attrs-
Quoted 1 line

preferred_mem_loc.migration_policy,

lib/xe/xe_ioctl.c +9 -0
+				mem_attrs->preferred_mem_loc.devmem_fd);
+
+		ptr += query.sizeof_mem_range_attr;
+	}
+
+	free(ptr_start);
+	return 0;
+}
+
 uint32_t xe_vm_create(int fd, uint32_t flags, uint64_t ext)  {
 	struct drm_xe_vm_create create = {
lib/xe/xe_ioctl.h index 8e13ffe65..14245eeec renamed from lib/xe/xe_ioctl.h +0 -0

100644

lib/xe/xe_ioctl.h +0 -0
@@ -103,4 +103,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); void
xe_vm_madvise(int fd, uint32_t vm, uint64_t addr, uint64_t range, uint64_t ext,

lib/xe/xe_ioctl.h +2 -0
 		  uint32_t type, uint32_t op_val, uint16_t policy);
+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,

lib/xe/xe_ioctl.h

Quoted 3 lines
lib/xe/xe_ioctl.h +3 -0
+		    struct drm_xe_mem_range_attr *mem_attr); int
+xe_vm_print_mem_attr_values_in_range(int fd, uint32_t vm, uint64_t
+start, uint64_t range);
Quoted 3 lines

#endif /* XE_IOCTL_H */
--
2.43.0

#6 lore raw

PATCH v9 4/5 tests/intel/xe_exec_system_allocator: Add madvise-swizzle test

In reply to [email protected]

From: Nishit Sharma <[email protected]>

madvise-swizzle test introduced which is called in combination with other tests as well. In this test the buffer object preferred location is system memory. MADVISE_SWIZZLE called in main loop to toggle between SYSTEM/DEVICE on each pass.

---
 tests/intel/xe_exec_system_allocator.c | 32 ++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
tests/intel/xe_exec_system_allocator.c +32 -0
index e7f3d423a..4a208f6d8 100644
@@ -777,6 +777,7 @@ partial(int fd, struct drm_xe_engine_class_instance *eci, unsigned int flags)
 #define PROCESSES		(0x1 << 24)
 #define PREFETCH_BENCHMARK	(0x1 << 25)
 #define PREFETCH_SYS_BENCHMARK	(0x1 << 26)
+#define MADVISE_SWIZZLE			(0x1 << 27)
 
 #define N_MULTI_FAULT		4
 
@@ -885,7 +886,9 @@ partial(int fd, struct drm_xe_engine_class_instance *eci, unsigned int flags)
  * arg[1]:
  *
  * @malloc:				malloc single buffer for all execs, issue a command which will trigger multiple faults
+ * @malloc-madvise:			malloc single buffer for all execs, issue a command which will trigger multiple faults, perfoems madvise operation
  * @malloc-prefetch:			malloc single buffer for all execs, prefetch buffer before each exec
+ * @malloc-prefetch-madvise:		malloc single buffer for all execs, prefetch buffer before each exec, performs madvise operation
  * @malloc-multi-fault:			malloc single buffer for all execs
  * @malloc-fork-read:			malloc single buffer for all execs, fork a process to read test output
  * @malloc-fork-read-after:		malloc single buffer for all execs, fork a process to read test output, check again after fork returns in parent
@@ -897,6 +900,7 @@ partial(int fd, struct drm_xe_engine_class_instance *eci, unsigned int flags)
  * @mmap:				mmap single buffer for all execs
  * @mmap-prefetch:			mmap single buffer for all execs, prefetch buffer before each exec
  * @mmap-remap:				mmap and mremap a buffer for all execs
+ * @mmap-remap-madvise:			mmap and mremap a buffer for all execs, performs madvise operations
  * @mmap-remap-dontunmap:		mmap and mremap a buffer with dontunmap flag for all execs
  * @mmap-remap-ro:			mmap and mremap a read-only buffer for all execs
  * @mmap-remap-ro-dontunmap:		mmap and mremap a read-only buffer with dontunmap flag for all execs
@@ -916,8 +920,10 @@ partial(int fd, struct drm_xe_engine_class_instance *eci, unsigned int flags)
  * @mmap-file-mlock:			mmap and mlock single buffer, with file backing, for all execs
  * @mmap-race:				mmap single buffer for all execs with race between cpu and gpu access
  * @free:				malloc and free buffer for each exec
+ * @free-madvise:			malloc and free buffer for each exec, performs madvise operation
  * @free-race:				malloc and free buffer for each exec with race between cpu and gpu access
  * @new:				malloc a new buffer for each exec
+ * @new-madvise:			malloc a new buffer for each exec, performs madvise operation
  * @new-prefetch:			malloc a new buffer and prefetch for each exec
  * @new-race:				malloc a new buffer for each exec with race between cpu and gpu access
  * @new-bo-map:				malloc a new buffer or map BO for each exec
@@ -999,6 +1005,23 @@ static void igt_require_hugepages(void)
 		      "No huge pages available!\n");
 }
 
+static void
+madvise_swizzle_op_exec(int fd, uint32_t vm, struct test_exec_data *data,
+		size_t bo_size, uint64_t addr, int index)
+{
+	int preferred_loc;
+
+	if (index % 2 == 0)
+		preferred_loc = DRM_XE_PREFERRED_LOC_DEFAULT_SYSTEM;
+	else
+		preferred_loc = DRM_XE_PREFERRED_LOC_DEFAULT_DEVICE;
+
+	xe_vm_madvise(fd, vm, to_user_pointer(data), bo_size, 0,
+			DRM_XE_MEM_RANGE_ATTR_PREFERRED_LOC,
+			preferred_loc,
+			0);
+}
+
 static void
 test_exec(int fd, struct drm_xe_engine_class_instance *eci,
 	  int n_exec_queues, int n_execs, size_t bo_size,
@@ -1210,6 +1233,9 @@ test_exec(int fd, struct drm_xe_engine_class_instance *eci,
 		if (barrier)
 			pthread_barrier_wait(barrier);
 
+		if (flags & MADVISE_SWIZZLE)
+			madvise_swizzle_op_exec(fd, vm, data, bo_size, addr, i);
+
 		if (flags & MULTI_FAULT) {
 			b = 0;
 			for (j = 0; j < N_MULTI_FAULT - 1; ++j)
@@ -1790,7 +1816,9 @@ igt_main
 	struct drm_xe_engine_class_instance *hwe;
 	const struct section sections[] = {
 		{ "malloc", 0 },
+		{ "malloc-madvise", MADVISE_SWIZZLE },
 		{ "malloc-prefetch", PREFETCH },
+		{ "malloc-prefetch-madvise", PREFETCH | MADVISE_SWIZZLE },
 		{ "malloc-multi-fault", MULTI_FAULT },
 		{ "malloc-fork-read", FORK_READ },
 		{ "malloc-fork-read-after", FORK_READ | FORK_READ_AFTER },
@@ -1802,6 +1830,7 @@ igt_main
 		{ "mmap", MMAP },
 		{ "mmap-prefetch", MMAP | PREFETCH },
 		{ "mmap-remap", MMAP | MREMAP },
+		{ "mmap-remap-madvise", MMAP | MREMAP | MADVISE_SWIZZLE },
 		{ "mmap-remap-dontunmap", MMAP | MREMAP | DONTUNMAP },
 		{ "mmap-remap-ro", MMAP | MREMAP | READ_ONLY_REMAP },
 		{ "mmap-remap-ro-dontunmap", MMAP | MREMAP | DONTUNMAP |
@@ -1828,13 +1857,16 @@ igt_main
 		{ "mmap-file-mlock", MMAP | LOCK | FILE_BACKED },
 		{ "mmap-race", MMAP | RACE },
 		{ "free", NEW | FREE },
+		{ "free-madvise", NEW | FREE | MADVISE_SWIZZLE },
 		{ "free-race", NEW | FREE | RACE },
 		{ "new", NEW },
+		{ "new-madvise", NEW | MADVISE_SWIZZLE },
 		{ "new-prefetch", NEW | PREFETCH },
 		{ "new-race", NEW | RACE },
 		{ "new-bo-map", NEW | BO_MAP },
 		{ "new-busy", NEW | BUSY },
 		{ "mmap-free", MMAP | NEW | FREE },
+		{ "mmap-free", MMAP | NEW | FREE | MADVISE_SWIZZLE },
 		{ "mmap-free-huge", MMAP | NEW | FREE | HUGE_PAGE },
 		{ "mmap-free-race", MMAP | NEW | FREE | RACE },
 		{ "mmap-new", MMAP | NEW },
Signature · 1 line
2.43.0
#7 lore raw

RE: [PATCH i-g-t v9 4/5] tests/intel/xe_exec_system_allocator: Add madvise-swizzle test

Quoted 152 lines

-----Original Message-----
From: Sharma, Nishit <[email protected]>
Sent: Wednesday, September 3, 2025 4:07 PM
To: [email protected]; Gurram, Pravalika <[email protected]>; Ghimiray, Himal Prasad <[email protected]>; Brost, Matthew <[email protected]>; Sharma, Nishit <[email protected]>
Subject: [PATCH i-g-t v9 4/5] tests/intel/xe_exec_system_allocator: Add madvise-swizzle test

From: Nishit Sharma <[email protected]>

madvise-swizzle test introduced which is called in combination with other tests as well. In this test the buffer object preferred location is system memory. MADVISE_SWIZZLE called in main loop to toggle between SYSTEM/DEVICE on each pass.

---
 tests/intel/xe_exec_system_allocator.c | 32 ++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
tests/intel/xe_exec_system_allocator.c +1 -0
index e7f3d423a..4a208f6d8 100644
@@ -777,6 +777,7 @@ partial(int fd, struct drm_xe_engine_class_instance *eci,
 unsigned int flags)
 #define PROCESSES		(0x1 << 24)
 #define PREFETCH_BENCHMARK	(0x1 << 25)
 #define PREFETCH_SYS_BENCHMARK	(0x1 << 26)
+#define MADVISE_SWIZZLE			(0x1 << 27)
 
 #define N_MULTI_FAULT		4
@@ -885,7 +886,9 @@ partial(int fd, struct drm_xe_engine_class_instance *eci,
 unsigned int flags)
  * arg[1]:
  *
  * @malloc:				malloc single buffer for all execs, issue

a command which will trigger multiple faults
+ * @malloc-madvise: malloc single buffer for all execs, issue
a command which will trigger multiple faults, perfoems madvise operation
* @malloc-prefetch: malloc single buffer for all execs,
prefetch buffer before each exec
+ * @malloc-prefetch-madvise: malloc single buffer for all execs,
prefetch buffer before each exec, performs madvise operation
* @malloc-multi-fault: malloc single buffer for all execs
* @malloc-fork-read: malloc single buffer for all execs, fork a
process to read test output
* @malloc-fork-read-after: malloc single buffer for all execs, fork a
process to read test output, check again after fork returns in parent

tests/intel/xe_exec_system_allocator.c +0 -0
@@ -897,6 +900,7 @@ partial(int fd, struct drm_xe_engine_class_instance *eci,
 unsigned int flags)
  * @mmap:				mmap single buffer for all execs
  * @mmap-prefetch:			mmap single buffer for all execs,

prefetch buffer before each exec
* @mmap-remap: mmap and mremap a buffer
for all execs
+ * @mmap-remap-madvise: mmap and mremap a buffer
for all execs, performs madvise operations
* @mmap-remap-dontunmap: mmap and mremap a buffer with
dontunmap flag for all execs
* @mmap-remap-ro: mmap and mremap a read-only buffer
for all execs
* @mmap-remap-ro-dontunmap: mmap and mremap a read-
only buffer with dontunmap flag for all execs

tests/intel/xe_exec_system_allocator.c +0 -0
@@ -916,8 +920,10 @@ partial(int fd, struct drm_xe_engine_class_instance
 *eci, unsigned int flags)
  * @mmap-file-mlock:			mmap and mlock single buffer, with file

backing, for all execs
* @mmap-race: mmap single buffer for all
execs with race between cpu and gpu access
* @free: malloc and free buffer for each exec
+ * @free-madvise: malloc and free buffer for each exec,
performs madvise operation
* @free-race: malloc and free buffer for each exec
with race between cpu and gpu access
* @new: malloc a new buffer for each exec
+ * @new-madvise: malloc a new buffer for each exec,
performs madvise operation
* @new-prefetch: malloc a new buffer and prefetch for
each exec
* @new-race: malloc a new buffer for each exec with
race between cpu and gpu access
* @new-bo-map: malloc a new buffer or map BO
for each exec

tests/intel/xe_exec_system_allocator.c +16 -0
@@ -999,6 +1005,23 @@ static void igt_require_hugepages(void)
 		      "No huge pages available!\n");
 }
 
+static void
+madvise_swizzle_op_exec(int fd, uint32_t vm, struct test_exec_data *data,
+		size_t bo_size, uint64_t addr, int index) {
+	int preferred_loc;
+
+	if (index % 2 == 0)
+		preferred_loc = DRM_XE_PREFERRED_LOC_DEFAULT_SYSTEM;
+	else
+		preferred_loc = DRM_XE_PREFERRED_LOC_DEFAULT_DEVICE;
+
+	xe_vm_madvise(fd, vm, to_user_pointer(data), bo_size, 0,
+			DRM_XE_MEM_RANGE_ATTR_PREFERRED_LOC,
+			preferred_loc,
+			0);
+}
+
 static void
 test_exec(int fd, struct drm_xe_engine_class_instance *eci,
 	  int n_exec_queues, int n_execs, size_t bo_size, @@ -1210,6 +1233,9

@@ test_exec(int fd, struct drm_xe_engine_class_instance *eci,

tests/intel/xe_exec_system_allocator.c +2 -0
 		if (barrier)
 			pthread_barrier_wait(barrier);
 
+		if (flags & MADVISE_SWIZZLE)
+			madvise_swizzle_op_exec(fd, vm, data, bo_size, addr,

i);

tests/intel/xe_exec_system_allocator.c +5 -0
+
 		if (flags & MULTI_FAULT) {
 			b = 0;
 			for (j = 0; j < N_MULTI_FAULT - 1; ++j) @@ -1790,7
+1816,9 @@ igt_main
 	struct drm_xe_engine_class_instance *hwe;
 	const struct section sections[] = {
 		{ "malloc", 0 },
+		{ "malloc-madvise", MADVISE_SWIZZLE },
 		{ "malloc-prefetch", PREFETCH },
+		{ "malloc-prefetch-madvise", PREFETCH | MADVISE_SWIZZLE },
 		{ "malloc-multi-fault", MULTI_FAULT },
 		{ "malloc-fork-read", FORK_READ },
 		{ "malloc-fork-read-after", FORK_READ | FORK_READ_AFTER },
@@ -1802,6 +1830,7 @@ igt_main
 		{ "mmap", MMAP },
 		{ "mmap-prefetch", MMAP | PREFETCH },
 		{ "mmap-remap", MMAP | MREMAP },
+		{ "mmap-remap-madvise", MMAP | MREMAP |
 MADVISE_SWIZZLE },
 		{ "mmap-remap-dontunmap", MMAP | MREMAP |
 DONTUNMAP },

{ "mmap-remap-ro", MMAP | MREMAP | READ_ONLY_REMAP
},
{ "mmap-remap-ro-dontunmap", MMAP | MREMAP |
DONTUNMAP | @@ -1828,13 +1857,16 @@ igt_main

tests/intel/xe_exec_system_allocator.c +1 -0
 		{ "mmap-file-mlock", MMAP | LOCK | FILE_BACKED },
 		{ "mmap-race", MMAP | RACE },
 		{ "free", NEW | FREE },
+		{ "free-madvise", NEW | FREE | MADVISE_SWIZZLE },
 		{ "free-race", NEW | FREE | RACE },
 		{ "new", NEW },

tests/intel/xe_exec_system_allocator.c igt_main · line 1872

Quoted 10 lines
tests/intel/xe_exec_system_allocator.c +2 -0
+		{ "new-madvise", NEW | MADVISE_SWIZZLE },
 		{ "new-prefetch", NEW | PREFETCH },
 		{ "new-race", NEW | RACE },
 		{ "new-bo-map", NEW | BO_MAP },
 		{ "new-busy", NEW | BUSY },
 		{ "mmap-free", MMAP | NEW | FREE },
+		{ "mmap-free", MMAP | NEW | FREE | MADVISE_SWIZZLE },
 		{ "mmap-free-huge", MMAP | NEW | FREE | HUGE_PAGE },
 		{ "mmap-free-race", MMAP | NEW | FREE | RACE },
 		{ "mmap-new", MMAP | NEW },

Looks good to me, I let others to comment
--Pravalika

Quoted 2 lines

--
2.43.0

#8 lore raw

PATCH v9 5/5 tests/intel/xe_exec_system_allocator: Add atomic_batch test in IGT

In reply to [email protected]

From: Nishit Sharma <[email protected]>

ATOMIC_BATCH flag is introduced when true MI_ATOMIC | MI_ATOMIC_INC operation will be called. This will avoid writing another function which performs atomic increment operations. ATOMIC_BATCH flag is passed as argument in write_dword() if true then value will be written on passed address and incremented by ATOMIC_INC operation. For all memory operations this flag will be used to verify if ATOMIC operation is working or not.

---
 tests/intel/xe_exec_system_allocator.c | 403 ++++++++++++++++++++++---
 1 file changed, 366 insertions(+), 37 deletions(-)
tests/intel/xe_exec_system_allocator.c +366 -37
index 4a208f6d8..4d0c1047a 100644
@@ -17,6 +17,7 @@
 #include <time.h>
 
 #include "igt.h"
+#include "intel_pat.h"
 #include "lib/igt_syncobj.h"
 #include "lib/intel_compute.h"
 #include "lib/intel_reg.h"
@@ -31,6 +32,15 @@
 #define QUARTER_SEC		(NSEC_PER_SEC / 4)
 #define FIVE_SEC		(5LL * NSEC_PER_SEC)
 
+struct test_exec_data {
+	uint32_t batch[32];
+	uint64_t pad;
+	uint64_t vm_sync;
+	uint64_t exec_sync;
+	uint32_t data;
+	uint32_t expected_data;
+};
+
 struct batch_data {
 	uint32_t batch[16];
 	uint64_t pad;
@@ -38,6 +48,7 @@ struct batch_data {
 	uint32_t expected_data;
 };
 
+#define VAL_ATOMIC_EXPECTED  56
 #define WRITE_VALUE(data__, i__)	({			\
 	if (!(data__)->expected_data)				\
 		(data__)->expected_data = rand() << 12 | (i__);	\
@@ -54,10 +65,19 @@ static void __write_dword(uint32_t *batch, uint64_t sdi_addr, uint32_t wdata,
 	batch[(*idx)++] = wdata;
 }
 
-static void write_dword(uint32_t *batch, uint64_t sdi_addr, uint32_t wdata,
-			int *idx)
+static void write_dword(struct test_exec_data *data, uint64_t sdi_addr, uint32_t wdata,
+			int *idx, bool atomic)
 {
-	__write_dword(batch, sdi_addr, wdata, idx);
+	uint32_t *batch = data->batch;
+
+	if (atomic) {
+		data->data = 55;
+		batch[(*idx)++] = MI_ATOMIC | MI_ATOMIC_INC;
+		batch[(*idx)++] = sdi_addr;
+		batch[(*idx)++] = sdi_addr >> 32;
+	} else
+		__write_dword(batch, sdi_addr, wdata, idx);
+
 	batch[(*idx)++] = MI_BATCH_BUFFER_END;
 }
 
@@ -304,7 +324,7 @@ static void touch_all_pages(int fd, uint32_t exec_queue, void *ptr,
 		uint64_t sdi_addr = addr + sdi_offset;
 		int b = 0;
 
-		write_dword(data->batch, sdi_addr, WRITE_VALUE(data, i), &b);
+		write_dword((struct test_exec_data *)data, sdi_addr, WRITE_VALUE(data, i), &b, false);
 		igt_assert(b <= ARRAY_SIZE(data->batch));
 	}
 
@@ -447,6 +467,54 @@ static void __aligned_partial_free(struct aligned_alloc_type  *aligned_alloc_typ
  * SUBTEST: processes-evict-malloc-mix-bo
  * Description: multi-process trigger eviction of VRAM allocated via malloc and BO create
  * Test category: stress test
+ *
+ * SUBTEST: madvise-multi-vma
+ * Description: performs multiple madvise operations on multiple virtual memory area using atomic device attributes
+ * Test category: functionality test
+ *
+ * SUBTEST: madvise-split-vma
+ * Description: perform madvise operations on multiple type VMAs (BO and CPU VMAs)
+ * Test category: perform madvise operations on multiple type VMAs (BO and CPU VMAs)
+ *
+ * SUBTEST: madvise-atomic-vma
+ * Description: perform madvise atomic operations on BO in VRAM/SMEM if atomic ATTR global/device
+ * Test category: functionality test
+ *
+ * SUBTEST: madvise-split-vma-with-mapping
+ * Description: performs prefetch and page migration
+ * Test category: functionality test
+ *
+ * SUBTEST: madvise-preffered-loc-atomic-vram
+ * Description: performs both atomic and preferred loc madvise operations atomic device attributes set
+ * Test category: functionality test
+ *
+ * SUBTEST: madvise-preffered-loc-atomic-gl
+ * Description: performs both atomic and preferred loc madvise operations with atomic global attributes set
+ * Test category: functionality test
+ *
+ * SUBTEST: madvise-preffered-loc-atomic-cpu
+ * Description: performs both atomic and preferred loc madvise operations with atomic cpu attributes set
+ * Test category: functionality test
+ *
+ * SUBTEST: madvise-preffered-loc-sram-migrate-pages
+ * Description: performs preferred loc madvise operations and migrating all pages in smem
+ * Test category: functionality test
+ *
+ * SUBTEST: madvise-no-range-invalidate-same-attr
+ * Description: performs atomic global madvise operation, prefetch and again madvise operation with same atomic attribute
+ * Test category: functionality test
+ *
+ * SUBTEST: madvise-range-invalidate-change-attr
+ * Description: performs atomic global madvise operation, prefetch and again madvise operation with different atomic attribute
+ * Test category: functionality test
+ *
+ * SUBTEST: madvise-preffered-loc-atomic-und
+ * Description: Tests madvise with preferred location set for atomic operations, but with an undefined
+ * Test category: functionality test
+ *
+ * SUBTEST: madvise-atomic-inc
+ * Description: Tests madvise atomic operations
+ * Test category: functionality test
  */
 
 static void
@@ -701,7 +769,8 @@ partial(int fd, struct drm_xe_engine_class_instance *eci, unsigned int flags)
 		uint64_t sdi_addr = addr + sdi_offset;
 		int b = 0;
 
-		write_dword(data[i].batch, sdi_addr, WRITE_VALUE(&data[i], i), &b);
+		write_dword((struct test_exec_data *)&data[i], sdi_addr, WRITE_VALUE(&data[i], i),
+			     &b, false);
 		igt_assert(b <= ARRAY_SIZE(data[i].batch));
 
 		if (!i)
@@ -778,6 +847,20 @@ partial(int fd, struct drm_xe_engine_class_instance *eci, unsigned int flags)
 #define PREFETCH_BENCHMARK	(0x1 << 25)
 #define PREFETCH_SYS_BENCHMARK	(0x1 << 26)
 #define MADVISE_SWIZZLE			(0x1 << 27)
+#define MADVISE_OP			(0x1 << 28)
+#define ATOMIC_BATCH			(0x1 << 29)
+#define MIGRATE_ALL_PAGES		(0x1 << 30)
+#define PREFERRED_LOC_ATOMIC_DEVICE	(0x1ull << 31)
+#define PREFERRED_LOC_ATOMIC_GL		(0x1ull << 32)
+#define PREFERRED_LOC_ATOMIC_CPU	(0x1ull << 33)
+#define MADVISE_MULTI_VMA		(0x1ull << 34)
+#define MADVISE_SPLIT_VMA		(0x1ull << 35)
+#define MADVISE_ATOMIC_VMA		(0x1ull << 36)
+#define PREFETCH_SPLIT_VMA		(0x1ull << 37)
+#define PREFETCH_CHANGE_ATTR		(0x1ull << 38)
+#define PREFETCH_SAME_ATTR		(0x1ull << 39)
+#define PREFERRED_LOC_ATOMIC_UND	(0x1ull << 40)
+#define MADVISE_ATOMIC_DEVICE		(0x1ull << 41)
 
 #define N_MULTI_FAULT		4
 
@@ -886,7 +969,7 @@ partial(int fd, struct drm_xe_engine_class_instance *eci, unsigned int flags)
  * arg[1]:
  *
  * @malloc:				malloc single buffer for all execs, issue a command which will trigger multiple faults
- * @malloc-madvise:			malloc single buffer for all execs, issue a command which will trigger multiple faults, perfoems madvise operation
+ * @malloc-madvise:			malloc single buffer for all execs, issue a command which will trigger multiple faults, performs madvise operation
  * @malloc-prefetch:			malloc single buffer for all execs, prefetch buffer before each exec
  * @malloc-prefetch-madvise:		malloc single buffer for all execs, prefetch buffer before each exec, performs madvise operation
  * @malloc-multi-fault:			malloc single buffer for all execs
@@ -987,16 +1070,6 @@ partial(int fd, struct drm_xe_engine_class_instance *eci, unsigned int flags)
  * Description: Create multiple threads with a faults on different hardware engines to same addresses, racing between CPU and GPU access
  * Test category: stress test
  */
-
-struct test_exec_data {
-	uint32_t batch[32];
-	uint64_t pad;
-	uint64_t vm_sync;
-	uint64_t exec_sync;
-	uint32_t data;
-	uint32_t expected_data;
-};
-
 static void igt_require_hugepages(void)
 {
 	igt_skip_on_f(!igt_get_meminfo("HugePages_Total"),
@@ -1022,11 +1095,184 @@ madvise_swizzle_op_exec(int fd, uint32_t vm, struct test_exec_data *data,
 			0);
 }
 
+static void
+xe_vm_madvixe_pat_attr(int fd, uint32_t vm, uint64_t addr, uint64_t range,
+		int pat_index)
+{
+	xe_vm_madvise(fd, vm, addr, range, 0,
+			DRM_XE_MEM_RANGE_ATTR_PAT, pat_index, 0);
+}
+
+static void
+xe_vm_madvise_atomic_attr(int fd, uint32_t vm, uint64_t addr, uint64_t range,
+		int mem_attr)
+{
+	xe_vm_madvise(fd, vm, addr, range, 0,
+			DRM_XE_MEM_RANGE_ATTR_ATOMIC,
+			mem_attr, 0);
+}
+
+static void
+xe_vm_madvise_migrate_pages(int fd, uint32_t vm, uint64_t addr, uint64_t range)
+{
+	xe_vm_madvise(fd, vm, addr, range, 0,
+			DRM_XE_MEM_RANGE_ATTR_PREFERRED_LOC,
+			DRM_XE_PREFERRED_LOC_DEFAULT_SYSTEM,
+			DRM_XE_MIGRATE_ALL_PAGES);
+}
+
+static void
+xe_vm_parse_execute_madvise(int fd, uint32_t vm, struct test_exec_data *data,
+		size_t bo_size, struct drm_xe_engine_class_instance *eci,
+		uint64_t addr, unsigned long long flags,
+		struct drm_xe_sync *sync)
+{
+	uint32_t bo_flags, bo = 0;
+
+
+	if (flags & MADVISE_ATOMIC_DEVICE)
+		xe_vm_madvise_atomic_attr(fd, vm, to_user_pointer(data), bo_size,
+				DRM_XE_ATOMIC_DEVICE);
+
+	if (flags & PREFERRED_LOC_ATOMIC_UND) {
+		xe_vm_madvise_migrate_pages(fd, vm, to_user_pointer(data), bo_size);
+
+		xe_vm_madvise_atomic_attr(fd, vm, to_user_pointer(data), bo_size,
+				DRM_XE_ATOMIC_UNDEFINED);
+	}
+
+	if (flags & PREFERRED_LOC_ATOMIC_DEVICE) {
+		xe_vm_madvise_migrate_pages(fd, vm, to_user_pointer(data), bo_size);
+
+		xe_vm_madvise_atomic_attr(fd, vm, to_user_pointer(data), bo_size,
+				DRM_XE_ATOMIC_DEVICE);
+	}
+
+	if (flags & PREFERRED_LOC_ATOMIC_GL) {
+		xe_vm_madvise_migrate_pages(fd, vm, to_user_pointer(data), bo_size);
+
+		xe_vm_madvise_atomic_attr(fd, vm, to_user_pointer(data), bo_size,
+				DRM_XE_ATOMIC_GLOBAL);
+	}
+
+	if (flags & PREFERRED_LOC_ATOMIC_CPU) {
+		xe_vm_madvise_migrate_pages(fd, vm, to_user_pointer(data), bo_size);
+
+		xe_vm_madvise_atomic_attr(fd, vm, to_user_pointer(data), bo_size,
+				DRM_XE_ATOMIC_CPU);
+	}
+
+	if (flags & MADVISE_MULTI_VMA) {
+		if (bo_size)
+			bo_size = ALIGN(bo_size, SZ_4K);
+		xe_vm_madvise_atomic_attr(fd, vm, to_user_pointer(data) + bo_size/2,
+				bo_size/2, DRM_XE_ATOMIC_DEVICE);
+
+		xe_vm_madvixe_pat_attr(fd, vm, to_user_pointer(data) + bo_size/2, bo_size/2,
+				intel_get_pat_idx_wb(fd));
+
+		xe_vm_madvise_atomic_attr(fd, vm, to_user_pointer(data) + bo_size,
+				bo_size, DRM_XE_ATOMIC_DEVICE);
+
+		xe_vm_madvixe_pat_attr(fd, vm, to_user_pointer(data), bo_size,
+				intel_get_pat_idx_wb(fd));
+	}
+
+	if (flags & MADVISE_SPLIT_VMA) {
+		if (bo_size)
+			bo_size = ALIGN(bo_size, SZ_4K);
+
+		bo_flags = DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM;
+		bo = xe_bo_create(fd, vm, bo_size, vram_if_possible(fd, eci->gt_id),
+				bo_flags);
+		xe_vm_bind_async(fd, vm, 0, bo, 0, to_user_pointer(data) + bo_size/2,
+				bo_size/2, 0, 0);
+
+		__xe_vm_bind_assert(fd, vm, 0, 0, 0, to_user_pointer(data) + bo_size/2,
+				bo_size/2, DRM_XE_VM_BIND_OP_MAP,
+				DRM_XE_VM_BIND_FLAG_CPU_ADDR_MIRROR, sync,
+				1, 0, 0);
+		xe_wait_ufence(fd, &data[0].vm_sync, USER_FENCE_VALUE, 0, FIVE_SEC);
+		data[0].vm_sync = 0;
+		gem_close(fd, bo);
+		bo = 0;
+
+
+		xe_vm_madvise_atomic_attr(fd, vm, to_user_pointer(data), bo_size/2,
+				DRM_XE_ATOMIC_GLOBAL);
+	}
+}
+
+static void
+madvise_prefetch_op(int fd, uint32_t vm, uint64_t addr, size_t bo_size,
+		unsigned long long flags, struct test_exec_data *data)
+{
+	uint32_t val;
+
+	if (flags & PREFETCH_SPLIT_VMA) {
+		bo_size = ALIGN(bo_size, SZ_4K);
+
+		xe_vm_prefetch_async(fd, vm, 0, 0, addr, bo_size, NULL, 0, 0);
+
+		val = xe_vm_print_mem_attr_values_in_range(fd, vm, addr,  bo_size);
+
+		igt_debug("num_vmas before madvise = %d\n", val);
+
+		xe_vm_madvise_migrate_pages(fd, vm, to_user_pointer(data), bo_size/2);
+
+		val = xe_vm_print_mem_attr_values_in_range(fd, vm, addr,  bo_size);
+
+		igt_debug("num_vmas after madvise= %d\n", val);
+	} else if (flags & PREFETCH_SAME_ATTR) {
+		xe_vm_madvise_atomic_attr(fd, vm, to_user_pointer(data), bo_size,
+				DRM_XE_ATOMIC_GLOBAL);
+		val = xe_vm_print_mem_attr_values_in_range(fd, vm, addr,  bo_size);
+
+		xe_vm_prefetch_async(fd, vm, 0, 0, addr, bo_size, NULL, 0,
+				DRM_XE_CONSULT_MEM_ADVISE_PREF_LOC);
+
+		xe_vm_madvise_atomic_attr(fd, vm, to_user_pointer(data), bo_size/2,
+				DRM_XE_ATOMIC_GLOBAL);
+	} else if (flags & PREFETCH_CHANGE_ATTR) {
+		xe_vm_madvise_atomic_attr(fd, vm, to_user_pointer(data), bo_size,
+				DRM_XE_ATOMIC_GLOBAL);
+
+		val = xe_vm_print_mem_attr_values_in_range(fd, vm, addr,  bo_size);
+
+		xe_vm_prefetch_async(fd, vm, 0, 0, addr, bo_size, NULL, 0,
+				DRM_XE_CONSULT_MEM_ADVISE_PREF_LOC);
+
+		xe_vm_madvise_atomic_attr(fd, vm, to_user_pointer(data), bo_size,
+				DRM_XE_ATOMIC_DEVICE);
+
+		val = xe_vm_print_mem_attr_values_in_range(fd, vm, addr,  bo_size);
+	}
+}
+
+static void
+madvise_vma_addr_map(uint64_t addr, int i, int idx, size_t bo_size,
+		     struct test_exec_data *data,
+		     uint64_t *batch_offset,
+		     uint64_t *batch_addr, uint64_t *sdi_offset,
+		     uint64_t *sdi_addr,
+		     unsigned long long flags,
+		     uint64_t *split_vma_offset)
+{
+	if (flags & MADVISE_MULTI_VMA) {
+		addr = addr + i * bo_size;
+		data = from_user_pointer(addr);
+		*batch_offset = (size_t)((char *)&(data[idx].batch) - (char *)data);
+		*batch_addr = addr + *batch_offset;
+		*sdi_offset = (size_t)((char *)&(data[idx].data) - (char *)data);
+		*sdi_addr = addr + *sdi_offset;
+	}
+}
+
 static void
 test_exec(int fd, struct drm_xe_engine_class_instance *eci,
 	  int n_exec_queues, int n_execs, size_t bo_size,
 	  size_t stride, uint32_t vm, void *alloc, pthread_barrier_t *barrier,
-	  unsigned int flags)
+	  unsigned long long flags)
 {
 	uint64_t addr;
 	struct drm_xe_sync sync[1] = {
@@ -1039,7 +1285,7 @@ test_exec(int fd, struct drm_xe_engine_class_instance *eci,
 		.syncs = to_user_pointer(sync),
 	};
 	uint32_t exec_queues[MAX_N_EXEC_QUEUES];
-	struct test_exec_data *data, *next_data = NULL;
+	struct test_exec_data *data, *next_data = NULL, *original_data = NULL;
 	uint32_t bo_flags;
 	uint32_t bo = 0, bind_sync = 0;
 	void **pending_free;
@@ -1157,6 +1403,9 @@ test_exec(int fd, struct drm_xe_engine_class_instance *eci,
 
 	addr = to_user_pointer(data);
 
+	if (flags & MADVISE_OP)
+		xe_vm_parse_execute_madvise(fd, vm, data, bo_size, eci, addr, flags, sync);
+
 	if (flags & BO_UNMAP) {
 		bo_flags = DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM;
 		bo = xe_bo_create(fd, vm, bo_size,
@@ -1230,42 +1479,61 @@ test_exec(int fd, struct drm_xe_engine_class_instance *eci,
 		bool fault_inject = (FAULT & flags) && i == n_execs / 2;
 		bool fault_injected = (FAULT & flags) && i > n_execs;
 
+		uint64_t split_vma_offset;
+
 		if (barrier)
 			pthread_barrier_wait(barrier);
 
 		if (flags & MADVISE_SWIZZLE)
 			madvise_swizzle_op_exec(fd, vm, data, bo_size, addr, i);
 
+		if (flags & MADVISE_OP) {
+			if (flags & MADVISE_MULTI_VMA)
+				original_data = data;
+
+			madvise_vma_addr_map(addr, i, idx, bo_size, data, &batch_offset,
+					      &batch_addr, &sdi_offset, &sdi_addr, flags,
+					      &split_vma_offset);
+		}
+
 		if (flags & MULTI_FAULT) {
 			b = 0;
 			for (j = 0; j < N_MULTI_FAULT - 1; ++j)
 				__write_dword(data[idx].batch,
 					      sdi_addr + j * orig_size,
 					      WRITE_VALUE(&data[idx], idx), &b);
-			write_dword(data[idx].batch, sdi_addr + j * orig_size,
-				    WRITE_VALUE(&data[idx], idx), &b);
+			write_dword(&data[idx], sdi_addr + j * orig_size,
+				    WRITE_VALUE(&data[idx], idx), &b,
+				    flags & ATOMIC_BATCH ? true : false);
 			igt_assert(b <= ARRAY_SIZE(data[idx].batch));
 		} else if (!(flags & EVERY_OTHER_CHECK)) {
 			b = 0;
-			write_dword(data[idx].batch, sdi_addr,
-				    WRITE_VALUE(&data[idx], idx), &b);
+			write_dword(&data[idx], sdi_addr,
+					WRITE_VALUE(&data[idx], idx), &b,
+					flags & ATOMIC_BATCH ? true : false);
 			igt_assert(b <= ARRAY_SIZE(data[idx].batch));
+			if (flags & PREFETCH)
+				madvise_prefetch_op(fd, vm, addr, bo_size, flags, data);
 		} else if (flags & EVERY_OTHER_CHECK && !odd(i)) {
 			b = 0;
-			write_dword(data[idx].batch, sdi_addr,
-				    WRITE_VALUE(&data[idx], idx), &b);
+			write_dword(&data[idx], sdi_addr,
+				    WRITE_VALUE(&data[idx], idx), &b,
+				    flags & ATOMIC_BATCH ? true : false);
 			igt_assert(b <= ARRAY_SIZE(data[idx].batch));
 
 			aligned_alloc_type = __aligned_alloc(aligned_size, bo_size);
 			next_data = aligned_alloc_type.ptr;
 			igt_assert(next_data);
+
+			xe_vm_parse_execute_madvise(fd, vm, data, bo_size, eci, addr, flags, sync);
 			__aligned_partial_free(&aligned_alloc_type);
 
 			b = 0;
-			write_dword(data[next_idx].batch,
+			write_dword(&data[next_idx],
 				    to_user_pointer(next_data) +
 				    (char *)&data[next_idx].data - (char *)data,
-				    WRITE_VALUE(&data[next_idx], next_idx), &b);
+				    WRITE_VALUE(&data[next_idx], next_idx), &b,
+				    flags & ATOMIC_BATCH ? true : false);
 			igt_assert(b <= ARRAY_SIZE(data[next_idx].batch));
 		}
 
@@ -1299,7 +1567,6 @@ test_exec(int fd, struct drm_xe_engine_class_instance *eci,
 			exec.address = batch_addr * 2;
 		else
 			exec.address = batch_addr;
-
 		if (fault_injected) {
 			err = __xe_exec(fd, &exec);
 			igt_assert(err == -ENOENT);
@@ -1319,9 +1586,19 @@ test_exec(int fd, struct drm_xe_engine_class_instance *eci,
 					       exec_queues[e], &timeout);
 			igt_assert(err == -ETIME || err == -EIO);
 		} else {
-			xe_wait_ufence(fd, exec_ufence ? exec_ufence :
-				       &data[idx].exec_sync, USER_FENCE_VALUE,
-				       exec_queues[e], FIVE_SEC);
+			if (flags & PREFERRED_LOC_ATOMIC_CPU || flags & PREFERRED_LOC_ATOMIC_UND) {
+				int64_t timeout = QUARTER_SEC;
+
+				err = __xe_wait_ufence(fd, exec_ufence ? exec_ufence :
+						       &data[idx].exec_sync,
+						       USER_FENCE_VALUE,
+						       exec_queues[e], &timeout);
+				if (err)
+					goto cleanup;
+			} else
+				xe_wait_ufence(fd, exec_ufence ? exec_ufence :
+					       &data[idx].exec_sync, USER_FENCE_VALUE,
+					       exec_queues[e], FIVE_SEC);
 			if (flags & LOCK && !i)
 				munlock(data, bo_size);
 
@@ -1371,17 +1648,25 @@ test_exec(int fd, struct drm_xe_engine_class_instance *eci,
 				if (flags & FORK_READ) {
 					igt_fork(child, 1)
 						igt_assert_eq(data[idx].data,
-							      READ_VALUE(&data[idx]));
+							      flags & ATOMIC_BATCH
+							      ? VAL_ATOMIC_EXPECTED
+							      : READ_VALUE(&data[idx]));
 					if (!(flags & FORK_READ_AFTER))
 						igt_assert_eq(data[idx].data,
-							      READ_VALUE(&data[idx]));
+							      flags & ATOMIC_BATCH
+							      ? VAL_ATOMIC_EXPECTED
+							      : READ_VALUE(&data[idx]));
 					igt_waitchildren();
 					if (flags & FORK_READ_AFTER)
 						igt_assert_eq(data[idx].data,
-							      READ_VALUE(&data[idx]));
+							      flags & ATOMIC_BATCH
+							      ? VAL_ATOMIC_EXPECTED
+							      : READ_VALUE(&data[idx]));
 				} else {
 					igt_assert_eq(data[idx].data,
-						      READ_VALUE(&data[idx]));
+						      flags & ATOMIC_BATCH
+						      ? VAL_ATOMIC_EXPECTED
+						      : READ_VALUE(&data[idx]));
 					if (flags & PREFETCH_SYS_BENCHMARK) {
 						struct timespec tv = {};
 						u64 start, end;
@@ -1408,13 +1693,17 @@ test_exec(int fd, struct drm_xe_engine_class_instance *eci,
 								((void *)data) + j * orig_size;
 
 							igt_assert_eq(__data[idx].data,
-								      READ_VALUE(&data[idx]));
+								      flags & ATOMIC_BATCH
+								      ? VAL_ATOMIC_EXPECTED
+								      : READ_VALUE(&data[idx]));
 						}
 					}
 				}
 				if (flags & EVERY_OTHER_CHECK)
 					igt_assert_eq(data[prev_idx].data,
-						      READ_VALUE(&data[prev_idx]));
+						      flags & ATOMIC_BATCH
+						      ? VAL_ATOMIC_EXPECTED
+						      : READ_VALUE(&data[prev_idx]));
 			}
 		}
 
@@ -1435,6 +1724,11 @@ test_exec(int fd, struct drm_xe_engine_class_instance *eci,
 			gem_close(fd, bo);
 		}
 
+		if (flags & MADVISE_MULTI_VMA) {
+			data = original_data;
+			original_data = NULL;
+		}
+
 		if (flags & NEW) {
 			if (flags & MMAP) {
 				if (flags & FREE)
@@ -1513,6 +1807,7 @@ test_exec(int fd, struct drm_xe_engine_class_instance *eci,
 				 pf_count, pf_count_after);
 	}
 
+cleanup:
 	if (bo) {
 		sync[0].addr = to_user_pointer(bind_ufence);
 		__xe_vm_bind_assert(fd, vm, 0,
@@ -1808,7 +2103,7 @@ test_compute(int fd, struct drm_xe_engine_class_instance *eci, size_t size)
 
 struct section {
 	const char *name;
-	unsigned int flags;
+	unsigned long long flags;
 };
 
 igt_main
@@ -1914,6 +2209,32 @@ igt_main
 		{ "malloc-mix-bo", MIX_BO_ALLOC },
 		{ NULL },
 	};
+	const struct section msections[] = {
+		{ "atomic-inc", MADVISE_OP | MADVISE_ATOMIC_DEVICE | ATOMIC_BATCH },
+		{ "preffered-loc-sram-migrate-pages",
+		  MADVISE_OP | MADVISE_SWIZZLE | MIGRATE_ALL_PAGES | ATOMIC_BATCH },
+		{ "preffered-loc-atomic-vram",
+		  MADVISE_OP | PREFERRED_LOC_ATOMIC_DEVICE | ATOMIC_BATCH },
+		{ "preffered-loc-atomic-gl",
+		  MADVISE_OP | PREFERRED_LOC_ATOMIC_GL | ATOMIC_BATCH },
+		{ "preffered-loc-atomic-cpu",
+		  MADVISE_OP | PREFERRED_LOC_ATOMIC_CPU | ATOMIC_BATCH },
+		{ "preffered-loc-atomic-und",
+		  MADVISE_OP | PREFERRED_LOC_ATOMIC_UND | ATOMIC_BATCH },
+		{ "multi-vma",
+		  MADVISE_OP | MADVISE_MULTI_VMA | ATOMIC_BATCH },
+		{ "split-vma",
+		  MADVISE_OP | MADVISE_SPLIT_VMA | ATOMIC_BATCH },
+		{ "atomic-vma",
+		  MADVISE_OP | MADVISE_ATOMIC_VMA | ATOMIC_BATCH },
+		{ "split-vma-with-mapping",
+		  MADVISE_OP | PREFETCH | PREFETCH_SPLIT_VMA | ATOMIC_BATCH },
+		{ "range-invalidate-change-attr",
+		  MADVISE_OP | PREFETCH | PREFETCH_CHANGE_ATTR | ATOMIC_BATCH },
+		{ "no-range-invalidate-same-attr",
+		  MADVISE_OP | PREFETCH | PREFETCH_SAME_ATTR | ATOMIC_BATCH },
+		{ NULL },
+	};
 	int fd;
 
 	igt_fixture {
@@ -2110,6 +2431,14 @@ igt_main
 			processes_evict(fd, SZ_8M, SZ_1M, s->flags);
 	}
 
+	for (const struct section *s = msections; s->name; s++) {
+		igt_subtest_f("madvise-%s", s->name) {
+			xe_for_each_engine(fd, hwe)
+				test_exec(fd, hwe, 1, 1, SZ_64K, 0, 0, NULL,
+					  NULL, s->flags);
+		}
+	}
+
 	igt_subtest("compute")
 		xe_for_each_engine(fd, hwe)
 			test_compute(fd, hwe, SZ_2M);
Signature · 1 line
2.43.0
#9 lore raw

RE: [PATCH i-g-t v9 5/5] tests/intel/xe_exec_system_allocator: Add atomic_batch test in IGT

Quoted 748 lines

-----Original Message-----
From: Sharma, Nishit <[email protected]>
Sent: Wednesday, September 3, 2025 4:07 PM
To: [email protected]; Gurram, Pravalika <[email protected]>; Ghimiray, Himal Prasad <[email protected]>; Brost, Matthew <[email protected]>; Sharma, Nishit <[email protected]>
Subject: [PATCH i-g-t v9 5/5] tests/intel/xe_exec_system_allocator: Add atomic_batch test in IGT

From: Nishit Sharma <[email protected]>

ATOMIC_BATCH flag is introduced when true MI_ATOMIC | MI_ATOMIC_INC operation will be called. This will avoid writing another function which performs atomic increment operations. ATOMIC_BATCH flag is passed as argument in write_dword() if true then value will be written on passed address and incremented by ATOMIC_INC operation. For all memory operations this flag will be used to verify if ATOMIC operation is working or not.

---
 tests/intel/xe_exec_system_allocator.c | 403 ++++++++++++++++++++++---
 1 file changed, 366 insertions(+), 37 deletions(-)
tests/intel/xe_exec_system_allocator.c +25 -4
index 4a208f6d8..4d0c1047a 100644
@@ -17,6 +17,7 @@
 #include <time.h>
 
 #include "igt.h"
+#include "intel_pat.h"
 #include "lib/igt_syncobj.h"
 #include "lib/intel_compute.h"
 #include "lib/intel_reg.h"
@@ -31,6 +32,15 @@
 #define QUARTER_SEC		(NSEC_PER_SEC / 4)
 #define FIVE_SEC		(5LL * NSEC_PER_SEC)
 
+struct test_exec_data {
+	uint32_t batch[32];
+	uint64_t pad;
+	uint64_t vm_sync;
+	uint64_t exec_sync;
+	uint32_t data;
+	uint32_t expected_data;
+};
+
 struct batch_data {
 	uint32_t batch[16];
 	uint64_t pad;
@@ -38,6 +48,7 @@ struct batch_data {
 	uint32_t expected_data;
 };
 
+#define VAL_ATOMIC_EXPECTED  56
 #define WRITE_VALUE(data__, i__)	({			\
 	if (!(data__)->expected_data)				\
 		(data__)->expected_data = rand() << 12 | (i__);	\
@@ -54,10 +65,19 @@ static void __write_dword(uint32_t *batch, uint64_t
 sdi_addr, uint32_t wdata,
 	batch[(*idx)++] = wdata;
 }
 
-static void write_dword(uint32_t *batch, uint64_t sdi_addr, uint32_t wdata,
-			int *idx)
+static void write_dword(struct test_exec_data *data, uint64_t sdi_addr,
 uint32_t wdata,
+			int *idx, bool atomic)
 {
-	__write_dword(batch, sdi_addr, wdata, idx);
+	uint32_t *batch = data->batch;
+
+	if (atomic) {
+		data->data = 55;
+		batch[(*idx)++] = MI_ATOMIC | MI_ATOMIC_INC;
+		batch[(*idx)++] = sdi_addr;
+		batch[(*idx)++] = sdi_addr >> 32;
+	} else
+		__write_dword(batch, sdi_addr, wdata, idx);
+
 	batch[(*idx)++] = MI_BATCH_BUFFER_END;  }
@@ -304,7 +324,7 @@ static void touch_all_pages(int fd, uint32_t exec_queue,
 void *ptr,
 		uint64_t sdi_addr = addr + sdi_offset;
 		int b = 0;
 
-		write_dword(data->batch, sdi_addr, WRITE_VALUE(data, i),
 &b);
+		write_dword((struct test_exec_data *)data, sdi_addr,
+WRITE_VALUE(data, i), &b, false);
 		igt_assert(b <= ARRAY_SIZE(data->batch));
 	}
@@ -447,6 +467,54 @@ static void __aligned_partial_free(struct

aligned_alloc_type *aligned_alloc_typ
* SUBTEST: processes-evict-malloc-mix-bo
* Description: multi-process trigger eviction of VRAM allocated via malloc and
BO create
* Test category: stress test
+ *
+ * SUBTEST: madvise-multi-vma
+ * Description: performs multiple madvise operations on multiple
+ virtual memory area using atomic device attributes
+ * Test category: functionality test
+ *
+ * SUBTEST: madvise-split-vma
+ * Description: perform madvise operations on multiple type VMAs (BO
+ and CPU VMAs)
+ * Test category: perform madvise operations on multiple type VMAs (BO
+ and CPU VMAs)
+ *
+ * SUBTEST: madvise-atomic-vma
+ * Description: perform madvise atomic operations on BO in VRAM/SMEM if
+ atomic ATTR global/device
+ * Test category: functionality test
+ *
+ * SUBTEST: madvise-split-vma-with-mapping
+ * Description: performs prefetch and page migration
+ * Test category: functionality test
+ *
+ * SUBTEST: madvise-preffered-loc-atomic-vram
+ * Description: performs both atomic and preferred loc madvise
+ operations atomic device attributes set
+ * Test category: functionality test
+ *
+ * SUBTEST: madvise-preffered-loc-atomic-gl
+ * Description: performs both atomic and preferred loc madvise
+ operations with atomic global attributes set
+ * Test category: functionality test
+ *
+ * SUBTEST: madvise-preffered-loc-atomic-cpu
+ * Description: performs both atomic and preferred loc madvise
+ operations with atomic cpu attributes set
+ * Test category: functionality test
+ *
+ * SUBTEST: madvise-preffered-loc-sram-migrate-pages
+ * Description: performs preferred loc madvise operations and migrating
+ all pages in smem
+ * Test category: functionality test
+ *
+ * SUBTEST: madvise-no-range-invalidate-same-attr
+ * Description: performs atomic global madvise operation, prefetch and
+ again madvise operation with same atomic attribute
+ * Test category: functionality test
+ *
+ * SUBTEST: madvise-range-invalidate-change-attr
+ * Description: performs atomic global madvise operation, prefetch and
+ again madvise operation with different atomic attribute
+ * Test category: functionality test
+ *
+ * SUBTEST: madvise-preffered-loc-atomic-und
+ * Description: Tests madvise with preferred location set for atomic
+ operations, but with an undefined
+ * Test category: functionality test
+ *
+ * SUBTEST: madvise-atomic-inc
+ * Description: Tests madvise atomic operations
+ * Test category: functionality test
*/

static void

tests/intel/xe_exec_system_allocator.c +2 -1
@@ -701,7 +769,8 @@ partial(int fd, struct drm_xe_engine_class_instance *eci,
 unsigned int flags)
 		uint64_t sdi_addr = addr + sdi_offset;
 		int b = 0;
 
-		write_dword(data[i].batch, sdi_addr, WRITE_VALUE(&data[i],
 i), &b);
+		write_dword((struct test_exec_data *)&data[i], sdi_addr,
 WRITE_VALUE(&data[i], i),
+			     &b, false);
tests/intel/xe_exec_system_allocator.c +14 -0
 		igt_assert(b <= ARRAY_SIZE(data[i].batch));
 
 		if (!i)
@@ -778,6 +847,20 @@ partial(int fd, struct drm_xe_engine_class_instance
 *eci, unsigned int flags)
 #define PREFETCH_BENCHMARK	(0x1 << 25)
 #define PREFETCH_SYS_BENCHMARK	(0x1 << 26)
 #define MADVISE_SWIZZLE			(0x1 << 27)
+#define MADVISE_OP			(0x1 << 28)
+#define ATOMIC_BATCH			(0x1 << 29)
+#define MIGRATE_ALL_PAGES		(0x1 << 30)
+#define PREFERRED_LOC_ATOMIC_DEVICE	(0x1ull << 31)
+#define PREFERRED_LOC_ATOMIC_GL		(0x1ull << 32)
+#define PREFERRED_LOC_ATOMIC_CPU	(0x1ull << 33)
+#define MADVISE_MULTI_VMA		(0x1ull << 34)
+#define MADVISE_SPLIT_VMA		(0x1ull << 35)
+#define MADVISE_ATOMIC_VMA		(0x1ull << 36)
+#define PREFETCH_SPLIT_VMA		(0x1ull << 37)
+#define PREFETCH_CHANGE_ATTR		(0x1ull << 38)
+#define PREFETCH_SAME_ATTR		(0x1ull << 39)
+#define PREFERRED_LOC_ATOMIC_UND	(0x1ull << 40)
+#define MADVISE_ATOMIC_DEVICE		(0x1ull << 41)
 
 #define N_MULTI_FAULT		4
@@ -886,7 +969,7 @@ partial(int fd, struct drm_xe_engine_class_instance *eci,
 unsigned int flags)
  * arg[1]:
  *
  * @malloc:				malloc single buffer for all execs, issue

a command which will trigger multiple faults
- * @malloc-madvise: malloc single buffer for all execs, issue
a command which will trigger multiple faults, perfoems madvise operation
+ * @malloc-madvise: malloc single buffer for all execs, issue
a command which will trigger multiple faults, performs madvise operation
* @malloc-prefetch: malloc single buffer for all execs,
prefetch buffer before each exec
* @malloc-prefetch-madvise: malloc single buffer for all execs,
prefetch buffer before each exec, performs madvise operation
* @malloc-multi-fault: malloc single buffer for all execs

tests/intel/xe_exec_system_allocator.c +0 -0
@@ -987,16 +1070,6 @@ partial(int fd, struct drm_xe_engine_class_instance
 *eci, unsigned int flags)
  * Description: Create multiple threads with a faults on different hardware

engines to same addresses, racing between CPU and GPU access
* Test category: stress test
*/

tests/intel/xe_exec_system_allocator.c +54 -10
-
-struct test_exec_data {
-	uint32_t batch[32];
-	uint64_t pad;
-	uint64_t vm_sync;
-	uint64_t exec_sync;
-	uint32_t data;
-	uint32_t expected_data;
-};
-
 static void igt_require_hugepages(void)  {
 	igt_skip_on_f(!igt_get_meminfo("HugePages_Total"),
@@ -1022,11 +1095,184 @@ madvise_swizzle_op_exec(int fd, uint32_t vm,
 struct test_exec_data *data,
 			0);
 }
 
+static void
+xe_vm_madvixe_pat_attr(int fd, uint32_t vm, uint64_t addr, uint64_t range,
+		int pat_index)
+{
+	xe_vm_madvise(fd, vm, addr, range, 0,
+			DRM_XE_MEM_RANGE_ATTR_PAT, pat_index, 0); }
+
+static void
+xe_vm_madvise_atomic_attr(int fd, uint32_t vm, uint64_t addr, uint64_t
 range,
+		int mem_attr)
+{
+	xe_vm_madvise(fd, vm, addr, range, 0,
+			DRM_XE_MEM_RANGE_ATTR_ATOMIC,
+			mem_attr, 0);
+}
+
+static void
+xe_vm_madvise_migrate_pages(int fd, uint32_t vm, uint64_t addr,
+uint64_t range) {
+	xe_vm_madvise(fd, vm, addr, range, 0,
+			DRM_XE_MEM_RANGE_ATTR_PREFERRED_LOC,
+			DRM_XE_PREFERRED_LOC_DEFAULT_SYSTEM,
+			DRM_XE_MIGRATE_ALL_PAGES);
+}
+
+static void
+xe_vm_parse_execute_madvise(int fd, uint32_t vm, struct test_exec_data
 *data,
+		size_t bo_size, struct drm_xe_engine_class_instance *eci,
+		uint64_t addr, unsigned long long flags,
+		struct drm_xe_sync *sync)
+{
+	uint32_t bo_flags, bo = 0;
+
+
+	if (flags & MADVISE_ATOMIC_DEVICE)
+		xe_vm_madvise_atomic_attr(fd, vm, to_user_pointer(data),
 bo_size,
+				DRM_XE_ATOMIC_DEVICE);
+
+	if (flags & PREFERRED_LOC_ATOMIC_UND) {
+		xe_vm_madvise_migrate_pages(fd, vm, to_user_pointer(data),
 bo_size);
+
+		xe_vm_madvise_atomic_attr(fd, vm, to_user_pointer(data),
 bo_size,
+				DRM_XE_ATOMIC_UNDEFINED);
+	}
+
+	if (flags & PREFERRED_LOC_ATOMIC_DEVICE) {
+		xe_vm_madvise_migrate_pages(fd, vm, to_user_pointer(data),
 bo_size);
+
+		xe_vm_madvise_atomic_attr(fd, vm, to_user_pointer(data),
 bo_size,
+				DRM_XE_ATOMIC_DEVICE);
+	}
+
+	if (flags & PREFERRED_LOC_ATOMIC_GL) {
+		xe_vm_madvise_migrate_pages(fd, vm, to_user_pointer(data),

bo_size);

tests/intel/xe_exec_system_allocator.c +2 -0
+
+		xe_vm_madvise_atomic_attr(fd, vm, to_user_pointer(data),

bo_size,

tests/intel/xe_exec_system_allocator.c +5 -0
+				DRM_XE_ATOMIC_GLOBAL);
+	}
+
+	if (flags & PREFERRED_LOC_ATOMIC_CPU) {
+		xe_vm_madvise_migrate_pages(fd, vm, to_user_pointer(data),

bo_size);

tests/intel/xe_exec_system_allocator.c +2 -0
+
+		xe_vm_madvise_atomic_attr(fd, vm, to_user_pointer(data),

bo_size,

tests/intel/xe_exec_system_allocator.c +7 -0
+				DRM_XE_ATOMIC_CPU);
+	}
+
+	if (flags & MADVISE_MULTI_VMA) {
+		if (bo_size)
+			bo_size = ALIGN(bo_size, SZ_4K);
+		xe_vm_madvise_atomic_attr(fd, vm, to_user_pointer(data) +

bo_size/2,

tests/intel/xe_exec_system_allocator.c +3 -0
+				bo_size/2, DRM_XE_ATOMIC_DEVICE);
+
+		xe_vm_madvixe_pat_attr(fd, vm, to_user_pointer(data) +

bo_size/2, bo_size/2,

tests/intel/xe_exec_system_allocator.c +3 -0
+				intel_get_pat_idx_wb(fd));
+
+		xe_vm_madvise_atomic_attr(fd, vm, to_user_pointer(data) +

bo_size,

tests/intel/xe_exec_system_allocator.c +3 -0
+				bo_size, DRM_XE_ATOMIC_DEVICE);
+
+		xe_vm_madvixe_pat_attr(fd, vm, to_user_pointer(data),

bo_size,

tests/intel/xe_exec_system_allocator.c +8 -0
+				intel_get_pat_idx_wb(fd));
+	}
+
+	if (flags & MADVISE_SPLIT_VMA) {
+		if (bo_size)
+			bo_size = ALIGN(bo_size, SZ_4K);
+
+		bo_flags =

DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM;

tests/intel/xe_exec_system_allocator.c +1 -0
+		bo = xe_bo_create(fd, vm, bo_size, vram_if_possible(fd, eci-
Quoted 1 line

gt_id),

tests/intel/xe_exec_system_allocator.c +2 -0
+				bo_flags);
+		xe_vm_bind_async(fd, vm, 0, bo, 0, to_user_pointer(data) +

bo_size/2,

tests/intel/xe_exec_system_allocator.c +3 -0
+				bo_size/2, 0, 0);
+
+		__xe_vm_bind_assert(fd, vm, 0, 0, 0, to_user_pointer(data) +

bo_size/2,

tests/intel/xe_exec_system_allocator.c +2 -0
+				bo_size/2, DRM_XE_VM_BIND_OP_MAP,
+

DRM_XE_VM_BIND_FLAG_CPU_ADDR_MIRROR, sync,

tests/intel/xe_exec_system_allocator.c +2 -0
+				1, 0, 0);
+		xe_wait_ufence(fd, &data[0].vm_sync, USER_FENCE_VALUE, 0,

FIVE_SEC);

tests/intel/xe_exec_system_allocator.c +6 -0
+		data[0].vm_sync = 0;
+		gem_close(fd, bo);
+		bo = 0;
+
+
+		xe_vm_madvise_atomic_attr(fd, vm, to_user_pointer(data),

bo_size/2,

tests/intel/xe_exec_system_allocator.c +15 -0
+				DRM_XE_ATOMIC_GLOBAL);
+	}
+}
+
+static void
+madvise_prefetch_op(int fd, uint32_t vm, uint64_t addr, size_t bo_size,
+		unsigned long long flags, struct test_exec_data *data) {
+	uint32_t val;
+
+	if (flags & PREFETCH_SPLIT_VMA) {
+		bo_size = ALIGN(bo_size, SZ_4K);
+
+		xe_vm_prefetch_async(fd, vm, 0, 0, addr, bo_size, NULL, 0, 0);
+
+		val = xe_vm_print_mem_attr_values_in_range(fd, vm, addr,

bo_size);

tests/intel/xe_exec_system_allocator.c +7 -0
+
+		igt_debug("num_vmas before madvise = %d\n", val);
+
+		xe_vm_madvise_migrate_pages(fd, vm, to_user_pointer(data),
+bo_size/2);
+
+		val = xe_vm_print_mem_attr_values_in_range(fd, vm, addr,

bo_size);

tests/intel/xe_exec_system_allocator.c +4 -0
+
+		igt_debug("num_vmas after madvise= %d\n", val);
+	} else if (flags & PREFETCH_SAME_ATTR) {
+		xe_vm_madvise_atomic_attr(fd, vm, to_user_pointer(data),

bo_size,

tests/intel/xe_exec_system_allocator.c +2 -0
+				DRM_XE_ATOMIC_GLOBAL);
+		val = xe_vm_print_mem_attr_values_in_range(fd, vm, addr,

bo_size);

tests/intel/xe_exec_system_allocator.c +5 -0
+
+		xe_vm_prefetch_async(fd, vm, 0, 0, addr, bo_size, NULL, 0,
+				DRM_XE_CONSULT_MEM_ADVISE_PREF_LOC);
+
+		xe_vm_madvise_atomic_attr(fd, vm, to_user_pointer(data),

bo_size/2,

tests/intel/xe_exec_system_allocator.c +3 -0
+				DRM_XE_ATOMIC_GLOBAL);
+	} else if (flags & PREFETCH_CHANGE_ATTR) {
+		xe_vm_madvise_atomic_attr(fd, vm, to_user_pointer(data),

bo_size,

tests/intel/xe_exec_system_allocator.c +3 -0
+				DRM_XE_ATOMIC_GLOBAL);
+
+		val = xe_vm_print_mem_attr_values_in_range(fd, vm, addr,

bo_size);

tests/intel/xe_exec_system_allocator.c +5 -0
+
+		xe_vm_prefetch_async(fd, vm, 0, 0, addr, bo_size, NULL, 0,
+				DRM_XE_CONSULT_MEM_ADVISE_PREF_LOC);
+
+		xe_vm_madvise_atomic_attr(fd, vm, to_user_pointer(data),

bo_size,

tests/intel/xe_exec_system_allocator.c +3 -0
+				DRM_XE_ATOMIC_DEVICE);
+
+		val = xe_vm_print_mem_attr_values_in_range(fd, vm, addr,

bo_size);

tests/intel/xe_exec_system_allocator.c +16 -0
+	}
+}
+
+static void
+madvise_vma_addr_map(uint64_t addr, int i, int idx, size_t bo_size,
+		     struct test_exec_data *data,
+		     uint64_t *batch_offset,
+		     uint64_t *batch_addr, uint64_t *sdi_offset,
+		     uint64_t *sdi_addr,
+		     unsigned long long flags,
+		     uint64_t *split_vma_offset)
+{
+	if (flags & MADVISE_MULTI_VMA) {
+		addr = addr + i * bo_size;
+		data = from_user_pointer(addr);
+		*batch_offset = (size_t)((char *)&(data[idx].batch) - (char

*)data);

tests/intel/xe_exec_system_allocator.c +8 -2
+		*batch_addr = addr + *batch_offset;
+		*sdi_offset = (size_t)((char *)&(data[idx].data) - (char *)data);
+		*sdi_addr = addr + *sdi_offset;
+	}
+}
+
 static void
 test_exec(int fd, struct drm_xe_engine_class_instance *eci,
 	  int n_exec_queues, int n_execs, size_t bo_size,
 	  size_t stride, uint32_t vm, void *alloc, pthread_barrier_t *barrier,
-	  unsigned int flags)
+	  unsigned long long flags)
 {
 	uint64_t addr;
 	struct drm_xe_sync sync[1] = {
@@ -1039,7 +1285,7 @@ test_exec(int fd, struct
 drm_xe_engine_class_instance *eci,
 		.syncs = to_user_pointer(sync),
 	};
 	uint32_t exec_queues[MAX_N_EXEC_QUEUES];
-	struct test_exec_data *data, *next_data = NULL;
+	struct test_exec_data *data, *next_data = NULL, *original_data =
 NULL;
 	uint32_t bo_flags;
tests/intel/xe_exec_system_allocator.c +4 -0
 	uint32_t bo = 0, bind_sync = 0;
 	void **pending_free;
@@ -1157,6 +1403,9 @@ test_exec(int fd, struct
 drm_xe_engine_class_instance *eci,
 
 	addr = to_user_pointer(data);
 
+	if (flags & MADVISE_OP)
+		xe_vm_parse_execute_madvise(fd, vm, data, bo_size, eci, addr,
 flags,
+sync);
+
 	if (flags & BO_UNMAP) {
 		bo_flags =

DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM;

tests/intel/xe_exec_system_allocator.c +25 -6
 		bo = xe_bo_create(fd, vm, bo_size,
@@ -1230,42 +1479,61 @@ test_exec(int fd, struct
 drm_xe_engine_class_instance *eci,
 		bool fault_inject = (FAULT & flags) && i == n_execs / 2;
 		bool fault_injected = (FAULT & flags) && i > n_execs;
 
+		uint64_t split_vma_offset;
+
 		if (barrier)
 			pthread_barrier_wait(barrier);
 
 		if (flags & MADVISE_SWIZZLE)
 			madvise_swizzle_op_exec(fd, vm, data, bo_size, addr,
 i);
 
+		if (flags & MADVISE_OP) {
+			if (flags & MADVISE_MULTI_VMA)
+				original_data = data;
+
+			madvise_vma_addr_map(addr, i, idx, bo_size, data,
 &batch_offset,
+					      &batch_addr, &sdi_offset,
 &sdi_addr, flags,
+					      &split_vma_offset);
+		}
+
 		if (flags & MULTI_FAULT) {
 			b = 0;
 			for (j = 0; j < N_MULTI_FAULT - 1; ++j)
 				__write_dword(data[idx].batch,
 					      sdi_addr + j * orig_size,
 					      WRITE_VALUE(&data[idx], idx),
 &b);
-			write_dword(data[idx].batch, sdi_addr + j * orig_size,
-				    WRITE_VALUE(&data[idx], idx), &b);
+			write_dword(&data[idx], sdi_addr + j * orig_size,
+				    WRITE_VALUE(&data[idx], idx), &b,
+				    flags & ATOMIC_BATCH ? true : false);
 			igt_assert(b <= ARRAY_SIZE(data[idx].batch));
 		} else if (!(flags & EVERY_OTHER_CHECK)) {
 			b = 0;
-			write_dword(data[idx].batch, sdi_addr,
-				    WRITE_VALUE(&data[idx], idx), &b);
+			write_dword(&data[idx], sdi_addr,
+					WRITE_VALUE(&data[idx], idx), &b,
+					flags & ATOMIC_BATCH ? true : false);
 			igt_assert(b <= ARRAY_SIZE(data[idx].batch));
+			if (flags & PREFETCH)
+				madvise_prefetch_op(fd, vm, addr, bo_size,
 flags, data);
 		} else if (flags & EVERY_OTHER_CHECK && !odd(i)) {
 			b = 0;
-			write_dword(data[idx].batch, sdi_addr,
-				    WRITE_VALUE(&data[idx], idx), &b);
+			write_dword(&data[idx], sdi_addr,
+				    WRITE_VALUE(&data[idx], idx), &b,
+				    flags & ATOMIC_BATCH ? true : false);
 			igt_assert(b <= ARRAY_SIZE(data[idx].batch));
 
 			aligned_alloc_type = __aligned_alloc(aligned_size,
 bo_size);
 			next_data = aligned_alloc_type.ptr;
 			igt_assert(next_data);
+
+			xe_vm_parse_execute_madvise(fd, vm, data, bo_size,
 eci, addr, flags,
+sync);
 			__aligned_partial_free(&aligned_alloc_type);
 
tests/intel/xe_exec_system_allocator.c +1 -2
 			b = 0;
-			write_dword(data[next_idx].batch,
+			write_dword(&data[next_idx],
 				    to_user_pointer(next_data) +
 				    (char *)&data[next_idx].data - (char *)data,
-				    WRITE_VALUE(&data[next_idx], next_idx),

&b);

tests/intel/xe_exec_system_allocator.c +1 -0
+				    WRITE_VALUE(&data[next_idx], next_idx),

&b,

tests/intel/xe_exec_system_allocator.c +1 -1
+				    flags & ATOMIC_BATCH ? true : false);
 			igt_assert(b <= ARRAY_SIZE(data[next_idx].batch));
 		}
@@ -1299,7 +1567,6 @@ test_exec(int fd, struct
 drm_xe_engine_class_instance *eci,
 			exec.address = batch_addr * 2;
 		else
 			exec.address = batch_addr;
-
 		if (fault_injected) {
 			err = __xe_exec(fd, &exec);
tests/intel/xe_exec_system_allocator.c +4 -3
 			igt_assert(err == -ENOENT);
@@ -1319,9 +1586,19 @@ test_exec(int fd, struct
 drm_xe_engine_class_instance *eci,
 					       exec_queues[e], &timeout);
 			igt_assert(err == -ETIME || err == -EIO);
 		} else {
-			xe_wait_ufence(fd, exec_ufence ? exec_ufence :
-				       &data[idx].exec_sync,
 USER_FENCE_VALUE,
-				       exec_queues[e], FIVE_SEC);
+			if (flags & PREFERRED_LOC_ATOMIC_CPU || flags &
 PREFERRED_LOC_ATOMIC_UND) {
+				int64_t timeout = QUARTER_SEC;
+
+				err = __xe_wait_ufence(fd, exec_ufence ?

exec_ufence :

tests/intel/xe_exec_system_allocator.c +3 -0
+						       &data[idx].exec_sync,
+						       USER_FENCE_VALUE,
+						       exec_queues[e],

&timeout);

tests/intel/xe_exec_system_allocator.c +4 -0
+				if (err)
+					goto cleanup;
+			} else
+				xe_wait_ufence(fd, exec_ufence ? exec_ufence

:

tests/intel/xe_exec_system_allocator.c +1 -0
+					       &data[idx].exec_sync,

USER_FENCE_VALUE,

tests/intel/xe_exec_system_allocator.c +2 -1
+					       exec_queues[e], FIVE_SEC);
 			if (flags & LOCK && !i)
 				munlock(data, bo_size);
@@ -1371,17 +1648,25 @@ test_exec(int fd, struct
 drm_xe_engine_class_instance *eci,
 				if (flags & FORK_READ) {
 					igt_fork(child, 1)
 						igt_assert_eq(data[idx].data,
-
 READ_VALUE(&data[idx]));
+							      flags &

ATOMIC_BATCH

tests/intel/xe_exec_system_allocator.c +1 -0
+							      ?

VAL_ATOMIC_EXPECTED

tests/intel/xe_exec_system_allocator.c +1 -0
+							      :

READ_VALUE(&data[idx]));

tests/intel/xe_exec_system_allocator.c +0 -1
 					if (!(flags & FORK_READ_AFTER))
 						igt_assert_eq(data[idx].data,
-

READ_VALUE(&data[idx]));

tests/intel/xe_exec_system_allocator.c +1 -0
+							      flags &

ATOMIC_BATCH

tests/intel/xe_exec_system_allocator.c +1 -0
+							      ?

VAL_ATOMIC_EXPECTED

tests/intel/xe_exec_system_allocator.c +1 -0
+							      :

READ_VALUE(&data[idx]));

tests/intel/xe_exec_system_allocator.c +0 -1
 					igt_waitchildren();
 					if (flags & FORK_READ_AFTER)
 						igt_assert_eq(data[idx].data,
-

READ_VALUE(&data[idx]));

tests/intel/xe_exec_system_allocator.c +1 -0
+							      flags &

ATOMIC_BATCH

tests/intel/xe_exec_system_allocator.c +1 -0
+							      ?

VAL_ATOMIC_EXPECTED

tests/intel/xe_exec_system_allocator.c +1 -0
+							      :

READ_VALUE(&data[idx]));

tests/intel/xe_exec_system_allocator.c +3 -1
 				} else {
 					igt_assert_eq(data[idx].data,
-						      READ_VALUE(&data[idx]));
+						      flags & ATOMIC_BATCH
+						      ? VAL_ATOMIC_EXPECTED
+						      :

READ_VALUE(&data[idx]));
if (flags &
PREFETCH_SYS_BENCHMARK) {

tests/intel/xe_exec_system_allocator.c +2 -1
 						struct timespec tv = {};
 						u64 start, end;
@@ -1408,13 +1693,17 @@ test_exec(int fd, struct
 drm_xe_engine_class_instance *eci,
 								((void *)data)
+ j * orig_size;
 
 
 	igt_assert_eq(__data[idx].data,
-
 READ_VALUE(&data[idx]));
+								      flags &

ATOMIC_BATCH

tests/intel/xe_exec_system_allocator.c +1 -0
+								      ?

VAL_ATOMIC_EXPECTED

tests/intel/xe_exec_system_allocator.c +1 -0
+								      :

READ_VALUE(&data[idx]));

tests/intel/xe_exec_system_allocator.c +0 -1
 						}
 					}
 				}
 				if (flags & EVERY_OTHER_CHECK)
 					igt_assert_eq(data[prev_idx].data,
-

READ_VALUE(&data[prev_idx]));

tests/intel/xe_exec_system_allocator.c +3 -0
+						      flags & ATOMIC_BATCH
+						      ? VAL_ATOMIC_EXPECTED
+						      :

READ_VALUE(&data[prev_idx]));

tests/intel/xe_exec_system_allocator.c +5 -0
 			}
 		}
@@ -1435,6 +1724,11 @@ test_exec(int fd, struct
 drm_xe_engine_class_instance *eci,
 			gem_close(fd, bo);
 		}
 
+		if (flags & MADVISE_MULTI_VMA) {
+			data = original_data;
+			original_data = NULL;
+		}
+
 		if (flags & NEW) {
 			if (flags & MMAP) {
tests/intel/xe_exec_system_allocator.c +1 -0
 				if (flags & FREE)
@@ -1513,6 +1807,7 @@ test_exec(int fd, struct
 drm_xe_engine_class_instance *eci,
 				 pf_count, pf_count_after);
 	}
 
+cleanup:
 	if (bo) {
 		sync[0].addr = to_user_pointer(bind_ufence);
tests/intel/xe_exec_system_allocator.c +1 -1
 		__xe_vm_bind_assert(fd, vm, 0,
@@ -1808,7 +2103,7 @@ test_compute(int fd, struct
 drm_xe_engine_class_instance *eci, size_t size)
 
 struct section {
 	const char *name;
-	unsigned int flags;
+	unsigned long long flags;
 };
 

igt_main

tests/intel/xe_exec_system_allocator.c +8 -0
@@ -1914,6 +2209,32 @@ igt_main
 		{ "malloc-mix-bo", MIX_BO_ALLOC },
 		{ NULL },
 	};
+	const struct section msections[] = {
+		{ "atomic-inc", MADVISE_OP | MADVISE_ATOMIC_DEVICE |
 ATOMIC_BATCH },
+		{ "preffered-loc-sram-migrate-pages",
+		  MADVISE_OP | MADVISE_SWIZZLE | MIGRATE_ALL_PAGES |
 ATOMIC_BATCH },
+		{ "preffered-loc-atomic-vram",
+		  MADVISE_OP | PREFERRED_LOC_ATOMIC_DEVICE |
 ATOMIC_BATCH },
+		{ "preffered-loc-atomic-gl",
+		  MADVISE_OP | PREFERRED_LOC_ATOMIC_GL |

ATOMIC_BATCH },

tests/intel/xe_exec_system_allocator.c +2 -0
+		{ "preffered-loc-atomic-cpu",
+		  MADVISE_OP | PREFERRED_LOC_ATOMIC_CPU |

ATOMIC_BATCH },

tests/intel/xe_exec_system_allocator.c +2 -0
+		{ "preffered-loc-atomic-und",
+		  MADVISE_OP | PREFERRED_LOC_ATOMIC_UND |

ATOMIC_BATCH },

tests/intel/xe_exec_system_allocator.c +8 -0
+		{ "multi-vma",
+		  MADVISE_OP | MADVISE_MULTI_VMA | ATOMIC_BATCH },
+		{ "split-vma",
+		  MADVISE_OP | MADVISE_SPLIT_VMA | ATOMIC_BATCH },
+		{ "atomic-vma",
+		  MADVISE_OP | MADVISE_ATOMIC_VMA | ATOMIC_BATCH },
+		{ "split-vma-with-mapping",
+		  MADVISE_OP | PREFETCH | PREFETCH_SPLIT_VMA |

ATOMIC_BATCH },

tests/intel/xe_exec_system_allocator.c +2 -0
+		{ "range-invalidate-change-attr",
+		  MADVISE_OP | PREFETCH | PREFETCH_CHANGE_ATTR |

ATOMIC_BATCH },

tests/intel/xe_exec_system_allocator.c +2 -0
+		{ "no-range-invalidate-same-attr",
+		  MADVISE_OP | PREFETCH | PREFETCH_SAME_ATTR |

ATOMIC_BATCH },

tests/intel/xe_exec_system_allocator.c +2 -0
+		{ NULL },
+	};
 	int fd;
 
 	igt_fixture {
@@ -2110,6 +2431,14 @@ igt_main
 			processes_evict(fd, SZ_8M, SZ_1M, s->flags);

tests/intel/xe_exec_system_allocator.c igt_main · line 2441

Quoted 10 lines
tests/intel/xe_exec_system_allocator.c +8 -0
 	}
 
+	for (const struct section *s = msections; s->name; s++) {
+		igt_subtest_f("madvise-%s", s->name) {
+			xe_for_each_engine(fd, hwe)
+				test_exec(fd, hwe, 1, 1, SZ_64K, 0, 0, NULL,
+					  NULL, s->flags);
+		}
+	}
+

Looks good to me, I let others to comment
--Pravalika

Quoted 5 lines
tests/intel/xe_exec_system_allocator.c +0 -1
 	igt_subtest("compute")
 		xe_for_each_engine(fd, hwe)
 			test_compute(fd, hwe, SZ_2M);
--

2.43.0