drm/amdgpu: Patches for CRIU checkpoint and restore of userqueues
Subsystems (3)
This patch series adds two USERQ ioctl options: LIST and CHANGE_ID, which are needed for CRIU to work with amdgpu userqueues.
CRIU patch: https://github.com/fdavid-amd/criu/tree/amdgpu-userq
IGT patch: https://gitlab.freedesktop.org/fdavid-amd/igt-gpu-tools/-/tree/amdgpu_userq
PATCH 1/3 drm/amdgpu: Protect userq create and free fully with mutex
amdgpu userq has a userq_mutex meant to protect the list of userqs and their state.
The CREATE option of the USERQ ioctl released the lock before performing a last xa_alloc, relying on the xa lock to avoid races.
The FREE option of the same ioctl didn't use the lock at all.
Acquire and hold the lock in both these cases, to ensure correct serialization of these ioctl options against each other and against future ioctl options.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -6
@@ -748,11 +748,11 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) }atomic_inc (&uq_mgr->userq_count[queue->queue_type]); -mutex_unlock (&uq_mgr->userq_mutex); - r =xa_alloc (&uq_mgr->userq_xa, &qid, queue,XA_LIMIT (1 ,AMDGPU_MAX_USERQ_COUNT ),GFP_KERNEL ); + +mutex_unlock (&uq_mgr->userq_mutex);if (r) {/* * This drops the last reference which should take care of
@@ -890,18 +890,17 @@ int amdgpu_userq_ioctl(struct drm_device *dev, void *data,if (r)drm_file_err (filp,"Failed to create usermode queue\n" );break ; - -case AMDGPU_USERQ_OP_FREE : { +case AMDGPU_USERQ_OP_FREE : +mutex_lock (&fpriv->userq_mgr.userq_mutex);xa_lock (&fpriv->userq_mgr.userq_xa); queue =__xa_erase (&fpriv->userq_mgr.userq_xa, args->in.queue_id);xa_unlock (&fpriv->userq_mgr.userq_xa); +mutex_unlock (&fpriv->userq_mgr.userq_mutex);if (!queue)return -ENOENT ;amdgpu_userq_put (queue);break ; - } -default :drm_dbg_driver (dev,"Invalid user queue op specified: %d\n" , args->in.op);return -EINVAL ;
Signature · 1 line
2.34.1
Re: [PATCH 1/3] drm/amdgpu: Protect userq create and free fully with mutex
Quoted 23 lines · David Francis
amdgpu userq has a
userq_mutexmeant to protect the list of userqs and their state.The CREATE option of the USERQ ioctl released the lock before performing a last
xa_alloc, relying on the xa lock to avoid races.The FREE option of the same ioctl didn't use the lock at all.
Acquire and hold the lock in both these cases, to ensure correct serialization of these ioctl options against each other and against future ioctl options.
Signed-off-by David Francis <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) · line 755
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-2@@ -748,11 +748,11 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) }atomic_inc (&uq_mgr->userq_count[queue->queue_type]); -mutex_unlock (&uq_mgr->userq_mutex); - r =xa_alloc (&uq_mgr->userq_xa, &qid, queue,XA_LIMIT (1 ,AMDGPU_MAX_USERQ_COUNT ),GFP_KERNEL ); + +mutex_unlock (&uq_mgr->userq_mutex);
So the bug/race is that AMDGPU_USERQ_OP_FREE can drop the reference before amdgpu_userq_create() fully finishes, but then I think it is still there after this patch. Because a bit lower down, on the success path, there is this:
amdgpu_debugfs_userq_init(filp, queue, qid); trace_amdgpu_userq_create_end(queue, 0); args->out.queue_id = qid; return 0;
So as soon as userq_mutex is unlocked, racing thread can do the last amdgpu_userq_put() making the first two of above use after free.
Possibly xa_alloc needs to be truly last with the corresponding debugfs unwind on the failure path. That would fix the existing race without the need to widen the mutex scope, but perhaps wouldn't help you for the LIST implementation in the following patch. Question there is could you get away with not locking the mutex.
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0if (r) {/* * This drops the last reference which should take care of
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_ioctl(struct drm_device *dev, void *data, · line 897
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-2@@ -890,18 +890,17 @@ int amdgpu_userq_ioctl(struct drm_device *dev, void *data,if (r)drm_file_err (filp,"Failed to create usermode queue\n" );break ; - -case AMDGPU_USERQ_OP_FREE : { +case AMDGPU_USERQ_OP_FREE : +mutex_lock (&fpriv->userq_mgr.userq_mutex);xa_lock (&fpriv->userq_mgr.userq_xa); queue =__xa_erase (&fpriv->userq_mgr.userq_xa, args->in.queue_id);xa_unlock (&fpriv->userq_mgr.userq_xa);
xa_lock + __xa_erase + xa_unlock = xa_erase
Regards,
Tvrtko
Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-2+mutex_unlock (&fpriv->userq_mgr.userq_mutex);if (!queue)return -ENOENT ;amdgpu_userq_put (queue);break ; - } -default :drm_dbg_driver (dev,"Invalid user queue op specified: %d\n" , args->in.op);return -EINVAL ;
PATCH 2/3 drm/amdgpu: Add LIST option to USERQ ioctl
In reply to David FrancisAdd a new option to ioctl USERQ which provides information to a process about their own user queues on the queried device.
The returned data is in the same format as that used to create the queue in the first place.
The interface uses retries; the user first sends a request with num_entries 0, then receives the right number of entries. The same is done for the mqd sizes of each entry.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 159 ++++++++++++++++++++++ include/uapi/drm/amdgpu_drm.h | 39 ++++++ 2 files changed, 198 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +159 -0
@@ -850,6 +850,8 @@ static int amdgpu_userq_input_args_validate(struct drm_device *dev, args->in.mqd_size)return -EINVAL ;break ; +case AMDGPU_USERQ_OP_LIST : +break ;default :return -EINVAL ; }
@@ -857,6 +859,158 @@ static int amdgpu_userq_input_args_validate(struct drm_device *dev,return 0 ; } +static int +amdgpu_userq_list (struct drm_file *filp,union drm_amdgpu_userq *args) +{ +struct amdgpu_fpriv *fpriv = filp->driver_priv; +struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr; +struct drm_amdgpu_userq_list_entry *entries; +struct amdgpu_usermode_queue *queue; +bool mqd_sizes_correct = true; +unsigned long queue_id; +size_t mqd_size; +uint32_t num_queues =0 ; +int ret =0 ; +int i =0 ; + +mutex_lock (&uq_mgr->userq_mutex); + +xa_for_each (&uq_mgr->userq_xa, queue_id, queue) { + num_queues +=1 ; + } + +if (num_queues != args->list_in_out.num_entries) { +/** +* If the num_entries is not the number of queues, +* return the correct number. User should +* try again with the right space allocated. +*/ + args->list_in_out.num_entries = num_queues; +mutex_unlock (&uq_mgr->userq_mutex); +if (args->list_in_out.num_entries ==0 ) +return 0 ; +return -EINVAL ; + } +if (num_queues ==0 ) { +mutex_unlock (&uq_mgr->userq_mutex); +return 0 ; + } + + entries =kvmalloc_array (num_queues,sizeof (*entries),GFP_KERNEL ); + +if (!entries) { +mutex_unlock (&uq_mgr->userq_mutex); +return -ENOMEM ; + } + + ret =copy_from_user (entries,u64_to_user_ptr (args->list_in_out.entries), + num_queues *sizeof (*entries)); + +if (ret) { + ret = -EFAULT ; + goto exit; + } + +xa_for_each (&uq_mgr->userq_xa, queue_id, queue) { + mqd_size =0 ; +/** +* Check mqd size. As with num_entries, return the right sizes +* if they are not correct. These sizes also serve as +* versioning for the mqd. +*/ +if (queue->queue_type ==AMDGPU_HW_IP_COMPUTE ) + mqd_size =sizeof (struct drm_amdgpu_userq_mqd_compute_gfx11 ); +else if (queue->queue_type ==AMDGPU_HW_IP_GFX ) + mqd_size =sizeof (struct drm_amdgpu_userq_mqd_gfx11 ); +else if (queue->queue_type ==AMDGPU_HW_IP_DMA ) + mqd_size =sizeof (struct drm_amdgpu_userq_mqd_sdma_gfx11 ); + +if (mqd_size != entries[i].mqd_size) { + mqd_sizes_correct = false; + entries[i].mqd_size = mqd_size; + entries[i].ip_type = queue->queue_type; + } + i +=1 ; + } +if (!mqd_sizes_correct) { + ret =copy_to_user (u64_to_user_ptr (args->list_in_out.entries), entries, + num_queues *sizeof (*entries)); +if (ret) + ret = -EFAULT ; + goto exit; + } + i =0 ; +xa_for_each (&uq_mgr->userq_xa, queue_id, queue) { + entries[i].queue_id = queue_id; + entries[i].ip_type = queue->queue_type; +/* userq prop handling */ + entries[i].queue_va = queue->userq_prop->hqd_base_gpu_addr; + entries[i].queue_size = queue->userq_prop->queue_size; + entries[i].rptr_va = queue->userq_prop->rptr_gpu_addr; + entries[i].wptr_va = queue->userq_prop->wptr_gpu_addr; +/* flag handling (AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK is the only flag in use) */ + entries[i].flags = (queue->priority <<AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_SHIFT ) + &AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK ; +/* doorbell handling */ + entries[i].doorbell_handle = queue->doorbell_handle; +/* This is the inverse of the calculation used in amdgpu_userq_get_doorbell_index */ + entries[i].doorbell_offset = + (queue->doorbell_index -amdgpu_bo_gpu_offset_no_check (queue->db_obj.obj) /sizeof (u32)) + /DIV_ROUND_UP (sizeof (u64),4 ); + +if (queue->queue_type ==AMDGPU_HW_IP_COMPUTE ) { +struct drm_amdgpu_userq_mqd_compute_gfx11 compute_mqd = {0 }; + + compute_mqd.eop_va = queue->userq_prop->eop_gpu_addr; + + ret =copy_to_user (u64_to_user_ptr (entries[i].mqd_data), + &compute_mqd, + entries[i].mqd_size); + +if (ret) { + ret = -EFAULT ; + goto exit; + } + }else if (queue->queue_type ==AMDGPU_HW_IP_GFX ) { +struct drm_amdgpu_userq_mqd_gfx11 mqd_gfx_v11 = {0 }; + + mqd_gfx_v11.shadow_va = queue->userq_prop->shadow_addr; + mqd_gfx_v11.csa_va = queue->userq_prop->csa_addr; + + ret =copy_to_user (u64_to_user_ptr (entries[i].mqd_data), + &mqd_gfx_v11, + entries[i].mqd_size); +if (ret) { + ret = -EFAULT ; + goto exit; + } + }else if (queue->queue_type ==AMDGPU_HW_IP_DMA ) { +struct drm_amdgpu_userq_mqd_sdma_gfx11 mqd_sdma_v11 = {0 }; + + mqd_sdma_v11.csa_va = queue->userq_prop->csa_addr; + + ret =copy_to_user (u64_to_user_ptr (entries[i].mqd_data), + &mqd_sdma_v11, + entries[i].mqd_size); +if (ret) { + ret = -EFAULT ; + goto exit; + } + } + i +=1 ; + } + ret =copy_to_user (u64_to_user_ptr (args->list_in_out.entries), entries, + num_queues *sizeof (*entries)); +if (ret) { + ret = -EFAULT ; + goto exit; + } +exit: +mutex_unlock (&uq_mgr->userq_mutex); +kvfree (entries); +return ret; +} +bool amdgpu_userq_enabled (struct drm_device *dev) {struct amdgpu_device *adev =drm_to_adev (dev);
@@ -901,6 +1055,11 @@ int amdgpu_userq_ioctl(struct drm_device *dev, void *data,amdgpu_userq_put (queue);break ; +case AMDGPU_USERQ_OP_LIST : + r =amdgpu_userq_list (filp, args); +if (r) +drm_file_err (filp,"Failed to get list of usermode queues\n" ); +break ;default :drm_dbg_driver (dev,"Invalid user queue op specified: %d\n" , args->in.op);return -EINVAL ;
include/uapi/drm/amdgpu_drm.h +39 -0
@@ -332,6 +332,7 @@ union drm_amdgpu_ctx {/* user queue IOCTL operations */ #define AMDGPU_USERQ_OP_CREATE 1#define AMDGPU_USERQ_OP_FREE 2 +#define AMDGPU_USERQ_OP_LIST 3/* queue priority levels */ /* low < normal low < normal high < high */
@@ -425,9 +426,47 @@ struct drm_amdgpu_userq_out {__u32 _pad; }; +struct drm_amdgpu_userq_list_entry { +/** Definitions same as drm_amdgpu_userq_in */ +__u32 queue_id; +__u32 ip_type; +__u32 doorbell_handle; +__u32 doorbell_offset; +__u32 flags; +__u64 queue_va; +__u64 queue_size; +__u64 rptr_va; +__u64 wptr_va; +/** Userspace pointer to buffer holding mqd */ +__u64 mqd_data; +/** +* In: Size of mqd_data user-allocated buffer. +* Out: If mqd_data was insufficiently large, the +* size it needs to be. +*/ +__u64 mqd_size; +}; + +struct drm_amdgpu_userq_list_in_out { +/** +* For operation AMDGPU_USERQ_OP_LIST: User will provide a buffer, which the +* driver will fill with information about all of that process's queues on this device. +*/ +/** AMDGPU_USERQ_OP_LIST */ +__u32 op; +/** +* Size of entries buffer / Number of handles in process +* (if larger than size of buffer, must retry) +*/ +__u32 num_entries; +/* User pointer to array of drm_amdgpu_userq_list_entry */ +__u64 entries; +}; +union drm_amdgpu_userq {struct drm_amdgpu_userq_in in;struct drm_amdgpu_userq_out out; +struct drm_amdgpu_userq_list_in_out list_in_out; };/* GFX V11 IP specific MQD parameters */
Signature · 1 line
2.34.1
Re: [PATCH 2/3] drm/amdgpu: Add LIST option to USERQ ioctl
Quoted 37 lines · David Francis
Add a new option to ioctl USERQ which provides information to a process about their own user queues on the queried device.
The returned data is in the same format as that used to create the queue in the first place.
The interface uses retries; the user first sends a request with
num_entries0, then receives the right number of entries. The same is done for the mqd sizes of each entry.Signed-off-by David Francis <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 159 ++++++++++++++++++++++ include/uapi/drm/amdgpu_drm.h | 39 ++++++ 2 files changed, 198 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-0@@ -850,6 +850,8 @@ static int amdgpu_userq_input_args_validate(struct drm_device *dev, args->in.mqd_size)return -EINVAL ;break ; +case AMDGPU_USERQ_OP_LIST : +break ;default :return -EINVAL ; }@@ -857,6 +859,158 @@ static int amdgpu_userq_input_args_validate(struct drm_device *dev,return 0 ; } +static int +amdgpu_userq_list (struct drm_file *filp,union drm_amdgpu_userq *args) +{
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 874
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10
-0+struct amdgpu_fpriv *fpriv = filp->driver_priv; +struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr; +struct drm_amdgpu_userq_list_entry *entries; +struct amdgpu_usermode_queue *queue; +bool mqd_sizes_correct = true; +unsigned long queue_id; +size_t mqd_size; +uint32_t num_queues =0 ; +int ret =0 ; +int i =0 ;
On a glance ret does not need to be initialized and i would potentially be better done so above the first block which uses it, in order to match the pattern of the second user.
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 876
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-0+ +mutex_lock (&uq_mgr->userq_mutex);
Design question is whether there is any benefit from the mutex. Alternative could be to simply abort the attempt if the number of queues changed between counting to filling the data. For CRIU that's fine since everything will be idled. And for non-CRIU it doesn't matter since the by the time the query returns the data may be stale anyway.
In which case not extending the scope of the mutex would simplify the previous patch and this one.
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6
-0+ +xa_for_each (&uq_mgr->userq_xa, queue_id, queue) { + num_queues +=1 ; + } + +if (num_queues != args->list_in_out.num_entries) {
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 892
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10
-0+/** +* If the num_entries is not the number of queues, +* return the correct number. User should +* try again with the right space allocated. +*/ + args->list_in_out.num_entries = num_queues; +mutex_unlock (&uq_mgr->userq_mutex); +if (args->list_in_out.num_entries ==0 ) +return 0 ; +return -EINVAL ;
Why -EINVAL? It differs from the established practice from amdgpu_gem_list_handles_ioctl for example.
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 899
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7
-0+ } +if (num_queues ==0 ) { +mutex_unlock (&uq_mgr->userq_mutex); +return 0 ; + } + + entries =kvmalloc_array (num_queues,sizeof (*entries),GFP_KERNEL );
kvmalloc_objs is preferred these days.
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 907
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8
-0+ +if (!entries) { +mutex_unlock (&uq_mgr->userq_mutex); +return -ENOMEM ; + } + + ret =copy_from_user (entries,u64_to_user_ptr (args->list_in_out.entries), + num_queues *sizeof (*entries));
Multiplication can overflow on 32-bit so best would be to use check_mul_oveflow and stash the total size into a size_t local.
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 915
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8
-0+ +if (ret) { + ret = -EFAULT ; + goto exit; + } + +xa_for_each (&uq_mgr->userq_xa, queue_id, queue) { + mqd_size =0 ;
Move to else?
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 922
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7
-0+/** +* Check mqd size. As with num_entries, return the right sizes +* if they are not correct. These sizes also serve as +* versioning for the mqd. +*/ +if (queue->queue_type ==AMDGPU_HW_IP_COMPUTE ) + mqd_size =sizeof (struct drm_amdgpu_userq_mqd_compute_gfx11 );
Mention of gfx11 feels a bit out of place - how will this work for future extensions?
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 926
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4
-0+else if (queue->queue_type ==AMDGPU_HW_IP_GFX ) + mqd_size =sizeof (struct drm_amdgpu_userq_mqd_gfx11 ); +else if (queue->queue_type ==AMDGPU_HW_IP_DMA ) + mqd_size =sizeof (struct drm_amdgpu_userq_mqd_sdma_gfx11 );
Emit a warning and fail if unknown queue type is encountered?
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 931
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-0+ +if (mqd_size != entries[i].mqd_size) { + mqd_sizes_correct = false; + entries[i].mqd_size = mqd_size; + entries[i].ip_type = queue->queue_type;
Why not break out?
Or could you even merge this into the main xa_for_each below? Simply walk it and branch between good and bad mqd_sizes, each filling their respecive block.
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 940
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +9
-0+ } + i +=1 ; + } +if (!mqd_sizes_correct) { + ret =copy_to_user (u64_to_user_ptr (args->list_in_out.entries), entries, + num_queues *sizeof (*entries)); +if (ret) + ret = -EFAULT ; + goto exit;
So userspace gets a success but entries[] has only been partially filled? How is userspace expected to detect and handle that? By looking if any entry mqd_size returned different than the one it passed in?
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +9
-0+ } + i =0 ; +xa_for_each (&uq_mgr->userq_xa, queue_id, queue) { + entries[i].queue_id = queue_id; + entries[i].ip_type = queue->queue_type; +/* userq prop handling */ + entries[i].queue_va = queue->userq_prop->hqd_base_gpu_addr; + entries[i].queue_size = queue->userq_prop->queue_size; + entries[i].rptr_va = queue->userq_prop->rptr_gpu_addr;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 959
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10
-0+ entries[i].wptr_va = queue->userq_prop->wptr_gpu_addr; +/* flag handling (AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK is the only flag in use) */ + entries[i].flags = (queue->priority <<AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_SHIFT ) + &AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK ; +/* doorbell handling */ + entries[i].doorbell_handle = queue->doorbell_handle; +/* This is the inverse of the calculation used in amdgpu_userq_get_doorbell_index */ + entries[i].doorbell_offset = + (queue->doorbell_index -amdgpu_bo_gpu_offset_no_check (queue->db_obj.obj) /sizeof (u32)) + /DIV_ROUND_UP (sizeof (u64),4 );
What is the point of DIV_ROUND_UP here?
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 962
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-0+ +if (queue->queue_type ==AMDGPU_HW_IP_COMPUTE ) { +struct drm_amdgpu_userq_mqd_compute_gfx11 compute_mqd = {0 };
= {} is preferred I think, or maybe better, all initializers in this blow and the ones below can be made at declaration time which would be a bit more compact.
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 973
Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +11
-0+ + compute_mqd.eop_va = queue->userq_prop->eop_gpu_addr; + + ret =copy_to_user (u64_to_user_ptr (entries[i].mqd_data), + &compute_mqd, + entries[i].mqd_size); + +if (ret) { + ret = -EFAULT ; + goto exit; + }
if (ret) etc block could be moved to a single one after the if-elseif- ladder.
Quoted 44 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +40
-0+ }else if (queue->queue_type ==AMDGPU_HW_IP_GFX ) { +struct drm_amdgpu_userq_mqd_gfx11 mqd_gfx_v11 = {0 }; + + mqd_gfx_v11.shadow_va = queue->userq_prop->shadow_addr; + mqd_gfx_v11.csa_va = queue->userq_prop->csa_addr; + + ret =copy_to_user (u64_to_user_ptr (entries[i].mqd_data), + &mqd_gfx_v11, + entries[i].mqd_size); +if (ret) { + ret = -EFAULT ; + goto exit; + } + }else if (queue->queue_type ==AMDGPU_HW_IP_DMA ) { +struct drm_amdgpu_userq_mqd_sdma_gfx11 mqd_sdma_v11 = {0 }; + + mqd_sdma_v11.csa_va = queue->userq_prop->csa_addr; + + ret =copy_to_user (u64_to_user_ptr (entries[i].mqd_data), + &mqd_sdma_v11, + entries[i].mqd_size); +if (ret) { + ret = -EFAULT ; + goto exit; + } + } + i +=1 ; + } + ret =copy_to_user (u64_to_user_ptr (args->list_in_out.entries), entries, + num_queues *sizeof (*entries)); +if (ret) { + ret = -EFAULT ; + goto exit; + } +exit: +mutex_unlock (&uq_mgr->userq_mutex); +kvfree (entries); +return ret; +} +bool amdgpu_userq_enabled (struct drm_device *dev) {struct amdgpu_device *adev =drm_to_adev (dev);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_ioctl(struct drm_device *dev, void *data, · line 1061
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4
-0@@ -901,6 +1055,11 @@ int amdgpu_userq_ioctl(struct drm_device *dev, void *data,amdgpu_userq_put (queue);break ; +case AMDGPU_USERQ_OP_LIST : + r =amdgpu_userq_list (filp, args); +if (r) +drm_file_err (filp,"Failed to get list of usermode queues\n" );
Drop this - we don't want to allow naughty userspace spam the kernel log at will.
Quoted 38 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-0+break ;default :drm_dbg_driver (dev,"Invalid user queue op specified: %d\n" , args->in.op);return -EINVAL ;include/uapi/drm/amdgpu_drm.h +19
-0@@ -332,6 +332,7 @@ union drm_amdgpu_ctx {/* user queue IOCTL operations */ #define AMDGPU_USERQ_OP_CREATE 1#define AMDGPU_USERQ_OP_FREE 2 +#define AMDGPU_USERQ_OP_LIST 3/* queue priority levels */ /* low < normal low < normal high < high */ @@ -425,9 +426,47 @@ struct drm_amdgpu_userq_out {__u32 _pad; }; +struct drm_amdgpu_userq_list_entry { +/** Definitions same as drm_amdgpu_userq_in */ +__u32 queue_id; +__u32 ip_type; +__u32 doorbell_handle; +__u32 doorbell_offset; +__u32 flags; +__u64 queue_va; +__u64 queue_size; +__u64 rptr_va; +__u64 wptr_va; +/** Userspace pointer to buffer holding mqd */ +__u64 mqd_data; +/** +* In: Size of mqd_data user-allocated buffer. +* Out: If mqd_data was insufficiently large, the +* size it needs to be. +*/
include/uapi/drm/amdgpu_drm.h struct drm_amdgpu_userq_out { · line 456
Quoted 10 lines
include/uapi/drm/amdgpu_drm.h +10
-0+__u64 mqd_size; +}; + +struct drm_amdgpu_userq_list_in_out { +/** +* For operation AMDGPU_USERQ_OP_LIST: User will provide a buffer, which the +* driver will fill with information about all of that process's queues on this device. +*/ +/** AMDGPU_USERQ_OP_LIST */ +__u32 op;
At first I wanted to ask what is this for, that it seems unused, but then I realized it is for..
include/uapi/drm/amdgpu_drm.h struct drm_amdgpu_userq_out { · line 469
Quoted 13 lines
include/uapi/drm/amdgpu_drm.h +10
-0+/** +* Size of entries buffer / Number of handles in process +* (if larger than size of buffer, must retry) +*/ +__u32 num_entries; +/* User pointer to array of drm_amdgpu_userq_list_entry */ +__u64 entries; +}; +union drm_amdgpu_userq {struct drm_amdgpu_userq_in in;struct drm_amdgpu_userq_out out; +struct drm_amdgpu_userq_list_in_out list_in_out;
.. the union! And that the list_in_out.op is needed to match the exact position of in.op. A bit evil but I guess it works.
Regards,
Tvrtko
Quoted 3 lines
include/uapi/drm/amdgpu_drm.h +0
-0};/* GFX V11 IP specific MQD parameters */
Re: [PATCH 2/3] drm/amdgpu: Add LIST option to USERQ ioctl
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 874
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4
-0+size_t mqd_size; +uint32_t num_queues =0 ; +int ret =0 ; +int i =0 ;On a glance ret does not need to be initialized and i would potentially be better done so above the first block which uses it, in order to match the pattern of the second user.
Thanks, fixed
Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 876
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-0+ +mutex_lock (&uq_mgr->userq_mutex);Design question is whether there is any benefit from the mutex. Alternative could be to simply abort the attempt if the number of queues changed between counting to filling the data. For CRIU that's fine since everything will be idled. And for non-CRIU it doesn't matter since the by the time the query returns the data may be stale anyway.
In which case not extending the scope of the mutex would simplify the previous patch and this one.
On reconsideration, I'm having both LIST and CHANGE_IF hold the xa_lock for their entire duration, to avoid any shenanigans with things being removed or created under them.
I am also having them hold the userq_mutex. I don't think this is strictly necessary since a userq cannot be freed while it is in the xa_array but no reason not to.
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 892
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4
-0+mutex_unlock (&uq_mgr->userq_mutex); +if (args->list_in_out.num_entries ==0 ) +return 0 ; +return -EINVAL ;Why
-EINVAL? It differs from the established practice fromamdgpu_gem_list_handles_ioctlfor example.
Thanks, Fixed.
Quoted 3 lines
Quoted 1 line
+ entries =
kvmalloc_array(num_queues, sizeof(*entries), GFP_KERNEL);
kvmalloc_objsis preferred these days.
Thanks, fixed
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ ret = copy_from_user(entries, u64_to_user_ptr(args->list_in_out.entries), + num_queues * sizeof(*entries));Multiplication can overflow on 32-bit so best would be to use
check_mul_oveflowand stash the total size into asize_tlocal.
Thanks, fixed
Quoted 9 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ xa_for_each(&uq_mgr->userq_xa, queue_id, queue) { + mqd_size = 0;Move to else?
...
Emit a warning and fail if unknown queue type is encountered?
...
Why not break out?
Quoted 3 lines
Or could you even merge this into the main
xa_for_eachbelow? Simply walk it and branch between good and badmqd_sizes, each filling their respecive block.
I think there's enough shared handling for two ifs, but definitely close Making invalid mqd type an error.
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 922
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-0+if (queue->queue_type ==AMDGPU_HW_IP_COMPUTE ) + mqd_size =sizeof (struct drm_amdgpu_userq_mqd_compute_gfx11 );Mention of gfx11 feels a bit out of place - how will this work for
future extensions?
Despite the names these are currently the structs for both gfx11 and gfx12 If gfx13 is different this will need to check for it somehow.
Quoted 6 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ ret = -EFAULT; + goto exit;So userspace gets a success but entries[] has only been partially filled? How is userspace expected to detect and handle that? By looking if any entry
mqd_sizereturned different than the one it passed in?
Reconsidering this interface. Now, each entry is handled independently. Some might have correct mqd and get filled while others have incorrect mqd size and get just the size.
Seems the most consistent way.
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 959
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-0+ (queue->doorbell_index -amdgpu_bo_gpu_offset_no_check (queue->db_obj.obj) /sizeof (u32)) + /DIV_ROUND_UP (sizeof (u64),4 );What is the point of
DIV_ROUND_UPhere?
Whoops, fixed (and made this comment clearer)
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 962
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-0+if (queue->queue_type ==AMDGPU_HW_IP_COMPUTE ) { +struct drm_amdgpu_userq_mqd_compute_gfx11 compute_mqd = {0 };= {} is preferred I think, or maybe better, all initializers in this blow and the ones below can be made at declaration time which would be a bit more compact.
Near as I can tell amdgpu uses {0}. They're the same thing in C as far as I know.
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 912
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4
-0+if (ret) { + ret = -EFAULT ; + goto exit; + }if (ret) etc block could be moved to a single one after the if-elseif-
ladder.
Thanks, fixed
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_ioctl(struct drm_device *dev, void *data, · line 1061
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-0+if (r) +drm_file_err (filp,"Failed to get list of usermode queues\n" );Drop this - we don't want to allow naughty userspace spam the kernel log
at will.
Thanks, fixed.
Quoted 8 lines
include/uapi/drm/amdgpu_drm.h struct drm_amdgpu_userq_out { · line 456
Quoted 2 lines
include/uapi/drm/amdgpu_drm.h +2
-0+/** AMDGPU_USERQ_OP_LIST */ +__u32 op;At first I wanted to ask what is this for, that it seems unused, but then I realized it is for..
.. the union! And that the list_in_out.op is needed to match the exact position of in.op. A bit evil but I guess it works.
Agreed that this is slightly evil but it's what needs to be done.
________________________________________
From: Tvrtko Ursulin <[email protected]>
Sent: Wednesday, August 5, 2026 10:11 AM
To: Francis, David; [email protected]
Subject: Re: [PATCH 2/3] drm/amdgpu: Add LIST option to USERQ ioctl
Quoted 37 lines · David Francis
Add a new option to ioctl USERQ which provides information to a process about their own user queues on the queried device.
The returned data is in the same format as that used to create the queue in the first place.
The interface uses retries; the user first sends a request with
num_entries0, then receives the right number of entries. The same is done for the mqd sizes of each entry.Signed-off-by David Francis <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 159 ++++++++++++++++++++++ include/uapi/drm/amdgpu_drm.h | 39 ++++++ 2 files changed, 198 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-0@@ -850,6 +850,8 @@ static int amdgpu_userq_input_args_validate(struct drm_device *dev, args->in.mqd_size)return -EINVAL ;break ; +case AMDGPU_USERQ_OP_LIST : +break ;default :return -EINVAL ; }@@ -857,6 +859,158 @@ static int amdgpu_userq_input_args_validate(struct drm_device *dev,return 0 ; } +static int +amdgpu_userq_list (struct drm_file *filp,union drm_amdgpu_userq *args) +{
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 874
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10
-0+struct amdgpu_fpriv *fpriv = filp->driver_priv; +struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr; +struct drm_amdgpu_userq_list_entry *entries; +struct amdgpu_usermode_queue *queue; +bool mqd_sizes_correct = true; +unsigned long queue_id; +size_t mqd_size; +uint32_t num_queues =0 ; +int ret =0 ; +int i =0 ;
On a glance ret does not need to be initialized and i would potentially be better done so above the first block which uses it, in order to match the pattern of the second user.
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 876
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-0+ +mutex_lock (&uq_mgr->userq_mutex);
Design question is whether there is any benefit from the mutex. Alternative could be to simply abort the attempt if the number of queues changed between counting to filling the data. For CRIU that's fine since everything will be idled. And for non-CRIU it doesn't matter since the by the time the query returns the data may be stale anyway.
In which case not extending the scope of the mutex would simplify the previous patch and this one.
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6
-0+ +xa_for_each (&uq_mgr->userq_xa, queue_id, queue) { + num_queues +=1 ; + } + +if (num_queues != args->list_in_out.num_entries) {
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 892
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10
-0+/** +* If the num_entries is not the number of queues, +* return the correct number. User should +* try again with the right space allocated. +*/ + args->list_in_out.num_entries = num_queues; +mutex_unlock (&uq_mgr->userq_mutex); +if (args->list_in_out.num_entries ==0 ) +return 0 ; +return -EINVAL ;
Why -EINVAL? It differs from the established practice from amdgpu_gem_list_handles_ioctl for example.
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 899
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7
-0+ } +if (num_queues ==0 ) { +mutex_unlock (&uq_mgr->userq_mutex); +return 0 ; + } + + entries =kvmalloc_array (num_queues,sizeof (*entries),GFP_KERNEL );
kvmalloc_objs is preferred these days.
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 907
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8
-0+ +if (!entries) { +mutex_unlock (&uq_mgr->userq_mutex); +return -ENOMEM ; + } + + ret =copy_from_user (entries,u64_to_user_ptr (args->list_in_out.entries), + num_queues *sizeof (*entries));
Multiplication can overflow on 32-bit so best would be to use check_mul_oveflow and stash the total size into a size_t local.
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 915
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8
-0+ +if (ret) { + ret = -EFAULT ; + goto exit; + } + +xa_for_each (&uq_mgr->userq_xa, queue_id, queue) { + mqd_size =0 ;
Move to else?
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 922
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7
-0+/** +* Check mqd size. As with num_entries, return the right sizes +* if they are not correct. These sizes also serve as +* versioning for the mqd. +*/ +if (queue->queue_type ==AMDGPU_HW_IP_COMPUTE ) + mqd_size =sizeof (struct drm_amdgpu_userq_mqd_compute_gfx11 );
Mention of gfx11 feels a bit out of place - how will this work for future extensions?
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 926
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4
-0+else if (queue->queue_type ==AMDGPU_HW_IP_GFX ) + mqd_size =sizeof (struct drm_amdgpu_userq_mqd_gfx11 ); +else if (queue->queue_type ==AMDGPU_HW_IP_DMA ) + mqd_size =sizeof (struct drm_amdgpu_userq_mqd_sdma_gfx11 );
Emit a warning and fail if unknown queue type is encountered?
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 931
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-0+ +if (mqd_size != entries[i].mqd_size) { + mqd_sizes_correct = false; + entries[i].mqd_size = mqd_size; + entries[i].ip_type = queue->queue_type;
Why not break out?
Or could you even merge this into the main xa_for_each below? Simply walk it and branch between good and bad mqd_sizes, each filling their respecive block.
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 940
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +9
-0+ } + i +=1 ; + } +if (!mqd_sizes_correct) { + ret =copy_to_user (u64_to_user_ptr (args->list_in_out.entries), entries, + num_queues *sizeof (*entries)); +if (ret) + ret = -EFAULT ; + goto exit;
So userspace gets a success but entries[] has only been partially filled? How is userspace expected to detect and handle that? By looking if any entry mqd_size returned different than the one it passed in?
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +9
-0+ } + i =0 ; +xa_for_each (&uq_mgr->userq_xa, queue_id, queue) { + entries[i].queue_id = queue_id; + entries[i].ip_type = queue->queue_type; +/* userq prop handling */ + entries[i].queue_va = queue->userq_prop->hqd_base_gpu_addr; + entries[i].queue_size = queue->userq_prop->queue_size; + entries[i].rptr_va = queue->userq_prop->rptr_gpu_addr;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 959
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10
-0+ entries[i].wptr_va = queue->userq_prop->wptr_gpu_addr; +/* flag handling (AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK is the only flag in use) */ + entries[i].flags = (queue->priority <<AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_SHIFT ) + &AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK ; +/* doorbell handling */ + entries[i].doorbell_handle = queue->doorbell_handle; +/* This is the inverse of the calculation used in amdgpu_userq_get_doorbell_index */ + entries[i].doorbell_offset = + (queue->doorbell_index -amdgpu_bo_gpu_offset_no_check (queue->db_obj.obj) /sizeof (u32)) + /DIV_ROUND_UP (sizeof (u64),4 );
What is the point of DIV_ROUND_UP here?
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 962
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-0+ +if (queue->queue_type ==AMDGPU_HW_IP_COMPUTE ) { +struct drm_amdgpu_userq_mqd_compute_gfx11 compute_mqd = {0 };
= {} is preferred I think, or maybe better, all initializers in this blow and the ones below can be made at declaration time which would be a bit more compact.
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 973
Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +11
-0+ + compute_mqd.eop_va = queue->userq_prop->eop_gpu_addr; + + ret =copy_to_user (u64_to_user_ptr (entries[i].mqd_data), + &compute_mqd, + entries[i].mqd_size); + +if (ret) { + ret = -EFAULT ; + goto exit; + }
if (ret) etc block could be moved to a single one after the if-elseif- ladder.
Quoted 44 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +40
-0+ }else if (queue->queue_type ==AMDGPU_HW_IP_GFX ) { +struct drm_amdgpu_userq_mqd_gfx11 mqd_gfx_v11 = {0 }; + + mqd_gfx_v11.shadow_va = queue->userq_prop->shadow_addr; + mqd_gfx_v11.csa_va = queue->userq_prop->csa_addr; + + ret =copy_to_user (u64_to_user_ptr (entries[i].mqd_data), + &mqd_gfx_v11, + entries[i].mqd_size); +if (ret) { + ret = -EFAULT ; + goto exit; + } + }else if (queue->queue_type ==AMDGPU_HW_IP_DMA ) { +struct drm_amdgpu_userq_mqd_sdma_gfx11 mqd_sdma_v11 = {0 }; + + mqd_sdma_v11.csa_va = queue->userq_prop->csa_addr; + + ret =copy_to_user (u64_to_user_ptr (entries[i].mqd_data), + &mqd_sdma_v11, + entries[i].mqd_size); +if (ret) { + ret = -EFAULT ; + goto exit; + } + } + i +=1 ; + } + ret =copy_to_user (u64_to_user_ptr (args->list_in_out.entries), entries, + num_queues *sizeof (*entries)); +if (ret) { + ret = -EFAULT ; + goto exit; + } +exit: +mutex_unlock (&uq_mgr->userq_mutex); +kvfree (entries); +return ret; +} +bool amdgpu_userq_enabled (struct drm_device *dev) {struct amdgpu_device *adev =drm_to_adev (dev);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_ioctl(struct drm_device *dev, void *data, · line 1061
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4
-0@@ -901,6 +1055,11 @@ int amdgpu_userq_ioctl(struct drm_device *dev, void *data,amdgpu_userq_put (queue);break ; +case AMDGPU_USERQ_OP_LIST : + r =amdgpu_userq_list (filp, args); +if (r) +drm_file_err (filp,"Failed to get list of usermode queues\n" );
Drop this - we don't want to allow naughty userspace spam the kernel log at will.
Quoted 38 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-0+break ;default :drm_dbg_driver (dev,"Invalid user queue op specified: %d\n" , args->in.op);return -EINVAL ;include/uapi/drm/amdgpu_drm.h +19
-0@@ -332,6 +332,7 @@ union drm_amdgpu_ctx {/* user queue IOCTL operations */ #define AMDGPU_USERQ_OP_CREATE 1#define AMDGPU_USERQ_OP_FREE 2 +#define AMDGPU_USERQ_OP_LIST 3/* queue priority levels */ /* low < normal low < normal high < high */ @@ -425,9 +426,47 @@ struct drm_amdgpu_userq_out {__u32 _pad; }; +struct drm_amdgpu_userq_list_entry { +/** Definitions same as drm_amdgpu_userq_in */ +__u32 queue_id; +__u32 ip_type; +__u32 doorbell_handle; +__u32 doorbell_offset; +__u32 flags; +__u64 queue_va; +__u64 queue_size; +__u64 rptr_va; +__u64 wptr_va; +/** Userspace pointer to buffer holding mqd */ +__u64 mqd_data; +/** +* In: Size of mqd_data user-allocated buffer. +* Out: If mqd_data was insufficiently large, the +* size it needs to be. +*/
include/uapi/drm/amdgpu_drm.h struct drm_amdgpu_userq_out { · line 456
Quoted 10 lines
include/uapi/drm/amdgpu_drm.h +10
-0+__u64 mqd_size; +}; + +struct drm_amdgpu_userq_list_in_out { +/** +* For operation AMDGPU_USERQ_OP_LIST: User will provide a buffer, which the +* driver will fill with information about all of that process's queues on this device. +*/ +/** AMDGPU_USERQ_OP_LIST */ +__u32 op;
At first I wanted to ask what is this for, that it seems unused, but then I realized it is for..
include/uapi/drm/amdgpu_drm.h struct drm_amdgpu_userq_out { · line 469
Quoted 13 lines
include/uapi/drm/amdgpu_drm.h +10
-0+/** +* Size of entries buffer / Number of handles in process +* (if larger than size of buffer, must retry) +*/ +__u32 num_entries; +/* User pointer to array of drm_amdgpu_userq_list_entry */ +__u64 entries; +}; +union drm_amdgpu_userq {struct drm_amdgpu_userq_in in;struct drm_amdgpu_userq_out out; +struct drm_amdgpu_userq_list_in_out list_in_out;
.. the union! And that the list_in_out.op is needed to match the exact position of in.op. A bit evil but I guess it works.
Regards,
Tvrtko
Quoted 3 lines
include/uapi/drm/amdgpu_drm.h +0
-0};/* GFX V11 IP specific MQD parameters */
PATCH 3/3 drm/amdgpu: Add CHANGE_ID option for USERQ ioctl
In reply to David FrancisAdd a new option to the USERQ ioctl, which is called with the queue_id of an existing user queue and an unused queue_id, and changes that queue's id to the new value.
The operation is fully protected by userq_mutex to avoid races.
Performing this operation on a queue with signals or waits outstanding is fine, as those hold not the queue_id but a direct reference to the queue object.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 37 +++++++++++++++++++++++ include/uapi/drm/amdgpu_drm.h | 17 +++++++++-- 2 files changed, 51 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +37 -0
@@ -852,6 +852,11 @@ static int amdgpu_userq_input_args_validate(struct drm_device *dev,break ;case AMDGPU_USERQ_OP_LIST :break ; +case AMDGPU_USERQ_OP_CHANGE_ID : +if (!args->change_in.new_queue_id || + args->change_in.new_queue_id >AMDGPU_MAX_USERQ_COUNT ) +return -EINVAL ; +break ;default :return -EINVAL ; }
@@ -1011,6 +1016,33 @@ amdgpu_userq_list(struct drm_file *filp, union drm_amdgpu_userq *args)return ret; } +static int amdgpu_userq_change_id (struct drm_file *filp,union drm_amdgpu_userq *args) +{ +struct amdgpu_fpriv *fpriv = filp->driver_priv; +struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr; +struct amdgpu_usermode_queue *queue; +int ret =0 ; + +mutex_lock (&uq_mgr->userq_mutex); + + queue =amdgpu_userq_get (uq_mgr, args->change_in.queue_id); +if (!queue) { + ret = -EINVAL ; + goto unlock; + } + + ret =xa_insert (&uq_mgr->userq_xa, args->change_in.new_queue_id, queue,GFP_KERNEL ); +if (ret) + goto put; + +xa_erase (&uq_mgr->userq_xa, args->change_in.queue_id); +put: +amdgpu_userq_put (queue); +unlock: +mutex_unlock (&uq_mgr->userq_mutex); +return ret; +} +bool amdgpu_userq_enabled (struct drm_device *dev) {struct amdgpu_device *adev =drm_to_adev (dev);
@@ -1060,6 +1092,11 @@ int amdgpu_userq_ioctl(struct drm_device *dev, void *data,if (r)drm_file_err (filp,"Failed to get list of usermode queues\n" );break ; +case AMDGPU_USERQ_OP_CHANGE_ID : + r =amdgpu_userq_change_id (filp, args); +if (r) +drm_file_err (filp,"Failed to change queue id\n" ); +break ;default :drm_dbg_driver (dev,"Invalid user queue op specified: %d\n" , args->in.op);return -EINVAL ;
include/uapi/drm/amdgpu_drm.h +14 -3
@@ -330,9 +330,10 @@ union drm_amdgpu_ctx { };/* user queue IOCTL operations */ -#define AMDGPU_USERQ_OP_CREATE 1 -#define AMDGPU_USERQ_OP_FREE 2 -#define AMDGPU_USERQ_OP_LIST 3 +#define AMDGPU_USERQ_OP_CREATE 1 +#define AMDGPU_USERQ_OP_FREE 2 +#define AMDGPU_USERQ_OP_LIST 3 +#define AMDGPU_USERQ_OP_CHANGE_ID 4/* queue priority levels */ /* low < normal low < normal high < high */
@@ -463,10 +464,20 @@ struct drm_amdgpu_userq_list_in_out {__u64 entries; }; +struct drm_amdgpu_userq_change_id_in { +/** AMDGPU_USERQ_OP_CHANGE_ID */ +__u32 op; +/** Queue id of some queue */ +__u32 queue_id; +/** Queue id to change that queue to */ +__u32 new_queue_id; +}; +union drm_amdgpu_userq {struct drm_amdgpu_userq_in in;struct drm_amdgpu_userq_out out;struct drm_amdgpu_userq_list_in_out list_in_out; +struct drm_amdgpu_userq_change_id_in change_in; };/* GFX V11 IP specific MQD parameters */
Signature · 1 line
2.34.1
Re: [PATCH 3/3] drm/amdgpu: Add CHANGE_ID option for USERQ ioctl
Quoted 21 lines · David Francis
Add a new option to the USERQ ioctl, which is called with the
queue_idof an existing user queue and an unusedqueue_id, and changes that queue's id to the new value.The operation is fully protected by
userq_mutexto avoid races.Performing this operation on a queue with signals or waits outstanding is fine, as those hold not the
queue_idbut a direct reference to the queue object.Signed-off-by David Francis <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 37 +++++++++++++++++++++++ include/uapi/drm/amdgpu_drm.h | 17 +++++++++-- 2 files changed, 51 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 857
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-0@@ -852,6 +852,11 @@ static int amdgpu_userq_input_args_validate(struct drm_device *dev,break ;case AMDGPU_USERQ_OP_LIST :break ; +case AMDGPU_USERQ_OP_CHANGE_ID : +if (!args->change_in.new_queue_id || + args->change_in.new_queue_id >AMDGPU_MAX_USERQ_COUNT )
Maybe also check they are not the same? Currently same id will fail in xa_insert with -EBUSY which is maybe not what you intended.
Quoted 19 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +12
-0+return -EINVAL ; +break ;default :return -EINVAL ; }@@ -1011,6 +1016,33 @@ amdgpu_userq_list(struct drm_file *filp, union drm_amdgpu_userq *args)return ret; } +static int amdgpu_userq_change_id (struct drm_file *filp,union drm_amdgpu_userq *args) +{ +struct amdgpu_fpriv *fpriv = filp->driver_priv; +struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr; +struct amdgpu_usermode_queue *queue; +int ret =0 ; + +mutex_lock (&uq_mgr->userq_mutex); + + queue =amdgpu_userq_get (uq_mgr, args->change_in.queue_id);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_list(struct drm_file *filp, union drm_amdgpu_userq *args) · line 1038
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10
-0+if (!queue) { + ret = -EINVAL ; + goto unlock; + } + + ret =xa_insert (&uq_mgr->userq_xa, args->change_in.new_queue_id, queue,GFP_KERNEL ); +if (ret) + goto put; + +xa_erase (&uq_mgr->userq_xa, args->change_in.queue_id);
I guess here the mutex makes it easier although it is probably doable without it by preallocating and using the internal lock and advanced api.
Hmm.. I will have to implement the same thing for ctx handle rename any day now, I was planning to respin the render node CRIU RFC this week. Not that I think taking the mutex is a problem at all, just that my initial reaction was that in 1/3 it did not fix the race fully so maybe we don't even need it. But maybe it is easier with it.
I'll see there how doing it with internal lock only pans out in context rename.
Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7
-0+put: +amdgpu_userq_put (queue); +unlock: +mutex_unlock (&uq_mgr->userq_mutex); +return ret; +} +bool amdgpu_userq_enabled (struct drm_device *dev) {struct amdgpu_device *adev =drm_to_adev (dev);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_ioctl(struct drm_device *dev, void *data, · line 1098
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4
-0@@ -1060,6 +1092,11 @@ int amdgpu_userq_ioctl(struct drm_device *dev, void *data,if (r)drm_file_err (filp,"Failed to get list of usermode queues\n" );break ; +case AMDGPU_USERQ_OP_CHANGE_ID : + r =amdgpu_userq_change_id (filp, args); +if (r) +drm_file_err (filp,"Failed to change queue id\n" );
Remove the logging as in the previous patch. Or downgrade to debug if must be.
Regards,
Tvrtko
Quoted 43 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-0+break ;default :drm_dbg_driver (dev,"Invalid user queue op specified: %d\n" , args->in.op);return -EINVAL ;include/uapi/drm/amdgpu_drm.h +14
-3@@ -330,9 +330,10 @@ union drm_amdgpu_ctx { };/* user queue IOCTL operations */ -#define AMDGPU_USERQ_OP_CREATE 1 -#define AMDGPU_USERQ_OP_FREE 2 -#define AMDGPU_USERQ_OP_LIST 3 +#define AMDGPU_USERQ_OP_CREATE 1 +#define AMDGPU_USERQ_OP_FREE 2 +#define AMDGPU_USERQ_OP_LIST 3 +#define AMDGPU_USERQ_OP_CHANGE_ID 4/* queue priority levels */ /* low < normal low < normal high < high */ @@ -463,10 +464,20 @@ struct drm_amdgpu_userq_list_in_out {__u64 entries; }; +struct drm_amdgpu_userq_change_id_in { +/** AMDGPU_USERQ_OP_CHANGE_ID */ +__u32 op; +/** Queue id of some queue */ +__u32 queue_id; +/** Queue id to change that queue to */ +__u32 new_queue_id; +}; +union drm_amdgpu_userq {struct drm_amdgpu_userq_in in;struct drm_amdgpu_userq_out out;struct drm_amdgpu_userq_list_in_out list_in_out; +struct drm_amdgpu_userq_change_id_in change_in; };/* GFX V11 IP specific MQD parameters */