diff mbox series

[1/4] prandom.h: add *_state variant of prandom_u32_max

Message ID 20201025214842.5924-2-linux@rasmusvillemoes.dk (mailing list archive)
State New
Headers show
Series deterministic random testing | expand

Commit Message

Rasmus Villemoes Oct. 25, 2020, 9:48 p.m. UTC
It is useful for test modules that make use of random numbers to allow
the exact same series of test cases to be repeated (e.g., after fixing
a bug in the code being tested). For that, the test module needs to
obtain its random numbers from a private state that can be seeded by a
known seed, e.g. given as a module parameter (and using a random seed
when that parameter is not given).

There's a few test modules I'm going to modify to follow that
scheme. As preparation, add a _state variant of the existing
prandom_u32_max(), and for convenience, also add a variant that
produces a value in a given range.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 include/linux/prandom.h | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

Comments

Petr Mladek Oct. 30, 2020, 4 p.m. UTC | #1
On Sun 2020-10-25 22:48:39, Rasmus Villemoes wrote:
> It is useful for test modules that make use of random numbers to allow
> the exact same series of test cases to be repeated (e.g., after fixing
> a bug in the code being tested). For that, the test module needs to
> obtain its random numbers from a private state that can be seeded by a
> known seed, e.g. given as a module parameter (and using a random seed
> when that parameter is not given).
> 
> There's a few test modules I'm going to modify to follow that
> scheme. As preparation, add a _state variant of the existing
> prandom_u32_max(), and for convenience, also add a variant that
> produces a value in a given range.
> 
> Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
> ---
>  include/linux/prandom.h | 29 +++++++++++++++++++++++++++++
>  1 file changed, 29 insertions(+)
> 
> diff --git a/include/linux/prandom.h b/include/linux/prandom.h
> index aa16e6468f91e79e1f31..58ffcd56c705be34fb98 100644
> --- a/include/linux/prandom.h
> +++ b/include/linux/prandom.h
> @@ -46,6 +46,35 @@ static inline u32 prandom_u32_max(u32 ep_ro)
>  	return (u32)(((u64) prandom_u32() * ep_ro) >> 32);
>  }
>  
> +/**
> + * prandom_u32_max_state - get pseudo-random number in internal [0, hi)

s/internal/interval/

> + *
> + * Like prandom_u32_max, but use the given state structure.
> + * @state: pointer to state structure
> + * @hi: (exclusive) upper bound
> + *
> + * Exception: If @hi == 0, this returns 0.
> + */
> +static inline u32 prandom_u32_max_state(struct rnd_state *state, u32 hi)
> +{
> +	return ((u64)prandom_u32_state(state) * hi) >> 32;
> +}
> +
> +/**
> + * prandom_u32_range_state - get pseudo-random number in internal [lo, hi)

same here

> + *
> + * @state: pointer to state structure
> + * @lo: (inclusive) lower bound
> + * @hi: (exclusive) upper bound
> + *
> + * Exception: If @lo == @hi, this returns @lo. Results are unspecified
> + * for @lo > @hi.
> + */
> +static inline u32 prandom_u32_range_state(struct rnd_state *state, u32 lo, u32 hi)
> +{
> +	return lo + prandom_u32_max_state(state, hi - lo);
> +}

With the above typo fixes:

Reviewed-by: Petr Mladek <pmladek@suse.com>

Well, I guess that we need ack from Willy.

Best Regards,
Petr
diff mbox series

Patch

diff --git a/include/linux/prandom.h b/include/linux/prandom.h
index aa16e6468f91e79e1f31..58ffcd56c705be34fb98 100644
--- a/include/linux/prandom.h
+++ b/include/linux/prandom.h
@@ -46,6 +46,35 @@  static inline u32 prandom_u32_max(u32 ep_ro)
 	return (u32)(((u64) prandom_u32() * ep_ro) >> 32);
 }
 
+/**
+ * prandom_u32_max_state - get pseudo-random number in internal [0, hi)
+ *
+ * Like prandom_u32_max, but use the given state structure.
+ * @state: pointer to state structure
+ * @hi: (exclusive) upper bound
+ *
+ * Exception: If @hi == 0, this returns 0.
+ */
+static inline u32 prandom_u32_max_state(struct rnd_state *state, u32 hi)
+{
+	return ((u64)prandom_u32_state(state) * hi) >> 32;
+}
+
+/**
+ * prandom_u32_range_state - get pseudo-random number in internal [lo, hi)
+ *
+ * @state: pointer to state structure
+ * @lo: (inclusive) lower bound
+ * @hi: (exclusive) upper bound
+ *
+ * Exception: If @lo == @hi, this returns @lo. Results are unspecified
+ * for @lo > @hi.
+ */
+static inline u32 prandom_u32_range_state(struct rnd_state *state, u32 lo, u32 hi)
+{
+	return lo + prandom_u32_max_state(state, hi - lo);
+}
+
 /*
  * Handle minimum values for seeds
  */