Skip to contentSkip to conversation

drm/v3d: Don't free the core's sg_table when PRIME import fails

PATCH In review Roy 2 participants 2 messages Nov 20

Subsystems (4)
lore
Share & tools
lore

Roy

author
#0 lore raw

PATCH 1/1 drm/v3d: Don't free the core's sg_table when PRIME import fails

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.")
---
 drivers/gpu/drm/v3d/v3d_bo.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/gpu/drm/v3d/v3d_bo.c +7 -0
index db95a2f..d0b672d 100644
@@ -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);
 	}
Signature · 1 line
2.51.1.windows.1
maintainer
#1 lore raw

Hi Roy,

This is a duplicate of [1].

[1] https://lore.kernel.org/dri-devel/[email protected]/

Best regards,
- Maíra

Quoted 70 lines · Roy

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.")
---
  drivers/gpu/drm/v3d/v3d_bo.c | 7 +++++++
  1 file changed, 7 insertions(+)
drivers/gpu/drm/v3d/v3d_bo.c +7 -0
index db95a2f..d0b672d 100644
@@ -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);
  	}