public inbox for igt-dev@lists.freedesktop.org
 help / color / mirror / Atom feed
From: Arvind Yadav <arvind.yadav@intel.com>
To: igt-dev@lists.freedesktop.org
Cc: matthew.brost@intel.com, himal.prasad.ghimiray@intel.com,
	thomas.hellstrom@linux.intel.com, nishit.sharma@intel.com,
	pravalika.gurram@intel.com
Subject: [PATCH i-g-t v7 2/9] lib/xe: Add purgeable memory ioctl support
Date: Thu,  9 Apr 2026 12:31:11 +0530	[thread overview]
Message-ID: <20260409070118.2211602-3-arvind.yadav@intel.com> (raw)
In-Reply-To: <20260409070118.2211602-1-arvind.yadav@intel.com>

Add 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)

Cc: Matthew Brost <matthew.brost@intel.com>
Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Cc: Himal Prasad Ghimiray <himal.prasad.ghimiray@intel.com>
Cc: Pravalika Gurram <pravalika.gurram@intel.com>
Reviewed-by: Nishit Sharma <nishit.sharma@intel.com>
Signed-off-by: Arvind Yadav <arvind.yadav@intel.com>
---
 lib/xe/xe_ioctl.c | 33 +++++++++++++++++++++++++++++++++
 lib/xe/xe_ioctl.h |  2 ++
 2 files changed, 35 insertions(+)

diff --git a/lib/xe/xe_ioctl.c b/lib/xe/xe_ioctl.c
index ea3f2fcaa..955607f93 100644
--- a/lib/xe/xe_ioctl.c
+++ b/lib/xe/xe_ioctl.c
@@ -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	0x686868
 void 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)
diff --git a/lib/xe/xe_ioctl.h b/lib/xe/xe_ioctl.h
index b62d259fd..a02d68cfe 100644
--- a/lib/xe/xe_ioctl.h
+++ b/lib/xe/xe_ioctl.h
@@ -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);
-- 
2.43.0


  parent reply	other threads:[~2026-04-09  7:02 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-09  7:01 [PATCH i-g-t v7 0/9] tests/xe: Add purgeable memory madvise tests for system allocator Arvind Yadav
2026-04-09  7:01 ` [PATCH i-g-t v7 1/9] drm-uapi/xe_drm: Sync with Add UAPI support for purgeable buffer objects Arvind Yadav
2026-04-09 15:42   ` Kamil Konieczny
2026-04-10  5:35     ` Yadav, Arvind
2026-04-09  7:01 ` Arvind Yadav [this message]
2026-04-09  7:01 ` [PATCH i-g-t v7 3/9] tests/intel/xe_madvise: Add dontneed-before-mmap subtest Arvind Yadav
2026-04-10  7:10   ` Sharma, Nishit
2026-04-09  7:01 ` [PATCH i-g-t v7 4/9] tests/intel/xe_madvise: Add purged-mmap-blocked subtest Arvind Yadav
2026-04-10  8:06   ` Sharma, Nishit
2026-04-09  7:01 ` [PATCH i-g-t v7 5/9] tests/intel/xe_madvise: Add dontneed-after-mmap subtest Arvind Yadav
2026-04-10  8:22   ` Sharma, Nishit
2026-04-10  8:59     ` Yadav, Arvind
2026-04-09  7:01 ` [PATCH i-g-t v7 6/9] tests/intel/xe_madvise: Add dontneed-before-exec subtest Arvind Yadav
2026-04-10  8:32   ` Sharma, Nishit
2026-04-09  7:01 ` [PATCH i-g-t v7 7/9] tests/intel/xe_madvise: Add dontneed-after-exec subtest Arvind Yadav
2026-04-09  7:01 ` [PATCH i-g-t v7 8/9] tests/intel/xe_madvise: Add per-vma-tracking subtest Arvind Yadav
2026-04-10  8:41   ` Sharma, Nishit
2026-04-09  7:01 ` [PATCH i-g-t v7 9/9] tests/intel/xe_madvise: Add per-vma-protection subtest Arvind Yadav
2026-04-10  1:08 ` ✗ Fi.CI.BUILD: failure for tests/xe: Add purgeable memory madvise tests for system allocator (rev7) Patchwork

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260409070118.2211602-3-arvind.yadav@intel.com \
    --to=arvind.yadav@intel.com \
    --cc=himal.prasad.ghimiray@intel.com \
    --cc=igt-dev@lists.freedesktop.org \
    --cc=matthew.brost@intel.com \
    --cc=nishit.sharma@intel.com \
    --cc=pravalika.gurram@intel.com \
    --cc=thomas.hellstrom@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox