Skip to content

Commit 91ec0fe

Browse files
committed
random: cleanup poolinfo abstraction
Now that we're only using one polynomial, we can cleanup its representation into constants, instead of passing around pointers dynamically to select different polynomials. This improves the codegen and makes the code a bit more straightforward. Reviewed-by: Dominik Brodowski <[email protected]> Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent c0a8a61 commit 91ec0fe

File tree

1 file changed

+30
-37
lines changed

1 file changed

+30
-37
lines changed

drivers/char/random.c

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -430,14 +430,20 @@ static int random_write_wakeup_bits = 28 * OUTPUT_POOL_WORDS;
430430
* polynomial which improves the resulting TGFSR polynomial to be
431431
* irreducible, which we have made here.
432432
*/
433-
static const struct poolinfo {
434-
int poolbitshift, poolwords, poolbytes, poolfracbits;
435-
#define S(x) ilog2(x)+5, (x), (x)*4, (x) << (ENTROPY_SHIFT+5)
436-
int tap1, tap2, tap3, tap4, tap5;
437-
} poolinfo_table[] = {
438-
/* was: x^128 + x^103 + x^76 + x^51 +x^25 + x + 1 */
433+
enum poolinfo {
434+
POOL_WORDS = 128,
435+
POOL_WORDMASK = POOL_WORDS - 1,
436+
POOL_BYTES = POOL_WORDS * sizeof(u32),
437+
POOL_BITS = POOL_BYTES * 8,
438+
POOL_BITSHIFT = ilog2(POOL_WORDS) + 5,
439+
POOL_FRACBITS = POOL_WORDS << (ENTROPY_SHIFT + 5),
440+
439441
/* x^128 + x^104 + x^76 + x^51 +x^25 + x + 1 */
440-
{ S(128), 104, 76, 51, 25, 1 },
442+
POOL_TAP1 = 104,
443+
POOL_TAP2 = 76,
444+
POOL_TAP3 = 51,
445+
POOL_TAP4 = 25,
446+
POOL_TAP5 = 1
441447
};
442448

443449
/*
@@ -503,7 +509,6 @@ MODULE_PARM_DESC(ratelimit_disable, "Disable random ratelimit suppression");
503509
struct entropy_store;
504510
struct entropy_store {
505511
/* read-only data: */
506-
const struct poolinfo *poolinfo;
507512
__u32 *pool;
508513
const char *name;
509514

@@ -525,7 +530,6 @@ static void crng_reseed(struct crng_state *crng, struct entropy_store *r);
525530
static __u32 input_pool_data[INPUT_POOL_WORDS] __latent_entropy;
526531

527532
static struct entropy_store input_pool = {
528-
.poolinfo = &poolinfo_table[0],
529533
.name = "input",
530534
.lock = __SPIN_LOCK_UNLOCKED(input_pool.lock),
531535
.pool = input_pool_data
@@ -548,33 +552,26 @@ static __u32 const twist_table[8] = {
548552
static void _mix_pool_bytes(struct entropy_store *r, const void *in,
549553
int nbytes)
550554
{
551-
unsigned long i, tap1, tap2, tap3, tap4, tap5;
555+
unsigned long i;
552556
int input_rotate;
553-
int wordmask = r->poolinfo->poolwords - 1;
554557
const unsigned char *bytes = in;
555558
__u32 w;
556559

557-
tap1 = r->poolinfo->tap1;
558-
tap2 = r->poolinfo->tap2;
559-
tap3 = r->poolinfo->tap3;
560-
tap4 = r->poolinfo->tap4;
561-
tap5 = r->poolinfo->tap5;
562-
563560
input_rotate = r->input_rotate;
564561
i = r->add_ptr;
565562

566563
/* mix one byte at a time to simplify size handling and churn faster */
567564
while (nbytes--) {
568565
w = rol32(*bytes++, input_rotate);
569-
i = (i - 1) & wordmask;
566+
i = (i - 1) & POOL_WORDMASK;
570567

571568
/* XOR in the various taps */
572569
w ^= r->pool[i];
573-
w ^= r->pool[(i + tap1) & wordmask];
574-
w ^= r->pool[(i + tap2) & wordmask];
575-
w ^= r->pool[(i + tap3) & wordmask];
576-
w ^= r->pool[(i + tap4) & wordmask];
577-
w ^= r->pool[(i + tap5) & wordmask];
570+
w ^= r->pool[(i + POOL_TAP1) & POOL_WORDMASK];
571+
w ^= r->pool[(i + POOL_TAP2) & POOL_WORDMASK];
572+
w ^= r->pool[(i + POOL_TAP3) & POOL_WORDMASK];
573+
w ^= r->pool[(i + POOL_TAP4) & POOL_WORDMASK];
574+
w ^= r->pool[(i + POOL_TAP5) & POOL_WORDMASK];
578575

579576
/* Mix the result back in with a twist */
580577
r->pool[i] = (w >> 3) ^ twist_table[w & 7];
@@ -672,7 +669,6 @@ static void process_random_ready_list(void)
672669
static void credit_entropy_bits(struct entropy_store *r, int nbits)
673670
{
674671
int entropy_count, orig;
675-
const int pool_size = r->poolinfo->poolfracbits;
676672
int nfrac = nbits << ENTROPY_SHIFT;
677673

678674
if (!nbits)
@@ -706,25 +702,25 @@ static void credit_entropy_bits(struct entropy_store *r, int nbits)
706702
* turns no matter how large nbits is.
707703
*/
708704
int pnfrac = nfrac;
709-
const int s = r->poolinfo->poolbitshift + ENTROPY_SHIFT + 2;
705+
const int s = POOL_BITSHIFT + ENTROPY_SHIFT + 2;
710706
/* The +2 corresponds to the /4 in the denominator */
711707

712708
do {
713-
unsigned int anfrac = min(pnfrac, pool_size/2);
709+
unsigned int anfrac = min(pnfrac, POOL_FRACBITS/2);
714710
unsigned int add =
715-
((pool_size - entropy_count)*anfrac*3) >> s;
711+
((POOL_FRACBITS - entropy_count)*anfrac*3) >> s;
716712

717713
entropy_count += add;
718714
pnfrac -= anfrac;
719-
} while (unlikely(entropy_count < pool_size-2 && pnfrac));
715+
} while (unlikely(entropy_count < POOL_FRACBITS-2 && pnfrac));
720716
}
721717

722718
if (WARN_ON(entropy_count < 0)) {
723719
pr_warn("negative entropy/overflow: pool %s count %d\n",
724720
r->name, entropy_count);
725721
entropy_count = 0;
726-
} else if (entropy_count > pool_size)
727-
entropy_count = pool_size;
722+
} else if (entropy_count > POOL_FRACBITS)
723+
entropy_count = POOL_FRACBITS;
728724
if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig)
729725
goto retry;
730726

@@ -741,13 +737,11 @@ static void credit_entropy_bits(struct entropy_store *r, int nbits)
741737

742738
static int credit_entropy_bits_safe(struct entropy_store *r, int nbits)
743739
{
744-
const int nbits_max = r->poolinfo->poolwords * 32;
745-
746740
if (nbits < 0)
747741
return -EINVAL;
748742

749743
/* Cap the value to avoid overflows */
750-
nbits = min(nbits, nbits_max);
744+
nbits = min(nbits, POOL_BITS);
751745

752746
credit_entropy_bits(r, nbits);
753747
return 0;
@@ -1343,7 +1337,7 @@ static size_t account(struct entropy_store *r, size_t nbytes, int min,
13431337
int entropy_count, orig, have_bytes;
13441338
size_t ibytes, nfrac;
13451339

1346-
BUG_ON(r->entropy_count > r->poolinfo->poolfracbits);
1340+
BUG_ON(r->entropy_count > POOL_FRACBITS);
13471341

13481342
/* Can we pull enough? */
13491343
retry:
@@ -1409,8 +1403,7 @@ static void extract_buf(struct entropy_store *r, __u8 *out)
14091403

14101404
/* Generate a hash across the pool */
14111405
spin_lock_irqsave(&r->lock, flags);
1412-
blake2s_update(&state, (const u8 *)r->pool,
1413-
r->poolinfo->poolwords * sizeof(*r->pool));
1406+
blake2s_update(&state, (const u8 *)r->pool, POOL_BYTES);
14141407
blake2s_final(&state, hash); /* final zeros out state */
14151408

14161409
/*
@@ -1766,7 +1759,7 @@ static void __init init_std_data(struct entropy_store *r)
17661759
unsigned long rv;
17671760

17681761
mix_pool_bytes(r, &now, sizeof(now));
1769-
for (i = r->poolinfo->poolbytes; i > 0; i -= sizeof(rv)) {
1762+
for (i = POOL_BYTES; i > 0; i -= sizeof(rv)) {
17701763
if (!arch_get_random_seed_long(&rv) &&
17711764
!arch_get_random_long(&rv))
17721765
rv = random_get_entropy();

0 commit comments

Comments
 (0)