Skip to contentSkip to conversation

drm/imagination: guard fence release against device removal

PATCH In review Lewis Lakerink 2 participants 5 messages Nov 25 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/2 drm/imagination: guard fence release against device removal

pvr_queue_fence_release() queues fence->release_work onto pvr_dev->sched_wq to drop the context reference and free the fence. In drm_file_free(), drm_syncobj_release() runs before postclose; if a syncobj holds the last reference on a job's finished fence after pvr_remove() has destroyed sched_wq, releasing it dereferences the freed workqueue:

  pc : __queue_work+0x240/0x550
  x25: 0xdead000000000100
  Call trace:
   queue_work_on
   pvr_queue_fence_release
   drm_sched_fence_release_scheduled
   drm_sched_fence_release_finished
   drm_syncobj_release
   drm_file_free

Guard pvr_queue_fence_release() with drm_dev_enter(). When unplugged, call dma_fence_free() directly and leak the context reference: the callback runs under rcu_read_lock() in dma_fence_release(), and the device state needed by pvr_context_release() may already be torn down.

Fixes df1a1ed5e1bd ("drm/imagination: avoid deadlock on fence release")
Assisted-by LLM
---
 drivers/gpu/drm/imagination/pvr_queue.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
drivers/gpu/drm/imagination/pvr_queue.c +13 -1
index 34b8263..2a6bfb0 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-only OR MIT
 /* Copyright (c) 2023 Imagination Technologies Ltd. */
 
+#include <drm/drm_drv.h>
 #include <drm/drm_managed.h>
 #include <drm/gpu_scheduler.h>
 #include <linux/overflow.h>
@@ -122,8 +123,19 @@ static void pvr_queue_fence_release(struct dma_fence *f)
 {
 	struct pvr_queue_fence *fence = container_of(f, struct pvr_queue_fence, base);
 	struct pvr_device *pvr_dev = fence->queue->ctx->pvr_dev;
+	int idx;
 
-	queue_work(pvr_dev->sched_wq, &fence->release_work);
+	/*
+	 * Fences held by drm_syncobj are released in drm_file_free() before
+	 * postclose. After unplug, sched_wq and kernel_vm_ctx may already be
+	 * torn down; free the fence directly and leak the context reference.
+	 */
+	if (drm_dev_enter(from_pvr_device(pvr_dev), &idx)) {
+		queue_work(pvr_dev->sched_wq, &fence->release_work);
+		drm_dev_exit(idx);
+	} else {
+		dma_fence_free(&fence->base);
+	}
 }
 
 static const char *
Signature · 1 line
2.43.0
#2 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/imagination: context and drm_device leak in pvr_queue_fence_release() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/imagination: context and drm_device leak in pvr_queue_fence_release()
--

commit 98562e29f0071aecadfba68150e688168073f7c8
Author: Lewis Lakerink <[email protected]>

drm/imagination: guard fence release against device removal

This commit guards work queueing in pvr_queue_fence_release() with drm_dev_enter(), freeing the fence directly and skipping pvr_context_put() when the device is unplugged.

Quoted 4 lines
drivers/gpu/drm/imagination/pvr_queue.c +0 -0
index 34b826381aa2b..2a6bfb090fadd 100644

[ ... ]

Quoted 9 lines
drivers/gpu/drm/imagination/pvr_queue.c +3 -1
@@ -122,8 +123,19 @@ static void pvr_queue_fence_release(struct dma_fence *f)
 {
 	struct pvr_queue_fence *fence = container_of(f, struct pvr_queue_fence, base);
 	struct pvr_device *pvr_dev = fence->queue->ctx->pvr_dev;
+	int idx;
 
-	queue_work(pvr_dev->sched_wq, &fence->release_work);
+	/*
+	 * Fences held by drm_syncobj are released in drm_file_free() before

drivers/gpu/drm/imagination/pvr_queue.c static void pvr_queue_fence_release(struct dma_fence *f) · line 139

Quoted 10 lines
drivers/gpu/drm/imagination/pvr_queue.c +9 -0
+	 * postclose. After unplug, sched_wq and kernel_vm_ctx may already be
+	 * torn down; free the fence directly and leak the context reference.
+	 */
+	if (drm_dev_enter(from_pvr_device(pvr_dev), &idx)) {
+		queue_work(pvr_dev->sched_wq, &fence->release_work);
+		drm_dev_exit(idx);
+	} else {
+		dma_fence_free(&fence->base);
+	}
 }

[Severity: High]
Does leaking the fence->queue->ctx reference here also permanently leak pvr_dev and trigger teardown warnings?

drm_dev_unplug() is called not only during pvr_remove(), but also at runtime when a GPU reset or MMU flush fails:

drivers/gpu/drm/imagination/pvr_power.c:pvr_device_lost() {
	if (!pvr_dev->lost) {
		pvr_dev->lost = true;
		drm_dev_unplug(from_pvr_device(pvr_dev));
	}
}

Once unplugged, dropping the context reference in the else branch prevents pvr_context_release() from ever running. That leaves the context in pvr_dev->ctx_ids and its firmware objects in fw_objs.list and fw_dev->fw_mm (triggering WARN_ONs in pvr_fw_fini() and pvr_context_device_fini()), and never drops ctx->vm_ctx, whose drm_gpuvm manager and GEM objects hold drm_device references that prevent drm_dev_release() from freeing pvr_dev.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/010101a211d07983-72e5379a-adfd-4134-b23d-3e2b1422bc2e-000000@us-west-2.amazonses.com?part=1
#3 lore raw

Yes, that leak (including the drm_gpuvm -> drm_dev_put() reference on pvr_dev, and on pvr_device_lost() as well as pvr_remove()) is intentional here.

pvr_queue_fence_release() runs under rcu_read_lock(), and pvr_context_release() tears down FW objects against kernel_vm_ctx and fw_mm, which pvr_remove() -> pvr_device_fini() destroys after drm_dev_unplug(). Avoiding the leak cleanly requires moving device teardown to drmm_ / refcounted lifetime (noted in [PATCH 0/2]), which was left out so these use-after-free fixes remain minimal and backportable.

Quoted 64 lines · On 2026-11-25 19:42, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/imagination: context and drm_device leak in pvr_queue_fence_release()
--

commit 98562e29f0071aecadfba68150e688168073f7c8
Author: Lewis Lakerink <[email protected]>

drm/imagination: guard fence release against device removal

This commit guards work queueing in pvr_queue_fence_release() with drm_dev_enter(), freeing the fence directly and skipping pvr_context_put() when the device is unplugged.

Quoted 4 lines
drivers/gpu/drm/imagination/pvr_queue.c +0 -0
index 34b826381aa2b..2a6bfb090fadd 100644

[ ... ]

Quoted 15 lines
drivers/gpu/drm/imagination/pvr_queue.c +4 -1
@@ -122,8 +123,19 @@ static void pvr_queue_fence_release(struct 
 dma_fence *f)
 {
 	struct pvr_queue_fence *fence = container_of(f, struct 
 pvr_queue_fence, base);
 	struct pvr_device *pvr_dev = fence->queue->ctx->pvr_dev;
+	int idx;
 
-	queue_work(pvr_dev->sched_wq, &fence->release_work);
+	/*
+	 * Fences held by drm_syncobj are released in drm_file_free() before
+	 * postclose. After unplug, sched_wq and kernel_vm_ctx may already 

be

drivers/gpu/drm/imagination/pvr_queue.c +1 -0
+	 * torn down; free the fence directly and leak the context 

reference.

drivers/gpu/drm/imagination/pvr_queue.c static void pvr_queue_fence_release(struct dma_fence *f) · line 139

Quoted 8 lines
drivers/gpu/drm/imagination/pvr_queue.c +7 -0
+	 */
+	if (drm_dev_enter(from_pvr_device(pvr_dev), &idx)) {
+		queue_work(pvr_dev->sched_wq, &fence->release_work);
+		drm_dev_exit(idx);
+	} else {
+		dma_fence_free(&fence->base);
+	}
 }

[Severity: High]
Does leaking the fence->queue->ctx reference here also permanently leak pvr_dev and trigger teardown warnings?

drm_dev_unplug() is called not only during pvr_remove(), but also at runtime when a GPU reset or MMU flush fails:

drivers/gpu/drm/imagination/pvr_power.c:pvr_device_lost() {
	if (!pvr_dev->lost) {
		pvr_dev->lost = true;
		drm_dev_unplug(from_pvr_device(pvr_dev));
	}
}

Once unplugged, dropping the context reference in the else branch prevents pvr_context_release() from ever running. That leaves the context in pvr_dev->ctx_ids and its firmware objects in fw_objs.list and fw_dev->fw_mm (triggering WARN_ONs in pvr_fw_fini() and pvr_context_device_fini()), and never drops ctx->vm_ctx, whose drm_gpuvm manager and GEM objects hold drm_device references that prevent drm_dev_release() from freeing pvr_dev.

#4 lore raw

PATCH 2/2 drm/imagination: quiesce queues before device teardown

In reply to a message not in the archive

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")
Assisted-by LLM
---
 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(-)
drivers/gpu/drm/imagination/pvr_drv.c +69 -4
index 2d522a8..65c0747 100644
@@ -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);
drivers/gpu/drm/imagination/pvr_queue.c +64 -2

92 lines not on this page · raw message

drivers/gpu/drm/imagination/pvr_queue.h +2 -0
index 149cc6d..fab168e 100644
@@ -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 */
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH 2/2] drm/imagination: quiesce queues before device teardown

Thank you for your contribution! Sashiko AI review found 8 potential issues to consider: - [High] drm/imagination: pvr_remove() races pvr_device_lost() and IRQ pvr_power_reset() - [High]…

Thank you for your contribution! Sashiko AI review found 8 potential issues to consider:
- [High] drm/imagination: pvr_remove() races pvr_device_lost() and IRQ pvr_power_reset()
- [High] drm/imagination: unplugging before pm_runtime_suspend() breaks GPU power-off
- [High] drm/imagination: pvr_power_device_idle() UAF when runtime_error is not latched
- [High] drm/imagination: drm_dev_enter() across drm_sched_entity_destroy() deadlocks
- [High] drm/imagination: unflushed entity kill work in pvr_queue_device_quiesce()
- [Medium] drm/imagination: unplugging before pvr_queue_device_quiesce() leaks contexts
- [Medium] drm/imagination: PM ref leak on unplug in pvr_queue_timedout_job()
- [Medium] drm/imagination: paired frag job PM ref leak in pvr_queue_device_quiesce()
It also noticed 1 related pre-existing issue this patch does not introduce:
- [High] drm/imagination: unmapped firmware dereference in pvr_power_device_idle() [1]
--

commit 65a076714aeb37e172c20303373b34d2af84b0ea
Author: Lewis Lakerink <[email protected]>

drm/imagination: quiesce queues before device teardown

This commit reorders pvr_remove() to unplug the DRM device and quiesce active and idle scheduler queues before tearing down device state, and adds drm_dev_enter() guards to context destruction, postclose, and job timeout handling.

Quoted 12 lines
drivers/gpu/drm/imagination/pvr_drv.c +2 -0
index 2d522a8e46ad6..65c0747cf4d19 100644
@@ -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;

drivers/gpu/drm/imagination/pvr_drv.c pvr_ioctl_destroy_context(struct drm_device *drm_dev, void *raw_args, · line 739

Quoted 10 lines
drivers/gpu/drm/imagination/pvr_drv.c +7 -1
 
-	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;
 }

[Severity: High]
Can holding drm_dev_enter() across pvr_context_destroy() here (and in pvr_drm_driver_postclose() below) deadlock against drm_dev_unplug() in pvr_remove()?

pvr_context_destroy() calls pvr_context_kill_queues() -> pvr_queue_kill() -> drm_sched_entity_destroy() -> drm_sched_entity_flush(), which blocks non-exiting tasks in wait_event_killable(sched->job_scheduled, ...) until the entity drains:

CPU0 CPU1
pvr_ioctl_destroy_context()
drm_dev_enter()
pvr_context_destroy()

    ...
      drm_sched_entity_flush()
        wait_event_killable(
          sched->job_scheduled)
                                  pvr_remove()
                                    drm_dev_unplug()
                                      synchronize_srcu()  <-- waits for CPU0
                                    pvr_queue_device_quiesce()

If an entity job is waiting on an unsignaled dependency fence or a stalled GPU, CPU0 stays inside the drm_unplug_srcu read-side critical section while waiting for sched->job_scheduled, and pvr_remove() cannot reach pvr_queue_device_quiesce() -> drm_sched_fini() to wake it.

[ ... ]

Quoted 5 lines
drivers/gpu/drm/imagination/pvr_drv.c +1 -0
@@ -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. */

drivers/gpu/drm/imagination/pvr_drv.c static void pvr_remove(struct platform_device *plat_dev) · line 1522

Quoted 10 lines
drivers/gpu/drm/imagination/pvr_drv.c +10 -0
+	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);

[Severity: High]
Can this unsynchronized pvr_dev->lost check race with pvr_device_lost() and an IRQ-driven pvr_power_reset()?

The threaded IRQ handler remains active until pvr_device_fini() calls free_irq(), and pvr_remove() neither sets pvr_dev->lost = true nor holds pvr_dev->reset_sem:

CPU0 CPU1 (IRQ thread)
pvr_remove()
if (!READ_ONCE(pvr_dev->lost))

    drm_dev_unplug(drm_dev)
                                  pvr_fwccb_process()
                                    pvr_power_reset()
                                      pvr_power_device_resume()
                                        drm_dev_enter() -> -EIO
                                      pvr_device_lost()
                                        if (!pvr_dev->lost)
                                          drm_dev_unplug()  <-- second unplug

Conversely, pvr_device_lost() sets pvr_dev->lost = true before calling drm_dev_unplug():

drivers/gpu/drm/imagination/pvr_power.c:pvr_device_lost() {
	if (!pvr_dev->lost) {
		pvr_dev->lost = true;
		drm_dev_unplug(from_pvr_device(pvr_dev));
	}
}

If pvr_device_lost() sets pvr_dev->lost = true just before pvr_remove() checks it, pvr_remove() skips drm_dev_unplug() and proceeds to pvr_queue_device_quiesce(), xa_destroy(), and pvr_device_fini() before pvr_device_lost()'s synchronize_srcu() has finished waiting for in-flight drm_dev_enter() sections.

Also, because free_irq() does not run until pvr_device_fini(), an IRQ thread running pvr_fwccb_process() can still look up pvr_dev->free_list_ids after xa_destroy(&pvr_dev->free_list_ids) below, or call pvr_power_reset() -> pvr_power_get_clear() after pm_runtime_suspend() and clear the dev->power.runtime_error latch back to 0.

drivers/gpu/drm/imagination/pvr_drv.c static void pvr_remove(struct platform_device *plat_dev) · line 1525

Quoted 3 lines
drivers/gpu/drm/imagination/pvr_drv.c +3 -0
+
+	/* Stop all queues and their drm_sched workers/timers before teardown. */
+	pvr_queue_device_quiesce(pvr_dev);

[Severity: Medium]
Does calling drm_dev_unplug() before pvr_queue_device_quiesce() leak every context that has ever submitted a job?

Each queue retains a reference to the last submitted job's scheduled fence in queue->entity.last_scheduled and queue->last_queued_job_scheduled_fence, whose parent pvr_queue_fence holds a reference to queue->ctx. When pvr_queue_device_quiesce() drops those fences after drm_dev_unplug(), pvr_queue_fence_release() sees drm_dev_enter() fail:

drivers/gpu/drm/imagination/pvr_queue.c:pvr_queue_fence_release() {
	...
	if (drm_dev_enter(from_pvr_device(pvr_dev), &idx)) {
		queue_work(pvr_dev->sched_wq, &fence->release_work);
		drm_dev_exit(idx);
	} else {
		dma_fence_free(&fence->base);
	}
}

Instead of queueing fence->release_work to be drained by the flush_workqueue(pvr_dev->sched_wq) below, pvr_queue_fence_release() frees the fence without calling pvr_context_put(), leaking the context and its firmware objects and VM mappings and triggering the WARN_ON() checks in pvr_fw_fini(), drm_mm_takedown(), and pvr_context_device_fini().

Quoted 14 lines
drivers/gpu/drm/imagination/pvr_drv.c +7 -0
+
+	/*
+	 * 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);
 
+	/*

drivers/gpu/drm/imagination/pvr_drv.c static void pvr_remove(struct platform_device *plat_dev) · line 1549

Quoted 10 lines
drivers/gpu/drm/imagination/pvr_drv.c +8 -0
+	 * 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);

[Severity: High]
Does moving drm_dev_unplug() ahead of pm_runtime_suspend() leave the firmware processor and clocks running while pvr_device_fini() frees the firmware DMA buffers and GPU page tables?

Before calling pvr_remove(), __device_release_driver() calls pm_runtime_get_sync() followed by pm_runtime_put_sync(), which wakes the GPU and starts the firmware processor via pvr_power_device_resume() and only arms the 50 ms autosuspend timer. Previously, pm_runtime_suspend() ran before drm_dev_unplug(), so pvr_power_device_suspend() stopped the firmware processor via pvr_power_fw_disable() and cut clocks via pwr_ops->power_off() (and a successful pm_runtime_suspend() sets runtime_status to RPM_SUSPENDED, which also prevents rpm_idle() from calling pvr_power_device_idle()). With drm_dev_unplug() moved first, pvr_power_device_suspend() always fails drm_dev_enter() with -EIO, so pvr_device_fini() -> pvr_fw_fini() unmaps and frees all firmware objects and destroys kernel_vm_ctx while the GPU is still powered, clocked, and executing from those DMA pages, and leaks the clock enable references.

[Severity: High]
Can pvr_power_device_idle() still dereference unmapped fwif_sysdata during or after pvr_device_fini() despite the pm_runtime_suspend() call?

pvr_queue_device_quiesce() calls pvr_job_release_pm_ref() -> pvr_power_put() -> pm_runtime_put(), which queues an asynchronous RPM_REQ_IDLE work item on pm_wq. Unlike __pm_runtime_barrier(), pm_runtime_suspend() does not wait for an in-flight dev->power.idle_notification callback to finish:

CPU0 CPU1 (pm_wq)
pvr_remove()
pvr_queue_device_quiesce()

    pvr_job_release_pm_ref()
      pm_runtime_put()
                                  pm_runtime_work()
                                    rpm_idle()
                                      pvr_power_device_idle()
  pm_runtime_suspend() -> -EIO
  pvr_device_fini()
    pvr_fw_fini()
      /* unmaps fwif_sysdata */
                                        pvr_power_is_idle()
                                          fwif_sysdata->pow_state  <-- UAF

In addition, if dev->power.usage_count > 0 when pm_runtime_suspend() runs (for instance when power/autosuspend_delay_ms is negative, or while an IRQ-driven pvr_power_reset() holds a PM reference), rpm_suspend() returns -EAGAIN before invoking pvr_power_device_suspend(), so runtime_error is never latched and devres teardown's pm_runtime_disable_action() later calls pvr_power_device_idle() on the unmapped fwif_sysdata.

[Severity: High]
This problem wasn't introduced by this patch, but pvr_power_device_idle() has no drm_dev_enter() or READ_ONCE(pvr_dev->fw_dev.initialised) check before calling pvr_power_is_idle() [1], so on the pvr_probe() error path (if drm_dev_register() fails after pvr_device_init() has queued an asynchronous idle request via pm_runtime_put()) or whenever pm_runtime_suspend() in pvr_remove() fails with -EAGAIN while pvr_watchdog_worker() holds a PM reference, runtime PM can still invoke pvr_power_device_idle() after pvr_fw_fini() has unmapped fwif_sysdata and kccb.ccb.ctrl.

drivers/gpu/drm/imagination/pvr_queue.c pvr_queue_timedout_job(struct drm_sched_job *s_job) · line 887

Quoted 12 lines
drivers/gpu/drm/imagination/pvr_queue.c +4 -0
index 2a6bfb090fadd..c8d8a2285b0ac 100644
@@ -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;

[Severity: Medium]
Does returning DRM_GPU_SCHED_STAT_ENODEV here leak s_job's runtime PM reference?

Before invoking timedout_job(), drm_sched_job_timedout() unlinks s_job from sched->pending_list via list_del_init(&job->list), and it does not reinsert the job when timedout_job() returns DRM_GPU_SCHED_STAT_ENODEV. When pvr_queue_device_quiesce() later walks queue->scheduler.pending_list to call pvr_job_release_pm_ref(), s_job is no longer on pending_list, so its runtime PM reference is never released.

[ ... ]

Quoted 36 lines
drivers/gpu/drm/imagination/pvr_queue.c +32 -0
@@ -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);
+

drivers/gpu/drm/imagination/pvr_queue.c int pvr_queue_device_init(struct pvr_device *pvr_dev) · line 1564

Quoted 10 lines
drivers/gpu/drm/imagination/pvr_queue.c +10 -0
+		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;
+		}

[Severity: High]
Can jobs killed by drm_sched_entity_fini() run pvr_job_release() on the system workqueue after pvr_remove() has destroyed job_ids and kernel_vm_ctx?

For each unpopped job still in entity->job_queue, drm_sched_entity_fini() -> drm_sched_entity_kill() schedules drm_sched_entity_kill_jobs_work via schedule_work(&job->work) onto the global system workqueue (or defers it to an unsignaled dependency fence callback), not onto pvr_dev->sched_wq:

CPU0 CPU1 (system_wq)
pvr_remove()
pvr_queue_device_quiesce()

    drm_sched_entity_fini()
      ...
        schedule_work(&job->work)
  flush_workqueue(pvr_dev->sched_wq)
  xa_destroy(&pvr_dev->job_ids)
  pvr_device_fini()
    /* frees kernel_vm_ctx */
                                  drm_sched_entity_kill_jobs_work()
                                    pvr_queue_free_job()
                                      pvr_job_release()
                                        xa_erase(&pvr_dev->job_ids, ...)
                                        pvr_context_put()
                                          ...
                                            pvr_meta_vm_unmap()  <-- UAF

Because flush_workqueue(pvr_dev->sched_wq) does not drain those system workqueue items, pvr_job_release() (which has no drm_dev_enter() guard) can erase from the destroyed job_ids xarray and release the last context or HWRT reference after pvr_device_fini() has freed kernel_vm_ctx. Also, if pvr_queue_prepare_job() had already run for a job still in entity->job_queue, queue->cccb_fence_ctx.job and pvr_dev->kccb.waiters / reserved_count are left behind when pvr_kccb_fini() runs.

drivers/gpu/drm/imagination/pvr_queue.c int pvr_queue_device_init(struct pvr_device *pvr_dev) · line 1571

Quoted 7 lines
drivers/gpu/drm/imagination/pvr_queue.c +7 -0
+		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);
+		}

[Severity: Medium]
Can this loop miss the runtime PM reference held by a paired fragment job?

In pvr_queue_run_job(), running a geometry job with a paired fragment job acquires the PM reference for both jobs before the fragment job is popped from frag_queue->entity onto frag_queue->scheduler.pending_list:

drivers/gpu/drm/imagination/pvr_queue.c:pvr_queue_run_job() {
	...
	if (job->paired_job) {
		err = pvr_job_get_pm_ref(job->paired_job);
		if (WARN_ON(err))
			return ERR_PTR(err);
	}
	...
}

If pvr_queue_device_quiesce() runs after the geometry job's run_job() has taken job->paired_job's PM reference but before frag_queue's work_run_job pops the fragment job onto frag_queue->scheduler.pending_list, the fragment job is killed in frag_queue->entity and never reaches pending_list, so walking pending_list without also releasing job->paired_job's PM reference leaks it.

[1] https://sashiko.dev/#/bug/linux-00298821-c6b8-4257-b57f-8521b3a59a07

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/010101a211d07983-72e5379a-adfd-4134-b23d-3e2b1422bc2e-000000@us-west-2.amazonses.com?part=2