Skip to content

Commit 9a5bf2f

Browse files
nehebBartosz Golaszewski
authored andcommitted
gpio: dwapb: reduce allocation to single kzalloc
Instead of kzalloc + kcalloc, Combine the two using a flexible array member. Allows using __counted_by for extra runtime analysis. Move counting variable to right after allocation as required by __counted_by. Signed-off-by: Rosen Penev <rosenp@gmail.com> Reviewed-by: Linus Walleij <linusw@kernel.org> Link: https://patch.msgid.link/20260320005338.30355-1-rosenp@gmail.com Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
1 parent a6e53d0 commit 9a5bf2f

1 file changed

Lines changed: 5 additions & 14 deletions

File tree

drivers/gpio/gpio-dwapb.c

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ struct dwapb_port_property {
7575
};
7676

7777
struct dwapb_platform_data {
78-
struct dwapb_port_property *properties;
7978
unsigned int nports;
79+
struct dwapb_port_property properties[] __counted_by(nports);
8080
};
8181

8282
/* Store GPIO context across system-wide suspend/resume transitions */
@@ -114,11 +114,11 @@ static inline struct dwapb_gpio *to_dwapb_gpio(struct gpio_chip *gc)
114114
struct dwapb_gpio {
115115
struct device *dev;
116116
void __iomem *regs;
117-
struct dwapb_gpio_port *ports;
118117
unsigned int nr_ports;
119118
unsigned int flags;
120119
struct reset_control *rst;
121120
struct clk_bulk_data clks[DWAPB_NR_CLOCKS];
121+
struct dwapb_gpio_port ports[] __counted_by(nr_ports);
122122
};
123123

124124
static inline u32 gpio_reg_v2_convert(unsigned int offset)
@@ -585,14 +585,10 @@ static struct dwapb_platform_data *dwapb_gpio_get_pdata(struct device *dev)
585585
if (nports == 0)
586586
return ERR_PTR(-ENODEV);
587587

588-
pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
588+
pdata = devm_kzalloc(dev, struct_size(pdata, properties, nports), GFP_KERNEL);
589589
if (!pdata)
590590
return ERR_PTR(-ENOMEM);
591591

592-
pdata->properties = devm_kcalloc(dev, nports, sizeof(*pp), GFP_KERNEL);
593-
if (!pdata->properties)
594-
return ERR_PTR(-ENOMEM);
595-
596592
pdata->nports = nports;
597593

598594
i = 0;
@@ -714,22 +710,17 @@ static int dwapb_gpio_probe(struct platform_device *pdev)
714710
if (IS_ERR(pdata))
715711
return PTR_ERR(pdata);
716712

717-
gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
713+
gpio = devm_kzalloc(&pdev->dev, struct_size(gpio, ports, pdata->nports), GFP_KERNEL);
718714
if (!gpio)
719715
return -ENOMEM;
720716

721-
gpio->dev = &pdev->dev;
722717
gpio->nr_ports = pdata->nports;
718+
gpio->dev = &pdev->dev;
723719

724720
err = dwapb_get_reset(gpio);
725721
if (err)
726722
return err;
727723

728-
gpio->ports = devm_kcalloc(&pdev->dev, gpio->nr_ports,
729-
sizeof(*gpio->ports), GFP_KERNEL);
730-
if (!gpio->ports)
731-
return -ENOMEM;
732-
733724
gpio->regs = devm_platform_ioremap_resource(pdev, 0);
734725
if (IS_ERR(gpio->regs))
735726
return PTR_ERR(gpio->regs);

0 commit comments

Comments
 (0)