Linux USB
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Chunfeng Yun" <chunfeng.yun@mediatek.com>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH v4 11/14] thunderbolt: Add platform NHI driver
Date: Sat, 05 Dec 2026 08:56:21 +0000	[thread overview]
Message-ID: <sashiko-outbox-251319@kernel.org> (raw)
In-Reply-To: <20261205083342.4009278-12-chunfeng.yun@mediatek.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] thunderbolt: tb_nhi_pm_ops noirq callbacks invert ACPI platform
  PM ordering
- [High] thunderbolt: nhi_isr() causes interrupt storm on
  level-triggered platform IRQs
- [Medium] thunderbolt: nhi_platform_do_remove() leaks wakeup_source on
  teardown
- [Medium] thunderbolt: pm_runtime_put_noidle() before
  pm_runtime_disable() races drvdata
- [Low] thunderbolt: nhi_platform_driver assigns .driver.pm without
  pm_ptr()
--

commit b442c5ebfee8bab338c63df36d4eb9041e54f694
Author: Chunfeng Yun <chunfeng.yun@mediatek.com>

thunderbolt: Add platform NHI driver

This commit adds a platform driver for ACPI-enumerated Thunderbolt/USB4
NHI controllers matching ACPI0015, including optional dedicated wake IRQ
support.

> diff --git a/drivers/thunderbolt/platform.c b/drivers/thunderbolt/platform.c
> new file mode 100644
> index 0000000000000..ce3bb65543c11
> --- /dev/null
> +++ b/drivers/thunderbolt/platform.c
> @@ -0,0 +1,155 @@

[ ... ]

> +	ret = platform_get_irq(pdev, 0);
> +	if (ret < 0)
> +		return ret;
> +
> +	nhi->irq = ret;

[Severity: High]
Can this cause an interrupt storm on level-triggered platform IRQs?

tb_nhi_probe() requests nhi->irq via devm_request_irq() with nhi_isr(),
which only calls schedule_work(&nhi->interrupt_work) and returns
IRQ_HANDLED without masking or acknowledging the interrupt.
REG_RING_NOTIFY_BASE is only read later in nhi_interrupt_work().

Previously nhi->irq came from PCI MSI (edge-triggered), whereas wired
platform IRQs on ACPI SoCs are commonly level-triggered.  When a
level-triggered interrupt fires, returning IRQ_HANDLED with the line
still asserted causes the controller to re-deliver the IRQ immediately,
starving nhi_interrupt_work() and livelocking the CPU.  Masking or
acknowledging the interrupt in hardirq context, or using
devm_request_threaded_irq() with IRQF_ONESHOT, would avoid this.

[ ... ]

> +static void nhi_platform_do_remove(struct platform_device *pdev, bool reset)
> +{
> +	struct tb *tb = platform_get_drvdata(pdev);
> +	struct device *dev = &pdev->dev;
> +	struct tb_nhi *nhi = tb->nhi;
> +	struct tb_nhi_platform *nhi_plat = nhi_to_platform(nhi);
> +
> +	tb_nhi_remove(nhi, reset);
> +
> +	if (nhi_plat->wake_irq)
> +		dev_pm_clear_wake_irq(dev);
> +
> +	device_set_wakeup_capable(dev, false);

[Severity: Medium]
Does this leak the wakeup_source registered by tb_nhi_probe()?

tb_nhi_probe() calls device_wakeup_enable(dev), which allocates and
registers dev->power.wakeup (along with its child wakeupN sysfs device),
while tb_nhi_remove() does not call device_wakeup_disable(dev).
device_set_wakeup_capable(dev, false) clears dev->power.can_wakeup
without detaching dev->power.wakeup.

Because device_wakeup_disable() returns early when !dev->power.can_wakeup,
even device_pm_remove() cannot free the wakeup_source later, and a
subsequent re-probe fails in device_wakeup_enable() with -EEXIST.
Calling device_init_wakeup(dev, false) instead avoids the leak.

> +	pm_runtime_put_noidle(dev);
> +	pm_runtime_disable(dev);
> +	pm_runtime_set_suspended(dev);
> +}

[Severity: Medium]
Can calling pm_runtime_put_noidle() before pm_runtime_disable() race with
nhi_runtime_suspend() and cause a use-after-free on tb?

CPU0                            CPU1
nhi_platform_do_remove()
  tb_nhi_remove()
    tb_domain_remove(tb)
    ...
    kfree(tb)
  pm_runtime_put_noidle(dev)
                                pm_suspend_timer_fn()
                                  nhi_runtime_suspend(dev)
                                    tb = dev_get_drvdata(dev)
                                    tb->nhi  <-- use-after-free
  pm_runtime_disable(dev)

tb_nhi_remove() waits for tb_domain_release() to free tb, but
dev->driver_data still points to it until device_unbind_cleanup().
Dropping usage_count to zero while runtime PM is still enabled lets an
armed autosuspend timer (or a stateless USB3 consumer device link)
invoke nhi_runtime_suspend() on freed memory.

The same ordering in nhi_platform_probe() under wake_irq_err can trigger
a NULL pointer dereference in nhi_runtime_suspend() while
dev_get_drvdata(dev) is NULL.  Calling pm_runtime_disable(dev) before
pm_runtime_put_noidle(dev) closes both windows.

[ ... ]

> +static struct platform_driver nhi_platform_driver = {
> +	.probe = nhi_platform_probe,
> +	.remove = nhi_platform_remove,
> +	.shutdown = nhi_platform_shutdown,
> +	.driver = {
> +		.name = "thunderbolt",
> +		.acpi_match_table = ACPI_PTR(nhi_platform_acpi_match),
> +		.pm = &tb_nhi_pm_ops,
> +	},
> +};

[Severity: High]
Does using tb_nhi_pm_ops invert the power-state ordering under
acpi_general_pm_domain?

When platform_probe() binds an ACPI platform device,
dev_pm_domain_attach() attaches acpi_general_pm_domain.  Unlike PCI PM,
acpi_general_pm_domain transitions the device to low power in
acpi_subsys_suspend_late() (and acpi_subsys_poweroff_late()), before
tb_nhi_pm_ops.suspend_noirq (nhi_suspend_noirq()) runs.

On resume, acpi_subsys_resume_noirq() runs nhi_resume_noirq() while the
device is still powered off, and because tb_nhi_pm_ops.resume_early is
NULL, acpi_subsys_resume_early() postpones acpi_dev_resume() until
acpi_subsys_resume() (with the same inversion in hibernation
restore_noirq vs acpi_subsys_restore_early()).

On SoCs whose ACPI power states gate the NHI clocks or power domain,
nhi_suspend_noirq() and nhi_resume_noirq() will perform MMIO register
accesses and control-channel DMA while the controller is powered off,
causing MMIO timeouts, failed tunnel recovery, or bus faults and hangs.

[Severity: Low]
This isn't a bug, but .pm should be wrapped with pm_ptr(&tb_nhi_pm_ops)
so the PM ops pointer evaluates to NULL and can be discarded when
CONFIG_PM is disabled.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205083342.4009278-1-chunfeng.yun@mediatek.com?part=11

  reply	other threads:[~2026-12-05  8:56 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05  8:33 [PATCH v4 00/14] thunderbolt: Add platform NHI driver and modularize PCIe NHI Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 01/14] thunderbolt: Fix error handling and unwind paths in nhi_probe() Chunfeng Yun
2026-12-05  8:50   ` sashiko-bot
2026-12-05  8:33 ` [PATCH v4 02/14] thunderbolt: Prefix NHI core symbols with tb_ and tb_ring_ Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 03/14] thunderbolt: Internalize non-MSI-X interrupt request and work in NHI core Chunfeng Yun
2026-12-05  8:51   ` sashiko-bot
2026-12-05  8:33 ` [PATCH v4 04/14] thunderbolt: Encapsulate router device type checks in switch.c Chunfeng Yun
2026-12-05  8:41   ` sashiko-bot
2026-12-05  8:33 ` [PATCH v4 05/14] thunderbolt: Move rpm_complete bus walk to domain.c Chunfeng Yun
2026-12-05  8:50   ` sashiko-bot
2026-12-05  8:33 ` [PATCH v4 06/14] thunderbolt: Consolidate domain and NHI teardown into tb_nhi_remove() Chunfeng Yun
2026-12-05  8:50   ` sashiko-bot
2026-12-05  8:33 ` [PATCH v4 07/14] thunderbolt: Move ICM mailbox functions to icm.c Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 08/14] thunderbolt: Decouple ICM probe from NHI core via .select_cm hook Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 09/14] thunderbolt: Make PCIe NHI support opt-in Chunfeng Yun
2026-12-05  8:58   ` sashiko-bot
2026-12-05  8:33 ` [PATCH v4 10/14] thunderbolt: Make nhi->ops optional Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 11/14] thunderbolt: Add platform NHI driver Chunfeng Yun
2026-12-05  8:56   ` sashiko-bot [this message]
2026-12-05  8:33 ` [PATCH v4 12/14] thunderbolt: Increase the lane adapter link-up timeout value Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 13/14] thunderbolt: Decouple the waiting time of TB_PORT_UNPLUGGED and TB_PORT_UP Chunfeng Yun
2026-12-05  8:56   ` sashiko-bot
2026-12-05  8:33 ` [PATCH v4 14/14] thunderbolt: Drop WARN_ON() in tb_disconnect_pci() Chunfeng Yun
2026-12-05  8:55   ` sashiko-bot
2026-12-05  8:58 ` [PATCH v4 00/14] thunderbolt: Add platform NHI driver and modularize PCIe NHI sashiko-bot

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-251319@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=chunfeng.yun@mediatek.com \
    --cc=linux-usb@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