gpio: mockup: allocate lines with main struct
Subsystems (2)
PATCH 1/1 gpio: mockup: allocate lines with main struct
Remove no longer needed kcalloc to simplify allocation.
Added __counted_by along with a counting variable to get extra runtime analysis.
--- drivers/gpio/gpio-mockup.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-)
drivers/gpio/gpio-mockup.c +8 -9
@@ -52,10 +52,11 @@ struct gpio_mockup_line_status {struct gpio_mockup_chip {struct gpio_chip gc; -struct gpio_mockup_line_status *lines;struct irq_domain *irq_sim_domain;struct dentry *dbg_dir;struct mutex lock; +int nr_lines; + struct gpio_mockup_line_status lines[] __counted_by(nr_lines); };struct gpio_mockup_dbgfs_private {
@@ -436,15 +437,18 @@ static int gpio_mockup_probe(struct platform_device *pdev)if (rv) name =dev_name (dev); - chip =devm_kzalloc (dev,sizeof (*chip),GFP_KERNEL ); + chip =devm_kzalloc (dev,struct_size (chip, lines, ngpio),GFP_KERNEL );if (!chip)return -ENOMEM ; -mutex_init (&chip->lock); + chip->nr_lines = ngpio; gc = &chip->gc; - gc->base = base; gc->ngpio = ngpio; + gc->base = base; + +mutex_init (&chip->lock); + gc->label = name; gc->owner =THIS_MODULE ; gc->parent = dev;
@@ -460,11 +464,6 @@ static int gpio_mockup_probe(struct platform_device *pdev) gc->request = gpio_mockup_request; gc->free = gpio_mockup_free; - chip->lines =devm_kcalloc (dev, gc->ngpio, -sizeof (*chip->lines),GFP_KERNEL ); -if (!chip->lines) -return -ENOMEM ; -for (i =0 ; i < gc->ngpio; i++) chip->lines[i].dir =GPIO_LINE_DIRECTION_IN ;
Signature · 1 line
2.53.0
Quoted 6 lines · Rosen Penev
Remove no longer needed kcalloc to simplify allocation.
Added
__counted_byalong with a counting variable to get extra runtime analysis.Signed-off-by Rosen Penev <[email protected]>
Sweet!
Yours,
Linus Walleij
Quoted 15 lines · Rosen Penev
Remove no longer needed kcalloc to simplify allocation.
Added
__counted_byalong with a counting variable to get extra runtime analysis.Signed-off-by Rosen Penev <[email protected]>--- drivers/gpio/gpio-mockup.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-)drivers/gpio/gpio-mockup.c +0
-0
drivers/gpio/gpio-mockup.c struct gpio_mockup_line_status { · line 60
Quoted 10 lines
drivers/gpio/gpio-mockup.c +2
-1@@ -52,10 +52,11 @@ struct gpio_mockup_line_status {struct gpio_mockup_chip {struct gpio_chip gc; -struct gpio_mockup_line_status *lines;struct irq_domain *irq_sim_domain;struct dentry *dbg_dir;struct mutex lock; +int nr_lines; + struct gpio_mockup_line_status lines[] __counted_by(nr_lines); };
In the cases where a new counter variable is being added to the struct, I think it might be better to have those be unsigned.
drivers/gpio/gpio-mockup.c static int gpio_mockup_probe(struct platform_device *pdev) · line 444
Quoted 13 lines
drivers/gpio/gpio-mockup.c +2
-2struct gpio_mockup_dbgfs_private {@@ -436,15 +437,18 @@ static int gpio_mockup_probe(struct platform_device *pdev)if (rv) name =dev_name (dev); - chip =devm_kzalloc (dev,sizeof (*chip),GFP_KERNEL ); + chip =devm_kzalloc (dev,struct_size (chip, lines, ngpio),GFP_KERNEL );if (!chip)return -ENOMEM ; -mutex_init (&chip->lock); + chip->nr_lines = ngpio;
Besides the new variable being meaningless for negative values, there's a strong hint about its type even from the counter used to perform the calculation (u16):
static int gpio_mockup_probe(struct platform_device *pdev)
{
...
u16 ngpio;
...
rv = device_property_read_u16(dev, "nr-gpios", &ngpio);
...
gc->ngpio = ngpio;
...
chip->lines = devm_kcalloc(dev, gc->ngpio,
sizeof(*chip->lines), GFP_KERNEL);But this begs the question: why add nr_lines when ngpio is already part of the struct:
struct gpio_chip {
...
u16 ngpio;?
-Kees
Signature · 1 line
Kees Cook
Quoted 61 lines · Kees Cook
Quoted 15 lines · Rosen Penev
Remove no longer needed kcalloc to simplify allocation.
Added
__counted_byalong with a counting variable to get extra runtime analysis.Signed-off-by Rosen Penev <[email protected]>--- drivers/gpio/gpio-mockup.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-)drivers/gpio/gpio-mockup.c +0
-0drivers/gpio/gpio-mockup.c struct gpio_mockup_line_status { · line 60
Quoted 10 lines
drivers/gpio/gpio-mockup.c +2
-1@@ -52,10 +52,11 @@ struct gpio_mockup_line_status {struct gpio_mockup_chip {struct gpio_chip gc; -struct gpio_mockup_line_status *lines;struct irq_domain *irq_sim_domain;struct dentry *dbg_dir;struct mutex lock; +int nr_lines; + struct gpio_mockup_line_status lines[] __counted_by(nr_lines); };In the cases where a new counter variable is being added to the struct, I think it might be better to have those be unsigned.
drivers/gpio/gpio-mockup.c static int gpio_mockup_probe(struct platform_device *pdev) · line 444
Quoted 13 lines
drivers/gpio/gpio-mockup.c +2
-2struct gpio_mockup_dbgfs_private {@@ -436,15 +437,18 @@ static int gpio_mockup_probe(struct platform_device *pdev)if (rv) name =dev_name (dev); - chip =devm_kzalloc (dev,sizeof (*chip),GFP_KERNEL ); + chip =devm_kzalloc (dev,struct_size (chip, lines, ngpio),GFP_KERNEL );if (!chip)return -ENOMEM ; -mutex_init (&chip->lock); + chip->nr_lines = ngpio;Besides the new variable being meaningless for negative values, there's a strong hint about its type even from the counter used to perform the calculation (u16):
static int
gpio_mockup_probe(struct platform_device *pdev){ ... u16 ngpio; ... rv = device_property_read_u16(dev, "nr-gpios", &ngpio); ... gc->ngpio = ngpio; ... chip->lines = devm_kcalloc(dev, gc->ngpio, sizeof(*chip->lines), GFP_KERNEL);
Quoted 2 lines
But this begs the question: why add
nr_lineswhen ngpio is already part of the struct:
Maintainers for some inexplicable reason want an extra variable for __counted_by works.
Quoted 12 lines
struct gpio_chip { ... u16 ngpio;?
-Kees
--
Kees Cook
Bartosz Golaszewski
maintainerQuoted 14 lines · Rosen Penev
Quoted 14 lines
static int
gpio_mockup_probe(struct platform_device *pdev){ ... u16 ngpio; ... rv = device_property_read_u16(dev, "nr-gpios", &ngpio); ... gc->ngpio = ngpio; ... chip->lines = devm_kcalloc(dev, gc->ngpio, sizeof(*chip->lines), GFP_KERNEL);
Quoted 4 lines
Quoted 2 lines
But this begs the question: why add
nr_lineswhen ngpio is already part of the struct:Maintainers for some inexplicable reason want an extra variable for
__counted_byworks.
I believe what Kees means here is: you can use ngpio for __counted_by() like so:
__counted_by(gc.ngpio)
I didn't think about it and I do prefer it of course over an extra field.
Bart
Quoted 16 lines · Bartosz Golaszewski
Quoted 14 lines · Rosen Penev
Quoted 14 lines
static int
gpio_mockup_probe(struct platform_device *pdev){ ... u16 ngpio; ... rv = device_property_read_u16(dev, "nr-gpios", &ngpio); ... gc->ngpio = ngpio; ... chip->lines = devm_kcalloc(dev, gc->ngpio, sizeof(*chip->lines), GFP_KERNEL);
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
But this begs the question: why add
nr_lineswhen ngpio is already part of the struct:Maintainers for some inexplicable reason want an extra variable for
__counted_byworks.I believe what Kees means here is: you can use ngpio for
__counted_by()like so:
__counted_by(gc.ngpio)
__counted_by doesn't support nested variables like that.
drivers/gpio/gpio-mockup.c:59:61: error: ‘gc’ undeclared here (not in a function)
59 | struct gpio_mockup_line_status lines[] __counted_by(gc.ngpio);
Quoted 4 lines
I didn't think about it and I do prefer it of course over an extra field.
Bart
Bartosz Golaszewski
maintainerQuoted 22 lines · Rosen Penev
Quoted 21 lines · Bartosz Golaszewski
Quoted 18 lines · Rosen Penev
Quoted 14 lines
static int
gpio_mockup_probe(struct platform_device *pdev){ ... u16 ngpio; ... rv = device_property_read_u16(dev, "nr-gpios", &ngpio); ... gc->ngpio = ngpio; ... chip->lines = devm_kcalloc(dev, gc->ngpio, sizeof(*chip->lines), GFP_KERNEL);Quoted 2 lines
But this begs the question: why add
nr_lineswhen ngpio is already part of the struct:Maintainers for some inexplicable reason want an extra variable for
__counted_byworks.
Quoted 8 lines
Quoted 3 lines
I believe what Kees means here is: you can use ngpio for
__counted_by()like so:
__counted_by(gc.ngpio)
__counted_bydoesn't support nested variables like that.
drivers/gpio/gpio-mockup.c:59:61: error: ‘gc’ undeclared here (not in a function)
59 | structgpio_mockup_line_statuslines[]__counted_by(gc.ngpio);
The following spin on your patch builds fine for me:
drivers/gpio/gpio-mockup.c +6 -10
@@ -52,10 +52,10 @@ struct gpio_mockup_line_status {struct gpio_mockup_chip {struct gpio_chip gc; -struct gpio_mockup_line_status *lines;struct irq_domain *irq_sim_domain;struct dentry *dbg_dir;struct mutex lock; + struct gpio_mockup_line_status lines[] __counted_by(gc.ngpio); };struct gpio_mockup_dbgfs_private {
@@ -436,15 +436,16 @@ static int gpio_mockup_probe(struct platform_device *pdev)if (rv) name =dev_name (dev); - chip =devm_kzalloc (dev,sizeof (*chip),GFP_KERNEL ); + chip =devm_kzalloc (dev,struct_size (chip, lines, ngpio),GFP_KERNEL );if (!chip)return -ENOMEM ; -mutex_init (&chip->lock); - gc = &chip->gc; - gc->base = base; gc->ngpio = ngpio; + gc->base = base; + +mutex_init (&chip->lock); + gc->label = name; gc->owner =THIS_MODULE ; gc->parent = dev;
@@ -460,11 +461,6 @@ static int gpio_mockup_probe(struct platform_device *pdev) gc->request = gpio_mockup_request; gc->free = gpio_mockup_free; - chip->lines =devm_kcalloc (dev, gc->ngpio, -sizeof (*chip->lines),GFP_KERNEL ); -if (!chip->lines) -return -ENOMEM ; -for (i =0 ; i < gc->ngpio; i++) chip->lines[i].dir =GPIO_LINE_DIRECTION_IN ;
Bart
Quoted 40 lines · Bartosz Golaszewski
Quoted 22 lines · Rosen Penev
Quoted 21 lines · Bartosz Golaszewski
Quoted 18 lines · Rosen Penev
Quoted 14 lines
static int
gpio_mockup_probe(struct platform_device *pdev){ ... u16 ngpio; ... rv = device_property_read_u16(dev, "nr-gpios", &ngpio); ... gc->ngpio = ngpio; ... chip->lines = devm_kcalloc(dev, gc->ngpio, sizeof(*chip->lines), GFP_KERNEL);Quoted 2 lines
But this begs the question: why add
nr_lineswhen ngpio is already part of the struct:Maintainers for some inexplicable reason want an extra variable for
__counted_byworks.Quoted 8 lines
Quoted 3 lines
I believe what Kees means here is: you can use ngpio for
__counted_by()like so:
__counted_by(gc.ngpio)
__counted_bydoesn't support nested variables like that.
drivers/gpio/gpio-mockup.c:59:61: error: ‘gc’ undeclared here (not in a function)
59 | structgpio_mockup_line_statuslines[]__counted_by(gc.ngpio);The following spin on your patch builds fine for me:
drivers/gpio/gpio-mockup.c +0
-0
drivers/gpio/gpio-mockup.c struct gpio_mockup_line_status { · line 58
Quoted 8 lines
drivers/gpio/gpio-mockup.c +1
-1@@ -52,10 +52,10 @@ struct gpio_mockup_line_status {struct gpio_mockup_chip {struct gpio_chip gc; -struct gpio_mockup_line_status *lines;struct irq_domain *irq_sim_domain;struct dentry *dbg_dir;struct mutex lock; + struct gpio_mockup_line_status lines[] __counted_by(gc.ngpio);
You're using an older compiler. This does not work at all.
* Optional: only supported since gcc >= 15
* Optional: only supported since clang >= 18
Quoted 37 lines
drivers/gpio/gpio-mockup.c +5
-9};struct gpio_mockup_dbgfs_private {@@ -436,15 +436,16 @@ static int gpio_mockup_probe(struct platform_device *pdev)if (rv) name =dev_name (dev); - chip =devm_kzalloc (dev,sizeof (*chip),GFP_KERNEL ); + chip =devm_kzalloc (dev,struct_size (chip, lines, ngpio),GFP_KERNEL );if (!chip)return -ENOMEM ; -mutex_init (&chip->lock); - gc = &chip->gc; - gc->base = base; gc->ngpio = ngpio; + gc->base = base; + +mutex_init (&chip->lock); + gc->label = name; gc->owner =THIS_MODULE ; gc->parent = dev;@@ -460,11 +461,6 @@ static int gpio_mockup_probe(struct platform_device *pdev) gc->request = gpio_mockup_request; gc->free = gpio_mockup_free; - chip->lines =devm_kcalloc (dev, gc->ngpio, -sizeof (*chip->lines),GFP_KERNEL ); -if (!chip->lines) -return -ENOMEM ; -for (i =0 ; i < gc->ngpio; i++) chip->lines[i].dir =GPIO_LINE_DIRECTION_IN ;Bart
Bartosz Golaszewski
maintainerQuoted 52 lines · Rosen Penev
Quoted 40 lines · Bartosz Golaszewski
Quoted 22 lines · Rosen Penev
Quoted 21 lines · Bartosz Golaszewski
Quoted 18 lines · Rosen Penev
Quoted 14 lines
static int
gpio_mockup_probe(struct platform_device *pdev){ ... u16 ngpio; ... rv = device_property_read_u16(dev, "nr-gpios", &ngpio); ... gc->ngpio = ngpio; ... chip->lines = devm_kcalloc(dev, gc->ngpio, sizeof(*chip->lines), GFP_KERNEL);Quoted 2 lines
But this begs the question: why add
nr_lineswhen ngpio is already part of the struct:Maintainers for some inexplicable reason want an extra variable for
__counted_byworks.Quoted 8 lines
Quoted 3 lines
I believe what Kees means here is: you can use ngpio for
__counted_by()like so:
__counted_by(gc.ngpio)
__counted_bydoesn't support nested variables like that.
drivers/gpio/gpio-mockup.c:59:61: error: ‘gc’ undeclared here (not in a function)
59 | structgpio_mockup_line_statuslines[]__counted_by(gc.ngpio);The following spin on your patch builds fine for me:
drivers/gpio/gpio-mockup.c +0
-0drivers/gpio/gpio-mockup.c struct gpio_mockup_line_status { · line 58
Quoted 8 lines
drivers/gpio/gpio-mockup.c +1
-1@@ -52,10 +52,10 @@ struct gpio_mockup_line_status {struct gpio_mockup_chip {struct gpio_chip gc; -struct gpio_mockup_line_status *lines;struct irq_domain *irq_sim_domain;struct dentry *dbg_dir;struct mutex lock; + struct gpio_mockup_line_status lines[] __counted_by(gc.ngpio);You're using an older compiler. This does not work at all.
Quoted 4 lines
* Optional: only supported since gcc >= 15
* Optional: only supported since clang >= 18Quoted 2 lines
drivers/gpio/gpio-mockup.c +0
-0};
Ok, what is this feature called in gcc parlance and where does this info come from?
Bart
Quoted 54 lines · Bartosz Golaszewski
Quoted 52 lines · Rosen Penev
Quoted 40 lines · Bartosz Golaszewski
Quoted 22 lines · Rosen Penev
Quoted 21 lines · Bartosz Golaszewski
Quoted 18 lines · Rosen Penev
Quoted 14 lines
static int
gpio_mockup_probe(struct platform_device *pdev){ ... u16 ngpio; ... rv = device_property_read_u16(dev, "nr-gpios", &ngpio); ... gc->ngpio = ngpio; ... chip->lines = devm_kcalloc(dev, gc->ngpio, sizeof(*chip->lines), GFP_KERNEL);Quoted 2 lines
But this begs the question: why add
nr_lineswhen ngpio is already part of the struct:Maintainers for some inexplicable reason want an extra variable for
__counted_byworks.Quoted 8 lines
Quoted 3 lines
I believe what Kees means here is: you can use ngpio for
__counted_by()like so:
__counted_by(gc.ngpio)
__counted_bydoesn't support nested variables like that.
drivers/gpio/gpio-mockup.c:59:61: error: ‘gc’ undeclared here (not in a function)
59 | structgpio_mockup_line_statuslines[]__counted_by(gc.ngpio);The following spin on your patch builds fine for me:
drivers/gpio/gpio-mockup.c +0
-0drivers/gpio/gpio-mockup.c struct gpio_mockup_line_status { · line 58
Quoted 8 lines
drivers/gpio/gpio-mockup.c +1
-1@@ -52,10 +52,10 @@ struct gpio_mockup_line_status {struct gpio_mockup_chip {struct gpio_chip gc; -struct gpio_mockup_line_status *lines;struct irq_domain *irq_sim_domain;struct dentry *dbg_dir;struct mutex lock; + struct gpio_mockup_line_status lines[] __counted_by(gc.ngpio);You're using an older compiler. This does not work at all.
Quoted 7 lines
Quoted 4 lines
* Optional: only supported since gcc >= 15
* Optional: only supported since clang >= 18Quoted 2 lines
drivers/gpio/gpio-mockup.c +0
-0};Ok, what is this feature called in gcc parlance and where does this info come from?
include/linux/compiler_types.h
Lines 363-364 in linux-next.
Quoted 2 lines
Bart
Bartosz Golaszewski
maintainerQuoted 56 lines · Rosen Penev
Quoted 54 lines · Bartosz Golaszewski
Quoted 52 lines · Rosen Penev
Quoted 40 lines · Bartosz Golaszewski
Quoted 22 lines · Rosen Penev
Quoted 21 lines · Bartosz Golaszewski
Quoted 18 lines · Rosen Penev
Quoted 14 lines
static int
gpio_mockup_probe(struct platform_device *pdev){ ... u16 ngpio; ... rv = device_property_read_u16(dev, "nr-gpios", &ngpio); ... gc->ngpio = ngpio; ... chip->lines = devm_kcalloc(dev, gc->ngpio, sizeof(*chip->lines), GFP_KERNEL);Quoted 2 lines
But this begs the question: why add
nr_lineswhen ngpio is already part of the struct:Maintainers for some inexplicable reason want an extra variable for
__counted_byworks.Quoted 8 lines
Quoted 3 lines
I believe what Kees means here is: you can use ngpio for
__counted_by()like so:
__counted_by(gc.ngpio)
__counted_bydoesn't support nested variables like that.
drivers/gpio/gpio-mockup.c:59:61: error: ‘gc’ undeclared here (not in a function)
59 | structgpio_mockup_line_statuslines[]__counted_by(gc.ngpio);The following spin on your patch builds fine for me:
drivers/gpio/gpio-mockup.c +0
-0drivers/gpio/gpio-mockup.c struct gpio_mockup_line_status { · line 58
Quoted 8 lines
drivers/gpio/gpio-mockup.c +1
-1@@ -52,10 +52,10 @@ struct gpio_mockup_line_status {struct gpio_mockup_chip {struct gpio_chip gc; -struct gpio_mockup_line_status *lines;struct irq_domain *irq_sim_domain;struct dentry *dbg_dir;struct mutex lock; + struct gpio_mockup_line_status lines[] __counted_by(gc.ngpio);You're using an older compiler. This does not work at all.
Quoted 1 line
Quoted 1 line
Quoted 1 line
*
Indeed, sorry. In that case I don't know what Kees meant. I'm fine with a new variable.
Bartosz
Quoted 58 lines · Bartosz Golaszewski
Quoted 56 lines · Rosen Penev
Quoted 54 lines · Bartosz Golaszewski
Quoted 52 lines · Rosen Penev
Quoted 40 lines · Bartosz Golaszewski
Quoted 22 lines · Rosen Penev
Quoted 21 lines · Bartosz Golaszewski
Quoted 18 lines · Rosen Penev
Quoted 14 lines
static int
gpio_mockup_probe(struct platform_device *pdev){ ... u16 ngpio; ... rv = device_property_read_u16(dev, "nr-gpios", &ngpio); ... gc->ngpio = ngpio; ... chip->lines = devm_kcalloc(dev, gc->ngpio, sizeof(*chip->lines), GFP_KERNEL);Quoted 2 lines
But this begs the question: why add
nr_lineswhen ngpio is already part of the struct:Maintainers for some inexplicable reason want an extra variable for
__counted_byworks.Quoted 8 lines
Quoted 3 lines
I believe what Kees means here is: you can use ngpio for
__counted_by()like so:
__counted_by(gc.ngpio)
__counted_bydoesn't support nested variables like that.
drivers/gpio/gpio-mockup.c:59:61: error: ‘gc’ undeclared here (not in a function)
59 | structgpio_mockup_line_statuslines[]__counted_by(gc.ngpio);The following spin on your patch builds fine for me:
drivers/gpio/gpio-mockup.c +0
-0drivers/gpio/gpio-mockup.c struct gpio_mockup_line_status { · line 58
Quoted 8 lines
drivers/gpio/gpio-mockup.c +1
-1@@ -52,10 +52,10 @@ struct gpio_mockup_line_status {struct gpio_mockup_chip {struct gpio_chip gc; -struct gpio_mockup_line_status *lines;struct irq_domain *irq_sim_domain;struct dentry *dbg_dir;struct mutex lock; + struct gpio_mockup_line_status lines[] __counted_by(gc.ngpio);You're using an older compiler. This does not work at all.
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
*
Indeed, sorry. In that case I don't know what Kees meant. I'm fine with a new variable.
I think his comment is that there's already a counting variable, with or without counted_by.
Quoted 2 lines
Bartosz