If MMU cache invalidation or prefetch setup fails after
map_phys_pg_pack() succeeds, map_device_va() returns the VA range
to the free list without removing the device page table entries.

Unmap the page pack and invalidate the MMU cache under mmu_lock
before continuing with the existing cleanup.

Fixes: 8ff5f4fd40df ("habanalabs: handle MMU cache invalidation timeout")
Signed-off-by: Haoxiang Li <[email protected]>
---
 drivers/accel/habanalabs/common/memory.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/drivers/accel/habanalabs/common/memory.c 
b/drivers/accel/habanalabs/common/memory.c
index 2f76776b4039..6269f9371426 100644
--- a/drivers/accel/habanalabs/common/memory.c
+++ b/drivers/accel/habanalabs/common/memory.c
@@ -1195,7 +1195,7 @@ static int map_device_va(struct hl_ctx *ctx, struct 
hl_mem_in *args, u64 *device
                                ctx->asid, ret_vaddr, phys_pg_pack->total_size);
        mutex_unlock(&hdev->mmu_lock);
        if (rc)
-               goto map_err;
+               goto range_err;
 
        /*
         * prefetch is done upon user's request. it is performed in WQ as and 
so can
@@ -1205,7 +1205,7 @@ static int map_device_va(struct hl_ctx *ctx, struct 
hl_mem_in *args, u64 *device
                rc = hl_mmu_prefetch_cache_range(ctx, *vm_type, ctx->asid, 
ret_vaddr,
                                                        
phys_pg_pack->total_size);
                if (rc)
-                       goto map_err;
+                       goto range_err;
        }
 
        ret_vaddr += phys_pg_pack->offset;
@@ -1225,6 +1225,14 @@ static int map_device_va(struct hl_ctx *ctx, struct 
hl_mem_in *args, u64 *device
 
        return rc;
 
+range_err:
+       mutex_lock(&hdev->mmu_lock);
+
+       unmap_phys_pg_pack(ctx, ret_vaddr, phys_pg_pack);
+       hl_mmu_invalidate_cache_range(hdev, true, *vm_type,
+                       ctx->asid, ret_vaddr, phys_pg_pack->total_size);
+
+       mutex_unlock(&hdev->mmu_lock);
 map_err:
        if (add_va_block(hdev, va_range, ret_vaddr,
                                ret_vaddr + phys_pg_pack->total_size - 1))
-- 
2.25.1

Reply via email to