Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Daniel Borkmann and committed by
David S. Miller
f337db64 6cd28f04

+20 -17
+1 -7
drivers/net/team/team_mode_random.c
··· 13 13 #include <linux/module.h> 14 14 #include <linux/init.h> 15 15 #include <linux/skbuff.h> 16 - #include <linux/reciprocal_div.h> 17 16 #include <linux/if_team.h> 18 - 19 - static u32 random_N(unsigned int N) 20 - { 21 - return reciprocal_divide(prandom_u32(), N); 22 - } 23 17 24 18 static bool rnd_transmit(struct team *team, struct sk_buff *skb) 25 19 { 26 20 struct team_port *port; 27 21 int port_index; 28 22 29 - port_index = random_N(team->en_port_count); 23 + port_index = prandom_u32_max(team->en_port_count); 30 24 port = team_get_port_by_index_rcu(team, port_index); 31 25 if (unlikely(!port)) 32 26 goto drop;
+17 -1
include/linux/random.h
··· 8 8 9 9 #include <uapi/linux/random.h> 10 10 11 - 12 11 extern void add_device_randomness(const void *, unsigned int); 13 12 extern void add_input_randomness(unsigned int type, unsigned int code, 14 13 unsigned int value); ··· 36 37 37 38 u32 prandom_u32_state(struct rnd_state *state); 38 39 void prandom_bytes_state(struct rnd_state *state, void *buf, int nbytes); 40 + 41 + /** 42 + * prandom_u32_max - returns a pseudo-random number in interval [0, ep_ro) 43 + * @ep_ro: right open interval endpoint 44 + * 45 + * Returns a pseudo-random number that is in interval [0, ep_ro). Note 46 + * that the result depends on PRNG being well distributed in [0, ~0U] 47 + * u32 space. Here we use maximally equidistributed combined Tausworthe 48 + * generator, that is, prandom_u32(). This is useful when requesting a 49 + * random index of an array containing ep_ro elements, for example. 50 + * 51 + * Returns: pseudo-random number in interval [0, ep_ro) 52 + */ 53 + static inline u32 prandom_u32_max(u32 ep_ro) 54 + { 55 + return (u32)(((u64) prandom_u32() * ep_ro) >> 32); 56 + } 39 57 40 58 /* 41 59 * Handle minimum values for seeds
+1 -1
net/packet/af_packet.c
··· 1289 1289 struct sk_buff *skb, 1290 1290 unsigned int num) 1291 1291 { 1292 - return reciprocal_divide(prandom_u32(), num); 1292 + return prandom_u32_max(num); 1293 1293 } 1294 1294 1295 1295 static unsigned int fanout_demux_rollover(struct packet_fanout *f,
+1 -8
net/sched/sch_choke.c
··· 14 14 #include <linux/types.h> 15 15 #include <linux/kernel.h> 16 16 #include <linux/skbuff.h> 17 - #include <linux/reciprocal_div.h> 18 17 #include <linux/vmalloc.h> 19 18 #include <net/pkt_sched.h> 20 19 #include <net/inet_ecn.h> ··· 75 76 76 77 struct sk_buff **tab; 77 78 }; 78 - 79 - /* deliver a random number between 0 and N - 1 */ 80 - static u32 random_N(unsigned int N) 81 - { 82 - return reciprocal_divide(prandom_u32(), N); 83 - } 84 79 85 80 /* number of elements in queue including holes */ 86 81 static unsigned int choke_len(const struct choke_sched_data *q) ··· 226 233 int retrys = 3; 227 234 228 235 do { 229 - *pidx = (q->head + random_N(choke_len(q))) & q->tab_mask; 236 + *pidx = (q->head + prandom_u32_max(choke_len(q))) & q->tab_mask; 230 237 skb = q->tab[*pidx]; 231 238 if (skb) 232 239 return skb;