1
0
Fork 0

treewide: Use struct_size() for devm_kmalloc() and friends

Replaces open-coded struct size calculations with struct_size() for
devm_*, f2fs_*, and sock_* allocations. Automatically generated (and
manually adjusted) from the following Coccinelle script:

// Direct reference to struct field.
@@
identifier alloc =~ "devm_kmalloc|devm_kzalloc|sock_kmalloc|f2fs_kmalloc|f2fs_kzalloc";
expression HANDLE;
expression GFP;
identifier VAR, ELEMENT;
expression COUNT;
@@

- alloc(HANDLE, sizeof(*VAR) + COUNT * sizeof(*VAR->ELEMENT), GFP)
+ alloc(HANDLE, struct_size(VAR, ELEMENT, COUNT), GFP)

// mr = kzalloc(sizeof(*mr) + m * sizeof(mr->map[0]), GFP_KERNEL);
@@
identifier alloc =~ "devm_kmalloc|devm_kzalloc|sock_kmalloc|f2fs_kmalloc|f2fs_kzalloc";
expression HANDLE;
expression GFP;
identifier VAR, ELEMENT;
expression COUNT;
@@

- alloc(HANDLE, sizeof(*VAR) + COUNT * sizeof(VAR->ELEMENT[0]), GFP)
+ alloc(HANDLE, struct_size(VAR, ELEMENT, COUNT), GFP)

// Same pattern, but can't trivially locate the trailing element name,
// or variable name.
@@
identifier alloc =~ "devm_kmalloc|devm_kzalloc|sock_kmalloc|f2fs_kmalloc|f2fs_kzalloc";
expression HANDLE;
expression GFP;
expression SOMETHING, COUNT, ELEMENT;
@@

- alloc(HANDLE, sizeof(SOMETHING) + COUNT * sizeof(ELEMENT), GFP)
+ alloc(HANDLE, CHECKME_struct_size(&SOMETHING, ELEMENT, COUNT), GFP)

Signed-off-by: Kees Cook <keescook@chromium.org>
hifive-unleashed-5.1
Kees Cook 2018-05-08 16:08:53 -07:00
parent b4b06db115
commit 0ed2dd03b9
32 changed files with 71 additions and 71 deletions

View File

@ -501,8 +501,8 @@ int af_alg_alloc_tsgl(struct sock *sk)
sg = sgl->sg; sg = sgl->sg;
if (!sg || sgl->cur >= MAX_SGL_ENTS) { if (!sg || sgl->cur >= MAX_SGL_ENTS) {
sgl = sock_kmalloc(sk, sizeof(*sgl) + sgl = sock_kmalloc(sk,
sizeof(sgl->sg[0]) * (MAX_SGL_ENTS + 1), struct_size(sgl, sg, (MAX_SGL_ENTS + 1)),
GFP_KERNEL); GFP_KERNEL);
if (!sgl) if (!sgl)
return -ENOMEM; return -ENOMEM;

View File

@ -40,8 +40,10 @@ static int bcm2835_aux_clk_probe(struct platform_device *pdev)
if (IS_ERR(reg)) if (IS_ERR(reg))
return PTR_ERR(reg); return PTR_ERR(reg);
onecell = devm_kmalloc(dev, sizeof(*onecell) + sizeof(*onecell->hws) * onecell = devm_kmalloc(dev,
BCM2835_AUX_CLOCK_COUNT, GFP_KERNEL); struct_size(onecell, hws,
BCM2835_AUX_CLOCK_COUNT),
GFP_KERNEL);
if (!onecell) if (!onecell)
return -ENOMEM; return -ENOMEM;
onecell->num = BCM2835_AUX_CLOCK_COUNT; onecell->num = BCM2835_AUX_CLOCK_COUNT;

View File

@ -2147,8 +2147,8 @@ static int bcm2835_clk_probe(struct platform_device *pdev)
size_t i; size_t i;
int ret; int ret;
cprman = devm_kzalloc(dev, sizeof(*cprman) + cprman = devm_kzalloc(dev,
sizeof(*cprman->onecell.hws) * asize, struct_size(cprman, onecell.hws, asize),
GFP_KERNEL); GFP_KERNEL);
if (!cprman) if (!cprman)
return -ENOMEM; return -ENOMEM;

View File

@ -147,8 +147,8 @@ static int s2mps11_clk_probe(struct platform_device *pdev)
if (!s2mps11_clks) if (!s2mps11_clks)
return -ENOMEM; return -ENOMEM;
clk_data = devm_kzalloc(&pdev->dev, sizeof(*clk_data) + clk_data = devm_kzalloc(&pdev->dev,
sizeof(*clk_data->hws) * S2MPS11_CLKS_NUM, struct_size(clk_data, hws, S2MPS11_CLKS_NUM),
GFP_KERNEL); GFP_KERNEL);
if (!clk_data) if (!clk_data)
return -ENOMEM; return -ENOMEM;

View File

@ -137,8 +137,8 @@ static int scmi_clocks_probe(struct scmi_device *sdev)
return -EINVAL; return -EINVAL;
} }
clk_data = devm_kzalloc(dev, sizeof(*clk_data) + clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, count),
sizeof(*clk_data->hws) * count, GFP_KERNEL); GFP_KERNEL);
if (!clk_data) if (!clk_data)
return -ENOMEM; return -ENOMEM;

View File

@ -650,8 +650,8 @@ static int of_da8xx_usb_phy_clk_init(struct device *dev, struct regmap *regmap)
struct da8xx_usb0_clk48 *usb0; struct da8xx_usb0_clk48 *usb0;
struct da8xx_usb1_clk48 *usb1; struct da8xx_usb1_clk48 *usb1;
clk_data = devm_kzalloc(dev, sizeof(*clk_data) + 2 * clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, 2),
sizeof(*clk_data->hws), GFP_KERNEL); GFP_KERNEL);
if (!clk_data) if (!clk_data)
return -ENOMEM; return -ENOMEM;

View File

@ -667,9 +667,10 @@ static int armada_3700_periph_clock_probe(struct platform_device *pdev)
if (!driver_data) if (!driver_data)
return -ENOMEM; return -ENOMEM;
driver_data->hw_data = devm_kzalloc(dev, sizeof(*driver_data->hw_data) + driver_data->hw_data = devm_kzalloc(dev,
sizeof(*driver_data->hw_data->hws) * num_periph, struct_size(driver_data->hw_data,
GFP_KERNEL); hws, num_periph),
GFP_KERNEL);
if (!driver_data->hw_data) if (!driver_data->hw_data)
return -ENOMEM; return -ENOMEM;
driver_data->hw_data->num = num_periph; driver_data->hw_data->num = num_periph;

View File

@ -91,8 +91,8 @@ static int armada_3700_tbg_clock_probe(struct platform_device *pdev)
void __iomem *reg; void __iomem *reg;
int i, ret; int i, ret;
hw_tbg_data = devm_kzalloc(&pdev->dev, sizeof(*hw_tbg_data) hw_tbg_data = devm_kzalloc(&pdev->dev,
+ sizeof(*hw_tbg_data->hws) * NUM_TBG, struct_size(hw_tbg_data, hws, NUM_TBG),
GFP_KERNEL); GFP_KERNEL);
if (!hw_tbg_data) if (!hw_tbg_data)
return -ENOMEM; return -ENOMEM;

View File

@ -239,8 +239,7 @@ static int spmi_pmic_clkdiv_probe(struct platform_device *pdev)
if (!nclks) if (!nclks)
return -EINVAL; return -EINVAL;
cc = devm_kzalloc(dev, sizeof(*cc) + sizeof(*cc->clks) * nclks, cc = devm_kzalloc(dev, struct_size(cc, clks, nclks), GFP_KERNEL);
GFP_KERNEL);
if (!cc) if (!cc)
return -ENOMEM; return -ENOMEM;
cc->nclks = nclks; cc->nclks = nclks;

View File

@ -149,8 +149,8 @@ static int exynos_audss_clk_probe(struct platform_device *pdev)
epll = ERR_PTR(-ENODEV); epll = ERR_PTR(-ENODEV);
clk_data = devm_kzalloc(dev, clk_data = devm_kzalloc(dev,
sizeof(*clk_data) + struct_size(clk_data, hws,
sizeof(*clk_data->hws) * EXYNOS_AUDSS_MAX_CLKS, EXYNOS_AUDSS_MAX_CLKS),
GFP_KERNEL); GFP_KERNEL);
if (!clk_data) if (!clk_data)
return -ENOMEM; return -ENOMEM;

View File

@ -5505,8 +5505,8 @@ static int __init exynos5433_cmu_probe(struct platform_device *pdev)
info = of_device_get_match_data(dev); info = of_device_get_match_data(dev);
data = devm_kzalloc(dev, sizeof(*data) + data = devm_kzalloc(dev,
sizeof(*data->ctx.clk_data.hws) * info->nr_clk_ids, struct_size(data, ctx.clk_data.hws, info->nr_clk_ids),
GFP_KERNEL); GFP_KERNEL);
if (!data) if (!data)
return -ENOMEM; return -ENOMEM;

View File

@ -247,9 +247,10 @@ static int s3c24xx_dclk_probe(struct platform_device *pdev)
struct clk_hw **clk_table; struct clk_hw **clk_table;
int ret, i; int ret, i;
s3c24xx_dclk = devm_kzalloc(&pdev->dev, sizeof(*s3c24xx_dclk) + s3c24xx_dclk = devm_kzalloc(&pdev->dev,
sizeof(*s3c24xx_dclk->clk_data.hws) * DCLK_MAX_CLKS, struct_size(s3c24xx_dclk, clk_data.hws,
GFP_KERNEL); DCLK_MAX_CLKS),
GFP_KERNEL);
if (!s3c24xx_dclk) if (!s3c24xx_dclk)
return -ENOMEM; return -ENOMEM;

View File

@ -81,8 +81,7 @@ static int s5pv210_audss_clk_probe(struct platform_device *pdev)
} }
clk_data = devm_kzalloc(&pdev->dev, clk_data = devm_kzalloc(&pdev->dev,
sizeof(*clk_data) + struct_size(clk_data, hws, AUDSS_MAX_CLKS),
sizeof(*clk_data->hws) * AUDSS_MAX_CLKS,
GFP_KERNEL); GFP_KERNEL);
if (!clk_data) if (!clk_data)

View File

@ -1499,9 +1499,8 @@ static int sba_prealloc_channel_resources(struct sba_device *sba)
for (i = 0; i < sba->max_req; i++) { for (i = 0; i < sba->max_req; i++) {
req = devm_kzalloc(sba->dev, req = devm_kzalloc(sba->dev,
sizeof(*req) + struct_size(req, cmds, sba->max_cmd_per_req),
sba->max_cmd_per_req * sizeof(req->cmds[0]), GFP_KERNEL);
GFP_KERNEL);
if (!req) { if (!req) {
ret = -ENOMEM; ret = -ENOMEM;
goto fail_free_cmds_pool; goto fail_free_cmds_pool;

View File

@ -1305,8 +1305,8 @@ static int nbpf_probe(struct platform_device *pdev)
cfg = of_device_get_match_data(dev); cfg = of_device_get_match_data(dev);
num_channels = cfg->num_channels; num_channels = cfg->num_channels;
nbpf = devm_kzalloc(dev, sizeof(*nbpf) + num_channels * nbpf = devm_kzalloc(dev, struct_size(nbpf, chan, num_channels),
sizeof(nbpf->chan[0]), GFP_KERNEL); GFP_KERNEL);
if (!nbpf) if (!nbpf)
return -ENOMEM; return -ENOMEM;

View File

@ -805,8 +805,8 @@ static int sprd_dma_probe(struct platform_device *pdev)
return ret; return ret;
} }
sdev = devm_kzalloc(&pdev->dev, sizeof(*sdev) + sdev = devm_kzalloc(&pdev->dev,
sizeof(*dma_chn) * chn_count, struct_size(sdev, channels, chn_count),
GFP_KERNEL); GFP_KERNEL);
if (!sdev) if (!sdev)
return -ENOMEM; return -ENOMEM;

View File

@ -371,8 +371,7 @@ static int uniphier_gpio_probe(struct platform_device *pdev)
return ret; return ret;
nregs = uniphier_gpio_get_nbanks(ngpios) * 2 + 3; nregs = uniphier_gpio_get_nbanks(ngpios) * 2 + 3;
priv = devm_kzalloc(dev, priv = devm_kzalloc(dev, struct_size(priv, saved_vals, nregs),
sizeof(*priv) + sizeof(priv->saved_vals[0]) * nregs,
GFP_KERNEL); GFP_KERNEL);
if (!priv) if (!priv)
return -ENOMEM; return -ENOMEM;

View File

@ -62,8 +62,10 @@ static int sirf_hwspinlock_probe(struct platform_device *pdev)
if (!pdev->dev.of_node) if (!pdev->dev.of_node)
return -ENODEV; return -ENODEV;
hwspin = devm_kzalloc(&pdev->dev, sizeof(*hwspin) + hwspin = devm_kzalloc(&pdev->dev,
sizeof(*hwlock) * HW_SPINLOCK_NUMBER, GFP_KERNEL); struct_size(hwspin, bank.lock,
HW_SPINLOCK_NUMBER),
GFP_KERNEL);
if (!hwspin) if (!hwspin)
return -ENOMEM; return -ENOMEM;

View File

@ -357,8 +357,7 @@ static int cap11xx_i2c_probe(struct i2c_client *i2c_client,
} }
priv = devm_kzalloc(dev, priv = devm_kzalloc(dev,
sizeof(*priv) + struct_size(priv, keycodes, cap->num_channels),
cap->num_channels * sizeof(priv->keycodes[0]),
GFP_KERNEL); GFP_KERNEL);
if (!priv) if (!priv)
return -ENOMEM; return -ENOMEM;

View File

@ -563,8 +563,8 @@ static int pm8xxx_probe(struct platform_device *pdev)
pr_info("PMIC revision 2: %02X\n", val); pr_info("PMIC revision 2: %02X\n", val);
rev |= val << BITS_PER_BYTE; rev |= val << BITS_PER_BYTE;
chip = devm_kzalloc(&pdev->dev, sizeof(*chip) + chip = devm_kzalloc(&pdev->dev,
sizeof(chip->config[0]) * data->num_irqs, struct_size(chip, config, data->num_irqs),
GFP_KERNEL); GFP_KERNEL);
if (!chip) if (!chip)
return -ENOMEM; return -ENOMEM;

View File

@ -232,8 +232,8 @@ static int cb710_probe(struct pci_dev *pdev,
if (val & CB710_SLOT_SM) if (val & CB710_SLOT_SM)
++n; ++n;
chip = devm_kzalloc(&pdev->dev, chip = devm_kzalloc(&pdev->dev, struct_size(chip, slot, n),
sizeof(*chip) + n * sizeof(*chip->slot), GFP_KERNEL); GFP_KERNEL);
if (!chip) if (!chip)
return -ENOMEM; return -ENOMEM;

View File

@ -861,8 +861,9 @@ static int aspeed_smc_probe(struct platform_device *pdev)
return -ENODEV; return -ENODEV;
info = match->data; info = match->data;
controller = devm_kzalloc(&pdev->dev, sizeof(*controller) + controller = devm_kzalloc(&pdev->dev,
info->nce * sizeof(controller->chips[0]), GFP_KERNEL); struct_size(controller, chips, info->nce),
GFP_KERNEL);
if (!controller) if (!controller)
return -ENOMEM; return -ENOMEM;
controller->info = info; controller->info = info;

View File

@ -752,8 +752,7 @@ static int peak_pciefd_probe(struct pci_dev *pdev,
can_count = 1; can_count = 1;
/* allocate board structure object */ /* allocate board structure object */
pciefd = devm_kzalloc(&pdev->dev, sizeof(*pciefd) + pciefd = devm_kzalloc(&pdev->dev, struct_size(pciefd, can, can_count),
can_count * sizeof(*pciefd->can),
GFP_KERNEL); GFP_KERNEL);
if (!pciefd) { if (!pciefd) {
err = -ENOMEM; err = -ENOMEM;

View File

@ -483,8 +483,8 @@ static int s3c64xx_eint_gpio_init(struct samsung_pinctrl_drv_data *d)
++nr_domains; ++nr_domains;
} }
data = devm_kzalloc(dev, sizeof(*data) data = devm_kzalloc(dev, struct_size(data, domains, nr_domains),
+ nr_domains * sizeof(*data->domains), GFP_KERNEL); GFP_KERNEL);
if (!data) if (!data)
return -ENOMEM; return -ENOMEM;
data->drvdata = d; data->drvdata = d;

View File

@ -759,8 +759,7 @@ static int uniphier_pinctrl_add_reg_region(struct device *dev,
nregs = DIV_ROUND_UP(count * width, 32); nregs = DIV_ROUND_UP(count * width, 32);
region = devm_kzalloc(dev, region = devm_kzalloc(dev, struct_size(region, vals, nregs),
sizeof(*region) + sizeof(region->vals[0]) * nregs,
GFP_KERNEL); GFP_KERNEL);
if (!region) if (!region)
return -ENOMEM; return -ENOMEM;

View File

@ -409,9 +409,9 @@ static int mc13783_regulator_probe(struct platform_device *pdev)
if (num_regulators <= 0) if (num_regulators <= 0)
return -EINVAL; return -EINVAL;
priv = devm_kzalloc(&pdev->dev, sizeof(*priv) + priv = devm_kzalloc(&pdev->dev,
num_regulators * sizeof(priv->regulators[0]), struct_size(priv, regulators, num_regulators),
GFP_KERNEL); GFP_KERNEL);
if (!priv) if (!priv)
return -ENOMEM; return -ENOMEM;

View File

@ -547,9 +547,9 @@ static int mc13892_regulator_probe(struct platform_device *pdev)
if (num_regulators <= 0) if (num_regulators <= 0)
return -EINVAL; return -EINVAL;
priv = devm_kzalloc(&pdev->dev, sizeof(*priv) + priv = devm_kzalloc(&pdev->dev,
num_regulators * sizeof(priv->regulators[0]), struct_size(priv, regulators, num_regulators),
GFP_KERNEL); GFP_KERNEL);
if (!priv) if (!priv)
return -ENOMEM; return -ENOMEM;

View File

@ -317,10 +317,10 @@ static int ac100_rtc_register_clks(struct ac100_rtc_dev *chip)
const char *parents[2] = {AC100_RTC_32K_NAME}; const char *parents[2] = {AC100_RTC_32K_NAME};
int i, ret; int i, ret;
chip->clk_data = devm_kzalloc(chip->dev, sizeof(*chip->clk_data) + chip->clk_data = devm_kzalloc(chip->dev,
sizeof(*chip->clk_data->hws) * struct_size(chip->clk_data, hws,
AC100_CLKOUT_NUM, AC100_CLKOUT_NUM),
GFP_KERNEL); GFP_KERNEL);
if (!chip->clk_data) if (!chip->clk_data)
return -ENOMEM; return -ENOMEM;

View File

@ -117,8 +117,8 @@ static int owl_sps_probe(struct platform_device *pdev)
sps_info = match->data; sps_info = match->data;
sps = devm_kzalloc(&pdev->dev, sizeof(*sps) + sps = devm_kzalloc(&pdev->dev,
sps_info->num_domains * sizeof(sps->domains[0]), struct_size(sps, domains, sps_info->num_domains),
GFP_KERNEL); GFP_KERNEL);
if (!sps) if (!sps)
return -ENOMEM; return -ENOMEM;

View File

@ -626,8 +626,7 @@ static int rockchip_pm_domain_probe(struct platform_device *pdev)
pmu_info = match->data; pmu_info = match->data;
pmu = devm_kzalloc(dev, pmu = devm_kzalloc(dev,
sizeof(*pmu) + struct_size(pmu, domains, pmu_info->num_domains),
pmu_info->num_domains * sizeof(pmu->domains[0]),
GFP_KERNEL); GFP_KERNEL);
if (!pmu) if (!pmu)
return -ENOMEM; return -ENOMEM;

View File

@ -112,7 +112,6 @@ static int tsens_probe(struct platform_device *pdev)
int ret, i; int ret, i;
struct device *dev; struct device *dev;
struct device_node *np; struct device_node *np;
struct tsens_sensor *s;
struct tsens_device *tmdev; struct tsens_device *tmdev;
const struct tsens_data *data; const struct tsens_data *data;
const struct of_device_id *id; const struct of_device_id *id;
@ -135,8 +134,9 @@ static int tsens_probe(struct platform_device *pdev)
return -EINVAL; return -EINVAL;
} }
tmdev = devm_kzalloc(dev, sizeof(*tmdev) + tmdev = devm_kzalloc(dev,
data->num_sensors * sizeof(*s), GFP_KERNEL); struct_size(tmdev, sensor, data->num_sensors),
GFP_KERNEL);
if (!tmdev) if (!tmdev)
return -ENOMEM; return -ENOMEM;

View File

@ -147,7 +147,8 @@ static struct apq8016_sbc_data *apq8016_sbc_parse_of(struct snd_soc_card *card)
num_links = of_get_child_count(node); num_links = of_get_child_count(node);
/* Allocate the private data and the DAI link array */ /* Allocate the private data and the DAI link array */
data = devm_kzalloc(dev, sizeof(*data) + sizeof(*link) * num_links, data = devm_kzalloc(dev,
struct_size(data, dai_link, num_links),
GFP_KERNEL); GFP_KERNEL);
if (!data) if (!data)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);