Linux GPIO subsystem development
 help / color / mirror / Atom feed
From: Dmitry Torokhov <dmitry.torokhov@gmail.com>
To: Bartosz Golaszewski <brgl@kernel.org>
Cc: sashiko-reviews@lists.linux.dev, linux-gpio@vger.kernel.org
Subject: Re: [PATCH v2 6/7] gpiolib: avoid krealloc() in gpiod_get_array()
Date: Wed, 4 Nov 2026 09:13:37 -0800	[thread overview]
Message-ID: <autm__StHhgBMcdd@google.com> (raw)
In-Reply-To: <atiydn7hetz4pfgpgybjfcdcwugp3rnqqbovtm6h45kaeh5bov@tqkc4qqm5zbx>

On Wed, Nov 04, 2026 at 03:23:26PM +0100, Bartosz Golaszewski wrote:
> On Wed, Nov 04, 2026 at 03:48:34AM +0000, sashiko-bot@kernel.org wrote:
> > Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
> > - [Low] gpiolib: delayed initialization of __free() variable in gpiod_get_array()
> > --
> > 
> > commit ab11568406b8d4aa5255ce95e09509f3e6c86285
> > Author: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> > 
> > gpiolib: avoid krealloc() in gpiod_get_array()
> > 
> > This commit defers allocating the gpio_descs structure in gpiod_get_array()
> > until the first GPIO descriptor is retrieved so that it can be allocated in
> > a single pass without needing krealloc(). It also factors descriptor
> > allocation and fast-path checks into helper functions.
> >
> 
> ...
> 
> > > +			descs = gpio_descs_alloc(count, desc);
> > > +			if (!descs)
> > >  				return ERR_PTR(-ENOMEM);
> > > -
> > > -			descs = array;
> > > -
> > > -			array_info = (void *)descs + descs_size;
> > > -			array_info->get_mask = array_info->invert_mask +
> > > -						  bitmap_size;
> > > -			array_info->set_mask = array_info->get_mask +
> > > -						  bitmap_size;
> > > -
> > > -			array_info->desc = descs->desc;
> > > -			array_info->size = count;
> > > -			array_info->gdev = gdev;
> > > -			bitmap_set(array_info->get_mask, 0, count);
> > > -			bitmap_set(array_info->set_mask, 0, count);
> > > -			descs->info = array_info;
> > >  		}
> > 
> > [Severity: Low]
> > This isn't a bug, but in gpiod_get_array(), descs is declared at the top of
> > the function with __free(gpiod_put_array) = NULL and then assigned later
> > inside the loop when i == 0.
> > 
> > The cleanup.h conventions ask that __free() variables be defined and
> > initialized in a single statement at the point of resource acquisition
> > rather than initialized to NULL at the top of the function and assigned
> > later. Could the first descriptor be fetched before the loop, or the
> > allocation structured so that descs is defined and initialized in one
> > statement?
> >
> 
> Hi Dmitry, please follow the cleanup.h conventions here and in the
> subsequent patch.

Hi Bart,

The convention (combining declaration with allocation) will not work
here because that would make the allocation have wrong scope.

If you want I can move declaration + NULL initialization just before the
loop. The topic of pulling allocation out of the loop was discussed with
Andy, it would result in duplicating the code and making the result much
worse.

Thanks.

-- 
Dmitry

  reply	other threads:[~2026-11-04 17:13 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-04  3:40 [PATCH v2 0/7] gpiolib: scope-based management and cleanup for GPIO descriptor arrays Dmitry Torokhov
2026-11-04  3:40 ` [PATCH v2 1/7] gpiolib: fix GPIO descriptor leak in gpiod_get_array() Dmitry Torokhov
2026-11-04 12:06   ` Andy Shevchenko
2026-11-04 12:08   ` Andy Shevchenko
2026-11-04  3:40 ` [PATCH v2 2/7] gpiolib: fix fast-path cancellation " Dmitry Torokhov
2026-11-04 12:09   ` Andy Shevchenko
2026-11-04  3:40 ` [PATCH v2 3/7] gpiolib: of: return -ENOENT from of_gpio_count() stub Dmitry Torokhov
2026-11-04 12:07   ` Andy Shevchenko
2026-11-04  3:40 ` [PATCH v2 4/7] gpiolib: add support for scope-based management of GPIO descriptors Dmitry Torokhov
2026-11-04 12:10   ` Andy Shevchenko
2026-11-04  3:40 ` [PATCH v2 5/7] gpiolib: use __free(gpiod_put_array) in gpiod_get_array() Dmitry Torokhov
2026-11-04 12:13   ` Andy Shevchenko
2026-11-04  3:40 ` [PATCH v2 6/7] gpiolib: avoid krealloc() " Dmitry Torokhov
2026-11-04  3:48   ` sashiko-bot
2026-11-04 14:23     ` Bartosz Golaszewski
2026-11-04 17:13       ` Dmitry Torokhov [this message]
2026-11-04 12:20   ` Andy Shevchenko
2026-11-04 18:14     ` Dmitry Torokhov
2026-11-04 21:40       ` Andy Shevchenko
2026-11-04 21:51         ` Dmitry Torokhov
2026-11-04 22:05           ` Andy Shevchenko
2026-11-04  3:40 ` [PATCH v2 7/7] gpiolib: use bitmap_zalloc() for fastpath bitmaps Dmitry Torokhov
2026-11-04  3:50   ` sashiko-bot
2026-11-04 17:14     ` Dmitry Torokhov
2026-11-04 12:17   ` Andy Shevchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=autm__StHhgBMcdd@google.com \
    --to=dmitry.torokhov@gmail.com \
    --cc=brgl@kernel.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox