[PATCH 27/74] backport: add get_random_u32_inclusive()
Johannes Berg <[email protected]> Fri, 24 May 2024 19:07:39 +0200
| Newsgroups | org.kernel.vger.backports |
|---|---|
| Message-ID | <20240524190907.9b29c2303625.I3466484d655a8d5dbf9208a3f64ea5a926dd844e@changeid> |
From: Johannes Berg <[email protected]> For get_random_u32_inclusive() we need get_random_u32_below(), implement it with prandom_u32_max() as it used to be in iwlwifi. Reviewed-by: Gregory Greenman <[email protected]> Signed-off-by: Johannes Berg <[email protected]> --- backport/backport-include/linux/random.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 backport/backport-include/linux/random.h diff --git a/backport/backport-include/linux/random.h b/backport/backport-include/linux/random.h new file mode 100644 index 000000000000..0a247321785a --- /dev/null +++ b/backport/backport-include/linux/random.h @@ -0,0 +1,21 @@ +#ifndef __BACKPORT_RANDOM_H +#define __BACKPORT_RANDOM_H +#include_next <linux/random.h> +#include <linux/version.h> + +#if LINUX_VERSION_IS_LESS(6,2,0) +static inline u32 get_random_u32_below(u32 ceil) +{ + return prandom_u32_max(ceil); +} + +static inline u32 get_random_u32_inclusive(u32 floor, u32 ceil) +{ + BUILD_BUG_ON_MSG(__builtin_constant_p(floor) && __builtin_constant_p(ceil) && + (floor > ceil || ceil - floor == U32_MAX), + "get_random_u32_inclusive() must take floor <= ceil"); + return floor + get_random_u32_below(ceil - floor + 1); +} +#endif + +#endif /* __BACKPORT_RANDOM_H */ -- 2.45.1