From: sashiko-bot@kernel.org
To: "Szuying Chen" <Chloe_Chen@asmedia.com.tw>, linux-gpio@vger.kernel.org
Subject: Re: [PATCH v4] gpio: amdptpci: add support for AMD PT PCI controller
Date: Mon, 16 Nov 2026 08:42:54 +0000 [thread overview]
Message-ID: <sashiko-outbox-216556@kernel.org> (raw)
In-Reply-To: <20261116075510.5722-1-Chloe_Chen@asmedia.com.tw>
Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] gpio: amdptpci: devres order in pt27_gpio_probe() frees
irq_domain early
- [High] gpio: amdptpci: MMIO access while suspended and clobber in
pt27_restore_regs()
- [High] gpio: amdptpci: runtime PM calls in pt27_gpio_isr() break IRQs
and lockdep
- [Medium] gpio: amdptpci: sleep ops in pt27_gpio_pm_ops race noirq wake
IRQ handling
- [Medium] gpio: amdptpci: unmasked wake_status and skipped wake disable
on suspend
- [Medium] gpio: amdptpci: duplicate wake IRQ dispatch in
pt27_gpio_resume()
--
commit afe67f7f6f93220edaeaabd38709a16694605523
Author: Szuying Chen <Chloe_Chen@asmedia.com.tw>
gpio: amdptpci: add support for AMD PT PCI controller
This commit adds a PCI GPIO and interrupt controller driver for AMD
Promontory (PT27) platforms, including runtime PM and system wakeup support.
> diff --git a/drivers/gpio/gpio-amdptpci.c b/drivers/gpio/gpio-amdptpci.c
> new file mode 100644
> index 0000000000000..d676fd8c0b1c7
> --- /dev/null
> +++ b/drivers/gpio/gpio-amdptpci.c
[ ... ]
> +static void pt27_restore_regs(struct pt27_gpio *chip)
> +{
> + iowrite32(chip->saved.out, chip->base + PT27_REG_OUT);
> + iowrite32(chip->saved.dir, chip->base + PT27_REG_DIR);
> +
> + iowrite32(chip->saved.int_lvl, chip->base + PT27_REG_INT_LVL);
> + iowrite32(chip->saved.int_mode0, chip->base + PT27_REG_INT_MODE0);
> + iowrite16(chip->saved.int_mode1, chip->base + PT27_REG_INT_MODE1);
> + iowrite8(chip->saved.int_ctl, chip->base + PT27_REG_INT_CTL);
> +
> + iowrite16(chip->saved.wake_lvl, chip->base + PT27_REG_WAKE_LVL);
> + iowrite16(chip->saved.wake_mode, chip->base + PT27_REG_WAKE_MODE);
> + iowrite16(chip->saved.wake_en, chip->base + PT27_REG_WAKE_EN);
> +
> + iowrite32(chip->saved.int_en, chip->base + PT27_REG_INT_EN);
> + iowrite32(chip->saved.int_mask, chip->base + PT27_REG_INT_MASK);
> +}
[ ... ]
> +static int pt27_irq_reqres(struct irq_data *d)
> +{
> + struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
> + int ret;
> +
> + ret = pm_runtime_resume_and_get(gc->parent);
> + if (ret < 0) {
> + dev_err(gc->parent, "%s: Fail to resume controller for IRQ: %d\n", __func__, ret);
> + return ret;
> + }
> +
> + ret = gpiochip_reqres_irq(gc, irqd_to_hwirq(d));
> + if (ret) {
> + pm_runtime_put_autosuspend(gc->parent);
> + return ret;
> + }
> + return 0;
> +}
[ ... ]
> +static int pt27_irq_set_type(struct irq_data *d, unsigned int type)
> +{
[ ... ]
> + scoped_guard(raw_spinlock_irqsave, &chip->gen.lock) {
> + lvl_reg = ioread32(chip->base + PT27_REG_INT_LVL);
> + if (lvl_bit)
> + lvl_reg |= BIT(hwirq);
> + else
> + lvl_reg &= ~BIT(hwirq);
> +
> + iowrite32(lvl_reg, chip->base + PT27_REG_INT_LVL);
> +
> + if (hwirq < 16) {
> + u32 reg = ioread32(chip->base + PT27_REG_INT_MODE0);
> +
> + shift = hwirq * 2;
> + reg &= ~(PT27_INT_MODE_MASK << shift);
> + reg |= ((u32)mode_val << shift);
> + iowrite32(reg, chip->base + PT27_REG_INT_MODE0);
> + } else {
> + u16 reg = ioread16(chip->base + PT27_REG_INT_MODE1);
> +
> + shift = (hwirq - 16) * 2;
> + reg &= ~((u16)(PT27_INT_MODE_MASK << shift));
> + reg |= (u16)((u16)mode_val << shift);
> + iowrite16(reg, chip->base + PT27_REG_INT_MODE1);
> + }
> +
> + iowrite32(BIT(hwirq), chip->base + PT27_REG_INT_STAT);
> +
> + }
[ ... ]
> +static int pt27_irq_set_wake(struct irq_data *d, unsigned int on)
> +{
[ ... ]
> + guard(raw_spinlock_irqsave)(&chip->gen.lock);
> +
> + wake_en = ioread16(chip->base + PT27_REG_WAKE_EN);
> + wake_lvl = ioread16(chip->base + PT27_REG_WAKE_LVL);
> + wake_mode = ioread16(chip->base + PT27_REG_WAKE_MODE);
[Severity: High]
Can GPIO IRQ setup access MMIO while the PCI device is in D3 and then have
pt27_restore_regs() overwrite the configuration on resume?
Runtime PM references are only taken in pt27_gpio_request() and
pt27_irq_reqres(). Standard IRQ mapping paths such as
acpi_dev_gpio_irq_wake_get_by() configure input direction via
gpiod_direction_input_nonotify() -> gpio_mmio_dir_in() and trigger type via
irq_set_irq_type() -> pt27_irq_set_type() before request_irq() is called and
without calling gpiod_request().
If the controller has already runtime-suspended after probe, those MMIO
reads and writes happen while the device is in D3hot/D3cold and do not
update chip->saved. When request_threaded_irq() then calls
pt27_irq_reqres(), pm_runtime_resume_and_get() runs pt27_restore_regs(),
clobbering PT27_REG_DIR, PT27_REG_INT_LVL, PT27_REG_INT_MODE0/1, and
PT27_REG_WAKE_* with the pre-suspend values in chip->saved. Right after
that, gpiochip_reqres_irq() -> gpiochip_lock_as_irq() reads PT27_REG_DIR
from hardware via gpio_mmio_get_dir() and fails with -EIO if the pin was
previously an output, or leaves the interrupt trigger unconfigured.
Also, if irq_set_irq_type() is called after enable_irq_wake(),
pt27_irq_set_type() does not update PT27_REG_WAKE_LVL or PT27_REG_WAKE_MODE.
[ ... ]
> +static irqreturn_t pt27_gpio_isr(int irq, void *data)
> +{
> + struct pt27_gpio *chip = data;
> + irqreturn_t ret = IRQ_NONE;
> + unsigned long pending;
> +
> + if (pm_runtime_get_if_in_use(chip->dev) <= 0)
> + return IRQ_NONE;
[ ... ]
> + pm_runtime_put(chip->dev);
> + return ret;
> +}
[Severity: High]
Does calling pm_runtime_get_if_in_use() and pm_runtime_put() in
pt27_gpio_isr() break interrupt handling when CONFIG_PM is disabled and take
a sleeping spinlock in hardirq context?
GPIO_AMDPTPCI does not depend on PM, and when CONFIG_PM=n,
pm_runtime_get_if_in_use() returns -EINVAL, causing pt27_gpio_isr() to
return IRQ_NONE on every interrupt. Even with CONFIG_PM=y,
pm_runtime_get_if_in_use() returns 0 while runtime_status is still
RPM_RESUMING (after pt27_restore_regs() unmasks interrupts in
pt27_gpio_runtime_resume()) and returns -EINVAL during early system resume
before runtime PM is re-enabled.
In addition, pt27_gpio_isr() is requested with IRQF_NO_THREAD, so calling
pm_runtime_get_if_in_use() and pm_runtime_put() acquires dev->power.lock (a
non-raw spinlock_t) from hardirq context, triggering lockdep invalid wait
context warnings under CONFIG_PROVE_RAW_LOCK_NESTING and sleeping in atomic
context on PREEMPT_RT.
> +static void pt27_hw_clear(void *data)
> +{
> + struct pt27_gpio *chip = data;
> +
> + iowrite32(0, chip->base + PT27_REG_INT_EN);
> + iowrite32(PT27_GPIO_MASK, chip->base + PT27_REG_INT_MASK);
> + iowrite32(PT27_GPIO_MASK, chip->base + PT27_REG_INT_STAT);
> + iowrite16(0, chip->base + PT27_REG_WAKE_EN);
> + iowrite16(PT27_WAKE_MASK, chip->base + PT27_REG_WAKE_STAT);
> +}
[ ... ]
> +static int pt27_gpio_probe(struct pci_dev *pdev, const struct pci_device_id *id)
> +{
[ ... ]
> + ret = devm_request_irq(dev, chip->irq, pt27_gpio_isr,
> + irq_flags | IRQF_NO_THREAD, dev_name(dev), chip);
> + if (ret) {
> + dev_err_probe(dev, ret, "failed to request IRQ %d\n", chip->irq);
> + goto err_pm_disable;
> + }
> +
> + ret = devm_add_action_or_reset(dev, pt27_hw_clear, chip);
> + if (ret)
> + goto err_pm_disable;
> +
> + ret = devm_gpiochip_add_data(dev, gc, chip);
[Severity: High]
Does this devres registration order in pt27_gpio_probe() cause a
use-after-free of chip->gen.gc.irq.domain in pt27_gpio_isr() during unbind?
Because devm_request_irq() and pt27_hw_clear() are registered before
devm_gpiochip_add_data(), devres teardown runs in reverse order when the
driver is unbound:
CPU0 CPU1
pci_device_remove()
pt27_gpio_remove()
pm_runtime_forbid(dev)
pt27_restore_regs()
devres_release_all()
gpiochip_remove()
irq_domain_remove(gc->irq.domain)
<hardirq>
pt27_gpio_isr()
generic_handle_domain_irq(
chip->gen.gc.irq.domain, hwirq)
<-- use-after-free
pt27_hw_clear()
devm_irq_release()
During probe, chip->gen.gc.irq.domain is also still NULL between
devm_request_irq() and devm_gpiochip_add_data(). In addition,
pt27_hw_clear() modifies PT27_REG_INT_EN, PT27_REG_INT_MASK, and
PT27_REG_INT_STAT without holding chip->gen.lock while pt27_gpio_isr() is
still active, and pt27_gpio_isr() does not mask pending with PT27_GPIO_MASK.
Registering devm_gpiochip_add_data() before pt27_hw_clear() and
devm_request_irq() ensures the IRQ handler is freed first on teardown.
[ ... ]
> +static int __maybe_unused pt27_gpio_suspend(struct device *dev)
> +{
> + struct pt27_gpio *chip = dev_get_drvdata(dev);
> +
> + if (pm_runtime_status_suspended(dev)) {
> + dev_dbg(dev, "%s: already runtime-suspended, skipping save\n", __func__);
> + return 0;
> + }
> +
> + scoped_guard(raw_spinlock_irqsave, &chip->gen.lock) {
> + pt27_save_regs(chip);
> + iowrite32(0, chip->base + PT27_REG_INT_EN);
> + iowrite32(PT27_GPIO_MASK, chip->base + PT27_REG_INT_MASK);
> + iowrite32(PT27_GPIO_MASK, chip->base + PT27_REG_INT_STAT);
> +
> + if (!device_may_wakeup(dev))
> + iowrite16(0, chip->base + PT27_REG_WAKE_EN);
> +
> + iowrite16(PT27_WAKE_MASK, chip->base + PT27_REG_WAKE_STAT);
> + }
> +
> + synchronize_irq(chip->irq);
> + return 0;
> +}
> +
> +static int __maybe_unused pt27_gpio_resume(struct device *dev)
> +{
> + struct pt27_gpio *chip = dev_get_drvdata(dev);
> + unsigned long replay;
> + unsigned int hwirq;
> + u16 wake_status;
> +
> + if (pm_runtime_status_suspended(dev))
> + return 0;
> +
> + scoped_guard(raw_spinlock_irqsave, &chip->gen.lock) {
> + wake_status = ioread16(chip->base + PT27_REG_WAKE_STAT) & PT27_WAKE_MASK;
> + iowrite16(wake_status, chip->base + PT27_REG_WAKE_STAT);
> + pt27_restore_regs(chip);
> +
> + replay = wake_status &
> + ioread32(chip->base + PT27_REG_INT_EN) &
> + ~ioread32(chip->base + PT27_REG_INT_MASK);
> + }
> +
> + if (!wake_status)
> + return 0;
> +
> + pm_wakeup_event(dev, 0);
[Severity: Medium]
Should wake_status in pt27_gpio_resume() be masked with chip->wake_mask, and
what happens in pt27_gpio_suspend() if wakeup was disabled after the device
entered runtime suspend?
In pt27_gpio_resume() (which is also called by pt27_gpio_runtime_resume()),
wake_status is only masked with PT27_WAKE_MASK, so any latched status bits
on pins 0..11 that do not have wakeup enabled in chip->wake_mask will still
report pm_wakeup_event() and replay interrupts.
In pt27_gpio_suspend(), if the device entered runtime suspend with
PT27_REG_WAKE_EN enabled and wakeup was then disabled via sysfs before
system suspend, returning early when pm_runtime_status_suspended(dev) is
true skips clearing PT27_REG_WAKE_EN, leaving hardware wakeup active during
system sleep.
> +
> + for_each_set_bit(hwirq, &replay, PT27_WAKE_GPIOS)
> + generic_handle_domain_irq_safe(chip->gen.gc.irq.domain, hwirq);
[Severity: Medium]
Can pt27_gpio_resume() dispatch the same wake interrupt twice?
Inside the lock scope above, pt27_restore_regs() writes PT27_REG_INT_EN and
PT27_REG_INT_MASK without clearing PT27_REG_INT_STAT, and then computes
replay. As soon as the scoped_guard releases chip->gen.lock and restores
local interrupts, an asserted level-triggered wake line or a latched bit in
PT27_REG_INT_STAT can fire chip->irq and run pt27_gpio_isr() ->
generic_handle_domain_irq(). When pt27_gpio_isr() returns, this loop still
iterates over the pre-computed replay bitmap and calls
generic_handle_domain_irq_safe() a second time for the same hwirq, which
invokes handle_irq_event() again even if the interrupt was already serviced
and masked.
[ ... ]
> +static const struct dev_pm_ops pt27_gpio_pm_ops __maybe_unused = {
> + SYSTEM_SLEEP_PM_OPS(pt27_gpio_suspend, pt27_gpio_resume)
> + RUNTIME_PM_OPS(pt27_gpio_runtime_suspend, pt27_gpio_runtime_resume, NULL)
> +};
[Severity: Medium]
Should pt27_gpio_suspend() and pt27_gpio_resume() be wired as noirq sleep
callbacks instead of SYSTEM_SLEEP_PM_OPS()?
With SYSTEM_SLEEP_PM_OPS(), pt27_gpio_suspend() saves PT27_REG_WAKE_EN and
masks interrupts during dpm_suspend(), before dpm_suspend_noirq() runs
device_wakeup_arm_wake_irqs() and suspend_device_irqs(). On resume,
dpm_resume_noirq() runs device_wakeup_disarm_wake_irqs() (calling
pt27_irq_set_wake(d, 0)) before pt27_gpio_resume() runs in dpm_resume(), so
pt27_restore_regs() overwrites PT27_REG_WAKE_EN with chip->saved.wake_en and
clobbers any GPIO or wake state updated during the late/noirq/early phases.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261116075510.5722-1-Chloe_Chen@asmedia.com.tw?part=1
prev parent reply other threads:[~2026-11-16 8:42 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-16 7:55 [PATCH v4] gpio: amdptpci: add support for AMD PT PCI controller Szuying Chen
2026-11-16 8:42 ` sashiko-bot [this message]
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=sashiko-outbox-216556@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=Chloe_Chen@asmedia.com.tw \
--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