Re: [PATCH 4/5] tsc: wire up entropy generation function
From: Venkatesh Pallipadi <hidden>
Date: 2011-06-13 22:27:30
On Mon, Jun 13, 2011 at 3:06 PM, Jarod Wilson [off-list ref] wrote:
TSC is high enough resolution that we can use its low-order byte to stir new data into the random number generator entropy pool.
From what I vaguely remember from years past, rdtsc, especially last few bits of it are not very good as random number source. As they are based on lower bus frequency and a multiplier. May be things have changed these days. Adding Peter and Suresh for comments. Thanks, Venki
quoted hunk ↗ jump to hunk
CC: Matt Mackall <redacted> CC: "Venkatesh Pallipadi (Venki)" <redacted> CC: Thomas Gleixner <redacted> CC: Ingo Molnar <redacted> CC: John Stultz <redacted> CC: Herbert Xu <herbert@gondor.apana.org.au> CC: "David S. Miller" <davem@davemloft.net> Signed-off-by: Jarod Wilson <redacted> --- arch/x86/kernel/tsc.c | 18 ++++++++++++++++++ 1 files changed, 18 insertions(+), 0 deletions(-)diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c index 6cc6922..d206ec3 100644 --- a/arch/x86/kernel/tsc.c +++ b/arch/x86/kernel/tsc.c@@ -10,6 +10,7 @@#include <linux/clocksource.h> #include <linux/percpu.h> #include <linux/timex.h> +#include <linux/random.h> #include <asm/hpet.h> #include <asm/timer.h>@@ -768,11 +769,28 @@ static void resume_tsc(struct clocksource *cs)clocksource_tsc.cycle_last = 0; } +static void tsc_add_entropy(void) +{ + static u64 last; + u64 counter; + int delta; + + rdtscll(counter); + delta = (int)(counter - last); + last = counter; + + if (delta == counter) + return; + + add_clocksource_randomness(delta); +} + static struct clocksource clocksource_tsc = { .name = "tsc", .rating = 300, .read = read_tsc, .resume = resume_tsc, + .entropy = tsc_add_entropy, .mask = CLOCKSOURCE_MASK(64), .flags = CLOCK_SOURCE_IS_CONTINUOUS | CLOCK_SOURCE_MUST_VERIFY, -- 1.7.1