diff options
author | Daniel Borkmann <dborkman@redhat.com> | 2014-01-21 20:29:39 -0500 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2014-01-22 02:17:20 -0500 |
commit | f337db64af059c9a94278a8b0ab97d87259ff62f (patch) | |
tree | ef7db2258d5929760f2a22b74779d374916634bd /include/linux/random.h | |
parent | 6cd28f044b47aeeba91807d97d6f3ea5a048e88d (diff) |
random32: add prandom_u32_max and convert open coded users
Many functions have open coded a function that returns a random
number in range [0,N-1]. Under the assumption that we have a PRNG
such as taus113 with being well distributed in [0, ~0U] space,
we can implement such a function as uword t = (n*m')>>32, where
m' is a random number obtained from PRNG, n the right open interval
border and t our resulting random number, with n,m',t in u32 universe.
Lets go with Joe and simply call it prandom_u32_max(), although
technically we have an right open interval endpoint, but that we
have documented. Other users can further be migrated to the new
prandom_u32_max() function later on; for now, we need to make sure
to migrate reciprocal_divide() users for the reciprocal_divide()
follow-up fixup since their function signatures are going to change.
Joint work with Hannes Frederic Sowa.
Cc: Jakub Zawadzki <darkjames-ws@darkjames.pl>
Cc: Eric Dumazet <eric.dumazet@gmail.com>
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Hannes Frederic Sowa <hannes@stressinduktion.org>
Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'include/linux/random.h')
-rw-r--r-- | include/linux/random.h | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/include/linux/random.h b/include/linux/random.h index 4002b3df4c85..1cfce0e24dbd 100644 --- a/include/linux/random.h +++ b/include/linux/random.h | |||
@@ -8,7 +8,6 @@ | |||
8 | 8 | ||
9 | #include <uapi/linux/random.h> | 9 | #include <uapi/linux/random.h> |
10 | 10 | ||
11 | |||
12 | extern void add_device_randomness(const void *, unsigned int); | 11 | extern void add_device_randomness(const void *, unsigned int); |
13 | extern void add_input_randomness(unsigned int type, unsigned int code, | 12 | extern void add_input_randomness(unsigned int type, unsigned int code, |
14 | unsigned int value); | 13 | unsigned int value); |
@@ -38,6 +37,23 @@ struct rnd_state { | |||
38 | u32 prandom_u32_state(struct rnd_state *state); | 37 | u32 prandom_u32_state(struct rnd_state *state); |
39 | void prandom_bytes_state(struct rnd_state *state, void *buf, int nbytes); | 38 | void prandom_bytes_state(struct rnd_state *state, void *buf, int nbytes); |
40 | 39 | ||
40 | /** | ||
41 | * prandom_u32_max - returns a pseudo-random number in interval [0, ep_ro) | ||
42 | * @ep_ro: right open interval endpoint | ||
43 | * | ||
44 | * Returns a pseudo-random number that is in interval [0, ep_ro). Note | ||
45 | * that the result depends on PRNG being well distributed in [0, ~0U] | ||
46 | * u32 space. Here we use maximally equidistributed combined Tausworthe | ||
47 | * generator, that is, prandom_u32(). This is useful when requesting a | ||
48 | * random index of an array containing ep_ro elements, for example. | ||
49 | * | ||
50 | * Returns: pseudo-random number in interval [0, ep_ro) | ||
51 | */ | ||
52 | static inline u32 prandom_u32_max(u32 ep_ro) | ||
53 | { | ||
54 | return (u32)(((u64) prandom_u32() * ep_ro) >> 32); | ||
55 | } | ||
56 | |||
41 | /* | 57 | /* |
42 | * Handle minimum values for seeds | 58 | * Handle minimum values for seeds |
43 | */ | 59 | */ |