Skip to contentSkip to conversation

genirq/devres: Use struct_size() in devm_kzalloc()

PATCH New Gustavo A. R. Silva 2 participants 2 messages Apr 10, 2019 – Apr 16, 2019 IRQ

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 genirq/devres: Use struct_size() in devm_kzalloc()

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;
    struct boo entry[];
};

size = sizeof(struct foo) + count * sizeof(struct boo); instance = devm_kzalloc(dev, size, GFP_KERNEL);

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

instance = devm_kzalloc(dev, struct_size(instance, entry, count), GFP_KERNEL);

Notice that, in this case, variable sz is not necessary, hence it is removed.

This code was detected with the help of Coccinelle.

---
 kernel/irq/devres.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/irq/devres.c +1 -2
index f808c6a97dcc..f6e5515ee077 100644
@@ -220,9 +220,8 @@ devm_irq_alloc_generic_chip(struct device *dev, const char *name, int num_ct,
 			    irq_flow_handler_t handler)
 {
 	struct irq_chip_generic *gc;
-	unsigned long sz = sizeof(*gc) + num_ct * sizeof(struct irq_chip_type);
 
-	gc = devm_kzalloc(dev, sz, GFP_KERNEL);
+	gc = devm_kzalloc(dev, struct_size(gc, chip_types, num_ct), GFP_KERNEL);
 	if (gc)
 		irq_init_generic_chip(gc, name, num_ct,
 				      irq_base, reg_base, handler);
Signature · 1 line
2.21.0
#1 lore raw
genirq/devres: Use struct_size() in devm_kzalloc()
Commit-ID:  2d65c42b43e53d61f1fd6b8d0a097451a4cffa24
Gitweb:     https://git.kernel.org/tip/2d65c42b43e53d61f1fd6b8d0a097451a4cffa24
Author:     Gustavo A. R. Silva <[email protected]>
AuthorDate: Wed, 10 Apr 2019 12:09:14 -0500
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Tue, 16 Apr 2019 21:54:03 +0200

genirq/devres: Use struct_size() in devm_kzalloc()

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;
    struct boo entry[];
};

size = sizeof(struct foo) + count * sizeof(struct boo); instance = devm_kzalloc(dev, size, GFP_KERNEL);

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

instance = devm_kzalloc(dev, struct_size(instance, entry, count), GFP_KERNEL);

This code was detected with the help of Coccinelle.

---
 kernel/irq/devres.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/irq/devres.c +1 -2
index f808c6a97dcc..f6e5515ee077 100644
@@ -220,9 +220,8 @@ devm_irq_alloc_generic_chip(struct device *dev, const char *name, int num_ct,
 			    irq_flow_handler_t handler)
 {
 	struct irq_chip_generic *gc;
-	unsigned long sz = sizeof(*gc) + num_ct * sizeof(struct irq_chip_type);
 
-	gc = devm_kzalloc(dev, sz, GFP_KERNEL);
+	gc = devm_kzalloc(dev, struct_size(gc, chip_types, num_ct), GFP_KERNEL);
 	if (gc)
 		irq_init_generic_chip(gc, name, num_ct,
 				      irq_base, reg_base, handler);