* [PATCH v3 0/2] Defer vm_bo cleanup in GPUVM with DRM_GPUVM_IMMEDIATE_MODE
@ 2025-10-01 10:41 Alice Ryhl
2025-10-01 10:41 ` [PATCH v3 1/2] drm/gpuvm: add deferred vm_bo cleanup Alice Ryhl
2025-10-01 10:41 ` [PATCH v3 2/2] panthor: use drm_gpuva_unlink_defer() Alice Ryhl
0 siblings, 2 replies; 18+ messages in thread
From: Alice Ryhl @ 2025-10-01 10:41 UTC (permalink / raw)
To: Danilo Krummrich, Matthew Brost, Thomas Hellström
Cc: Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie,
Simona Vetter, Boris Brezillon, Steven Price, Daniel Almeida,
Liviu Dudau, dri-devel, linux-kernel, rust-for-linux, Alice Ryhl
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, we must manage the vm_bos and vas during the fence
signalling critical path.
The first part of that work was the introduction of a fence signalling
safe mutex for the GEMs GPUVA list in commit e7fa80e2932c ("drm_gem: add
mutex to drm_gem_object.gpuva").
This is series the second part of that work: Dropping a vm_bo object in
the fence signalling critical 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. Take inspiration from that by moving the logic into GPUVM, and
adjust Panthor to use the new GPUVM logic.
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
Changes in v3:
- Unpin in panthor on drm_gpuvm_bo_create() failure.
- Use llist for bo_defer list.
- Rename drm_gpuvm_bo_is_dead() to drm_gpuvm_bo_is_zombie().
- Rename drm_gpuvm_bo_defer() to drm_gpuvm_bo_defer_free().
- Link to v2: https://lore.kernel.org/r/20250909-vmbo-defer-v2-0-9835d7349089@google.com
Changes in v2:
- Fix missing kfree in Panthor.
- Rework mutex_lock() calls to be less confusing.
- Add note about resv lock in drm_gpuvm_bo_is_dead() docs.
- Link to v1: https://lore.kernel.org/r/20250905-vmbo-defer-v1-0-7ae1a382b674@google.com
---
Alice Ryhl (2):
drm/gpuvm: add deferred vm_bo cleanup
panthor: use drm_gpuva_unlink_defer()
drivers/gpu/drm/drm_gpuvm.c | 184 ++++++++++++++++++++++++++++++++++
drivers/gpu/drm/panthor/panthor_mmu.c | 110 ++++----------------
include/drm/drm_gpuvm.h | 16 +++
3 files changed, 219 insertions(+), 91 deletions(-)
---
base-commit: b2ec5ca9d5c2c019e2316f7ba447596d1dcd8fde
change-id: 20250905-vmbo-defer-3faf90d821f5
Best regards,
--
Alice Ryhl <aliceryhl@google.com>
^ permalink raw reply [flat|nested] 18+ messages in thread* [PATCH v3 1/2] drm/gpuvm: add deferred vm_bo cleanup 2025-10-01 10:41 [PATCH v3 0/2] Defer vm_bo cleanup in GPUVM with DRM_GPUVM_IMMEDIATE_MODE Alice Ryhl @ 2025-10-01 10:41 ` Alice Ryhl 2025-10-01 11:27 ` Boris Brezillon ` (2 more replies) 2025-10-01 10:41 ` [PATCH v3 2/2] panthor: use drm_gpuva_unlink_defer() Alice Ryhl 1 sibling, 3 replies; 18+ messages in thread From: Alice Ryhl @ 2025-10-01 10:41 UTC (permalink / raw) To: Danilo Krummrich, Matthew Brost, Thomas Hellström Cc: Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie, Simona Vetter, Boris Brezillon, Steven Price, Daniel Almeida, Liviu Dudau, dri-devel, linux-kernel, rust-for-linux, Alice Ryhl When using GPUVM in immediate mode, it is necessary to call drm_gpuvm_unlink() from the fence signalling critical path. However, unlink may call drm_gpuvm_bo_put(), which causes some challenges: 1. drm_gpuvm_bo_put() often requires you to take resv locks, which you can't do from the fence signalling critical path. 2. drm_gpuvm_bo_put() calls drm_gem_object_put(), which is often going to be unsafe to call from the fence signalling critical path. To solve these issues, add a deferred version of drm_gpuvm_unlink() that adds the vm_bo to a deferred cleanup list, and then clean it up later. The new methods take the GEMs GPUVA lock internally rather than letting the caller do it because it also needs to perform an operation after releasing the mutex again. This is to prevent freeing the GEM while holding the mutex (more info as comments in the patch). This means that the new methods can only be used with DRM_GPUVM_IMMEDIATE_MODE. Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com> Signed-off-by: Alice Ryhl <aliceryhl@google.com> --- drivers/gpu/drm/drm_gpuvm.c | 184 ++++++++++++++++++++++++++++++++++++++++++++ include/drm/drm_gpuvm.h | 16 ++++ 2 files changed, 200 insertions(+) diff --git a/drivers/gpu/drm/drm_gpuvm.c b/drivers/gpu/drm/drm_gpuvm.c index a52e95555549a16c062168253477035679d4775b..a530cf0864c5dd837840f31d3e698d4a82c58d3c 100644 --- a/drivers/gpu/drm/drm_gpuvm.c +++ b/drivers/gpu/drm/drm_gpuvm.c @@ -876,6 +876,27 @@ __drm_gpuvm_bo_list_add(struct drm_gpuvm *gpuvm, spinlock_t *lock, cond_spin_unlock(lock, !!lock); } +/** + * drm_gpuvm_bo_is_zombie() - check whether this vm_bo is scheduled for cleanup + * @vm_bo: the &drm_gpuvm_bo + * + * When a vm_bo is scheduled for cleanup using the bo_defer list, it is not + * immediately removed from the evict and extobj lists if they are protected by + * the resv lock, as we can't take that lock during run_job() in immediate + * mode. Therefore, anyone iterating these lists should skip entries that are + * being destroyed. + * + * Checking the refcount without incrementing it is okay as long as the lock + * protecting the evict/extobj list is held for as long as you are using the + * vm_bo, because even if the refcount hits zero while you are using it, freeing + * the vm_bo requires taking the list's lock. + */ +static bool +drm_gpuvm_bo_is_zombie(struct drm_gpuvm_bo *vm_bo) +{ + return !kref_read(&vm_bo->kref); +} + /** * drm_gpuvm_bo_list_add() - insert a vm_bo into the given list * @__vm_bo: the &drm_gpuvm_bo @@ -1081,6 +1102,8 @@ drm_gpuvm_init(struct drm_gpuvm *gpuvm, const char *name, INIT_LIST_HEAD(&gpuvm->evict.list); spin_lock_init(&gpuvm->evict.lock); + init_llist_head(&gpuvm->bo_defer); + kref_init(&gpuvm->kref); gpuvm->name = name ? name : "unknown"; @@ -1122,6 +1145,8 @@ drm_gpuvm_fini(struct drm_gpuvm *gpuvm) "Extobj list should be empty.\n"); drm_WARN(gpuvm->drm, !list_empty(&gpuvm->evict.list), "Evict list should be empty.\n"); + drm_WARN(gpuvm->drm, !llist_empty(&gpuvm->bo_defer), + "VM BO cleanup list should be empty.\n"); drm_gem_object_put(gpuvm->r_obj); } @@ -1217,6 +1242,9 @@ drm_gpuvm_prepare_objects_locked(struct drm_gpuvm *gpuvm, drm_gpuvm_resv_assert_held(gpuvm); list_for_each_entry(vm_bo, &gpuvm->extobj.list, list.entry.extobj) { + if (drm_gpuvm_bo_is_zombie(vm_bo)) + continue; + ret = exec_prepare_obj(exec, vm_bo->obj, num_fences); if (ret) break; @@ -1460,6 +1488,9 @@ drm_gpuvm_validate_locked(struct drm_gpuvm *gpuvm, struct drm_exec *exec) list_for_each_entry_safe(vm_bo, next, &gpuvm->evict.list, list.entry.evict) { + if (drm_gpuvm_bo_is_zombie(vm_bo)) + continue; + ret = ops->vm_bo_validate(vm_bo, exec); if (ret) break; @@ -1560,6 +1591,7 @@ drm_gpuvm_bo_create(struct drm_gpuvm *gpuvm, INIT_LIST_HEAD(&vm_bo->list.entry.extobj); INIT_LIST_HEAD(&vm_bo->list.entry.evict); + init_llist_node(&vm_bo->list.entry.bo_defer); return vm_bo; } @@ -1621,6 +1653,124 @@ drm_gpuvm_bo_put(struct drm_gpuvm_bo *vm_bo) } EXPORT_SYMBOL_GPL(drm_gpuvm_bo_put); +/* + * Must be called with GEM mutex held. After releasing GEM mutex, + * drm_gpuvm_bo_defer_free_unlocked() must be called. + */ +static void +drm_gpuvm_bo_defer_free_locked(struct kref *kref) +{ + struct drm_gpuvm_bo *vm_bo = container_of(kref, struct drm_gpuvm_bo, + kref); + struct drm_gpuvm *gpuvm = vm_bo->vm; + + if (!drm_gpuvm_resv_protected(gpuvm)) { + drm_gpuvm_bo_list_del(vm_bo, extobj, true); + drm_gpuvm_bo_list_del(vm_bo, evict, true); + } + + list_del(&vm_bo->list.entry.gem); +} + +/* + * GEM mutex must not be held. Called after drm_gpuvm_bo_defer_free_locked(). + */ +static void +drm_gpuvm_bo_defer_free_unlocked(struct drm_gpuvm_bo *vm_bo) +{ + struct drm_gpuvm *gpuvm = vm_bo->vm; + + llist_add(&vm_bo->list.entry.bo_defer, &gpuvm->bo_defer); +} + +static void +drm_gpuvm_bo_defer_free(struct kref *kref) +{ + struct drm_gpuvm_bo *vm_bo = container_of(kref, struct drm_gpuvm_bo, + kref); + + mutex_lock(&vm_bo->obj->gpuva.lock); + drm_gpuvm_bo_defer_free_locked(kref); + mutex_unlock(&vm_bo->obj->gpuva.lock); + + /* + * It's important that the GEM stays alive for the duration in which we + * hold the mutex, but the instant we add the vm_bo to bo_defer, + * another thread might call drm_gpuvm_bo_deferred_cleanup() and put + * the GEM. Therefore, to avoid kfreeing a mutex we are holding, we add + * the vm_bo to bo_defer *after* releasing the GEM's mutex. + */ + drm_gpuvm_bo_defer_free_unlocked(vm_bo); +} + +/** + * drm_gpuvm_bo_put_deferred() - drop a struct drm_gpuvm_bo reference with + * deferred cleanup + * @vm_bo: the &drm_gpuvm_bo to release the reference of + * + * This releases a reference to @vm_bo. + * + * This might take and release the GEMs GPUVA lock. You should call + * drm_gpuvm_bo_deferred_cleanup() later to complete the cleanup process. + * + * Returns: true if vm_bo is being destroyed, false otherwise. + */ +bool +drm_gpuvm_bo_put_deferred(struct drm_gpuvm_bo *vm_bo) +{ + if (!vm_bo) + return false; + + drm_WARN_ON(vm_bo->vm->drm, !drm_gpuvm_immediate_mode(vm_bo->vm)); + + return !!kref_put(&vm_bo->kref, drm_gpuvm_bo_defer_free); +} +EXPORT_SYMBOL_GPL(drm_gpuvm_bo_put_deferred); + +/** + * drm_gpuvm_bo_deferred_cleanup() - clean up BOs in the deferred list + * deferred cleanup + * @gpuvm: the VM to clean up + * + * Cleans up &drm_gpuvm_bo instances in the deferred cleanup list. + */ +void +drm_gpuvm_bo_deferred_cleanup(struct drm_gpuvm *gpuvm) +{ + const struct drm_gpuvm_ops *ops = gpuvm->ops; + struct drm_gpuvm_bo *vm_bo; + struct drm_gem_object *obj; + struct llist_node *bo_defer; + + bo_defer = llist_del_all(&gpuvm->bo_defer); + if (!bo_defer) + return; + + if (drm_gpuvm_resv_protected(gpuvm)) { + dma_resv_lock(drm_gpuvm_resv(gpuvm), NULL); + llist_for_each_entry(vm_bo, bo_defer, list.entry.bo_defer) { + drm_gpuvm_bo_list_del(vm_bo, extobj, false); + drm_gpuvm_bo_list_del(vm_bo, evict, false); + } + dma_resv_unlock(drm_gpuvm_resv(gpuvm)); + } + + while (bo_defer) { + vm_bo = llist_entry(bo_defer, + struct drm_gpuvm_bo, list.entry.bo_defer); + bo_defer = bo_defer->next; + obj = vm_bo->obj; + if (ops && ops->vm_bo_free) + ops->vm_bo_free(vm_bo); + else + kfree(vm_bo); + + drm_gpuvm_put(gpuvm); + drm_gem_object_put(obj); + } +} +EXPORT_SYMBOL_GPL(drm_gpuvm_bo_deferred_cleanup); + static struct drm_gpuvm_bo * __drm_gpuvm_bo_find(struct drm_gpuvm *gpuvm, struct drm_gem_object *obj) @@ -1948,6 +2098,40 @@ drm_gpuva_unlink(struct drm_gpuva *va) } EXPORT_SYMBOL_GPL(drm_gpuva_unlink); +/** + * drm_gpuva_unlink_defer() - unlink a &drm_gpuva with deferred vm_bo cleanup + * @va: the &drm_gpuva to unlink + * + * Similar to drm_gpuva_unlink(), but uses drm_gpuvm_bo_put_deferred() and takes + * the lock for the caller. + */ +void +drm_gpuva_unlink_defer(struct drm_gpuva *va) +{ + struct drm_gem_object *obj = va->gem.obj; + struct drm_gpuvm_bo *vm_bo = va->vm_bo; + bool should_defer_bo; + + if (unlikely(!obj)) + return; + + drm_WARN_ON(vm_bo->vm->drm, !drm_gpuvm_immediate_mode(vm_bo->vm)); + + mutex_lock(&obj->gpuva.lock); + list_del_init(&va->gem.entry); + + /* + * This is drm_gpuvm_bo_put_deferred() except we already hold the mutex. + */ + should_defer_bo = kref_put(&vm_bo->kref, drm_gpuvm_bo_defer_free_locked); + mutex_unlock(&obj->gpuva.lock); + if (should_defer_bo) + drm_gpuvm_bo_defer_free_unlocked(vm_bo); + + va->vm_bo = NULL; +} +EXPORT_SYMBOL_GPL(drm_gpuva_unlink_defer); + /** * drm_gpuva_find_first() - find the first &drm_gpuva in the given range * @gpuvm: the &drm_gpuvm to search in diff --git a/include/drm/drm_gpuvm.h b/include/drm/drm_gpuvm.h index 8890ded1d90752a2acbb564f697aa5ab03b5d052..81cc7672cf2d5362c637abfa2a75471e5274ed08 100644 --- a/include/drm/drm_gpuvm.h +++ b/include/drm/drm_gpuvm.h @@ -27,6 +27,7 @@ #include <linux/dma-resv.h> #include <linux/list.h> +#include <linux/llist.h> #include <linux/rbtree.h> #include <linux/types.h> @@ -152,6 +153,7 @@ void drm_gpuva_remove(struct drm_gpuva *va); void drm_gpuva_link(struct drm_gpuva *va, struct drm_gpuvm_bo *vm_bo); void drm_gpuva_unlink(struct drm_gpuva *va); +void drm_gpuva_unlink_defer(struct drm_gpuva *va); struct drm_gpuva *drm_gpuva_find(struct drm_gpuvm *gpuvm, u64 addr, u64 range); @@ -331,6 +333,11 @@ struct drm_gpuvm { */ spinlock_t lock; } evict; + + /** + * @bo_defer: structure holding vm_bos that need to be destroyed + */ + struct llist_head bo_defer; }; void drm_gpuvm_init(struct drm_gpuvm *gpuvm, const char *name, @@ -714,6 +721,12 @@ struct drm_gpuvm_bo { * &drm_gpuvms evict list. */ struct list_head evict; + + /** + * @list.entry.bo_defer: List entry to attach to + * the &drm_gpuvms bo_defer list. + */ + struct llist_node bo_defer; } entry; } list; }; @@ -746,6 +759,9 @@ drm_gpuvm_bo_get(struct drm_gpuvm_bo *vm_bo) bool drm_gpuvm_bo_put(struct drm_gpuvm_bo *vm_bo); +bool drm_gpuvm_bo_put_deferred(struct drm_gpuvm_bo *vm_bo); +void drm_gpuvm_bo_deferred_cleanup(struct drm_gpuvm *gpuvm); + struct drm_gpuvm_bo * drm_gpuvm_bo_find(struct drm_gpuvm *gpuvm, struct drm_gem_object *obj); -- 2.51.0.618.g983fd99d29-goog ^ permalink raw reply related [flat|nested] 18+ messages in thread
* Re: [PATCH v3 1/2] drm/gpuvm: add deferred vm_bo cleanup 2025-10-01 10:41 ` [PATCH v3 1/2] drm/gpuvm: add deferred vm_bo cleanup Alice Ryhl @ 2025-10-01 11:27 ` Boris Brezillon 2025-10-01 11:45 ` Alice Ryhl 2025-10-01 14:01 ` Danilo Krummrich 2025-10-06 11:30 ` Alice Ryhl 2 siblings, 1 reply; 18+ messages in thread From: Boris Brezillon @ 2025-10-01 11:27 UTC (permalink / raw) To: Alice Ryhl Cc: Danilo Krummrich, Matthew Brost, Thomas Hellström, Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie, Simona Vetter, Steven Price, Daniel Almeida, Liviu Dudau, dri-devel, linux-kernel, rust-for-linux On Wed, 01 Oct 2025 10:41:36 +0000 Alice Ryhl <aliceryhl@google.com> wrote: > When using GPUVM in immediate mode, it is necessary to call > drm_gpuvm_unlink() from the fence signalling critical path. However, > unlink may call drm_gpuvm_bo_put(), which causes some challenges: > > 1. drm_gpuvm_bo_put() often requires you to take resv locks, which you > can't do from the fence signalling critical path. > 2. drm_gpuvm_bo_put() calls drm_gem_object_put(), which is often going > to be unsafe to call from the fence signalling critical path. > > To solve these issues, add a deferred version of drm_gpuvm_unlink() that > adds the vm_bo to a deferred cleanup list, and then clean it up later. > > The new methods take the GEMs GPUVA lock internally rather than letting > the caller do it because it also needs to perform an operation after > releasing the mutex again. This is to prevent freeing the GEM while > holding the mutex (more info as comments in the patch). This means that > the new methods can only be used with DRM_GPUVM_IMMEDIATE_MODE. > > Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com> > Signed-off-by: Alice Ryhl <aliceryhl@google.com> > --- > drivers/gpu/drm/drm_gpuvm.c | 184 ++++++++++++++++++++++++++++++++++++++++++++ > include/drm/drm_gpuvm.h | 16 ++++ > 2 files changed, 200 insertions(+) > > diff --git a/drivers/gpu/drm/drm_gpuvm.c b/drivers/gpu/drm/drm_gpuvm.c > index a52e95555549a16c062168253477035679d4775b..a530cf0864c5dd837840f31d3e698d4a82c58d3c 100644 > --- a/drivers/gpu/drm/drm_gpuvm.c > +++ b/drivers/gpu/drm/drm_gpuvm.c > @@ -876,6 +876,27 @@ __drm_gpuvm_bo_list_add(struct drm_gpuvm *gpuvm, spinlock_t *lock, > cond_spin_unlock(lock, !!lock); > } > > +/** > + * drm_gpuvm_bo_is_zombie() - check whether this vm_bo is scheduled for cleanup > + * @vm_bo: the &drm_gpuvm_bo > + * > + * When a vm_bo is scheduled for cleanup using the bo_defer list, it is not > + * immediately removed from the evict and extobj lists if they are protected by > + * the resv lock, as we can't take that lock during run_job() in immediate > + * mode. Therefore, anyone iterating these lists should skip entries that are > + * being destroyed. > + * > + * Checking the refcount without incrementing it is okay as long as the lock > + * protecting the evict/extobj list is held for as long as you are using the > + * vm_bo, because even if the refcount hits zero while you are using it, freeing > + * the vm_bo requires taking the list's lock. > + */ > +static bool > +drm_gpuvm_bo_is_zombie(struct drm_gpuvm_bo *vm_bo) > +{ > + return !kref_read(&vm_bo->kref); > +} > + > /** > * drm_gpuvm_bo_list_add() - insert a vm_bo into the given list > * @__vm_bo: the &drm_gpuvm_bo > @@ -1081,6 +1102,8 @@ drm_gpuvm_init(struct drm_gpuvm *gpuvm, const char *name, > INIT_LIST_HEAD(&gpuvm->evict.list); > spin_lock_init(&gpuvm->evict.lock); > > + init_llist_head(&gpuvm->bo_defer); > + > kref_init(&gpuvm->kref); > > gpuvm->name = name ? name : "unknown"; > @@ -1122,6 +1145,8 @@ drm_gpuvm_fini(struct drm_gpuvm *gpuvm) > "Extobj list should be empty.\n"); > drm_WARN(gpuvm->drm, !list_empty(&gpuvm->evict.list), > "Evict list should be empty.\n"); > + drm_WARN(gpuvm->drm, !llist_empty(&gpuvm->bo_defer), > + "VM BO cleanup list should be empty.\n"); > > drm_gem_object_put(gpuvm->r_obj); > } > @@ -1217,6 +1242,9 @@ drm_gpuvm_prepare_objects_locked(struct drm_gpuvm *gpuvm, > > drm_gpuvm_resv_assert_held(gpuvm); > list_for_each_entry(vm_bo, &gpuvm->extobj.list, list.entry.extobj) { > + if (drm_gpuvm_bo_is_zombie(vm_bo)) > + continue; > + > ret = exec_prepare_obj(exec, vm_bo->obj, num_fences); > if (ret) > break; > @@ -1460,6 +1488,9 @@ drm_gpuvm_validate_locked(struct drm_gpuvm *gpuvm, struct drm_exec *exec) > > list_for_each_entry_safe(vm_bo, next, &gpuvm->evict.list, > list.entry.evict) { > + if (drm_gpuvm_bo_is_zombie(vm_bo)) > + continue; > + > ret = ops->vm_bo_validate(vm_bo, exec); > if (ret) > break; > @@ -1560,6 +1591,7 @@ drm_gpuvm_bo_create(struct drm_gpuvm *gpuvm, > > INIT_LIST_HEAD(&vm_bo->list.entry.extobj); > INIT_LIST_HEAD(&vm_bo->list.entry.evict); > + init_llist_node(&vm_bo->list.entry.bo_defer); > > return vm_bo; > } > @@ -1621,6 +1653,124 @@ drm_gpuvm_bo_put(struct drm_gpuvm_bo *vm_bo) > } > EXPORT_SYMBOL_GPL(drm_gpuvm_bo_put); > > +/* > + * Must be called with GEM mutex held. After releasing GEM mutex, > + * drm_gpuvm_bo_defer_free_unlocked() must be called. > + */ > +static void > +drm_gpuvm_bo_defer_free_locked(struct kref *kref) > +{ > + struct drm_gpuvm_bo *vm_bo = container_of(kref, struct drm_gpuvm_bo, > + kref); > + struct drm_gpuvm *gpuvm = vm_bo->vm; > + > + if (!drm_gpuvm_resv_protected(gpuvm)) { > + drm_gpuvm_bo_list_del(vm_bo, extobj, true); > + drm_gpuvm_bo_list_del(vm_bo, evict, true); > + } > + > + list_del(&vm_bo->list.entry.gem); > +} > + > +/* > + * GEM mutex must not be held. Called after drm_gpuvm_bo_defer_free_locked(). > + */ > +static void > +drm_gpuvm_bo_defer_free_unlocked(struct drm_gpuvm_bo *vm_bo) > +{ > + struct drm_gpuvm *gpuvm = vm_bo->vm; > + > + llist_add(&vm_bo->list.entry.bo_defer, &gpuvm->bo_defer); Could we simply move this line to drm_gpuvm_bo_defer_free_locked()? I might be missing something, but I don't really see a reason to have it exposed as a separate operation. > +} > + > +static void > +drm_gpuvm_bo_defer_free(struct kref *kref) > +{ > + struct drm_gpuvm_bo *vm_bo = container_of(kref, struct drm_gpuvm_bo, > + kref); > + > + mutex_lock(&vm_bo->obj->gpuva.lock); > + drm_gpuvm_bo_defer_free_locked(kref); > + mutex_unlock(&vm_bo->obj->gpuva.lock); > + > + /* > + * It's important that the GEM stays alive for the duration in which we > + * hold the mutex, but the instant we add the vm_bo to bo_defer, > + * another thread might call drm_gpuvm_bo_deferred_cleanup() and put > + * the GEM. Therefore, to avoid kfreeing a mutex we are holding, we add > + * the vm_bo to bo_defer *after* releasing the GEM's mutex. > + */ > + drm_gpuvm_bo_defer_free_unlocked(vm_bo); > +} > + > +/** > + * drm_gpuvm_bo_put_deferred() - drop a struct drm_gpuvm_bo reference with > + * deferred cleanup > + * @vm_bo: the &drm_gpuvm_bo to release the reference of > + * > + * This releases a reference to @vm_bo. > + * > + * This might take and release the GEMs GPUVA lock. You should call > + * drm_gpuvm_bo_deferred_cleanup() later to complete the cleanup process. > + * > + * Returns: true if vm_bo is being destroyed, false otherwise. > + */ > +bool > +drm_gpuvm_bo_put_deferred(struct drm_gpuvm_bo *vm_bo) > +{ > + if (!vm_bo) > + return false; > + > + drm_WARN_ON(vm_bo->vm->drm, !drm_gpuvm_immediate_mode(vm_bo->vm)); > + > + return !!kref_put(&vm_bo->kref, drm_gpuvm_bo_defer_free); > +} > +EXPORT_SYMBOL_GPL(drm_gpuvm_bo_put_deferred); > + > +/** > + * drm_gpuvm_bo_deferred_cleanup() - clean up BOs in the deferred list > + * deferred cleanup > + * @gpuvm: the VM to clean up > + * > + * Cleans up &drm_gpuvm_bo instances in the deferred cleanup list. > + */ > +void > +drm_gpuvm_bo_deferred_cleanup(struct drm_gpuvm *gpuvm) > +{ > + const struct drm_gpuvm_ops *ops = gpuvm->ops; > + struct drm_gpuvm_bo *vm_bo; > + struct drm_gem_object *obj; > + struct llist_node *bo_defer; > + > + bo_defer = llist_del_all(&gpuvm->bo_defer); > + if (!bo_defer) > + return; > + > + if (drm_gpuvm_resv_protected(gpuvm)) { > + dma_resv_lock(drm_gpuvm_resv(gpuvm), NULL); > + llist_for_each_entry(vm_bo, bo_defer, list.entry.bo_defer) { > + drm_gpuvm_bo_list_del(vm_bo, extobj, false); > + drm_gpuvm_bo_list_del(vm_bo, evict, false); > + } > + dma_resv_unlock(drm_gpuvm_resv(gpuvm)); > + } > + > + while (bo_defer) { > + vm_bo = llist_entry(bo_defer, > + struct drm_gpuvm_bo, list.entry.bo_defer); nit: second line of arguments should be aligned on the open parenthesis. vm_bo = llist_entry(bo_defer, struct drm_gpuvm_bo, list.entry.bo_defer); > + bo_defer = bo_defer->next; > + obj = vm_bo->obj; > + if (ops && ops->vm_bo_free) > + ops->vm_bo_free(vm_bo); > + else > + kfree(vm_bo); > + > + drm_gpuvm_put(gpuvm); > + drm_gem_object_put(obj); > + } > +} > +EXPORT_SYMBOL_GPL(drm_gpuvm_bo_deferred_cleanup); > + > static struct drm_gpuvm_bo * > __drm_gpuvm_bo_find(struct drm_gpuvm *gpuvm, > struct drm_gem_object *obj) > @@ -1948,6 +2098,40 @@ drm_gpuva_unlink(struct drm_gpuva *va) > } > EXPORT_SYMBOL_GPL(drm_gpuva_unlink); > > +/** > + * drm_gpuva_unlink_defer() - unlink a &drm_gpuva with deferred vm_bo cleanup > + * @va: the &drm_gpuva to unlink > + * > + * Similar to drm_gpuva_unlink(), but uses drm_gpuvm_bo_put_deferred() and takes > + * the lock for the caller. > + */ > +void > +drm_gpuva_unlink_defer(struct drm_gpuva *va) > +{ > + struct drm_gem_object *obj = va->gem.obj; > + struct drm_gpuvm_bo *vm_bo = va->vm_bo; > + bool should_defer_bo; > + > + if (unlikely(!obj)) > + return; > + > + drm_WARN_ON(vm_bo->vm->drm, !drm_gpuvm_immediate_mode(vm_bo->vm)); > + > + mutex_lock(&obj->gpuva.lock); > + list_del_init(&va->gem.entry); > + > + /* > + * This is drm_gpuvm_bo_put_deferred() except we already hold the mutex. > + */ > + should_defer_bo = kref_put(&vm_bo->kref, drm_gpuvm_bo_defer_free_locked); > + mutex_unlock(&obj->gpuva.lock); > + if (should_defer_bo) > + drm_gpuvm_bo_defer_free_unlocked(vm_bo); > + > + va->vm_bo = NULL; > +} > +EXPORT_SYMBOL_GPL(drm_gpuva_unlink_defer); > + > /** > * drm_gpuva_find_first() - find the first &drm_gpuva in the given range > * @gpuvm: the &drm_gpuvm to search in > diff --git a/include/drm/drm_gpuvm.h b/include/drm/drm_gpuvm.h > index 8890ded1d90752a2acbb564f697aa5ab03b5d052..81cc7672cf2d5362c637abfa2a75471e5274ed08 100644 > --- a/include/drm/drm_gpuvm.h > +++ b/include/drm/drm_gpuvm.h > @@ -27,6 +27,7 @@ > > #include <linux/dma-resv.h> > #include <linux/list.h> > +#include <linux/llist.h> > #include <linux/rbtree.h> > #include <linux/types.h> > > @@ -152,6 +153,7 @@ void drm_gpuva_remove(struct drm_gpuva *va); > > void drm_gpuva_link(struct drm_gpuva *va, struct drm_gpuvm_bo *vm_bo); > void drm_gpuva_unlink(struct drm_gpuva *va); > +void drm_gpuva_unlink_defer(struct drm_gpuva *va); > > struct drm_gpuva *drm_gpuva_find(struct drm_gpuvm *gpuvm, > u64 addr, u64 range); > @@ -331,6 +333,11 @@ struct drm_gpuvm { > */ > spinlock_t lock; > } evict; > + > + /** > + * @bo_defer: structure holding vm_bos that need to be destroyed > + */ > + struct llist_head bo_defer; > }; > > void drm_gpuvm_init(struct drm_gpuvm *gpuvm, const char *name, > @@ -714,6 +721,12 @@ struct drm_gpuvm_bo { > * &drm_gpuvms evict list. > */ > struct list_head evict; > + > + /** > + * @list.entry.bo_defer: List entry to attach to > + * the &drm_gpuvms bo_defer list. > + */ > + struct llist_node bo_defer; > } entry; > } list; > }; > @@ -746,6 +759,9 @@ drm_gpuvm_bo_get(struct drm_gpuvm_bo *vm_bo) > > bool drm_gpuvm_bo_put(struct drm_gpuvm_bo *vm_bo); > > +bool drm_gpuvm_bo_put_deferred(struct drm_gpuvm_bo *vm_bo); > +void drm_gpuvm_bo_deferred_cleanup(struct drm_gpuvm *gpuvm); > + > struct drm_gpuvm_bo * > drm_gpuvm_bo_find(struct drm_gpuvm *gpuvm, > struct drm_gem_object *obj); > ^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v3 1/2] drm/gpuvm: add deferred vm_bo cleanup 2025-10-01 11:27 ` Boris Brezillon @ 2025-10-01 11:45 ` Alice Ryhl 2025-10-01 12:04 ` Boris Brezillon 0 siblings, 1 reply; 18+ messages in thread From: Alice Ryhl @ 2025-10-01 11:45 UTC (permalink / raw) To: Boris Brezillon Cc: Danilo Krummrich, Matthew Brost, Thomas Hellström, Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie, Simona Vetter, Steven Price, Daniel Almeida, Liviu Dudau, dri-devel, linux-kernel, rust-for-linux On Wed, Oct 1, 2025 at 1:27 PM Boris Brezillon <boris.brezillon@collabora.com> wrote: > > On Wed, 01 Oct 2025 10:41:36 +0000 > Alice Ryhl <aliceryhl@google.com> wrote: > > > When using GPUVM in immediate mode, it is necessary to call > > drm_gpuvm_unlink() from the fence signalling critical path. However, > > unlink may call drm_gpuvm_bo_put(), which causes some challenges: > > > > 1. drm_gpuvm_bo_put() often requires you to take resv locks, which you > > can't do from the fence signalling critical path. > > 2. drm_gpuvm_bo_put() calls drm_gem_object_put(), which is often going > > to be unsafe to call from the fence signalling critical path. > > > > To solve these issues, add a deferred version of drm_gpuvm_unlink() that > > adds the vm_bo to a deferred cleanup list, and then clean it up later. > > > > The new methods take the GEMs GPUVA lock internally rather than letting > > the caller do it because it also needs to perform an operation after > > releasing the mutex again. This is to prevent freeing the GEM while > > holding the mutex (more info as comments in the patch). This means that > > the new methods can only be used with DRM_GPUVM_IMMEDIATE_MODE. > > > > Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com> > > Signed-off-by: Alice Ryhl <aliceryhl@google.com> > > +/* > > + * Must be called with GEM mutex held. After releasing GEM mutex, > > + * drm_gpuvm_bo_defer_free_unlocked() must be called. > > + */ > > +static void > > +drm_gpuvm_bo_defer_free_locked(struct kref *kref) > > +{ > > + struct drm_gpuvm_bo *vm_bo = container_of(kref, struct drm_gpuvm_bo, > > + kref); > > + struct drm_gpuvm *gpuvm = vm_bo->vm; > > + > > + if (!drm_gpuvm_resv_protected(gpuvm)) { > > + drm_gpuvm_bo_list_del(vm_bo, extobj, true); > > + drm_gpuvm_bo_list_del(vm_bo, evict, true); > > + } > > + > > + list_del(&vm_bo->list.entry.gem); > > +} > > + > > +/* > > + * GEM mutex must not be held. Called after drm_gpuvm_bo_defer_free_locked(). > > + */ > > +static void > > +drm_gpuvm_bo_defer_free_unlocked(struct drm_gpuvm_bo *vm_bo) > > +{ > > + struct drm_gpuvm *gpuvm = vm_bo->vm; > > + > > + llist_add(&vm_bo->list.entry.bo_defer, &gpuvm->bo_defer); > > Could we simply move this line to drm_gpuvm_bo_defer_free_locked()? > I might be missing something, but I don't really see a reason to > have it exposed as a separate operation. No, if drm_gpuvm_bo_deferred_cleanup() is called in parallel (e.g. from a workqueue as we discussed), then this can lead to kfreeing the GEM while we hold the mutex. We must not add the vm_bo until it's safe to kfree the GEM. See the comment on drm_gpuvm_bo_defer_free_unlocked() below. > > +} > > + > > +static void > > +drm_gpuvm_bo_defer_free(struct kref *kref) > > +{ > > + struct drm_gpuvm_bo *vm_bo = container_of(kref, struct drm_gpuvm_bo, > > + kref); > > + > > + mutex_lock(&vm_bo->obj->gpuva.lock); > > + drm_gpuvm_bo_defer_free_locked(kref); > > + mutex_unlock(&vm_bo->obj->gpuva.lock); > > + > > + /* > > + * It's important that the GEM stays alive for the duration in which we > > + * hold the mutex, but the instant we add the vm_bo to bo_defer, > > + * another thread might call drm_gpuvm_bo_deferred_cleanup() and put > > + * the GEM. Therefore, to avoid kfreeing a mutex we are holding, we add > > + * the vm_bo to bo_defer *after* releasing the GEM's mutex. > > + */ > > + drm_gpuvm_bo_defer_free_unlocked(vm_bo); > > +} Alice ^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v3 1/2] drm/gpuvm: add deferred vm_bo cleanup 2025-10-01 11:45 ` Alice Ryhl @ 2025-10-01 12:04 ` Boris Brezillon 2025-10-01 12:13 ` Boris Brezillon 0 siblings, 1 reply; 18+ messages in thread From: Boris Brezillon @ 2025-10-01 12:04 UTC (permalink / raw) To: Alice Ryhl Cc: Danilo Krummrich, Matthew Brost, Thomas Hellström, Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie, Simona Vetter, Steven Price, Daniel Almeida, Liviu Dudau, dri-devel, linux-kernel, rust-for-linux On Wed, 1 Oct 2025 13:45:36 +0200 Alice Ryhl <aliceryhl@google.com> wrote: > On Wed, Oct 1, 2025 at 1:27 PM Boris Brezillon > <boris.brezillon@collabora.com> wrote: > > > > On Wed, 01 Oct 2025 10:41:36 +0000 > > Alice Ryhl <aliceryhl@google.com> wrote: > > > > > When using GPUVM in immediate mode, it is necessary to call > > > drm_gpuvm_unlink() from the fence signalling critical path. However, > > > unlink may call drm_gpuvm_bo_put(), which causes some challenges: > > > > > > 1. drm_gpuvm_bo_put() often requires you to take resv locks, which you > > > can't do from the fence signalling critical path. > > > 2. drm_gpuvm_bo_put() calls drm_gem_object_put(), which is often going > > > to be unsafe to call from the fence signalling critical path. > > > > > > To solve these issues, add a deferred version of drm_gpuvm_unlink() that > > > adds the vm_bo to a deferred cleanup list, and then clean it up later. > > > > > > The new methods take the GEMs GPUVA lock internally rather than letting > > > the caller do it because it also needs to perform an operation after > > > releasing the mutex again. This is to prevent freeing the GEM while > > > holding the mutex (more info as comments in the patch). This means that > > > the new methods can only be used with DRM_GPUVM_IMMEDIATE_MODE. > > > > > > Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com> > > > Signed-off-by: Alice Ryhl <aliceryhl@google.com> > > > > +/* > > > + * Must be called with GEM mutex held. After releasing GEM mutex, > > > + * drm_gpuvm_bo_defer_free_unlocked() must be called. > > > + */ > > > +static void > > > +drm_gpuvm_bo_defer_free_locked(struct kref *kref) > > > +{ > > > + struct drm_gpuvm_bo *vm_bo = container_of(kref, struct drm_gpuvm_bo, > > > + kref); > > > + struct drm_gpuvm *gpuvm = vm_bo->vm; > > > + > > > + if (!drm_gpuvm_resv_protected(gpuvm)) { > > > + drm_gpuvm_bo_list_del(vm_bo, extobj, true); > > > + drm_gpuvm_bo_list_del(vm_bo, evict, true); > > > + } > > > + > > > + list_del(&vm_bo->list.entry.gem); > > > +} > > > + > > > +/* > > > + * GEM mutex must not be held. Called after drm_gpuvm_bo_defer_free_locked(). > > > + */ > > > +static void > > > +drm_gpuvm_bo_defer_free_unlocked(struct drm_gpuvm_bo *vm_bo) > > > +{ > > > + struct drm_gpuvm *gpuvm = vm_bo->vm; > > > + > > > + llist_add(&vm_bo->list.entry.bo_defer, &gpuvm->bo_defer); > > > > Could we simply move this line to drm_gpuvm_bo_defer_free_locked()? > > I might be missing something, but I don't really see a reason to > > have it exposed as a separate operation. > > No, if drm_gpuvm_bo_deferred_cleanup() is called in parallel (e.g. > from a workqueue as we discussed), then this can lead to kfreeing the > GEM while we hold the mutex. We must not add the vm_bo until it's safe > to kfree the GEM. See the comment on > drm_gpuvm_bo_defer_free_unlocked() below. Uh, right, I forgot that the lock was embedded in the BO, which we're releasing a ref on in the cleanup path. ^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v3 1/2] drm/gpuvm: add deferred vm_bo cleanup 2025-10-01 12:04 ` Boris Brezillon @ 2025-10-01 12:13 ` Boris Brezillon 2025-10-01 12:22 ` Alice Ryhl 0 siblings, 1 reply; 18+ messages in thread From: Boris Brezillon @ 2025-10-01 12:13 UTC (permalink / raw) To: Alice Ryhl Cc: Danilo Krummrich, Matthew Brost, Thomas Hellström, Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie, Simona Vetter, Steven Price, Daniel Almeida, Liviu Dudau, dri-devel, linux-kernel, rust-for-linux On Wed, 1 Oct 2025 14:04:18 +0200 Boris Brezillon <boris.brezillon@collabora.com> wrote: > On Wed, 1 Oct 2025 13:45:36 +0200 > Alice Ryhl <aliceryhl@google.com> wrote: > > > On Wed, Oct 1, 2025 at 1:27 PM Boris Brezillon > > <boris.brezillon@collabora.com> wrote: > > > > > > On Wed, 01 Oct 2025 10:41:36 +0000 > > > Alice Ryhl <aliceryhl@google.com> wrote: > > > > > > > When using GPUVM in immediate mode, it is necessary to call > > > > drm_gpuvm_unlink() from the fence signalling critical path. However, > > > > unlink may call drm_gpuvm_bo_put(), which causes some challenges: > > > > > > > > 1. drm_gpuvm_bo_put() often requires you to take resv locks, which you > > > > can't do from the fence signalling critical path. > > > > 2. drm_gpuvm_bo_put() calls drm_gem_object_put(), which is often going > > > > to be unsafe to call from the fence signalling critical path. > > > > > > > > To solve these issues, add a deferred version of drm_gpuvm_unlink() that > > > > adds the vm_bo to a deferred cleanup list, and then clean it up later. > > > > > > > > The new methods take the GEMs GPUVA lock internally rather than letting > > > > the caller do it because it also needs to perform an operation after > > > > releasing the mutex again. This is to prevent freeing the GEM while > > > > holding the mutex (more info as comments in the patch). This means that > > > > the new methods can only be used with DRM_GPUVM_IMMEDIATE_MODE. > > > > > > > > Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com> > > > > Signed-off-by: Alice Ryhl <aliceryhl@google.com> > > > > > > +/* > > > > + * Must be called with GEM mutex held. After releasing GEM mutex, > > > > + * drm_gpuvm_bo_defer_free_unlocked() must be called. > > > > + */ > > > > +static void > > > > +drm_gpuvm_bo_defer_free_locked(struct kref *kref) > > > > +{ > > > > + struct drm_gpuvm_bo *vm_bo = container_of(kref, struct drm_gpuvm_bo, > > > > + kref); > > > > + struct drm_gpuvm *gpuvm = vm_bo->vm; > > > > + > > > > + if (!drm_gpuvm_resv_protected(gpuvm)) { > > > > + drm_gpuvm_bo_list_del(vm_bo, extobj, true); > > > > + drm_gpuvm_bo_list_del(vm_bo, evict, true); > > > > + } > > > > + > > > > + list_del(&vm_bo->list.entry.gem); > > > > +} > > > > + > > > > +/* > > > > + * GEM mutex must not be held. Called after drm_gpuvm_bo_defer_free_locked(). > > > > + */ > > > > +static void > > > > +drm_gpuvm_bo_defer_free_unlocked(struct drm_gpuvm_bo *vm_bo) > > > > +{ > > > > + struct drm_gpuvm *gpuvm = vm_bo->vm; > > > > + > > > > + llist_add(&vm_bo->list.entry.bo_defer, &gpuvm->bo_defer); > > > > > > Could we simply move this line to drm_gpuvm_bo_defer_free_locked()? > > > I might be missing something, but I don't really see a reason to > > > have it exposed as a separate operation. > > > > No, if drm_gpuvm_bo_deferred_cleanup() is called in parallel (e.g. > > from a workqueue as we discussed), then this can lead to kfreeing the > > GEM while we hold the mutex. We must not add the vm_bo until it's safe > > to kfree the GEM. See the comment on > > drm_gpuvm_bo_defer_free_unlocked() below. > > Uh, right, I forgot that the lock was embedded in the BO, which we're > releasing a ref on in the cleanup path. Would be good to document the race in the comment saying that gpuva.lock shouldn't be held though. ^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v3 1/2] drm/gpuvm: add deferred vm_bo cleanup 2025-10-01 12:13 ` Boris Brezillon @ 2025-10-01 12:22 ` Alice Ryhl 2025-10-01 13:01 ` Boris Brezillon 0 siblings, 1 reply; 18+ messages in thread From: Alice Ryhl @ 2025-10-01 12:22 UTC (permalink / raw) To: Boris Brezillon Cc: Danilo Krummrich, Matthew Brost, Thomas Hellström, Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie, Simona Vetter, Steven Price, Daniel Almeida, Liviu Dudau, dri-devel, linux-kernel, rust-for-linux On Wed, Oct 1, 2025 at 2:13 PM Boris Brezillon <boris.brezillon@collabora.com> wrote: > > On Wed, 1 Oct 2025 14:04:18 +0200 > Boris Brezillon <boris.brezillon@collabora.com> wrote: > > > On Wed, 1 Oct 2025 13:45:36 +0200 > > Alice Ryhl <aliceryhl@google.com> wrote: > > > > > On Wed, Oct 1, 2025 at 1:27 PM Boris Brezillon > > > <boris.brezillon@collabora.com> wrote: > > > > > > > > On Wed, 01 Oct 2025 10:41:36 +0000 > > > > Alice Ryhl <aliceryhl@google.com> wrote: > > > > > > > > > When using GPUVM in immediate mode, it is necessary to call > > > > > drm_gpuvm_unlink() from the fence signalling critical path. However, > > > > > unlink may call drm_gpuvm_bo_put(), which causes some challenges: > > > > > > > > > > 1. drm_gpuvm_bo_put() often requires you to take resv locks, which you > > > > > can't do from the fence signalling critical path. > > > > > 2. drm_gpuvm_bo_put() calls drm_gem_object_put(), which is often going > > > > > to be unsafe to call from the fence signalling critical path. > > > > > > > > > > To solve these issues, add a deferred version of drm_gpuvm_unlink() that > > > > > adds the vm_bo to a deferred cleanup list, and then clean it up later. > > > > > > > > > > The new methods take the GEMs GPUVA lock internally rather than letting > > > > > the caller do it because it also needs to perform an operation after > > > > > releasing the mutex again. This is to prevent freeing the GEM while > > > > > holding the mutex (more info as comments in the patch). This means that > > > > > the new methods can only be used with DRM_GPUVM_IMMEDIATE_MODE. > > > > > > > > > > Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com> > > > > > Signed-off-by: Alice Ryhl <aliceryhl@google.com> > > > > > > > > +/* > > > > > + * Must be called with GEM mutex held. After releasing GEM mutex, > > > > > + * drm_gpuvm_bo_defer_free_unlocked() must be called. > > > > > + */ > > > > > +static void > > > > > +drm_gpuvm_bo_defer_free_locked(struct kref *kref) > > > > > +{ > > > > > + struct drm_gpuvm_bo *vm_bo = container_of(kref, struct drm_gpuvm_bo, > > > > > + kref); > > > > > + struct drm_gpuvm *gpuvm = vm_bo->vm; > > > > > + > > > > > + if (!drm_gpuvm_resv_protected(gpuvm)) { > > > > > + drm_gpuvm_bo_list_del(vm_bo, extobj, true); > > > > > + drm_gpuvm_bo_list_del(vm_bo, evict, true); > > > > > + } > > > > > + > > > > > + list_del(&vm_bo->list.entry.gem); > > > > > +} > > > > > + > > > > > +/* > > > > > + * GEM mutex must not be held. Called after drm_gpuvm_bo_defer_free_locked(). > > > > > + */ > > > > > +static void > > > > > +drm_gpuvm_bo_defer_free_unlocked(struct drm_gpuvm_bo *vm_bo) > > > > > +{ > > > > > + struct drm_gpuvm *gpuvm = vm_bo->vm; > > > > > + > > > > > + llist_add(&vm_bo->list.entry.bo_defer, &gpuvm->bo_defer); > > > > > > > > Could we simply move this line to drm_gpuvm_bo_defer_free_locked()? > > > > I might be missing something, but I don't really see a reason to > > > > have it exposed as a separate operation. > > > > > > No, if drm_gpuvm_bo_deferred_cleanup() is called in parallel (e.g. > > > from a workqueue as we discussed), then this can lead to kfreeing the > > > GEM while we hold the mutex. We must not add the vm_bo until it's safe > > > to kfree the GEM. See the comment on > > > drm_gpuvm_bo_defer_free_unlocked() below. > > > > Uh, right, I forgot that the lock was embedded in the BO, which we're > > releasing a ref on in the cleanup path. > > Would be good to document the race in the comment saying that > gpuva.lock shouldn't be held though. I can move the comment in drm_gpuvm_bo_defer_free() to the function comment. Alice ^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v3 1/2] drm/gpuvm: add deferred vm_bo cleanup 2025-10-01 12:22 ` Alice Ryhl @ 2025-10-01 13:01 ` Boris Brezillon 0 siblings, 0 replies; 18+ messages in thread From: Boris Brezillon @ 2025-10-01 13:01 UTC (permalink / raw) To: Alice Ryhl Cc: Danilo Krummrich, Matthew Brost, Thomas Hellström, Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie, Simona Vetter, Steven Price, Daniel Almeida, Liviu Dudau, dri-devel, linux-kernel, rust-for-linux On Wed, 1 Oct 2025 14:22:06 +0200 Alice Ryhl <aliceryhl@google.com> wrote: > On Wed, Oct 1, 2025 at 2:13 PM Boris Brezillon > <boris.brezillon@collabora.com> wrote: > > > > On Wed, 1 Oct 2025 14:04:18 +0200 > > Boris Brezillon <boris.brezillon@collabora.com> wrote: > > > > > On Wed, 1 Oct 2025 13:45:36 +0200 > > > Alice Ryhl <aliceryhl@google.com> wrote: > > > > > > > On Wed, Oct 1, 2025 at 1:27 PM Boris Brezillon > > > > <boris.brezillon@collabora.com> wrote: > > > > > > > > > > On Wed, 01 Oct 2025 10:41:36 +0000 > > > > > Alice Ryhl <aliceryhl@google.com> wrote: > > > > > > > > > > > When using GPUVM in immediate mode, it is necessary to call > > > > > > drm_gpuvm_unlink() from the fence signalling critical path. However, > > > > > > unlink may call drm_gpuvm_bo_put(), which causes some challenges: > > > > > > > > > > > > 1. drm_gpuvm_bo_put() often requires you to take resv locks, which you > > > > > > can't do from the fence signalling critical path. > > > > > > 2. drm_gpuvm_bo_put() calls drm_gem_object_put(), which is often going > > > > > > to be unsafe to call from the fence signalling critical path. > > > > > > > > > > > > To solve these issues, add a deferred version of drm_gpuvm_unlink() that > > > > > > adds the vm_bo to a deferred cleanup list, and then clean it up later. > > > > > > > > > > > > The new methods take the GEMs GPUVA lock internally rather than letting > > > > > > the caller do it because it also needs to perform an operation after > > > > > > releasing the mutex again. This is to prevent freeing the GEM while > > > > > > holding the mutex (more info as comments in the patch). This means that > > > > > > the new methods can only be used with DRM_GPUVM_IMMEDIATE_MODE. > > > > > > > > > > > > Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com> > > > > > > Signed-off-by: Alice Ryhl <aliceryhl@google.com> > > > > > > > > > > +/* > > > > > > + * Must be called with GEM mutex held. After releasing GEM mutex, > > > > > > + * drm_gpuvm_bo_defer_free_unlocked() must be called. > > > > > > + */ > > > > > > +static void > > > > > > +drm_gpuvm_bo_defer_free_locked(struct kref *kref) > > > > > > +{ > > > > > > + struct drm_gpuvm_bo *vm_bo = container_of(kref, struct drm_gpuvm_bo, > > > > > > + kref); > > > > > > + struct drm_gpuvm *gpuvm = vm_bo->vm; > > > > > > + > > > > > > + if (!drm_gpuvm_resv_protected(gpuvm)) { > > > > > > + drm_gpuvm_bo_list_del(vm_bo, extobj, true); > > > > > > + drm_gpuvm_bo_list_del(vm_bo, evict, true); > > > > > > + } > > > > > > + > > > > > > + list_del(&vm_bo->list.entry.gem); > > > > > > +} > > > > > > + > > > > > > +/* > > > > > > + * GEM mutex must not be held. Called after drm_gpuvm_bo_defer_free_locked(). > > > > > > + */ > > > > > > +static void > > > > > > +drm_gpuvm_bo_defer_free_unlocked(struct drm_gpuvm_bo *vm_bo) > > > > > > +{ > > > > > > + struct drm_gpuvm *gpuvm = vm_bo->vm; > > > > > > + > > > > > > + llist_add(&vm_bo->list.entry.bo_defer, &gpuvm->bo_defer); > > > > > > > > > > Could we simply move this line to drm_gpuvm_bo_defer_free_locked()? > > > > > I might be missing something, but I don't really see a reason to > > > > > have it exposed as a separate operation. > > > > > > > > No, if drm_gpuvm_bo_deferred_cleanup() is called in parallel (e.g. > > > > from a workqueue as we discussed), then this can lead to kfreeing the > > > > GEM while we hold the mutex. We must not add the vm_bo until it's safe > > > > to kfree the GEM. See the comment on > > > > drm_gpuvm_bo_defer_free_unlocked() below. > > > > > > Uh, right, I forgot that the lock was embedded in the BO, which we're > > > releasing a ref on in the cleanup path. > > > > Would be good to document the race in the comment saying that > > gpuva.lock shouldn't be held though. > > I can move the comment in drm_gpuvm_bo_defer_free() to the function comment. That, or you refer to the function where it's documented in the comment. ^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v3 1/2] drm/gpuvm: add deferred vm_bo cleanup 2025-10-01 10:41 ` [PATCH v3 1/2] drm/gpuvm: add deferred vm_bo cleanup Alice Ryhl 2025-10-01 11:27 ` Boris Brezillon @ 2025-10-01 14:01 ` Danilo Krummrich 2025-10-01 14:42 ` Alice Ryhl 2025-10-06 11:30 ` Alice Ryhl 2 siblings, 1 reply; 18+ messages in thread From: Danilo Krummrich @ 2025-10-01 14:01 UTC (permalink / raw) To: Alice Ryhl Cc: Matthew Brost, Thomas Hellström, Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie, Simona Vetter, Boris Brezillon, Steven Price, Daniel Almeida, Liviu Dudau, dri-devel, linux-kernel, rust-for-linux On Wed Oct 1, 2025 at 12:41 PM CEST, Alice Ryhl wrote: > +/* > + * Must be called with GEM mutex held. After releasing GEM mutex, > + * drm_gpuvm_bo_defer_free_unlocked() must be called. > + */ > +static void > +drm_gpuvm_bo_defer_free_locked(struct kref *kref) > +{ > + struct drm_gpuvm_bo *vm_bo = container_of(kref, struct drm_gpuvm_bo, > + kref); > + struct drm_gpuvm *gpuvm = vm_bo->vm; > + > + if (!drm_gpuvm_resv_protected(gpuvm)) { > + drm_gpuvm_bo_list_del(vm_bo, extobj, true); > + drm_gpuvm_bo_list_del(vm_bo, evict, true); > + } > + > + list_del(&vm_bo->list.entry.gem); > +} > + > +/* > + * GEM mutex must not be held. Called after drm_gpuvm_bo_defer_free_locked(). > + */ > +static void > +drm_gpuvm_bo_defer_free_unlocked(struct drm_gpuvm_bo *vm_bo) > +{ > + struct drm_gpuvm *gpuvm = vm_bo->vm; > + > + llist_add(&vm_bo->list.entry.bo_defer, &gpuvm->bo_defer); > +} > + > +static void > +drm_gpuvm_bo_defer_free(struct kref *kref) > +{ > + struct drm_gpuvm_bo *vm_bo = container_of(kref, struct drm_gpuvm_bo, > + kref); > + > + mutex_lock(&vm_bo->obj->gpuva.lock); > + drm_gpuvm_bo_defer_free_locked(kref); > + mutex_unlock(&vm_bo->obj->gpuva.lock); > + > + /* > + * It's important that the GEM stays alive for the duration in which we > + * hold the mutex, but the instant we add the vm_bo to bo_defer, > + * another thread might call drm_gpuvm_bo_deferred_cleanup() and put > + * the GEM. Therefore, to avoid kfreeing a mutex we are holding, we add > + * the vm_bo to bo_defer *after* releasing the GEM's mutex. > + */ > + drm_gpuvm_bo_defer_free_unlocked(vm_bo); > +} So, you're splitting drm_gpuvm_bo_defer_free() into two functions, one doing the work that is required to be called with the gpuva lock held and one that does the work that does not require a lock, which makes perfect sense. However, the naming chosen for the two functions, i.e. drm_gpuvm_bo_defer_free_unlocked() and drm_gpuvm_bo_defer_free_locked() is confusing: What you mean semantically mean is "do part 1 with lock held" and "do part 2 without lock held", but the the chosen names suggest that both functions are identical, with the only difference that one takes the lock internally and the other one requires the caller to take the lock. It's probably better to name them after what they do and not what they're part of. If you prefer the latter, that's fine with me too, but please choose a name that makes this circumstance obvious. With that addressed, Acked-by: Danilo Krummrich <dakr@kernel.org> ^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v3 1/2] drm/gpuvm: add deferred vm_bo cleanup 2025-10-01 14:01 ` Danilo Krummrich @ 2025-10-01 14:42 ` Alice Ryhl 2025-10-01 15:13 ` Boris Brezillon 0 siblings, 1 reply; 18+ messages in thread From: Alice Ryhl @ 2025-10-01 14:42 UTC (permalink / raw) To: Danilo Krummrich Cc: Matthew Brost, Thomas Hellström, Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie, Simona Vetter, Boris Brezillon, Steven Price, Daniel Almeida, Liviu Dudau, dri-devel, linux-kernel, rust-for-linux On Wed, Oct 1, 2025 at 4:01 PM Danilo Krummrich <dakr@kernel.org> wrote: > > On Wed Oct 1, 2025 at 12:41 PM CEST, Alice Ryhl wrote: > > +/* > > + * Must be called with GEM mutex held. After releasing GEM mutex, > > + * drm_gpuvm_bo_defer_free_unlocked() must be called. > > + */ > > +static void > > +drm_gpuvm_bo_defer_free_locked(struct kref *kref) > > +{ > > + struct drm_gpuvm_bo *vm_bo = container_of(kref, struct drm_gpuvm_bo, > > + kref); > > + struct drm_gpuvm *gpuvm = vm_bo->vm; > > + > > + if (!drm_gpuvm_resv_protected(gpuvm)) { > > + drm_gpuvm_bo_list_del(vm_bo, extobj, true); > > + drm_gpuvm_bo_list_del(vm_bo, evict, true); > > + } > > + > > + list_del(&vm_bo->list.entry.gem); > > +} > > + > > +/* > > + * GEM mutex must not be held. Called after drm_gpuvm_bo_defer_free_locked(). > > + */ > > +static void > > +drm_gpuvm_bo_defer_free_unlocked(struct drm_gpuvm_bo *vm_bo) > > +{ > > + struct drm_gpuvm *gpuvm = vm_bo->vm; > > + > > + llist_add(&vm_bo->list.entry.bo_defer, &gpuvm->bo_defer); > > +} > > + > > +static void > > +drm_gpuvm_bo_defer_free(struct kref *kref) > > +{ > > + struct drm_gpuvm_bo *vm_bo = container_of(kref, struct drm_gpuvm_bo, > > + kref); > > + > > + mutex_lock(&vm_bo->obj->gpuva.lock); > > + drm_gpuvm_bo_defer_free_locked(kref); > > + mutex_unlock(&vm_bo->obj->gpuva.lock); > > + > > + /* > > + * It's important that the GEM stays alive for the duration in which we > > + * hold the mutex, but the instant we add the vm_bo to bo_defer, > > + * another thread might call drm_gpuvm_bo_deferred_cleanup() and put > > + * the GEM. Therefore, to avoid kfreeing a mutex we are holding, we add > > + * the vm_bo to bo_defer *after* releasing the GEM's mutex. > > + */ > > + drm_gpuvm_bo_defer_free_unlocked(vm_bo); > > +} > > So, you're splitting drm_gpuvm_bo_defer_free() into two functions, one doing the > work that is required to be called with the gpuva lock held and one that does > the work that does not require a lock, which makes perfect sense. > > However, the naming chosen for the two functions, i.e. > drm_gpuvm_bo_defer_free_unlocked() and drm_gpuvm_bo_defer_free_locked() is > confusing: > > What you mean semantically mean is "do part 1 with lock held" and "do part 2 > without lock held", but the the chosen names suggest that both functions are > identical, with the only difference that one takes the lock internally and the > other one requires the caller to take the lock. > > It's probably better to name them after what they do and not what they're part > of. If you prefer the latter, that's fine with me too, but please choose a name > that makes this circumstance obvious. Fair point. Do you have naming suggestions? Otherwise I can name them drm_gpuvm_bo_defer_free_part1() and drm_gpuvm_bo_defer_free_part2(). :) Alice ^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v3 1/2] drm/gpuvm: add deferred vm_bo cleanup 2025-10-01 14:42 ` Alice Ryhl @ 2025-10-01 15:13 ` Boris Brezillon 2025-10-06 11:31 ` Alice Ryhl 0 siblings, 1 reply; 18+ messages in thread From: Boris Brezillon @ 2025-10-01 15:13 UTC (permalink / raw) To: Alice Ryhl Cc: Danilo Krummrich, Matthew Brost, Thomas Hellström, Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie, Simona Vetter, Steven Price, Daniel Almeida, Liviu Dudau, dri-devel, linux-kernel, rust-for-linux On Wed, 1 Oct 2025 16:42:35 +0200 Alice Ryhl <aliceryhl@google.com> wrote: > On Wed, Oct 1, 2025 at 4:01 PM Danilo Krummrich <dakr@kernel.org> wrote: > > > > On Wed Oct 1, 2025 at 12:41 PM CEST, Alice Ryhl wrote: > > > +/* > > > + * Must be called with GEM mutex held. After releasing GEM mutex, > > > + * drm_gpuvm_bo_defer_free_unlocked() must be called. > > > + */ > > > +static void > > > +drm_gpuvm_bo_defer_free_locked(struct kref *kref) > > > +{ > > > + struct drm_gpuvm_bo *vm_bo = container_of(kref, struct drm_gpuvm_bo, > > > + kref); > > > + struct drm_gpuvm *gpuvm = vm_bo->vm; > > > + > > > + if (!drm_gpuvm_resv_protected(gpuvm)) { > > > + drm_gpuvm_bo_list_del(vm_bo, extobj, true); > > > + drm_gpuvm_bo_list_del(vm_bo, evict, true); > > > + } > > > + > > > + list_del(&vm_bo->list.entry.gem); > > > +} > > > + > > > +/* > > > + * GEM mutex must not be held. Called after drm_gpuvm_bo_defer_free_locked(). > > > + */ > > > +static void > > > +drm_gpuvm_bo_defer_free_unlocked(struct drm_gpuvm_bo *vm_bo) > > > +{ > > > + struct drm_gpuvm *gpuvm = vm_bo->vm; > > > + > > > + llist_add(&vm_bo->list.entry.bo_defer, &gpuvm->bo_defer); > > > +} > > > + > > > +static void > > > +drm_gpuvm_bo_defer_free(struct kref *kref) > > > +{ > > > + struct drm_gpuvm_bo *vm_bo = container_of(kref, struct drm_gpuvm_bo, > > > + kref); > > > + > > > + mutex_lock(&vm_bo->obj->gpuva.lock); > > > + drm_gpuvm_bo_defer_free_locked(kref); > > > + mutex_unlock(&vm_bo->obj->gpuva.lock); > > > + > > > + /* > > > + * It's important that the GEM stays alive for the duration in which we > > > + * hold the mutex, but the instant we add the vm_bo to bo_defer, > > > + * another thread might call drm_gpuvm_bo_deferred_cleanup() and put > > > + * the GEM. Therefore, to avoid kfreeing a mutex we are holding, we add > > > + * the vm_bo to bo_defer *after* releasing the GEM's mutex. > > > + */ > > > + drm_gpuvm_bo_defer_free_unlocked(vm_bo); > > > +} > > > > So, you're splitting drm_gpuvm_bo_defer_free() into two functions, one doing the > > work that is required to be called with the gpuva lock held and one that does > > the work that does not require a lock, which makes perfect sense. > > > > However, the naming chosen for the two functions, i.e. > > drm_gpuvm_bo_defer_free_unlocked() and drm_gpuvm_bo_defer_free_locked() is > > confusing: > > > > What you mean semantically mean is "do part 1 with lock held" and "do part 2 > > without lock held", but the the chosen names suggest that both functions are > > identical, with the only difference that one takes the lock internally and the > > other one requires the caller to take the lock. > > > > It's probably better to name them after what they do and not what they're part > > of. If you prefer the latter, that's fine with me too, but please choose a name > > that makes this circumstance obvious. > > Fair point. Do you have naming suggestions? Otherwise I can name them > drm_gpuvm_bo_defer_free_part1() and drm_gpuvm_bo_defer_free_part2(). > :) drm_gpuvm_bo_free_deferral_extract_locked() and drm_gpuvm_bo_free_deferral_enqueue()? Definitely not short names though. ^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v3 1/2] drm/gpuvm: add deferred vm_bo cleanup 2025-10-01 15:13 ` Boris Brezillon @ 2025-10-06 11:31 ` Alice Ryhl 2025-10-06 11:41 ` Boris Brezillon 0 siblings, 1 reply; 18+ messages in thread From: Alice Ryhl @ 2025-10-06 11:31 UTC (permalink / raw) To: Boris Brezillon Cc: Danilo Krummrich, Matthew Brost, Thomas Hellström, Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie, Simona Vetter, Steven Price, Daniel Almeida, Liviu Dudau, dri-devel, linux-kernel, rust-for-linux On Wed, Oct 1, 2025 at 5:13 PM Boris Brezillon <boris.brezillon@collabora.com> wrote: > > On Wed, 1 Oct 2025 16:42:35 +0200 > Alice Ryhl <aliceryhl@google.com> wrote: > > > On Wed, Oct 1, 2025 at 4:01 PM Danilo Krummrich <dakr@kernel.org> wrote: > > > > > > On Wed Oct 1, 2025 at 12:41 PM CEST, Alice Ryhl wrote: > > > > +/* > > > > + * Must be called with GEM mutex held. After releasing GEM mutex, > > > > + * drm_gpuvm_bo_defer_free_unlocked() must be called. > > > > + */ > > > > +static void > > > > +drm_gpuvm_bo_defer_free_locked(struct kref *kref) > > > > +{ > > > > + struct drm_gpuvm_bo *vm_bo = container_of(kref, struct drm_gpuvm_bo, > > > > + kref); > > > > + struct drm_gpuvm *gpuvm = vm_bo->vm; > > > > + > > > > + if (!drm_gpuvm_resv_protected(gpuvm)) { > > > > + drm_gpuvm_bo_list_del(vm_bo, extobj, true); > > > > + drm_gpuvm_bo_list_del(vm_bo, evict, true); > > > > + } > > > > + > > > > + list_del(&vm_bo->list.entry.gem); > > > > +} > > > > + > > > > +/* > > > > + * GEM mutex must not be held. Called after drm_gpuvm_bo_defer_free_locked(). > > > > + */ > > > > +static void > > > > +drm_gpuvm_bo_defer_free_unlocked(struct drm_gpuvm_bo *vm_bo) > > > > +{ > > > > + struct drm_gpuvm *gpuvm = vm_bo->vm; > > > > + > > > > + llist_add(&vm_bo->list.entry.bo_defer, &gpuvm->bo_defer); > > > > +} > > > > + > > > > +static void > > > > +drm_gpuvm_bo_defer_free(struct kref *kref) > > > > +{ > > > > + struct drm_gpuvm_bo *vm_bo = container_of(kref, struct drm_gpuvm_bo, > > > > + kref); > > > > + > > > > + mutex_lock(&vm_bo->obj->gpuva.lock); > > > > + drm_gpuvm_bo_defer_free_locked(kref); > > > > + mutex_unlock(&vm_bo->obj->gpuva.lock); > > > > + > > > > + /* > > > > + * It's important that the GEM stays alive for the duration in which we > > > > + * hold the mutex, but the instant we add the vm_bo to bo_defer, > > > > + * another thread might call drm_gpuvm_bo_deferred_cleanup() and put > > > > + * the GEM. Therefore, to avoid kfreeing a mutex we are holding, we add > > > > + * the vm_bo to bo_defer *after* releasing the GEM's mutex. > > > > + */ > > > > + drm_gpuvm_bo_defer_free_unlocked(vm_bo); > > > > +} > > > > > > So, you're splitting drm_gpuvm_bo_defer_free() into two functions, one doing the > > > work that is required to be called with the gpuva lock held and one that does > > > the work that does not require a lock, which makes perfect sense. > > > > > > However, the naming chosen for the two functions, i.e. > > > drm_gpuvm_bo_defer_free_unlocked() and drm_gpuvm_bo_defer_free_locked() is > > > confusing: > > > > > > What you mean semantically mean is "do part 1 with lock held" and "do part 2 > > > without lock held", but the the chosen names suggest that both functions are > > > identical, with the only difference that one takes the lock internally and the > > > other one requires the caller to take the lock. > > > > > > It's probably better to name them after what they do and not what they're part > > > of. If you prefer the latter, that's fine with me too, but please choose a name > > > that makes this circumstance obvious. > > > > Fair point. Do you have naming suggestions? Otherwise I can name them > > drm_gpuvm_bo_defer_free_part1() and drm_gpuvm_bo_defer_free_part2(). > > :) > > drm_gpuvm_bo_free_deferral_extract_locked() and > drm_gpuvm_bo_free_deferral_enqueue()? Definitely not short names though. With those names I have to do some additional line breaks. How about: drm_gpuvm_bo_into_zombie() drm_gpuvm_bo_defer_zombie() leaning on the zombie terminology I already added for the drm_gpuvm_bo_is_zombie() function. Alice ^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v3 1/2] drm/gpuvm: add deferred vm_bo cleanup 2025-10-06 11:31 ` Alice Ryhl @ 2025-10-06 11:41 ` Boris Brezillon 2025-10-06 11:49 ` Alice Ryhl 0 siblings, 1 reply; 18+ messages in thread From: Boris Brezillon @ 2025-10-06 11:41 UTC (permalink / raw) To: Alice Ryhl Cc: Danilo Krummrich, Matthew Brost, Thomas Hellström, Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie, Simona Vetter, Steven Price, Daniel Almeida, Liviu Dudau, dri-devel, linux-kernel, rust-for-linux On Mon, 6 Oct 2025 13:31:51 +0200 Alice Ryhl <aliceryhl@google.com> wrote: > On Wed, Oct 1, 2025 at 5:13 PM Boris Brezillon > <boris.brezillon@collabora.com> wrote: > > > > On Wed, 1 Oct 2025 16:42:35 +0200 > > Alice Ryhl <aliceryhl@google.com> wrote: > > > > > On Wed, Oct 1, 2025 at 4:01 PM Danilo Krummrich <dakr@kernel.org> wrote: > > > > > > > > On Wed Oct 1, 2025 at 12:41 PM CEST, Alice Ryhl wrote: > > > > > +/* > > > > > + * Must be called with GEM mutex held. After releasing GEM mutex, > > > > > + * drm_gpuvm_bo_defer_free_unlocked() must be called. > > > > > + */ > > > > > +static void > > > > > +drm_gpuvm_bo_defer_free_locked(struct kref *kref) > > > > > +{ > > > > > + struct drm_gpuvm_bo *vm_bo = container_of(kref, struct drm_gpuvm_bo, > > > > > + kref); > > > > > + struct drm_gpuvm *gpuvm = vm_bo->vm; > > > > > + > > > > > + if (!drm_gpuvm_resv_protected(gpuvm)) { > > > > > + drm_gpuvm_bo_list_del(vm_bo, extobj, true); > > > > > + drm_gpuvm_bo_list_del(vm_bo, evict, true); > > > > > + } > > > > > + > > > > > + list_del(&vm_bo->list.entry.gem); > > > > > +} > > > > > + > > > > > +/* > > > > > + * GEM mutex must not be held. Called after drm_gpuvm_bo_defer_free_locked(). > > > > > + */ > > > > > +static void > > > > > +drm_gpuvm_bo_defer_free_unlocked(struct drm_gpuvm_bo *vm_bo) > > > > > +{ > > > > > + struct drm_gpuvm *gpuvm = vm_bo->vm; > > > > > + > > > > > + llist_add(&vm_bo->list.entry.bo_defer, &gpuvm->bo_defer); > > > > > +} > > > > > + > > > > > +static void > > > > > +drm_gpuvm_bo_defer_free(struct kref *kref) > > > > > +{ > > > > > + struct drm_gpuvm_bo *vm_bo = container_of(kref, struct drm_gpuvm_bo, > > > > > + kref); > > > > > + > > > > > + mutex_lock(&vm_bo->obj->gpuva.lock); > > > > > + drm_gpuvm_bo_defer_free_locked(kref); > > > > > + mutex_unlock(&vm_bo->obj->gpuva.lock); > > > > > + > > > > > + /* > > > > > + * It's important that the GEM stays alive for the duration in which we > > > > > + * hold the mutex, but the instant we add the vm_bo to bo_defer, > > > > > + * another thread might call drm_gpuvm_bo_deferred_cleanup() and put > > > > > + * the GEM. Therefore, to avoid kfreeing a mutex we are holding, we add > > > > > + * the vm_bo to bo_defer *after* releasing the GEM's mutex. > > > > > + */ > > > > > + drm_gpuvm_bo_defer_free_unlocked(vm_bo); > > > > > +} > > > > > > > > So, you're splitting drm_gpuvm_bo_defer_free() into two functions, one doing the > > > > work that is required to be called with the gpuva lock held and one that does > > > > the work that does not require a lock, which makes perfect sense. > > > > > > > > However, the naming chosen for the two functions, i.e. > > > > drm_gpuvm_bo_defer_free_unlocked() and drm_gpuvm_bo_defer_free_locked() is > > > > confusing: > > > > > > > > What you mean semantically mean is "do part 1 with lock held" and "do part 2 > > > > without lock held", but the the chosen names suggest that both functions are > > > > identical, with the only difference that one takes the lock internally and the > > > > other one requires the caller to take the lock. > > > > > > > > It's probably better to name them after what they do and not what they're part > > > > of. If you prefer the latter, that's fine with me too, but please choose a name > > > > that makes this circumstance obvious. > > > > > > Fair point. Do you have naming suggestions? Otherwise I can name them > > > drm_gpuvm_bo_defer_free_part1() and drm_gpuvm_bo_defer_free_part2(). > > > :) > > > > drm_gpuvm_bo_free_deferral_extract_locked() and > > drm_gpuvm_bo_free_deferral_enqueue()? Definitely not short names though. > > With those names I have to do some additional line breaks. How about: > > drm_gpuvm_bo_into_zombie() > drm_gpuvm_bo_defer_zombie() Sounds good. I think I'd prefer if the second one was called drm_gpuvm_bo_defer_zombie_cleanup() to make clear what the deferral is about, but feel free to ignore this if you think it's too long. ^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v3 1/2] drm/gpuvm: add deferred vm_bo cleanup 2025-10-06 11:41 ` Boris Brezillon @ 2025-10-06 11:49 ` Alice Ryhl 0 siblings, 0 replies; 18+ messages in thread From: Alice Ryhl @ 2025-10-06 11:49 UTC (permalink / raw) To: Boris Brezillon Cc: Danilo Krummrich, Matthew Brost, Thomas Hellström, Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie, Simona Vetter, Steven Price, Daniel Almeida, Liviu Dudau, dri-devel, linux-kernel, rust-for-linux On Mon, Oct 6, 2025 at 1:41 PM Boris Brezillon <boris.brezillon@collabora.com> wrote: > > On Mon, 6 Oct 2025 13:31:51 +0200 > Alice Ryhl <aliceryhl@google.com> wrote: > > > On Wed, Oct 1, 2025 at 5:13 PM Boris Brezillon > > <boris.brezillon@collabora.com> wrote: > > > > > > On Wed, 1 Oct 2025 16:42:35 +0200 > > > Alice Ryhl <aliceryhl@google.com> wrote: > > > > > > > On Wed, Oct 1, 2025 at 4:01 PM Danilo Krummrich <dakr@kernel.org> wrote: > > > > > > > > > > On Wed Oct 1, 2025 at 12:41 PM CEST, Alice Ryhl wrote: > > > > > > +/* > > > > > > + * Must be called with GEM mutex held. After releasing GEM mutex, > > > > > > + * drm_gpuvm_bo_defer_free_unlocked() must be called. > > > > > > + */ > > > > > > +static void > > > > > > +drm_gpuvm_bo_defer_free_locked(struct kref *kref) > > > > > > +{ > > > > > > + struct drm_gpuvm_bo *vm_bo = container_of(kref, struct drm_gpuvm_bo, > > > > > > + kref); > > > > > > + struct drm_gpuvm *gpuvm = vm_bo->vm; > > > > > > + > > > > > > + if (!drm_gpuvm_resv_protected(gpuvm)) { > > > > > > + drm_gpuvm_bo_list_del(vm_bo, extobj, true); > > > > > > + drm_gpuvm_bo_list_del(vm_bo, evict, true); > > > > > > + } > > > > > > + > > > > > > + list_del(&vm_bo->list.entry.gem); > > > > > > +} > > > > > > + > > > > > > +/* > > > > > > + * GEM mutex must not be held. Called after drm_gpuvm_bo_defer_free_locked(). > > > > > > + */ > > > > > > +static void > > > > > > +drm_gpuvm_bo_defer_free_unlocked(struct drm_gpuvm_bo *vm_bo) > > > > > > +{ > > > > > > + struct drm_gpuvm *gpuvm = vm_bo->vm; > > > > > > + > > > > > > + llist_add(&vm_bo->list.entry.bo_defer, &gpuvm->bo_defer); > > > > > > +} > > > > > > + > > > > > > +static void > > > > > > +drm_gpuvm_bo_defer_free(struct kref *kref) > > > > > > +{ > > > > > > + struct drm_gpuvm_bo *vm_bo = container_of(kref, struct drm_gpuvm_bo, > > > > > > + kref); > > > > > > + > > > > > > + mutex_lock(&vm_bo->obj->gpuva.lock); > > > > > > + drm_gpuvm_bo_defer_free_locked(kref); > > > > > > + mutex_unlock(&vm_bo->obj->gpuva.lock); > > > > > > + > > > > > > + /* > > > > > > + * It's important that the GEM stays alive for the duration in which we > > > > > > + * hold the mutex, but the instant we add the vm_bo to bo_defer, > > > > > > + * another thread might call drm_gpuvm_bo_deferred_cleanup() and put > > > > > > + * the GEM. Therefore, to avoid kfreeing a mutex we are holding, we add > > > > > > + * the vm_bo to bo_defer *after* releasing the GEM's mutex. > > > > > > + */ > > > > > > + drm_gpuvm_bo_defer_free_unlocked(vm_bo); > > > > > > +} > > > > > > > > > > So, you're splitting drm_gpuvm_bo_defer_free() into two functions, one doing the > > > > > work that is required to be called with the gpuva lock held and one that does > > > > > the work that does not require a lock, which makes perfect sense. > > > > > > > > > > However, the naming chosen for the two functions, i.e. > > > > > drm_gpuvm_bo_defer_free_unlocked() and drm_gpuvm_bo_defer_free_locked() is > > > > > confusing: > > > > > > > > > > What you mean semantically mean is "do part 1 with lock held" and "do part 2 > > > > > without lock held", but the the chosen names suggest that both functions are > > > > > identical, with the only difference that one takes the lock internally and the > > > > > other one requires the caller to take the lock. > > > > > > > > > > It's probably better to name them after what they do and not what they're part > > > > > of. If you prefer the latter, that's fine with me too, but please choose a name > > > > > that makes this circumstance obvious. > > > > > > > > Fair point. Do you have naming suggestions? Otherwise I can name them > > > > drm_gpuvm_bo_defer_free_part1() and drm_gpuvm_bo_defer_free_part2(). > > > > :) > > > > > > drm_gpuvm_bo_free_deferral_extract_locked() and > > > drm_gpuvm_bo_free_deferral_enqueue()? Definitely not short names though. > > > > With those names I have to do some additional line breaks. How about: > > > > drm_gpuvm_bo_into_zombie() > > drm_gpuvm_bo_defer_zombie() > > Sounds good. I think I'd prefer if the second one was called > drm_gpuvm_bo_defer_zombie_cleanup() to make clear what the deferral is > about, but feel free to ignore this if you think it's too long. Sounds good. It's more the first one that I want to keep short because it's used with kref_put(). Alice ^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v3 1/2] drm/gpuvm: add deferred vm_bo cleanup 2025-10-01 10:41 ` [PATCH v3 1/2] drm/gpuvm: add deferred vm_bo cleanup Alice Ryhl 2025-10-01 11:27 ` Boris Brezillon 2025-10-01 14:01 ` Danilo Krummrich @ 2025-10-06 11:30 ` Alice Ryhl 2025-10-06 11:38 ` Boris Brezillon 2 siblings, 1 reply; 18+ messages in thread From: Alice Ryhl @ 2025-10-06 11:30 UTC (permalink / raw) To: Danilo Krummrich, Matthew Brost, Thomas Hellström Cc: Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie, Simona Vetter, Boris Brezillon, Steven Price, Daniel Almeida, Liviu Dudau, dri-devel, linux-kernel, rust-for-linux On Wed, Oct 1, 2025 at 12:41 PM Alice Ryhl <aliceryhl@google.com> wrote: > > When using GPUVM in immediate mode, it is necessary to call > drm_gpuvm_unlink() from the fence signalling critical path. However, > unlink may call drm_gpuvm_bo_put(), which causes some challenges: > > 1. drm_gpuvm_bo_put() often requires you to take resv locks, which you > can't do from the fence signalling critical path. > 2. drm_gpuvm_bo_put() calls drm_gem_object_put(), which is often going > to be unsafe to call from the fence signalling critical path. > > To solve these issues, add a deferred version of drm_gpuvm_unlink() that > adds the vm_bo to a deferred cleanup list, and then clean it up later. > > The new methods take the GEMs GPUVA lock internally rather than letting > the caller do it because it also needs to perform an operation after > releasing the mutex again. This is to prevent freeing the GEM while > holding the mutex (more info as comments in the patch). This means that > the new methods can only be used with DRM_GPUVM_IMMEDIATE_MODE. > > Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com> > Signed-off-by: Alice Ryhl <aliceryhl@google.com> In this version, I got rid of the kref_put_mutex() usage, but I realized that maybe we should bring it back. With the current code, it's actually possible to observe a zombie vm_bo in the GEM's list because we don't drop the refcount while holding the mutex. Alice ^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v3 1/2] drm/gpuvm: add deferred vm_bo cleanup 2025-10-06 11:30 ` Alice Ryhl @ 2025-10-06 11:38 ` Boris Brezillon 0 siblings, 0 replies; 18+ messages in thread From: Boris Brezillon @ 2025-10-06 11:38 UTC (permalink / raw) To: Alice Ryhl Cc: Danilo Krummrich, Matthew Brost, Thomas Hellström, Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie, Simona Vetter, Steven Price, Daniel Almeida, Liviu Dudau, dri-devel, linux-kernel, rust-for-linux On Mon, 6 Oct 2025 13:30:59 +0200 Alice Ryhl <aliceryhl@google.com> wrote: > On Wed, Oct 1, 2025 at 12:41 PM Alice Ryhl <aliceryhl@google.com> wrote: > > > > When using GPUVM in immediate mode, it is necessary to call > > drm_gpuvm_unlink() from the fence signalling critical path. However, > > unlink may call drm_gpuvm_bo_put(), which causes some challenges: > > > > 1. drm_gpuvm_bo_put() often requires you to take resv locks, which you > > can't do from the fence signalling critical path. > > 2. drm_gpuvm_bo_put() calls drm_gem_object_put(), which is often going > > to be unsafe to call from the fence signalling critical path. > > > > To solve these issues, add a deferred version of drm_gpuvm_unlink() that > > adds the vm_bo to a deferred cleanup list, and then clean it up later. > > > > The new methods take the GEMs GPUVA lock internally rather than letting > > the caller do it because it also needs to perform an operation after > > releasing the mutex again. This is to prevent freeing the GEM while > > holding the mutex (more info as comments in the patch). This means that > > the new methods can only be used with DRM_GPUVM_IMMEDIATE_MODE. > > > > Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com> > > Signed-off-by: Alice Ryhl <aliceryhl@google.com> > > In this version, I got rid of the kref_put_mutex() usage, but I > realized that maybe we should bring it back. With the current code, > it's actually possible to observe a zombie vm_bo in the GEM's list > because we don't drop the refcount while holding the mutex. Alright, let's get back to the kref_put_mutex() approach then. ^ permalink raw reply [flat|nested] 18+ messages in thread
* [PATCH v3 2/2] panthor: use drm_gpuva_unlink_defer() 2025-10-01 10:41 [PATCH v3 0/2] Defer vm_bo cleanup in GPUVM with DRM_GPUVM_IMMEDIATE_MODE Alice Ryhl 2025-10-01 10:41 ` [PATCH v3 1/2] drm/gpuvm: add deferred vm_bo cleanup Alice Ryhl @ 2025-10-01 10:41 ` Alice Ryhl 2025-10-01 11:31 ` Boris Brezillon 1 sibling, 1 reply; 18+ messages in thread From: Alice Ryhl @ 2025-10-01 10:41 UTC (permalink / raw) To: Danilo Krummrich, Matthew Brost, Thomas Hellström Cc: Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie, Simona Vetter, Boris Brezillon, Steven Price, Daniel Almeida, Liviu Dudau, dri-devel, linux-kernel, rust-for-linux, Alice Ryhl Instead of manually deferring cleanup of vm_bos, use the new GPUVM infrastructure for doing so. To avoid manual management of vm_bo refcounts, the panthor_vma_link() and panthor_vma_unlink() methods are changed to get and put a vm_bo refcount on the vm_bo. This simplifies the code a lot. I preserved the behavior where panthor_gpuva_sm_step_map() drops the refcount right away rather than letting panthor_vm_cleanup_op_ctx() do it later. Signed-off-by: Alice Ryhl <aliceryhl@google.com> --- drivers/gpu/drm/panthor/panthor_mmu.c | 110 ++++++---------------------------- 1 file changed, 19 insertions(+), 91 deletions(-) diff --git a/drivers/gpu/drm/panthor/panthor_mmu.c b/drivers/gpu/drm/panthor/panthor_mmu.c index 6dec4354e3789d17c5a87fc8de3bc86764b804bc..9f5f4ddf291024121f3fd5644f2fdeba354fa67c 100644 --- a/drivers/gpu/drm/panthor/panthor_mmu.c +++ b/drivers/gpu/drm/panthor/panthor_mmu.c @@ -181,20 +181,6 @@ struct panthor_vm_op_ctx { u64 range; } va; - /** - * @returned_vmas: List of panthor_vma objects returned after a VM operation. - * - * For unmap operations, this will contain all VMAs that were covered by the - * specified VA range. - * - * For map operations, this will contain all VMAs that previously mapped to - * the specified VA range. - * - * Those VMAs, and the resources they point to will be released as part of - * the op_ctx cleanup operation. - */ - struct list_head returned_vmas; - /** @map: Fields specific to a map operation. */ struct { /** @map.vm_bo: Buffer object to map. */ @@ -1081,47 +1067,18 @@ void panthor_vm_free_va(struct panthor_vm *vm, struct drm_mm_node *va_node) mutex_unlock(&vm->mm_lock); } -static void panthor_vm_bo_put(struct drm_gpuvm_bo *vm_bo) +static void panthor_vm_bo_free(struct drm_gpuvm_bo *vm_bo) { struct panthor_gem_object *bo = to_panthor_bo(vm_bo->obj); - struct drm_gpuvm *vm = vm_bo->vm; - bool unpin; - - /* We must retain the GEM before calling drm_gpuvm_bo_put(), - * otherwise the mutex might be destroyed while we hold it. - * Same goes for the VM, since we take the VM resv lock. - */ - drm_gem_object_get(&bo->base.base); - drm_gpuvm_get(vm); - - /* We take the resv lock to protect against concurrent accesses to the - * gpuvm evicted/extobj lists that are modified in - * drm_gpuvm_bo_destroy(), which is called if drm_gpuvm_bo_put() - * releases sthe last vm_bo reference. - * We take the BO GPUVA list lock to protect the vm_bo removal from the - * GEM vm_bo list. - */ - dma_resv_lock(drm_gpuvm_resv(vm), NULL); - mutex_lock(&bo->base.base.gpuva.lock); - unpin = drm_gpuvm_bo_put(vm_bo); - 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 - * was hold by this object. - */ - if (unpin && !drm_gem_is_imported(&bo->base.base)) + if (!drm_gem_is_imported(&bo->base.base)) drm_gem_shmem_unpin(&bo->base); - - drm_gpuvm_put(vm); - drm_gem_object_put(&bo->base.base); + kfree(vm_bo); } static void panthor_vm_cleanup_op_ctx(struct panthor_vm_op_ctx *op_ctx, struct panthor_vm *vm) { - struct panthor_vma *vma, *tmp_vma; - u32 remaining_pt_count = op_ctx->rsvd_page_tables.count - op_ctx->rsvd_page_tables.ptr; @@ -1134,16 +1091,12 @@ static void panthor_vm_cleanup_op_ctx(struct panthor_vm_op_ctx *op_ctx, kfree(op_ctx->rsvd_page_tables.pages); if (op_ctx->map.vm_bo) - panthor_vm_bo_put(op_ctx->map.vm_bo); + drm_gpuvm_bo_put_deferred(op_ctx->map.vm_bo); for (u32 i = 0; i < ARRAY_SIZE(op_ctx->preallocated_vmas); i++) kfree(op_ctx->preallocated_vmas[i]); - list_for_each_entry_safe(vma, tmp_vma, &op_ctx->returned_vmas, node) { - list_del(&vma->node); - panthor_vm_bo_put(vma->base.vm_bo); - kfree(vma); - } + drm_gpuvm_bo_deferred_cleanup(&vm->base); } static struct panthor_vma * @@ -1232,7 +1185,6 @@ static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx, return -EINVAL; memset(op_ctx, 0, sizeof(*op_ctx)); - INIT_LIST_HEAD(&op_ctx->returned_vmas); op_ctx->flags = flags; op_ctx->va.range = size; op_ctx->va.addr = va; @@ -1243,7 +1195,9 @@ static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx, if (!drm_gem_is_imported(&bo->base.base)) { /* Pre-reserve the BO pages, so the map operation doesn't have to - * allocate. + * allocate. This pin is dropped in panthor_vm_bo_free(), so + * once we have successfully called drm_gpuvm_bo_create(), + * GPUVM will take care of dropping the pin for us. */ ret = drm_gem_shmem_pin(&bo->base); if (ret) @@ -1282,16 +1236,6 @@ static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx, 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 - * retains a pin ref, and we can release the one we took earlier. - * - * If our pre-allocated vm_bo is picked, it now retains the pin ref, - * which will be released in panthor_vm_bo_put(). - */ - if (preallocated_vm_bo != op_ctx->map.vm_bo && - !drm_gem_is_imported(&bo->base.base)) - drm_gem_shmem_unpin(&bo->base); - op_ctx->map.bo_offset = offset; /* L1, L2 and L3 page tables. @@ -1339,7 +1283,6 @@ static int panthor_vm_prepare_unmap_op_ctx(struct panthor_vm_op_ctx *op_ctx, int ret; memset(op_ctx, 0, sizeof(*op_ctx)); - INIT_LIST_HEAD(&op_ctx->returned_vmas); op_ctx->va.range = size; op_ctx->va.addr = va; op_ctx->flags = DRM_PANTHOR_VM_BIND_OP_TYPE_UNMAP; @@ -1387,7 +1330,6 @@ static void panthor_vm_prepare_sync_only_op_ctx(struct panthor_vm_op_ctx *op_ctx struct panthor_vm *vm) { memset(op_ctx, 0, sizeof(*op_ctx)); - INIT_LIST_HEAD(&op_ctx->returned_vmas); op_ctx->flags = DRM_PANTHOR_VM_BIND_OP_TYPE_SYNC_ONLY; } @@ -2033,26 +1975,13 @@ static void panthor_vma_link(struct panthor_vm *vm, 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->base.base.gpuva.lock); } -static void panthor_vma_unlink(struct panthor_vm *vm, - struct panthor_vma *vma) +static void panthor_vma_unlink(struct panthor_vma *vma) { - 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->base.base.gpuva.lock); - drm_gpuva_unlink(&vma->base); - 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 - * destruction. Re-assign it here. - */ - vma->base.vm_bo = vm_bo; - list_add_tail(&vma->node, &vm->op_ctx->returned_vmas); + drm_gpuva_unlink_defer(&vma->base); + kfree(vma); } static void panthor_vma_init(struct panthor_vma *vma, u32 flags) @@ -2084,12 +2013,12 @@ static int panthor_gpuva_sm_step_map(struct drm_gpuva_op *op, void *priv) if (ret) return ret; - /* Ref owned by the mapping now, clear the obj field so we don't release the - * pinning/obj ref behind GPUVA's back. - */ drm_gpuva_map(&vm->base, &vma->base, &op->map); panthor_vma_link(vm, vma, op_ctx->map.vm_bo); + + drm_gpuvm_bo_put_deferred(op_ctx->map.vm_bo); op_ctx->map.vm_bo = NULL; + return 0; } @@ -2128,16 +2057,14 @@ static int panthor_gpuva_sm_step_remap(struct drm_gpuva_op *op, * owned by the old mapping which will be released when this * mapping is destroyed, we need to grab a ref here. */ - panthor_vma_link(vm, prev_vma, - drm_gpuvm_bo_get(op->remap.unmap->va->vm_bo)); + panthor_vma_link(vm, prev_vma, op->remap.unmap->va->vm_bo); } if (next_vma) { - panthor_vma_link(vm, next_vma, - drm_gpuvm_bo_get(op->remap.unmap->va->vm_bo)); + panthor_vma_link(vm, next_vma, op->remap.unmap->va->vm_bo); } - panthor_vma_unlink(vm, unmap_vma); + panthor_vma_unlink(unmap_vma); return 0; } @@ -2154,12 +2081,13 @@ static int panthor_gpuva_sm_step_unmap(struct drm_gpuva_op *op, return ret; drm_gpuva_unmap(&op->unmap); - panthor_vma_unlink(vm, unmap_vma); + panthor_vma_unlink(unmap_vma); return 0; } static const struct drm_gpuvm_ops panthor_gpuvm_ops = { .vm_free = panthor_vm_free, + .vm_bo_free = panthor_vm_bo_free, .sm_step_map = panthor_gpuva_sm_step_map, .sm_step_remap = panthor_gpuva_sm_step_remap, .sm_step_unmap = panthor_gpuva_sm_step_unmap, -- 2.51.0.618.g983fd99d29-goog ^ permalink raw reply related [flat|nested] 18+ messages in thread
* Re: [PATCH v3 2/2] panthor: use drm_gpuva_unlink_defer() 2025-10-01 10:41 ` [PATCH v3 2/2] panthor: use drm_gpuva_unlink_defer() Alice Ryhl @ 2025-10-01 11:31 ` Boris Brezillon 0 siblings, 0 replies; 18+ messages in thread From: Boris Brezillon @ 2025-10-01 11:31 UTC (permalink / raw) To: Alice Ryhl Cc: Danilo Krummrich, Matthew Brost, Thomas Hellström, Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie, Simona Vetter, Steven Price, Daniel Almeida, Liviu Dudau, dri-devel, linux-kernel, rust-for-linux On Wed, 01 Oct 2025 10:41:37 +0000 Alice Ryhl <aliceryhl@google.com> wrote: > Instead of manually deferring cleanup of vm_bos, use the new GPUVM > infrastructure for doing so. > > To avoid manual management of vm_bo refcounts, the panthor_vma_link() > and panthor_vma_unlink() methods are changed to get and put a vm_bo > refcount on the vm_bo. This simplifies the code a lot. I preserved the > behavior where panthor_gpuva_sm_step_map() drops the refcount right away > rather than letting panthor_vm_cleanup_op_ctx() do it later. > > Signed-off-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com> > --- > drivers/gpu/drm/panthor/panthor_mmu.c | 110 ++++++---------------------------- > 1 file changed, 19 insertions(+), 91 deletions(-) > > diff --git a/drivers/gpu/drm/panthor/panthor_mmu.c b/drivers/gpu/drm/panthor/panthor_mmu.c > index 6dec4354e3789d17c5a87fc8de3bc86764b804bc..9f5f4ddf291024121f3fd5644f2fdeba354fa67c 100644 > --- a/drivers/gpu/drm/panthor/panthor_mmu.c > +++ b/drivers/gpu/drm/panthor/panthor_mmu.c > @@ -181,20 +181,6 @@ struct panthor_vm_op_ctx { > u64 range; > } va; > > - /** > - * @returned_vmas: List of panthor_vma objects returned after a VM operation. > - * > - * For unmap operations, this will contain all VMAs that were covered by the > - * specified VA range. > - * > - * For map operations, this will contain all VMAs that previously mapped to > - * the specified VA range. > - * > - * Those VMAs, and the resources they point to will be released as part of > - * the op_ctx cleanup operation. > - */ > - struct list_head returned_vmas; > - > /** @map: Fields specific to a map operation. */ > struct { > /** @map.vm_bo: Buffer object to map. */ > @@ -1081,47 +1067,18 @@ void panthor_vm_free_va(struct panthor_vm *vm, struct drm_mm_node *va_node) > mutex_unlock(&vm->mm_lock); > } > > -static void panthor_vm_bo_put(struct drm_gpuvm_bo *vm_bo) > +static void panthor_vm_bo_free(struct drm_gpuvm_bo *vm_bo) > { > struct panthor_gem_object *bo = to_panthor_bo(vm_bo->obj); > - struct drm_gpuvm *vm = vm_bo->vm; > - bool unpin; > - > - /* We must retain the GEM before calling drm_gpuvm_bo_put(), > - * otherwise the mutex might be destroyed while we hold it. > - * Same goes for the VM, since we take the VM resv lock. > - */ > - drm_gem_object_get(&bo->base.base); > - drm_gpuvm_get(vm); > - > - /* We take the resv lock to protect against concurrent accesses to the > - * gpuvm evicted/extobj lists that are modified in > - * drm_gpuvm_bo_destroy(), which is called if drm_gpuvm_bo_put() > - * releases sthe last vm_bo reference. > - * We take the BO GPUVA list lock to protect the vm_bo removal from the > - * GEM vm_bo list. > - */ > - dma_resv_lock(drm_gpuvm_resv(vm), NULL); > - mutex_lock(&bo->base.base.gpuva.lock); > - unpin = drm_gpuvm_bo_put(vm_bo); > - 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 > - * was hold by this object. > - */ > - if (unpin && !drm_gem_is_imported(&bo->base.base)) > + if (!drm_gem_is_imported(&bo->base.base)) > drm_gem_shmem_unpin(&bo->base); > - > - drm_gpuvm_put(vm); > - drm_gem_object_put(&bo->base.base); > + kfree(vm_bo); > } > > static void panthor_vm_cleanup_op_ctx(struct panthor_vm_op_ctx *op_ctx, > struct panthor_vm *vm) > { > - struct panthor_vma *vma, *tmp_vma; > - > u32 remaining_pt_count = op_ctx->rsvd_page_tables.count - > op_ctx->rsvd_page_tables.ptr; > > @@ -1134,16 +1091,12 @@ static void panthor_vm_cleanup_op_ctx(struct panthor_vm_op_ctx *op_ctx, > kfree(op_ctx->rsvd_page_tables.pages); > > if (op_ctx->map.vm_bo) > - panthor_vm_bo_put(op_ctx->map.vm_bo); > + drm_gpuvm_bo_put_deferred(op_ctx->map.vm_bo); > > for (u32 i = 0; i < ARRAY_SIZE(op_ctx->preallocated_vmas); i++) > kfree(op_ctx->preallocated_vmas[i]); > > - list_for_each_entry_safe(vma, tmp_vma, &op_ctx->returned_vmas, node) { > - list_del(&vma->node); > - panthor_vm_bo_put(vma->base.vm_bo); > - kfree(vma); > - } > + drm_gpuvm_bo_deferred_cleanup(&vm->base); > } > > static struct panthor_vma * > @@ -1232,7 +1185,6 @@ static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx, > return -EINVAL; > > memset(op_ctx, 0, sizeof(*op_ctx)); > - INIT_LIST_HEAD(&op_ctx->returned_vmas); > op_ctx->flags = flags; > op_ctx->va.range = size; > op_ctx->va.addr = va; > @@ -1243,7 +1195,9 @@ static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx, > > if (!drm_gem_is_imported(&bo->base.base)) { > /* Pre-reserve the BO pages, so the map operation doesn't have to > - * allocate. > + * allocate. This pin is dropped in panthor_vm_bo_free(), so > + * once we have successfully called drm_gpuvm_bo_create(), > + * GPUVM will take care of dropping the pin for us. > */ > ret = drm_gem_shmem_pin(&bo->base); > if (ret) > @@ -1282,16 +1236,6 @@ static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx, > 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 > - * retains a pin ref, and we can release the one we took earlier. > - * > - * If our pre-allocated vm_bo is picked, it now retains the pin ref, > - * which will be released in panthor_vm_bo_put(). > - */ > - if (preallocated_vm_bo != op_ctx->map.vm_bo && > - !drm_gem_is_imported(&bo->base.base)) > - drm_gem_shmem_unpin(&bo->base); > - > op_ctx->map.bo_offset = offset; > > /* L1, L2 and L3 page tables. > @@ -1339,7 +1283,6 @@ static int panthor_vm_prepare_unmap_op_ctx(struct panthor_vm_op_ctx *op_ctx, > int ret; > > memset(op_ctx, 0, sizeof(*op_ctx)); > - INIT_LIST_HEAD(&op_ctx->returned_vmas); > op_ctx->va.range = size; > op_ctx->va.addr = va; > op_ctx->flags = DRM_PANTHOR_VM_BIND_OP_TYPE_UNMAP; > @@ -1387,7 +1330,6 @@ static void panthor_vm_prepare_sync_only_op_ctx(struct panthor_vm_op_ctx *op_ctx > struct panthor_vm *vm) > { > memset(op_ctx, 0, sizeof(*op_ctx)); > - INIT_LIST_HEAD(&op_ctx->returned_vmas); > op_ctx->flags = DRM_PANTHOR_VM_BIND_OP_TYPE_SYNC_ONLY; > } > > @@ -2033,26 +1975,13 @@ static void panthor_vma_link(struct panthor_vm *vm, > > 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->base.base.gpuva.lock); > } > > -static void panthor_vma_unlink(struct panthor_vm *vm, > - struct panthor_vma *vma) > +static void panthor_vma_unlink(struct panthor_vma *vma) > { > - 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->base.base.gpuva.lock); > - drm_gpuva_unlink(&vma->base); > - 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 > - * destruction. Re-assign it here. > - */ > - vma->base.vm_bo = vm_bo; > - list_add_tail(&vma->node, &vm->op_ctx->returned_vmas); > + drm_gpuva_unlink_defer(&vma->base); > + kfree(vma); > } > > static void panthor_vma_init(struct panthor_vma *vma, u32 flags) > @@ -2084,12 +2013,12 @@ static int panthor_gpuva_sm_step_map(struct drm_gpuva_op *op, void *priv) > if (ret) > return ret; > > - /* Ref owned by the mapping now, clear the obj field so we don't release the > - * pinning/obj ref behind GPUVA's back. > - */ > drm_gpuva_map(&vm->base, &vma->base, &op->map); > panthor_vma_link(vm, vma, op_ctx->map.vm_bo); > + > + drm_gpuvm_bo_put_deferred(op_ctx->map.vm_bo); > op_ctx->map.vm_bo = NULL; > + > return 0; > } > > @@ -2128,16 +2057,14 @@ static int panthor_gpuva_sm_step_remap(struct drm_gpuva_op *op, > * owned by the old mapping which will be released when this > * mapping is destroyed, we need to grab a ref here. > */ > - panthor_vma_link(vm, prev_vma, > - drm_gpuvm_bo_get(op->remap.unmap->va->vm_bo)); > + panthor_vma_link(vm, prev_vma, op->remap.unmap->va->vm_bo); > } > > if (next_vma) { > - panthor_vma_link(vm, next_vma, > - drm_gpuvm_bo_get(op->remap.unmap->va->vm_bo)); > + panthor_vma_link(vm, next_vma, op->remap.unmap->va->vm_bo); > } > > - panthor_vma_unlink(vm, unmap_vma); > + panthor_vma_unlink(unmap_vma); > return 0; > } > > @@ -2154,12 +2081,13 @@ static int panthor_gpuva_sm_step_unmap(struct drm_gpuva_op *op, > return ret; > > drm_gpuva_unmap(&op->unmap); > - panthor_vma_unlink(vm, unmap_vma); > + panthor_vma_unlink(unmap_vma); > return 0; > } > > static const struct drm_gpuvm_ops panthor_gpuvm_ops = { > .vm_free = panthor_vm_free, > + .vm_bo_free = panthor_vm_bo_free, > .sm_step_map = panthor_gpuva_sm_step_map, > .sm_step_remap = panthor_gpuva_sm_step_remap, > .sm_step_unmap = panthor_gpuva_sm_step_unmap, > ^ permalink raw reply [flat|nested] 18+ messages in thread
end of thread, other threads:[~2025-10-06 11:50 UTC | newest] Thread overview: 18+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2025-10-01 10:41 [PATCH v3 0/2] Defer vm_bo cleanup in GPUVM with DRM_GPUVM_IMMEDIATE_MODE Alice Ryhl 2025-10-01 10:41 ` [PATCH v3 1/2] drm/gpuvm: add deferred vm_bo cleanup Alice Ryhl 2025-10-01 11:27 ` Boris Brezillon 2025-10-01 11:45 ` Alice Ryhl 2025-10-01 12:04 ` Boris Brezillon 2025-10-01 12:13 ` Boris Brezillon 2025-10-01 12:22 ` Alice Ryhl 2025-10-01 13:01 ` Boris Brezillon 2025-10-01 14:01 ` Danilo Krummrich 2025-10-01 14:42 ` Alice Ryhl 2025-10-01 15:13 ` Boris Brezillon 2025-10-06 11:31 ` Alice Ryhl 2025-10-06 11:41 ` Boris Brezillon 2025-10-06 11:49 ` Alice Ryhl 2025-10-06 11:30 ` Alice Ryhl 2025-10-06 11:38 ` Boris Brezillon 2025-10-01 10:41 ` [PATCH v3 2/2] panthor: use drm_gpuva_unlink_defer() Alice Ryhl 2025-10-01 11:31 ` Boris Brezillon
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).