* [PATCH v2] drm/v3d: Fix double free of the sg_table when a PRIME import fails
@ 2026-11-19 14:43 Aleksei Riapolov
0 siblings, 0 replies; only message in thread
From: Aleksei Riapolov @ 2026-11-19 14:43 UTC (permalink / raw)
To: mwen, mcanal; +Cc: dri-devel, linux-kernel
When v3d_bo_create_finish() fails in v3d_prime_import_sg_table(), the
error path calls drm_gem_shmem_free() on an object that the DRM core
does not consider imported yet: obj->import_attach is only assigned by
drm_gem_prime_import_dev() after the callback has returned. So
drm_gem_shmem_release() takes the branch for native objects and unmaps
and frees shmem->sgt, which is the sg_table of the dma-buf attachment:
drm_gem_prime_import_dev()
sgt = dma_buf_map_attachment_unlocked(attach)
dev->driver->gem_prime_import_sg_table(dev, attach, sgt)
v3d_prime_import_sg_table()
drm_gem_shmem_prime_import_sg_table() shmem->sgt = sgt
v3d_bo_create_finish() -ENOSPC
drm_gem_shmem_free()
drm_gem_shmem_release()
__drm_gem_shmem_free_sgt_locked()
dma_unmap_sgtable(sgt)
sg_free_table(sgt)
kfree(sgt)
dma_buf_unmap_attachment_unlocked(attach, sgt) <- freed sgt
The caller then hands the freed table to the exporter, which unmaps and
frees it a second time.
v3d_bo_create_finish() fails here when the 4 GiB V3D address space is
exhausted and drm_mm_insert_node_generic() returns -ENOSPC, which a
client of the render node can arrange with imports alone. On a
Raspberry Pi 5 (BCM2712, v7.4-rc2) booted with slub_debug=FZPU the
import that hits -ENOSPC oopses:
Unable to handle kernel paging request at virtual address 006b6b6b6b6b6b87
pc : dma_direct_unmap_sg+0x90/0x268
lr : dma_unmap_sg_attrs+0x60/0x1c8
Call trace:
dma_direct_unmap_sg+0x90/0x268 (P)
dma_unmap_sg_attrs+0x60/0x1c8
unmap_udmabuf+0x34/0x60
dma_buf_unmap_attachment+0x3c/0xa0
dma_buf_unmap_attachment_unlocked+0x58/0x98
drm_gem_prime_import_dev+0x1a0/0x1e0
drm_gem_prime_fd_to_handle+0x1a8/0x280
drm_prime_fd_to_handle_ioctl+0x40/0x58
drm_ioctl_kernel+0xc8/0x140
drm_ioctl+0x368/0x4e8
__arm64_sys_ioctl+0xb0/0x110
and the task is left unkillable, as it dies holding the reservation
lock of the dma-buf.
Inside the callback the object cannot be torn down as an import,
because the core has not marked it as one yet. Allocate the GPU address
after the import has completed instead: implement .gem_prime_import as
a wrapper around drm_gem_prime_import() and call v3d_bo_create_finish()
once it has returned. By then obj->import_attach is set, so freeing the
object on failure goes through drm_prime_gem_destroy(), which unmaps
the sg_table once and detaches. .gem_prime_import_sg_table becomes
drm_gem_shmem_prime_import_sg_table() itself.
For a dma-buf that this device exported, drm_gem_prime_import() returns
the BO it was exported from. That BO already has its GPU address, so
the wrapper returns it untouched.
The error path frees the object with drm_gem_shmem_free(), as
v3d_bo_create() does, and not with drm_gem_object_put():
v3d_free_object() expects a BO that v3d_bo_create_finish() completed.
For one whose node was never inserted it would clear page table entries
starting at page 0, decrement bo_stats and call drm_mm_remove_node() on
a node that is not allocated. With drm_gem_object_put() there, the
failing import oopses in drm_mm_remove_node(), called from
v3d_free_object().
v3d_bo_create_finish() now runs after obj->resv has been pointed at the
reservation object of the dma-buf, so drm_gem_shmem_get_pages_sgt()
takes that lock instead of the private lock of a new object, and an
import can return -EINTR if a signal arrives while it waits for it.
With this change, on the same board and kernel:
- the failing import returns -ENOSPC in each of three consecutive
fills, and after 50 such failures debugfs dma_buf/bufinfo lists no
dma-buf;
- a v3d BO imported back into v3d through a second file is the same
BO at the same offset over 1000 iterations, with bo_stats back at
its starting value;
- a vc4 dumb buffer imported into v3d can be mapped, written, read
and closed;
- under labwc, glmark2-es2-wayland runs, and mpv playing 4K HEVC from
the hardware decoder imports the decoder's dma-heap buffers into
v3d about 2500 times, none of which is left attached afterwards;
the time an import takes and the number of dropped frames are the
same as with the unpatched module, with and without slub_debug.
The kernel log stays clean throughout. Other V3D generations were not
tested.
A reproducer is available on request.
Fixes: 40609d4820b2 ("drm/v3d: Use the new shmem helpers to reduce driver boilerplate.")
Cc: stable@vger.kernel.org
Suggested-by: Maíra Canal <mcanal@igalia.com>
Signed-off-by: Aleksei Riapolov <tsbol@icloud.com>
---
v2: Allocate the GPU address after the import has completed, in a
.gem_prime_import wrapper, instead of clearing shmem->sgt on the
error path of the callback (Maíra).
v1: https://lore.kernel.org/all/20261114164451.9222-1-tsbol@icloud.com/
For stable: drm_gem_is_imported() was added in v6.15, so earlier trees
need obj->import_attach in the wrapper.
drivers/gpu/drm/v3d/v3d_bo.c | 17 +++++++++++++----
drivers/gpu/drm/v3d/v3d_drv.c | 3 ++-
drivers/gpu/drm/v3d/v3d_drv.h | 5 ++---
3 files changed, 17 insertions(+), 8 deletions(-)
diff --git a/drivers/gpu/drm/v3d/v3d_bo.c b/drivers/gpu/drm/v3d/v3d_bo.c
index db95a2f..34aca97 100644
--- a/drivers/gpu/drm/v3d/v3d_bo.c
+++ b/drivers/gpu/drm/v3d/v3d_bo.c
@@ -172,17 +172,26 @@ free_obj:
}
struct drm_gem_object *
-v3d_prime_import_sg_table(struct drm_device *dev,
- struct dma_buf_attachment *attach,
- struct sg_table *sgt)
+v3d_prime_import(struct drm_device *dev, struct dma_buf *dma_buf)
{
struct drm_gem_object *obj;
int ret;
- obj = drm_gem_shmem_prime_import_sg_table(dev, attach, sgt);
+ obj = drm_gem_prime_import(dev, dma_buf);
if (IS_ERR(obj))
return obj;
+ /* A dma-buf exported by this device comes back as the BO it was
+ * exported from, which already has its GPU address.
+ */
+ if (!drm_gem_is_imported(obj))
+ return obj;
+
+ /* The GPU address is allocated only now that the core has set
+ * obj->import_attach, so that a failure here is torn down as an
+ * import: the sg_table is unmapped once and the attachment is
+ * detached.
+ */
ret = v3d_bo_create_finish(obj);
if (ret) {
drm_gem_shmem_free(&to_v3d_bo(obj)->base);
diff --git a/drivers/gpu/drm/v3d/v3d_drv.c b/drivers/gpu/drm/v3d/v3d_drv.c
index 8d914db..70b4e59 100644
--- a/drivers/gpu/drm/v3d/v3d_drv.c
+++ b/drivers/gpu/drm/v3d/v3d_drv.c
@@ -279,7 +279,8 @@ static const struct drm_driver v3d_drm_driver = {
#endif
.gem_create_object = v3d_create_object,
- .gem_prime_import_sg_table = v3d_prime_import_sg_table,
+ .gem_prime_import = v3d_prime_import,
+ .gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table,
.ioctls = v3d_drm_ioctls,
.num_ioctls = ARRAY_SIZE(v3d_drm_ioctls),
diff --git a/drivers/gpu/drm/v3d/v3d_drv.h b/drivers/gpu/drm/v3d/v3d_drv.h
index 7c7bdf6..63a3ca3 100644
--- a/drivers/gpu/drm/v3d/v3d_drv.h
+++ b/drivers/gpu/drm/v3d/v3d_drv.h
@@ -603,9 +603,8 @@ int v3d_get_bo_offset_ioctl(struct drm_device *dev, void *data,
struct drm_file *file_priv);
int v3d_wait_bo_ioctl(struct drm_device *dev, void *data,
struct drm_file *file_priv);
-struct drm_gem_object *v3d_prime_import_sg_table(struct drm_device *dev,
- struct dma_buf_attachment *attach,
- struct sg_table *sgt);
+struct drm_gem_object *v3d_prime_import(struct drm_device *dev,
+ struct dma_buf *dma_buf);
/* v3d_debugfs.c */
void v3d_debugfs_init(struct drm_minor *minor);
--
2.54.0 (Apple Git-157)
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-19 14:43 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 14:43 [PATCH v2] drm/v3d: Fix double free of the sg_table when a PRIME import fails Aleksei Riapolov
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®