On Fri, Mar 20, 2026 at 11:10 AM Kees Cook <[email protected]> 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 <[email protected]>
> > ---
> > 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