Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [Critical] drm/amdgpu: amdgpu_vm_update_ctx_rollback() fails to unlink ctx->cleared - [High] drm/amdgpu: missing interval tree iteration in amdgpu_vm_bo_map() - [High] drm/amdgpu: amdgpu_vm_bo_map() ignores conflicts on partially cleared ranges - [High] drm/amdgpu: amdgpu_vm_update_ctx_rollback() leaks ctx->new mappings - [High] drm/amdgpu: missing amdgpu_vm_update_ctx_commit() in unmap_bo_from_gpuvm() --
commit 4bdb539ca2c3dc59efa5a70eaf09b4e87c351cb1 Author: Natalie Vock <[email protected]> drm/amdgpu: Only reject conflicting mappings if they are not cleared This commit updates amdgpu_vm_bo_map() to only reject an overlapping mapping in the VM interval tree when its pending_list is empty. > diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c > b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c > index ac4a1ed9b1196..491f4b50f7602 100644 > --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c > +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c > @@ -2241,7 +2241,7 @@ int amdgpu_vm_bo_map(struct amdgpu_vm_update_ctx *ctx, > eaddr = saddr + (size - 1) / AMDGPU_GPU_PAGE_SIZE; > > tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr); > - if (tmp) { > + if (tmp && list_empty(&tmp->pending_list)) { > /* bo and tmp overlap, invalid addr */ > dev_err(ctx->adev->dev, > "bo %p va 0x%010Lx-0x%010Lx conflict with " [Severity: High] Can amdgpu_vm_bo_map() miss a conflict when [saddr, eaddr] overlaps more than one mapping in vm->va? If the first node returned by amdgpu_vm_it_iter_first() has a non-empty pending_list (because it was unmapped or cleared earlier in the same update context) and a subsequent overlapping node in [saddr, eaddr] is still active with an empty pending_list, amdgpu_vm_bo_map() never walks the remaining nodes via amdgpu_vm_it_iter_next(). amdgpu_vm_update_ctx_commit() then inserts the new mapping into vm->va on top of the active mapping. Should this loop with amdgpu_vm_it_iter_next(tmp, saddr, eaddr) over all overlapping nodes in [saddr, eaddr] and reject the map if any node has an empty pending_list? [Severity: High] Does checking list_empty(&tmp->pending_list) also allow mapping over the uncleared portion of a partially cleared mapping? When amdgpu_vm_bo_clear_mappings() partially clears an existing mapping tmp, it adds the entire unsplit mapping to ctx->cleared via tmp->pending_list while recording only the cleared sub-range in ctx->cleared_ranges: drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c:amdgpu_vm_bo_clear_mappings() { ... tmp = amdgpu_vm_it_iter_first(&ctx->vm->va, saddr, eaddr); while (tmp) { amdgpu_vm_update_ctx_add_replaced_mapping(ctx, tmp); tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr); } cleared_range->start = saddr; cleared_range->last = eaddr; interval_tree_insert(cleared_range, &ctx->cleared_ranges); ... } Because list_empty(&tmp->pending_list) is false across all of [tmp->start, tmp->last], a subsequent amdgpu_vm_bo_map() overlapping the still-active part of tmp is not rejected here. If a later clear in the same update context covers that remaining range, amdgpu_vm_update_ctx_execute_replace() produces no gap mapping to collide with ctx->pending_maps, so the invalid map succeeds without ever reporting a conflict. [Severity: Critical] What happens to tmp->pending_list when an update context is rolled back after staging cleared mappings on ctx->cleared? In commit "drm/amdgpu: Add function to roll back a VM update ctx", amdgpu_vm_update_ctx_rollback() unlinks ctx->new and ctx->deleted, but does not unlink ctx->cleared: drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c:amdgpu_vm_update_ctx_rollback() { ... list_for_each_entry_safe(mapping, next, &ctx->new, pending_list) list_del_init(&mapping->pending_list); list_for_each_entry_safe(mapping, next, &ctx->deleted, pending_list) list_del_init(&mapping->pending_list); ... } If an operation rolls back after amdgpu_vm_bo_clear_mappings() or amdgpu_vm_bo_replace_map() has placed a live mapping onto ctx->cleared, that mapping stays in vm->va with mapping->pending_list pointing into the expired stack-allocated update_ctx.cleared head. Because list_empty(&mapping->pending_list) then remains false, this check in amdgpu_vm_bo_map() treats the live mapping as cleared and allows conflicting mappings over it. In addition, amdgpu_vm_update_ctx_add_freed_mapping() skips any mapping whose pending_list is not empty, so amdgpu_vm_bo_del() frees bo_va while leaving its mapping in vm->va with a dangling bo_va pointer, leading to a use-after-free on later VM interval tree walks. [Severity: High] Does rolling back a context after amdgpu_vm_bo_map() queues a new mapping leak the mapping allocation? amdgpu_vm_bo_map() allocates mapping with kmalloc_obj(*mapping) and queues it on ctx->new via amdgpu_vm_update_ctx_add_new_mapping(), only inserting it into vm->va and bo_va->invalids when amdgpu_vm_update_ctx_commit() runs. When a later step fails (such as amdgpu_vm_update_ctx_execute_replace() after mapping over a partially cleared range) or hits lock contention, amdgpu_vm_update_ctx_rollback() calls list_del_init(&mapping->pending_list) on each entry in ctx->new without freeing mapping. [Severity: High] In commit "drm/amdgpu: Make removed/cleared mappings pending per update context", does unmap_bo_from_gpuvm() also miss a call to amdgpu_vm_update_ctx_commit() before clearing freed mappings? drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c:unmap_bo_from_gpuvm() { ... amdgpu_vm_update_ctx_init(&update_ctx, adev, vm); (void)amdgpu_vm_bo_unmap(&update_ctx, bo_va, entry->va); /* VM entity stopped if process killed, don't clear freed pt bo */ if (!amdgpu_vm_ready(vm)) goto out; (void)amdgpu_vm_clear_freed(&update_ctx, &bo_va->last_pt_update); (void)amdgpu_sync_fence(sync, bo_va->last_pt_update, GFP_KERNEL); out: amdgpu_vm_update_ctx_fini(&update_ctx); return 0; } Because amdgpu_vm_bo_unmap() only stages the mapping on update_ctx.deleted until amdgpu_vm_update_ctx_commit() moves it to update_ctx.freed and removes it from vm->va, amdgpu_vm_clear_freed() sees an empty freed list and amdgpu_vm_update_ctx_fini() rolls the unmap back, leaving the mapping active in vm->va. -- Sashiko AI review ยท https://sashiko.dev/#/patchset/[email protected]?part=8
