[PATCH 2/2] drm/imagination: quiesce queues before device teardown
From: Lewis Lakerink
Date: Wed Nov 25 2026 - 03:26:49 EST
pvr_remove() calls pvr_device_fini() before drm_dev_unplug() and does
not stop active or idle queues first:
* pvr_drm_driver_postclose() has no drm_dev_enter() guard, so closing
a file after removal runs pvr_fw_object_destroy() ->
pvr_meta_vm_unmap() against kernel_vm_ctx after pvr_device_gpu_fini()
has freed it;
* drm_sched_entity_destroy() does not wait for jobs on
sched->pending_list or cancel work_free_job and work_tdr, leaving
scheduler work and the timeout timer armed across destroy_workqueue()
and FW/VM teardown;
* an IRQ-driven reset via pvr_fwccb_process() can re-arm watchdog.work
before free_irq() runs in pvr_device_fini().
Reorder pvr_remove() so drm_dev_unplug() comes first and the queues are
stopped before device state is torn down:
* Stop the watchdog via pvr_watchdog_fini() first so it cannot race
pvr_dev->lost, then call drm_dev_unplug() when
!READ_ONCE(pvr_dev->lost) (pvr_device_lost() may already have
unplugged it, and drm_dev_unplug() is not idempotent) so in-flight
drm_dev_enter() sections finish and no new ones can start.
* Add pvr_queue_device_quiesce() to detach every queue from
queues.{active,idle} under queues.lock while pinning its context via
pvr_context_get_if_referenced(). Call drm_sched_entity_fini()
directly (avoiding drm_sched_entity_flush()'s wait_event_killable()
hang on non-exiting unbind tasks), then drm_sched_fini() to cancel
work_run_job, work_free_job and work_tdr. After drm_sched_fini(),
walk sched->pending_list and call pvr_job_release_pm_ref() on each
remaining job: those jobs still hold a pvr_job_get_pm_ref()
reference, and nothing else releases it once the scheduler is gone,
so without this the device's PM usage count never returns to zero
and every later runtime suspend is refused. Skip drm_sched_fini()
in pvr_queue_destroy() when sched_rq is already NULL.
* Guard postclose, pvr_ioctl_destroy_context() and
pvr_queue_timedout_job() with drm_dev_enter(), returning
DRM_GPU_SCHED_STAT_ENODEV on timeout after unplug.
* Run flush_workqueue(pvr_dev->sched_wq) while the GPU, job_ids, FW
objects and kernel_vm_ctx are still live, xa_destroy() the job and
free-list id arrays before pvr_device_fini(), and re-run
pvr_watchdog_fini() after free_irq() in case an IRQ-driven reset
re-armed it.
Runtime PM is deliberately unchanged: pm_runtime_suspend() still runs
after drm_dev_unplug(), so pvr_power_device_suspend() returns -EIO from
its drm_dev_enter() guard and latches runtime_error. That latch is what
keeps rpm_idle() from invoking pvr_power_device_idle() during devres
teardown, after pvr_device_fini() has unmapped the fwif_sysdata memory
that callback reads; making the suspend succeed removes the latch and
oopses there. The GPU is therefore left powered when it was runtime
active at removal, a known limitation rather than a use-after-free.
When postclose runs after unplug, detach pvr_file->contexts under
ctx_list_lock, xa_destroy() the four handle xarrays and kfree(pvr_file)
as required by drm_file_free(), deliberately leaking the underlying
device objects; the existing WARN_ON() checks in pvr_remove(),
pvr_fw_fini(), drm_mm_takedown() and pvr_context_device_fini() report
those leaks.
Fixes: 1f88f017e649 ("drm/imagination: Get GPU resources")
Cc: stable@xxxxxxxxxxxxxxx # 6.8+
Assisted-by: LLM
Signed-off-by: Lewis Lakerink <lewis@xxxxxxxxxx>
---
drivers/gpu/drm/imagination/pvr_drv.c | 73 +++++++++++++++++++++++--
drivers/gpu/drm/imagination/pvr_queue.c | 66 +++++++++++++++++++++-
drivers/gpu/drm/imagination/pvr_queue.h | 2 +
3 files changed, 135 insertions(+), 6 deletions(-)
diff --git a/drivers/gpu/drm/imagination/pvr_drv.c b/drivers/gpu/drm/imagination/pvr_drv.c
index 2d522a8..65c0747 100644
--- a/drivers/gpu/drm/imagination/pvr_drv.c
+++ b/drivers/gpu/drm/imagination/pvr_drv.c
@@ -724,10 +724,18 @@ pvr_ioctl_destroy_context(struct drm_device *drm_dev, void *raw_args,
struct drm_pvr_ioctl_destroy_context_args *args = raw_args;
struct pvr_file *pvr_file = file->driver_priv;
+ int idx, err;
+
if (args->_padding_4)
return -EINVAL;
- return pvr_context_destroy(pvr_file, args->handle);
+ if (!drm_dev_enter(drm_dev, &idx))
+ return -EIO;
+
+ err = pvr_context_destroy(pvr_file, args->handle);
+ drm_dev_exit(idx);
+
+ return err;
}
/**
@@ -1356,7 +1364,7 @@ pvr_drm_driver_open(struct drm_device *drm_dev, struct drm_file *file)
/**
* pvr_drm_driver_postclose() - One of the driver callbacks when a &struct
* drm_file is closed.
- * @drm_dev: [IN] DRM device (unused).
+ * @drm_dev: [IN] DRM device.
* @file: [IN] DRM file private data.
*
* Frees powervr-specific file private data (&struct pvr_file).
@@ -1364,10 +1372,35 @@ pvr_drm_driver_open(struct drm_device *drm_dev, struct drm_file *file)
* Registered in &pvr_drm_driver.
*/
static void
-pvr_drm_driver_postclose(__always_unused struct drm_device *drm_dev,
+pvr_drm_driver_postclose(struct drm_device *drm_dev,
struct drm_file *file)
{
struct pvr_file *pvr_file = to_pvr_file(file);
+ int idx;
+
+ /*
+ * If the device is already unplugged, pvr_remove() may have torn down
+ * sched_wq, kernel_vm_ctx and FW objects. Release only the per-file
+ * container bookkeeping (detaching file_link so leaked contexts cannot
+ * point into freed pvr_file) and deliberately leak the device objects.
+ */
+ if (!drm_dev_enter(drm_dev, &idx)) {
+ struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
+ struct pvr_context *ctx, *tmp;
+
+ spin_lock(&pvr_dev->ctx_list_lock);
+ list_for_each_entry_safe(ctx, tmp, &pvr_file->contexts, file_link)
+ list_del_init(&ctx->file_link);
+ spin_unlock(&pvr_dev->ctx_list_lock);
+
+ xa_destroy(&pvr_file->ctx_handles);
+ xa_destroy(&pvr_file->free_list_handles);
+ xa_destroy(&pvr_file->hwrt_handles);
+ xa_destroy(&pvr_file->vm_ctx_handles);
+ kfree(pvr_file);
+ file->driver_priv = NULL;
+ return;
+ }
/* Kill remaining contexts. */
pvr_destroy_contexts_for_file(pvr_file);
@@ -1379,6 +1412,8 @@ pvr_drm_driver_postclose(__always_unused struct drm_device *drm_dev,
kfree(pvr_file);
file->driver_priv = NULL;
+
+ drm_dev_exit(idx);
}
DEFINE_DRM_GEM_FOPS(pvr_drm_driver_fops);
@@ -1474,15 +1509,45 @@ static void pvr_remove(struct platform_device *plat_dev)
struct drm_device *drm_dev = platform_get_drvdata(plat_dev);
struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
+ /* Stop the watchdog first so it cannot race the pvr_dev->lost check. */
+ pvr_watchdog_fini(pvr_dev);
+
+ /*
+ * Unplug first so in-flight ioctls, postclose, timeouts and fence
+ * releases finish and no new ones (or runtime resumes) can start.
+ * pvr_device_lost() may already have unplugged it, and drm_dev_unplug()
+ * is not idempotent.
+ */
+ if (!READ_ONCE(pvr_dev->lost))
+ drm_dev_unplug(drm_dev);
+
+ /* Stop all queues and their drm_sched workers/timers before teardown. */
+ pvr_queue_device_quiesce(pvr_dev);
+
+ /*
+ * Drain pre-unplug pvr_queue_fence_release_work items while the GPU,
+ * kernel_vm_ctx, FW objects and job_ids are still alive.
+ */
+ flush_workqueue(pvr_dev->sched_wq);
+
WARN_ON(!xa_empty(&pvr_dev->job_ids));
WARN_ON(!xa_empty(&pvr_dev->free_list_ids));
xa_destroy(&pvr_dev->job_ids);
xa_destroy(&pvr_dev->free_list_ids);
+ /*
+ * Leave runtime PM to the driver core. This call happens after
+ * drm_dev_unplug(), so pvr_power_device_suspend() returns -EIO from
+ * its drm_dev_enter() guard and latches runtime_error. That latch is
+ * load-bearing: it makes rpm_idle() bail out before invoking
+ * pvr_power_device_idle(), which reads pvr_dev->fw_dev.fwif_sysdata
+ * memory that pvr_device_fini() has already unmapped below.
+ */
pm_runtime_suspend(drm_dev->dev);
+
pvr_device_fini(pvr_dev);
- drm_dev_unplug(drm_dev);
+ /* Cancel again in case an IRQ-driven reset re-armed watchdog before free_irq(). */
pvr_watchdog_fini(pvr_dev);
pvr_queue_device_fini(pvr_dev);
pvr_context_device_fini(pvr_dev);
diff --git a/drivers/gpu/drm/imagination/pvr_queue.c b/drivers/gpu/drm/imagination/pvr_queue.c
index 2a6bfb0..c8d8a22 100644
--- a/drivers/gpu/drm/imagination/pvr_queue.c
+++ b/drivers/gpu/drm/imagination/pvr_queue.c
@@ -881,9 +881,15 @@ pvr_queue_timedout_job(struct drm_sched_job *s_job)
struct pvr_device *pvr_dev = queue->ctx->pvr_dev;
struct pvr_job *job;
u32 job_count = 0;
+ int idx;
+
+ if (!drm_dev_enter(from_pvr_device(pvr_dev), &idx))
+ return DRM_GPU_SCHED_STAT_ENODEV;
- if (dma_fence_is_signaled(s_job->s_fence->parent))
+ if (dma_fence_is_signaled(s_job->s_fence->parent)) {
+ drm_dev_exit(idx);
return DRM_GPU_SCHED_STAT_NO_HANG;
+ }
dev_err(sched->dev, "Job timeout\n");
@@ -924,6 +930,7 @@ pvr_queue_timedout_job(struct drm_sched_job *s_job)
mutex_unlock(&pvr_dev->queues.lock);
drm_sched_start(sched, 0);
+ drm_dev_exit(idx);
return DRM_GPU_SCHED_STAT_RESET;
}
@@ -1469,7 +1476,8 @@ void pvr_queue_destroy(struct pvr_queue *queue, bool cleanup_queue_entity)
list_del_init(&queue->node);
mutex_unlock(&queue->ctx->pvr_dev->queues.lock);
- drm_sched_fini(&queue->scheduler);
+ if (queue->scheduler.sched_rq)
+ drm_sched_fini(&queue->scheduler);
if (cleanup_queue_entity)
drm_sched_entity_fini(&queue->entity);
@@ -1512,6 +1520,60 @@ int pvr_queue_device_init(struct pvr_device *pvr_dev)
return 0;
}
+/**
+ * pvr_queue_device_quiesce() - Stop all remaining queues during device removal.
+ * @pvr_dev: The device to quiesce.
+ *
+ * Detaches every registered queue from @pvr_dev->queues.{active,idle} so the
+ * IRQ handler cannot signal fences or re-queue free_job work, tears down its
+ * scheduler entity without blocking in drm_sched_entity_flush(), finalises its
+ * scheduler so work_run_job, work_free_job and work_tdr are stopped, and
+ * releases runtime PM references held by remaining in-flight jobs before
+ * device teardown. Pins each queue's context while operating on it to avoid
+ * racing with concurrent pvr_queue_destroy().
+ */
+void pvr_queue_device_quiesce(struct pvr_device *pvr_dev)
+{
+ struct pvr_queue *queue;
+
+ for (;;) {
+ struct pvr_context *ctx = NULL;
+
+ mutex_lock(&pvr_dev->queues.lock);
+ queue = list_first_entry_or_null(&pvr_dev->queues.active,
+ struct pvr_queue, node);
+ if (!queue)
+ queue = list_first_entry_or_null(&pvr_dev->queues.idle,
+ struct pvr_queue, node);
+ if (queue) {
+ list_del_init(&queue->node);
+ if (pvr_context_get_if_referenced(queue->ctx))
+ ctx = queue->ctx;
+ }
+ mutex_unlock(&pvr_dev->queues.lock);
+
+ if (!queue)
+ break;
+ if (!ctx)
+ continue;
+
+ if (!queue->entity.stopped) {
+ drm_sched_entity_fini(&queue->entity);
+ dma_fence_put(queue->last_queued_job_scheduled_fence);
+ queue->last_queued_job_scheduled_fence = NULL;
+ }
+ if (queue->scheduler.sched_rq) {
+ struct pvr_job *job;
+
+ drm_sched_fini(&queue->scheduler);
+ list_for_each_entry(job, &queue->scheduler.pending_list, base.list)
+ pvr_job_release_pm_ref(job);
+ }
+
+ pvr_context_put(ctx);
+ }
+}
+
/**
* pvr_queue_device_fini() - Device-level cleanup of queue related fields.
* @pvr_dev: The device to cleanup.
diff --git a/drivers/gpu/drm/imagination/pvr_queue.h b/drivers/gpu/drm/imagination/pvr_queue.h
index 149cc6d..fab168e 100644
--- a/drivers/gpu/drm/imagination/pvr_queue.h
+++ b/drivers/gpu/drm/imagination/pvr_queue.h
@@ -168,6 +168,8 @@ void pvr_queue_device_post_reset(struct pvr_device *pvr_dev);
int pvr_queue_device_init(struct pvr_device *pvr_dev);
+void pvr_queue_device_quiesce(struct pvr_device *pvr_dev);
+
void pvr_queue_device_fini(struct pvr_device *pvr_dev);
#endif /* PVR_QUEUE_H */
--
2.43.0