From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796380696; b=TCeZxxYhKodhqdRRUkDm8vlO9fN44Q5ZXUfrHXJ/9iBNZbtdSd/3tkcyz5SvaJEikuI8 s9PiBIWVmH91jRgtBDXQXVVkkEE8vw2x/5nXCauH005vMO/p27BMjrQdQid/New+UjbI/ +CeOrmCIjbAmd7a7IkKlogojoNp578aW7mF4FDkpNuK7nyMDbfJId8JNUqBkzsp0T/Zad 2nHndlqHPckh+anl1l1dsLwhsV6fNtOz09KgMvFoPVexYOwcrVL9Ba4PJxgFl890T0ybB 2ouZWQLs/FM0Mn0+yJoqHAoysmd1ROMYmPBbeyV9UBiPei1UeQX/BSqFr/2PuLkssxw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796380696; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Type:Content-Transfer-Encoding; bh=glyXiWBnHPkeVS0q6S1X1lPSY0LyZIdBK3DwZhDn0es=; b=h84DRdSuzW90yTzTeohf56YC3pJWu/Jin139rUpJnP5YrkiQQuwKIKcuUsedJV9lTfr4 j/Ap+xAOsxup01UlZc6d7dt5Ubt0LBVRGppJSTLQ5485slDeXuvjf7dhwviRBeb1vkt/u ytv3zJm3duV5lNU0IxgVSm32txd5fYCjycC8DgE4mGwUspt49oFVuP/LQIsO83nwbp/RI XiqXTWNnBXEjpb0XU7wYHQVwFNQKU8lJxVXuacvb2VjT3ZdADU/7XmuIeyZlDjOYT+4SX AlpR8kiA2yGOturv4U0IcSn18Aywiaq0YlwsCJiHhr6OFIEnhsn3VJS8EQ5UBfxoF2A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=Hh3lIZeC; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B7853606A322; Fri, 4 Dec 2026 10:38:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=Hh3lIZeC Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2FADC1F000C2; Fri, 4 Dec 2026 10:38:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796380696; bh=glyXiWBnHPkeVS0q6S1X1lPSY0LyZIdBK3DwZhDn0es=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Hh3lIZeCq653MVGl/Y5NCcAs/6I1QMg1PXXzax66QPM2sUNYhMTsX1hAif7zcsor5 jx4G+RM6dUCMfTqgxt18uLmLxNLmffPNYaASjEEMXK8uFQElM/hckQCySW7Y7MGuiW e27/X/X0dBYqHC37K/7P2EBF2zHOKtRtY6qFhzjQ= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, =?UTF-8?q?Christian=20K=C3=B6nig?= , Felix Kuehling , Alex Deucher Subject: [PATCH 7.3 384/742] drm/amdgpu: fix TLB invalidation fence handling v4 Date: Fri, 4 Dec 2026 19:04:08 +0900 Message-ID: <20261204100645.287623421@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: Christian König commit 566ed98e48be75a76eea34745815a51ba36687a8 upstream. After each unmap it might be necessary to invalidate the TLB to flush out VM changes to the HW and make it visible to executing work. We tracked that as part of the vm->last_update fence, but as pointed out by David it can be that this is overwritten by PDE updates when we have a combined unmap/map operation. In that case userspace doesn't correctly wait for the TLB invalidation to finish leading to all kind of problems (sq_int, page faults etc...). v2: fix ref leaking and error handling as pointed out by AI v3: fix inverted ! pointed out by Benjamin v4: fix || / && pointed out by Benjamin Signed-off-by: Christian König Reviewed-by: Felix Kuehling Signed-off-by: Alex Deucher (cherry picked from commit 892f46c7ae70969dbe0e0b88b5864df6f5582b98) Cc: stable@vger.kernel.org Signed-off-by: Greg Kroah-Hartman --- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 32 ++++++++++-------- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 39 +++++++++++------------ drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 13 ++++--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c | 22 ++++++++++-- 4 files changed, 60 insertions(+), 46 deletions(-) --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c @@ -801,8 +801,10 @@ amdgpu_gem_va_update_vm(struct amdgpu_de /* Always update PDEs after we touched the mappings. */ r = amdgpu_vm_update_pdes(adev, vm, false); - if (r) - goto error; + +error: + if (r && r != -ERESTARTSYS) + DRM_ERROR("Couldn't update BO_VA (%d)\n", r); if ((operation == AMDGPU_VA_OP_MAP || operation == AMDGPU_VA_OP_REPLACE) && @@ -811,27 +813,27 @@ amdgpu_gem_va_update_vm(struct amdgpu_de /* * For MAP/REPLACE of non per-VM BOs we need to sync to both the * bo_va->last_pt_update and vm->last_update or otherwise we - * potentially miss the PDE updates. + * potentially miss the PDE updates or TLB invalidation. */ fence = dma_fence_unwrap_merge(vm->last_update, + vm->last_tlb_inv, bo_va->last_pt_update); - if (!fence) { - /* As fallback in OOM situations */ - dma_fence_wait(vm->last_update, false); + if (!fence) dma_fence_wait(bo_va->last_pt_update, false); - fence = dma_fence_get_stub(); - } + } else { - fence = dma_fence_get(vm->last_update); + fence = dma_fence_unwrap_merge(vm->last_update, + vm->last_tlb_inv); } - return fence; - -error: - if (r && r != -ERESTARTSYS) - DRM_ERROR("Couldn't update BO_VA (%d)\n", r); + if (!fence) { + /* As fallback in OOM situations */ + dma_fence_wait(vm->last_update, false); + dma_fence_wait(vm->last_tlb_inv, false); + fence = dma_fence_get_stub(); + } - return dma_fence_get(vm->last_update); + return fence; } int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data, --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c @@ -1083,21 +1083,16 @@ static void amdgpu_vm_tlb_seq_cb(struct */ static void amdgpu_vm_tlb_flush(struct amdgpu_vm_update_params *params, - struct dma_fence **fence, + struct dma_fence *fence, struct amdgpu_vm_tlb_seq_struct *tlb_cb) { struct amdgpu_vm *vm = params->vm; tlb_cb->vm = vm; - if (!fence || !*fence) { - amdgpu_vm_tlb_seq_cb(NULL, &tlb_cb->cb); - return; - } - - if (!dma_fence_add_callback(*fence, &tlb_cb->cb, - amdgpu_vm_tlb_seq_cb)) { - dma_fence_put(vm->last_tlb_flush); - vm->last_tlb_flush = dma_fence_get(*fence); + if (fence && !dma_fence_add_callback(fence, &tlb_cb->cb, + amdgpu_vm_tlb_seq_cb)) { + dma_fence_put(vm->last_seq_flush); + vm->last_seq_flush = dma_fence_get(fence); } else { amdgpu_vm_tlb_seq_cb(NULL, &tlb_cb->cb); } @@ -1107,11 +1102,12 @@ amdgpu_vm_tlb_flush(struct amdgpu_vm_upd * sort out the issues with KIQ/MES TLB invalidation timeouts. */ if (!params->unlocked && vm->need_tlb_fence) { - amdgpu_vm_tlb_fence_create(params->adev, vm, fence); + amdgpu_vm_tlb_fence_create(params->adev, vm, fence, + &vm->last_tlb_inv); /* Makes sure no PD/PT is freed before the flush */ - dma_resv_add_fence(vm->root.bo->tbo.base.resv, *fence, - DMA_RESV_USAGE_BOOKKEEP); + dma_resv_add_fence(vm->root.bo->tbo.base.resv, + vm->last_tlb_inv, DMA_RESV_USAGE_BOOKKEEP); } } @@ -1260,7 +1256,7 @@ int amdgpu_vm_update_range(struct amdgpu goto error_free; if (params.needs_flush) { - amdgpu_vm_tlb_flush(¶ms, fence, tlb_cb); + amdgpu_vm_tlb_flush(¶ms, *fence, tlb_cb); tlb_cb = NULL; } @@ -2647,7 +2643,8 @@ int amdgpu_vm_init(struct amdgpu_device vm->last_update = dma_fence_get_stub(); vm->last_unlocked = dma_fence_get_stub(); - vm->last_tlb_flush = dma_fence_get_stub(); + vm->last_seq_flush = dma_fence_get_stub(); + vm->last_tlb_inv = dma_fence_get_stub(); vm->generation = amdgpu_vm_generation(adev, NULL); mutex_init(&vm->eviction_lock); @@ -2691,7 +2688,8 @@ error_free_root: error_free_delayed: dma_fence_put(vm->last_update); - dma_fence_put(vm->last_tlb_flush); + dma_fence_put(vm->last_seq_flush); + dma_fence_put(vm->last_tlb_inv); dma_fence_put(vm->last_unlocked); ttm_lru_bulk_move_fini(&adev->mman.bdev, &vm->lru_bulk_move); amdgpu_vm_fini_entities(vm); @@ -2791,11 +2789,12 @@ void amdgpu_vm_fini(struct amdgpu_device amdgpu_bo_reserve(root, true); dma_fence_wait(vm->last_unlocked, false); dma_fence_put(vm->last_unlocked); - dma_fence_wait(vm->last_tlb_flush, false); + dma_fence_wait(vm->last_seq_flush, false); /* Make sure that all fence callbacks have completed */ - dma_fence_lock_irqsave(vm->last_tlb_flush, flags); - dma_fence_unlock_irqrestore(vm->last_tlb_flush, flags); - dma_fence_put(vm->last_tlb_flush); + dma_fence_lock_irqsave(vm->last_seq_flush, flags); + dma_fence_unlock_irqrestore(vm->last_seq_flush, flags); + dma_fence_put(vm->last_seq_flush); + dma_fence_put(vm->last_tlb_inv); list_for_each_entry_safe(mapping, tmp, &vm->freed, list) { if (mapping->flags & AMDGPU_VM_PAGE_PRT && prt_fini_needed) { --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h @@ -401,11 +401,12 @@ struct amdgpu_vm { struct drm_sched_entity immediate; struct drm_sched_entity delayed; - /* Last finished delayed update */ + /* Last TLB seq/fence */ atomic64_t tlb_seq; - struct dma_fence *last_tlb_flush; + struct dma_fence *last_seq_flush; atomic64_t kfd_last_flushed_seq; uint64_t tlb_fence_context; + struct dma_fence *last_tlb_inv; /* How many times we had to re-generate the page tables */ uint64_t generation; @@ -642,7 +643,7 @@ static inline uint64_t amdgpu_vm_tlb_seq * sure that the dma_fence structure isn't freed up. */ rcu_read_lock(); - lock = dma_fence_spinlock(vm->last_tlb_flush); + lock = dma_fence_spinlock(vm->last_seq_flush); rcu_read_unlock(); spin_lock_irqsave(lock, flags); @@ -683,9 +684,9 @@ void amdgpu_vm_update_fault_cache(struct uint32_t status, unsigned int vmhub); void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev, - struct amdgpu_vm *vm, - struct dma_fence **fence); - + struct amdgpu_vm *vm, + struct dma_fence *dep, + struct dma_fence **fence); void amdgpu_vm_print_task_info(struct amdgpu_device *adev, struct amdgpu_task_info *task_info); --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c @@ -75,7 +75,19 @@ static const struct dma_fence_ops amdgpu .get_timeline_name = amdgpu_tlb_fence_get_timeline_name }; -void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev, struct amdgpu_vm *vm, +/** + * amdgpu_vm_tlb_fence_create - create a TLB invalidation fence + * @adev: the amdgpu device + * @vm: the vm to use + * @dep: the dependency to wait for + * @fence: the resulting fence + * + * Creates a TLB invalidation fence which signals when all the VMIDs assigned to + * the PASID of the VM are invalidated. + */ +void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev, + struct amdgpu_vm *vm, + struct dma_fence *dep, struct dma_fence **fence) { struct amdgpu_tlb_fence *f; @@ -86,16 +98,15 @@ void amdgpu_vm_tlb_fence_create(struct a * We can't fail since the PDEs and PTEs are already updated, so * just block for the dependency and execute the TLB flush */ - if (*fence) - dma_fence_wait(*fence, false); + if (dep) + dma_fence_wait(dep, false); amdgpu_gmc_flush_gpu_tlb_pasid(adev, vm->pasid, 2, true, 0); - *fence = dma_fence_get_stub(); return; } f->adev = adev; - f->dependency = *fence; + f->dependency = dma_fence_get(dep); f->pasid = vm->pasid; INIT_WORK(&f->work, amdgpu_tlb_fence_work); spin_lock_init(&f->lock); @@ -107,5 +118,6 @@ void amdgpu_vm_tlb_fence_create(struct a dma_fence_get(&f->base); schedule_work(&f->work); + dma_fence_put(*fence); *fence = &f->base; }