Skip to content

Commit 6396bb2

Browse files
committed
treewide: kzalloc() -> kcalloc()
The kzalloc() function has a 2-factor argument form, kcalloc(). This patch replaces cases of: kzalloc(a * b, gfp) with: kcalloc(a * b, gfp) as well as handling cases of: kzalloc(a * b * c, gfp) with: kzalloc(array3_size(a, b, c), gfp) as it's slightly less ugly than: kzalloc_array(array_size(a, b), c, gfp) This does, however, attempt to ignore constant size factors like: kzalloc(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; @@ ( kzalloc( - (sizeof(TYPE)) * E + sizeof(TYPE) * E , ...) | kzalloc( - (sizeof(THING)) * E + sizeof(THING) * E , ...) ) // Drop single-byte sizes and redundant parens. @@ expression COUNT; typedef u8; typedef __u8; @@ ( kzalloc( - sizeof(u8) * (COUNT) + COUNT , ...) | kzalloc( - sizeof(__u8) * (COUNT) + COUNT , ...) | kzalloc( - sizeof(char) * (COUNT) + COUNT , ...) | kzalloc( - sizeof(unsigned char) * (COUNT) + COUNT , ...) | kzalloc( - sizeof(u8) * COUNT + COUNT , ...) | kzalloc( - sizeof(__u8) * COUNT + COUNT , ...) | kzalloc( - sizeof(char) * COUNT + COUNT , ...) | kzalloc( - 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; @@ ( - kzalloc + kcalloc ( - sizeof(TYPE) * (COUNT_ID) + COUNT_ID, sizeof(TYPE) , ...) | - kzalloc + kcalloc ( - sizeof(TYPE) * COUNT_ID + COUNT_ID, sizeof(TYPE) , ...) | - kzalloc + kcalloc ( - sizeof(TYPE) * (COUNT_CONST) + COUNT_CONST, sizeof(TYPE) , ...) | - kzalloc + kcalloc ( - sizeof(TYPE) * COUNT_CONST + COUNT_CONST, sizeof(TYPE) , ...) | - kzalloc + kcalloc ( - sizeof(THING) * (COUNT_ID) + COUNT_ID, sizeof(THING) , ...) | - kzalloc + kcalloc ( - sizeof(THING) * COUNT_ID + COUNT_ID, sizeof(THING) , ...) | - kzalloc + kcalloc ( - sizeof(THING) * (COUNT_CONST) + COUNT_CONST, sizeof(THING) , ...) | - kzalloc + kcalloc ( - sizeof(THING) * COUNT_CONST + COUNT_CONST, sizeof(THING) , ...) ) // 2-factor product, only identifiers. @@ identifier SIZE, COUNT; @@ - kzalloc + kcalloc ( - 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; @@ ( kzalloc( - sizeof(TYPE) * (COUNT) * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | kzalloc( - sizeof(TYPE) * (COUNT) * STRIDE + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | kzalloc( - sizeof(TYPE) * COUNT * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | kzalloc( - sizeof(TYPE) * COUNT * STRIDE + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | kzalloc( - sizeof(THING) * (COUNT) * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | kzalloc( - sizeof(THING) * (COUNT) * STRIDE + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | kzalloc( - sizeof(THING) * COUNT * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | kzalloc( - 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; @@ ( kzalloc( - sizeof(TYPE1) * sizeof(TYPE2) * COUNT + array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2)) , ...) | kzalloc( - sizeof(TYPE1) * sizeof(THING2) * (COUNT) + array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2)) , ...) | kzalloc( - sizeof(THING1) * sizeof(THING2) * COUNT + array3_size(COUNT, sizeof(THING1), sizeof(THING2)) , ...) | kzalloc( - sizeof(THING1) * sizeof(THING2) * (COUNT) + array3_size(COUNT, sizeof(THING1), sizeof(THING2)) , ...) | kzalloc( - sizeof(TYPE1) * sizeof(THING2) * COUNT + array3_size(COUNT, sizeof(TYPE1), sizeof(THING2)) , ...) | kzalloc( - 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; @@ ( kzalloc( - (COUNT) * STRIDE * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | kzalloc( - COUNT * (STRIDE) * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | kzalloc( - COUNT * STRIDE * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | kzalloc( - (COUNT) * (STRIDE) * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | kzalloc( - COUNT * (STRIDE) * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | kzalloc( - (COUNT) * STRIDE * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | kzalloc( - (COUNT) * (STRIDE) * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | kzalloc( - 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; @@ ( kzalloc(C1 * C2 * C3, ...) | kzalloc( - (E1) * E2 * E3 + array3_size(E1, E2, E3) , ...) | kzalloc( - (E1) * (E2) * E3 + array3_size(E1, E2, E3) , ...) | kzalloc( - (E1) * (E2) * (E3) + array3_size(E1, E2, E3) , ...) | kzalloc( - 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; @@ ( kzalloc(sizeof(THING) * C2, ...) | kzalloc(sizeof(TYPE) * C2, ...) | kzalloc(C1 * C2 * C3, ...) | kzalloc(C1 * C2, ...) | - kzalloc + kcalloc ( - sizeof(TYPE) * (E2) + E2, sizeof(TYPE) , ...) | - kzalloc + kcalloc ( - sizeof(TYPE) * E2 + E2, sizeof(TYPE) , ...) | - kzalloc + kcalloc ( - sizeof(THING) * (E2) + E2, sizeof(THING) , ...) | - kzalloc + kcalloc ( - sizeof(THING) * E2 + E2, sizeof(THING) , ...) | - kzalloc + kcalloc ( - (E1) * E2 + E1, E2 , ...) | - kzalloc + kcalloc ( - (E1) * (E2) + E1, E2 , ...) | - kzalloc + kcalloc ( - E1 * E2 + E1, E2 , ...) ) Signed-off-by: Kees Cook <keescook@chromium.org>
1 parent 6da2ec5 commit 6396bb2

484 files changed

Lines changed: 1177 additions & 977 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/arm/mach-footbridge/dc21285.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ int __init dc21285_setup(int nr, struct pci_sys_data *sys)
252252
if (nr || !footbridge_cfn_mode())
253253
return 0;
254254

255-
res = kzalloc(sizeof(struct resource) * 2, GFP_KERNEL);
255+
res = kcalloc(2, sizeof(struct resource), GFP_KERNEL);
256256
if (!res) {
257257
printk("out of memory for root bus resources");
258258
return 0;

‎arch/arm/mach-ixp4xx/common-pci.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ int ixp4xx_setup(int nr, struct pci_sys_data *sys)
421421
if (nr >= 1)
422422
return 0;
423423

424-
res = kzalloc(sizeof(*res) * 2, GFP_KERNEL);
424+
res = kcalloc(2, sizeof(*res), GFP_KERNEL);
425425
if (res == NULL) {
426426
/*
427427
* If we're out of memory this early, something is wrong,

‎arch/arm/mach-omap1/mcbsp.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ static void omap_mcbsp_register_board_cfg(struct resource *res, int res_count,
389389
{
390390
int i;
391391

392-
omap_mcbsp_devices = kzalloc(size * sizeof(struct platform_device *),
392+
omap_mcbsp_devices = kcalloc(size, sizeof(struct platform_device *),
393393
GFP_KERNEL);
394394
if (!omap_mcbsp_devices) {
395395
printk(KERN_ERR "Could not register McBSP devices\n");

‎arch/arm/mach-omap2/hsmmc.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static int __init omap_hsmmc_pdata_init(struct omap2_hsmmc_info *c,
3535
{
3636
char *hc_name;
3737

38-
hc_name = kzalloc(sizeof(char) * (HSMMC_NAME_LEN + 1), GFP_KERNEL);
38+
hc_name = kzalloc(HSMMC_NAME_LEN + 1, GFP_KERNEL);
3939
if (!hc_name) {
4040
kfree(hc_name);
4141
return -ENOMEM;

‎arch/arm/mach-omap2/omap_device.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ static int omap_device_build_from_dt(struct platform_device *pdev)
155155
if (!omap_hwmod_parse_module_range(NULL, node, &res))
156156
return -ENODEV;
157157

158-
hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
158+
hwmods = kcalloc(oh_cnt, sizeof(struct omap_hwmod *), GFP_KERNEL);
159159
if (!hwmods) {
160160
ret = -ENOMEM;
161161
goto odbfd_exit;
@@ -405,7 +405,7 @@ omap_device_copy_resources(struct omap_hwmod *oh,
405405
goto error;
406406
}
407407

408-
res = kzalloc(sizeof(*res) * 2, GFP_KERNEL);
408+
res = kcalloc(2, sizeof(*res), GFP_KERNEL);
409409
if (!res)
410410
return -ENOMEM;
411411

‎arch/arm/mach-omap2/prm_common.c‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,11 @@ int omap_prcm_register_chain_handler(struct omap_prcm_irq_setup *irq_setup)
285285

286286
prcm_irq_setup = irq_setup;
287287

288-
prcm_irq_chips = kzalloc(sizeof(void *) * nr_regs, GFP_KERNEL);
289-
prcm_irq_setup->saved_mask = kzalloc(sizeof(u32) * nr_regs, GFP_KERNEL);
290-
prcm_irq_setup->priority_mask = kzalloc(sizeof(u32) * nr_regs,
291-
GFP_KERNEL);
288+
prcm_irq_chips = kcalloc(nr_regs, sizeof(void *), GFP_KERNEL);
289+
prcm_irq_setup->saved_mask = kcalloc(nr_regs, sizeof(u32),
290+
GFP_KERNEL);
291+
prcm_irq_setup->priority_mask = kcalloc(nr_regs, sizeof(u32),
292+
GFP_KERNEL);
292293

293294
if (!prcm_irq_chips || !prcm_irq_setup->saved_mask ||
294295
!prcm_irq_setup->priority_mask)

‎arch/arm/mach-vexpress/spc.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ static int ve_spc_populate_opps(uint32_t cluster)
403403
uint32_t data = 0, off, ret, idx;
404404
struct ve_spc_opp *opps;
405405

406-
opps = kzalloc(sizeof(*opps) * MAX_OPPS, GFP_KERNEL);
406+
opps = kcalloc(MAX_OPPS, sizeof(*opps), GFP_KERNEL);
407407
if (!opps)
408408
return -ENOMEM;
409409

‎arch/arm/mm/dma-mapping.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,8 +2162,8 @@ arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, u64 size)
21622162
goto err;
21632163

21642164
mapping->bitmap_size = bitmap_size;
2165-
mapping->bitmaps = kzalloc(extensions * sizeof(unsigned long *),
2166-
GFP_KERNEL);
2165+
mapping->bitmaps = kcalloc(extensions, sizeof(unsigned long *),
2166+
GFP_KERNEL);
21672167
if (!mapping->bitmaps)
21682168
goto err2;
21692169

‎arch/arm64/kernel/armv8_deprecated.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ static void __init register_insn_emulation_sysctl(void)
234234
struct insn_emulation *insn;
235235
struct ctl_table *insns_sysctl, *sysctl;
236236

237-
insns_sysctl = kzalloc(sizeof(*sysctl) * (nr_insn_emulated + 1),
238-
GFP_KERNEL);
237+
insns_sysctl = kcalloc(nr_insn_emulated + 1, sizeof(*sysctl),
238+
GFP_KERNEL);
239239

240240
raw_spin_lock_irqsave(&insn_emulation_lock, flags);
241241
list_for_each_entry(insn, &insn_emulation, node) {

‎arch/arm64/mm/context.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ static int asids_init(void)
263263
*/
264264
WARN_ON(NUM_USER_ASIDS - 1 <= num_possible_cpus());
265265
atomic64_set(&asid_generation, ASID_FIRST_VERSION);
266-
asid_map = kzalloc(BITS_TO_LONGS(NUM_USER_ASIDS) * sizeof(*asid_map),
266+
asid_map = kcalloc(BITS_TO_LONGS(NUM_USER_ASIDS), sizeof(*asid_map),
267267
GFP_KERNEL);
268268
if (!asid_map)
269269
panic("Failed to allocate bitmap for %lu ASIDs\n",

0 commit comments

Comments
 (0)