All of 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, ojeda@kernel.org, gary@garyguo.net
Subject: Re: [PATCH 1/6] drm/shmem: Add a drm_gem_shmem_sync() helper
Date: Thu, 19 Nov 2026 19:55:20 +0000	[thread overview]
Message-ID: <sashiko-outbox-223986@kernel.org> (raw)
In-Reply-To: <20261119-pan-drop-dma-buf-hooks-v1-1-6c6f1629e7f9@igalia.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] drm/shmem: unlocked sgt traversal in drm_gem_shmem_sync()
  causes UAF
- [Medium] drm/shmem: drm_gem_shmem_sync() populates pages on purged or
  unbacked BOs
--

commit 9fdf5667225c80a2c62b9b65dbf8c174b87f854f
Author: Faith Ekstrand <faith.ekstrand@collabora.com>

drm/shmem: Add a drm_gem_shmem_sync() helper

This commit adds a drm_gem_shmem_sync() helper to flush or invalidate CPU
caches for a range of a shmem GEM object by walking its scatter-gather table
and calling dma_sync_single_for_device() and dma_sync_single_for_cpu().

> diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c
> index d654a22a30ded..72933ed283731 100644
> --- a/drivers/gpu/drm/drm_gem_shmem_helper.c
> +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c
> @@ -790,6 +790,99 @@ int drm_gem_shmem_mmap(struct drm_gem_shmem_object *shmem, struct vm_area_struct
>  }
>  EXPORT_SYMBOL_GPL(drm_gem_shmem_mmap);
>  
> +/**
> + * drm_gem_shmem_sync - Sync CPU-mapped data
> + * @shmem: shmem GEM object
> + * @offset: Offset into the GEM object
> + * @size: Size of the area to sync
> + * @type: Type of synchronization
> + *
> + * Returns:
> + * 0 on success or a negative error code on failure.
> + */
> +int drm_gem_shmem_sync(struct drm_gem_shmem_object *shmem, size_t offset,
> +		       size_t size, enum drm_gem_shmem_sync_type type)
> +{
> +	const struct drm_device *dev = shmem->base.dev;
> +	struct sg_table *sgt;
> +	struct scatterlist *sgl;
> +	unsigned int count;
> +
> +	/* Make sure the range is in bounds. */
> +	if (offset + size < offset || offset + size > shmem->base.size)
> +		return -EINVAL;
> +
> +	/* Disallow CPU-cache maintenance on imported buffers. */
> +	if (drm_gem_is_imported(&shmem->base))
> +		return -EINVAL;
> +
> +	switch (type) {
> +	case DRM_GEM_SHMEM_SYNC_CPU_CACHE_FLUSH:
> +	case DRM_GEM_SHMEM_SYNC_CPU_CACHE_FLUSH_AND_INVALIDATE:
> +		break;
> +
> +	default:
> +		return -EINVAL;
> +	}
> +
> +	/* Don't bother if it's WC-mapped */
> +	if (shmem->map_wc)
> +		return 0;
> +
> +	/* Nothing to do if the size is zero. */
> +	if (size == 0)
> +		return 0;
> +
> +	sgt = drm_gem_shmem_get_pages_sgt(shmem);
> +	if (IS_ERR(sgt))
> +		return PTR_ERR(sgt);

[Severity: Medium]
Should drm_gem_shmem_sync() populate backing pages and a scatter-gather
table when shmem->pages is NULL or the object has been purged?

Calling drm_gem_shmem_get_pages_sgt() on an unbacked object allocates and
DMA-maps pages when there is no CPU-cached data to sync.  If the object was
already purged (shmem->madv < 0), drm_gem_shmem_get_pages_sgt() allocates a
new shmem->sgt and pages (or takes another reference on truncated pages
kept alive by an open VMA), and because drm_gem_shmem_is_purgeable()
requires shmem->madv > 0, the newly populated pages and sgt can never be
purged again until the GEM object is freed.

> +
> +	for_each_sgtable_dma_sg(sgt, sgl, count) {
> +		if (size == 0)
> +			break;
> +
> +		dma_addr_t paddr = sg_dma_address(sgl);
> +		size_t len = sg_dma_len(sgl);
> +
> +		if (len <= offset) {
> +			offset -= len;
> +			continue;
> +		}
> +
> +		paddr += offset;
> +		len -= offset;
> +		len = min_t(size_t, len, size);
> +		size -= len;
> +		offset = 0;
[ ... ]
> +		dma_sync_single_for_device(dev->dev, paddr, len, DMA_TO_DEVICE);
> +		if (type == DRM_GEM_SHMEM_SYNC_CPU_CACHE_FLUSH_AND_INVALIDATE)
> +			dma_sync_single_for_cpu(dev->dev, paddr, len, DMA_FROM_DEVICE);
> +	}

[Severity: High]
Can sgt be unmapped and freed while drm_gem_shmem_sync() is walking it?

drm_gem_shmem_get_pages_sgt() unlocks shmem->base.resv before returning
sgt, and does not increment shmem->pages_pin_count.  If the buffer is
marked purgeable (shmem->madv > 0, pages_pin_count == 0), a concurrent
shrinker can purge it while the loop runs:

CPU0                                  CPU1
drm_gem_shmem_sync()
  drm_gem_shmem_get_pages_sgt()
    dma_resv_lock_interruptible(...)
    drm_gem_shmem_get_pages_sgt_locked()
    dma_resv_unlock(...)
                                      ...
                                        drm_gem_shmem_purge_locked()
                                          dma_unmap_sgtable(..., shmem->sgt, ...)
                                          sg_free_table(shmem->sgt)
                                          kfree(shmem->sgt)
  for_each_sgtable_dma_sg(sgt, ...)
    sg_dma_address(sgl)  <-- use-after-free

Holding shmem->base.resv across drm_gem_shmem_get_pages_sgt_locked() and
the scatterlist walk would prevent concurrent purging or sgt teardown.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119-pan-drop-dma-buf-hooks-v1-0-6c6f1629e7f9@igalia.com?part=1

  reply	other threads:[~2026-11-19 19:55 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 19:38 [PATCH 0/6] drm/panfrost,panthor: Refuse to export WB_MMAP BOs Maíra Canal
2026-11-19 19:38 ` [PATCH 1/6] drm/shmem: Add a drm_gem_shmem_sync() helper Maíra Canal
2026-11-19 19:55   ` sashiko-bot [this message]
2026-11-19 19:38 ` [PATCH 2/6] drm/panfrost: Refuse to export WB_MMAP BOs Maíra Canal
2026-11-19 19:38 ` [PATCH 3/6] drm/panfrost: Drop custom dma_buf_ops Maíra Canal
2026-11-19 19:38 ` [PATCH 4/6] drm/panfrost: Use drm_gem_shmem_sync() Maíra Canal
2026-11-19 19:38 ` [PATCH 5/6] drm/panthor: Refuse to export WB_MMAP BOs Maíra Canal
2026-11-19 19:38 ` [PATCH 6/6] drm/panthor: Drop custom dma_buf_ops Maíra Canal
2026-11-20  9:56 ` [PATCH 0/6] drm/panfrost,panthor: Refuse to export WB_MMAP BOs Boris Brezillon
2026-11-20 16:05   ` Maíra Canal
2026-11-20 16:24     ` Boris Brezillon

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-223986@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gary@garyguo.net \
    --cc=mcanal@igalia.com \
    --cc=ojeda@kernel.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.