From: David Marchand <david.marchand@redhat.com>
To: Stephen Hemminger <stephen@networkplumber.org>
Cc: dev@dpdk.org, "Mattias Rönnblom" <mattias.ronnblom@ericsson.com>,
"Bruce Richardson" <bruce.richardson@intel.com>
Subject: Re: [PATCH v2 2/2] random: make rte_rand() thread safe for non-EAL threads
Date: Thu, 7 Sep 2023 18:10:30 +0200 [thread overview]
Message-ID: <CAJFAV8xG_uoZH-KKpGSjeAt5i1rH-UpttQ6yZ5GxXWvGN42ZOw@mail.gmail.com> (raw)
In-Reply-To: <20230907084743.6e43e885@hermes.local>
On Thu, Sep 7, 2023 at 5:48 PM Stephen Hemminger
<stephen@networkplumber.org> wrote:
>
> On Thu, 7 Sep 2023 08:24:56 -0700
> Stephen Hemminger <stephen@networkplumber.org> wrote:
>
> >
> > +static __rte_always_inline
> > +void __rte_rand_put_state(struct rte_rand_state *state)
> > +{
> > + if (state == &rand_states[RTE_MAX_LCORE])
> > + rte_spinlock_unlock(&rte_rand_lock);
> > +}
>
> Conditional locking like this make clang lock analyzer unhappy though.
Ugly, but some macro can do the job...
diff --git a/lib/eal/common/rte_random.c b/lib/eal/common/rte_random.c
index 02b6b6b97b..3f2a4830fd 100644
--- a/lib/eal/common/rte_random.c
+++ b/lib/eal/common/rte_random.c
@@ -128,20 +128,22 @@ struct rte_rand_state *__rte_rand_get_state(void)
idx = rte_lcore_id();
/* last instance reserved for unregistered non-EAL threads */
- if (unlikely(idx == LCORE_ID_ANY)) {
+ if (unlikely(idx == LCORE_ID_ANY))
idx = RTE_MAX_LCORE;
- rte_spinlock_lock(&rte_rand_lock);
- }
return &rand_states[idx];
}
-static __rte_always_inline
-void __rte_rand_put_state(struct rte_rand_state *state)
-{
- if (state == &rand_states[RTE_MAX_LCORE])
- rte_spinlock_unlock(&rte_rand_lock);
-}
+#define PROTECT_NON_EAL_THREADS(...) do { \
+ unsigned int idx = rte_lcore_id(); \
+ if (idx == LCORE_ID_ANY) { \
+ rte_spinlock_lock(&rte_rand_lock); \
+ __VA_ARGS__ \
+ rte_spinlock_unlock(&rte_rand_lock); \
+ } else { \
+ __VA_ARGS__ \
+ } \
+} while (0)
uint64_t
rte_rand(void)
@@ -149,9 +151,10 @@ rte_rand(void)
struct rte_rand_state *state;
uint64_t res;
+ PROTECT_NON_EAL_THREADS(
state = __rte_rand_get_state();
res = __rte_rand_lfsr258(state);
- __rte_rand_put_state(state);
+ );
return res;
}
@@ -168,6 +171,7 @@ rte_rand_max(uint64_t upper_bound)
if (unlikely(upper_bound < 2))
return 0;
+ PROTECT_NON_EAL_THREADS(
state = __rte_rand_get_state();
ones = rte_popcount64(upper_bound);
@@ -192,7 +196,7 @@ rte_rand_max(uint64_t upper_bound)
res = __rte_rand_lfsr258(state) & mask;
} while (unlikely(res >= upper_bound));
}
- __rte_rand_put_state(state);
+ );
return res;
}
--
David Marchand
next prev parent reply other threads:[~2023-09-07 16:10 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-06 15:53 [PATCH] random: initialize the random state for non-eal lcores Stephen Hemminger
2023-09-06 16:25 ` Morten Brørup
2023-09-06 16:28 ` Stephen Hemminger
2023-09-07 15:24 ` [PATCH v2 0/2] fixes to rte_random for non-EAL threads Stephen Hemminger
2023-09-07 15:24 ` [PATCH v2 1/2] random: initialize the random state " Stephen Hemminger
2023-10-02 9:00 ` Morten Brørup
2023-10-02 12:27 ` Mattias Rönnblom
2023-10-02 16:07 ` Stephen Hemminger
2023-10-04 8:45 ` David Marchand
2023-09-07 15:24 ` [PATCH v2 2/2] random: make rte_rand() thread safe " Stephen Hemminger
2023-09-07 15:47 ` Stephen Hemminger
2023-09-07 16:10 ` David Marchand [this message]
2023-09-08 20:48 ` Mattias Rönnblom
2023-09-08 20:56 ` Stephen Hemminger
2023-09-09 7:00 ` Mattias Rönnblom
2023-10-02 16:10 ` Stephen Hemminger
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAJFAV8xG_uoZH-KKpGSjeAt5i1rH-UpttQ6yZ5GxXWvGN42ZOw@mail.gmail.com \
--to=david.marchand@redhat.com \
--cc=bruce.richardson@intel.com \
--cc=dev@dpdk.org \
--cc=mattias.ronnblom@ericsson.com \
--cc=stephen@networkplumber.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).