Skip to content

Commit f743f16

Browse files
committed
treewide: use get_random_{u8,u16}() when possible, part 2
Rather than truncate a 32-bit value to a 16-bit value or an 8-bit value, simply use the get_random_{u8,u16}() functions, which are faster than wasting the additional bytes from a 32-bit value. This was done by hand, identifying all of the places where one of the random integer functions was used in a non-32-bit context. Reviewed-by: Greg Kroah-Hartman <[email protected]> Reviewed-by: Kees Cook <[email protected]> Reviewed-by: Yury Norov <[email protected]> Acked-by: Jakub Kicinski <[email protected]> Acked-by: Heiko Carstens <[email protected]> # for s390 Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent 7e3cf08 commit f743f16

File tree

6 files changed

+6
-6
lines changed

6 files changed

+6
-6
lines changed

arch/s390/kernel/process.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ unsigned long arch_align_stack(unsigned long sp)
230230

231231
static inline unsigned long brk_rnd(void)
232232
{
233-
return (get_random_int() & BRK_RND_MASK) << PAGE_SHIFT;
233+
return (get_random_u16() & BRK_RND_MASK) << PAGE_SHIFT;
234234
}
235235

236236
unsigned long arch_randomize_brk(struct mm_struct *mm)

drivers/mtd/nand/raw/nandsim.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1402,7 +1402,7 @@ static int ns_do_read_error(struct nandsim *ns, int num)
14021402

14031403
static void ns_do_bit_flips(struct nandsim *ns, int num)
14041404
{
1405-
if (bitflips && prandom_u32() < (1 << 22)) {
1405+
if (bitflips && get_random_u16() < (1 << 6)) {
14061406
int flips = 1;
14071407
if (bitflips > 1)
14081408
flips = prandom_u32_max(bitflips) + 1;

drivers/net/wireless/broadcom/brcm80211/brcmfmac/pno.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ static int brcmf_pno_set_random(struct brcmf_if *ifp, struct brcmf_pno_info *pi)
177177
memcpy(pfn_mac.mac, mac_addr, ETH_ALEN);
178178
for (i = 0; i < ETH_ALEN; i++) {
179179
pfn_mac.mac[i] &= mac_mask[i];
180-
pfn_mac.mac[i] |= get_random_int() & ~(mac_mask[i]);
180+
pfn_mac.mac[i] |= get_random_u8() & ~(mac_mask[i]);
181181
}
182182
/* Clear multi bit */
183183
pfn_mac.mac[0] &= 0xFE;

lib/test_vmalloc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static int random_size_align_alloc_test(void)
8080
int i;
8181

8282
for (i = 0; i < test_loop_count; i++) {
83-
rnd = prandom_u32();
83+
rnd = get_random_u8();
8484

8585
/*
8686
* Maximum 1024 pages, if PAGE_SIZE is 4096.

net/rds/bind.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ static int rds_add_bound(struct rds_sock *rs, const struct in6_addr *addr,
104104
return -EINVAL;
105105
last = rover;
106106
} else {
107-
rover = max_t(u16, prandom_u32(), 2);
107+
rover = max_t(u16, get_random_u16(), 2);
108108
last = rover - 1;
109109
}
110110

net/sched/sch_sfb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ static int sfb_enqueue(struct sk_buff *skb, struct Qdisc *sch,
379379
goto enqueue;
380380
}
381381

382-
r = prandom_u32() & SFB_MAX_PROB;
382+
r = get_random_u16() & SFB_MAX_PROB;
383383

384384
if (unlikely(r < p_min)) {
385385
if (unlikely(p_min > SFB_MAX_PROB / 2)) {

0 commit comments

Comments
 (0)