* [PATCH 1/6] drm/shmem: Add a drm_gem_shmem_sync() helper
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 ` Maíra Canal
2026-11-19 19:38 ` [PATCH 2/6] drm/panfrost: Refuse to export WB_MMAP BOs Maíra Canal
` (5 subsequent siblings)
6 siblings, 0 replies; 10+ messages in thread
From: Maíra Canal @ 2026-11-19 19:38 UTC (permalink / raw)
To: Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie,
Simona Vetter, Faith Ekstrand, Boris Brezillon, Rob Herring,
Steven Price, Adrián Larumbe, Liviu Dudau, Akash Goel,
Karunika Choo, Lukas Zapolskas, Chris Diamand, Daniel Almeida,
Alice Ryhl
Cc: kernel-dev, dri-devel, rust-for-linux, Maíra Canal
From: Faith Ekstrand <faith.ekstrand@collabora.com>
This enables syncing mapped GEM objects between the CPU and GPU via calls
to dma_sync_*(). It's a bit annoying as it requires walking the sg_table
so it's best if every driver doesn't hand-roll it.
Signed-off-by: Faith Ekstrand <faith.ekstrand@collabora.com>
Signed-off-by: Boris Brezillon <boris.brezillon@collabora.com>
Reviewed-by: Steven Price <steven.price@arm.com>
[mcanal: rebased on top of drm-misc-next and fixed a kernel-doc nit]
Signed-off-by: Maíra Canal <mcanal@igalia.com>
---
drivers/gpu/drm/drm_gem_shmem_helper.c | 93 ++++++++++++++++++++++++++++++++++
include/drm/drm_gem_shmem_helper.h | 17 +++++++
2 files changed, 110 insertions(+)
diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c
index d654a22a30de..72933ed28373 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);
+
+ 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;
+
+ /* It's unclear whether dma_sync_xxx() is the right API to do CPU
+ * cache maintenance given an IOMMU can register their own
+ * implementation doing more than just CPU cache flushes/invalidation,
+ * and what we really care about here is CPU caches only, but that's
+ * the best we have that is both arch-agnostic and does at least the
+ * CPU cache maintenance on a <page,offset,size> tuple.
+ *
+ * Also, I wish we could do a single
+ *
+ * dma_sync_single_for_device(BIDIR)
+ *
+ * and get a flush+invalidate, but that's not how it's implemented
+ * in practice (at least on arm64), so we have to make it
+ *
+ * dma_sync_single_for_device(TO_DEVICE)
+ * dma_sync_single_for_cpu(FROM_DEVICE)
+ *
+ * for the flush+invalidate case.
+ */
+ 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);
+ }
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(drm_gem_shmem_sync);
+
/**
* drm_gem_shmem_print_info() - Print &drm_gem_shmem_object info for debugfs
* @shmem: shmem GEM object
diff --git a/include/drm/drm_gem_shmem_helper.h b/include/drm/drm_gem_shmem_helper.h
index bcbb92f3fe9b..a5def70fae7a 100644
--- a/include/drm/drm_gem_shmem_helper.h
+++ b/include/drm/drm_gem_shmem_helper.h
@@ -122,6 +122,23 @@ void drm_gem_shmem_vunmap_locked(struct drm_gem_shmem_object *shmem,
struct iosys_map *map);
int drm_gem_shmem_mmap(struct drm_gem_shmem_object *shmem, struct vm_area_struct *vma);
+/**
+ * enum drm_gem_shmem_sync_type - Type of synchronization
+ */
+enum drm_gem_shmem_sync_type {
+ /** @DRM_GEM_SHMEM_SYNC_CPU_CACHE_FLUSH: Flush CPU caches */
+ DRM_GEM_SHMEM_SYNC_CPU_CACHE_FLUSH = 0,
+
+ /**
+ * @DRM_GEM_SHMEM_SYNC_CPU_CACHE_FLUSH_AND_INVALIDATE: Flush and
+ * invalidate CPU caches
+ */
+ DRM_GEM_SHMEM_SYNC_CPU_CACHE_FLUSH_AND_INVALIDATE,
+};
+
+int drm_gem_shmem_sync(struct drm_gem_shmem_object *shmem, size_t offset,
+ size_t size, enum drm_gem_shmem_sync_type type);
+
int drm_gem_shmem_pin_locked(struct drm_gem_shmem_object *shmem);
void drm_gem_shmem_unpin_locked(struct drm_gem_shmem_object *shmem);
--
2.56.0
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH 2/6] drm/panfrost: Refuse to export WB_MMAP BOs
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:38 ` Maíra Canal
2026-11-19 19:38 ` [PATCH 3/6] drm/panfrost: Drop custom dma_buf_ops Maíra Canal
` (4 subsequent siblings)
6 siblings, 0 replies; 10+ messages in thread
From: Maíra Canal @ 2026-11-19 19:38 UTC (permalink / raw)
To: Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie,
Simona Vetter, Faith Ekstrand, Boris Brezillon, Rob Herring,
Steven Price, Adrián Larumbe, Liviu Dudau, Akash Goel,
Karunika Choo, Lukas Zapolskas, Chris Diamand, Daniel Almeida,
Alice Ryhl
Cc: kernel-dev, dri-devel, rust-for-linux, Maíra Canal
When a buffer is shared through dma-buf, clients are expected to bracket
their CPU accesses with DMA_BUF_IOCTL_SYNC, but there's no way to ensure
they do. With WC mappings, there are no CPU cache lines to maintain, so
waiting on fences is enough. With WB mappings, a missed sync can leave
dirty cache lines behind, which may later be written back over data the
GPU produced for another client.
To avoid this, don't allow WB_MMAP BOs to be shared. PanVK already keeps
WB_MMAP BOs private, so this makes the kernel follow the same policy.
Suggested-by: Faith Ekstrand <faith.ekstrand@collabora.com>
Suggested-by: Boris Brezillon <boris.brezillon@collabora.com>
Signed-off-by: Maíra Canal <mcanal@igalia.com>
---
drivers/gpu/drm/panfrost/panfrost_gem.c | 4 ++++
include/uapi/drm/panfrost_drm.h | 1 +
2 files changed, 5 insertions(+)
diff --git a/drivers/gpu/drm/panfrost/panfrost_gem.c b/drivers/gpu/drm/panfrost/panfrost_gem.c
index 518f24549b9d..c7160223ea17 100644
--- a/drivers/gpu/drm/panfrost/panfrost_gem.c
+++ b/drivers/gpu/drm/panfrost/panfrost_gem.c
@@ -391,6 +391,10 @@ panfrost_gem_prime_export(struct drm_gem_object *obj, int flags)
.resv = obj->resv,
};
+ /* WB_MMAP BOs can't be shared. */
+ if (to_panfrost_bo(obj)->wb_mmap)
+ return ERR_PTR(-EINVAL);
+
return drm_gem_dmabuf_export(dev, &exp_info);
}
diff --git a/include/uapi/drm/panfrost_drm.h b/include/uapi/drm/panfrost_drm.h
index 7831c48c59c0..2cc44591e82b 100644
--- a/include/uapi/drm/panfrost_drm.h
+++ b/include/uapi/drm/panfrost_drm.h
@@ -126,6 +126,7 @@ struct drm_panfrost_wait_bo {
/* Valid flags to pass to drm_panfrost_create_bo.
* PANFROST_BO_WB_MMAP can't be set if PANFROST_BO_HEAP is.
+ * Also, PANFROST_BO_WB_MMAP BOs can't be exported.
*/
#define PANFROST_BO_NOEXEC 1
#define PANFROST_BO_HEAP 2
--
2.56.0
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH 3/6] drm/panfrost: Drop custom dma_buf_ops
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:38 ` [PATCH 2/6] drm/panfrost: Refuse to export WB_MMAP BOs Maíra Canal
@ 2026-11-19 19:38 ` Maíra Canal
2026-11-19 19:38 ` [PATCH 4/6] drm/panfrost: Use drm_gem_shmem_sync() Maíra Canal
` (3 subsequent siblings)
6 siblings, 0 replies; 10+ messages in thread
From: Maíra Canal @ 2026-11-19 19:38 UTC (permalink / raw)
To: Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie,
Simona Vetter, Faith Ekstrand, Boris Brezillon, Rob Herring,
Steven Price, Adrián Larumbe, Liviu Dudau, Akash Goel,
Karunika Choo, Lukas Zapolskas, Chris Diamand, Daniel Almeida,
Alice Ryhl
Cc: kernel-dev, dri-devel, rust-for-linux, Maíra Canal
The custom dma_buf_ops were added to provide CPU cache maintenance in
begin/end_cpu_access() for WB_MMAP BOs. As those can no longer be
exported, use the default PRIME ops.
On IO-coherent GPUs, all BOs are CPU-mapped WB, so exported BOs lose
begin/end_cpu_access() cache maintenance for non-coherent importers.
This restores the behavior prior to commit 3ae6637378e6 ("drm/panfrost:
Provide a custom dma_buf implementation").
Suggested-by: Faith Ekstrand <faith.ekstrand@collabora.com>
Suggested-by: Boris Brezillon <boris.brezillon@collabora.com>
Signed-off-by: Maíra Canal <mcanal@igalia.com>
---
drivers/gpu/drm/panfrost/panfrost_drv.c | 1 -
drivers/gpu/drm/panfrost/panfrost_gem.c | 118 +-------------------------------
drivers/gpu/drm/panfrost/panfrost_gem.h | 3 -
3 files changed, 1 insertion(+), 121 deletions(-)
diff --git a/drivers/gpu/drm/panfrost/panfrost_drv.c b/drivers/gpu/drm/panfrost/panfrost_drv.c
index eeda113037fe..0a6ff8bec66d 100644
--- a/drivers/gpu/drm/panfrost/panfrost_drv.c
+++ b/drivers/gpu/drm/panfrost/panfrost_drv.c
@@ -824,7 +824,6 @@ static const struct drm_driver panfrost_drm_driver = {
.minor = 7,
.gem_create_object = panfrost_gem_create_object,
- .gem_prime_import = panfrost_gem_prime_import,
.gem_prime_import_sg_table = panfrost_gem_prime_import_sg_table,
#ifdef CONFIG_DEBUG_FS
.debugfs_init = panfrost_device_debugfs_init,
diff --git a/drivers/gpu/drm/panfrost/panfrost_gem.c b/drivers/gpu/drm/panfrost/panfrost_gem.c
index c7160223ea17..7d04793cc6ce 100644
--- a/drivers/gpu/drm/panfrost/panfrost_gem.c
+++ b/drivers/gpu/drm/panfrost/panfrost_gem.c
@@ -289,130 +289,14 @@ static size_t panfrost_gem_rss(struct drm_gem_object *obj)
return 0;
}
-static struct sg_table *
-panfrost_gem_prime_map_dma_buf(struct dma_buf_attachment *attach,
- enum dma_data_direction dir)
-{
- struct sg_table *sgt = drm_gem_map_dma_buf(attach, dir);
-
- if (!IS_ERR(sgt))
- attach->priv = sgt;
-
- return sgt;
-}
-
-static void
-panfrost_gem_prime_unmap_dma_buf(struct dma_buf_attachment *attach,
- struct sg_table *sgt,
- enum dma_data_direction dir)
-{
- attach->priv = NULL;
- drm_gem_unmap_dma_buf(attach, sgt, dir);
-}
-
-static int
-panfrost_gem_prime_begin_cpu_access(struct dma_buf *dma_buf,
- enum dma_data_direction dir)
-{
- struct drm_gem_object *obj = dma_buf->priv;
- struct drm_device *dev = obj->dev;
- struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
- struct dma_buf_attachment *attach;
-
- dma_resv_lock(obj->resv, NULL);
- if (shmem->sgt)
- dma_sync_sgtable_for_cpu(dev->dev, shmem->sgt, dir);
-
- if (shmem->vaddr)
- invalidate_kernel_vmap_range(shmem->vaddr, shmem->base.size);
-
- list_for_each_entry(attach, &dma_buf->attachments, node) {
- struct sg_table *sgt = attach->priv;
-
- if (sgt)
- dma_sync_sgtable_for_cpu(attach->dev, sgt, dir);
- }
- dma_resv_unlock(obj->resv);
-
- return 0;
-}
-
-static int
-panfrost_gem_prime_end_cpu_access(struct dma_buf *dma_buf,
- enum dma_data_direction dir)
-{
- struct drm_gem_object *obj = dma_buf->priv;
- struct drm_device *dev = obj->dev;
- struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
- struct dma_buf_attachment *attach;
-
- dma_resv_lock(obj->resv, NULL);
- list_for_each_entry(attach, &dma_buf->attachments, node) {
- struct sg_table *sgt = attach->priv;
-
- if (sgt)
- dma_sync_sgtable_for_device(attach->dev, sgt, dir);
- }
-
- if (shmem->vaddr)
- flush_kernel_vmap_range(shmem->vaddr, shmem->base.size);
-
- if (shmem->sgt)
- dma_sync_sgtable_for_device(dev->dev, shmem->sgt, dir);
-
- dma_resv_unlock(obj->resv);
- return 0;
-}
-
-static const struct dma_buf_ops panfrost_dma_buf_ops = {
- .attach = drm_gem_map_attach,
- .detach = drm_gem_map_detach,
- .map_dma_buf = panfrost_gem_prime_map_dma_buf,
- .unmap_dma_buf = panfrost_gem_prime_unmap_dma_buf,
- .release = drm_gem_dmabuf_release,
- .mmap = drm_gem_dmabuf_mmap,
- .vmap = drm_gem_dmabuf_vmap,
- .vunmap = drm_gem_dmabuf_vunmap,
- .begin_cpu_access = panfrost_gem_prime_begin_cpu_access,
- .end_cpu_access = panfrost_gem_prime_end_cpu_access,
-};
-
static struct dma_buf *
panfrost_gem_prime_export(struct drm_gem_object *obj, int flags)
{
- struct drm_device *dev = obj->dev;
- struct dma_buf_export_info exp_info = {
- .exp_name = KBUILD_MODNAME,
- .owner = THIS_MODULE,
- .ops = &panfrost_dma_buf_ops,
- .size = obj->size,
- .flags = flags,
- .priv = obj,
- .resv = obj->resv,
- };
-
/* WB_MMAP BOs can't be shared. */
if (to_panfrost_bo(obj)->wb_mmap)
return ERR_PTR(-EINVAL);
- return drm_gem_dmabuf_export(dev, &exp_info);
-}
-
-struct drm_gem_object *
-panfrost_gem_prime_import(struct drm_device *dev,
- struct dma_buf *dma_buf)
-{
- struct drm_gem_object *obj = dma_buf->priv;
-
- if (dma_buf->ops == &panfrost_dma_buf_ops && obj->dev == dev) {
- /* Importing dmabuf exported from our own gem increases
- * refcount on gem itself instead of f_count of dmabuf.
- */
- drm_gem_object_get(obj);
- return obj;
- }
-
- return drm_gem_prime_import(dev, dma_buf);
+ return drm_gem_prime_export(obj, flags);
}
static const struct drm_gem_object_funcs panfrost_gem_funcs = {
diff --git a/drivers/gpu/drm/panfrost/panfrost_gem.h b/drivers/gpu/drm/panfrost/panfrost_gem.h
index 3008afcf4bff..b80bd5f2295f 100644
--- a/drivers/gpu/drm/panfrost/panfrost_gem.h
+++ b/drivers/gpu/drm/panfrost/panfrost_gem.h
@@ -138,9 +138,6 @@ struct drm_gem_object *
panfrost_gem_prime_import_sg_table(struct drm_device *dev,
struct dma_buf_attachment *attach,
struct sg_table *sgt);
-struct drm_gem_object *
-panfrost_gem_prime_import(struct drm_device *dev,
- struct dma_buf *dma_buf);
struct panfrost_gem_object *
panfrost_gem_create(struct drm_device *dev, size_t size, u32 flags);
--
2.56.0
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH 4/6] drm/panfrost: Use drm_gem_shmem_sync()
2026-11-19 19:38 [PATCH 0/6] drm/panfrost,panthor: Refuse to export WB_MMAP BOs Maíra Canal
` (2 preceding siblings ...)
2026-11-19 19:38 ` [PATCH 3/6] drm/panfrost: Drop custom dma_buf_ops Maíra Canal
@ 2026-11-19 19:38 ` Maíra Canal
2026-11-19 19:38 ` [PATCH 5/6] drm/panthor: Refuse to export WB_MMAP BOs Maíra Canal
` (2 subsequent siblings)
6 siblings, 0 replies; 10+ messages in thread
From: Maíra Canal @ 2026-11-19 19:38 UTC (permalink / raw)
To: Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie,
Simona Vetter, Faith Ekstrand, Boris Brezillon, Rob Herring,
Steven Price, Adrián Larumbe, Liviu Dudau, Akash Goel,
Karunika Choo, Lukas Zapolskas, Chris Diamand, Daniel Almeida,
Alice Ryhl
Cc: kernel-dev, dri-devel, rust-for-linux, Maíra Canal
panfrost_gem_sync() has the same code as the function drm_gem_shmem_sync().
Translate the UAPI sync type to the common shmem type and use the shmem
helper instead.
Signed-off-by: Maíra Canal <mcanal@igalia.com>
---
drivers/gpu/drm/panfrost/panfrost_gem.c | 74 +++------------------------------
1 file changed, 5 insertions(+), 69 deletions(-)
diff --git a/drivers/gpu/drm/panfrost/panfrost_gem.c b/drivers/gpu/drm/panfrost/panfrost_gem.c
index 7d04793cc6ce..5b4401b967aa 100644
--- a/drivers/gpu/drm/panfrost/panfrost_gem.c
+++ b/drivers/gpu/drm/panfrost/panfrost_gem.c
@@ -443,84 +443,20 @@ int
panfrost_gem_sync(struct drm_gem_object *obj, u32 type, u32 offset, u32 size)
{
struct panfrost_gem_object *bo = to_panfrost_bo(obj);
- struct drm_gem_shmem_object *shmem = &bo->base;
- 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;
+ enum drm_gem_shmem_sync_type shmem_type;
switch (type) {
case PANFROST_BO_SYNC_CPU_CACHE_FLUSH:
+ shmem_type = DRM_GEM_SHMEM_SYNC_CPU_CACHE_FLUSH;
+ break;
case PANFROST_BO_SYNC_CPU_CACHE_FLUSH_AND_INVALIDATE:
+ shmem_type = 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);
-
- 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;
-
- /* It's unclear whether dma_sync_xxx() is the right API to do CPU
- * cache maintenance given an IOMMU can register their own
- * implementation doing more than just CPU cache flushes/invalidation,
- * and what we really care about here is CPU caches only, but that's
- * the best we have that is both arch-agnostic and does at least the
- * CPU cache maintenance on a <page,offset,size> tuple.
- *
- * Also, I wish we could do a single
- *
- * dma_sync_single_for_device(BIDIR)
- *
- * and get a flush+invalidate, but that's not how it's implemented
- * in practice (at least on arm64), so we have to make it
- *
- * dma_sync_single_for_device(TO_DEVICE)
- * dma_sync_single_for_cpu(FROM_DEVICE)
- *
- * for the flush+invalidate case.
- */
- dma_sync_single_for_device(dev->dev, paddr, len, DMA_TO_DEVICE);
- if (type == PANFROST_BO_SYNC_CPU_CACHE_FLUSH_AND_INVALIDATE)
- dma_sync_single_for_cpu(dev->dev, paddr, len, DMA_FROM_DEVICE);
- }
-
- return 0;
+ return drm_gem_shmem_sync(&bo->base, offset, size, shmem_type);
}
void
--
2.56.0
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH 5/6] drm/panthor: Refuse to export WB_MMAP BOs
2026-11-19 19:38 [PATCH 0/6] drm/panfrost,panthor: Refuse to export WB_MMAP BOs Maíra Canal
` (3 preceding siblings ...)
2026-11-19 19:38 ` [PATCH 4/6] drm/panfrost: Use drm_gem_shmem_sync() Maíra Canal
@ 2026-11-19 19:38 ` 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
6 siblings, 0 replies; 10+ messages in thread
From: Maíra Canal @ 2026-11-19 19:38 UTC (permalink / raw)
To: Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie,
Simona Vetter, Faith Ekstrand, Boris Brezillon, Rob Herring,
Steven Price, Adrián Larumbe, Liviu Dudau, Akash Goel,
Karunika Choo, Lukas Zapolskas, Chris Diamand, Daniel Almeida,
Alice Ryhl
Cc: kernel-dev, dri-devel, rust-for-linux, Maíra Canal
When a buffer is shared through dma-buf, clients are expected to bracket
their CPU accesses with DMA_BUF_IOCTL_SYNC, but there's no way to ensure
they do. With WC mappings, there are no CPU cache lines to maintain, so
waiting on fences is enough. With WB mappings, a missed sync can leave
dirty cache lines behind, which may later be written back over data the
GPU produced for another client.
To avoid this, don't allow WB_MMAP BOs to be shared. PanVK already keeps
WB_MMAP BOs private, so this makes the kernel enforce the same policy.
Suggested-by: Faith Ekstrand <faith.ekstrand@collabora.com>
Suggested-by: Boris Brezillon <boris.brezillon@collabora.com>
Signed-off-by: Maíra Canal <mcanal@igalia.com>
---
drivers/gpu/drm/panthor/panthor_gem.c | 4 ++++
include/uapi/drm/panthor_drm.h | 2 ++
2 files changed, 6 insertions(+)
diff --git a/drivers/gpu/drm/panthor/panthor_gem.c b/drivers/gpu/drm/panthor/panthor_gem.c
index 22b57ab6f929..730567d97f46 100644
--- a/drivers/gpu/drm/panthor/panthor_gem.c
+++ b/drivers/gpu/drm/panthor/panthor_gem.c
@@ -577,6 +577,10 @@ panthor_gem_prime_export(struct drm_gem_object *obj, int flags)
if (to_panthor_bo(obj)->exclusive_vm_root_gem)
return ERR_PTR(-EINVAL);
+ /* WB_MMAP BOs can't be shared. */
+ if (to_panthor_bo(obj)->flags & DRM_PANTHOR_BO_WB_MMAP)
+ return ERR_PTR(-EINVAL);
+
return drm_gem_dmabuf_export(dev, &exp_info);
}
diff --git a/include/uapi/drm/panthor_drm.h b/include/uapi/drm/panthor_drm.h
index a2ff0f4ec691..8f3a3e680264 100644
--- a/include/uapi/drm/panthor_drm.h
+++ b/include/uapi/drm/panthor_drm.h
@@ -771,6 +771,8 @@ enum drm_panthor_bo_flags {
* CPU map the buffer object in userspace by forcing the "Write-Back
* Cacheable" cacheability attribute. The mapping otherwise uses the
* "Non-Cacheable" attribute if the GPU is not IO coherent.
+ *
+ * Buffer objects created with this flag can't be exported.
*/
DRM_PANTHOR_BO_WB_MMAP = (1 << 1),
};
--
2.56.0
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH 6/6] drm/panthor: Drop custom dma_buf_ops
2026-11-19 19:38 [PATCH 0/6] drm/panfrost,panthor: Refuse to export WB_MMAP BOs Maíra Canal
` (4 preceding siblings ...)
2026-11-19 19:38 ` [PATCH 5/6] drm/panthor: Refuse to export WB_MMAP BOs Maíra Canal
@ 2026-11-19 19:38 ` Maíra Canal
2026-11-20 9:56 ` [PATCH 0/6] drm/panfrost,panthor: Refuse to export WB_MMAP BOs Boris Brezillon
6 siblings, 0 replies; 10+ messages in thread
From: Maíra Canal @ 2026-11-19 19:38 UTC (permalink / raw)
To: Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie,
Simona Vetter, Faith Ekstrand, Boris Brezillon, Rob Herring,
Steven Price, Adrián Larumbe, Liviu Dudau, Akash Goel,
Karunika Choo, Lukas Zapolskas, Chris Diamand, Daniel Almeida,
Alice Ryhl
Cc: kernel-dev, dri-devel, rust-for-linux, Maíra Canal
The custom dma_buf_ops were added to provide CPU cache maintenance in
begin/end_cpu_access() for WB_MMAP BOs. As those can no longer be
exported, use the default PRIME ops.
On IO-coherent GPUs, all BOs are CPU-mapped WB, so exported BOs lose
begin/end_cpu_access() cache maintenance for non-coherent importers.
This restores the behavior prior to commit 925ad0c26dd8 ("drm/panthor:
Provide a custom dma_buf implementation").
Suggested-by: Faith Ekstrand <faith.ekstrand@collabora.com>
Suggested-by: Boris Brezillon <boris.brezillon@collabora.com>
Signed-off-by: Maíra Canal <mcanal@igalia.com>
---
drivers/gpu/drm/panthor/panthor_drv.c | 1 -
drivers/gpu/drm/panthor/panthor_gem.c | 118 +---------------------------------
drivers/gpu/drm/panthor/panthor_gem.h | 4 --
3 files changed, 1 insertion(+), 122 deletions(-)
diff --git a/drivers/gpu/drm/panthor/panthor_drv.c b/drivers/gpu/drm/panthor/panthor_drv.c
index dc8d0191aef4..8712587b37d6 100644
--- a/drivers/gpu/drm/panthor/panthor_drv.c
+++ b/drivers/gpu/drm/panthor/panthor_drv.c
@@ -1801,7 +1801,6 @@ static const struct drm_driver panthor_drm_driver = {
.minor = 9,
.gem_prime_import_sg_table = panthor_gem_prime_import_sg_table,
- .gem_prime_import = panthor_gem_prime_import,
#ifdef CONFIG_DEBUG_FS
.debugfs_init = panthor_debugfs_init,
#endif
diff --git a/drivers/gpu/drm/panthor/panthor_gem.c b/drivers/gpu/drm/panthor/panthor_gem.c
index 730567d97f46..af8c94031d14 100644
--- a/drivers/gpu/drm/panthor/panthor_gem.c
+++ b/drivers/gpu/drm/panthor/panthor_gem.c
@@ -471,108 +471,9 @@ static void panthor_gem_free_object(struct drm_gem_object *obj)
drm_gem_object_put(vm_root_gem);
}
-static struct sg_table *
-panthor_gem_prime_map_dma_buf(struct dma_buf_attachment *attach,
- enum dma_data_direction dir)
-{
- struct sg_table *sgt = drm_gem_map_dma_buf(attach, dir);
-
- if (!IS_ERR(sgt))
- attach->priv = sgt;
-
- return sgt;
-}
-
-static void
-panthor_gem_prime_unmap_dma_buf(struct dma_buf_attachment *attach,
- struct sg_table *sgt,
- enum dma_data_direction dir)
-{
- attach->priv = NULL;
- drm_gem_unmap_dma_buf(attach, sgt, dir);
-}
-
-static int
-panthor_gem_prime_begin_cpu_access(struct dma_buf *dma_buf,
- enum dma_data_direction dir)
-{
- struct drm_gem_object *obj = dma_buf->priv;
- struct drm_device *dev = obj->dev;
- struct panthor_gem_object *bo = to_panthor_bo(obj);
- struct dma_buf_attachment *attach;
-
- dma_resv_lock(obj->resv, NULL);
- if (bo->dmap.sgt)
- dma_sync_sgtable_for_cpu(drm_dev_dma_dev(dev), bo->dmap.sgt, dir);
-
- if (bo->cmap.vaddr)
- invalidate_kernel_vmap_range(bo->cmap.vaddr, bo->base.size);
-
- list_for_each_entry(attach, &dma_buf->attachments, node) {
- struct sg_table *sgt = attach->priv;
-
- if (sgt)
- dma_sync_sgtable_for_cpu(attach->dev, sgt, dir);
- }
- dma_resv_unlock(obj->resv);
-
- return 0;
-}
-
-static int
-panthor_gem_prime_end_cpu_access(struct dma_buf *dma_buf,
- enum dma_data_direction dir)
-{
- struct drm_gem_object *obj = dma_buf->priv;
- struct drm_device *dev = obj->dev;
- struct panthor_gem_object *bo = to_panthor_bo(obj);
- struct dma_buf_attachment *attach;
-
- dma_resv_lock(obj->resv, NULL);
- list_for_each_entry(attach, &dma_buf->attachments, node) {
- struct sg_table *sgt = attach->priv;
-
- if (sgt)
- dma_sync_sgtable_for_device(attach->dev, sgt, dir);
- }
-
- if (bo->cmap.vaddr)
- flush_kernel_vmap_range(bo->cmap.vaddr, bo->base.size);
-
- if (bo->dmap.sgt)
- dma_sync_sgtable_for_device(drm_dev_dma_dev(dev), bo->dmap.sgt, dir);
-
- dma_resv_unlock(obj->resv);
- return 0;
-}
-
-static const struct dma_buf_ops panthor_dma_buf_ops = {
- .attach = drm_gem_map_attach,
- .detach = drm_gem_map_detach,
- .map_dma_buf = panthor_gem_prime_map_dma_buf,
- .unmap_dma_buf = panthor_gem_prime_unmap_dma_buf,
- .release = drm_gem_dmabuf_release,
- .mmap = drm_gem_dmabuf_mmap,
- .vmap = drm_gem_dmabuf_vmap,
- .vunmap = drm_gem_dmabuf_vunmap,
- .begin_cpu_access = panthor_gem_prime_begin_cpu_access,
- .end_cpu_access = panthor_gem_prime_end_cpu_access,
-};
-
static struct dma_buf *
panthor_gem_prime_export(struct drm_gem_object *obj, int flags)
{
- struct drm_device *dev = obj->dev;
- struct dma_buf_export_info exp_info = {
- .exp_name = KBUILD_MODNAME,
- .owner = THIS_MODULE,
- .ops = &panthor_dma_buf_ops,
- .size = obj->size,
- .flags = flags,
- .priv = obj,
- .resv = obj->resv,
- };
-
/* We can't export GEMs that have an exclusive VM. */
if (to_panthor_bo(obj)->exclusive_vm_root_gem)
return ERR_PTR(-EINVAL);
@@ -581,24 +482,7 @@ panthor_gem_prime_export(struct drm_gem_object *obj, int flags)
if (to_panthor_bo(obj)->flags & DRM_PANTHOR_BO_WB_MMAP)
return ERR_PTR(-EINVAL);
- return drm_gem_dmabuf_export(dev, &exp_info);
-}
-
-struct drm_gem_object *
-panthor_gem_prime_import(struct drm_device *dev,
- struct dma_buf *dma_buf)
-{
- struct drm_gem_object *obj = dma_buf->priv;
-
- if (dma_buf->ops == &panthor_dma_buf_ops && obj->dev == dev) {
- /* Importing dmabuf exported from our own gem increases
- * refcount on gem itself instead of f_count of dmabuf.
- */
- drm_gem_object_get(obj);
- return obj;
- }
-
- return drm_gem_prime_import(dev, dma_buf);
+ return drm_gem_prime_export(obj, flags);
}
static void panthor_gem_print_info(struct drm_printer *p, unsigned int indent,
diff --git a/drivers/gpu/drm/panthor/panthor_gem.h b/drivers/gpu/drm/panthor/panthor_gem.h
index 5ae37d0d3646..444ef9f66adf 100644
--- a/drivers/gpu/drm/panthor/panthor_gem.h
+++ b/drivers/gpu/drm/panthor/panthor_gem.h
@@ -274,10 +274,6 @@ void panthor_gem_kernel_bo_set_label(struct panthor_kernel_bo *bo, const char *l
int panthor_gem_sync(struct drm_gem_object *obj,
u32 type, u64 offset, u64 size);
-struct drm_gem_object *
-panthor_gem_prime_import(struct drm_device *dev,
- struct dma_buf *dma_buf);
-
static inline u64
panthor_kernel_bo_gpuva(struct panthor_kernel_bo *bo)
{
--
2.56.0
^ permalink raw reply related [flat|nested] 10+ messages in thread* Re: [PATCH 0/6] drm/panfrost,panthor: Refuse to export WB_MMAP BOs
2026-11-19 19:38 [PATCH 0/6] drm/panfrost,panthor: Refuse to export WB_MMAP BOs Maíra Canal
` (5 preceding siblings ...)
2026-11-19 19:38 ` [PATCH 6/6] drm/panthor: Drop custom dma_buf_ops Maíra Canal
@ 2026-11-20 9:56 ` Boris Brezillon
2026-11-20 16:05 ` Maíra Canal
6 siblings, 1 reply; 10+ messages in thread
From: Boris Brezillon @ 2026-11-20 9:56 UTC (permalink / raw)
To: Maíra Canal
Cc: Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie,
Simona Vetter, Faith Ekstrand, Rob Herring, Steven Price,
Adrián Larumbe, Liviu Dudau, Akash Goel, Karunika Choo,
Lukas Zapolskas, Chris Diamand, Daniel Almeida, Alice Ryhl,
kernel-dev, dri-devel, rust-for-linux
Hi Maira,
On Thu, 19 Nov 2026 16:38:15 -0300
Maíra Canal <mcanal@igalia.com> wrote:
> This series follows up on the discussion in the V3D cached maps series
> [1], about how to handle CPU-cached BOs that are shared through dma-buf.
>
> As pointed by Faith and Boris, when a buffer is shared through dma-buf,
> there's no way to ensure that a client will properly bracket their CPU
> accesses with DMA_BUF_IOCTL_SYNC. With WC mappings, waiting on fences is
> enough to guarantee consistency. However, with WB mappings, a missed sync
> can leave dirty cache lines behind, which another client can end up
> writing over.
>
> PanVK already keeps WB_MMAP BOs private, so this series only makes the
> kernel enforce the same policy on Panfrost and Panthor. As WB_MMAP BOs can
> no longer be exported, the custom dma_buf_ops providing begin/end_cpu_access()
> are no longer needed for them and are replaced by the default PRIME ops.
>
> - Patch 1 adds the drm_gem_shmem_sync() helper, taken from [2].
> - Patches 2 and 5 refuse to export WB_MMAP BOs in Panfrost and Panthor.
> - Patches 3 and 6 drop the custom dma_buf_ops from Panfrost and Panthor.
> - Patch 4 converts Panfrost to drm_gem_shmem_sync(). V3D will also use it
> to implement its cached maps, which I'll send in a separate series.
>
> Patches 3 and 6 need some extra attention as they aren't a no-op on
> IO-coherent GPUs. In this case, all BOs are CPU-mapped WB, and the custom
> begin/end_cpu_access() could be used to sync the mappings of non-coherent
> importers.
Ah, I completely forgot about this, thanks for the reminder. If we have
a system where the GPU is IO-coherent, but the video decoder or the
display controller isn't, we either need to force CPU-uncached
mappings for everybody with extra forced syncs on the UMD side for that
case (right now we assume that IO coherent implies always-WB IIRC), or
we need CPU access signalization with a proper begin/end_cpu_access()
implementation to keep things consistent when cross-dev sharing is
involved. I think this was the very reason I decided to keep the
begin/end_cpu_access() implementation in panfrost/panthor in the first
place.
Note that the same problem can exist if the other device is the
exporter and is IO coherent, but the GPU isn't. If the exporter were to
expose CPU mappings as cached without proper sync hooks, we'd face the
same problem. One way to avoid it would be to never CPU map imported
buffers in mesa, but IIRC, that's not an option.
>
> However, I decided to send these patches either way because they only
> restore the behavior prior to commits 3ae6637378e6 and 925ad0c26dd8, which
> is what we discussed in [1] (IIUC). Also, Mesa writes through the GEM mmap
> without DMA_BUF_IOCTL_SYNC, so it never relied on these hooks anyway.
Mesa doesn't, but there might be other elements in-between (video
decoder, and some custom layers analyzing/touching frames from the CPU
before they're passed to the GPU). I get the "it used to work" argument,
but I'm worried we're going backwards here. I also hate the fact we're
going back to a "broken" implementation without even updating the dmabuf
docs to explain that this sync-around-CPU-access mechanism is
deprecated/fragile/broken and shouldn't be relied on.
>
> Still, if you think this would be problematic, I can drop patches 3 and 6.
I was about to suggest forcing uncached CPU mappings for shareable BOs
on systems where the GPU is IO coherent, but there's actually no way we
can know whether a BO will be exported on not on Panfrost (on Panthor
we at least have this exclusive_vm thing that gives this guarantee). So
there's actually no way around it, it's either going back to a "broken"
state (broken in the sense it's not doing what it's supposed to, but no
one really complained in the past) or keep this sync logic to try to do
our best (even if it's not entirely safe, especially if the importer
does crazy stuff like pretending to be UMA when it's not).
Sorry for not having a better answer than: it might be okay, but it
looks wrong. Every time I tried to understand what's expected from us
for this shared buffer synchronization, I ended up more confused than
before...
>
> The Panthor changes were tested on hardware, but Panfrost changes were
> only compile-tested.
Could you prepare a custom gfx-ci kernel [1] so we can check that mesa
CI is not regressed by this change? I know none of the tests in mesa CI
would normally hit the removed logic, but better safe than sorry.
Regards,
Boris
[1]https://gitlab.freedesktop.org/gfx-ci/linux
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: [PATCH 0/6] drm/panfrost,panthor: Refuse to export WB_MMAP BOs
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
0 siblings, 1 reply; 10+ messages in thread
From: Maíra Canal @ 2026-11-20 16:05 UTC (permalink / raw)
To: Boris Brezillon
Cc: Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie,
Simona Vetter, Faith Ekstrand, Rob Herring, Steven Price,
Adrián Larumbe, Liviu Dudau, Akash Goel, Karunika Choo,
Lukas Zapolskas, Chris Diamand, Daniel Almeida, Alice Ryhl,
kernel-dev, dri-devel, rust-for-linux
Hi Boris,
On 20/11/26 06:56, Boris Brezillon wrote:
>>
>> Still, if you think this would be problematic, I can drop patches 3 and 6.
>
> I was about to suggest forcing uncached CPU mappings for shareable BOs
> on systems where the GPU is IO coherent, but there's actually no way we
> can know whether a BO will be exported on not on Panfrost (on Panthor
> we at least have this exclusive_vm thing that gives this guarantee). So
> there's actually no way around it, it's either going back to a "broken"
> state (broken in the sense it's not doing what it's supposed to, but no
> one really complained in the past) or keep this sync logic to try to do
> our best (even if it's not entirely safe, especially if the importer
> does crazy stuff like pretending to be UMA when it's not).
>
> Sorry for not having a better answer than: it might be okay, but it
> looks wrong. Every time I tried to understand what's expected from us
> for this shared buffer synchronization, I ended up more confused than
> before...
>
That makes two of us :)
I'm okay dropping patches 3 and 6. I decided to send them as suggested
in our last discussion, but when I analyzed the IO-coherent case, I also
had the impression that this could be a step back.
V3D doesn't have the IO coherent case, so I can move forward with only
Faith's drm_gem_shmem_sync() helper and I won't need the custom
dma_buf_ops.
If possible, could you take a look at patches 1-2 and 4-5? I'd
appreciate it.
>>
>> The Panthor changes were tested on hardware, but Panfrost changes were
>> only compile-tested.
>
> Could you prepare a custom gfx-ci kernel [1] so we can check that mesa
> CI is not regressed by this change? I know none of the tests in mesa CI
> would normally hit the removed logic, but better safe than sorry.
Sure, I'll test the series on Mesa CI (without patches 3 and 6).
Best regards,
- Maíra
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH 0/6] drm/panfrost,panthor: Refuse to export WB_MMAP BOs
2026-11-20 16:05 ` Maíra Canal
@ 2026-11-20 16:24 ` Boris Brezillon
0 siblings, 0 replies; 10+ messages in thread
From: Boris Brezillon @ 2026-11-20 16:24 UTC (permalink / raw)
To: Maíra Canal
Cc: Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie,
Simona Vetter, Faith Ekstrand, Rob Herring, Steven Price,
Adrián Larumbe, Liviu Dudau, Akash Goel, Karunika Choo,
Lukas Zapolskas, Chris Diamand, Daniel Almeida, Alice Ryhl,
kernel-dev, dri-devel, rust-for-linux
On Fri, 20 Nov 2026 13:05:50 -0300
Maíra Canal <mcanal@igalia.com> wrote:
> Hi Boris,
>
> On 20/11/26 06:56, Boris Brezillon wrote:
> >>
> >> Still, if you think this would be problematic, I can drop patches 3 and 6.
> >
> > I was about to suggest forcing uncached CPU mappings for shareable BOs
> > on systems where the GPU is IO coherent, but there's actually no way we
> > can know whether a BO will be exported on not on Panfrost (on Panthor
> > we at least have this exclusive_vm thing that gives this guarantee). So
> > there's actually no way around it, it's either going back to a "broken"
> > state (broken in the sense it's not doing what it's supposed to, but no
> > one really complained in the past) or keep this sync logic to try to do
> > our best (even if it's not entirely safe, especially if the importer
> > does crazy stuff like pretending to be UMA when it's not).
> >
> > Sorry for not having a better answer than: it might be okay, but it
> > looks wrong. Every time I tried to understand what's expected from us
> > for this shared buffer synchronization, I ended up more confused than
> > before...
> >
>
> That makes two of us :)
>
> I'm okay dropping patches 3 and 6. I decided to send them as suggested
> in our last discussion, but when I analyzed the IO-coherent case, I also
> had the impression that this could be a step back.
>
> V3D doesn't have the IO coherent case, so I can move forward with only
> Faith's drm_gem_shmem_sync() helper and I won't need the custom
> dma_buf_ops.
>
> If possible, could you take a look at patches 1-2 and 4-5? I'd
> appreciate it.
They look good to me.
>
> >>
> >> The Panthor changes were tested on hardware, but Panfrost changes were
> >> only compile-tested.
> >
> > Could you prepare a custom gfx-ci kernel [1] so we can check that mesa
> > CI is not regressed by this change? I know none of the tests in mesa CI
> > would normally hit the removed logic, but better safe than sorry.
>
> Sure, I'll test the series on Mesa CI (without patches 3 and 6).
>
> Best regards,
> - Maíra
>
^ permalink raw reply [flat|nested] 10+ messages in thread