From: Kees Cook <keescook@chromium.org>
To: "Jason A. Donenfeld" <Jason@zx2c4.com>
Cc: "Andrew Lunn" <andrew@lunn.ch>,
"Darrick J . Wong" <djwong@kernel.org>,
"Ulf Hansson" <ulf.hansson@linaro.org>,
dri-devel@lists.freedesktop.org,
"Andrii Nakryiko" <andrii@kernel.org>,
"Hans Verkuil" <hverkuil@xs4all.nl>,
linux-sctp@vger.kernel.org,
"Md . Haris Iqbal" <haris.iqbal@ionos.com>,
"Miquel Raynal" <miquel.raynal@bootlin.com>,
"Christoph Hellwig" <hch@lst.de>,
"Andy Gospodarek" <andy@greyhouse.net>,
"Sergey Matyukevich" <geomatsi@gmail.com>,
"Rohit Maheshwari" <rohitm@chelsio.com>,
ceph-devel@vger.kernel.org,
"Jozsef Kadlecsik" <kadlec@netfilter.org>,
"Nilesh Javali" <njavali@marvell.com>,
"Jean-Paul Roubelat" <jpr@f6fbb.org>,
"Dick Kennedy" <dick.kennedy@broadcom.com>,
"Jay Vosburgh" <j.vosburgh@gmail.com>,
"Potnuri Bharat Teja" <bharat@chelsio.com>,
"Vinay Kumar Yadav" <vinay.yadav@chelsio.com>,
linux-nfs@vger.kernel.org, "Nicholas Piggin" <npiggin@gmail.com>,
"Igor Mitsyanko" <imitsyanko@quantenna.com>,
"Andy Lutomirski" <luto@kernel.org>,
linux-hams@vger.kernel.org,
"Thomas Gleixner" <tglx@linutronix.de>,
"Trond Myklebust" <trond.myklebust@hammerspace.com>,
linux-raid@vger.kernel.org, "Neil Horman" <nhorman@tuxdriver.com>,
"Hante Meuleman" <hante.meuleman@broadcom.com>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
linux-usb@vger.kernel.org,
"Michael Chan" <michael.chan@broadcom.com>,
linux-kernel@vger.kernel.org, "Varun Prakash" <varun@chelsio.com>,
"Chuck Lever" <chuck.lever@oracle.com>,
netfilter-devel@vger.kernel.org,
"Masami Hiramatsu" <mhiramat@kernel.org>,
"Jiri Olsa" <jolsa@kernel.org>, "Jan Kara" <jack@suse.com>,
linux-fsdevel@vger.kernel.org,
"Lars Ellenberg" <lars.ellenberg@linbit.com>,
linux-media@vger.kernel.org,
"Claudiu Beznea" <claudiu.beznea@microchip.com>,
"Sharvari Harisangam" <sharvari.harisangam@nxp.com>,
linux-fbdev@vger.kernel.org, linux-doc@vger.kernel.org,
linux-mmc@vger.kernel.org,
"Dave Hansen" <dave.hansen@linux.intel.com>,
"Song Liu" <song@kernel.org>,
"Eric Dumazet" <edumazet@google.com>,
target-devel@vger.kernel.org, "John Stultz" <jstultz@google.com>,
"Stanislav Fomichev" <sdf@googl>
Subject: Re: [PATCH v1 0/5] treewide cleanup of random integer usage
Date: Wed, 5 Oct 2022 21:55:43 -0700 [thread overview]
Message-ID: <202210052148.B11CBC60@keescook> (raw)
In-Reply-To: <20221005214844.2699-1-Jason@zx2c4.com>
On Wed, Oct 05, 2022 at 11:48:39PM +0200, Jason A. Donenfeld wrote:
> Hi folks,
>
> This is a five part treewide cleanup of random integer handling. The
> rules for random integers are:
>
> - If you want a secure or an insecure random u64, use get_random_u64().
> - If you want a secure or an insecure random u32, use get_random_u32().
> * The old function prandom_u32() has been deprecated for a while now
> and is just a wrapper around get_random_u32().
> - If you want a secure or an insecure random u16, use get_random_u16().
> - If you want a secure or an insecure random u8, use get_random_u8().
> - If you want secure or insecure random bytes, use get_random_bytes().
> * The old function prandom_bytes() has been deprecated for a while now
> and has long been a wrapper around get_random_bytes().
> - If you want a non-uniform random u32, u16, or u8 bounded by a certain
> open interval maximum, use prandom_u32_max().
> * I say "non-uniform", because it doesn't do any rejection sampling or
> divisions. Hence, it stays within the prandom_* namespace.
>
> These rules ought to be applied uniformly, so that we can clean up the
> deprecated functions, and earn the benefits of using the modern
> functions. In particular, in addition to the boring substitutions, this
> patchset accomplishes a few nice effects:
>
> - By using prandom_u32_max() with an upper-bound that the compiler can
> prove at compile-time is ≤65536 or ≤256, internally get_random_u16()
> or get_random_u8() is used, which wastes fewer batched random bytes,
> and hence has higher throughput.
>
> - By using prandom_u32_max() instead of %, when the upper-bound is not a
> constant, division is still avoided, because prandom_u32_max() uses
> a faster multiplication-based trick instead.
>
> - By using get_random_u16() or get_random_u8() in cases where the return
> value is intended to indeed be a u16 or a u8, we waste fewer batched
> random bytes, and hence have higher throughput.
>
> So, based on those rules and benefits from following them, this patchset
> breaks down into the following five steps:
>
> 1) Replace `prandom_u32() % max` and variants thereof with
> prandom_u32_max(max).
>
> 2) Replace `(type)get_random_u32()` and variants thereof with
> get_random_u16() or get_random_u8(). I took the pains to actually
> look and see what every lvalue type was across the entire tree.
>
> 3) Replace remaining deprecated uses of prandom_u32() with
> get_random_u32().
>
> 4) Replace remaining deprecated uses of prandom_bytes() with
> get_random_bytes().
>
> 5) Remove the deprecated and now-unused prandom_u32() and
> prandom_bytes() inline wrapper functions.
>
> I was thinking of taking this through my random.git tree (on which this
> series is currently based) and submitting it near the end of the merge
> window, or waiting for the very end of the 6.1 cycle when there will be
> the fewest new patches brewing. If somebody with some treewide-cleanup
> experience might share some wisdom about what the best timing usually
> winds up being, I'm all ears.
It'd be nice to capture some (all?) of the above somewhere. Perhaps just
a massive comment in the header?
> I've CC'd get_maintainers.pl, which is a pretty big list. Probably some
> portion of those are going to bounce, too, and everytime you reply to
> this thread, you'll have to deal with a bunch of bounces coming
> immediately after. And a recipient list this big will probably dock my
> email domain's spam reputation, at least temporarily. Sigh. I think
> that's just how it goes with treewide cleanups though. Again, let me
> know if I'm doing it wrong.
I usually stick to just mailing lists and subsystem maintainers.
If any of the subsystems ask you to break this up (I hope not), I've got
this[1], which does a reasonable job of splitting a commit up into
separate commits for each matching subsystem.
Showing that a treewide change can be reproduced mechanically helps with
keeping it together as one bit treewide patch, too, I've found. :)
Thank you for the cleanup! The "u8 rnd = get_random_u32()" in the tree
has bothered me for a loooong time.
-Kees
--
Kees Cook
next prev parent reply other threads:[~2022-10-06 6:28 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-05 21:48 [PATCH v1 0/5] treewide cleanup of random integer usage Jason A. Donenfeld
2022-10-05 21:48 ` [PATCH v1 1/5] treewide: use prandom_u32_max() when possible Jason A. Donenfeld
2022-10-06 4:16 ` Kees Cook
2022-10-06 4:22 ` KP Singh
2022-10-06 12:45 ` Jason A. Donenfeld
2022-10-06 12:55 ` Jason Gunthorpe
2022-10-06 13:05 ` Andy Shevchenko
2022-10-05 21:48 ` [PATCH v1 2/5] treewide: use get_random_{u8,u16}() " Jason A. Donenfeld
2022-10-06 4:38 ` Kees Cook
2022-10-06 12:28 ` Jason A. Donenfeld
2022-10-05 21:48 ` [PATCH v1 3/5] treewide: use get_random_u32() " Jason A. Donenfeld
2022-10-06 8:43 ` Jan Kara
2022-10-06 12:33 ` [f2fs-dev] " Jason A. Donenfeld
2022-10-06 13:01 ` Andy Shevchenko
2022-10-06 13:07 ` Jason A. Donenfeld
2022-10-06 12:47 ` Jason Gunthorpe
2022-10-06 13:05 ` Jason A. Donenfeld
2022-10-06 13:15 ` Jason Gunthorpe
2022-10-06 13:20 ` Andy Shevchenko
2022-10-12 19:16 ` Joe Perches
2022-10-12 21:29 ` David Laight
2022-10-13 1:37 ` Joe Perches
2022-10-05 21:48 ` [PATCH v1 4/5] treewide: use get_random_bytes " Jason A. Donenfeld
2022-10-06 4:45 ` Kees Cook
2022-10-06 4:48 ` Kees Cook
2022-10-05 21:48 ` [PATCH v1 5/5] prandom: remove unused functions Jason A. Donenfeld
2022-10-06 4:39 ` Kees Cook
2022-10-06 4:55 ` Kees Cook [this message]
2022-10-06 5:40 ` [PATCH v1 0/5] treewide cleanup of random integer usage Kees Cook
2022-10-06 12:53 ` Jason A. Donenfeld
2022-10-06 13:49 ` [f2fs-dev] " 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=202210052148.B11CBC60@keescook \
--to=keescook@chromium.org \
--cc=Jason@zx2c4.com \
--cc=andrew@lunn.ch \
--cc=andrii@kernel.org \
--cc=andy@greyhouse.net \
--cc=bharat@chelsio.com \
--cc=ceph-devel@vger.kernel.org \
--cc=chuck.lever@oracle.com \
--cc=claudiu.beznea@microchip.com \
--cc=dave.hansen@linux.intel.com \
--cc=dick.kennedy@broadcom.com \
--cc=djwong@kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=edumazet@google.com \
--cc=geomatsi@gmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=hante.meuleman@broadcom.com \
--cc=haris.iqbal@ionos.com \
--cc=hch@lst.de \
--cc=hverkuil@xs4all.nl \
--cc=imitsyanko@quantenna.com \
--cc=j.vosburgh@gmail.com \
--cc=jack@suse.com \
--cc=jolsa@kernel.org \
--cc=jpr@f6fbb.org \
--cc=jstultz@google.com \
--cc=kadlec@netfilter.org \
--cc=lars.ellenberg@linbit.com \
--cc=linux-doc@vger.kernel.org \
--cc=linux-fbdev@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-hams@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=linux-mmc@vger.kernel.org \
--cc=linux-nfs@vger.kernel.org \
--cc=linux-raid@vger.kernel.org \
--cc=linux-sctp@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=luto@kernel.org \
--cc=mhiramat@kernel.org \
--cc=michael.chan@broadcom.com \
--cc=miquel.raynal@bootlin.com \
--cc=netfilter-devel@vger.kernel.org \
--cc=nhorman@tuxdriver.com \
--cc=njavali@marvell.com \
--cc=npiggin@gmail.com \
--cc=rohitm@chelsio.com \
--cc=sdf@googl \
--cc=sharvari.harisangam@nxp.com \
--cc=song@kernel.org \
--cc=target-devel@vger.kernel.org \
--cc=tglx@linutronix.de \
--cc=trond.myklebust@hammerspace.com \
--cc=ulf.hansson@linaro.org \
--cc=varun@chelsio.com \
--cc=vinay.yadav@chelsio.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).