public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Jason A. Donenfeld" <Jason@zx2c4.com>
To: David Laight <David.Laight@ACULAB.COM>
Cc: "Jason A. Donenfeld" <zx2c4@kernel.org>,
	"kbuild-all@lists.01.org" <kbuild-all@lists.01.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [crng-random:jd/get_random_u32_below 23/23] include/linux/random.h:64:69: sparse: sparse: cast truncates bits from constant value (1f4 becomes f4)
Date: Mon, 10 Oct 2022 19:53:51 +0200	[thread overview]
Message-ID: <Y0RcL2j98CatloRr@zx2c4.com> (raw)
In-Reply-To: <a473cc4da4874235b2b98fd1a57e782e@AcuMS.aculab.com>

On Mon, Oct 10, 2022 at 05:18:40PM +0000, David Laight wrote:
> From: kernel test robot <lkp@intel.com>
> > Sent: 10 October 2022 00:32
> > To: Jason A. Donenfeld <zx2c4@kernel.org>
> ...
> 
> I'm missing the main mailing list email for this change.
> I'm guessing the non-inlined code for non-constant ceil
> is similar.

It's part of a development tree I already linked you to. It's not done
yet. This alert is just about needing a __force.

> 
> > vim +64 include/linux/random.h
> > 
> >     53
> >     54	u32 __get_random_u32_below(u32 ceil);
> >     55	/* Returns a random integer in the interval [0, ceil), with uniform distribution. */
> >     56	static inline u32 get_random_u32_below(u32 ceil)
> >     57	{
> >     58		if (!__builtin_constant_p(ceil))
> >     59			return __get_random_u32_below(ceil);
> >     60
> >     61		for (;;) {
> >     62			if (ceil <= 1U << 8) {
> >     63				u32 mult = ceil * get_random_u8();
> >   > 64				if (is_power_of_2(ceil) || (u8)mult >= -(u8)ceil % ceil)
> >     65					return mult >> 8;
> 
> If you are going to check is_power_of_2() then you might as well do:
> 		u32 val = get_random_u8();
> 		if (is_power_of_2(ceil))
> 			return ceil == 0x100 ? val : val & (ceil - 1);
> Except that you don't want to loop on zero - so !(ceil & (ceil - 1))

I thought I opted out of the Laight drive-bys?

Either way, I don't understand your comments or how they pertain to the
current code in that unfinished development branch. Please just send
real patches.

Jason

  reply	other threads:[~2022-10-10 17:54 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-09 23:32 [crng-random:jd/get_random_u32_below 23/23] include/linux/random.h:64:69: sparse: sparse: cast truncates bits from constant value (1f4 becomes f4) kernel test robot
2022-10-10 17:18 ` David Laight
2022-10-10 17:53   ` Jason A. Donenfeld [this message]
2022-10-11 23:24     ` David Laight
2022-10-11 23:31       ` Jason A. Donenfeld

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=Y0RcL2j98CatloRr@zx2c4.com \
    --to=jason@zx2c4.com \
    --cc=David.Laight@ACULAB.COM \
    --cc=kbuild-all@lists.01.org \
    --cc=linux-kernel@vger.kernel.org \
    --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