dri-devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] gpu/buddy: bail out of try_harder when alignment cannot be honoured
@ 2026-06-29  7:43 Arunpravin Paneer Selvam
  2026-07-02 10:48 ` Matthew Auld
  0 siblings, 1 reply; 9+ messages in thread
From: Arunpravin Paneer Selvam @ 2026-06-29  7:43 UTC (permalink / raw)
  To: matthew.auld, christian.koenig, dri-devel, intel-gfx, intel-xe,
	amd-gfx
  Cc: alexander.deucher, Arunpravin Paneer Selvam, Timur Kristóf,
	John Olender, stable

The try_harder contiguous fallback could return a range whose start
offset did not match the caller's min_block_size. When a candidate's
start is misaligned, realign it: free the misaligned run and reallocate
exactly @size at the next lower min_block_size boundary. This keeps the
returned size unchanged with no surplus to trim, and rejects the request
only when no aligned candidate fits.

v2: align misaligned candidates down to min_block_size instead of
    bailing out, for both the RHS and LHS paths (Matthew).

Suggested-by: Christian König <christian.koenig@amd.com>
Fixes: 0a1844bf0b53 ("drm/buddy: Improve contiguous memory allocation")
Cc: Matthew Auld <matthew.auld@intel.com>
Cc: Christian König <christian.koenig@amd.com>
Cc: Timur Kristóf <timur.kristof@gmail.com>
Cc: John Olender <john.olender@gmail.com>
Cc: stable@vger.kernel.org
Signed-off-by: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com>
---
 drivers/gpu/buddy.c | 63 +++++++++++++++++++++++++++++++--------------
 1 file changed, 44 insertions(+), 19 deletions(-)

diff --git a/drivers/gpu/buddy.c b/drivers/gpu/buddy.c
index dc81fe0301ce..3c73ae87f3c5 100644
--- a/drivers/gpu/buddy.c
+++ b/drivers/gpu/buddy.c
@@ -1118,22 +1118,30 @@ static int __gpu_buddy_alloc_range(struct gpu_buddy *mm,
 			     blocks, total_allocated_on_err);
 }
 
+static int __alloc_contig_aligned_retry(struct gpu_buddy *mm,
+					u64 unaligned_offset,
+					u64 size,
+					u64 min_block_size,
+					struct list_head *blocks)
+{
+	u64 aligned_offset = round_down(unaligned_offset, min_block_size);
+
+	return __gpu_buddy_alloc_range(mm, aligned_offset, size, NULL, blocks);
+}
+
 static int __alloc_contig_try_harder(struct gpu_buddy *mm,
 				     u64 size,
 				     u64 min_block_size,
 				     struct list_head *blocks)
 {
-	u64 rhs_offset, lhs_offset, lhs_size, filled;
+	u64 rhs_offset, lhs_offset, filled;
 	struct gpu_buddy_block *block;
 	unsigned int tree, order;
-	LIST_HEAD(blocks_lhs);
-	unsigned long pages;
 	u64 modify_size;
 	int err;
 
 	modify_size = rounddown_pow_of_two(size);
-	pages = modify_size >> ilog2(mm->chunk_size);
-	order = fls(pages) - 1;
+	order = ilog2(modify_size) - ilog2(mm->chunk_size);
 	if (order == 0)
 		return -ENOSPC;
 
@@ -1149,31 +1157,48 @@ static int __alloc_contig_try_harder(struct gpu_buddy *mm,
 		while (iter) {
 			block = rbtree_get_free_block(iter);
 
-			/* Allocate blocks traversing RHS */
 			rhs_offset = gpu_buddy_block_offset(block);
+
+			/* Allocate blocks traversing RHS */
 			err =  __gpu_buddy_alloc_range(mm, rhs_offset, size,
 						       &filled, blocks);
-			if (!err || err != -ENOSPC)
+			if (err && err != -ENOSPC)
 				return err;
+			if (!err && IS_ALIGNED(rhs_offset, min_block_size))
+				return 0;
+			if (!err) {
+				/* Allocate the unaligned RHS offset using round_down */
+				gpu_buddy_free_list_internal(mm, blocks);
+				err = __alloc_contig_aligned_retry(mm, rhs_offset,
+								   size,
+								   min_block_size,
+								   blocks);
+				if (!err)
+					return 0;
+				if (err != -ENOSPC) {
+					gpu_buddy_free_list_internal(mm, blocks);
+					return err;
+				}
+				goto next;
+			}
 
-			lhs_size = max((size - filled), min_block_size);
-			if (!IS_ALIGNED(lhs_size, min_block_size))
-				lhs_size = round_up(lhs_size, min_block_size);
+			if (size - filled > rhs_offset)
+				goto next;
 
-			/* Allocate blocks traversing LHS */
-			lhs_offset = gpu_buddy_block_offset(block) - lhs_size;
-			err =  __gpu_buddy_alloc_range(mm, lhs_offset, lhs_size,
-						       NULL, &blocks_lhs);
-			if (!err) {
-				list_splice(&blocks_lhs, blocks);
+			lhs_offset = rhs_offset - (size - filled);
+
+			/* Allocate the unaligned LHS offset using round_down */
+			gpu_buddy_free_list_internal(mm, blocks);
+			err = __alloc_contig_aligned_retry(mm, lhs_offset, size,
+							   min_block_size, blocks);
+			if (!err)
 				return 0;
-			} else if (err != -ENOSPC) {
+			if (err != -ENOSPC) {
 				gpu_buddy_free_list_internal(mm, blocks);
 				return err;
 			}
-			/* Free blocks for the next iteration */
+next:
 			gpu_buddy_free_list_internal(mm, blocks);
-
 			iter = rb_prev(iter);
 		}
 	}

base-commit: 6648301c5bb2ef23f0fb15bcb01d21ff66f36799
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] gpu/buddy: bail out of try_harder when alignment cannot be honoured
  2026-06-29  7:43 Arunpravin Paneer Selvam
@ 2026-07-02 10:48 ` Matthew Auld
  2026-07-03  0:35   ` John Olender
  0 siblings, 1 reply; 9+ messages in thread
From: Matthew Auld @ 2026-07-02 10:48 UTC (permalink / raw)
  To: Arunpravin Paneer Selvam, christian.koenig, dri-devel, intel-gfx,
	intel-xe, amd-gfx
  Cc: alexander.deucher, Timur Kristóf, John Olender, stable

On 29/06/2026 08:43, Arunpravin Paneer Selvam wrote:
> The try_harder contiguous fallback could return a range whose start
> offset did not match the caller's min_block_size. When a candidate's
> start is misaligned, realign it: free the misaligned run and reallocate
> exactly @size at the next lower min_block_size boundary. This keeps the
> returned size unchanged with no surplus to trim, and rejects the request
> only when no aligned candidate fits.
> 
> v2: align misaligned candidates down to min_block_size instead of
>      bailing out, for both the RHS and LHS paths (Matthew).
> 
> Suggested-by: Christian König <christian.koenig@amd.com>
> Fixes: 0a1844bf0b53 ("drm/buddy: Improve contiguous memory allocation")
> Cc: Matthew Auld <matthew.auld@intel.com>
> Cc: Christian König <christian.koenig@amd.com>
> Cc: Timur Kristóf <timur.kristof@gmail.com>
> Cc: John Olender <john.olender@gmail.com>
> Cc: stable@vger.kernel.org
> Signed-off-by: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com>

Reviewed-by: Matthew Auld <matthew.auld@intel.com>

> ---
>   drivers/gpu/buddy.c | 63 +++++++++++++++++++++++++++++++--------------
>   1 file changed, 44 insertions(+), 19 deletions(-)
> 
> diff --git a/drivers/gpu/buddy.c b/drivers/gpu/buddy.c
> index dc81fe0301ce..3c73ae87f3c5 100644
> --- a/drivers/gpu/buddy.c
> +++ b/drivers/gpu/buddy.c
> @@ -1118,22 +1118,30 @@ static int __gpu_buddy_alloc_range(struct gpu_buddy *mm,
>   			     blocks, total_allocated_on_err);
>   }
>   
> +static int __alloc_contig_aligned_retry(struct gpu_buddy *mm,
> +					u64 unaligned_offset,
> +					u64 size,
> +					u64 min_block_size,
> +					struct list_head *blocks)
> +{
> +	u64 aligned_offset = round_down(unaligned_offset, min_block_size);
> +
> +	return __gpu_buddy_alloc_range(mm, aligned_offset, size, NULL, blocks);
> +}
> +
>   static int __alloc_contig_try_harder(struct gpu_buddy *mm,
>   				     u64 size,
>   				     u64 min_block_size,
>   				     struct list_head *blocks)
>   {
> -	u64 rhs_offset, lhs_offset, lhs_size, filled;
> +	u64 rhs_offset, lhs_offset, filled;
>   	struct gpu_buddy_block *block;
>   	unsigned int tree, order;
> -	LIST_HEAD(blocks_lhs);
> -	unsigned long pages;
>   	u64 modify_size;
>   	int err;
>   
>   	modify_size = rounddown_pow_of_two(size);
> -	pages = modify_size >> ilog2(mm->chunk_size);
> -	order = fls(pages) - 1;
> +	order = ilog2(modify_size) - ilog2(mm->chunk_size);
>   	if (order == 0)
>   		return -ENOSPC;
>   
> @@ -1149,31 +1157,48 @@ static int __alloc_contig_try_harder(struct gpu_buddy *mm,
>   		while (iter) {
>   			block = rbtree_get_free_block(iter);
>   
> -			/* Allocate blocks traversing RHS */
>   			rhs_offset = gpu_buddy_block_offset(block);
> +
> +			/* Allocate blocks traversing RHS */
>   			err =  __gpu_buddy_alloc_range(mm, rhs_offset, size,
>   						       &filled, blocks);
> -			if (!err || err != -ENOSPC)
> +			if (err && err != -ENOSPC)
>   				return err;
> +			if (!err && IS_ALIGNED(rhs_offset, min_block_size))
> +				return 0;
> +			if (!err) {
> +				/* Allocate the unaligned RHS offset using round_down */
> +				gpu_buddy_free_list_internal(mm, blocks);
> +				err = __alloc_contig_aligned_retry(mm, rhs_offset,
> +								   size,
> +								   min_block_size,
> +								   blocks);
> +				if (!err)
> +					return 0;
> +				if (err != -ENOSPC) {
> +					gpu_buddy_free_list_internal(mm, blocks);
> +					return err;
> +				}
> +				goto next;
> +			}
>   
> -			lhs_size = max((size - filled), min_block_size);
> -			if (!IS_ALIGNED(lhs_size, min_block_size))
> -				lhs_size = round_up(lhs_size, min_block_size);
> +			if (size - filled > rhs_offset)
> +				goto next;
>   
> -			/* Allocate blocks traversing LHS */
> -			lhs_offset = gpu_buddy_block_offset(block) - lhs_size;
> -			err =  __gpu_buddy_alloc_range(mm, lhs_offset, lhs_size,
> -						       NULL, &blocks_lhs);
> -			if (!err) {
> -				list_splice(&blocks_lhs, blocks);
> +			lhs_offset = rhs_offset - (size - filled);
> +
> +			/* Allocate the unaligned LHS offset using round_down */
> +			gpu_buddy_free_list_internal(mm, blocks);
> +			err = __alloc_contig_aligned_retry(mm, lhs_offset, size,
> +							   min_block_size, blocks);
> +			if (!err)
>   				return 0;
> -			} else if (err != -ENOSPC) {
> +			if (err != -ENOSPC) {
>   				gpu_buddy_free_list_internal(mm, blocks);
>   				return err;
>   			}
> -			/* Free blocks for the next iteration */
> +next:
>   			gpu_buddy_free_list_internal(mm, blocks);
> -
>   			iter = rb_prev(iter);
>   		}
>   	}
> 
> base-commit: 6648301c5bb2ef23f0fb15bcb01d21ff66f36799


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] gpu/buddy: bail out of try_harder when alignment cannot be honoured
  2026-07-02 10:48 ` Matthew Auld
@ 2026-07-03  0:35   ` John Olender
  2026-07-03 13:40     ` Arunpravin Paneer Selvam
  2026-07-09 13:25     ` Arunpravin Paneer Selvam
  0 siblings, 2 replies; 9+ messages in thread
From: John Olender @ 2026-07-03  0:35 UTC (permalink / raw)
  To: Matthew Auld, Arunpravin Paneer Selvam, christian.koenig,
	dri-devel, intel-gfx, intel-xe, amd-gfx
  Cc: alexander.deucher, Timur Kristóf, stable

On 7/2/26 6:48 AM, Matthew Auld wrote:
> On 29/06/2026 08:43, Arunpravin Paneer Selvam wrote:
>> The try_harder contiguous fallback could return a range whose start
>> offset did not match the caller's min_block_size. When a candidate's
>> start is misaligned, realign it: free the misaligned run and reallocate
>> exactly @size at the next lower min_block_size boundary. This keeps the
>> returned size unchanged with no surplus to trim, and rejects the request
>> only when no aligned candidate fits.
>>
>> v2: align misaligned candidates down to min_block_size instead of
>>      bailing out, for both the RHS and LHS paths (Matthew).
>>
>> Suggested-by: Christian König <christian.koenig@amd.com>
>> Fixes: 0a1844bf0b53 ("drm/buddy: Improve contiguous memory allocation")
>> Cc: Matthew Auld <matthew.auld@intel.com>
>> Cc: Christian König <christian.koenig@amd.com>
>> Cc: Timur Kristóf <timur.kristof@gmail.com>
>> Cc: John Olender <john.olender@gmail.com>
>> Cc: stable@vger.kernel.org
>> Signed-off-by: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com>
> 
> Reviewed-by: Matthew Auld <matthew.auld@intel.com>
> 

I haven't hit any issues with this revision during testing.

Thanks,
John

>> ---
>>   drivers/gpu/buddy.c | 63 +++++++++++++++++++++++++++++++--------------
>>   1 file changed, 44 insertions(+), 19 deletions(-)
>>
>> diff --git a/drivers/gpu/buddy.c b/drivers/gpu/buddy.c
>> index dc81fe0301ce..3c73ae87f3c5 100644
>> --- a/drivers/gpu/buddy.c
>> +++ b/drivers/gpu/buddy.c
>> @@ -1118,22 +1118,30 @@ static int __gpu_buddy_alloc_range(struct gpu_buddy *mm,
>>                    blocks, total_allocated_on_err);
>>   }
>>   +static int __alloc_contig_aligned_retry(struct gpu_buddy *mm,
>> +                    u64 unaligned_offset,
>> +                    u64 size,
>> +                    u64 min_block_size,
>> +                    struct list_head *blocks)
>> +{
>> +    u64 aligned_offset = round_down(unaligned_offset, min_block_size);
>> +
>> +    return __gpu_buddy_alloc_range(mm, aligned_offset, size, NULL, blocks);
>> +}
>> +
>>   static int __alloc_contig_try_harder(struct gpu_buddy *mm,
>>                        u64 size,
>>                        u64 min_block_size,
>>                        struct list_head *blocks)
>>   {
>> -    u64 rhs_offset, lhs_offset, lhs_size, filled;
>> +    u64 rhs_offset, lhs_offset, filled;
>>       struct gpu_buddy_block *block;
>>       unsigned int tree, order;
>> -    LIST_HEAD(blocks_lhs);
>> -    unsigned long pages;
>>       u64 modify_size;
>>       int err;
>>         modify_size = rounddown_pow_of_two(size);
>> -    pages = modify_size >> ilog2(mm->chunk_size);
>> -    order = fls(pages) - 1;
>> +    order = ilog2(modify_size) - ilog2(mm->chunk_size);
>>       if (order == 0)
>>           return -ENOSPC;
>>   @@ -1149,31 +1157,48 @@ static int __alloc_contig_try_harder(struct gpu_buddy *mm,
>>           while (iter) {
>>               block = rbtree_get_free_block(iter);
>>   -            /* Allocate blocks traversing RHS */
>>               rhs_offset = gpu_buddy_block_offset(block);
>> +
>> +            /* Allocate blocks traversing RHS */
>>               err =  __gpu_buddy_alloc_range(mm, rhs_offset, size,
>>                                  &filled, blocks);
>> -            if (!err || err != -ENOSPC)
>> +            if (err && err != -ENOSPC)
>>                   return err;
>> +            if (!err && IS_ALIGNED(rhs_offset, min_block_size))
>> +                return 0;
>> +            if (!err) {
>> +                /* Allocate the unaligned RHS offset using round_down */
>> +                gpu_buddy_free_list_internal(mm, blocks);
>> +                err = __alloc_contig_aligned_retry(mm, rhs_offset,
>> +                                   size,
>> +                                   min_block_size,
>> +                                   blocks);
>> +                if (!err)
>> +                    return 0;
>> +                if (err != -ENOSPC) {
>> +                    gpu_buddy_free_list_internal(mm, blocks);
>> +                    return err;
>> +                }
>> +                goto next;
>> +            }
>>   -            lhs_size = max((size - filled), min_block_size);
>> -            if (!IS_ALIGNED(lhs_size, min_block_size))
>> -                lhs_size = round_up(lhs_size, min_block_size);
>> +            if (size - filled > rhs_offset)
>> +                goto next;
>>   -            /* Allocate blocks traversing LHS */
>> -            lhs_offset = gpu_buddy_block_offset(block) - lhs_size;
>> -            err =  __gpu_buddy_alloc_range(mm, lhs_offset, lhs_size,
>> -                               NULL, &blocks_lhs);
>> -            if (!err) {
>> -                list_splice(&blocks_lhs, blocks);
>> +            lhs_offset = rhs_offset - (size - filled);
>> +
>> +            /* Allocate the unaligned LHS offset using round_down */
>> +            gpu_buddy_free_list_internal(mm, blocks);
>> +            err = __alloc_contig_aligned_retry(mm, lhs_offset, size,
>> +                               min_block_size, blocks);
>> +            if (!err)
>>                   return 0;
>> -            } else if (err != -ENOSPC) {
>> +            if (err != -ENOSPC) {
>>                   gpu_buddy_free_list_internal(mm, blocks);
>>                   return err;
>>               }
>> -            /* Free blocks for the next iteration */
>> +next:
>>               gpu_buddy_free_list_internal(mm, blocks);
>> -
>>               iter = rb_prev(iter);
>>           }
>>       }
>>
>> base-commit: 6648301c5bb2ef23f0fb15bcb01d21ff66f36799
> 


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] gpu/buddy: bail out of try_harder when alignment cannot be honoured
  2026-07-03  0:35   ` John Olender
@ 2026-07-03 13:40     ` Arunpravin Paneer Selvam
  2026-07-09 13:25     ` Arunpravin Paneer Selvam
  1 sibling, 0 replies; 9+ messages in thread
From: Arunpravin Paneer Selvam @ 2026-07-03 13:40 UTC (permalink / raw)
  To: John Olender, Matthew Auld, christian.koenig, dri-devel,
	intel-gfx, intel-xe, amd-gfx
  Cc: alexander.deucher, Timur Kristóf, stable



On 7/3/2026 6:05 AM, John Olender wrote:
> On 7/2/26 6:48 AM, Matthew Auld wrote:
>> On 29/06/2026 08:43, Arunpravin Paneer Selvam wrote:
>>> The try_harder contiguous fallback could return a range whose start
>>> offset did not match the caller's min_block_size. When a candidate's
>>> start is misaligned, realign it: free the misaligned run and reallocate
>>> exactly @size at the next lower min_block_size boundary. This keeps the
>>> returned size unchanged with no surplus to trim, and rejects the request
>>> only when no aligned candidate fits.
>>>
>>> v2: align misaligned candidates down to min_block_size instead of
>>>       bailing out, for both the RHS and LHS paths (Matthew).
>>>
>>> Suggested-by: Christian König <christian.koenig@amd.com>
>>> Fixes: 0a1844bf0b53 ("drm/buddy: Improve contiguous memory allocation")
>>> Cc: Matthew Auld <matthew.auld@intel.com>
>>> Cc: Christian König <christian.koenig@amd.com>
>>> Cc: Timur Kristóf <timur.kristof@gmail.com>
>>> Cc: John Olender <john.olender@gmail.com>
>>> Cc: stable@vger.kernel.org
>>> Signed-off-by: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com>
>> Reviewed-by: Matthew Auld <matthew.auld@intel.com>
>>
> I haven't hit any issues with this revision during testing.
Thanks for testing.

Regards,
Arun.
>
> Thanks,
> John
>
>>> ---
>>>    drivers/gpu/buddy.c | 63 +++++++++++++++++++++++++++++++--------------
>>>    1 file changed, 44 insertions(+), 19 deletions(-)
>>>
>>> diff --git a/drivers/gpu/buddy.c b/drivers/gpu/buddy.c
>>> index dc81fe0301ce..3c73ae87f3c5 100644
>>> --- a/drivers/gpu/buddy.c
>>> +++ b/drivers/gpu/buddy.c
>>> @@ -1118,22 +1118,30 @@ static int __gpu_buddy_alloc_range(struct gpu_buddy *mm,
>>>                     blocks, total_allocated_on_err);
>>>    }
>>>    +static int __alloc_contig_aligned_retry(struct gpu_buddy *mm,
>>> +                    u64 unaligned_offset,
>>> +                    u64 size,
>>> +                    u64 min_block_size,
>>> +                    struct list_head *blocks)
>>> +{
>>> +    u64 aligned_offset = round_down(unaligned_offset, min_block_size);
>>> +
>>> +    return __gpu_buddy_alloc_range(mm, aligned_offset, size, NULL, blocks);
>>> +}
>>> +
>>>    static int __alloc_contig_try_harder(struct gpu_buddy *mm,
>>>                         u64 size,
>>>                         u64 min_block_size,
>>>                         struct list_head *blocks)
>>>    {
>>> -    u64 rhs_offset, lhs_offset, lhs_size, filled;
>>> +    u64 rhs_offset, lhs_offset, filled;
>>>        struct gpu_buddy_block *block;
>>>        unsigned int tree, order;
>>> -    LIST_HEAD(blocks_lhs);
>>> -    unsigned long pages;
>>>        u64 modify_size;
>>>        int err;
>>>          modify_size = rounddown_pow_of_two(size);
>>> -    pages = modify_size >> ilog2(mm->chunk_size);
>>> -    order = fls(pages) - 1;
>>> +    order = ilog2(modify_size) - ilog2(mm->chunk_size);
>>>        if (order == 0)
>>>            return -ENOSPC;
>>>    @@ -1149,31 +1157,48 @@ static int __alloc_contig_try_harder(struct gpu_buddy *mm,
>>>            while (iter) {
>>>                block = rbtree_get_free_block(iter);
>>>    -            /* Allocate blocks traversing RHS */
>>>                rhs_offset = gpu_buddy_block_offset(block);
>>> +
>>> +            /* Allocate blocks traversing RHS */
>>>                err =  __gpu_buddy_alloc_range(mm, rhs_offset, size,
>>>                                   &filled, blocks);
>>> -            if (!err || err != -ENOSPC)
>>> +            if (err && err != -ENOSPC)
>>>                    return err;
>>> +            if (!err && IS_ALIGNED(rhs_offset, min_block_size))
>>> +                return 0;
>>> +            if (!err) {
>>> +                /* Allocate the unaligned RHS offset using round_down */
>>> +                gpu_buddy_free_list_internal(mm, blocks);
>>> +                err = __alloc_contig_aligned_retry(mm, rhs_offset,
>>> +                                   size,
>>> +                                   min_block_size,
>>> +                                   blocks);
>>> +                if (!err)
>>> +                    return 0;
>>> +                if (err != -ENOSPC) {
>>> +                    gpu_buddy_free_list_internal(mm, blocks);
>>> +                    return err;
>>> +                }
>>> +                goto next;
>>> +            }
>>>    -            lhs_size = max((size - filled), min_block_size);
>>> -            if (!IS_ALIGNED(lhs_size, min_block_size))
>>> -                lhs_size = round_up(lhs_size, min_block_size);
>>> +            if (size - filled > rhs_offset)
>>> +                goto next;
>>>    -            /* Allocate blocks traversing LHS */
>>> -            lhs_offset = gpu_buddy_block_offset(block) - lhs_size;
>>> -            err =  __gpu_buddy_alloc_range(mm, lhs_offset, lhs_size,
>>> -                               NULL, &blocks_lhs);
>>> -            if (!err) {
>>> -                list_splice(&blocks_lhs, blocks);
>>> +            lhs_offset = rhs_offset - (size - filled);
>>> +
>>> +            /* Allocate the unaligned LHS offset using round_down */
>>> +            gpu_buddy_free_list_internal(mm, blocks);
>>> +            err = __alloc_contig_aligned_retry(mm, lhs_offset, size,
>>> +                               min_block_size, blocks);
>>> +            if (!err)
>>>                    return 0;
>>> -            } else if (err != -ENOSPC) {
>>> +            if (err != -ENOSPC) {
>>>                    gpu_buddy_free_list_internal(mm, blocks);
>>>                    return err;
>>>                }
>>> -            /* Free blocks for the next iteration */
>>> +next:
>>>                gpu_buddy_free_list_internal(mm, blocks);
>>> -
>>>                iter = rb_prev(iter);
>>>            }
>>>        }
>>>
>>> base-commit: 6648301c5bb2ef23f0fb15bcb01d21ff66f36799


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2] gpu/buddy: bail out of try_harder when alignment cannot be honoured
@ 2026-07-09 13:02 Arunpravin Paneer Selvam
  2026-07-09 13:25 ` sashiko-bot
  0 siblings, 1 reply; 9+ messages in thread
From: Arunpravin Paneer Selvam @ 2026-07-09 13:02 UTC (permalink / raw)
  To: matthew.auld, christian.koenig, dri-devel, intel-gfx, intel-xe,
	amd-gfx
  Cc: alexander.deucher, Arunpravin Paneer Selvam, Timur Kristóf,
	stable, John Olender

The try_harder contiguous fallback could return a range whose start
offset did not match the caller's min_block_size. When a candidate's
start is misaligned, realign it: free the misaligned run and reallocate
exactly @size at the next lower min_block_size boundary. This keeps the
returned size unchanged with no surplus to trim, and rejects the request
only when no aligned candidate fits.

v2: align misaligned candidates down to min_block_size instead of
    bailing out, for both the RHS and LHS paths (Matthew).

Fixes: 0a1844bf0b53 ("drm/buddy: Improve contiguous memory allocation")
Suggested-by: Christian König <christian.koenig@amd.com>
Cc: Matthew Auld <matthew.auld@intel.com>
Cc: Christian König <christian.koenig@amd.com>
Cc: Timur Kristóf <timur.kristof@gmail.com>
Cc: stable@vger.kernel.org
Reviewed-by: Matthew Auld <matthew.auld@intel.com>
Tested-by: John Olender <john.olender@gmail.com>
Signed-off-by: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com>
---
 drivers/gpu/buddy.c | 63 +++++++++++++++++++++++++++++++--------------
 1 file changed, 44 insertions(+), 19 deletions(-)

diff --git a/drivers/gpu/buddy.c b/drivers/gpu/buddy.c
index dc81fe0301ce..3c73ae87f3c5 100644
--- a/drivers/gpu/buddy.c
+++ b/drivers/gpu/buddy.c
@@ -1118,22 +1118,30 @@ static int __gpu_buddy_alloc_range(struct gpu_buddy *mm,
 			     blocks, total_allocated_on_err);
 }
 
+static int __alloc_contig_aligned_retry(struct gpu_buddy *mm,
+					u64 unaligned_offset,
+					u64 size,
+					u64 min_block_size,
+					struct list_head *blocks)
+{
+	u64 aligned_offset = round_down(unaligned_offset, min_block_size);
+
+	return __gpu_buddy_alloc_range(mm, aligned_offset, size, NULL, blocks);
+}
+
 static int __alloc_contig_try_harder(struct gpu_buddy *mm,
 				     u64 size,
 				     u64 min_block_size,
 				     struct list_head *blocks)
 {
-	u64 rhs_offset, lhs_offset, lhs_size, filled;
+	u64 rhs_offset, lhs_offset, filled;
 	struct gpu_buddy_block *block;
 	unsigned int tree, order;
-	LIST_HEAD(blocks_lhs);
-	unsigned long pages;
 	u64 modify_size;
 	int err;
 
 	modify_size = rounddown_pow_of_two(size);
-	pages = modify_size >> ilog2(mm->chunk_size);
-	order = fls(pages) - 1;
+	order = ilog2(modify_size) - ilog2(mm->chunk_size);
 	if (order == 0)
 		return -ENOSPC;
 
@@ -1149,31 +1157,48 @@ static int __alloc_contig_try_harder(struct gpu_buddy *mm,
 		while (iter) {
 			block = rbtree_get_free_block(iter);
 
-			/* Allocate blocks traversing RHS */
 			rhs_offset = gpu_buddy_block_offset(block);
+
+			/* Allocate blocks traversing RHS */
 			err =  __gpu_buddy_alloc_range(mm, rhs_offset, size,
 						       &filled, blocks);
-			if (!err || err != -ENOSPC)
+			if (err && err != -ENOSPC)
 				return err;
+			if (!err && IS_ALIGNED(rhs_offset, min_block_size))
+				return 0;
+			if (!err) {
+				/* Allocate the unaligned RHS offset using round_down */
+				gpu_buddy_free_list_internal(mm, blocks);
+				err = __alloc_contig_aligned_retry(mm, rhs_offset,
+								   size,
+								   min_block_size,
+								   blocks);
+				if (!err)
+					return 0;
+				if (err != -ENOSPC) {
+					gpu_buddy_free_list_internal(mm, blocks);
+					return err;
+				}
+				goto next;
+			}
 
-			lhs_size = max((size - filled), min_block_size);
-			if (!IS_ALIGNED(lhs_size, min_block_size))
-				lhs_size = round_up(lhs_size, min_block_size);
+			if (size - filled > rhs_offset)
+				goto next;
 
-			/* Allocate blocks traversing LHS */
-			lhs_offset = gpu_buddy_block_offset(block) - lhs_size;
-			err =  __gpu_buddy_alloc_range(mm, lhs_offset, lhs_size,
-						       NULL, &blocks_lhs);
-			if (!err) {
-				list_splice(&blocks_lhs, blocks);
+			lhs_offset = rhs_offset - (size - filled);
+
+			/* Allocate the unaligned LHS offset using round_down */
+			gpu_buddy_free_list_internal(mm, blocks);
+			err = __alloc_contig_aligned_retry(mm, lhs_offset, size,
+							   min_block_size, blocks);
+			if (!err)
 				return 0;
-			} else if (err != -ENOSPC) {
+			if (err != -ENOSPC) {
 				gpu_buddy_free_list_internal(mm, blocks);
 				return err;
 			}
-			/* Free blocks for the next iteration */
+next:
 			gpu_buddy_free_list_internal(mm, blocks);
-
 			iter = rb_prev(iter);
 		}
 	}

base-commit: 104c00917264c5b9571072471e3a8689cd1a2c4d
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v2] gpu/buddy: bail out of try_harder when alignment cannot be honoured
@ 2026-07-09 13:10 Arunpravin Paneer Selvam
  2026-07-09 13:29 ` sashiko-bot
  0 siblings, 1 reply; 9+ messages in thread
From: Arunpravin Paneer Selvam @ 2026-07-09 13:10 UTC (permalink / raw)
  To: matthew.auld, christian.koenig, dri-devel, intel-gfx, intel-xe,
	amd-gfx
  Cc: alexander.deucher, Arunpravin Paneer Selvam, Timur Kristóf,
	stable, John Olender

The try_harder contiguous fallback could return a range whose start
offset did not match the caller's min_block_size. When a candidate's
start is misaligned, realign it: free the misaligned run and reallocate
exactly @size at the next lower min_block_size boundary. This keeps the
returned size unchanged with no surplus to trim, and rejects the request
only when no aligned candidate fits.

v2: align misaligned candidates down to min_block_size instead of
    bailing out, for both the RHS and LHS paths (Matthew).

Fixes: 0a1844bf0b53 ("drm/buddy: Improve contiguous memory allocation")
Suggested-by: Christian König <christian.koenig@amd.com>
Cc: Matthew Auld <matthew.auld@intel.com>
Cc: Christian König <christian.koenig@amd.com>
Cc: Timur Kristóf <timur.kristof@gmail.com>
Cc: stable@vger.kernel.org
Reviewed-by: Matthew Auld <matthew.auld@intel.com>
Tested-by: John Olender <john.olender@gmail.com>
Signed-off-by: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com>
---
 drivers/gpu/buddy.c | 63 +++++++++++++++++++++++++++++++--------------
 1 file changed, 44 insertions(+), 19 deletions(-)

diff --git a/drivers/gpu/buddy.c b/drivers/gpu/buddy.c
index dc81fe0301ce..3c73ae87f3c5 100644
--- a/drivers/gpu/buddy.c
+++ b/drivers/gpu/buddy.c
@@ -1118,22 +1118,30 @@ static int __gpu_buddy_alloc_range(struct gpu_buddy *mm,
 			     blocks, total_allocated_on_err);
 }
 
+static int __alloc_contig_aligned_retry(struct gpu_buddy *mm,
+					u64 unaligned_offset,
+					u64 size,
+					u64 min_block_size,
+					struct list_head *blocks)
+{
+	u64 aligned_offset = round_down(unaligned_offset, min_block_size);
+
+	return __gpu_buddy_alloc_range(mm, aligned_offset, size, NULL, blocks);
+}
+
 static int __alloc_contig_try_harder(struct gpu_buddy *mm,
 				     u64 size,
 				     u64 min_block_size,
 				     struct list_head *blocks)
 {
-	u64 rhs_offset, lhs_offset, lhs_size, filled;
+	u64 rhs_offset, lhs_offset, filled;
 	struct gpu_buddy_block *block;
 	unsigned int tree, order;
-	LIST_HEAD(blocks_lhs);
-	unsigned long pages;
 	u64 modify_size;
 	int err;
 
 	modify_size = rounddown_pow_of_two(size);
-	pages = modify_size >> ilog2(mm->chunk_size);
-	order = fls(pages) - 1;
+	order = ilog2(modify_size) - ilog2(mm->chunk_size);
 	if (order == 0)
 		return -ENOSPC;
 
@@ -1149,31 +1157,48 @@ static int __alloc_contig_try_harder(struct gpu_buddy *mm,
 		while (iter) {
 			block = rbtree_get_free_block(iter);
 
-			/* Allocate blocks traversing RHS */
 			rhs_offset = gpu_buddy_block_offset(block);
+
+			/* Allocate blocks traversing RHS */
 			err =  __gpu_buddy_alloc_range(mm, rhs_offset, size,
 						       &filled, blocks);
-			if (!err || err != -ENOSPC)
+			if (err && err != -ENOSPC)
 				return err;
+			if (!err && IS_ALIGNED(rhs_offset, min_block_size))
+				return 0;
+			if (!err) {
+				/* Allocate the unaligned RHS offset using round_down */
+				gpu_buddy_free_list_internal(mm, blocks);
+				err = __alloc_contig_aligned_retry(mm, rhs_offset,
+								   size,
+								   min_block_size,
+								   blocks);
+				if (!err)
+					return 0;
+				if (err != -ENOSPC) {
+					gpu_buddy_free_list_internal(mm, blocks);
+					return err;
+				}
+				goto next;
+			}
 
-			lhs_size = max((size - filled), min_block_size);
-			if (!IS_ALIGNED(lhs_size, min_block_size))
-				lhs_size = round_up(lhs_size, min_block_size);
+			if (size - filled > rhs_offset)
+				goto next;
 
-			/* Allocate blocks traversing LHS */
-			lhs_offset = gpu_buddy_block_offset(block) - lhs_size;
-			err =  __gpu_buddy_alloc_range(mm, lhs_offset, lhs_size,
-						       NULL, &blocks_lhs);
-			if (!err) {
-				list_splice(&blocks_lhs, blocks);
+			lhs_offset = rhs_offset - (size - filled);
+
+			/* Allocate the unaligned LHS offset using round_down */
+			gpu_buddy_free_list_internal(mm, blocks);
+			err = __alloc_contig_aligned_retry(mm, lhs_offset, size,
+							   min_block_size, blocks);
+			if (!err)
 				return 0;
-			} else if (err != -ENOSPC) {
+			if (err != -ENOSPC) {
 				gpu_buddy_free_list_internal(mm, blocks);
 				return err;
 			}
-			/* Free blocks for the next iteration */
+next:
 			gpu_buddy_free_list_internal(mm, blocks);
-
 			iter = rb_prev(iter);
 		}
 	}

base-commit: 104c00917264c5b9571072471e3a8689cd1a2c4d
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] gpu/buddy: bail out of try_harder when alignment cannot be honoured
  2026-07-03  0:35   ` John Olender
  2026-07-03 13:40     ` Arunpravin Paneer Selvam
@ 2026-07-09 13:25     ` Arunpravin Paneer Selvam
  1 sibling, 0 replies; 9+ messages in thread
From: Arunpravin Paneer Selvam @ 2026-07-09 13:25 UTC (permalink / raw)
  To: John Olender, Matthew Auld, christian.koenig, dri-devel,
	intel-gfx, intel-xe, amd-gfx
  Cc: alexander.deucher, Timur Kristóf, stable



On 7/3/2026 6:05 AM, John Olender wrote:
> On 7/2/26 6:48 AM, Matthew Auld wrote:
>> On 29/06/2026 08:43, Arunpravin Paneer Selvam wrote:
>>> The try_harder contiguous fallback could return a range whose start
>>> offset did not match the caller's min_block_size. When a candidate's
>>> start is misaligned, realign it: free the misaligned run and reallocate
>>> exactly @size at the next lower min_block_size boundary. This keeps the
>>> returned size unchanged with no surplus to trim, and rejects the request
>>> only when no aligned candidate fits.
>>>
>>> v2: align misaligned candidates down to min_block_size instead of
>>>       bailing out, for both the RHS and LHS paths (Matthew).
>>>
>>> Suggested-by: Christian König <christian.koenig@amd.com>
>>> Fixes: 0a1844bf0b53 ("drm/buddy: Improve contiguous memory allocation")
>>> Cc: Matthew Auld <matthew.auld@intel.com>
>>> Cc: Christian König <christian.koenig@amd.com>
>>> Cc: Timur Kristóf <timur.kristof@gmail.com>
>>> Cc: John Olender <john.olender@gmail.com>
>>> Cc: stable@vger.kernel.org
>>> Signed-off-by: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com>
>> Reviewed-by: Matthew Auld <matthew.auld@intel.com>
Merged upstream into drm-misc-fixes.

Regards,
Arun.
>>
> I haven't hit any issues with this revision during testing.
>
> Thanks,
> John
>
>>> ---
>>>    drivers/gpu/buddy.c | 63 +++++++++++++++++++++++++++++++--------------
>>>    1 file changed, 44 insertions(+), 19 deletions(-)
>>>
>>> diff --git a/drivers/gpu/buddy.c b/drivers/gpu/buddy.c
>>> index dc81fe0301ce..3c73ae87f3c5 100644
>>> --- a/drivers/gpu/buddy.c
>>> +++ b/drivers/gpu/buddy.c
>>> @@ -1118,22 +1118,30 @@ static int __gpu_buddy_alloc_range(struct gpu_buddy *mm,
>>>                     blocks, total_allocated_on_err);
>>>    }
>>>    +static int __alloc_contig_aligned_retry(struct gpu_buddy *mm,
>>> +                    u64 unaligned_offset,
>>> +                    u64 size,
>>> +                    u64 min_block_size,
>>> +                    struct list_head *blocks)
>>> +{
>>> +    u64 aligned_offset = round_down(unaligned_offset, min_block_size);
>>> +
>>> +    return __gpu_buddy_alloc_range(mm, aligned_offset, size, NULL, blocks);
>>> +}
>>> +
>>>    static int __alloc_contig_try_harder(struct gpu_buddy *mm,
>>>                         u64 size,
>>>                         u64 min_block_size,
>>>                         struct list_head *blocks)
>>>    {
>>> -    u64 rhs_offset, lhs_offset, lhs_size, filled;
>>> +    u64 rhs_offset, lhs_offset, filled;
>>>        struct gpu_buddy_block *block;
>>>        unsigned int tree, order;
>>> -    LIST_HEAD(blocks_lhs);
>>> -    unsigned long pages;
>>>        u64 modify_size;
>>>        int err;
>>>          modify_size = rounddown_pow_of_two(size);
>>> -    pages = modify_size >> ilog2(mm->chunk_size);
>>> -    order = fls(pages) - 1;
>>> +    order = ilog2(modify_size) - ilog2(mm->chunk_size);
>>>        if (order == 0)
>>>            return -ENOSPC;
>>>    @@ -1149,31 +1157,48 @@ static int __alloc_contig_try_harder(struct gpu_buddy *mm,
>>>            while (iter) {
>>>                block = rbtree_get_free_block(iter);
>>>    -            /* Allocate blocks traversing RHS */
>>>                rhs_offset = gpu_buddy_block_offset(block);
>>> +
>>> +            /* Allocate blocks traversing RHS */
>>>                err =  __gpu_buddy_alloc_range(mm, rhs_offset, size,
>>>                                   &filled, blocks);
>>> -            if (!err || err != -ENOSPC)
>>> +            if (err && err != -ENOSPC)
>>>                    return err;
>>> +            if (!err && IS_ALIGNED(rhs_offset, min_block_size))
>>> +                return 0;
>>> +            if (!err) {
>>> +                /* Allocate the unaligned RHS offset using round_down */
>>> +                gpu_buddy_free_list_internal(mm, blocks);
>>> +                err = __alloc_contig_aligned_retry(mm, rhs_offset,
>>> +                                   size,
>>> +                                   min_block_size,
>>> +                                   blocks);
>>> +                if (!err)
>>> +                    return 0;
>>> +                if (err != -ENOSPC) {
>>> +                    gpu_buddy_free_list_internal(mm, blocks);
>>> +                    return err;
>>> +                }
>>> +                goto next;
>>> +            }
>>>    -            lhs_size = max((size - filled), min_block_size);
>>> -            if (!IS_ALIGNED(lhs_size, min_block_size))
>>> -                lhs_size = round_up(lhs_size, min_block_size);
>>> +            if (size - filled > rhs_offset)
>>> +                goto next;
>>>    -            /* Allocate blocks traversing LHS */
>>> -            lhs_offset = gpu_buddy_block_offset(block) - lhs_size;
>>> -            err =  __gpu_buddy_alloc_range(mm, lhs_offset, lhs_size,
>>> -                               NULL, &blocks_lhs);
>>> -            if (!err) {
>>> -                list_splice(&blocks_lhs, blocks);
>>> +            lhs_offset = rhs_offset - (size - filled);
>>> +
>>> +            /* Allocate the unaligned LHS offset using round_down */
>>> +            gpu_buddy_free_list_internal(mm, blocks);
>>> +            err = __alloc_contig_aligned_retry(mm, lhs_offset, size,
>>> +                               min_block_size, blocks);
>>> +            if (!err)
>>>                    return 0;
>>> -            } else if (err != -ENOSPC) {
>>> +            if (err != -ENOSPC) {
>>>                    gpu_buddy_free_list_internal(mm, blocks);
>>>                    return err;
>>>                }
>>> -            /* Free blocks for the next iteration */
>>> +next:
>>>                gpu_buddy_free_list_internal(mm, blocks);
>>> -
>>>                iter = rb_prev(iter);
>>>            }
>>>        }
>>>
>>> base-commit: 6648301c5bb2ef23f0fb15bcb01d21ff66f36799


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] gpu/buddy: bail out of try_harder when alignment cannot be honoured
  2026-07-09 13:02 Arunpravin Paneer Selvam
@ 2026-07-09 13:25 ` sashiko-bot
  0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-07-09 13:25 UTC (permalink / raw)
  To: Arunpravin Paneer Selvam; +Cc: dri-devel

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] On 32-bit architectures, passing a size >= 4GB to __alloc_contig_try_harder() causes silent truncation in rounddown_pow_of_two(), leading to an underflow in the order calculation and a subsequent massive out-of-bounds array access.
--

--- Patch [1]: [PATCH v2] gpu/buddy: bail out of try_harder when alignment cannot be honoured ---
commit bd00afca04077e9aca5c8cacc93b25007c5cb352
Author: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com>

gpu/buddy: bail out of try_harder when alignment cannot be honoured

The try_harder contiguous fallback is updated to realign a range whose start
offset does not match min_block_size. It frees the misaligned run and
reallocates at the next lower boundary to maintain the requested size.

> diff --git a/drivers/gpu/buddy.c b/drivers/gpu/buddy.c
> --- a/drivers/gpu/buddy.c
> +++ b/drivers/gpu/buddy.c
[ ... ]
>  static int __alloc_contig_try_harder(struct gpu_buddy *mm,
>  				     u64 size,
>  				     u64 min_block_size,
>  				     struct list_head *blocks)
>  {
> -	u64 rhs_offset, lhs_offset, lhs_size, filled;
> +	u64 rhs_offset, lhs_offset, filled;
>  	struct gpu_buddy_block *block;
>  	unsigned int tree, order;
> -	LIST_HEAD(blocks_lhs);
> -	unsigned long pages;
>  	u64 modify_size;
>  	int err;
>  
>  	modify_size = rounddown_pow_of_two(size);
> -	pages = modify_size >> ilog2(mm->chunk_size);
> -	order = fls(pages) - 1;
> +	order = ilog2(modify_size) - ilog2(mm->chunk_size);

[Severity: High]
This is a pre-existing issue, but can this lead to an out-of-bounds array
access on 32-bit architectures?

In __alloc_contig_try_harder(), size is a u64. If a contiguous block of 4GB
(0x100000000) or larger is requested, rounddown_pow_of_two(size) delegates
to __rounddown_pow_of_two(unsigned long n), which silently truncates the
64-bit size to 32 bits on 32-bit architectures. For exactly 4GB, the
truncated value becomes 0.

Because ilog2(0) evaluates to -1, the order calculation underflows,
producing a large positive value in the unsigned int order variable.

When this underflowed order is subsequently used to index into the free
trees further down in this function:

	root = &mm->free_trees[tree][order];

Would this result in an out-of-bounds memory read and a potential crash?

>  	if (order == 0)
>  		return -ENOSPC;
>

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260709130221.1021376-1-Arunpravin.PaneerSelvam@amd.com?part=1

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] gpu/buddy: bail out of try_harder when alignment cannot be honoured
  2026-07-09 13:10 [PATCH v2] gpu/buddy: bail out of try_harder when alignment cannot be honoured Arunpravin Paneer Selvam
@ 2026-07-09 13:29 ` sashiko-bot
  0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-07-09 13:29 UTC (permalink / raw)
  To: Arunpravin Paneer Selvam; +Cc: dri-devel

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Integer truncation in `rounddown_pow_of_two(size)` on 32-bit architectures leads to out-of-bounds array access.
--

--- Patch [1]: [PATCH v2] gpu/buddy: bail out of try_harder when alignment cannot be honoured ---
commit e9f823b96a2660e1690b65d2c8c95099338dc996
Author: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com>

gpu/buddy: bail out of try_harder when alignment cannot be honoured

The try_harder contiguous fallback could return a range whose start
offset did not match the caller's min_block_size. When a candidate's
start is misaligned, realign it: free the misaligned run and reallocate
exactly @size at the next lower min_block_size boundary.

> diff --git a/drivers/gpu/buddy.c b/drivers/gpu/buddy.c
> index dc81fe0301ce8..3c73ae87f3c5b 100644
> --- a/drivers/gpu/buddy.c
> +++ b/drivers/gpu/buddy.c

[ ... ]

>  static int __alloc_contig_try_harder(struct gpu_buddy *mm,
>  				     u64 size,
>  				     u64 min_block_size,
>  				     struct list_head *blocks)
>  {
> -	u64 rhs_offset, lhs_offset, lhs_size, filled;
> +	u64 rhs_offset, lhs_offset, filled;
>  	struct gpu_buddy_block *block;
>  	unsigned int tree, order;
> -	LIST_HEAD(blocks_lhs);
> -	unsigned long pages;
>  	u64 modify_size;
>  	int err;
>  
>  	modify_size = rounddown_pow_of_two(size);
> -	pages = modify_size >> ilog2(mm->chunk_size);
> -	order = fls(pages) - 1;
> +	order = ilog2(modify_size) - ilog2(mm->chunk_size);

[Severity: High]
This isn't a bug introduced by this patch, but could this integer truncation
lead to out-of-bounds array accesses on 32-bit architectures?

The size parameter is a u64, but the rounddown_pow_of_two() macro operates
on an unsigned long. On a 32-bit system, if a large size is passed
(e.g., 4GB) where the lower 32 bits are smaller than chunk_size or zero,
the truncation can result in modify_size becoming 0.

When modify_size is 0, ilog2(0) yields -1. This causes order (an unsigned
int) to underflow into a massive positive value. The if (order == 0) check
is bypassed, and this huge order value is then used to index into the
mm->free_trees[tree][order] array, which could crash the kernel during the
RB tree traversal.

>  	if (order == 0)
>  		return -ENOSPC;

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260709131050.1022759-1-Arunpravin.PaneerSelvam@amd.com?part=1

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-07-09 13:29 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-09 13:10 [PATCH v2] gpu/buddy: bail out of try_harder when alignment cannot be honoured Arunpravin Paneer Selvam
2026-07-09 13:29 ` sashiko-bot
  -- strict thread matches above, loose matches on Subject: below --
2026-07-09 13:02 Arunpravin Paneer Selvam
2026-07-09 13:25 ` sashiko-bot
2026-06-29  7:43 Arunpravin Paneer Selvam
2026-07-02 10:48 ` Matthew Auld
2026-07-03  0:35   ` John Olender
2026-07-03 13:40     ` Arunpravin Paneer Selvam
2026-07-09 13:25     ` Arunpravin Paneer Selvam

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox