From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: Lukas Wunner <lukas@wunner.de>
Cc: Bjorn Helgaas <helgaas@kernel.org>,
Krzysztof Wilczynski <kwilczynski@kernel.org>,
linux-pci@vger.kernel.org, Niklas Schnelle <niks@kernel.org>,
Jonathan Cameron <Jonathan.Cameron@huawei.com>,
Mika Westerberg <mika.westerberg@linux.intel.com>,
"Maciej W. Rozycki" <macro@orcam.me.uk>,
Mario Limonciello <mario.limonciello@amd.com>,
Evert Vorster <evorster@gmail.com>
Subject: Re: [PATCH for-linus] PCI/bwctrl: Fix NULL pointer deref on unbind and bind
Date: Sun, 5 Jan 2025 18:54:24 +0200 (EET) [thread overview]
Message-ID: <e08d30b5-50e0-4381-d2e7-61c2da12966f@linux.intel.com> (raw)
In-Reply-To: <0ee5faf5395cad8d29fb66e1ec444c8d882a4201.1735852688.git.lukas@wunner.de>
On Thu, 2 Jan 2025, Lukas Wunner wrote:
> The interrupt handler for bandwidth notifications, pcie_bwnotif_irq(),
> dereferences a "data" pointer.
>
> On unbind, that pointer is set to NULL by pcie_bwnotif_remove(). However
> the interrupt handler may still be invoked afterwards and will dereference
> that NULL pointer.
>
> That's because the interrupt is requested using a devm_*() helper and the
> driver core releases devm_*() resources *after* calling ->remove().
>
> pcie_bwnotif_remove() does clear the Link Bandwidth Management Interrupt
> Enable and Link Autonomous Bandwidth Interrupt Enable bits in the Link
> Control Register, but that won't prevent execution of pcie_bwnotif_irq():
> The interrupt for bandwidth notifications may be shared with AER, DPC,
> PME, and hotplug. So pcie_bwnotif_irq() may be executed as long as the
> interrupt is requested.
>
> There's a similar race on bind: pcie_bwnotif_probe() requests the
> interrupt when the "data" pointer still points to NULL. A NULL pointer
> deref may thus likewise occur if AER, DPC, PME or hotplug raise an
> interrupt in-between the bandwidth controller's call to devm_request_irq()
> and assignment of the "data" pointer.
>
> Drop the devm_*() usage and reorder requesting of the interrupt to fix the
> issue.
>
> While at it, drop a stray but harmless no_free_ptr() invocation when
> assigning the "data" pointer in pcie_bwnotif_probe().
>
> Evert reports a hang on shutdown of an ASUS ROG Strix SCAR 17 G733PYV.
> The issue is no longer reproducible with the present commit.
>
> Evert found that attaching a USB-C monitor prevented the hang. The
> machine contains an ASMedia USB 3.2 controller below a hotplug-capable
> Root Port. So one possible explanation is that the controller gets
> hot-removed on shutdown unless something is connected. And the ensuing
> hotplug interrupt occurs exactly when the bandwidth controller is
> unregistering. The precise cause could not be determined because the
> screen had already turned black when the hang occurred.
>
> Fixes: 665745f27487 ("PCI/bwctrl: Re-add BW notification portdrv as PCIe BW controller")
> Reported-by: Evert Vorster <evorster@gmail.com>
> Tested-by: Evert Vorster <evorster@gmail.com>
> Closes: https://bugzilla.kernel.org/show_bug.cgi?id=219629
> Signed-off-by: Lukas Wunner <lukas@wunner.de>
> ---
> drivers/pci/pcie/bwctrl.c | 25 +++++++++++++++++--------
> 1 file changed, 17 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/pci/pcie/bwctrl.c b/drivers/pci/pcie/bwctrl.c
> index b59cacc..7cd8211 100644
> --- a/drivers/pci/pcie/bwctrl.c
> +++ b/drivers/pci/pcie/bwctrl.c
> @@ -303,17 +303,18 @@ static int pcie_bwnotif_probe(struct pcie_device *srv)
> if (ret)
> return ret;
>
> - ret = devm_request_irq(&srv->device, srv->irq, pcie_bwnotif_irq,
> - IRQF_SHARED, "PCIe bwctrl", srv);
> + scoped_guard(rwsem_write, &pcie_bwctrl_setspeed_rwsem)
> + scoped_guard(rwsem_write, &pcie_bwctrl_lbms_rwsem)
> + port->link_bwctrl = data;
> +
> + ret = request_irq(srv->irq, pcie_bwnotif_irq, IRQF_SHARED,
> + "PCIe bwctrl", srv);
> if (ret)
> - return ret;
> + goto err_reset_data;
>
> - scoped_guard(rwsem_write, &pcie_bwctrl_setspeed_rwsem) {
> - scoped_guard(rwsem_write, &pcie_bwctrl_lbms_rwsem) {
> - port->link_bwctrl = no_free_ptr(data);
> + scoped_guard(rwsem_write, &pcie_bwctrl_setspeed_rwsem)
> + scoped_guard(rwsem_write, &pcie_bwctrl_lbms_rwsem)
> pcie_bwnotif_enable(srv);
> - }
> - }
Hi Lukas,
Indeed, it certainly didn't occur to me while arranging the code the way
it is that there are other sources for the same irq. However, there is a
reason those lines where within the same critical section (I also realized
it's not documented anywhere):
As bwctrl has two operating modes, one with BW notifications and the other
without them, there are races when switching between those modes during
probe wrt. call to lbms counting accessor, and I reused those rw
semaphores to prevent those race (the race fixes were noted only in a
history bullet of the bwctrl series).
Do you think it would be possible to put also request_irq() inside that
critical section to synchronize lbms count calls with the mode switch?
(Also beware that with scoped_guards, a goto out of the critical section
cannot be used in error handling and the NULL needs to be assigned inside
it.)
(I know Bjorn has taken this already in so if needed, we can consider
making that change later on top of this as those races are much less
severe issue than what this fix is correcting.)
> pci_dbg(port, "enabled with IRQ %d\n", srv->irq);
>
> @@ -323,6 +324,12 @@ static int pcie_bwnotif_probe(struct pcie_device *srv)
> port->link_bwctrl->cdev = NULL;
>
> return 0;
> +
> +err_reset_data:
> + scoped_guard(rwsem_write, &pcie_bwctrl_setspeed_rwsem)
> + scoped_guard(rwsem_write, &pcie_bwctrl_lbms_rwsem)
> + port->link_bwctrl = NULL;
> + return ret;
> }
>
> static void pcie_bwnotif_remove(struct pcie_device *srv)
> @@ -333,6 +340,8 @@ static void pcie_bwnotif_remove(struct pcie_device *srv)
>
> pcie_bwnotif_disable(srv->port);
>
> + free_irq(srv->irq, srv);
> +
> scoped_guard(rwsem_write, &pcie_bwctrl_setspeed_rwsem)
> scoped_guard(rwsem_write, &pcie_bwctrl_lbms_rwsem)
> srv->port->link_bwctrl = NULL;
It seems I didn't remember to fix the mode switch races here at all.
So I think also here, pcie_bwnotif_disable(), free_irq() and NULL
assignment, should be within the same critical section. Although this
doesn't seem very big problemn compared with the probe side.
--
i.
next prev parent reply other threads:[~2025-01-05 16:54 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-02 21:20 [PATCH for-linus] PCI/bwctrl: Fix NULL pointer deref on unbind and bind Lukas Wunner
2025-01-02 23:22 ` Bjorn Helgaas
2025-01-05 16:54 ` Ilpo Järvinen [this message]
2025-01-07 4:29 ` Lukas Wunner
2025-01-07 14:29 ` Ilpo Järvinen
2025-01-08 12:05 ` Lukas Wunner
2025-01-08 12:55 ` Ilpo Järvinen
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=e08d30b5-50e0-4381-d2e7-61c2da12966f@linux.intel.com \
--to=ilpo.jarvinen@linux.intel.com \
--cc=Jonathan.Cameron@huawei.com \
--cc=evorster@gmail.com \
--cc=helgaas@kernel.org \
--cc=kwilczynski@kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=lukas@wunner.de \
--cc=macro@orcam.me.uk \
--cc=mario.limonciello@amd.com \
--cc=mika.westerberg@linux.intel.com \
--cc=niks@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