evict_process_queues_cpsch() returns at the first REMOVE_QUEUE that
fails. The remaining queues are neither removed nor marked inactive and
the TLB flush is skipped, while qpd->evicted is already raised. During a
GPU reset every REMOVE_QUEUE fails with -EIO, so all queues but the
first stay marked active across the reset.

kfd_process_evict_queues() returns the result of the last device. When
an earlier device reported -EIO, the caller is told that everything was
saved, signals the eviction fence and schedules a restore for a process
that still has a queue on the hardware of that device.

restore_process_queues_cpsch() marks a queue active and counts it before
ADD_QUEUE is sent. When a later queue fails, qpd->evicted stays set, and
the retry counts the queues that were restored already and adds them
again. Until the retry, the process is half restored: an eviction that
comes first finds qpd->evicted set and leaves the restored queues
running while their buffers are moved.

Evict every queue and report MES failures as -EIO, which
kfd_process_evict_queues() already takes to mean "evicted, do not add
back", and let it keep that answer when any device gave it. On a failed
restore, take the queues restored in this attempt off the hardware
again, so that the process is fully evicted and the retry starts from a
known state. A queue is counted before ADD_QUEUE is sent, because the
oversubscription check in add_queue_mes() wants to see it, and taken
out of the count again when the command fails.

Signed-off-by: Armas Spann <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
---
 .../drm/amd/amdkfd/kfd_device_queue_manager.c | 57 +++++++++++++++----
 drivers/gpu/drm/amd/amdkfd/kfd_process.c      |  7 ++-
 2 files changed, 52 insertions(+), 12 deletions(-)

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 c691df753..eb75858bd 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
@@ -1984,11 +1984,24 @@ static int evict_process_queues_cpsch(struct 
device_queue_manager *dqm,
                decrement_queue_count(dqm, qpd, q);
 
                if (dqm->dev->kfd->shared_resources.enable_mes) {
-                       retval = remove_queue_mes(dqm, q, qpd);
-                       if (retval) {
-                               dev_err(dev, "Failed to evict queue %d\n",
-                                       q->properties.queue_id);
-                               goto out;
+                       int r = remove_queue_mes(dqm, q, qpd);
+
+                       if (r) {
+                               /*
+                                * -EIO means a GPU reset is under way or MES
+                                * is fenced off, both reported already.
+                                */
+                               if (r != -EIO)
+                                       dev_err(dev, "Failed to evict queue 
%d\n",
+                                               q->properties.queue_id);
+                               /*
+                                * Keep going so that every queue ends up in
+                                * the evicted state. The caller treats -EIO
+                                * as evicted, not to be added back. MES may
+                                * still use this MQD, so it stays pinned.
+                                */
+                               retval = -EIO;
+                               continue;
                        }
                }
 
@@ -2201,22 +2214,25 @@ static int restore_process_queues_cpsch(struct 
device_queue_manager *dqm,
                if (!QUEUE_IS_ACTIVE(q->properties))
                        continue;
 
-               q->properties.is_active = true;
-               increment_queue_count(dqm, &pdd->qpd, q);
-
                retval = dqm_repin_mqd_bo(dqm, q);
                if (retval) {
                        dev_err(dev, "Failed to repin MQD for queue %d\n",
                                q->properties.queue_id);
-                       goto out;
+                       goto rollback;
                }
 
+               /* Counted first: add_queue_mes() looks at the active count */
+               q->properties.is_active = true;
+               increment_queue_count(dqm, &pdd->qpd, q);
+
                if (dqm->dev->kfd->shared_resources.enable_mes) {
                        retval = add_queue_mes(dqm, q, qpd);
                        if (retval) {
                                dev_err(dev, "Failed to restore queue %d\n",
                                        q->properties.queue_id);
-                               goto out;
+                               q->properties.is_active = false;
+                               decrement_queue_count(dqm, &pdd->qpd, q);
+                               goto rollback;
                        }
                }
        }
@@ -2230,6 +2246,27 @@ static int restore_process_queues_cpsch(struct 
device_queue_manager *dqm,
 out:
        dqm_unlock(dqm);
        return retval;
+
+rollback:
+       /*
+        * A half restored process is a problem for the next eviction: it
+        * finds qpd->evicted set and leaves the queues restored so far
+        * running. Take those off the hardware again, so that the process
+        * is fully evicted and the retry starts from a known state. A
+        * removal that cannot be sent (-EIO) leaves the queue mapped and
+        * marked evicted, as evict_process_queues_cpsch() does.
+        */
+       list_for_each_entry(q, &qpd->queues_list, list) {
+               q->properties.is_evicted = true;
+               if (!q->properties.is_active)
+                       continue;
+
+               q->properties.is_active = false;
+               decrement_queue_count(dqm, qpd, q);
+               if (dqm->dev->kfd->shared_resources.enable_mes)
+                       remove_queue_mes(dqm, q, qpd);
+       }
+       goto out;
 }
 
 static int register_process(struct device_queue_manager *dqm,
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_process.c 
b/drivers/gpu/drm/amd/amdkfd/kfd_process.c
index 51c3fa39f..546aeef04 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_process.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_process.c
@@ -2134,7 +2134,7 @@ struct kfd_process *kfd_lookup_process_by_id(const struct 
mm_struct *mm, u16 id)
  */
 int kfd_process_evict_queues(struct kfd_process *p, uint32_t trigger)
 {
-       int r = 0;
+       int r = 0, ret = 0;
        int i;
        unsigned int n_evicted = 0;
 
@@ -2155,12 +2155,15 @@ int kfd_process_evict_queues(struct kfd_process *p, 
uint32_t trigger)
                        dev_err(dev, "Failed to evict process queues\n");
                        goto fail;
                }
+               /* One device that could not save its queues is enough */
+               if (r)
+                       ret = r;
                n_evicted++;
 
                pdd->dev->dqm->is_hws_hang = false;
        }
 
-       return r;
+       return ret;
 
 fail:
        /* To keep state consistent, roll back partial eviction by
-- 
2.47.3

Reply via email to