Skip to content

Commit adc782d

Browse files
Matt Mackalltorvalds
authored andcommitted
random: simplify and rename credit_entropy_store
- emphasize bits in the name - make zero bits lock-free - simplify logic Signed-off-by: Matt Mackall <mpm@selenic.com> Cc: Theodore Ts'o <tytso@mit.edu> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent e68e5b6 commit adc782d

1 file changed

Lines changed: 15 additions & 17 deletions

File tree

‎drivers/char/random.c‎

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ static struct entropy_store nonblocking_pool = {
441441
/*
442442
* This function adds bytes into the entropy "pool". It does not
443443
* update the entropy estimate. The caller should call
444-
* credit_entropy_store if this is appropriate.
444+
* credit_entropy_bits if this is appropriate.
445445
*
446446
* The pool is stirred with a primitive polynomial of the appropriate
447447
* 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)
515515
/*
516516
* Credit (or debit) the entropy store with n bits of entropy
517517
*/
518-
static void credit_entropy_store(struct entropy_store *r, int nbits)
518+
static void credit_entropy_bits(struct entropy_store *r, int nbits)
519519
{
520520
unsigned long flags;
521521

522+
if (!nbits)
523+
return;
524+
522525
spin_lock_irqsave(&r->lock, flags);
523526

524-
if (r->entropy_count + nbits < 0) {
525-
DEBUG_ENT("negative entropy/overflow (%d+%d)\n",
526-
r->entropy_count, nbits);
527+
DEBUG_ENT("added %d entropy credits to %s\n", nbits, r->name);
528+
r->entropy_count += nbits;
529+
if (r->entropy_count < 0) {
530+
DEBUG_ENT("negative entropy/overflow\n");
527531
r->entropy_count = 0;
528-
} else if (r->entropy_count + nbits > r->poolinfo->POOLBITS) {
532+
} else if (r->entropy_count > r->poolinfo->POOLBITS)
529533
r->entropy_count = r->poolinfo->POOLBITS;
530-
} else {
531-
r->entropy_count += nbits;
532-
if (nbits)
533-
DEBUG_ENT("added %d entropy credits to %s\n",
534-
nbits, r->name);
535-
}
536534

537535
/* should we wake readers? */
538536
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)
619617
* Round down by 1 bit on general principles,
620618
* and limit entropy entimate to 12 bits.
621619
*/
622-
credit_entropy_store(&input_pool,
623-
min_t(int, fls(delta>>1), 11));
620+
credit_entropy_bits(&input_pool,
621+
min_t(int, fls(delta>>1), 11));
624622
}
625623
out:
626624
preempt_enable();
@@ -702,7 +700,7 @@ static void xfer_secondary_pool(struct entropy_store *r, size_t nbytes)
702700
bytes = extract_entropy(r->pull, tmp, bytes,
703701
random_read_wakeup_thresh / 8, rsvd);
704702
mix_pool_bytes(r, tmp, bytes);
705-
credit_entropy_store(r, bytes*8);
703+
credit_entropy_bits(r, bytes*8);
706704
}
707705
}
708706

@@ -1073,7 +1071,7 @@ static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
10731071
return -EPERM;
10741072
if (get_user(ent_count, p))
10751073
return -EFAULT;
1076-
credit_entropy_store(&input_pool, ent_count);
1074+
credit_entropy_bits(&input_pool, ent_count);
10771075
return 0;
10781076
case RNDADDENTROPY:
10791077
if (!capable(CAP_SYS_ADMIN))
@@ -1088,7 +1086,7 @@ static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
10881086
size);
10891087
if (retval < 0)
10901088
return retval;
1091-
credit_entropy_store(&input_pool, ent_count);
1089+
credit_entropy_bits(&input_pool, ent_count);
10921090
return 0;
10931091
case RNDZAPENTCNT:
10941092
case RNDCLEARPOOL:

0 commit comments

Comments
 (0)