Skip to contentSkip to conversation

Fixes for dma-buf locking issues found by Smatch

PATCH Superseded Dmitry Osipenko 3 participants 8 messages Oct 26, 2022 – Oct 27, 2022

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Fixes for dma-buf locking issues found by Smatch

Hello,

Here are the two patches fixing minor problems introduced by my "dma-buf locking convention" series. Thanks to Dan Carpenter who checked linux-next with Smatch and reported the found issues!

Please review/ack, I'll apply the patches to misc-next afterwards.

Thanks!

Dmitry Osipenko (2):
dma-buf: Make locking consistent in dma_buf_detach() drm/gem: Check whether object is NULL in drm_gem_vunmap()

 drivers/dma-buf/dma-buf.c | 3 ++-
 drivers/gpu/drm/drm_gem.c | 9 ++++++---
 2 files changed, 8 insertions(+), 4 deletions(-)
Signature · 1 line
2.37.3
#1 lore raw

PATCH 1/2 dma-buf: Make locking consistent in dma_buf_detach()

The dma_buf_detach() locks attach->dmabuf->resv and then unlocks dmabuf->resv, which could be a two different locks from a static code checker perspective. In particular this triggers Smatch to report the "double unlock" error. Make the locking pointers consistent.

Fixes 809d9c72c2f8 ("dma-buf: Move dma_buf_attach() to dynamic locking specification")
---
 drivers/dma-buf/dma-buf.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/dma-buf/dma-buf.c +2 -1
index c40d72d318fd..6e33ef4fde34 100644
@@ -998,9 +998,10 @@ void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
 	if (WARN_ON(!dmabuf || !attach))
 		return;
 
-	dma_resv_lock(attach->dmabuf->resv, NULL);
+	dma_resv_lock(dmabuf->resv, NULL);
 
 	if (attach->sgt) {
+		WARN_ON(dmabuf != attach->dmabuf);
 
 		__unmap_dma_buf(attach, attach->sgt, attach->dir);
 
Signature · 1 line
2.37.3
maintainer
#2 lore raw

Re: [PATCH v1 1/2] dma-buf: Make locking consistent in dma_buf_detach()

Am 27.10.22 um 00:46 schrieb Dmitry Osipenko:

Quoted 9 lines

The dma_buf_detach() locks attach->dmabuf->resv and then unlocks dmabuf->resv, which could be a two different locks from a static code checker perspective. In particular this triggers Smatch to report the "double unlock" error. Make the locking pointers consistent.

Fixes 809d9c72c2f8 ("dma-buf: Move dma_buf_attach() to dynamic locking specification")

It would be even cleaner if we completely drop the dmabuf parameter for the function and just use the inside the attachment.

Anyway patch is Reviewed-by: Christian König <[email protected]> for now, wider cleanups can come later on.

Regards,
Christian.

Quoted 20 lines
---
  drivers/dma-buf/dma-buf.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/dma-buf/dma-buf.c +2 -1
index c40d72d318fd..6e33ef4fde34 100644
@@ -998,9 +998,10 @@ void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
  	if (WARN_ON(!dmabuf || !attach))
  		return;
 
-	dma_resv_lock(attach->dmabuf->resv, NULL);
+	dma_resv_lock(dmabuf->resv, NULL);
 
  	if (attach->sgt) {
+		WARN_ON(dmabuf != attach->dmabuf);
 
  		__unmap_dma_buf(attach, attach->sgt, attach->dir);
 
#3 lore raw

Re: [PATCH v1 1/2] dma-buf: Make locking consistent in dma_buf_detach()

Quoted 12 lines · Christian König

Am 27.10.22 um 00:46 schrieb Dmitry Osipenko:

Quoted 10 lines

The dma_buf_detach() locks attach->dmabuf->resv and then unlocks dmabuf->resv, which could be a two different locks from a static code checker perspective. In particular this triggers Smatch to report the "double unlock" error. Make the locking pointers consistent.

Fixes 809d9c72c2f8 ("dma-buf: Move dma_buf_attach() to dynamic

locking specification")

Quoted 5 lines

It would be even cleaner if we completely drop the dmabuf parameter for the function and just use the inside the attachment.

Anyway patch is Reviewed-by: Christian König <[email protected]> for now, wider cleanups can come later on.

I had the same thought about dropping the dmabuf parameter.

Looking at this patch again, perhaps a better dmabuf sanity-check will be:

- 	if (WARN_ON(!dmabuf || !attach))
+ 	if (WARN_ON(!dmabuf || !attach || dmabuf != attach->dmabuf))

I'll switch to this version in v2, if there are no objections.

Quoted 20 lines
Quoted 19 lines

---
  drivers/dma-buf/dma-buf.c | 3 ++-

  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/dma-buf/dma-buf.c +2 -0
index c40d72d318fd..6e33ef4fde34 100644
@@ -998,9 +998,10 @@ void dma_buf_detach(struct dma_buf *dmabuf,
 struct dma_buf_attachment *attach)
       if (WARN_ON(!dmabuf || !attach))
           return;
   -    dma_resv_lock(attach->dmabuf->resv, NULL);
+    dma_resv_lock(dmabuf->resv, NULL);
         if (attach->sgt) {
+        WARN_ON(dmabuf != attach->dmabuf);
             __unmap_dma_buf(attach, attach->sgt, attach->dir);

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

PATCH 2/2 drm/gem: Check whether object is NULL in drm_gem_vunmap()

In reply to Dmitry Osipenko

The drm_gem_vunmap() will crash with a NULL dereference if the passed object pointer is NULL. It wasn't a problem before we added the locking support to drm_gem_vunmap function because the mapping argument was always NULL together with the object. Make drm_gem_vunmap() functions to handle the NULL pointers better.

Fixes 79e2cf2e7a19 ("drm/gem: Take reservation lock for vmap/vunmap operations")
---
 drivers/gpu/drm/drm_gem.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_gem.c +6 -3
index b8db675e7fb5..ee0a246ff4ac 100644
@@ -1175,11 +1175,11 @@ EXPORT_SYMBOL(drm_gem_vmap);
 
 void drm_gem_vunmap(struct drm_gem_object *obj, struct iosys_map *map)
 {
-	dma_resv_assert_held(obj->resv);
-
-	if (iosys_map_is_null(map))
+	if (!obj || iosys_map_is_null(map))
 		return;
 
+	dma_resv_assert_held(obj->resv);
+
 	if (obj->funcs->vunmap)
 		obj->funcs->vunmap(obj, map);
 
@@ -1202,6 +1202,9 @@ EXPORT_SYMBOL(drm_gem_vmap_unlocked);
 
 void drm_gem_vunmap_unlocked(struct drm_gem_object *obj, struct iosys_map *map)
 {
+	if (!obj || iosys_map_is_null(map))
+		return;
+
 	dma_resv_lock(obj->resv, NULL);
 	drm_gem_vunmap(obj, map);
 	dma_resv_unlock(obj->resv);
Signature · 1 line
2.37.3
maintainer
#5 lore raw

Re: [PATCH v1 2/2] drm/gem: Check whether object is NULL in drm_gem_vunmap()

Am 27.10.22 um 00:46 schrieb Dmitry Osipenko:

Quoted 19 lines

The drm_gem_vunmap() will crash with a NULL dereference if the passed object pointer is NULL. It wasn't a problem before we added the locking support to drm_gem_vunmap function because the mapping argument was always NULL together with the object. Make drm_gem_vunmap() functions to handle the NULL pointers better.

Fixes 79e2cf2e7a19 ("drm/gem: Take reservation lock for vmap/vunmap operations")
---
  drivers/gpu/drm/drm_gem.c | 9 ++++++---
  1 file changed, 6 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_gem.c +0 -0
index b8db675e7fb5..ee0a246ff4ac 100644

drivers/gpu/drm/drm_gem.c EXPORT_SYMBOL(drm_gem_vmap); · line 1179

Quoted 8 lines
drivers/gpu/drm/drm_gem.c +1 -3
@@ -1175,11 +1175,11 @@ EXPORT_SYMBOL(drm_gem_vmap);
 
  void drm_gem_vunmap(struct drm_gem_object *obj, struct iosys_map *map)
  {
-	dma_resv_assert_held(obj->resv);
-
-	if (iosys_map_is_null(map))
+	if (!obj || iosys_map_is_null(map))
  		return;

I'm not very keen about that. Calling a function with all parameters NULL doesn't make much sense and is clearly a coding error. Hiding that somehow doesn't help but rather makes things worse.

The only execption to that are things like kfree() or *_put() which work with the lifetime of objects.

Why is the static checker complaining about that in the first place?

Regards,
Christian.

Quoted 16 lines
drivers/gpu/drm/drm_gem.c +5 -0
+	dma_resv_assert_held(obj->resv);
+
  	if (obj->funcs->vunmap)
  		obj->funcs->vunmap(obj, map);
 
@@ -1202,6 +1202,9 @@ EXPORT_SYMBOL(drm_gem_vmap_unlocked);
 
  void drm_gem_vunmap_unlocked(struct drm_gem_object *obj, struct iosys_map *map)
  {
+	if (!obj || iosys_map_is_null(map))
+		return;
+
  	dma_resv_lock(obj->resv, NULL);
  	drm_gem_vunmap(obj, map);
  	dma_resv_unlock(obj->resv);
#6 lore raw

Re: [PATCH v1 2/2] drm/gem: Check whether object is NULL in drm_gem_vunmap()

Quoted 35 lines · Christian König

Am 27.10.22 um 00:46 schrieb Dmitry Osipenko:

Quoted 20 lines

The drm_gem_vunmap() will crash with a NULL dereference if the passed object pointer is NULL. It wasn't a problem before we added the locking support to drm_gem_vunmap function because the mapping argument was always NULL together with the object. Make drm_gem_vunmap() functions to handle the NULL pointers better.

79e2cf2e7a19 ("drm/gem: Take reservation lock for vmap/vunmap operations")

---
  drivers/gpu/drm/drm_gem.c | 9 ++++++---
  1 file changed, 6 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_gem.c +0 -0
index b8db675e7fb5..ee0a246ff4ac 100644

drivers/gpu/drm/drm_gem.c EXPORT_SYMBOL(drm_gem_vmap); · line 1178

Quoted 7 lines
drivers/gpu/drm/drm_gem.c +1 -3
@@ -1175,11 +1175,11 @@ EXPORT_SYMBOL(drm_gem_vmap);
  void drm_gem_vunmap(struct drm_gem_object *obj, struct iosys_map *map)
  {
-	dma_resv_assert_held(obj->resv);
-
-	if (iosys_map_is_null(map))
+	if (!obj || iosys_map_is_null(map))
  		return;

I'm not very keen about that. Calling a function with all parameters NULL doesn't make much sense and is clearly a coding error. Hiding that somehow doesn't help but rather makes things worse.

Quoted 5 lines

The only execption to that are things like kfree() or *_put() which work with the lifetime of objects.

Why is the static checker complaining about that in the first place?

drivers/gpu/drm/drm_client.c:240 drm_client_buffer_delete()
warn: variable dereferenced before check 'buffer->gem' (see line 238)

regards,
dan carpenter

maintainer
#7 lore raw

Re: [PATCH v1 2/2] drm/gem: Check whether object is NULL in drm_gem_vunmap()

Am 27.10.22 um 09:28 schrieb Dan Carpenter:

Quoted 34 lines · Christian König
Quoted 33 lines

Am 27.10.22 um 00:46 schrieb Dmitry Osipenko:

Quoted 20 lines

The drm_gem_vunmap() will crash with a NULL dereference if the passed object pointer is NULL. It wasn't a problem before we added the locking support to drm_gem_vunmap function because the mapping argument was always NULL together with the object. Make drm_gem_vunmap() functions to handle the NULL pointers better.

79e2cf2e7a19 ("drm/gem: Take reservation lock for vmap/vunmap operations")

---
   drivers/gpu/drm/drm_gem.c | 9 ++++++---
   1 file changed, 6 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_gem.c +0 -0
index b8db675e7fb5..ee0a246ff4ac 100644

drivers/gpu/drm/drm_gem.c EXPORT_SYMBOL(drm_gem_vmap); · line 1178

Quoted 7 lines
drivers/gpu/drm/drm_gem.c +1 -3
@@ -1175,11 +1175,11 @@ EXPORT_SYMBOL(drm_gem_vmap);
   void drm_gem_vunmap(struct drm_gem_object *obj, struct iosys_map *map)
   {
-	dma_resv_assert_held(obj->resv);
-
-	if (iosys_map_is_null(map))
+	if (!obj || iosys_map_is_null(map))
   		return;

I'm not very keen about that. Calling a function with all parameters NULL doesn't make much sense and is clearly a coding error. Hiding that somehow doesn't help but rather makes things worse.

Quoted 7 lines
Quoted 5 lines

The only execption to that are things like kfree() or *_put() which work with the lifetime of objects.

Why is the static checker complaining about that in the first place?

drivers/gpu/drm/drm_client.c:240 drm_client_buffer_delete()
warn: variable dereferenced before check 'buffer->gem' (see line 238)

Well than rather modify drm_client_buffer_delete() instead.

Calling drm_gem_vunmap() with obj NULL is certainly not a good idea if we even check that for drm_gem_object_put().

Interesting that the static checkers complain about that :)

Regards,
Christian.

Quoted 4 lines

regards,
dan carpenter