From: George Spelvin <lkml@sdf.org>
To: linux-kernel@vger.kernel.org, lkml@sdf.org
Cc: Thomas Hellstrom <thellstrom@vmware.com>,
Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>, Will Deacon <will@kernel.org>
Subject: [RFC PATCH v1 30/50] kernel/locking/test-ww_mutex.c: Use cheaper prandom_u32_max()
Date: Fri, 29 Nov 2019 17:10:04 -0500 [thread overview]
Message-ID: <202003281643.02SGhI3G001046@sdf.org> (raw)
This is test code; it doesn't need crypto-quality random numbers.
Also use the simpler initialize-while-shuffling variant of the
Fisher-Yates shuffle in get_random_order().
(It's not clear that we need the order[] array at all.
Could we just shuffle the stress->locks array directly?)
Signed-off-by: George Spelvin <lkml@sdf.org>
Cc: Thomas Hellstrom <thellstrom@vmware.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Will Deacon <will@kernel.org>
---
kernel/locking/test-ww_mutex.c | 23 ++++++++---------------
1 file changed, 8 insertions(+), 15 deletions(-)
diff --git a/kernel/locking/test-ww_mutex.c b/kernel/locking/test-ww_mutex.c
index 3e82f449b4ff7..3fc54d88eb842 100644
--- a/kernel/locking/test-ww_mutex.c
+++ b/kernel/locking/test-ww_mutex.c
@@ -348,25 +348,18 @@ struct stress {
static int *get_random_order(int count)
{
- int *order;
- int n, r, tmp;
+ int n, *order;
order = kmalloc_array(count, sizeof(*order), GFP_KERNEL);
- if (!order)
+ if (!order || !count)
return order;
- for (n = 0; n < count; n++)
- order[n] = n;
-
- for (n = count - 1; n > 1; n--) {
- r = get_random_int() % (n + 1);
- if (r != n) {
- tmp = order[n];
- order[n] = order[r];
- order[r] = tmp;
- }
+ order[0] = 0;
+ for (n = 1; n < count; n++) {
+ int r = prandom_u32_max(n+1);
+ order[n] = order[r];
+ order[r] = n;
}
-
return order;
}
@@ -498,7 +491,7 @@ static void stress_one_work(struct work_struct *work)
{
struct stress *stress = container_of(work, typeof(*stress), work);
const int nlocks = stress->nlocks;
- struct ww_mutex *lock = stress->locks + (get_random_int() % nlocks);
+ struct ww_mutex *lock = stress->locks + prandom_u32_max(nlocks);
int err;
do {
--
2.26.0
reply other threads:[~2020-03-28 16:44 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=202003281643.02SGhI3G001046@sdf.org \
--to=lkml@sdf.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=thellstrom@vmware.com \
--cc=will@kernel.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
all inboxes | Powered by JetHome®