Commit 6da2ec56 authored by Kees Cook's avatar Kees Cook

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: default avatarKees Cook <keescook@chromium.org>
parent 1c542f38
...@@ -286,7 +286,7 @@ asmlinkage long sys_oabi_epoll_wait(int epfd, ...@@ -286,7 +286,7 @@ asmlinkage long sys_oabi_epoll_wait(int epfd,
return -EINVAL; return -EINVAL;
if (!access_ok(VERIFY_WRITE, events, sizeof(*events) * maxevents)) if (!access_ok(VERIFY_WRITE, events, sizeof(*events) * maxevents))
return -EFAULT; return -EFAULT;
kbuf = kmalloc(sizeof(*kbuf) * maxevents, GFP_KERNEL); kbuf = kmalloc_array(maxevents, sizeof(*kbuf), GFP_KERNEL);
if (!kbuf) if (!kbuf)
return -ENOMEM; return -ENOMEM;
fs = get_fs(); fs = get_fs();
...@@ -324,7 +324,7 @@ asmlinkage long sys_oabi_semtimedop(int semid, ...@@ -324,7 +324,7 @@ asmlinkage long sys_oabi_semtimedop(int semid,
return -EINVAL; return -EINVAL;
if (!access_ok(VERIFY_READ, tsops, sizeof(*tsops) * nsops)) if (!access_ok(VERIFY_READ, tsops, sizeof(*tsops) * nsops))
return -EFAULT; return -EFAULT;
sops = kmalloc(sizeof(*sops) * nsops, GFP_KERNEL); sops = kmalloc_array(nsops, sizeof(*sops), GFP_KERNEL);
if (!sops) if (!sops)
return -ENOMEM; return -ENOMEM;
err = 0; err = 0;
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
#include "mm.h" #include "mm.h"
#ifdef CONFIG_ARM_LPAE #ifdef CONFIG_ARM_LPAE
#define __pgd_alloc() kmalloc(PTRS_PER_PGD * sizeof(pgd_t), GFP_KERNEL) #define __pgd_alloc() kmalloc_array(PTRS_PER_PGD, sizeof(pgd_t), GFP_KERNEL)
#define __pgd_free(pgd) kfree(pgd) #define __pgd_free(pgd) kfree(pgd)
#else #else
#define __pgd_alloc() (pgd_t *)__get_free_pages(GFP_KERNEL, 2) #define __pgd_alloc() (pgd_t *)__get_free_pages(GFP_KERNEL, 2)
......
...@@ -766,8 +766,9 @@ static int coverage_start_fn(const struct decode_header *h, void *args) ...@@ -766,8 +766,9 @@ static int coverage_start_fn(const struct decode_header *h, void *args)
static int coverage_start(const union decode_item *table) static int coverage_start(const union decode_item *table)
{ {
coverage.base = kmalloc(MAX_COVERAGE_ENTRIES * coverage.base = kmalloc_array(MAX_COVERAGE_ENTRIES,
sizeof(struct coverage_entry), GFP_KERNEL); sizeof(struct coverage_entry),
GFP_KERNEL);
coverage.num_entries = 0; coverage.num_entries = 0;
coverage.nesting = 0; coverage.nesting = 0;
return table_iter(table, coverage_start_fn, &coverage); return table_iter(table, coverage_start_fn, &coverage);
......
...@@ -350,7 +350,8 @@ init_record_index_pools(void) ...@@ -350,7 +350,8 @@ init_record_index_pools(void)
/* - 3 - */ /* - 3 - */
slidx_pool.max_idx = (rec_max_size/sect_min_size) * 2 + 1; slidx_pool.max_idx = (rec_max_size/sect_min_size) * 2 + 1;
slidx_pool.buffer = slidx_pool.buffer =
kmalloc(slidx_pool.max_idx * sizeof(slidx_list_t), GFP_KERNEL); kmalloc_array(slidx_pool.max_idx, sizeof(slidx_list_t),
GFP_KERNEL);
return slidx_pool.buffer ? 0 : -ENOMEM; return slidx_pool.buffer ? 0 : -ENOMEM;
} }
......
...@@ -430,8 +430,9 @@ int ia64_itr_entry(u64 target_mask, u64 va, u64 pte, u64 log_size) ...@@ -430,8 +430,9 @@ int ia64_itr_entry(u64 target_mask, u64 va, u64 pte, u64 log_size)
int cpu = smp_processor_id(); int cpu = smp_processor_id();
if (!ia64_idtrs[cpu]) { if (!ia64_idtrs[cpu]) {
ia64_idtrs[cpu] = kmalloc(2 * IA64_TR_ALLOC_MAX * ia64_idtrs[cpu] = kmalloc_array(2 * IA64_TR_ALLOC_MAX,
sizeof (struct ia64_tr_entry), GFP_KERNEL); sizeof(struct ia64_tr_entry),
GFP_KERNEL);
if (!ia64_idtrs[cpu]) if (!ia64_idtrs[cpu])
return -ENOMEM; return -ENOMEM;
} }
......
...@@ -474,7 +474,8 @@ void __init sn_irq_lh_init(void) ...@@ -474,7 +474,8 @@ void __init sn_irq_lh_init(void)
{ {
int i; int i;
sn_irq_lh = kmalloc(sizeof(struct list_head *) * NR_IRQS, GFP_KERNEL); sn_irq_lh = kmalloc_array(NR_IRQS, sizeof(struct list_head *),
GFP_KERNEL);
if (!sn_irq_lh) if (!sn_irq_lh)
panic("SN PCI INIT: Failed to allocate memory for PCI init\n"); panic("SN PCI INIT: Failed to allocate memory for PCI init\n");
......
...@@ -411,8 +411,8 @@ u32 au1xxx_dbdma_ring_alloc(u32 chanid, int entries) ...@@ -411,8 +411,8 @@ u32 au1xxx_dbdma_ring_alloc(u32 chanid, int entries)
* and if we try that first we are likely to not waste larger * and if we try that first we are likely to not waste larger
* slabs of memory. * slabs of memory.
*/ */
desc_base = (u32)kmalloc(entries * sizeof(au1x_ddma_desc_t), desc_base = (u32)kmalloc_array(entries, sizeof(au1x_ddma_desc_t),
GFP_KERNEL|GFP_DMA); GFP_KERNEL|GFP_DMA);
if (desc_base == 0) if (desc_base == 0)
return 0; return 0;
......
...@@ -54,7 +54,7 @@ static int grow(rh_info_t * info, int max_blocks) ...@@ -54,7 +54,7 @@ static int grow(rh_info_t * info, int max_blocks)
new_blocks = max_blocks - info->max_blocks; new_blocks = max_blocks - info->max_blocks;
block = kmalloc(sizeof(rh_block_t) * max_blocks, GFP_ATOMIC); block = kmalloc_array(max_blocks, sizeof(rh_block_t), GFP_ATOMIC);
if (block == NULL) if (block == NULL)
return -ENOMEM; return -ENOMEM;
......
...@@ -156,7 +156,8 @@ static int hsta_msi_probe(struct platform_device *pdev) ...@@ -156,7 +156,8 @@ static int hsta_msi_probe(struct platform_device *pdev)
if (ret) if (ret)
goto out; goto out;
ppc4xx_hsta_msi.irq_map = kmalloc(sizeof(int) * irq_count, GFP_KERNEL); ppc4xx_hsta_msi.irq_map = kmalloc_array(irq_count, sizeof(int),
GFP_KERNEL);
if (!ppc4xx_hsta_msi.irq_map) { if (!ppc4xx_hsta_msi.irq_map) {
ret = -ENOMEM; ret = -ENOMEM;
goto out1; goto out1;
......
...@@ -89,7 +89,7 @@ static int ppc4xx_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) ...@@ -89,7 +89,7 @@ static int ppc4xx_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
if (type == PCI_CAP_ID_MSIX) if (type == PCI_CAP_ID_MSIX)
pr_debug("ppc4xx msi: MSI-X untested, trying anyway.\n"); pr_debug("ppc4xx msi: MSI-X untested, trying anyway.\n");
msi_data->msi_virqs = kmalloc((msi_irqs) * sizeof(int), GFP_KERNEL); msi_data->msi_virqs = kmalloc_array(msi_irqs, sizeof(int), GFP_KERNEL);
if (!msi_data->msi_virqs) if (!msi_data->msi_virqs)
return -ENOMEM; return -ENOMEM;
......
...@@ -1639,8 +1639,9 @@ void __init mpic_init(struct mpic *mpic) ...@@ -1639,8 +1639,9 @@ void __init mpic_init(struct mpic *mpic)
#ifdef CONFIG_PM #ifdef CONFIG_PM
/* allocate memory to save mpic state */ /* allocate memory to save mpic state */
mpic->save_data = kmalloc(mpic->num_sources * sizeof(*mpic->save_data), mpic->save_data = kmalloc_array(mpic->num_sources,
GFP_KERNEL); sizeof(*mpic->save_data),
GFP_KERNEL);
BUG_ON(mpic->save_data == NULL); BUG_ON(mpic->save_data == NULL);
#endif #endif
......
...@@ -49,7 +49,8 @@ static void *diag0c_store(unsigned int *count) ...@@ -49,7 +49,8 @@ static void *diag0c_store(unsigned int *count)
get_online_cpus(); get_online_cpus();
cpu_count = num_online_cpus(); cpu_count = num_online_cpus();
cpu_vec = kmalloc(sizeof(*cpu_vec) * num_possible_cpus(), GFP_KERNEL); cpu_vec = kmalloc_array(num_possible_cpus(), sizeof(*cpu_vec),
GFP_KERNEL);
if (!cpu_vec) if (!cpu_vec)
goto fail_put_online_cpus; goto fail_put_online_cpus;
/* Note: Diag 0c needs 8 byte alignment and real storage */ /* Note: Diag 0c needs 8 byte alignment and real storage */
......
...@@ -194,11 +194,13 @@ static debug_entry_t ***debug_areas_alloc(int pages_per_area, int nr_areas) ...@@ -194,11 +194,13 @@ static debug_entry_t ***debug_areas_alloc(int pages_per_area, int nr_areas)
debug_entry_t ***areas; debug_entry_t ***areas;
int i, j; int i, j;
areas = kmalloc(nr_areas * sizeof(debug_entry_t **), GFP_KERNEL); areas = kmalloc_array(nr_areas, sizeof(debug_entry_t **), GFP_KERNEL);
if (!areas) if (!areas)
goto fail_malloc_areas; goto fail_malloc_areas;
for (i = 0; i < nr_areas; i++) { for (i = 0; i < nr_areas; i++) {
areas[i] = kmalloc(pages_per_area * sizeof(debug_entry_t *), GFP_KERNEL); areas[i] = kmalloc_array(pages_per_area,
sizeof(debug_entry_t *),
GFP_KERNEL);
if (!areas[i]) if (!areas[i])
goto fail_malloc_areas2; goto fail_malloc_areas2;
for (j = 0; j < pages_per_area; j++) { for (j = 0; j < pages_per_area; j++) {
......
...@@ -527,7 +527,7 @@ static __init struct attribute **merge_attr(struct attribute **a, ...@@ -527,7 +527,7 @@ static __init struct attribute **merge_attr(struct attribute **a,
j++; j++;
j++; j++;
new = kmalloc(sizeof(struct attribute *) * j, GFP_KERNEL); new = kmalloc_array(j, sizeof(struct attribute *), GFP_KERNEL);
if (!new) if (!new)
return NULL; return NULL;
j = 0; j = 0;
......
...@@ -103,7 +103,7 @@ static int scode_set; ...@@ -103,7 +103,7 @@ static int scode_set;
static int static int
dcss_set_subcodes(void) dcss_set_subcodes(void)
{ {
char *name = kmalloc(8 * sizeof(char), GFP_KERNEL | GFP_DMA); char *name = kmalloc(8, GFP_KERNEL | GFP_DMA);
unsigned long rx, ry; unsigned long rx, ry;
int rc; int rc;
......
...@@ -166,7 +166,8 @@ static int __init check_nmi_watchdog(void) ...@@ -166,7 +166,8 @@ static int __init check_nmi_watchdog(void)
if (!atomic_read(&nmi_active)) if (!atomic_read(&nmi_active))
return 0; return 0;
prev_nmi_count = kmalloc(nr_cpu_ids * sizeof(unsigned int), GFP_KERNEL); prev_nmi_count = kmalloc_array(nr_cpu_ids, sizeof(unsigned int),
GFP_KERNEL);
if (!prev_nmi_count) { if (!prev_nmi_count) {
err = -ENOMEM; err = -ENOMEM;
goto error; goto error;
......
...@@ -575,8 +575,9 @@ SYSCALL_DEFINE5(utrap_install, utrap_entry_t, type, ...@@ -575,8 +575,9 @@ SYSCALL_DEFINE5(utrap_install, utrap_entry_t, type,
unsigned long *p = current_thread_info()->utraps; unsigned long *p = current_thread_info()->utraps;
current_thread_info()->utraps = current_thread_info()->utraps =
kmalloc((UT_TRAP_INSTRUCTION_31+1)*sizeof(long), kmalloc_array(UT_TRAP_INSTRUCTION_31 + 1,
GFP_KERNEL); sizeof(long),
GFP_KERNEL);
if (!current_thread_info()->utraps) { if (!current_thread_info()->utraps) {
current_thread_info()->utraps = p; current_thread_info()->utraps = p;
return -ENOMEM; return -ENOMEM;
......
...@@ -335,7 +335,7 @@ void bpf_jit_compile(struct bpf_prog *fp) ...@@ -335,7 +335,7 @@ void bpf_jit_compile(struct bpf_prog *fp)
if (!bpf_jit_enable) if (!bpf_jit_enable)
return; return;
addrs = kmalloc(flen * sizeof(*addrs), GFP_KERNEL); addrs = kmalloc_array(flen, sizeof(*addrs), GFP_KERNEL);
if (addrs == NULL) if (addrs == NULL)
return; return;
......
...@@ -1127,9 +1127,9 @@ static int __init ubd_init(void) ...@@ -1127,9 +1127,9 @@ static int __init ubd_init(void)
return -1; return -1;
} }
irq_req_buffer = kmalloc( irq_req_buffer = kmalloc_array(UBD_REQ_BUFFER_SIZE,
sizeof(struct io_thread_req *) * UBD_REQ_BUFFER_SIZE, sizeof(struct io_thread_req *),
GFP_KERNEL GFP_KERNEL
); );
irq_remainder = 0; irq_remainder = 0;
...@@ -1137,9 +1137,9 @@ static int __init ubd_init(void) ...@@ -1137,9 +1137,9 @@ static int __init ubd_init(void)
printk(KERN_ERR "Failed to initialize ubd buffering\n"); printk(KERN_ERR "Failed to initialize ubd buffering\n");
return -1; return -1;
} }
io_req_buffer = kmalloc( io_req_buffer = kmalloc_array(UBD_REQ_BUFFER_SIZE,
sizeof(struct io_thread_req *) * UBD_REQ_BUFFER_SIZE, sizeof(struct io_thread_req *),
GFP_KERNEL GFP_KERNEL
); );
io_remainder = 0; io_remainder = 0;
......
...@@ -527,14 +527,14 @@ static struct vector_queue *create_queue( ...@@ -527,14 +527,14 @@ static struct vector_queue *create_queue(
result->max_iov_frags = num_extra_frags; result->max_iov_frags = num_extra_frags;
for (i = 0; i < max_size; i++) { for (i = 0; i < max_size; i++) {
if (vp->header_size > 0) if (vp->header_size > 0)
iov = kmalloc( iov = kmalloc_array(3 + num_extra_frags,
sizeof(struct iovec) * (3 + num_extra_frags), sizeof(struct iovec),
GFP_KERNEL GFP_KERNEL
); );
else else
iov = kmalloc( iov = kmalloc_array(2 + num_extra_frags,
sizeof(struct iovec) * (2 + num_extra_frags), sizeof(struct iovec),
GFP_KERNEL GFP_KERNEL
); );
if (iov == NULL) if (iov == NULL)
goto out_fail; goto out_fail;
......
...@@ -109,8 +109,9 @@ static int __init puv3_pm_init(void) ...@@ -109,8 +109,9 @@ static int __init puv3_pm_init(void)
return -EINVAL; return -EINVAL;
} }
sleep_save = kmalloc(puv3_cpu_pm_fns->save_count sleep_save = kmalloc_array(puv3_cpu_pm_fns->save_count,
* sizeof(unsigned long), GFP_KERNEL); sizeof(unsigned long),
GFP_KERNEL);
if (!sleep_save) { if (!sleep_save) {
printk(KERN_ERR "failed to alloc memory for pm save\n"); printk(KERN_ERR "failed to alloc memory for pm save\n");
return -ENOMEM; return -ENOMEM;
......
...@@ -1637,7 +1637,7 @@ __init struct attribute **merge_attr(struct attribute **a, struct attribute **b) ...@@ -1637,7 +1637,7 @@ __init struct attribute **merge_attr(struct attribute **a, struct attribute **b)
j++; j++;
j++; j++;
new = kmalloc(sizeof(struct attribute *) * j, GFP_KERNEL); new = kmalloc_array(j, sizeof(struct attribute *), GFP_KERNEL);
if (!new) if (!new)
return NULL; return NULL;
......
...@@ -966,8 +966,8 @@ int __init hpet_enable(void) ...@@ -966,8 +966,8 @@ int __init hpet_enable(void)
#endif #endif
cfg = hpet_readl(HPET_CFG); cfg = hpet_readl(HPET_CFG);
hpet_boot_cfg = kmalloc((last + 2) * sizeof(*hpet_boot_cfg), hpet_boot_cfg = kmalloc_array(last + 2, sizeof(*hpet_boot_cfg),
GFP_KERNEL); GFP_KERNEL);
if (hpet_boot_cfg) if (hpet_boot_cfg)
*hpet_boot_cfg = cfg; *hpet_boot_cfg = cfg;
else else
......
...@@ -283,7 +283,7 @@ static int __init create_setup_data_nodes(struct kobject *parent) ...@@ -283,7 +283,7 @@ static int __init create_setup_data_nodes(struct kobject *parent)
if (ret) if (ret)
goto out_setup_data_kobj; goto out_setup_data_kobj;
kobjp = kmalloc(sizeof(*kobjp) * nr, GFP_KERNEL); kobjp = kmalloc_array(nr, sizeof(*kobjp), GFP_KERNEL);
if (!kobjp) { if (!kobjp) {
ret = -ENOMEM; ret = -ENOMEM;
goto out_setup_data_kobj; goto out_setup_data_kobj;
......
...@@ -1001,7 +1001,9 @@ static int svm_cpu_init(int cpu) ...@@ -1001,7 +1001,9 @@ static int svm_cpu_init(int cpu)
if (svm_sev_enabled()) { if (svm_sev_enabled()) {
r = -ENOMEM; r = -ENOMEM;
sd->sev_vmcbs = kmalloc((max_sev_asid + 1) * sizeof(void *), GFP_KERNEL); sd->sev_vmcbs = kmalloc_array(max_sev_asid + 1,
sizeof(void *),
GFP_KERNEL);
if (!sd->sev_vmcbs) if (!sd->sev_vmcbs)
goto err_1; goto err_1;
} }
......
...@@ -1107,7 +1107,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog) ...@@ -1107,7 +1107,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
extra_pass = true; extra_pass = true;
goto skip_init_addrs; goto skip_init_addrs;
} }
addrs = kmalloc(prog->len * sizeof(*addrs), GFP_KERNEL); addrs = kmalloc_array(prog->len, sizeof(*addrs), GFP_KERNEL);
if (!addrs) { if (!addrs) {
prog = orig_prog; prog = orig_prog;
goto out_addrs; goto out_addrs;
......
...@@ -2345,7 +2345,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog) ...@@ -2345,7 +2345,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
prog = tmp; prog = tmp;
} }
addrs = kmalloc(prog->len * sizeof(*addrs), GFP_KERNEL); addrs = kmalloc_array(prog->len, sizeof(*addrs), GFP_KERNEL);
if (!addrs) { if (!addrs) {
prog = orig_prog; prog = orig_prog;
goto out; goto out;
......
...@@ -2142,7 +2142,7 @@ static int __init init_per_cpu(int nuvhubs, int base_part_pnode) ...@@ -2142,7 +2142,7 @@ static int __init init_per_cpu(int nuvhubs, int base_part_pnode)
if (is_uv3_hub() || is_uv2_hub() || is_uv1_hub()) if (is_uv3_hub() || is_uv2_hub() || is_uv1_hub())
timeout_us = calculate_destination_timeout(); timeout_us = calculate_destination_timeout();
vp = kmalloc(nuvhubs * sizeof(struct uvhub_desc), GFP_KERNEL); vp = kmalloc_array(nuvhubs, sizeof(struct uvhub_desc), GFP_KERNEL);
uvhub_descs = (struct uvhub_desc *)vp; uvhub_descs = (struct uvhub_desc *)vp;
memset(uvhub_descs, 0, nuvhubs * sizeof(struct uvhub_desc)); memset(uvhub_descs, 0, nuvhubs * sizeof(struct uvhub_desc));
uvhub_mask = kzalloc((nuvhubs+7)/8, GFP_KERNEL); uvhub_mask = kzalloc((nuvhubs+7)/8, GFP_KERNEL);
......
...@@ -378,7 +378,7 @@ static bool ldm_validate_tocblocks(struct parsed_partitions *state, ...@@ -378,7 +378,7 @@ static bool ldm_validate_tocblocks(struct parsed_partitions *state,
BUG_ON(!state || !ldb); BUG_ON(!state || !ldb);
ph = &ldb->ph; ph = &ldb->ph;
tb[0] = &ldb->toc; tb[0] = &ldb->toc;
tb[1] = kmalloc(sizeof(*tb[1]) * 3, GFP_KERNEL); tb[1] = kmalloc_array(3, sizeof(*tb[1]), GFP_KERNEL);
if (!tb[1]) { if (!tb[1]) {
ldm_crit("Out of memory."); ldm_crit("Out of memory.");
goto err; goto err;
......
...@@ -603,7 +603,8 @@ static int __test_aead(struct crypto_aead *tfm, int enc, ...@@ -603,7 +603,8 @@ static int __test_aead(struct crypto_aead *tfm, int enc,
goto out_nooutbuf; goto out_nooutbuf;
/* avoid "the frame size is larger than 1024 bytes" compiler warning */ /* avoid "the frame size is larger than 1024 bytes" compiler warning */
sg = kmalloc(sizeof(*sg) * 8 * (diff_dst ? 4 : 2), GFP_KERNEL); sg = kmalloc(array3_size(sizeof(*sg), 8, (diff_dst ? 4 : 2)),
GFP_KERNEL);
if (!sg) if (!sg)
goto out_nosg; goto out_nosg;
sgout = &sg[16]; sgout = &sg[16];
......
...@@ -832,8 +832,9 @@ int acpi_video_get_levels(struct acpi_device *device, ...@@ -832,8 +832,9 @@ int acpi_video_get_levels(struct acpi_device *device,
* in order to account for buggy BIOS which don't export the first two * in order to account for buggy BIOS which don't export the first two
* special levels (see below) * special levels (see below)
*/ */
br->levels = kmalloc((obj->package.count + ACPI_VIDEO_FIRST_LEVEL) * br->levels = kmalloc_array(obj->package.count + ACPI_VIDEO_FIRST_LEVEL,
sizeof(*br->levels), GFP_KERNEL); sizeof(*br->levels),
GFP_KERNEL);
if (!br->levels) {