From: Peng Fan <peng.fan@oss.nxp.com>
To: sashiko-reviews@lists.linux.dev
Cc: linux-gpio@vger.kernel.org, Frank.Li@kernel.org, imx@lists.linux.dev
Subject: Re: [PATCH v6 10/13] gpio: mxc: use devres functions simplify probe code
Date: Mon, 12 Oct 2026 12:12:09 +0800 [thread overview]
Message-ID: <asxeGf7LQN+B6Kht@shlinux89> (raw)
In-Reply-To: <sashiko-outbox-167212@kernel.org>
On Sun, Oct 11, 2026 at 12:05:31PM +0000, sashiko-bot@kernel.org wrote:
>Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
>- [High] gpio: use-after-free and premature exposure of irq_domain
>--
>
>commit e70e13ad3287f8fd0b2357d428d23270ccfc9522
>Author: Peng Fan <peng.fan@nxp.com>
>
>gpio: mxc: use devres functions simplify probe code
>
>This commit replaces legacy IRQ domain creation and manual PM runtime error
>handling with devres equivalents in the MXC GPIO driver probe. It removes
>error path labels by relying on device-managed allocations to unwind resources.
>
>> diff --git a/drivers/gpio/gpio-mxc.c b/drivers/gpio/gpio-mxc.c
>> index 8dcfd05e658a6..09acb186c84c1 100644
>> --- a/drivers/gpio/gpio-mxc.c
>> +++ b/drivers/gpio/gpio-mxc.c
>
>[ ... ]
>
>> @@ -534,27 +536,31 @@ static int mxc_gpio_probe(struct platform_device *pdev)
>>
>> err = devm_gpiochip_add_data(dev, &port->gen_gc.gc, port);
>> if (err)
>> - goto out_bgio;
>> + return err;
>
>[Severity: High]
>Does this premature exposure of the GPIO chip cause a use-after-free risk?
>
>By calling devm_gpiochip_add_data() before devm_irq_domain_instantiate(), the
>GPIO chip is registered and immediately exposed to kernel consumers and
>userspace.
This is a pre-existing issue. Seems I need to add another fix patch
before doing the cleanup.
Regards
Peng
>
>If a consumer requests an IRQ during this window, mxc_gpio_to_irq() will
>execute:
>
>drivers/gpio/gpio-mxc.c:mxc_gpio_to_irq() {
> struct mxc_gpio_port *port = gpiochip_get_data(gc);
>
> return irq_find_mapping(port->domain, offset);
>}
>
>This passes a NULL port->domain to irq_find_mapping(), failing to map the IRQ.
>
>Additionally, if a subsequent probe step like mxc_gpio_init_gc() fails, devres
>unwinds allocations in reverse order (LIFO). Devres will first destroy the IRQ
>domain, leaving port->domain as a dangling pointer to freed memory.
>
>The GPIO chip remains fully registered and exposed until its own devres release
>action runs later. A concurrent gpiod_to_irq() call during this unwinding
>window will dereference the freed port->domain, causing a use-after-free.
>
>Should devm_irq_domain_instantiate() be called before
>devm_gpiochip_add_data() to ensure the domain is fully prepared before exposure
>and destroyed after the chip is unregistered?
>
>>
>> irq_base = devm_irq_alloc_descs(dev, -1, 0, 32, numa_node_id());
>> - if (irq_base < 0) {
>> - err = irq_base;
>> - goto out_bgio;
>> - }
>> + if (irq_base < 0)
>> + return irq_base;
>> +
>> + d_info = (struct irq_domain_info) {
>> + .fwnode = dev_fwnode(dev),
>> + .size = 32,
>> + .hwirq_max = 32,
>> + .virq_base = irq_base,
>> + .ops = &irq_domain_simple_ops,
>> + .dev = dev,
>> + };
>>
>> - port->domain = irq_domain_create_legacy(dev_fwnode(dev), 32, irq_base, 0,
>> - &irq_domain_simple_ops, NULL);
>> - if (!port->domain) {
>> - err = -ENODEV;
>> - goto out_bgio;
>> - }
>> + port->domain = devm_irq_domain_instantiate(dev, &d_info);
>> + if (IS_ERR(port->domain))
>> + return PTR_ERR(port->domain);
>>
>> irq_domain_set_pm_device(port->domain, dev);
>>
>> /* gpio-mxc can be a generic irq chip */
>> err = mxc_gpio_init_gc(port, irq_base);
>> if (err < 0)
>> - goto out_irqdomain_remove;
>> + return err;
>
>--
>Sashiko AI review · https://sashiko.dev/#/patchset/20261011-gpio-mxc-cleanup-v6-0-cb47ce2ad758@nxp.com?part=10
>
next prev parent reply other threads:[~2026-10-12 4:07 UTC|newest]
Thread overview: 42+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-11 11:51 [PATCH v6 00/13] gpio: mxc: bug fixes and probe cleanup Peng Fan (OSS)
2026-10-11 11:51 ` [PATCH v6 01/13] gpio: mxc: fix race between chained IRQ handler install and probe completion Peng Fan (OSS)
2026-10-11 12:05 ` sashiko-bot
2026-10-12 4:14 ` Peng Fan
2026-10-11 11:51 ` [PATCH v6 02/13] gpio: mxc: fix both_edges bit operations Peng Fan (OSS)
2026-10-12 9:18 ` Andy Shevchenko
2026-10-12 9:52 ` Peng Fan
2026-10-11 11:51 ` [PATCH v6 03/13] gpio: mxc: fix wakeup_pads " Peng Fan (OSS)
2026-10-12 9:15 ` Andy Shevchenko
2026-10-12 9:51 ` Peng Fan
2026-10-12 10:30 ` Andy Shevchenko
2026-10-12 11:41 ` Peng Fan
2026-10-12 18:25 ` Andy Shevchenko
2026-10-11 11:51 ` [PATCH v6 04/13] gpio: mxc: use irq_set_irq_wake() to simplify code Peng Fan (OSS)
2026-10-11 11:51 ` [PATCH v6 05/13] gpio: mxc: use for_each_set_bit() to iterate wakeup pads Peng Fan (OSS)
2026-10-12 9:19 ` Andy Shevchenko
2026-10-12 9:53 ` Peng Fan
2026-10-11 11:51 ` [PATCH v6 06/13] gpio: mxc: replace of_device_is_compatible() with hwdata flags Peng Fan (OSS)
2026-10-12 9:25 ` Andy Shevchenko
2026-10-12 9:59 ` Peng Fan
2026-10-12 10:32 ` Andy Shevchenko
2026-10-11 11:51 ` [PATCH v6 07/13] gpio: mxc: convert pad wakeup compatible checks to " Peng Fan (OSS)
2026-10-12 9:27 ` Andy Shevchenko
2026-10-12 10:01 ` Peng Fan
2026-10-12 14:31 ` Frank Li
2026-10-11 11:51 ` [PATCH v6 08/13] gpio: mxc: use local dev variable Peng Fan (OSS)
2026-10-12 9:30 ` Andy Shevchenko
2026-10-12 10:05 ` Peng Fan
2026-10-12 10:39 ` Andy Shevchenko
2026-10-12 11:52 ` Peng Fan
2026-10-11 11:51 ` [PATCH v6 09/13] gpio: mxc: use cleanup guard for pm_runtime_get_noresume() balance Peng Fan (OSS)
2026-10-12 9:38 ` Andy Shevchenko
2026-10-12 10:10 ` Peng Fan
2026-10-12 10:41 ` Andy Shevchenko
2026-10-11 11:51 ` [PATCH v6 10/13] gpio: mxc: use devres functions simplify probe code Peng Fan (OSS)
2026-10-11 12:05 ` sashiko-bot
2026-10-12 4:12 ` Peng Fan [this message]
2026-10-11 11:51 ` [PATCH v6 11/13] gpio: mxc: add dev_err_probe() for error paths to help diagnostics Peng Fan (OSS)
2026-10-11 12:04 ` sashiko-bot
2026-10-12 4:13 ` Peng Fan
2026-10-11 11:51 ` [PATCH v6 12/13] gpio: mxc: introduce MXC_ICR macros and use field_prep/field_get Peng Fan (OSS)
2026-10-11 11:51 ` [PATCH v6 13/13] gpio: mxc: use BIT() macro for single-bit operations Peng Fan (OSS)
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=asxeGf7LQN+B6Kht@shlinux89 \
--to=peng.fan@oss.nxp.com \
--cc=Frank.Li@kernel.org \
--cc=imx@lists.linux.dev \
--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