From: sashiko-bot@kernel.org
To: "Lewis Lakerink" <lewis@retsim.com>
Cc: dri-devel@lists.freedesktop.org
Subject: Re: [PATCH 2/2] drm/imagination: quiesce queues before device teardown
Date: Thu, 26 Nov 2026 08:46:23 +0000 [thread overview]
Message-ID: <sashiko-outbox-235052@kernel.org> (raw)
In-Reply-To: <010101a211d07fcc-55f41080-fd57-47e4-8a51-a92b1adf86de-000000@us-west-2.amazonses.com>
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
next prev parent reply other threads:[~2026-11-26 8:46 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
[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 [this message]
[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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-235052@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=lewis@retsim.com \
--cc=sashiko-reviews@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox