Skip to contentSkip to conversation

random: avoid reading two cache lines on irq randomness

PATCH In review Jason A. Donenfeld 2 participants 2 messages Sep 23, 2022 – Sep 28, 2022 Random number driver

lore
Share & tools
lore

Jason A. Donenfeld

author maintainer
#0 lore raw

PATCH 1/1 random: avoid reading two cache lines on irq randomness

In order to avoid reading and dirtying two cache lines on every IRQ, move the work_struct to the bottom of the fast_pool struct. add_interrupt_ randomness() always touches .pool and .count, which are currently split, because .mix pushes everything down. Instead, move .mix to the bottom, so that .pool and .count are always in the first cache line, since the .mix is only accessed when the pool is full.

Fixes 58340f8e952b ("random: defer fast pool mixing to worker")
Cc Sebastian Andrzej Siewior <[email protected]>
Cc Sultan Alsawaf <[email protected]>
---
 drivers/char/random.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/random.c +1 -1
index 2f370aa248b2..a90d96f4b3bb 100644
@@ -918,10 +918,10 @@ EXPORT_SYMBOL_GPL(unregister_random_vmfork_notifier);
 #endif
 
 struct fast_pool {
-	struct work_struct mix;
 	unsigned long pool[4];
 	unsigned long last;
 	unsigned int count;
+	struct work_struct mix;
 };
 
 static DEFINE_PER_CPU(struct fast_pool, irq_randomness) = {
Signature · 1 line
2.37.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Jason A. Donenfeld

In order to avoid reading and dirtying two cache lines on every IRQ, move the work_struct to the bottom of the fast_pool struct. add_interrupt_ randomness() always touches .pool and .count, which are currently split, because .mix pushes everything down. Instead, move .mix to the bottom, so that .pool and .count are always in the first cache line, since the .mix is only accessed when the pool is full.

Fixes 58340f8e952b ("random: defer fast pool mixing to worker")
Cc Sebastian Andrzej Siewior <[email protected]>
Cc Sultan Alsawaf <[email protected]>

Yup, makes sense.

Sebastian