Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] drm/amdkfd: unretired queue visibility gap in
  process_termination_cpsch()
- [Medium] drm/amdkfd: unbalanced SVM queue_refcount put when
  kfd_queue_hold_buffers() fails
--

commit 1de59d68e639f24ab5a36271e17baa8bd9d8fd34
Author: Armas Spann <[email protected]>

drm/amdkfd: keep queue storage until MES confirmed retirement

This commit quarantines queues whose retirement MES has not confirmed during
queue creation error cleanup, queue destruction, and process termination,
keeping their MQDs, IDs, and user buffer references until MES releases them.

> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c 
> b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
> index 0c72cf55224cc..eabb9a6953451 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c

[ ... ]

> @@ -3813,7 +3846,22 @@ static int process_termination_cpsch(struct 
> device_queue_manager *dqm,
>               mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
>                               q->properties.type)];
>               list_del(&q->list);
> +             q->dqm_dequeued = true;
>               qpd->queue_count--;
> +
> +             /*
> +              * Decide once whether MES has let go. A late completion can
> +              * change the answer at any time, and the SDMA id has to
> +              * follow the same decision as the MQD.
> +              */
> +             if (!kfd_mes_queue_retired(dqm, q)) {
> +                     dqm_quarantine_queue(dqm, qpd, q);
> +                     continue;
> +             }
> +             if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
> +                 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
> +                     deallocate_sdma_queue(dqm, q);
> +
>               dqm_unlock(dqm);
>               /* Repin the MQD BO if still evicted for hibernation, before 
> free. */
>               dqm_repin_mqd_bo(dqm, q);

[Severity: Medium]
Can an unretired queue in qpd->queues_list be missed by a concurrent MES
per-queue reset while dqm_lock(dqm) is dropped to free an earlier retired
queue's MQD?

process_termination_cpsch() unlinks qpd from dqm->queues before running this
drain loop, and find_queue_by_doorbell_offset() only searches dqm->queues and
dqm->zombie_queues:

CPU0                                  CPU1
process_termination_cpsch()
  /* unlink qpd from dqm->queues */
  /* q1 is retired, q2 is not */
  list_del(&q1->list)
  dqm_unlock(dqm)
                                      kfd_reset_queue_mes()
                                        dqm_lock(dqm)
                                        find_queue_by_doorbell_offset(..., q2)
                                          /* not in dqm->queues or
                                           * dqm->zombie_queues -> NULL
                                           */
                                        dqm_unlock(dqm)
  mqd_mgr->free_mqd(..., q1->mqd, ...)
  dqm_lock(dqm)
  list_del(&q2->list)
  dqm_quarantine_queue(dqm, qpd, q2)

If MES reports q2 as hung during that window, kfd_reset_queue_mes() fails to
find q2 and skips resetting and removing it in MES.  Moving unretired queues
to dqm->zombie_queues before dropping dqm_lock(dqm), or unlinking qpd from
dqm->queues after qpd->queues_list is drained, would keep q2 visible.

> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c 
> b/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c
> index ef9ad6dc874dc..e44ccc803024c 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c

[ ... ]

> @@ -518,7 +530,23 @@ int pqm_create_queue(struct process_queue_manager *pqm,
>       return retval;
>  
>  err_create_queue:
> -     uninit_queue(q);
> +     if (q && q->is_zombie) {
> +             /*
> +              * The dqm still needs the MQD, the ids and what MES uses. The
> +              * user buffers are part of that: MES may yet execute the
> +              * ADD_QUEUE and run the queue from them. The ioctl drops its
> +              * references on them when this returns, so take our own. The
> +              * reaper drops them once MES has let go of the queue.
> +              */
> +             if (kfd_queue_hold_buffers(pdd, &q->properties))
> +                     pr_warn("process pid %d: quarantined queue holds no 
> reference on its CWSR area\n",
> +                             pqm->process->lead_thread->pid);
> +             kfd_dqm_zombie_ready(dev->dqm, q);

[Severity: Medium]
If kfd_queue_hold_buffers() fails in kfd_queue_buffer_svm_get(), will the
zombie reaper later underflow prange->queue_refcount?

When kfd_queue_hold_buffers() returns an error, no extra SVM reference is
taken for the quarantined queue, and kfd_ioctl_create_queue() drops the
initial reference taken by kfd_queue_acquire_buffers().  Because
q->properties.ctx_save_restore_area_address and ctx_save_restore_area_size
remain set, kfd_dqm_release_zombie() -> kfd_queue_release_buffers() ->
kfd_queue_buffer_svm_put() still decrements prange->queue_refcount when the
zombie queue is reaped:

drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c:kfd_dqm_release_zombie() {
    ...
        mutex_lock(&p->mutex);
        deallocate_doorbell(&pdd->qpd, q);
        kfd_queue_unref_bo_vas(pdd, &q->properties);
        kfd_queue_release_buffers(pdd, &q->properties, false);
        mutex_unlock(&p->mutex);
    ...
}

If another queue of the process shares or reuses that SVM range for its CWSR
area before the zombie is reaped, its prange->queue_refcount is decremented
early, allowing the range to be unmapped without evicting the active queue.
Clearing q->properties.ctx_save_restore_area_address (or zeroing
ctx_save_restore_area_size) when kfd_queue_buffer_svm_get() fails would avoid
the unmatched put.

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

Reply via email to