Skip to content

Commit 18263c4

Browse files
committed
random: cleanup fractional entropy shift constants
The entropy estimator is calculated in terms of 1/8 bits, which means there are various constants where things are shifted by 3. Move these into our pool info enum with the other relevant constants. While we're at it, move an English assertion about sizes into a proper BUILD_BUG_ON so that the compiler can ensure this invariant. Reviewed-by: Dominik Brodowski <linux@dominikbrodowski.net> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
1 parent b3d51c1 commit 18263c4

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

drivers/char/random.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -358,16 +358,6 @@
358358

359359
/* #define ADD_INTERRUPT_BENCH */
360360

361-
/*
362-
* To allow fractional bits to be tracked, the entropy_count field is
363-
* denominated in units of 1/8th bits.
364-
*
365-
* 2*(POOL_ENTROPY_SHIFT + poolbitshift) must <= 31, or the multiply in
366-
* credit_entropy_bits() needs to be 64 bits wide.
367-
*/
368-
#define POOL_ENTROPY_SHIFT 3
369-
#define POOL_ENTROPY_BITS() (input_pool.entropy_count >> POOL_ENTROPY_SHIFT)
370-
371361
/*
372362
* If the entropy count falls under this number of bits, then we
373363
* should wake up processes which are selecting or polling on write
@@ -425,8 +415,13 @@ enum poolinfo {
425415
POOL_WORDMASK = POOL_WORDS - 1,
426416
POOL_BYTES = POOL_WORDS * sizeof(u32),
427417
POOL_BITS = POOL_BYTES * 8,
428-
POOL_BITSHIFT = ilog2(POOL_WORDS) + 5,
429-
POOL_FRACBITS = POOL_WORDS << (POOL_ENTROPY_SHIFT + 5),
418+
POOL_BITSHIFT = ilog2(POOL_BITS),
419+
420+
/* To allow fractional bits to be tracked, the entropy_count field is
421+
* denominated in units of 1/8th bits. */
422+
POOL_ENTROPY_SHIFT = 3,
423+
#define POOL_ENTROPY_BITS() (input_pool.entropy_count >> POOL_ENTROPY_SHIFT)
424+
POOL_FRACBITS = POOL_BITS << POOL_ENTROPY_SHIFT,
430425

431426
/* x^128 + x^104 + x^76 + x^51 +x^25 + x + 1 */
432427
POOL_TAP1 = 104,
@@ -652,6 +647,9 @@ static void credit_entropy_bits(int nbits)
652647
int entropy_count, entropy_bits, orig;
653648
int nfrac = nbits << POOL_ENTROPY_SHIFT;
654649

650+
/* Ensure that the multiplication can avoid being 64 bits wide. */
651+
BUILD_BUG_ON(2 * (POOL_ENTROPY_SHIFT + POOL_BITSHIFT) > 31);
652+
655653
if (!nbits)
656654
return;
657655

@@ -687,13 +685,13 @@ static void credit_entropy_bits(int nbits)
687685
/* The +2 corresponds to the /4 in the denominator */
688686

689687
do {
690-
unsigned int anfrac = min(pnfrac, POOL_FRACBITS/2);
688+
unsigned int anfrac = min(pnfrac, POOL_FRACBITS / 2);
691689
unsigned int add =
692-
((POOL_FRACBITS - entropy_count)*anfrac*3) >> s;
690+
((POOL_FRACBITS - entropy_count) * anfrac * 3) >> s;
693691

694692
entropy_count += add;
695693
pnfrac -= anfrac;
696-
} while (unlikely(entropy_count < POOL_FRACBITS-2 && pnfrac));
694+
} while (unlikely(entropy_count < POOL_FRACBITS - 2 && pnfrac));
697695
}
698696

699697
if (WARN_ON(entropy_count < 0)) {

0 commit comments

Comments
 (0)