treewide: kvmalloc() -> kvmalloc_array()
authorKees Cook <keescook@chromium.org>
Tue, 12 Jun 2018 21:04:32 +0000 (14:04 -0700)
committerKees Cook <keescook@chromium.org>
Tue, 12 Jun 2018 23:19:22 +0000 (16:19 -0700)
The kvmalloc() function has a 2-factor argument form, kvmalloc_array(). This
patch replaces cases of:

        kvmalloc(a * b, gfp)

with:
        kvmalloc_array(a * b, gfp)

as well as handling cases of:

        kvmalloc(a * b * c, gfp)

with:

        kvmalloc(array3_size(a, b, c), gfp)

as it's slightly less ugly than:

        kvmalloc_array(array_size(a, b), c, gfp)

This does, however, attempt to ignore constant size factors like:

        kvmalloc(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;
@@

(
  kvmalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
  , ...)
|
  kvmalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
  , ...)
)

// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@

(
  kvmalloc(
- sizeof(u8) * (COUNT)
+ COUNT
  , ...)
|
  kvmalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
  , ...)
|
  kvmalloc(
- sizeof(char) * (COUNT)
+ COUNT
  , ...)
|
  kvmalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
  , ...)
|
  kvmalloc(
- sizeof(u8) * COUNT
+ COUNT
  , ...)
|
  kvmalloc(
- sizeof(__u8) * COUNT
+ COUNT
  , ...)
|
  kvmalloc(
- sizeof(char) * COUNT
+ COUNT
  , ...)
|
  kvmalloc(
- 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;
@@

(
- kvmalloc
+ kvmalloc_array
  (
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
  , ...)
|
- kvmalloc
+ kvmalloc_array
  (
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
  , ...)
|
- kvmalloc
+ kvmalloc_array
  (
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
  , ...)
|
- kvmalloc
+ kvmalloc_array
  (
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
  , ...)
|
- kvmalloc
+ kvmalloc_array
  (
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
  , ...)
|
- kvmalloc
+ kvmalloc_array
  (
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
  , ...)
|
- kvmalloc
+ kvmalloc_array
  (
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
  , ...)
|
- kvmalloc
+ kvmalloc_array
  (
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
  , ...)
)

// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@

- kvmalloc
+ kvmalloc_array
  (
- 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;
@@

(
  kvmalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  kvmalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  kvmalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  kvmalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  kvmalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
  , ...)
|
  kvmalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
  , ...)
|
  kvmalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
  , ...)
|
  kvmalloc(
- 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;
@@

(
  kvmalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
  , ...)
|
  kvmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
  , ...)
|
  kvmalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
  , ...)
|
  kvmalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
  , ...)
|
  kvmalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
  , ...)
|
  kvmalloc(
- 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;
@@

(
  kvmalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kvmalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kvmalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kvmalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kvmalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kvmalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kvmalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kvmalloc(
- 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;
@@

(
  kvmalloc(C1 * C2 * C3, ...)
|
  kvmalloc(
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
  , ...)
|
  kvmalloc(
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
  , ...)
|
  kvmalloc(
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
  , ...)
|
  kvmalloc(
- 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;
@@

(
  kvmalloc(sizeof(THING) * C2, ...)
|
  kvmalloc(sizeof(TYPE) * C2, ...)
|
  kvmalloc(C1 * C2 * C3, ...)
|
  kvmalloc(C1 * C2, ...)
|
- kvmalloc
+ kvmalloc_array
  (
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
  , ...)
|
- kvmalloc
+ kvmalloc_array
  (
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
  , ...)
|
- kvmalloc
+ kvmalloc_array
  (
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
  , ...)
|
- kvmalloc
+ kvmalloc_array
  (
- sizeof(THING) * E2
+ E2, sizeof(THING)
  , ...)
|
- kvmalloc
+ kvmalloc_array
  (
- (E1) * E2
+ E1, E2
  , ...)
|
- kvmalloc
+ kvmalloc_array
  (
- (E1) * (E2)
+ E1, E2
  , ...)
|
- kvmalloc
+ kvmalloc_array
  (
- E1 * E2
+ E1, E2
  , ...)
)

Signed-off-by: Kees Cook <keescook@chromium.org>
block/blk-zoned.c
drivers/acpi/apei/erst.c
drivers/gpu/drm/nouveau/nvkm/subdev/mmu/mem.c
drivers/md/dm-integrity.c
drivers/xen/evtchn.c
fs/ext4/super.c
ipc/sem.c
net/ipv6/ila/ila_xlat.c

index 3d08dc84db1674b056d9a5a6078e276f2af37b70..51000914e23f966f7a9e324cfcf301c27f0a105c 100644 (file)
@@ -331,8 +331,8 @@ int blkdev_report_zones_ioctl(struct block_device *bdev, fmode_t mode,
        if (rep.nr_zones > INT_MAX / sizeof(struct blk_zone))
                return -ERANGE;
 
-       zones = kvmalloc(rep.nr_zones * sizeof(struct blk_zone),
-                       GFP_KERNEL | __GFP_ZERO);
+       zones = kvmalloc_array(rep.nr_zones, sizeof(struct blk_zone),
+                              GFP_KERNEL | __GFP_ZERO);
        if (!zones)
                return -ENOMEM;
 
index 9bff853e85f37831d8d053a2aa363f139537c9b5..3c5ea7cb693ef558829ab15b930d234cd51b9d67 100644 (file)
@@ -524,7 +524,8 @@ retry:
                                pr_warn(FW_WARN "too many record IDs!\n");
                        return 0;
                }
-               new_entries = kvmalloc(new_size * sizeof(entries[0]), GFP_KERNEL);
+               new_entries = kvmalloc_array(new_size, sizeof(entries[0]),
+                                            GFP_KERNEL);
                if (!new_entries)
                        return -ENOMEM;
                memcpy(new_entries, entries,
index 39808489f21d02fae71e699c688c12ebde077bcb..92e363dbbc5a6efeba3c28a03f8c3f1e47fa20d6 100644 (file)
@@ -191,9 +191,9 @@ nvkm_mem_new_host(struct nvkm_mmu *mmu, int type, u8 page, u64 size,
        nvkm_memory_ctor(&nvkm_mem_dma, &mem->memory);
        size = ALIGN(size, PAGE_SIZE) >> PAGE_SHIFT;
 
-       if (!(mem->mem = kvmalloc(sizeof(*mem->mem) * size, GFP_KERNEL)))
+       if (!(mem->mem = kvmalloc_array(size, sizeof(*mem->mem), GFP_KERNEL)))
                return -ENOMEM;
-       if (!(mem->dma = kvmalloc(sizeof(*mem->dma) * size, GFP_KERNEL)))
+       if (!(mem->dma = kvmalloc_array(size, sizeof(*mem->dma), GFP_KERNEL)))
                return -ENOMEM;
 
        if (mmu->dma_bits > 32)
index 136e7e66d870f86d79f3b18bb69955339abe3947..86438b2f10dd0a92b0e4fbd46eba6e04fff478e6 100644 (file)
@@ -2448,7 +2448,9 @@ static struct scatterlist **dm_integrity_alloc_journal_scatterlist(struct dm_int
        struct scatterlist **sl;
        unsigned i;
 
-       sl = kvmalloc(ic->journal_sections * sizeof(struct scatterlist *), GFP_KERNEL | __GFP_ZERO);
+       sl = kvmalloc_array(ic->journal_sections,
+                           sizeof(struct scatterlist *),
+                           GFP_KERNEL | __GFP_ZERO);
        if (!sl)
                return NULL;
 
@@ -2464,7 +2466,8 @@ static struct scatterlist **dm_integrity_alloc_journal_scatterlist(struct dm_int
 
                n_pages = (end_index - start_index + 1);
 
-               s = kvmalloc(n_pages * sizeof(struct scatterlist), GFP_KERNEL);
+               s = kvmalloc_array(n_pages, sizeof(struct scatterlist),
+                                  GFP_KERNEL);
                if (!s) {
                        dm_integrity_free_journal_scatterlist(ic, sl);
                        return NULL;
@@ -2643,7 +2646,9 @@ static int create_journal(struct dm_integrity_c *ic, char **error)
                                goto bad;
                        }
 
-                       sg = kvmalloc((ic->journal_pages + 1) * sizeof(struct scatterlist), GFP_KERNEL);
+                       sg = kvmalloc_array(ic->journal_pages + 1,
+                                           sizeof(struct scatterlist),
+                                           GFP_KERNEL);
                        if (!sg) {
                                *error = "Unable to allocate sg list";
                                r = -ENOMEM;
@@ -2709,7 +2714,9 @@ static int create_journal(struct dm_integrity_c *ic, char **error)
                                r = -ENOMEM;
                                goto bad;
                        }
-                       ic->sk_requests = kvmalloc(ic->journal_sections * sizeof(struct skcipher_request *), GFP_KERNEL | __GFP_ZERO);
+                       ic->sk_requests = kvmalloc_array(ic->journal_sections,
+                                                        sizeof(struct skcipher_request *),
+                                                        GFP_KERNEL | __GFP_ZERO);
                        if (!ic->sk_requests) {
                                *error = "Unable to allocate sk requests";
                                r = -ENOMEM;
index 8cac07ab60abd4bc2c200be8bfd63fc5aa70b477..6d1a5e58968ffdfb42a71e5f984a52475ea0ca9c 100644 (file)
@@ -322,7 +322,7 @@ static int evtchn_resize_ring(struct per_user_data *u)
        else
                new_size = 2 * u->ring_size;
 
-       new_ring = kvmalloc(new_size * sizeof(*new_ring), GFP_KERNEL);
+       new_ring = kvmalloc_array(new_size, sizeof(*new_ring), GFP_KERNEL);
        if (!new_ring)
                return -ENOMEM;
 
index 00fe75a71c4b6a2bd6b6afb7c49e8dd02e67c963..0c4c2201b3aa2ee9680478f8fd11685e66634f50 100644 (file)
@@ -3993,9 +3993,9 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
                        goto failed_mount;
                }
        }
-       sbi->s_group_desc = kvmalloc(db_count *
-                                         sizeof(struct buffer_head *),
-                                         GFP_KERNEL);
+       sbi->s_group_desc = kvmalloc_array(db_count,
+                                          sizeof(struct buffer_head *),
+                                          GFP_KERNEL);
        if (sbi->s_group_desc == NULL) {
                ext4_msg(sb, KERN_ERR, "not enough memory");
                ret = -ENOMEM;
index cfd94d48a9aa7ad719ed17e2e9f1a2d53ca028ee..59a3cd1d3252426c24cc83f08b0960af75386fe5 100644 (file)
--- a/ipc/sem.c
+++ b/ipc/sem.c
@@ -1945,7 +1945,7 @@ static long do_semtimedop(int semid, struct sembuf __user *tsops,
        if (nsops > ns->sc_semopm)
                return -E2BIG;
        if (nsops > SEMOPM_FAST) {
-               sops = kvmalloc(sizeof(*sops)*nsops, GFP_KERNEL);
+               sops = kvmalloc_array(nsops, sizeof(*sops), GFP_KERNEL);
                if (sops == NULL)
                        return -ENOMEM;
        }
index 44c39c5f06384c6c83901036a2e94bcda439f91c..10ae13560b407e28643fe2ed772de868ef41804f 100644 (file)
@@ -42,7 +42,8 @@ static int alloc_ila_locks(struct ila_net *ilan)
        size = roundup_pow_of_two(nr_pcpus * LOCKS_PER_CPU);
 
        if (sizeof(spinlock_t) != 0) {
-               ilan->locks = kvmalloc(size * sizeof(spinlock_t), GFP_KERNEL);
+               ilan->locks = kvmalloc_array(size, sizeof(spinlock_t),
+                                            GFP_KERNEL);
                if (!ilan->locks)
                        return -ENOMEM;
                for (i = 0; i < size; i++)