Skip to contentSkip to conversation

accel/ivpu: Remove ctx->lock on MMU context unmap

PATCH In review Dawid Osuchowski 2 participants 7 messages Dec 1 – Dec 2

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover accel/ivpu: Remove ctx->lock on MMU context unmap

This series reduces lock contention during IVPU BO teardown and avoids a direct-reclaim deadlock by separating VA-node locking from page-table operations. In BO free, bo_list_lock is held only for the final list removal.

Jozef Wludzik (3):
accel/ivpu: Avoid taking the context lock when unmapping pages accel/ivpu: Separate VA allocator locking from page-table locking accel/ivpu: Shorten bo_list_lock hold time in BO free

 drivers/accel/ivpu/ivpu_gem.c         |  5 ++---
 drivers/accel/ivpu/ivpu_mmu_context.c | 22 ++++++++--------------
 drivers/accel/ivpu/ivpu_mmu_context.h |  5 ++++-
 3 files changed, 14 insertions(+), 18 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/3 accel/ivpu: Avoid taking the context lock when unmapping pages

From: Jozef Wludzik <[email protected]>

Unmapping a BO only invalidates leaf PTEs in its reserved VA range. The caller serializes access to that range and keeps the context and VA node alive until unmapping completes. Intermediate page tables remain allocated until context teardown.

Remove the context mutex from scatter-gather unmapping and use WRITE_ONCE() for leaf PTE invalidation. Keep the write barrier and TLB invalidation, as well as the existing locking for mapping, page-table allocation and drm_mm updates.

---
 drivers/accel/ivpu/ivpu_mmu_context.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/accel/ivpu/ivpu_mmu_context.c +2 -5
index bb3ca81838e3..6429cddf968f 100644
@@ -303,7 +303,8 @@ static void ivpu_mmu_context_unmap_page(struct ivpu_mmu_context *ctx, u64 vpu_ad
 	int pte_idx = FIELD_GET(IVPU_MMU_PTE_INDEX_MASK, vpu_addr);
 
 	/* Update PTE with dummy physical address and clear flags */
-	ctx->pgtable.pte_ptrs[pgd_idx][pud_idx][pmd_idx][pte_idx] = IVPU_MMU_ENTRY_INVALID;
+	WRITE_ONCE(ctx->pgtable.pte_ptrs[pgd_idx][pud_idx][pmd_idx][pte_idx],
+		   IVPU_MMU_ENTRY_INVALID);
 }
 
 static int
@@ -523,8 +524,6 @@ ivpu_mmu_context_unmap_sgt(struct ivpu_device *vdev, struct ivpu_mmu_context *ct
 	if (drm_WARN_ON(&vdev->drm, !ctx))
 		return;
 
-	mutex_lock(&ctx->lock);
-
 	for_each_sgtable_dma_sg(sgt, sg, i) {
 		dma_addr_t dma_addr = sg_dma_address(sg) - sg->offset;
 		size_t size = sg_dma_len(sg) + sg->offset;
@@ -539,8 +538,6 @@ ivpu_mmu_context_unmap_sgt(struct ivpu_device *vdev, struct ivpu_mmu_context *ct
 	/* Ensure page table modifications are flushed from wc buffers to memory */
 	wmb();
 
-	mutex_unlock(&ctx->lock);
-
 	ret = ivpu_mmu_invalidate_tlb(vdev, ctx->id);
 	if (ret)
 		ivpu_warn_ratelimited(vdev, "Failed to invalidate TLB for ctx %u: %d\n",
Signature · 1 line
2.43.0
#3 lore raw

PATCH 2/3 accel/ivpu: Separate VA allocator locking from page-table locking

In reply to Dawid Osuchowski

From: Jozef Wludzik <[email protected]>

The context mutex is held across reclaimable page-table allocations. Taking the same mutex to remove a VA node during BO destruction can deadlock when destruction runs in direct reclaim.

Protect drm_mm insertion and removal with a dedicated spinlock instead. These operations use preallocated nodes and do not sleep. Keep both alignment attempts under the same lock and retain the context mutex for page-table mapping, allocation and context descriptor updates.

---
 drivers/accel/ivpu/ivpu_mmu_context.c | 15 ++++++---------
 drivers/accel/ivpu/ivpu_mmu_context.h |  5 ++++-
 2 files changed, 10 insertions(+), 10 deletions(-)
drivers/accel/ivpu/ivpu_mmu_context.c +6 -9
index 6429cddf968f..13e5dc455ad6 100644
@@ -552,27 +552,23 @@ ivpu_mmu_context_insert_node(struct ivpu_mmu_context *ctx, const struct ivpu_add
 
 	WARN_ON(!range);
 
-	mutex_lock(&ctx->lock);
+	guard(spinlock)(&ctx->mm_lock);
 	if (!ivpu_disable_mmu_cont_pages && size >= IVPU_MMU_CONT_PAGES_SIZE) {
 		ret = drm_mm_insert_node_in_range(&ctx->mm, node, size, IVPU_MMU_CONT_PAGES_SIZE, 0,
 						  range->start, range->end, DRM_MM_INSERT_BEST);
 		if (!ret)
-			goto unlock;
+			return 0;
 	}
 
-	ret = drm_mm_insert_node_in_range(&ctx->mm, node, size, IVPU_MMU_PAGE_SIZE, 0,
-					  range->start, range->end, DRM_MM_INSERT_BEST);
-unlock:
-	mutex_unlock(&ctx->lock);
-	return ret;
+	return drm_mm_insert_node_in_range(&ctx->mm, node, size, IVPU_MMU_PAGE_SIZE, 0,
+					   range->start, range->end, DRM_MM_INSERT_BEST);
 }
 
 void
 ivpu_mmu_context_remove_node(struct ivpu_mmu_context *ctx, struct drm_mm_node *node)
 {
-	mutex_lock(&ctx->lock);
+	guard(spinlock)(&ctx->mm_lock);
 	drm_mm_remove_node(node);
-	mutex_unlock(&ctx->lock);
 }
 
 void ivpu_mmu_context_init(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx, u32 context_id)
@@ -580,6 +576,7 @@ void ivpu_mmu_context_init(struct ivpu_device *vdev, struct ivpu_mmu_context *ct
 	u64 start, end;
 
 	mutex_init(&ctx->lock);
+	spin_lock_init(&ctx->mm_lock);
 
 	if (!context_id) {
 		start = vdev->hw->ranges.runtime.start;
drivers/accel/ivpu/ivpu_mmu_context.h +4 -1
index cc02e7bab04e..9040d216ff2e 100644
@@ -6,6 +6,8 @@
 #ifndef __IVPU_MMU_CONTEXT_H__
 #define __IVPU_MMU_CONTEXT_H__
 
+#include <linux/spinlock.h>
+
 #include <drm/drm_mm.h>
 
 struct ivpu_device;
@@ -23,7 +25,8 @@ struct ivpu_mmu_pgtable {
 };
 
 struct ivpu_mmu_context {
-	struct mutex lock; /* Protects: mm, pgtable, is_cd_valid */
+	struct mutex lock; /* Protects: pgtable allocation and mapping, is_cd_valid */
+	spinlock_t mm_lock; /* Protects: mm */
 	struct drm_mm mm;
 	struct ivpu_mmu_pgtable pgtable;
 	bool is_cd_valid;
Signature · 1 line
2.43.0
#5 lore raw

PATCH 3/3 accel/ivpu: Shorten bo_list_lock hold time in BO free

In reply to Dawid Osuchowski

From: Jozef Wludzik <[email protected]>

ivpu_gem_bo_free() held bo_list_lock across the whole BO unbind, including MMU unmapping, TLB invalidation and DMA unmapping.

Unbind the BO under its reservation lock first, and take bo_list_lock only around list_del() at the end. Context teardown can still find the BO on the list until it is unbound, and the reservation lock serializes the two unbind paths, so the context outlives any unbind in progress in the free path.

---
 drivers/accel/ivpu/ivpu_gem.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/accel/ivpu/ivpu_gem.c +2 -3
index 276f2a04aeda..d4250d8d576a 100644
@@ -311,9 +311,6 @@ static void ivpu_gem_bo_free(struct drm_gem_object *obj)
 
 	drm_WARN_ON(&vdev->drm, list_empty(&bo->bo_list_node));
 
-	mutex_lock(&vdev->bo_list_lock);
-	list_del(&bo->bo_list_node);
-
 	drm_WARN_ON(&vdev->drm, !drm_gem_is_imported(&bo->base.base) &&
 		    !dma_resv_test_signaled(obj->resv, DMA_RESV_USAGE_READ));
 	drm_WARN_ON(&vdev->drm, ivpu_bo_size(bo) == 0);
@@ -323,6 +320,8 @@ static void ivpu_gem_bo_free(struct drm_gem_object *obj)
 	ivpu_bo_unbind_locked(bo);
 	ivpu_bo_unlock(bo);
 
+	mutex_lock(&vdev->bo_list_lock);
+	list_del(&bo->bo_list_node);
 	mutex_unlock(&vdev->bo_list_lock);
 
 	drm_WARN_ON(&vdev->drm, bo->mmu_mapped);
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH 1/3] accel/ivpu: Avoid taking the context lock when unmapping pages

In reply to Dawid Osuchowski
Quoted 15 lines · Dawid Osuchowski

From: Jozef Wludzik <[email protected]>

Unmapping a BO only invalidates leaf PTEs in its reserved VA range. The caller serializes access to that range and keeps the context and VA node alive until unmapping completes. Intermediate page tables remain allocated until context teardown.

Remove the context mutex from scatter-gather unmapping and use WRITE_ONCE() for leaf PTE invalidation. Keep the write barrier and TLB invalidation, as well as the existing locking for mapping, page-table allocation and drm_mm updates.

---Reviewed-by: Andrzej Kacprowski <[email protected]>

maintainer Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] accel/ivpu: Separate VA allocator locking from page-table locking

In reply to Dawid Osuchowski
Quoted 6 lines · Dawid Osuchowski

From: Jozef Wludzik <[email protected]>

The context mutex is held across reclaimable page-table allocations. Taking the same mutex to remove a VA node during BO destruction can deadlock when destruction runs in direct reclaim.

Quoted 8 lines

Protect drm_mm insertion and removal with a dedicated spinlock instead. These operations use preallocated nodes and do not sleep. Keep both alignment attempts under the same lock and retain the context mutex for page-table mapping, allocation and context descriptor updates.

---

maintainer Reviewed-by on v1 3/3
#6 lore raw

Re: [PATCH 3/3] accel/ivpu: Shorten bo_list_lock hold time in BO free

In reply to Dawid Osuchowski
Quoted 32 lines · Dawid Osuchowski

From: Jozef Wludzik <[email protected]>

ivpu_gem_bo_free() held bo_list_lock across the whole BO unbind, including MMU unmapping, TLB invalidation and DMA unmapping.

Unbind the BO under its reservation lock first, and take bo_list_lock only around list_del() at the end. Context teardown can still find the BO on the list until it is unbound, and the reservation lock serializes the two unbind paths, so the context outlives any unbind in progress in the free path.

---
  drivers/accel/ivpu/ivpu_gem.c | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)
drivers/accel/ivpu/ivpu_gem.c +0 -3
index 276f2a04aeda..d4250d8d576a 100644
@@ -311,9 +311,6 @@ static void ivpu_gem_bo_free(struct drm_gem_object *obj)
 
  	drm_WARN_ON(&vdev->drm, list_empty(&bo->bo_list_node));
 
-	mutex_lock(&vdev->bo_list_lock);
-	list_del(&bo->bo_list_node);
-
  	drm_WARN_ON(&vdev->drm, !drm_gem_is_imported(&bo->base.base) &&
  		    !dma_resv_test_signaled(obj->resv, DMA_RESV_USAGE_READ));
  	drm_WARN_ON(&vdev->drm, ivpu_bo_size(bo) == 0);

drivers/accel/ivpu/ivpu_gem.c static void ivpu_gem_bo_free(struct drm_gem_object *obj) · line 327

Quoted 8 lines
drivers/accel/ivpu/ivpu_gem.c +2 -0
@@ -323,6 +320,8 @@ static void ivpu_gem_bo_free(struct drm_gem_object *obj)
  	ivpu_bo_unbind_locked(bo);
  	ivpu_bo_unlock(bo);
 
+	mutex_lock(&vdev->bo_list_lock);
+	list_del(&bo->bo_list_node);
  	mutex_unlock(&vdev->bo_list_lock);
 
  	drm_WARN_ON(&vdev->drm, bo->mmu_mapped);