Skip to contentSkip to conversation

drm/gem: Internally test import_attach for imported objects

PATCH In review Thomas Zimmermann 3 participants 6 messages Apr 16, 2025 – May 6, 2025

Subsystems (3)
lore
Share & tools
lore

Thomas Zimmermann

author maintainer
#0 lore raw

PATCH v3 1/1 drm/gem: Internally test import_attach for imported objects

Test struct drm_gem_object.import_attach to detect imported objects.

During object clenanup, the dma_buf field might be NULL. Testing it in an object's free callback then incorrectly does a cleanup as for native objects. Happens for calls to drm_mode_destroy_dumb_ioctl() that clears the dma_buf field in drm_gem_object_exported_dma_buf_free().

v3:
- only test for import_attach (Boris)
v2:
- use import_attach.dmabuf instead of dma_buf (Christian)

Fixes b57aa47d39e9 ("drm/gem: Test for imported GEM buffers with helper")
Cc Thomas Zimmermann <[email protected]>
Cc Anusha Srivatsa <[email protected]>
Cc Christian König <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Sumit Semwal <[email protected]>
Cc "Christian König" <[email protected]>
---
 include/drm/drm_gem.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
include/drm/drm_gem.h +1 -2
index 9b71f7a9f3f8..a3133a08267c 100644
@@ -588,8 +588,7 @@ static inline bool drm_gem_object_is_shared_for_memory_stats(struct drm_gem_obje
  */
 static inline bool drm_gem_is_imported(const struct drm_gem_object *obj)
 {
-	/* The dma-buf's priv field points to the original GEM object. */
-	return obj->dma_buf && (obj->dma_buf->priv != obj);
+	return !!obj->import_attach;
 }
 
 #ifdef CONFIG_LOCKDEP
Signature · 1 line
2.49.0
Reviewed-by on v3 1/1
#1 lore raw
Quoted 4 lines · On Wed, 16 Apr 2025 08:57:45 +0200 Thomas Zimmermann <[email protected]>…

Test struct drm_gem_object.import_attach to detect imported objects.

During object clenanup, the dma_buf field might be NULL. Testing it in an object's free callback then incorrectly does a cleanup as for native

Just pointing it for completeness: this happens even outside the free callback, because the GEM handle lifetime (time userspace holds this GEM object) might differ from the GEM lifetime itself (KMD might have refs to the GEM after the handle is released).

Quoted 2 lines

objects. Happens for calls to drm_mode_destroy_dumb_ioctl() that clears the dma_buf field in drm_gem_object_exported_dma_buf_free().

Also happens when the GEM handle is released, which is the case we were hitting in Panthor.

Quoted 3 lines

v3:
- only test for import_attach (Boris)

Quoted 38 lines

v2:
- use import_attach.dmabuf instead of dma_buf (Christian)

Fixes b57aa47d39e9 ("drm/gem: Test for imported GEM buffers with helper")
Cc Thomas Zimmermann <[email protected]>
Cc Anusha Srivatsa <[email protected]>
Cc Christian König <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Sumit Semwal <[email protected]>
Cc "Christian König" <[email protected]>
---
 include/drm/drm_gem.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
include/drm/drm_gem.h +1 -2
index 9b71f7a9f3f8..a3133a08267c 100644
@@ -588,8 +588,7 @@ static inline bool drm_gem_object_is_shared_for_memory_stats(struct drm_gem_obje
  */
 static inline bool drm_gem_is_imported(const struct drm_gem_object *obj)
 {
-	/* The dma-buf's priv field points to the original GEM object. */
-	return obj->dma_buf && (obj->dma_buf->priv != obj);
+	return !!obj->import_attach;
 }
 
 #ifdef CONFIG_LOCKDEP
Reviewed-by on v3 1/1
#2 lore raw
In reply to Thomas Zimmermann
Quoted 21 lines · Thomas Zimmermann

Test struct drm_gem_object.import_attach to detect imported objects.

During object clenanup, the dma_buf field might be NULL. Testing it in an object's free callback then incorrectly does a cleanup as for native objects. Happens for calls to drm_mode_destroy_dumb_ioctl() that clears the dma_buf field in drm_gem_object_exported_dma_buf_free().

v3:
- only test for import_attach (Boris)
v2:
- use import_attach.dmabuf instead of dma_buf (Christian)

Fixes b57aa47d39e9 ("drm/gem: Test for imported GEM buffers with helper")
Cc Thomas Zimmermann <[email protected]>
Cc Anusha Srivatsa <[email protected]>
Cc Christian König <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Quoted 8 lines
Cc Maxime Ripard <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Sumit Semwal <[email protected]>
Cc "Christian König" <[email protected]>
Quoted 21 lines
---
 include/drm/drm_gem.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
include/drm/drm_gem.h +1 -2
index 9b71f7a9f3f8..a3133a08267c 100644
@@ -588,8 +588,7 @@ static inline bool drm_gem_object_is_shared_for_memory_stats(struct drm_gem_obje
  */
 static inline bool drm_gem_is_imported(const struct drm_gem_object *obj)
 {
-	/* The dma-buf's priv field points to the original GEM object. */
-	return obj->dma_buf && (obj->dma_buf->priv != obj);
+	return !!obj->import_attach;
 }
 
 #ifdef CONFIG_LOCKDEP
Signature · 1 line
2.49.0
Signature · 3 lines
Simona Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#3 lore raw
Quoted 31 lines · Simona Vetter
Quoted 21 lines · Thomas Zimmermann

Test struct drm_gem_object.import_attach to detect imported objects.

During object clenanup, the dma_buf field might be NULL. Testing it in an object's free callback then incorrectly does a cleanup as for native objects. Happens for calls to drm_mode_destroy_dumb_ioctl() that clears the dma_buf field in drm_gem_object_exported_dma_buf_free().

v3:
- only test for import_attach (Boris)
v2:
- use import_attach.dmabuf instead of dma_buf (Christian)

Fixes b57aa47d39e9 ("drm/gem: Test for imported GEM buffers with helper")
Cc Thomas Zimmermann <[email protected]>
Cc Anusha Srivatsa <[email protected]>
Cc Christian König <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Quoted 8 lines
Cc Maxime Ripard <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Sumit Semwal <[email protected]>
Cc "Christian König" <[email protected]>
Quoted 1 line

Also quick doc request: We do have a bit of overview documentation for prime here about specifically this lifetime fun, and why there's a chain of references and hence a distinction between imported foreign dma-buf and re-imported native dma-buf:

https://dri.freedesktop.org/docs/drm/gpu/drm-mm.html#reference-counting-for-gem-drivers

I think it would be good to augment this with more links to functions (like this one recently added and fixed in this patch here) and struct members to that overview. And maybe also link from key function and struct functions back to that overview doc. Otherwise I think the next person will get confused by this rather tricky code again and break a corner cases.

Thanks, Sima

Quoted 27 lines
Quoted 22 lines
---
 include/drm/drm_gem.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
include/drm/drm_gem.h +1 -2
index 9b71f7a9f3f8..a3133a08267c 100644
@@ -588,8 +588,7 @@ static inline bool drm_gem_object_is_shared_for_memory_stats(struct drm_gem_obje
  */
 static inline bool drm_gem_is_imported(const struct drm_gem_object *obj)
 {
-	/* The dma-buf's priv field points to the original GEM object. */
-	return obj->dma_buf && (obj->dma_buf->priv != obj);
+	return !!obj->import_attach;
 }
 
 #ifdef CONFIG_LOCKDEP
Signature · 1 line
2.49.0
Signature · 3 lines
Simona Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
Signature · 3 lines
Simona Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#4 lore raw
Quoted 39 lines · On Wed, 30 Apr 2025 11:07:17 +0200 Simona Vetter <[email protected]> wrote:
Quoted 31 lines · Simona Vetter
Quoted 21 lines · Thomas Zimmermann

Test struct drm_gem_object.import_attach to detect imported objects.

During object clenanup, the dma_buf field might be NULL. Testing it in an object's free callback then incorrectly does a cleanup as for native objects. Happens for calls to drm_mode_destroy_dumb_ioctl() that clears the dma_buf field in drm_gem_object_exported_dma_buf_free().

v3:
- only test for import_attach (Boris)
v2:
- use import_attach.dmabuf instead of dma_buf (Christian)

Fixes b57aa47d39e9 ("drm/gem: Test for imported GEM buffers with helper")
Cc Thomas Zimmermann <[email protected]>
Cc Anusha Srivatsa <[email protected]>
Cc Christian König <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Quoted 8 lines
Cc Maxime Ripard <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Sumit Semwal <[email protected]>
Cc "Christian König" <[email protected]>
Quoted 1 line

Also quick doc request: We do have a bit of overview documentation for prime here about specifically this lifetime fun, and why there's a chain of references and hence a distinction between imported foreign dma-buf and re-imported native dma-buf:

Quoted 8 lines

https://dri.freedesktop.org/docs/drm/gpu/drm-mm.html#reference-counting-for-gem-drivers

I think it would be good to augment this with more links to functions (like this one recently added and fixed in this patch here) and struct members to that overview. And maybe also link from key function and struct functions back to that overview doc. Otherwise I think the next person will get confused by this rather tricky code again and break a corner cases.

BTW, could we also backmerge 6.15-rc5 into drm-misc-next so the fix is also present in drm-misc-next?

author maintainer
#5 lore raw

Hi

Am 05.05.25 um 09:29 schrieb Boris Brezillon:

Quoted 42 lines
Quoted 39 lines · On Wed, 30 Apr 2025 11:07:17 +0200 Simona Vetter <[email protected]> wrote:
Quoted 30 lines · Simona Vetter
Quoted 21 lines · Thomas Zimmermann

Test struct drm_gem_object.import_attach to detect imported objects.

During object clenanup, the dma_buf field might be NULL. Testing it in an object's free callback then incorrectly does a cleanup as for native objects. Happens for calls to drm_mode_destroy_dumb_ioctl() that clears the dma_buf field in drm_gem_object_exported_dma_buf_free().

v3:
- only test for import_attach (Boris)
v2:
- use import_attach.dmabuf instead of dma_buf (Christian)

Fixes b57aa47d39e9 ("drm/gem: Test for imported GEM buffers with helper")
Cc Thomas Zimmermann <[email protected]>
Cc Anusha Srivatsa <[email protected]>
Cc Christian König <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Quoted 8 lines
Cc Maxime Ripard <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Sumit Semwal <[email protected]>
Cc "Christian König" <[email protected]>
Quoted 1 line

Also quick doc request: We do have a bit of overview documentation for prime here about specifically this lifetime fun, and why there's a chain of references and hence a distinction between imported foreign dma-buf and re-imported native dma-buf:

https://dri.freedesktop.org/docs/drm/gpu/drm-mm.html#reference-counting-for-gem-drivers

Quoted 8 lines
Quoted 6 lines

I think it would be good to augment this with more links to functions (like this one recently added and fixed in this patch here) and struct members to that overview. And maybe also link from key function and struct functions back to that overview doc. Otherwise I think the next person will get confused by this rather tricky code again and break a corner cases.

BTW, could we also backmerge 6.15-rc5 into drm-misc-next so the fix is also present in drm-misc-next?

drm-misc-next is now at -rc5

Best regards
Thomas

Signature · 7 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)