dri-devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "David Gow" <david@davidgow.net>
Cc: dri-devel@lists.freedesktop.org, dsterba@suse.cz
Subject: Re: [PATCH v2 1/3] linux/log2.h: Add 64-bit safe variants of power-of-two functions
Date: Sun, 30 Aug 2026 10:47:11 +0000	[thread overview]
Message-ID: <20260830104711.DBA221F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260830103321.2042968-1-david@davidgow.net>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] `__roundup_pow_of_two_u64()` computes the wrong value for `n = 1` when optimizations propagate the constant, due to misuse of `ilog2(0)`.
- [Low] Kernel-doc comment for `__rounddown_pow_of_two_u64` incorrectly uses the suffix `_64` instead of `_u64`.
--

commit 1b2e30cc48e12ee945f73271fbcb60bed282f343
Author: David Gow <david@davidgow.net>

linux/log2.h: Add 64-bit safe variants of power-of-two functions

This commit adds 64-bit safe variants of power-of-two functions which always
operate on a 64-bit value regardless of sizeof(long). It also removes a
conflicting implementation from i915.

> diff --git a/include/linux/log2.h b/include/linux/log2.h
> index e17ceb32e0c97..fc44e59f57328 100644
> --- a/include/linux/log2.h
> +++ b/include/linux/log2.h
[ ... ]
> @@ -195,6 +211,63 @@ unsigned long __rounddown_pow_of_two(unsigned long n)
>  	__rounddown_pow_of_two(n)		\
>   )
>  
> +/**
> + * __rounddown_pow_of_two_64() - round a 64-bit value down to nearest power of two
> + * @n: value to round down
> + */
> +static inline __attribute_const__
> +u64 __rounddown_pow_of_two_u64(u64 n)

[Severity: Low]
Is there a typo in the kernel-doc function name?

The comment documents __rounddown_pow_of_two_64, but the function is named
__rounddown_pow_of_two_u64. Will this cause kernel-doc warnings?

> +{
> +	return 1ULL << ilog2(n);
> +}
> +
[ ... ]
> +/**
> + * __roundup_pow_of_two_u64() - round a 64-bit value up to nearest power of two
> + * @n: value to round up
> + */
> +static inline __attribute_const__
> +u64 __roundup_pow_of_two_u64(u64 n)
> +{
> +	return 1ULL << (ilog2(n - 1) + 1);
> +}

[Severity: High]
Can this compute the wrong value for n = 1 if the compiler propagates the
constant after macro expansion?

If n = 1 is passed to roundup_pow_of_two_u64(n) and __builtin_constant_p(n)
evaluates to false at macro expansion time, it will fall back to calling this
inline function.

When the compiler later inlines the function and proves n = 1 via constant
propagation, it will evaluate ilog2(0).

Inside the ilog2(0) macro, __builtin_constant_p(0) evaluates to true, so it
takes the constant branch (0 < 2 ? 0 : ...), which evaluates to 0.

Would this result in 1ULL << (0 + 1) returning 2 instead of 1?

Should this use fls64(n - 1) instead, similar to how the 32-bit
__roundup_pow_of_two() uses fls_long(n - 1)?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260830103321.2042968-1-david@davidgow.net?part=1

  parent reply	other threads:[~2026-08-30 10:47 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-30 10:33 [PATCH v2 1/3] linux/log2.h: Add 64-bit safe variants of power-of-two functions David Gow
2026-08-30 10:33 ` [PATCH v2 2/3] drm_buddy: fix power-of-2 rounding errs David Gow
2026-08-30 10:58   ` sashiko-bot
2026-08-30 10:33 ` [PATCH v2 3/3] fs:btrfs: Use the new global is_power_of_2_u64() helper David Gow
2026-08-31 18:46   ` David Sterba
2026-08-30 10:47 ` sashiko-bot [this message]
2026-08-30 11:43 ` [PATCH v2 1/3] linux/log2.h: Add 64-bit safe variants of power-of-two functions David Laight

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=20260830104711.DBA221F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=david@davidgow.net \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=dsterba@suse.cz \
    --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