Thread (23 messages) flat view 23 messages, 7 authors, 2022-10-09

RE: [PATCH v3 3/5] treewide: use get_random_u32() when possible

From: David Laight <hidden>
Date: 2022-10-08 21:54:10
Also in: dri-devel, kernel-janitors, linux-block, linux-crypto, linux-doc, linux-fsdevel, linux-media, linux-mips, linux-mm, linux-mmc, linux-nvme, linux-patches, linux-rdma, linux-s390, linux-um, linux-usb, linux-wireless, lkml, loongarch, netdev, sparclinux

From: Jason A. Donenfeld
Sent: 07 October 2022 18:56
...
quoted
Given these kinds of less mechanical changes, it may make sense to split
these from the "trivial" conversions in a treewide patch. The chance of
needing a revert from the simple 1:1 conversions is much lower than the
need to revert by-hand changes.

The Cocci script I suggested in my v1 review gets 80% of the first
patch, for example.
I'll split things up into a mechanical step and a non-mechanical step.
Good idea.
I'd also do something about the 'get_random_int() & 3' cases.
(ie remainder by 2^n-1)
These can be converted to 'get_random_u8() & 3' (etc).
So they only need one random byte (not 4) and no multiply.

Possibly something based on (the quickly typed, and not C):
#define get_random_below(val) [
	if (builtin_constant(val))
		BUILD_BUG_ON(!val || val > 0x100000000ull)
		if (!(val & (val - 1)) {
			if (val <= 0x100)
				return get_random_u8() & (val - 1);
			if (val <= 0x10000)
				return get_random_u16() & (val - 1);
			return get_random_u32() & (val - 1);
		}
	}
	BUILD_BUG_ON(sizeof (val) > 4);
	return ((u64)get_random_u32() * val) >> 32;
}

get_random_below() is a much better name than prandom_u32_max().

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help