On Mon, 23 Mar 2026 17:43:00 +0100, Rosen Penev <[email protected]> said:
> On Mon, Mar 23, 2026 at 3:00 AM Bartosz Golaszewski <[email protected]> wrote:
>>
>> On Sat, Mar 21, 2026 at 12:00 AM Rosen Penev <[email protected]> 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