提交 0866992d 编写于 作者: J Jason A. Donenfeld 提交者: Zheng Zengkai

random: cleanup fractional entropy shift constants

stable inclusion
from stable-v5.10.119
commit a9db850c219fdaf144997c683ba35f293225dfdf
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/I5L6BB

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=a9db850c219fdaf144997c683ba35f293225dfdf

--------------------------------

commit 18263c4e upstream.

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: NDominik Brodowski <linux@dominikbrodowski.net>
Signed-off-by: NJason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: NZheng Zengkai <zhengzengkai@huawei.com>
Acked-by: NXie XiuQi <xiexiuqi@huawei.com>
上级 4fccd911
...@@ -358,16 +358,6 @@ ...@@ -358,16 +358,6 @@
/* #define ADD_INTERRUPT_BENCH */ /* #define ADD_INTERRUPT_BENCH */
/*
* To allow fractional bits to be tracked, the entropy_count field is
* denominated in units of 1/8th bits.
*
* 2*(POOL_ENTROPY_SHIFT + poolbitshift) must <= 31, or the multiply in
* credit_entropy_bits() needs to be 64 bits wide.
*/
#define POOL_ENTROPY_SHIFT 3
#define POOL_ENTROPY_BITS() (input_pool.entropy_count >> POOL_ENTROPY_SHIFT)
/* /*
* If the entropy count falls under this number of bits, then we * If the entropy count falls under this number of bits, then we
* should wake up processes which are selecting or polling on write * should wake up processes which are selecting or polling on write
...@@ -425,8 +415,13 @@ enum poolinfo { ...@@ -425,8 +415,13 @@ enum poolinfo {
POOL_WORDMASK = POOL_WORDS - 1, POOL_WORDMASK = POOL_WORDS - 1,
POOL_BYTES = POOL_WORDS * sizeof(u32), POOL_BYTES = POOL_WORDS * sizeof(u32),
POOL_BITS = POOL_BYTES * 8, POOL_BITS = POOL_BYTES * 8,
POOL_BITSHIFT = ilog2(POOL_WORDS) + 5, POOL_BITSHIFT = ilog2(POOL_BITS),
POOL_FRACBITS = POOL_WORDS << (POOL_ENTROPY_SHIFT + 5),
/* To allow fractional bits to be tracked, the entropy_count field is
* denominated in units of 1/8th bits. */
POOL_ENTROPY_SHIFT = 3,
#define POOL_ENTROPY_BITS() (input_pool.entropy_count >> POOL_ENTROPY_SHIFT)
POOL_FRACBITS = POOL_BITS << POOL_ENTROPY_SHIFT,
/* x^128 + x^104 + x^76 + x^51 +x^25 + x + 1 */ /* x^128 + x^104 + x^76 + x^51 +x^25 + x + 1 */
POOL_TAP1 = 104, POOL_TAP1 = 104,
...@@ -652,6 +647,9 @@ static void credit_entropy_bits(int nbits) ...@@ -652,6 +647,9 @@ static void credit_entropy_bits(int nbits)
int entropy_count, entropy_bits, orig; int entropy_count, entropy_bits, orig;
int nfrac = nbits << POOL_ENTROPY_SHIFT; int nfrac = nbits << POOL_ENTROPY_SHIFT;
/* Ensure that the multiplication can avoid being 64 bits wide. */
BUILD_BUG_ON(2 * (POOL_ENTROPY_SHIFT + POOL_BITSHIFT) > 31);
if (!nbits) if (!nbits)
return; return;
...@@ -687,13 +685,13 @@ static void credit_entropy_bits(int nbits) ...@@ -687,13 +685,13 @@ static void credit_entropy_bits(int nbits)
/* The +2 corresponds to the /4 in the denominator */ /* The +2 corresponds to the /4 in the denominator */
do { do {
unsigned int anfrac = min(pnfrac, POOL_FRACBITS/2); unsigned int anfrac = min(pnfrac, POOL_FRACBITS / 2);
unsigned int add = unsigned int add =
((POOL_FRACBITS - entropy_count)*anfrac*3) >> s; ((POOL_FRACBITS - entropy_count) * anfrac * 3) >> s;
entropy_count += add; entropy_count += add;
pnfrac -= anfrac; pnfrac -= anfrac;
} while (unlikely(entropy_count < POOL_FRACBITS-2 && pnfrac)); } while (unlikely(entropy_count < POOL_FRACBITS - 2 && pnfrac));
} }
if (WARN_ON(entropy_count < 0)) { if (WARN_ON(entropy_count < 0)) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册