* [PATCH] gpio: mockup: allocate lines with main struct
@ 2026-03-19 0:05 Rosen Penev
2026-03-20 13:28 ` Linus Walleij
2026-03-20 18:10 ` Kees Cook
0 siblings, 2 replies; 12+ messages in thread
From: Rosen Penev @ 2026-03-19 0:05 UTC (permalink / raw)
To: linux-gpio
Cc: Bamvor Jian Zhang, Linus Walleij, Bartosz Golaszewski, Kees Cook,
Gustavo A. R. Silva, open list,
open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b
Remove no longer needed kcalloc to simplify allocation.
Added __counted_by along with a counting variable to get extra runtime
analysis.
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
drivers/gpio/gpio-mockup.c | 17 ++++++++---------
1 file changed, 8 insertions(+), 9 deletions(-)
diff --git a/drivers/gpio/gpio-mockup.c b/drivers/gpio/gpio-mockup.c
index a7d69f3835c1..a907ce28cbbb 100644
--- a/drivers/gpio/gpio-mockup.c
+++ b/drivers/gpio/gpio-mockup.c
@@ -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;
--
2.53.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH] gpio: mockup: allocate lines with main struct
2026-03-19 0:05 [PATCH] gpio: mockup: allocate lines with main struct Rosen Penev
@ 2026-03-20 13:28 ` Linus Walleij
2026-03-20 18:10 ` Kees Cook
1 sibling, 0 replies; 12+ messages in thread
From: Linus Walleij @ 2026-03-20 13:28 UTC (permalink / raw)
To: Rosen Penev
Cc: linux-gpio, Bamvor Jian Zhang, Bartosz Golaszewski, Kees Cook,
Gustavo A. R. Silva, open list,
open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b
On Thu, Mar 19, 2026 at 1:06 AM Rosen Penev <rosenp@gmail.com> wrote:
> Remove no longer needed kcalloc to simplify allocation.
>
> Added __counted_by along with a counting variable to get extra runtime
> analysis.
>
> Signed-off-by: Rosen Penev <rosenp@gmail.com>
Sweet!
Reviewed-by: Linus Walleij <linusw@kernel.org>
Yours,
Linus Walleij
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] gpio: mockup: allocate lines with main struct
2026-03-19 0:05 [PATCH] gpio: mockup: allocate lines with main struct Rosen Penev
2026-03-20 13:28 ` Linus Walleij
@ 2026-03-20 18:10 ` Kees Cook
2026-03-20 23:00 ` Rosen Penev
1 sibling, 1 reply; 12+ messages in thread
From: Kees Cook @ 2026-03-20 18:10 UTC (permalink / raw)
To: Rosen Penev
Cc: linux-gpio, Bamvor Jian Zhang, Linus Walleij, Bartosz Golaszewski,
Gustavo A. R. Silva, open list,
open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b
On Wed, Mar 18, 2026 at 05:05:58PM -0700, Rosen Penev wrote:
> Remove no longer needed kcalloc to simplify allocation.
>
> Added __counted_by along with a counting variable to get extra runtime
> analysis.
>
> Signed-off-by: Rosen Penev <rosenp@gmail.com>
> ---
> drivers/gpio/gpio-mockup.c | 17 ++++++++---------
> 1 file changed, 8 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/gpio/gpio-mockup.c b/drivers/gpio/gpio-mockup.c
> index a7d69f3835c1..a907ce28cbbb 100644
> --- a/drivers/gpio/gpio-mockup.c
> +++ b/drivers/gpio/gpio-mockup.c
> @@ -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.
>
> 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;
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
--
Kees Cook
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] gpio: mockup: allocate lines with main struct
2026-03-20 18:10 ` Kees Cook
@ 2026-03-20 23:00 ` Rosen Penev
2026-03-23 10:00 ` Bartosz Golaszewski
0 siblings, 1 reply; 12+ messages in thread
From: Rosen Penev @ 2026-03-20 23:00 UTC (permalink / raw)
To: Kees Cook
Cc: linux-gpio, Bamvor Jian Zhang, Linus Walleij, Bartosz Golaszewski,
Gustavo A. R. Silva, open list,
open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b
On Fri, Mar 20, 2026 at 11:10 AM Kees Cook <kees@kernel.org> wrote:
>
> On Wed, Mar 18, 2026 at 05:05:58PM -0700, Rosen Penev wrote:
> > Remove no longer needed kcalloc to simplify allocation.
> >
> > Added __counted_by along with a counting variable to get extra runtime
> > analysis.
> >
> > Signed-off-by: Rosen Penev <rosenp@gmail.com>
> > ---
> > drivers/gpio/gpio-mockup.c | 17 ++++++++---------
> > 1 file changed, 8 insertions(+), 9 deletions(-)
> >
> > diff --git a/drivers/gpio/gpio-mockup.c b/drivers/gpio/gpio-mockup.c
> > index a7d69f3835c1..a907ce28cbbb 100644
> > --- a/drivers/gpio/gpio-mockup.c
> > +++ b/drivers/gpio/gpio-mockup.c
> > @@ -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.
>
> >
> > 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;
>
> 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:
Maintainers for some inexplicable reason want an extra variable for
__counted_by works.
>
> struct gpio_chip {
> ...
> u16 ngpio;
>
>
> ?
>
> -Kees
>
> --
> Kees Cook
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] gpio: mockup: allocate lines with main struct
2026-03-20 23:00 ` Rosen Penev
@ 2026-03-23 10:00 ` Bartosz Golaszewski
2026-03-23 16:43 ` Rosen Penev
0 siblings, 1 reply; 12+ messages in thread
From: Bartosz Golaszewski @ 2026-03-23 10:00 UTC (permalink / raw)
To: Rosen Penev
Cc: Kees Cook, linux-gpio, Bamvor Jian Zhang, Linus Walleij,
Gustavo A. R. Silva, open list,
open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b
On Sat, Mar 21, 2026 at 12:00 AM Rosen Penev <rosenp@gmail.com> wrote:
>
> >
> > 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:
> Maintainers for some inexplicable reason want an extra variable for
> __counted_by works.
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
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] gpio: mockup: allocate lines with main struct
2026-03-23 10:00 ` Bartosz Golaszewski
@ 2026-03-23 16:43 ` Rosen Penev
2026-03-24 9:16 ` Bartosz Golaszewski
0 siblings, 1 reply; 12+ messages in thread
From: Rosen Penev @ 2026-03-23 16:43 UTC (permalink / raw)
To: Bartosz Golaszewski
Cc: Kees Cook, linux-gpio, Bamvor Jian Zhang, Linus Walleij,
Gustavo A. R. Silva, open list,
open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b
On Mon, Mar 23, 2026 at 3:00 AM Bartosz Golaszewski <brgl@kernel.org> wrote:
>
> On Sat, Mar 21, 2026 at 12:00 AM Rosen Penev <rosenp@gmail.com> wrote:
> >
> > >
> > > 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:
> > Maintainers for some inexplicable reason want an extra variable for
> > __counted_by works.
>
> 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);
>
> I didn't think about it and I do prefer it of course over an extra field.
>
> Bart
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] gpio: mockup: allocate lines with main struct
2026-03-23 16:43 ` Rosen Penev
@ 2026-03-24 9:16 ` Bartosz Golaszewski
2026-03-24 15:51 ` Rosen Penev
0 siblings, 1 reply; 12+ messages in thread
From: Bartosz Golaszewski @ 2026-03-24 9:16 UTC (permalink / raw)
To: Rosen Penev
Cc: Kees Cook, linux-gpio, Bamvor Jian Zhang, Linus Walleij,
Gustavo A. R. Silva, open list,
open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b,
Bartosz Golaszewski
On Mon, 23 Mar 2026 17:43:00 +0100, Rosen Penev <rosenp@gmail.com> said:
> On Mon, Mar 23, 2026 at 3:00 AM Bartosz Golaszewski <brgl@kernel.org> wrote:
>>
>> On Sat, Mar 21, 2026 at 12:00 AM Rosen Penev <rosenp@gmail.com> wrote:
>> >
>> > >
>> > > 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:
>> > Maintainers for some inexplicable reason want an extra variable for
>> > __counted_by works.
>>
>> 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);
The following spin on your patch builds fine for me:
diff --git a/drivers/gpio/gpio-mockup.c b/drivers/gpio/gpio-mockup.c
index a7d69f3835c1e..9427ab8c45f73 100644
--- a/drivers/gpio/gpio-mockup.c
+++ b/drivers/gpio/gpio-mockup.c
@@ -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
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH] gpio: mockup: allocate lines with main struct
2026-03-24 9:16 ` Bartosz Golaszewski
@ 2026-03-24 15:51 ` Rosen Penev
2026-03-24 15:54 ` Bartosz Golaszewski
0 siblings, 1 reply; 12+ messages in thread
From: Rosen Penev @ 2026-03-24 15:51 UTC (permalink / raw)
To: Bartosz Golaszewski
Cc: Kees Cook, linux-gpio, Bamvor Jian Zhang, Linus Walleij,
Gustavo A. R. Silva, open list,
open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b
On Tue, Mar 24, 2026 at 2:16 AM Bartosz Golaszewski <brgl@kernel.org> wrote:
>
> On Mon, 23 Mar 2026 17:43:00 +0100, Rosen Penev <rosenp@gmail.com> said:
> > On Mon, Mar 23, 2026 at 3:00 AM Bartosz Golaszewski <brgl@kernel.org> wrote:
> >>
> >> On Sat, Mar 21, 2026 at 12:00 AM Rosen Penev <rosenp@gmail.com> wrote:
> >> >
> >> > >
> >> > > 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:
> >> > Maintainers for some inexplicable reason want an extra variable for
> >> > __counted_by works.
> >>
> >> 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);
>
> The following spin on your patch builds fine for me:
>
> diff --git a/drivers/gpio/gpio-mockup.c b/drivers/gpio/gpio-mockup.c
> index a7d69f3835c1e..9427ab8c45f73 100644
> --- a/drivers/gpio/gpio-mockup.c
> +++ b/drivers/gpio/gpio-mockup.c
> @@ -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
> };
>
> 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
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] gpio: mockup: allocate lines with main struct
2026-03-24 15:51 ` Rosen Penev
@ 2026-03-24 15:54 ` Bartosz Golaszewski
2026-03-24 15:56 ` Rosen Penev
0 siblings, 1 reply; 12+ messages in thread
From: Bartosz Golaszewski @ 2026-03-24 15:54 UTC (permalink / raw)
To: Rosen Penev
Cc: Kees Cook, linux-gpio, Bamvor Jian Zhang, Linus Walleij,
Gustavo A. R. Silva, open list,
open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b
On Tue, Mar 24, 2026 at 4:52 PM Rosen Penev <rosenp@gmail.com> wrote:
>
> On Tue, Mar 24, 2026 at 2:16 AM Bartosz Golaszewski <brgl@kernel.org> wrote:
> >
> > On Mon, 23 Mar 2026 17:43:00 +0100, Rosen Penev <rosenp@gmail.com> said:
> > > On Mon, Mar 23, 2026 at 3:00 AM Bartosz Golaszewski <brgl@kernel.org> wrote:
> > >>
> > >> On Sat, Mar 21, 2026 at 12:00 AM Rosen Penev <rosenp@gmail.com> wrote:
> > >> >
> > >> > >
> > >> > > 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:
> > >> > Maintainers for some inexplicable reason want an extra variable for
> > >> > __counted_by works.
> > >>
> > >> 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);
> >
> > The following spin on your patch builds fine for me:
> >
> > diff --git a/drivers/gpio/gpio-mockup.c b/drivers/gpio/gpio-mockup.c
> > index a7d69f3835c1e..9427ab8c45f73 100644
> > --- a/drivers/gpio/gpio-mockup.c
> > +++ b/drivers/gpio/gpio-mockup.c
> > @@ -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
> > };
> >
Ok, what is this feature called in gcc parlance and where does this
info come from?
Bart
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] gpio: mockup: allocate lines with main struct
2026-03-24 15:54 ` Bartosz Golaszewski
@ 2026-03-24 15:56 ` Rosen Penev
2026-03-24 17:16 ` Bartosz Golaszewski
0 siblings, 1 reply; 12+ messages in thread
From: Rosen Penev @ 2026-03-24 15:56 UTC (permalink / raw)
To: Bartosz Golaszewski
Cc: Kees Cook, linux-gpio, Bamvor Jian Zhang, Linus Walleij,
Gustavo A. R. Silva, open list,
open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b
On Tue, Mar 24, 2026 at 8:54 AM Bartosz Golaszewski <brgl@kernel.org> wrote:
>
> On Tue, Mar 24, 2026 at 4:52 PM Rosen Penev <rosenp@gmail.com> wrote:
> >
> > On Tue, Mar 24, 2026 at 2:16 AM Bartosz Golaszewski <brgl@kernel.org> wrote:
> > >
> > > On Mon, 23 Mar 2026 17:43:00 +0100, Rosen Penev <rosenp@gmail.com> said:
> > > > On Mon, Mar 23, 2026 at 3:00 AM Bartosz Golaszewski <brgl@kernel.org> wrote:
> > > >>
> > > >> On Sat, Mar 21, 2026 at 12:00 AM Rosen Penev <rosenp@gmail.com> wrote:
> > > >> >
> > > >> > >
> > > >> > > 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:
> > > >> > Maintainers for some inexplicable reason want an extra variable for
> > > >> > __counted_by works.
> > > >>
> > > >> 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);
> > >
> > > The following spin on your patch builds fine for me:
> > >
> > > diff --git a/drivers/gpio/gpio-mockup.c b/drivers/gpio/gpio-mockup.c
> > > index a7d69f3835c1e..9427ab8c45f73 100644
> > > --- a/drivers/gpio/gpio-mockup.c
> > > +++ b/drivers/gpio/gpio-mockup.c
> > > @@ -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
> > > };
> > >
>
> 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.
>
> Bart
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] gpio: mockup: allocate lines with main struct
2026-03-24 15:56 ` Rosen Penev
@ 2026-03-24 17:16 ` Bartosz Golaszewski
2026-03-24 18:25 ` Rosen Penev
0 siblings, 1 reply; 12+ messages in thread
From: Bartosz Golaszewski @ 2026-03-24 17:16 UTC (permalink / raw)
To: Rosen Penev
Cc: Kees Cook, linux-gpio, Bamvor Jian Zhang, Linus Walleij,
Gustavo A. R. Silva, open list,
open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b
On Tue, Mar 24, 2026 at 4:56 PM Rosen Penev <rosenp@gmail.com> wrote:
>
> On Tue, Mar 24, 2026 at 8:54 AM Bartosz Golaszewski <brgl@kernel.org> wrote:
> >
> > On Tue, Mar 24, 2026 at 4:52 PM Rosen Penev <rosenp@gmail.com> wrote:
> > >
> > > On Tue, Mar 24, 2026 at 2:16 AM Bartosz Golaszewski <brgl@kernel.org> wrote:
> > > >
> > > > On Mon, 23 Mar 2026 17:43:00 +0100, Rosen Penev <rosenp@gmail.com> said:
> > > > > On Mon, Mar 23, 2026 at 3:00 AM Bartosz Golaszewski <brgl@kernel.org> wrote:
> > > > >>
> > > > >> On Sat, Mar 21, 2026 at 12:00 AM Rosen Penev <rosenp@gmail.com> wrote:
> > > > >> >
> > > > >> > >
> > > > >> > > 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:
> > > > >> > Maintainers for some inexplicable reason want an extra variable for
> > > > >> > __counted_by works.
> > > > >>
> > > > >> 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);
> > > >
> > > > The following spin on your patch builds fine for me:
> > > >
> > > > diff --git a/drivers/gpio/gpio-mockup.c b/drivers/gpio/gpio-mockup.c
> > > > index a7d69f3835c1e..9427ab8c45f73 100644
> > > > --- a/drivers/gpio/gpio-mockup.c
> > > > +++ b/drivers/gpio/gpio-mockup.c
> > > > @@ -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.
> > >
> > > *
Indeed, sorry. In that case I don't know what Kees meant. I'm fine
with a new variable.
Bartosz
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] gpio: mockup: allocate lines with main struct
2026-03-24 17:16 ` Bartosz Golaszewski
@ 2026-03-24 18:25 ` Rosen Penev
0 siblings, 0 replies; 12+ messages in thread
From: Rosen Penev @ 2026-03-24 18:25 UTC (permalink / raw)
To: Bartosz Golaszewski
Cc: Kees Cook, linux-gpio, Bamvor Jian Zhang, Linus Walleij,
Gustavo A. R. Silva, open list,
open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b
On Tue, Mar 24, 2026 at 10:16 AM Bartosz Golaszewski <brgl@kernel.org> wrote:
>
> On Tue, Mar 24, 2026 at 4:56 PM Rosen Penev <rosenp@gmail.com> wrote:
> >
> > On Tue, Mar 24, 2026 at 8:54 AM Bartosz Golaszewski <brgl@kernel.org> wrote:
> > >
> > > On Tue, Mar 24, 2026 at 4:52 PM Rosen Penev <rosenp@gmail.com> wrote:
> > > >
> > > > On Tue, Mar 24, 2026 at 2:16 AM Bartosz Golaszewski <brgl@kernel.org> wrote:
> > > > >
> > > > > On Mon, 23 Mar 2026 17:43:00 +0100, Rosen Penev <rosenp@gmail.com> said:
> > > > > > On Mon, Mar 23, 2026 at 3:00 AM Bartosz Golaszewski <brgl@kernel.org> wrote:
> > > > > >>
> > > > > >> On Sat, Mar 21, 2026 at 12:00 AM Rosen Penev <rosenp@gmail.com> wrote:
> > > > > >> >
> > > > > >> > >
> > > > > >> > > 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:
> > > > > >> > Maintainers for some inexplicable reason want an extra variable for
> > > > > >> > __counted_by works.
> > > > > >>
> > > > > >> 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);
> > > > >
> > > > > The following spin on your patch builds fine for me:
> > > > >
> > > > > diff --git a/drivers/gpio/gpio-mockup.c b/drivers/gpio/gpio-mockup.c
> > > > > index a7d69f3835c1e..9427ab8c45f73 100644
> > > > > --- a/drivers/gpio/gpio-mockup.c
> > > > > +++ b/drivers/gpio/gpio-mockup.c
> > > > > @@ -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.
> > > >
> > > > *
>
> 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.
>
> Bartosz
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2026-03-24 18:25 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-19 0:05 [PATCH] gpio: mockup: allocate lines with main struct Rosen Penev
2026-03-20 13:28 ` Linus Walleij
2026-03-20 18:10 ` Kees Cook
2026-03-20 23:00 ` Rosen Penev
2026-03-23 10:00 ` Bartosz Golaszewski
2026-03-23 16:43 ` Rosen Penev
2026-03-24 9:16 ` Bartosz Golaszewski
2026-03-24 15:51 ` Rosen Penev
2026-03-24 15:54 ` Bartosz Golaszewski
2026-03-24 15:56 ` Rosen Penev
2026-03-24 17:16 ` Bartosz Golaszewski
2026-03-24 18:25 ` Rosen Penev
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox