Skip to contentSkip to conversation

gpio: msc313: remove kcalloc

PATCH Superseded Rosen Penev 5 participants 8 messages Mar 8 – Mar 10

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpio: msc313: remove kcalloc

Use a flexible array member to combine kzalloc and kcalloc.

---
 drivers/gpio/gpio-msc313.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/gpio/gpio-msc313.c +2 -6
index 7345afdc78de..de3e6da2a089 100644
@@ -483,7 +483,7 @@ MSC313_GPIO_CHIPDATA(ssd20xd);
 struct msc313_gpio {
 	void __iomem *base;
 	const struct msc313_gpio_data *gpio_data;
-	u8 *saved;
+	u8 saved[];
 };
 
 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->gpio_data = match_data;
 
-	gpio->saved = devm_kcalloc(dev, gpio->gpio_data->num, sizeof(*gpio->saved), GFP_KERNEL);
-	if (!gpio->saved)
-		return -ENOMEM;
-
 	gpio->base = devm_platform_ioremap_resource(pdev, 0);
 	if (IS_ERR(gpio->base))
 		return PTR_ERR(gpio->base);
Signature · 1 line
2.53.0
maintainer Acked-by on v1 1/1
#1 lore raw

Hi Rosen,

Quoted 2 lines · Rosen Penev

Use a flexible array member to combine kzalloc and kcalloc.

Thanks for the patch. This looks good to me. If I get time I will build and test this but I can see why it shouldn't work.

maintainer
#2 lore raw
In reply to Rosen Penev
Quoted 3 lines · Rosen Penev

Use a flexible array member to combine kzalloc and kcalloc.

(...)

Quoted 3 lines
 struct msc313_gpio {
        void __iomem *base;
        const struct msc313_gpio_data *gpio_data;

Do you wanna add:

const unsigned int saved_size;

drivers/gpio/gpio-msc313.c MSC313_GPIO_CHIPDATA(ssd20xd); · line 486

Quoted 2 lines
drivers/gpio/gpio-msc313.c +1 -1
-       u8 *saved;
+       u8 saved[];

u8 saved[] __counted_by(saved_size);

drivers/gpio/gpio-msc313.c static int msc313_gpio_probe(struct platform_device *pdev) · line 636

Quoted 9 lines

static int msc313_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)

drivers/gpio/gpio-msc313.c +1 -1
@@ -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

#3 lore raw
Quoted 6 lines · Linus Walleij
Quoted 3 lines · Rosen Penev

Use a flexible array member to combine kzalloc and kcalloc.

Quoted 8 lines

(...)

Quoted 3 lines
 struct msc313_gpio {
        void __iomem *base;
        const struct msc313_gpio_data *gpio_data;

Do you wanna add:

const unsigned int saved_size;

So in the code there's

for (i = 0; i < gpio->gpio_data->num; i++)

which is equivalent to match->num.

__counted_by doesn't support pointers AFAIK.

Quoted 22 lines

drivers/gpio/gpio-msc313.c MSC313_GPIO_CHIPDATA(ssd20xd); · line 486

Quoted 3 lines
drivers/gpio/gpio-msc313.c +1 -1
-       u8 *saved;
+       u8 saved[];

u8 saved[] __counted_by(saved_size);

drivers/gpio/gpio-msc313.c static int msc313_gpio_probe(struct platform_device *pdev) · line 636

Quoted 9 lines

static int msc313_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)

drivers/gpio/gpio-msc313.c +1 -1
@@ -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

#4 lore raw
Quoted 15 lines · Rosen Penev
Quoted 13 lines · Linus Walleij
Quoted 3 lines · Rosen Penev

Use a flexible array member to combine kzalloc and kcalloc.

(...)

Quoted 3 lines
 struct msc313_gpio {
        void __iomem *base;
        const struct msc313_gpio_data *gpio_data;

Do you wanna add:

Quoted 9 lines
Quoted 1 line

const unsigned int saved_size;

So in the code there's

for (i = 0; i < gpio->gpio_data->num; i++)

which is equivalent to match->num.

__counted_by doesn't support pointers AFAIK.

Quoted 1 line

This is a regular flexible array of u8. It should work fine with __counted_by and I too am in favor of using it as it has become standard for flexible arrays.

Bart

#7 lore raw
In reply to Rosen Penev
Quoted 14 lines · Rosen Penev
Quoted 13 lines · Linus Walleij
Quoted 3 lines · Rosen Penev

Use a flexible array member to combine kzalloc and kcalloc.

(...)

Quoted 3 lines
  struct msc313_gpio {
         void __iomem *base;
         const struct msc313_gpio_data *gpio_data;

Do you wanna add:

Quoted 8 lines
Quoted 1 line

const unsigned int saved_size;

So in the code there's

for (i = 0; i < gpio->gpio_data->num; i++)

which is equivalent to match->num.

__counted_by doesn't support pointers AFAIK.

We actually have __counted_by_ptr().

-Gustavo

#5 lore raw
In reply to Bartosz Golaszewski
Quoted 22 lines · Bartosz Golaszewski
Quoted 20 lines · Rosen Penev
Quoted 6 lines · Linus Walleij
Quoted 3 lines · Rosen Penev

Use a flexible array member to combine kzalloc and kcalloc.

Quoted 8 lines

(...)

Quoted 3 lines
 struct msc313_gpio {
        void __iomem *base;
        const struct msc313_gpio_data *gpio_data;

Do you wanna add:

const unsigned int saved_size;

So in the code there's

for (i = 0; i < gpio->gpio_data->num; i++)

Quoted 8 lines
Quoted 4 lines

which is equivalent to match->num.

__counted_by doesn't support pointers AFAIK.

Quoted 1 line

This is a regular flexible array of u8. It should work fine with __counted_by and I too am in favor of using it as it has become standard for flexible arrays.

That requires adding an extra variable in the struct.

Quoted 2 lines

Bart

#6 lore raw
Quoted 26 lines · Rosen Penev
Quoted 24 lines · Bartosz Golaszewski
Quoted 22 lines · Rosen Penev
Quoted 6 lines · Linus Walleij
Quoted 3 lines · Rosen Penev

Use a flexible array member to combine kzalloc and kcalloc.

Quoted 8 lines

(...)

Quoted 3 lines
 struct msc313_gpio {
        void __iomem *base;
        const struct msc313_gpio_data *gpio_data;

Do you wanna add:

const unsigned int saved_size;

So in the code there's

for (i = 0; i < gpio->gpio_data->num; i++)

which is equivalent to match->num.

Quoted 7 lines
Quoted 6 lines
Quoted 2 lines

__counted_by doesn't support pointers AFAIK.

Quoted 1 line

This is a regular flexible array of u8. It should work fine with __counted_by and I too am in favor of using it as it has become standard for flexible arrays.

That requires adding an extra variable in the struct.

That's ok, it's negligible and provides tangible advantages.

Please do the same for your other patches.

Bart