mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] drm/v3d: Fix double free of the sg_table when a PRIME import fails
@ 2026-11-14 16:44 Aleksei Riapolov
  2026-11-18 12:54 ` Maíra Canal
  0 siblings, 1 reply; 3+ messages in thread
From: Aleksei Riapolov @ 2026-11-14 16:44 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.

Hand the table back to its owner before freeing the object: with
shmem->sgt cleared, drm_gem_shmem_release() leaves it alone and it is
unmapped once, by the exporter, on drm_gem_prime_import_dev()'s error
path. With this change the same test gets -ENOSPC from the failing
import in each of three consecutive fills and the kernel log stays
clean.

A reproducer is available on request.

Fixes: 40609d4820b2 ("drm/v3d: Use the new shmem helpers to reduce driver boilerplate.")
Cc: stable@vger.kernel.org
Signed-off-by: Aleksei Riapolov <tsbol@icloud.com>
---
 drivers/gpu/drm/v3d/v3d_bo.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/drivers/gpu/drm/v3d/v3d_bo.c b/drivers/gpu/drm/v3d/v3d_bo.c
index db95a2f..48c7807 100644
--- a/drivers/gpu/drm/v3d/v3d_bo.c
+++ b/drivers/gpu/drm/v3d/v3d_bo.c
@@ -185,6 +185,14 @@ v3d_prime_import_sg_table(struct drm_device *dev,
 
 	ret = v3d_bo_create_finish(obj);
 	if (ret) {
+		/*
+		 * obj->import_attach is only set by drm_gem_prime_import_dev()
+		 * after we return, so drm_gem_shmem_release() would treat this
+		 * object as native and unmap/free the attachment's sg_table,
+		 * which the caller unmaps again on its error path. Hand the
+		 * table back to its owner before freeing the object.
+		 */
+		to_v3d_bo(obj)->base.sgt = NULL;
 		drm_gem_shmem_free(&to_v3d_bo(obj)->base);
 		return ERR_PTR(ret);
 	}
-- 
2.54.0 (Apple Git-157)


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] drm/v3d: Fix double free of the sg_table when a PRIME import fails
  2026-11-14 16:44 [PATCH] drm/v3d: Fix double free of the sg_table when a PRIME import fails Aleksei Riapolov
@ 2026-11-18 12:54 ` Maíra Canal
  2026-11-19 14:45   ` Aleksei Riapolov
  0 siblings, 1 reply; 3+ messages in thread
From: Maíra Canal @ 2026-11-18 12:54 UTC (permalink / raw)
  To: Aleksei Riapolov, mwen; +Cc: dri-devel, linux-kernel

Hi Aleksei,

On 14/11/26 13:44, Aleksei Riapolov wrote:

[...]

> 
> Hand the table back to its owner before freeing the object: with
> shmem->sgt cleared, drm_gem_shmem_release() leaves it alone and it is
> unmapped once, by the exporter, on drm_gem_prime_import_dev()'s error
> path. With this change the same test gets -ENOSPC from the failing
> import in each of three consecutive fills and the kernel log stays
> clean.

The issue you reported is real and makes sense, but your fix only treats
the symptom, not the problem.

V3D allocates the GPU address inside the gem_prime_import_sg_table()
callback, before the core marks the object as imported. No teardown
would be correct at that point.

I'd rather move the allocation after the import completes. Implement
.gem_prime_import as a wrapper around drm_gem_prime_import() and call
v3d_bo_create_finish() once it returns. At that point import_attach is
set, so drm_gem_object_put() takes the regular import teardown path,
which unmaps the sg_table once and detaches.

Best regards,
- Maíra

> 
> A reproducer is available on request.
> 
> Fixes: 40609d4820b2 ("drm/v3d: Use the new shmem helpers to reduce driver boilerplate.")
> Cc: stable@vger.kernel.org
> Signed-off-by: Aleksei Riapolov <tsbol@icloud.com>
> ---
>   drivers/gpu/drm/v3d/v3d_bo.c | 8 ++++++++
>   1 file changed, 8 insertions(+)
> 
> diff --git a/drivers/gpu/drm/v3d/v3d_bo.c b/drivers/gpu/drm/v3d/v3d_bo.c
> index db95a2f..48c7807 100644
> --- a/drivers/gpu/drm/v3d/v3d_bo.c
> +++ b/drivers/gpu/drm/v3d/v3d_bo.c
> @@ -185,6 +185,14 @@ v3d_prime_import_sg_table(struct drm_device *dev,
>   
>   	ret = v3d_bo_create_finish(obj);
>   	if (ret) {
> +		/*
> +		 * obj->import_attach is only set by drm_gem_prime_import_dev()
> +		 * after we return, so drm_gem_shmem_release() would treat this
> +		 * object as native and unmap/free the attachment's sg_table,
> +		 * which the caller unmaps again on its error path. Hand the
> +		 * table back to its owner before freeing the object.
> +		 */
> +		to_v3d_bo(obj)->base.sgt = NULL;
>   		drm_gem_shmem_free(&to_v3d_bo(obj)->base);
>   		return ERR_PTR(ret);
>   	}


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] drm/v3d: Fix double free of the sg_table when a PRIME import fails
  2026-11-18 12:54 ` Maíra Canal
@ 2026-11-19 14:45   ` Aleksei Riapolov
  0 siblings, 0 replies; 3+ messages in thread
From: Aleksei Riapolov @ 2026-11-19 14:45 UTC (permalink / raw)
  To: mcanal, mwen; +Cc: dri-devel, linux-kernel

Hi Maíra,

On 18/11/26 09:54, Maíra Canal wrote:
> The issue you reported is real and makes sense, but your fix only treats
> the symptom, not the problem.
>
> V3D allocates the GPU address inside the gem_prime_import_sg_table()
> callback, before the core marks the object as imported. No teardown
> would be correct at that point.

Thanks for the review. v2 moves the allocation out of the callback:

  https://lore.kernel.org/all/20261119144339.43896-1-tsbol@icloud.com/

> I'd rather move the allocation after the import completes. Implement
> .gem_prime_import as a wrapper around drm_gem_prime_import() and call
> v3d_bo_create_finish() once it returns. At that point import_attach is
> set, so drm_gem_object_put() takes the regular import teardown path,
> which unmaps the sg_table once and detaches.

v2 follows this and differs from it in two places.

1. Same-device imports

For a dma-buf that this device exported, drm_gem_prime_import() returns
the BO it was exported from, with one more reference. That BO already
has its node in v3d->mm, so the wrapper returns it without calling
v3d_bo_create_finish() again. A compositor importing its clients'
buffers takes this path: in a glmark2-es2-wayland run under labwc all
5 calls of the wrapper were same-device imports.

2. drm_gem_shmem_free() on failure, not drm_gem_object_put()

drm_gem_object_put() ends in v3d_free_object(), which expects a BO that
v3d_bo_create_finish() completed. When the node was never inserted,
node.start is 0, so v3d_mmu_remove_ptes() would clear the page table
entries from page 0 up to the size of the BO, and those can belong to
other BOs. bo_stats would be decremented without having been
incremented, and drm_mm_remove_node() would run on a node that is not
allocated. With drm_gem_object_put() in the wrapper, the failing import
oopses on the same board:

  Unable to handle kernel NULL pointer dereference at virtual address 00000000000000c8
  pc : drm_mm_remove_node+0x34/0x388
  lr : v3d_free_object+0x88/0xc0 [v3d]
  Call trace:
   drm_mm_remove_node+0x34/0x388 (P)
   v3d_free_object+0x88/0xc0 [v3d]
   drm_gem_object_free+0x24/0x40
   v3d_prime_import+0xc8/0xe0 [v3d]
   drm_gem_prime_fd_to_handle+0xd4/0x280

and bo_stats then counts 14 BOs while 15 are alive. The cleared page
table entries I did not observe: no other BO was alive in that test.

So the wrapper frees the object the way v3d_bo_create() does when
v3d_bo_create_finish() fails. With import_attach set,
drm_gem_shmem_free() goes through drm_prime_gem_destroy(): the sg_table
is unmapped once, the attachment is detached and the reference on the
dma-buf is dropped.

Would you rather have drm_gem_object_put() there, with v3d_free_object()
checking drm_mm_node_allocated()?

One side effect of moving the allocation: 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. It is taken interruptibly, so an
import can return -EINTR if a signal arrives while it waits. libdrm's
drmPrimeFDToHandle() retries on EINTR.

Tested on a Raspberry Pi 5, v7.4-rc2 with slub_debug=FZPU:

- the import that exhausts the V3D address space returns -ENOSPC in
  three consecutive fills, on two boots;
- after 50 such failures /sys/kernel/debug/dma_buf/bufinfo lists no
  dma-buf (15 udmabufs attached to v3d while the test holds them);
- a v3d BO re-imported through a second file is the same BO at the same
  offset over 1000 iterations, and bo_stats returns to its starting
  value;
- a vc4 dumb buffer imported into v3d can be mapped, written, read back
  and closed;
- under labwc, glmark2-es2-wayland runs, and mpv playing 4K HEVC from
  the hardware decoder (--vo=gpu and --vo=dmabuf-wayland, --hwdec=drm)
  imports the decoder's dma-heap buffers into v3d about 2500 times;
  none of them is left attached afterwards. The median time of an
  import and the number of dropped frames are the same as with the
  unpatched module, with and without slub_debug.

The kernel log stays clean in all of them. Not tested on other V3D
generations.

Best regards,
Aleksei

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-19 14:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-14 16:44 [PATCH] drm/v3d: Fix double free of the sg_table when a PRIME import fails Aleksei Riapolov
2026-11-18 12:54 ` Maíra Canal
2026-11-19 14:45   ` 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®