treewide: Use array_size() in vmalloc()
authorKees Cook <keescook@chromium.org>
Tue, 12 Jun 2018 21:27:11 +0000 (14:27 -0700)
committerKees Cook <keescook@chromium.org>
Tue, 12 Jun 2018 23:19:22 +0000 (16:19 -0700)
commit42bc47b35320e0e587a88e437e18f80f9c5bcbb2
tree95c9f023d6f31176ea867c6bb13d85a1963c6d18
parenta86854d0c599b3202307abceb68feee4d7061578
treewide: Use array_size() in vmalloc()

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: Kees Cook <keescook@chromium.org>
78 files changed:
arch/powerpc/kernel/rtasd.c
arch/powerpc/kvm/book3s_64_mmu_hv.c
arch/s390/hypfs/hypfs_diag.c
arch/s390/kernel/module.c
arch/s390/kernel/sthyi.c
arch/s390/kvm/gaccess.c
arch/s390/kvm/kvm-s390.c
arch/x86/kvm/cpuid.c
drivers/base/firmware_loader/fallback.c
drivers/dma/ipu/ipu_idmac.c
drivers/gpu/drm/drm_memory.c
drivers/gpu/drm/nouveau/nv84_fence.c
drivers/gpu/drm/qxl/qxl_fb.c
drivers/gpu/drm/radeon/radeon_gart.c
drivers/gpu/drm/selftests/test-drm_mm.c
drivers/iommu/tegra-gart.c
drivers/isdn/i4l/isdn_bsdcomp.c
drivers/lightnvm/pblk-gc.c
drivers/md/bcache/sysfs.c
drivers/md/dm-cache-policy-smq.c
drivers/md/dm-region-hash.c
drivers/md/dm-switch.c
drivers/md/dm-thin.c
drivers/media/dvb-core/dmxdev.c
drivers/media/dvb-core/dvb_demux.c
drivers/media/pci/meye/meye.c
drivers/media/pci/pt1/pt1.c
drivers/media/pci/ttpci/av7110_ipack.c
drivers/media/platform/soc_camera/soc_camera.c
drivers/media/v4l2-core/videobuf-dma-sg.c
drivers/mtd/ftl.c
drivers/mtd/mtdoops.c
drivers/mtd/mtdswap.c
drivers/mtd/nand/raw/nandsim.c
drivers/mtd/rfd_ftl.c
drivers/net/ethernet/cavium/liquidio/request_manager.c
drivers/net/ethernet/intel/fm10k/fm10k_ethtool.c
drivers/net/ethernet/intel/igb/igb_ethtool.c
drivers/net/ethernet/intel/ixgbe/ixgbe_ethtool.c
drivers/net/ethernet/intel/ixgbevf/ethtool.c
drivers/net/ethernet/netronome/nfp/flower/metadata.c
drivers/net/ppp/bsd_comp.c
drivers/net/wireless/ath/ath5k/debug.c
drivers/net/wireless/marvell/mwifiex/cfg80211.c
drivers/oprofile/event_buffer.c
drivers/rapidio/devices/rio_mport_cdev.c
drivers/scsi/fnic/fnic_debugfs.c
drivers/scsi/fnic/fnic_trace.c
drivers/scsi/ipr.c
drivers/scsi/osst.c
drivers/scsi/scsi_debug.c
drivers/staging/android/ion/ion_heap.c
drivers/staging/greybus/camera.c
drivers/staging/media/zoran/zoran_driver.c
drivers/staging/rts5208/ms.c
drivers/staging/rts5208/rtsx_chip.c
drivers/usb/misc/sisusbvga/sisusb_con.c
drivers/video/fbdev/xen-fbfront.c
fs/binfmt_elf.c
fs/cifs/misc.c
fs/dlm/lockspace.c
fs/reiserfs/bitmap.c
fs/ubifs/lpt.c
kernel/cgroup/cgroup-v1.c
kernel/power/swap.c
kernel/rcu/rcutorture.c
kernel/trace/tracing_map.c
mm/percpu-stats.c
net/bridge/netfilter/ebtables.c
net/netfilter/ipvs/ip_vs_conn.c
sound/core/seq/seq_memory.c
sound/pci/cs46xx/dsp_spos.c
sound/pci/emu10k1/emu10k1_main.c
sound/pci/emu10k1/emufx.c
sound/pci/emu10k1/p16v.c
sound/pci/maestro3.c
sound/pci/trident/trident_main.c
virt/kvm/kvm_main.c