treewide: Use struct_size() for kmalloc()-family

One of the more common cases of allocation size calculations is finding
the size of a structure that has a zero-sized array at the end, along
with memory for some number of elements for that array. For example:

struct foo {
    int stuff;
    void *entry[];
};

instance = kmalloc(sizeof(struct foo) + sizeof(void *) * count, GFP_KERNEL);

Instead of leaving these open-coded and prone to type mistakes, we can
now use the new struct_size() helper:

instance = kmalloc(struct_size(instance, entry, count), GFP_KERNEL);

This patch makes the changes for kmalloc()-family (and kvmalloc()-family)
uses. It was done via automatic conversion with manual review for the
"CHECKME" non-standard cases noted below, using the following Coccinelle
script:

// pkey_cache = kmalloc(sizeof *pkey_cache + tprops->pkey_tbl_len *
//                      sizeof *pkey_cache->table, GFP_KERNEL);
@@
identifier alloc =~ "kmalloc|kzalloc|kvmalloc|kvzalloc";
expression GFP;
identifier VAR, ELEMENT;
expression COUNT;
@@

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

// mr = kzalloc(sizeof(*mr) + m * sizeof(mr->map[0]), GFP_KERNEL);
@@
identifier alloc =~ "kmalloc|kzalloc|kvmalloc|kvzalloc";
expression GFP;
identifier VAR, ELEMENT;
expression COUNT;
@@

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

// Same pattern, but can't trivially locate the trailing element name,
// or variable name.
@@
identifier alloc =~ "kmalloc|kzalloc|kvmalloc|kvzalloc";
expression GFP;
expression SOMETHING, COUNT, ELEMENT;
@@

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

Signed-off-by: Kees Cook <keescook@chromium.org>
This commit is contained in:
Kees Cook 2018-05-08 13:45:50 -07:00
parent 2509b561f7
commit acafe7e302
53 changed files with 89 additions and 116 deletions

View File

@ -197,8 +197,8 @@ void __init iproc_asiu_setup(struct device_node *node,
if (WARN_ON(!asiu)) if (WARN_ON(!asiu))
return; return;
asiu->clk_data = kzalloc(sizeof(*asiu->clk_data->hws) * num_clks + asiu->clk_data = kzalloc(struct_size(asiu->clk_data, hws, num_clks),
sizeof(*asiu->clk_data), GFP_KERNEL); GFP_KERNEL);
if (WARN_ON(!asiu->clk_data)) if (WARN_ON(!asiu->clk_data))
goto err_clks; goto err_clks;
asiu->clk_data->num = num_clks; asiu->clk_data->num = num_clks;

View File

@ -744,8 +744,7 @@ void iproc_pll_clk_setup(struct device_node *node,
if (WARN_ON(!pll)) if (WARN_ON(!pll))
return; return;
clk_data = kzalloc(sizeof(*clk_data->hws) * num_clks + clk_data = kzalloc(struct_size(clk_data, hws, num_clks), GFP_KERNEL);
sizeof(*clk_data), GFP_KERNEL);
if (WARN_ON(!clk_data)) if (WARN_ON(!clk_data))
goto err_clk_data; goto err_clk_data;
clk_data->num = num_clks; clk_data->num = num_clks;

View File

@ -509,8 +509,7 @@ static void __init berlin2_clock_setup(struct device_node *np)
u8 avpll_flags = 0; u8 avpll_flags = 0;
int n, ret; int n, ret;
clk_data = kzalloc(sizeof(*clk_data) + clk_data = kzalloc(struct_size(clk_data, hws, MAX_CLKS), GFP_KERNEL);
sizeof(*clk_data->hws) * MAX_CLKS, GFP_KERNEL);
if (!clk_data) if (!clk_data)
return; return;
clk_data->num = MAX_CLKS; clk_data->num = MAX_CLKS;

View File

@ -295,8 +295,7 @@ static void __init berlin2q_clock_setup(struct device_node *np)
struct clk_hw **hws; struct clk_hw **hws;
int n, ret; int n, ret;
clk_data = kzalloc(sizeof(*clk_data) + clk_data = kzalloc(struct_size(clk_data, hws, MAX_CLKS), GFP_KERNEL);
sizeof(*clk_data->hws) * MAX_CLKS, GFP_KERNEL);
if (!clk_data) if (!clk_data)
return; return;
clk_data->num = MAX_CLKS; clk_data->num = MAX_CLKS;

View File

@ -273,8 +273,7 @@ static void __init asm9260_acc_init(struct device_node *np)
int n; int n;
u32 accuracy = 0; u32 accuracy = 0;
clk_data = kzalloc(sizeof(*clk_data) + clk_data = kzalloc(struct_size(clk_data, hws, MAX_CLKS), GFP_KERNEL);
sizeof(*clk_data->hws) * MAX_CLKS, GFP_KERNEL);
if (!clk_data) if (!clk_data)
return; return;
clk_data->num = MAX_CLKS; clk_data->num = MAX_CLKS;

View File

@ -627,9 +627,9 @@ static void __init aspeed_cc_init(struct device_node *np)
if (!scu_base) if (!scu_base)
return; return;
aspeed_clk_data = kzalloc(sizeof(*aspeed_clk_data) + aspeed_clk_data = kzalloc(struct_size(aspeed_clk_data, hws,
sizeof(*aspeed_clk_data->hws) * ASPEED_NUM_CLKS, ASPEED_NUM_CLKS),
GFP_KERNEL); GFP_KERNEL);
if (!aspeed_clk_data) if (!aspeed_clk_data)
return; return;

View File

@ -54,9 +54,9 @@ static struct clps711x_clk * __init _clps711x_clk_init(void __iomem *base,
if (!base) if (!base)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
clps711x_clk = kzalloc(sizeof(*clps711x_clk) + clps711x_clk = kzalloc(struct_size(clps711x_clk, clk_data.hws,
sizeof(*clps711x_clk->clk_data.hws) * CLPS711X_CLK_MAX, CLPS711X_CLK_MAX),
GFP_KERNEL); GFP_KERNEL);
if (!clps711x_clk) if (!clps711x_clk)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);

View File

@ -25,8 +25,8 @@ static void __init efm32gg_cmu_init(struct device_node *np)
void __iomem *base; void __iomem *base;
struct clk_hw **hws; struct clk_hw **hws;
clk_data = kzalloc(sizeof(*clk_data) + clk_data = kzalloc(struct_size(clk_data, hws, CMU_MAX_CLKS),
sizeof(*clk_data->hws) * CMU_MAX_CLKS, GFP_KERNEL); GFP_KERNEL);
if (!clk_data) if (!clk_data)
return; return;

View File

@ -399,9 +399,9 @@ static void __init gemini_cc_init(struct device_node *np)
int ret; int ret;
int i; int i;
gemini_clk_data = kzalloc(sizeof(*gemini_clk_data) + gemini_clk_data = kzalloc(struct_size(gemini_clk_data, hws,
sizeof(*gemini_clk_data->hws) * GEMINI_NUM_CLKS, GEMINI_NUM_CLKS),
GFP_KERNEL); GFP_KERNEL);
if (!gemini_clk_data) if (!gemini_clk_data)
return; return;

View File

@ -1201,9 +1201,8 @@ static void __init stm32h7_rcc_init(struct device_node *np)
const char *hse_clk, *lse_clk, *i2s_clk; const char *hse_clk, *lse_clk, *i2s_clk;
struct regmap *pdrm; struct regmap *pdrm;
clk_data = kzalloc(sizeof(*clk_data) + clk_data = kzalloc(struct_size(clk_data, hws, STM32H7_MAX_CLKS),
sizeof(*clk_data->hws) * STM32H7_MAX_CLKS, GFP_KERNEL);
GFP_KERNEL);
if (!clk_data) if (!clk_data)
return; return;

View File

@ -2060,9 +2060,8 @@ static int stm32_rcc_init(struct device_node *np,
max_binding = data->maxbinding; max_binding = data->maxbinding;
clk_data = kzalloc(sizeof(*clk_data) + clk_data = kzalloc(struct_size(clk_data, hws, max_binding),
sizeof(*clk_data->hws) * max_binding, GFP_KERNEL);
GFP_KERNEL);
if (!clk_data) if (!clk_data)
return -ENOMEM; return -ENOMEM;

View File

@ -61,8 +61,7 @@ static void __init exynos_clkout_init(struct device_node *node, u32 mux_mask)
int ret; int ret;
int i; int i;
clkout = kzalloc(sizeof(*clkout) + clkout = kzalloc(struct_size(clkout, data.hws, EXYNOS_CLKOUT_NR_CLKS),
sizeof(*clkout->data.hws) * EXYNOS_CLKOUT_NR_CLKS,
GFP_KERNEL); GFP_KERNEL);
if (!clkout) if (!clkout)
return; return;

View File

@ -594,7 +594,7 @@ struct dev_dax *devm_create_dev_dax(struct dax_region *dax_region,
if (!count) if (!count)
return ERR_PTR(-EINVAL); return ERR_PTR(-EINVAL);
dev_dax = kzalloc(sizeof(*dev_dax) + sizeof(*res) * count, GFP_KERNEL); dev_dax = kzalloc(struct_size(dev_dax, res, count), GFP_KERNEL);
if (!dev_dax) if (!dev_dax)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);

View File

@ -1074,8 +1074,7 @@ static struct dma_async_tx_descriptor *edma_prep_slave_sg(
return NULL; return NULL;
} }
edesc = kzalloc(sizeof(*edesc) + sg_len * sizeof(edesc->pset[0]), edesc = kzalloc(struct_size(edesc, pset, sg_len), GFP_ATOMIC);
GFP_ATOMIC);
if (!edesc) if (!edesc)
return NULL; return NULL;
@ -1192,8 +1191,7 @@ static struct dma_async_tx_descriptor *edma_prep_dma_memcpy(
nslots = 2; nslots = 2;
} }
edesc = kzalloc(sizeof(*edesc) + nslots * sizeof(edesc->pset[0]), edesc = kzalloc(struct_size(edesc, pset, nslots), GFP_ATOMIC);
GFP_ATOMIC);
if (!edesc) if (!edesc)
return NULL; return NULL;
@ -1315,8 +1313,7 @@ static struct dma_async_tx_descriptor *edma_prep_dma_cyclic(
} }
} }
edesc = kzalloc(sizeof(*edesc) + nslots * sizeof(edesc->pset[0]), edesc = kzalloc(struct_size(edesc, pset, nslots), GFP_ATOMIC);
GFP_ATOMIC);
if (!edesc) if (!edesc)
return NULL; return NULL;

View File

@ -309,7 +309,7 @@ static struct dma_async_tx_descriptor *moxart_prep_slave_sg(
return NULL; return NULL;
} }
d = kzalloc(sizeof(*d) + sg_len * sizeof(d->sg[0]), GFP_ATOMIC); d = kzalloc(struct_size(d, sg, sg_len), GFP_ATOMIC);
if (!d) if (!d)
return NULL; return NULL;

View File

@ -917,7 +917,7 @@ static struct dma_async_tx_descriptor *omap_dma_prep_slave_sg(
} }
/* Now allocate and setup the descriptor. */ /* Now allocate and setup the descriptor. */
d = kzalloc(sizeof(*d) + sglen * sizeof(d->sg[0]), GFP_ATOMIC); d = kzalloc(struct_size(d, sg, sglen), GFP_ATOMIC);
if (!d) if (!d)
return NULL; return NULL;

View File

@ -557,7 +557,7 @@ static struct dma_async_tx_descriptor *sa11x0_dma_prep_slave_sg(
} }
} }
txd = kzalloc(sizeof(*txd) + j * sizeof(txd->sg[0]), GFP_ATOMIC); txd = kzalloc(struct_size(txd, sg, j), GFP_ATOMIC);
if (!txd) { if (!txd) {
dev_dbg(chan->device->dev, "vchan %p: kzalloc failed\n", &c->vc); dev_dbg(chan->device->dev, "vchan %p: kzalloc failed\n", &c->vc);
return NULL; return NULL;
@ -627,7 +627,7 @@ static struct dma_async_tx_descriptor *sa11x0_dma_prep_dma_cyclic(
if (sglen == 0) if (sglen == 0)
return NULL; return NULL;
txd = kzalloc(sizeof(*txd) + sglen * sizeof(txd->sg[0]), GFP_ATOMIC); txd = kzalloc(struct_size(txd, sg, sglen), GFP_ATOMIC);
if (!txd) { if (!txd) {
dev_dbg(chan->device->dev, "vchan %p: kzalloc failed\n", &c->vc); dev_dbg(chan->device->dev, "vchan %p: kzalloc failed\n", &c->vc);
return NULL; return NULL;

View File

@ -269,7 +269,7 @@ static int usb_dmac_desc_alloc(struct usb_dmac_chan *chan, unsigned int sg_len,
struct usb_dmac_desc *desc; struct usb_dmac_desc *desc;
unsigned long flags; unsigned long flags;
desc = kzalloc(sizeof(*desc) + sg_len * sizeof(desc->sg[0]), gfp); desc = kzalloc(struct_size(desc, sg, sg_len), gfp);
if (!desc) if (!desc)
return -ENOMEM; return -ENOMEM;

View File

@ -112,8 +112,7 @@ static struct fw_node *fw_node_create(u32 sid, int port_count, int color)
{ {
struct fw_node *node; struct fw_node *node;
node = kzalloc(sizeof(*node) + port_count * sizeof(node->ports[0]), node = kzalloc(struct_size(node, ports, port_count), GFP_ATOMIC);
GFP_ATOMIC);
if (node == NULL) if (node == NULL)
return NULL; return NULL;

View File

@ -4022,8 +4022,7 @@ struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
if (count < 0) if (count < 0)
return ERR_PTR(count); return ERR_PTR(count);
descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count, descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
GFP_KERNEL);
if (!descs) if (!descs)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);

View File

@ -779,8 +779,8 @@ nvkm_perfdom_new(struct nvkm_pm *pm, const char *name, u32 mask,
sdom = spec; sdom = spec;
while (sdom->signal_nr) { while (sdom->signal_nr) {
dom = kzalloc(sizeof(*dom) + sdom->signal_nr * dom = kzalloc(struct_size(dom, signal, sdom->signal_nr),
sizeof(*dom->signal), GFP_KERNEL); GFP_KERNEL);
if (!dom) if (!dom)
return -ENOMEM; return -ENOMEM;

View File

@ -132,7 +132,7 @@ static int omap_hwspinlock_probe(struct platform_device *pdev)
num_locks = i * 32; /* actual number of locks in this device */ num_locks = i * 32; /* actual number of locks in this device */
bank = kzalloc(sizeof(*bank) + num_locks * sizeof(*hwlock), GFP_KERNEL); bank = kzalloc(struct_size(bank, lock, num_locks), GFP_KERNEL);
if (!bank) { if (!bank) {
ret = -ENOMEM; ret = -ENOMEM;
goto iounmap_base; goto iounmap_base;

View File

@ -119,7 +119,7 @@ static int u8500_hsem_probe(struct platform_device *pdev)
/* clear all interrupts */ /* clear all interrupts */
writel(0xFFFF, io_base + HSEM_ICRALL); writel(0xFFFF, io_base + HSEM_ICRALL);
bank = kzalloc(sizeof(*bank) + num_locks * sizeof(*hwlock), GFP_KERNEL); bank = kzalloc(struct_size(bank, lock, num_locks), GFP_KERNEL);
if (!bank) { if (!bank) {
ret = -ENOMEM; ret = -ENOMEM;
goto iounmap_base; goto iounmap_base;

View File

@ -1157,8 +1157,9 @@ static void ib_cache_update(struct ib_device *device,
goto err; goto err;
} }
pkey_cache = kmalloc(sizeof *pkey_cache + tprops->pkey_tbl_len * pkey_cache = kmalloc(struct_size(pkey_cache, table,
sizeof *pkey_cache->table, GFP_KERNEL); tprops->pkey_tbl_len),
GFP_KERNEL);
if (!pkey_cache) if (!pkey_cache)
goto err; goto err;

View File

@ -4298,8 +4298,8 @@ static void cm_add_one(struct ib_device *ib_device)
int count = 0; int count = 0;
u8 i; u8 i;
cm_dev = kzalloc(sizeof(*cm_dev) + sizeof(*port) * cm_dev = kzalloc(struct_size(cm_dev, port, ib_device->phys_port_cnt),
ib_device->phys_port_cnt, GFP_KERNEL); GFP_KERNEL);
if (!cm_dev) if (!cm_dev)
return; return;

View File

@ -813,7 +813,7 @@ static void mcast_add_one(struct ib_device *device)
int i; int i;
int count = 0; int count = 0;
dev = kmalloc(sizeof *dev + device->phys_port_cnt * sizeof *port, dev = kmalloc(struct_size(dev, port, device->phys_port_cnt),
GFP_KERNEL); GFP_KERNEL);
if (!dev) if (!dev)
return; return;

View File

@ -2756,8 +2756,8 @@ static struct ib_uflow_resources *flow_resources_alloc(size_t num_specs)
struct ib_uflow_resources *resources; struct ib_uflow_resources *resources;
resources = resources =
kmalloc(sizeof(*resources) + kmalloc(struct_size(resources, collection, num_specs),
num_specs * sizeof(*resources->collection), GFP_KERNEL); GFP_KERNEL);
if (!resources) if (!resources)
return NULL; return NULL;

View File

@ -297,8 +297,7 @@ static struct uverbs_method_spec *build_method_with_attrs(const struct uverbs_me
if (max_attr_buckets >= 0) if (max_attr_buckets >= 0)
num_attr_buckets = max_attr_buckets + 1; num_attr_buckets = max_attr_buckets + 1;
method = kzalloc(sizeof(*method) + method = kzalloc(struct_size(method, attr_buckets, num_attr_buckets),
num_attr_buckets * sizeof(*method->attr_buckets),
GFP_KERNEL); GFP_KERNEL);
if (!method) if (!method)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
@ -446,9 +445,9 @@ static struct uverbs_object_spec *build_object_with_methods(const struct uverbs_
if (max_method_buckets >= 0) if (max_method_buckets >= 0)
num_method_buckets = max_method_buckets + 1; num_method_buckets = max_method_buckets + 1;
object = kzalloc(sizeof(*object) + object = kzalloc(struct_size(object, method_buckets,
num_method_buckets * num_method_buckets),
sizeof(*object->method_buckets), GFP_KERNEL); GFP_KERNEL);
if (!object) if (!object)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
@ -469,8 +468,8 @@ static struct uverbs_object_spec *build_object_with_methods(const struct uverbs_
if (methods_max_bucket < 0) if (methods_max_bucket < 0)
continue; continue;
hash = kzalloc(sizeof(*hash) + hash = kzalloc(struct_size(hash, methods,
sizeof(*hash->methods) * (methods_max_bucket + 1), methods_max_bucket + 1),
GFP_KERNEL); GFP_KERNEL);
if (!hash) { if (!hash) {
res = -ENOMEM; res = -ENOMEM;
@ -579,8 +578,8 @@ struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
if (max_object_buckets >= 0) if (max_object_buckets >= 0)
num_objects_buckets = max_object_buckets + 1; num_objects_buckets = max_object_buckets + 1;
root_spec = kzalloc(sizeof(*root_spec) + root_spec = kzalloc(struct_size(root_spec, object_buckets,
num_objects_buckets * sizeof(*root_spec->object_buckets), num_objects_buckets),
GFP_KERNEL); GFP_KERNEL);
if (!root_spec) if (!root_spec)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
@ -603,8 +602,8 @@ struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
if (objects_max_bucket < 0) if (objects_max_bucket < 0)
continue; continue;
hash = kzalloc(sizeof(*hash) + hash = kzalloc(struct_size(hash, objects,
sizeof(*hash->objects) * (objects_max_bucket + 1), objects_max_bucket + 1),
GFP_KERNEL); GFP_KERNEL);
if (!hash) { if (!hash) {
res = -ENOMEM; res = -ENOMEM;

View File

@ -367,7 +367,7 @@ struct mthca_icm_table *mthca_alloc_icm_table(struct mthca_dev *dev,
obj_per_chunk = MTHCA_TABLE_CHUNK_SIZE / obj_size; obj_per_chunk = MTHCA_TABLE_CHUNK_SIZE / obj_size;
num_icm = DIV_ROUND_UP(nobj, obj_per_chunk); num_icm = DIV_ROUND_UP(nobj, obj_per_chunk);
table = kmalloc(sizeof *table + num_icm * sizeof *table->icm, GFP_KERNEL); table = kmalloc(struct_size(table, icm, num_icm), GFP_KERNEL);
if (!table) if (!table)
return NULL; return NULL;
@ -529,7 +529,7 @@ struct mthca_user_db_table *mthca_init_user_db_tab(struct mthca_dev *dev)
return NULL; return NULL;
npages = dev->uar_table.uarc_size / MTHCA_ICM_PAGE_SIZE; npages = dev->uar_table.uarc_size / MTHCA_ICM_PAGE_SIZE;
db_tab = kmalloc(sizeof *db_tab + npages * sizeof *db_tab->page, GFP_KERNEL); db_tab = kmalloc(struct_size(db_tab, page, npages), GFP_KERNEL);
if (!db_tab) if (!db_tab)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);

View File

@ -283,7 +283,7 @@ static struct rvt_mr *__rvt_alloc_mr(int count, struct ib_pd *pd)
/* Allocate struct plus pointers to first level page tables. */ /* Allocate struct plus pointers to first level page tables. */
m = (count + RVT_SEGSZ - 1) / RVT_SEGSZ; m = (count + RVT_SEGSZ - 1) / RVT_SEGSZ;
mr = kzalloc(sizeof(*mr) + m * sizeof(mr->mr.map[0]), GFP_KERNEL); mr = kzalloc(struct_size(mr, mr.map, m), GFP_KERNEL);
if (!mr) if (!mr)
goto bail; goto bail;
@ -730,7 +730,7 @@ struct ib_fmr *rvt_alloc_fmr(struct ib_pd *pd, int mr_access_flags,
/* Allocate struct plus pointers to first level page tables. */ /* Allocate struct plus pointers to first level page tables. */
m = (fmr_attr->max_pages + RVT_SEGSZ - 1) / RVT_SEGSZ; m = (fmr_attr->max_pages + RVT_SEGSZ - 1) / RVT_SEGSZ;
fmr = kzalloc(sizeof(*fmr) + m * sizeof(fmr->mr.map[0]), GFP_KERNEL); fmr = kzalloc(struct_size(fmr, mr.map, m), GFP_KERNEL);
if (!fmr) if (!fmr)
goto bail; goto bail;

View File

@ -98,8 +98,7 @@ static int input_leds_connect(struct input_handler *handler,
if (!num_leds) if (!num_leds)
return -ENXIO; return -ENXIO;
leds = kzalloc(sizeof(*leds) + num_leds * sizeof(*leds->leds), leds = kzalloc(struct_size(leds, leds, num_leds), GFP_KERNEL);
GFP_KERNEL);
if (!leds) if (!leds)
return -ENOMEM; return -ENOMEM;

View File

@ -49,7 +49,7 @@ int input_mt_init_slots(struct input_dev *dev, unsigned int num_slots,
if (mt) if (mt)
return mt->num_slots != num_slots ? -EINVAL : 0; return mt->num_slots != num_slots ? -EINVAL : 0;
mt = kzalloc(sizeof(*mt) + num_slots * sizeof(*mt->slots), GFP_KERNEL); mt = kzalloc(struct_size(mt, slots, num_slots), GFP_KERNEL);
if (!mt) if (!mt)
goto err_mem; goto err_mem;

View File

@ -756,7 +756,7 @@ static struct raid_set *raid_set_alloc(struct dm_target *ti, struct raid_type *r
return ERR_PTR(-EINVAL); return ERR_PTR(-EINVAL);
} }
rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL); rs = kzalloc(struct_size(rs, dev, raid_devs), GFP_KERNEL);
if (!rs) { if (!rs) {
ti->error = "Cannot allocate raid context"; ti->error = "Cannot allocate raid context";
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);

View File

@ -182,8 +182,7 @@ static struct regmap *vexpress_syscfg_regmap_init(struct device *dev,
val = energy_quirk; val = energy_quirk;
} }
func = kzalloc(sizeof(*func) + sizeof(*func->template) * num, func = kzalloc(struct_size(func, template, num), GFP_KERNEL);
GFP_KERNEL);
if (!func) if (!func)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);

View File

@ -494,7 +494,7 @@ static int add_res_tree(struct mlx5_core_dev *dev, enum dbg_rsc_type type,
int err; int err;
int i; int i;
d = kzalloc(sizeof(*d) + nfile * sizeof(d->fields[0]), GFP_KERNEL); d = kzalloc(struct_size(d, fields, nfile), GFP_KERNEL);
if (!d) if (!d)
return -ENOMEM; return -ENOMEM;

View File

@ -1191,8 +1191,7 @@ static struct mlx5_flow_handle *alloc_handle(int num_rules)
{ {
struct mlx5_flow_handle *handle; struct mlx5_flow_handle *handle;
handle = kzalloc(sizeof(*handle) + sizeof(handle->rule[0]) * handle = kzalloc(struct_size(handle, rule, num_rules), GFP_KERNEL);
num_rules, GFP_KERNEL);
if (!handle) if (!handle)
return NULL; return NULL;

View File

@ -2987,9 +2987,8 @@ static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
mvmsta = iwl_mvm_sta_from_mac80211(sta); mvmsta = iwl_mvm_sta_from_mac80211(sta);
WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx])); WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
ptk_pn = kzalloc(sizeof(*ptk_pn) + ptk_pn = kzalloc(struct_size(ptk_pn, q,
mvm->trans->num_rx_queues * mvm->trans->num_rx_queues),
sizeof(ptk_pn->q[0]),
GFP_KERNEL); GFP_KERNEL);
if (!ptk_pn) { if (!ptk_pn) {
ret = -ENOMEM; ret = -ENOMEM;

View File

@ -236,8 +236,7 @@ int mt76_rx_aggr_start(struct mt76_dev *dev, struct mt76_wcid *wcid, u8 tidno,
mt76_rx_aggr_stop(dev, wcid, tidno); mt76_rx_aggr_stop(dev, wcid, tidno);
tid = kzalloc(sizeof(*tid) + size * sizeof(tid->reorder_buf[0]), tid = kzalloc(struct_size(tid, reorder_buf, size), GFP_KERNEL);
GFP_KERNEL);
if (!tid) if (!tid)
return -ENOMEM; return -ENOMEM;

View File

@ -730,8 +730,7 @@ of_reset_control_array_get(struct device_node *np, bool shared, bool optional)
if (num < 0) if (num < 0)
return optional ? NULL : ERR_PTR(num); return optional ? NULL : ERR_PTR(num);
resets = kzalloc(sizeof(*resets) + sizeof(resets->rstc[0]) * num, resets = kzalloc(struct_size(resets, rstc, num), GFP_KERNEL);
GFP_KERNEL);
if (!resets) if (!resets)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);

View File

@ -326,8 +326,7 @@ int ccwgroup_create_dev(struct device *parent, struct ccwgroup_driver *gdrv,
if (num_devices < 1) if (num_devices < 1)
return -EINVAL; return -EINVAL;
gdev = kzalloc(sizeof(*gdev) + num_devices * sizeof(gdev->cdev[0]), gdev = kzalloc(struct_size(gdev, cdev, num_devices), GFP_KERNEL);
GFP_KERNEL);
if (!gdev) if (!gdev)
return -ENOMEM; return -ENOMEM;

View File

@ -94,8 +94,8 @@ struct gb_module *gb_module_create(struct gb_host_device *hd, u8 module_id,
struct gb_module *module; struct gb_module *module;
int i; int i;
module = kzalloc(sizeof(*module) + num_interfaces * sizeof(intf), module = kzalloc(struct_size(module, interfaces, num_interfaces),
GFP_KERNEL); GFP_KERNEL);
if (!module) if (!module)
return NULL; return NULL;

View File

@ -1287,9 +1287,8 @@ static struct usb_function *f_midi_alloc(struct usb_function_instance *fi)
} }
/* allocate and initialize one new instance */ /* allocate and initialize one new instance */
midi = kzalloc( midi = kzalloc(struct_size(midi, in_ports_array, opts->in_ports),
sizeof(*midi) + opts->in_ports * sizeof(*midi->in_ports_array), GFP_KERNEL);
GFP_KERNEL);
if (!midi) { if (!midi) {
status = -ENOMEM; status = -ENOMEM;
goto setup_fail; goto setup_fail;

View File

@ -136,8 +136,7 @@ static int __init amiga_zorro_probe(struct platform_device *pdev)
int error; int error;
/* Initialize the Zorro bus */ /* Initialize the Zorro bus */
bus = kzalloc(sizeof(*bus) + bus = kzalloc(struct_size(bus, devices, zorro_num_autocon),
zorro_num_autocon * sizeof(bus->devices[0]),
GFP_KERNEL); GFP_KERNEL);
if (!bus) if (!bus)
return -ENOMEM; return -ENOMEM;

View File

@ -43,8 +43,7 @@ struct afs_addr_list *afs_alloc_addrlist(unsigned int nr,
_enter("%u,%u,%u", nr, service, port); _enter("%u,%u,%u", nr, service, port);
alist = kzalloc(sizeof(*alist) + sizeof(alist->addrs[0]) * nr, alist = kzalloc(struct_size(alist, addrs, nr), GFP_KERNEL);
GFP_KERNEL);
if (!alist) if (!alist)
return NULL; return NULL;

View File

@ -4775,8 +4775,8 @@ static struct cgroup *cgroup_create(struct cgroup *parent)
int ret; int ret;
/* allocate the cgroup and its ID, 0 is reserved for the root */ /* allocate the cgroup and its ID, 0 is reserved for the root */
cgrp = kzalloc(sizeof(*cgrp) + cgrp = kzalloc(struct_size(cgrp, ancestor_ids, (level + 1)),
sizeof(cgrp->ancestor_ids[0]) * (level + 1), GFP_KERNEL); GFP_KERNEL);
if (!cgrp) if (!cgrp)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);

View File

@ -1604,8 +1604,7 @@ static void add_notes_attrs(struct module *mod, const struct load_info *info)
if (notes == 0) if (notes == 0)
return; return;
notes_attrs = kzalloc(sizeof(*notes_attrs) notes_attrs = kzalloc(struct_size(notes_attrs, attrs, notes),
+ notes * sizeof(notes_attrs->attrs[0]),
GFP_KERNEL); GFP_KERNEL);
if (notes_attrs == NULL) if (notes_attrs == NULL)
return; return;

View File

@ -3700,8 +3700,7 @@ apply_wqattrs_prepare(struct workqueue_struct *wq,
lockdep_assert_held(&wq_pool_mutex); lockdep_assert_held(&wq_pool_mutex);
ctx = kzalloc(sizeof(*ctx) + nr_node_ids * sizeof(ctx->pwq_tbl[0]), ctx = kzalloc(struct_size(ctx, pwq_tbl, nr_node_ids), GFP_KERNEL);
GFP_KERNEL);
new_attrs = alloc_workqueue_attrs(GFP_KERNEL); new_attrs = alloc_workqueue_attrs(GFP_KERNEL);
tmp_attrs = alloc_workqueue_attrs(GFP_KERNEL); tmp_attrs = alloc_workqueue_attrs(GFP_KERNEL);

View File

@ -62,7 +62,7 @@ struct ceph_monmap *ceph_monmap_decode(void *p, void *end)
if (num_mon > CEPH_MAX_MON) if (num_mon > CEPH_MAX_MON)
goto bad; goto bad;
m = kmalloc(sizeof(*m) + sizeof(m->mon_inst[0])*num_mon, GFP_NOFS); m = kmalloc(struct_size(m, mon_inst, num_mon), GFP_NOFS);
if (m == NULL) if (m == NULL)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
m->fsid = fsid; m->fsid = fsid;
@ -1000,8 +1000,7 @@ static int build_initial_monmap(struct ceph_mon_client *monc)
int i; int i;
/* build initial monmap */ /* build initial monmap */
monc->monmap = kzalloc(sizeof(*monc->monmap) + monc->monmap = kzalloc(struct_size(monc->monmap, mon_inst, num_mon),
num_mon*sizeof(monc->monmap->mon_inst[0]),
GFP_KERNEL); GFP_KERNEL);
if (!monc->monmap) if (!monc->monmap)
return -ENOMEM; return -ENOMEM;

View File

@ -565,8 +565,7 @@ struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags); req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
} else { } else {
BUG_ON(num_ops > CEPH_OSD_MAX_OPS); BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]), req = kmalloc(struct_size(req, r_ops, num_ops), gfp_flags);
gfp_flags);
} }
if (unlikely(!req)) if (unlikely(!req))
return NULL; return NULL;

View File

@ -184,8 +184,7 @@ recent_entry_init(struct recent_table *t, const union nf_inet_addr *addr,
} }
nstamps_max += 1; nstamps_max += 1;
e = kmalloc(sizeof(*e) + sizeof(e->stamps[0]) * nstamps_max, e = kmalloc(struct_size(e, stamps, nstamps_max), GFP_ATOMIC);
GFP_ATOMIC);
if (e == NULL) if (e == NULL)
return NULL; return NULL;
memcpy(&e->addr, addr, sizeof(e->addr)); memcpy(&e->addr, addr, sizeof(e->addr));

View File

@ -73,8 +73,8 @@ static struct sctp_endpoint *sctp_endpoint_init(struct sctp_endpoint *ep,
* variables. There are arrays that we encode directly * variables. There are arrays that we encode directly
* into parameters to make the rest of the operations easier. * into parameters to make the rest of the operations easier.
*/ */
auth_hmacs = kzalloc(sizeof(*auth_hmacs) + auth_hmacs = kzalloc(struct_size(auth_hmacs, hmac_ids,
sizeof(__u16) * SCTP_AUTH_NUM_HMACS, gfp); SCTP_AUTH_NUM_HMACS), gfp);
if (!auth_hmacs) if (!auth_hmacs)
goto nomem; goto nomem;

View File

@ -259,8 +259,8 @@ int _snd_ctl_add_slave(struct snd_kcontrol *master, struct snd_kcontrol *slave,
struct link_master *master_link = snd_kcontrol_chip(master); struct link_master *master_link = snd_kcontrol_chip(master);
struct link_slave *srec; struct link_slave *srec;
srec = kzalloc(sizeof(*srec) + srec = kzalloc(struct_size(srec, slave.vd, slave->count),
slave->count * sizeof(*slave->vd), GFP_KERNEL); GFP_KERNEL);
if (!srec) if (!srec)
return -ENOMEM; return -ENOMEM;
srec->kctl = slave; srec->kctl = slave;

View File

@ -1088,7 +1088,7 @@ static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
list_for_each(it, widgets) list_for_each(it, widgets)
size++; size++;
*list = kzalloc(sizeof(**list) + size * sizeof(*w), GFP_KERNEL); *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
if (*list == NULL) if (*list == NULL)
return -ENOMEM; return -ENOMEM;