public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH] syscalls/getrandom02: lower bufsize if low on entropy
@ 2020-02-05  9:19 Jan Stancek
  2020-02-05 13:57 ` Li Wang
  2020-02-07 11:19 ` Cyril Hrubis
  0 siblings, 2 replies; 7+ messages in thread
From: Jan Stancek @ 2020-02-05  9:19 UTC (permalink / raw)
  To: ltp

Some PPC KVM guests accumulate about 1 bit of entropy per second while idle
and running getrandom02. Which isn't enough and test sporadically fails on
timeout.

Adjust the buffer size by looking at entropy_avail. We want to run the test,
even if all entropy is exhausted, but with smaller buffer we don't set
as high expectations on how much entropy is generated within default test time.

Signed-off-by: Jan Stancek <jstancek@redhat.com>
---
 testcases/kernel/syscalls/getrandom/getrandom02.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/testcases/kernel/syscalls/getrandom/getrandom02.c b/testcases/kernel/syscalls/getrandom/getrandom02.c
index ee0193df9897..1384fc5f32c0 100644
--- a/testcases/kernel/syscalls/getrandom/getrandom02.c
+++ b/testcases/kernel/syscalls/getrandom/getrandom02.c
@@ -10,6 +10,8 @@
 #include "lapi/syscalls.h"
 #include "tst_test.h"
 
+#define PROC_ENTROPY_AVAIL "/proc/sys/kernel/random/entropy_avail"
+
 static int modes[] = { 0, GRND_RANDOM, GRND_NONBLOCK,
 		       GRND_RANDOM | GRND_NONBLOCK };
 
@@ -37,11 +39,17 @@ static int check_content(unsigned char *buf, int nb)
 static void verify_getrandom(unsigned int n)
 {
 	unsigned char buf[256];
+	int bufsize = 64, entropy_avail;
 
-	memset(buf, 0, sizeof(buf));
+	if (access(PROC_ENTROPY_AVAIL, F_OK) == 0) {
+		SAFE_FILE_SCANF(PROC_ENTROPY_AVAIL, "%d", &entropy_avail);
+		if (entropy_avail > 256)
+			bufsize = sizeof(buf);
+	}
 
+	memset(buf, 0, sizeof(buf));
 	do {
-		TEST(tst_syscall(__NR_getrandom, buf, sizeof(buf), modes[n]));
+		TEST(tst_syscall(__NR_getrandom, buf, bufsize, modes[n]));
 	} while ((modes[n] & GRND_NONBLOCK) && TST_RET == -1
 		  && TST_ERR == EAGAIN);
 
-- 
2.18.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [LTP] [PATCH] syscalls/getrandom02: lower bufsize if low on entropy
  2020-02-05  9:19 [LTP] [PATCH] syscalls/getrandom02: lower bufsize if low on entropy Jan Stancek
@ 2020-02-05 13:57 ` Li Wang
  2020-02-05 14:20   ` Li Wang
  2020-02-05 14:32   ` Jan Stancek
  2020-02-07 11:19 ` Cyril Hrubis
  1 sibling, 2 replies; 7+ messages in thread
From: Li Wang @ 2020-02-05 13:57 UTC (permalink / raw)
  To: ltp

On Wed, Feb 5, 2020 at 5:19 PM Jan Stancek <jstancek@redhat.com> wrote:

> Some PPC KVM guests accumulate about 1 bit of entropy per second while idle
> and running getrandom02. Which isn't enough and test sporadically fails on
> timeout.
>
> Adjust the buffer size by looking at entropy_avail. We want to run the
> test,
> even if all entropy is exhausted, but with smaller buffer we don't set
> as high expectations on how much entropy is generated within default test
> time.
>
> Signed-off-by: Jan Stancek <jstancek@redhat.com>
> ---
>  testcases/kernel/syscalls/getrandom/getrandom02.c | 12 ++++++++++--
>  1 file changed, 10 insertions(+), 2 deletions(-)
>
> diff --git a/testcases/kernel/syscalls/getrandom/getrandom02.c
> b/testcases/kernel/syscalls/getrandom/getrandom02.c
> index ee0193df9897..1384fc5f32c0 100644
> --- a/testcases/kernel/syscalls/getrandom/getrandom02.c
> +++ b/testcases/kernel/syscalls/getrandom/getrandom02.c
> @@ -10,6 +10,8 @@
>  #include "lapi/syscalls.h"
>  #include "tst_test.h"
>
> +#define PROC_ENTROPY_AVAIL "/proc/sys/kernel/random/entropy_avail"
> +
>  static int modes[] = { 0, GRND_RANDOM, GRND_NONBLOCK,
>                        GRND_RANDOM | GRND_NONBLOCK };
>
> @@ -37,11 +39,17 @@ static int check_content(unsigned char *buf, int nb)
>  static void verify_getrandom(unsigned int n)
>  {
>         unsigned char buf[256];
> +       int bufsize = 64, entropy_avail;
>

I'm not sure why here initialize bufsize as 64? can you explain more?


>
> -       memset(buf, 0, sizeof(buf));
> +       if (access(PROC_ENTROPY_AVAIL, F_OK) == 0) {
> +               SAFE_FILE_SCANF(PROC_ENTROPY_AVAIL, "%d", &entropy_avail);
> +               if (entropy_avail > 256)
> +                       bufsize = sizeof(buf);
> +       }
>
> +       memset(buf, 0, sizeof(buf));
>         do {
> -               TEST(tst_syscall(__NR_getrandom, buf, sizeof(buf),
> modes[n]));
> +               TEST(tst_syscall(__NR_getrandom, buf, bufsize, modes[n]));
>         } while ((modes[n] & GRND_NONBLOCK) && TST_RET == -1
>                   && TST_ERR == EAGAIN);
>
> --
> 2.18.1
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
>
>

-- 
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20200205/5da9c352/attachment-0001.htm>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [LTP] [PATCH] syscalls/getrandom02: lower bufsize if low on entropy
  2020-02-05 13:57 ` Li Wang
@ 2020-02-05 14:20   ` Li Wang
  2020-02-05 14:32   ` Jan Stancek
  1 sibling, 0 replies; 7+ messages in thread
From: Li Wang @ 2020-02-05 14:20 UTC (permalink / raw)
  To: ltp

On Wed, Feb 5, 2020 at 9:57 PM Li Wang <liwang@redhat.com> wrote:

>
>
> On Wed, Feb 5, 2020 at 5:19 PM Jan Stancek <jstancek@redhat.com> wrote:
>
>> Some PPC KVM guests accumulate about 1 bit of entropy per second while
>> idle
>> and running getrandom02. Which isn't enough and test sporadically fails on
>> timeout.
>>
>> Adjust the buffer size by looking at entropy_avail. We want to run the
>> test,
>> even if all entropy is exhausted, but with smaller buffer we don't set
>> as high expectations on how much entropy is generated within default test
>> time.
>>
>> Signed-off-by: Jan Stancek <jstancek@redhat.com>
>> ---
>>  testcases/kernel/syscalls/getrandom/getrandom02.c | 12 ++++++++++--
>>  1 file changed, 10 insertions(+), 2 deletions(-)
>>
>> diff --git a/testcases/kernel/syscalls/getrandom/getrandom02.c
>> b/testcases/kernel/syscalls/getrandom/getrandom02.c
>> index ee0193df9897..1384fc5f32c0 100644
>> --- a/testcases/kernel/syscalls/getrandom/getrandom02.c
>> +++ b/testcases/kernel/syscalls/getrandom/getrandom02.c
>> @@ -10,6 +10,8 @@
>>  #include "lapi/syscalls.h"
>>  #include "tst_test.h"
>>
>> +#define PROC_ENTROPY_AVAIL "/proc/sys/kernel/random/entropy_avail"
>> +
>>  static int modes[] = { 0, GRND_RANDOM, GRND_NONBLOCK,
>>                        GRND_RANDOM | GRND_NONBLOCK };
>>
>> @@ -37,11 +39,17 @@ static int check_content(unsigned char *buf, int nb)
>>  static void verify_getrandom(unsigned int n)
>>  {
>>         unsigned char buf[256];
>> +       int bufsize = 64, entropy_avail;
>>
>
> I'm not sure why here initialize bufsize as 64? can you explain more?
>

Ah I see, it just picks a smaller size to obtain littler random bytes to
decrease the failure probability. But not has special meaning for 64, right?

-- 
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20200205/845b3877/attachment.htm>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [LTP] [PATCH] syscalls/getrandom02: lower bufsize if low on entropy
  2020-02-05 13:57 ` Li Wang
  2020-02-05 14:20   ` Li Wang
@ 2020-02-05 14:32   ` Jan Stancek
  2020-02-06  7:01     ` Li Wang
  1 sibling, 1 reply; 7+ messages in thread
From: Jan Stancek @ 2020-02-05 14:32 UTC (permalink / raw)
  To: ltp



----- Original Message -----
> >
> > @@ -37,11 +39,17 @@ static int check_content(unsigned char *buf, int nb)
> >  static void verify_getrandom(unsigned int n)
> >  {
> >         unsigned char buf[256];
> > +       int bufsize = 64, entropy_avail;
> >
> 
> I'm not sure why here initialize bufsize as 64? can you explain more?

That would be the default, unless we know we have enough entropy.

I picked 64 as it matches 'random_read_wakeup_bits'. Assuming we can get
1bit/s entropy, then potentially worst case of waiting 4x for 64bit,
it should still fit within default 300 seconds.

But it's also rule of thumb, because it's smaller.

> 
> 
> >
> > -       memset(buf, 0, sizeof(buf));
> > +       if (access(PROC_ENTROPY_AVAIL, F_OK) == 0) {
> > +               SAFE_FILE_SCANF(PROC_ENTROPY_AVAIL, "%d", &entropy_avail);
> > +               if (entropy_avail > 256)
> > +                       bufsize = sizeof(buf);
> > +       }
> >
> > +       memset(buf, 0, sizeof(buf));
> >         do {
> > -               TEST(tst_syscall(__NR_getrandom, buf, sizeof(buf),
> > modes[n]));
> > +               TEST(tst_syscall(__NR_getrandom, buf, bufsize, modes[n]));
> >         } while ((modes[n] & GRND_NONBLOCK) && TST_RET == -1
> >                   && TST_ERR == EAGAIN);
> >
> > --
> > 2.18.1
> >
> >
> > --
> > Mailing list info: https://lists.linux.it/listinfo/ltp
> >
> >
> 
> --
> Regards,
> Li Wang
> 


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [LTP] [PATCH] syscalls/getrandom02: lower bufsize if low on entropy
  2020-02-05 14:32   ` Jan Stancek
@ 2020-02-06  7:01     ` Li Wang
  0 siblings, 0 replies; 7+ messages in thread
From: Li Wang @ 2020-02-06  7:01 UTC (permalink / raw)
  To: ltp

On Wed, Feb 5, 2020 at 10:32 PM Jan Stancek <jstancek@redhat.com> wrote:

>
>
> ----- Original Message -----
> > >
> > > @@ -37,11 +39,17 @@ static int check_content(unsigned char *buf, int
> nb)
> > >  static void verify_getrandom(unsigned int n)
> > >  {
> > >         unsigned char buf[256];
> > > +       int bufsize = 64, entropy_avail;
> > >
> >
> > I'm not sure why here initialize bufsize as 64? can you explain more?
>
> That would be the default, unless we know we have enough entropy.
>
> I picked 64 as it matches 'random_read_wakeup_bits'. Assuming we can get
> 1bit/s entropy, then potentially worst case of waiting 4x for 64bit,
> it should still fit within default 300 seconds.
>

That sounds reasonable. Thanks for the analysis.

Reviewed-by: Li Wang <liwang@redhat.com>

-- 
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20200206/521ada62/attachment.htm>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [LTP] [PATCH] syscalls/getrandom02: lower bufsize if low on entropy
  2020-02-05  9:19 [LTP] [PATCH] syscalls/getrandom02: lower bufsize if low on entropy Jan Stancek
  2020-02-05 13:57 ` Li Wang
@ 2020-02-07 11:19 ` Cyril Hrubis
  2020-02-07 11:26   ` Jan Stancek
  1 sibling, 1 reply; 7+ messages in thread
From: Cyril Hrubis @ 2020-02-07 11:19 UTC (permalink / raw)
  To: ltp

Hi!
> Some PPC KVM guests accumulate about 1 bit of entropy per second while idle
> and running getrandom02. Which isn't enough and test sporadically fails on
> timeout.
> 
> Adjust the buffer size by looking at entropy_avail. We want to run the test,
> even if all entropy is exhausted, but with smaller buffer we don't set
> as high expectations on how much entropy is generated within default test time.
> 
> Signed-off-by: Jan Stancek <jstancek@redhat.com>
> ---
>  testcases/kernel/syscalls/getrandom/getrandom02.c | 12 ++++++++++--
>  1 file changed, 10 insertions(+), 2 deletions(-)
> 
> diff --git a/testcases/kernel/syscalls/getrandom/getrandom02.c b/testcases/kernel/syscalls/getrandom/getrandom02.c
> index ee0193df9897..1384fc5f32c0 100644
> --- a/testcases/kernel/syscalls/getrandom/getrandom02.c
> +++ b/testcases/kernel/syscalls/getrandom/getrandom02.c
> @@ -10,6 +10,8 @@
>  #include "lapi/syscalls.h"
>  #include "tst_test.h"
>  
> +#define PROC_ENTROPY_AVAIL "/proc/sys/kernel/random/entropy_avail"
> +
>  static int modes[] = { 0, GRND_RANDOM, GRND_NONBLOCK,
>  		       GRND_RANDOM | GRND_NONBLOCK };
>  
> @@ -37,11 +39,17 @@ static int check_content(unsigned char *buf, int nb)
>  static void verify_getrandom(unsigned int n)
>  {
>  	unsigned char buf[256];
> +	int bufsize = 64, entropy_avail;
>  
> -	memset(buf, 0, sizeof(buf));
> +	if (access(PROC_ENTROPY_AVAIL, F_OK) == 0) {
> +		SAFE_FILE_SCANF(PROC_ENTROPY_AVAIL, "%d", &entropy_avail);
> +		if (entropy_avail > 256)
> +			bufsize = sizeof(buf);
> +	}

Looks good to me, acked.

-- 
Cyril Hrubis
chrubis@suse.cz

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [LTP] [PATCH] syscalls/getrandom02: lower bufsize if low on entropy
  2020-02-07 11:19 ` Cyril Hrubis
@ 2020-02-07 11:26   ` Jan Stancek
  0 siblings, 0 replies; 7+ messages in thread
From: Jan Stancek @ 2020-02-07 11:26 UTC (permalink / raw)
  To: ltp



----- Original Message -----
> 
> Looks good to me, acked.

Thanks, pushed.


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-02-07 11:26 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-02-05  9:19 [LTP] [PATCH] syscalls/getrandom02: lower bufsize if low on entropy Jan Stancek
2020-02-05 13:57 ` Li Wang
2020-02-05 14:20   ` Li Wang
2020-02-05 14:32   ` Jan Stancek
2020-02-06  7:01     ` Li Wang
2020-02-07 11:19 ` Cyril Hrubis
2020-02-07 11:26   ` Jan Stancek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox