lib/genalloc.c: start search from start of chunk
[GitHub/LineageOS/android_kernel_samsung_universal7580.git] / lib / random32.c
1 /*
2 This is a maximally equidistributed combined Tausworthe generator
3 based on code from GNU Scientific Library 1.5 (30 Jun 2004)
4
5 x_n = (s1_n ^ s2_n ^ s3_n)
6
7 s1_{n+1} = (((s1_n & 4294967294) <<12) ^ (((s1_n <<13) ^ s1_n) >>19))
8 s2_{n+1} = (((s2_n & 4294967288) << 4) ^ (((s2_n << 2) ^ s2_n) >>25))
9 s3_{n+1} = (((s3_n & 4294967280) <<17) ^ (((s3_n << 3) ^ s3_n) >>11))
10
11 The period of this generator is about 2^88.
12
13 From: P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe
14 Generators", Mathematics of Computation, 65, 213 (1996), 203--213.
15
16 This is available on the net from L'Ecuyer's home page,
17
18 http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
19 ftp://ftp.iro.umontreal.ca/pub/simulation/lecuyer/papers/tausme.ps
20
21 There is an erratum in the paper "Tables of Maximally
22 Equidistributed Combined LFSR Generators", Mathematics of
23 Computation, 68, 225 (1999), 261--269:
24 http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
25
26 ... the k_j most significant bits of z_j must be non-
27 zero, for each j. (Note: this restriction also applies to the
28 computer code given in [4], but was mistakenly not mentioned in
29 that paper.)
30
31 This affects the seeding procedure by imposing the requirement
32 s1 > 1, s2 > 7, s3 > 15.
33
34 */
35
36 #include <linux/types.h>
37 #include <linux/percpu.h>
38 #include <linux/export.h>
39 #include <linux/jiffies.h>
40 #include <linux/random.h>
41 #include <linux/timer.h>
42
43 static DEFINE_PER_CPU(struct rnd_state, net_rand_state);
44
45 /**
46 * prandom_u32_state - seeded pseudo-random number generator.
47 * @state: pointer to state structure holding seeded state.
48 *
49 * This is used for pseudo-randomness with no outside seeding.
50 * For more random results, use prandom_u32().
51 */
52 u32 prandom_u32_state(struct rnd_state *state)
53 {
54 #define TAUSWORTHE(s,a,b,c,d) ((s&c)<<d) ^ (((s <<a) ^ s)>>b)
55
56 state->s1 = TAUSWORTHE(state->s1, 13, 19, 4294967294UL, 12);
57 state->s2 = TAUSWORTHE(state->s2, 2, 25, 4294967288UL, 4);
58 state->s3 = TAUSWORTHE(state->s3, 3, 11, 4294967280UL, 17);
59
60 return (state->s1 ^ state->s2 ^ state->s3);
61 }
62 EXPORT_SYMBOL(prandom_u32_state);
63
64 /**
65 * prandom_u32 - pseudo random number generator
66 *
67 * A 32 bit pseudo-random number is generated using a fast
68 * algorithm suitable for simulation. This algorithm is NOT
69 * considered safe for cryptographic use.
70 */
71 u32 prandom_u32(void)
72 {
73 unsigned long r;
74 struct rnd_state *state = &get_cpu_var(net_rand_state);
75 r = prandom_u32_state(state);
76 put_cpu_var(state);
77 return r;
78 }
79 EXPORT_SYMBOL(prandom_u32);
80
81 /*
82 * prandom_bytes_state - get the requested number of pseudo-random bytes
83 *
84 * @state: pointer to state structure holding seeded state.
85 * @buf: where to copy the pseudo-random bytes to
86 * @bytes: the requested number of bytes
87 *
88 * This is used for pseudo-randomness with no outside seeding.
89 * For more random results, use prandom_bytes().
90 */
91 void prandom_bytes_state(struct rnd_state *state, void *buf, int bytes)
92 {
93 unsigned char *p = buf;
94 int i;
95
96 for (i = 0; i < round_down(bytes, sizeof(u32)); i += sizeof(u32)) {
97 u32 random = prandom_u32_state(state);
98 int j;
99
100 for (j = 0; j < sizeof(u32); j++) {
101 p[i + j] = random;
102 random >>= BITS_PER_BYTE;
103 }
104 }
105 if (i < bytes) {
106 u32 random = prandom_u32_state(state);
107
108 for (; i < bytes; i++) {
109 p[i] = random;
110 random >>= BITS_PER_BYTE;
111 }
112 }
113 }
114 EXPORT_SYMBOL(prandom_bytes_state);
115
116 /**
117 * prandom_bytes - get the requested number of pseudo-random bytes
118 * @buf: where to copy the pseudo-random bytes to
119 * @bytes: the requested number of bytes
120 */
121 void prandom_bytes(void *buf, int bytes)
122 {
123 struct rnd_state *state = &get_cpu_var(net_rand_state);
124
125 prandom_bytes_state(state, buf, bytes);
126 put_cpu_var(state);
127 }
128 EXPORT_SYMBOL(prandom_bytes);
129
130 /**
131 * prandom_seed - add entropy to pseudo random number generator
132 * @seed: seed value
133 *
134 * Add some additional seeding to the prandom pool.
135 */
136 void prandom_seed(u32 entropy)
137 {
138 int i;
139 /*
140 * No locking on the CPUs, but then somewhat random results are, well,
141 * expected.
142 */
143 for_each_possible_cpu (i) {
144 struct rnd_state *state = &per_cpu(net_rand_state, i);
145 state->s1 = __seed(state->s1 ^ entropy, 2);
146 prandom_u32_state(state);
147 }
148 }
149 EXPORT_SYMBOL(prandom_seed);
150
151 /*
152 * Generate some initially weak seeding values to allow
153 * to start the prandom_u32() engine.
154 */
155 static int __init prandom_init(void)
156 {
157 int i;
158
159 for_each_possible_cpu(i) {
160 struct rnd_state *state = &per_cpu(net_rand_state,i);
161
162 #define LCG(x) ((x) * 69069) /* super-duper LCG */
163 state->s1 = __seed(LCG(i + jiffies), 2);
164 state->s2 = __seed(LCG(state->s1), 8);
165 state->s3 = __seed(LCG(state->s2), 16);
166
167 /* "warm it up" */
168 prandom_u32_state(state);
169 prandom_u32_state(state);
170 prandom_u32_state(state);
171 prandom_u32_state(state);
172 prandom_u32_state(state);
173 prandom_u32_state(state);
174 }
175 return 0;
176 }
177 core_initcall(prandom_init);
178
179 static void __prandom_timer(unsigned long dontcare);
180 static DEFINE_TIMER(seed_timer, __prandom_timer, 0, 0);
181 static void __prandom_timer(unsigned long dontcare)
182 {
183 u32 entropy;
184 get_random_bytes(&entropy, sizeof(entropy));
185 prandom_seed(entropy);
186 /* reseed every ~60 seconds, in [40 .. 80) interval with slack */
187 seed_timer.expires = jiffies + (40 * HZ + (prandom_u32() % (40 * HZ)));
188 add_timer(&seed_timer);
189 }
190 static void prandom_start_seed_timer(void)
191 {
192 set_timer_slack(&seed_timer, HZ);
193 seed_timer.expires = jiffies + 40 * HZ;
194 add_timer(&seed_timer);
195 }
196
197 /*
198 * Generate better values after random number generator
199 * is fully initialized.
200 */
201 static void __prandom_reseed(bool late)
202 {
203 int i;
204 unsigned long flags;
205 static bool latch = false;
206 static DEFINE_SPINLOCK(lock);
207 /* only allow initial seeding (late == false) once */
208 spin_lock_irqsave(&lock, flags);
209 if (latch && !late)
210 goto out;
211 latch = true;
212
213 for_each_possible_cpu(i) {
214 struct rnd_state *state = &per_cpu(net_rand_state,i);
215 u32 seeds[3];
216
217 get_random_bytes(&seeds, sizeof(seeds));
218 state->s1 = __seed(seeds[0], 2);
219 state->s2 = __seed(seeds[1], 8);
220 state->s3 = __seed(seeds[2], 16);
221
222 /* mix it in */
223 prandom_u32_state(state);
224 }
225 out:
226 spin_unlock_irqrestore(&lock, flags);
227 }
228
229 void prandom_reseed_late(void)
230 {
231 __prandom_reseed(true);
232 }
233
234 static int __init prandom_reseed(void)
235 {
236 __prandom_reseed(false);
237 prandom_start_seed_timer();
238 return 0;
239 }
240 late_initcall(prandom_reseed);