提交 f715729e 编写于 作者: L Linus Torvalds

Merge tag 'random_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/random

Pull /dev/random changes from Ted Ts'o:
 "These patches are designed to enable improvements to /dev/random for
  non-x86 platforms, in particular MIPS and ARM"

* tag 'random_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/random:
  random: allow architectures to optionally define random_get_entropy()
  random: run random_int_secret_init() run after all late_initcalls
...@@ -640,7 +640,7 @@ struct timer_rand_state { ...@@ -640,7 +640,7 @@ struct timer_rand_state {
*/ */
void add_device_randomness(const void *buf, unsigned int size) void add_device_randomness(const void *buf, unsigned int size)
{ {
unsigned long time = get_cycles() ^ jiffies; unsigned long time = random_get_entropy() ^ jiffies;
mix_pool_bytes(&input_pool, buf, size, NULL); mix_pool_bytes(&input_pool, buf, size, NULL);
mix_pool_bytes(&input_pool, &time, sizeof(time), NULL); mix_pool_bytes(&input_pool, &time, sizeof(time), NULL);
...@@ -677,7 +677,7 @@ static void add_timer_randomness(struct timer_rand_state *state, unsigned num) ...@@ -677,7 +677,7 @@ static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
goto out; goto out;
sample.jiffies = jiffies; sample.jiffies = jiffies;
sample.cycles = get_cycles(); sample.cycles = random_get_entropy();
sample.num = num; sample.num = num;
mix_pool_bytes(&input_pool, &sample, sizeof(sample), NULL); mix_pool_bytes(&input_pool, &sample, sizeof(sample), NULL);
...@@ -744,7 +744,7 @@ void add_interrupt_randomness(int irq, int irq_flags) ...@@ -744,7 +744,7 @@ void add_interrupt_randomness(int irq, int irq_flags)
struct fast_pool *fast_pool = &__get_cpu_var(irq_randomness); struct fast_pool *fast_pool = &__get_cpu_var(irq_randomness);
struct pt_regs *regs = get_irq_regs(); struct pt_regs *regs = get_irq_regs();
unsigned long now = jiffies; unsigned long now = jiffies;
__u32 input[4], cycles = get_cycles(); __u32 input[4], cycles = random_get_entropy();
input[0] = cycles ^ jiffies; input[0] = cycles ^ jiffies;
input[1] = irq; input[1] = irq;
...@@ -1459,12 +1459,11 @@ struct ctl_table random_table[] = { ...@@ -1459,12 +1459,11 @@ struct ctl_table random_table[] = {
static u32 random_int_secret[MD5_MESSAGE_BYTES / 4] ____cacheline_aligned; static u32 random_int_secret[MD5_MESSAGE_BYTES / 4] ____cacheline_aligned;
static int __init random_int_secret_init(void) int random_int_secret_init(void)
{ {
get_random_bytes(random_int_secret, sizeof(random_int_secret)); get_random_bytes(random_int_secret, sizeof(random_int_secret));
return 0; return 0;
} }
late_initcall(random_int_secret_init);
/* /*
* Get a random word for internal kernel use only. Similar to urandom but * Get a random word for internal kernel use only. Similar to urandom but
...@@ -1483,7 +1482,7 @@ unsigned int get_random_int(void) ...@@ -1483,7 +1482,7 @@ unsigned int get_random_int(void)
hash = get_cpu_var(get_random_int_hash); hash = get_cpu_var(get_random_int_hash);
hash[0] += current->pid + jiffies + get_cycles(); hash[0] += current->pid + jiffies + random_get_entropy();
md5_transform(hash, random_int_secret); md5_transform(hash, random_int_secret);
ret = hash[0]; ret = hash[0];
put_cpu_var(get_random_int_hash); put_cpu_var(get_random_int_hash);
......
...@@ -17,6 +17,7 @@ extern void add_interrupt_randomness(int irq, int irq_flags); ...@@ -17,6 +17,7 @@ extern void add_interrupt_randomness(int irq, int irq_flags);
extern void get_random_bytes(void *buf, int nbytes); extern void get_random_bytes(void *buf, int nbytes);
extern void get_random_bytes_arch(void *buf, int nbytes); extern void get_random_bytes_arch(void *buf, int nbytes);
void generate_random_uuid(unsigned char uuid_out[16]); void generate_random_uuid(unsigned char uuid_out[16]);
extern int random_int_secret_init(void);
#ifndef MODULE #ifndef MODULE
extern const struct file_operations random_fops, urandom_fops; extern const struct file_operations random_fops, urandom_fops;
......
...@@ -64,6 +64,20 @@ ...@@ -64,6 +64,20 @@
#include <asm/timex.h> #include <asm/timex.h>
#ifndef random_get_entropy
/*
* The random_get_entropy() function is used by the /dev/random driver
* in order to extract entropy via the relative unpredictability of
* when an interrupt takes places versus a high speed, fine-grained
* timing source or cycle counter. Since it will be occurred on every
* single interrupt, it must have a very low cost/overhead.
*
* By default we use get_cycles() for this purpose, but individual
* architectures may override this in their asm/timex.h header file.
*/
#define random_get_entropy() get_cycles()
#endif
/* /*
* SHIFT_PLL is used as a dampening factor to define how much we * SHIFT_PLL is used as a dampening factor to define how much we
* adjust the frequency correction for a given offset in PLL mode. * adjust the frequency correction for a given offset in PLL mode.
......
...@@ -76,6 +76,7 @@ ...@@ -76,6 +76,7 @@
#include <linux/elevator.h> #include <linux/elevator.h>
#include <linux/sched_clock.h> #include <linux/sched_clock.h>
#include <linux/context_tracking.h> #include <linux/context_tracking.h>
#include <linux/random.h>
#include <asm/io.h> #include <asm/io.h>
#include <asm/bugs.h> #include <asm/bugs.h>
...@@ -780,6 +781,7 @@ static void __init do_basic_setup(void) ...@@ -780,6 +781,7 @@ static void __init do_basic_setup(void)
do_ctors(); do_ctors();
usermodehelper_enable(); usermodehelper_enable();
do_initcalls(); do_initcalls();
random_int_secret_init();
} }
static void __init do_pre_smp_initcalls(void) static void __init do_pre_smp_initcalls(void)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册