Skip to content

Commit 840f950

Browse files
committed
random: clean up interrupt entropy accounting for archs w/o cycle counters
For architectures that don't have cycle counters, the algorithm for deciding when to avoid giving entropy credit due to back-to-back timer interrupts didn't make any sense, since we were checking every 64 interrupts. Change it so that we only give an entropy credit if the majority of the interrupts are not based on the timer. Signed-off-by: Theodore Ts'o <tytso@mit.edu> Cc: George Spelvin <linux@horizon.com>
1 parent cff8503 commit 840f950

1 file changed

Lines changed: 23 additions & 19 deletions

File tree

‎drivers/char/random.c‎

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -548,9 +548,9 @@ static void mix_pool_bytes(struct entropy_store *r, const void *in,
548548
struct fast_pool {
549549
__u32 pool[4];
550550
unsigned long last;
551-
unsigned short count;
551+
unsigned char count;
552+
unsigned char notimer_count;
552553
unsigned char rotate;
553-
unsigned char last_timer_intr;
554554
};
555555

556556
/*
@@ -850,15 +850,23 @@ void add_interrupt_randomness(int irq, int irq_flags)
850850
input[3] = ip >> 32;
851851

852852
fast_mix(fast_pool, input);
853+
if ((irq_flags & __IRQF_TIMER) == 0)
854+
fast_pool->notimer_count++;
853855

854-
if ((fast_pool->count & 63) && !time_after(now, fast_pool->last + HZ))
855-
return;
856+
if (cycles) {
857+
if ((fast_pool->count < 64) &&
858+
!time_after(now, fast_pool->last + HZ))
859+
return;
860+
} else {
861+
/* CPU does not have a cycle counting register :-( */
862+
if (fast_pool->count < 64)
863+
return;
864+
}
856865

857866
r = nonblocking_pool.initialized ? &input_pool : &nonblocking_pool;
858-
if (!spin_trylock(&r->lock)) {
859-
fast_pool->count--;
867+
if (!spin_trylock(&r->lock))
860868
return;
861-
}
869+
862870
fast_pool->last = now;
863871
__mix_pool_bytes(r, &fast_pool->pool, sizeof(fast_pool->pool));
864872

@@ -874,19 +882,15 @@ void add_interrupt_randomness(int irq, int irq_flags)
874882
spin_unlock(&r->lock);
875883

876884
/*
877-
* If we don't have a valid cycle counter, and we see
878-
* back-to-back timer interrupts, then skip giving credit for
879-
* any entropy, otherwise credit 1 bit.
885+
* If we have a valid cycle counter or if the majority of
886+
* interrupts collected were non-timer interrupts, then give
887+
* an entropy credit of 1 bit. Yes, this is being very
888+
* conservative.
880889
*/
881-
credit++;
882-
if (cycles == 0) {
883-
if (irq_flags & __IRQF_TIMER) {
884-
if (fast_pool->last_timer_intr)
885-
credit--;
886-
fast_pool->last_timer_intr = 1;
887-
} else
888-
fast_pool->last_timer_intr = 0;
889-
}
890+
if (cycles || (fast_pool->notimer_count >= 32))
891+
credit++;
892+
893+
fast_pool->count = fast_pool->notimer_count = 0;
890894

891895
credit_entropy_bits(r, credit);
892896
}

0 commit comments

Comments
 (0)