diff options
author | Kent Overstreet <kent.overstreet@linux.dev> | 2025-03-19 10:15:48 -0400 |
---|---|---|
committer | Kent Overstreet <kent.overstreet@linux.dev> | 2025-03-19 10:18:56 -0400 |
commit | 62ea232b09a6ed6464ccd23d3b60f5acf284a77b (patch) | |
tree | 836c5603fce33a4d3c7379607ec48017ec37bea4 /libbcachefs/util.c | |
parent | 64ce740ac675de883ed6ce99be134a1096708063 (diff) |
Update bcachefs sources to dbe591cee299 bcachefs: Add missing smp_rmb()
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
Diffstat (limited to 'libbcachefs/util.c')
-rw-r--r-- | libbcachefs/util.c | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/libbcachefs/util.c b/libbcachefs/util.c index bf555ae7..553de8d8 100644 --- a/libbcachefs/util.c +++ b/libbcachefs/util.c @@ -653,6 +653,27 @@ int bch2_bio_alloc_pages(struct bio *bio, size_t size, gfp_t gfp_mask) return 0; } +u64 bch2_get_random_u64_below(u64 ceil) +{ + if (ceil <= U32_MAX) + return __get_random_u32_below(ceil); + + /* this is the same (clever) algorithm as in __get_random_u32_below() */ + u64 rand = get_random_u64(); + u64 mult = ceil * rand; + + if (unlikely(mult < ceil)) { + u64 bound; + div64_u64_rem(-ceil, ceil, &bound); + while (unlikely(mult < bound)) { + rand = get_random_u64(); + mult = ceil * rand; + } + } + + return mul_u64_u64_shr(ceil, rand, 64); +} + void memcpy_to_bio(struct bio *dst, struct bvec_iter dst_iter, const void *src) { struct bio_vec bv; |