Skip to content

Commit 060bc1d

Browse files
tytsobwhacks
authored andcommitted
random: use the arch-specific rng in xfer_secondary_pool
commit e6d4947 upstream. If the CPU supports a hardware random number generator, use it in xfer_secondary_pool(), where it will significantly improve things and where we can afford it. Also, remove the use of the arch-specific rng in add_timer_randomness(), since the call is significantly slower than get_cycles(), and we're much better off using it in xfer_secondary_pool() anyway. Signed-off-by: "Theodore Ts'o" <tytso@mit.edu> Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
1 parent 322f326 commit 060bc1d

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

‎drivers/char/random.c‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@
254254
#include <linux/cryptohash.h>
255255
#include <linux/fips.h>
256256
#include <linux/ptrace.h>
257+
#include <linux/kmemcheck.h>
257258

258259
#ifdef CONFIG_GENERIC_HARDIRQS
259260
# include <linux/irq.h>
@@ -702,11 +703,7 @@ static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
702703
goto out;
703704

704705
sample.jiffies = jiffies;
705-
706-
/* Use arch random value, fall back to cycles */
707-
if (!arch_get_random_int(&sample.cycles))
708-
sample.cycles = get_cycles();
709-
706+
sample.cycles = get_cycles();
710707
sample.num = num;
711708
mix_pool_bytes(&input_pool, &sample, sizeof(sample), NULL);
712709

@@ -838,7 +835,11 @@ static ssize_t extract_entropy(struct entropy_store *r, void *buf,
838835
*/
839836
static void xfer_secondary_pool(struct entropy_store *r, size_t nbytes)
840837
{
841-
__u32 tmp[OUTPUT_POOL_WORDS];
838+
union {
839+
__u32 tmp[OUTPUT_POOL_WORDS];
840+
long hwrand[4];
841+
} u;
842+
int i;
842843

843844
if (r->pull && r->entropy_count < nbytes * 8 &&
844845
r->entropy_count < r->poolinfo->POOLBITS) {
@@ -849,17 +850,23 @@ static void xfer_secondary_pool(struct entropy_store *r, size_t nbytes)
849850
/* pull at least as many as BYTES as wakeup BITS */
850851
bytes = max_t(int, bytes, random_read_wakeup_thresh / 8);
851852
/* but never more than the buffer size */
852-
bytes = min_t(int, bytes, sizeof(tmp));
853+
bytes = min_t(int, bytes, sizeof(u.tmp));
853854

854855
DEBUG_ENT("going to reseed %s with %d bits "
855856
"(%d of %d requested)\n",
856857
r->name, bytes * 8, nbytes * 8, r->entropy_count);
857858

858-
bytes = extract_entropy(r->pull, tmp, bytes,
859+
bytes = extract_entropy(r->pull, u.tmp, bytes,
859860
random_read_wakeup_thresh / 8, rsvd);
860-
mix_pool_bytes(r, tmp, bytes, NULL);
861+
mix_pool_bytes(r, u.tmp, bytes, NULL);
861862
credit_entropy_bits(r, bytes*8);
862863
}
864+
kmemcheck_mark_initialized(&u.hwrand, sizeof(u.hwrand));
865+
for (i = 0; i < 4; i++)
866+
if (arch_get_random_long(&u.hwrand[i]))
867+
break;
868+
if (i)
869+
mix_pool_bytes(r, &u.hwrand, sizeof(u.hwrand), 0);
863870
}
864871

865872
/*

0 commit comments

Comments
 (0)