Skip to content

Commit eb5723a

Browse files
Thomas Hellströmrodrigovivi
authored andcommitted
drm/xe: Block exec and rebind worker while evicting for suspend / hibernate
When the xe pm_notifier evicts for suspend / hibernate, there might be racing tasks trying to re-validate again. This can lead to suspend taking excessive time or get stuck in a live-lock. This behaviour becomes much worse with the fix that actually makes re-validation bring back bos to VRAM rather than letting them remain in TT. Prevent that by having exec and the rebind worker waiting for a completion that is set to block by the pm_notifier before suspend and is signaled by the pm_notifier after resume / wakeup. It's probably still possible to craft malicious applications that block suspending. More work is pending to fix that. v3: - Avoid wait_for_completion() in the kernel worker since it could potentially cause work item flushes from freezable processes to wait forever. Instead terminate the rebind workers if needed and re-launch at resume. (Matt Auld) v4: - Fix some bad naming and leftover debug printouts. - Fix kerneldoc. - Use drmm_mutex_init() for the xe->rebind_resume_lock (Matt Auld). - Rework the interface of xe_vm_rebind_resume_worker (Matt Auld). Link: https://gitlab.freedesktop.org/drm/xe/kernel/-/issues/4288 Fixes: c6a4d46 ("drm/xe: evict user memory in PM notifier") Cc: Matthew Auld <matthew.auld@intel.com> Cc: Rodrigo Vivi <rodrigo.vivi@intel.com> Cc: <stable@vger.kernel.org> # v6.16+ Signed-off-by: Thomas Hellström <thomas.hellstrom@linux.intel.com> Reviewed-by: Matthew Auld <matthew.auld@intel.com> Link: https://lore.kernel.org/r/20250904160715.2613-4-thomas.hellstrom@linux.intel.com (cherry picked from commit 5993345) Signed-off-by: Rodrigo Vivi <rodrigo.vivi@intel.com>
1 parent d848203 commit eb5723a

File tree

6 files changed

+88
-1
lines changed

6 files changed

+88
-1
lines changed

drivers/gpu/drm/xe/xe_device_types.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,12 @@ struct xe_device {
553553

554554
/** @pm_notifier: Our PM notifier to perform actions in response to various PM events. */
555555
struct notifier_block pm_notifier;
556+
/** @pm_block: Completion to block validating tasks on suspend / hibernate prepare */
557+
struct completion pm_block;
558+
/** @rebind_resume_list: List of wq items to kick on resume. */
559+
struct list_head rebind_resume_list;
560+
/** @rebind_resume_lock: Lock to protect the rebind_resume_list */
561+
struct mutex rebind_resume_lock;
556562

557563
/** @pmt: Support the PMT driver callback interface */
558564
struct {

drivers/gpu/drm/xe/xe_exec.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,15 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
237237
goto err_unlock_list;
238238
}
239239

240+
/*
241+
* It's OK to block interruptible here with the vm lock held, since
242+
* on task freezing during suspend / hibernate, the call will
243+
* return -ERESTARTSYS and the IOCTL will be rerun.
244+
*/
245+
err = wait_for_completion_interruptible(&xe->pm_block);
246+
if (err)
247+
goto err_unlock_list;
248+
240249
vm_exec.vm = &vm->gpuvm;
241250
vm_exec.flags = DRM_EXEC_INTERRUPTIBLE_WAIT;
242251
if (xe_vm_in_lr_mode(vm)) {

drivers/gpu/drm/xe/xe_pm.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "xe_pcode.h"
2525
#include "xe_pxp.h"
2626
#include "xe_trace.h"
27+
#include "xe_vm.h"
2728
#include "xe_wa.h"
2829

2930
/**
@@ -290,6 +291,19 @@ static u32 vram_threshold_value(struct xe_device *xe)
290291
return DEFAULT_VRAM_THRESHOLD;
291292
}
292293

294+
static void xe_pm_wake_rebind_workers(struct xe_device *xe)
295+
{
296+
struct xe_vm *vm, *next;
297+
298+
mutex_lock(&xe->rebind_resume_lock);
299+
list_for_each_entry_safe(vm, next, &xe->rebind_resume_list,
300+
preempt.pm_activate_link) {
301+
list_del_init(&vm->preempt.pm_activate_link);
302+
xe_vm_resume_rebind_worker(vm);
303+
}
304+
mutex_unlock(&xe->rebind_resume_lock);
305+
}
306+
293307
static int xe_pm_notifier_callback(struct notifier_block *nb,
294308
unsigned long action, void *data)
295309
{
@@ -299,6 +313,7 @@ static int xe_pm_notifier_callback(struct notifier_block *nb,
299313
switch (action) {
300314
case PM_HIBERNATION_PREPARE:
301315
case PM_SUSPEND_PREPARE:
316+
reinit_completion(&xe->pm_block);
302317
xe_pm_runtime_get(xe);
303318
err = xe_bo_evict_all_user(xe);
304319
if (err)
@@ -315,6 +330,8 @@ static int xe_pm_notifier_callback(struct notifier_block *nb,
315330
break;
316331
case PM_POST_HIBERNATION:
317332
case PM_POST_SUSPEND:
333+
complete_all(&xe->pm_block);
334+
xe_pm_wake_rebind_workers(xe);
318335
xe_bo_notifier_unprepare_all_pinned(xe);
319336
xe_pm_runtime_put(xe);
320337
break;
@@ -341,6 +358,14 @@ int xe_pm_init(struct xe_device *xe)
341358
if (err)
342359
return err;
343360

361+
err = drmm_mutex_init(&xe->drm, &xe->rebind_resume_lock);
362+
if (err)
363+
goto err_unregister;
364+
365+
init_completion(&xe->pm_block);
366+
complete_all(&xe->pm_block);
367+
INIT_LIST_HEAD(&xe->rebind_resume_list);
368+
344369
/* For now suspend/resume is only allowed with GuC */
345370
if (!xe_device_uc_enabled(xe))
346371
return 0;

drivers/gpu/drm/xe/xe_vm.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,9 @@ static int xe_gpuvm_validate(struct drm_gpuvm_bo *vm_bo, struct drm_exec *exec)
393393
list_move_tail(&gpuva_to_vma(gpuva)->combined_links.rebind,
394394
&vm->rebind_list);
395395

396+
if (!try_wait_for_completion(&vm->xe->pm_block))
397+
return -EAGAIN;
398+
396399
ret = xe_bo_validate(gem_to_xe_bo(vm_bo->obj), vm, false);
397400
if (ret)
398401
return ret;
@@ -479,6 +482,33 @@ static int xe_preempt_work_begin(struct drm_exec *exec, struct xe_vm *vm,
479482
return xe_vm_validate_rebind(vm, exec, vm->preempt.num_exec_queues);
480483
}
481484

485+
static bool vm_suspend_rebind_worker(struct xe_vm *vm)
486+
{
487+
struct xe_device *xe = vm->xe;
488+
bool ret = false;
489+
490+
mutex_lock(&xe->rebind_resume_lock);
491+
if (!try_wait_for_completion(&vm->xe->pm_block)) {
492+
ret = true;
493+
list_move_tail(&vm->preempt.pm_activate_link, &xe->rebind_resume_list);
494+
}
495+
mutex_unlock(&xe->rebind_resume_lock);
496+
497+
return ret;
498+
}
499+
500+
/**
501+
* xe_vm_resume_rebind_worker() - Resume the rebind worker.
502+
* @vm: The vm whose preempt worker to resume.
503+
*
504+
* Resume a preempt worker that was previously suspended by
505+
* vm_suspend_rebind_worker().
506+
*/
507+
void xe_vm_resume_rebind_worker(struct xe_vm *vm)
508+
{
509+
queue_work(vm->xe->ordered_wq, &vm->preempt.rebind_work);
510+
}
511+
482512
static void preempt_rebind_work_func(struct work_struct *w)
483513
{
484514
struct xe_vm *vm = container_of(w, struct xe_vm, preempt.rebind_work);
@@ -502,6 +532,11 @@ static void preempt_rebind_work_func(struct work_struct *w)
502532
}
503533

504534
retry:
535+
if (!try_wait_for_completion(&vm->xe->pm_block) && vm_suspend_rebind_worker(vm)) {
536+
up_write(&vm->lock);
537+
return;
538+
}
539+
505540
if (xe_vm_userptr_check_repin(vm)) {
506541
err = xe_vm_userptr_pin(vm);
507542
if (err)
@@ -1714,6 +1749,7 @@ struct xe_vm *xe_vm_create(struct xe_device *xe, u32 flags, struct xe_file *xef)
17141749
if (flags & XE_VM_FLAG_LR_MODE) {
17151750
INIT_WORK(&vm->preempt.rebind_work, preempt_rebind_work_func);
17161751
xe_pm_runtime_get_noresume(xe);
1752+
INIT_LIST_HEAD(&vm->preempt.pm_activate_link);
17171753
}
17181754

17191755
if (flags & XE_VM_FLAG_FAULT_MODE) {
@@ -1895,8 +1931,12 @@ void xe_vm_close_and_put(struct xe_vm *vm)
18951931
xe_assert(xe, !vm->preempt.num_exec_queues);
18961932

18971933
xe_vm_close(vm);
1898-
if (xe_vm_in_preempt_fence_mode(vm))
1934+
if (xe_vm_in_preempt_fence_mode(vm)) {
1935+
mutex_lock(&xe->rebind_resume_lock);
1936+
list_del_init(&vm->preempt.pm_activate_link);
1937+
mutex_unlock(&xe->rebind_resume_lock);
18991938
flush_work(&vm->preempt.rebind_work);
1939+
}
19001940
if (xe_vm_in_fault_mode(vm))
19011941
xe_svm_close(vm);
19021942

drivers/gpu/drm/xe/xe_vm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ struct dma_fence *xe_vm_bind_kernel_bo(struct xe_vm *vm, struct xe_bo *bo,
273273
struct xe_exec_queue *q, u64 addr,
274274
enum xe_cache_level cache_lvl);
275275

276+
void xe_vm_resume_rebind_worker(struct xe_vm *vm);
277+
276278
/**
277279
* xe_vm_resv() - Return's the vm's reservation object
278280
* @vm: The vm

drivers/gpu/drm/xe/xe_vm_types.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,11 @@ struct xe_vm {
293293
* BOs
294294
*/
295295
struct work_struct rebind_work;
296+
/**
297+
* @preempt.pm_activate_link: Link to list of rebind workers to be
298+
* kicked on resume.
299+
*/
300+
struct list_head pm_activate_link;
296301
} preempt;
297302

298303
/** @um: unified memory state */

0 commit comments

Comments
 (0)