linux-stable/drivers
Jason A. Donenfeld 81895a65ec treewide: use prandom_u32_max() when possible, part 1
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>
2022-10-11 17:42:55 -06:00
..
accessibility
acpi More ACPI updates for 6.1-rc1 2022-10-10 13:28:06 -07:00
amba
android Scheduler changes for v6.1: 2022-10-10 09:10:28 -07:00
ata ata changes for 6.1-rc1 2022-10-07 10:48:49 -07:00
atm
auxdisplay
base - Yu Zhao's Multi-Gen LRU patches are here. They've been under test in 2022-10-10 17:53:04 -07:00
bcma
block treewide: use prandom_u32_max() when possible, part 1 2022-10-11 17:42:55 -06:00
bluetooth
bus Char/Misc and other driver changes for 6.1-rc1 2022-10-08 08:56:37 -07:00
cdrom
char Fix a bunch of little problems in IPMI 2022-10-11 10:42:25 -07:00
clk Here's the main clk pull request for this merge window. We have some 2022-10-08 10:06:48 -07:00
clocksource A boring time, timekeeping, timers update: 2022-10-10 10:16:00 -07:00
comedi
connector
counter gpio updates for v6.1-rc1 2022-10-08 09:46:29 -07:00
cpufreq Char/Misc and other driver changes for 6.1-rc1 2022-10-08 08:56:37 -07:00
cpuidle RISC-V Patches for the 6.1 Merge Window, Part 1 2022-10-09 13:24:01 -07:00
crypto This update includes the following changes: 2022-10-10 13:04:25 -07:00
cxl
dax - Yu Zhao's Multi-Gen LRU patches are here. They've been under test in 2022-10-10 17:53:04 -07:00
dca
devfreq
dio
dma dmaengine updates for v6.1-rc1 2022-10-07 15:56:34 -07:00
dma-buf whack-a-mole: cropped up open-coded file_inode() uses... 2022-10-06 17:22:11 -07:00
edac Merge branches 'edac-drivers' and 'edac-misc' into edac-updates-for-v6.1 2022-10-04 10:00:25 +02:00
eisa
extcon Char/Misc and other driver changes for 6.1-rc1 2022-10-08 08:56:37 -07:00
firewire
firmware Merge branch 'dmi-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jdelvare/staging 2022-10-11 10:44:20 -07:00
fpga Char/Misc and other driver changes for 6.1-rc1 2022-10-08 08:56:37 -07:00
fsi
gnss
gpio These are the pin control changes for the v6.1 kernel cycle: 2022-10-11 10:59:59 -07:00
gpu treewide: use prandom_u32_max() when possible, part 1 2022-10-11 17:42:55 -06:00
greybus
hid Input updates for 6.1 merge window: 2022-10-11 10:53:25 -07:00
hsi
hte
hv hyperv-next for 6.1 2022-10-10 13:59:01 -07:00
hwmon Char/Misc and other driver changes for 6.1-rc1 2022-10-08 08:56:37 -07:00
hwspinlock
hwtracing Char/Misc and other driver changes for 6.1-rc1 2022-10-08 08:56:37 -07:00
i2c - correct a variable type in the new pci1xxxx driver 2022-10-11 11:03:42 -07:00
i3c
idle
iio Char/Misc and other driver changes for 6.1-rc1 2022-10-08 08:56:37 -07:00
infiniband treewide: use prandom_u32_max() when possible, part 1 2022-10-11 17:42:55 -06:00
input Input updates for 6.1 merge window: 2022-10-11 10:53:25 -07:00
interconnect
iommu IOMMU Updates for Linux v6.1: 2022-10-10 13:20:53 -07:00
ipack Char/Misc and other driver changes for 6.1-rc1 2022-10-08 08:56:37 -07:00
irqchip IOMMU Updates for Linux v6.1: 2022-10-10 13:20:53 -07:00
isdn Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net 2022-10-03 17:44:18 -07:00
leds platform-drivers-x86 for v6.1-1 2022-10-05 10:38:24 -07:00
macintosh powerpc updates for 6.1 2022-10-09 14:05:15 -07:00
mailbox mailbox: qcom-ipcc: flag IRQ NO_THREAD 2022-10-05 21:51:58 -05:00
mcb
md treewide: use prandom_u32_max() when possible, part 1 2022-10-11 17:42:55 -06:00
media treewide: use prandom_u32_max() when possible, part 1 2022-10-11 17:42:55 -06:00
memory
memstick
message
mfd - Core Frameworks 2022-10-07 11:24:20 -07:00
misc - Yu Zhao's Multi-Gen LRU patches are here. They've been under test in 2022-10-10 17:53:04 -07:00
mmc treewide: use prandom_u32_max() when possible, part 1 2022-10-11 17:42:55 -06:00
most
mtd treewide: use prandom_u32_max() when possible, part 1 2022-10-11 17:42:55 -06:00
mux
net treewide: use prandom_u32_max() when possible, part 1 2022-10-11 17:42:55 -06:00
nfc
ntb
nubus
nvdimm - Yu Zhao's Multi-Gen LRU patches are here. They've been under test in 2022-10-10 17:53:04 -07:00
nvme for-6.1/passthrough-2022-10-04 2022-10-07 09:35:50 -07:00
nvmem
of Devicetree updates for v6.1: 2022-10-10 13:13:51 -07:00
opp
parisc asm-generic updates for v6.1 2022-10-06 12:10:37 -07:00
parport
pci pci-v6.1-changes 2022-10-11 11:08:18 -07:00
pcmcia
peci
perf Perf events updates for v6.1: 2022-10-10 09:27:46 -07:00
phy pci-v6.1-changes 2022-10-11 11:08:18 -07:00
pinctrl These are the pin control changes for the v6.1 kernel cycle: 2022-10-11 10:59:59 -07:00
platform More ACPI updates for 6.1-rc1 2022-10-10 13:28:06 -07:00
pnp Merge branches 'acpi-apei', 'acpi-wakeup', 'acpi-reboot' and 'acpi-thermal' 2022-10-10 18:11:11 +02:00
power power supply and reset changes for the v6.1 series 2022-10-07 11:48:30 -07:00
powercap Scheduler changes for v6.1: 2022-10-10 09:10:28 -07:00
pps
ps3
ptp
pwm pwm: Changes for v6.1-rc1 2022-10-07 11:32:10 -07:00
rapidio
ras
regulator - Core Frameworks 2022-10-07 11:24:20 -07:00
remoteproc remoteproc: virtio: Fix warning on bindings by removing the of_match_table 2022-10-05 09:20:44 -06:00
reset Here's the main clk pull request for this merge window. We have some 2022-10-08 10:06:48 -07:00
rpmsg
rtc
s390 s390 updates for the 6.1 merge window 2022-10-09 13:51:40 -07:00
sbus
scsi treewide: use prandom_u32_max() when possible, part 1 2022-10-11 17:42:55 -06:00
sh
siox
slimbus
soc Char/Misc and other driver changes for 6.1-rc1 2022-10-08 08:56:37 -07:00
soundwire soundwire updates for 6.1-rc1 2022-10-07 16:13:55 -07:00
spi spi: Updates for v6.1 2022-10-04 19:36:53 -07:00
spmi spmi: pmic-arb: increase SPMI transaction timeout delay 2022-09-30 14:33:23 +02:00
ssb
staging Here's the main clk pull request for this merge window. We have some 2022-10-08 10:06:48 -07:00
target SCSI misc on 20221007 2022-10-07 12:33:18 -07:00
tc
tee - Yu Zhao's Multi-Gen LRU patches are here. They've been under test in 2022-10-10 17:53:04 -07:00
thermal More thermal control updates for 6.1-rc1 2022-10-10 13:52:14 -07:00
thunderbolt USB/Thunderbolt changes for 6.1-rc1 2022-10-07 16:48:26 -07:00
tty TTY/Serial driver update for 6.1-rc1 2022-10-07 16:36:24 -07:00
ufs SCSI misc on 20221007 2022-10-07 12:33:18 -07:00
uio
usb - Yu Zhao's Multi-Gen LRU patches are here. They've been under test in 2022-10-10 17:53:04 -07:00
vdpa virtio: fixes, features 2022-10-10 14:02:53 -07:00
vfio
vhost virtio: fixes, features 2022-10-10 14:02:53 -07:00
video fbdev: mb862xx: Fix check of return value from irq_of_parse_and_map() 2022-10-08 15:20:09 +02:00
virt Char/Misc and other driver changes for 6.1-rc1 2022-10-08 08:56:37 -07:00
virtio - Yu Zhao's Multi-Gen LRU patches are here. They've been under test in 2022-10-10 17:53:04 -07:00
vlynq
w1 Char/Misc and other driver changes for 6.1-rc1 2022-10-08 08:56:37 -07:00
watchdog powerpc updates for 6.1 2022-10-09 14:05:15 -07:00
xen - Yu Zhao's Multi-Gen LRU patches are here. They've been under test in 2022-10-10 17:53:04 -07:00
zorro
Kconfig
Makefile