From: sashiko-bot@kernel.org
To: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
linux-pci@vger.kernel.org
Subject: Re: [PATCH v4 5/7] linux/log2.h: Add 64-bit roundup/down_pow_of_two64() variants
Date: Wed, 21 Oct 2026 12:57:48 +0000 [thread overview]
Message-ID: <sashiko-outbox-180283@kernel.org> (raw)
In-Reply-To: <20261021124956.4250-6-ilpo.jarvinen@linux.intel.com>
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] log2: 32-bit 1UL shift overflow in roundup/down_pow_of_two64()
--
commit 43925e5ad2f800f4d000698aad5a8426cb02653d
Author: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
linux/log2.h: Add 64-bit roundup/down_pow_of_two64() variants
This commit adds 64-bit roundup_pow_of_two64() and rounddown_pow_of_two64()
helpers in include/linux/log2.h so 64-bit inputs are not truncated to
unsigned long on 32-bit architectures.
> diff --git a/include/linux/log2.h b/include/linux/log2.h
> index e17ceb32e0c97..180e5702f6779 100644
> --- a/include/linux/log2.h
> +++ b/include/linux/log2.h
[ ... ]
> @@ -163,6 +183,23 @@ unsigned long __rounddown_pow_of_two(unsigned long n)
> __ilog2_u64(n) \
> )
>
> +/**
> + * roundup_pow_of_two64 - round the given value up to nearest power of two
> + * @n: parameter
> + *
> + * round the given value up to the nearest power of two
> + * - 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) ? 1 : \
> + (1UL << (ilog2((n) - 1) + 1)) \
> + ) : \
> + __roundup_pow_of_two64(n) \
> + )
> +
> /**
> * roundup_pow_of_two - round the given value up to nearest power of two
> * @n: parameter
> @@ -180,6 +217,21 @@ unsigned long __rounddown_pow_of_two(unsigned long n)
> __roundup_pow_of_two(n) \
> )
>
> +/**
> + * rounddown_pow_of_two64 - round the given value down to nearest power of two
> + * @n: parameter
> + *
> + * round the given value down to the nearest power of two
> + * - 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) ? ( \
> + (1UL << ilog2(n))) : \
> + __rounddown_pow_of_two64(n) \
> + )
[Severity: Medium]
In include/linux/log2.h, __roundup_pow_of_two64() and
__rounddown_pow_of_two64() shift 1ULL, but the __builtin_constant_p(n)
branches in roundup_pow_of_two64() and rounddown_pow_of_two64() still shift
1UL:
(1UL << (ilog2((n) - 1) + 1))
and:
(1UL << ilog2(n))
On 32-bit architectures where unsigned long is 32 bits wide, the left shift
is evaluated in the type of its left operand (unsigned long) before the
conditional expression is converted to u64. When n is a compile-time constant
with n > (1ULL << 31) in roundup_pow_of_two64(n) or n >= (1ULL << 32) in
rounddown_pow_of_two64(n), the shift count is at least 32, which overflows
the 32-bit 1UL operand and triggers a shift-count-overflow warning while
truncating the result.
Should both __builtin_constant_p(n) branches use 1ULL instead of 1UL (and
1ULL for the (n) == 1 case in roundup_pow_of_two64())?
> +
> /**
> * rounddown_pow_of_two - round the given value down to nearest power of two
> * @n: parameter
[ ... ]
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261021124956.4250-1-ilpo.jarvinen@linux.intel.com?part=5
next prev parent reply other threads:[~2026-10-21 12:57 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-21 12:49 [PATCH v4 0/7] PCI: Resource placement algorithm fixe Ilpo Järvinen
2026-10-21 12:49 ` [PATCH v4 1/7] resource: Mark free space assigned Ilpo Järvinen
2026-10-21 12:56 ` sashiko-bot
2026-10-21 12:49 ` [PATCH v4 2/7] PCI/parisc: Clean up resource debug print & use dynamic debug Ilpo Järvinen
2026-10-21 12:56 ` sashiko-bot
2026-10-21 12:49 ` [PATCH v4 3/7] PCI: Honor alignment overrides Ilpo Järvinen
2026-10-21 13:22 ` sashiko-bot
2026-10-21 12:49 ` [PATCH v4 4/7] PCI: Fix nesting windows with remainder at the left edge Ilpo Järvinen
2026-10-21 13:32 ` sashiko-bot
2026-10-21 12:49 ` [PATCH v4 5/7] linux/log2.h: Add 64-bit roundup/down_pow_of_two64() variants Ilpo Järvinen
2026-10-21 12:57 ` sashiko-bot [this message]
2026-10-21 12:49 ` [PATCH v4 6/7] PCI: Place resources to either edge of the window Ilpo Järvinen
2026-10-21 13:30 ` sashiko-bot
2026-10-21 12:49 ` [PATCH v4 7/7] PCI: Fix composite resource sizing Ilpo Järvinen
2026-10-21 13:25 ` sashiko-bot
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=sashiko-outbox-180283@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=ilpo.jarvinen@linux.intel.com \
--cc=linux-pci@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/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