Before system sleep, amdgpu_device_prepare() moves all BOs out of VRAM.
When it evicts a BO of a process with user queues, the eviction fence of
that process signals, and amdgpu_eviction_fence_suspend_worker() queues
the restore worker on system_wq. That workqueue is not frozen, so the
restore worker runs while the eviction is still going on and moves the
BOs back into VRAM, waiting for the page table updates while it holds
their reservations.

On an RX 9070 XT (gfx12) this hung S3 suspend for good after a few hours
of uptime. A restore worker waited forever on a page table update fence
while holding the reservations, and systemd-sleep waited in
ttm_bo_evict_first() for one of them.

Queue the restore worker on system_freezable_wq, like the amdkfd restore
workers. Its work is frozen before dpm_prepare(), so a restore queued
during the eviction stays pending until amdgpu_userq_suspend() cancels
it.

The processes are then still evicted at resume, so amdgpu_userq_resume()
must not map their queues: their BOs are not validated and their page
tables are out of date. Mapping them made MES time out on ADD_QUEUE and
the resume fail with -110. Queue the restore worker for them instead,
which validates the BOs, rearms the eviction fence and maps the queues
once the system is thawed.

Fixes: 44cfdf368fb7 ("drm/amdgpu: resume gfx userqueues")
Signed-off-by: Arisa Snowbell <[email protected]>
---
v3: bring back the resume change from v1: amdgpu_userq_resume() does not
    map the queues of processes whose BOs are evicted, and queues the
    restore worker for them instead. With v2 alone, resume mapped them
    with the BOs still evicted, MES timed out on ADD_QUEUE and the resume
    failed with -110.

v2: drop the restore block flag and the resume changes, only move the
    restore worker to system_freezable_wq (Christian). v1 claimed that
    for hibernation dpm_prepare() runs before freeze_kernel_threads(),
    which is wrong.

Tested with S3 on an RX 9070 XT, with the desktop (Hyprland) and a game
on user queues: suspend and resume complete and the queues keep running.
Rendering after resume is still corrupted (the game, and small artifacts
on the desktop), but that also happens with kernel queues; I'll report
it separately.

 .../drm/amd/amdgpu/amdgpu_eviction_fence.c    |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c     | 19 ++++++++++++++++++-
 2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c
index f8652fd0525d..18b11d7f457e 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c
@@ -91,7 +91,7 @@ amdgpu_eviction_fence_suspend_worker(struct work_struct *work)
        dma_fence_put(ev_fence);
 
        if (!evf_mgr->shutdown)
-               schedule_delayed_work(&uq_mgr->resume_work, 0);
+               queue_delayed_work(system_freezable_wq, &uq_mgr->resume_work, 
0);
 
        mutex_unlock(&uq_mgr->userq_mutex);
 }
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
index 9fc8e9e97595..4f41163a0a55 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
@@ -456,7 +456,7 @@ amdgpu_userq_ensure_ev_fence(struct amdgpu_userq_mgr 
*uq_mgr,
                 * Looks like there was no pending resume work,
                 * add one now to create a valid eviction fence
                 */
-               schedule_delayed_work(&uq_mgr->resume_work, 0);
+               queue_delayed_work(system_freezable_wq, &uq_mgr->resume_work, 
0);
                goto retry;
        }
        dma_fence_put(ev_fence);
@@ -1428,6 +1428,16 @@ int amdgpu_userq_suspend(struct amdgpu_device *adev)
        return 0;
 }
 
+static bool amdgpu_userq_evicted(struct amdgpu_userq_mgr *uq_mgr)
+{
+       struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
+       struct dma_fence *ev_fence = amdgpu_evf_mgr_get_fence(&fpriv->evf_mgr);
+       bool evicted = dma_fence_is_signaled(ev_fence);
+
+       dma_fence_put(ev_fence);
+       return evicted;
+}
+
 int amdgpu_userq_resume(struct amdgpu_device *adev)
 {
        struct amdgpu_usermode_queue *queue;
@@ -1438,6 +1448,13 @@ int amdgpu_userq_resume(struct amdgpu_device *adev)
        xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
                uqm = queue->userq_mgr;
                guard(mutex)(&uqm->userq_mutex);
+
+               /* The BOs moved, let the restore worker validate them first */
+               if (amdgpu_userq_evicted(uqm)) {
+                       queue_delayed_work(system_freezable_wq, 
&uqm->resume_work, 0);
+                       continue;
+               }
+
                if (adev->in_s0ix)
                        r = amdgpu_userq_restore_helper(queue);
                else
-- 
2.56.1

Reply via email to