Skip to content

Commit 6da2ec5

Browse files
committed
treewide: kmalloc() -> kmalloc_array()
The kmalloc() function has a 2-factor argument form, kmalloc_array(). This patch replaces cases of: kmalloc(a * b, gfp) with: kmalloc_array(a * b, gfp) as well as handling cases of: kmalloc(a * b * c, gfp) with: kmalloc(array3_size(a, b, c), gfp) as it's slightly less ugly than: kmalloc_array(array_size(a, b), c, gfp) This does, however, attempt to ignore constant size factors like: kmalloc(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 tools/ directory was manually excluded, since it has its own implementation of kmalloc(). The Coccinelle script used for this was: // Fix redundant parens around sizeof(). @@ type TYPE; expression THING, E; @@ ( kmalloc( - (sizeof(TYPE)) * E + sizeof(TYPE) * E , ...) | kmalloc( - (sizeof(THING)) * E + sizeof(THING) * E , ...) ) // Drop single-byte sizes and redundant parens. @@ expression COUNT; typedef u8; typedef __u8; @@ ( kmalloc( - sizeof(u8) * (COUNT) + COUNT , ...) | kmalloc( - sizeof(__u8) * (COUNT) + COUNT , ...) | kmalloc( - sizeof(char) * (COUNT) + COUNT , ...) | kmalloc( - sizeof(unsigned char) * (COUNT) + COUNT , ...) | kmalloc( - sizeof(u8) * COUNT + COUNT , ...) | kmalloc( - sizeof(__u8) * COUNT + COUNT , ...) | kmalloc( - sizeof(char) * COUNT + COUNT , ...) | kmalloc( - 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; @@ ( - kmalloc + kmalloc_array ( - sizeof(TYPE) * (COUNT_ID) + COUNT_ID, sizeof(TYPE) , ...) | - kmalloc + kmalloc_array ( - sizeof(TYPE) * COUNT_ID + COUNT_ID, sizeof(TYPE) , ...) | - kmalloc + kmalloc_array ( - sizeof(TYPE) * (COUNT_CONST) + COUNT_CONST, sizeof(TYPE) , ...) | - kmalloc + kmalloc_array ( - sizeof(TYPE) * COUNT_CONST + COUNT_CONST, sizeof(TYPE) , ...) | - kmalloc + kmalloc_array ( - sizeof(THING) * (COUNT_ID) + COUNT_ID, sizeof(THING) , ...) | - kmalloc + kmalloc_array ( - sizeof(THING) * COUNT_ID + COUNT_ID, sizeof(THING) , ...) | - kmalloc + kmalloc_array ( - sizeof(THING) * (COUNT_CONST) + COUNT_CONST, sizeof(THING) , ...) | - kmalloc + kmalloc_array ( - sizeof(THING) * COUNT_CONST + COUNT_CONST, sizeof(THING) , ...) ) // 2-factor product, only identifiers. @@ identifier SIZE, COUNT; @@ - kmalloc + kmalloc_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; @@ ( kmalloc( - sizeof(TYPE) * (COUNT) * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | kmalloc( - sizeof(TYPE) * (COUNT) * STRIDE + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | kmalloc( - sizeof(TYPE) * COUNT * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | kmalloc( - sizeof(TYPE) * COUNT * STRIDE + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | kmalloc( - sizeof(THING) * (COUNT) * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | kmalloc( - sizeof(THING) * (COUNT) * STRIDE + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | kmalloc( - sizeof(THING) * COUNT * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | kmalloc( - 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; @@ ( kmalloc( - sizeof(TYPE1) * sizeof(TYPE2) * COUNT + array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2)) , ...) | kmalloc( - sizeof(TYPE1) * sizeof(THING2) * (COUNT) + array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2)) , ...) | kmalloc( - sizeof(THING1) * sizeof(THING2) * COUNT + array3_size(COUNT, sizeof(THING1), sizeof(THING2)) , ...) | kmalloc( - sizeof(THING1) * sizeof(THING2) * (COUNT) + array3_size(COUNT, sizeof(THING1), sizeof(THING2)) , ...) | kmalloc( - sizeof(TYPE1) * sizeof(THING2) * COUNT + array3_size(COUNT, sizeof(TYPE1), sizeof(THING2)) , ...) | kmalloc( - 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; @@ ( kmalloc( - (COUNT) * STRIDE * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | kmalloc( - COUNT * (STRIDE) * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | kmalloc( - COUNT * STRIDE * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | kmalloc( - (COUNT) * (STRIDE) * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | kmalloc( - COUNT * (STRIDE) * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | kmalloc( - (COUNT) * STRIDE * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | kmalloc( - (COUNT) * (STRIDE) * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | kmalloc( - 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; @@ ( kmalloc(C1 * C2 * C3, ...) | kmalloc( - (E1) * E2 * E3 + array3_size(E1, E2, E3) , ...) | kmalloc( - (E1) * (E2) * E3 + array3_size(E1, E2, E3) , ...) | kmalloc( - (E1) * (E2) * (E3) + array3_size(E1, E2, E3) , ...) | kmalloc( - 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; @@ ( kmalloc(sizeof(THING) * C2, ...) | kmalloc(sizeof(TYPE) * C2, ...) | kmalloc(C1 * C2 * C3, ...) | kmalloc(C1 * C2, ...) | - kmalloc + kmalloc_array ( - sizeof(TYPE) * (E2) + E2, sizeof(TYPE) , ...) | - kmalloc + kmalloc_array ( - sizeof(TYPE) * E2 + E2, sizeof(TYPE) , ...) | - kmalloc + kmalloc_array ( - sizeof(THING) * (E2) + E2, sizeof(THING) , ...) | - kmalloc + kmalloc_array ( - sizeof(THING) * E2 + E2, sizeof(THING) , ...) | - kmalloc + kmalloc_array ( - (E1) * E2 + E1, E2 , ...) | - kmalloc + kmalloc_array ( - (E1) * (E2) + E1, E2 , ...) | - kmalloc + kmalloc_array ( - E1 * E2 + E1, E2 , ...) ) Signed-off-by: Kees Cook <[email protected]>
1 parent 1c542f3 commit 6da2ec5

File tree

377 files changed

+1014
-748
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

377 files changed

+1014
-748
lines changed

arch/arm/kernel/sys_oabi-compat.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ asmlinkage long sys_oabi_epoll_wait(int epfd,
286286
return -EINVAL;
287287
if (!access_ok(VERIFY_WRITE, events, sizeof(*events) * maxevents))
288288
return -EFAULT;
289-
kbuf = kmalloc(sizeof(*kbuf) * maxevents, GFP_KERNEL);
289+
kbuf = kmalloc_array(maxevents, sizeof(*kbuf), GFP_KERNEL);
290290
if (!kbuf)
291291
return -ENOMEM;
292292
fs = get_fs();
@@ -324,7 +324,7 @@ asmlinkage long sys_oabi_semtimedop(int semid,
324324
return -EINVAL;
325325
if (!access_ok(VERIFY_READ, tsops, sizeof(*tsops) * nsops))
326326
return -EFAULT;
327-
sops = kmalloc(sizeof(*sops) * nsops, GFP_KERNEL);
327+
sops = kmalloc_array(nsops, sizeof(*sops), GFP_KERNEL);
328328
if (!sops)
329329
return -ENOMEM;
330330
err = 0;

arch/arm/mm/pgd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "mm.h"
2121

2222
#ifdef CONFIG_ARM_LPAE
23-
#define __pgd_alloc() kmalloc(PTRS_PER_PGD * sizeof(pgd_t), GFP_KERNEL)
23+
#define __pgd_alloc() kmalloc_array(PTRS_PER_PGD, sizeof(pgd_t), GFP_KERNEL)
2424
#define __pgd_free(pgd) kfree(pgd)
2525
#else
2626
#define __pgd_alloc() (pgd_t *)__get_free_pages(GFP_KERNEL, 2)

arch/arm/probes/kprobes/test-core.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -766,8 +766,9 @@ static int coverage_start_fn(const struct decode_header *h, void *args)
766766

767767
static int coverage_start(const union decode_item *table)
768768
{
769-
coverage.base = kmalloc(MAX_COVERAGE_ENTRIES *
770-
sizeof(struct coverage_entry), GFP_KERNEL);
769+
coverage.base = kmalloc_array(MAX_COVERAGE_ENTRIES,
770+
sizeof(struct coverage_entry),
771+
GFP_KERNEL);
771772
coverage.num_entries = 0;
772773
coverage.nesting = 0;
773774
return table_iter(table, coverage_start_fn, &coverage);

arch/ia64/kernel/mca_drv.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,8 @@ init_record_index_pools(void)
350350
/* - 3 - */
351351
slidx_pool.max_idx = (rec_max_size/sect_min_size) * 2 + 1;
352352
slidx_pool.buffer =
353-
kmalloc(slidx_pool.max_idx * sizeof(slidx_list_t), GFP_KERNEL);
353+
kmalloc_array(slidx_pool.max_idx, sizeof(slidx_list_t),
354+
GFP_KERNEL);
354355

355356
return slidx_pool.buffer ? 0 : -ENOMEM;
356357
}

arch/ia64/mm/tlb.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,9 @@ int ia64_itr_entry(u64 target_mask, u64 va, u64 pte, u64 log_size)
430430
int cpu = smp_processor_id();
431431

432432
if (!ia64_idtrs[cpu]) {
433-
ia64_idtrs[cpu] = kmalloc(2 * IA64_TR_ALLOC_MAX *
434-
sizeof (struct ia64_tr_entry), GFP_KERNEL);
433+
ia64_idtrs[cpu] = kmalloc_array(2 * IA64_TR_ALLOC_MAX,
434+
sizeof(struct ia64_tr_entry),
435+
GFP_KERNEL);
435436
if (!ia64_idtrs[cpu])
436437
return -ENOMEM;
437438
}

arch/ia64/sn/kernel/irq.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,8 @@ void __init sn_irq_lh_init(void)
474474
{
475475
int i;
476476

477-
sn_irq_lh = kmalloc(sizeof(struct list_head *) * NR_IRQS, GFP_KERNEL);
477+
sn_irq_lh = kmalloc_array(NR_IRQS, sizeof(struct list_head *),
478+
GFP_KERNEL);
478479
if (!sn_irq_lh)
479480
panic("SN PCI INIT: Failed to allocate memory for PCI init\n");
480481

arch/mips/alchemy/common/dbdma.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,8 @@ u32 au1xxx_dbdma_ring_alloc(u32 chanid, int entries)
411411
* and if we try that first we are likely to not waste larger
412412
* slabs of memory.
413413
*/
414-
desc_base = (u32)kmalloc(entries * sizeof(au1x_ddma_desc_t),
415-
GFP_KERNEL|GFP_DMA);
414+
desc_base = (u32)kmalloc_array(entries, sizeof(au1x_ddma_desc_t),
415+
GFP_KERNEL|GFP_DMA);
416416
if (desc_base == 0)
417417
return 0;
418418

arch/powerpc/lib/rheap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static int grow(rh_info_t * info, int max_blocks)
5454

5555
new_blocks = max_blocks - info->max_blocks;
5656

57-
block = kmalloc(sizeof(rh_block_t) * max_blocks, GFP_ATOMIC);
57+
block = kmalloc_array(max_blocks, sizeof(rh_block_t), GFP_ATOMIC);
5858
if (block == NULL)
5959
return -ENOMEM;
6060

arch/powerpc/platforms/4xx/hsta_msi.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ static int hsta_msi_probe(struct platform_device *pdev)
156156
if (ret)
157157
goto out;
158158

159-
ppc4xx_hsta_msi.irq_map = kmalloc(sizeof(int) * irq_count, GFP_KERNEL);
159+
ppc4xx_hsta_msi.irq_map = kmalloc_array(irq_count, sizeof(int),
160+
GFP_KERNEL);
160161
if (!ppc4xx_hsta_msi.irq_map) {
161162
ret = -ENOMEM;
162163
goto out1;

arch/powerpc/platforms/4xx/msi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static int ppc4xx_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
8989
if (type == PCI_CAP_ID_MSIX)
9090
pr_debug("ppc4xx msi: MSI-X untested, trying anyway.\n");
9191

92-
msi_data->msi_virqs = kmalloc((msi_irqs) * sizeof(int), GFP_KERNEL);
92+
msi_data->msi_virqs = kmalloc_array(msi_irqs, sizeof(int), GFP_KERNEL);
9393
if (!msi_data->msi_virqs)
9494
return -ENOMEM;
9595

0 commit comments

Comments
 (0)