• Kees Cook's avatar
    treewide: kzalloc() -> kcalloc() · 6396bb22
    Kees Cook authored
    The kzalloc() function has a 2-factor argument form, kcalloc(). This
    patch replaces cases of:
    
            kzalloc(a * b, gfp)
    
    with:
            kcalloc(a * b, gfp)
    
    as well as handling cases of:
    
            kzalloc(a * b * c, gfp)
    
    with:
    
            kzalloc(array3_size(a, b, c), gfp)
    
    as it's slightly less ugly than:
    
            kzalloc_array(array_size(a, b), c, gfp)
    
    This does, however, attempt to ignore constant size factors like:
    
            kzalloc(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 Coccinelle script used for this was:
    
    // Fix redundant parens around sizeof().
    @@
    type TYPE;
    expression THING, E;
    @@
    
    (
      kzalloc(
    -	(sizeof(TYPE)) * E
    +	sizeof(TYPE) * E
      , ...)
    |
      kzalloc(
    -	(sizeof(THING)) * E
    +	sizeof(THING) * E
      , ...)
    )
    
    // Drop single-byte sizes and redundant parens.
    @@
    expression COUNT;
    typedef u8;
    typedef __u8;
    @@
    
    (
      kzalloc(
    -	sizeof(u8) * (COUNT)
    +	COUNT
      , ...)
    |
      kzalloc(
    -	sizeof(__u8) * (COUNT)
    +	COUNT
      , ...)
    |
      kzalloc(
    -	sizeof(char) * (COUNT)
    +	COUNT
      , ...)
    |
      kzalloc(
    -	sizeof(unsigned char) * (COUNT)
    +	COUNT
      , ...)
    |
      kzalloc(
    -	sizeof(u8) * COUNT
    +	COUNT
      , ...)
    |
      kzalloc(
    -	sizeof(__u8) * COUNT
    +	COUNT
      , ...)
    |
      kzalloc(
    -	sizeof(char) * COUNT
    +	COUNT
      , ...)
    |
      kzalloc(
    -	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;
    @@
    
    (
    - kzalloc
    + kcalloc
      (
    -	sizeof(TYPE) * (COUNT_ID)
    +	COUNT_ID, sizeof(TYPE)
      , ...)
    |
    - kzalloc
    + kcalloc
      (
    -	sizeof(TYPE) * COUNT_ID
    +	COUNT_ID, sizeof(TYPE)
      , ...)
    |
    - kzalloc
    + kcalloc
      (
    -	sizeof(TYPE) * (COUNT_CONST)
    +	COUNT_CONST, sizeof(TYPE)
      , ...)
    |
    - kzalloc
    + kcalloc
      (
    -	sizeof(TYPE) * COUNT_CONST
    +	COUNT_CONST, sizeof(TYPE)
      , ...)
    |
    - kzalloc
    + kcalloc
      (
    -	sizeof(THING) * (COUNT_ID)
    +	COUNT_ID, sizeof(THING)
      , ...)
    |
    - kzalloc
    + kcalloc
      (
    -	sizeof(THING) * COUNT_ID
    +	COUNT_ID, sizeof(THING)
      , ...)
    |
    - kzalloc
    + kcalloc
      (
    -	sizeof(THING) * (COUNT_CONST)
    +	COUNT_CONST, sizeof(THING)
      , ...)
    |
    - kzalloc
    + kcalloc
      (
    -	sizeof(THING) * COUNT_CONST
    +	COUNT_CONST, sizeof(THING)
      , ...)
    )
    
    // 2-factor product, only identifiers.
    @@
    identifier SIZE, COUNT;
    @@
    
    - kzalloc
    + kcalloc
      (
    -	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;
    @@
    
    (
      kzalloc(
    -	sizeof(TYPE) * (COUNT) * (STRIDE)
    +	array3_size(COUNT, STRIDE, sizeof(TYPE))
      , ...)
    |
      kzalloc(
    -	sizeof(TYPE) * (COUNT) * STRIDE
    +	array3_size(COUNT, STRIDE, sizeof(TYPE))
      , ...)
    |
      kzalloc(
    -	sizeof(TYPE) * COUNT * (STRIDE)
    +	array3_size(COUNT, STRIDE, sizeof(TYPE))
      , ...)
    |
      kzalloc(
    -	sizeof(TYPE) * COUNT * STRIDE
    +	array3_size(COUNT, STRIDE, sizeof(TYPE))
      , ...)
    |
      kzalloc(
    -	sizeof(THING) * (COUNT) * (STRIDE)
    +	array3_size(COUNT, STRIDE, sizeof(THING))
      , ...)
    |
      kzalloc(
    -	sizeof(THING) * (COUNT) * STRIDE
    +	array3_size(COUNT, STRIDE, sizeof(THING))
      , ...)
    |
      kzalloc(
    -	sizeof(THING) * COUNT * (STRIDE)
    +	array3_size(COUNT, STRIDE, sizeof(THING))
      , ...)
    |
      kzalloc(
    -	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;
    @@
    
    (
      kzalloc(
    -	sizeof(TYPE1) * sizeof(TYPE2) * COUNT
    +	array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
      , ...)
    |
      kzalloc(
    -	sizeof(TYPE1) * sizeof(THING2) * (COUNT)
    +	array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
      , ...)
    |
      kzalloc(
    -	sizeof(THING1) * sizeof(THING2) * COUNT
    +	array3_size(COUNT, sizeof(THING1), sizeof(THING2))
      , ...)
    |
      kzalloc(
    -	sizeof(THING1) * sizeof(THING2) * (COUNT)
    +	array3_size(COUNT, sizeof(THING1), sizeof(THING2))
      , ...)
    |
      kzalloc(
    -	sizeof(TYPE1) * sizeof(THING2) * COUNT
    +	array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
      , ...)
    |
      kzalloc(
    -	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;
    @@
    
    (
      kzalloc(
    -	(COUNT) * STRIDE * SIZE
    +	array3_size(COUNT, STRIDE, SIZE)
      , ...)
    |
      kzalloc(
    -	COUNT * (STRIDE) * SIZE
    +	array3_size(COUNT, STRIDE, SIZE)
      , ...)
    |
      kzalloc(
    -	COUNT * STRIDE * (SIZE)
    +	array3_size(COUNT, STRIDE, SIZE)
      , ...)
    |
      kzalloc(
    -	(COUNT) * (STRIDE) * SIZE
    +	array3_size(COUNT, STRIDE, SIZE)
      , ...)
    |
      kzalloc(
    -	COUNT * (STRIDE) * (SIZE)
    +	array3_size(COUNT, STRIDE, SIZE)
      , ...)
    |
      kzalloc(
    -	(COUNT) * STRIDE * (SIZE)
    +	array3_size(COUNT, STRIDE, SIZE)
      , ...)
    |
      kzalloc(
    -	(COUNT) * (STRIDE) * (SIZE)
    +	array3_size(COUNT, STRIDE, SIZE)
      , ...)
    |
      kzalloc(
    -	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;
    @@
    
    (
      kzalloc(C1 * C2 * C3, ...)
    |
      kzalloc(
    -	(E1) * E2 * E3
    +	array3_size(E1, E2, E3)
      , ...)
    |
      kzalloc(
    -	(E1) * (E2) * E3
    +	array3_size(E1, E2, E3)
      , ...)
    |
      kzalloc(
    -	(E1) * (E2) * (E3)
    +	array3_size(E1, E2, E3)
      , ...)
    |
      kzalloc(
    -	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;
    @@
    
    (
      kzalloc(sizeof(THING) * C2, ...)
    |
      kzalloc(sizeof(TYPE) * C2, ...)
    |
      kzalloc(C1 * C2 * C3, ...)
    |
      kzalloc(C1 * C2, ...)
    |
    - kzalloc
    + kcalloc
      (
    -	sizeof(TYPE) * (E2)
    +	E2, sizeof(TYPE)
      , ...)
    |
    - kzalloc
    + kcalloc
      (
    -	sizeof(TYPE) * E2
    +	E2, sizeof(TYPE)
      , ...)
    |
    - kzalloc
    + kcalloc
      (
    -	sizeof(THING) * (E2)
    +	E2, sizeof(THING)
      , ...)
    |
    - kzalloc
    + kcalloc
      (
    -	sizeof(THING) * E2
    +	E2, sizeof(THING)
      , ...)
    |
    - kzalloc
    + kcalloc
      (
    -	(E1) * E2
    +	E1, E2
      , ...)
    |
    - kzalloc
    + kcalloc
      (
    -	(E1) * (E2)
    +	E1, E2
      , ...)
    |
    - kzalloc
    + kcalloc
      (
    -	E1 * E2
    +	E1, E2
      , ...)
    )
    Signed-off-by: default avatarKees Cook <keescook@chromium.org>
    6396bb22
Name
Last commit
Last update
..
boot Loading commit data...
common Loading commit data...
configs Loading commit data...
crypto Loading commit data...
firmware Loading commit data...
include Loading commit data...
kernel Loading commit data...
kvm Loading commit data...
lib Loading commit data...
mach-actions Loading commit data...
mach-alpine Loading commit data...
mach-artpec Loading commit data...
mach-asm9260 Loading commit data...
mach-aspeed Loading commit data...
mach-at91 Loading commit data...
mach-axxia Loading commit data...
mach-bcm Loading commit data...
mach-berlin Loading commit data...
mach-clps711x Loading commit data...
mach-cns3xxx Loading commit data...
mach-davinci Loading commit data...
mach-digicolor Loading commit data...
mach-dove Loading commit data...
mach-ebsa110 Loading commit data...
mach-efm32 Loading commit data...
mach-ep93xx Loading commit data...
mach-exynos Loading commit data...
mach-footbridge Loading commit data...
mach-gemini Loading commit data...
mach-highbank Loading commit data...
mach-hisi Loading commit data...
mach-imx Loading commit data...
mach-integrator Loading commit data...
mach-iop13xx Loading commit data...
mach-iop32x Loading commit data...
mach-iop33x Loading commit data...
mach-ixp4xx Loading commit data...
mach-keystone Loading commit data...
mach-ks8695 Loading commit data...
mach-lpc18xx Loading commit data...
mach-lpc32xx Loading commit data...
mach-mediatek Loading commit data...
mach-meson Loading commit data...
mach-mmp Loading commit data...
mach-moxart Loading commit data...
mach-mv78xx0 Loading commit data...
mach-mvebu Loading commit data...
mach-mxs Loading commit data...
mach-netx Loading commit data...
mach-nomadik Loading commit data...
mach-npcm Loading commit data...
mach-nspire Loading commit data...
mach-omap1 Loading commit data...
mach-omap2 Loading commit data...
mach-orion5x Loading commit data...
mach-oxnas Loading commit data...
mach-picoxcell Loading commit data...
mach-prima2 Loading commit data...
mach-pxa Loading commit data...
mach-qcom Loading commit data...
mach-realview Loading commit data...
mach-rockchip Loading commit data...
mach-rpc Loading commit data...
mach-s3c24xx Loading commit data...
mach-s3c64xx Loading commit data...
mach-s5pv210 Loading commit data...
mach-sa1100 Loading commit data...
mach-shmobile Loading commit data...
mach-socfpga Loading commit data...
mach-spear Loading commit data...
mach-sti Loading commit data...
mach-stm32 Loading commit data...
mach-sunxi Loading commit data...
mach-tango Loading commit data...
mach-tegra Loading commit data...
mach-u300 Loading commit data...
mach-uniphier Loading commit data...
mach-ux500 Loading commit data...
mach-versatile Loading commit data...
mach-vexpress Loading commit data...
mach-vt8500 Loading commit data...
mach-w90x900 Loading commit data...
mach-zx Loading commit data...
mach-zynq Loading commit data...
mm Loading commit data...
net Loading commit data...
nwfpe Loading commit data...
oprofile Loading commit data...
plat-iop Loading commit data...
plat-omap Loading commit data...
plat-orion Loading commit data...
plat-pxa Loading commit data...
plat-samsung Loading commit data...
plat-versatile Loading commit data...
probes Loading commit data...
tools Loading commit data...
vdso Loading commit data...
vfp Loading commit data...
xen Loading commit data...
Kconfig Loading commit data...
Kconfig-nommu Loading commit data...
Kconfig.debug Loading commit data...
Makefile Loading commit data...