diff mbox series

hw_random: use add_hwgenerator_randomness() for early entropy

Message ID 20221105204417.137001-1-Jason@zx2c4.com (mailing list archive)
State Not Applicable
Delegated to: Herbert Xu
Headers show
Series hw_random: use add_hwgenerator_randomness() for early entropy | expand

Commit Message

Jason A. Donenfeld Nov. 5, 2022, 8:44 p.m. UTC
Rather than calling add_device_randomness(), the add_early_randomness()
function should use add_hwgenerator_randomness(), so that the early
entropy can be potentially credited, which allows for the RNG to
initialize earlier without having to wait for the kthread to come up.

Cc: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
 drivers/char/hw_random/core.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Jason A. Donenfeld Nov. 6, 2022, 1:31 a.m. UTC | #1
On Sat, Nov 5, 2022 at 9:44 PM Jason A. Donenfeld <Jason@zx2c4.com> wrote:
>
> Rather than calling add_device_randomness(), the add_early_randomness()
> function should use add_hwgenerator_randomness(), so that the early
> entropy can be potentially credited, which allows for the RNG to
> initialize earlier without having to wait for the kthread to come up.
>
> Cc: Herbert Xu <herbert@gondor.apana.org.au>
> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
> ---
>  drivers/char/hw_random/core.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
> index cc002b0c2f0c..8c0819ce2781 100644
> --- a/drivers/char/hw_random/core.c
> +++ b/drivers/char/hw_random/core.c
> @@ -69,8 +69,10 @@ static void add_early_randomness(struct hwrng *rng)
>         mutex_lock(&reading_mutex);
>         bytes_read = rng_get_data(rng, rng_fillbuf, 32, 0);
>         mutex_unlock(&reading_mutex);
> -       if (bytes_read > 0)
> -               add_device_randomness(rng_fillbuf, bytes_read);
> +       if (bytes_read > 0) {
> +               size_t entropy = bytes_read * 8 * rng->quality / 1024;
> +               add_hwgenerator_randomness(rng_fillbuf, bytes_read, entropy);
> +       }

This will cause problems, because add_hwgenerator_randomness() will
sleep. I'll look into a more robust change.

Jason
diff mbox series

Patch

diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
index cc002b0c2f0c..8c0819ce2781 100644
--- a/drivers/char/hw_random/core.c
+++ b/drivers/char/hw_random/core.c
@@ -69,8 +69,10 @@  static void add_early_randomness(struct hwrng *rng)
 	mutex_lock(&reading_mutex);
 	bytes_read = rng_get_data(rng, rng_fillbuf, 32, 0);
 	mutex_unlock(&reading_mutex);
-	if (bytes_read > 0)
-		add_device_randomness(rng_fillbuf, bytes_read);
+	if (bytes_read > 0) {
+		size_t entropy = bytes_read * 8 * rng->quality / 1024;
+		add_hwgenerator_randomness(rng_fillbuf, bytes_read, entropy);
+	}
 }
 
 static inline void cleanup_rng(struct kref *kref)