Skip to content

Commit 50c577e

Browse files
Jonathan-Cavittmbrost05
authored andcommitted
drm/xe/xe_vm: Implement xe_vm_get_property_ioctl
Add support for userspace to request a list of observed faults from a specified VM. v2: - Only allow querying of failed pagefaults (Matt Brost) v3: - Remove unnecessary size parameter from helper function, as it is a property of the arguments. (jcavitt) - Remove unnecessary copy_from_user (Jainxun) - Set address_precision to 1 (Jainxun) - Report max size instead of dynamic size for memory allocation purposes. Total memory usage is reported separately. v4: - Return int from xe_vm_get_property_size (Shuicheng) - Fix memory leak (Shuicheng) - Remove unnecessary size variable (jcavitt) v5: - Rename ioctl to xe_vm_get_faults_ioctl (jcavitt) - Update fill_property_pfs to eliminate need for kzalloc (Jianxun) v6: - Repair and move fill_faults break condition (Dan Carpenter) - Free vm after use (jcavitt) - Combine assertions (jcavitt) - Expand size check in xe_vm_get_faults_ioctl (jcavitt) - Remove return mask from fill_faults, as return is already -EFAULT or 0 (jcavitt) v7: - Revert back to using xe_vm_get_property_ioctl - Apply better copy_to_user logic (jcavitt) v8: - Fix and clean up error value handling in ioctl (jcavitt) - Reapply return mask for fill_faults (jcavitt) v9: - Future-proof size logic for zero-size properties (jcavitt) - Add access and fault types (Jianxun) - Remove address type (Jianxun) v10: - Remove unnecessary switch case logic (Raag) - Compress size get, size validation, and property fill functions into a single helper function (jcavitt) - Assert valid size (jcavitt) v11: - Remove unnecessary else condition - Correct backwards helper function size logic (jcavitt) v12: - Use size_t instead of int (Raag) v13: - Remove engine class and instance (Ivan) v14: - Map access type, fault type, and fault level to user macros (Matt Brost, Ivan) v15: - Remove unnecessary size assertion (jcavitt) v16: - Nit fixes (Matt Brost) v17: - Rebase and refactor (jcavitt) v18: - Do not copy_to_user in critical section (Matt Brost) - Assert args->size is multiple of sizeof(struct xe_vm_fault) (Matt Brost) v19: - Remove unnecessary memset (Matt Brost) v20: - Report canonicalized address (Jose) - Mask out prefetch data from access type (Jose, jcavitt) v21: - s/uAPI/Link in the commit log links - Align debug parameters Link: intel/compute-runtime#878 Signed-off-by: Jonathan Cavitt <jonathan.cavitt@intel.com> Suggested-by: Matthew Brost <matthew.brost@intel.com> Reviewed-by: Matthew Brost <matthew.brost@intel.com> Acked-by: Michal Mrozek <michal.mrozek@intel.com> Cc: Jainxun Zhang <jianxun.zhang@intel.com> Cc: Shuicheng Lin <shuicheng.lin@intel.com> Cc: Raag Jadav <raag.jadav@intel.com> Cc: Ivan Briano <ivan.briano@intel.com> Cc: Jose Souza <jose.souza@intel.com> Signed-off-by: Matthew Brost <matthew.brost@intel.com> Link: https://patch.msgid.link/20260324152935.72444-10-jonathan.cavitt@intel.com
1 parent 64c732e commit 50c577e

3 files changed

Lines changed: 122 additions & 0 deletions

File tree

drivers/gpu/drm/xe/xe_device.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ static const struct drm_ioctl_desc xe_ioctls[] = {
211211
DRM_RENDER_ALLOW),
212212
DRM_IOCTL_DEF_DRV(XE_EXEC_QUEUE_SET_PROPERTY, xe_exec_queue_set_property_ioctl,
213213
DRM_RENDER_ALLOW),
214+
DRM_IOCTL_DEF_DRV(XE_VM_GET_PROPERTY, xe_vm_get_property_ioctl,
215+
DRM_RENDER_ALLOW),
214216
};
215217

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

drivers/gpu/drm/xe/xe_vm.c

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3974,6 +3974,123 @@ int xe_vm_bind_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
39743974
return err;
39753975
}
39763976

3977+
/*
3978+
* Map access type, fault type, and fault level from current bspec
3979+
* specification to user spec abstraction. The current mapping is
3980+
* approximately 1-to-1, with access type being the only notable
3981+
* exception as it carries additional data with respect to prefetch
3982+
* status that needs to be masked out.
3983+
*/
3984+
static u8 xe_to_user_access_type(u8 access_type)
3985+
{
3986+
return access_type & XE_PAGEFAULT_ACCESS_TYPE_MASK;
3987+
}
3988+
3989+
static u8 xe_to_user_fault_type(u8 fault_type)
3990+
{
3991+
return fault_type;
3992+
}
3993+
3994+
static u8 xe_to_user_fault_level(u8 fault_level)
3995+
{
3996+
return fault_level;
3997+
}
3998+
3999+
static int fill_faults(struct xe_vm *vm,
4000+
struct drm_xe_vm_get_property *args)
4001+
{
4002+
struct xe_vm_fault __user *usr_ptr = u64_to_user_ptr(args->data);
4003+
struct xe_vm_fault *fault_list, fault_entry = { 0 };
4004+
struct xe_vm_fault_entry *entry;
4005+
int ret = 0, i = 0, count, entry_size;
4006+
4007+
entry_size = sizeof(struct xe_vm_fault);
4008+
count = args->size / entry_size;
4009+
4010+
fault_list = kcalloc(count, sizeof(struct xe_vm_fault), GFP_KERNEL);
4011+
if (!fault_list)
4012+
return -ENOMEM;
4013+
4014+
spin_lock(&vm->faults.lock);
4015+
list_for_each_entry(entry, &vm->faults.list, list) {
4016+
if (i == count)
4017+
break;
4018+
4019+
fault_entry.address = xe_device_canonicalize_addr(vm->xe, entry->address);
4020+
fault_entry.address_precision = entry->address_precision;
4021+
4022+
fault_entry.access_type = xe_to_user_access_type(entry->access_type);
4023+
fault_entry.fault_type = xe_to_user_fault_type(entry->fault_type);
4024+
fault_entry.fault_level = xe_to_user_fault_level(entry->fault_level);
4025+
4026+
memcpy(&fault_list[i], &fault_entry, entry_size);
4027+
4028+
i++;
4029+
}
4030+
spin_unlock(&vm->faults.lock);
4031+
4032+
ret = copy_to_user(usr_ptr, fault_list, args->size);
4033+
4034+
kfree(fault_list);
4035+
return ret ? -EFAULT : 0;
4036+
}
4037+
4038+
static int xe_vm_get_property_helper(struct xe_vm *vm,
4039+
struct drm_xe_vm_get_property *args)
4040+
{
4041+
size_t size;
4042+
4043+
switch (args->property) {
4044+
case DRM_XE_VM_GET_PROPERTY_FAULTS:
4045+
spin_lock(&vm->faults.lock);
4046+
size = size_mul(sizeof(struct xe_vm_fault), vm->faults.len);
4047+
spin_unlock(&vm->faults.lock);
4048+
4049+
if (!args->size) {
4050+
args->size = size;
4051+
return 0;
4052+
}
4053+
4054+
/*
4055+
* Number of faults may increase between calls to
4056+
* xe_vm_get_property_ioctl, so just report the number of
4057+
* faults the user requests if it's less than or equal to
4058+
* the number of faults in the VM fault array.
4059+
*
4060+
* We should also at least assert that the args->size value
4061+
* is a multiple of the xe_vm_fault struct size.
4062+
*/
4063+
if (args->size > size || args->size % sizeof(struct xe_vm_fault))
4064+
return -EINVAL;
4065+
4066+
return fill_faults(vm, args);
4067+
}
4068+
return -EINVAL;
4069+
}
4070+
4071+
int xe_vm_get_property_ioctl(struct drm_device *drm, void *data,
4072+
struct drm_file *file)
4073+
{
4074+
struct xe_device *xe = to_xe_device(drm);
4075+
struct xe_file *xef = to_xe_file(file);
4076+
struct drm_xe_vm_get_property *args = data;
4077+
struct xe_vm *vm;
4078+
int ret = 0;
4079+
4080+
if (XE_IOCTL_DBG(xe, (args->reserved[0] || args->reserved[1] ||
4081+
args->reserved[2])))
4082+
return -EINVAL;
4083+
4084+
vm = xe_vm_lookup(xef, args->vm_id);
4085+
if (XE_IOCTL_DBG(xe, !vm))
4086+
return -ENOENT;
4087+
4088+
ret = xe_vm_get_property_helper(vm, args);
4089+
4090+
xe_vm_put(vm);
4091+
return ret;
4092+
}
4093+
39774094
/**
39784095
* xe_vm_bind_kernel_bo - bind a kernel BO to a VM
39794096
* @vm: VM to bind the BO to

drivers/gpu/drm/xe/xe_vm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ int xe_vm_destroy_ioctl(struct drm_device *dev, void *data,
210210
int xe_vm_bind_ioctl(struct drm_device *dev, void *data,
211211
struct drm_file *file);
212212
int xe_vm_query_vmas_attrs_ioctl(struct drm_device *dev, void *data, struct drm_file *file);
213+
int xe_vm_get_property_ioctl(struct drm_device *dev, void *data,
214+
struct drm_file *file);
215+
213216
void xe_vm_close_and_put(struct xe_vm *vm);
214217

215218
static inline bool xe_vm_in_fault_mode(struct xe_vm *vm)

0 commit comments

Comments
 (0)