gpio: msc313: remove kcalloc
Subsystems (2)
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
@@ -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
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.
Linus Walleij
maintainerQuoted 3 lines · Rosen Penev
Use a flexible array member to combine kzalloc and kcalloc.
Signed-off-by Rosen Penev <[email protected]>
(...)
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
Quoted 6 lines · Linus Walleij
Quoted 3 lines · Rosen Penev
Use a flexible array member to combine kzalloc and kcalloc.
Signed-off-by Rosen Penev <[email protected]>
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
Bartosz Golaszewski
maintainerQuoted 15 lines · Rosen Penev
Quoted 13 lines · Linus Walleij
Quoted 3 lines · Rosen Penev
Use a flexible array member to combine kzalloc and kcalloc.
Signed-off-by Rosen Penev <[email protected]>(...)
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_bydoesn'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
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.
Signed-off-by Rosen Penev <[email protected]>(...)
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_bydoesn't support pointers AFAIK.
We actually have __counted_by_ptr().
-Gustavo
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.
Signed-off-by Rosen Penev <[email protected]>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_bydoesn't support pointers AFAIK.Quoted 1 line
This is a regular flexible array of u8. It should work fine with
__counted_byand 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
Bartosz Golaszewski
maintainerQuoted 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.
Signed-off-by Rosen Penev <[email protected]>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_bydoesn't support pointers AFAIK.Quoted 1 line
This is a regular flexible array of u8. It should work fine with
__counted_byand 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