A GPU reset restarts the MES firmware, which then knows no user queues.
KFD keeps its processes, in an evicted state, until they exit. Their
queues cannot be removed cleanly on the way into the reset, because
every REMOVE_QUEUE fails with -EIO at that point.

On an R9700 this showed up as resets that come in pairs. A process that
had lived through a GPU reset exited 62 s later. KFD sent REMOVE_QUEUE
for queues the new firmware had never seen, MES rejected each command
within a fraction of a millisecond, and KFD reset the GPU again.

The per-queue MES state already stops those commands one queue at a
time. Put the driver state right in one place as well: when the
scheduler starts, walk the queues of every process, drop the MES state
that belongs to an earlier firmware instance, release the status slots
of commands that were lost with it, and lift the fence that was raised
for such a command.

The queues are inactive by then. Every path that restarts the firmware
with live processes evicts them first, through kgd2kfd_pre_reset() or
kgd2kfd_suspend(), and set_queue_as_reset() takes care of what a
partial eviction left active. The reconcile does not deactivate queues
itself; a queue it finds still marked active is reported, because a
restart path has then forgotten to evict.

Signed-off-by: Armas Spann <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
---
 .../drm/amd/amdkfd/kfd_device_queue_manager.c | 62 +++++++++++++++++++
 1 file changed, 62 insertions(+)

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 a75c88356..41e97f74e 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
@@ -2756,6 +2756,66 @@ 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. 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, return the status slots of commands
+ * that were lost with the old firmware, and lift the fence raised for one
+ * of them. The queues themselves are inactive at this point: every path
+ * that restarts the firmware with live processes evicts them first, and
+ * set_queue_as_reset() deactivates what a partial eviction left active.
+ * A queue that is still marked active means a restart path forgot to
+ * evict; say so, but leave the counters to the paths that own them.
+ *
+ * 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;
+
+                       /* Also returns the status slot of a lost command */
+                       kfd_mes_queue_retired(dqm, q);
+                       stale++;
+
+                       if (q->properties.is_active)
+                               active++;
+               }
+       }
+
+       if (active)
+               dev_warn(dev,
+                        "MES was restarted with %u queue(s) still marked 
active, they were not evicted\n",
+                        active);
+       if (stale)
+               dev_info(dev, "MES was restarted, forgot %u user queue(s)\n",
+                        stale);
+}
+
 static int start_cpsch(struct device_queue_manager *dqm)
 {
        struct device *dev = dqm->dev->adev->dev;
@@ -2820,6 +2880,8 @@ static int start_cpsch(struct device_queue_manager *dqm)
                hqd_info_size * sizeof(struct amdgpu_mes_hung_queue_hqd_info),
                GFP_KERNEL);
 
+       kfd_dqm_reconcile_mes_queues(dqm);
+
        /* A restart of the MES firmware retires every quarantined queue */
        if (!list_empty(&dqm->zombie_queues))
                mod_delayed_work(system_dfl_wq, &dqm->zombie_work, 0);
-- 
2.47.3

Reply via email to