diff mbox series

[27/74] backport: add get_random_u32_inclusive()

Message ID 20240524190907.9b29c2303625.I3466484d655a8d5dbf9208a3f64ea5a926dd844e@changeid (mailing list archive)
State New, archived
Headers show
Series backport updates from Intel | expand

Commit Message

Johannes Berg May 24, 2024, 5:07 p.m. UTC
From: Johannes Berg <johannes.berg@intel.com>

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 <gregory.greenman@intel.com>
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
---
 backport/backport-include/linux/random.h | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
 create mode 100644 backport/backport-include/linux/random.h
diff mbox series

Patch

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 */