提交 adc782da 编写于 作者: M Matt Mackall 提交者: Linus Torvalds

random: simplify and rename credit_entropy_store

- emphasize bits in the name
- make zero bits lock-free
- simplify logic
Signed-off-by: NMatt Mackall <mpm@selenic.com>
Cc: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: NAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: NLinus Torvalds <torvalds@linux-foundation.org>
上级 e68e5b66
...@@ -441,7 +441,7 @@ static struct entropy_store nonblocking_pool = { ...@@ -441,7 +441,7 @@ static struct entropy_store nonblocking_pool = {
/* /*
* This function adds bytes into the entropy "pool". It does not * This function adds bytes into the entropy "pool". It does not
* update the entropy estimate. The caller should call * update the entropy estimate. The caller should call
* credit_entropy_store if this is appropriate. * credit_entropy_bits if this is appropriate.
* *
* The pool is stirred with a primitive polynomial of the appropriate * The pool is stirred with a primitive polynomial of the appropriate
* degree, and then twisted. We twist by three bits at a time because * degree, and then twisted. We twist by three bits at a time because
...@@ -515,24 +515,22 @@ static void mix_pool_bytes(struct entropy_store *r, const void *in, int bytes) ...@@ -515,24 +515,22 @@ static void mix_pool_bytes(struct entropy_store *r, const void *in, int bytes)
/* /*
* Credit (or debit) the entropy store with n bits of entropy * Credit (or debit) the entropy store with n bits of entropy
*/ */
static void credit_entropy_store(struct entropy_store *r, int nbits) static void credit_entropy_bits(struct entropy_store *r, int nbits)
{ {
unsigned long flags; unsigned long flags;
if (!nbits)
return;
spin_lock_irqsave(&r->lock, flags); spin_lock_irqsave(&r->lock, flags);
if (r->entropy_count + nbits < 0) { DEBUG_ENT("added %d entropy credits to %s\n", nbits, r->name);
DEBUG_ENT("negative entropy/overflow (%d+%d)\n", r->entropy_count += nbits;
r->entropy_count, nbits); if (r->entropy_count < 0) {
DEBUG_ENT("negative entropy/overflow\n");
r->entropy_count = 0; r->entropy_count = 0;
} else if (r->entropy_count + nbits > r->poolinfo->POOLBITS) { } else if (r->entropy_count > r->poolinfo->POOLBITS)
r->entropy_count = r->poolinfo->POOLBITS; r->entropy_count = r->poolinfo->POOLBITS;
} else {
r->entropy_count += nbits;
if (nbits)
DEBUG_ENT("added %d entropy credits to %s\n",
nbits, r->name);
}
/* should we wake readers? */ /* should we wake readers? */
if (r == &input_pool && r->entropy_count >= random_read_wakeup_thresh) if (r == &input_pool && r->entropy_count >= random_read_wakeup_thresh)
...@@ -619,8 +617,8 @@ static void add_timer_randomness(struct timer_rand_state *state, unsigned num) ...@@ -619,8 +617,8 @@ static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
* Round down by 1 bit on general principles, * Round down by 1 bit on general principles,
* and limit entropy entimate to 12 bits. * and limit entropy entimate to 12 bits.
*/ */
credit_entropy_store(&input_pool, credit_entropy_bits(&input_pool,
min_t(int, fls(delta>>1), 11)); min_t(int, fls(delta>>1), 11));
} }
out: out:
preempt_enable(); preempt_enable();
...@@ -702,7 +700,7 @@ static void xfer_secondary_pool(struct entropy_store *r, size_t nbytes) ...@@ -702,7 +700,7 @@ static void xfer_secondary_pool(struct entropy_store *r, size_t nbytes)
bytes = extract_entropy(r->pull, tmp, bytes, bytes = extract_entropy(r->pull, tmp, bytes,
random_read_wakeup_thresh / 8, rsvd); random_read_wakeup_thresh / 8, rsvd);
mix_pool_bytes(r, tmp, bytes); mix_pool_bytes(r, tmp, bytes);
credit_entropy_store(r, bytes*8); credit_entropy_bits(r, bytes*8);
} }
} }
...@@ -1073,7 +1071,7 @@ static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg) ...@@ -1073,7 +1071,7 @@ static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
return -EPERM; return -EPERM;
if (get_user(ent_count, p)) if (get_user(ent_count, p))
return -EFAULT; return -EFAULT;
credit_entropy_store(&input_pool, ent_count); credit_entropy_bits(&input_pool, ent_count);
return 0; return 0;
case RNDADDENTROPY: case RNDADDENTROPY:
if (!capable(CAP_SYS_ADMIN)) if (!capable(CAP_SYS_ADMIN))
...@@ -1088,7 +1086,7 @@ static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg) ...@@ -1088,7 +1086,7 @@ static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
size); size);
if (retval < 0) if (retval < 0)
return retval; return retval;
credit_entropy_store(&input_pool, ent_count); credit_entropy_bits(&input_pool, ent_count);
return 0; return 0;
case RNDZAPENTCNT: case RNDZAPENTCNT:
case RNDCLEARPOOL: case RNDCLEARPOOL:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册