From: "Jason A. Donenfeld" <Jason@zx2c4.com>
To: Kees Cook <keescook@chromium.org>
Cc: "Darrick J . Wong" <djwong@kernel.org>,
patches@lists.linux.dev, netdev@vger.kernel.org,
"Andreas Dilger" <adilger.kernel@dilger.ca>,
ydroneaud@opteya.com, "Herbert Xu" <herbert@gondor.apana.org.au>,
"Richard Weinberger" <richard@nod.at>,
"Helge Deller" <deller@gmx.de>,
"Russell King" <linux@armlinux.org.uk>,
"Jason Gunthorpe" <jgg@nvidia.com>,
"Catalin Marinas" <catalin.marinas@arm.com>,
"Jakub Kicinski" <kuba@kernel.org>,
linux-mips@vger.kernel.org, linux-media@vger.kernel.org,
"Heiko Carstens" <hca@linux.ibm.com>,
"Jani Nikula" <jani.nikula@linux.intel.com>,
linux-block@vger.kernel.org, "SeongJae Park" <sj@kernel.org>,
loongarch@lists.linux.dev, "Jaegeuk Kim" <jaegeuk@kernel.org>,
"Thomas Gleixner" <tglx@linutronix.de>,
linux-arm-kernel@lists.infradead.org,
"Thomas Bogendoerfer" <tsbogend@alpha.franken.de>,
"Theodore Ts'o" <tytso@mit.edu>,
linux-parisc@vger.kernel.org,
"Martin K . Petersen" <martin.petersen@oracle.com>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
linux-mmc@vger.kernel.org
Subject: Re: [PATCH v2 3/3] treewide: use get_random_u32_between() when possible
Date: Thu, 17 Nov 2022 00:55:47 +0100 [thread overview]
Message-ID: <Y3V4g8eorwiU++Y3@zx2c4.com> (raw)
In-Reply-To: <202211161436.A45AD719A@keescook>
On Wed, Nov 16, 2022 at 02:43:13PM -0800, Kees Cook wrote:
> On Mon, Nov 14, 2022 at 05:45:58PM +0100, Jason A. Donenfeld wrote:
> > - (get_random_u32_below(1024) + 1) * PAGE_SIZE;
> > + get_random_u32_between(1, 1024 + 1) * PAGE_SIZE;
>
> I really don't like "between". Can't this be named "inclusive" (and
> avoid adding 1 everywhere, which seems ugly), or at least named
> something less ambiguous?
>
> > - n = get_random_u32_below(100) + 1;
> > + n = get_random_u32_between(1, 101);
>
> Because I find this much less readable. "Below 100" is clear: 0-99
> inclusive, plus 1, so 1-100 inclusive. "Between 1 and 101" is not obvious
> to me to mean: 1-100 inclusive.
>
> These seem so much nicer:
> get_random_u32_inclusive(1, 1024)
> get_random_u32_inclusive(1, 100)
Yann pointed out something similar -- the half-closed interval being
confusing -- and while I was initially dismissive, I've warmed up to
doing this fully closed after sending a diff of that:
https://lore.kernel.org/lkml/Y3Qt8HiXj8giOnZy@zx2c4.com/
So okay, let's say that I'll implement the inclusive version instead. We
now have two problems to solve:
1) How/whether to make f(0, UR2_MAX) safe,
- without additional 64-bit arithmetic,
- minimizing the number of branches.
I have a few ideas I'll code golf for a bit.
2) What to call it:
- between I still like, because it mirrors "I'm thinking of a number
between 1 and 10 and..." that everybody knows,
- inclusive I guess works, but it's not a preposition,
- bikeshed color #3?
I think I can make progress with (1) alone by fiddling around with
godbolt enough, like usual. I could use some more ideas for (2) though.
Jason
next prev parent reply other threads:[~2022-11-16 23:57 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-14 16:45 [PATCH v2 0/3] convert tree to get_random_u32_{below,above,between}() Jason A. Donenfeld
2022-11-14 16:45 ` [PATCH v2 1/3] treewide: use get_random_u32_below() instead of deprecated function Jason A. Donenfeld
2022-11-14 17:35 ` Russell King (Oracle)
2022-11-14 16:45 ` [PATCH v2 2/3] treewide: use get_random_u32_{above,below}() instead of manual loop Jason A. Donenfeld
2022-11-14 16:45 ` [PATCH v2 3/3] treewide: use get_random_u32_between() when possible Jason A. Donenfeld
2022-11-16 22:43 ` Kees Cook
2022-11-16 23:55 ` Jason A. Donenfeld [this message]
2022-11-17 0:03 ` Jason A. Donenfeld
2022-11-17 0:31 ` Kees Cook
2022-11-17 0:43 ` Jason A. Donenfeld
2022-11-17 0:47 ` Kees Cook
2022-11-17 15:42 ` Theodore Ts'o
2022-11-17 16:19 ` Jason A. Donenfeld
2022-11-17 22:15 ` David Laight
2022-11-17 2:05 ` Jason A. Donenfeld
2022-11-17 10:32 ` Russell King (Oracle)
2022-11-17 20:29 ` [PATCH v3 0/3] convert tree to get_random_u32_{below,above,inclusive}() Jason A. Donenfeld
2022-11-17 20:29 ` [PATCH v3 1/3] treewide: use get_random_u32_below() instead of deprecated function Jason A. Donenfeld
2022-11-17 21:59 ` Kees Cook
2022-11-17 20:29 ` [PATCH v3 2/3] treewide: use get_random_u32_{above,below}() instead of manual loop Jason A. Donenfeld
2022-11-17 21:58 ` Kees Cook
2022-11-17 20:29 ` [PATCH v3 3/3] treewide: use get_random_u32_inclusive() when possible Jason A. Donenfeld
2022-11-17 21:57 ` Kees Cook
2022-11-17 22:10 ` 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=Y3V4g8eorwiU++Y3@zx2c4.com \
--to=jason@zx2c4.com \
--cc=adilger.kernel@dilger.ca \
--cc=catalin.marinas@arm.com \
--cc=deller@gmx.de \
--cc=djwong@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=hca@linux.ibm.com \
--cc=herbert@gondor.apana.org.au \
--cc=jaegeuk@kernel.org \
--cc=jani.nikula@linux.intel.com \
--cc=jgg@nvidia.com \
--cc=keescook@chromium.org \
--cc=kuba@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-block@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=linux-mips@vger.kernel.org \
--cc=linux-mmc@vger.kernel.org \
--cc=linux-parisc@vger.kernel.org \
--cc=linux@armlinux.org.uk \
--cc=loongarch@lists.linux.dev \
--cc=martin.petersen@oracle.com \
--cc=netdev@vger.kernel.org \
--cc=patches@lists.linux.dev \
--cc=richard@nod.at \
--cc=sj@kernel.org \
--cc=tglx@linutronix.de \
--cc=tsbogend@alpha.franken.de \
--cc=tytso@mit.edu \
--cc=ydroneaud@opteya.com \
/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;
as well as URLs for NNTP newsgroup(s).