Skip to content

Commit 42bc47b

Browse files
committed
treewide: Use array_size() in vmalloc()
The vmalloc() function has no 2-factor argument form, so multiplication factors need to be wrapped in array_size(). This patch replaces cases of: vmalloc(a * b) with: vmalloc(array_size(a, b)) as well as handling cases of: vmalloc(a * b * c) with: vmalloc(array3_size(a, b, c)) This does, however, attempt to ignore constant size factors like: vmalloc(4 * 1024) 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; @@ ( vmalloc( - (sizeof(TYPE)) * E + sizeof(TYPE) * E , ...) | vmalloc( - (sizeof(THING)) * E + sizeof(THING) * E , ...) ) // Drop single-byte sizes and redundant parens. @@ expression COUNT; typedef u8; typedef __u8; @@ ( vmalloc( - sizeof(u8) * (COUNT) + COUNT , ...) | vmalloc( - sizeof(__u8) * (COUNT) + COUNT , ...) | vmalloc( - sizeof(char) * (COUNT) + COUNT , ...) | vmalloc( - sizeof(unsigned char) * (COUNT) + COUNT , ...) | vmalloc( - sizeof(u8) * COUNT + COUNT , ...) | vmalloc( - sizeof(__u8) * COUNT + COUNT , ...) | vmalloc( - sizeof(char) * COUNT + COUNT , ...) | vmalloc( - 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; @@ ( vmalloc( - sizeof(TYPE) * (COUNT_ID) + array_size(COUNT_ID, sizeof(TYPE)) , ...) | vmalloc( - sizeof(TYPE) * COUNT_ID + array_size(COUNT_ID, sizeof(TYPE)) , ...) | vmalloc( - sizeof(TYPE) * (COUNT_CONST) + array_size(COUNT_CONST, sizeof(TYPE)) , ...) | vmalloc( - sizeof(TYPE) * COUNT_CONST + array_size(COUNT_CONST, sizeof(TYPE)) , ...) | vmalloc( - sizeof(THING) * (COUNT_ID) + array_size(COUNT_ID, sizeof(THING)) , ...) | vmalloc( - sizeof(THING) * COUNT_ID + array_size(COUNT_ID, sizeof(THING)) , ...) | vmalloc( - sizeof(THING) * (COUNT_CONST) + array_size(COUNT_CONST, sizeof(THING)) , ...) | vmalloc( - sizeof(THING) * COUNT_CONST + array_size(COUNT_CONST, sizeof(THING)) , ...) ) // 2-factor product, only identifiers. @@ identifier SIZE, COUNT; @@ vmalloc( - SIZE * COUNT + array_size(COUNT, SIZE) , ...) // 3-factor product with 1 sizeof(type) or sizeof(expression), with // redundant parens removed. @@ expression THING; identifier STRIDE, COUNT; type TYPE; @@ ( vmalloc( - sizeof(TYPE) * (COUNT) * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | vmalloc( - sizeof(TYPE) * (COUNT) * STRIDE + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | vmalloc( - sizeof(TYPE) * COUNT * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | vmalloc( - sizeof(TYPE) * COUNT * STRIDE + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | vmalloc( - sizeof(THING) * (COUNT) * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | vmalloc( - sizeof(THING) * (COUNT) * STRIDE + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | vmalloc( - sizeof(THING) * COUNT * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | vmalloc( - 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; @@ ( vmalloc( - sizeof(TYPE1) * sizeof(TYPE2) * COUNT + array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2)) , ...) | vmalloc( - sizeof(TYPE1) * sizeof(THING2) * (COUNT) + array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2)) , ...) | vmalloc( - sizeof(THING1) * sizeof(THING2) * COUNT + array3_size(COUNT, sizeof(THING1), sizeof(THING2)) , ...) | vmalloc( - sizeof(THING1) * sizeof(THING2) * (COUNT) + array3_size(COUNT, sizeof(THING1), sizeof(THING2)) , ...) | vmalloc( - sizeof(TYPE1) * sizeof(THING2) * COUNT + array3_size(COUNT, sizeof(TYPE1), sizeof(THING2)) , ...) | vmalloc( - 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; @@ ( vmalloc( - (COUNT) * STRIDE * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | vmalloc( - COUNT * (STRIDE) * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | vmalloc( - COUNT * STRIDE * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | vmalloc( - (COUNT) * (STRIDE) * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | vmalloc( - COUNT * (STRIDE) * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | vmalloc( - (COUNT) * STRIDE * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | vmalloc( - (COUNT) * (STRIDE) * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | vmalloc( - 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; @@ ( vmalloc(C1 * C2 * C3, ...) | vmalloc( - E1 * E2 * E3 + array3_size(E1, E2, E3) , ...) ) // And then all remaining 2 factors products when they're not all constants. @@ expression E1, E2; constant C1, C2; @@ ( vmalloc(C1 * C2, ...) | vmalloc( - E1 * E2 + array_size(E1, E2) , ...) ) Signed-off-by: Kees Cook <keescook@chromium.org>
1 parent a86854d commit 42bc47b

78 files changed

Lines changed: 160 additions & 116 deletions

File tree

Some content is hidden

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

arch/powerpc/kernel/rtasd.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,8 @@ static int __init rtas_event_scan_init(void)
559559
rtas_error_log_max = rtas_get_error_log_max();
560560
rtas_error_log_buffer_max = rtas_error_log_max + sizeof(int);
561561

562-
rtas_log_buf = vmalloc(rtas_error_log_buffer_max*LOG_NUMBER);
562+
rtas_log_buf = vmalloc(array_size(LOG_NUMBER,
563+
rtas_error_log_buffer_max));
563564
if (!rtas_log_buf) {
564565
printk(KERN_ERR "rtasd: no memory\n");
565566
return -ENOMEM;

arch/powerpc/kvm/book3s_64_mmu_hv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ int kvmppc_allocate_hpt(struct kvm_hpt_info *info, u32 order)
108108
npte = 1ul << (order - 4);
109109

110110
/* Allocate reverse map array */
111-
rev = vmalloc(sizeof(struct revmap_entry) * npte);
111+
rev = vmalloc(array_size(npte, sizeof(struct revmap_entry)));
112112
if (!rev) {
113113
if (cma)
114114
kvm_free_hpt_cma(page, 1 << (order - PAGE_SHIFT));

arch/s390/hypfs/hypfs_diag.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ static void *page_align_ptr(void *ptr)
239239
static void *diag204_alloc_vbuf(int pages)
240240
{
241241
/* The buffer has to be page aligned! */
242-
diag204_buf_vmalloc = vmalloc(PAGE_SIZE * (pages + 1));
242+
diag204_buf_vmalloc = vmalloc(array_size(PAGE_SIZE, (pages + 1)));
243243
if (!diag204_buf_vmalloc)
244244
return ERR_PTR(-ENOMEM);
245245
diag204_buf = page_align_ptr(diag204_buf_vmalloc);

arch/s390/kernel/module.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
123123

124124
/* Allocate one syminfo structure per symbol. */
125125
me->arch.nsyms = symtab->sh_size / sizeof(Elf_Sym);
126-
me->arch.syminfo = vmalloc(me->arch.nsyms *
127-
sizeof(struct mod_arch_syminfo));
126+
me->arch.syminfo = vmalloc(array_size(sizeof(struct mod_arch_syminfo),
127+
me->arch.nsyms));
128128
if (!me->arch.syminfo)
129129
return -ENOMEM;
130130
symbols = (void *) hdr + symtab->sh_offset;

arch/s390/kernel/sthyi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ static void fill_diag(struct sthyi_sctns *sctns)
315315
if (pages <= 0)
316316
return;
317317

318-
diag204_buf = vmalloc(PAGE_SIZE * pages);
318+
diag204_buf = vmalloc(array_size(pages, PAGE_SIZE));
319319
if (!diag204_buf)
320320
return;
321321

arch/s390/kvm/gaccess.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
847847
nr_pages = (((ga & ~PAGE_MASK) + len - 1) >> PAGE_SHIFT) + 1;
848848
pages = pages_array;
849849
if (nr_pages > ARRAY_SIZE(pages_array))
850-
pages = vmalloc(nr_pages * sizeof(unsigned long));
850+
pages = vmalloc(array_size(nr_pages, sizeof(unsigned long)));
851851
if (!pages)
852852
return -ENOMEM;
853853
need_ipte_lock = psw_bits(*psw).dat && !asce.r;

arch/s390/kvm/kvm-s390.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1725,7 +1725,7 @@ static int kvm_s390_set_cmma_bits(struct kvm *kvm,
17251725
if (args->count == 0)
17261726
return 0;
17271727

1728-
bits = vmalloc(sizeof(*bits) * args->count);
1728+
bits = vmalloc(array_size(sizeof(*bits), args->count));
17291729
if (!bits)
17301730
return -ENOMEM;
17311731

arch/x86/kvm/cpuid.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,9 @@ int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
203203
goto out;
204204
r = -ENOMEM;
205205
if (cpuid->nent) {
206-
cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) *
207-
cpuid->nent);
206+
cpuid_entries =
207+
vmalloc(array_size(sizeof(struct kvm_cpuid_entry),
208+
cpuid->nent));
208209
if (!cpuid_entries)
209210
goto out;
210211
r = -EFAULT;

drivers/base/firmware_loader/fallback.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ static int fw_realloc_pages(struct fw_sysfs *fw_sysfs, int min_size)
403403
fw_priv->page_array_size * 2);
404404
struct page **new_pages;
405405

406-
new_pages = vmalloc(new_array_size * sizeof(void *));
406+
new_pages = vmalloc(array_size(new_array_size, sizeof(void *)));
407407
if (!new_pages) {
408408
fw_load_abort(fw_sysfs);
409409
return -ENOMEM;

drivers/dma/ipu/ipu_idmac.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,8 @@ static dma_cookie_t idmac_tx_submit(struct dma_async_tx_descriptor *tx)
910910
/* Called with ichan->chan_mutex held */
911911
static int idmac_desc_alloc(struct idmac_channel *ichan, int n)
912912
{
913-
struct idmac_tx_desc *desc = vmalloc(n * sizeof(struct idmac_tx_desc));
913+
struct idmac_tx_desc *desc =
914+
vmalloc(array_size(n, sizeof(struct idmac_tx_desc)));
914915
struct idmac *idmac = to_idmac(ichan->dma_chan.device);
915916

916917
if (!desc)

0 commit comments

Comments
 (0)