diff mbox series

random: cap jitter samples per bit to factor of HZ

Message ID 20220713151115.1014188-1-Jason@zx2c4.com (mailing list archive)
State Not Applicable
Delegated to: Herbert Xu
Headers show
Series random: cap jitter samples per bit to factor of HZ | expand

Commit Message

Jason A. Donenfeld July 13, 2022, 3:11 p.m. UTC
Currently the jitter mechanism will require two timer ticks per
iteration, and it requires N iterations per bit. This N is determined
with a small measurement, and if it's too big, it won't waste time with
jitter entropy because it'd take too long or not have sufficient entropy
anyway.

With the current max N of 32, there are large timeouts on systems with a
small CONFIG_HZ. Rather than set that maximum to 32, instead choose a
factor of CONFIG_HZ. In this case, 1/30 seems to yield sane values for
different configurations of CONFIG_HZ.

Reported-by: Vladimir Murzin <vladimir.murzin@arm.com>
Fixes: 78c768e619fb ("random: vary jitter iterations based on cycle counter speed")
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
Vladimir - Can you let me know if this appears to fix the issue you're
seeing? -Jason

 drivers/char/random.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Vladimir Murzin July 13, 2022, 3:52 p.m. UTC | #1
On 7/13/22 16:11, Jason A. Donenfeld wrote:
> Currently the jitter mechanism will require two timer ticks per
> iteration, and it requires N iterations per bit. This N is determined
> with a small measurement, and if it's too big, it won't waste time with
> jitter entropy because it'd take too long or not have sufficient entropy
> anyway.
> 
> With the current max N of 32, there are large timeouts on systems with a
> small CONFIG_HZ. Rather than set that maximum to 32, instead choose a
> factor of CONFIG_HZ. In this case, 1/30 seems to yield sane values for
> different configurations of CONFIG_HZ.
> 
> Reported-by: Vladimir Murzin <vladimir.murzin@arm.com>
> Fixes: 78c768e619fb ("random: vary jitter iterations based on cycle counter speed")
> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
> ---
> Vladimir - Can you let me know if this appears to fix the issue you're
> seeing? -Jason

Works for me, thanks! :) 

> 
>  drivers/char/random.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/char/random.c b/drivers/char/random.c
> index e3dd1dd3dd22..a1af90bacc9f 100644
> --- a/drivers/char/random.c
> +++ b/drivers/char/random.c
> @@ -1174,7 +1174,7 @@ static void __cold entropy_timer(struct timer_list *timer)
>   */
>  static void __cold try_to_generate_entropy(void)
>  {
> -	enum { NUM_TRIAL_SAMPLES = 8192, MAX_SAMPLES_PER_BIT = 32 };
> +	enum { NUM_TRIAL_SAMPLES = 8192, MAX_SAMPLES_PER_BIT = HZ / 30 };
>  	struct entropy_timer_state stack;
>  	unsigned int i, num_different = 0;
>  	unsigned long last = random_get_entropy();

FWIW

Tested-by: Vladimir Murzin <vladimir.murzin@arm.com>

Cheers
Vladimir
Jason A. Donenfeld July 13, 2022, 4:21 p.m. UTC | #2
Hi Vladmir,

On Wed, Jul 13, 2022 at 04:52:20PM +0100, Vladimir Murzin wrote:
> On 7/13/22 16:11, Jason A. Donenfeld wrote:
> > Vladimir - Can you let me know if this appears to fix the issue you're
> > seeing? -Jason
> 
> Works for me, thanks! :) 
> Tested-by: Vladimir Murzin <vladimir.murzin@arm.com>

Thanks for testing. I'll push this out to Linus probably tomorrow.

(Though I noticed that Linus is in the CC for this thread already, and
he's been on a patch picking spree as of late, so in case he happens to
be following along, fell free to pick away. Otherwise I'll send a pull
not before long.)

Jason
Linus Torvalds July 16, 2022, 5:45 p.m. UTC | #3
On Wed, Jul 13, 2022 at 9:22 AM Jason A. Donenfeld <Jason@zx2c4.com> wrote:
>
> Thanks for testing. I'll push this out to Linus probably tomorrow.
>
> (Though I noticed that Linus is in the CC for this thread already, and
> he's been on a patch picking spree as of late, so in case he happens to
> be following along, fell free to pick away. Otherwise I'll send a pull
> not before long.)

Well, the "probably tomorrow" didn't happen, so yes, I've just picked
it up directly.

                 Linus
Jason A. Donenfeld July 16, 2022, 6:09 p.m. UTC | #4
Hey Linus,

On Sat, Jul 16, 2022 at 10:45:24AM -0700, Linus Torvalds wrote:
> On Wed, Jul 13, 2022 at 9:22 AM Jason A. Donenfeld <Jason@zx2c4.com> wrote:
> >
> > Thanks for testing. I'll push this out to Linus probably tomorrow.
> >
> > (Though I noticed that Linus is in the CC for this thread already, and
> > he's been on a patch picking spree as of late, so in case he happens to
> > be following along, fell free to pick away. Otherwise I'll send a pull
> > not before long.)
> 
> Well, the "probably tomorrow" didn't happen, so yes, I've just picked
> it up directly.

Oh, okay. Something came up on Thursday, and I just sat down (on the
train back home) to do this now. So I didn't forget! But thanks for
taking it nonetheless.

Jason
diff mbox series

Patch

diff --git a/drivers/char/random.c b/drivers/char/random.c
index e3dd1dd3dd22..a1af90bacc9f 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -1174,7 +1174,7 @@  static void __cold entropy_timer(struct timer_list *timer)
  */
 static void __cold try_to_generate_entropy(void)
 {
-	enum { NUM_TRIAL_SAMPLES = 8192, MAX_SAMPLES_PER_BIT = 32 };
+	enum { NUM_TRIAL_SAMPLES = 8192, MAX_SAMPLES_PER_BIT = HZ / 30 };
 	struct entropy_timer_state stack;
 	unsigned int i, num_different = 0;
 	unsigned long last = random_get_entropy();