From: Dmitry Torokhov <dmitry.torokhov@gmail.com>
To: Andy Shevchenko <andriy.shevchenko@intel.com>
Cc: Linus Walleij <linusw@kernel.org>,
Bartosz Golaszewski <brgl@kernel.org>,
Andy Shevchenko <andy@kernel.org>,
Janusz Krzysztofik <jmkrzyszt@gmail.com>,
linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 5/5] gpiolib: avoid krealloc() in gpiod_get_array()
Date: Wed, 21 Oct 2026 14:17:56 -0700 [thread overview]
Message-ID: <atkp9edGVHRWrJCa@google.com> (raw)
In-Reply-To: <atkmj6rxedA-XPf-@ashevche-desk.local>
On Wed, Oct 21, 2026 at 11:54:39PM +0300, Andy Shevchenko wrote:
> On Wed, Oct 21, 2026 at 01:39:49PM -0700, Dmitry Torokhov wrote:
> > On Wed, Oct 21, 2026 at 10:34:01PM +0300, Andy Shevchenko wrote:
> > > On Wed, Oct 21, 2026 at 12:02:10PM -0700, Dmitry Torokhov wrote:
>
> ...
>
> > > > + bitmap_set(array_info->get_mask, 0, count);
> > > > + bitmap_set(array_info->set_mask, 0, count);
> > >
> > > With the above being done, I would rather see bitmap_zalloc() for bitmaps.
> >
> > struct gpio_array is defined with a flexible array member (invert_mask[])
> > specifically so that descs, array_info, and all three bitmaps live in a
> > single allocation. Switching to bitmap_zalloc() would turn one allocation
> > into four or five separate allocations and require freeing the bitmaps
> > both in gpiod_put_array() and when cancelling the fast path in
> > gpio_descs_check_fastpath(), so I would prefer to keep the single
> > contiguous allocation here.
>
> Why? You can also allocate 3x bitmaps with bitmap_zalloc(). So I see only
> a single additional allocation.
So the proposal is to continue combining gpio_descs and gpio_array
allocation but split out bitmap allocations (still combining them into
one, so needing to round up nbits).
This looks like worst combination of all. Why do you prefer
bitmap_zalloc()?
>
> ...
>
> > > > count = gpiod_count(dev, con_id);
> > > > - if (count < 0)
> > > > - return ERR_PTR(count);
> > >
> > > Leave this and simply add
> > >
> > > > - struct gpio_descs *descs __free(gpiod_put_array) =
> > > > - kzalloc_flex(*descs, desc, count);
> > > > - if (!descs)
> > > > - return ERR_PTR(-ENOMEM);
> > > > + if (count <= 0)
> > > > + return ERR_PTR(count ?: -ENOENT);
> > >
> > > if (count == 0)
> > > return -ENOENT;
> > >
> > > The currently proposed code is less readable.
> >
> > Looking closer at gpiod_count(), all of its real backends
> > (of_gpio_count(), acpi_gpio_count(), swnode_gpio_count(), and
> > platform_gpio_count()) return either a positive count or -ENOENT, and
> > the !CONFIG_GPIO_ACPI stub for acpi_gpio_count() returns -ENODEV. The
> > only outlier is the !CONFIG_OF_GPIO stub for of_gpio_count() in
> > drivers/gpio/gpiolib-of.h, which returns 0 (and also causes
> > gpiod_count() to skip the if (count < 0) fallback to
> > platform_gpio_count()).
> >
> > Do we actually need to check for count == 0 in gpiod_get_array(), or can
> > we fix the !CONFIG_OF_GPIO stub of of_gpio_count() to return -ENOENT and
> > leave the count < 0 check in gpiod_get_array() untouched?
>
> I would fix of_gpio_count() stub. But I haven't check its users for correctness
> of such a change. I think it would be weird for them to expect 0 when OF_GPIO=n.
OK, I can fix up the stub. The only user of of_gpio_count() is
gpiod_count().
>
> ...
>
> > > > for (i = 0; i < count; i++) {
> > > > - desc = gpiod_get_index(dev, con_id, i, flags);
> > > > + struct gpio_desc *desc __free(gpiod_put) =
> > > > + gpiod_get_index(dev, con_id, i, flags);
> > > > if (IS_ERR(desc))
> > > > return ERR_CAST(desc);
> > > >
> > > > + if (i == 0) {
> > >
> > > This is almost invariant to the loop. Can we move it out?
> > >
> > > > + descs = gpio_descs_alloc(count, desc);
> > > > + if (!descs)
> > > > return ERR_PTR(-ENOMEM);
> > > > }
> > > >
> > > > + gpio_descs_check_fastpath(descs, desc);
> > > >
> > > > + descs->desc[descs->ndescs++] = no_free_ptr(desc);
> > > > }
> >
> > gpio_descs_alloc() needs the first descriptor (desc at index 0, acquired
> > by gpiod_get_index() inside the loop) to check gpiod_hwgpio(desc) and
> > gdev->ngpio so it knows whether to allocate space for array_info and the
> > bitmaps up front instead of reallocating with krealloc(). Before the
> > loop we have not looked up the first descriptor yet.
>
> That's why I put 'almost' which means we might need to duplicate a few calls
> outside as well.
Yeah, I think doing it inside the loop rather than duplication
gpiod_get_index() outside makes it cleaner.
Thanks.
--
Dmitry
next prev parent reply other threads:[~2026-10-21 21:18 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-21 19:02 [PATCH 1/5] gpiolib: fix GPIO descriptor leak " Dmitry Torokhov
2026-10-21 19:02 ` [PATCH 2/5] gpiolib: fix fast-path cancellation " Dmitry Torokhov
2026-10-21 19:02 ` [PATCH 3/5] gpiolib: add support for scope-based management of GPIO descriptors Dmitry Torokhov
2026-10-21 19:02 ` [PATCH 4/5] gpiolib: use __free(gpiod_put_array) in gpiod_get_array() Dmitry Torokhov
2026-10-21 19:02 ` [PATCH 5/5] gpiolib: avoid krealloc() " Dmitry Torokhov
2026-10-21 19:34 ` Andy Shevchenko
2026-10-21 20:39 ` Dmitry Torokhov
2026-10-21 20:54 ` Andy Shevchenko
2026-10-21 21:17 ` Dmitry Torokhov [this message]
2026-10-21 19:39 ` Andy Shevchenko
2026-10-21 19:36 ` [PATCH 1/5] gpiolib: fix GPIO descriptor leak " Andy Shevchenko
2026-10-21 19:43 ` Dmitry Torokhov
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=atkp9edGVHRWrJCa@google.com \
--to=dmitry.torokhov@gmail.com \
--cc=andriy.shevchenko@intel.com \
--cc=andy@kernel.org \
--cc=brgl@kernel.org \
--cc=jmkrzyszt@gmail.com \
--cc=linusw@kernel.org \
--cc=linux-gpio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/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
all inboxes | Powered by JetHome®