kees treewide: devm_kzalloc() -> devm_kcalloc()
The devm_kzalloc() function has a 2-factor argument form, devm_kcalloc().
This patch replaces cases of:

        devm_kzalloc(handle, a * b, gfp)

with:
        devm_kcalloc(handle, a * b, gfp)

as well as handling cases of:

        devm_kzalloc(handle, a * b * c, gfp)

with:

        devm_kzalloc(handle, array3_size(a, b, c), gfp)

as it's slightly less ugly than:

        devm_kcalloc(handle, array_size(a, b), c, gfp)

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

        devm_kzalloc(handle, 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.

Some manual whitespace fixes were needed in this patch, as Coccinelle
really liked to write "=devm_kcalloc..." instead of "= devm_kcalloc...".

The Coccinelle script used for this was:

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

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

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

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

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

(
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	sizeof(TYPE) * (COUNT_ID)
+	COUNT_ID, sizeof(TYPE)
  , ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	sizeof(TYPE) * COUNT_ID
+	COUNT_ID, sizeof(TYPE)
  , ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	sizeof(TYPE) * (COUNT_CONST)
+	COUNT_CONST, sizeof(TYPE)
  , ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	sizeof(TYPE) * COUNT_CONST
+	COUNT_CONST, sizeof(TYPE)
  , ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	sizeof(THING) * (COUNT_ID)
+	COUNT_ID, sizeof(THING)
  , ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	sizeof(THING) * COUNT_ID
+	COUNT_ID, sizeof(THING)
  , ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	sizeof(THING) * (COUNT_CONST)
+	COUNT_CONST, sizeof(THING)
  , ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	sizeof(THING) * COUNT_CONST
+	COUNT_CONST, sizeof(THING)
  , ...)
)

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

- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	SIZE * COUNT
+	COUNT, SIZE
  , ...)

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

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

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

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

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

(
  devm_kzalloc(HANDLE,
-	(COUNT) * STRIDE * SIZE
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  devm_kzalloc(HANDLE,
-	COUNT * (STRIDE) * SIZE
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  devm_kzalloc(HANDLE,
-	COUNT * STRIDE * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  devm_kzalloc(HANDLE,
-	(COUNT) * (STRIDE) * SIZE
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  devm_kzalloc(HANDLE,
-	COUNT * (STRIDE) * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  devm_kzalloc(HANDLE,
-	(COUNT) * STRIDE * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  devm_kzalloc(HANDLE,
-	(COUNT) * (STRIDE) * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  devm_kzalloc(HANDLE,
-	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 HANDLE;
expression E1, E2, E3;
constant C1, C2, C3;
@@

(
  devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
  devm_kzalloc(HANDLE,
-	(E1) * E2 * E3
+	array3_size(E1, E2, E3)
  , ...)
|
  devm_kzalloc(HANDLE,
-	(E1) * (E2) * E3
+	array3_size(E1, E2, E3)
  , ...)
|
  devm_kzalloc(HANDLE,
-	(E1) * (E2) * (E3)
+	array3_size(E1, E2, E3)
  , ...)
|
  devm_kzalloc(HANDLE,
-	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 HANDLE;
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@

(
  devm_kzalloc(HANDLE, sizeof(THING) * C2, ...)
|
  devm_kzalloc(HANDLE, sizeof(TYPE) * C2, ...)
|
  devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
  devm_kzalloc(HANDLE, C1 * C2, ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	sizeof(TYPE) * (E2)
+	E2, sizeof(TYPE)
  , ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	sizeof(TYPE) * E2
+	E2, sizeof(TYPE)
  , ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	sizeof(THING) * (E2)
+	E2, sizeof(THING)
  , ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	sizeof(THING) * E2
+	E2, sizeof(THING)
  , ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	(E1) * E2
+	E1, E2
  , ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	(E1) * (E2)
+	E1, E2
  , ...)
|
- devm_kzalloc
+ devm_kcalloc
  (HANDLE,
-	E1 * E2
+	E1, E2
  , ...)
)

Signed-off-by: Kees Cook <keescook@chromium.org>
Latest commit a86854d Jun 12, 2018
Permalink
..
Failed to load latest commit information.
trigger leds: Extends disk trigger for reads and writes Mar 11, 2018
Kconfig leds: lm3601x: Introduce the lm3601x LED driver May 23, 2018
Makefile leds: lm3601x: Introduce the lm3601x LED driver May 23, 2018
led-class-flash.c leds: flash: Remove checking for state < 1 in flash_strobe_store() Jan 4, 2016
led-class.c leds: class: ensure workqueue is initialized before setting brightness May 24, 2018
led-core.c leds: core: Fix regression caused by commit 2b83ff9 Jan 7, 2018
led-triggers.c leds: triggers: Check return value of kobject_uevent_env() Sep 20, 2016
leds-88pm860x.c leds: 88pm860x: Use devm_led_classdev_register Mar 14, 2016
leds-aat1290.c Merge tag 'leds_for_4.14' of git://git.kernel.org/pub/scm/linux/kerne… Sep 7, 2017
leds-adp5520.c treewide: devm_kzalloc() -> devm_kcalloc() Jun 12, 2018
leds-apu.c treewide: devm_kzalloc() -> devm_kcalloc() Jun 12, 2018
leds-as3645a.c leds: as3645a: Fix line over 80 characters Jan 8, 2018
leds-asic3.c leds: drop owner assignment from platform_drivers Oct 20, 2014
leds-bcm6328.c leds: bcm6328: fix signal source assignment for leds 4 to 7 Jun 13, 2017
leds-bcm6358.c leds: bcm6358: remove unneeded busy status check Jan 4, 2016
leds-bd2802.c leds: bd2802: Remove work queue Jan 4, 2016
leds-blinkm.c leds: blinkm: avoid uninitialized data use Jan 8, 2018
leds-clevo-mail.c dmi: Mark all struct dmi_system_id instances const Sep 14, 2017
leds-cobalt-qube.c leds: leds-cobalt-qube: Use devm_led_classdev_register Nov 3, 2015
leds-cobalt-raq.c leds: leds-cobalt-raq: use builtin_platform_driver Nov 23, 2016
leds-cpcap.c leds: cpcap: new driver Mar 29, 2017
leds-cr0014114.c leds: Use struct_size() in allocation Jun 12, 2018
leds-da903x.c leds: da903x: Use devm_led_classdev_register Mar 14, 2016
leds-da9052.c treewide: devm_kzalloc() -> devm_kcalloc() Jun 12, 2018
leds-dac124s085.c leds: dac124d085: Remove work queue Jan 4, 2016
leds-fsg.c leds: leds-fsg: Use devm_led_classdev_register Aug 28, 2015
leds-gpio-register.c led: gpio: Fix possible ZERO_SIZE_PTR pointer dereferencing error. Sep 29, 2014
leds-gpio.c leds: gpio: Allow LED to retain state at shutdown Aug 29, 2017
leds-hp6xx.c leds: convert IDE trigger to common disk trigger Jun 20, 2016
leds-ipaq-micro.c leds: ipaq-micro: Implement brightness_set_blocking op Jan 4, 2016
leds-is31fl319x.c leds: is31fl319x: 1/3/6/9-channel light effect led driver Aug 15, 2016
leds-is31fl32xx.c leds: Convert to using %pOF instead of full_name Aug 12, 2017
leds-ktd2692.c leds: ktd2692: avoid harmless maybe-uninitialized warning Jan 26, 2017
leds-lm3530.c leds: Drop owner assignment from i2c_driver Aug 28, 2015
leds-lm3533.c leds: lm3533: constify attribute_group structure Aug 29, 2017
leds-lm355x.c leds: lm355x: Remove work queue Jan 4, 2016
leds-lm3601x.c leds: lm3601x: Introduce the lm3601x LED driver May 23, 2018
leds-lm3642.c leds: lm3642: Remove work queue Jan 4, 2016
leds-lm3692x.c leds: lm3692x: Introduce LM3692x dual string driver Jan 8, 2018
leds-locomo.c leds: leds-locomo.c: Use devm_led_classdev_register Nov 3, 2015
leds-lp3944.c leds: lp3944: improve wording and formatting in a comment Mar 14, 2016
leds-lp3952.c leds: lp3952: Use 'if (ret)' pattern Mar 23, 2017
leds-lp5521.c treewide: devm_kzalloc() -> devm_kcalloc() Jun 12, 2018
leds-lp5523.c treewide: devm_kzalloc() -> devm_kcalloc() Jun 12, 2018
leds-lp5562.c treewide: devm_kzalloc() -> devm_kcalloc() Jun 12, 2018
leds-lp55xx-common.c treewide: devm_kzalloc() -> devm_kcalloc() Jun 12, 2018
leds-lp55xx-common.h leds: lp55xx: Remove work queue Jan 4, 2016
leds-lp8501.c treewide: devm_kzalloc() -> devm_kcalloc() Jun 12, 2018
leds-lp8788.c leds: lp8788: Use devm_led_classdev_register Mar 14, 2016
leds-lp8860.c leds: lp8860: Various fixes to align with LED framework Jan 8, 2018
leds-lt3593.c treewide: devm_kzalloc() -> devm_kcalloc() Jun 12, 2018
leds-max77693.c media: v4l2-flash-led-class: Create separate sub-devices for indicators Aug 27, 2017
leds-max8997.c leds: max8997: Use devm_led_classdev_register Mar 14, 2016
leds-mc13783.c treewide: devm_kzalloc() -> devm_kcalloc() Jun 12, 2018
leds-menf21bmc.c leds: leds-menf21bmc.c: Use devm_led_class_register Nov 3, 2015
leds-mlxcpld.c treewide: devm_kzalloc() -> devm_kcalloc() Jun 12, 2018
leds-mlxreg.c leds: add driver for support Mellanox regmap LEDs for BMC and x86 pla… Feb 19, 2018
leds-mt6323.c leds: mt6323: Fix an off by one bug in probe Mar 23, 2017
leds-net48xx.c leds: leds-net48xx: Use devm_led_classdev_register Nov 3, 2015
leds-netxbig.c treewide: devm_kzalloc() -> devm_kcalloc() Jun 12, 2018
leds-nic78bx.c leds: Add user LED driver for NIC78bx device Nov 22, 2016
leds-ns2.c treewide: devm_kzalloc() -> devm_kcalloc() Jun 12, 2018
leds-ot200.c leds: leds-ot200: Use devm_led_classdev_register Nov 3, 2015
leds-pca9532.c leds: pca9532: Extend pca9532 device tree support Apr 19, 2017
leds-pca955x.c treewide: devm_kzalloc() -> devm_kcalloc() Jun 12, 2018
leds-pca963x.c treewide: devm_kzalloc() -> devm_kcalloc() Jun 12, 2018
leds-pm8058.c leds: pm8058: Silence pointer to integer size warning Dec 1, 2017
leds-powernv.c leds: powernv: Delete an error message for a failed memory allocation… Aug 29, 2017
leds-pwm.c leds: pwm: Remove unneeded header file Jan 8, 2018
leds-rb532.c leds: drop owner assignment from platform_drivers Oct 20, 2014
leds-regulator.c leds: regulator: Remove work queue Jan 4, 2016
leds-s3c24xx.c leds: s3c24xx: Use devm_led_classdev_register Mar 14, 2016
leds-sc27xx-bltc.c leds: sc27xx: Fix return value check in sc27xx_led_probe() May 22, 2018
leds-ss4200.c dmi: Mark all struct dmi_system_id instances const Sep 14, 2017
leds-sunfire.c leds: sunfire: Use platform_register/unregister_drivers() Jan 4, 2016
leds-syscon.c leds: syscon: Make the driver explicitly non-modular Jan 4, 2016
leds-tca6507.c treewide: devm_kzalloc() -> devm_kcalloc() Jun 12, 2018
leds-tlc591xx.c leds: tlc591xx: add missing of_node_put Jul 16, 2017
leds-wm831x-status.c leds: wm831x-status: Use sysfs_match_string() helper May 3, 2018
leds-wm8350.c leds: wm8350: Remove work queue Jan 4, 2016
leds-wrap.c leds: leds-wrap.c: Use devm_led_classdev_register Nov 3, 2015
leds.h leds: triggers: Allow to switch the trigger to "panic" on a kernel panic May 6, 2016
uleds.c vfs: do bulk POLL* -> EPOLL* replacement Feb 11, 2018