Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] drm/amdgpu: leak of ctx->new mappings in
  amdgpu_vm_update_ctx_rollback()
- [High] drm/amdgpu: missing ctx->cleared unlink in
  amdgpu_vm_update_ctx_rollback()
--

commit b40894c15667a95794d5db4dfea6f22963fb9ec5
Author: Natalie Vock <[email protected]>

drm/amdgpu: Add function to roll back a VM update ctx

This commit introduces amdgpu_vm_update_ctx_rollback() to discard pending
mapping changes in a VM update context and calls it from
amdgpu_vm_update_ctx_fini().

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c 
> b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> index 330c288053352..ac4a1ed9b1196 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> @@ -1874,37 +1874,48 @@ void amdgpu_vm_update_ctx_init(struct 
> amdgpu_vm_update_ctx *ctx,
>  }
>  
>  /**
> - * amdgpu_vm_update_ctx_fini - finish a VM update operation
> - * @ctx: The context of the update
> + * amdgpu_vm_update_ctx_rollback - clear pending VM updates
> + * @ctx: The context to roll back
>   *
> - * Adds any leftover mappings needing to be cleared to the VM's delayed free 
> list,
> - * then frees up resources used by the context.
> + * Cleans up any pending mapping changes, but does not reset
> + * anything else (like attached syncobjs).
>   */
> -void amdgpu_vm_update_ctx_fini(struct amdgpu_vm_update_ctx *ctx)
> +void amdgpu_vm_update_ctx_rollback(struct amdgpu_vm_update_ctx *ctx)
>  {
> +     struct amdgpu_bo_va_mapping *mapping, *next;
>       struct interval_tree_node *node, *next_node;
>       struct amdgpu_bo_va *bo_va, *next_bo_va;
>  
> -     list_splice(&ctx->freed, &ctx->vm->delayed_freed);
> -     amdgpu_sync_free(&ctx->sync);
> +     list_for_each_entry_safe(mapping, next, &ctx->new, pending_list)
> +             list_del_init(&mapping->pending_list);

[Severity: High]
Does this leak each struct amdgpu_bo_va_mapping on ctx->new?

Unlike mappings on ctx->deleted, newly allocated mappings added by
amdgpu_vm_update_ctx_add_new_mapping() are not inserted into bo_va->invalids
or vm->va until amdgpu_vm_update_ctx_commit() runs.  If a rollback or an
error before commit hits this loop, unlinking mapping->pending_list without
calling kfree(mapping) drops the only reference to those allocations.

> +     list_for_each_entry_safe(mapping, next, &ctx->deleted, pending_list)
> +             list_del_init(&mapping->pending_list);

[Severity: High]
Should amdgpu_vm_update_ctx_rollback() also unlink mappings on
ctx->cleared?

amdgpu_vm_bo_clear_mappings() links overlapping live mappings from vm->va
onto ctx->cleared via mapping->pending_list.  If a replace or clear
operation rolls back or fails before amdgpu_vm_update_ctx_execute_replace()
drains ctx->cleared, those mappings stay linked to ctx->cleared while
ctx->cleared_ranges is reset.  If the context is then destroyed,
list_empty(&mapping->pending_list) remains false on the live mappings in
vm->va, causing later unmaps and amdgpu_vm_bo_del() to skip removing them
from vm->va before freeing bo_va, which leaves dangling pointers and leads
to a use-after-free.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=6

Reply via email to