• 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
..
aacraid Loading commit data...
aic7xxx Loading commit data...
aic94xx Loading commit data...
arcmsr Loading commit data...
arm Loading commit data...
be2iscsi Loading commit data...
bfa Loading commit data...
bnx2fc Loading commit data...
bnx2i Loading commit data...
csiostor Loading commit data...
cxgbi Loading commit data...
cxlflash Loading commit data...
device_handler Loading commit data...
dpt Loading commit data...
esas2r Loading commit data...
fcoe Loading commit data...
fnic Loading commit data...
hisi_sas Loading commit data...
ibmvscsi Loading commit data...
ibmvscsi_tgt Loading commit data...
isci Loading commit data...
libfc Loading commit data...
libsas Loading commit data...
lpfc Loading commit data...
megaraid Loading commit data...
mpt3sas Loading commit data...
mvsas Loading commit data...
osd Loading commit data...
pcmcia Loading commit data...
pm8001 Loading commit data...
qedf Loading commit data...
qedi Loading commit data...
qla2xxx Loading commit data...
qla4xxx Loading commit data...
smartpqi Loading commit data...
snic Loading commit data...
sym53c8xx_2 Loading commit data...
ufs Loading commit data...
.gitignore Loading commit data...
3w-9xxx.c Loading commit data...
3w-9xxx.h Loading commit data...
3w-sas.c Loading commit data...
3w-sas.h Loading commit data...
3w-xxxx.c Loading commit data...
3w-xxxx.h Loading commit data...
53c700.c Loading commit data...
53c700.h Loading commit data...
53c700.scr Loading commit data...
53c700_d.h_shipped Loading commit data...
BusLogic.c Loading commit data...
BusLogic.h Loading commit data...
FlashPoint.c Loading commit data...
Kconfig Loading commit data...
Makefile Loading commit data...
NCR5380.c Loading commit data...
NCR5380.h Loading commit data...
NCR_D700.c Loading commit data...
NCR_D700.h Loading commit data...
NCR_Q720.c Loading commit data...
NCR_Q720.h Loading commit data...
a100u2w.c Loading commit data...
a100u2w.h Loading commit data...
a2091.c Loading commit data...
a2091.h Loading commit data...
a3000.c Loading commit data...
a3000.h Loading commit data...
a4000t.c Loading commit data...
advansys.c Loading commit data...
aha152x.c Loading commit data...
aha152x.h Loading commit data...
aha1542.c Loading commit data...
aha1542.h Loading commit data...
aha1740.c Loading commit data...
aha1740.h Loading commit data...
am53c974.c Loading commit data...
atari_scsi.c Loading commit data...
atp870u.c Loading commit data...
atp870u.h Loading commit data...
bvme6000_scsi.c Loading commit data...
ch.c Loading commit data...
constants.c Loading commit data...
dc395x.c Loading commit data...
dc395x.h Loading commit data...
dmx3191d.c Loading commit data...
dpt_i2o.c Loading commit data...
dpti.h Loading commit data...
esp_scsi.c Loading commit data...
esp_scsi.h Loading commit data...
g_NCR5380.c Loading commit data...
gdth.c Loading commit data...
gdth.h Loading commit data...
gdth_ioctl.h Loading commit data...
gdth_proc.c Loading commit data...
gdth_proc.h Loading commit data...
gvp11.c Loading commit data...
gvp11.h Loading commit data...
hosts.c Loading commit data...
hpsa.c Loading commit data...
hpsa.h Loading commit data...
hpsa_cmd.h Loading commit data...
hptiop.c Loading commit data...
hptiop.h Loading commit data...
imm.c Loading commit data...
imm.h Loading commit data...
initio.c Loading commit data...
initio.h Loading commit data...
ipr.c Loading commit data...
ipr.h Loading commit data...
ips.c Loading commit data...
ips.h Loading commit data...
iscsi_boot_sysfs.c Loading commit data...
iscsi_tcp.c Loading commit data...
iscsi_tcp.h Loading commit data...
jazz_esp.c Loading commit data...
lasi700.c Loading commit data...
libiscsi.c Loading commit data...
libiscsi_tcp.c Loading commit data...
mac53c94.c Loading commit data...
mac53c94.h Loading commit data...
mac_esp.c Loading commit data...
mac_scsi.c Loading commit data...
megaraid.c Loading commit data...
megaraid.h Loading commit data...
mesh.c Loading commit data...
mesh.h Loading commit data...
mvme147.c Loading commit data...
mvme147.h Loading commit data...
mvme16x_scsi.c Loading commit data...
mvumi.c Loading commit data...
mvumi.h Loading commit data...
ncr53c8xx.c Loading commit data...
ncr53c8xx.h Loading commit data...
nsp32.c Loading commit data...
nsp32.h Loading commit data...
nsp32_debug.c Loading commit data...
nsp32_io.h Loading commit data...
osst.c Loading commit data...
osst.h Loading commit data...
osst_detect.h Loading commit data...
osst_options.h Loading commit data...
pmcraid.c Loading commit data...
pmcraid.h Loading commit data...
ppa.c Loading commit data...
ppa.h Loading commit data...
ps3rom.c Loading commit data...
qla1280.c Loading commit data...
qla1280.h Loading commit data...
qlogicfas.c Loading commit data...
qlogicfas408.c Loading commit data...
qlogicfas408.h Loading commit data...
qlogicpti.c Loading commit data...
qlogicpti.h Loading commit data...
raid_class.c Loading commit data...
script_asm.pl Loading commit data...
scsi.c Loading commit data...
scsi.h Loading commit data...
scsi_common.c Loading commit data...
scsi_debug.c Loading commit data...
scsi_debugfs.c Loading commit data...
scsi_debugfs.h Loading commit data...
scsi_devinfo.c Loading commit data...
scsi_dh.c Loading commit data...
scsi_error.c Loading commit data...
scsi_ioctl.c Loading commit data...
scsi_lib.c Loading commit data...
scsi_lib_dma.c Loading commit data...
scsi_logging.c Loading commit data...
scsi_logging.h Loading commit data...
scsi_netlink.c Loading commit data...
scsi_pm.c Loading commit data...
scsi_priv.h Loading commit data...
scsi_proc.c Loading commit data...
scsi_sas_internal.h Loading commit data...
scsi_scan.c Loading commit data...
scsi_sysctl.c Loading commit data...
scsi_sysfs.c Loading commit data...
scsi_trace.c Loading commit data...
scsi_transport_api.h Loading commit data...
scsi_transport_fc.c Loading commit data...
scsi_transport_iscsi.c Loading commit data...
scsi_transport_sas.c Loading commit data...
scsi_transport_spi.c Loading commit data...
scsi_transport_srp.c Loading commit data...
scsi_typedefs.h Loading commit data...
scsicam.c Loading commit data...
sd.c Loading commit data...
sd.h Loading commit data...
sd_dif.c Loading commit data...
sd_zbc.c Loading commit data...
sense_codes.h Loading commit data...
ses.c Loading commit data...
sg.c Loading commit data...
sgiwd93.c Loading commit data...
sim710.c Loading commit data...
sni_53c710.c Loading commit data...
sr.c Loading commit data...
sr.h Loading commit data...
sr_ioctl.c Loading commit data...
sr_vendor.c Loading commit data...
st.c Loading commit data...
st.h Loading commit data...
st_options.h Loading commit data...
stex.c Loading commit data...
storvsc_drv.c Loading commit data...
sun3_scsi.c Loading commit data...
sun3_scsi_vme.c Loading commit data...
sun3x_esp.c Loading commit data...
sun_esp.c Loading commit data...
virtio_scsi.c Loading commit data...
vmw_pvscsi.c Loading commit data...
vmw_pvscsi.h Loading commit data...
wd33c93.c Loading commit data...
wd33c93.h Loading commit data...
wd719x.c Loading commit data...
wd719x.h Loading commit data...
xen-scsifront.c Loading commit data...
zalon.c Loading commit data...
zorro7xx.c Loading commit data...
zorro_esp.c Loading commit data...