Skip to contentSkip to conversation

drm: Fix dma_resv deadlock at drm object pin time

PATCH Applied Adrián Larumbe 3 participants 9 messages May 23, 2024 – May 29, 2024

Subsystems (5)
lore
Share & tools
lore

Adrián Larumbe

author maintainer
#0 lore raw

v4 cover drm: Fix dma_resv deadlock at drm object pin time

This is v4 of https://lore.kernel.org/lkml/[email protected]/T/

The goal of this patch series is fixing a deadlock upon locking the dma reservation of a DRM gem object when pinning it, at a prime import operation.

Changelog:
v3:
- Split driver fixes into separate commits for Panfrost and Lima
- Make drivers call drm_gem_shmem_pin_locked instead of drm_gem_shmem_object_pin
- Improved commit message for first patch to explain why dma resv locking in the pin callback is no longer necessary.
v2:
- Removed comment explaining reason why an already-locked
pin function replaced the locked variant inside Panfrost's
object pin callback.
- Moved already-assigned attachment warning into generic
already-locked gem object pin function

Adrián Larumbe (3):
drm/panfrost: Fix dma_resv deadlock at drm object pin time drm/lima: Fix dma_resv deadlock at drm object pin time drm/gem-shmem: Add import attachment warning to locked pin function

 drivers/gpu/drm/drm_gem_shmem_helper.c  | 2 ++
 drivers/gpu/drm/lima/lima_gem.c         | 2 +-
 drivers/gpu/drm/panfrost/panfrost_gem.c | 2 +-
 3 files changed, 4 insertions(+), 2 deletions(-)

base-commit: 7acacca1b157fcb258cfd781603425f73bc7370b

Signature · 1 line
2.45.1
author maintainer
#1 lore raw

PATCH v4 1/3 drm/panfrost: Fix dma_resv deadlock at drm object pin time

When Panfrost must pin an object that is being prepared a dma-buf attachment for on behalf of another driver, the core drm gem object pinning code already takes a lock on the object's dma reservation.

However, Panfrost GEM object's pinning callback would eventually try taking the lock on the same dma reservation when delegating pinning of the object onto the shmem subsystem, which led to a deadlock.

This can be shown by enabling CONFIG_DEBUG_WW_MUTEX_SLOWPATH, which throws the following recursive locking situation:

weston/3440 is trying to acquire lock:
ffff000000e235a0 (reservation_ww_class_mutex){+.+.}-{3:3}, at: drm_gem_shmem_pin+0x34/0xb8 [drm_shmem_helper] but task is already holding lock:
ffff000000e235a0 (reservation_ww_class_mutex){+.+.}-{3:3}, at: drm_gem_pin+0x2c/0x80 [drm]

Fix it by replacing drm_gem_shmem_pin with its locked version, as the lock had already been taken by drm_gem_pin().

Cc Thomas Zimmermann <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Boris Brezillon <[email protected]>
Cc Steven Price <[email protected]>
Fixes a78027847226 ("drm/gem: Acquire reservation lock in drm_gem_{pin/unpin}()")
---
 drivers/gpu/drm/panfrost/panfrost_gem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/panfrost/panfrost_gem.c +1 -1
index d47b40b82b0b..8e0ff3efede7 100644
@@ -192,7 +192,7 @@ static int panfrost_gem_pin(struct drm_gem_object *obj)
 	if (bo->is_heap)
 		return -EINVAL;
 
-	return drm_gem_shmem_pin(&bo->base);
+	return drm_gem_shmem_pin_locked(&bo->base);
 }
 
 static enum drm_gem_object_status panfrost_gem_status(struct drm_gem_object *obj)
Signature · 1 line
2.45.1
maintainer Reviewed-by on v4 1/3
#2 lore raw

Re: [PATCH v4 1/3] drm/panfrost: Fix dma_resv deadlock at drm object pin time

Quoted 19 lines · On Thu, 23 May 2024 12:32:17 +0100 Adrián Larumbe…

When Panfrost must pin an object that is being prepared a dma-buf attachment for on behalf of another driver, the core drm gem object pinning code already takes a lock on the object's dma reservation.

However, Panfrost GEM object's pinning callback would eventually try taking the lock on the same dma reservation when delegating pinning of the object onto the shmem subsystem, which led to a deadlock.

This can be shown by enabling CONFIG_DEBUG_WW_MUTEX_SLOWPATH, which throws the following recursive locking situation:

weston/3440 is trying to acquire lock:
ffff000000e235a0 (reservation_ww_class_mutex){+.+.}-{3:3}, at: drm_gem_shmem_pin+0x34/0xb8 [drm_shmem_helper] but task is already holding lock:
ffff000000e235a0 (reservation_ww_class_mutex){+.+.}-{3:3}, at: drm_gem_pin+0x2c/0x80 [drm]

Fix it by replacing drm_gem_shmem_pin with its locked version, as the lock had already been taken by drm_gem_pin().

Quoted 6 lines
Cc Thomas Zimmermann <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Boris Brezillon <[email protected]>
Cc Steven Price <[email protected]>
Fixes a78027847226 ("drm/gem: Acquire reservation lock in drm_gem_{pin/unpin}()")
Quoted 17 lines
---
 drivers/gpu/drm/panfrost/panfrost_gem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/panfrost/panfrost_gem.c +1 -1
index d47b40b82b0b..8e0ff3efede7 100644
@@ -192,7 +192,7 @@ static int panfrost_gem_pin(struct drm_gem_object *obj)
 	if (bo->is_heap)
 		return -EINVAL;
 
-	return drm_gem_shmem_pin(&bo->base);
+	return drm_gem_shmem_pin_locked(&bo->base);
 }
 
 static enum drm_gem_object_status panfrost_gem_status(struct drm_gem_object *obj)
author maintainer
#3 lore raw

PATCH v4 2/3 drm/lima: Fix dma_resv deadlock at drm object pin time

In reply to Adrián Larumbe

Commit a78027847226 ("drm/gem: Acquire reservation lock in drm_gem_{pin/unpin}()") moved locking the DRM object's dma reservation to drm_gem_pin(), but Lima's pin callback kept calling drm_gem_shmem_pin, which also tries to lock the same dma_resv, leading to a double lock situation.

As was already done for Panfrost in the previous commit, fix it by replacing drm_gem_shmem_pin() with its locked variant.

Cc Thomas Zimmermann <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Boris Brezillon <[email protected]>
Cc Steven Price <[email protected]>
Fixes a78027847226 ("drm/gem: Acquire reservation lock in drm_gem_{pin/unpin}()")
---
 drivers/gpu/drm/lima/lima_gem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/lima/lima_gem.c +1 -1
index 7ea244d876ca..9bb997dbb4b9 100644
@@ -185,7 +185,7 @@ static int lima_gem_pin(struct drm_gem_object *obj)
 	if (bo->heap_size)
 		return -EINVAL;
 
-	return drm_gem_shmem_pin(&bo->base);
+	return drm_gem_shmem_pin_locked(&bo->base);
 }
 
 static int lima_gem_vmap(struct drm_gem_object *obj, struct iosys_map *map)
Signature · 1 line
2.45.1
maintainer Reviewed-by on v4 2/3
#4 lore raw

Re: [PATCH v4 2/3] drm/lima: Fix dma_resv deadlock at drm object pin time

Quoted 9 lines · On Thu, 23 May 2024 12:32:18 +0100 Adrián Larumbe…

Commit a78027847226 ("drm/gem: Acquire reservation lock in drm_gem_{pin/unpin}()") moved locking the DRM object's dma reservation to drm_gem_pin(), but Lima's pin callback kept calling drm_gem_shmem_pin, which also tries to lock the same dma_resv, leading to a double lock situation.

As was already done for Panfrost in the previous commit, fix it by replacing drm_gem_shmem_pin() with its locked variant.

Quoted 6 lines
Cc Thomas Zimmermann <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Boris Brezillon <[email protected]>
Cc Steven Price <[email protected]>
Fixes a78027847226 ("drm/gem: Acquire reservation lock in drm_gem_{pin/unpin}()")
Quoted 17 lines
---
 drivers/gpu/drm/lima/lima_gem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/lima/lima_gem.c +1 -1
index 7ea244d876ca..9bb997dbb4b9 100644
@@ -185,7 +185,7 @@ static int lima_gem_pin(struct drm_gem_object *obj)
 	if (bo->heap_size)
 		return -EINVAL;
 
-	return drm_gem_shmem_pin(&bo->base);
+	return drm_gem_shmem_pin_locked(&bo->base);
 }
 
 static int lima_gem_vmap(struct drm_gem_object *obj, struct iosys_map *map)
Tested-by on v4 2/3
#5 lore raw

Re: [PATCH v4 2/3] drm/lima: Fix dma_resv deadlock at drm object pin time

In reply to Adrián Larumbe
Quoted 8 lines · Adrián Larumbe

Commit a78027847226 ("drm/gem: Acquire reservation lock in drm_gem_{pin/unpin}()") moved locking the DRM object's dma reservation to drm_gem_pin(), but Lima's pin callback kept calling drm_gem_shmem_pin, which also tries to lock the same dma_resv, leading to a double lock situation.

As was already done for Panfrost in the previous commit, fix it by replacing drm_gem_shmem_pin() with its locked variant.

Hi, just found this while dealing with compositor lockups upon launching a GL client on an old Rockchip RK3066 tablet, and it did fix the problem :) Thank you.

author maintainer
#6 lore raw

PATCH v4 3/3 drm/gem-shmem: Add import attachment warning to locked pin function

In reply to Adrián Larumbe

Commit ec144244a43f ("drm/gem-shmem: Acquire reservation lock in GEM pin/unpin callbacks") moved locking DRM object's dma reservation to drm_gem_shmem_object_pin, and made drm_gem_shmem_pin_locked public, so we need to make sure the non-NULL check warning is also added to the latter.

Cc Thomas Zimmermann <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Boris Brezillon <[email protected]>
Fixes a78027847226 ("drm/gem: Acquire reservation lock in drm_gem_{pin/unpin}()")
---
 drivers/gpu/drm/drm_gem_shmem_helper.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/drm_gem_shmem_helper.c +2 -0
index 177773bcdbfd..ad5d9f704e15 100644
@@ -233,6 +233,8 @@ int drm_gem_shmem_pin_locked(struct drm_gem_shmem_object *shmem)
 
 	dma_resv_assert_held(shmem->base.resv);
 
+	drm_WARN_ON(shmem->base.dev, shmem->base.import_attach);
+
 	ret = drm_gem_shmem_get_pages(shmem);
 
 	return ret;
Signature · 1 line
2.45.1
maintainer Reviewed-by on v4 3/3
#7 lore raw

Re: [PATCH v4 3/3] drm/gem-shmem: Add import attachment warning to locked pin function

Quoted 10 lines · On Thu, 23 May 2024 12:32:19 +0100 Adrián Larumbe…

Commit ec144244a43f ("drm/gem-shmem: Acquire reservation lock in GEM pin/unpin callbacks") moved locking DRM object's dma reservation to drm_gem_shmem_object_pin, and made drm_gem_shmem_pin_locked public, so we need to make sure the non-NULL check warning is also added to the latter.

Cc Thomas Zimmermann <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Boris Brezillon <[email protected]>
Fixes a78027847226 ("drm/gem: Acquire reservation lock in drm_gem_{pin/unpin}()")
Quoted 17 lines
---
 drivers/gpu/drm/drm_gem_shmem_helper.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/drm_gem_shmem_helper.c +2 -0
index 177773bcdbfd..ad5d9f704e15 100644
@@ -233,6 +233,8 @@ int drm_gem_shmem_pin_locked(struct drm_gem_shmem_object *shmem)
 
 	dma_resv_assert_held(shmem->base.resv);
 
+	drm_WARN_ON(shmem->base.dev, shmem->base.import_attach);
+
 	ret = drm_gem_shmem_get_pages(shmem);
 
 	return ret;
maintainer Applied
#8 lore raw
In reply to Adrián Larumbe
Quoted 19 lines · On Thu, 23 May 2024 12:32:16 +0100 Adrián Larumbe…

This is v4 of https://lore.kernel.org/lkml/[email protected]/T/

The goal of this patch series is fixing a deadlock upon locking the dma reservation of a DRM gem object when pinning it, at a prime import operation.

Changelog:
v3:
- Split driver fixes into separate commits for Panfrost and Lima
- Make drivers call drm_gem_shmem_pin_locked instead of drm_gem_shmem_object_pin
- Improved commit message for first patch to explain why dma resv locking in the pin callback is no longer necessary.
v2:
- Removed comment explaining reason why an already-locked
pin function replaced the locked variant inside Panfrost's
object pin callback.
- Moved already-assigned attachment warning into generic
already-locked gem object pin function

Quoted 4 lines

Adrián Larumbe (3):
drm/panfrost: Fix dma_resv deadlock at drm object pin time drm/lima: Fix dma_resv deadlock at drm object pin time drm/gem-shmem: Add import attachment warning to locked pin function

Queued to drm-misc-fixes.

Thanks!

Boris