From: Markus Theil <theil.markus@gmail.com>
To: netdev@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: davem@davemloft.net, akpm@linux-foundation.org, Jason@zx2c4.com,
Markus Theil <theil.markus@gmail.com>
Subject: [PATCH v2 4/4] test_hash.c: replace custom PRNG by prandom
Date: Fri, 4 Jul 2025 14:52:33 +0200 [thread overview]
Message-ID: <20250704125233.2653779-5-theil.markus@gmail.com> (raw)
In-Reply-To: <20250704125233.2653779-1-theil.markus@gmail.com>
Use default PRNG, as there is no real need
for a custom solution here.
We know the sequence provided by our seed
and do not need to do bit magic in order
to obtain a non-zero byte. Just iterate a
small number of times, if needed.
Signed-off-by: Markus Theil <theil.markus@gmail.com>
---
lib/tests/test_hash.c | 40 +++++++++++++---------------------------
1 file changed, 13 insertions(+), 27 deletions(-)
diff --git a/lib/tests/test_hash.c b/lib/tests/test_hash.c
index a7af39662a0a..308446ea3431 100644
--- a/lib/tests/test_hash.c
+++ b/lib/tests/test_hash.c
@@ -17,39 +17,21 @@
#include <linux/compiler.h>
#include <linux/types.h>
#include <linux/module.h>
+#include <linux/prandom.h>
#include <linux/hash.h>
#include <linux/stringhash.h>
#include <kunit/test.h>
-/* 32-bit XORSHIFT generator. Seed must not be zero. */
-static u32 __attribute_const__
-xorshift(u32 seed)
-{
- seed ^= seed << 13;
- seed ^= seed >> 17;
- seed ^= seed << 5;
- return seed;
-}
-
-/* Given a non-zero x, returns a non-zero byte. */
-static u8 __attribute_const__
-mod255(u32 x)
-{
- x = (x & 0xffff) + (x >> 16); /* 1 <= x <= 0x1fffe */
- x = (x & 0xff) + (x >> 8); /* 1 <= x <= 0x2fd */
- x = (x & 0xff) + (x >> 8); /* 1 <= x <= 0x100 */
- x = (x & 0xff) + (x >> 8); /* 1 <= x <= 0xff */
- return x;
-}
-
/* Fill the buffer with non-zero bytes. */
-static void fill_buf(char *buf, size_t len, u32 seed)
+static void fill_buf(char *buf, size_t len, struct rnd_state *prng)
{
size_t i;
for (i = 0; i < len; i++) {
- seed = xorshift(seed);
- buf[i] = mod255(seed);
+ /* we know our seeds, no need to worry about endless runtime */
+ do {
+ buf[i] = (u8) prandom_u32_state(prng);
+ } while (!buf[i]);
}
}
@@ -143,11 +125,13 @@ test_int_hash(struct kunit *test, unsigned long long h64, u32 hash_or[2][33])
static void test_string_or(struct kunit *test)
{
- char buf[SIZE+1];
+ struct rnd_state prng;
u32 string_or = 0;
+ char buf[SIZE+1];
int i, j;
- fill_buf(buf, SIZE, 1);
+ prandom_seed_state(&prng, 0x1);
+ fill_buf(buf, SIZE, &prng);
/* Test every possible non-empty substring in the buffer. */
for (j = SIZE; j > 0; --j) {
@@ -171,9 +155,11 @@ static void test_hash_or(struct kunit *test)
char buf[SIZE+1];
u32 hash_or[2][33] = { { 0, } };
unsigned long long h64 = 0;
+ struct rnd_state prng;
int i, j;
- fill_buf(buf, SIZE, 1);
+ prandom_seed_state(&prng, 0x1);
+ fill_buf(buf, SIZE, &prng);
/* Test every possible non-empty substring in the buffer. */
for (j = SIZE; j > 0; --j) {
--
2.49.0
prev parent reply other threads:[~2025-07-04 12:52 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-04 12:52 [PATCH v2 0/4] add usage hints to prandom and switch to Xoshiro Markus Theil
2025-07-04 12:52 ` [PATCH v2 1/4] prandom: add usage comments for cryptography Markus Theil
2025-07-04 12:52 ` [PATCH v2 2/4] prandom/random32: switch to Xoshiro256++ Markus Theil
2025-07-04 12:52 ` [PATCH v2 3/4] prandom/random32: add checks against invalid state Markus Theil
2025-07-04 12:52 ` Markus Theil [this message]
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=20250704125233.2653779-5-theil.markus@gmail.com \
--to=theil.markus@gmail.com \
--cc=Jason@zx2c4.com \
--cc=akpm@linux-foundation.org \
--cc=davem@davemloft.net \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.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®