Skip to contentSkip to conversation

DRM GEM fixes

PATCH Superseded Dmitry Osipenko 3 participants 8 messages Jun 30, 2022 – Aug 9, 2022

Subsystems (4)

v7 of 19 Superseded Latest is v19 →

lore
Share & tools
lore
#0 lore raw

v7 cover DRM GEM fixes

This patchset fixes two problems in the common GEM code. First fixed problem is the bogus lockdep splat that complicates debugging of DRM drivers. Second problem is the inconsistency in behaviour and improper handling of mapping the imported GEMs by some drivers, to fix it we will prohibit to map the imported GEMs like majority of drivers already do.

Changelog:

v7: - Factored out GEM patches from [1] since I'll be working on the
dma-buf locking in a separate patchsets now.

[1] https://lore.kernel.org/all/[email protected]/

- Improved commit message and added fixes tag to the "Properly annotate WW context" patch.

- Replaced "Move mapping of imported dma-bufs to drm_gem_mmap_obj()" patch with "Don't map imported GEMs", like was suggested by Thomas Hellström.

- Added r-b and suggested-by from Thomas Hellström.

Dmitry Osipenko (2):
drm/gem: Properly annotate WW context on drm_gem_lock_reservations()
error
drm/gem: Don't map imported GEMs

 drivers/gpu/drm/drm_gem.c              | 8 ++++++--
 drivers/gpu/drm/drm_gem_shmem_helper.c | 9 ---------
 2 files changed, 6 insertions(+), 11 deletions(-)
Signature · 1 line
2.36.1
#1 lore raw

PATCH v7 1/2 drm/gem: Properly annotate WW context on drm_gem_lock_reservations() error

Use ww_acquire_fini() in the error code paths. Otherwise lockdep thinks that lock is held when lock's memory is freed after the drm_gem_lock_reservations() error. The ww_acquire_context needs to be annotated as "released", which fixes the noisy "WARNING: held lock freed!" splat of VirtIO-GPU driver with CONFIG_DEBUG_MUTEXES=y and enabled lockdep.

Fixes 7edc3e3b975b5 ("drm: Add helpers for locking an array of BO reservations.")
---
 drivers/gpu/drm/drm_gem.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_gem.c +2 -2
index eb0c2d041f13..86d670c71286 100644
@@ -1226,7 +1226,7 @@ drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
 		ret = dma_resv_lock_slow_interruptible(obj->resv,
 								 acquire_ctx);
 		if (ret) {
-			ww_acquire_done(acquire_ctx);
+			ww_acquire_fini(acquire_ctx);
 			return ret;
 		}
 	}
@@ -1251,7 +1251,7 @@ drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
 				goto retry;
 			}
 
-			ww_acquire_done(acquire_ctx);
+			ww_acquire_fini(acquire_ctx);
 			return ret;
 		}
 	}
Signature · 1 line
2.36.1
#2 lore raw

Re: [PATCH v7 1/2] drm/gem: Properly annotate WW context on drm_gem_lock_reservations() error

Quoted 10 lines · Dmitry Osipenko

Use ww_acquire_fini() in the error code paths. Otherwise lockdep thinks that lock is held when lock's memory is freed after the drm_gem_lock_reservations() error. The ww_acquire_context needs to be annotated as "released", which fixes the noisy "WARNING: held lock freed!" splat of VirtIO-GPU driver with CONFIG_DEBUG_MUTEXES=y and enabled lockdep.

Fixes 7edc3e3b975b5 ("drm: Add helpers for locking an array of BO reservations.")

I merged this one to drm-misc-next-fixes. The other one looks like there's still opens pending, pls resubmit appropriately (and maybe with some analysis in the commit message of how exactly this impacts other drivers). -Daniel

Quoted 29 lines
---
 drivers/gpu/drm/drm_gem.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_gem.c +2 -2
index eb0c2d041f13..86d670c71286 100644
@@ -1226,7 +1226,7 @@ drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
 		ret = dma_resv_lock_slow_interruptible(obj->resv,
 								 acquire_ctx);
 		if (ret) {
-			ww_acquire_done(acquire_ctx);
+			ww_acquire_fini(acquire_ctx);
 			return ret;
 		}
 	}
@@ -1251,7 +1251,7 @@ drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
 				goto retry;
 			}
 
-			ww_acquire_done(acquire_ctx);
+			ww_acquire_fini(acquire_ctx);
 			return ret;
 		}
 	}
Signature · 1 line
2.36.1
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#3 lore raw

PATCH v7 2/2 drm/gem: Don't map imported GEMs

In reply to Dmitry Osipenko

Drivers that use drm_gem_mmap() and drm_gem_mmap_obj() helpers don't handle imported dma-bufs properly, which results in mapping of something else than the imported dma-buf. On NVIDIA Tegra we get a hard lockup when userspace writes to the memory mapping of a dma-buf that was imported into Tegra's DRM GEM.

Majority of DRM drivers prohibit mapping of the imported GEM objects. Mapping of imported GEMs require special care from userspace since it should sync dma-buf because mapping coherency of the exporter device may not match the DRM device. Let's prohibit the mapping for all DRM drivers for consistency.

---
 drivers/gpu/drm/drm_gem.c              | 4 ++++
 drivers/gpu/drm/drm_gem_shmem_helper.c | 9 ---------
 2 files changed, 4 insertions(+), 9 deletions(-)
drivers/gpu/drm/drm_gem.c +4 -0
index 86d670c71286..fc9ec42fa0ab 100644
@@ -1034,6 +1034,10 @@ int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
 {
 	int ret;
 
+	/* Don't allow imported objects to be mapped */
+	if (obj->import_attach)
+		return -EINVAL;
+
 	/* Check for valid size. */
 	if (obj_size < vma->vm_end - vma->vm_start)
 		return -EINVAL;
drivers/gpu/drm/drm_gem_shmem_helper.c +0 -9
index 8ad0e02991ca..6190f5018986 100644
@@ -609,17 +609,8 @@ EXPORT_SYMBOL_GPL(drm_gem_shmem_vm_ops);
  */
 int drm_gem_shmem_mmap(struct drm_gem_shmem_object *shmem, struct vm_area_struct *vma)
 {
-	struct drm_gem_object *obj = &shmem->base;
 	int ret;
 
-	if (obj->import_attach) {
-		/* Drop the reference drm_gem_mmap_obj() acquired.*/
-		drm_gem_object_put(obj);
-		vma->vm_private_data = NULL;
-
-		return dma_buf_mmap(obj->dma_buf, vma, 0);
-	}
-
 	ret = drm_gem_shmem_get_pages(shmem);
 	if (ret) {
 		drm_gem_vm_close(vma);
Signature · 1 line
2.36.1
#4 lore raw

Re: [PATCH v7 2/2] drm/gem: Don't map imported GEMs

Hi, Dmitry,

Quoted 12 lines · Dmitry Osipenko

Drivers that use drm_gem_mmap() and drm_gem_mmap_obj() helpers don't handle imported dma-bufs properly, which results in mapping of something else than the imported dma-buf. On NVIDIA Tegra we get a hard lockup when userspace writes to the memory mapping of a dma-buf that was imported into Tegra's DRM GEM.

Majority of DRM drivers prohibit mapping of the imported GEM objects. Mapping of imported GEMs require special care from userspace since it should sync dma-buf because mapping coherency of the exporter device may not match the DRM device. Let's prohibit the mapping for all DRM drivers for consistency.

Quoted 3 lines

This might break drivers whose obj->funcs->mmap() callback already handles this case, and has userspace that does the right thing.

I think the disabling must be checked on a per-driver basis to avoid that; in particular drivers that already call dma_buf_mmap() should be able to continue doing this.

Also the Fixes: review comment remains,

thanks,

Thomas

Quoted 42 lines
---
  drivers/gpu/drm/drm_gem.c              | 4 ++++
  drivers/gpu/drm/drm_gem_shmem_helper.c | 9 ---------
  2 files changed, 4 insertions(+), 9 deletions(-)
drivers/gpu/drm/drm_gem.c +4 -0
index 86d670c71286..fc9ec42fa0ab 100644
@@ -1034,6 +1034,10 @@ int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
  {
  	int ret;
 
+	/* Don't allow imported objects to be mapped */
+	if (obj->import_attach)
+		return -EINVAL;
+
  	/* Check for valid size. */
  	if (obj_size < vma->vm_end - vma->vm_start)
  		return -EINVAL;
drivers/gpu/drm/drm_gem_shmem_helper.c +0 -9
index 8ad0e02991ca..6190f5018986 100644
@@ -609,17 +609,8 @@ EXPORT_SYMBOL_GPL(drm_gem_shmem_vm_ops);
   */
  int drm_gem_shmem_mmap(struct drm_gem_shmem_object *shmem, struct vm_area_struct *vma)
  {
-	struct drm_gem_object *obj = &shmem->base;
  	int ret;
 
-	if (obj->import_attach) {
-		/* Drop the reference drm_gem_mmap_obj() acquired.*/
-		drm_gem_object_put(obj);
-		vma->vm_private_data = NULL;
-
-		return dma_buf_mmap(obj->dma_buf, vma, 0);
-	}
-
  	ret = drm_gem_shmem_get_pages(shmem);
  	if (ret) {
  		drm_gem_vm_close(vma);
#5 lore raw

Re: [PATCH v7 2/2] drm/gem: Don't map imported GEMs

Hello Thomas,

Quoted 16 lines · Thomas Hellström (Intel)

Hi, Dmitry,

Quoted 13 lines · Dmitry Osipenko

Drivers that use drm_gem_mmap() and drm_gem_mmap_obj() helpers don't handle imported dma-bufs properly, which results in mapping of something else than the imported dma-buf. On NVIDIA Tegra we get a hard lockup when userspace writes to the memory mapping of a dma-buf that was imported into
Tegra's DRM GEM.

Majority of DRM drivers prohibit mapping of the imported GEM objects. Mapping of imported GEMs require special care from userspace since it should sync dma-buf because mapping coherency of the exporter device may not match the DRM device. Let's prohibit the mapping for all DRM drivers for consistency.

Quoted 6 lines
Quoted 3 lines

This might break drivers whose obj->funcs->mmap() callback already handles this case, and has userspace that does the right thing.

The drm-shmem helper should be the only that maps imported GEMs properly, but drivers that use drm-shmem already prohibit to map imported GEMs. Okay, I'll try to re-check once again to be sure.

Quoted 5 lines

I think the disabling must be checked on a per-driver basis to avoid that; in particular drivers that already call dma_buf_mmap() should be able to continue doing this.

Also the Fixes: review comment remains,

This should've been broken forever, don't think that we have a fixes tag here. I looked thought all my previous patches and added the Fixes wherever was possible. If I really missed something, then please let me know.

Signature · 2 lines
Best regards,
Dmitry
#6 lore raw

Re: [PATCH v7 2/2] drm/gem: Don't map imported GEMs

Quoted 19 lines · Dmitry Osipenko

Hello Thomas,

Quoted 16 lines · Thomas Hellström (Intel)

Hi, Dmitry,

Quoted 13 lines · Dmitry Osipenko

Drivers that use drm_gem_mmap() and drm_gem_mmap_obj() helpers don't handle imported dma-bufs properly, which results in mapping of something else than the imported dma-buf. On NVIDIA Tegra we get a hard lockup when userspace writes to the memory mapping of a dma-buf that was imported into
Tegra's DRM GEM.

Majority of DRM drivers prohibit mapping of the imported GEM objects. Mapping of imported GEMs require special care from userspace since it should sync dma-buf because mapping coherency of the exporter device may not match the DRM device. Let's prohibit the mapping for all DRM drivers for consistency.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

This might break drivers whose obj->funcs->mmap() callback already handles this case, and has userspace that does the right thing.

The drm-shmem helper should be the only that maps imported GEMs properly, but drivers that use drm-shmem already prohibit to map imported GEMs. Okay, I'll try to re-check once again to be sure.

OK. If you aren't 100.1% sure, then please drop the Cc: stable tag and let the patch ride out at least an -rc series, because breaking a stable kernel is something we woudln't want to do.

Thanks,

Thomas

Quoted 11 lines
Quoted 6 lines

I think the disabling must be checked on a per-driver basis to avoid that; in particular drivers that already call dma_buf_mmap() should be able to continue doing this.

Also the Fixes: review comment remains,

This should've been broken forever, don't think that we have a fixes tag here. I looked thought all my previous patches and added the Fixes wherever was possible. If I really missed something, then please let me know.

#7 lore raw

Re: [PATCH v7 2/2] drm/gem: Don't map imported GEMs

Quoted 30 lines · Thomas Hellström (Intel)
Quoted 28 lines · Dmitry Osipenko

Hello Thomas,

Quoted 25 lines · Thomas Hellström (Intel)

Hi, Dmitry,

Quoted 17 lines · Dmitry Osipenko

Drivers that use drm_gem_mmap() and drm_gem_mmap_obj() helpers don't handle imported dma-bufs properly, which results in mapping of something
else than the imported dma-buf. On NVIDIA Tegra we get a hard lockup when
userspace writes to the memory mapping of a dma-buf that was imported into
Tegra's DRM GEM.

Majority of DRM drivers prohibit mapping of the imported GEM objects. Mapping of imported GEMs require special care from userspace since it should sync dma-buf because mapping coherency of the exporter device may
not match the DRM device. Let's prohibit the mapping for all DRM drivers
for consistency.

Quoted 3 lines

This might break drivers whose obj->funcs->mmap() callback already handles this case, and has userspace that does the right thing.

Quoted 7 lines
Quoted 3 lines

The drm-shmem helper should be the only that maps imported GEMs properly, but drivers that use drm-shmem already prohibit to map imported GEMs. Okay, I'll try to re-check once again to be sure.

OK. If you aren't 100.1% sure, then please drop the Cc: stable tag and let the patch ride out at least an -rc series, because breaking a stable kernel is something we woudln't want to do.

Apparently the OMAP DRM driver should be broken similarly to the Tegra DRM. Unlikely that anyone else maps the imported GEMs in practice, other drivers are prohibiting the mapping AFAICS. I'll make the v8 without the stable tag since it's not a critical problem after all because it never worked for the broken drivers.

Signature · 2 lines
Best regards,
Dmitry