* [PATCH 1/2] drm/imagination: guard fence release against device removal [not found] <20261125082637.2839673-1-lewis@retsim.com> @ 2026-11-25 8:26 ` Lewis Lakerink 2026-11-26 8:28 ` sashiko-bot 2026-11-25 8:26 ` [PATCH 2/2] drm/imagination: quiesce queues before device teardown Lewis Lakerink ` (2 subsequent siblings) 3 siblings, 1 reply; 7+ messages in thread From: Lewis Lakerink @ 2026-11-25 8:26 UTC (permalink / raw) To: imagination Cc: Alessio Belle, Luigi Santivetti, Ryan Brue, dri-devel, linux-kernel, Lewis Lakerink 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") Cc: stable@vger.kernel.org # 6.14+ Assisted-by: LLM Signed-off-by: Lewis Lakerink <lewis@retsim.com> --- drivers/gpu/drm/imagination/pvr_queue.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/imagination/pvr_queue.c b/drivers/gpu/drm/imagination/pvr_queue.c index 34b8263..2a6bfb0 100644 --- a/drivers/gpu/drm/imagination/pvr_queue.c +++ b/drivers/gpu/drm/imagination/pvr_queue.c @@ -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 * -- 2.43.0 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] drm/imagination: guard fence release against device removal 2026-11-25 8:26 ` [PATCH 1/2] drm/imagination: guard fence release against device removal Lewis Lakerink @ 2026-11-26 8:28 ` sashiko-bot 0 siblings, 0 replies; 7+ messages in thread From: sashiko-bot @ 2026-11-26 8:28 UTC (permalink / raw) To: Lewis Lakerink; +Cc: dri-devel Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/imagination: pvr_queue_fence_release() leaks context when unplugged -- commit 9773b5621e802ef35e9ac3c4db81027170ee1477 Author: Lewis Lakerink <lewis@retsim.com> 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 fence->release_work when the DRM device is marked unplugged. > diff --git a/drivers/gpu/drm/imagination/pvr_queue.c b/drivers/gpu/drm/imagination/pvr_queue.c > index 34b826381aa2b..2a6bfb090fadd 100644 > --- a/drivers/gpu/drm/imagination/pvr_queue.c > +++ b/drivers/gpu/drm/imagination/pvr_queue.c [ ... ] > @@ -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); > + } > } [Severity: High] Does taking the else branch leak fence->queue->ctx on runtime device loss as well as on driver unbind? pvr_device_lost() calls drm_dev_unplug() at runtime when a GPU hard reset or MMU flush fails, while the device is still bound and sched_wq is still active. Every pvr_queue_fence released after pvr_device_lost() (or after drm_dev_unplug() in pvr_remove()) will skip pvr_context_put(), so pvr_context_release() never runs. That leaves the context's queues, firmware objects and ctx->vm_ctx allocated, triggering WARN_ON(!list_empty(&pvr_dev->fw_dev.fw_objs.list)) in pvr_fw_fini() and WARN_ON(!xa_empty(&pvr_dev->ctx_ids)) in pvr_context_device_fini() on unbind, and keeps ctx->vm_ctx->gpuvm_mgr's drm_device reference alive so struct pvr_device is never freed. -- Sashiko AI review · https://sashiko.dev/#/patchset/010101a211d07798-70c4219d-e457-43d8-835c-321a7c035057-000000@us-west-2.amazonses.com?part=1 ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 2/2] drm/imagination: quiesce queues before device teardown [not found] <20261125082637.2839673-1-lewis@retsim.com> 2026-11-25 8:26 ` [PATCH 1/2] drm/imagination: guard fence release against device removal Lewis Lakerink @ 2026-11-25 8:26 ` Lewis Lakerink 2026-11-26 8:46 ` sashiko-bot [not found] ` <010101a211d07983-72e5379a-adfd-4134-b23d-3e2b1422bc2e-000000@us-west-2.amazonses.com> [not found] ` <010101a211d07b13-42cf1386-97c8-41d3-8eca-2e56ccb33258-000000@us-west-2.amazonses.com> 3 siblings, 1 reply; 7+ messages in thread From: Lewis Lakerink @ 2026-11-25 8:26 UTC (permalink / raw) To: imagination Cc: Alessio Belle, Luigi Santivetti, Ryan Brue, dri-devel, linux-kernel, Lewis Lakerink 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@vger.kernel.org # 6.8+ Assisted-by: LLM Signed-off-by: Lewis Lakerink <lewis@retsim.com> --- 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 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 2/2] drm/imagination: quiesce queues before device teardown 2026-11-25 8:26 ` [PATCH 2/2] drm/imagination: quiesce queues before device teardown Lewis Lakerink @ 2026-11-26 8:46 ` sashiko-bot 0 siblings, 0 replies; 7+ messages in thread From: sashiko-bot @ 2026-11-26 8:46 UTC (permalink / raw) To: Lewis Lakerink; +Cc: dri-devel Thank you for your contribution! Sashiko AI review found 7 potential issues to consider: - [High] drm/imagination: race with IRQ reset and pvr_device_lost() in pvr_remove() - [High] drm/imagination: deadlock in pvr_remove() and reset via drm_dev_enter() - [High] drm/imagination: active FW DMA and clock leak in pvr_remove() after unplug - [High] drm/imagination: async entity job teardown race in pvr_queue_device_quiesce() - [Medium] drm/imagination: runtime PM ref leak in pvr_queue_timedout_job() on unplug - [Medium] drm/imagination: paired fragment job PM ref leak in pvr_queue_device_quiesce() - [Medium] drm/imagination: pvr_power_device_idle() UAF race with pvr_device_fini() It also noticed 2 related pre-existing issues this patch does not introduce: - [High] drm/imagination: race between pvr_queue_timedout_job() and hard reset [1] - [High] drm/imagination: scheduler timeout on PM ref failure in pvr_queue_run_job() [2] -- commit 183994369960829d968efc4d07cee9c3ce99db2a Author: Lewis Lakerink <lewis@retsim.com> 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 timeouts. > diff --git a/drivers/gpu/drm/imagination/pvr_drv.c b/drivers/gpu/drm/imagination/pvr_drv.c > index 2d522a8e46ad6..65c0747cf4d19 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); > } [Severity: High] Can holding drm_dev_enter() across pvr_context_destroy() and pvr_drm_driver_postclose() deadlock with drm_dev_unplug() in pvr_remove() and pvr_power_reset()? In the first case, pvr_context_destroy() calls pvr_context_kill_queues() -> pvr_queue_kill() -> drm_sched_entity_destroy() -> drm_sched_entity_flush(). For pvr_ioctl_destroy_context() or an explicit close() where PF_EXITING is not set, drm_sched_entity_flush() waits in wait_event_killable() on sched->job_scheduled for entity->job_queue to drain: CPU0 CPU1 pvr_ioctl_destroy_context() drm_dev_enter() pvr_context_destroy() ... drm_sched_entity_flush() pvr_remove() drm_dev_unplug() dev->unplugged = true synchronize_srcu(&drm_unplug_srcu) wait_event_killable( sched->job_scheduled, ...) <-- waits for entity idle; pvr_remove() cannot reach pvr_queue_device_quiesce() If a queued job is waiting on an unsignaled dependency or a wedged job (whose timeout handler now returns DRM_GPU_SCHED_STAT_ENODEV once dev->unplugged is set), drm_sched_entity_flush() never wakes up and pvr_remove() blocks in synchronize_srcu() before reaching pvr_queue_device_quiesce(). In the second case, wrapping these paths in drm_dev_enter() inverts the lock order between drm_unplug_srcu and pvr_dev->reset_sem: CPU0 CPU1 ---- ---- down_write(&pvr_dev->reset_sem) drm_dev_enter() [drm_unplug_srcu] down_read(&pvr_dev->reset_sem) drm_dev_unplug() [drm_unplug_srcu] *** DEADLOCK *** CPU0: pvr_power_reset() -> pvr_device_lost() -> drm_dev_unplug() CPU1: pvr_drm_driver_postclose() -> pvr_destroy_contexts_for_file() -> pvr_context_destroy() -> pvr_context_put() -> pvr_context_release() -> pvr_queue_destroy() -> pvr_queue_cleanup_fw_context() -> pvr_fw_structure_cleanup() > @@ -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); [Severity: High] Can pvr_remove() still race with an IRQ-driven pvr_power_reset() or an in-flight pvr_device_lost() here? The threaded IRQ handler pvr_device_irq_thread_handler() remains active until pvr_device_fini() -> pvr_device_irq_fini() -> free_irq(), and pvr_remove() neither takes pvr_dev->reset_sem nor sets pvr_dev->lost = true: CPU0 CPU1 pvr_remove() if (!READ_ONCE(pvr_dev->lost)) drm_dev_unplug(drm_dev) pvr_device_irq_thread_handler() pvr_fwccb_process() pvr_power_reset(pvr_dev, false) pvr_power_get_clear() ... pvr_power_device_resume() -> -EIO pvr_device_lost(pvr_dev) if (!pvr_dev->lost) drm_dev_unplug() <-- second drm_dev_unplug() Conversely, pvr_device_lost() sets pvr_dev->lost = true before calling drm_dev_unplug(). If pvr_device_lost() sets pvr_dev->lost just before pvr_remove() checks !READ_ONCE(pvr_dev->lost), pvr_remove() skips drm_dev_unplug() while pvr_device_lost() is still inside synchronize_srcu(&drm_unplug_srcu), tearing down queues, sched_wq, and kernel_vm_ctx while in-flight drm_dev_enter() sections are still running. Leaving the IRQ handler active across the rest of pvr_remove() causes three additional races before free_irq(): - An IRQ-driven pvr_power_reset() calls pvr_power_get_clear() -> pvr_power_clear_error(), which clears the runtime_error latch that keeps devres teardown from running pvr_power_device_idle() on unmapped fwif_sysdata. - pvr_fwccb_process() can look up entries in pvr_dev->free_list_ids after xa_destroy(&pvr_dev->free_list_ids) runs. - The second pvr_watchdog_fini() is placed after pvr_device_fini(), which calls pvr_device_gpu_fini() -> pvr_fw_fini() after free_irq(), so a watchdog work item re-armed by an IRQ reset before free_irq() can run pvr_watchdog_worker() concurrently with pvr_fw_fini(). > + > + /* 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); [Severity: High] When the GPU is runtime-active during pvr_remove(), doesn't making pm_runtime_suspend() fail with -EIO cause pvr_device_fini() -> pvr_fw_fini() to free all firmware DMA buffers and MMU page tables while the firmware processor is still running? Because pvr_power_device_suspend() aborts at its drm_dev_enter() check after drm_dev_unplug(), neither pvr_power_fw_disable() (pvr_fw_stop()) nor pwr_ops->power_off() runs. pvr_fw_fini() then unmaps and frees the firmware code, data, CCB, and page-table GEM objects back to the page allocator while the GPU remains powered, clocked, out of reset, and performing DMA through those page tables, and leaks the clk_prepare_enable() references on core_clk, sys_clk, and mem_clk. Note that when pm_runtime_suspend() succeeds, dev->power.runtime_status is set to RPM_SUSPENDED, and rpm_idle() immediately returns -EAGAIN when runtime_status != RPM_ACTIVE without invoking pvr_power_device_idle(). [Severity: Medium] Can an asynchronous runtime PM idle check triggered by pvr_queue_device_quiesce() still race with pvr_device_fini() here? When the last pvr_job_release_pm_ref() in pvr_queue_device_quiesce() calls pvr_power_put() -> pm_runtime_put(), dropping usage_count to zero queues async RPM_REQ_IDLE work on pm_wq: CPU0 kworker (pm_wq) pvr_remove() pvr_queue_device_quiesce() pvr_job_release_pm_ref() pm_runtime_put() pm_runtime_work() rpm_idle() dev->power.idle_notification = true spin_unlock(&dev->power.lock) pvr_power_device_idle() pm_runtime_suspend() -> -EIO pvr_device_fini() ... pvr_fw_Destroy_structures() /* unmaps fwif_sysdata */ pvr_power_is_idle() fwif_sysdata->pow_state <-- use-after-free Unlike pm_runtime_barrier(), pm_runtime_suspend() does not wait for an in-progress dev->power.idle_notification callback to finish, and pvr_power_device_idle() has no drm_dev_enter() guard. [ ... ] > diff --git a/drivers/gpu/drm/imagination/pvr_queue.c b/drivers/gpu/drm/imagination/pvr_queue.c > index 2a6bfb090fadd..c8d8a2285b0ac 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; [Severity: Medium] Does returning DRM_GPU_SCHED_STAT_ENODEV here leak the timed-out job's runtime PM reference? drm_sched_job_timedout() unlinks s_job from sched->pending_list via list_del_init(&job->list) before calling timedout_job(), and only reinserts it on DRM_GPU_SCHED_STAT_NO_HANG (or when drm_sched_stop() runs). When pvr_queue_timedout_job() returns DRM_GPU_SCHED_STAT_ENODEV after unplug, s_job stays off sched->pending_list, so pvr_queue_device_quiesce() misses it when walking pending_list to call pvr_job_release_pm_ref(). > > - 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; > + } [Severity: High] This problem wasn't introduced by this patch, but pvr_queue_timedout_job() runs without holding pvr_dev->reset_sem and can race with a hard reset in pvr_power_reset() [1]: CPU0 CPU1 drm_sched_job_timedout() pvr_watchdog_worker() pvr_queue_timedout_job() pvr_power_reset(..., true) pvr_queue_device_pre_reset() pvr_queue_stop() drm_sched_stop() s_job->s_fence->parent = NULL dma_fence_is_signaled( s_job->s_fence->parent) <-- NULL pointer dereference drm_sched_stop() clears s_job->s_fence->parent and only calls non-synchronous cancel_delayed_work(&sched->work_tdr) at the end. If pvr_queue_timedout_job() starts first, it also unlinks queue->node from pvr_dev->queues.active before dropping pvr_dev->queues.lock, causing pvr_queue_device_pre_reset() to miss the queue while pvr_queue_timedout_job() restarts and kicks it in the middle of the reset. [ ... ] > @@ -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; > + } [Severity: High] Can unscheduled jobs killed by drm_sched_entity_fini() here outlive flush_workqueue(pvr_dev->sched_wq) and race with pvr_device_fini()? drm_sched_entity_fini() -> drm_sched_entity_kill() frees queued entity jobs via schedule_work(&job->work) on system_percpu_wq (or defers them to a dma_fence callback when a job has an unsignaled dependency), not on pvr_dev->sched_wq: CPU0 kworker (system_percpu_wq) pvr_remove() pvr_queue_device_quiesce() drm_sched_entity_fini() drm_sched_entity_kill() schedule_work(&job->work) flush_workqueue(pvr_dev->sched_wq) xa_destroy(&pvr_dev->job_ids) pvr_device_fini(pvr_dev) /* 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() <-- use-after-free Because jobs that never reached pvr_queue_prepare_job() have an uninitialized job->done_fence, their release goes directly through pvr_job_release() without any drm_dev_enter() check, accessing the destroyed job_ids XArray and unmapping FW objects against the freed kernel_vm_ctx. > + 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 walking only queue->scheduler.pending_list leak the runtime PM reference of a paired fragment job? In pvr_queue_run_job(), running a geometry job that has job->paired_job acquires a PM reference for both job and job->paired_job while the paired fragment job is still in frag_queue->entity.job_queue waiting on the geometry job's scheduled fence. If pvr_queue_device_quiesce() stops the fragment queue before that fragment job is moved to frag_queue->scheduler.pending_list, drm_sched_entity_fini() pops it from the entity queue (where it waits indefinitely on the geometry job's unsignaled finished fence), so neither pending_list walk releases job->paired_job's PM reference. [Severity: High] This problem wasn't introduced by this patch, but in pvr_queue_run_job(), if pvr_job_get_pm_ref(job->paired_job) fails after pvr_job_get_pm_ref(job) succeeds, the early return leaks the geometry job's PM reference and aborts the combined kick [2]: drivers/gpu/drm/imagination/pvr_queue.c:pvr_queue_run_job() { ... err = pvr_job_get_pm_ref(job); if (WARN_ON(err)) return ERR_PTR(err); if (job->paired_job) { err = pvr_job_get_pm_ref(job->paired_job); if (WARN_ON(err)) return ERR_PTR(err); } ... } When the paired fragment job later runs on the fragment queue, pvr_queue_run_job() returns job->done_fence, which is never signaled because the combined kick was never sent, causing a scheduler timeout. [1] https://sashiko.dev/#/bug/linux-df7937e5-34ea-4049-927a-b1a2ab49472c [2] https://sashiko.dev/#/bug/linux-323442c2-f456-45f6-8b7f-1b059c35de97 -- Sashiko AI review · https://sashiko.dev/#/patchset/010101a211d07798-70c4219d-e457-43d8-835c-321a7c035057-000000@us-west-2.amazonses.com?part=2 ^ permalink raw reply [flat|nested] 7+ messages in thread
[parent not found: <010101a211d07983-72e5379a-adfd-4134-b23d-3e2b1422bc2e-000000@us-west-2.amazonses.com>]
* Re: [PATCH 1/2] drm/imagination: guard fence release against device removal [not found] ` <010101a211d07983-72e5379a-adfd-4134-b23d-3e2b1422bc2e-000000@us-west-2.amazonses.com> @ 2026-11-25 8:42 ` sashiko-bot 2026-11-25 8:47 ` lewis 0 siblings, 1 reply; 7+ messages in thread From: sashiko-bot @ 2026-11-25 8:42 UTC (permalink / raw) To: Lewis Lakerink; +Cc: dri-devel 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 <lewis@retsim.com> 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. > diff --git a/drivers/gpu/drm/imagination/pvr_queue.c b/drivers/gpu/drm/imagination/pvr_queue.c > index 34b826381aa2b..2a6bfb090fadd 100644 > --- a/drivers/gpu/drm/imagination/pvr_queue.c > +++ b/drivers/gpu/drm/imagination/pvr_queue.c [ ... ] > @@ -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); > + } > } [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. -- Sashiko AI review · https://sashiko.dev/#/patchset/010101a211d07983-72e5379a-adfd-4134-b23d-3e2b1422bc2e-000000@us-west-2.amazonses.com?part=1 ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] drm/imagination: guard fence release against device removal 2026-11-25 8:42 ` [PATCH 1/2] drm/imagination: guard fence release against device removal sashiko-bot @ 2026-11-25 8:47 ` lewis 0 siblings, 0 replies; 7+ messages in thread From: lewis @ 2026-11-25 8:47 UTC (permalink / raw) To: sashiko-reviews; +Cc: dri-devel 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. On 2026-11-25 19:42, sashiko-bot@kernel.org 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 <lewis@retsim.com> > > 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. > >> diff --git a/drivers/gpu/drm/imagination/pvr_queue.c >> b/drivers/gpu/drm/imagination/pvr_queue.c >> index 34b826381aa2b..2a6bfb090fadd 100644 >> --- a/drivers/gpu/drm/imagination/pvr_queue.c >> +++ b/drivers/gpu/drm/imagination/pvr_queue.c > [ ... ] >> @@ -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); >> + } >> } > > [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. ^ permalink raw reply [flat|nested] 7+ messages in thread
[parent not found: <010101a211d07b13-42cf1386-97c8-41d3-8eca-2e56ccb33258-000000@us-west-2.amazonses.com>]
* Re: [PATCH 2/2] drm/imagination: quiesce queues before device teardown [not found] ` <010101a211d07b13-42cf1386-97c8-41d3-8eca-2e56ccb33258-000000@us-west-2.amazonses.com> @ 2026-11-25 8:55 ` sashiko-bot 0 siblings, 0 replies; 7+ messages in thread From: sashiko-bot @ 2026-11-25 8:55 UTC (permalink / raw) To: Lewis Lakerink; +Cc: dri-devel 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 <lewis@retsim.com> 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. > diff --git a/drivers/gpu/drm/imagination/pvr_drv.c b/drivers/gpu/drm/imagination/pvr_drv.c > index 2d522a8e46ad6..65c0747cf4d19 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; > } [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. [ ... ] > @@ -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); [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. > + > + /* 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(). > + > + /* > + * 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); [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. > diff --git a/drivers/gpu/drm/imagination/pvr_queue.c b/drivers/gpu/drm/imagination/pvr_queue.c > index 2a6bfb090fadd..c8d8a2285b0ac 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; [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. [ ... ] > @@ -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; > + } [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. > + 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 -- Sashiko AI review · https://sashiko.dev/#/patchset/010101a211d07983-72e5379a-adfd-4134-b23d-3e2b1422bc2e-000000@us-west-2.amazonses.com?part=2 ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-26 8:46 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20261125082637.2839673-1-lewis@retsim.com>
2026-11-25 8:26 ` [PATCH 1/2] drm/imagination: guard fence release against device removal Lewis Lakerink
2026-11-26 8:28 ` sashiko-bot
2026-11-25 8:26 ` [PATCH 2/2] drm/imagination: quiesce queues before device teardown Lewis Lakerink
2026-11-26 8:46 ` sashiko-bot
[not found] ` <010101a211d07983-72e5379a-adfd-4134-b23d-3e2b1422bc2e-000000@us-west-2.amazonses.com>
2026-11-25 8:42 ` [PATCH 1/2] drm/imagination: guard fence release against device removal sashiko-bot
2026-11-25 8:47 ` lewis
[not found] ` <010101a211d07b13-42cf1386-97c8-41d3-8eca-2e56ccb33258-000000@us-west-2.amazonses.com>
2026-11-25 8:55 ` [PATCH 2/2] drm/imagination: quiesce queues before device teardown sashiko-bot
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox