• Kees Cook's avatar
    treewide: Use array_size() in vmalloc() · 42bc47b3
    Kees Cook authored
    The vmalloc() function has no 2-factor argument form, so multiplication
    factors need to be wrapped in array_size(). This patch replaces cases of:
    
            vmalloc(a * b)
    
    with:
            vmalloc(array_size(a, b))
    
    as well as handling cases of:
    
            vmalloc(a * b * c)
    
    with:
    
            vmalloc(array3_size(a, b, c))
    
    This does, however, attempt to ignore constant size factors like:
    
            vmalloc(4 * 1024)
    
    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;
    @@
    
    (
      vmalloc(
    -	(sizeof(TYPE)) * E
    +	sizeof(TYPE) * E
      , ...)
    |
      vmalloc(
    -	(sizeof(THING)) * E
    +	sizeof(THING) * E
      , ...)
    )
    
    // Drop single-byte sizes and redundant parens.
    @@
    expression COUNT;
    typedef u8;
    typedef __u8;
    @@
    
    (
      vmalloc(
    -	sizeof(u8) * (COUNT)
    +	COUNT
      , ...)
    |
      vmalloc(
    -	sizeof(__u8) * (COUNT)
    +	COUNT
      , ...)
    |
      vmalloc(
    -	sizeof(char) * (COUNT)
    +	COUNT
      , ...)
    |
      vmalloc(
    -	sizeof(unsigned char) * (COUNT)
    +	COUNT
      , ...)
    |
      vmalloc(
    -	sizeof(u8) * COUNT
    +	COUNT
      , ...)
    |
      vmalloc(
    -	sizeof(__u8) * COUNT
    +	COUNT
      , ...)
    |
      vmalloc(
    -	sizeof(char) * COUNT
    +	COUNT
      , ...)
    |
      vmalloc(
    -	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;
    @@
    
    (
      vmalloc(
    -	sizeof(TYPE) * (COUNT_ID)
    +	array_size(COUNT_ID, sizeof(TYPE))
      , ...)
    |
      vmalloc(
    -	sizeof(TYPE) * COUNT_ID
    +	array_size(COUNT_ID, sizeof(TYPE))
      , ...)
    |
      vmalloc(
    -	sizeof(TYPE) * (COUNT_CONST)
    +	array_size(COUNT_CONST, sizeof(TYPE))
      , ...)
    |
      vmalloc(
    -	sizeof(TYPE) * COUNT_CONST
    +	array_size(COUNT_CONST, sizeof(TYPE))
      , ...)
    |
      vmalloc(
    -	sizeof(THING) * (COUNT_ID)
    +	array_size(COUNT_ID, sizeof(THING))
      , ...)
    |
      vmalloc(
    -	sizeof(THING) * COUNT_ID
    +	array_size(COUNT_ID, sizeof(THING))
      , ...)
    |
      vmalloc(
    -	sizeof(THING) * (COUNT_CONST)
    +	array_size(COUNT_CONST, sizeof(THING))
      , ...)
    |
      vmalloc(
    -	sizeof(THING) * COUNT_CONST
    +	array_size(COUNT_CONST, sizeof(THING))
      , ...)
    )
    
    // 2-factor product, only identifiers.
    @@
    identifier SIZE, COUNT;
    @@
    
      vmalloc(
    -	SIZE * COUNT
    +	array_size(COUNT, SIZE)
      , ...)
    
    // 3-factor product with 1 sizeof(type) or sizeof(expression), with
    // redundant parens removed.
    @@
    expression THING;
    identifier STRIDE, COUNT;
    type TYPE;
    @@
    
    (
      vmalloc(
    -	sizeof(TYPE) * (COUNT) * (STRIDE)
    +	array3_size(COUNT, STRIDE, sizeof(TYPE))
      , ...)
    |
      vmalloc(
    -	sizeof(TYPE) * (COUNT) * STRIDE
    +	array3_size(COUNT, STRIDE, sizeof(TYPE))
      , ...)
    |
      vmalloc(
    -	sizeof(TYPE) * COUNT * (STRIDE)
    +	array3_size(COUNT, STRIDE, sizeof(TYPE))
      , ...)
    |
      vmalloc(
    -	sizeof(TYPE) * COUNT * STRIDE
    +	array3_size(COUNT, STRIDE, sizeof(TYPE))
      , ...)
    |
      vmalloc(
    -	sizeof(THING) * (COUNT) * (STRIDE)
    +	array3_size(COUNT, STRIDE, sizeof(THING))
      , ...)
    |
      vmalloc(
    -	sizeof(THING) * (COUNT) * STRIDE
    +	array3_size(COUNT, STRIDE, sizeof(THING))
      , ...)
    |
      vmalloc(
    -	sizeof(THING) * COUNT * (STRIDE)
    +	array3_size(COUNT, STRIDE, sizeof(THING))
      , ...)
    |
      vmalloc(
    -	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;
    @@
    
    (
      vmalloc(
    -	sizeof(TYPE1) * sizeof(TYPE2) * COUNT
    +	array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
      , ...)
    |
      vmalloc(
    -	sizeof(TYPE1) * sizeof(THING2) * (COUNT)
    +	array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
      , ...)
    |
      vmalloc(
    -	sizeof(THING1) * sizeof(THING2) * COUNT
    +	array3_size(COUNT, sizeof(THING1), sizeof(THING2))
      , ...)
    |
      vmalloc(
    -	sizeof(THING1) * sizeof(THING2) * (COUNT)
    +	array3_size(COUNT, sizeof(THING1), sizeof(THING2))
      , ...)
    |
      vmalloc(
    -	sizeof(TYPE1) * sizeof(THING2) * COUNT
    +	array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
      , ...)
    |
      vmalloc(
    -	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;
    @@
    
    (
      vmalloc(
    -	(COUNT) * STRIDE * SIZE
    +	array3_size(COUNT, STRIDE, SIZE)
      , ...)
    |
      vmalloc(
    -	COUNT * (STRIDE) * SIZE
    +	array3_size(COUNT, STRIDE, SIZE)
      , ...)
    |
      vmalloc(
    -	COUNT * STRIDE * (SIZE)
    +	array3_size(COUNT, STRIDE, SIZE)
      , ...)
    |
      vmalloc(
    -	(COUNT) * (STRIDE) * SIZE
    +	array3_size(COUNT, STRIDE, SIZE)
      , ...)
    |
      vmalloc(
    -	COUNT * (STRIDE) * (SIZE)
    +	array3_size(COUNT, STRIDE, SIZE)
      , ...)
    |
      vmalloc(
    -	(COUNT) * STRIDE * (SIZE)
    +	array3_size(COUNT, STRIDE, SIZE)
      , ...)
    |
      vmalloc(
    -	(COUNT) * (STRIDE) * (SIZE)
    +	array3_size(COUNT, STRIDE, SIZE)
      , ...)
    |
      vmalloc(
    -	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;
    @@
    
    (
      vmalloc(C1 * C2 * C3, ...)
    |
      vmalloc(
    -	E1 * E2 * E3
    +	array3_size(E1, E2, E3)
      , ...)
    )
    
    // And then all remaining 2 factors products when they're not all constants.
    @@
    expression E1, E2;
    constant C1, C2;
    @@
    
    (
      vmalloc(C1 * C2, ...)
    |
      vmalloc(
    -	E1 * E2
    +	array_size(E1, E2)
      , ...)
    )
    Signed-off-by: default avatarKees Cook <keescook@chromium.org>
    42bc47b3
Name
Last commit
Last update
..
amd Loading commit data...
arc Loading commit data...
arm Loading commit data...
armada Loading commit data...
ast Loading commit data...
atmel-hlcdc Loading commit data...
bochs Loading commit data...
bridge Loading commit data...
cirrus Loading commit data...
etnaviv Loading commit data...
exynos Loading commit data...
fsl-dcu Loading commit data...
gma500 Loading commit data...
hisilicon Loading commit data...
i2c Loading commit data...
i810 Loading commit data...
i915 Loading commit data...
imx Loading commit data...
lib Loading commit data...
mediatek Loading commit data...
meson Loading commit data...
mga Loading commit data...
mgag200 Loading commit data...
msm Loading commit data...
mxsfb Loading commit data...
nouveau Loading commit data...
omapdrm Loading commit data...
panel Loading commit data...
pl111 Loading commit data...
qxl Loading commit data...
r128 Loading commit data...
radeon Loading commit data...
rcar-du Loading commit data...
rockchip Loading commit data...
savage Loading commit data...
scheduler Loading commit data...
selftests Loading commit data...
shmobile Loading commit data...
sis Loading commit data...
sti Loading commit data...
stm Loading commit data...
sun4i Loading commit data...
tdfx Loading commit data...
tegra Loading commit data...
tilcdc Loading commit data...
tinydrm Loading commit data...
ttm Loading commit data...
tve200 Loading commit data...
udl Loading commit data...
v3d Loading commit data...
vc4 Loading commit data...
vgem Loading commit data...
via Loading commit data...
virtio Loading commit data...
vmwgfx Loading commit data...
xen Loading commit data...
zte Loading commit data...
Kconfig Loading commit data...
Makefile Loading commit data...
ati_pcigart.c Loading commit data...
drm_agpsupport.c Loading commit data...
drm_atomic.c Loading commit data...
drm_atomic_helper.c Loading commit data...
drm_auth.c Loading commit data...
drm_blend.c Loading commit data...
drm_bridge.c Loading commit data...
drm_bufs.c Loading commit data...
drm_cache.c Loading commit data...
drm_color_mgmt.c Loading commit data...
drm_connector.c Loading commit data...
drm_context.c Loading commit data...
drm_crtc.c Loading commit data...
drm_crtc_helper.c Loading commit data...
drm_crtc_helper_internal.h Loading commit data...
drm_crtc_internal.h Loading commit data...
drm_debugfs.c Loading commit data...
drm_debugfs_crc.c Loading commit data...
drm_dma.c Loading commit data...
drm_dp_aux_dev.c Loading commit data...
drm_dp_dual_mode_helper.c Loading commit data...
drm_dp_helper.c Loading commit data...
drm_dp_mst_topology.c Loading commit data...
drm_drv.c Loading commit data...
drm_dumb_buffers.c Loading commit data...
drm_edid.c Loading commit data...
drm_edid_load.c Loading commit data...
drm_encoder.c Loading commit data...
drm_encoder_slave.c Loading commit data...
drm_fb_cma_helper.c Loading commit data...
drm_fb_helper.c Loading commit data...
drm_file.c Loading commit data...
drm_flip_work.c Loading commit data...
drm_fourcc.c Loading commit data...
drm_framebuffer.c Loading commit data...
drm_gem.c Loading commit data...
drm_gem_cma_helper.c Loading commit data...
drm_gem_framebuffer_helper.c Loading commit data...
drm_global.c Loading commit data...
drm_hashtab.c Loading commit data...
drm_info.c Loading commit data...
drm_internal.h Loading commit data...
drm_ioc32.c Loading commit data...
drm_ioctl.c Loading commit data...
drm_irq.c Loading commit data...
drm_kms_helper_common.c Loading commit data...
drm_lease.c Loading commit data...
drm_legacy.h Loading commit data...
drm_lock.c Loading commit data...
drm_memory.c Loading commit data...
drm_mipi_dsi.c Loading commit data...
drm_mm.c Loading commit data...
drm_mode_config.c Loading commit data...
drm_mode_object.c Loading commit data...
drm_modes.c Loading commit data...
drm_modeset_helper.c Loading commit data...
drm_modeset_lock.c Loading commit data...
drm_of.c Loading commit data...
drm_panel.c Loading commit data...
drm_panel_orientation_quirks.c Loading commit data...
drm_pci.c Loading commit data...
drm_plane.c Loading commit data...
drm_plane_helper.c Loading commit data...
drm_prime.c Loading commit data...
drm_print.c Loading commit data...
drm_probe_helper.c Loading commit data...
drm_property.c Loading commit data...
drm_rect.c Loading commit data...
drm_scatter.c Loading commit data...
drm_scdc_helper.c Loading commit data...
drm_simple_kms_helper.c Loading commit data...
drm_syncobj.c Loading commit data...
drm_sysfs.c Loading commit data...
drm_trace.h Loading commit data...
drm_trace_points.c Loading commit data...
drm_vblank.c Loading commit data...
drm_vm.c Loading commit data...
drm_vma_manager.c Loading commit data...