• Kees Cook's avatar
    treewide: devm_kzalloc() -> devm_kcalloc() · a86854d0
    Kees Cook authored
    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: default avatarKees Cook <keescook@chromium.org>
    a86854d0
Name
Last commit
Last update
..
88pg86x.c Loading commit data...
88pm800.c Loading commit data...
88pm8607.c Loading commit data...
Kconfig Loading commit data...
Makefile Loading commit data...
aat2870-regulator.c Loading commit data...
ab3100.c Loading commit data...
ab8500-ext.c Loading commit data...
ab8500.c Loading commit data...
act8865-regulator.c Loading commit data...
act8945a-regulator.c Loading commit data...
ad5398.c Loading commit data...
anatop-regulator.c Loading commit data...
arizona-ldo1.c Loading commit data...
arizona-micsupp.c Loading commit data...
as3711-regulator.c Loading commit data...
as3722-regulator.c Loading commit data...
axp20x-regulator.c Loading commit data...
bcm590xx-regulator.c Loading commit data...
bd71837-regulator.c Loading commit data...
bd9571mwv-regulator.c Loading commit data...
core.c Loading commit data...
cpcap-regulator.c Loading commit data...
da903x.c Loading commit data...
da9052-regulator.c Loading commit data...
da9055-regulator.c Loading commit data...
da9062-regulator.c Loading commit data...
da9063-regulator.c Loading commit data...
da9210-regulator.c Loading commit data...
da9210-regulator.h Loading commit data...
da9211-regulator.c Loading commit data...
da9211-regulator.h Loading commit data...
db8500-prcmu.c Loading commit data...
dbx500-prcmu.c Loading commit data...
dbx500-prcmu.h Loading commit data...
devres.c Loading commit data...
dummy.c Loading commit data...
dummy.h Loading commit data...
fan53555.c Loading commit data...
fixed-helper.c Loading commit data...
fixed.c Loading commit data...
gpio-regulator.c Loading commit data...
helpers.c Loading commit data...
hi6421-regulator.c Loading commit data...
hi6421v530-regulator.c Loading commit data...
hi655x-regulator.c Loading commit data...
internal.h Loading commit data...
isl6271a-regulator.c Loading commit data...
isl9305.c Loading commit data...
lm363x-regulator.c Loading commit data...
lp3971.c Loading commit data...
lp3972.c Loading commit data...
lp872x.c Loading commit data...
lp873x-regulator.c Loading commit data...
lp8755.c Loading commit data...
lp87565-regulator.c Loading commit data...
lp8788-buck.c Loading commit data...
lp8788-ldo.c Loading commit data...
ltc3589.c Loading commit data...
ltc3676.c Loading commit data...
max14577-regulator.c Loading commit data...
max1586.c Loading commit data...
max77620-regulator.c Loading commit data...
max77686-regulator.c Loading commit data...
max77693-regulator.c Loading commit data...
max77802-regulator.c Loading commit data...
max8649.c Loading commit data...
max8660.c Loading commit data...
max8907-regulator.c Loading commit data...
max8925-regulator.c Loading commit data...
max8952.c Loading commit data...
max8973-regulator.c Loading commit data...
max8997-regulator.c Loading commit data...
max8998.c Loading commit data...
mc13783-regulator.c Loading commit data...
mc13892-regulator.c Loading commit data...
mc13xxx-regulator-core.c Loading commit data...
mc13xxx.h Loading commit data...
mt6311-regulator.c Loading commit data...
mt6311-regulator.h Loading commit data...
mt6323-regulator.c Loading commit data...
mt6380-regulator.c Loading commit data...
mt6397-regulator.c Loading commit data...
of_regulator.c Loading commit data...
palmas-regulator.c Loading commit data...
pbias-regulator.c Loading commit data...
pcap-regulator.c Loading commit data...
pcf50633-regulator.c Loading commit data...
pfuze100-regulator.c Loading commit data...
pv88060-regulator.c Loading commit data...
pv88060-regulator.h Loading commit data...
pv88080-regulator.c Loading commit data...
pv88080-regulator.h Loading commit data...
pv88090-regulator.c Loading commit data...
pv88090-regulator.h Loading commit data...
pwm-regulator.c Loading commit data...
qcom_rpm-regulator.c Loading commit data...
qcom_smd-regulator.c Loading commit data...
qcom_spmi-regulator.c Loading commit data...
rc5t583-regulator.c Loading commit data...
rk808-regulator.c Loading commit data...
rn5t618-regulator.c Loading commit data...
rt5033-regulator.c Loading commit data...
s2mpa01.c Loading commit data...
s2mps11.c Loading commit data...
s5m8767.c Loading commit data...
sc2731-regulator.c Loading commit data...
sky81452-regulator.c Loading commit data...
stm32-vrefbuf.c Loading commit data...
stw481x-vmmc.c Loading commit data...
sy8106a-regulator.c Loading commit data...
ti-abb-regulator.c Loading commit data...
tps51632-regulator.c Loading commit data...
tps6105x-regulator.c Loading commit data...
tps62360-regulator.c Loading commit data...
tps65023-regulator.c Loading commit data...
tps6507x-regulator.c Loading commit data...
tps65086-regulator.c Loading commit data...
tps65090-regulator.c Loading commit data...
tps65132-regulator.c Loading commit data...
tps65217-regulator.c Loading commit data...
tps65218-regulator.c Loading commit data...
tps6524x-regulator.c Loading commit data...
tps6586x-regulator.c Loading commit data...
tps65910-regulator.c Loading commit data...
tps65912-regulator.c Loading commit data...
tps80031-regulator.c Loading commit data...
twl-regulator.c Loading commit data...
twl6030-regulator.c Loading commit data...
userspace-consumer.c Loading commit data...
vctrl-regulator.c Loading commit data...
vexpress-regulator.c Loading commit data...
virtual.c Loading commit data...
wm831x-dcdc.c Loading commit data...
wm831x-isink.c Loading commit data...
wm831x-ldo.c Loading commit data...
wm8350-regulator.c Loading commit data...
wm8400-regulator.c Loading commit data...
wm8994-regulator.c Loading commit data...