Skip to content

Commit a86854d

Browse files
committed
treewide: devm_kzalloc() -> devm_kcalloc()
The devm_kzalloc() function has a 2-factor argument form, devm_kcalloc(). This patch replaces cases of: devm_kzalloc(handle, a * b, gfp) with: devm_kcalloc(handle, a * b, gfp) as well as handling cases of: devm_kzalloc(handle, a * b * c, gfp) with: devm_kzalloc(handle, array3_size(a, b, c), gfp) as it's slightly less ugly than: devm_kcalloc(handle, array_size(a, b), c, gfp) This does, however, attempt to ignore constant size factors like: devm_kzalloc(handle, 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. Some manual whitespace fixes were needed in this patch, as Coccinelle really liked to write "=devm_kcalloc..." instead of "= devm_kcalloc...". The Coccinelle script used for this was: // Fix redundant parens around sizeof(). @@ expression HANDLE; type TYPE; expression THING, E; @@ ( devm_kzalloc(HANDLE, - (sizeof(TYPE)) * E + sizeof(TYPE) * E , ...) | devm_kzalloc(HANDLE, - (sizeof(THING)) * E + sizeof(THING) * E , ...) ) // Drop single-byte sizes and redundant parens. @@ expression HANDLE; expression COUNT; typedef u8; typedef __u8; @@ ( devm_kzalloc(HANDLE, - sizeof(u8) * (COUNT) + COUNT , ...) | devm_kzalloc(HANDLE, - sizeof(__u8) * (COUNT) + COUNT , ...) | devm_kzalloc(HANDLE, - sizeof(char) * (COUNT) + COUNT , ...) | devm_kzalloc(HANDLE, - sizeof(unsigned char) * (COUNT) + COUNT , ...) | devm_kzalloc(HANDLE, - sizeof(u8) * COUNT + COUNT , ...) | devm_kzalloc(HANDLE, - sizeof(__u8) * COUNT + COUNT , ...) | devm_kzalloc(HANDLE, - sizeof(char) * COUNT + COUNT , ...) | devm_kzalloc(HANDLE, - sizeof(unsigned char) * COUNT + COUNT , ...) ) // 2-factor product with sizeof(type/expression) and identifier or constant. @@ expression HANDLE; type TYPE; expression THING; identifier COUNT_ID; constant COUNT_CONST; @@ ( - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(TYPE) * (COUNT_ID) + COUNT_ID, sizeof(TYPE) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(TYPE) * COUNT_ID + COUNT_ID, sizeof(TYPE) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(TYPE) * (COUNT_CONST) + COUNT_CONST, sizeof(TYPE) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(TYPE) * COUNT_CONST + COUNT_CONST, sizeof(TYPE) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(THING) * (COUNT_ID) + COUNT_ID, sizeof(THING) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(THING) * COUNT_ID + COUNT_ID, sizeof(THING) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(THING) * (COUNT_CONST) + COUNT_CONST, sizeof(THING) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(THING) * COUNT_CONST + COUNT_CONST, sizeof(THING) , ...) ) // 2-factor product, only identifiers. @@ expression HANDLE; identifier SIZE, COUNT; @@ - devm_kzalloc + devm_kcalloc (HANDLE, - SIZE * COUNT + COUNT, SIZE , ...) // 3-factor product with 1 sizeof(type) or sizeof(expression), with // redundant parens removed. @@ expression HANDLE; expression THING; identifier STRIDE, COUNT; type TYPE; @@ ( devm_kzalloc(HANDLE, - sizeof(TYPE) * (COUNT) * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | devm_kzalloc(HANDLE, - sizeof(TYPE) * (COUNT) * STRIDE + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | devm_kzalloc(HANDLE, - sizeof(TYPE) * COUNT * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | devm_kzalloc(HANDLE, - sizeof(TYPE) * COUNT * STRIDE + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | devm_kzalloc(HANDLE, - sizeof(THING) * (COUNT) * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | devm_kzalloc(HANDLE, - sizeof(THING) * (COUNT) * STRIDE + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | devm_kzalloc(HANDLE, - sizeof(THING) * COUNT * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | devm_kzalloc(HANDLE, - sizeof(THING) * COUNT * STRIDE + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) ) // 3-factor product with 2 sizeof(variable), with redundant parens removed. @@ expression HANDLE; expression THING1, THING2; identifier COUNT; type TYPE1, TYPE2; @@ ( devm_kzalloc(HANDLE, - sizeof(TYPE1) * sizeof(TYPE2) * COUNT + array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2)) , ...) | devm_kzalloc(HANDLE, - sizeof(TYPE1) * sizeof(THING2) * (COUNT) + array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2)) , ...) | devm_kzalloc(HANDLE, - sizeof(THING1) * sizeof(THING2) * COUNT + array3_size(COUNT, sizeof(THING1), sizeof(THING2)) , ...) | devm_kzalloc(HANDLE, - sizeof(THING1) * sizeof(THING2) * (COUNT) + array3_size(COUNT, sizeof(THING1), sizeof(THING2)) , ...) | devm_kzalloc(HANDLE, - sizeof(TYPE1) * sizeof(THING2) * COUNT + array3_size(COUNT, sizeof(TYPE1), sizeof(THING2)) , ...) | devm_kzalloc(HANDLE, - sizeof(TYPE1) * sizeof(THING2) * (COUNT) + array3_size(COUNT, sizeof(TYPE1), sizeof(THING2)) , ...) ) // 3-factor product, only identifiers, with redundant parens removed. @@ expression HANDLE; identifier STRIDE, SIZE, COUNT; @@ ( devm_kzalloc(HANDLE, - (COUNT) * STRIDE * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | devm_kzalloc(HANDLE, - COUNT * (STRIDE) * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | devm_kzalloc(HANDLE, - COUNT * STRIDE * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | devm_kzalloc(HANDLE, - (COUNT) * (STRIDE) * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | devm_kzalloc(HANDLE, - COUNT * (STRIDE) * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | devm_kzalloc(HANDLE, - (COUNT) * STRIDE * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | devm_kzalloc(HANDLE, - (COUNT) * (STRIDE) * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | devm_kzalloc(HANDLE, - 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 HANDLE; expression E1, E2, E3; constant C1, C2, C3; @@ ( devm_kzalloc(HANDLE, C1 * C2 * C3, ...) | devm_kzalloc(HANDLE, - (E1) * E2 * E3 + array3_size(E1, E2, E3) , ...) | devm_kzalloc(HANDLE, - (E1) * (E2) * E3 + array3_size(E1, E2, E3) , ...) | devm_kzalloc(HANDLE, - (E1) * (E2) * (E3) + array3_size(E1, E2, E3) , ...) | devm_kzalloc(HANDLE, - 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 HANDLE; expression THING, E1, E2; type TYPE; constant C1, C2, C3; @@ ( devm_kzalloc(HANDLE, sizeof(THING) * C2, ...) | devm_kzalloc(HANDLE, sizeof(TYPE) * C2, ...) | devm_kzalloc(HANDLE, C1 * C2 * C3, ...) | devm_kzalloc(HANDLE, C1 * C2, ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(TYPE) * (E2) + E2, sizeof(TYPE) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(TYPE) * E2 + E2, sizeof(TYPE) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(THING) * (E2) + E2, sizeof(THING) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(THING) * E2 + E2, sizeof(THING) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - (E1) * E2 + E1, E2 , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - (E1) * (E2) + E1, E2 , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - E1 * E2 + E1, E2 , ...) ) Signed-off-by: Kees Cook <keescook@chromium.org>
1 parent 3c4211b commit a86854d

229 files changed

Lines changed: 847 additions & 664 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.

drivers/acpi/fan.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ static int acpi_fan_get_fps(struct acpi_device *device)
298298
}
299299

300300
fan->fps_count = obj->package.count - 1; /* minus revision field */
301-
fan->fps = devm_kzalloc(&device->dev,
302-
fan->fps_count * sizeof(struct acpi_fan_fps),
301+
fan->fps = devm_kcalloc(&device->dev,
302+
fan->fps_count, sizeof(struct acpi_fan_fps),
303303
GFP_KERNEL);
304304
if (!fan->fps) {
305305
dev_err(&device->dev, "Not enough memory\n");

drivers/acpi/nfit/core.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,9 +1082,10 @@ static int __nfit_mem_init(struct acpi_nfit_desc *acpi_desc,
10821082
continue;
10831083
nfit_mem->nfit_flush = nfit_flush;
10841084
flush = nfit_flush->flush;
1085-
nfit_mem->flush_wpq = devm_kzalloc(acpi_desc->dev,
1086-
flush->hint_count
1087-
* sizeof(struct resource), GFP_KERNEL);
1085+
nfit_mem->flush_wpq = devm_kcalloc(acpi_desc->dev,
1086+
flush->hint_count,
1087+
sizeof(struct resource),
1088+
GFP_KERNEL);
10881089
if (!nfit_mem->flush_wpq)
10891090
return -ENOMEM;
10901091
for (i = 0; i < flush->hint_count; i++) {

drivers/ata/sata_mv.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4114,13 +4114,13 @@ static int mv_platform_probe(struct platform_device *pdev)
41144114

41154115
if (!host || !hpriv)
41164116
return -ENOMEM;
4117-
hpriv->port_clks = devm_kzalloc(&pdev->dev,
4118-
sizeof(struct clk *) * n_ports,
4117+
hpriv->port_clks = devm_kcalloc(&pdev->dev,
4118+
n_ports, sizeof(struct clk *),
41194119
GFP_KERNEL);
41204120
if (!hpriv->port_clks)
41214121
return -ENOMEM;
4122-
hpriv->port_phys = devm_kzalloc(&pdev->dev,
4123-
sizeof(struct phy *) * n_ports,
4122+
hpriv->port_phys = devm_kcalloc(&pdev->dev,
4123+
n_ports, sizeof(struct phy *),
41244124
GFP_KERNEL);
41254125
if (!hpriv->port_phys)
41264126
return -ENOMEM;

drivers/bus/fsl-mc/fsl-mc-allocator.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,8 @@ int fsl_mc_populate_irq_pool(struct fsl_mc_bus *mc_bus,
354354
if (error < 0)
355355
return error;
356356

357-
irq_resources = devm_kzalloc(&mc_bus_dev->dev,
358-
sizeof(*irq_resources) * irq_count,
357+
irq_resources = devm_kcalloc(&mc_bus_dev->dev,
358+
irq_count, sizeof(*irq_resources),
359359
GFP_KERNEL);
360360
if (!irq_resources) {
361361
error = -ENOMEM;
@@ -455,7 +455,7 @@ int __must_check fsl_mc_allocate_irqs(struct fsl_mc_device *mc_dev)
455455
return -ENOSPC;
456456
}
457457

458-
irqs = devm_kzalloc(&mc_dev->dev, irq_count * sizeof(irqs[0]),
458+
irqs = devm_kcalloc(&mc_dev->dev, irq_count, sizeof(irqs[0]),
459459
GFP_KERNEL);
460460
if (!irqs)
461461
return -ENOMEM;

drivers/char/tpm/tpm2-cmd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,7 @@ static int tpm2_get_cc_attrs_tbl(struct tpm_chip *chip)
980980
goto out;
981981
}
982982

983-
chip->cc_attrs_tbl = devm_kzalloc(&chip->dev, 4 * nr_commands,
983+
chip->cc_attrs_tbl = devm_kcalloc(&chip->dev, 4, nr_commands,
984984
GFP_KERNEL);
985985

986986
rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);

drivers/clk/bcm/clk-bcm2835.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ static void bcm2835_pll_debug_init(struct clk_hw *hw,
734734
const struct bcm2835_pll_data *data = pll->data;
735735
struct debugfs_reg32 *regs;
736736

737-
regs = devm_kzalloc(cprman->dev, 7 * sizeof(*regs), GFP_KERNEL);
737+
regs = devm_kcalloc(cprman->dev, 7, sizeof(*regs), GFP_KERNEL);
738738
if (!regs)
739739
return;
740740

@@ -865,7 +865,7 @@ static void bcm2835_pll_divider_debug_init(struct clk_hw *hw,
865865
const struct bcm2835_pll_divider_data *data = divider->data;
866866
struct debugfs_reg32 *regs;
867867

868-
regs = devm_kzalloc(cprman->dev, 7 * sizeof(*regs), GFP_KERNEL);
868+
regs = devm_kcalloc(cprman->dev, 7, sizeof(*regs), GFP_KERNEL);
869869
if (!regs)
870870
return;
871871

drivers/clk/ti/adpll.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,9 @@ static int ti_adpll_init_dco(struct ti_adpll_data *d)
501501
const char *postfix;
502502
int width, err;
503503

504-
d->outputs.clks = devm_kzalloc(d->dev, sizeof(struct clk *) *
504+
d->outputs.clks = devm_kcalloc(d->dev,
505505
MAX_ADPLL_OUTPUTS,
506+
sizeof(struct clk *),
506507
GFP_KERNEL);
507508
if (!d->outputs.clks)
508509
return -ENOMEM;
@@ -915,8 +916,9 @@ static int ti_adpll_probe(struct platform_device *pdev)
915916
if (err)
916917
return err;
917918

918-
d->clocks = devm_kzalloc(d->dev, sizeof(struct ti_adpll_clock) *
919+
d->clocks = devm_kcalloc(d->dev,
919920
TI_ADPLL_NR_CLOCKS,
921+
sizeof(struct ti_adpll_clock),
920922
GFP_KERNEL);
921923
if (!d->clocks)
922924
return -ENOMEM;

drivers/cpufreq/brcmstb-avs-cpufreq.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ brcm_avs_get_freq_table(struct device *dev, struct private_data *priv)
410410
if (ret)
411411
return ERR_PTR(ret);
412412

413-
table = devm_kzalloc(dev, (AVS_PSTATE_MAX + 1) * sizeof(*table),
413+
table = devm_kcalloc(dev, AVS_PSTATE_MAX + 1, sizeof(*table),
414414
GFP_KERNEL);
415415
if (!table)
416416
return ERR_PTR(-ENOMEM);

drivers/cpufreq/imx6q-cpufreq.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,8 @@ static int imx6q_cpufreq_probe(struct platform_device *pdev)
377377
}
378378

379379
/* Make imx6_soc_volt array's size same as arm opp number */
380-
imx6_soc_volt = devm_kzalloc(cpu_dev, sizeof(*imx6_soc_volt) * num, GFP_KERNEL);
380+
imx6_soc_volt = devm_kcalloc(cpu_dev, num, sizeof(*imx6_soc_volt),
381+
GFP_KERNEL);
381382
if (imx6_soc_volt == NULL) {
382383
ret = -ENOMEM;
383384
goto free_freq_table;

drivers/crypto/marvell/cesa.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ static int mv_cesa_probe(struct platform_device *pdev)
471471
sram_size = CESA_SA_MIN_SRAM_SIZE;
472472

473473
cesa->sram_size = sram_size;
474-
cesa->engines = devm_kzalloc(dev, caps->nengines * sizeof(*engines),
474+
cesa->engines = devm_kcalloc(dev, caps->nengines, sizeof(*engines),
475475
GFP_KERNEL);
476476
if (!cesa->engines)
477477
return -ENOMEM;

0 commit comments

Comments
 (0)