[v2,1/2] random: initialize the random state for non-EAL threads

Message ID 20230907152456.20570-2-stephen@networkplumber.org (mailing list archive)
State Accepted, archived
Delegated to: David Marchand
Headers
Series fixes to rte_random for non-EAL threads |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Stephen Hemminger Sept. 7, 2023, 3:24 p.m. UTC
  The per-lcore PRNG was not initializing the rand_state of all
the lcores. Any usage of rte_random by a non-EAL lcore would
use rand_states[RTE_MAX_LCORE] which was never initialized.

Fix by using RTE_DIM() which will get all lcores.

Fixes: 3f002f069612 ("eal: replace libc-based random generation with LFSR")
Cc: mattias.ronnblom@ericsson.com
Acked-by: Morten Brørup <mb@smartsharesystems.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/common/rte_random.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Morten Brørup Oct. 2, 2023, 9 a.m. UTC | #1
> From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> Sent: Thursday, 7 September 2023 17.25
> 
> The per-lcore PRNG was not initializing the rand_state of all
> the lcores. Any usage of rte_random by a non-EAL lcore would
> use rand_states[RTE_MAX_LCORE] which was never initialized.
> 
> Fix by using RTE_DIM() which will get all lcores.
> 
> Fixes: 3f002f069612 ("eal: replace libc-based random generation with LFSR")
> Cc: mattias.ronnblom@ericsson.com
> Acked-by: Morten Brørup <mb@smartsharesystems.com>
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---
>  lib/eal/common/rte_random.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/lib/eal/common/rte_random.c b/lib/eal/common/rte_random.c
> index 53636331a27b..812e5b4757b5 100644
> --- a/lib/eal/common/rte_random.c
> +++ b/lib/eal/common/rte_random.c
> @@ -84,7 +84,7 @@ rte_srand(uint64_t seed)
>  	unsigned int lcore_id;
> 
>  	/* add lcore_id to seed to avoid having the same sequence */
> -	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
> +	for (lcore_id = 0; lcore_id < RTE_DIM(rand_states); lcore_id++)
>  		__rte_srand_lfsr258(seed + lcore_id, &rand_states[lcore_id]);
>  }
> 
> --
> 2.39.2

Now also...

Reviewed-by: Morten Brørup <mb@smartsharesystems.com>
  
Mattias Rönnblom Oct. 2, 2023, 12:27 p.m. UTC | #2
On 2023-09-07 17:24, Stephen Hemminger wrote:
> The per-lcore PRNG was not initializing the rand_state of all
> the lcores. Any usage of rte_random by a non-EAL lcore would

"/../ by an unregistered non-EAL thread /../"

> use rand_states[RTE_MAX_LCORE] which was never initialized.
> 
> Fix by using RTE_DIM() which will get all lcores.
> 
> Fixes: 3f002f069612 ("eal: replace libc-based random generation with LFSR")
> Cc: mattias.ronnblom@ericsson.com
> Acked-by: Morten Brørup <mb@smartsharesystems.com>
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---
>   lib/eal/common/rte_random.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/lib/eal/common/rte_random.c b/lib/eal/common/rte_random.c
> index 53636331a27b..812e5b4757b5 100644
> --- a/lib/eal/common/rte_random.c
> +++ b/lib/eal/common/rte_random.c
> @@ -84,7 +84,7 @@ rte_srand(uint64_t seed)
>   	unsigned int lcore_id;
>   
>   	/* add lcore_id to seed to avoid having the same sequence */
> -	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
> +	for (lcore_id = 0; lcore_id < RTE_DIM(rand_states); lcore_id++)
>   		__rte_srand_lfsr258(seed + lcore_id, &rand_states[lcore_id]);
>   }
>   

With the above-mentioned commit message rewording:

Acked-by: Mattias Rönnblom <mattias.ronnblom@ericsson.com>
  
Stephen Hemminger Oct. 2, 2023, 4:07 p.m. UTC | #3
On Mon, 2 Oct 2023 14:27:48 +0200
Mattias Rönnblom <hofors@lysator.liu.se> wrote:

> On 2023-09-07 17:24, Stephen Hemminger wrote:
> > The per-lcore PRNG was not initializing the rand_state of all
> > the lcores. Any usage of rte_random by a non-EAL lcore would  
> 
> "/../ by an unregistered non-EAL thread /../"
> 
> > use rand_states[RTE_MAX_LCORE] which was never initialized.

Sure, that is a more precise wording.
  
David Marchand Oct. 4, 2023, 8:45 a.m. UTC | #4
On Mon, Oct 2, 2023 at 2:28 PM Mattias Rönnblom <hofors@lysator.liu.se> wrote:
>
> On 2023-09-07 17:24, Stephen Hemminger wrote:
> > The per-lcore PRNG was not initializing the rand_state of all
> > the lcores. Any usage of rte_random by a non-EAL lcore would
>
> "/../ by an unregistered non-EAL thread /../"
>
> > use rand_states[RTE_MAX_LCORE] which was never initialized.
> >
> > Fix by using RTE_DIM() which will get all lcores.
> >
> > Fixes: 3f002f069612 ("eal: replace libc-based random generation with LFSR")
> > Cc: mattias.ronnblom@ericsson.com
> > Acked-by: Morten Brørup <mb@smartsharesystems.com>
> > Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> > ---
> >   lib/eal/common/rte_random.c | 2 +-
> >   1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/lib/eal/common/rte_random.c b/lib/eal/common/rte_random.c
> > index 53636331a27b..812e5b4757b5 100644
> > --- a/lib/eal/common/rte_random.c
> > +++ b/lib/eal/common/rte_random.c
> > @@ -84,7 +84,7 @@ rte_srand(uint64_t seed)
> >       unsigned int lcore_id;
> >
> >       /* add lcore_id to seed to avoid having the same sequence */
> > -     for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
> > +     for (lcore_id = 0; lcore_id < RTE_DIM(rand_states); lcore_id++)
> >               __rte_srand_lfsr258(seed + lcore_id, &rand_states[lcore_id]);
> >   }
> >
>
> With the above-mentioned commit message rewording:
>
> Acked-by: Mattias Rönnblom <mattias.ronnblom@ericsson.com>
>

Applied this patch.
The second patch is marked as rejected in pw, in favor of a followup doc patch.

Thanks.
  

Patch

diff --git a/lib/eal/common/rte_random.c b/lib/eal/common/rte_random.c
index 53636331a27b..812e5b4757b5 100644
--- a/lib/eal/common/rte_random.c
+++ b/lib/eal/common/rte_random.c
@@ -84,7 +84,7 @@  rte_srand(uint64_t seed)
 	unsigned int lcore_id;
 
 	/* add lcore_id to seed to avoid having the same sequence */
-	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
+	for (lcore_id = 0; lcore_id < RTE_DIM(rand_states); lcore_id++)
 		__rte_srand_lfsr258(seed + lcore_id, &rand_states[lcore_id]);
 }