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

arch/powerpc/sysdev/mpic.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,8 +1639,9 @@ void __init mpic_init(struct mpic *mpic)
16391639

16401640
#ifdef CONFIG_PM
16411641
/* allocate memory to save mpic state */
1642-
mpic->save_data = kmalloc(mpic->num_sources * sizeof(*mpic->save_data),
1643-
GFP_KERNEL);
1642+
mpic->save_data = kmalloc_array(mpic->num_sources,
1643+
sizeof(*mpic->save_data),
1644+
GFP_KERNEL);
16441645
BUG_ON(mpic->save_data == NULL);
16451646
#endif
16461647

arch/s390/hypfs/hypfs_diag0c.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ static void *diag0c_store(unsigned int *count)
4949

5050
get_online_cpus();
5151
cpu_count = num_online_cpus();
52-
cpu_vec = kmalloc(sizeof(*cpu_vec) * num_possible_cpus(), GFP_KERNEL);
52+
cpu_vec = kmalloc_array(num_possible_cpus(), sizeof(*cpu_vec),
53+
GFP_KERNEL);
5354
if (!cpu_vec)
5455
goto fail_put_online_cpus;
5556
/* Note: Diag 0c needs 8 byte alignment and real storage */

arch/s390/kernel/debug.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,13 @@ static debug_entry_t ***debug_areas_alloc(int pages_per_area, int nr_areas)
194194
debug_entry_t ***areas;
195195
int i, j;
196196

197-
areas = kmalloc(nr_areas * sizeof(debug_entry_t **), GFP_KERNEL);
197+
areas = kmalloc_array(nr_areas, sizeof(debug_entry_t **), GFP_KERNEL);
198198
if (!areas)
199199
goto fail_malloc_areas;
200200
for (i = 0; i < nr_areas; i++) {
201-
areas[i] = kmalloc(pages_per_area * sizeof(debug_entry_t *), GFP_KERNEL);
201+
areas[i] = kmalloc_array(pages_per_area,
202+
sizeof(debug_entry_t *),
203+
GFP_KERNEL);
202204
if (!areas[i])
203205
goto fail_malloc_areas2;
204206
for (j = 0; j < pages_per_area; j++) {

arch/s390/kernel/perf_cpum_cf_events.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ static __init struct attribute **merge_attr(struct attribute **a,
527527
j++;
528528
j++;
529529

530-
new = kmalloc(sizeof(struct attribute *) * j, GFP_KERNEL);
530+
new = kmalloc_array(j, sizeof(struct attribute *), GFP_KERNEL);
531531
if (!new)
532532
return NULL;
533533
j = 0;

arch/s390/mm/extmem.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static int scode_set;
103103
static int
104104
dcss_set_subcodes(void)
105105
{
106-
char *name = kmalloc(8 * sizeof(char), GFP_KERNEL | GFP_DMA);
106+
char *name = kmalloc(8, GFP_KERNEL | GFP_DMA);
107107
unsigned long rx, ry;
108108
int rc;
109109

arch/sparc/kernel/nmi.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ static int __init check_nmi_watchdog(void)
166166
if (!atomic_read(&nmi_active))
167167
return 0;
168168

169-
prev_nmi_count = kmalloc(nr_cpu_ids * sizeof(unsigned int), GFP_KERNEL);
169+
prev_nmi_count = kmalloc_array(nr_cpu_ids, sizeof(unsigned int),
170+
GFP_KERNEL);
170171
if (!prev_nmi_count) {
171172
err = -ENOMEM;
172173
goto error;

arch/sparc/kernel/sys_sparc_64.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,8 +575,9 @@ SYSCALL_DEFINE5(utrap_install, utrap_entry_t, type,
575575
unsigned long *p = current_thread_info()->utraps;
576576

577577
current_thread_info()->utraps =
578-
kmalloc((UT_TRAP_INSTRUCTION_31+1)*sizeof(long),
579-
GFP_KERNEL);
578+
kmalloc_array(UT_TRAP_INSTRUCTION_31 + 1,
579+
sizeof(long),
580+
GFP_KERNEL);
580581
if (!current_thread_info()->utraps) {
581582
current_thread_info()->utraps = p;
582583
return -ENOMEM;

arch/sparc/net/bpf_jit_comp_32.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ void bpf_jit_compile(struct bpf_prog *fp)
335335
if (!bpf_jit_enable)
336336
return;
337337

338-
addrs = kmalloc(flen * sizeof(*addrs), GFP_KERNEL);
338+
addrs = kmalloc_array(flen, sizeof(*addrs), GFP_KERNEL);
339339
if (addrs == NULL)
340340
return;
341341

arch/um/drivers/ubd_kern.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,19 +1127,19 @@ static int __init ubd_init(void)
11271127
return -1;
11281128
}
11291129

1130-
irq_req_buffer = kmalloc(
1131-
sizeof(struct io_thread_req *) * UBD_REQ_BUFFER_SIZE,
1132-
GFP_KERNEL
1130+
irq_req_buffer = kmalloc_array(UBD_REQ_BUFFER_SIZE,
1131+
sizeof(struct io_thread_req *),
1132+
GFP_KERNEL
11331133
);
11341134
irq_remainder = 0;
11351135

11361136
if (irq_req_buffer == NULL) {
11371137
printk(KERN_ERR "Failed to initialize ubd buffering\n");
11381138
return -1;
11391139
}
1140-
io_req_buffer = kmalloc(
1141-
sizeof(struct io_thread_req *) * UBD_REQ_BUFFER_SIZE,
1142-
GFP_KERNEL
1140+
io_req_buffer = kmalloc_array(UBD_REQ_BUFFER_SIZE,
1141+
sizeof(struct io_thread_req *),
1142+
GFP_KERNEL
11431143
);
11441144

11451145
io_remainder = 0;

arch/um/drivers/vector_kern.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -527,14 +527,14 @@ static struct vector_queue *create_queue(
527527
result->max_iov_frags = num_extra_frags;
528528
for (i = 0; i < max_size; i++) {
529529
if (vp->header_size > 0)
530-
iov = kmalloc(
531-
sizeof(struct iovec) * (3 + num_extra_frags),
532-
GFP_KERNEL
530+
iov = kmalloc_array(3 + num_extra_frags,
531+
sizeof(struct iovec),
532+
GFP_KERNEL
533533
);
534534
else
535-
iov = kmalloc(
536-
sizeof(struct iovec) * (2 + num_extra_frags),
537-
GFP_KERNEL
535+
iov = kmalloc_array(2 + num_extra_frags,
536+
sizeof(struct iovec),
537+
GFP_KERNEL
538538
);
539539
if (iov == NULL)
540540
goto out_fail;

arch/unicore32/kernel/pm.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ static int __init puv3_pm_init(void)
109109
return -EINVAL;
110110
}
111111

112-
sleep_save = kmalloc(puv3_cpu_pm_fns->save_count
113-
* sizeof(unsigned long), GFP_KERNEL);
112+
sleep_save = kmalloc_array(puv3_cpu_pm_fns->save_count,
113+
sizeof(unsigned long),
114+
GFP_KERNEL);
114115
if (!sleep_save) {
115116
printk(KERN_ERR "failed to alloc memory for pm save\n");
116117
return -ENOMEM;

arch/x86/events/core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1637,7 +1637,7 @@ __init struct attribute **merge_attr(struct attribute **a, struct attribute **b)
16371637
j++;
16381638
j++;
16391639

1640-
new = kmalloc(sizeof(struct attribute *) * j, GFP_KERNEL);
1640+
new = kmalloc_array(j, sizeof(struct attribute *), GFP_KERNEL);
16411641
if (!new)
16421642
return NULL;
16431643

arch/x86/kernel/hpet.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -966,8 +966,8 @@ int __init hpet_enable(void)
966966
#endif
967967

968968
cfg = hpet_readl(HPET_CFG);
969-
hpet_boot_cfg = kmalloc((last + 2) * sizeof(*hpet_boot_cfg),
970-
GFP_KERNEL);
969+
hpet_boot_cfg = kmalloc_array(last + 2, sizeof(*hpet_boot_cfg),
970+
GFP_KERNEL);
971971
if (hpet_boot_cfg)
972972
*hpet_boot_cfg = cfg;
973973
else

arch/x86/kernel/ksysfs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ static int __init create_setup_data_nodes(struct kobject *parent)
283283
if (ret)
284284
goto out_setup_data_kobj;
285285

286-
kobjp = kmalloc(sizeof(*kobjp) * nr, GFP_KERNEL);
286+
kobjp = kmalloc_array(nr, sizeof(*kobjp), GFP_KERNEL);
287287
if (!kobjp) {
288288
ret = -ENOMEM;
289289
goto out_setup_data_kobj;

arch/x86/kvm/svm.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,9 @@ static int svm_cpu_init(int cpu)
10011001

10021002
if (svm_sev_enabled()) {
10031003
r = -ENOMEM;
1004-
sd->sev_vmcbs = kmalloc((max_sev_asid + 1) * sizeof(void *), GFP_KERNEL);
1004+
sd->sev_vmcbs = kmalloc_array(max_sev_asid + 1,
1005+
sizeof(void *),
1006+
GFP_KERNEL);
10051007
if (!sd->sev_vmcbs)
10061008
goto err_1;
10071009
}

arch/x86/net/bpf_jit_comp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
11071107
extra_pass = true;
11081108
goto skip_init_addrs;
11091109
}
1110-
addrs = kmalloc(prog->len * sizeof(*addrs), GFP_KERNEL);
1110+
addrs = kmalloc_array(prog->len, sizeof(*addrs), GFP_KERNEL);
11111111
if (!addrs) {
11121112
prog = orig_prog;
11131113
goto out_addrs;

arch/x86/net/bpf_jit_comp32.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2345,7 +2345,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
23452345
prog = tmp;
23462346
}
23472347

2348-
addrs = kmalloc(prog->len * sizeof(*addrs), GFP_KERNEL);
2348+
addrs = kmalloc_array(prog->len, sizeof(*addrs), GFP_KERNEL);
23492349
if (!addrs) {
23502350
prog = orig_prog;
23512351
goto out;

arch/x86/platform/uv/tlb_uv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2142,7 +2142,7 @@ static int __init init_per_cpu(int nuvhubs, int base_part_pnode)
21422142
if (is_uv3_hub() || is_uv2_hub() || is_uv1_hub())
21432143
timeout_us = calculate_destination_timeout();
21442144

2145-
vp = kmalloc(nuvhubs * sizeof(struct uvhub_desc), GFP_KERNEL);
2145+
vp = kmalloc_array(nuvhubs, sizeof(struct uvhub_desc), GFP_KERNEL);
21462146
uvhub_descs = (struct uvhub_desc *)vp;
21472147
memset(uvhub_descs, 0, nuvhubs * sizeof(struct uvhub_desc));
21482148
uvhub_mask = kzalloc((nuvhubs+7)/8, GFP_KERNEL);

block/partitions/ldm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ static bool ldm_validate_tocblocks(struct parsed_partitions *state,
378378
BUG_ON(!state || !ldb);
379379
ph = &ldb->ph;
380380
tb[0] = &ldb->toc;
381-
tb[1] = kmalloc(sizeof(*tb[1]) * 3, GFP_KERNEL);
381+
tb[1] = kmalloc_array(3, sizeof(*tb[1]), GFP_KERNEL);
382382
if (!tb[1]) {
383383
ldm_crit("Out of memory.");
384384
goto err;

crypto/testmgr.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,8 @@ static int __test_aead(struct crypto_aead *tfm, int enc,
603603
goto out_nooutbuf;
604604

605605
/* avoid "the frame size is larger than 1024 bytes" compiler warning */
606-
sg = kmalloc(sizeof(*sg) * 8 * (diff_dst ? 4 : 2), GFP_KERNEL);
606+
sg = kmalloc(array3_size(sizeof(*sg), 8, (diff_dst ? 4 : 2)),
607+
GFP_KERNEL);
607608
if (!sg)
608609
goto out_nosg;
609610
sgout = &sg[16];

drivers/acpi/acpi_video.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -832,8 +832,9 @@ int acpi_video_get_levels(struct acpi_device *device,
832832
* in order to account for buggy BIOS which don't export the first two
833833
* special levels (see below)
834834
*/
835-
br->levels = kmalloc((obj->package.count + ACPI_VIDEO_FIRST_LEVEL) *
836-
sizeof(*br->levels), GFP_KERNEL);
835+
br->levels = kmalloc_array(obj->package.count + ACPI_VIDEO_FIRST_LEVEL,
836+
sizeof(*br->levels),
837+
GFP_KERNEL);
837838
if (!br->levels) {
838839
result = -ENOMEM;
839840
goto out_free;

drivers/acpi/apei/hest.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ static int __init hest_ghes_dev_register(unsigned int ghes_count)
195195
struct ghes_arr ghes_arr;
196196

197197
ghes_arr.count = 0;
198-
ghes_arr.ghes_devs = kmalloc(sizeof(void *) * ghes_count, GFP_KERNEL);
198+
ghes_arr.ghes_devs = kmalloc_array(ghes_count, sizeof(void *),
199+
GFP_KERNEL);
199200
if (!ghes_arr.ghes_devs)
200201
return -ENOMEM;
201202

drivers/acpi/processor_perflib.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,9 @@ static int acpi_processor_get_performance_states(struct acpi_processor *pr)
343343

344344
pr->performance->state_count = pss->package.count;
345345
pr->performance->states =
346-
kmalloc(sizeof(struct acpi_processor_px) * pss->package.count,
347-
GFP_KERNEL);
346+
kmalloc_array(pss->package.count,
347+
sizeof(struct acpi_processor_px),
348+
GFP_KERNEL);
348349
if (!pr->performance->states) {
349350
result = -ENOMEM;
350351
goto end;

drivers/acpi/processor_throttling.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,9 @@ static int acpi_processor_get_throttling_states(struct acpi_processor *pr)
534534

535535
pr->throttling.state_count = tss->package.count;
536536
pr->throttling.states_tss =
537-
kmalloc(sizeof(struct acpi_processor_tx_tss) * tss->package.count,
538-
GFP_KERNEL);
537+
kmalloc_array(tss->package.count,
538+
sizeof(struct acpi_processor_tx_tss),
539+
GFP_KERNEL);
539540
if (!pr->throttling.states_tss) {
540541
result = -ENOMEM;
541542
goto end;

drivers/atm/solos-pci.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,8 @@ static int fpga_probe(struct pci_dev *dev, const struct pci_device_id *id)
12911291
card->using_dma = 1;
12921292
if (1) { /* All known FPGA versions so far */
12931293
card->dma_alignment = 3;
1294-
card->dma_bounce = kmalloc(card->nr_ports * BUF_SIZE, GFP_KERNEL);
1294+
card->dma_bounce = kmalloc_array(card->nr_ports,
1295+
BUF_SIZE, GFP_KERNEL);
12951296
if (!card->dma_bounce) {
12961297
dev_warn(&card->dev->dev, "Failed to allocate DMA bounce buffers\n");
12971298
err = -ENOMEM;

0 commit comments

Comments
 (0)