Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [High] drm/amdgpu/userq: unlocked queue state and unmap/map in
  mes_userq_reset() [1]
--

commit 8f1348ec761b7ada9e6ca46fe71c338002a3662a
Author: Alex Deucher <[email protected]>

drm/amdgpu/userq: implement support for query status

This commit implements the AMDGPU_USERQ_OP_QUERY_STATUS operation to report
when a usermode queue is hung or its VM generation has changed due to lost
VRAM.

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c 
> b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
> index ed6f3b67e8720..496137fca1f0c 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
[ ... ]
> @@ -817,6 +818,33 @@ amdgpu_userq_create(struct drm_file *filp, union 
> drm_amdgpu_userq *args)
>       return r;
>  }
>  
> +static int
> +amdgpu_userq_query_status(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_device *adev = uq_mgr->adev;
> +     struct amdgpu_usermode_queue *queue;
> +     int queue_id = args->in.queue_id;
> +
> +     mutex_lock(&uq_mgr->userq_mutex);
> +
> +     queue = amdgpu_userq_get(uq_mgr, queue_id);
> +     if (!queue) {
> +             mutex_unlock(&uq_mgr->userq_mutex);
> +             return -EINVAL;
> +     }
> +
> +     args->out_qs.flags = 0;
> +     if (queue->state == AMDGPU_USERQ_STATE_HUNG ||
> +         queue->generation != amdgpu_vm_generation(adev, &fpriv->vm))
> +             args->out_qs.flags |= AMDGPU_USERQ_QUERY_STATUS_FLAGS_BROKEN;

[Severity: High]
This problem wasn't introduced by this patch, but can a concurrent eviction
in amdgpu_userq_unmap_helper() race with mes_userq_reset_hw() [1] and
overwrite AMDGPU_USERQ_STATE_HUNG?

amdgpu_userq_query_status() and amdgpu_userq_unmap_helper() serialize
queue->state under uq_mgr->userq_mutex, whereas mes_userq_reset_hw() unmaps
the queue and sets queue->state = AMDGPU_USERQ_STATE_HUNG without holding
userq_mutex:

CPU0                                  CPU1
amdgpu_userq_hang_detect_work()       amdgpu_eviction_fence_suspend_worker()
  mes_userq_reset()                     mutex_lock(&uq_mgr->userq_mutex)
    mes_userq_reset_hw()                amdgpu_userq_evict_all()
      mes_userq_unmap(queue)              amdgpu_userq_unmap_helper(queue)
                                            // sees AMDGPU_USERQ_STATE_MAPPED
                                            mes_userq_unmap(queue)
      queue->state =
        AMDGPU_USERQ_STATE_HUNG
                                            queue->state =
                                              AMDGPU_USERQ_STATE_UNMAPPED
                                              <-- clobbers HUNG state
                                        mutex_unlock(&uq_mgr->userq_mutex)

Both paths call mes_userq_unmap() concurrently, issuing duplicate
remove_hw_queue MES packets and double-freeing queue->gang_ctx_array_index.
Overwriting AMDGPU_USERQ_STATE_HUNG with AMDGPU_USERQ_STATE_UNMAPPED also
hides the broken status from amdgpu_userq_query_status() and allows
amdgpu_userq_restore_worker() to remap and re-execute the hung queue.

[1] https://sashiko.dev/#/bug/linux-ea51473b-6c36-421a-880f-c4fff09847a7

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=2

Reply via email to