]> git.itanic.dy.fi Git - linux-stable/commitdiff
treewide: use prandom_u32_max() when possible, part 1
authorJason A. Donenfeld <Jason@zx2c4.com>
Wed, 5 Oct 2022 14:43:38 +0000 (16:43 +0200)
committerJason A. Donenfeld <Jason@zx2c4.com>
Tue, 11 Oct 2022 23:42:55 +0000 (17:42 -0600)
Rather than incurring a division or requesting too many random bytes for
the given range, use the prandom_u32_max() function, which only takes
the minimum required bytes from the RNG and avoids divisions. This was
done mechanically with this coccinelle script:

@basic@
expression E;
type T;
identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32";
typedef u64;
@@
(
- ((T)get_random_u32() % (E))
+ prandom_u32_max(E)
|
- ((T)get_random_u32() & ((E) - 1))
+ prandom_u32_max(E * XXX_MAKE_SURE_E_IS_POW2)
|
- ((u64)(E) * get_random_u32() >> 32)
+ prandom_u32_max(E)
|
- ((T)get_random_u32() & ~PAGE_MASK)
+ prandom_u32_max(PAGE_SIZE)
)

@multi_line@
identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32";
identifier RAND;
expression E;
@@

-       RAND = get_random_u32();
        ... when != RAND
-       RAND %= (E);
+       RAND = prandom_u32_max(E);

// Find a potential literal
@literal_mask@
expression LITERAL;
type T;
identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32";
position p;
@@

        ((T)get_random_u32()@p & (LITERAL))

// Add one to the literal.
@script:python add_one@
literal << literal_mask.LITERAL;
RESULT;
@@

value = None
if literal.startswith('0x'):
        value = int(literal, 16)
elif literal[0] in '123456789':
        value = int(literal, 10)
if value is None:
        print("I don't know how to handle %s" % (literal))
        cocci.include_match(False)
elif value == 2**32 - 1 or value == 2**31 - 1 or value == 2**24 - 1 or value == 2**16 - 1 or value == 2**8 - 1:
        print("Skipping 0x%x for cleanup elsewhere" % (value))
        cocci.include_match(False)
elif value & (value + 1) != 0:
        print("Skipping 0x%x because it's not a power of two minus one" % (value))
        cocci.include_match(False)
elif literal.startswith('0x'):
        coccinelle.RESULT = cocci.make_expr("0x%x" % (value + 1))
else:
        coccinelle.RESULT = cocci.make_expr("%d" % (value + 1))

// Replace the literal mask with the calculated result.
@plus_one@
expression literal_mask.LITERAL;
position literal_mask.p;
expression add_one.RESULT;
identifier FUNC;
@@

-       (FUNC()@p & (LITERAL))
+       prandom_u32_max(RESULT)

@collapse_ret@
type T;
identifier VAR;
expression E;
@@

 {
-       T VAR;
-       VAR = (E);
-       return VAR;
+       return E;
 }

@drop_var@
type T;
identifier VAR;
@@

 {
-       T VAR;
        ... when != VAR
 }

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Yury Norov <yury.norov@gmail.com>
Reviewed-by: KP Singh <kpsingh@kernel.org>
Reviewed-by: Jan Kara <jack@suse.cz> # for ext4 and sbitmap
Reviewed-by: Christoph Böhmwalder <christoph.boehmwalder@linbit.com> # for drbd
Acked-by: Jakub Kicinski <kuba@kernel.org>
Acked-by: Heiko Carstens <hca@linux.ibm.com> # for s390
Acked-by: Ulf Hansson <ulf.hansson@linaro.org> # for mmc
Acked-by: Darrick J. Wong <djwong@kernel.org> # for xfs
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
89 files changed:
arch/arm/kernel/process.c
arch/arm64/kernel/process.c
arch/loongarch/kernel/process.c
arch/loongarch/kernel/vdso.c
arch/mips/kernel/process.c
arch/mips/kernel/vdso.c
arch/parisc/kernel/vdso.c
arch/powerpc/kernel/process.c
arch/s390/kernel/process.c
arch/s390/kernel/vdso.c
arch/sparc/vdso/vma.c
arch/um/kernel/process.c
arch/x86/entry/vdso/vma.c
arch/x86/kernel/module.c
arch/x86/kernel/process.c
arch/x86/mm/pat/cpa-test.c
crypto/testmgr.c
drivers/block/drbd/drbd_receiver.c
drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c
drivers/infiniband/core/cma.c
drivers/infiniband/hw/cxgb4/id_table.c
drivers/infiniband/hw/hns/hns_roce_ah.c
drivers/infiniband/ulp/rtrs/rtrs-clt.c
drivers/md/bcache/request.c
drivers/media/test-drivers/vivid/vivid-touch-cap.c
drivers/mmc/core/core.c
drivers/mmc/host/dw_mmc.c
drivers/mtd/nand/raw/nandsim.c
drivers/mtd/tests/mtd_nandecctest.c
drivers/mtd/tests/stresstest.c
drivers/mtd/ubi/debug.c
drivers/mtd/ubi/debug.h
drivers/net/ethernet/broadcom/cnic.c
drivers/net/ethernet/chelsio/inline_crypto/chtls/chtls_io.c
drivers/net/hamradio/baycom_epp.c
drivers/net/hamradio/hdlcdrv.c
drivers/net/hamradio/yam.c
drivers/net/phy/at803x.c
drivers/net/wireless/broadcom/brcm80211/brcmfmac/p2p.c
drivers/net/wireless/intel/iwlwifi/mvm/mac-ctxt.c
drivers/scsi/fcoe/fcoe_ctlr.c
drivers/scsi/qedi/qedi_main.c
fs/ceph/inode.c
fs/ceph/mdsmap.c
fs/ext4/super.c
fs/f2fs/gc.c
fs/f2fs/segment.c
fs/ubifs/debug.c
fs/ubifs/lpt_commit.c
fs/ubifs/tnc_commit.c
fs/xfs/libxfs/xfs_alloc.c
fs/xfs/libxfs/xfs_ialloc.c
fs/xfs/xfs_error.c
include/linux/nodemask.h
kernel/bpf/core.c
kernel/locking/test-ww_mutex.c
kernel/time/clocksource.c
lib/fault-inject.c
lib/find_bit_benchmark.c
lib/kobject.c
lib/reed_solomon/test_rslib.c
lib/sbitmap.c
lib/test-string_helpers.c
lib/test_hexdump.c
lib/test_list_sort.c
mm/kasan/kasan_test.c
mm/slub.c
net/802/garp.c
net/802/mrp.c
net/ceph/mon_client.c
net/ceph/osd_client.c
net/core/neighbour.c
net/core/pktgen.c
net/core/stream.c
net/ipv4/igmp.c
net/ipv4/inet_connection_sock.c
net/ipv4/inet_hashtables.c
net/ipv6/addrconf.c
net/ipv6/mcast.c
net/netfilter/ipvs/ip_vs_twos.c
net/packet/af_packet.c
net/sched/act_gact.c
net/sched/act_sample.c
net/sched/sch_netem.c
net/sctp/socket.c
net/sunrpc/cache.c
net/sunrpc/xprtsock.c
net/tipc/socket.c
net/xfrm/xfrm_state.c

index 96f3fbd51764292a8c96c456420ffb4e3c0500a1..129279b33b1d4c4c6b29a5a23c8885a4b13b1f06 100644 (file)
@@ -375,7 +375,7 @@ static unsigned long sigpage_addr(const struct mm_struct *mm,
 
        slots = ((last - first) >> PAGE_SHIFT) + 1;
 
-       offset = get_random_int() % slots;
+       offset = prandom_u32_max(slots);
 
        addr = first + (offset << PAGE_SHIFT);
 
index 92bcc1768f0b997b761771ba2dad659a0fe16953..87203429f80233c4c30e4406ba442978f97f401a 100644 (file)
@@ -595,7 +595,7 @@ unsigned long __get_wchan(struct task_struct *p)
 unsigned long arch_align_stack(unsigned long sp)
 {
        if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
-               sp -= get_random_int() & ~PAGE_MASK;
+               sp -= prandom_u32_max(PAGE_SIZE);
        return sp & ~0xf;
 }
 
index 660492f064e7e4584fa2f04088506fc8ed7076f2..1256e3582475fbc06f4f7b712252ddaf2eeb84e0 100644 (file)
@@ -293,7 +293,7 @@ unsigned long stack_top(void)
 unsigned long arch_align_stack(unsigned long sp)
 {
        if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
-               sp -= get_random_int() & ~PAGE_MASK;
+               sp -= prandom_u32_max(PAGE_SIZE);
 
        return sp & STACK_ALIGN;
 }
index f32c38abd791589527bbba2eda8bd91a7065244a..8c9826062652e3b39a6ee2c03d02370b519eb07d 100644 (file)
@@ -78,7 +78,7 @@ static unsigned long vdso_base(void)
        unsigned long base = STACK_TOP;
 
        if (current->flags & PF_RANDOMIZE) {
-               base += get_random_int() & (VDSO_RANDOMIZE_SIZE - 1);
+               base += prandom_u32_max(VDSO_RANDOMIZE_SIZE);
                base = PAGE_ALIGN(base);
        }
 
index 35b912bce42975340d6674ef2a607fa84f165696..bbe9ce471791e7895fd57c4756474b36ffa1a4bf 100644 (file)
@@ -711,7 +711,7 @@ unsigned long mips_stack_top(void)
 unsigned long arch_align_stack(unsigned long sp)
 {
        if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
-               sp -= get_random_int() & ~PAGE_MASK;
+               sp -= prandom_u32_max(PAGE_SIZE);
 
        return sp & ALMASK;
 }
index b2cc2c2dd4bfc44493dd11526b1eebe3b23ef2d2..5fd9bf1d596c6451b6d819952427c11389671f2a 100644 (file)
@@ -79,7 +79,7 @@ static unsigned long vdso_base(void)
        }
 
        if (current->flags & PF_RANDOMIZE) {
-               base += get_random_int() & (VDSO_RANDOMIZE_SIZE - 1);
+               base += prandom_u32_max(VDSO_RANDOMIZE_SIZE);
                base = PAGE_ALIGN(base);
        }
 
index 63dc44c4c246bc755c3325faaa8f07b3188bb810..47e5960a2f961f44c70336a35be3c970ae7d2c3b 100644 (file)
@@ -75,7 +75,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm,
 
        map_base = mm->mmap_base;
        if (current->flags & PF_RANDOMIZE)
-               map_base -= (get_random_int() & 0x1f) * PAGE_SIZE;
+               map_base -= prandom_u32_max(0x20) * PAGE_SIZE;
 
        vdso_text_start = get_unmapped_area(NULL, map_base, vdso_text_len, 0, 0);
 
index 37df0428e4fbea69344f09b13f2b884c9fbb2197..599391c235738b05ea3f1e6bec1ae011b71a50a5 100644 (file)
@@ -2308,6 +2308,6 @@ void notrace __ppc64_runlatch_off(void)
 unsigned long arch_align_stack(unsigned long sp)
 {
        if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
-               sp -= get_random_int() & ~PAGE_MASK;
+               sp -= prandom_u32_max(PAGE_SIZE);
        return sp & ~0xf;
 }
index d5119e039d8551194e19fd634267780c4b7acb94..5ec78555dd2e5c70e7683e76729a2b5ab8bc995f 100644 (file)
@@ -224,7 +224,7 @@ unsigned long __get_wchan(struct task_struct *p)
 unsigned long arch_align_stack(unsigned long sp)
 {
        if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
-               sp -= get_random_int() & ~PAGE_MASK;
+               sp -= prandom_u32_max(PAGE_SIZE);
        return sp & ~0xf;
 }
 
index 535099f2736dab852b318ee0b443bb3d2fd3dbb5..3105ca5bd4701d14831078559431aaed0f70d1d1 100644 (file)
@@ -227,7 +227,7 @@ static unsigned long vdso_addr(unsigned long start, unsigned long len)
        end -= len;
 
        if (end > start) {
-               offset = get_random_int() % (((end - start) >> PAGE_SHIFT) + 1);
+               offset = prandom_u32_max(((end - start) >> PAGE_SHIFT) + 1);
                addr = start + (offset << PAGE_SHIFT);
        } else {
                addr = start;
index cc19e09b0fa1e545b629050eded90701e50267e0..ae9a86cb6f3d978245005c06777f12990b9fcad3 100644 (file)
@@ -354,7 +354,7 @@ static unsigned long vdso_addr(unsigned long start, unsigned int len)
        unsigned int offset;
 
        /* This loses some more bits than a modulo, but is cheaper */
-       offset = get_random_int() & (PTRS_PER_PTE - 1);
+       offset = prandom_u32_max(PTRS_PER_PTE);
        return start + (offset << PAGE_SHIFT);
 }
 
index 80b90b1276a1937030f570df079d9f72cb11b72f..010bc422a09dd0ad29093b661f9afe5f7f32e54f 100644 (file)
@@ -356,7 +356,7 @@ int singlestepping(void * t)
 unsigned long arch_align_stack(unsigned long sp)
 {
        if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
-               sp -= get_random_int() % 8192;
+               sp -= prandom_u32_max(8192);
        return sp & ~0xf;
 }
 #endif
index 6292b960037b79d50d187d0982455df8ecd51fb4..311eae30e08944c767995572bce85965038cb145 100644 (file)
@@ -327,7 +327,7 @@ static unsigned long vdso_addr(unsigned long start, unsigned len)
        end -= len;
 
        if (end > start) {
-               offset = get_random_int() % (((end - start) >> PAGE_SHIFT) + 1);
+               offset = prandom_u32_max(((end - start) >> PAGE_SHIFT) + 1);
                addr = start + (offset << PAGE_SHIFT);
        } else {
                addr = start;
index b1abf663417cdef7eff8f58fbbdfa3732a8ff6c9..c032edcd3d95e06941a4810e09ba18da1707b196 100644 (file)
@@ -53,7 +53,7 @@ static unsigned long int get_module_load_offset(void)
                 */
                if (module_load_offset == 0)
                        module_load_offset =
-                               (get_random_int() % 1024 + 1) * PAGE_SIZE;
+                               (prandom_u32_max(1024) + 1) * PAGE_SIZE;
                mutex_unlock(&module_kaslr_mutex);
        }
        return module_load_offset;
index 58a6ea472db92b4efdb7ddaf5eea68034525bbb7..c21b7347a26dd5f26df8cf1802375059dbc659fd 100644 (file)
@@ -965,7 +965,7 @@ early_param("idle", idle_setup);
 unsigned long arch_align_stack(unsigned long sp)
 {
        if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
-               sp -= get_random_int() % 8192;
+               sp -= prandom_u32_max(8192);
        return sp & ~0xf;
 }
 
index 0612a73638a81290556c9db3b82ec191ec37fb3d..423b21e80929ab12adfa742b50a931656f96d4cb 100644 (file)
@@ -136,10 +136,10 @@ static int pageattr_test(void)
        failed += print_split(&sa);
 
        for (i = 0; i < NTEST; i++) {
-               unsigned long pfn = prandom_u32() % max_pfn_mapped;
+               unsigned long pfn = prandom_u32_max(max_pfn_mapped);
 
                addr[i] = (unsigned long)__va(pfn << PAGE_SHIFT);
-               len[i] = prandom_u32() % NPAGES;
+               len[i] = prandom_u32_max(NPAGES);
                len[i] = min_t(unsigned long, len[i], max_pfn_mapped - pfn - 1);
 
                if (len[i] == 0)
index e4bb03b8b9245601b724dd52d7627858b2f0b3b4..bff4833dbe7c8477421e9680d8d026cbeb452404 100644 (file)
@@ -855,9 +855,9 @@ static int prepare_keybuf(const u8 *key, unsigned int ksize,
 /* Generate a random length in range [0, max_len], but prefer smaller values */
 static unsigned int generate_random_length(unsigned int max_len)
 {
-       unsigned int len = prandom_u32() % (max_len + 1);
+       unsigned int len = prandom_u32_max(max_len + 1);
 
-       switch (prandom_u32() % 4) {
+       switch (prandom_u32_max(4)) {
        case 0:
                return len % 64;
        case 1:
@@ -874,14 +874,14 @@ static void flip_random_bit(u8 *buf, size_t size)
 {
        size_t bitpos;
 
-       bitpos = prandom_u32() % (size * 8);
+       bitpos = prandom_u32_max(size * 8);
        buf[bitpos / 8] ^= 1 << (bitpos % 8);
 }
 
 /* Flip a random byte in the given nonempty data buffer */
 static void flip_random_byte(u8 *buf, size_t size)
 {
-       buf[prandom_u32() % size] ^= 0xff;
+       buf[prandom_u32_max(size)] ^= 0xff;
 }
 
 /* Sometimes make some random changes to the given nonempty data buffer */
@@ -891,15 +891,15 @@ static void mutate_buffer(u8 *buf, size_t size)
        size_t i;
 
        /* Sometimes flip some bits */
-       if (prandom_u32() % 4 == 0) {
-               num_flips = min_t(size_t, 1 << (prandom_u32() % 8), size * 8);
+       if (prandom_u32_max(4) == 0) {
+               num_flips = min_t(size_t, 1 << prandom_u32_max(8), size * 8);
                for (i = 0; i < num_flips; i++)
                        flip_random_bit(buf, size);
        }
 
        /* Sometimes flip some bytes */
-       if (prandom_u32() % 4 == 0) {
-               num_flips = min_t(size_t, 1 << (prandom_u32() % 8), size);
+       if (prandom_u32_max(4) == 0) {
+               num_flips = min_t(size_t, 1 << prandom_u32_max(8), size);
                for (i = 0; i < num_flips; i++)
                        flip_random_byte(buf, size);
        }
@@ -915,11 +915,11 @@ static void generate_random_bytes(u8 *buf, size_t count)
        if (count == 0)
                return;
 
-       switch (prandom_u32() % 8) { /* Choose a generation strategy */
+       switch (prandom_u32_max(8)) { /* Choose a generation strategy */
        case 0:
        case 1:
                /* All the same byte, plus optional mutations */
-               switch (prandom_u32() % 4) {
+               switch (prandom_u32_max(4)) {
                case 0:
                        b = 0x00;
                        break;
@@ -959,24 +959,24 @@ static char *generate_random_sgl_divisions(struct test_sg_division *divs,
                unsigned int this_len;
                const char *flushtype_str;
 
-               if (div == &divs[max_divs - 1] || prandom_u32() % 2 == 0)
+               if (div == &divs[max_divs - 1] || prandom_u32_max(2) == 0)
                        this_len = remaining;
                else
-                       this_len = 1 + (prandom_u32() % remaining);
+                       this_len = 1 + prandom_u32_max(remaining);
                div->proportion_of_total = this_len;
 
-               if (prandom_u32() % 4 == 0)
-                       div->offset = (PAGE_SIZE - 128) + (prandom_u32() % 128);
-               else if (prandom_u32() % 2 == 0)
-                       div->offset = prandom_u32() % 32;
+               if (prandom_u32_max(4) == 0)
+                       div->offset = (PAGE_SIZE - 128) + prandom_u32_max(128);
+               else if (prandom_u32_max(2) == 0)
+                       div->offset = prandom_u32_max(32);
                else
-                       div->offset = prandom_u32() % PAGE_SIZE;
-               if (prandom_u32() % 8 == 0)
+                       div->offset = prandom_u32_max(PAGE_SIZE);
+               if (prandom_u32_max(8) == 0)
                        div->offset_relative_to_alignmask = true;
 
                div->flush_type = FLUSH_TYPE_NONE;
                if (gen_flushes) {
-                       switch (prandom_u32() % 4) {
+                       switch (prandom_u32_max(4)) {
                        case 0:
                                div->flush_type = FLUSH_TYPE_REIMPORT;
                                break;
@@ -988,7 +988,7 @@ static char *generate_random_sgl_divisions(struct test_sg_division *divs,
 
                if (div->flush_type != FLUSH_TYPE_NONE &&
                    !(req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) &&
-                   prandom_u32() % 2 == 0)
+                   prandom_u32_max(2) == 0)
                        div->nosimd = true;
 
                switch (div->flush_type) {
@@ -1035,7 +1035,7 @@ static void generate_random_testvec_config(struct testvec_config *cfg,
 
        p += scnprintf(p, end - p, "random:");
 
-       switch (prandom_u32() % 4) {
+       switch (prandom_u32_max(4)) {
        case 0:
        case 1:
                cfg->inplace_mode = OUT_OF_PLACE;
@@ -1050,12 +1050,12 @@ static void generate_random_testvec_config(struct testvec_config *cfg,
                break;
        }
 
-       if (prandom_u32() % 2 == 0) {
+       if (prandom_u32_max(2) == 0) {
                cfg->req_flags |= CRYPTO_TFM_REQ_MAY_SLEEP;
                p += scnprintf(p, end - p, " may_sleep");
        }
 
-       switch (prandom_u32() % 4) {
+       switch (prandom_u32_max(4)) {
        case 0:
                cfg->finalization_type = FINALIZATION_TYPE_FINAL;
                p += scnprintf(p, end - p, " use_final");
@@ -1071,7 +1071,7 @@ static void generate_random_testvec_config(struct testvec_config *cfg,
        }
 
        if (!(cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) &&
-           prandom_u32() % 2 == 0) {
+           prandom_u32_max(2) == 0) {
                cfg->nosimd = true;
                p += scnprintf(p, end - p, " nosimd");
        }
@@ -1084,7 +1084,7 @@ static void generate_random_testvec_config(struct testvec_config *cfg,
                                          cfg->req_flags);
        p += scnprintf(p, end - p, "]");
 
-       if (cfg->inplace_mode == OUT_OF_PLACE && prandom_u32() % 2 == 0) {
+       if (cfg->inplace_mode == OUT_OF_PLACE && prandom_u32_max(2) == 0) {
                p += scnprintf(p, end - p, " dst_divs=[");
                p = generate_random_sgl_divisions(cfg->dst_divs,
                                                  ARRAY_SIZE(cfg->dst_divs),
@@ -1093,13 +1093,13 @@ static void generate_random_testvec_config(struct testvec_config *cfg,
                p += scnprintf(p, end - p, "]");
        }
 
-       if (prandom_u32() % 2 == 0) {
-               cfg->iv_offset = 1 + (prandom_u32() % MAX_ALGAPI_ALIGNMASK);
+       if (prandom_u32_max(2) == 0) {
+               cfg->iv_offset = 1 + prandom_u32_max(MAX_ALGAPI_ALIGNMASK);
                p += scnprintf(p, end - p, " iv_offset=%u", cfg->iv_offset);
        }
 
-       if (prandom_u32() % 2 == 0) {
-               cfg->key_offset = 1 + (prandom_u32() % MAX_ALGAPI_ALIGNMASK);
+       if (prandom_u32_max(2) == 0) {
+               cfg->key_offset = 1 + prandom_u32_max(MAX_ALGAPI_ALIGNMASK);
                p += scnprintf(p, end - p, " key_offset=%u", cfg->key_offset);
        }
 
@@ -1652,8 +1652,8 @@ static void generate_random_hash_testvec(struct shash_desc *desc,
        vec->ksize = 0;
        if (maxkeysize) {
                vec->ksize = maxkeysize;
-               if (prandom_u32() % 4 == 0)
-                       vec->ksize = 1 + (prandom_u32() % maxkeysize);
+               if (prandom_u32_max(4) == 0)
+                       vec->ksize = 1 + prandom_u32_max(maxkeysize);
                generate_random_bytes((u8 *)vec->key, vec->ksize);
 
                vec->setkey_error = crypto_shash_setkey(desc->tfm, vec->key,
@@ -2218,13 +2218,13 @@ static void mutate_aead_message(struct aead_testvec *vec, bool aad_iv,
        const unsigned int aad_tail_size = aad_iv ? ivsize : 0;
        const unsigned int authsize = vec->clen - vec->plen;
 
-       if (prandom_u32() % 2 == 0 && vec->alen > aad_tail_size) {
+       if (prandom_u32_max(2) == 0 && vec->alen > aad_tail_size) {
                 /* Mutate the AAD */
                flip_random_bit((u8 *)vec->assoc, vec->alen - aad_tail_size);
-               if (prandom_u32() % 2 == 0)
+               if (prandom_u32_max(2) == 0)
                        return;
        }
-       if (prandom_u32() % 2 == 0) {
+       if (prandom_u32_max(2) == 0) {
                /* Mutate auth tag (assuming it's at the end of ciphertext) */
                flip_random_bit((u8 *)vec->ctext + vec->plen, authsize);
        } else {
@@ -2249,7 +2249,7 @@ static void generate_aead_message(struct aead_request *req,
        const unsigned int ivsize = crypto_aead_ivsize(tfm);
        const unsigned int authsize = vec->clen - vec->plen;
        const bool inauthentic = (authsize >= MIN_COLLISION_FREE_AUTHSIZE) &&
-                                (prefer_inauthentic || prandom_u32() % 4 == 0);
+                                (prefer_inauthentic || prandom_u32_max(4) == 0);
 
        /* Generate the AAD. */
        generate_random_bytes((u8 *)vec->assoc, vec->alen);
@@ -2257,7 +2257,7 @@ static void generate_aead_message(struct aead_request *req,
                /* Avoid implementation-defined behavior. */
                memcpy((u8 *)vec->assoc + vec->alen - ivsize, vec->iv, ivsize);
 
-       if (inauthentic && prandom_u32() % 2 == 0) {
+       if (inauthentic && prandom_u32_max(2) == 0) {
                /* Generate a random ciphertext. */
                generate_random_bytes((u8 *)vec->ctext, vec->clen);
        } else {
@@ -2321,8 +2321,8 @@ static void generate_random_aead_testvec(struct aead_request *req,
 
        /* Key: length in [0, maxkeysize], but usually choose maxkeysize */
        vec->klen = maxkeysize;
-       if (prandom_u32() % 4 == 0)
-               vec->klen = prandom_u32() % (maxkeysize + 1);
+       if (prandom_u32_max(4) == 0)
+               vec->klen = prandom_u32_max(maxkeysize + 1);
        generate_random_bytes((u8 *)vec->key, vec->klen);
        vec->setkey_error = crypto_aead_setkey(tfm, vec->key, vec->klen);
 
@@ -2331,8 +2331,8 @@ static void generate_random_aead_testvec(struct aead_request *req,
 
        /* Tag length: in [0, maxauthsize], but usually choose maxauthsize */
        authsize = maxauthsize;
-       if (prandom_u32() % 4 == 0)
-               authsize = prandom_u32() % (maxauthsize + 1);
+       if (prandom_u32_max(4) == 0)
+               authsize = prandom_u32_max(maxauthsize + 1);
        if (prefer_inauthentic && authsize < MIN_COLLISION_FREE_AUTHSIZE)
                authsize = MIN_COLLISION_FREE_AUTHSIZE;
        if (WARN_ON(authsize > maxdatasize))
@@ -2342,7 +2342,7 @@ static void generate_random_aead_testvec(struct aead_request *req,
 
        /* AAD, plaintext, and ciphertext lengths */
        total_len = generate_random_length(maxdatasize);
-       if (prandom_u32() % 4 == 0)
+       if (prandom_u32_max(4) == 0)
                vec->alen = 0;
        else
                vec->alen = generate_random_length(total_len);
@@ -2958,8 +2958,8 @@ static void generate_random_cipher_testvec(struct skcipher_request *req,
 
        /* Key: length in [0, maxkeysize], but usually choose maxkeysize */
        vec->klen = maxkeysize;
-       if (prandom_u32() % 4 == 0)
-               vec->klen = prandom_u32() % (maxkeysize + 1);
+       if (prandom_u32_max(4) == 0)
+               vec->klen = prandom_u32_max(maxkeysize + 1);
        generate_random_bytes((u8 *)vec->key, vec->klen);
        vec->setkey_error = crypto_skcipher_setkey(tfm, vec->key, vec->klen);
 
index c897c457203652a4c7d86dc185af5c3b440fcd03..ee69d50ba4fd3bd977fd4c0da72d3a2d4c59ab32 100644 (file)
@@ -781,7 +781,7 @@ static struct socket *drbd_wait_for_connect(struct drbd_connection *connection,
 
        timeo = connect_int * HZ;
        /* 28.5% random jitter */
-       timeo += (prandom_u32() & 1) ? timeo / 7 : -timeo / 7;
+       timeo += prandom_u32_max(2) ? timeo / 7 : -timeo / 7;
 
        err = wait_for_completion_interruptible_timeout(&ad->door_bell, timeo);
        if (err <= 0)
@@ -1004,7 +1004,7 @@ static int conn_connect(struct drbd_connection *connection)
                                drbd_warn(connection, "Error receiving initial packet\n");
                                sock_release(s);
 randomize:
-                               if (prandom_u32() & 1)
+                               if (prandom_u32_max(2))
                                        goto retry;
                        }
                }
index cd75b0ca2555f49dde7b8a80d14e9bd97361700f..845023c14eb36f1b525d943fe632e58da95f82d8 100644 (file)
@@ -2424,7 +2424,7 @@ gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv,
        /* Check whether the file_priv has already selected one ring. */
        if ((int)file_priv->bsd_engine < 0)
                file_priv->bsd_engine =
-                       get_random_int() % num_vcs_engines(dev_priv);
+                       prandom_u32_max(num_vcs_engines(dev_priv));
 
        return file_priv->bsd_engine;
 }
index 70da57ef2eeb68e162d30da50de73d41b46dd82d..cc2222b85c88174a1b3b9c49beb05d701df78e2a 100644 (file)
@@ -3807,7 +3807,7 @@ static int cma_alloc_any_port(enum rdma_ucm_port_space ps,
 
        inet_get_local_port_range(net, &low, &high);
        remaining = (high - low) + 1;
-       rover = prandom_u32() % remaining + low;
+       rover = prandom_u32_max(remaining) + low;
 retry:
        if (last_used_port != rover) {
                struct rdma_bind_list *bind_list;
index f64e7e02b129f1a4bced436cd4fc570d51a986c5..280d61466855676dfd17df8b2ee154c01e62810e 100644 (file)
@@ -54,7 +54,7 @@ u32 c4iw_id_alloc(struct c4iw_id_table *alloc)
 
        if (obj < alloc->max) {
                if (alloc->flags & C4IW_ID_TABLE_F_RANDOM)
-                       alloc->last += prandom_u32() % RANDOM_SKIP;
+                       alloc->last += prandom_u32_max(RANDOM_SKIP);
                else
                        alloc->last = obj + 1;
                if (alloc->last >= alloc->max)
@@ -85,7 +85,7 @@ int c4iw_id_table_alloc(struct c4iw_id_table *alloc, u32 start, u32 num,
        alloc->start = start;
        alloc->flags = flags;
        if (flags & C4IW_ID_TABLE_F_RANDOM)
-               alloc->last = prandom_u32() % RANDOM_SKIP;
+               alloc->last = prandom_u32_max(RANDOM_SKIP);
        else
                alloc->last = 0;
        alloc->max = num;
index 492b122d052199f13c062d08f252b5a9be171080..480c062dd04f1f04c82087c0b6f566263a2e0c7c 100644 (file)
@@ -41,9 +41,8 @@ static inline u16 get_ah_udp_sport(const struct rdma_ah_attr *ah_attr)
        u16 sport;
 
        if (!fl)
-               sport = get_random_u32() %
-                       (IB_ROCE_UDP_ENCAP_VALID_PORT_MAX + 1 -
-                        IB_ROCE_UDP_ENCAP_VALID_PORT_MIN) +
+               sport = prandom_u32_max(IB_ROCE_UDP_ENCAP_VALID_PORT_MAX + 1 -
+                                       IB_ROCE_UDP_ENCAP_VALID_PORT_MIN) +
                        IB_ROCE_UDP_ENCAP_VALID_PORT_MIN;
        else
                sport = rdma_flow_label_to_udp_sport(fl);
index 758e1d7ebc36549e5313c0a1aabdd23b0739ec32..8546b8816524cba880626287d1dd7568bf683167 100644 (file)
@@ -1517,8 +1517,7 @@ static void rtrs_clt_err_recovery_work(struct work_struct *work)
        rtrs_clt_stop_and_destroy_conns(clt_path);
        queue_delayed_work(rtrs_wq, &clt_path->reconnect_dwork,
                           msecs_to_jiffies(delay_ms +
-                                           prandom_u32() %
-                                           RTRS_RECONNECT_SEED));
+                                           prandom_u32_max(RTRS_RECONNECT_SEED)));
 }
 
 static struct rtrs_clt_path *alloc_path(struct rtrs_clt_sess *clt,
index f2c5a7e06fa9366667736e6952bf6438f72cc4f1..3427555b0ccae4d3f4a0992b8d741eced97e45c7 100644 (file)
@@ -401,7 +401,7 @@ static bool check_should_bypass(struct cached_dev *dc, struct bio *bio)
        }
 
        if (bypass_torture_test(dc)) {
-               if ((get_random_int() & 3) == 3)
+               if (prandom_u32_max(4) == 3)
                        goto skip;
                else
                        goto rescale;
index 64e3e4cb30c20ce893c84933d39dafddd2018df0..792660a85bc11bd3171a44416bfd1b89847f2d3a 100644 (file)
@@ -221,7 +221,7 @@ static void vivid_fill_buff_noise(__s16 *tch_buf, int size)
 
 static inline int get_random_pressure(void)
 {
-       return get_random_int() % VIVID_PRESSURE_LIMIT;
+       return prandom_u32_max(VIVID_PRESSURE_LIMIT);
 }
 
 static void vivid_tch_buf_set(struct v4l2_pix_format *f,
index ef53a25788248ed6d57472f7a9cf26ee979f254e..95fa8fb1d45f2b5475805147b0bcf40b601abbfd 100644 (file)
@@ -97,8 +97,8 @@ static void mmc_should_fail_request(struct mmc_host *host,
            !should_fail(&host->fail_mmc_request, data->blksz * data->blocks))
                return;
 
-       data->error = data_errors[prandom_u32() % ARRAY_SIZE(data_errors)];
-       data->bytes_xfered = (prandom_u32() % (data->bytes_xfered >> 9)) << 9;
+       data->error = data_errors[prandom_u32_max(ARRAY_SIZE(data_errors))];
+       data->bytes_xfered = prandom_u32_max(data->bytes_xfered >> 9) << 9;
 }
 
 #else /* CONFIG_FAIL_MMC_REQUEST */
index 581614196a8413317c651e9d405a891e2c102add..c78bbc22e0d1eaaa05df6ca0bb38e07191f9d0c1 100644 (file)
@@ -1858,7 +1858,7 @@ static void dw_mci_start_fault_timer(struct dw_mci *host)
         * Try to inject the error at random points during the data transfer.
         */
        hrtimer_start(&host->fault_timer,
-                     ms_to_ktime(prandom_u32() % 25),
+                     ms_to_ktime(prandom_u32_max(25)),
                      HRTIMER_MODE_REL);
 }
 
index 24beade95c7fb1fa32444911e8c592824a887c9e..50bcf745e81645aa03efa6f4310101228d59073f 100644 (file)
@@ -1405,9 +1405,9 @@ static void ns_do_bit_flips(struct nandsim *ns, int num)
        if (bitflips && prandom_u32() < (1 << 22)) {
                int flips = 1;
                if (bitflips > 1)
-                       flips = (prandom_u32() % (int) bitflips) + 1;
+                       flips = prandom_u32_max(bitflips) + 1;
                while (flips--) {
-                       int pos = prandom_u32() % (num * 8);
+                       int pos = prandom_u32_max(num * 8);
                        ns->buf.byte[pos / 8] ^= (1 << (pos % 8));
                        NS_WARN("read_page: flipping bit %d in page %d "
                                "reading from %d ecc: corrected=%u failed=%u\n",
index c4f271314f5264acf7f6c0bfa1475f00619e0074..1c7201b0f372dfeb45ac107783b83d36f3f4de53 100644 (file)
@@ -47,7 +47,7 @@ struct nand_ecc_test {
 static void single_bit_error_data(void *error_data, void *correct_data,
                                size_t size)
 {
-       unsigned int offset = prandom_u32() % (size * BITS_PER_BYTE);
+       unsigned int offset = prandom_u32_max(size * BITS_PER_BYTE);
 
        memcpy(error_data, correct_data, size);
        __change_bit_le(offset, error_data);
@@ -58,9 +58,9 @@ static void double_bit_error_data(void *error_data, void *correct_data,
 {
        unsigned int offset[2];
 
-       offset[0] = prandom_u32() % (size * BITS_PER_BYTE);
+       offset[0] = prandom_u32_max(size * BITS_PER_BYTE);
        do {
-               offset[1] = prandom_u32() % (size * BITS_PER_BYTE);
+               offset[1] = prandom_u32_max(size * BITS_PER_BYTE);
        } while (offset[0] == offset[1]);
 
        memcpy(error_data, correct_data, size);
@@ -71,7 +71,7 @@ static void double_bit_error_data(void *error_data, void *correct_data,
 
 static unsigned int random_ecc_bit(size_t size)
 {
-       unsigned int offset = prandom_u32() % (3 * BITS_PER_BYTE);
+       unsigned int offset = prandom_u32_max(3 * BITS_PER_BYTE);
 
        if (size == 256) {
                /*
@@ -79,7 +79,7 @@ static unsigned int random_ecc_bit(size_t size)
                 * and 17th bit) in ECC code for 256 byte data block
                 */
                while (offset == 16 || offset == 17)
-                       offset = prandom_u32() % (3 * BITS_PER_BYTE);
+                       offset = prandom_u32_max(3 * BITS_PER_BYTE);
        }
 
        return offset;
index cb29c8c1b37033f118333121ab13d7abc9eea68c..d2faaca7f19d79f3727012f4bd51b4b4798d21c5 100644 (file)
@@ -45,9 +45,8 @@ static int rand_eb(void)
        unsigned int eb;
 
 again:
-       eb = prandom_u32();
        /* Read or write up 2 eraseblocks at a time - hence 'ebcnt - 1' */
-       eb %= (ebcnt - 1);
+       eb = prandom_u32_max(ebcnt - 1);
        if (bbt[eb])
                goto again;
        return eb;
@@ -55,20 +54,12 @@ static int rand_eb(void)
 
 static int rand_offs(void)
 {
-       unsigned int offs;
-
-       offs = prandom_u32();
-       offs %= bufsize;
-       return offs;
+       return prandom_u32_max(bufsize);
 }
 
 static int rand_len(int offs)
 {
-       unsigned int len;
-
-       len = prandom_u32();
-       len %= (bufsize - offs);
-       return len;
+       return prandom_u32_max(bufsize - offs);
 }
 
 static int do_read(void)
@@ -127,7 +118,7 @@ static int do_write(void)
 
 static int do_operation(void)
 {
-       if (prandom_u32() & 1)
+       if (prandom_u32_max(2))
                return do_read();
        else
                return do_write();
index 31d427ee191a3805624025389fc55f7e10b6a7fb..908d0e0885574bd7a68e82d4b49372bb908ba7f7 100644 (file)
@@ -590,7 +590,7 @@ int ubi_dbg_power_cut(struct ubi_device *ubi, int caller)
 
                if (ubi->dbg.power_cut_max > ubi->dbg.power_cut_min) {
                        range = ubi->dbg.power_cut_max - ubi->dbg.power_cut_min;
-                       ubi->dbg.power_cut_counter += prandom_u32() % range;
+                       ubi->dbg.power_cut_counter += prandom_u32_max(range);
                }
                return 0;
        }
index 118248a5d7d487f005d7b35613e7bb3b88a5918e..dc8d8f83657a0f782c369816074c75f22891f27b 100644 (file)
@@ -73,7 +73,7 @@ static inline int ubi_dbg_is_bgt_disabled(const struct ubi_device *ubi)
 static inline int ubi_dbg_is_bitflip(const struct ubi_device *ubi)
 {
        if (ubi->dbg.emulate_bitflips)
-               return !(prandom_u32() % 200);
+               return !prandom_u32_max(200);
        return 0;
 }
 
@@ -87,7 +87,7 @@ static inline int ubi_dbg_is_bitflip(const struct ubi_device *ubi)
 static inline int ubi_dbg_is_write_failure(const struct ubi_device *ubi)
 {
        if (ubi->dbg.emulate_io_failures)
-               return !(prandom_u32() % 500);
+               return !prandom_u32_max(500);
        return 0;
 }
 
@@ -101,7 +101,7 @@ static inline int ubi_dbg_is_write_failure(const struct ubi_device *ubi)
 static inline int ubi_dbg_is_erase_failure(const struct ubi_device *ubi)
 {
        if (ubi->dbg.emulate_io_failures)
-               return !(prandom_u32() % 400);
+               return !prandom_u32_max(400);
        return 0;
 }
 
index e86503d97f32bffc1eb4bc52b710c2b093cf2e89..f597b313acaa3c8755e43e2759a3ab93714ff49a 100644 (file)
@@ -4105,8 +4105,7 @@ static int cnic_cm_alloc_mem(struct cnic_dev *dev)
        for (i = 0; i < MAX_CM_SK_TBL_SZ; i++)
                atomic_set(&cp->csk_tbl[i].ref_count, 0);
 
-       port_id = prandom_u32();
-       port_id %= CNIC_LOCAL_PORT_RANGE;
+       port_id = prandom_u32_max(CNIC_LOCAL_PORT_RANGE);
        if (cnic_init_id_tbl(&cp->csk_port_tbl, CNIC_LOCAL_PORT_RANGE,
                             CNIC_LOCAL_PORT_MIN, port_id)) {
                cnic_cm_free_mem(dev);
index 539992dad8ba3444dc4c68720d7c74882fdf00f4..a4256087ac82867ae00cafdd20f70ccd990aa838 100644 (file)
@@ -919,8 +919,8 @@ static int csk_wait_memory(struct chtls_dev *cdev,
        current_timeo = *timeo_p;
        noblock = (*timeo_p ? false : true);
        if (csk_mem_free(cdev, sk)) {
-               current_timeo = (prandom_u32() % (HZ / 5)) + 2;
-               vm_wait = (prandom_u32() % (HZ / 5)) + 2;
+               current_timeo = prandom_u32_max(HZ / 5) + 2;
+               vm_wait = prandom_u32_max(HZ / 5) + 2;
        }
 
        add_wait_queue(sk_sleep(sk), &wait);
index 3e69079ed694b33c9b70fd94747281f647722ef3..7df78a721b04eff3e241554b2867da7fa26c1b0e 100644 (file)
@@ -438,7 +438,7 @@ static int transmit(struct baycom_state *bc, int cnt, unsigned char stat)
                        if ((--bc->hdlctx.slotcnt) > 0)
                                return 0;
                        bc->hdlctx.slotcnt = bc->ch_params.slottime;
-                       if ((prandom_u32() % 256) > bc->ch_params.ppersist)
+                       if (prandom_u32_max(256) > bc->ch_params.ppersist)
                                return 0;
                }
        }
index a6184d6c7b15f291b1e63f3b7ea891ac2930c7fc..bef904325a0fbb4c3988659e0f045cabc6b8c67c 100644 (file)
@@ -377,7 +377,7 @@ void hdlcdrv_arbitrate(struct net_device *dev, struct hdlcdrv_state *s)
        if ((--s->hdlctx.slotcnt) > 0)
                return;
        s->hdlctx.slotcnt = s->ch_params.slottime;
-       if ((prandom_u32() % 256) > s->ch_params.ppersist)
+       if (prandom_u32_max(256) > s->ch_params.ppersist)
                return;
        start_tx(dev, s);
 }
index 980f2be32f05a1974c13aba7d44f257901ed8cd6..97a6cc5c7ae89f1041e708835cbd9589c6ab8782 100644 (file)
@@ -626,7 +626,7 @@ static void yam_arbitrate(struct net_device *dev)
        yp->slotcnt = yp->slot / 10;
 
        /* is random > persist ? */
-       if ((prandom_u32() % 256) > yp->pers)
+       if (prandom_u32_max(256) > yp->pers)
                return;
 
        yam_start_tx(dev, yp);
index 9e9adde335c834e77c28abd933209a7d090321c9..349b7b1dbbf292fccee52f355d0a194b334f5a07 100644 (file)
@@ -1758,7 +1758,7 @@ static int qca808x_phy_fast_retrain_config(struct phy_device *phydev)
 
 static int qca808x_phy_ms_random_seed_set(struct phy_device *phydev)
 {
-       u16 seed_value = (prandom_u32() % QCA808X_MASTER_SLAVE_SEED_RANGE);
+       u16 seed_value = prandom_u32_max(QCA808X_MASTER_SLAVE_SEED_RANGE);
 
        return at803x_debug_reg_mask(phydev, QCA808X_PHY_DEBUG_LOCAL_SEED,
                        QCA808X_MASTER_SLAVE_SEED_CFG,
index 479041f070f98b3ca646577ffa8d2a7a3da62eee..10d9d9c63b2811106e5a7dd967fac22fee57b166 100644 (file)
@@ -1128,7 +1128,7 @@ static void brcmf_p2p_afx_handler(struct work_struct *work)
        if (afx_hdl->is_listen && afx_hdl->my_listen_chan)
                /* 100ms ~ 300ms */
                err = brcmf_p2p_discover_listen(p2p, afx_hdl->my_listen_chan,
-                                               100 * (1 + prandom_u32() % 3));
+                                               100 * (1 + prandom_u32_max(3)));
        else
                err = brcmf_p2p_act_frm_search(p2p, afx_hdl->peer_listen_chan);
 
index ed586e6d7d64b2bf5dfc158f085c2c6596e760af..de0c545d50fd5935eb0a0e6faca315791b67e2d3 100644 (file)
@@ -1099,7 +1099,7 @@ static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
                        iwl_mvm_mac_ap_iterator, &data);
 
                if (data.beacon_device_ts) {
-                       u32 rand = (prandom_u32() % (64 - 36)) + 36;
+                       u32 rand = prandom_u32_max(64 - 36) + 36;
                        mvmvif->ap_beacon_time = data.beacon_device_ts +
                                ieee80211_tu_to_usec(data.beacon_int * rand /
                                                     100);
index 39e16eab47aad4d4671ec7dbf58628435d267dbf..ddc048069af25aeac8680512e4bae041bfef9c2b 100644 (file)
@@ -2233,7 +2233,7 @@ static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
 
        if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
                fip->probe_tries++;
-               wait = prandom_u32() % FIP_VN_PROBE_WAIT;
+               wait = prandom_u32_max(FIP_VN_PROBE_WAIT);
        } else
                wait = FIP_VN_RLIM_INT;
        mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
@@ -3125,7 +3125,7 @@ static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
                                          fcoe_all_vn2vn, 0);
                        fip->port_ka_time = jiffies +
                                 msecs_to_jiffies(FIP_VN_BEACON_INT +
-                                       (prandom_u32() % FIP_VN_BEACON_FUZZ));
+                                       prandom_u32_max(FIP_VN_BEACON_FUZZ));
                }
                if (time_before(fip->port_ka_time, next_time))
                        next_time = fip->port_ka_time;
index cecfb2cb4c7beb87cc61d8519c20be53753fa2c9..df2fe7bd26d1b72b0ecd622038c6afe73e29d327 100644 (file)
@@ -618,7 +618,7 @@ static int qedi_cm_alloc_mem(struct qedi_ctx *qedi)
                                sizeof(struct qedi_endpoint *)), GFP_KERNEL);
        if (!qedi->ep_tbl)
                return -ENOMEM;
-       port_id = prandom_u32() % QEDI_LOCAL_PORT_RANGE;
+       port_id = prandom_u32_max(QEDI_LOCAL_PORT_RANGE);
        if (qedi_init_id_tbl(&qedi->lcl_port_tbl, QEDI_LOCAL_PORT_RANGE,
                             QEDI_LOCAL_PORT_MIN, port_id)) {
                qedi_cm_free_mem(qedi);
index 42351d7a0dd6b74b3a246ad472db6f35c6742c11..f0c6e7e7b92b961dff95fc986312b48cf07360ac 100644 (file)
@@ -362,7 +362,7 @@ static int ceph_fill_fragtree(struct inode *inode,
        if (nsplits != ci->i_fragtree_nsplits) {
                update = true;
        } else if (nsplits) {
-               i = prandom_u32() % nsplits;
+               i = prandom_u32_max(nsplits);
                id = le32_to_cpu(fragtree->splits[i].frag);
                if (!__ceph_find_frag(ci, id))
                        update = true;
index 8d0a6d2c2da4332c007fc0386d8912a5fda29c65..3fbabc98e1f702a90ea75ce6d4f7c5f9fb7b4a16 100644 (file)
@@ -29,7 +29,7 @@ static int __mdsmap_get_random_mds(struct ceph_mdsmap *m, bool ignore_laggy)
                return -1;
 
        /* pick */
-       n = prandom_u32() % n;
+       n = prandom_u32_max(n);
        for (j = 0, i = 0; i < m->possible_max_rank; i++) {
                if (CEPH_MDS_IS_READY(i, ignore_laggy))
                        j++;
index d733db8a0b026258b590219b8e9cc76f4ffffe9a..989365b878a67f5271176bd40f5a9cf97fd17224 100644 (file)
@@ -3782,8 +3782,7 @@ static int ext4_lazyinit_thread(void *arg)
                        }
                        if (!progress) {
                                elr->lr_next_sched = jiffies +
-                                       (prandom_u32()
-                                        % (EXT4_DEF_LI_MAX_START_DELAY * HZ));
+                                       prandom_u32_max(EXT4_DEF_LI_MAX_START_DELAY * HZ);
                        }
                        if (time_before(elr->lr_next_sched, next_wakeup))
                                next_wakeup = elr->lr_next_sched;
@@ -3930,8 +3929,8 @@ static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
         * spread the inode table initialization requests
         * better.
         */
-       elr->lr_next_sched = jiffies + (prandom_u32() %
-                               (EXT4_DEF_LI_MAX_START_DELAY * HZ));
+       elr->lr_next_sched = jiffies + prandom_u32_max(
+                               EXT4_DEF_LI_MAX_START_DELAY * HZ);
        return elr;
 }
 
index d36bcb23ccfec78e651df64fe3f199dad56c7941..4546e01b2ee082a9eca2a02e4f87d380e274bf40 100644 (file)
@@ -282,7 +282,7 @@ static void select_policy(struct f2fs_sb_info *sbi, int gc_type,
 
        /* let's select beginning hot/small space first in no_heap mode*/
        if (f2fs_need_rand_seg(sbi))
-               p->offset = prandom_u32() % (MAIN_SECS(sbi) * sbi->segs_per_sec);
+               p->offset = prandom_u32_max(MAIN_SECS(sbi) * sbi->segs_per_sec);
        else if (test_opt(sbi, NOHEAP) &&
                (type == CURSEG_HOT_DATA || IS_NODESEG(type)))
                p->offset = 0;
index 289bcb7ca30095bec8ca4a8283a6d23678e2b080..acf3d3fa4363578fac53964314361f4c97ac1a52 100644 (file)
@@ -2534,7 +2534,7 @@ static unsigned int __get_next_segno(struct f2fs_sb_info *sbi, int type)
 
        sanity_check_seg_type(sbi, seg_type);
        if (f2fs_need_rand_seg(sbi))
-               return prandom_u32() % (MAIN_SECS(sbi) * sbi->segs_per_sec);
+               return prandom_u32_max(MAIN_SECS(sbi) * sbi->segs_per_sec);
 
        /* if segs_per_sec is large than 1, we need to keep original policy. */
        if (__is_large_section(sbi))
@@ -2588,7 +2588,7 @@ static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
        curseg->alloc_type = LFS;
        if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK)
                curseg->fragment_remained_chunk =
-                               prandom_u32() % sbi->max_fragment_chunk + 1;
+                               prandom_u32_max(sbi->max_fragment_chunk) + 1;
 }
 
 static int __next_free_blkoff(struct f2fs_sb_info *sbi,
@@ -2625,9 +2625,9 @@ static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
                        /* To allocate block chunks in different sizes, use random number */
                        if (--seg->fragment_remained_chunk <= 0) {
                                seg->fragment_remained_chunk =
-                                  prandom_u32() % sbi->max_fragment_chunk + 1;
+                                  prandom_u32_max(sbi->max_fragment_chunk) + 1;
                                seg->next_blkoff +=
-                                  prandom_u32() % sbi->max_fragment_hole + 1;
+                                  prandom_u32_max(sbi->max_fragment_hole) + 1;
                        }
                }
        }
index fc718f6178f2553621fa7ed296b08ad23e8fa7cb..f4d3b568aa64a7e139ede904924d41de5c3b95b9 100644 (file)
@@ -2467,7 +2467,7 @@ int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)
 
 static inline int chance(unsigned int n, unsigned int out_of)
 {
-       return !!((prandom_u32() % out_of) + 1 <= n);
+       return !!(prandom_u32_max(out_of) + 1 <= n);
 
 }
 
@@ -2485,13 +2485,13 @@ static int power_cut_emulated(struct ubifs_info *c, int lnum, int write)
                        if (chance(1, 2)) {
                                d->pc_delay = 1;
                                /* Fail within 1 minute */
-                               delay = prandom_u32() % 60000;
+                               delay = prandom_u32_max(60000);
                                d->pc_timeout = jiffies;
                                d->pc_timeout += msecs_to_jiffies(delay);
                                ubifs_warn(c, "failing after %lums", delay);
                        } else {
                                d->pc_delay = 2;
-                               delay = prandom_u32() % 10000;
+                               delay = prandom_u32_max(10000);
                                /* Fail within 10000 operations */
                                d->pc_cnt_max = delay;
                                ubifs_warn(c, "failing after %lu calls", delay);
@@ -2571,7 +2571,7 @@ static int corrupt_data(const struct ubifs_info *c, const void *buf,
        unsigned int from, to, ffs = chance(1, 2);
        unsigned char *p = (void *)buf;
 
-       from = prandom_u32() % len;
+       from = prandom_u32_max(len);
        /* Corruption span max to end of write unit */
        to = min(len, ALIGN(from + 1, c->max_write_size));
 
index d76a19e460cd421b863760dfabfa6b4bc0329c9b..cfbc31f709f4b19c53c8c0a413b58f1b1b0a3c5b 100644 (file)
@@ -1970,28 +1970,28 @@ static int dbg_populate_lsave(struct ubifs_info *c)
 
        if (!dbg_is_chk_gen(c))
                return 0;
-       if (prandom_u32() & 3)
+       if (prandom_u32_max(4))
                return 0;
 
        for (i = 0; i < c->lsave_cnt; i++)
                c->lsave[i] = c->main_first;
 
        list_for_each_entry(lprops, &c->empty_list, list)
-               c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum;
+               c->lsave[prandom_u32_max(c->lsave_cnt)] = lprops->lnum;
        list_for_each_entry(lprops, &c->freeable_list, list)
-               c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum;
+               c->lsave[prandom_u32_max(c->lsave_cnt)] = lprops->lnum;
        list_for_each_entry(lprops, &c->frdi_idx_list, list)
-               c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum;
+               c->lsave[prandom_u32_max(c->lsave_cnt)] = lprops->lnum;
 
        heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
        for (i = 0; i < heap->cnt; i++)
-               c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum;
+               c->lsave[prandom_u32_max(c->lsave_cnt)] = heap->arr[i]->lnum;
        heap = &c->lpt_heap[LPROPS_DIRTY - 1];
        for (i = 0; i < heap->cnt; i++)
-               c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum;
+               c->lsave[prandom_u32_max(c->lsave_cnt)] = heap->arr[i]->lnum;
        heap = &c->lpt_heap[LPROPS_FREE - 1];
        for (i = 0; i < heap->cnt; i++)
-               c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum;
+               c->lsave[prandom_u32_max(c->lsave_cnt)] = heap->arr[i]->lnum;
 
        return 1;
 }
index 58c92c96ecef251619ff13e6bbf32931f5f165ec..01362ad5f804ae67b73fba3953695ef52525e2f9 100644 (file)
@@ -700,7 +700,7 @@ static int alloc_idx_lebs(struct ubifs_info *c, int cnt)
                c->ilebs[c->ileb_cnt++] = lnum;
                dbg_cmt("LEB %d", lnum);
        }
-       if (dbg_is_chk_index(c) && !(prandom_u32() & 7))
+       if (dbg_is_chk_index(c) && !prandom_u32_max(8))
                return -ENOSPC;
        return 0;
 }
index e2bdf089c0a31eccc23da94ef68a6b7a2193b605..6261599bb389af955f4dc16fe63b36d1cdf0d8c7 100644 (file)
@@ -1520,7 +1520,7 @@ xfs_alloc_ag_vextent_lastblock(
 
 #ifdef DEBUG
        /* Randomly don't execute the first algorithm. */
-       if (prandom_u32() & 1)
+       if (prandom_u32_max(2))
                return 0;
 #endif
 
index 6cdfd64bc56bdd2132721a38edf5c16a7e9fc64a..7838b31126e22536347350742186833d54bb2a25 100644 (file)
@@ -636,7 +636,7 @@ xfs_ialloc_ag_alloc(
        /* randomly do sparse inode allocations */
        if (xfs_has_sparseinodes(tp->t_mountp) &&
            igeo->ialloc_min_blks < igeo->ialloc_blks)
-               do_sparse = prandom_u32() & 1;
+               do_sparse = prandom_u32_max(2);
 #endif
 
        /*
index 296faa41d81d51ee4d0ebdad3fefbf5d671f92ec..7db588ed0be597b9329e856a726795a1d1da6893 100644 (file)
@@ -274,7 +274,7 @@ xfs_errortag_test(
 
        ASSERT(error_tag < XFS_ERRTAG_MAX);
        randfactor = mp->m_errortag[error_tag];
-       if (!randfactor || prandom_u32() % randfactor)
+       if (!randfactor || prandom_u32_max(randfactor))
                return false;
 
        xfs_warn_ratelimited(mp,
index 378956c93c94711276b3678c37f10953099a94d8..efef68c9352a0001f9e037dea139d23d80afd269 100644 (file)
@@ -516,7 +516,7 @@ static inline int node_random(const nodemask_t *maskp)
                bit = first_node(*maskp);
                break;
        default:
-               bit = find_nth_bit(maskp->bits, MAX_NUMNODES, get_random_int() % w);
+               bit = find_nth_bit(maskp->bits, MAX_NUMNODES, prandom_u32_max(w));
                break;
        }
        return bit;
index 59cf4dc728a55c427213d34546f1ce5a0ebd3d66..53c6c98bda7b6d6bea42e66db40de2b6be94b925 100644 (file)
@@ -1032,7 +1032,7 @@ bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
        hdr->size = size;
        hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
                     PAGE_SIZE - sizeof(*hdr));
-       start = (get_random_int() % hole) & ~(alignment - 1);
+       start = prandom_u32_max(hole) & ~(alignment - 1);
 
        /* Leave a random number of instructions before BPF code. */
        *image_ptr = &hdr->image[start];
@@ -1094,7 +1094,7 @@ bpf_jit_binary_pack_alloc(unsigned int proglen, u8 **image_ptr,
 
        hole = min_t(unsigned int, size - (proglen + sizeof(*ro_header)),
                     BPF_PROG_CHUNK_SIZE - sizeof(*ro_header));
-       start = (get_random_int() % hole) & ~(alignment - 1);
+       start = prandom_u32_max(hole) & ~(alignment - 1);
 
        *image_ptr = &ro_header->image[start];
        *rw_image = &(*rw_header)->image[start];
index 353004155d659fa271d0921bb454a2eb27ad1b8a..43efb2a0416025f9dc9ff4f89bbec416fd79d10a 100644 (file)
@@ -399,7 +399,7 @@ static int *get_random_order(int count)
                order[n] = n;
 
        for (n = count - 1; n > 1; n--) {
-               r = get_random_int() % (n + 1);
+               r = prandom_u32_max(n + 1);
                if (r != n) {
                        tmp = order[n];
                        order[n] = order[r];
@@ -538,7 +538,7 @@ static void stress_one_work(struct work_struct *work)
 {
        struct stress *stress = container_of(work, typeof(*stress), work);
        const int nlocks = stress->nlocks;
-       struct ww_mutex *lock = stress->locks + (get_random_int() % nlocks);
+       struct ww_mutex *lock = stress->locks + prandom_u32_max(nlocks);
        int err;
 
        do {
index cee5da1e54c4121d771bf5fa19c07584e000bf49..8058bec87acee919514d13a98be07b37b423b4f4 100644 (file)
@@ -310,7 +310,7 @@ static void clocksource_verify_choose_cpus(void)
         * CPUs that are currently online.
         */
        for (i = 1; i < n; i++) {
-               cpu = prandom_u32() % nr_cpu_ids;
+               cpu = prandom_u32_max(nr_cpu_ids);
                cpu = cpumask_next(cpu - 1, cpu_online_mask);
                if (cpu >= nr_cpu_ids)
                        cpu = cpumask_first(cpu_online_mask);
index 423784d9c058eaf7d8833eade65bd389bd6a993e..96e092de5b72364205085aedba1e30909a3046d6 100644 (file)
@@ -139,7 +139,7 @@ bool should_fail(struct fault_attr *attr, ssize_t size)
                        return false;
        }
 
-       if (attr->probability <= prandom_u32() % 100)
+       if (attr->probability <= prandom_u32_max(100))
                return false;
 
        if (!fail_stacktrace(attr))
index 10754586403b1b1ba0e72b1e4ee9105a8b10a3e5..7c3c011abd29489d7914f5d3e87aaa90c7c24e14 100644 (file)
@@ -174,8 +174,8 @@ static int __init find_bit_test(void)
        bitmap_zero(bitmap2, BITMAP_LEN);
 
        while (nbits--) {
-               __set_bit(prandom_u32() % BITMAP_LEN, bitmap);
-               __set_bit(prandom_u32() % BITMAP_LEN, bitmap2);
+               __set_bit(prandom_u32_max(BITMAP_LEN), bitmap);
+               __set_bit(prandom_u32_max(BITMAP_LEN), bitmap2);
        }
 
        test_find_next_bit(bitmap, BITMAP_LEN);
index 5f0e71ab292cb07e3650a09665afe977d149c7df..a0b2dbfcfa23347d6e546fbb79da78ba21e16c79 100644 (file)
@@ -694,7 +694,7 @@ static void kobject_release(struct kref *kref)
 {
        struct kobject *kobj = container_of(kref, struct kobject, kref);
 #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
-       unsigned long delay = HZ + HZ * (get_random_int() & 0x3);
+       unsigned long delay = HZ + HZ * prandom_u32_max(4);
        pr_info("kobject: '%s' (%p): %s, parent %p (delayed %ld)\n",
                 kobject_name(kobj), kobj, __func__, kobj->parent, delay);
        INIT_DELAYED_WORK(&kobj->release, kobject_delayed_cleanup);
index d9d1c33aebdaee97dae05004bf75f8e95a9edc38..4d241bdc88aa8178f7de0443841a7061df194e18 100644 (file)
@@ -183,7 +183,7 @@ static int get_rcw_we(struct rs_control *rs, struct wspace *ws,
 
                do {
                        /* Must not choose the same location twice */
-                       errloc = prandom_u32() % len;
+                       errloc = prandom_u32_max(len);
                } while (errlocs[errloc] != 0);
 
                errlocs[errloc] = 1;
@@ -194,12 +194,12 @@ static int get_rcw_we(struct rs_control *rs, struct wspace *ws,
        for (i = 0; i < eras; i++) {
                do {
                        /* Must not choose the same location twice */
-                       errloc = prandom_u32() % len;
+                       errloc = prandom_u32_max(len);
                } while (errlocs[errloc] != 0);
 
                derrlocs[i] = errloc;
 
-               if (ewsc && (prandom_u32() & 1)) {
+               if (ewsc && prandom_u32_max(2)) {
                        /* Erasure with the symbol intact */
                        errlocs[errloc] = 2;
                } else {
index a8108a962dfd41f0b4afab3acfd77634a5b5d518..055dac069afb9ef04f4f4949adcba7e8ff2b3aba 100644 (file)
@@ -33,7 +33,7 @@ static inline unsigned update_alloc_hint_before_get(struct sbitmap *sb,
 
        hint = this_cpu_read(*sb->alloc_hint);
        if (unlikely(hint >= depth)) {
-               hint = depth ? prandom_u32() % depth : 0;
+               hint = depth ? prandom_u32_max(depth) : 0;
                this_cpu_write(*sb->alloc_hint, hint);
        }
 
index 437d8e6b7cb124798ef0dc3b2ab99e1b0e8bd3fe..86fadd3ba08c58ed23961a2378bb25f82e793d1a 100644 (file)
@@ -587,7 +587,7 @@ static int __init test_string_helpers_init(void)
        for (i = 0; i < UNESCAPE_ALL_MASK + 1; i++)
                test_string_unescape("unescape", i, false);
        test_string_unescape("unescape inplace",
-                            get_random_int() % (UNESCAPE_ANY + 1), true);
+                            prandom_u32_max(UNESCAPE_ANY + 1), true);
 
        /* Without dictionary */
        for (i = 0; i < ESCAPE_ALL_MASK + 1; i++)
index 5144899d3c6b8b235d33c8a988f3a413fa34aff8..0927f44cd4787f565140c9e75c92c87b0b026d76 100644 (file)
@@ -149,7 +149,7 @@ static void __init test_hexdump(size_t len, int rowsize, int groupsize,
 static void __init test_hexdump_set(int rowsize, bool ascii)
 {
        size_t d = min_t(size_t, sizeof(data_b), rowsize);
-       size_t len = get_random_int() % d + 1;
+       size_t len = prandom_u32_max(d) + 1;
 
        test_hexdump(len, rowsize, 4, ascii);
        test_hexdump(len, rowsize, 2, ascii);
@@ -208,11 +208,11 @@ static void __init test_hexdump_overflow(size_t buflen, size_t len,
 static void __init test_hexdump_overflow_set(size_t buflen, bool ascii)
 {
        unsigned int i = 0;
-       int rs = (get_random_int() % 2 + 1) * 16;
+       int rs = (prandom_u32_max(2) + 1) * 16;
 
        do {
                int gs = 1 << i;
-               size_t len = get_random_int() % rs + gs;
+               size_t len = prandom_u32_max(rs) + gs;
 
                test_hexdump_overflow(buflen, rounddown(len, gs), rs, gs, ascii);
        } while (i++ < 3);
@@ -223,11 +223,11 @@ static int __init test_hexdump_init(void)
        unsigned int i;
        int rowsize;
 
-       rowsize = (get_random_int() % 2 + 1) * 16;
+       rowsize = (prandom_u32_max(2) + 1) * 16;
        for (i = 0; i < 16; i++)
                test_hexdump_set(rowsize, false);
 
-       rowsize = (get_random_int() % 2 + 1) * 16;
+       rowsize = (prandom_u32_max(2) + 1) * 16;
        for (i = 0; i < 16; i++)
                test_hexdump_set(rowsize, true);
 
index ade7a1ea0c8e25006af3772f91b793602aa4d605..19ff229b9c3a7838a9a216e71bd9fe866c53da2f 100644 (file)
@@ -71,7 +71,7 @@ static void list_sort_test(struct kunit *test)
                KUNIT_ASSERT_NOT_ERR_OR_NULL(test, el);
 
                 /* force some equivalencies */
-               el->value = prandom_u32() % (TEST_LIST_LEN / 3);
+               el->value = prandom_u32_max(TEST_LIST_LEN / 3);
                el->serial = i;
                el->poison1 = TEST_POISON1;
                el->poison2 = TEST_POISON2;
index f25692def781363ed5ef647ad0d3ca1a5db18372..2503ae2ae65d364fbcd917882e97f1f4efaccf2d 100644 (file)
@@ -1292,7 +1292,7 @@ static void match_all_not_assigned(struct kunit *test)
        KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
 
        for (i = 0; i < 256; i++) {
-               size = (get_random_int() % 1024) + 1;
+               size = prandom_u32_max(1024) + 1;
                ptr = kmalloc(size, GFP_KERNEL);
                KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
                KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
@@ -1301,7 +1301,7 @@ static void match_all_not_assigned(struct kunit *test)
        }
 
        for (i = 0; i < 256; i++) {
-               order = (get_random_int() % 4) + 1;
+               order = prandom_u32_max(4) + 1;
                pages = alloc_pages(GFP_KERNEL, order);
                ptr = page_address(pages);
                KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
@@ -1314,7 +1314,7 @@ static void match_all_not_assigned(struct kunit *test)
                return;
 
        for (i = 0; i < 256; i++) {
-               size = (get_random_int() % 1024) + 1;
+               size = prandom_u32_max(1024) + 1;
                ptr = vmalloc(size);
                KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
                KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
index 96dd392d7f99f9be14fd5a33761867523ddfc1a0..157527d7101be0da54026376ff4f4095deb9ec2d 100644 (file)
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -1881,7 +1881,7 @@ static bool shuffle_freelist(struct kmem_cache *s, struct slab *slab)
                return false;
 
        freelist_count = oo_objects(s->oo);
-       pos = get_random_int() % freelist_count;
+       pos = prandom_u32_max(freelist_count);
 
        page_limit = slab->objects * s->size;
        start = fixup_red_left(s, slab_address(slab));
index f6012f8e59f00521e36a7e251ce3b452818a010e..fc9eb02a912f819459d6ba2922619bd85d8f6ba2 100644 (file)
@@ -407,7 +407,7 @@ static void garp_join_timer_arm(struct garp_applicant *app)
 {
        unsigned long delay;
 
-       delay = (u64)msecs_to_jiffies(garp_join_time) * prandom_u32() >> 32;
+       delay = prandom_u32_max(msecs_to_jiffies(garp_join_time));
        mod_timer(&app->join_timer, jiffies + delay);
 }
 
index 35e04cc5390c49e54b3bef5e7157c9e3db2514b3..155f74d8b14f4b66fad5451a88a10be01604b57d 100644 (file)
@@ -592,7 +592,7 @@ static void mrp_join_timer_arm(struct mrp_applicant *app)
 {
        unsigned long delay;
 
-       delay = (u64)msecs_to_jiffies(mrp_join_time) * prandom_u32() >> 32;
+       delay = prandom_u32_max(msecs_to_jiffies(mrp_join_time));
        mod_timer(&app->join_timer, jiffies + delay);
 }
 
index 6a6898ee40495d9de053396ae29cf482c0a62c66..db60217f911b31ca04a768b3dee562d1d165ca02 100644 (file)
@@ -222,7 +222,7 @@ static void pick_new_mon(struct ceph_mon_client *monc)
                                max--;
                }
 
-               n = prandom_u32() % max;
+               n = prandom_u32_max(max);
                if (o >= 0 && n >= o)
                        n++;
 
index 87b883c7bfd6492ac95c550af64db09ea9acc612..4e4f1e4bc265a9294103131b565ac955a8f25c3f 100644 (file)
@@ -1479,7 +1479,7 @@ static bool target_should_be_paused(struct ceph_osd_client *osdc,
 
 static int pick_random_replica(const struct ceph_osds *acting)
 {
-       int i = prandom_u32() % acting->size;
+       int i = prandom_u32_max(acting->size);
 
        dout("%s picked osd%d, primary osd%d\n", __func__,
             acting->osds[i], acting->primary);
index e93edb81010363a4e41e248fd652d68f8ab958e4..3c4786b9990703814995832027af16d8c8e06c72 100644 (file)
@@ -111,7 +111,7 @@ static void neigh_cleanup_and_release(struct neighbour *neigh)
 
 unsigned long neigh_rand_reach_time(unsigned long base)
 {
-       return base ? (prandom_u32() % base) + (base >> 1) : 0;
+       return base ? prandom_u32_max(base) + (base >> 1) : 0;
 }
 EXPORT_SYMBOL(neigh_rand_reach_time);
 
index 88906ba6d9a7851dfbb4762d3504cc20423e1053..5ca4f953034cabd9cfbb2ea4b21043b0f0f5eeea 100644 (file)
@@ -2324,7 +2324,7 @@ static inline int f_pick(struct pktgen_dev *pkt_dev)
                                pkt_dev->curfl = 0; /*reset */
                }
        } else {
-               flow = prandom_u32() % pkt_dev->cflows;
+               flow = prandom_u32_max(pkt_dev->cflows);
                pkt_dev->curfl = flow;
 
                if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
@@ -2380,10 +2380,9 @@ static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
        else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
                __u16 t;
                if (pkt_dev->flags & F_QUEUE_MAP_RND) {
-                       t = prandom_u32() %
-                               (pkt_dev->queue_map_max -
-                                pkt_dev->queue_map_min + 1)
-                               + pkt_dev->queue_map_min;
+                       t = prandom_u32_max(pkt_dev->queue_map_max -
+                                           pkt_dev->queue_map_min + 1) +
+                           pkt_dev->queue_map_min;
                } else {
                        t = pkt_dev->cur_queue_map + 1;
                        if (t > pkt_dev->queue_map_max)
@@ -2412,7 +2411,7 @@ static void mod_cur_headers(struct pktgen_dev *pkt_dev)
                __u32 tmp;
 
                if (pkt_dev->flags & F_MACSRC_RND)
-                       mc = prandom_u32() % pkt_dev->src_mac_count;
+                       mc = prandom_u32_max(pkt_dev->src_mac_count);
                else {
                        mc = pkt_dev->cur_src_mac_offset++;
                        if (pkt_dev->cur_src_mac_offset >=
@@ -2438,7 +2437,7 @@ static void mod_cur_headers(struct pktgen_dev *pkt_dev)
                __u32 tmp;
 
                if (pkt_dev->flags & F_MACDST_RND)
-                       mc = prandom_u32() % pkt_dev->dst_mac_count;
+                       mc = prandom_u32_max(pkt_dev->dst_mac_count);
 
                else {
                        mc = pkt_dev->cur_dst_mac_offset++;
@@ -2470,18 +2469,18 @@ static void mod_cur_headers(struct pktgen_dev *pkt_dev)
        }
 
        if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
-               pkt_dev->vlan_id = prandom_u32() & (4096 - 1);
+               pkt_dev->vlan_id = prandom_u32_max(4096);
        }
 
        if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
-               pkt_dev->svlan_id = prandom_u32() & (4096 - 1);
+               pkt_dev->svlan_id = prandom_u32_max(4096);
        }
 
        if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
                if (pkt_dev->flags & F_UDPSRC_RND)
-                       pkt_dev->cur_udp_src = prandom_u32() %
-                               (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
-                               pkt_dev->udp_src_min;
+                       pkt_dev->cur_udp_src = prandom_u32_max(
+                               pkt_dev->udp_src_max - pkt_dev->udp_src_min) +
+                               pkt_dev->udp_src_min;
 
                else {
                        pkt_dev->cur_udp_src++;
@@ -2492,9 +2491,9 @@ static void mod_cur_headers(struct pktgen_dev *pkt_dev)
 
        if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
                if (pkt_dev->flags & F_UDPDST_RND) {
-                       pkt_dev->cur_udp_dst = prandom_u32() %
-                               (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
-                               pkt_dev->udp_dst_min;
+                       pkt_dev->cur_udp_dst = prandom_u32_max(
+                               pkt_dev->udp_dst_max - pkt_dev->udp_dst_min) +
+                               pkt_dev->udp_dst_min;
                } else {
                        pkt_dev->cur_udp_dst++;
                        if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
@@ -2509,7 +2508,7 @@ static void mod_cur_headers(struct pktgen_dev *pkt_dev)
                if (imn < imx) {
                        __u32 t;
                        if (pkt_dev->flags & F_IPSRC_RND)
-                               t = prandom_u32() % (imx - imn) + imn;
+                               t = prandom_u32_max(imx - imn) + imn;
                        else {
                                t = ntohl(pkt_dev->cur_saddr);
                                t++;
@@ -2531,8 +2530,8 @@ static void mod_cur_headers(struct pktgen_dev *pkt_dev)
                                if (pkt_dev->flags & F_IPDST_RND) {
 
                                        do {
-                                               t = prandom_u32() %
-                                                       (imx - imn) + imn;
+                                               t = prandom_u32_max(imx - imn) +
+                                                   imn;
                                                s = htonl(t);
                                        } while (ipv4_is_loopback(s) ||
                                                ipv4_is_multicast(s) ||
@@ -2579,9 +2578,9 @@ static void mod_cur_headers(struct pktgen_dev *pkt_dev)
        if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
                __u32 t;
                if (pkt_dev->flags & F_TXSIZE_RND) {
-                       t = prandom_u32() %
-                               (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
-                               + pkt_dev->min_pkt_size;
+                       t = prandom_u32_max(pkt_dev->max_pkt_size -
+                                           pkt_dev->min_pkt_size) +
+                           pkt_dev->min_pkt_size;
                } else {
                        t = pkt_dev->cur_pkt_size + 1;
                        if (t > pkt_dev->max_pkt_size)
@@ -2590,7 +2589,7 @@ static void mod_cur_headers(struct pktgen_dev *pkt_dev)
                pkt_dev->cur_pkt_size = t;
        } else if (pkt_dev->n_imix_entries > 0) {
                struct imix_pkt *entry;
-               __u32 t = prandom_u32() % IMIX_PRECISION;
+               __u32 t = prandom_u32_max(IMIX_PRECISION);
                __u8 entry_index = pkt_dev->imix_distribution[t];
 
                entry = &pkt_dev->imix_entries[entry_index];
index 1105057ce00a53e190b7aefa3dfb80c73e1ddaea..75fded8495f5b70700637b125a4993021deac407 100644 (file)
@@ -123,7 +123,7 @@ int sk_stream_wait_memory(struct sock *sk, long *timeo_p)
        DEFINE_WAIT_FUNC(wait, woken_wake_function);
 
        if (sk_stream_memory_free(sk))
-               current_timeo = vm_wait = (prandom_u32() % (HZ / 5)) + 2;
+               current_timeo = vm_wait = prandom_u32_max(HZ / 5) + 2;
 
        add_wait_queue(sk_sleep(sk), &wait);
 
index df0660d818ac500c9bfc17ce21e77585b4bd747b..81be3e0f0e70471f40db9d6a0b832cc8d3398a81 100644 (file)
@@ -213,7 +213,7 @@ static void igmp_stop_timer(struct ip_mc_list *im)
 /* It must be called with locked im->lock */
 static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
 {
-       int tv = prandom_u32() % max_delay;
+       int tv = prandom_u32_max(max_delay);
 
        im->tm_running = 1;
        if (!mod_timer(&im->timer, jiffies+tv+2))
@@ -222,7 +222,7 @@ static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
 
 static void igmp_gq_start_timer(struct in_device *in_dev)
 {
-       int tv = prandom_u32() % in_dev->mr_maxdelay;
+       int tv = prandom_u32_max(in_dev->mr_maxdelay);
        unsigned long exp = jiffies + tv + 2;
 
        if (in_dev->mr_gq_running &&
@@ -236,7 +236,7 @@ static void igmp_gq_start_timer(struct in_device *in_dev)
 
 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
 {
-       int tv = prandom_u32() % delay;
+       int tv = prandom_u32_max(delay);
 
        if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
                in_dev_hold(in_dev);
index ebca860e113f93563606d31490ea46f93bcd4cc7..4e84ed21d16fedc7515728f94a5b560589c989e1 100644 (file)
@@ -314,7 +314,7 @@ inet_csk_find_open_port(const struct sock *sk, struct inet_bind_bucket **tb_ret,
        if (likely(remaining > 1))
                remaining &= ~1U;
 
-       offset = prandom_u32() % remaining;
+       offset = prandom_u32_max(remaining);
        /* __inet_hash_connect() favors ports having @low parity
         * We do the opposite to not pollute connect() users.
         */
index a0ad34e4f044b7b12ef3fbcdc4eed37ce76dbe46..d3dc281566229479aa9027229a37973f8507d014 100644 (file)
@@ -1037,7 +1037,7 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,
         * on low contention the randomness is maximal and on high contention
         * it may be inexistent.
         */
-       i = max_t(int, i, (prandom_u32() & 7) * 2);
+       i = max_t(int, i, prandom_u32_max(8) * 2);
        WRITE_ONCE(table_perturb[index], READ_ONCE(table_perturb[index]) + i + 2);
 
        /* Head lock still held and bh's disabled */
index 10ce86bf228e186aee1942079b54042c30223126..417834b7169d7afc9730d2a2fe919261e77871f1 100644 (file)
@@ -104,7 +104,7 @@ static inline u32 cstamp_delta(unsigned long cstamp)
 static inline s32 rfc3315_s14_backoff_init(s32 irt)
 {
        /* multiply 'initial retransmission time' by 0.9 .. 1.1 */
-       u64 tmp = (900000 + prandom_u32() % 200001) * (u64)irt;
+       u64 tmp = (900000 + prandom_u32_max(200001)) * (u64)irt;
        do_div(tmp, 1000000);
        return (s32)tmp;
 }
@@ -112,11 +112,11 @@ static inline s32 rfc3315_s14_backoff_init(s32 irt)
 static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt)
 {
        /* multiply 'retransmission timeout' by 1.9 .. 2.1 */
-       u64 tmp = (1900000 + prandom_u32() % 200001) * (u64)rt;
+       u64 tmp = (1900000 + prandom_u32_max(200001)) * (u64)rt;
        do_div(tmp, 1000000);
        if ((s32)tmp > mrt) {
                /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
-               tmp = (900000 + prandom_u32() % 200001) * (u64)mrt;
+               tmp = (900000 + prandom_u32_max(200001)) * (u64)mrt;
                do_div(tmp, 1000000);
        }
        return (s32)tmp;
@@ -3967,7 +3967,7 @@ static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
        if (ifp->flags & IFA_F_OPTIMISTIC)
                rand_num = 0;
        else
-               rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
+               rand_num = prandom_u32_max(idev->cnf.rtr_solicit_delay ?: 1);
 
        nonce = 0;
        if (idev->cnf.enhanced_dad ||
index 0566ab03ddbee47b1d77c99300d9eea6f47a78bc..7860383295d84728dacb7e5331a489c3bc4f50e0 100644 (file)
@@ -1050,7 +1050,7 @@ bool ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
 /* called with mc_lock */
 static void mld_gq_start_work(struct inet6_dev *idev)
 {
-       unsigned long tv = prandom_u32() % idev->mc_maxdelay;
+       unsigned long tv = prandom_u32_max(idev->mc_maxdelay);
 
        idev->mc_gq_running = 1;
        if (!mod_delayed_work(mld_wq, &idev->mc_gq_work, tv + 2))
@@ -1068,7 +1068,7 @@ static void mld_gq_stop_work(struct inet6_dev *idev)
 /* called with mc_lock */
 static void mld_ifc_start_work(struct inet6_dev *idev, unsigned long delay)
 {
-       unsigned long tv = prandom_u32() % delay;
+       unsigned long tv = prandom_u32_max(delay);
 
        if (!mod_delayed_work(mld_wq, &idev->mc_ifc_work, tv + 2))
                in6_dev_hold(idev);
@@ -1085,7 +1085,7 @@ static void mld_ifc_stop_work(struct inet6_dev *idev)
 /* called with mc_lock */
 static void mld_dad_start_work(struct inet6_dev *idev, unsigned long delay)
 {
-       unsigned long tv = prandom_u32() % delay;
+       unsigned long tv = prandom_u32_max(delay);
 
        if (!mod_delayed_work(mld_wq, &idev->mc_dad_work, tv + 2))
                in6_dev_hold(idev);
@@ -1130,7 +1130,7 @@ static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
        }
 
        if (delay >= resptime)
-               delay = prandom_u32() % resptime;
+               delay = prandom_u32_max(resptime);
 
        if (!mod_delayed_work(mld_wq, &ma->mca_work, delay))
                refcount_inc(&ma->mca_refcnt);
@@ -2574,7 +2574,7 @@ static void igmp6_join_group(struct ifmcaddr6 *ma)
 
        igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
 
-       delay = prandom_u32() % unsolicited_report_interval(ma->idev);
+       delay = prandom_u32_max(unsolicited_report_interval(ma->idev));
 
        if (cancel_delayed_work(&ma->mca_work)) {
                refcount_dec(&ma->mca_refcnt);
index acb55d8393ef6933d003ff3ffaa21891c1aee595..f2579fc9c75bd3f9ffbe4e77fd4f36d3721e1b60 100644 (file)
@@ -71,8 +71,8 @@ static struct ip_vs_dest *ip_vs_twos_schedule(struct ip_vs_service *svc,
         * from 0 to total_weight
         */
        total_weight += 1;
-       rweight1 = prandom_u32() % total_weight;
-       rweight2 = prandom_u32() % total_weight;
+       rweight1 = prandom_u32_max(total_weight);
+       rweight2 = prandom_u32_max(total_weight);
 
        /* Pick two weighted servers */
        list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
index d3f6db350de779f237a48af6c79fef0c6e425da4..6ce8dd19f33c3aafef0b9dab648a44fdd3478866 100644 (file)
@@ -1350,7 +1350,7 @@ static bool fanout_flow_is_huge(struct packet_sock *po, struct sk_buff *skb)
                if (READ_ONCE(history[i]) == rxhash)
                        count++;
 
-       victim = prandom_u32() % ROLLOVER_HLEN;
+       victim = prandom_u32_max(ROLLOVER_HLEN);
 
        /* Avoid dirtying the cache line if possible */
        if (READ_ONCE(history[victim]) != rxhash)
index abe1bcc5c79716d896967e9dcde8104960eccbf6..62d682b96b88527314387c9180df2ffc9371d160 100644 (file)
@@ -25,7 +25,7 @@ static struct tc_action_ops act_gact_ops;
 static int gact_net_rand(struct tcf_gact *gact)
 {
        smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */
-       if (prandom_u32() % gact->tcfg_pval)
+       if (prandom_u32_max(gact->tcfg_pval))
                return gact->tcf_action;
        return gact->tcfg_paction;
 }
index 5ba36f70e3a138b23da22f5b10eb7ca971737ea4..7a25477f5d996aa633c15f5f82a65ef45aae7203 100644 (file)
@@ -168,7 +168,7 @@ static int tcf_sample_act(struct sk_buff *skb, const struct tc_action *a,
        psample_group = rcu_dereference_bh(s->psample_group);
 
        /* randomly sample packets according to rate */
-       if (psample_group && (prandom_u32() % s->rate == 0)) {
+       if (psample_group && (prandom_u32_max(s->rate) == 0)) {
                if (!skb_at_tc_ingress(skb)) {
                        md.in_ifindex = skb->skb_iif;
                        md.out_ifindex = skb->dev->ifindex;
index 18f4273a835b9a4cd651e00cc673b36f20e87659..bab45b3b1fdb5bc7c9584b4251500e47c606e028 100644 (file)
@@ -513,8 +513,8 @@ static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch,
                        goto finish_segs;
                }
 
-               skb->data[prandom_u32() % skb_headlen(skb)] ^=
-                       1<<(prandom_u32() % 8);
+               skb->data[prandom_u32_max(skb_headlen(skb))] ^=
+                       1<<prandom_u32_max(8);
        }
 
        if (unlikely(sch->q.qlen >= sch->limit)) {
index 171f1a35d205225cdec9dbc6d0a07f446ce92996..1e354ba449607d496619de795236882ae5a44ad1 100644 (file)
@@ -8319,7 +8319,7 @@ static int sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
 
                inet_get_local_port_range(net, &low, &high);
                remaining = (high - low) + 1;
-               rover = prandom_u32() % remaining + low;
+               rover = prandom_u32_max(remaining) + low;
 
                do {
                        rover++;
index c3c693b51c94582766e00a0e816c748512e76c47..f075a9fb5ccc6cf5a008c44ac391436689f2d030 100644 (file)
@@ -677,7 +677,7 @@ static void cache_limit_defers(void)
 
        /* Consider removing either the first or the last */
        if (cache_defer_cnt > DFR_MAX) {
-               if (prandom_u32() & 1)
+               if (prandom_u32_max(2))
                        discard = list_entry(cache_defer_list.next,
                                             struct cache_deferred_req, recent);
                else
index e976007f4fd00f2c6238c9756370dd3231f2c98a..f55ff5155b6e28bb54b55335b3b77b94366c545d 100644 (file)
@@ -1619,7 +1619,7 @@ static int xs_get_random_port(void)
        if (max < min)
                return -EADDRINUSE;
        range = max - min + 1;
-       rand = (unsigned short) prandom_u32() % range;
+       rand = prandom_u32_max(range);
        return rand + min;
 }
 
index f1c3b8eb4b3d3356070f77f7065591bd23681480..e902b01ea3cb189ac560e531bd25c7068fe9e322 100644 (file)
@@ -3010,7 +3010,7 @@ static int tipc_sk_insert(struct tipc_sock *tsk)
        struct net *net = sock_net(sk);
        struct tipc_net *tn = net_generic(net, tipc_net_id);
        u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
-       u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
+       u32 portid = prandom_u32_max(remaining) + TIPC_MIN_PORT;
 
        while (remaining--) {
                portid++;
index 81df34b3da6ed5141760e7415a57fceb2de3a647..3d2fe7712ac5b4191830d9eb272f55b4e035b949 100644 (file)
@@ -2072,7 +2072,7 @@ int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
        } else {
                u32 spi = 0;
                for (h = 0; h < high-low+1; h++) {
-                       spi = low + prandom_u32()%(high-low+1);
+                       spi = low + prandom_u32_max(high - low + 1);
                        x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
                        if (x0 == NULL) {
                                newspi = htonl(spi);