@@ -430,8 +430,6 @@ static DEFINE_PER_CPU(struct crng, crngs) = {
430
430
431
431
static DECLARE_WAIT_QUEUE_HEAD (crng_init_wait );
432
432
433
- static void invalidate_batched_entropy (void );
434
-
435
433
/*
436
434
* crng_fast_load() can be called by code in the interrupt service
437
435
* path. So we can't afford to dilly-dally. Returns the number of
@@ -454,7 +452,7 @@ static size_t crng_fast_load(const void *cp, size_t len)
454
452
src ++ ; crng_init_cnt ++ ; len -- ; ret ++ ;
455
453
}
456
454
if (crng_init_cnt >= CRNG_INIT_CNT_THRESH ) {
457
- invalidate_batched_entropy () ;
455
+ ++ base_crng . generation ;
458
456
crng_init = 1 ;
459
457
}
460
458
spin_unlock_irqrestore (& base_crng .lock , flags );
@@ -531,7 +529,6 @@ static void crng_reseed(void)
531
529
WRITE_ONCE (base_crng .generation , next_gen );
532
530
WRITE_ONCE (base_crng .birth , jiffies );
533
531
if (crng_init < 2 ) {
534
- invalidate_batched_entropy ();
535
532
crng_init = 2 ;
536
533
finalize_init = true;
537
534
}
@@ -1256,8 +1253,9 @@ int __init rand_initialize(void)
1256
1253
mix_pool_bytes (utsname (), sizeof (* (utsname ())));
1257
1254
1258
1255
extract_entropy (base_crng .key , sizeof (base_crng .key ));
1256
+ ++ base_crng .generation ;
1257
+
1259
1258
if (arch_init && trust_cpu && crng_init < 2 ) {
1260
- invalidate_batched_entropy ();
1261
1259
crng_init = 2 ;
1262
1260
pr_notice ("crng init done (trusting CPU's manufacturer)\n" );
1263
1261
}
@@ -1607,8 +1605,6 @@ static int __init random_sysctls_init(void)
1607
1605
device_initcall (random_sysctls_init );
1608
1606
#endif /* CONFIG_SYSCTL */
1609
1607
1610
- static atomic_t batch_generation = ATOMIC_INIT (0 );
1611
-
1612
1608
struct batched_entropy {
1613
1609
union {
1614
1610
/*
@@ -1622,8 +1618,8 @@ struct batched_entropy {
1622
1618
u32 entropy_u32 [CHACHA_BLOCK_SIZE * 3 / (2 * sizeof (u32 ))];
1623
1619
};
1624
1620
local_lock_t lock ;
1621
+ unsigned long generation ;
1625
1622
unsigned int position ;
1626
- int generation ;
1627
1623
};
1628
1624
1629
1625
/*
@@ -1643,14 +1639,14 @@ u64 get_random_u64(void)
1643
1639
unsigned long flags ;
1644
1640
struct batched_entropy * batch ;
1645
1641
static void * previous ;
1646
- int next_gen ;
1642
+ unsigned long next_gen ;
1647
1643
1648
1644
warn_unseeded_randomness (& previous );
1649
1645
1650
1646
local_lock_irqsave (& batched_entropy_u64 .lock , flags );
1651
1647
batch = raw_cpu_ptr (& batched_entropy_u64 );
1652
1648
1653
- next_gen = atomic_read ( & batch_generation );
1649
+ next_gen = READ_ONCE ( base_crng . generation );
1654
1650
if (batch -> position >= ARRAY_SIZE (batch -> entropy_u64 ) ||
1655
1651
next_gen != batch -> generation ) {
1656
1652
_get_random_bytes (batch -> entropy_u64 , sizeof (batch -> entropy_u64 ));
@@ -1677,14 +1673,14 @@ u32 get_random_u32(void)
1677
1673
unsigned long flags ;
1678
1674
struct batched_entropy * batch ;
1679
1675
static void * previous ;
1680
- int next_gen ;
1676
+ unsigned long next_gen ;
1681
1677
1682
1678
warn_unseeded_randomness (& previous );
1683
1679
1684
1680
local_lock_irqsave (& batched_entropy_u32 .lock , flags );
1685
1681
batch = raw_cpu_ptr (& batched_entropy_u32 );
1686
1682
1687
- next_gen = atomic_read ( & batch_generation );
1683
+ next_gen = READ_ONCE ( base_crng . generation );
1688
1684
if (batch -> position >= ARRAY_SIZE (batch -> entropy_u32 ) ||
1689
1685
next_gen != batch -> generation ) {
1690
1686
_get_random_bytes (batch -> entropy_u32 , sizeof (batch -> entropy_u32 ));
@@ -1700,15 +1696,6 @@ u32 get_random_u32(void)
1700
1696
}
1701
1697
EXPORT_SYMBOL (get_random_u32 );
1702
1698
1703
- /* It's important to invalidate all potential batched entropy that might
1704
- * be stored before the crng is initialized, which we can do lazily by
1705
- * bumping the generation counter.
1706
- */
1707
- static void invalidate_batched_entropy (void )
1708
- {
1709
- atomic_inc (& batch_generation );
1710
- }
1711
-
1712
1699
/**
1713
1700
* randomize_page - Generate a random, page aligned address
1714
1701
* @start: The smallest acceptable address the caller will take.
0 commit comments