Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] drm/amdkfd: set_queue_as_reset() clears is_active before MES
  reconciliation
- [Medium] drm/amdkfd: kfd_dqm_reconcile_mes_queues() skips active
  unmapped queues
- [Medium] drm/amdkfd: incomplete process eviction in
  kfd_dqm_reconcile_mes_queues()
--

commit 57ed11ff8b41067e05295c9b1d33b02e617c5472
Author: Armas Spann <[email protected]>

drm/amdkfd: reconcile queue state with MES after a restart

This commit adds kfd_dqm_reconcile_mes_queues() to start_cpsch() to drop
stale MES queue state, release lost command tickets, and mark active queues
and their processes evicted after an MES firmware restart.

> 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 1c348f36cd3c3..f008b05e95993 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
> @@ -2649,6 +2649,74 @@ static int init_dequeue_wait_counts_mes(struct 
> device_queue_manager *dqm)
>       return 0;
>  }
>  
> +/*
> + * Bring the queue bookkeeping in line with MES when the scheduler starts.
> + *
> + * If the MES firmware was set up from scratch since a queue was added (GPU
> + * reset, resume), it has no record of that queue. A GPU reset cannot evict
> + * cleanly: the REMOVE_QUEUE commands fail with -EIO while the reset is
> + * running, and whatever was not marked inactive at that point stays active
> + * in the driver. The next REMOVE_QUEUE for such a queue, typically when its
> + * process exits, is rejected by the new firmware instance and that failure
> + * is escalated to another GPU reset.
> + *
> + * So drop the stale MES state here, and deactivate every queue that the
> + * driver still counts as running although MES has lost it. KFD does not
> + * resume processes after a GPU reset. Such a queue is marked evicted, and
> + * its process with it, which is the state kgd2kfd_pre_reset() leaves all
> + * the queues in that it did manage to evict. After a system resume
> + * kfd_resume_all_processes() maps the queues again from that state.
> + *
> + * dqm lock held.
> + */
> +static void kfd_dqm_reconcile_mes_queues(struct device_queue_manager *dqm)
> +{
> +     struct device *dev = dqm->dev->adev->dev;
> +     struct device_process_node *cur;
> +     struct qcm_process_device *qpd;
> +     unsigned int stale = 0, active = 0;
> +     struct queue *q;
> +
> +     if (!dqm->dev->kfd->shared_resources.enable_mes)
> +             return;
> +
> +     /* A command that got no answer died with the old firmware */
> +     kfd_mes_fenced(dqm);
> +
> +     list_for_each_entry(cur, &dqm->queues, list) {
> +             qpd = cur->qpd;
> +             list_for_each_entry(q, &qpd->queues_list, list) {
> +                     /* Not in MES, or added to the instance now running */
> +                     if (q->mes_state == KFD_MES_QUEUE_UNMAPPED ||
> +                         q->mes_fw_gen == kfd_mes_gen(dqm))
> +                             continue;

[Severity: Medium]
Can skipping queues with q->mes_state == KFD_MES_QUEUE_UNMAPPED leave an
unmapped queue marked q->properties.is_active == true across stop_cpsch() and
start_cpsch()?

When stop_cpsch() calls remove_all_kfd_queues_mes(), remove_queue_mes() 
transitions
any still-active queue to KFD_MES_QUEUE_UNMAPPED without clearing
q->properties.is_active (and add_queue_mes() similarly returns 0 without mapping
into MES while !dqm->sched_running).  Because start_cpsch() does not re-add 
active
queues when MES is enabled and restore_process_queues_cpsch() skips queues whose
q->properties.is_active is true, such a queue is never re-added to MES on 
resume.

> +
> +                     /* Also returns the status slot of a lost command */
> +                     kfd_mes_queue_retired(dqm, q);
> +                     stale++;
> +
> +                     if (!q->properties.is_active)
> +                             continue;

[Severity: High]
Does set_queue_as_reset() clear q->properties.is_active before
kfd_dqm_reconcile_mes_queues() runs on a GPU reset, preventing queues and
their qpd from being marked evicted here?

In kgd2kfd_post_reset(), kfd_dqm_set_queues_as_reset() runs before
kfd_resume() -> start_cpsch():

drivers/gpu/drm/amd/amdkfd/kfd_device.c:kgd2kfd_post_reset() {
    ...
        for (i = 0; i < kfd->num_nodes; i++)
                kfd_dqm_set_queues_as_reset(kfd->nodes[i]->dqm);

        for (i = 0; i < kfd->num_nodes; i++) {
                ret = kfd_resume(kfd->nodes[i]);
    ...
}

set_queue_as_reset() clears q->properties.is_active to false without setting
q->properties.is_evicted = true or qpd->evicted = 1.  When
kfd_dqm_reconcile_mes_queues() then runs, it retires q->mes_state to
KFD_MES_QUEUE_UNMAPPED and hits this !q->properties.is_active check, leaving
any queue or process that kgd2kfd_pre_reset() did not evict (for example,
after an earlier partial restore_process_queues_cpsch() failure left
qpd->evicted == 1 with active queues) un-evicted and eligible to be re-added
to MES.

> +
> +                     q->properties.is_active = false;
> +                     q->properties.is_evicted = true;
> +                     decrement_queue_count(dqm, qpd, q);
> +                     /* The process stays evicted from here on */
> +                     if (!qpd->evicted)
> +                             qpd->evicted = 1;
> +                     active++;
> +             }
> +     }

[Severity: Medium]
When this transitions qpd->evicted from 0 to 1, does it leave inactive
sibling queues on qpd->queues_list with q->properties.is_evicted == false
and leave pdd->last_evict_timestamp unset?

If a process has both a stale active queue and an inactive or suspended
queue (whose mes_state is KFD_MES_QUEUE_UNMAPPED or is_active is false),
only the active queue gets q->properties.is_evicted = true while
qpd->evicted becomes 1.  Because QUEUE_IS_ACTIVE(q->properties) checks
!q->properties.is_evicted rather than qpd->evicted, a subsequent
update_queue() or resume_single_queue() on the sibling queue will call
add_queue_mes() while the process is evicted, and a later
restore_process_queues_cpsch() will compute eviction duration from a zero or
stale pdd->last_evict_timestamp.

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

Reply via email to