random: add get_random_max() function
RFC PATCH 45/50 random: add get_random_max() function
I got annoyed that randomize_page() wasn't exactly uniform, and used (slow) modulo-based range reduction rather than the (fast) multiplicative algorithm, so I fixed it.
I figure, if you're going to the trouble of using cryptographic random numbers, you don't want biased outputs, even if it's slight.
This includes optimized code for compile-time constant ranges, even though randomize_page() can't use it; later patches will add more callers.
The multiple implementation cases make the code bulky, but each call site uses only one (chosen at compile time) and they are individually quite small.
The SPARC64 implementation of _get_random_max64(), the inline function which handles constant ranges, is particularly large.
--- drivers/char/random.c | 155 ++++++++++++++++++++++++++++++++-- include/linux/random.h | 183 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 325 insertions(+), 13 deletions(-)
drivers/char/random.c +148 -7
@@ -2569,6 +2569,143 @@ u32 get_random_u32(void) }EXPORT_SYMBOL (get_random_u32); +/** +* __get_random_max32 - Generate a uniform random number 0 <= x < range < 2^32 +* @range: Modulus for random number; must not be zero! +* +* This generates an exactly uniform distribution over [0, range), where +* range is *not* a compile-time constant. +* +* This uses Lemire's multiplicative algorithm, from +* Fast Random Integer Generation in an Interval +* https://arxiv.org/abs/1805.10941 +* +* The standard mutiplicative range technique is (rand32() * range) >> 32. +* However, there are either floor((1<<32) / range) or ceil((1<<32) / range) +* random values that will result in each output value. More specifically, +* lim = (1 << 32) % range values will be generated one extra time. +* +* Lemire proves (Lemma 4.1) that those extra values can be identified +* by the lsbits of the product. If you discard and retry whenever the +* lsbits are less than lim, you get the floor option always. +*/ +u32 __get_random_max32 (u32 range) +{ +u64 prod =mul_u32_u32 (get_random_u32 (), range); + +/* +* Fast-path check: lim < range, so lsbits >= range-1 +* implies lsbits >= lim. +*/ +if (unlikely ((u32 )prod < range -1 )) { +/* Slow path; we need to divide to check exactly */ +u32 lim = -range % range;/* = (1<<32) % range */ + +while (unlikely ((u32 )prod < lim)) + prod =mul_u32_u32 (get_random_u32 (), range); + } +return prod >>32 ; +} +EXPORT_SYMBOL (__get_random_max32); + +#ifdef CONFIG_64BIT +/** +* __get_random_max64 - Generate a uniform random number 0 <= x < range < 2^64 +* @range: Modulus for random number; must not be zero! +* +* Like get_random_max32, but for larger ranges. There are two +* implementations, depending on CONFIG_ARCH_SUPPORTS_INT128. +*/ +u64 __get_random_max64 (u64 range) +{ +#if defined(CONFIG_ARCH_SUPPORTS_INT128 ) && defined(__SIZEOF_INT128__) +/* Same as get_random_max32(), with larger data types. */ +unsigned __int128 prod; + +if (range >>32 ==0 ) +return __get_random_max32 (range); + + prod = (unsigned __int128 )get_random_u64 () * range; + +if (likely ((u64 )prod < range -1 )) { +/* Slow path; we need to divide to check exactly */ +u64 lim = -range % range;/* = (1<<64) % range */ + +while ((u64 )prod < lim) + prod = (unsigned __int128 )get_random_u64 () * range; + } +return prod >>64 ; +#else +/* +* We don't have efficient 128-bit products (e.g. SPARCv9), so +* break it into two halves: generate the high bits in +* the desired range (rounding up), append some uniform low +* bits, and retry in the rare case that the rounding up +* produces a result exceeding the range. +*/ +u32 msbits; +u64 result; + +if (range >>32 ==0 ) +return __get_random_max32 (range); + +/* +* If the range has less than 64 significant bits, there is +* more than one way to split it into msbits and lsbits. +* A very large msbits causes retries within __get_random_max32. +* A very small msbits causes retries here, with +* probability about 1/(2*msbits). +* +* While we could use a fixed split of 32 lsbits, that results +* in an average of 2.75 calls to get_random_u32() when the range +* has 33 significant bits, which is 0.75 more than necessary. +* And unfortunately, we expect ranges comparable to +* physical memory size, which is often just over 32 bits. +* +* Generally, the best split is 17 significant msbits and the rest +* as lsbits. *But* this code path is primarily for SPARCv9, +* which lacks a count-leading-zeros operation, so using fls() +* at runtime probably isn't worth it. +* +* The cost (in extra random words) approximately doubles per +* bit distant from that optimum. The best single split handles +* 44+ bits with 32 lsbits (5 bits from the optimum of 44-17 = +* 27 lsbits), and 33..43 bits with 21 lsbits (5 bits from the +* optima of 16..26 lsbits). +* +* As mentioned above, using 32 lsbits rather than 16 lsbits +* results in an additional 0.75 random words per call. 5 bits +* from optimum rather than 16 bits is a factor of 2^11 = 2048 +* better, using an additional 0.75/2048 = 3.6e-4 random words +* per call. Spending one cycle for a more sophisticated split +* is only worth it if a word of CRNG output costs 2048/0.75 = +* 2730 cycles, which it doesn't. +*/ +if (range >>43 ==0 ) { + msbits = (range +0x1fffff ) >>21 ; +do { +u32 t =__get_random_max32 (msbits); + result = (u64 )t <<21 |get_random_u32 () >>11 ; + }while (unlikely (result >= range)); + }else { + msbits = (range +0xffffffff ) >>32 ; +/* If range >= 0xffffffff00000001, msbits will wrap to 0 */ +do { +u32 t; + +if (likely (msbits)) + t =__get_random_max32 (msbits); +else + t =get_random_u32 (); + result = (u64 )t <<32 |get_random_u32 (); + }while (unlikely (result >= range)); + } +return result; +#endif +} +EXPORT_SYMBOL (__get_random_max64); +#endif /* CONFIG_64BIT */ +/** * randomize_page - Generate a random, page aligned address * @start: The smallest acceptable address the caller will take.
@@ -2586,20 +2723,24 @@ EXPORT_SYMBOL(get_random_u32);unsigned long randomize_page (unsigned long start,unsigned long range) { -if (!PAGE_ALIGNED (start)) { - range -=PAGE_ALIGN (start) - start; - start =PAGE_ALIGN (start); - } +/* How much to round up to make start page-aligned */ +unsigned long align = -start & (PAGE_SIZE -1 ); -if (start >ULONG_MAX - range) - range =ULONG_MAX - start; +if (range < align) +return start; + + start += align; + range -= align; + +if (range > -start) + range = -start; range >>=PAGE_SHIFT ;if (range ==0 )return start; -return start + (get_random_long () % range <<PAGE_SHIFT ); +return start + (get_random_max (range) <<PAGE_SHIFT ); }/* Interface for in-kernel drivers of true hardware RNGs.
include/linux/random.h +177 -6
195 lines not on this page · raw message
Signature · 1 line
2.26.0