[PATCH] random: limit overhead of add_interrupt_randomness

From: Joern Engel
Date: Wed Feb 13 2013 - 13:34:26 EST


fast_mix is noticeably less fast than the name might imply. Add
rate-limiting to it, so we only run it once per jiffie and cpu for the
painful case of a single interrupt hammering a cpu. Instead we do the
dumbest possible mixing - we xor the input with the pool without any
shifting whatsoever. Gathers some randomness at near-zero cost.

Signed-off-by: Joern Engel <joern@xxxxxxxxx>
---
drivers/char/random.c | 12 ++++++++++++
1 file changed, 12 insertions(+)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index b86eae9..7b7f64e 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -557,6 +557,7 @@ static void mix_pool_bytes(struct entropy_store *r, const void *in,
struct fast_pool {
__u32 pool[4];
unsigned long last;
+ unsigned long last_jiffies;
unsigned short count;
unsigned char rotate;
unsigned char last_timer_intr;
@@ -760,6 +761,17 @@ void add_interrupt_randomness(int irq, int irq_flags)
input[3] = ip >> 32;
}

+ /*
+ * Even fast_mix is slow when dealing with 6-digit interrupt
+ * rates. Rate-limit this to once per jiffie. If we get lots
+ * of interrupts, this still generates 1.6 bits of entropy per
+ * second and cpu. If we get few interrupts, it shouldn't
+ * substantially change the entropy collection.
+ */
+ if (fast_pool->last_jiffies == jiffies)
+ return;
+ fast_pool->last_jiffies = jiffies;
+
fast_mix(fast_pool, input, sizeof(input));

if ((fast_pool->count & 1023) &&
--
1.7.10.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/