dri-devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Maíra Canal" <mcanal@igalia.com>
Cc: dri-devel@lists.freedesktop.org
Subject: Re: [PATCH v3 2/2] drm/v3d: Serialize jobs across queues when a perfmon is attached
Date: Sat, 04 Jul 2026 12:29:35 +0000	[thread overview]
Message-ID: <20260704122935.D2E8A1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260704-v3d-perfmon-lifetime-v3-2-fd26dfdc2153@igalia.com>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

New issues:
- [Medium] A Time-of-Check to Time-of-Use (TOCTOU) race condition between local perfmon job submission and global perfmon toggling allows jobs to bypass cross-queue serialization.
- [Medium] Out-of-order execution across different DRM scheduler entities breaks the single-fence tracking mechanism, allowing perfmon counter pollution.

Pre-existing issues:
- [High] Runtime PM usage counter underflow due to incorrect error checking on pm_runtime_get_if_active().
--

commit bade95affe13f920ce39322b17d721c548957d90
Author: Maíra Canal <mcanal@igalia.com>

drm/v3d: Serialize jobs across queues when a perfmon is attached

A non-global perfmon is meant to count events generated by a specific
submission. This commit adds cross-queue serialization by attaching fence
dependencies so a perfmon-carrying job waits for in-flight jobs, and
subsequent jobs wait for it to finish.

> diff --git a/drivers/gpu/drm/v3d/v3d_perfmon.c b/drivers/gpu/drm/v3d/v3d_perfmon.c
> index 3ad0f022753c8..07dab7fb30607 100644
> --- a/drivers/gpu/drm/v3d/v3d_perfmon.c
> +++ b/drivers/gpu/drm/v3d/v3d_perfmon.c
> @@ -275,6 +275,12 @@ void v3d_perfmon_start(struct v3d_dev *v3d, struct v3d_perfmon *perfmon)
>  	if (!perfmon || v3d->global_perfmon)
>  		return;
>  
> +	/* Cross-queue serialization should have drained any previous perfmon
> +	 * job before this one runs.
> +	 */
> +	if (WARN_ON_ONCE(v3d->perfmon_state.active))
> +		return;
> +
>  	if (!pm_runtime_get_if_active(v3d->drm.dev))
>  		return;

[Severity: High]
This is a pre-existing issue, but does this correctly handle the case where
Runtime PM is disabled?

pm_runtime_get_if_active() returns -EINVAL if Runtime PM is disabled (for
example, during system suspend). In C, -EINVAL evaluates as truthy, so the
condition !(-22) evaluates to false. 

Because the early return is skipped, the function proceeds to interact with
hardware and eventually drops a usage counter via v3d_pm_runtime_put().
Could this cause an unbalanced PM usage counter underflow?

[ ... ]

> diff --git a/drivers/gpu/drm/v3d/v3d_submit.c b/drivers/gpu/drm/v3d/v3d_submit.c
> index 27cc98770b37e..2bb955a9ed5e9 100644
> --- a/drivers/gpu/drm/v3d/v3d_submit.c
> +++ b/drivers/gpu/drm/v3d/v3d_submit.c
> @@ -320,6 +320,62 @@ v3d_attach_perfmon_to_jobs(struct v3d_submit *submit, u32 perfmon_id)

[ ... ]

> +static int
> +v3d_serialize_for_perfmon(struct v3d_job *job)
> +{
> +	struct v3d_dev *v3d = job->v3d;
> +	bool is_global_perfmon;
> +	int ret;
> +
> +	lockdep_assert_held(&v3d->sched_lock);
> +
> +	scoped_guard(spinlock_irqsave, &v3d->perfmon_state.lock)
> +		is_global_perfmon = !!v3d->global_perfmon;
> +
> +	if (is_global_perfmon)
> +		goto publish;

[Severity: Medium]
Could a race condition bypass cross-queue serialization here if the global
perfmon state changes while jobs are queued?

If userspace submits Job A with a local perfmon (which stays queued in the
scheduler without immediately setting perfmon_state.active), and then calls
the ioctl to set the global perfmon, a subsequent Job B without a perfmon 
would see is_global_perfmon as true and skip serialization.

If userspace then clears the global perfmon before execution, could Job A
and Job B end up running concurrently on the hardware and polluting the
performance counters?

> +
> +	if (job->perfmon) {
> +		for (enum v3d_queue q = 0; q < V3D_MAX_QUEUES; q++) {
> +			struct dma_fence *f = v3d->perfmon_state.last_hw_fence[q];

[ ... ]

> +publish:
> +	dma_fence_put(v3d->perfmon_state.last_hw_fence[job->queue]);
> +	v3d->perfmon_state.last_hw_fence[job->queue] = dma_fence_get(job->done_fence);

[Severity: Medium]
Does overwriting the tracked fence for the queue lose track of earlier jobs
that might still be pending due to external dependencies?

If userspace submits Job 1 (blocked on an external syncobj) via File A,
then submits Job 2 (no dependencies) via File B to the same queue, they
can execute out of order because they use different scheduler entities.

The last_hw_fence array would be overwritten by Job 2's fence. If a
subsequent perfmon Job 3 is submitted, it will only wait on Job 2.

When Job 1's dependency is finally satisfied, could Job 1 and Job 3 run
concurrently and bypass the intended serialization?

> +
> +	if (job->perfmon && !is_global_perfmon) {
> +		dma_fence_put(v3d->perfmon_state.fence);
> +		v3d->perfmon_state.fence = dma_fence_get(job->done_fence);
> +	}
> +
> +	return 0;
> +}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260704-v3d-perfmon-lifetime-v3-0-fd26dfdc2153@igalia.com?part=2

      reply	other threads:[~2026-07-04 12:29 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-04 12:15 [PATCH v3 0/2] drm/v3d: Fix perfmon locking and cross-queue isolation Maíra Canal
2026-07-04 12:15 ` [PATCH v3 1/2] drm/v3d: Refactor perfmon locking Maíra Canal
2026-07-04 12:31   ` sashiko-bot
2026-07-04 12:15 ` [PATCH v3 2/2] drm/v3d: Serialize jobs across queues when a perfmon is attached Maíra Canal
2026-07-04 12:29   ` sashiko-bot [this message]

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=20260704122935.D2E8A1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=mcanal@igalia.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