Skip to content

Commit 4188078

Browse files
committed
drm/xe/uapi: Add UAPI for querying VMA count and memory attributes
Introduce the DRM_IOCTL_XE_VM_QUERY_MEMORY_RANGE_ATTRS ioctl to allow userspace to query memory attributes of VMAs within a user specified virtual address range. 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 ranges (vmas) 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. This two-step interface allows userspace to first query the required buffer size, then retrieve detailed attributes efficiently. v2 (Matthew Brost) - Use same ioctl to overload functionality v3 - Add kernel-doc v4 - Make uapi future proof by passing struct size (Matthew Brost) - make lock interruptible (Matthew Brost) - set reserved bits to zero (Matthew Brost) - s/__copy_to_user/copy_to_user (Matthew Brost) - Avod using VMA term in uapi (Thomas) - xe_vm_put(vm) is missing (Shuicheng) v5 - Nits - Fix kernel-doc Cc: Matthew Brost <matthew.brost@intel.com> Cc: Shuicheng Lin <shuicheng.lin@intel.com> Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com> Reviewed-by: Matthew Brost <matthew.brost@intel.com> Link: https://lore.kernel.org/r/20250821173104.3030148-21-himal.prasad.ghimiray@intel.com Signed-off-by: Himal Prasad Ghimiray <himal.prasad.ghimiray@intel.com>
1 parent e80b05b commit 4188078

File tree

4 files changed

+245
-1
lines changed

4 files changed

+245
-1
lines changed

drivers/gpu/drm/xe/xe_device.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ static const struct drm_ioctl_desc xe_ioctls[] = {
204204
DRM_RENDER_ALLOW),
205205
DRM_IOCTL_DEF_DRV(XE_OBSERVATION, xe_observation_ioctl, DRM_RENDER_ALLOW),
206206
DRM_IOCTL_DEF_DRV(XE_MADVISE, xe_vm_madvise_ioctl, DRM_RENDER_ALLOW),
207+
DRM_IOCTL_DEF_DRV(XE_VM_QUERY_MEM_RANGE_ATTRS, xe_vm_query_vmas_attrs_ioctl,
208+
DRM_RENDER_ALLOW),
207209
};
208210

209211
static long xe_drm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)

drivers/gpu/drm/xe/xe_vm.c

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,6 +2196,108 @@ int xe_vm_destroy_ioctl(struct drm_device *dev, void *data,
21962196
return err;
21972197
}
21982198

2199+
static int xe_vm_query_vmas(struct xe_vm *vm, u64 start, u64 end)
2200+
{
2201+
struct drm_gpuva *gpuva;
2202+
u32 num_vmas = 0;
2203+
2204+
lockdep_assert_held(&vm->lock);
2205+
drm_gpuvm_for_each_va_range(gpuva, &vm->gpuvm, start, end)
2206+
num_vmas++;
2207+
2208+
return num_vmas;
2209+
}
2210+
2211+
static int get_mem_attrs(struct xe_vm *vm, u32 *num_vmas, u64 start,
2212+
u64 end, struct drm_xe_mem_range_attr *attrs)
2213+
{
2214+
struct drm_gpuva *gpuva;
2215+
int i = 0;
2216+
2217+
lockdep_assert_held(&vm->lock);
2218+
2219+
drm_gpuvm_for_each_va_range(gpuva, &vm->gpuvm, start, end) {
2220+
struct xe_vma *vma = gpuva_to_vma(gpuva);
2221+
2222+
if (i == *num_vmas)
2223+
return -ENOSPC;
2224+
2225+
attrs[i].start = xe_vma_start(vma);
2226+
attrs[i].end = xe_vma_end(vma);
2227+
attrs[i].atomic.val = vma->attr.atomic_access;
2228+
attrs[i].pat_index.val = vma->attr.pat_index;
2229+
attrs[i].preferred_mem_loc.devmem_fd = vma->attr.preferred_loc.devmem_fd;
2230+
attrs[i].preferred_mem_loc.migration_policy =
2231+
vma->attr.preferred_loc.migration_policy;
2232+
2233+
i++;
2234+
}
2235+
2236+
*num_vmas = i;
2237+
return 0;
2238+
}
2239+
2240+
int xe_vm_query_vmas_attrs_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
2241+
{
2242+
struct xe_device *xe = to_xe_device(dev);
2243+
struct xe_file *xef = to_xe_file(file);
2244+
struct drm_xe_mem_range_attr *mem_attrs;
2245+
struct drm_xe_vm_query_mem_range_attr *args = data;
2246+
u64 __user *attrs_user = u64_to_user_ptr(args->vector_of_mem_attr);
2247+
struct xe_vm *vm;
2248+
int err = 0;
2249+
2250+
if (XE_IOCTL_DBG(xe,
2251+
((args->num_mem_ranges == 0 &&
2252+
(attrs_user || args->sizeof_mem_range_attr != 0)) ||
2253+
(args->num_mem_ranges > 0 &&
2254+
(!attrs_user ||
2255+
args->sizeof_mem_range_attr !=
2256+
sizeof(struct drm_xe_mem_range_attr))))))
2257+
return -EINVAL;
2258+
2259+
vm = xe_vm_lookup(xef, args->vm_id);
2260+
if (XE_IOCTL_DBG(xe, !vm))
2261+
return -EINVAL;
2262+
2263+
err = down_read_interruptible(&vm->lock);
2264+
if (err)
2265+
goto put_vm;
2266+
2267+
attrs_user = u64_to_user_ptr(args->vector_of_mem_attr);
2268+
2269+
if (args->num_mem_ranges == 0 && !attrs_user) {
2270+
args->num_mem_ranges = xe_vm_query_vmas(vm, args->start, args->start + args->range);
2271+
args->sizeof_mem_range_attr = sizeof(struct drm_xe_mem_range_attr);
2272+
goto unlock_vm;
2273+
}
2274+
2275+
mem_attrs = kvmalloc_array(args->num_mem_ranges, args->sizeof_mem_range_attr,
2276+
GFP_KERNEL | __GFP_ACCOUNT |
2277+
__GFP_RETRY_MAYFAIL | __GFP_NOWARN);
2278+
if (!mem_attrs) {
2279+
err = args->num_mem_ranges > 1 ? -ENOBUFS : -ENOMEM;
2280+
goto unlock_vm;
2281+
}
2282+
2283+
memset(mem_attrs, 0, args->num_mem_ranges * args->sizeof_mem_range_attr);
2284+
err = get_mem_attrs(vm, &args->num_mem_ranges, args->start,
2285+
args->start + args->range, mem_attrs);
2286+
if (err)
2287+
goto free_mem_attrs;
2288+
2289+
err = copy_to_user(attrs_user, mem_attrs,
2290+
args->sizeof_mem_range_attr * args->num_mem_ranges);
2291+
2292+
free_mem_attrs:
2293+
kvfree(mem_attrs);
2294+
unlock_vm:
2295+
up_read(&vm->lock);
2296+
put_vm:
2297+
xe_vm_put(vm);
2298+
return err;
2299+
}
2300+
21992301
static bool vma_matches(struct xe_vma *vma, u64 page_addr)
22002302
{
22012303
if (page_addr > xe_vma_end(vma) - 1 ||

drivers/gpu/drm/xe/xe_vm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ int xe_vm_destroy_ioctl(struct drm_device *dev, void *data,
199199
struct drm_file *file);
200200
int xe_vm_bind_ioctl(struct drm_device *dev, void *data,
201201
struct drm_file *file);
202-
202+
int xe_vm_query_vmas_attrs_ioctl(struct drm_device *dev, void *data, struct drm_file *file);
203203
void xe_vm_close_and_put(struct xe_vm *vm);
204204

205205
static inline bool xe_vm_in_fault_mode(struct xe_vm *vm)

include/uapi/drm/xe_drm.h

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ extern "C" {
8282
* - &DRM_IOCTL_XE_WAIT_USER_FENCE
8383
* - &DRM_IOCTL_XE_OBSERVATION
8484
* - &DRM_IOCTL_XE_MADVISE
85+
* - &DRM_IOCTL_XE_VM_QUERY_MEM_RANGE_ATTRS
8586
*/
8687

8788
/*
@@ -104,6 +105,7 @@ extern "C" {
104105
#define DRM_XE_WAIT_USER_FENCE 0x0a
105106
#define DRM_XE_OBSERVATION 0x0b
106107
#define DRM_XE_MADVISE 0x0c
108+
#define DRM_XE_VM_QUERY_MEM_RANGE_ATTRS 0x0d
107109

108110
/* Must be kept compact -- no holes */
109111

@@ -120,6 +122,7 @@ extern "C" {
120122
#define DRM_IOCTL_XE_WAIT_USER_FENCE DRM_IOWR(DRM_COMMAND_BASE + DRM_XE_WAIT_USER_FENCE, struct drm_xe_wait_user_fence)
121123
#define DRM_IOCTL_XE_OBSERVATION DRM_IOW(DRM_COMMAND_BASE + DRM_XE_OBSERVATION, struct drm_xe_observation_param)
122124
#define DRM_IOCTL_XE_MADVISE DRM_IOW(DRM_COMMAND_BASE + DRM_XE_MADVISE, struct drm_xe_madvise)
125+
#define DRM_IOCTL_XE_VM_QUERY_MEM_RANGE_ATTRS DRM_IOWR(DRM_COMMAND_BASE + DRM_XE_VM_QUERY_MEM_RANGE_ATTRS, struct drm_xe_vm_query_mem_range_attr)
123126

124127
/**
125128
* DOC: Xe IOCTL Extensions
@@ -2113,6 +2116,143 @@ struct drm_xe_madvise {
21132116
__u64 reserved[2];
21142117
};
21152118

2119+
/**
2120+
* struct drm_xe_mem_range_attr - Output of &DRM_IOCTL_XE_VM_QUERY_MEM_RANGES_ATTRS
2121+
*
2122+
* This structure is provided by userspace and filled by KMD in response to the
2123+
* DRM_IOCTL_XE_VM_QUERY_MEM_RANGES_ATTRS ioctl. It describes memory attributes of
2124+
* a memory ranges within a user specified address range in a VM.
2125+
*
2126+
* The structure includes information such as atomic access policy,
2127+
* page attribute table (PAT) index, and preferred memory location.
2128+
* Userspace allocates an array of these structures and passes a pointer to the
2129+
* ioctl to retrieve attributes for each memory ranges
2130+
*
2131+
* @extensions: Pointer to the first extension struct, if any
2132+
* @start: Start address of the memory range
2133+
* @end: End address of the virtual memory range
2134+
*
2135+
*/
2136+
struct drm_xe_mem_range_attr {
2137+
/** @extensions: Pointer to the first extension struct, if any */
2138+
__u64 extensions;
2139+
2140+
/** @start: start of the memory range */
2141+
__u64 start;
2142+
2143+
/** @end: end of the memory range */
2144+
__u64 end;
2145+
2146+
/** @preferred_mem_loc: preferred memory location */
2147+
struct {
2148+
/** @preferred_mem_loc.devmem_fd: fd for preferred loc */
2149+
__u32 devmem_fd;
2150+
2151+
/** @preferred_mem_loc.migration_policy: Page migration policy */
2152+
__u32 migration_policy;
2153+
} preferred_mem_loc;
2154+
2155+
/** @atomic: Atomic access policy */
2156+
struct {
2157+
/** @atomic.val: atomic attribute */
2158+
__u32 val;
2159+
2160+
/** @atomic.reserved: Reserved */
2161+
__u32 reserved;
2162+
} atomic;
2163+
2164+
/** @pat_index: Page attribute table index */
2165+
struct {
2166+
/** @pat_index.val: PAT index */
2167+
__u32 val;
2168+
2169+
/** @pat_index.reserved: Reserved */
2170+
__u32 reserved;
2171+
} pat_index;
2172+
2173+
/** @reserved: Reserved */
2174+
__u64 reserved[2];
2175+
};
2176+
2177+
/**
2178+
* struct drm_xe_vm_query_mem_range_attr - Input of &DRM_IOCTL_XE_VM_QUERY_MEM_ATTRIBUTES
2179+
*
2180+
* This structure is used to query memory attributes of memory regions
2181+
* within a user specified address range in a VM. It provides detailed
2182+
* information about each memory range, including atomic access policy,
2183+
* page attribute table (PAT) index, and preferred memory location.
2184+
*
2185+
* Userspace first calls the ioctl with @num_mem_ranges = 0,
2186+
* @sizeof_mem_ranges_attr = 0 and @vector_of_vma_mem_attr = NULL to retrieve
2187+
* the number of memory regions and size of each memory range attribute.
2188+
* Then, it allocates a buffer of that size and calls the ioctl again to fill
2189+
* the buffer with memory range attributes.
2190+
*
2191+
* If second call fails with -ENOSPC, it means memory ranges changed between
2192+
* first call and now, retry IOCTL again with @num_mem_ranges = 0,
2193+
* @sizeof_mem_ranges_attr = 0 and @vector_of_vma_mem_attr = NULL followed by
2194+
* Second ioctl call.
2195+
*
2196+
* Example:
2197+
*
2198+
* .. code-block:: C
2199+
* struct drm_xe_vm_query_mem_range_attr query = {
2200+
* .vm_id = vm_id,
2201+
* .start = 0x100000,
2202+
* .range = 0x2000,
2203+
* };
2204+
*
2205+
* // First ioctl call to get num of mem regions and sizeof each attribute
2206+
* ioctl(fd, DRM_IOCTL_XE_VM_QUERY_MEM_RANGE_ATTRS, &query);
2207+
*
2208+
* // Allocate buffer for the memory region attributes
2209+
* void *ptr = malloc(query.num_mem_ranges * query.sizeof_mem_range_attr);
2210+
* void *ptr_start = ptr;
2211+
*
2212+
* query.vector_of_mem_attr = (uintptr_t)ptr;
2213+
*
2214+
* // Second ioctl call to actually fill the memory attributes
2215+
* ioctl(fd, DRM_IOCTL_XE_VM_QUERY_MEM_RANGE_ATTRS, &query);
2216+
*
2217+
* // Iterate over the returned memory region attributes
2218+
* for (unsigned int i = 0; i < query.num_mem_ranges; ++i) {
2219+
* struct drm_xe_mem_range_attr *attr = (struct drm_xe_mem_range_attr *)ptr;
2220+
*
2221+
* // Do something with attr
2222+
*
2223+
* // Move pointer by one entry
2224+
* ptr += query.sizeof_mem_range_attr;
2225+
* }
2226+
*
2227+
* free(ptr_start);
2228+
*/
2229+
struct drm_xe_vm_query_mem_range_attr {
2230+
/** @extensions: Pointer to the first extension struct, if any */
2231+
__u64 extensions;
2232+
2233+
/** @vm_id: vm_id of the virtual range */
2234+
__u32 vm_id;
2235+
2236+
/** @num_mem_ranges: number of mem_ranges in range */
2237+
__u32 num_mem_ranges;
2238+
2239+
/** @start: start of the virtual address range */
2240+
__u64 start;
2241+
2242+
/** @range: size of the virtual address range */
2243+
__u64 range;
2244+
2245+
/** @sizeof_mem_range_attr: size of struct drm_xe_mem_range_attr */
2246+
__u64 sizeof_mem_range_attr;
2247+
2248+
/** @vector_of_mem_attr: userptr to array of struct drm_xe_mem_range_attr */
2249+
__u64 vector_of_mem_attr;
2250+
2251+
/** @reserved: Reserved */
2252+
__u64 reserved[2];
2253+
2254+
};
2255+
21162256
#if defined(__cplusplus)
21172257
}
21182258
#endif

0 commit comments

Comments
 (0)