Re: [PATCH] drm/v3d: Fix double free of the sg_table when a PRIME import fails

From: Maíra Canal

Date: Wed Nov 18 2026 - 07:54:28 EST


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@xxxxxxxxxxxxxxx
Signed-off-by: Aleksei Riapolov <tsbol@xxxxxxxxxx>
---
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);
}