Skip to content

Commit a890d1c

Browse files
committed
random: clear new batches when bringing new CPUs online
The commit that added the new get_random_{u8,u16}() functions neglected to update the code that clears the batches when bringing up a new CPU. It also forgot a few comments and helper defines, so add those in too. Fixes: 585cd5f ("random: add 8-bit and 16-bit batches") Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
1 parent d687772 commit a890d1c

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

drivers/char/random.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ MODULE_PARM_DESC(ratelimit_disable, "Disable random ratelimit suppression");
9696
/*
9797
* Returns whether or not the input pool has been seeded and thus guaranteed
9898
* to supply cryptographically secure random numbers. This applies to: the
99-
* /dev/urandom device, the get_random_bytes function, and the get_random_{u32,
100-
* ,u64,int,long} family of functions.
99+
* /dev/urandom device, the get_random_bytes function, and the get_random_{u8,
100+
* u16,u32,u64,int,long} family of functions.
101101
*
102102
* Returns: true if the input pool has been seeded.
103103
* false if the input pool has not been seeded.
@@ -119,9 +119,9 @@ static void try_to_generate_entropy(void);
119119
/*
120120
* Wait for the input pool to be seeded and thus guaranteed to supply
121121
* cryptographically secure random numbers. This applies to: the /dev/urandom
122-
* device, the get_random_bytes function, and the get_random_{u32,u64,int,long}
123-
* family of functions. Using any of these functions without first calling
124-
* this function forfeits the guarantee of security.
122+
* device, the get_random_bytes function, and the get_random_{u8,u16,u32,u64,
123+
* int,long} family of functions. Using any of these functions without first
124+
* calling this function forfeits the guarantee of security.
125125
*
126126
* Returns: 0 if the input pool has been seeded.
127127
* -ERESTARTSYS if the function was interrupted by a signal.
@@ -157,17 +157,19 @@ EXPORT_SYMBOL(wait_for_random_bytes);
157157
* There are a few exported interfaces for use by other drivers:
158158
*
159159
* void get_random_bytes(void *buf, size_t len)
160+
* u8 get_random_u8()
161+
* u16 get_random_u16()
160162
* u32 get_random_u32()
161163
* u64 get_random_u64()
162164
* unsigned int get_random_int()
163165
* unsigned long get_random_long()
164166
*
165167
* These interfaces will return the requested number of random bytes
166168
* into the given buffer or as a return value. This is equivalent to
167-
* a read from /dev/urandom. The u32, u64, int, and long family of
168-
* functions may be higher performance for one-off random integers,
169-
* because they do a bit of buffering and do not invoke reseeding
170-
* until the buffer is emptied.
169+
* a read from /dev/urandom. The u8, u16, u32, u64, int, and long
170+
* family of functions may be higher performance for one-off random
171+
* integers, because they do a bit of buffering and do not invoke
172+
* reseeding until the buffer is emptied.
171173
*
172174
*********************************************************************/
173175

@@ -504,10 +506,10 @@ type get_random_ ##type(void) \
504506
} \
505507
EXPORT_SYMBOL(get_random_ ##type);
506508

507-
DEFINE_BATCHED_ENTROPY(u64)
508-
DEFINE_BATCHED_ENTROPY(u32)
509-
DEFINE_BATCHED_ENTROPY(u16)
510509
DEFINE_BATCHED_ENTROPY(u8)
510+
DEFINE_BATCHED_ENTROPY(u16)
511+
DEFINE_BATCHED_ENTROPY(u32)
512+
DEFINE_BATCHED_ENTROPY(u64)
511513

512514
#ifdef CONFIG_SMP
513515
/*
@@ -522,6 +524,8 @@ int __cold random_prepare_cpu(unsigned int cpu)
522524
* randomness.
523525
*/
524526
per_cpu_ptr(&crngs, cpu)->generation = ULONG_MAX;
527+
per_cpu_ptr(&batched_entropy_u8, cpu)->position = UINT_MAX;
528+
per_cpu_ptr(&batched_entropy_u16, cpu)->position = UINT_MAX;
525529
per_cpu_ptr(&batched_entropy_u32, cpu)->position = UINT_MAX;
526530
per_cpu_ptr(&batched_entropy_u64, cpu)->position = UINT_MAX;
527531
return 0;

include/linux/random.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ static inline int get_random_bytes_wait(void *buf, size_t nbytes)
9696
*out = get_random_ ## name(); \
9797
return 0; \
9898
}
99+
declare_get_random_var_wait(u8, u8)
100+
declare_get_random_var_wait(u16, u16)
99101
declare_get_random_var_wait(u32, u32)
100102
declare_get_random_var_wait(u64, u32)
101103
declare_get_random_var_wait(int, unsigned int)

0 commit comments

Comments
 (0)