Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

+24 -15
+2 -2
block/blk-zoned.c
··· 331 331 if (rep.nr_zones > INT_MAX / sizeof(struct blk_zone)) 332 332 return -ERANGE; 333 333 334 - zones = kvmalloc(rep.nr_zones * sizeof(struct blk_zone), 335 - GFP_KERNEL | __GFP_ZERO); 334 + zones = kvmalloc_array(rep.nr_zones, sizeof(struct blk_zone), 335 + GFP_KERNEL | __GFP_ZERO); 336 336 if (!zones) 337 337 return -ENOMEM; 338 338
+2 -1
drivers/acpi/apei/erst.c
··· 524 524 pr_warn(FW_WARN "too many record IDs!\n"); 525 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 529 if (!new_entries) 529 530 return -ENOMEM; 530 531 memcpy(new_entries, entries,
+2 -2
drivers/gpu/drm/nouveau/nvkm/subdev/mmu/mem.c
··· 191 191 nvkm_memory_ctor(&nvkm_mem_dma, &mem->memory); 192 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 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 197 return -ENOMEM; 198 198 199 199 if (mmu->dma_bits > 32)
+11 -4
drivers/md/dm-integrity.c
··· 2448 2448 struct scatterlist **sl; 2449 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 2454 if (!sl) 2453 2455 return NULL; 2454 2456 ··· 2466 2464 2467 2465 n_pages = (end_index - start_index + 1); 2468 2466 2469 - s = kvmalloc(n_pages * sizeof(struct scatterlist), GFP_KERNEL); 2467 + s = kvmalloc_array(n_pages, sizeof(struct scatterlist), 2468 + GFP_KERNEL); 2470 2469 if (!s) { 2471 2470 dm_integrity_free_journal_scatterlist(ic, sl); 2472 2471 return NULL; ··· 2646 2643 goto bad; 2647 2644 } 2648 2645 2649 - sg = kvmalloc((ic->journal_pages + 1) * sizeof(struct scatterlist), GFP_KERNEL); 2646 + sg = kvmalloc_array(ic->journal_pages + 1, 2647 + sizeof(struct scatterlist), 2648 + GFP_KERNEL); 2650 2649 if (!sg) { 2651 2650 *error = "Unable to allocate sg list"; 2652 2651 r = -ENOMEM; ··· 2714 2709 r = -ENOMEM; 2715 2710 goto bad; 2716 2711 } 2717 - ic->sk_requests = kvmalloc(ic->journal_sections * sizeof(struct skcipher_request *), GFP_KERNEL | __GFP_ZERO); 2712 + ic->sk_requests = kvmalloc_array(ic->journal_sections, 2713 + sizeof(struct skcipher_request *), 2714 + GFP_KERNEL | __GFP_ZERO); 2718 2715 if (!ic->sk_requests) { 2719 2716 *error = "Unable to allocate sk requests"; 2720 2717 r = -ENOMEM;
+1 -1
drivers/xen/evtchn.c
··· 322 322 else 323 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 326 if (!new_ring) 327 327 return -ENOMEM; 328 328
+3 -3
fs/ext4/super.c
··· 3993 3993 goto failed_mount; 3994 3994 } 3995 3995 } 3996 - sbi->s_group_desc = kvmalloc(db_count * 3997 - sizeof(struct buffer_head *), 3998 - GFP_KERNEL); 3996 + sbi->s_group_desc = kvmalloc_array(db_count, 3997 + sizeof(struct buffer_head *), 3998 + GFP_KERNEL); 3999 3999 if (sbi->s_group_desc == NULL) { 4000 4000 ext4_msg(sb, KERN_ERR, "not enough memory"); 4001 4001 ret = -ENOMEM;
+1 -1
ipc/sem.c
··· 1945 1945 if (nsops > ns->sc_semopm) 1946 1946 return -E2BIG; 1947 1947 if (nsops > SEMOPM_FAST) { 1948 - sops = kvmalloc(sizeof(*sops)*nsops, GFP_KERNEL); 1948 + sops = kvmalloc_array(nsops, sizeof(*sops), GFP_KERNEL); 1949 1949 if (sops == NULL) 1950 1950 return -ENOMEM; 1951 1951 }
+2 -1
net/ipv6/ila/ila_xlat.c
··· 42 42 size = roundup_pow_of_two(nr_pcpus * LOCKS_PER_CPU); 43 43 44 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 47 if (!ilan->locks) 47 48 return -ENOMEM; 48 49 for (i = 0; i < size; i++)