Add mutex to drm_gem_object.gpuva list
Subsystems (4)
Alice Ryhl
reviewercover Add mutex to drm_gem_object.gpuva list
See the first patch for motivation.
---
Alice Ryhl (2):
drm_gem: add mutex to drm_gem_object.gpuva
panthor: use drm_gem_object.gpuva.lock instead of gpuva_list_lock
drivers/gpu/drm/drm_gem.c | 2 ++ drivers/gpu/drm/panthor/panthor_gem.c | 4 +--- drivers/gpu/drm/panthor/panthor_gem.h | 12 ------------ drivers/gpu/drm/panthor/panthor_mmu.c | 16 ++++++++-------- include/drm/drm_gem.h | 4 +++- 5 files changed, 14 insertions(+), 24 deletions(-)
---
base-commit: 8f5ae30d69d7543eee0d70083daf4de8fe15d585
change-id: 20250814-gpuva-mutex-in-gem-a608ab334e51
Best regards,
Signature · 1 line
Alice Ryhl <[email protected]>
Alice Ryhl
reviewerPATCH 1/2 drm_gem: add mutex to drm_gem_object.gpuva
There are two main ways that GPUVM might be used:
* staged mode, where VM_BIND ioctls update the GPUVM immediately so that the GPUVM reflects the state of the VM *including* staged changes that are not yet applied to the GPU's virtual address space.
* immediate mode, where the GPUVM state is updated during run_job(), i.e., in the DMA fence signalling critical path, to ensure that the GPUVM and the GPU's virtual address space has the same state at all times.
Currently, only Panthor uses GPUVM in immediate mode, but the Rust drivers Tyr and Nova will also use GPUVM in immediate mode, so it is worth to support both staged and immediate mode well in GPUVM. To use immediate mode, the GEMs gpuva list must be modified during the fence signalling path, which means that it must be protected by a lock that is fence signalling safe.
For this reason, a mutex is added to struct drm_gem_object that is intended to achieve this purpose. Adding it directly in the GEM object both makes it easier to use GPUVM in immediate mode, but also makes it possible to take the gpuva lock from core drm code.
As a follow-up, another change that should probably be made to support immediate mode is a mechanism to postpone cleanup of vm_bo objects, as dropping a vm_bo object in the fence signalling path is problematic for two reasons:
* When using DRM_GPUVM_RESV_PROTECTED, you cannot remove the vm_bo from the extobj/evicted lists during the fence signalling path.
* Dropping a vm_bo could lead to the GEM object getting destroyed. The requirement that GEM object cleanup is fence signalling safe is dubious and likely to be violated in practice.
Panthor already has its own custom implementation of postponing vm_bo cleanup.
--- drivers/gpu/drm/drm_gem.c | 2 ++ include/drm/drm_gem.h | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_gem.c +2 -0
@@ -187,6 +187,7 @@ void drm_gem_private_object_init(struct drm_device *dev,kref_init (&obj->refcount); obj->handle_count =0 ; obj->size = size; +mutex_init (&obj->gpuva.lock);dma_resv_init (&obj->_resv);if (!obj->resv) obj->resv = &obj->_resv;
@@ -1057,6 +1058,7 @@ drm_gem_object_free(struct kref *kref)if (WARN_ON (!obj->funcs->free))return ; +mutex_destroy (&obj->gpuva.lock); obj->funcs->free (obj); }EXPORT_SYMBOL (drm_gem_object_free);
include/drm/drm_gem.h +3 -1
@@ -403,11 +403,13 @@ struct drm_gem_object {* Provides the list of GPU VAs attached to this GEM object. * * Drivers should lock list accesses with the GEMs &dma_resv lock -* (&drm_gem_object.resv) or a custom lock if one is provided. +* (&drm_gem_object.resv) or a custom lock if one is provided. The +* mutex inside this struct may be used as the custom lock. */ struct {struct list_head list; +struct mutex lock;#ifdef CONFIG_LOCKDEP struct lockdep_map *lock_dep_map;#endif
Signature · 1 line
2.51.0.rc0.215.g125493bb4a-goog
Danilo Krummrich
reviewerRe: [PATCH 1/2] drm_gem: add mutex to drm_gem_object.gpuva
Quoted 55 lines · Alice Ryhl
There are two main ways that GPUVM might be used:
* staged mode, where
VM_BINDioctls update the GPUVM immediately so that the GPUVM reflects the state of the VM *including* staged changes that are not yet applied to the GPU's virtual address space.
* immediate mode, where the GPUVM state is updated duringrun_job(), i.e., in the DMA fence signalling critical path, to ensure that the GPUVM and the GPU's virtual address space has the same state at all times.Currently, only Panthor uses GPUVM in immediate mode, but the Rust drivers Tyr and Nova will also use GPUVM in immediate mode, so it is worth to support both staged and immediate mode well in GPUVM. To use immediate mode, the GEMs gpuva list must be modified during the fence signalling path, which means that it must be protected by a lock that is fence signalling safe.
For this reason, a mutex is added to struct
drm_gem_objectthat is intended to achieve this purpose. Adding it directly in the GEM object both makes it easier to use GPUVM in immediate mode, but also makes it possible to take the gpuva lock from core drm code.As a follow-up, another change that should probably be made to support immediate mode is a mechanism to postpone cleanup of
vm_boobjects, as dropping avm_boobject in the fence signalling path is problematic for two reasons:* When using
DRM_GPUVM_RESV_PROTECTED, you cannot remove thevm_bofrom the extobj/evicted lists during the fence signalling path.
* Dropping avm_bocould lead to the GEM object getting destroyed. The requirement that GEM object cleanup is fence signalling safe is dubious and likely to be violated in practice.Panthor already has its own custom implementation of postponing
vm_bocleanup.Signed-off-by Alice Ryhl <[email protected]>--- drivers/gpu/drm/drm_gem.c | 2 ++ include/drm/drm_gem.h | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-)drivers/gpu/drm/drm_gem.c +1
-0@@ -187,6 +187,7 @@ void drm_gem_private_object_init(struct drm_device *dev,kref_init (&obj->refcount); obj->handle_count =0 ; obj->size = size; +mutex_init (&obj->gpuva.lock);dma_resv_init (&obj->_resv);if (!obj->resv) obj->resv = &obj->_resv;
drivers/gpu/drm/drm_gem.c drm_gem_object_free(struct kref *kref) · line 1062
Quoted 5 lines
drivers/gpu/drm/drm_gem.c +1
-0@@ -1057,6 +1058,7 @@ drm_gem_object_free(struct kref *kref)if (WARN_ON (!obj->funcs->free))return ; +mutex_destroy (&obj->gpuva.lock); obj->funcs->free (obj);
I really can't think of a valid case where we need to access this mutex from the GEM's free() callback, yet it probably doesn't hurt to mention it in the documentation of struct drm_gem_object_funcs.
Quoted 11 lines
drivers/gpu/drm/drm_gem.c +0
-0}EXPORT_SYMBOL (drm_gem_object_free);include/drm/drm_gem.h +0
-1@@ -403,11 +403,13 @@ struct drm_gem_object {* Provides the list of GPU VAs attached to this GEM object. * * Drivers should lock list accesses with the GEMs &dma_resv lock -* (&drm_gem_object.resv) or a custom lock if one is provided.
include/drm/drm_gem.h struct drm_gem_object { · line 415
Quoted 10 lines
include/drm/drm_gem.h +3
-0+* (&drm_gem_object.resv) or a custom lock if one is provided. The +* mutex inside this struct may be used as the custom lock. */ struct {struct list_head list; +struct mutex lock;#ifdef CONFIG_LOCKDEP struct lockdep_map *lock_dep_map;#endif
We should remove this and the corresponding functions (i.e. drm_gem_gpuva_set_lock(), drm_gem_gpuva_assert_lock_held()) in a subsequent patch and let GPUVM assert for this mutex directly rather than for the lockdep_map.
Alice Ryhl
reviewerRe: [PATCH 1/2] drm_gem: add mutex to drm_gem_object.gpuva
Quoted 60 lines · Danilo Krummrich
Quoted 58 lines · Alice Ryhl
There are two main ways that GPUVM might be used:
* staged mode, where
VM_BINDioctls update the GPUVM immediately so that the GPUVM reflects the state of the VM *including* staged changes that are not yet applied to the GPU's virtual address space.
* immediate mode, where the GPUVM state is updated duringrun_job(), i.e., in the DMA fence signalling critical path, to ensure that the GPUVM and the GPU's virtual address space has the same state at all times.Currently, only Panthor uses GPUVM in immediate mode, but the Rust drivers Tyr and Nova will also use GPUVM in immediate mode, so it is worth to support both staged and immediate mode well in GPUVM. To use immediate mode, the GEMs gpuva list must be modified during the fence signalling path, which means that it must be protected by a lock that is fence signalling safe.
For this reason, a mutex is added to struct
drm_gem_objectthat is intended to achieve this purpose. Adding it directly in the GEM object both makes it easier to use GPUVM in immediate mode, but also makes it possible to take the gpuva lock from core drm code.As a follow-up, another change that should probably be made to support immediate mode is a mechanism to postpone cleanup of
vm_boobjects, as dropping avm_boobject in the fence signalling path is problematic for two reasons:* When using
DRM_GPUVM_RESV_PROTECTED, you cannot remove thevm_bofrom the extobj/evicted lists during the fence signalling path.
* Dropping avm_bocould lead to the GEM object getting destroyed. The requirement that GEM object cleanup is fence signalling safe is dubious and likely to be violated in practice.Panthor already has its own custom implementation of postponing
vm_bocleanup.Signed-off-by Alice Ryhl <[email protected]>--- drivers/gpu/drm/drm_gem.c | 2 ++ include/drm/drm_gem.h | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-)drivers/gpu/drm/drm_gem.c +1
-0@@ -187,6 +187,7 @@ void drm_gem_private_object_init(struct drm_device *dev,kref_init (&obj->refcount); obj->handle_count =0 ; obj->size = size; +mutex_init (&obj->gpuva.lock);dma_resv_init (&obj->_resv);if (!obj->resv) obj->resv = &obj->_resv;@@ -1057,6 +1058,7 @@ drm_gem_object_free(struct kref *kref)if (WARN_ON (!obj->funcs->free))return ;
Quoted 6 lines
drivers/gpu/drm/drm_gem.c drm_gem_object_free(struct kref *kref) · line 1062
Quoted 2 lines
drivers/gpu/drm/drm_gem.c +1
-0+mutex_destroy (&obj->gpuva.lock); obj->funcs->free (obj);I really can't think of a valid case where we need to access this mutex from the GEM's
free()callback, yet it probably doesn't hurt to mention it in the documentation of structdrm_gem_object_funcs.
I had wanted to move it below free(), but we can't do that since free() also performs the kfree() call.
Quoted 17 lines
Quoted 17 lines
drivers/gpu/drm/drm_gem.c +0
-0}EXPORT_SYMBOL (drm_gem_object_free);include/drm/drm_gem.h +2
-1@@ -403,11 +403,13 @@ struct drm_gem_object {* Provides the list of GPU VAs attached to this GEM object. * * Drivers should lock list accesses with the GEMs &dma_resv lock -* (&drm_gem_object.resv) or a custom lock if one is provided. +* (&drm_gem_object.resv) or a custom lock if one is provided. The +* mutex inside this struct may be used as the custom lock. */ struct {struct list_head list;
Quoted 9 lines
include/drm/drm_gem.h struct drm_gem_object { · line 415
Quoted 4 lines
include/drm/drm_gem.h +1
-0+struct mutex lock;#ifdef CONFIG_LOCKDEP struct lockdep_map *lock_dep_map;#endif We should remove this and the corresponding functions (i.e.
drm_gem_gpuva_set_lock(),drm_gem_gpuva_assert_lock_held()) in a subsequent patch and let GPUVM assert for this mutex directly rather than for thelockdep_map.
Agreed.
Alice
Danilo Krummrich
reviewerRe: [PATCH 1/2] drm_gem: add mutex to drm_gem_object.gpuva
Quoted 2 lines · Alice Ryhl
I had wanted to move it below
free(), but we can't do that sincefree()also performs thekfree()call.
Actually, I think it belongs in drm_gem_private_object_fini(), where also call dma_resv_fini() for the GEM owned dma-resv lock.
Any cleanup should be done from where a driver calls drm_gem_object_release().
Alice Ryhl
reviewerPATCH 2/2 panthor: use drm_gem_object.gpuva.lock instead of gpuva_list_lock
In reply to Alice RyhlNow that drm_gem_object has a dedicated mutex for the gpuva list that is intended to be used in cases that must be fence signalling safe, use it in Panthor.
--- drivers/gpu/drm/panthor/panthor_gem.c | 4 +--- drivers/gpu/drm/panthor/panthor_gem.h | 12 ------------ drivers/gpu/drm/panthor/panthor_mmu.c | 16 ++++++++-------- 3 files changed, 9 insertions(+), 23 deletions(-)
drivers/gpu/drm/panthor/panthor_gem.c +1 -3
@@ -74,7 +74,6 @@ static void panthor_gem_free_object(struct drm_gem_object *obj)mutex_destroy (&bo->label.lock);drm_gem_free_mmap_offset (&bo->base.base); -mutex_destroy (&bo->gpuva_list_lock);drm_gem_shmem_free (&bo->base);drm_gem_object_put (vm_root_gem); }
@@ -246,8 +245,7 @@ struct drm_gem_object *panthor_gem_create_object(struct drm_device *ddev, size_t obj->base.base.funcs = &panthor_gem_funcs; obj->base.map_wc = !ptdev->coherent; -mutex_init (&obj->gpuva_list_lock); -drm_gem_gpuva_set_lock (&obj->base.base, &obj->gpuva_list_lock); +drm_gem_gpuva_set_lock (&obj->base.base, &obj->base.base.gpuva.lock);mutex_init (&obj->label.lock);panthor_gem_debugfs_bo_init (obj);
drivers/gpu/drm/panthor/panthor_gem.h +0 -12
@@ -79,18 +79,6 @@ struct panthor_gem_object {*/ struct drm_gem_object *exclusive_vm_root_gem; -/** -* @gpuva_list_lock: Custom GPUVA lock. -* -* Used to protect insertion of drm_gpuva elements to the -* drm_gem_object.gpuva.list list. -* -* We can't use the GEM resv for that, because drm_gpuva_link() is -* called in a dma-signaling path, where we're not allowed to take -* resv locks. -*/ -struct mutex gpuva_list_lock; -/** @flags: Combination of drm_panthor_bo_flags flags. */ u32 flags;
drivers/gpu/drm/panthor/panthor_mmu.c +8 -8
@@ -1074,9 +1074,9 @@ static void panthor_vm_bo_put(struct drm_gpuvm_bo *vm_bo)* GEM vm_bo list. */ dma_resv_lock (drm_gpuvm_resv (vm),NULL ); -mutex_lock (&bo->gpuva_list_lock); +mutex_lock (&bo->base.base.gpuva.lock); unpin =drm_gpuvm_bo_put (vm_bo); -mutex_unlock (&bo->gpuva_list_lock); +mutex_unlock (&bo->base.base.gpuva.lock);dma_resv_unlock (drm_gpuvm_resv (vm));/* If the vm_bo object was destroyed, release the pin reference that
@@ -1249,9 +1249,9 @@ static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx,* calling this function. */ dma_resv_lock (panthor_vm_resv (vm),NULL ); -mutex_lock (&bo->gpuva_list_lock); +mutex_lock (&bo->base.base.gpuva.lock); op_ctx->map.vm_bo =drm_gpuvm_bo_obtain_prealloc (preallocated_vm_bo); -mutex_unlock (&bo->gpuva_list_lock); +mutex_unlock (&bo->base.base.gpuva.lock);dma_resv_unlock (panthor_vm_resv (vm));/* If the a vm_bo for this <VM,BO> combination exists, it already
@@ -2003,10 +2003,10 @@ static void panthor_vma_link(struct panthor_vm *vm, {struct panthor_gem_object *bo =to_panthor_bo (vma->base.gem.obj); -mutex_lock (&bo->gpuva_list_lock); +mutex_lock (&bo->base.base.gpuva.lock);drm_gpuva_link (&vma->base, vm_bo);drm_WARN_ON (&vm->ptdev->base,drm_gpuvm_bo_put (vm_bo)); -mutex_unlock (&bo->gpuva_list_lock); +mutex_unlock (&bo->base.base.gpuva.lock); } static void panthor_vma_unlink(struct panthor_vm *vm,
@@ -2015,9 +2015,9 @@ static void panthor_vma_unlink(struct panthor_vm *vm,struct panthor_gem_object *bo =to_panthor_bo (vma->base.gem.obj);struct drm_gpuvm_bo *vm_bo =drm_gpuvm_bo_get (vma->base.vm_bo); -mutex_lock (&bo->gpuva_list_lock); +mutex_lock (&bo->base.base.gpuva.lock);drm_gpuva_unlink (&vma->base); -mutex_unlock (&bo->gpuva_list_lock); +mutex_unlock (&bo->base.base.gpuva.lock);/* drm_gpuva_unlink() release the vm_bo, but we manually retained it * when entering this function, so we can implement deferred VMA
Signature · 1 line
2.51.0.rc0.215.g125493bb4a-goog
Re: [PATCH 2/2] panthor: use drm_gem_object.gpuva.lock instead of gpuva_list_lock
Quoted 24 lines · On Thu, 14 Aug 2025 13:53:15 +0000 Alice Ryhl <[email protected]> wrote:
Now that
drm_gem_objecthas a dedicated mutex for the gpuva list that is intended to be used in cases that must be fence signalling safe, use it in Panthor.Signed-off-by Alice Ryhl <[email protected]>--- drivers/gpu/drm/panthor/panthor_gem.c | 4 +--- drivers/gpu/drm/panthor/panthor_gem.h | 12 ------------ drivers/gpu/drm/panthor/panthor_mmu.c | 16 ++++++++-------- 3 files changed, 9 insertions(+), 23 deletions(-)drivers/gpu/drm/panthor/panthor_gem.c +0
-1@@ -74,7 +74,6 @@ static void panthor_gem_free_object(struct drm_gem_object *obj)mutex_destroy (&bo->label.lock);drm_gem_free_mmap_offset (&bo->base.base); -mutex_destroy (&bo->gpuva_list_lock);drm_gem_shmem_free (&bo->base);drm_gem_object_put (vm_root_gem); }
drivers/gpu/drm/panthor/panthor_gem.c struct drm_gem_object *panthor_gem_create_object(struct drm_device *ddev, size_t · line 248
Quoted 6 lines
drivers/gpu/drm/panthor/panthor_gem.c +1
-2@@ -246,8 +245,7 @@ struct drm_gem_object *panthor_gem_create_object(struct drm_device *ddev, size_t obj->base.base.funcs = &panthor_gem_funcs; obj->base.map_wc = !ptdev->coherent; -mutex_init (&obj->gpuva_list_lock); -drm_gem_gpuva_set_lock (&obj->base.base, &obj->gpuva_list_lock); +drm_gem_gpuva_set_lock (&obj->base.base, &obj->base.base.gpuva.lock);
I guess this will go away in the previous patch in you follow Danilo's advice to get rid of drm_gem_gpuva_set_lock(). The rest looks good to me, so feel free to add
on the next version.
Quoted 80 lines
drivers/gpu/drm/panthor/panthor_gem.c +0
-0mutex_init (&obj->label.lock);panthor_gem_debugfs_bo_init (obj);drivers/gpu/drm/panthor/panthor_gem.h +0
-12@@ -79,18 +79,6 @@ struct panthor_gem_object {*/ struct drm_gem_object *exclusive_vm_root_gem; -/** -* @gpuva_list_lock: Custom GPUVA lock. -* -* Used to protect insertion of drm_gpuva elements to the -* drm_gem_object.gpuva.list list. -* -* We can't use the GEM resv for that, because drm_gpuva_link() is -* called in a dma-signaling path, where we're not allowed to take -* resv locks. -*/ -struct mutex gpuva_list_lock; -/** @flags: Combination of drm_panthor_bo_flags flags. */ u32 flags;drivers/gpu/drm/panthor/panthor_mmu.c +8
-8@@ -1074,9 +1074,9 @@ static void panthor_vm_bo_put(struct drm_gpuvm_bo *vm_bo)* GEM vm_bo list. */ dma_resv_lock (drm_gpuvm_resv (vm),NULL ); -mutex_lock (&bo->gpuva_list_lock); +mutex_lock (&bo->base.base.gpuva.lock); unpin =drm_gpuvm_bo_put (vm_bo); -mutex_unlock (&bo->gpuva_list_lock); +mutex_unlock (&bo->base.base.gpuva.lock);dma_resv_unlock (drm_gpuvm_resv (vm));/* If the vm_bo object was destroyed, release the pin reference that @@ -1249,9 +1249,9 @@ static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx,* calling this function. */ dma_resv_lock (panthor_vm_resv (vm),NULL ); -mutex_lock (&bo->gpuva_list_lock); +mutex_lock (&bo->base.base.gpuva.lock); op_ctx->map.vm_bo =drm_gpuvm_bo_obtain_prealloc (preallocated_vm_bo); -mutex_unlock (&bo->gpuva_list_lock); +mutex_unlock (&bo->base.base.gpuva.lock);dma_resv_unlock (panthor_vm_resv (vm));/* If the a vm_bo for this <VM,BO> combination exists, it already @@ -2003,10 +2003,10 @@ static void panthor_vma_link(struct panthor_vm *vm, {struct panthor_gem_object *bo =to_panthor_bo (vma->base.gem.obj); -mutex_lock (&bo->gpuva_list_lock); +mutex_lock (&bo->base.base.gpuva.lock);drm_gpuva_link (&vma->base, vm_bo);drm_WARN_ON (&vm->ptdev->base,drm_gpuvm_bo_put (vm_bo)); -mutex_unlock (&bo->gpuva_list_lock); +mutex_unlock (&bo->base.base.gpuva.lock); } static void panthor_vma_unlink(struct panthor_vm *vm,@@ -2015,9 +2015,9 @@ static void panthor_vma_unlink(struct panthor_vm *vm,struct panthor_gem_object *bo =to_panthor_bo (vma->base.gem.obj);struct drm_gpuvm_bo *vm_bo =drm_gpuvm_bo_get (vma->base.vm_bo); -mutex_lock (&bo->gpuva_list_lock); +mutex_lock (&bo->base.base.gpuva.lock);drm_gpuva_unlink (&vma->base); -mutex_unlock (&bo->gpuva_list_lock); +mutex_unlock (&bo->base.base.gpuva.lock);/* drm_gpuva_unlink() release the vm_bo, but we manually retained it * when entering this function, so we can implement deferred VMA