summaryrefslogtreecommitdiff
path: root/include/linux/random.h
diff options
context:
space:
mode:
authorKent Overstreet <kent.overstreet@linux.dev>2025-03-16 16:08:41 -0400
committerKent Overstreet <kent.overstreet@linux.dev>2025-03-17 14:23:49 -0400
commitc0836924b19ae84ad95d7ec97455c96f61b81201 (patch)
treee48afe4496a7e6ef8c7ec6a1d5d14064f69747ba /include/linux/random.h
parentf42ee45c6e6409ad7c971aa37aef69b97d761006 (diff)
Update bcachefs sources to 4d28432bcc5f bcachefs: Validate bch_sb.offset field
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
Diffstat (limited to 'include/linux/random.h')
-rw-r--r--include/linux/random.h17
1 files changed, 17 insertions, 0 deletions
diff --git a/include/linux/random.h b/include/linux/random.h
index 3203d13c..9b2bb59a 100644
--- a/include/linux/random.h
+++ b/include/linux/random.h
@@ -9,7 +9,9 @@
#include <unistd.h>
#include <sys/syscall.h>
#include <linux/bug.h>
+#include <linux/kernel.h>
#include <linux/log2.h>
+#include <linux/math64.h>
#ifdef SYS_getrandom
static inline int getrandom(void *buf, size_t buflen, unsigned int flags)
@@ -67,4 +69,19 @@ static inline u32 get_random_u32_below(u32 ceil)
}
}
+static inline u64 get_random_u64_below(u64 ceil)
+{
+ if (ceil <= 1)
+ return 0;
+ if (ceil <= U32_MAX)
+ return get_random_u32_below(ceil);
+
+ for (;;) {
+ u64 rand = get_random_u64();
+ u64 mult = ceil * rand;
+ if (likely(mult >= -ceil % ceil))
+ return mul_u64_u64_shr(ceil, rand, 64);
+ }
+}
+
#endif /* _LINUX_RANDOM_H */