random: PREEMPT_RT fixes
Subsystems (2)
Jason A. Donenfeld
maintainerv3 cover random: PREEMPT_RT fixes
Here are the two patches we've been looking at thus far in separate threads, now brought together as one patchset. This doesn't fix _all_ of the PREEMPT_RT issues, but it does very much move in the right direction of having less locking in places where that matters.
Jason A. Donenfeld (2):
random: remove batched entropy locking
random: defer fast pool mixing to worker
drivers/char/random.c | 101 +++++++++++++++++++--------------- include/trace/events/random.h | 6 -- 2 files changed, 56 insertions(+), 51 deletions(-)
Signature · 1 line
2.35.0
Jason A. Donenfeld
maintainerPATCH v3 1/2 random: remove batched entropy locking
Rather than use spinlocks to protect batched entropy, we can instead disable interrupts locally, since we're dealing with per-cpu data, and manage resets with a basic generation counter. At the same time, we can't quite do this on PREEMPT_RT, where we still want spinlocks-as- mutexes semantics. So we use a local_lock_t, which provides the right behavior for each. Because this is a per-cpu lock, that generation counter is still doing the necessary CPU-to-CPU communication.
This should improve performance a bit. It will also fix the linked splat that Jonathan received with a PROVE_RAW_LOCK_NESTING=y.
--- drivers/char/random.c | 51 ++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 25 deletions(-)
drivers/char/random.c +26 -25
@@ -1731,13 +1731,16 @@ static int __init random_sysctls_init(void)device_initcall (random_sysctls_init);#endif /* CONFIG_SYSCTL */ +static atomic_t batch_generation =ATOMIC_INIT (0 ); +struct batched_entropy {union {u64 entropy_u64[CHACHA_BLOCK_SIZE /sizeof (u64)];u32 entropy_u32[CHACHA_BLOCK_SIZE /sizeof (u32)]; }; +local_lock_t lock;unsigned int position; -spinlock_t batch_lock; +int generation; };/*
@@ -1749,7 +1752,7 @@ struct batched_entropy {* point prior. */ static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u64) = { - .batch_lock =__SPIN_LOCK_UNLOCKED (batched_entropy_u64.lock), + .lock =INIT_LOCAL_LOCK (batched_entropy_u64.lock) };u64 get_random_u64 (void )
@@ -1758,41 +1761,54 @@ u64 get_random_u64(void)unsigned long flags;struct batched_entropy *batch;static void *previous; +int next_gen;warn_unseeded_randomness (&previous); +local_lock_irqsave (&batched_entropy_u64.lock, flags); batch =raw_cpu_ptr (&batched_entropy_u64); -spin_lock_irqsave (&batch->batch_lock, flags); -if (batch->position %ARRAY_SIZE (batch->entropy_u64) ==0 ) { + + next_gen =atomic_read (&batch_generation); +if (batch->position %ARRAY_SIZE (batch->entropy_u64) ==0 || + next_gen != batch->generation) {extract_crng ((u8 *)batch->entropy_u64); batch->position =0 ; + batch->generation = next_gen; } + ret = batch->entropy_u64[batch->position++]; -spin_unlock_irqrestore (&batch->batch_lock, flags); +local_unlock_irqrestore (&batched_entropy_u64.lock, flags);return ret; }EXPORT_SYMBOL (get_random_u64); static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u32) = { - .batch_lock =__SPIN_LOCK_UNLOCKED (batched_entropy_u32.lock), + .lock =INIT_LOCAL_LOCK (batched_entropy_u32.lock) }; +u32 get_random_u32 (void ) {u32 ret;unsigned long flags;struct batched_entropy *batch;static void *previous; +int next_gen;warn_unseeded_randomness (&previous); +local_lock_irqsave (&batched_entropy_u32.lock, flags); batch =raw_cpu_ptr (&batched_entropy_u32); -spin_lock_irqsave (&batch->batch_lock, flags); -if (batch->position %ARRAY_SIZE (batch->entropy_u32) ==0 ) { + + next_gen =atomic_read (&batch_generation); +if (batch->position %ARRAY_SIZE (batch->entropy_u32) ==0 || + next_gen != batch->generation) {extract_crng ((u8 *)batch->entropy_u32); batch->position =0 ; + batch->generation = next_gen; } + ret = batch->entropy_u32[batch->position++]; -spin_unlock_irqrestore (&batch->batch_lock, flags); +local_unlock_irqrestore (&batched_entropy_u32.lock, flags);return ret; }EXPORT_SYMBOL (get_random_u32);
@@ -1803,22 +1819,7 @@ EXPORT_SYMBOL(get_random_u32);* next usage. */ static void invalidate_batched_entropy (void ) { -int cpu; -unsigned long flags; - -for_each_possible_cpu (cpu) { -struct batched_entropy *batched_entropy; - - batched_entropy =per_cpu_ptr (&batched_entropy_u32, cpu); -spin_lock_irqsave (&batched_entropy->batch_lock, flags); - batched_entropy->position =0 ; -spin_unlock (&batched_entropy->batch_lock); - - batched_entropy =per_cpu_ptr (&batched_entropy_u64, cpu); -spin_lock (&batched_entropy->batch_lock); - batched_entropy->position =0 ; -spin_unlock_irqrestore (&batched_entropy->batch_lock, flags); - } +atomic_inc (&batch_generation); }/**
Signature · 1 line
2.35.0
Re: [PATCH v3 1/2] random: remove batched entropy locking
Quoted 18 lines · Jason A. Donenfeld
Rather than use spinlocks to protect batched entropy, we can instead disable interrupts locally, since we're dealing with per-cpu data, and manage resets with a basic generation counter. At the same time, we can't quite do this on
PREEMPT_RT, where we still want spinlocks-as- mutexes semantics. So we use alocal_lock_t, which provides the right behavior for each. Because this is a per-cpu lock, that generation counter is still doing the necessary CPU-to-CPU communication.This should improve performance a bit. It will also fix the linked splat that Jonathan received with a PROVE_RAW_LOCK_NESTING=y.
Suggested-by Andy Lutomirski <[email protected]>Reported-by Jonathan Neuschäfer <[email protected]>Tested-by Jonathan Neuschäfer <[email protected]>Cc Sebastian Andrzej Siewior <[email protected]>Cc Sultan Alsawaf <[email protected]>Signed-off-by Jason A. Donenfeld <[email protected]>
Quoted 3 lines
--- drivers/char/random.c | 51 ++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 25 deletions(-)
Looks good. The comment above invalidate_batched_entropy() needs to be updated, though.
Also, please Cc patches for random.c to linux-crypto.
- Eric
Jason A. Donenfeld
maintainerRe: [PATCH v3 1/2] random: remove batched entropy locking
Quoted 2 lines · Eric Biggers
Looks good. The comment above
invalidate_batched_entropy()needs to be updated, though.
Fixed, thanks.
Jason A. Donenfeld
maintainerPATCH v3 2/2 random: defer fast pool mixing to worker
In reply to Jason A. DonenfeldOn PREEMPT_RT, it's problematic to take spinlocks from hard irq handlers. We can fix this by deferring to a work queue the dumping of the fast pool into the input pool.
We accomplish this with some careful rules on fast_pool->count:
- When it's incremented to >= 64, we schedule the work.
- If the top bit is set, we never schedule the work, even if >= 64.
- The worker is responsible for setting it back to 0 when it's done.
In the worst case, an irq handler is mixing a new irq into the pool at the same time as the worker is dumping it into the input pool. In this case, we only ever set the count back to 0 _after_ we're done, so that subsequent cycles will require a full 64 to dump it in again. In other words, the result of this race is only ever adding a little bit more information than normal, but never less, and never crediting any more for this partial additional information.
--- drivers/char/random.c | 50 +++++++++++++++++++++-------------- include/trace/events/random.h | 6 ----- 2 files changed, 30 insertions(+), 26 deletions(-)
drivers/char/random.c +30 -20
@@ -377,12 +377,6 @@ static void _mix_pool_bytes(const void *in, int nbytes)blake2s_update (&input_pool.hash, in, nbytes); } -static void __mix_pool_bytes (const void *in,int nbytes) -{ -trace_mix_pool_bytes_nolock (nbytes, _RET_IP_); -_mix_pool_bytes (in, nbytes); -} -static void mix_pool_bytes (const void *in,int nbytes) {unsigned long flags;
@@ -394,11 +388,13 @@ static void mix_pool_bytes(const void *in, int nbytes) }struct fast_pool { -u32 pool[4 ]; +struct work_struct mix;unsigned long last; +u32 pool[4 ]; +unsigned int count;u16 reg_idx; -u8 count; }; +#define FAST_POOL_MIX_INFLIGHT (1U << 31)/* * This is a fast mixing routine used by the interrupt randomness
@@ -428,7 +424,6 @@ static void fast_mix(struct fast_pool *f) f->pool[0 ] = a; f->pool[1 ] = b; f->pool[2 ] = c; f->pool[3 ] = d; - f->count++; }static void process_random_ready_list (void )
@@ -977,12 +972,30 @@ static u32 get_reg(struct fast_pool *f, struct pt_regs *regs)return *ptr; } +static void mix_interrupt_randomness (struct work_struct *work) +{ + struct fast_pool *fast_pool = container_of(work, struct fast_pool, mix); + +/* +* Since this is the result of a trip through the scheduler, xor in +* a cycle counter. It can't hurt, and might help. +*/ + fast_pool->pool[3 ] ^=random_get_entropy (); + +mix_pool_bytes (&fast_pool->pool,sizeof (fast_pool->pool)); +/* We take care to zero out the count only after we're done reading the pool. */ +WRITE_ONCE (fast_pool->count,0 ); + fast_pool->last = jiffies; +credit_entropy_bits (1 ); +} +void add_interrupt_randomness (int irq) {struct fast_pool *fast_pool =this_cpu_ptr (&irq_randomness);struct pt_regs *regs =get_irq_regs ();unsigned long now = jiffies;cycles_t cycles =random_get_entropy (); +unsigned int new_count;u32 c_high, j_high;u64 ip;
@@ -999,9 +1012,10 @@ void add_interrupt_randomness(int irq)fast_mix (fast_pool);add_interrupt_bench (cycles); + new_count = ++fast_pool->count;if (unlikely (crng_init ==0 )) { -if ((fast_pool->count >=64 ) && +if (new_count >=64 &&crng_fast_load ((u8 *)fast_pool->pool,sizeof (fast_pool->pool)) >0 ) { fast_pool->count =0 ; fast_pool->last = now;
@@ -1009,20 +1023,16 @@ void add_interrupt_randomness(int irq)return ; } -if ((fast_pool->count <64 ) && !time_after (now, fast_pool->last +HZ )) +if (new_count &FAST_POOL_MIX_INFLIGHT )return ; -if (!spin_trylock (&input_pool.lock)) +if (new_count <64 && !time_after (now, fast_pool->last +HZ ))return ; - fast_pool->last = now; -__mix_pool_bytes (&fast_pool->pool,sizeof (fast_pool->pool)); -spin_unlock (&input_pool.lock); - - fast_pool->count =0 ; - -/* award one bit for the contents of the fast pool */ -credit_entropy_bits (1 ); +if (unlikely (!fast_pool->mix.func)) +INIT_WORK (&fast_pool->mix, mix_interrupt_randomness); + fast_pool->count |=FAST_POOL_MIX_INFLIGHT ; +queue_work_on (raw_smp_processor_id (), system_highpri_wq, &fast_pool->mix); }EXPORT_SYMBOL_GPL (add_interrupt_randomness);
include/trace/events/random.h +0 -6
@@ -52,12 +52,6 @@ DEFINE_EVENT(random__mix_pool_bytes, mix_pool_bytes,TP_ARGS (bytes,IP ) ); -DEFINE_EVENT (random__mix_pool_bytes, mix_pool_bytes_nolock, - TP_PROTO(int bytes, unsigned long IP), - -TP_ARGS (bytes,IP ) -); - TRACE_EVENT(credit_entropy_bits, TP_PROTO(int bits, int entropy_count, unsigned long IP),
Signature · 1 line
2.35.0
Re: [PATCH v3 2/2] random: defer fast pool mixing to worker
Quoted 6 lines · Jason A. Donenfeld
drivers/char/random.c +6
-0+static void mix_interrupt_randomness (struct work_struct *work) +{ + struct fast_pool *fast_pool = container_of(work, struct fast_pool, mix); + +/* +* Since this is the result of a trip through the scheduler, xor in
drivers/char/random.c static u32 get_reg(struct fast_pool *f, struct pt_regs *regs) · line 990
Quoted 10 lines
drivers/char/random.c +10
-0+* a cycle counter. It can't hurt, and might help. +*/ + fast_pool->pool[3 ] ^=random_get_entropy (); + +mix_pool_bytes (&fast_pool->pool,sizeof (fast_pool->pool)); +/* We take care to zero out the count only after we're done reading the pool. */ +WRITE_ONCE (fast_pool->count,0 ); + fast_pool->last = jiffies; +credit_entropy_bits (1 ); +}
So, add_interrupt_randomness() can execute on the same CPU re-entrantly at any time this is executing? That could result in some pretty weird behavior, where the pool gets changed half-way through being used, so what is used is neither the old nor the new state of the pool. Is there a reason why this is okay?
Quoted 37 lines
void
add_interrupt_randomness(int irq)
{drivers/char/random.c +5
-6struct fast_pool *fast_pool =this_cpu_ptr (&irq_randomness);struct pt_regs *regs =get_irq_regs ();unsigned long now = jiffies;cycles_t cycles =random_get_entropy (); +unsigned int new_count;u32 c_high, j_high;u64 ip;@@ -999,9 +1012,10 @@ void add_interrupt_randomness(int irq)fast_mix (fast_pool);add_interrupt_bench (cycles); + new_count = ++fast_pool->count;if (unlikely (crng_init ==0 )) { -if ((fast_pool->count >=64 ) && +if (new_count >=64 &&crng_fast_load ((u8 *)fast_pool->pool,sizeof (fast_pool->pool)) >0 ) { fast_pool->count =0 ; fast_pool->last = now;@@ -1009,20 +1023,16 @@ void add_interrupt_randomness(int irq)return ; } -if ((fast_pool->count <64 ) && !time_after (now, fast_pool->last +HZ )) +if (new_count &FAST_POOL_MIX_INFLIGHT )return ; -if (!spin_trylock (&input_pool.lock)) +if (new_count <64 && !time_after (now, fast_pool->last +HZ ))return ; - fast_pool->last = now; -__mix_pool_bytes (&fast_pool->pool,sizeof (fast_pool->pool)); -spin_unlock (&input_pool.lock);
drivers/char/random.c void add_interrupt_randomness(int irq) · line 1036
Quoted 10 lines
drivers/char/random.c +4
-5- - fast_pool->count =0 ; - -/* award one bit for the contents of the fast pool */ -credit_entropy_bits (1 ); +if (unlikely (!fast_pool->mix.func)) +INIT_WORK (&fast_pool->mix, mix_interrupt_randomness); + fast_pool->count |=FAST_POOL_MIX_INFLIGHT ; +queue_work_on (raw_smp_processor_id (), system_highpri_wq, &fast_pool->mix); }
Is there a reason why the FAST_POOL_MIX_INFLIGHT is part of 'count' instead of a separate boolean?
Also, a high level question. Now that the call to mix_pool_bytes() would no longer occur in hard IRQ context, how much reason is there to minimize the amount of data passed to it? Would it be feasible to just concatenate the interrupt data into an array, and pass the whole array to mix_pool_bytes() -- eliminating the homebrew ARX thing entirely?
- Eric
Jason A. Donenfeld
maintainerRe: [PATCH v3 2/2] random: defer fast pool mixing to worker
Hi Eric,
Quoted 4 lines · Eric Biggers
So,
add_interrupt_randomness()can execute on the same CPU re-entrantly at any time this is executing? That could result in some pretty weird behavior, where the pool gets changed half-way through being used, so what is used is neither the old nor the new state of the pool. Is there a reason why this is okay?
Yes, right, that's the "idea" of this patch, if you could call it such a thing. The argument is that we set fast_pool->count to zero *after* mixing in the existing bytes + whatever partial bytes might be mixed in on an interrupt halfway through the execution of mix_pool_bytes. Since we set the count to zero after, it means we do not give any credit to those partial bytes for the following set of 64 interrupts. What winds up being mixed in will contain at least as much as what was there had it not been interrupted. And what gets mixed in the next time will only have more mixed in than it otherwise would have, not less.
Quoted 2 lines
Is there a reason why the
FAST_POOL_MIX_INFLIGHTis part of 'count' instead of a separate boolean?
So that we can clear it with a single WRITE_ONCE, and to save space in the per-cpu crng.
Quoted 5 lines
Also, a high level question. Now that the call to
mix_pool_bytes()would no longer occur in hard IRQ context, how much reason is there to minimize the amount of data passed to it? Would it be feasible to just concatenate the interrupt data into an array, and pass the whole array tomix_pool_bytes()-- eliminating the homebrew ARX thing entirely?
Indeed I'm working on replacing fast_mix() with something we can actually reason about. I thought about a big array but I'm not quite convinced that the memory overhead of this would be worth it. Right now, each interrupt generates 16 bytes of data, and we ingest that data after 64ish interrupts -- but sometimes more (and sometimes less). So that's a whole kilobyte in the best case. That's not /tons/ but it's not nothing either. So while the big array idea is one possibility, it's not the only one I'm evaluating. Hopefully in the coming weeks I'll have some ideas ready to discuss on that.
Jason
Jason A. Donenfeld
maintainerRe: [PATCH v3 2/2] random: defer fast pool mixing to worker
Quoted 6 lines · Jason A. Donenfeld
Quoted 4 lines · Eric Biggers
So,
add_interrupt_randomness()can execute on the same CPU re-entrantly at any time this is executing? That could result in some pretty weird behavior, where the pool gets changed half-way through being used, so what is used is neither the old nor the new state of the pool. Is there a reason why this is okay?
Quoted 10 lines
Yes, right, that's the "idea" of this patch, if you could call it such a thing. The argument is that we set
fast_pool->countto zero *after* mixing in the existing bytes + whatever partial bytes might be mixed in on an interrupt halfway through the execution ofmix_pool_bytes. Since we set the count to zero after, it means we do not give any credit to those partial bytes for the following set of 64 interrupts. What winds up being mixed in will contain at least as much as what was there had it not been interrupted. And what gets mixed in the next time will only have more mixed in than it otherwise would have, not less.
I can actually make it even better by memcpy()ing the fast pool first. This way any races only affect the fast_mix side -- harmless as described above -- without affecting blake2s. The latter was _already_ buffering it, but memcpy()ing makes it more clear and doesn't rely on that behavior. It also means that we get to set count to zero a bit sooner.