Skip to content

Commit

Permalink
random: Pull PREEMPT_RT changes from upstream
Browse files Browse the repository at this point in the history
In linux-stable backports local locks were open coded
for non-PREEMPT_RT kernels. Align locking with upstream and
adapt it for older patchset.

Signed-off-by: Alexander Winkowski <[email protected]>
  • Loading branch information
dereference23 committed Jun 18, 2023
1 parent d5a049a commit c0b3d65
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions drivers/char/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <linux/mm.h>
#include <linux/nodemask.h>
#include <linux/spinlock.h>
#include <linux/locallock.h>
#include <linux/kthread.h>
#include <linux/percpu.h>
#include <linux/ptrace.h>
Expand Down Expand Up @@ -235,6 +236,8 @@ static DEFINE_PER_CPU(struct crng, crngs) = {
.generation = ULONG_MAX
};

static DEFINE_LOCAL_IRQ_LOCK(crngs_lock);

/* Used by crng_reseed() and crng_make_state() to extract a new seed from the input pool. */
static void extract_entropy(void *buf, size_t len);

Expand Down Expand Up @@ -362,7 +365,7 @@ static void crng_make_state(u32 chacha_state[CHACHA20_BLOCK_SIZE / sizeof(u32)],
if (unlikely(crng_has_old_seed()))
crng_reseed();

local_irq_save(flags);
local_lock_irqsave(crngs_lock, flags);
crng = raw_cpu_ptr(&crngs);

/*
Expand All @@ -387,7 +390,7 @@ static void crng_make_state(u32 chacha_state[CHACHA20_BLOCK_SIZE / sizeof(u32)],
* should wind up here immediately.
*/
crng_fast_key_erasure(crng->key, chacha_state, random_data, random_data_len);
local_irq_restore(flags);
local_unlock_irqrestore(crngs_lock, flags);
}

static void _get_random_bytes(void *buf, size_t len)
Expand Down Expand Up @@ -509,6 +512,7 @@ struct batch_ ##type { \
unsigned int position; \
}; \
\
static DEFINE_LOCAL_IRQ_LOCK(type##_lock); \
static DEFINE_PER_CPU(struct batch_ ##type, batched_entropy_ ##type) = { \
.position = UINT_MAX \
}; \
Expand All @@ -527,7 +531,7 @@ type get_random_ ##type(void) \
return ret; \
} \
\
local_irq_save(flags); \
local_lock_irqsave(type##_lock, flags); \
batch = raw_cpu_ptr(&batched_entropy_##type); \
\
next_gen = READ_ONCE(base_crng.generation); \
Expand All @@ -541,7 +545,7 @@ type get_random_ ##type(void) \
ret = batch->entropy[batch->position]; \
batch->entropy[batch->position] = 0; \
++batch->position; \
local_irq_restore(flags); \
local_unlock_irqrestore(type##_lock, flags); \
return ret; \
} \
EXPORT_SYMBOL(get_random_ ##type);
Expand Down

0 comments on commit c0b3d65

Please sign in to comment.