aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--drivers/net/team/team_mode_random.c8
-rw-r--r--include/linux/random.h18
-rw-r--r--net/packet/af_packet.c2
-rw-r--r--net/sched/sch_choke.c9
4 files changed, 20 insertions, 17 deletions
diff --git a/drivers/net/team/team_mode_random.c b/drivers/net/team/team_mode_random.c
index 7f032e211343..cd2f692b8074 100644
--- a/drivers/net/team/team_mode_random.c
+++ b/drivers/net/team/team_mode_random.c
@@ -13,20 +13,14 @@
13#include <linux/module.h> 13#include <linux/module.h>
14#include <linux/init.h> 14#include <linux/init.h>
15#include <linux/skbuff.h> 15#include <linux/skbuff.h>
16#include <linux/reciprocal_div.h>
17#include <linux/if_team.h> 16#include <linux/if_team.h>
18 17
19static u32 random_N(unsigned int N)
20{
21 return reciprocal_divide(prandom_u32(), N);
22}
23
24static bool rnd_transmit(struct team *team, struct sk_buff *skb) 18static bool rnd_transmit(struct team *team, struct sk_buff *skb)
25{ 19{
26 struct team_port *port; 20 struct team_port *port;
27 int port_index; 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 port = team_get_port_by_index_rcu(team, port_index); 24 port = team_get_port_by_index_rcu(team, port_index);
31 if (unlikely(!port)) 25 if (unlikely(!port))
32 goto drop; 26 goto drop;
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
12extern void add_device_randomness(const void *, unsigned int); 11extern void add_device_randomness(const void *, unsigned int);
13extern void add_input_randomness(unsigned int type, unsigned int code, 12extern 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 {
38u32 prandom_u32_state(struct rnd_state *state); 37u32 prandom_u32_state(struct rnd_state *state);
39void prandom_bytes_state(struct rnd_state *state, void *buf, int nbytes); 38void 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 */
52static 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 */
diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
index 59fb3db2e713..df3cbdd585c7 100644
--- a/net/packet/af_packet.c
+++ b/net/packet/af_packet.c
@@ -1289,7 +1289,7 @@ static unsigned int fanout_demux_rnd(struct packet_fanout *f,
1289 struct sk_buff *skb, 1289 struct sk_buff *skb,
1290 unsigned int num) 1290 unsigned int num)
1291{ 1291{
1292 return reciprocal_divide(prandom_u32(), num); 1292 return prandom_u32_max(num);
1293} 1293}
1294 1294
1295static unsigned int fanout_demux_rollover(struct packet_fanout *f, 1295static unsigned int fanout_demux_rollover(struct packet_fanout *f,
diff --git a/net/sched/sch_choke.c b/net/sched/sch_choke.c
index ddd73cb2d7ba..2aee02802c27 100644
--- a/net/sched/sch_choke.c
+++ b/net/sched/sch_choke.c
@@ -14,7 +14,6 @@
14#include <linux/types.h> 14#include <linux/types.h>
15#include <linux/kernel.h> 15#include <linux/kernel.h>
16#include <linux/skbuff.h> 16#include <linux/skbuff.h>
17#include <linux/reciprocal_div.h>
18#include <linux/vmalloc.h> 17#include <linux/vmalloc.h>
19#include <net/pkt_sched.h> 18#include <net/pkt_sched.h>
20#include <net/inet_ecn.h> 19#include <net/inet_ecn.h>
@@ -77,12 +76,6 @@ struct choke_sched_data {
77 struct sk_buff **tab; 76 struct sk_buff **tab;
78}; 77};
79 78
80/* deliver a random number between 0 and N - 1 */
81static u32 random_N(unsigned int N)
82{
83 return reciprocal_divide(prandom_u32(), N);
84}
85
86/* number of elements in queue including holes */ 79/* number of elements in queue including holes */
87static unsigned int choke_len(const struct choke_sched_data *q) 80static unsigned int choke_len(const struct choke_sched_data *q)
88{ 81{
@@ -233,7 +226,7 @@ static struct sk_buff *choke_peek_random(const struct choke_sched_data *q,
233 int retrys = 3; 226 int retrys = 3;
234 227
235 do { 228 do {
236 *pidx = (q->head + random_N(choke_len(q))) & q->tab_mask; 229 *pidx = (q->head + prandom_u32_max(choke_len(q))) & q->tab_mask;
237 skb = q->tab[*pidx]; 230 skb = q->tab[*pidx];
238 if (skb) 231 if (skb)
239 return skb; 232 return skb;