• 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: 's avatarKees Cook <keescook@chromium.org>
    42bc47b3
Name
Last commit
Last update
..
Kconfig Loading commit data...
Makefile Loading commit data...
ast.c Loading commit data...
ast.h Loading commit data...
config.c Loading commit data...
config.h Loading commit data...
debug_fs.c Loading commit data...
dir.c Loading commit data...
dir.h Loading commit data...
dlm_internal.h Loading commit data...
lock.c Loading commit data...
lock.h Loading commit data...
lockspace.c Loading commit data...
lockspace.h Loading commit data...
lowcomms.c Loading commit data...
lowcomms.h Loading commit data...
lvb_table.h Loading commit data...
main.c Loading commit data...
member.c Loading commit data...
member.h Loading commit data...
memory.c Loading commit data...
memory.h Loading commit data...
midcomms.c Loading commit data...
midcomms.h Loading commit data...
netlink.c Loading commit data...
plock.c Loading commit data...
rcom.c Loading commit data...
rcom.h Loading commit data...
recover.c Loading commit data...
recover.h Loading commit data...
recoverd.c Loading commit data...
recoverd.h Loading commit data...
requestqueue.c Loading commit data...
requestqueue.h Loading commit data...
user.c Loading commit data...
user.h Loading commit data...
util.c Loading commit data...
util.h Loading commit data...