aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKees Cook <keescook@chromium.org>2018-06-12 17:04:32 -0400
committerKees Cook <keescook@chromium.org>2018-06-12 19:19:22 -0400
commit344476e16acbe20249675b75933be1ad52eff4df (patch)
treed080e81f89e1c198ac3655dad7bada20e984010a
parent590b5b7d8671e011d1a8e1ab20c60addb249d015 (diff)
treewide: kvmalloc() -> kvmalloc_array()
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>
-rw-r--r--block/blk-zoned.c4
-rw-r--r--drivers/acpi/apei/erst.c3
-rw-r--r--drivers/gpu/drm/nouveau/nvkm/subdev/mmu/mem.c4
-rw-r--r--drivers/md/dm-integrity.c15
-rw-r--r--drivers/xen/evtchn.c2
-rw-r--r--fs/ext4/super.c6
-rw-r--r--ipc/sem.c2
-rw-r--r--net/ipv6/ila/ila_xlat.c3
8 files changed, 24 insertions, 15 deletions
diff --git a/block/blk-zoned.c b/block/blk-zoned.c
index 3d08dc84db16..51000914e23f 100644
--- a/block/blk-zoned.c
+++ b/block/blk-zoned.c
@@ -331,8 +331,8 @@ int blkdev_report_zones_ioctl(struct block_device *bdev, fmode_t mode,
331 if (rep.nr_zones > INT_MAX / sizeof(struct blk_zone)) 331 if (rep.nr_zones > INT_MAX / sizeof(struct blk_zone))
332 return -ERANGE; 332 return -ERANGE;
333 333
334 zones = kvmalloc(rep.nr_zones * sizeof(struct blk_zone), 334 zones = kvmalloc_array(rep.nr_zones, sizeof(struct blk_zone),
335 GFP_KERNEL | __GFP_ZERO); 335 GFP_KERNEL | __GFP_ZERO);
336 if (!zones) 336 if (!zones)
337 return -ENOMEM; 337 return -ENOMEM;
338 338
diff --git a/drivers/acpi/apei/erst.c b/drivers/acpi/apei/erst.c
index 9bff853e85f3..3c5ea7cb693e 100644
--- a/drivers/acpi/apei/erst.c
+++ b/drivers/acpi/apei/erst.c
@@ -524,7 +524,8 @@ retry:
524 pr_warn(FW_WARN "too many record IDs!\n"); 524 pr_warn(FW_WARN "too many record IDs!\n");
525 return 0; 525 return 0;
526 } 526 }
527 new_entries = kvmalloc(new_size * sizeof(entries[0]), GFP_KERNEL); 527 new_entries = kvmalloc_array(new_size, sizeof(entries[0]),
528 GFP_KERNEL);
528 if (!new_entries) 529 if (!new_entries)
529 return -ENOMEM; 530 return -ENOMEM;
530 memcpy(new_entries, entries, 531 memcpy(new_entries, entries,
diff --git a/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/mem.c b/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/mem.c
index 39808489f21d..92e363dbbc5a 100644
--- a/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/mem.c
+++ b/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/mem.c
@@ -191,9 +191,9 @@ nvkm_mem_new_host(struct nvkm_mmu *mmu, int type, u8 page, u64 size,
191 nvkm_memory_ctor(&nvkm_mem_dma, &mem->memory); 191 nvkm_memory_ctor(&nvkm_mem_dma, &mem->memory);
192 size = ALIGN(size, PAGE_SIZE) >> PAGE_SHIFT; 192 size = ALIGN(size, PAGE_SIZE) >> PAGE_SHIFT;
193 193
194 if (!(mem->mem = kvmalloc(sizeof(*mem->mem) * size, GFP_KERNEL))) 194 if (!(mem->mem = kvmalloc_array(size, sizeof(*mem->mem), GFP_KERNEL)))
195 return -ENOMEM; 195 return -ENOMEM;
196 if (!(mem->dma = kvmalloc(sizeof(*mem->dma) * size, GFP_KERNEL))) 196 if (!(mem->dma = kvmalloc_array(size, sizeof(*mem->dma), GFP_KERNEL)))
197 return -ENOMEM; 197 return -ENOMEM;
198 198
199 if (mmu->dma_bits > 32) 199 if (mmu->dma_bits > 32)
diff --git a/drivers/md/dm-integrity.c b/drivers/md/dm-integrity.c
index 136e7e66d870..86438b2f10dd 100644
--- a/drivers/md/dm-integrity.c
+++ b/drivers/md/dm-integrity.c
@@ -2448,7 +2448,9 @@ static struct scatterlist **dm_integrity_alloc_journal_scatterlist(struct dm_int
2448 struct scatterlist **sl; 2448 struct scatterlist **sl;
2449 unsigned i; 2449 unsigned i;
2450 2450
2451 sl = kvmalloc(ic->journal_sections * sizeof(struct scatterlist *), GFP_KERNEL | __GFP_ZERO); 2451 sl = kvmalloc_array(ic->journal_sections,
2452 sizeof(struct scatterlist *),
2453 GFP_KERNEL | __GFP_ZERO);
2452 if (!sl) 2454 if (!sl)
2453 return NULL; 2455 return NULL;
2454 2456
@@ -2464,7 +2466,8 @@ static struct scatterlist **dm_integrity_alloc_journal_scatterlist(struct dm_int
2464 2466
2465 n_pages = (end_index - start_index + 1); 2467 n_pages = (end_index - start_index + 1);
2466 2468
2467 s = kvmalloc(n_pages * sizeof(struct scatterlist), GFP_KERNEL); 2469 s = kvmalloc_array(n_pages, sizeof(struct scatterlist),
2470 GFP_KERNEL);
2468 if (!s) { 2471 if (!s) {
2469 dm_integrity_free_journal_scatterlist(ic, sl); 2472 dm_integrity_free_journal_scatterlist(ic, sl);
2470 return NULL; 2473 return NULL;
@@ -2643,7 +2646,9 @@ static int create_journal(struct dm_integrity_c *ic, char **error)
2643 goto bad; 2646 goto bad;
2644 } 2647 }
2645 2648
2646 sg = kvmalloc((ic->journal_pages + 1) * sizeof(struct scatterlist), GFP_KERNEL); 2649 sg = kvmalloc_array(ic->journal_pages + 1,
2650 sizeof(struct scatterlist),
2651 GFP_KERNEL);
2647 if (!sg) { 2652 if (!sg) {
2648 *error = "Unable to allocate sg list"; 2653 *error = "Unable to allocate sg list";
2649 r = -ENOMEM; 2654 r = -ENOMEM;
@@ -2709,7 +2714,9 @@ static int create_journal(struct dm_integrity_c *ic, char **error)
2709 r = -ENOMEM; 2714 r = -ENOMEM;
2710 goto bad; 2715 goto bad;
2711 } 2716 }
2712 ic->sk_requests = kvmalloc(ic->journal_sections * sizeof(struct skcipher_request *), GFP_KERNEL | __GFP_ZERO); 2717 ic->sk_requests = kvmalloc_array(ic->journal_sections,
2718 sizeof(struct skcipher_request *),
2719 GFP_KERNEL | __GFP_ZERO);
2713 if (!ic->sk_requests) { 2720 if (!ic->sk_requests) {
2714 *error = "Unable to allocate sk requests"; 2721 *error = "Unable to allocate sk requests";
2715 r = -ENOMEM; 2722 r = -ENOMEM;
diff --git a/drivers/xen/evtchn.c b/drivers/xen/evtchn.c
index 8cac07ab60ab..6d1a5e58968f 100644
--- a/drivers/xen/evtchn.c
+++ b/drivers/xen/evtchn.c
@@ -322,7 +322,7 @@ static int evtchn_resize_ring(struct per_user_data *u)
322 else 322 else
323 new_size = 2 * u->ring_size; 323 new_size = 2 * u->ring_size;
324 324
325 new_ring = kvmalloc(new_size * sizeof(*new_ring), GFP_KERNEL); 325 new_ring = kvmalloc_array(new_size, sizeof(*new_ring), GFP_KERNEL);
326 if (!new_ring) 326 if (!new_ring)
327 return -ENOMEM; 327 return -ENOMEM;
328 328
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 00fe75a71c4b..0c4c2201b3aa 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -3993,9 +3993,9 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
3993 goto failed_mount; 3993 goto failed_mount;
3994 } 3994 }
3995 } 3995 }
3996 sbi->s_group_desc = kvmalloc(db_count * 3996 sbi->s_group_desc = kvmalloc_array(db_count,
3997 sizeof(struct buffer_head *), 3997 sizeof(struct buffer_head *),
3998 GFP_KERNEL); 3998 GFP_KERNEL);
3999 if (sbi->s_group_desc == NULL) { 3999 if (sbi->s_group_desc == NULL) {
4000 ext4_msg(sb, KERN_ERR, "not enough memory"); 4000 ext4_msg(sb, KERN_ERR, "not enough memory");
4001 ret = -ENOMEM; 4001 ret = -ENOMEM;
diff --git a/ipc/sem.c b/ipc/sem.c
index cfd94d48a9aa..59a3cd1d3252 100644
--- a/ipc/sem.c
+++ b/ipc/sem.c
@@ -1945,7 +1945,7 @@ static long do_semtimedop(int semid, struct sembuf __user *tsops,
1945 if (nsops > ns->sc_semopm) 1945 if (nsops > ns->sc_semopm)
1946 return -E2BIG; 1946 return -E2BIG;
1947 if (nsops > SEMOPM_FAST) { 1947 if (nsops > SEMOPM_FAST) {
1948 sops = kvmalloc(sizeof(*sops)*nsops, GFP_KERNEL); 1948 sops = kvmalloc_array(nsops, sizeof(*sops), GFP_KERNEL);
1949 if (sops == NULL) 1949 if (sops == NULL)
1950 return -ENOMEM; 1950 return -ENOMEM;
1951 } 1951 }
diff --git a/net/ipv6/ila/ila_xlat.c b/net/ipv6/ila/ila_xlat.c
index 44c39c5f0638..10ae13560b40 100644
--- a/net/ipv6/ila/ila_xlat.c
+++ b/net/ipv6/ila/ila_xlat.c
@@ -42,7 +42,8 @@ static int alloc_ila_locks(struct ila_net *ilan)
42 size = roundup_pow_of_two(nr_pcpus * LOCKS_PER_CPU); 42 size = roundup_pow_of_two(nr_pcpus * LOCKS_PER_CPU);
43 43
44 if (sizeof(spinlock_t) != 0) { 44 if (sizeof(spinlock_t) != 0) {
45 ilan->locks = kvmalloc(size * sizeof(spinlock_t), GFP_KERNEL); 45 ilan->locks = kvmalloc_array(size, sizeof(spinlock_t),
46 GFP_KERNEL);
46 if (!ilan->locks) 47 if (!ilan->locks)
47 return -ENOMEM; 48 return -ENOMEM;
48 for (i = 0; i < size; i++) 49 for (i = 0; i < size; i++)