From: Roy <rongh7426@gmail.com>
To: mwen@igalia.com, mcanal@igalia.com
Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org,
Roy <rongh7426@gmail.com>,
stable@vger.kernel.org
Subject: [PATCH] drm/v3d: Don't free the core's sg_table when PRIME import fails
Date: Fri, 20 Nov 2026 19:58:40 +0800 [thread overview]
Message-ID: <20261120115840.417741-1-rongh7426@gmail.com> (raw)
When v3d_bo_create_finish() fails in v3d_prime_import_sg_table(), for
example with -ENOSPC because the V3D address space is full, the object
is freed with drm_gem_shmem_free(). At that point obj->import_attach is
not set yet, since drm_gem_prime_import_dev() only sets it after the
driver callback returns. drm_gem_shmem_release() therefore takes the
non-imported path and treats shmem->sgt as its own: it unmaps it with
the V3D device, calls sg_free_table() and kfree()s it.
That sg_table belongs to drm_gem_prime_import_dev(), which then unmaps
it again through dma_buf_unmap_attachment_unlocked() on its fail_unmap
path. The exporter's unmap runs on the freed table, and the kernel
crashes:
Unable to handle kernel NULL pointer dereference at virtual address 000000000000001c
pc : dma_direct_unmap_sg+0x84/0x260
lr : dma_unmap_sg_attrs+0x60/0x148
Call trace:
dma_direct_unmap_sg+0x84/0x260 (P)
dma_unmap_sg_attrs+0x60/0x148
drm_gem_unmap_dma_buf+0x38/0x68 [drm]
dma_buf_unmap_attachment+0x3c/0xa0
dma_buf_unmap_attachment_unlocked+0x58/0x98
drm_gem_prime_import_dev+0x1a0/0x1e0 [drm]
drm_gem_prime_fd_to_handle+0x1a8/0x280 [drm]
drm_prime_fd_to_handle_ioctl+0x40/0x58 [drm]
Users have hit this on Raspberry Pi 5 with Xorg importing vc4 buffers
into v3d.
Clear shmem->sgt before freeing the object, so that only the PRIME core
unmaps it.
Tested on a Raspberry Pi 5 running 6.18.39 with a small program that
exports a vc4 dumb buffer, fills the V3D address space with BOs until
creation fails with -ENOSPC, then imports the dma-buf into v3d. Without
this patch the import oopses as above. With it, the import fails
cleanly with -ENOSPC (60 runs), BO stats stay at zero, and imports
succeed again once BOs are freed. The fix was tested as an out-of-tree
v3d module built against 6.18.39; this tree was not built for arm64.
The bug was found and the fix written with the help of an AI coding
assistant (Claude); I reproduced and tested it on hardware and reviewed
the change.
Fixes: 40609d4820b2 ("drm/v3d: Use the new shmem helpers to reduce driver boilerplate.")
Cc: stable@vger.kernel.org
Link: https://github.com/raspberrypi/linux/issues/7504
Signed-off-by: Roy <rongh7426@gmail.com>
---
drivers/gpu/drm/v3d/v3d_bo.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/drivers/gpu/drm/v3d/v3d_bo.c b/drivers/gpu/drm/v3d/v3d_bo.c
index db95a2f..d0b672d 100644
--- a/drivers/gpu/drm/v3d/v3d_bo.c
+++ b/drivers/gpu/drm/v3d/v3d_bo.c
@@ -185,6 +185,13 @@ v3d_prime_import_sg_table(struct drm_device *dev,
ret = v3d_bo_create_finish(obj);
if (ret) {
+ /*
+ * The sg_table still belongs to drm_gem_prime_import_dev(),
+ * which unmaps it when we return an error. import_attach is
+ * not set yet, so drm_gem_shmem_free() would otherwise unmap
+ * and free it as if it were ours.
+ */
+ to_v3d_bo(obj)->base.sgt = NULL;
drm_gem_shmem_free(&to_v3d_bo(obj)->base);
return ERR_PTR(ret);
}
--
2.51.1.windows.1
next reply other threads:[~2026-11-20 11:59 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 11:58 Roy [this message]
2026-11-20 13:21 ` [PATCH] drm/v3d: Don't free the core's sg_table when PRIME import fails Maíra Canal
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=20261120115840.417741-1-rongh7426@gmail.com \
--to=rongh7426@gmail.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mcanal@igalia.com \
--cc=mwen@igalia.com \
--cc=stable@vger.kernel.org \
/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.