• 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
..
Kconfig Loading commit data...
Makefile Loading commit data...
internals.h Loading commit data...
spi-altera.c Loading commit data...
spi-armada-3700.c Loading commit data...
spi-ath79.c Loading commit data...
spi-atmel.c Loading commit data...
spi-au1550.c Loading commit data...
spi-axi-spi-engine.c Loading commit data...
spi-bcm-qspi.c Loading commit data...
spi-bcm-qspi.h Loading commit data...
spi-bcm2835.c Loading commit data...
spi-bcm2835aux.c Loading commit data...
spi-bcm63xx-hsspi.c Loading commit data...
spi-bcm63xx.c Loading commit data...
spi-bitbang-txrx.h Loading commit data...
spi-bitbang.c Loading commit data...
spi-brcmstb-qspi.c Loading commit data...
spi-butterfly.c Loading commit data...
spi-cadence.c Loading commit data...
spi-cavium-octeon.c Loading commit data...
spi-cavium-thunderx.c Loading commit data...
spi-cavium.c Loading commit data...
spi-cavium.h Loading commit data...
spi-clps711x.c Loading commit data...
spi-coldfire-qspi.c Loading commit data...
spi-davinci.c Loading commit data...
spi-dln2.c Loading commit data...
spi-dw-mid.c Loading commit data...
spi-dw-mmio.c Loading commit data...
spi-dw-pci.c Loading commit data...
spi-dw.c Loading commit data...
spi-dw.h Loading commit data...
spi-efm32.c Loading commit data...
spi-ep93xx.c Loading commit data...
spi-falcon.c Loading commit data...
spi-fsl-cpm.c Loading commit data...
spi-fsl-cpm.h Loading commit data...
spi-fsl-dspi.c Loading commit data...
spi-fsl-espi.c Loading commit data...
spi-fsl-lib.c Loading commit data...
spi-fsl-lib.h Loading commit data...
spi-fsl-lpspi.c Loading commit data...
spi-fsl-spi.c Loading commit data...
spi-fsl-spi.h Loading commit data...
spi-gpio.c Loading commit data...
spi-img-spfi.c Loading commit data...
spi-imx.c Loading commit data...
spi-iproc-qspi.c Loading commit data...
spi-jcore.c Loading commit data...
spi-lantiq-ssc.c Loading commit data...
spi-lm70llp.c Loading commit data...
spi-loopback-test.c Loading commit data...
spi-lp8841-rtc.c Loading commit data...
spi-mem.c Loading commit data...
spi-meson-spicc.c Loading commit data...
spi-meson-spifc.c Loading commit data...
spi-mpc512x-psc.c Loading commit data...
spi-mpc52xx-psc.c Loading commit data...
spi-mpc52xx.c Loading commit data...
spi-mt65xx.c Loading commit data...
spi-mxs.c Loading commit data...
spi-nuc900.c Loading commit data...
spi-oc-tiny.c Loading commit data...
spi-omap-100k.c Loading commit data...
spi-omap-uwire.c Loading commit data...
spi-omap2-mcspi.c Loading commit data...
spi-orion.c Loading commit data...
spi-pic32-sqi.c Loading commit data...
spi-pic32.c Loading commit data...
spi-pl022.c Loading commit data...
spi-ppc4xx.c Loading commit data...
spi-pxa2xx-dma.c Loading commit data...
spi-pxa2xx-pci.c Loading commit data...
spi-pxa2xx.c Loading commit data...
spi-pxa2xx.h Loading commit data...
spi-qup.c Loading commit data...
spi-rb4xx.c Loading commit data...
spi-rockchip.c Loading commit data...
spi-rspi.c Loading commit data...
spi-s3c24xx-fiq.S Loading commit data...
spi-s3c24xx-fiq.h Loading commit data...
spi-s3c24xx.c Loading commit data...
spi-s3c64xx.c Loading commit data...
spi-sc18is602.c Loading commit data...
spi-sh-hspi.c Loading commit data...
spi-sh-msiof.c Loading commit data...
spi-sh-sci.c Loading commit data...
spi-sh.c Loading commit data...
spi-sirf.c Loading commit data...
spi-slave-system-control.c Loading commit data...
spi-slave-time.c Loading commit data...
spi-sprd-adi.c Loading commit data...
spi-st-ssc4.c Loading commit data...
spi-stm32.c Loading commit data...
spi-sun4i.c Loading commit data...
spi-sun6i.c Loading commit data...
spi-tegra114.c Loading commit data...
spi-tegra20-sflash.c Loading commit data...
spi-tegra20-slink.c Loading commit data...
spi-test.h Loading commit data...
spi-ti-qspi.c Loading commit data...
spi-tle62x0.c Loading commit data...
spi-topcliff-pch.c Loading commit data...
spi-txx9.c Loading commit data...
spi-xcomm.c Loading commit data...
spi-xilinx.c Loading commit data...
spi-xlp.c Loading commit data...
spi-xtensa-xtfpga.c Loading commit data...
spi-zynqmp-gqspi.c Loading commit data...
spi.c Loading commit data...
spidev.c Loading commit data...