From c3ee1c757adff0df6df675c3cae524f9466016ca Mon Sep 17 00:00:00 2001 From: Andrew Morton Date: Tue, 9 Sep 2003 10:21:30 -0700 Subject: [PATCH] [PATCH] Reduce random driver lock contention From: Matt Mackall The new locking in the random driver is consuming 60% of CPU resources in Anton's monster power5 boxes. Basically, when the primary pool is 7/8th full, we shut off the firehose and go into a trickle mode to keep the pool fresh. Saves CPU for everyone and should make the contention drop off the charts too (though the trickle factor might need adjusting again for Origin-class machines). --- drivers/char/random.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/drivers/char/random.c b/drivers/char/random.c index ef3fd221dd771..c36d2ea46b073 100644 --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -253,6 +253,7 @@ #include #include #include +#include #include #include @@ -280,6 +281,15 @@ static int random_read_wakeup_thresh = 64; */ static int random_write_wakeup_thresh = 128; +/* + * When the input pool goes over trickle_thresh, start dropping most + * samples to avoid wasting CPU time and reduce lock contention. + */ + +static int trickle_thresh = DEFAULT_POOL_SIZE * 7; + +static DEFINE_PER_CPU(int, trickle_count) = 0; + /* * A pool of size .poolwords is stirred with a primitive polynomial * of degree .poolwords over GF(2). The taps for various sizes are @@ -778,6 +788,11 @@ static void add_timer_randomness(struct timer_rand_state *state, unsigned num) __s32 delta, delta2, delta3; int entropy = 0; + /* if over the trickle threshold, use only 1 in 4096 samples */ + if ( random_state->entropy_count > trickle_thresh && + (__get_cpu_var(trickle_count)++ & 0xfff)) + return; + #if defined (__i386__) || defined (__x86_64__) if (cpu_has_tsc) { __u32 high; -- 2.53.0