Re: [PATCH] gpio: msc313: remove kcalloc

From: Linus Walleij

Date: Sun Mar 08 2026 - 20:06:24 EST


On Sun, Mar 8, 2026 at 3:15 AM Rosen Penev <rosenp@xxxxxxxxx> wrote:

> Use a flexible array member to combine kzalloc and kcalloc.
>
> Signed-off-by: Rosen Penev <rosenp@xxxxxxxxx>
(...)
> struct msc313_gpio {
> void __iomem *base;
> const struct msc313_gpio_data *gpio_data;

Do you wanna add:

const unsigned int saved_size;

> - u8 *saved;
> + u8 saved[];

u8 saved[] __counted_by(saved_size);

> static int msc313_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
> @@ -631,16 +631,12 @@ static int msc313_gpio_probe(struct platform_device *pdev)
> if (!parent_domain)
> return -ENODEV;
>
> - gpio = devm_kzalloc(dev, sizeof(*gpio), GFP_KERNEL);
> + gpio = devm_kzalloc(dev, struct_size(gpio, saved, match_data->num), GFP_KERNEL);
> if (!gpio)
> return -ENOMEM;

gpio->saved_size = match_data->num;

I know it takes some bytes more but it feels way safer.

Yours,
Linus Walleij