From: "Jason A. Donenfeld" <Jason@zx2c4.com>
To: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: kernel test robot <lkp@intel.com>,
"Jason A. Donenfeld" <zx2c4@kernel.org>,
kbuild-all@lists.01.org, linux-kernel@vger.kernel.org
Subject: Re: [crng-random:jd/get_random_u32_below 24/26] include/linux/random.h:64:77: sparse: sparse: cast truncates bits from constant value (e24d4d50 becomes 50)
Date: Tue, 11 Oct 2022 12:33:47 -0600 [thread overview]
Message-ID: <Y0W3C5n4Tjy/Cri6@zx2c4.com> (raw)
In-Reply-To: <0dc78ec5-e934-fcb4-5153-1e8d3840f473@rasmusvillemoes.dk>
On Tue, Oct 11, 2022 at 03:21:44PM +0200, Rasmus Villemoes wrote:
> On 11/10/2022 11.59, kernel test robot wrote:
>
> > c440408cf6901e Jason A. Donenfeld 2017-01-22 53
> > dbc0933c6b1d8c Jason A. Donenfeld 2022-10-08 54 u32 __get_random_u32_below(u32 ceil);
> > dbc0933c6b1d8c Jason A. Donenfeld 2022-10-08 55 /* Returns a random integer in the interval [0, ceil), with uniform distribution. */
> > dbc0933c6b1d8c Jason A. Donenfeld 2022-10-08 56 static inline u32 get_random_u32_below(u32 ceil)
> > dbc0933c6b1d8c Jason A. Donenfeld 2022-10-08 57 {
> > dbc0933c6b1d8c Jason A. Donenfeld 2022-10-08 58 if (!__builtin_constant_p(ceil))
> > dbc0933c6b1d8c Jason A. Donenfeld 2022-10-08 59 return __get_random_u32_below(ceil);
> > dbc0933c6b1d8c Jason A. Donenfeld 2022-10-08 60
> > dbc0933c6b1d8c Jason A. Donenfeld 2022-10-08 61 for (;;) {
> > dbc0933c6b1d8c Jason A. Donenfeld 2022-10-08 62 if (ceil <= 1U << 8) {
> > dbc0933c6b1d8c Jason A. Donenfeld 2022-10-08 63 u32 mult = ceil * get_random_u8();
> > dbc0933c6b1d8c Jason A. Donenfeld 2022-10-08 @64 if (is_power_of_2(ceil) || (u8)mult >= -(__force u8)ceil % ceil)
> > dbc0933c6b1d8c Jason A. Donenfeld 2022-10-08 65 return mult >> 8;
>
> I don't have a good suggestion for how to silence sparse, but I think
> the cast and unary minus here needs to be interchanged. I.e., the
> condition should be
>
> if (is_power_of_2(ceil) || (u8)mult >= ((__force u8)-ceil) % ceil)
>
> Otherwise it fails to provide uniform distribution for ceil=11, 19, 22,
> 23, ... [these are the numbers that are not divisors of 2^32-2^8].
Thanks! Note that this is an incomplete dev branch I haven't posted to
the ML yet. As for the sparse error, I think I'll fix both issues at
once by just doing (256 - ceil) % ceil.
Jason
prev parent reply other threads:[~2022-10-11 18:34 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-11 9:59 kernel test robot
2022-10-11 13:21 ` Rasmus Villemoes
2022-10-11 18:33 ` Jason A. Donenfeld [this message]
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=Y0W3C5n4Tjy/Cri6@zx2c4.com \
--to=jason@zx2c4.com \
--cc=kbuild-all@lists.01.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=lkp@intel.com \
--cc=zx2c4@kernel.org \
/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®