WSL2-Linux-Kernel/drivers/staging
Kees Cook 6da2ec5605 treewide: kmalloc() -> kmalloc_array()
The kmalloc() function has a 2-factor argument form, kmalloc_array(). This
patch replaces cases of:

        kmalloc(a * b, gfp)

with:
        kmalloc_array(a * b, gfp)

as well as handling cases of:

        kmalloc(a * b * c, gfp)

with:

        kmalloc(array3_size(a, b, c), gfp)

as it's slightly less ugly than:

        kmalloc_array(array_size(a, b), c, gfp)

This does, however, attempt to ignore constant size factors like:

        kmalloc(4 * 1024, gfp)

though any constants defined via macros get caught up in the conversion.

Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.

The tools/ directory was manually excluded, since it has its own
implementation of kmalloc().

The Coccinelle script used for this was:

// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@

(
  kmalloc(
-	(sizeof(TYPE)) * E
+	sizeof(TYPE) * E
  , ...)
|
  kmalloc(
-	(sizeof(THING)) * E
+	sizeof(THING) * E
  , ...)
)

// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@

(
  kmalloc(
-	sizeof(u8) * (COUNT)
+	COUNT
  , ...)
|
  kmalloc(
-	sizeof(__u8) * (COUNT)
+	COUNT
  , ...)
|
  kmalloc(
-	sizeof(char) * (COUNT)
+	COUNT
  , ...)
|
  kmalloc(
-	sizeof(unsigned char) * (COUNT)
+	COUNT
  , ...)
|
  kmalloc(
-	sizeof(u8) * COUNT
+	COUNT
  , ...)
|
  kmalloc(
-	sizeof(__u8) * COUNT
+	COUNT
  , ...)
|
  kmalloc(
-	sizeof(char) * COUNT
+	COUNT
  , ...)
|
  kmalloc(
-	sizeof(unsigned char) * COUNT
+	COUNT
  , ...)
)

// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@

(
- kmalloc
+ kmalloc_array
  (
-	sizeof(TYPE) * (COUNT_ID)
+	COUNT_ID, sizeof(TYPE)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(TYPE) * COUNT_ID
+	COUNT_ID, sizeof(TYPE)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(TYPE) * (COUNT_CONST)
+	COUNT_CONST, sizeof(TYPE)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(TYPE) * COUNT_CONST
+	COUNT_CONST, sizeof(TYPE)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(THING) * (COUNT_ID)
+	COUNT_ID, sizeof(THING)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(THING) * COUNT_ID
+	COUNT_ID, sizeof(THING)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(THING) * (COUNT_CONST)
+	COUNT_CONST, sizeof(THING)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(THING) * COUNT_CONST
+	COUNT_CONST, sizeof(THING)
  , ...)
)

// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@

- kmalloc
+ kmalloc_array
  (
-	SIZE * COUNT
+	COUNT, SIZE
  , ...)

// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@

(
  kmalloc(
-	sizeof(TYPE) * (COUNT) * (STRIDE)
+	array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  kmalloc(
-	sizeof(TYPE) * (COUNT) * STRIDE
+	array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  kmalloc(
-	sizeof(TYPE) * COUNT * (STRIDE)
+	array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  kmalloc(
-	sizeof(TYPE) * COUNT * STRIDE
+	array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  kmalloc(
-	sizeof(THING) * (COUNT) * (STRIDE)
+	array3_size(COUNT, STRIDE, sizeof(THING))
  , ...)
|
  kmalloc(
-	sizeof(THING) * (COUNT) * STRIDE
+	array3_size(COUNT, STRIDE, sizeof(THING))
  , ...)
|
  kmalloc(
-	sizeof(THING) * COUNT * (STRIDE)
+	array3_size(COUNT, STRIDE, sizeof(THING))
  , ...)
|
  kmalloc(
-	sizeof(THING) * COUNT * STRIDE
+	array3_size(COUNT, STRIDE, sizeof(THING))
  , ...)
)

// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@

(
  kmalloc(
-	sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+	array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
  , ...)
|
  kmalloc(
-	sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+	array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
  , ...)
|
  kmalloc(
-	sizeof(THING1) * sizeof(THING2) * COUNT
+	array3_size(COUNT, sizeof(THING1), sizeof(THING2))
  , ...)
|
  kmalloc(
-	sizeof(THING1) * sizeof(THING2) * (COUNT)
+	array3_size(COUNT, sizeof(THING1), sizeof(THING2))
  , ...)
|
  kmalloc(
-	sizeof(TYPE1) * sizeof(THING2) * COUNT
+	array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
  , ...)
|
  kmalloc(
-	sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+	array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
  , ...)
)

// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@

(
  kmalloc(
-	(COUNT) * STRIDE * SIZE
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	COUNT * (STRIDE) * SIZE
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	COUNT * STRIDE * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	(COUNT) * (STRIDE) * SIZE
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	COUNT * (STRIDE) * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	(COUNT) * STRIDE * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	(COUNT) * (STRIDE) * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	COUNT * STRIDE * SIZE
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
)

// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@

(
  kmalloc(C1 * C2 * C3, ...)
|
  kmalloc(
-	(E1) * E2 * E3
+	array3_size(E1, E2, E3)
  , ...)
|
  kmalloc(
-	(E1) * (E2) * E3
+	array3_size(E1, E2, E3)
  , ...)
|
  kmalloc(
-	(E1) * (E2) * (E3)
+	array3_size(E1, E2, E3)
  , ...)
|
  kmalloc(
-	E1 * E2 * E3
+	array3_size(E1, E2, E3)
  , ...)
)

// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@

(
  kmalloc(sizeof(THING) * C2, ...)
|
  kmalloc(sizeof(TYPE) * C2, ...)
|
  kmalloc(C1 * C2 * C3, ...)
|
  kmalloc(C1 * C2, ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(TYPE) * (E2)
+	E2, sizeof(TYPE)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(TYPE) * E2
+	E2, sizeof(TYPE)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(THING) * (E2)
+	E2, sizeof(THING)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(THING) * E2
+	E2, sizeof(THING)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	(E1) * E2
+	E1, E2
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	(E1) * (E2)
+	E1, E2
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	E1 * E2
+	E1, E2
  , ...)
)

Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-12 16:19:22 -07:00
..
android staging: android: ion: Switch to pr_warn_once in ion_buffer_destroy 2018-05-15 09:31:39 +02:00
board staging: board: Replace license boilerplate with SPDX identifiers 2018-05-06 19:11:23 -07:00
clocking-wizard
comedi Staging/IIO patches for 4.18-rc1 2018-06-09 10:32:39 -07:00
dgnc
emxx_udc staging: emxx_udc: Add GPIO descriptor work to TODO 2018-04-23 14:36:10 +02:00
fbtft - Core Frameworks 2018-06-11 07:23:19 -07:00
fsl-dpaa2 Staging/IIO patches for 4.18-rc1 2018-06-09 10:32:39 -07:00
fsl-mc staging: fsl-mc/dpio: Fix the error handling in probe() 2018-04-29 15:25:08 +02:00
fwserial tty: replace ->proc_fops with ->proc_show 2018-05-16 07:24:30 +02:00
gdm724x staging: gdm724x: remove redundant license information 2018-04-26 09:17:34 +02:00
goldfish Staging: goldfish: Kconfig: fixed code style issue 2018-05-20 14:31:28 +02:00
greybus Staging/IIO patches for 4.18-rc1 2018-06-09 10:32:39 -07:00
gs_fpgaboot
iio staging:iio:meter: Drop ADE7759 driver 2018-05-20 12:21:54 +01:00
ks7010 staging: ks7010: avoid casting inside cpu_to_* assignments 2018-05-20 14:47:14 +02:00
media treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
most staging: most: video: fix build warnings 2018-05-14 11:42:28 +02:00
mt29f_spinand
mt7621-dma
mt7621-dts staging: mt7621-gpio: update #interrupt-cells for the gpio node 2018-06-01 13:16:23 +02:00
mt7621-eth staging: mt7621-eth: Remove unused variable 2018-05-15 09:31:38 +02:00
mt7621-gpio staging: mt7621-gpio: reorder includes alphabetically 2018-06-01 13:16:24 +02:00
mt7621-mmc staging: mt7621-mmc: Fix line size exceeding 80 columns 2018-06-01 10:52:48 +02:00
mt7621-pci staging: mt7621-pci: Fix line size exceeding 80 columns. 2018-05-31 19:00:53 +02:00
mt7621-pinctrl staging: mt7621-pinctrl: Remove redundant owner assignment 2018-04-23 15:37:13 +02:00
mt7621-spi staging: mt7621-spi: remove unused lock. 2018-05-06 19:09:23 -07:00
netlogic
nvec Revert "staging: nvec: Augment TODO file with GPIO work item" 2018-04-23 18:49:51 +02:00
octeon
octeon-usb
olpc_dcon staging: olpc_dcon: Augment TODO file with GPIO work item 2018-04-23 14:36:10 +02:00
pi433 staging: pi433: break long lines in pi433_if.c 2018-04-25 15:58:04 +02:00
rtl8188eu Staging:rtl888eu:include Fix CamelCase issue 2018-05-31 19:04:39 +02:00
rtl8192e staging: rtl8192e: rtllib_tx: fix spelling issue. 2018-05-25 18:44:14 +02:00
rtl8192u treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
rtl8712
rtl8723bs staging: rtl8723bs: Fix grammar error in comment. 2018-05-25 18:47:22 +02:00
rtlwifi staging: rtlwifi: don't check the return value of debugfs_create_file 2018-06-01 10:47:43 +02:00
rts5208
skein
sm750fb staging: sm750fb: add drm development branch details 2018-05-06 18:51:38 -07:00
speakup staging: speakup: use true/false instead of 1/0 2018-05-15 09:31:38 +02:00
typec staging: typec: rt1711h typec chip driver 2018-04-22 16:08:25 +02:00
unisys staging: unisys: visornic: no need to check debugfs return values 2018-06-01 10:47:43 +02:00
vboxvideo Staging/IIO patches for 4.18-rc1 2018-06-09 10:32:39 -07:00
vc04_services staging: vc04_services: no need to save the log debufs dentries 2018-06-01 20:15:56 +02:00
vme
vt6655 staging: vt6655: remove unnecessary line breaks in function definition. 2018-04-23 15:23:36 +02:00
vt6656
wilc1000 staging: wilc1000: Avoid overriding rates_no while parsing ies element. 2018-05-25 18:45:47 +02:00
wlan-ng staging: wlan-ng: remove unused declarations from p80211types.h 2018-05-31 19:00:53 +02:00
xgifb
Kconfig staging: ipx: delete it from the tree 2018-06-05 19:23:26 +02:00
Makefile staging: ipx: delete it from the tree 2018-06-05 19:23:26 +02:00