random: replace urandom pool with a CRNG
Theodore Ts'o
maintainerv3 cover random: replace urandom pool with a CRNG
By using a CRNG to replace the urandom pool, we address a number of complaints which Stephan Mueller has been concerned about. We now use a much more aggressive interrupt sampling system to quickly initialize a CRNG which gets used in place of the original non-blocking pool. This tends to get initialized *very* quickly (before the devices are finished being proved.) Like Stephan's proposal, this assumes that we can get a bit of entropy per interrupt, which may be problematic on some architectures. So after we do this quick-and-dirty initialization, we then fall back to the slower, more conservative interrupt sampling system to fill the input pool, and we will do a catastrophic reseeding once we get 128 bits using the slower but more conservative system, and every five minutes afterwards, if possible.
In addition, on NUMA systems we make the CRNG state per-NUMA socket, to address the NUMA locking contention problem which Andi Kleen has been complaining about. I'm not entirely sure this will work well on the crazy big SGI systems, but they are rare. Whether they are rarer than abusive userspace programs that are continuously pounding /dev/urandom is unclear. If necessary we can make a config option to turn off the per-NUMA socket hack if it proves to be problematic.
Note: I didn't propose this for merging in 4.7 because I wanted to
further refine the reseeding logic and because I wanted to get more feedback. My plan is to merge these changes for the 4.8 merge window.
These patches are also available at:
git://git.kernel.org/pub/scm/linux/kernel/git/tytso/random.git
Changes since -v2:
* Rebased to v4.7-rc1
* Improved/reworked CRNG reseeding and backtracking protection
* Preseed the CRNG state from system data
* Added fix to properly align the get_random_int_hash[] array
Eric Biggers (1):
random: properly align get_random_int_hash
Stephan Mueller (1):
random: add interrupt callback to VMBus IRQ handler
Theodore Ts'o (3):
random: replace non-blocking pool with a Chacha20-based CRNG
random: make /dev/urandom scalable for silly userspace programs
random: add backtracking protection to the CRNG
crypto/chacha20_generic.c | 61 ------- drivers/char/random.c | 446 ++++++++++++++++++++++++++++++++++++---------- drivers/hv/vmbus_drv.c | 3 + include/crypto/chacha20.h | 1 + lib/Makefile | 2 +- lib/chacha20.c | 79 ++++++++ 6 files changed, 438 insertions(+), 154 deletions(-) create mode 100644 lib/chacha20.c
Signature · 1 line
2.5.0
Theodore Ts'o
maintainerPATCH v3 1/5 random: replace non-blocking pool with a Chacha20-based CRNG
The CRNG is faster, and we don't pretend to track entropy usage in the CRNG any more.
--- crypto/chacha20_generic.c | 61 -------- drivers/char/random.c | 350 ++++++++++++++++++++++++++++++++++------------ include/crypto/chacha20.h | 1 + lib/Makefile | 2 +- lib/chacha20.c | 79 +++++++++++ 5 files changed, 340 insertions(+), 153 deletions(-) create mode 100644 lib/chacha20.c
crypto/chacha20_generic.c +0 -61
@@ -15,72 +15,11 @@#include <linux/module.h> #include <crypto/chacha20.h> -static inline u32 rotl32 (u32 v,u8 n) -{ -return (v << n) | (v >> (sizeof (v) *8 - n)); -} -static inline u32 le32_to_cpuvp (const void *p) {return le32_to_cpup (p); } -static void chacha20_block (u32 *state,void *stream) -{ -u32 x[16 ], *out = stream; -int i; - -for (i =0 ; i <ARRAY_SIZE (x); i++) - x[i] = state[i]; - -for (i =0 ; i <20 ; i +=2 ) { - x[0 ] += x[4 ]; x[12 ] =rotl32 (x[12 ] ^ x[0 ],16 ); - x[1 ] += x[5 ]; x[13 ] =rotl32 (x[13 ] ^ x[1 ],16 ); - x[2 ] += x[6 ]; x[14 ] =rotl32 (x[14 ] ^ x[2 ],16 ); - x[3 ] += x[7 ]; x[15 ] =rotl32 (x[15 ] ^ x[3 ],16 ); - - x[8 ] += x[12 ]; x[4 ] =rotl32 (x[4 ] ^ x[8 ],12 ); - x[9 ] += x[13 ]; x[5 ] =rotl32 (x[5 ] ^ x[9 ],12 ); - x[10 ] += x[14 ]; x[6 ] =rotl32 (x[6 ] ^ x[10 ],12 ); - x[11 ] += x[15 ]; x[7 ] =rotl32 (x[7 ] ^ x[11 ],12 ); - - x[0 ] += x[4 ]; x[12 ] =rotl32 (x[12 ] ^ x[0 ],8 ); - x[1 ] += x[5 ]; x[13 ] =rotl32 (x[13 ] ^ x[1 ],8 ); - x[2 ] += x[6 ]; x[14 ] =rotl32 (x[14 ] ^ x[2 ],8 ); - x[3 ] += x[7 ]; x[15 ] =rotl32 (x[15 ] ^ x[3 ],8 ); - - x[8 ] += x[12 ]; x[4 ] =rotl32 (x[4 ] ^ x[8 ],7 ); - x[9 ] += x[13 ]; x[5 ] =rotl32 (x[5 ] ^ x[9 ],7 ); - x[10 ] += x[14 ]; x[6 ] =rotl32 (x[6 ] ^ x[10 ],7 ); - x[11 ] += x[15 ]; x[7 ] =rotl32 (x[7 ] ^ x[11 ],7 ); - - x[0 ] += x[5 ]; x[15 ] =rotl32 (x[15 ] ^ x[0 ],16 ); - x[1 ] += x[6 ]; x[12 ] =rotl32 (x[12 ] ^ x[1 ],16 ); - x[2 ] += x[7 ]; x[13 ] =rotl32 (x[13 ] ^ x[2 ],16 ); - x[3 ] += x[4 ]; x[14 ] =rotl32 (x[14 ] ^ x[3 ],16 ); - - x[10 ] += x[15 ]; x[5 ] =rotl32 (x[5 ] ^ x[10 ],12 ); - x[11 ] += x[12 ]; x[6 ] =rotl32 (x[6 ] ^ x[11 ],12 ); - x[8 ] += x[13 ]; x[7 ] =rotl32 (x[7 ] ^ x[8 ],12 ); - x[9 ] += x[14 ]; x[4 ] =rotl32 (x[4 ] ^ x[9 ],12 ); - - x[0 ] += x[5 ]; x[15 ] =rotl32 (x[15 ] ^ x[0 ],8 ); - x[1 ] += x[6 ]; x[12 ] =rotl32 (x[12 ] ^ x[1 ],8 ); - x[2 ] += x[7 ]; x[13 ] =rotl32 (x[13 ] ^ x[2 ],8 ); - x[3 ] += x[4 ]; x[14 ] =rotl32 (x[14 ] ^ x[3 ],8 ); - - x[10 ] += x[15 ]; x[5 ] =rotl32 (x[5 ] ^ x[10 ],7 ); - x[11 ] += x[12 ]; x[6 ] =rotl32 (x[6 ] ^ x[11 ],7 ); - x[8 ] += x[13 ]; x[7 ] =rotl32 (x[7 ] ^ x[8 ],7 ); - x[9 ] += x[14 ]; x[4 ] =rotl32 (x[4 ] ^ x[9 ],7 ); - } - -for (i =0 ; i <ARRAY_SIZE (x); i++) - out[i] =cpu_to_le32 (x[i] + state[i]); - - state[12 ]++; -} -static void chacha20_docrypt (u32 *state,u8 *dst,const u8 *src,unsigned int bytes) {
drivers/char/random.c +259 -91
518 lines not on this page · raw message
include/crypto/chacha20.h +1 -0
@@ -16,6 +16,7 @@ struct chacha20_ctx {u32 key[8 ]; }; +void chacha20_block (u32 *state,void *stream);void crypto_chacha20_init (u32 *state,struct chacha20_ctx *ctx,u8 *iv);int crypto_chacha20_setkey (struct crypto_tfm *tfm,const u8 *key,unsigned int keysize);
lib/Makefile +1 -1
@@ -22,7 +22,7 @@ KCOV_INSTRUMENT_hweight.o := n lib-y := ctype.o string.o vsprintf.o cmdline.o \ rbtree.o radix-tree.o dump_stack.o timerqueue.o\ idr.o int_sqrt.o extable.o \ - sha1.o md5.o irq_regs.o argv_split.o \ + sha1.o chacha20.o md5.o irq_regs.o argv_split.o \ flex_proportions.o ratelimit.o show_mem.o \ is_single_threaded.o plist.o decompress.o kobject_uevent.o \ earlycpio.o seq_buf.o nmi_backtrace.o nodemask.o
lib/chacha20.c new file +79 -0
79 lines not on this page · raw message
Signature · 1 line
2.5.0
Theodore Ts'o
maintainerPATCH v3 2/5 random: make /dev/urandom scalable for silly userspace programs
In reply to Theodore Ts'oOn a system with a 4 socket (NUMA) system where a large number of application threads were all trying to read from /dev/urandom, this can result in the system spending 80% of its time contending on the global urandom spinlock. The application should have used its own PRNG, but let's try to help it from running, lemming-like, straight over the locking cliff.
--- drivers/char/random.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 4 deletions(-)
drivers/char/random.c +53 -4
@@ -434,6 +434,8 @@ struct crng_state primary_crng = {*/ static int crng_init =0 ;#define crng_ready () (likely(crng_init >= 2)) +static void _extract_crng (struct crng_state *crng, +__u8 out[CHACHA20_BLOCK_SIZE ]);static void extract_crng (__u8 out[CHACHA20_BLOCK_SIZE ]);static void process_random_ready_list (void );
@@ -754,6 +756,17 @@ static void credit_entropy_bits_safe(struct entropy_store *r, int nbits)static DECLARE_WAIT_QUEUE_HEAD (crng_init_wait); +#ifdef CONFIG_NUMA +/* +* Hack to deal with crazy userspace progams when they are all trying +* to access /dev/urandom in parallel. The programs are almost +* certainly doing something terribly wrong, but we'll work around +* their brain damage. +*/ +static struct crng_state **crng_node_pool__read_mostly ; +#endif + +static void _initialize_crng (struct crng_state *crng) {int i;
@@ -774,11 +787,13 @@ static void _initialize_crng(struct crng_state *crng) crng->init_time = jiffies -CRNG_RESEED_INTERVAL -1 ; } +#ifdef CONFIG_NUMA static void initialize_crng (struct crng_state *crng) {_initialize_crng (crng);spin_lock_init (&crng->lock); } +#endif static int crng_fast_load (__u32 pool[4 ]) {
@@ -818,7 +833,7 @@ static void crng_reseed(struct crng_state *crng, struct entropy_store *r)if (num ==0 )return ; }else -extract_crng (buf.block); +_extract_crng (&primary_crng, buf.block);spin_lock_irqsave (&primary_crng.lock, flags);for (i =0 ; i <8 ; i++) {unsigned long rv;
@@ -838,19 +853,26 @@ static void crng_reseed(struct crng_state *crng, struct entropy_store *r)spin_unlock_irqrestore (&primary_crng.lock, flags); } +static inline void maybe_reseed_primary_crng (void ) +{ +if (crng_init >2 && +time_after (jiffies, primary_crng.init_time +CRNG_RESEED_INTERVAL )) +crng_reseed (&primary_crng, &input_pool); +} +static inline void crng_wait_ready (void ) {wait_event_interruptible (crng_init_wait,crng_ready ()); } -static void extract_crng (__u8 out[CHACHA20_BLOCK_SIZE ]) +static void _extract_crng (struct crng_state *crng, +__u8 out[CHACHA20_BLOCK_SIZE ]) {unsigned long v, flags; -struct crng_state *crng = &primary_crng;if (crng_init >2 &&time_after (jiffies, crng->init_time +CRNG_RESEED_INTERVAL )) -crng_reseed (crng, &input_pool); +crng_reseed (crng, crng == &primary_crng ? &input_pool :NULL );spin_lock_irqsave (&crng->lock, flags);if (arch_get_random_long (&v)) crng->state[14 ] ^= v;
@@ -860,6 +882,17 @@ static void extract_crng(__u8 out[CHACHA20_BLOCK_SIZE])spin_unlock_irqrestore (&crng->lock, flags); } +static void extract_crng (__u8 out[CHACHA20_BLOCK_SIZE ]) +{ +#ifndef CONFIG_NUMA +struct crng_state *crng = &primary_crng; +#else +struct crng_state *crng = crng_node_pool[numa_node_id ()]; +#endif + +_extract_crng (crng, out); +} +static ssize_t extract_crng_user (void __user *buf,size_t nbytes) {ssize_t ret =0 , i;
@@ -1573,6 +1606,22 @@ static void init_std_data(struct entropy_store *r)*/ static int rand_initialize (void ) { +#ifdef CONFIG_NUMA +int i; +int num_nodes =num_possible_nodes (); +struct crng_state *crng; + + crng_node_pool =kmalloc (num_nodes *sizeof (void *), +GFP_KERNEL |__GFP_NOFAIL); + +for (i=0 ; i < num_nodes; i++) { + crng =kmalloc (sizeof (struct crng_state ), +GFP_KERNEL | __GFP_NOFAIL); +initialize_crng (crng); + crng_node_pool[i] = crng; + + } +#endif init_std_data (&input_pool);init_std_data (&blocking_pool);_initialize_crng (&primary_crng);
Signature · 1 line
2.5.0
Theodore Ts'o
maintainerPATCH v3 5/5 random: properly align get_random_int_hash
In reply to Theodore Ts'oFrom: Eric Biggers <[email protected]>
get_random_long() reads from the get_random_int_hash array using an unsigned long pointer. For this code to be guaranteed correct on all architectures, the array must be aligned to an unsigned long boundary.
--- drivers/char/random.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/char/random.c +3 -1
@@ -2033,13 +2033,15 @@ int random_int_secret_init(void)return 0 ; } +static DEFINE_PER_CPU(__u32 [MD5_DIGEST_WORDS], get_random_int_hash) +__aligned(sizeof(unsigned long)) ; +/* * Get a random word for internal kernel use only. Similar to urandom but * with the goal of minimal entropy pool depletion. As a result, the random * value is not cryptographically secure but for several uses the cost of * depleting entropy is too high */ -static DEFINE_PER_CPU(__u32 [MD5_DIGEST_WORDS], get_random_int_hash);unsigned int get_random_int (void ) {__u32 *hash;
Signature · 1 line
2.5.0
Theodore Ts'o
maintainerPATCH v3 4/5 random: add backtracking protection to the CRNG
In reply to Theodore Ts'o--- drivers/char/random.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 5 deletions(-)
drivers/char/random.c +47 -5
@@ -436,7 +436,8 @@ static int crng_init = 0;#define crng_ready () (likely(crng_init >= 2))static void _extract_crng (struct crng_state *crng,__u8 out[CHACHA20_BLOCK_SIZE ]); -static void extract_crng (__u8 out[CHACHA20_BLOCK_SIZE ]); +static void _crng_backtrack_protect (struct crng_state *crng, +__u8 tmp[CHACHA20_BLOCK_SIZE ],int used);static void process_random_ready_list (void );/**********************************************************************
@@ -832,8 +833,11 @@ static void crng_reseed(struct crng_state *crng, struct entropy_store *r) num =extract_entropy (r, &buf,32 ,16 ,0 );if (num ==0 )return ; - }else + }else {_extract_crng (&primary_crng, buf.block); +_crng_backtrack_protect (&primary_crng, buf.block, +CHACHA20_KEY_SIZE ); + }spin_lock_irqsave (&primary_crng.lock, flags);for (i =0 ; i <8 ; i++) {unsigned long rv;
@@ -893,9 +897,44 @@ static void extract_crng(__u8 out[CHACHA20_BLOCK_SIZE])_extract_crng (crng, out); } +/* +* Use the leftover bytes from the CRNG block output (if there is +* enough) to mutate the CRNG key to provide backtracking protection. +*/ +static void _crng_backtrack_protect (struct crng_state *crng, +__u8 tmp[CHACHA20_BLOCK_SIZE ],int used) +{ +unsigned long flags; +__u32 *s, *d; +int i; + + used =round_up (used,sizeof (__u32)); +if (used +CHACHA20_KEY_SIZE >CHACHA20_BLOCK_SIZE ) { +extract_crng (tmp); + used =0 ; + } +spin_lock_irqsave (&crng->lock, flags); + s = (__u32 *) &tmp[used]; + d = &crng->state[4 ]; +for (i=0 ; i <8 ; i++) + *d++ ^= *s++; +spin_unlock_irqrestore (&crng->lock, flags); +} + +static void crng_backtrack_protect (__u8 tmp[CHACHA20_BLOCK_SIZE ],int used) +{ +#ifdef CONFIG_NUMA +struct crng_state *crng = crng_node_pool[numa_node_id ()]; +#else +struct crng_state *crng = &primary_crng; +#endif + +_crng_backtrack_protect (crng, tmp, used); +} +static ssize_t extract_crng_user (void __user *buf,size_t nbytes) { -ssize_t ret =0 , i; +ssize_t ret =0 , i =CHACHA20_BLOCK_SIZE ;__u8 tmp[CHACHA20_BLOCK_SIZE ];int large_request = (nbytes >256 );
@@ -920,6 +959,7 @@ static ssize_t extract_crng_user(void __user *buf, size_t nbytes) buf += i; ret += i; } +crng_backtrack_protect (tmp, i);/* Wipe data just written to memory */ memzero_explicit (tmp,sizeof (tmp));
@@ -1473,8 +1513,10 @@ void get_random_bytes(void *buf, int nbytes)if (nbytes >0 ) {extract_crng (tmp);memcpy (buf, tmp, nbytes); -memzero_explicit (tmp, nbytes); - } +crng_backtrack_protect (tmp, nbytes); + }else +crng_backtrack_protect (tmp,CHACHA20_BLOCK_SIZE ); +memzero_explicit (tmp,sizeof (tmp)); }EXPORT_SYMBOL (get_random_bytes);
Signature · 1 line
2.5.0
Theodore Ts'o
maintainerPATCH v3 3/5 random: add interrupt callback to VMBus IRQ handler
In reply to Theodore Ts'oFrom: Stephan Mueller <[email protected]>
The Hyper-V Linux Integration Services use the VMBus implementation for communication with the Hypervisor. VMBus registers its own interrupt handler that completely bypasses the common Linux interrupt handling. This implies that the interrupt entropy collector is not triggered.
This patch adds the interrupt entropy collection callback into the VMBus interrupt handler function.
--- drivers/char/random.c | 1 + drivers/hv/vmbus_drv.c | 3 +++ 2 files changed, 4 insertions(+)
drivers/char/random.c +1 -0
@@ -1141,6 +1141,7 @@ void add_interrupt_randomness(int irq, int irq_flags)/* award one bit for the contents of the fast pool */ credit_entropy_bits (r, credit +1 ); } +EXPORT_SYMBOL_GPL (add_interrupt_randomness);#ifdef CONFIG_BLOCK void add_disk_randomness (struct gendisk *disk)
drivers/hv/vmbus_drv.c +3 -0
@@ -42,6 +42,7 @@#include <linux/screen_info.h> #include <linux/kdebug.h> #include <linux/efi.h> +#include <linux/random.h> #include "hyperv_vmbus.h" static struct acpi_device *hv_acpi_dev;
@@ -806,6 +807,8 @@ static void vmbus_isr(void)else tasklet_schedule (hv_context.msg_dpc[cpu]); } + +add_interrupt_randomness (HYPERVISOR_CALLBACK_VECTOR ,0 ); }
Signature · 1 line
2.5.0
Re: [PATCH 2/5] random: make /dev/urandom scalable for silly userspace programs
In reply to Theodore Ts'oAm Montag, 30. Mai 2016, 01:39:22 schrieb Theodore Ts'o:
Hi Theodore,
Quoted 70 lines
On a system with a 4 socket (NUMA) system where a large number of application threads were all trying to read from
/dev/urandom, this can result in the system spending 80% of its time contending on the global urandom spinlock. The application should have used its own PRNG, but let's try to help it from running, lemming-like, straight over the locking cliff.Reported-by Andi Kleen <[email protected]>Signed-off-by Theodore Ts'o <[email protected]>--- drivers/char/random.c | 57+++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 4 deletions(-)
drivers/char/random.c +13
-0@@ -434,6 +434,8 @@ struct crng_state primary_crng = {*/ static int crng_init =0 ;#define crng_ready () (likely(crng_init >= 2)) +static void _extract_crng (struct crng_state *crng, +__u8 out[CHACHA20_BLOCK_SIZE ]);static void extract_crng (__u8 out[CHACHA20_BLOCK_SIZE ]);static void process_random_ready_list (void );@@ -754,6 +756,17 @@ static void credit_entropy_bits_safe(struct entropy_store *r, int nbits) static DECLARE_WAIT_QUEUE_HEAD(crng_init_wait); +#ifdef CONFIG_NUMA +/* +* Hack to deal with crazy userspace progams when they are all trying +* to access /dev/urandom in parallel. The programs are almost +* certainly doing something terribly wrong, but we'll work around +* their brain damage. +*/ +static struct crng_state **crng_node_pool__read_mostly ; +#endif + +static void _initialize_crng (struct crng_state *crng) {drivers/char/random.c +3
-1int i;@@ -774,11 +787,13 @@ static void _initialize_crng(struct crng_state *crng) crng->init_time = jiffies -CRNG_RESEED_INTERVAL -1 ; } +#ifdef CONFIG_NUMA static void initialize_crng (struct crng_state *crng) {_initialize_crng (crng);spin_lock_init (&crng->lock); } +#endif static int crng_fast_load (__u32 pool[4 ]) {@@ -818,7 +833,7 @@ static void crng_reseed(struct crng_state *crng, struct entropy_store *r) if (num == 0)return ; }else -extract_crng (buf.block); +_extract_crng (&primary_crng, buf.block);spin_lock_irqsave (&primary_crng.lock, flags);for (i =0 ; i <8 ; i++) {unsigned long rv;
drivers/char/random.c static void crng_reseed(struct crng_state *crng, · line 859
Quoted 7 lines
drivers/char/random.c +4
-0@@ -838,19 +853,26 @@ static void crng_reseed(struct crng_state *crng, struct entropy_store *r) spin_unlock_irqrestore(&primary_crng.lock, flags); } +static inline void maybe_reseed_primary_crng(void) +{ + if (crng_init > 2 && + time_after(jiffies, primary_crng.init_time +
CRNG_RESEED_INTERVAL))
Quoted 46 lines
drivers/char/random.c +19
-3+crng_reseed (&primary_crng, &input_pool); +} +static inline void crng_wait_ready (void ) {wait_event_interruptible (crng_init_wait,crng_ready ()); } -static void extract_crng (__u8 out[CHACHA20_BLOCK_SIZE ]) +static void _extract_crng (struct crng_state *crng, +__u8 out[CHACHA20_BLOCK_SIZE ]) {unsigned long v, flags; -struct crng_state *crng = &primary_crng;if (crng_init >2 &&time_after (jiffies, crng->init_time +CRNG_RESEED_INTERVAL )) -crng_reseed (crng, &input_pool); +crng_reseed (crng, crng == &primary_crng ? &input_pool :NULL );spin_lock_irqsave (&crng->lock, flags);if (arch_get_random_long (&v)) crng->state[14 ] ^= v;@@ -860,6 +882,17 @@ static void extract_crng(__u8 out[CHACHA20_BLOCK_SIZE])spin_unlock_irqrestore (&crng->lock, flags); } +static void extract_crng (__u8 out[CHACHA20_BLOCK_SIZE ]) +{ +#ifndef CONFIG_NUMA +struct crng_state *crng = &primary_crng; +#else +struct crng_state *crng = crng_node_pool[numa_node_id ()]; +#endif + +_extract_crng (crng, out); +} +static ssize_t extract_crng_user (void __user *buf,size_t nbytes) {ssize_t ret =0 , i;@@ -1573,6 +1606,22 @@ static void init_std_data(struct entropy_store *r)*/ static int rand_initialize (void ) { +#ifdef CONFIG_NUMA +int i;
drivers/char/random.c static void init_std_data(struct entropy_store *r) · line 1620
Quoted 10 lines
drivers/char/random.c +10
-0+int num_nodes =num_possible_nodes (); +struct crng_state *crng; + + crng_node_pool =kmalloc (num_nodes *sizeof (void *), +GFP_KERNEL |__GFP_NOFAIL); + +for (i=0 ; i < num_nodes; i++) { + crng =kmalloc (sizeof (struct crng_state ), +GFP_KERNEL | __GFP_NOFAIL); +initialize_crng (crng);
Could you please help me understand the logic flow here: The NUMA secondary DRNGs are initialized before the input/blocking pools and the primary DRNG.
The initialization call uses get_random_bytes() for the secondary DRNGs. But since the primary DRNG is not yet initialized, where does the get_random_bytes gets its randomness from?
drivers/char/random.c static void init_std_data(struct entropy_store *r) · line 1627
Quoted 7 lines
drivers/char/random.c +4
-0+ crng_node_pool[i] = crng; + + } +#endif init_std_data (&input_pool);init_std_data (&blocking_pool);_initialize_crng (&primary_crng);
Ciao
Stephan
Theodore Ts'o
maintainerRe: [PATCH 2/5] random: make /dev/urandom scalable for silly userspace programs
Quoted 18 lines · Stephan Mueller
Quoted 4 lines
static int
rand_initialize(void)
{drivers/char/random.c +2
-0+#ifdef CONFIG_NUMA +int i;drivers/char/random.c static void init_std_data(struct entropy_store *r) · line 1620
Quoted 10 lines
drivers/char/random.c +10
-0+int num_nodes =num_possible_nodes (); +struct crng_state *crng; + + crng_node_pool =kmalloc (num_nodes *sizeof (void *), +GFP_KERNEL |__GFP_NOFAIL); + +for (i=0 ; i < num_nodes; i++) { + crng =kmalloc (sizeof (struct crng_state ), +GFP_KERNEL | __GFP_NOFAIL); +initialize_crng (crng);Could you please help me understand the logic flow here: The NUMA secondary DRNGs are initialized before the input/blocking pools and the primary DRNG.
Quoted 3 lines
The initialization call uses
get_random_bytes()for the secondary DRNGs. But since the primary DRNG is not yet initialized, where does theget_random_bytesgets its randomness from?
Yeah, I screwed up. The hunk of code staring with "crng_node_pool = kmalloc(..." and the for loop afterwards should be moved to after _initialize_crng(). Serves me right for not testing CONFIG_NUMA before sending out the patches.
This is *not* about adding entropy; as you've noted, this is done very early in boot up, before there has been any chance for any kind of entropy to be collected in any of the input pools. It's more of a insurance policy just in case on some platform, if it turns out that assuming a bit's worth of entropy per interrupt was hopelessly over-optimistic, at least the starting point will be different across different kernels (and maybe different boot times, but on the sorts of platforms where I'm most concerned, there may not be a real-time clock and almost certainly not a architectural hwrng in the CPU).
- Ted
Quoted 4 lines
In addition, on NUMA systems we make the CRNG state per-NUMA socket, to address the NUMA locking contention problem which Andi Kleen has been complaining about. I'm not entirely sure this will work well on the crazy big SGI systems, but they are rare. Whether they are rarer than
It should work the same on larger systems, the solution scales naturally to lots of sockets. It's not clear it'll help enough on systems with a lot more cores per socket, like a Xeon Phi. But for now it should be good enough.
-Andi
Theodore Ts'o
maintainerQuoted 5 lines · Andi Kleen
It should work the same on larger systems, the solution scales naturally to lots of sockets. It's not clear it'll help enough on systems with a lot more cores per socket, like a Xeon Phi. But for now it should be good enough.
One change which I'm currently making is to use kmalloc_node() instead of kmalloc() for the per-NUMA node, and I suspect *that* is going to make a quite a lot of different on those systems where the ratio of remote to local memory access times is larger (as I assume it probably would be on really big systems).
- Ted