drm/v3d: Fix double free of the sg_table when a PRIME import fails
Subsystems (3)
PATCH v2 1/1 drm/v3d: Fix double free of the sg_table when a PRIME import fails
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 sgtThe 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.
---
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/[email protected]/
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(-)
drivers/gpu/drm/v3d/v3d_bo.c +13 -4
@@ -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);
drivers/gpu/drm/v3d/v3d_drv.c +2 -1
@@ -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),
drivers/gpu/drm/v3d/v3d_drv.h +2 -3
@@ -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);
Signature · 1 line
2.54.0 (Apple Git-157)
Maíra Canal
maintainerHi Aleksei,
On 19/11/26 11:43, Aleksei Riapolov wrote:
[...]
Quoted 9 lines
The error path frees the object with
drm_gem_shmem_free(), asv3d_bo_create()does, and not withdrm_gem_object_put():v3d_free_object()expects a BO thatv3d_bo_create_finish()completed. For one whose node was never inserted it would clear page table entries starting at page 0, decrementbo_statsand calldrm_mm_remove_node()on a node that is not allocated. Withdrm_gem_object_put()there, the failing import oopses indrm_mm_remove_node(), called fromv3d_free_object().
You don't need to explain what you didn't do, only what you did.
Also, the following paragraphs can be deleted. They are not clarifying at all.
Quoted 22 lines
v3d_bo_create_finish()now runs afterobj->resvhas been pointed at the reservation object of the dma-buf, sodrm_gem_shmem_get_pages_sgt()takes that lock instead of the private lock of a new object, and an import can return-EINTRif a signal arrives while it waits for it.With this change, on the same board and kernel:
- the failing import returns
-ENOSPCin 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, withbo_statsback at its starting value;
- a vc4 dumb buffer imported into v3d can be mapped, written, readand 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.
Quoted 4 lines
The kernel log stays clean throughout. Other V3D generations were not tested.
A reproducer is available on request.
As a general note, please make this commit message more readable for humans, not LLMs.
Quoted 33 lines
Fixes 40609d4820b2 ("drm/v3d: Use the new shmem helpers to reduce driver boilerplate.")Suggested-by Maíra Canal <[email protected]>Signed-off-by Aleksei Riapolov <[email protected]>---
v2: Allocate the GPU address after the import has completed, in a
.gem_prime_import wrapper, instead of clearingshmem->sgton the error path of the callback (Maíra).
v1: https://lore.kernel.org/all/[email protected]/For stable:
drm_gem_is_imported()was added in v6.15, so earlier trees needobj->import_attachin 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(-)drivers/gpu/drm/v3d/v3d_bo.c +1
-3@@ -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;
drivers/gpu/drm/v3d/v3d_bo.c free_obj: · line 186
Quoted 10 lines
drivers/gpu/drm/v3d/v3d_bo.c +4
-1int 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. +*/
How about "/* Self-import returns the native BO, already mapped. */"?
drivers/gpu/drm/v3d/v3d_bo.c free_obj: · line 194
Quoted 8 lines
drivers/gpu/drm/v3d/v3d_bo.c +8
-0+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. +*/
This comment is superfluous. Please, delete it.
Apart from this issues, it looks good to me. I believe we will be able to merge it in v3.
Best regards,
- Maíra