* [PATCH 1/2] linux/log2.h: Add round{up,down}_pow_of_two64()
@ 2026-08-21 9:19 David Gow
2026-08-21 9:19 ` [PATCH 2/2] drm_buddy: fix power-of-2 rounding errs David Gow
0 siblings, 1 reply; 3+ messages in thread
From: David Gow @ 2026-08-21 9:19 UTC (permalink / raw)
To: Jim Cromie, Maciej W . Rozycki, Andrew Morton, Matthew Auld,
Arun Pravin, Joel Fernandes, David Airlie, Simona Vetter
Cc: David Gow, dri-devel, linux-kernel
From: David Gow <david@davidgow.net>
The existing roundup_pow_of_two() and rounddown_pow_of_two() functions work
on values of type unsigned long, which is 32-bit on 32-bit systems. There
are cases (most notably in the GPU buddy allocator) which always operate on
64-bit values (as a 32-bit host can still use a GPU which internally has
64-bit addresses).
Add a version of these which always operate on a 64-bit value. These have
the (unimaginative) names roundup_pow_of_two64() and
rounddown_pow_of_two64(), and otherwise work identically to their unsigned
long counterparts. They are a bit ugly, but better than everyone hardcoding
calls to ilog2() -- which does handle 64-bit values -- directly.
Signed-off-by: David Gow <david@davidgow.net>
---
Hi all,
This series is basically a reworked and rebased version of
https://lore.kernel.org/all/20260326185413.1205870-40-jim.cromie@gmail.com/
Most importantly, the drm_test_buddy_alloc_exceeds_max_order KUnit test
was failing in linux-next on 32-bit systems, which this series fixes.
In rebasing it, I decided to add these helper functions, rather than
having an open-coded round{up,down} implementation in the buddy allocator,
but if no-one thinks it's worth having these for (at the moment) just one
user, I'm not too worried either way.
Cheers,
-- David
---
include/linux/log2.h | 57 ++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 57 insertions(+)
diff --git a/include/linux/log2.h b/include/linux/log2.h
index e17ceb32e0c9..67446d5ce9a3 100644
--- a/include/linux/log2.h
+++ b/include/linux/log2.h
@@ -195,6 +195,63 @@ unsigned long __rounddown_pow_of_two(unsigned long n)
__rounddown_pow_of_two(n) \
)
+/**
+ * __rounddown_pow_of_two64() - round a 64-bit value down to nearest power of two
+ * @n: value to round up
+ */
+static inline __attribute_const__
+u64 __rounddown_pow_of_two64(u64 n)
+{
+ return 1ULL << ilog2(n);
+}
+
+/**
+ * rounddown_pow_of_two64 - round a 64-bit value down to nearest power of two
+ * @n: parameter
+ *
+ * round the given value down to the nearest power of two
+ * - this always operates on 64-bit values, even on 32-bit systems
+ * - the result is undefined when n == 0
+ * - this can be used to initialise global variables from constant data
+ */
+#define rounddown_pow_of_two64(n) \
+( \
+ __builtin_constant_p(n) ? ( \
+ ((n) == 1) ? 1ULL : \
+ (1ULL << ilog2((n))) \
+ ) : \
+ __rounddown_pow_of_two64(n) \
+)
+
+
+/**
+ * __roundup_pow_of_two64() - round a 64-bit value up to nearest power of two
+ * @n: value to round up
+ */
+static inline __attribute_const__
+u64 __roundup_pow_of_two64(u64 n)
+{
+ return 1ULL << (ilog2(n - 1) + 1);
+}
+
+/**
+ * roundup_pow_of_two64 - round a 64-bit value up to nearest power of two
+ * @n: parameter
+ *
+ * round the given value up to the nearest power of two
+ * - this always operates on 64-bit values, even on 32-bit systems
+ * - the result is undefined when n == 0
+ * - this can be used to initialise global variables from constant data
+ */
+#define roundup_pow_of_two64(n) \
+( \
+ __builtin_constant_p(n) ? ( \
+ ((n) == 1) ? 1ULL : \
+ (1ULL << (ilog2((n) - 1) + 1)) \
+ ) : \
+ __roundup_pow_of_two64(n) \
+)
+
static inline __attribute_const__
int __order_base_2(unsigned long n)
{
--
2.55.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 2/2] drm_buddy: fix power-of-2 rounding errs
2026-08-21 9:19 [PATCH 1/2] linux/log2.h: Add round{up,down}_pow_of_two64() David Gow
@ 2026-08-21 9:19 ` David Gow
2026-08-21 9:47 ` Matthew Auld
0 siblings, 1 reply; 3+ messages in thread
From: David Gow @ 2026-08-21 9:19 UTC (permalink / raw)
To: Jim Cromie, Maciej W . Rozycki, Andrew Morton, Matthew Auld,
Arun Pravin, Joel Fernandes, David Airlie, Simona Vetter
Cc: dri-devel, linux-kernel, David Gow
From: Jim Cromie <jim.cromie@gmail.com>
The standard roundup_pow_of_two() and rounddown_pow_of_two() macros use
unsigned long internally, which on 32-bit architectures (like arm32) is
a 32-bit type.
drm_test_buddy_alloc_exceeds_max_order() uses these on a u64 value,
where they silently truncate the 10GB allocation, giving unexpected
success in DRM-CI. (see below the snip).
Fix this by replacing the those macros with the safe 64-bit power-of-two
equivalents added in the previous patch.
Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
Signed-off-by: David Gow <david@davidgow.net>
---
This should actually be version 13, I think, as it's just a rebase of v12
here, where it was part of a large series of fixups:
https://lore.kernel.org/all/20260326185413.1205870-40-jim.cromie@gmail.com/
The major changes since that version are:
- Add the helper functions rounddown_pow_of_two64() and
roundup_pow_of_two64() (see patch 1) instead of open-coding them
- Rebase now that the buddy allocator lives in drivers/gpu/buddy.c
instead of drivers/gpu/drm/drm_buddy.c
This is still breaking the gpu_test_buddy_alloc_exceeds_max_order
KUnit test on 32-bit systems:
[09:01:26] # gpu_test_buddy_alloc_exceeds_max_order: EXPECTATION FAILED at drivers/gpu/tests/gpu_buddy_test.c:1429
[09:01:26] Expected err == -22, but
[09:01:26] err == 0 (0x0)
[09:01:26] WARNING: drivers/gpu/buddy.c:508 at gpu_buddy_fini+0x244/0x2e0, CPU#0: kunit_try_catch/1595
[09:01:26] # gpu_test_buddy_alloc_exceeds_max_order: drivers/gpu/buddy.c:508: gpu_buddy_assert(gpu_buddy_block_is_free(mm->roots[i]))
[09:01:26] WARNING: drivers/gpu/buddy.c:516 at gpu_buddy_fini+0x284/0x2e0, CPU#0: kunit_try_catch/1595
[09:01:26] # gpu_test_buddy_alloc_exceeds_max_order: drivers/gpu/buddy.c:508: gpu_buddy_assert(gpu_buddy_block_is_free(mm->roots[i]))
[09:01:26] WARNING: drivers/gpu/buddy.c:516 at gpu_buddy_fini+0x284/0x2e0, CPU#0: kunit_try_catch/1595
[09:01:26] # gpu_test_buddy_alloc_exceeds_max_order: drivers/gpu/buddy.c:519: gpu_buddy_assert(!mm->used_scoreboard[i])
[09:01:26] [FAILED] gpu_test_buddy_alloc_exceeds_max_order
Cheers,
-- David
---
drivers/gpu/buddy.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/gpu/buddy.c b/drivers/gpu/buddy.c
index a5553fcec28c..64f41f1fe908 100644
--- a/drivers/gpu/buddy.c
+++ b/drivers/gpu/buddy.c
@@ -1193,7 +1193,7 @@ static int __alloc_contig_try_harder(struct gpu_buddy *mm,
u64 modify_size;
int err;
- modify_size = rounddown_pow_of_two(size);
+ modify_size = rounddown_pow_of_two64(size);
order = ilog2(modify_size) - ilog2(mm->chunk_size);
if (order == 0)
return -ENOSPC;
@@ -1440,7 +1440,7 @@ int gpu_buddy_alloc_blocks(struct gpu_buddy *mm,
/* Roundup the size to power of 2 */
if (flags & GPU_BUDDY_CONTIGUOUS_ALLOCATION) {
- size = roundup_pow_of_two(size);
+ size = roundup_pow_of_two64(size);
min_block_size = size;
/*
* Normalize the requested size to min_block_size for regular allocations.
--
2.55.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH 2/2] drm_buddy: fix power-of-2 rounding errs
2026-08-21 9:19 ` [PATCH 2/2] drm_buddy: fix power-of-2 rounding errs David Gow
@ 2026-08-21 9:47 ` Matthew Auld
0 siblings, 0 replies; 3+ messages in thread
From: Matthew Auld @ 2026-08-21 9:47 UTC (permalink / raw)
To: David Gow, Jim Cromie, Maciej W . Rozycki, Andrew Morton,
Arun Pravin, Joel Fernandes, David Airlie, Simona Vetter
Cc: dri-devel, linux-kernel, David Gow
On 21/08/2026 10:19, David Gow wrote:
> From: Jim Cromie <jim.cromie@gmail.com>
>
> The standard roundup_pow_of_two() and rounddown_pow_of_two() macros use
> unsigned long internally, which on 32-bit architectures (like arm32) is
> a 32-bit type.
>
> drm_test_buddy_alloc_exceeds_max_order() uses these on a u64 value,
> where they silently truncate the 10GB allocation, giving unexpected
> success in DRM-CI. (see below the snip).
>
> Fix this by replacing the those macros with the safe 64-bit power-of-two
> equivalents added in the previous patch.
Do we also need something like is_power_of_2_u64() ? I think we could
then remove that from i915_utils.h
>
> Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
> Signed-off-by: David Gow <david@davidgow.net>
> ---
>
> This should actually be version 13, I think, as it's just a rebase of v12
> here, where it was part of a large series of fixups:
> https://lore.kernel.org/all/20260326185413.1205870-40-jim.cromie@gmail.com/
>
> The major changes since that version are:
> - Add the helper functions rounddown_pow_of_two64() and
> roundup_pow_of_two64() (see patch 1) instead of open-coding them
> - Rebase now that the buddy allocator lives in drivers/gpu/buddy.c
> instead of drivers/gpu/drm/drm_buddy.c
>
> This is still breaking the gpu_test_buddy_alloc_exceeds_max_order
> KUnit test on 32-bit systems:
> [09:01:26] # gpu_test_buddy_alloc_exceeds_max_order: EXPECTATION FAILED at drivers/gpu/tests/gpu_buddy_test.c:1429
> [09:01:26] Expected err == -22, but
> [09:01:26] err == 0 (0x0)
> [09:01:26] WARNING: drivers/gpu/buddy.c:508 at gpu_buddy_fini+0x244/0x2e0, CPU#0: kunit_try_catch/1595
> [09:01:26] # gpu_test_buddy_alloc_exceeds_max_order: drivers/gpu/buddy.c:508: gpu_buddy_assert(gpu_buddy_block_is_free(mm->roots[i]))
> [09:01:26] WARNING: drivers/gpu/buddy.c:516 at gpu_buddy_fini+0x284/0x2e0, CPU#0: kunit_try_catch/1595
> [09:01:26] # gpu_test_buddy_alloc_exceeds_max_order: drivers/gpu/buddy.c:508: gpu_buddy_assert(gpu_buddy_block_is_free(mm->roots[i]))
> [09:01:26] WARNING: drivers/gpu/buddy.c:516 at gpu_buddy_fini+0x284/0x2e0, CPU#0: kunit_try_catch/1595
> [09:01:26] # gpu_test_buddy_alloc_exceeds_max_order: drivers/gpu/buddy.c:519: gpu_buddy_assert(!mm->used_scoreboard[i])
> [09:01:26] [FAILED] gpu_test_buddy_alloc_exceeds_max_order
>
> Cheers,
> -- David
>
> ---
> drivers/gpu/buddy.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/gpu/buddy.c b/drivers/gpu/buddy.c
> index a5553fcec28c..64f41f1fe908 100644
> --- a/drivers/gpu/buddy.c
> +++ b/drivers/gpu/buddy.c
> @@ -1193,7 +1193,7 @@ static int __alloc_contig_try_harder(struct gpu_buddy *mm,
> u64 modify_size;
> int err;
>
> - modify_size = rounddown_pow_of_two(size);
> + modify_size = rounddown_pow_of_two64(size);
> order = ilog2(modify_size) - ilog2(mm->chunk_size);
> if (order == 0)
> return -ENOSPC;
> @@ -1440,7 +1440,7 @@ int gpu_buddy_alloc_blocks(struct gpu_buddy *mm,
>
> /* Roundup the size to power of 2 */
> if (flags & GPU_BUDDY_CONTIGUOUS_ALLOCATION) {
> - size = roundup_pow_of_two(size);
> + size = roundup_pow_of_two64(size);
> min_block_size = size;
> /*
> * Normalize the requested size to min_block_size for regular allocations.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-08-21 9:47 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-08-21 9:19 [PATCH 1/2] linux/log2.h: Add round{up,down}_pow_of_two64() David Gow
2026-08-21 9:19 ` [PATCH 2/2] drm_buddy: fix power-of-2 rounding errs David Gow
2026-08-21 9:47 ` Matthew Auld
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®