Skip to content

Commit ce831bf

Browse files
Thomas Hellströmlucasdemarchi
authored andcommitted
drm/xe/uapi: Hide the madvise autoreset behind a VM_BIND flag
The madvise implementation currently resets the SVM madvise if the underlying CPU map is unmapped. This is in an attempt to mimic the CPU madvise behaviour. However, it's not clear that this is a desired behaviour since if the end app user relies on it for malloc()ed objects or stack objects, it may not work as intended. Instead of having the autoreset functionality being a direct application-facing implicit UAPI, make the UMD explicitly choose this behaviour if it wants to expose it by introducing DRM_XE_VM_BIND_FLAG_MADVISE_AUTORESET, and add a semantics description. v2: - Kerneldoc fixes. Fix a commit log message. Fixes: a2eb8ae ("drm/xe: Reset VMA attributes to default in SVM garbage collector") Cc: Matthew Brost <[email protected]> Cc: Himal Prasad Ghimiray <[email protected]> Cc: "Falkowski, John" <[email protected]> Cc: "Mrozek, Michal" <[email protected]> Signed-off-by: Thomas Hellström <[email protected]> Reviewed-by: Himal Prasad Ghimiray <[email protected]> Link: https://lore.kernel.org/r/[email protected] (cherry picked from commit 59a2d3f38ab23cce4cd9f0c4a5e08fdfe9e67ae7) Signed-off-by: Lucas De Marchi <[email protected]>
1 parent 9a3c0d6 commit ce831bf

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

drivers/gpu/drm/xe/xe_svm.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,11 @@ static int xe_svm_range_set_default_attr(struct xe_vm *vm, u64 range_start, u64
302302
if (!vma)
303303
return -EINVAL;
304304

305+
if (!(vma->gpuva.flags & XE_VMA_MADV_AUTORESET)) {
306+
drm_dbg(&vm->xe->drm, "Skipping madvise reset for vma.\n");
307+
return 0;
308+
}
309+
305310
if (xe_vma_has_default_mem_attrs(vma))
306311
return 0;
307312

drivers/gpu/drm/xe/xe_vm.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,8 @@ static void xe_vma_ops_incr_pt_update_ops(struct xe_vma_ops *vops, u8 tile_mask,
620620
XE_VMA_READ_ONLY | \
621621
XE_VMA_DUMPABLE | \
622622
XE_VMA_SYSTEM_ALLOCATOR | \
623-
DRM_GPUVA_SPARSE)
623+
DRM_GPUVA_SPARSE | \
624+
XE_VMA_MADV_AUTORESET)
624625

625626
static void xe_vm_populate_rebind(struct xe_vma_op *op, struct xe_vma *vma,
626627
u8 tile_mask)
@@ -2270,6 +2271,8 @@ vm_bind_ioctl_ops_create(struct xe_vm *vm, struct xe_vma_ops *vops,
22702271
op->map.vma_flags |= XE_VMA_SYSTEM_ALLOCATOR;
22712272
if (flags & DRM_XE_VM_BIND_FLAG_DUMPABLE)
22722273
op->map.vma_flags |= XE_VMA_DUMPABLE;
2274+
if (flags & DRM_XE_VM_BIND_FLAG_MADVISE_AUTORESET)
2275+
op->map.vma_flags |= XE_VMA_MADV_AUTORESET;
22732276
op->map.pat_index = pat_index;
22742277
op->map.invalidate_on_bind =
22752278
__xe_vm_needs_clear_scratch_pages(vm, flags);
@@ -3253,7 +3256,8 @@ ALLOW_ERROR_INJECTION(vm_bind_ioctl_ops_execute, ERRNO);
32533256
DRM_XE_VM_BIND_FLAG_NULL | \
32543257
DRM_XE_VM_BIND_FLAG_DUMPABLE | \
32553258
DRM_XE_VM_BIND_FLAG_CHECK_PXP | \
3256-
DRM_XE_VM_BIND_FLAG_CPU_ADDR_MIRROR)
3259+
DRM_XE_VM_BIND_FLAG_CPU_ADDR_MIRROR | \
3260+
DRM_XE_VM_BIND_FLAG_MADVISE_AUTORESET)
32573261

32583262
#ifdef TEST_VM_OPS_ERROR
32593263
#define SUPPORTED_FLAGS (SUPPORTED_FLAGS_STUB | FORCE_OP_ERROR)
@@ -3368,7 +3372,9 @@ static int vm_bind_ioctl_check_args(struct xe_device *xe, struct xe_vm *vm,
33683372
XE_IOCTL_DBG(xe, (prefetch_region != DRM_XE_CONSULT_MEM_ADVISE_PREF_LOC &&
33693373
!(BIT(prefetch_region) & xe->info.mem_region_mask))) ||
33703374
XE_IOCTL_DBG(xe, obj &&
3371-
op == DRM_XE_VM_BIND_OP_UNMAP)) {
3375+
op == DRM_XE_VM_BIND_OP_UNMAP) ||
3376+
XE_IOCTL_DBG(xe, (flags & DRM_XE_VM_BIND_FLAG_MADVISE_AUTORESET) &&
3377+
(!is_cpu_addr_mirror || op != DRM_XE_VM_BIND_OP_MAP))) {
33723378
err = -EINVAL;
33733379
goto free_bind_ops;
33743380
}

drivers/gpu/drm/xe/xe_vm_types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ struct xe_vm_pgtable_update_op;
4646
#define XE_VMA_PTE_COMPACT (DRM_GPUVA_USERBITS << 7)
4747
#define XE_VMA_DUMPABLE (DRM_GPUVA_USERBITS << 8)
4848
#define XE_VMA_SYSTEM_ALLOCATOR (DRM_GPUVA_USERBITS << 9)
49+
#define XE_VMA_MADV_AUTORESET (DRM_GPUVA_USERBITS << 10)
4950

5051
/**
5152
* struct xe_vma_mem_attr - memory attributes associated with vma

include/uapi/drm/xe_drm.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,20 @@ struct drm_xe_vm_destroy {
10131013
* valid on VMs with DRM_XE_VM_CREATE_FLAG_FAULT_MODE set. The CPU address
10141014
* mirror flag are only valid for DRM_XE_VM_BIND_OP_MAP operations, the BO
10151015
* handle MBZ, and the BO offset MBZ.
1016+
* - %DRM_XE_VM_BIND_FLAG_MADVISE_AUTORESET - Can be used in combination with
1017+
* %DRM_XE_VM_BIND_FLAG_CPU_ADDR_MIRROR to reset madvises when the underlying
1018+
* CPU address space range is unmapped (typically with munmap(2) or brk(2)).
1019+
* The madvise values set with &DRM_IOCTL_XE_MADVISE are reset to the values
1020+
* that were present immediately after the &DRM_IOCTL_XE_VM_BIND.
1021+
* The reset GPU virtual address range is the intersection of the range bound
1022+
* using &DRM_IOCTL_XE_VM_BIND and the virtual CPU address space range
1023+
* unmapped.
1024+
* This functionality is present to mimic the behaviour of CPU address space
1025+
* madvises set using madvise(2), which are typically reset on unmap.
1026+
* Note: free(3) may or may not call munmap(2) and/or brk(2), and may thus
1027+
* not invoke autoreset. Neither will stack variables going out of scope.
1028+
* Therefore it's recommended to always explicitly reset the madvises when
1029+
* freeing the memory backing a region used in a &DRM_IOCTL_XE_MADVISE call.
10161030
*
10171031
* The @prefetch_mem_region_instance for %DRM_XE_VM_BIND_OP_PREFETCH can also be:
10181032
* - %DRM_XE_CONSULT_MEM_ADVISE_PREF_LOC, which ensures prefetching occurs in
@@ -1119,6 +1133,7 @@ struct drm_xe_vm_bind_op {
11191133
#define DRM_XE_VM_BIND_FLAG_DUMPABLE (1 << 3)
11201134
#define DRM_XE_VM_BIND_FLAG_CHECK_PXP (1 << 4)
11211135
#define DRM_XE_VM_BIND_FLAG_CPU_ADDR_MIRROR (1 << 5)
1136+
#define DRM_XE_VM_BIND_FLAG_MADVISE_AUTORESET (1 << 6)
11221137
/** @flags: Bind flags */
11231138
__u32 flags;
11241139

0 commit comments

Comments
 (0)