Re: [PATCH] gpio: mockup: allocate lines with main struct

From: Bartosz Golaszewski

Date: Tue Mar 24 2026 - 11:59:09 EST


On Tue, Mar 24, 2026 at 4:52 PM Rosen Penev <rosenp@xxxxxxxxx> wrote:
>
> On Tue, Mar 24, 2026 at 2:16 AM Bartosz Golaszewski <brgl@xxxxxxxxxx> wrote:
> >
> > On Mon, 23 Mar 2026 17:43:00 +0100, Rosen Penev <rosenp@xxxxxxxxx> said:
> > > On Mon, Mar 23, 2026 at 3:00 AM Bartosz Golaszewski <brgl@xxxxxxxxxx> wrote:
> > >>
> > >> On Sat, Mar 21, 2026 at 12:00 AM Rosen Penev <rosenp@xxxxxxxxx> 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