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

2024-05-02 Thread Thomas Zimmermann

Hi

Am 01.05.24 um 08:55 schrieb 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:
00e235a0 (reservation_ww_class_mutex){+.+.}-{3:3}, at: 
drm_gem_shmem_pin+0x34/0xb8 [drm_shmem_helper]
but task is already holding lock:
00e235a0 (reservation_ww_class_mutex){+.+.}-{3:3}, at: 
drm_gem_pin+0x2c/0x80 [drm]

Fix it by assuming the object's reservation had already been locked by the
time we reach panfrost_gem_pin.


Maybe say that the reservation lock has been taken in drm_gem_pin()



Do the same thing for the Lima driver, as it most likely suffers from the
same issue.


Please split this patch into one for panfrost and one for lima. To each 
patch, you can add


Reviewed-by: Thomas Zimmermann 

Best regards
Thomas



Cc: Thomas Zimmermann 
Cc: Dmitry Osipenko 
Cc: Boris Brezillon 
Cc: Steven Price 
Fixes: a78027847226 ("drm/gem: Acquire reservation lock in 
drm_gem_{pin/unpin}()")
Signed-off-by: Adrián Larumbe 
---
  drivers/gpu/drm/lima/lima_gem.c | 2 +-
  drivers/gpu/drm/panfrost/panfrost_gem.c | 2 +-
  2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/lima/lima_gem.c b/drivers/gpu/drm/lima/lima_gem.c
index 7ea244d876ca..c4e0f9faaa47 100644
--- a/drivers/gpu/drm/lima/lima_gem.c
+++ b/drivers/gpu/drm/lima/lima_gem.c
@@ -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_object_pin(obj);
  }
  
  static int lima_gem_vmap(struct drm_gem_object *obj, struct iosys_map *map)

diff --git a/drivers/gpu/drm/panfrost/panfrost_gem.c 
b/drivers/gpu/drm/panfrost/panfrost_gem.c
index d47b40b82b0b..f268bd5c2884 100644
--- a/drivers/gpu/drm/panfrost/panfrost_gem.c
+++ b/drivers/gpu/drm/panfrost/panfrost_gem.c
@@ -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_object_pin(obj);
  }
  
  static enum drm_gem_object_status panfrost_gem_status(struct drm_gem_object *obj)


--
--
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)



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

2024-05-02 Thread Boris Brezillon
On Wed,  1 May 2024 07:55:59 +0100
Adrián Larumbe  wrote:

> 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:
> 00e235a0 (reservation_ww_class_mutex){+.+.}-{3:3}, at: 
> drm_gem_shmem_pin+0x34/0xb8 [drm_shmem_helper]
> but task is already holding lock:
> 00e235a0 (reservation_ww_class_mutex){+.+.}-{3:3}, at: 
> drm_gem_pin+0x2c/0x80 [drm]
> 
> Fix it by assuming the object's reservation had already been locked by the
> time we reach panfrost_gem_pin.

You should probably mention that drm_gem_shmem_object_pin() assumes the
lock to be held, thus explaining the drm_gem_shmem_pin() ->
drm_gem_shmem_object_pin() transition.

Oh, and the commit message refers explicitly to Panfrost in a few places
even though you're fixing Lima as well.

> 
> Do the same thing for the Lima driver, as it most likely suffers from the
> same issue.
> 
> Cc: Thomas Zimmermann 
> Cc: Dmitry Osipenko 
> Cc: Boris Brezillon 
> Cc: Steven Price 
> Fixes: a78027847226 ("drm/gem: Acquire reservation lock in 
> drm_gem_{pin/unpin}()")
> Signed-off-by: Adrián Larumbe 

With the commit message adjusted as suggested,

Reviewed-by: Boris Brezillon 

> ---
>  drivers/gpu/drm/lima/lima_gem.c | 2 +-
>  drivers/gpu/drm/panfrost/panfrost_gem.c | 2 +-
>  2 files changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/gpu/drm/lima/lima_gem.c b/drivers/gpu/drm/lima/lima_gem.c
> index 7ea244d876ca..c4e0f9faaa47 100644
> --- a/drivers/gpu/drm/lima/lima_gem.c
> +++ b/drivers/gpu/drm/lima/lima_gem.c
> @@ -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_object_pin(obj);
>  }
>  
>  static int lima_gem_vmap(struct drm_gem_object *obj, struct iosys_map *map)
> diff --git a/drivers/gpu/drm/panfrost/panfrost_gem.c 
> b/drivers/gpu/drm/panfrost/panfrost_gem.c
> index d47b40b82b0b..f268bd5c2884 100644
> --- a/drivers/gpu/drm/panfrost/panfrost_gem.c
> +++ b/drivers/gpu/drm/panfrost/panfrost_gem.c
> @@ -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_object_pin(obj);
>  }
>  
>  static enum drm_gem_object_status panfrost_gem_status(struct drm_gem_object 
> *obj)