Skip to content

Commit b0c3e79

Browse files
committed
random: make random_get_entropy() return an unsigned long
Some implementations were returning type `unsigned long`, while others that fell back to get_cycles() were implicitly returning a `cycles_t` or an untyped constant int literal. That makes for weird and confusing code, and basically all code in the kernel already handled it like it was an `unsigned long`. I recently tried to handle it as the largest type it could be, a `cycles_t`, but doing so doesn't really help with much. Instead let's just make random_get_entropy() return an unsigned long all the time. This also matches the commonly used `arch_get_random_long()` function, so now RDRAND and RDTSC return the same sized integer, which means one can fallback to the other more gracefully. Cc: Dominik Brodowski <linux@dominikbrodowski.net> Cc: Theodore Ts'o <tytso@mit.edu> Acked-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
1 parent 5209aed commit b0c3e79

2 files changed

Lines changed: 8 additions & 14 deletions

File tree

drivers/char/random.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,7 @@ int __init rand_initialize(void)
10181018
*/
10191019
void add_device_randomness(const void *buf, size_t size)
10201020
{
1021-
cycles_t cycles = random_get_entropy();
1021+
unsigned long cycles = random_get_entropy();
10221022
unsigned long flags, now = jiffies;
10231023

10241024
if (crng_init == 0 && size)
@@ -1049,8 +1049,7 @@ struct timer_rand_state {
10491049
*/
10501050
static void add_timer_randomness(struct timer_rand_state *state, unsigned int num)
10511051
{
1052-
cycles_t cycles = random_get_entropy();
1053-
unsigned long flags, now = jiffies;
1052+
unsigned long cycles = random_get_entropy(), now = jiffies, flags;
10541053
long delta, delta2, delta3;
10551054

10561055
spin_lock_irqsave(&input_pool.lock, flags);
@@ -1339,8 +1338,7 @@ static void mix_interrupt_randomness(struct work_struct *work)
13391338
void add_interrupt_randomness(int irq)
13401339
{
13411340
enum { MIX_INFLIGHT = 1U << 31 };
1342-
cycles_t cycles = random_get_entropy();
1343-
unsigned long now = jiffies;
1341+
unsigned long cycles = random_get_entropy(), now = jiffies;
13441342
struct fast_pool *fast_pool = this_cpu_ptr(&irq_randomness);
13451343
struct pt_regs *regs = get_irq_regs();
13461344
unsigned int new_count;
@@ -1353,16 +1351,12 @@ void add_interrupt_randomness(int irq)
13531351
if (cycles == 0)
13541352
cycles = get_reg(fast_pool, regs);
13551353

1356-
if (sizeof(cycles) == 8)
1354+
if (sizeof(unsigned long) == 8) {
13571355
irq_data.u64[0] = cycles ^ rol64(now, 32) ^ irq;
1358-
else {
1356+
irq_data.u64[1] = regs ? instruction_pointer(regs) : _RET_IP_;
1357+
} else {
13591358
irq_data.u32[0] = cycles ^ irq;
13601359
irq_data.u32[1] = now;
1361-
}
1362-
1363-
if (sizeof(unsigned long) == 8)
1364-
irq_data.u64[1] = regs ? instruction_pointer(regs) : _RET_IP_;
1365-
else {
13661360
irq_data.u32[2] = regs ? instruction_pointer(regs) : _RET_IP_;
13671361
irq_data.u32[3] = get_reg(fast_pool, regs);
13681362
}
@@ -1409,7 +1403,7 @@ static void entropy_timer(struct timer_list *t)
14091403
static void try_to_generate_entropy(void)
14101404
{
14111405
struct {
1412-
cycles_t cycles;
1406+
unsigned long cycles;
14131407
struct timer_list timer;
14141408
} stack;
14151409

include/linux/timex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
* By default we use get_cycles() for this purpose, but individual
7676
* architectures may override this in their asm/timex.h header file.
7777
*/
78-
#define random_get_entropy() get_cycles()
78+
#define random_get_entropy() ((unsigned long)get_cycles())
7979
#endif
8080

8181
/*

0 commit comments

Comments
 (0)