* [PATCH] random: make consistent usage of crng_ready()
@ 2022-03-08 18:25 Jason A. Donenfeld
2022-03-12 20:09 ` Eric Biggers
0 siblings, 1 reply; 4+ messages in thread
From: Jason A. Donenfeld @ 2022-03-08 18:25 UTC (permalink / raw)
To: linux-kernel; +Cc: Jason A. Donenfeld, Dominik Brodowski, Theodore Ts'o
Rather than sometimes checking `crng_init < 2`, we should always use the
crng_ready() macro, so that should we change anything later, it's
consistent. Additionally, that macro already has a likely() around it,
which means we don't need to open code our own likely() and unlikely()
annotations.
Cc: Dominik Brodowski <linux@dominikbrodowski.net>
Cc: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
drivers/char/random.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/drivers/char/random.c b/drivers/char/random.c
index 69591d599338..e37ae7ef039c 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -123,7 +123,7 @@ static void try_to_generate_entropy(void);
*/
int wait_for_random_bytes(void)
{
- if (likely(crng_ready()))
+ if (crng_ready())
return 0;
do {
@@ -289,7 +289,7 @@ static void crng_reseed(bool force)
++next_gen;
WRITE_ONCE(base_crng.generation, next_gen);
WRITE_ONCE(base_crng.birth, jiffies);
- if (crng_init < 2) {
+ if (!crng_ready()) {
crng_init = 2;
finalize_init = true;
}
@@ -352,7 +352,7 @@ static void crng_make_state(u32 chacha_state[CHACHA_STATE_WORDS],
* ready, we do fast key erasure with the base_crng directly, because
* this is what crng_pre_init_inject() mutates during early init.
*/
- if (unlikely(!crng_ready())) {
+ if (!crng_ready()) {
bool ready;
spin_lock_irqsave(&base_crng.lock, flags);
@@ -795,7 +795,7 @@ static void credit_entropy_bits(size_t nbits)
entropy_count = min_t(unsigned int, POOL_BITS, orig + add);
} while (cmpxchg(&input_pool.entropy_count, orig, entropy_count) != orig);
- if (crng_init < 2 && entropy_count >= POOL_MIN_BITS)
+ if (!crng_ready() && entropy_count >= POOL_MIN_BITS)
crng_reseed(false);
}
--
2.35.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] random: make consistent usage of crng_ready()
2022-03-08 18:25 [PATCH] random: make consistent usage of crng_ready() Jason A. Donenfeld
@ 2022-03-12 20:09 ` Eric Biggers
2022-03-12 23:58 ` Jason A. Donenfeld
0 siblings, 1 reply; 4+ messages in thread
From: Eric Biggers @ 2022-03-12 20:09 UTC (permalink / raw)
To: Jason A. Donenfeld; +Cc: linux-kernel, Dominik Brodowski, Theodore Ts'o
On Tue, Mar 08, 2022 at 11:25:17AM -0700, Jason A. Donenfeld wrote:
> diff --git a/drivers/char/random.c b/drivers/char/random.c
> index 69591d599338..e37ae7ef039c 100644
> --- a/drivers/char/random.c
> +++ b/drivers/char/random.c
> @@ -123,7 +123,7 @@ static void try_to_generate_entropy(void);
> */
> int wait_for_random_bytes(void)
> {
> - if (likely(crng_ready()))
> + if (crng_ready())
> return 0;
>
> do {
...
> } while (!crng_ready());
I guess the reason why the above doesn't simply use a 'while' loop is because
someone wanted 'likely()' on the first crng_ready()? That doesn't actually
apply, since crng_ready() has likely() itself, it should just be a 'while' loop.
- Eric
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] random: make consistent usage of crng_ready()
2022-03-12 20:09 ` Eric Biggers
@ 2022-03-12 23:58 ` Jason A. Donenfeld
2022-03-13 0:00 ` [PATCH v2] " Jason A. Donenfeld
0 siblings, 1 reply; 4+ messages in thread
From: Jason A. Donenfeld @ 2022-03-12 23:58 UTC (permalink / raw)
To: Eric Biggers; +Cc: LKML, Dominik Brodowski, Theodore Ts'o
Hi Eric,
On Sat, Mar 12, 2022 at 1:09 PM Eric Biggers <ebiggers@kernel.org> wrote:
>
> On Tue, Mar 08, 2022 at 11:25:17AM -0700, Jason A. Donenfeld wrote:
> > diff --git a/drivers/char/random.c b/drivers/char/random.c
> > index 69591d599338..e37ae7ef039c 100644
> > --- a/drivers/char/random.c
> > +++ b/drivers/char/random.c
> > @@ -123,7 +123,7 @@ static void try_to_generate_entropy(void);
> > */
> > int wait_for_random_bytes(void)
> > {
> > - if (likely(crng_ready()))
> > + if (crng_ready())
> > return 0;
> >
> > do {
> ...
> > } while (!crng_ready());
>
> I guess the reason why the above doesn't simply use a 'while' loop is because
> someone wanted 'likely()' on the first crng_ready()? That doesn't actually
> apply, since crng_ready() has likely() itself, it should just be a 'while' loop.
Nice catch, thanks.
Jason
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2] random: make consistent usage of crng_ready()
2022-03-12 23:58 ` Jason A. Donenfeld
@ 2022-03-13 0:00 ` Jason A. Donenfeld
0 siblings, 0 replies; 4+ messages in thread
From: Jason A. Donenfeld @ 2022-03-13 0:00 UTC (permalink / raw)
To: linux-kernel; +Cc: Jason A. Donenfeld, Theodore Ts'o, Dominik Brodowski
Rather than sometimes checking `crng_init < 2`, we should always use the
crng_ready() macro, so that should we change anything later, it's
consistent. Additionally, that macro already has a likely() around it,
which means we don't need to open code our own likely() and unlikely()
annotations.
Cc: Theodore Ts'o <tytso@mit.edu>
Reviewed-by: Dominik Brodowski <linux@dominikbrodowski.net>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
drivers/char/random.c | 19 +++++++------------
1 file changed, 7 insertions(+), 12 deletions(-)
diff --git a/drivers/char/random.c b/drivers/char/random.c
index edb5b06544da..596dc664b5bd 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -123,18 +123,13 @@ static void try_to_generate_entropy(void);
*/
int wait_for_random_bytes(void)
{
- if (likely(crng_ready()))
- return 0;
-
- do {
+ while (!crng_ready()) {
int ret;
ret = wait_event_interruptible_timeout(crng_init_wait, crng_ready(), HZ);
if (ret)
return ret > 0 ? 0 : ret;
-
try_to_generate_entropy();
- } while (!crng_ready());
-
+ }
return 0;
}
EXPORT_SYMBOL(wait_for_random_bytes);
@@ -289,7 +284,7 @@ static void crng_reseed(bool force)
++next_gen;
WRITE_ONCE(base_crng.generation, next_gen);
WRITE_ONCE(base_crng.birth, jiffies);
- if (crng_init < 2) {
+ if (!crng_ready()) {
crng_init = 2;
finalize_init = true;
}
@@ -352,7 +347,7 @@ static void crng_make_state(u32 chacha_state[CHACHA_STATE_WORDS],
* ready, we do fast key erasure with the base_crng directly, because
* this is what crng_pre_init_inject() mutates during early init.
*/
- if (unlikely(!crng_ready())) {
+ if (!crng_ready()) {
bool ready;
spin_lock_irqsave(&base_crng.lock, flags);
@@ -795,7 +790,7 @@ static void credit_entropy_bits(size_t nbits)
entropy_count = min_t(unsigned int, POOL_BITS, orig + add);
} while (cmpxchg(&input_pool.entropy_count, orig, entropy_count) != orig);
- if (crng_init < 2 && entropy_count >= POOL_MIN_BITS)
+ if (!crng_ready() && entropy_count >= POOL_MIN_BITS)
crng_reseed(false);
}
@@ -957,7 +952,7 @@ int __init rand_initialize(void)
extract_entropy(base_crng.key, sizeof(base_crng.key));
++base_crng.generation;
- if (arch_init && trust_cpu && crng_init < 2) {
+ if (arch_init && trust_cpu && !crng_ready()) {
crng_init = 2;
pr_notice("crng init done (trusting CPU's manufacturer)\n");
}
@@ -1551,7 +1546,7 @@ static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
case RNDRESEEDCRNG:
if (!capable(CAP_SYS_ADMIN))
return -EPERM;
- if (crng_init < 2)
+ if (!crng_ready())
return -ENODATA;
crng_reseed(false);
return 0;
--
2.35.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-03-13 0:01 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-08 18:25 [PATCH] random: make consistent usage of crng_ready() Jason A. Donenfeld
2022-03-12 20:09 ` Eric Biggers
2022-03-12 23:58 ` Jason A. Donenfeld
2022-03-13 0:00 ` [PATCH v2] " Jason A. Donenfeld
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.