From: David Gow <david@ingeniumdigital.com>
To: Jim Cromie <jim.cromie@gmail.com>,
"Maciej W . Rozycki" <macro@orcam.me.uk>,
Andrew Morton <akpm@linux-foundation.org>,
Matthew Auld <matthew.auld@intel.com>,
Arun Pravin <arunpravin.paneerselvam@amd.com>,
Joel Fernandes <joelagnelf@nvidia.com>,
David Airlie <airlied@gmail.com>, Simona Vetter <simona@ffwll.ch>
Cc: David Gow <david@davidgow.net>,
dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org
Subject: [PATCH 1/2] linux/log2.h: Add round{up,down}_pow_of_two64()
Date: Fri, 21 Aug 2026 17:19:14 +0800 [thread overview]
Message-ID: <20260821091918.1902032-1-david@ingeniumdigital.com> (raw)
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
next reply other threads:[~2026-08-21 9:19 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-21 9:19 David Gow [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260821091918.1902032-1-david@ingeniumdigital.com \
--to=david@ingeniumdigital.com \
--cc=airlied@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=arunpravin.paneerselvam@amd.com \
--cc=david@davidgow.net \
--cc=dri-devel@lists.freedesktop.org \
--cc=jim.cromie@gmail.com \
--cc=joelagnelf@nvidia.com \
--cc=linux-kernel@vger.kernel.org \
--cc=macro@orcam.me.uk \
--cc=matthew.auld@intel.com \
--cc=simona@ffwll.ch \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®