050-rng_git_backport.patch 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. --- a/drivers/char/random.c
  2. +++ b/drivers/char/random.c
  3. @@ -125,21 +125,26 @@
  4. * The current exported interfaces for gathering environmental noise
  5. * from the devices are:
  6. *
  7. + * void add_device_randomness(const void *buf, unsigned int size);
  8. * void add_input_randomness(unsigned int type, unsigned int code,
  9. * unsigned int value);
  10. - * void add_interrupt_randomness(int irq);
  11. + * void add_interrupt_randomness(int irq, int irq_flags);
  12. * void add_disk_randomness(struct gendisk *disk);
  13. *
  14. * add_input_randomness() uses the input layer interrupt timing, as well as
  15. * the event type information from the hardware.
  16. *
  17. - * add_interrupt_randomness() uses the inter-interrupt timing as random
  18. - * inputs to the entropy pool. Note that not all interrupts are good
  19. - * sources of randomness! For example, the timer interrupts is not a
  20. - * good choice, because the periodicity of the interrupts is too
  21. - * regular, and hence predictable to an attacker. Network Interface
  22. - * Controller interrupts are a better measure, since the timing of the
  23. - * NIC interrupts are more unpredictable.
  24. + * add_interrupt_randomness() uses the interrupt timing as random
  25. + * inputs to the entropy pool. Using the cycle counters and the irq source
  26. + * as inputs, it feeds the randomness roughly once a second.
  27. + *
  28. + * add_device_randomness() is for adding data to the random pool that
  29. + * is likely to differ between two devices (or possibly even per boot).
  30. + * This would be things like MAC addresses or serial numbers, or the
  31. + * read-out of the RTC. This does *not* add any actual entropy to the
  32. + * pool, but it initializes the pool to different values for devices
  33. + * that might otherwise be identical and have very little entropy
  34. + * available to them (particularly common in the embedded world).
  35. *
  36. * add_disk_randomness() uses what amounts to the seek time of block
  37. * layer request events, on a per-disk_devt basis, as input to the
  38. @@ -248,6 +253,7 @@
  39. #include <linux/percpu.h>
  40. #include <linux/cryptohash.h>
  41. #include <linux/fips.h>
  42. +#include <linux/ptrace.h>
  43. #ifdef CONFIG_GENERIC_HARDIRQS
  44. # include <linux/irq.h>
  45. @@ -256,8 +262,12 @@
  46. #include <asm/processor.h>
  47. #include <asm/uaccess.h>
  48. #include <asm/irq.h>
  49. +#include <asm/irq_regs.h>
  50. #include <asm/io.h>
  51. +#define CREATE_TRACE_POINTS
  52. +#include <trace/events/random.h>
  53. +
  54. /*
  55. * Configuration information
  56. */
  57. @@ -420,8 +430,10 @@ struct entropy_store {
  58. /* read-write data: */
  59. spinlock_t lock;
  60. unsigned add_ptr;
  61. + unsigned input_rotate;
  62. int entropy_count;
  63. - int input_rotate;
  64. + int entropy_total;
  65. + unsigned int initialized:1;
  66. __u8 last_data[EXTRACT_SIZE];
  67. };
  68. @@ -454,6 +466,10 @@ static struct entropy_store nonblocking_
  69. .pool = nonblocking_pool_data
  70. };
  71. +static __u32 const twist_table[8] = {
  72. + 0x00000000, 0x3b6e20c8, 0x76dc4190, 0x4db26158,
  73. + 0xedb88320, 0xd6d6a3e8, 0x9b64c2b0, 0xa00ae278 };
  74. +
  75. /*
  76. * This function adds bytes into the entropy "pool". It does not
  77. * update the entropy estimate. The caller should call
  78. @@ -464,29 +480,24 @@ static struct entropy_store nonblocking_
  79. * it's cheap to do so and helps slightly in the expected case where
  80. * the entropy is concentrated in the low-order bits.
  81. */
  82. -static void mix_pool_bytes_extract(struct entropy_store *r, const void *in,
  83. - int nbytes, __u8 out[64])
  84. +static void _mix_pool_bytes(struct entropy_store *r, const void *in,
  85. + int nbytes, __u8 out[64])
  86. {
  87. - static __u32 const twist_table[8] = {
  88. - 0x00000000, 0x3b6e20c8, 0x76dc4190, 0x4db26158,
  89. - 0xedb88320, 0xd6d6a3e8, 0x9b64c2b0, 0xa00ae278 };
  90. unsigned long i, j, tap1, tap2, tap3, tap4, tap5;
  91. int input_rotate;
  92. int wordmask = r->poolinfo->poolwords - 1;
  93. const char *bytes = in;
  94. __u32 w;
  95. - unsigned long flags;
  96. - /* Taps are constant, so we can load them without holding r->lock. */
  97. tap1 = r->poolinfo->tap1;
  98. tap2 = r->poolinfo->tap2;
  99. tap3 = r->poolinfo->tap3;
  100. tap4 = r->poolinfo->tap4;
  101. tap5 = r->poolinfo->tap5;
  102. - spin_lock_irqsave(&r->lock, flags);
  103. - input_rotate = r->input_rotate;
  104. - i = r->add_ptr;
  105. + smp_rmb();
  106. + input_rotate = ACCESS_ONCE(r->input_rotate);
  107. + i = ACCESS_ONCE(r->add_ptr);
  108. /* mix one byte at a time to simplify size handling and churn faster */
  109. while (nbytes--) {
  110. @@ -513,19 +524,61 @@ static void mix_pool_bytes_extract(struc
  111. input_rotate += i ? 7 : 14;
  112. }
  113. - r->input_rotate = input_rotate;
  114. - r->add_ptr = i;
  115. + ACCESS_ONCE(r->input_rotate) = input_rotate;
  116. + ACCESS_ONCE(r->add_ptr) = i;
  117. + smp_wmb();
  118. if (out)
  119. for (j = 0; j < 16; j++)
  120. ((__u32 *)out)[j] = r->pool[(i - j) & wordmask];
  121. +}
  122. +
  123. +static void __mix_pool_bytes(struct entropy_store *r, const void *in,
  124. + int nbytes, __u8 out[64])
  125. +{
  126. + trace_mix_pool_bytes_nolock(r->name, nbytes, _RET_IP_);
  127. + _mix_pool_bytes(r, in, nbytes, out);
  128. +}
  129. +static void mix_pool_bytes(struct entropy_store *r, const void *in,
  130. + int nbytes, __u8 out[64])
  131. +{
  132. + unsigned long flags;
  133. +
  134. + trace_mix_pool_bytes(r->name, nbytes, _RET_IP_);
  135. + spin_lock_irqsave(&r->lock, flags);
  136. + _mix_pool_bytes(r, in, nbytes, out);
  137. spin_unlock_irqrestore(&r->lock, flags);
  138. }
  139. -static void mix_pool_bytes(struct entropy_store *r, const void *in, int bytes)
  140. +struct fast_pool {
  141. + __u32 pool[4];
  142. + unsigned long last;
  143. + unsigned short count;
  144. + unsigned char rotate;
  145. + unsigned char last_timer_intr;
  146. +};
  147. +
  148. +/*
  149. + * This is a fast mixing routine used by the interrupt randomness
  150. + * collector. It's hardcoded for an 128 bit pool and assumes that any
  151. + * locks that might be needed are taken by the caller.
  152. + */
  153. +static void fast_mix(struct fast_pool *f, const void *in, int nbytes)
  154. {
  155. - mix_pool_bytes_extract(r, in, bytes, NULL);
  156. + const char *bytes = in;
  157. + __u32 w;
  158. + unsigned i = f->count;
  159. + unsigned input_rotate = f->rotate;
  160. +
  161. + while (nbytes--) {
  162. + w = rol32(*bytes++, input_rotate & 31) ^ f->pool[i & 3] ^
  163. + f->pool[(i + 1) & 3];
  164. + f->pool[i & 3] = (w >> 3) ^ twist_table[w & 7];
  165. + input_rotate += (i++ & 3) ? 7 : 14;
  166. + }
  167. + f->count = i;
  168. + f->rotate = input_rotate;
  169. }
  170. /*
  171. @@ -533,30 +586,38 @@ static void mix_pool_bytes(struct entrop
  172. */
  173. static void credit_entropy_bits(struct entropy_store *r, int nbits)
  174. {
  175. - unsigned long flags;
  176. - int entropy_count;
  177. + int entropy_count, orig;
  178. if (!nbits)
  179. return;
  180. - spin_lock_irqsave(&r->lock, flags);
  181. -
  182. DEBUG_ENT("added %d entropy credits to %s\n", nbits, r->name);
  183. - entropy_count = r->entropy_count;
  184. +retry:
  185. + entropy_count = orig = ACCESS_ONCE(r->entropy_count);
  186. entropy_count += nbits;
  187. +
  188. if (entropy_count < 0) {
  189. DEBUG_ENT("negative entropy/overflow\n");
  190. entropy_count = 0;
  191. } else if (entropy_count > r->poolinfo->POOLBITS)
  192. entropy_count = r->poolinfo->POOLBITS;
  193. - r->entropy_count = entropy_count;
  194. + if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig)
  195. + goto retry;
  196. +
  197. + if (!r->initialized && nbits > 0) {
  198. + r->entropy_total += nbits;
  199. + if (r->entropy_total > 128)
  200. + r->initialized = 1;
  201. + }
  202. +
  203. + trace_credit_entropy_bits(r->name, nbits, entropy_count,
  204. + r->entropy_total, _RET_IP_);
  205. /* should we wake readers? */
  206. if (r == &input_pool && entropy_count >= random_read_wakeup_thresh) {
  207. wake_up_interruptible(&random_read_wait);
  208. kill_fasync(&fasync, SIGIO, POLL_IN);
  209. }
  210. - spin_unlock_irqrestore(&r->lock, flags);
  211. }
  212. /*********************************************************************
  213. @@ -609,6 +670,25 @@ static void set_timer_rand_state(unsigne
  214. }
  215. #endif
  216. +/*
  217. + * Add device- or boot-specific data to the input and nonblocking
  218. + * pools to help initialize them to unique values.
  219. + *
  220. + * None of this adds any entropy, it is meant to avoid the
  221. + * problem of the nonblocking pool having similar initial state
  222. + * across largely identical devices.
  223. + */
  224. +void add_device_randomness(const void *buf, unsigned int size)
  225. +{
  226. + unsigned long time = get_cycles() ^ jiffies;
  227. +
  228. + mix_pool_bytes(&input_pool, buf, size, NULL);
  229. + mix_pool_bytes(&input_pool, &time, sizeof(time), NULL);
  230. + mix_pool_bytes(&nonblocking_pool, buf, size, NULL);
  231. + mix_pool_bytes(&nonblocking_pool, &time, sizeof(time), NULL);
  232. +}
  233. +EXPORT_SYMBOL(add_device_randomness);
  234. +
  235. static struct timer_rand_state input_timer_state;
  236. /*
  237. @@ -637,13 +717,9 @@ static void add_timer_randomness(struct
  238. goto out;
  239. sample.jiffies = jiffies;
  240. -
  241. - /* Use arch random value, fall back to cycles */
  242. - if (!arch_get_random_int(&sample.cycles))
  243. - sample.cycles = get_cycles();
  244. -
  245. + sample.cycles = get_cycles();
  246. sample.num = num;
  247. - mix_pool_bytes(&input_pool, &sample, sizeof(sample));
  248. + mix_pool_bytes(&input_pool, &sample, sizeof(sample), NULL);
  249. /*
  250. * Calculate number of bits of randomness we probably added.
  251. @@ -700,17 +776,48 @@ void add_input_randomness(unsigned int t
  252. }
  253. EXPORT_SYMBOL_GPL(add_input_randomness);
  254. -void add_interrupt_randomness(int irq)
  255. +static DEFINE_PER_CPU(struct fast_pool, irq_randomness);
  256. +
  257. +void add_interrupt_randomness(int irq, int irq_flags)
  258. {
  259. - struct timer_rand_state *state;
  260. + struct entropy_store *r;
  261. + struct fast_pool *fast_pool = &__get_cpu_var(irq_randomness);
  262. + struct pt_regs *regs = get_irq_regs();
  263. + unsigned long now = jiffies;
  264. + __u32 input[4], cycles = get_cycles();
  265. +
  266. + input[0] = cycles ^ jiffies;
  267. + input[1] = irq;
  268. + if (regs) {
  269. + __u64 ip = instruction_pointer(regs);
  270. + input[2] = ip;
  271. + input[3] = ip >> 32;
  272. + }
  273. - state = get_timer_rand_state(irq);
  274. + fast_mix(fast_pool, input, sizeof(input));
  275. - if (state == NULL)
  276. + if ((fast_pool->count & 1023) &&
  277. + !time_after(now, fast_pool->last + HZ))
  278. return;
  279. - DEBUG_ENT("irq event %d\n", irq);
  280. - add_timer_randomness(state, 0x100 + irq);
  281. + fast_pool->last = now;
  282. +
  283. + r = nonblocking_pool.initialized ? &input_pool : &nonblocking_pool;
  284. + __mix_pool_bytes(r, &fast_pool->pool, sizeof(fast_pool->pool), NULL);
  285. + /*
  286. + * If we don't have a valid cycle counter, and we see
  287. + * back-to-back timer interrupts, then skip giving credit for
  288. + * any entropy.
  289. + */
  290. + if (cycles == 0) {
  291. + if (irq_flags & __IRQF_TIMER) {
  292. + if (fast_pool->last_timer_intr)
  293. + return;
  294. + fast_pool->last_timer_intr = 1;
  295. + } else
  296. + fast_pool->last_timer_intr = 0;
  297. + }
  298. + credit_entropy_bits(r, 1);
  299. }
  300. #ifdef CONFIG_BLOCK
  301. @@ -742,7 +849,11 @@ static ssize_t extract_entropy(struct en
  302. */
  303. static void xfer_secondary_pool(struct entropy_store *r, size_t nbytes)
  304. {
  305. - __u32 tmp[OUTPUT_POOL_WORDS];
  306. + union {
  307. + __u32 tmp[OUTPUT_POOL_WORDS];
  308. + long hwrand[4];
  309. + } u;
  310. + int i;
  311. if (r->pull && r->entropy_count < nbytes * 8 &&
  312. r->entropy_count < r->poolinfo->POOLBITS) {
  313. @@ -753,17 +864,22 @@ static void xfer_secondary_pool(struct e
  314. /* pull at least as many as BYTES as wakeup BITS */
  315. bytes = max_t(int, bytes, random_read_wakeup_thresh / 8);
  316. /* but never more than the buffer size */
  317. - bytes = min_t(int, bytes, sizeof(tmp));
  318. + bytes = min_t(int, bytes, sizeof(u.tmp));
  319. DEBUG_ENT("going to reseed %s with %d bits "
  320. "(%d of %d requested)\n",
  321. r->name, bytes * 8, nbytes * 8, r->entropy_count);
  322. - bytes = extract_entropy(r->pull, tmp, bytes,
  323. + bytes = extract_entropy(r->pull, u.tmp, bytes,
  324. random_read_wakeup_thresh / 8, rsvd);
  325. - mix_pool_bytes(r, tmp, bytes);
  326. + mix_pool_bytes(r, u.tmp, bytes, NULL);
  327. credit_entropy_bits(r, bytes*8);
  328. }
  329. + for (i = 0; i < 4; i++)
  330. + if (arch_get_random_long(&u.hwrand[i]))
  331. + break;
  332. + if (i)
  333. + mix_pool_bytes(r, &u.hwrand, i * sizeof(u.hwrand[0]), 0);
  334. }
  335. /*
  336. @@ -822,9 +938,11 @@ static void extract_buf(struct entropy_s
  337. int i;
  338. __u32 hash[5], workspace[SHA_WORKSPACE_WORDS];
  339. __u8 extract[64];
  340. + unsigned long flags;
  341. /* Generate a hash across the pool, 16 words (512 bits) at a time */
  342. sha_init(hash);
  343. + spin_lock_irqsave(&r->lock, flags);
  344. for (i = 0; i < r->poolinfo->poolwords; i += 16)
  345. sha_transform(hash, (__u8 *)(r->pool + i), workspace);
  346. @@ -837,7 +955,8 @@ static void extract_buf(struct entropy_s
  347. * brute-forcing the feedback as hard as brute-forcing the
  348. * hash.
  349. */
  350. - mix_pool_bytes_extract(r, hash, sizeof(hash), extract);
  351. + __mix_pool_bytes(r, hash, sizeof(hash), extract);
  352. + spin_unlock_irqrestore(&r->lock, flags);
  353. /*
  354. * To avoid duplicates, we atomically extract a portion of the
  355. @@ -860,12 +979,12 @@ static void extract_buf(struct entropy_s
  356. }
  357. static ssize_t extract_entropy(struct entropy_store *r, void *buf,
  358. - size_t nbytes, int min, int reserved)
  359. + size_t nbytes, int min, int reserved)
  360. {
  361. ssize_t ret = 0, i;
  362. __u8 tmp[EXTRACT_SIZE];
  363. - unsigned long flags;
  364. + trace_extract_entropy(r->name, nbytes, r->entropy_count, _RET_IP_);
  365. xfer_secondary_pool(r, nbytes);
  366. nbytes = account(r, nbytes, min, reserved);
  367. @@ -873,6 +992,8 @@ static ssize_t extract_entropy(struct en
  368. extract_buf(r, tmp);
  369. if (fips_enabled) {
  370. + unsigned long flags;
  371. +
  372. spin_lock_irqsave(&r->lock, flags);
  373. if (!memcmp(tmp, r->last_data, EXTRACT_SIZE))
  374. panic("Hardware RNG duplicated output!\n");
  375. @@ -898,6 +1019,7 @@ static ssize_t extract_entropy_user(stru
  376. ssize_t ret = 0, i;
  377. __u8 tmp[EXTRACT_SIZE];
  378. + trace_extract_entropy_user(r->name, nbytes, r->entropy_count, _RET_IP_);
  379. xfer_secondary_pool(r, nbytes);
  380. nbytes = account(r, nbytes, 0, 0);
  381. @@ -931,17 +1053,35 @@ static ssize_t extract_entropy_user(stru
  382. /*
  383. * This function is the exported kernel interface. It returns some
  384. - * number of good random numbers, suitable for seeding TCP sequence
  385. - * numbers, etc.
  386. + * number of good random numbers, suitable for key generation, seeding
  387. + * TCP sequence numbers, etc. It does not use the hw random number
  388. + * generator, if available; use get_random_bytes_arch() for that.
  389. */
  390. void get_random_bytes(void *buf, int nbytes)
  391. {
  392. + extract_entropy(&nonblocking_pool, buf, nbytes, 0, 0);
  393. +}
  394. +EXPORT_SYMBOL(get_random_bytes);
  395. +
  396. +/*
  397. + * This function will use the architecture-specific hardware random
  398. + * number generator if it is available. The arch-specific hw RNG will
  399. + * almost certainly be faster than what we can do in software, but it
  400. + * is impossible to verify that it is implemented securely (as
  401. + * opposed, to, say, the AES encryption of a sequence number using a
  402. + * key known by the NSA). So it's useful if we need the speed, but
  403. + * only if we're willing to trust the hardware manufacturer not to
  404. + * have put in a back door.
  405. + */
  406. +void get_random_bytes_arch(void *buf, int nbytes)
  407. +{
  408. char *p = buf;
  409. + trace_get_random_bytes(nbytes, _RET_IP_);
  410. while (nbytes) {
  411. unsigned long v;
  412. int chunk = min(nbytes, (int)sizeof(unsigned long));
  413. -
  414. +
  415. if (!arch_get_random_long(&v))
  416. break;
  417. @@ -950,9 +1090,11 @@ void get_random_bytes(void *buf, int nby
  418. nbytes -= chunk;
  419. }
  420. - extract_entropy(&nonblocking_pool, p, nbytes, 0, 0);
  421. + if (nbytes)
  422. + extract_entropy(&nonblocking_pool, p, nbytes, 0, 0);
  423. }
  424. -EXPORT_SYMBOL(get_random_bytes);
  425. +EXPORT_SYMBOL(get_random_bytes_arch);
  426. +
  427. /*
  428. * init_std_data - initialize pool with system data
  429. @@ -966,21 +1108,18 @@ EXPORT_SYMBOL(get_random_bytes);
  430. static void init_std_data(struct entropy_store *r)
  431. {
  432. int i;
  433. - ktime_t now;
  434. - unsigned long flags;
  435. + ktime_t now = ktime_get_real();
  436. + unsigned long rv;
  437. - spin_lock_irqsave(&r->lock, flags);
  438. r->entropy_count = 0;
  439. - spin_unlock_irqrestore(&r->lock, flags);
  440. -
  441. - now = ktime_get_real();
  442. - mix_pool_bytes(r, &now, sizeof(now));
  443. - for (i = r->poolinfo->POOLBYTES; i > 0; i -= sizeof flags) {
  444. - if (!arch_get_random_long(&flags))
  445. + r->entropy_total = 0;
  446. + mix_pool_bytes(r, &now, sizeof(now), NULL);
  447. + for (i = r->poolinfo->POOLBYTES; i > 0; i -= sizeof(rv)) {
  448. + if (!arch_get_random_long(&rv))
  449. break;
  450. - mix_pool_bytes(r, &flags, sizeof(flags));
  451. + mix_pool_bytes(r, &rv, sizeof(rv), NULL);
  452. }
  453. - mix_pool_bytes(r, utsname(), sizeof(*(utsname())));
  454. + mix_pool_bytes(r, utsname(), sizeof(*(utsname())), NULL);
  455. }
  456. static int rand_initialize(void)
  457. @@ -1117,7 +1256,7 @@ write_pool(struct entropy_store *r, cons
  458. count -= bytes;
  459. p += bytes;
  460. - mix_pool_bytes(r, buf, bytes);
  461. + mix_pool_bytes(r, buf, bytes, NULL);
  462. cond_resched();
  463. }
  464. @@ -1274,6 +1413,7 @@ static int proc_do_uuid(ctl_table *table
  465. }
  466. static int sysctl_poolsize = INPUT_POOL_WORDS * 32;
  467. +extern ctl_table random_table[];
  468. ctl_table random_table[] = {
  469. {
  470. .procname = "poolsize",
  471. @@ -1339,7 +1479,7 @@ late_initcall(random_int_secret_init);
  472. * value is not cryptographically secure but for several uses the cost of
  473. * depleting entropy is too high
  474. */
  475. -DEFINE_PER_CPU(__u32 [MD5_DIGEST_WORDS], get_random_int_hash);
  476. +static DEFINE_PER_CPU(__u32 [MD5_DIGEST_WORDS], get_random_int_hash);
  477. unsigned int get_random_int(void)
  478. {
  479. __u32 *hash;
  480. --- a/drivers/mfd/ab3100-core.c
  481. +++ b/drivers/mfd/ab3100-core.c
  482. @@ -409,8 +409,6 @@ static irqreturn_t ab3100_irq_handler(in
  483. u32 fatevent;
  484. int err;
  485. - add_interrupt_randomness(irq);
  486. -
  487. err = ab3100_get_register_page_interruptible(ab3100, AB3100_EVENTA1,
  488. event_regs, 3);
  489. if (err)
  490. --- a/drivers/usb/core/hub.c
  491. +++ b/drivers/usb/core/hub.c
  492. @@ -24,6 +24,7 @@
  493. #include <linux/kthread.h>
  494. #include <linux/mutex.h>
  495. #include <linux/freezer.h>
  496. +#include <linux/random.h>
  497. #include <asm/uaccess.h>
  498. #include <asm/byteorder.h>
  499. @@ -1896,6 +1897,14 @@ int usb_new_device(struct usb_device *ud
  500. /* Tell the world! */
  501. announce_device(udev);
  502. + if (udev->serial)
  503. + add_device_randomness(udev->serial, strlen(udev->serial));
  504. + if (udev->product)
  505. + add_device_randomness(udev->product, strlen(udev->product));
  506. + if (udev->manufacturer)
  507. + add_device_randomness(udev->manufacturer,
  508. + strlen(udev->manufacturer));
  509. +
  510. device_enable_async_suspend(&udev->dev);
  511. /* Register the device. The device driver is responsible
  512. * for configuring the device and invoking the add-device
  513. --- a/include/linux/random.h
  514. +++ b/include/linux/random.h
  515. @@ -50,11 +50,13 @@ struct rnd_state {
  516. extern void rand_initialize_irq(int irq);
  517. +extern void add_device_randomness(const void *, unsigned int);
  518. extern void add_input_randomness(unsigned int type, unsigned int code,
  519. unsigned int value);
  520. -extern void add_interrupt_randomness(int irq);
  521. +extern void add_interrupt_randomness(int irq, int irq_flags);
  522. extern void get_random_bytes(void *buf, int nbytes);
  523. +extern void get_random_bytes_arch(void *buf, int nbytes);
  524. void generate_random_uuid(unsigned char uuid_out[16]);
  525. #ifndef MODULE
  526. --- /dev/null
  527. +++ b/include/trace/events/random.h
  528. @@ -0,0 +1,134 @@
  529. +#undef TRACE_SYSTEM
  530. +#define TRACE_SYSTEM random
  531. +
  532. +#if !defined(_TRACE_RANDOM_H) || defined(TRACE_HEADER_MULTI_READ)
  533. +#define _TRACE_RANDOM_H
  534. +
  535. +#include <linux/writeback.h>
  536. +#include <linux/tracepoint.h>
  537. +
  538. +DECLARE_EVENT_CLASS(random__mix_pool_bytes,
  539. + TP_PROTO(const char *pool_name, int bytes, unsigned long IP),
  540. +
  541. + TP_ARGS(pool_name, bytes, IP),
  542. +
  543. + TP_STRUCT__entry(
  544. + __field( const char *, pool_name )
  545. + __field( int, bytes )
  546. + __field(unsigned long, IP )
  547. + ),
  548. +
  549. + TP_fast_assign(
  550. + __entry->pool_name = pool_name;
  551. + __entry->bytes = bytes;
  552. + __entry->IP = IP;
  553. + ),
  554. +
  555. + TP_printk("%s pool: bytes %d caller %pF",
  556. + __entry->pool_name, __entry->bytes, (void *)__entry->IP)
  557. +);
  558. +
  559. +DEFINE_EVENT(random__mix_pool_bytes, mix_pool_bytes,
  560. + TP_PROTO(const char *pool_name, int bytes, unsigned long IP),
  561. +
  562. + TP_ARGS(pool_name, bytes, IP)
  563. +);
  564. +
  565. +DEFINE_EVENT(random__mix_pool_bytes, mix_pool_bytes_nolock,
  566. + TP_PROTO(const char *pool_name, int bytes, unsigned long IP),
  567. +
  568. + TP_ARGS(pool_name, bytes, IP)
  569. +);
  570. +
  571. +TRACE_EVENT(credit_entropy_bits,
  572. + TP_PROTO(const char *pool_name, int bits, int entropy_count,
  573. + int entropy_total, unsigned long IP),
  574. +
  575. + TP_ARGS(pool_name, bits, entropy_count, entropy_total, IP),
  576. +
  577. + TP_STRUCT__entry(
  578. + __field( const char *, pool_name )
  579. + __field( int, bits )
  580. + __field( int, entropy_count )
  581. + __field( int, entropy_total )
  582. + __field(unsigned long, IP )
  583. + ),
  584. +
  585. + TP_fast_assign(
  586. + __entry->pool_name = pool_name;
  587. + __entry->bits = bits;
  588. + __entry->entropy_count = entropy_count;
  589. + __entry->entropy_total = entropy_total;
  590. + __entry->IP = IP;
  591. + ),
  592. +
  593. + TP_printk("%s pool: bits %d entropy_count %d entropy_total %d "
  594. + "caller %pF", __entry->pool_name, __entry->bits,
  595. + __entry->entropy_count, __entry->entropy_total,
  596. + (void *)__entry->IP)
  597. +);
  598. +
  599. +TRACE_EVENT(get_random_bytes,
  600. + TP_PROTO(int nbytes, unsigned long IP),
  601. +
  602. + TP_ARGS(nbytes, IP),
  603. +
  604. + TP_STRUCT__entry(
  605. + __field( int, nbytes )
  606. + __field(unsigned long, IP )
  607. + ),
  608. +
  609. + TP_fast_assign(
  610. + __entry->nbytes = nbytes;
  611. + __entry->IP = IP;
  612. + ),
  613. +
  614. + TP_printk("nbytes %d caller %pF", __entry->nbytes, (void *)__entry->IP)
  615. +);
  616. +
  617. +DECLARE_EVENT_CLASS(random__extract_entropy,
  618. + TP_PROTO(const char *pool_name, int nbytes, int entropy_count,
  619. + unsigned long IP),
  620. +
  621. + TP_ARGS(pool_name, nbytes, entropy_count, IP),
  622. +
  623. + TP_STRUCT__entry(
  624. + __field( const char *, pool_name )
  625. + __field( int, nbytes )
  626. + __field( int, entropy_count )
  627. + __field(unsigned long, IP )
  628. + ),
  629. +
  630. + TP_fast_assign(
  631. + __entry->pool_name = pool_name;
  632. + __entry->nbytes = nbytes;
  633. + __entry->entropy_count = entropy_count;
  634. + __entry->IP = IP;
  635. + ),
  636. +
  637. + TP_printk("%s pool: nbytes %d entropy_count %d caller %pF",
  638. + __entry->pool_name, __entry->nbytes, __entry->entropy_count,
  639. + (void *)__entry->IP)
  640. +);
  641. +
  642. +
  643. +DEFINE_EVENT(random__extract_entropy, extract_entropy,
  644. + TP_PROTO(const char *pool_name, int nbytes, int entropy_count,
  645. + unsigned long IP),
  646. +
  647. + TP_ARGS(pool_name, nbytes, entropy_count, IP)
  648. +);
  649. +
  650. +DEFINE_EVENT(random__extract_entropy, extract_entropy_user,
  651. + TP_PROTO(const char *pool_name, int nbytes, int entropy_count,
  652. + unsigned long IP),
  653. +
  654. + TP_ARGS(pool_name, nbytes, entropy_count, IP)
  655. +);
  656. +
  657. +
  658. +
  659. +#endif /* _TRACE_RANDOM_H */
  660. +
  661. +/* This part must be outside protection */
  662. +#include <trace/define_trace.h>
  663. --- a/kernel/irq/handle.c
  664. +++ b/kernel/irq/handle.c
  665. @@ -117,7 +117,7 @@ irqreturn_t
  666. handle_irq_event_percpu(struct irq_desc *desc, struct irqaction *action)
  667. {
  668. irqreturn_t retval = IRQ_NONE;
  669. - unsigned int random = 0, irq = desc->irq_data.irq;
  670. + unsigned int flags = 0, irq = desc->irq_data.irq;
  671. do {
  672. irqreturn_t res;
  673. @@ -145,7 +145,7 @@ handle_irq_event_percpu(struct irq_desc
  674. /* Fall through to add to randomness */
  675. case IRQ_HANDLED:
  676. - random |= action->flags;
  677. + flags |= action->flags;
  678. break;
  679. default:
  680. @@ -156,8 +156,7 @@ handle_irq_event_percpu(struct irq_desc
  681. action = action->next;
  682. } while (action);
  683. - if (random & IRQF_SAMPLE_RANDOM)
  684. - add_interrupt_randomness(irq);
  685. + add_interrupt_randomness(irq, flags);
  686. if (!noirqdebug)
  687. note_interrupt(irq, desc, retval);
  688. --- a/net/core/dev.c
  689. +++ b/net/core/dev.c
  690. @@ -1176,6 +1176,7 @@ static int __dev_open(struct net_device
  691. net_dmaengine_get();
  692. dev_set_rx_mode(dev);
  693. dev_activate(dev);
  694. + add_device_randomness(dev->dev_addr, dev->addr_len);
  695. }
  696. return ret;
  697. @@ -4823,6 +4824,7 @@ int dev_set_mac_address(struct net_devic
  698. err = ops->ndo_set_mac_address(dev, sa);
  699. if (!err)
  700. call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
  701. + add_device_randomness(dev->dev_addr, dev->addr_len);
  702. return err;
  703. }
  704. EXPORT_SYMBOL(dev_set_mac_address);
  705. @@ -5602,6 +5604,7 @@ int register_netdevice(struct net_device
  706. dev_init_scheduler(dev);
  707. dev_hold(dev);
  708. list_netdevice(dev);
  709. + add_device_randomness(dev->dev_addr, dev->addr_len);
  710. /* Notify protocols, that a new device appeared. */
  711. ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
  712. --- a/net/core/rtnetlink.c
  713. +++ b/net/core/rtnetlink.c
  714. @@ -1371,6 +1371,7 @@ static int do_setlink(struct net_device
  715. goto errout;
  716. send_addr_notify = 1;
  717. modified = 1;
  718. + add_device_randomness(dev->dev_addr, dev->addr_len);
  719. }
  720. if (tb[IFLA_MTU]) {