Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] gpu/buddy: u64 underflow on inverted range in
  __alloc_range_bias()
--

commit 84539d9e7a89604b1f5d66dfb390e667e4fccb69
Author: William Palacek <[email protected]>

gpu/buddy: find range-biased blocks in the free trees

This commit replaces the root-to-leaf block tree walk in
__alloc_range_bias() with augmented RB-tree lookups in the per-order
free trees, adding a subtree_has_dirty augment to skip all-clear
subtrees on dirty requests.

> diff --git a/drivers/gpu/buddy.c b/drivers/gpu/buddy.c
> index e7d2212bcec95..baf17700ffba5 100644
> --- a/drivers/gpu/buddy.c
> +++ b/drivers/gpu/buddy.c

[ ... ]

> @@ -1247,96 +1262,155 @@ static bool block_incompatible(struct 
> gpu_buddy_block *block, unsigned int flags
>                            gpu_buddy_block_is_clear(block);
>  }
>  
> -static struct gpu_buddy_block *
> -__alloc_range_bias(struct gpu_buddy *mm,
> -                u64 start, u64 end,
> -                unsigned int order,
> -                unsigned long flags,
> -                bool fallback)

[ ... ]

> +static struct gpu_buddy_block *
> +__alloc_range_bias(struct gpu_buddy *mm,
> +                u64 start, u64 end,
> +                unsigned int order,
> +                unsigned long flags,
> +                bool fallback)
> +{
> +     u64 req_size = mm->chunk_size << order;
> +     struct gpu_buddy_block *block = NULL;
> +     u64 best = 0;
> +     unsigned int i;
> +     int err;
>  
> +     for (i = order; i <= mm->max_order; i++) {
> +             struct rb_root *root = &mm->free_tree[i];
> +             u64 size = mm->chunk_size << i;
> +             struct gpu_buddy_block *found;
> +             u64 offset = 0;
>  
> +             if (!fallback) {
> +                     if (end - start < size)
> +                             break;
>  
> +                     found = rbtree_last_range_block(root, end - size + 1,
> +                                                     flags, false);
> +                     if (!found || gpu_buddy_block_offset(found) < start)
> +                             continue;

[Severity: Medium]
Can end - start and end - size + 1 underflow when start > end, causing
__alloc_range_bias() to allocate a block outside [start, end)?

gpu_buddy_alloc_blocks() checks end <= mm->size and
range_overflows(start, size, mm->size), which only checks start + size
<= mm->size, so an inverted range (for example, when
AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED clamps lpfn to visible_pfn below a
memory partition's fpfn in amdgpu_bo_placement_from_domain()) reaches
__alloc_range_bias() with start > end.  Previously, the adjusted_end vs
adjusted_start check rejected inverted ranges with -ENOSPC.  In the new
strict pass, end - start wraps in u64 arithmetic past the size check,
and when size > end, end - size + 1 wraps to near U64_MAX, so
rbtree_last_range_block() matches and returns a block at offset >= start
> end.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/17f2a6366ba845edc799dbeb712c12f8d060afad.1794845083.git.william.pala...@amd.com?part=1

Reply via email to