mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Maíra Canal" <mcanal@igalia.com>
To: Michael Bommarito <michael.bommarito@gmail.com>,
	Melissa Wen <mwen@igalia.com>
Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>,
	Maxime Ripard <mripard@kernel.org>,
	Thomas Zimmermann <tzimmermann@suse.de>,
	David Airlie <airlied@gmail.com>, Simona Vetter <simona@ffwll.ch>,
	Iago Toral Quiroga <itoral@igalia.com>,
	dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3] drm/v3d: bound CPU-job query writes to their destination BO
Date: Sat, 4 Jul 2026 09:11:07 -0300	[thread overview]
Message-ID: <10d3d64a-0321-4223-8a56-d32a654297fd@igalia.com> (raw)
In-Reply-To: <20260617125811.3692951-1-michael.bommarito@gmail.com>

Hi Michael,

Thanks for your patch!

On 17/06/26 09:58, Michael Bommarito wrote:
> +
> +/* Reject a query CPU job whose writes would land outside their BO. */
> +static int
> +v3d_cpu_job_bounds_check(struct v3d_cpu_job *job)
> +{
> +	struct drm_device *dev = &job->base.v3d->drm;
> +	struct v3d_timestamp_query_info *tquery = &job->timestamp_query;
> +	struct v3d_copy_query_results_info *copy = &job->copy;
> +	u32 elem = copy->do_64bit ? sizeof(u64) : sizeof(u32);
> +	struct v3d_bo *dst, *src;
> +	u64 slots, write_size;
> +	int i;

Sashiko pointed out that this should be a u32 at least.

> +
> +	switch (job->job_type) {
> +	case V3D_CPU_JOB_TYPE_TIMESTAMP_QUERY:
> +	case V3D_CPU_JOB_TYPE_RESET_TIMESTAMP_QUERY:
> +		/*
> +		 * Each query writes one u64 timestamp slot into bo[0]. The
> +		 * offset is cast to u64 so a 0xffffffff query offset cannot wrap
> +		 * the addition past the BO size.
> +		 */

"/* Each query writes one u64 timestamp slot into bo[0]. */" was enough
of a comment.

> +		dst = to_v3d_bo(job->base.bo[0]);
> +
> +		for (i = 0; i < tquery->count; i++) {
> +			if ((u64)tquery->queries[i].offset + sizeof(u64) >
> +			    dst->base.base.size)
> +				goto err_range;
> +		}
> +		return 0;
> +	case V3D_CPU_JOB_TYPE_COPY_TIMESTAMP_QUERY:
> +		/* Copies one u64 per query from bo[1] into bo[0]. */
> +		dst = to_v3d_bo(job->base.bo[0]);
> +		src = to_v3d_bo(job->base.bo[1]);
> +
> +		for (i = 0; i < tquery->count; i++) {
> +			if ((u64)tquery->queries[i].offset + sizeof(u64) >
> +			    src->base.base.size)
> +				goto err_range;
> +		}
> +
> +		write_size = (copy->availability_bit ? 2 : 1) * elem;
> +		return v3d_check_copy_extent(dev, dst->base.base.size,
> +					     copy->offset, copy->stride,
> +					     tquery->count, write_size);
> +	case V3D_CPU_JOB_TYPE_COPY_PERFORMANCE_QUERY:
> +		/*
> +		 * Each query writes nperfmons * DRM_V3D_MAX_PERF_COUNTERS
> +		 * counter slots into bo[0], plus an availability slot at index
> +		 * ncounters. nperfmons and ncounters are user values; the slot
> +		 * count and write size are computed in u64 (u32 * small
> +		 * constant) so they cannot wrap.
> +		 */

I preferred the previous comment (from v2):

/*
  * Each query writes nperfmons * DRM_V3D_MAX_PERF_COUNTERS
  * counter slots into bo[0], plus an availability slot at
  * index ncounters. nperfmons and ncounters are user values,
  * so the slot count is computed overflow-safe.
  */

> +		dst = to_v3d_bo(job->base.bo[0]);
> +
> +		slots = (u64)job->performance_query.nperfmons *
> +			DRM_V3D_MAX_PERF_COUNTERS;
> +		if (copy->availability_bit)
> +			slots = max(slots,
> +				    (u64)job->performance_query.ncounters + 1);
> +
> +		write_size = slots * elem;
> +		return v3d_check_copy_extent(dev, dst->base.base.size,
> +					     copy->offset, copy->stride,
> +					     job->performance_query.count,
> +					     write_size);
> +	case V3D_CPU_JOB_TYPE_INDIRECT_CSD: {
> +		struct v3d_indirect_csd_info *icsd = &job->indirect_csd;

I'd prefer to call it "indirect_csd".

> +
> +		/*
> +		 * The exec-time work-group rewrite reads three u32 counts from
> +		 * bo[0] at the user-supplied offset, then writes each count back
> +		 * into the indirect BO at a user-supplied u32 index. Reject a
> +		 * missing indirect BO (drm_gem_object_lookup() returned NULL for
> +		 * a bad handle, which the exec path would otherwise dereference)
> +		 * and bound both accesses.
> +		 */
> +		if (!icsd->indirect)
> +			return -ENOENT;

No need for that after [1].

[1] 
https://lore.kernel.org/dri-devel/20260703-v3d-cpu-job-fixes-v3-0-bc51b1f3eeb5@igalia.com/T/#m6c2cb9021bc6b524fb713150f94bdfee3553cf72

> +
> +		dst = to_v3d_bo(job->base.bo[0]);

New line. Also, instead of the big comment on the beginning of this
case, I'd prefer a smaller comment here explaining that 3 is the three
dimensions of workgroup counts (x,y,z).

> +		if ((u64)icsd->offset + 3 * sizeof(u32) > dst->base.base.size)
> +			goto err_range;
> +
> +		src = to_v3d_bo(icsd->indirect);

I'm not sure src makes a lot of sense here, as we copy from BO to the
indirect BO. Maybe dst?

> +		for (i = 0; i < 3; i++) {
> +			u32 uidx = icsd->wg_uniform_offsets[i];
> +
> +			if (uidx != 0xffffffff &&

Add a comment explaining that, as 0xffffffff means "skip this rewrite",
the exec path doesn't write it, so there is no need to range-check it.

Finally, although it's a fix, I'd prefer to apply it to drm-misc-next as
(1) it's a reasonably big change and (2) it's not a critical bug. So, if
possible, rebase the patch on top of drm-misc-next.

Best regards,
- Maíra

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

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-17 12:58 Michael Bommarito
2026-07-04 12:11 ` Maíra Canal [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=10d3d64a-0321-4223-8a56-d32a654297fd@igalia.com \
    --to=mcanal@igalia.com \
    --cc=airlied@gmail.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=itoral@igalia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=michael.bommarito@gmail.com \
    --cc=mripard@kernel.org \
    --cc=mwen@igalia.com \
    --cc=simona@ffwll.ch \
    --cc=tzimmermann@suse.de \
    /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

all inboxes | Powered by JetHome®