From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Dmitry Torokhov <dmitry.torokhov@gmail.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 22:39:00 +0300 [thread overview]
Message-ID: <atkU1BKblQsuCPSn@ashevche-desk.local> (raw)
In-Reply-To: <20261021190212.2237804-5-dmitry.torokhov@gmail.com>
On Wed, Oct 21, 2026 at 12:02:10PM -0700, Dmitry Torokhov wrote:
> Instead of allocating descs before the loop and reallocating it via
> krealloc() when candidate hardware array matching is detected on the
> first iteration, defer allocation of descs until the first descriptor
> is retrieved.
>
> Factor descriptor allocation and fast-path validation into helper
> functions gpio_descs_alloc() and gpio_descs_check_fastpath(). Inside
> the loop, acquire the descriptor using __free(gpiod_put), allocate
> descs once on the first iteration, check fast-path candidate
> suitability, and transfer descriptor ownership using no_free_ptr().
...
> +static void gpio_descs_check_fastpath(struct gpio_descs *descs,
> + struct gpio_desc *desc)
> +{
> + struct gpio_array *array_info = descs->info;
Split the assignment. It's robust for long-term maintenance to have assignment
coupled with a validation check.
> + unsigned int idx = descs->ndescs;
> + unsigned long dflags;
array_info = desc->info;
> + if (!array_info)
> + return;
> +
> + /* Unmark array members which don't belong to the 'fast' chip */
> + if (array_info->gdev != gpiod_to_gpio_device(desc)) {
> + __clear_bit(idx, array_info->get_mask);
> + __clear_bit(idx, array_info->set_mask);
> + return;
> + }
> +
> + /*
> + * Detect array members which belong to the 'fast' chip
> + * but their pins are not in hardware order.
> + */
> + if (gpiod_hwgpio(desc) != idx) {
> + /*
> + * Don't use fast path if all array members processed so
> + * far belong to the same chip as this one but its pin
> + * hardware number is different from its array index.
> + */
> + if (bitmap_full(array_info->get_mask, idx)) {
> + descs->info = NULL;
> + } else {
> + __clear_bit(idx, array_info->get_mask);
> + __clear_bit(idx, array_info->set_mask);
> + }
> + return;
> + }
> +
> + dflags = READ_ONCE(desc->flags);
> + /* Exclude open drain or open source from fast output */
> + if (test_bit(GPIOD_FLAG_OPEN_DRAIN, &dflags) ||
> + test_bit(GPIOD_FLAG_OPEN_SOURCE, &dflags))
> + __clear_bit(idx, array_info->set_mask);
> +
> + /* Identify 'fast' pins which require invertion */
> + if (gpiod_is_active_low(desc))
> + __set_bit(idx, array_info->invert_mask);
> +}
--
With Best Regards,
Andy Shevchenko
next prev parent reply other threads:[~2026-10-21 19:39 UTC|newest]
Thread overview: 9+ 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 19:39 ` Andy Shevchenko [this message]
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=atkU1BKblQsuCPSn@ashevche-desk.local \
--to=andriy.shevchenko@intel.com \
--cc=andy@kernel.org \
--cc=brgl@kernel.org \
--cc=dmitry.torokhov@gmail.com \
--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®