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 right away. So the restore worker runs while the
eviction is still going on and does the opposite: it locks the VM and
its BOs, moves them back into VRAM and waits for the page table updates
while still holding the locks. amdgpu_userq_suspend() cancels the
restore worker, but only later, in amdgpu_device_suspend().
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. Why the fence never signaled is
not known, but the eviction should not depend on the restore at all.
Keep the restore out of system sleep:
- Prepare sets a flag that stops the eviction worker from queuing the
restore, then cancels any restore already queued. The worker checks
the flag under the userq_mutex, so prepare also takes and releases
each userq_mutex to wait for a worker that checked it just before.
- amdgpu_userq_resume() does not map the queues of a process whose BOs
were evicted, as its page tables are out of date. It queues the
restore worker instead, which validates the BOs, rearms the eviction
fence and maps the queues. It decides this under the userq_mutex,
which the eviction worker holds while it signals.
- Complete clears the flag and queues the restore for evicted processes,
unless the device is still suspended, as it is after the hibernation
snapshot. A failed prepare does the same.
- The restore worker runs on system_freezable_wq, like the amdkfd
restore workers, so a restore queued during resume waits until the
system is thawed.
The flag is needed besides the freezable workqueue because for
hibernation dpm_prepare() runs before freeze_kernel_threads().
amdgpu_userq_ensure_ev_fence() still queues the restore directly, since
only ioctls reach it and user space is frozen by then.
A restore worker that was already stuck before suspend started still
blocks it: hibernation waits for it in amdgpu_userq_block_restore(),
and for S3 and s2idle the freezer gives up after 20 seconds and aborts
the suspend instead of hanging.
Fixes: 44cfdf368fb7 ("drm/amdgpu: resume gfx userqueues")
Signed-off-by: Arisa Snowbell <[email protected]>
---
drivers/gpu/drm/amd/amdgpu/amdgpu.h | 1 +
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 13 +++-
.../drm/amd/amdgpu/amdgpu_eviction_fence.c | 2 +-
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 64 ++++++++++++++++++-
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 3 +
5 files changed, 79 insertions(+), 4 deletions(-)
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu.h
b/drivers/gpu/drm/amd/amdgpu/amdgpu.h
index c7577b608f3e..db1800421e37 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu.h
@@ -975,6 +975,7 @@ struct amdgpu_device {
bool apu_prefer_gtt;
bool userq_halt_for_enforce_isolation;
+ bool userq_restore_blocked;
struct amdgpu_uid *uid_info;
struct amdgpu_uma_carveout_info uma_info;
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c
index e3b3a2eebd95..01458a613e21 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c
@@ -2647,6 +2647,7 @@ static int amdgpu_pmops_prepare(struct device *dev)
{
struct drm_device *drm_dev = dev_get_drvdata(dev);
struct amdgpu_device *adev = drm_to_adev(drm_dev);
+ int r;
/* device maybe not resumed here, return immediately in this case */
if (adev->in_s4 && adev->in_suspend)
@@ -2665,12 +2666,20 @@ static int amdgpu_pmops_prepare(struct device *dev)
!amdgpu_acpi_is_s3_active(adev))
return 1;
- return amdgpu_device_prepare(drm_dev);
+ amdgpu_userq_block_restore(adev);
+ r = amdgpu_device_prepare(drm_dev);
+ if (r)
+ amdgpu_userq_unblock_restore(adev);
+
+ return r;
}
static void amdgpu_pmops_complete(struct device *dev)
{
- amdgpu_device_complete(dev_get_drvdata(dev));
+ struct drm_device *drm_dev = dev_get_drvdata(dev);
+
+ amdgpu_device_complete(drm_dev);
+ amdgpu_userq_unblock_restore(drm_to_adev(drm_dev));
}
static int amdgpu_pmops_suspend(struct device *dev)
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c
index f8652fd0525d..6c06c93d7f7f 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);
+ amdgpu_userq_schedule_resume(uq_mgr);
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..4dfe2d1e1b5c 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
@@ -428,6 +428,17 @@ static void amdgpu_userq_detach_doorbell(struct
amdgpu_usermode_queue *queue)
up_read(&adev->reset_domain->sem);
}
+static void amdgpu_userq_queue_restore(struct amdgpu_userq_mgr *uq_mgr)
+{
+ queue_delayed_work(system_freezable_wq, &uq_mgr->resume_work, 0);
+}
+
+void amdgpu_userq_schedule_resume(struct amdgpu_userq_mgr *uq_mgr)
+{
+ if (!READ_ONCE(uq_mgr->adev->userq_restore_blocked))
+ amdgpu_userq_queue_restore(uq_mgr);
+}
+
/**
* amdgpu_userq_ensure_ev_fence - ensure a valid, unsignaled eviction fence
exists
* @uq_mgr: the usermode queue manager for this process
@@ -456,7 +467,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);
+ amdgpu_userq_queue_restore(uq_mgr);
goto retry;
}
dma_fence_put(ev_fence);
@@ -1428,6 +1439,51 @@ 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;
+}
+
+void amdgpu_userq_block_restore(struct amdgpu_device *adev)
+{
+ struct amdgpu_usermode_queue *queue;
+ struct amdgpu_userq_mgr *uqm;
+ unsigned long queue_id;
+
+ WRITE_ONCE(adev->userq_restore_blocked, true);
+
+ xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
+ uqm = queue->userq_mgr;
+ /* Wait out an eviction worker that missed the flag */
+ mutex_lock(&uqm->userq_mutex);
+ mutex_unlock(&uqm->userq_mutex);
+ cancel_delayed_work_sync(&uqm->resume_work);
+ }
+}
+
+void amdgpu_userq_unblock_restore(struct amdgpu_device *adev)
+{
+ struct amdgpu_usermode_queue *queue;
+ struct amdgpu_userq_mgr *uqm;
+ unsigned long queue_id;
+
+ WRITE_ONCE(adev->userq_restore_blocked, false);
+ if (adev->in_suspend)
+ return;
+
+ xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
+ uqm = queue->userq_mgr;
+ guard(mutex)(&uqm->userq_mutex);
+ if (amdgpu_userq_evicted(uqm))
+ amdgpu_userq_queue_restore(uqm);
+ }
+}
+
int amdgpu_userq_resume(struct amdgpu_device *adev)
{
struct amdgpu_usermode_queue *queue;
@@ -1438,6 +1494,12 @@ 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);
+
+ if (amdgpu_userq_evicted(uqm)) {
+ amdgpu_userq_queue_restore(uqm);
+ continue;
+ }
+
if (adev->in_s0ix)
r = amdgpu_userq_restore_helper(queue);
else
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h
b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h
index 488dc21d7c81..41d26f95a306 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h
@@ -160,6 +160,7 @@ int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr
*userq_mgr, struct drm_file *f
void amdgpu_userq_mgr_cancel_reset_work(struct amdgpu_device *adev);
void amdgpu_userq_mgr_cancel_resume(struct amdgpu_userq_mgr *userq_mgr);
+void amdgpu_userq_schedule_resume(struct amdgpu_userq_mgr *uq_mgr);
void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr);
void amdgpu_userq_wait_for_signal(struct amdgpu_userq_mgr *uq_mgr);
@@ -173,6 +174,8 @@ bool amdgpu_userq_enabled(struct drm_device *dev);
int amdgpu_userq_suspend(struct amdgpu_device *adev);
int amdgpu_userq_resume(struct amdgpu_device *adev);
+void amdgpu_userq_block_restore(struct amdgpu_device *adev);
+void amdgpu_userq_unblock_restore(struct amdgpu_device *adev);
int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
u32 idx);
--
2.56.1