Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] drm/amdgpu: missing amdgpu_vm_update_ctx_commit() in
  unmap_bo_from_gpuvm()
- [High] drm/amdgpu: unclamped cleared mapping unmaps surviving gaps in
  clear_freed()
- [Medium] drm/amdgpu: uncleaned pending lists in
  amdgpu_vm_update_ctx_fini() on error
- [Medium] drm/amdgpu: cur_end moves backward on nested ranges in
  execute_replace()
- [Medium] drm/amdgpu: 32-bit PFN truncation in cleared_ranges
  interval_tree_node
- [Low] drm/amdgpu: mismatched @replaced kernel-doc tag for cleared
  list_head
--

commit 2c940ee3e7377ed3b2aa248bd851e7573259f51f
Author: Natalie Vock <[email protected]>

drm/amdgpu: Make removed/cleared mappings pending per update context

This commit defers VM mapping removals and range clears into lists and
interval trees on struct amdgpu_vm_update_ctx until the context is
committed, and allows explicit synchronization when unmapping always-valid
BOs or PRT mappings.

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c 
> b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> index 97cf04da6a025..1e2d45979efdb 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c

[ ... ]

> @@ -1652,10 +1716,103 @@ amdgpu_vm_update_ctx_add_new_mapping(struct 
> amdgpu_vm_update_ctx *ctx,
>       interval_tree_insert(pending, &ctx->pending_maps);
>  
>       mapping->bo_va = bo_va;
> +     WARN_ON(!list_empty(&mapping->pending_list));
>       list_add(&mapping->pending_list, &ctx->new);
>       return 0;
>  }
>  
> +static void
> +amdgpu_vm_update_ctx_add_replaced_mapping(struct amdgpu_vm_update_ctx *ctx,
> +                                       struct amdgpu_bo_va_mapping *mapping)
> +{
> +     if (list_empty(&mapping->pending_list))
> +             list_add(&mapping->pending_list, &ctx->cleared);
> +}
> +
> +static void
> +amdgpu_vm_update_ctx_add_freed_mapping(struct amdgpu_vm_update_ctx *ctx,
> +                                    struct amdgpu_bo_va_mapping *mapping)
> +{
> +     WARN_ON(!list_empty(&mapping->pending_list));
> +     list_add(&mapping->pending_list, &ctx->deleted);
> +}
> +
> +int amdgpu_vm_update_ctx_execute_replace(struct amdgpu_vm_update_ctx *ctx)
> +{
> +     LIST_HEAD(new_maps);
> +     struct interval_tree_node *map;
> +     struct amdgpu_bo_va_mapping *cleared, *next, *gap;
> +     uint64_t cur_end;
> +     int r;
> +
> +     list_for_each_entry_safe(cleared, next, &ctx->cleared, pending_list) {
> +             cur_end = cleared->start;
> +
> +             map = interval_tree_iter_first(&ctx->cleared_ranges, 
> cleared->start, cleared->last);
> +             while (map && map->start <= cleared->last) {
> +                     if (map->start <= cur_end) {
> +                             cur_end = map->last + 1;
> +                             map = interval_tree_iter_next(map, 
> cleared->start, cleared->last);
> +                             continue;
> +                     }

[Severity: Medium]
Can cur_end move backward here when ctx->cleared_ranges contains nested or
overlapping intervals?  interval_tree_iter_first() and
interval_tree_iter_next() walk intervals in ascending order of map->start,
so if an earlier interval extends past a later one (for example, [0x10,
0x20] followed by [0x12, 0x15]), the second iteration sees map->start <=
cur_end and rewinds cur_end from 0x21 back to 0x16, causing the already
cleared subrange [0x16, 0x20] to be treated as an uncleared gap and
re-mapped (or rejected as a duplicate in
amdgpu_vm_update_ctx_add_new_mapping()).  Using max(cur_end, map->last + 1)
would prevent cur_end from decreasing.

> +
> +                     gap = kmalloc_obj(*gap);
> +                     if (!gap) {
> +                             r = -ENOMEM;
> +                             goto error;
> +                     }
> +
> +                     gap->bo_va = cleared->bo_va;
> +                     gap->start = cur_end;
> +                     gap->last = map->start - 1;
> +                     gap->offset = cleared->offset;
> +                     gap->offset += (gap->start - cleared->start) << 
> AMDGPU_GPU_PAGE_SHIFT;
> +                     gap->flags = cleared->flags;
> +                     list_add(&gap->pending_list, &new_maps);
> +
> +                     cur_end = map->last + 1;
> +                     map = interval_tree_iter_next(map, cleared->start, 
> cleared->last);
> +             }
> +
> +             if (cur_end <= cleared->last) {
> +                     gap = kmalloc_obj(*gap);
> +                     if (!gap) {
> +                             r = -ENOMEM;
> +                             goto error;
> +                     }
> +
> +                     gap->bo_va = cleared->bo_va;
> +                     gap->start = cur_end;
> +                     gap->last = cleared->last;
> +                     gap->offset = cleared->offset;
> +                     gap->offset += (gap->start - cleared->start) << 
> AMDGPU_GPU_PAGE_SHIFT;
> +                     gap->flags = cleared->flags;
> +                     list_add(&gap->pending_list, &new_maps);
> +             }
> +
> +             list_del_init(&cleared->pending_list);
> +             amdgpu_vm_update_ctx_add_freed_mapping(ctx, cleared);
> +     }

[Severity: High]
Does queueing the unclamped cleared mapping onto ctx->deleted cause
amdgpu_vm_clear_freed() to wipe the hardware PTEs for the surviving gap
subranges too?

Previously, amdgpu_vm_bo_clear_mappings() clamped tmp->start and tmp->last
to [saddr, eaddr] before adding tmp to ctx->freed, so
amdgpu_vm_clear_freed() only cleared PTEs in the unmapped range.  With this
change, cleared keeps its full [cleared->start, cleared->last] span when
amdgpu_vm_bo_commit_remove() moves it to ctx->freed, while the surviving
gap mappings are added to cleared->bo_va->invalids.  In
amdgpu_gem_va_update_vm(), amdgpu_vm_clear_freed() then zeroes the hardware
PTEs across the entire original mapping, and the surviving gaps are never
re-populated in the page tables for AMDGPU_VA_OP_CLEAR (which never calls
amdgpu_vm_bo_update()) or for AMDGPU_VA_OP_REPLACE when the split mapping
belongs to another BO or fpriv->prt_va:

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c:amdgpu_gem_va_update_vm() {
    ...
        r = amdgpu_vm_clear_freed(ctx, &ctx->vm->last_update);
        if (r)
                goto error;

        /* For MAP/REPLACE we also need to update the BO mappings. */
        if (operation == AMDGPU_VA_OP_MAP ||
            operation == AMDGPU_VA_OP_REPLACE) {
                r = amdgpu_vm_bo_update(ctx, bo_va, false);
                if (r)
                        goto error;
        }
    ...
}

Even when replacing a subrange with the same bo_va, amdgpu_vm_clear_freed()
clears the full original range and flushes the TLB before
amdgpu_vm_bo_update() restores the surviving subranges, which can fault
concurrent GPU work accessing the unmodified parts of the mapping.

[ ... ]

> @@ -1715,17 +1877,23 @@ void amdgpu_vm_update_ctx_init(struct 
> amdgpu_vm_update_ctx *ctx,
>   */
>  void amdgpu_vm_update_ctx_fini(struct amdgpu_vm_update_ctx *ctx)
>  {
> -     struct interval_tree_node *pending, *next_pending;
> +     struct interval_tree_node *node, *next_node;
>  
>       list_splice(&ctx->freed, &ctx->vm->delayed_freed);
>       amdgpu_sync_free(&ctx->sync);
>  
> -     rbtree_postorder_for_each_entry_safe(pending, next_pending, 
> &ctx->pending_maps.rb_root, rb)
> -             kfree(pending);
> +     rbtree_postorder_for_each_entry_safe(node, next_node, 
> &ctx->pending_maps.rb_root, rb) {
> +             kfree(node);
> +     }
> +     rbtree_postorder_for_each_entry_safe(node, next_node, 
> &ctx->cleared_ranges.rb_root, rb) {
> +             kfree(node);
> +     }
>  
>       ctx->pending_maps = RB_ROOT_CACHED;
> +     ctx->cleared_ranges = RB_ROOT_CACHED;
>  
>       WARN_ON(!list_empty(&ctx->new));
> +     WARN_ON(!list_empty(&ctx->deleted));
>  }

[Severity: Medium]
If an error occurs before amdgpu_vm_update_ctx_commit() is called—for
example, when amdgpu_vm_update_ctx_add_new_mapping() fails in
amdgpu_vm_bo_replace_map() after amdgpu_vm_bo_clear_mappings() has already
populated ctx->cleared, or when an allocation or mapping insertion fails
midway through amdgpu_vm_update_ctx_execute_replace()—does
amdgpu_vm_update_ctx_fini() leak ctx->new mappings and leave live mappings
linked into the stack-allocated ctx->deleted and ctx->cleared lists?

In this commit, jumping to error in amdgpu_gem_va_ioctl() hits the WARN_ON()
checks, leaks any struct amdgpu_bo_va_mapping objects already moved to
ctx->new, and leaves existing mappings with mapping->pending_list pointing
into the popped update_ctx stack frame.  Note that commit "drm/amdgpu: Add
function to roll back a VM update ctx" later in the series adds
amdgpu_vm_update_ctx_rollback(), but that helper still only unlinks
ctx->new entries without calling kfree(mapping) and does not unlink entries
on ctx->cleared at all.

[ ... ]

> @@ -2178,24 +2345,7 @@ int amdgpu_vm_bo_unmap(struct amdgpu_vm_update_ctx 
> *ctx,
>                       return -ENOENT;
>       }
>  
> -     /* It's unlikely to happen that the mapping userq hasn't been idled
> -      * during user requests GEM unmap IOCTL except for forcing the unmap
> -      * from user space.
> -      */
> -     if (unlikely(bo_va->userq_va_mapped))
> -             amdgpu_userq_gem_va_unmap_validate(ctx->adev, mapping);
> -
> -     list_del(&mapping->list);
> -     amdgpu_vm_it_remove(mapping, &vm->va);
> -     mapping->bo_va = NULL;
> -     trace_amdgpu_vm_bo_unmap(bo_va, mapping);
> -
> -     if (valid)
> -             list_add(&mapping->list, &ctx->freed);
> -     else
> -             amdgpu_vm_free_mapping(ctx->adev, vm, mapping,
> -                                    bo_va->last_pt_update);
> -
> +     amdgpu_vm_update_ctx_add_freed_mapping(ctx, mapping);
>       return 0;
>  }

[Severity: High]
Does unmap_bo_from_gpuvm() also need to call amdgpu_vm_update_ctx_commit()
after amdgpu_vm_bo_unmap()?

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c:unmap_bo_from_gpuvm() {
        struct amdgpu_bo_va *bo_va = entry->bo_va;
        struct amdgpu_vm *vm = peer_vm;
        struct amdgpu_vm_update_ctx update_ctx;

        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() now only queues the mapping onto
update_ctx.deleted until amdgpu_vm_update_ctx_commit() runs,
unmap_bo_from_gpuvm() passes an empty update_ctx.freed list to
amdgpu_vm_clear_freed() and never removes the mapping from bo_va or vm->va.
Every KFD unmap leaves the GPU page tables mapped, triggers
WARN_ON(!list_empty(&ctx->deleted)) in amdgpu_vm_update_ctx_fini(), and
leaves mapping->pending_list pointing at the stack-allocated update_ctx (or
rolls the unmap back once commit "drm/amdgpu: Add function to roll back a
VM update ctx" is applied).

>  
> @@ -2214,8 +2364,8 @@ int amdgpu_vm_bo_unmap(struct amdgpu_vm_update_ctx *ctx,
>  int amdgpu_vm_bo_clear_mappings(struct amdgpu_vm_update_ctx *ctx,
>                               uint64_t saddr, uint64_t size)
>  {
> -     struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
> -     LIST_HEAD(removed);
> +     struct interval_tree_node *cleared_range;
> +     struct amdgpu_bo_va_mapping *tmp;
>       uint64_t eaddr;
>       int r;
>  
> @@ -2223,96 +2373,23 @@ int amdgpu_vm_bo_clear_mappings(struct 
> amdgpu_vm_update_ctx *ctx,
>       if (r)
>               return r;
>  
> -     saddr /= AMDGPU_GPU_PAGE_SIZE;
> -     eaddr = saddr + (size - 1) / AMDGPU_GPU_PAGE_SIZE;
> -
> -     /* Allocate all the needed memory */
> -     before = kzalloc_obj(*before);
> -     if (!before)
> +     cleared_range = kmalloc_obj(*cleared_range);
> +     if (!cleared_range)
>               return -ENOMEM;
> -     INIT_LIST_HEAD(&before->list);
>  
> -     after = kzalloc_obj(*after);
> -     if (!after) {
> -             kfree(before);
> -             return -ENOMEM;
> -     }
> -     INIT_LIST_HEAD(&after->list);
> +     saddr /= AMDGPU_GPU_PAGE_SIZE;
> +     eaddr = saddr + (size - 1) / AMDGPU_GPU_PAGE_SIZE;
>  
>       /* Now gather all removed mappings */
>       tmp = amdgpu_vm_it_iter_first(&ctx->vm->va, saddr, eaddr);
>       while (tmp) {
> -             /* Remember mapping split at the start */
> -             if (tmp->start < saddr) {
> -                     before->start = tmp->start;
> -                     before->last = saddr - 1;
> -                     before->offset = tmp->offset;
> -                     before->flags = tmp->flags;
> -                     before->bo_va = tmp->bo_va;
> -                     list_add(&before->list, &tmp->bo_va->invalids);
> -             }
> -
> -             /* Remember mapping split at the end */
> -             if (tmp->last > eaddr) {
> -                     after->start = eaddr + 1;
> -                     after->last = tmp->last;
> -                     after->offset = tmp->offset;
> -                     after->offset += (after->start - tmp->start) << 
> AMDGPU_GPU_PAGE_SHIFT;
> -                     after->flags = tmp->flags;
> -                     after->bo_va = tmp->bo_va;
> -                     list_add(&after->list, &tmp->bo_va->invalids);
> -             }
> -
> -             list_del(&tmp->list);
> -             list_add(&tmp->list, &removed);
> -
> +             amdgpu_vm_update_ctx_add_replaced_mapping(ctx, tmp);
>               tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
>       }
>  
> -     /* And free them up */
> -     list_for_each_entry_safe(tmp, next, &removed, list) {
> -             amdgpu_vm_it_remove(tmp, &ctx->vm->va);
> -             list_del(&tmp->list);
> -
> -             if (tmp->start < saddr)
> -                 tmp->start = saddr;
> -             if (tmp->last > eaddr)
> -                 tmp->last = eaddr;
> -
> -             tmp->bo_va = NULL;
> -             list_add(&tmp->list, &ctx->freed);
> -             trace_amdgpu_vm_bo_unmap(NULL, tmp);
> -     }
> -
> -     /* Insert partial mapping before the range */
> -     if (!list_empty(&before->list)) {
> -             struct amdgpu_bo *bo = before->bo_va->base.bo;
> -
> -             amdgpu_vm_it_insert(before, &ctx->vm->va);
> -             if (before->flags & AMDGPU_VM_PAGE_PRT)
> -                     amdgpu_vm_prt_get(ctx->adev);
> -
> -             if (amdgpu_vm_is_bo_always_valid(ctx->vm, bo) &&
> -                 !before->bo_va->base.moved)
> -                     amdgpu_vm_bo_needs_update(&before->bo_va->base);
> -     } else {
> -             kfree(before);
> -     }
> -
> -     /* Insert partial mapping after the range */
> -     if (!list_empty(&after->list)) {
> -             struct amdgpu_bo *bo = after->bo_va->base.bo;
> -
> -             amdgpu_vm_it_insert(after, &ctx->vm->va);
> -             if (after->flags & AMDGPU_VM_PAGE_PRT)
> -                     amdgpu_vm_prt_get(ctx->adev);
> -
> -             if (amdgpu_vm_is_bo_always_valid(ctx->vm, bo) &&
> -                 !after->bo_va->base.moved)
> -                     amdgpu_vm_bo_needs_update(&after->bo_va->base);
> -     } else {
> -             kfree(after);
> -     }
> +     cleared_range->start = saddr;
> +     cleared_range->last = eaddr;
> +     interval_tree_insert(cleared_range, &ctx->cleared_ranges);

[Severity: Medium]
On 32-bit architectures, does storing 64-bit GPU page frame numbers into
struct interval_tree_node truncate saddr and eaddr?

struct interval_tree_node uses unsigned long for start and last, whereas GPU
PFNs are uint64_t (up to 36 bits on 48-bit GPU VA spaces, which is why
amdgpu_vm.c defines its own uint64_t amdgpu_vm_it interval tree).  When a
mapping at or above 16 TiB (PFN >= 1ULL << 32) is cleared on a 32-bit build,
cleared_range->start and cleared_range->last are truncated to 32 bits; in
amdgpu_vm_update_ctx_execute_replace(), cur_end = map->last + 1 then drops
below the 64-bit cleared->start, causing (gap->start - cleared->start) <<
AMDGPU_GPU_PAGE_SHIFT to underflow and produce an out-of-bounds gap->offset.

[ ... ]

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h 
> b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
> index d35494440cb5a..5657aa63e9491 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
> @@ -284,11 +284,27 @@ struct amdgpu_vm_update_ctx {
>        */
>       struct rb_root_cached pending_maps;
>  
> +     /**
> +      * @cleared_ranges: Interval tree of VA ranges cleared by this update 
> operation.
> +      */
> +     struct rb_root_cached cleared_ranges;
> +
>       /**
>        * @new: Pending new mappings not yet applied to bookkeeping structures.
>        */
>       struct list_head new;
>  
> +     /**
> +      * @deleted: Pending freed mappings not yet applied to bookkeeping 
> structures.
> +      */
> +     struct list_head deleted;
> +
> +     /**
> +      * @replaced: Pending mappings that have been cleared, potentially 
> partially,
> +      * and are not yet applied to bookkeeping structures.
> +      */
> +     struct list_head cleared;

[Severity: Low]
This kernel-doc comment uses @replaced instead of @cleared to document
struct list_head cleared, which triggers a kernel-doc warning on W=1 builds.

-- 
Sashiko AI review · 
https://sashiko.dev/#/patchset/[email protected]?part=4

Reply via email to