[PATCH v1 1/5] treewide: use prandom_u32_max() when possible

Jason A. Donenfeld Jason at zx2c4.com
Thu Oct 6 05:45:25 PDT 2022


Hi Kees,

On Wed, Oct 05, 2022 at 09:16:50PM -0700, Kees Cook wrote:
> On Wed, Oct 05, 2022 at 11:48:40PM +0200, Jason A. Donenfeld wrote:
> > Rather than incurring a division or requesting too many random bytes for
> > the given range, use the prandom_u32_max() function, which only takes
> > the minimum required bytes from the RNG and avoids divisions.
> 
> Yes please!
> 
> Since this is a treewide patch, it's helpful for (me at least) doing
> reviews to detail the mechanism of the transformation.

This is hand done. There were also various wrong seds done. And then I'd
edit the .diff manually, and then reapply it, as an iterative process.
No internet on the airplane, and oddly no spatch already on my laptop (I
think I had some Gentoo ocaml issues at some point and removed it?).

> e.g. I imagine this could be done with something like Coccinelle and

Feel free to check the work here by using Coccinelle if you're into
that.

> >  static inline int ubi_dbg_is_bitflip(const struct ubi_device *ubi)
> >  {
> >  	if (ubi->dbg.emulate_bitflips)
> > -		return !(prandom_u32() % 200);
> > +		return !(prandom_u32_max(200));
> >  	return 0;
> >  }
> >  
> 
> Because some looks automated (why the parens?)

I saw this before going out and thought I'd fixed it but I guess I sent
the wrong one.

Jason



More information about the Linux-nvme mailing list