Linux PCI subsystem development
 help / color / mirror / Atom feed
From: Herve Codina <herve.codina@bootlin.com>
To: sashiko-bot@kernel.org
Cc: sashiko-reviews@lists.linux.dev, linux-cxl@vger.kernel.org,
	linux-pci@vger.kernel.org
Subject: Re: [PATCH v9 7/9] PCI: of: Clear fwnode->dev during root bridge node removal
Date: Fri, 17 Jul 2026 14:57:31 +0200	[thread overview]
Message-ID: <20260717145731.0b878b27@bootlin.com> (raw)
In-Reply-To: <20260708103435.2E2421F000E9@smtp.kernel.org>

On Wed, 08 Jul 2026 10:34:34 +0000
sashiko-bot@kernel.org wrote:

> Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
> 
> Pre-existing issues:
> - [High] Missing fw_devlink cleanup in the error path of of_pci_make_host_bridge_node(), leaving a dangling fwnode->dev pointer.
> - [High] Kernel panic on removal of overlay-provided PCI host bridge nodes due to unsafe changeset revert.
> --
> 
> --- Patch [7]: [PATCH v9 7/9] PCI: of: Clear fwnode->dev during root bridge node removal ---
> commit 9e6873c21d6442ce4222fe6e9ac417348c8b3fbd
> Author: Herve Codina <herve.codina@bootlin.com>
> 
> PCI: of: Clear fwnode->dev during root bridge node removal
> 
> This commit clears fwnode->dev during of_pci_remove_host_bridge_node() to
> prevent a dangling pointer. It ensures fw_devlink_set_device() is called
> with a NULL device before the fwnode is cleared.
> 
> > diff --git a/drivers/pci/of.c b/drivers/pci/of.c
> > index ee9eb384b377a..8dd558a490bbd 100644
> > --- a/drivers/pci/of.c
> > +++ b/drivers/pci/of.c
> > @@ -743,6 +743,7 @@ void of_pci_remove_host_bridge_node(struct pci_host_bridge *bridge)
> >  	if (!np || !of_node_check_flag(np, OF_DYNAMIC))
> >  		return;
> >  
> > +	fw_devlink_set_device(&np->fwnode, NULL);  
> 
> [Severity: High]
> This is a pre-existing issue, but does of_pci_make_host_bridge_node()
> also need to clear the fwnode->dev pointer on its error path?
> 
> If device_add_of_node() fails in of_pci_make_host_bridge_node(), it jumps
> to the error paths but leaves np->fwnode.dev pointing to the bridge device:
> 
> drivers/pci/of.c:of_pci_make_host_bridge_node() {
> 	...
> 	fw_devlink_set_device(&np->fwnode, &bridge->dev);
> 	...
> 	/* Add the of_node to host bridge and the root bus */
> 	ret = device_add_of_node(&bridge->dev, np);
> 	if (ret)
> 		goto out_revert_cset;
> 
> 	ret = device_add_of_node(&bridge->bus->dev, np);
> 	if (ret)
> 		goto out_remove_bridge_dev_of_node;
> 	...
> out_remove_bridge_dev_of_node:
> 	device_remove_of_node(&bridge->dev);
> out_revert_cset:
> 	np->data = NULL;
> 	of_changeset_revert(cset);
> out_free_node:
> 	of_node_put(np);
> 	...
> }
> 
> If a concurrent thread acquired a reference to np, could the node outlive
> the error path and result in a use-after-free where fwnode->dev points to
> freed memory?

This issue is relevant and clearing np->fwnode.dev in the error path will
be take into account in the next iteration.

> 
> >  	device_remove_of_node(&bridge->bus->dev);
> >  	device_remove_of_node(&bridge->dev);
> >  	of_changeset_revert(np->data);  
> 
> [Severity: High]
> This isn't a bug introduced by this patch, but does calling
> of_changeset_revert(np->data) unconditionally cause a crash when removing
> an overlay-provided PCI host bridge node?
> 
> When an overlay provides the device tree node for a PCI host bridge, the
> node inherently has the OF_DYNAMIC flag set. If the bridge is removed,
> of_pci_remove_host_bridge_node() assumes the node was created by
> of_pci_make_host_bridge_node() and executes of_changeset_revert(np->data).
> 
> Since np->data is NULL for standard overlay nodes, does this result in a
> NULL pointer dereference in __of_changeset_revert_entries() when it tries
> to evaluate &ocs->entries?
> 

This scenario should not exist but anyway, I will add a new patch in the
series in order to have a more robust code checking for a NULL np->data in
both of_pci_remove_host_bridge_node() and of_pci_remove_node().

Hervé

  reply	other threads:[~2026-07-17 12:57 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-08 10:02 [PATCH v9 0/9] lan966x pci device: Add support for SFPs, PCI part Herve Codina
2026-07-08 10:02 ` [PATCH v9 1/9] driver core: fw_devlink: Introduce fw_devlink_set_device() Herve Codina
2026-07-08 10:08   ` sashiko-bot
2026-07-08 10:02 ` [PATCH v9 2/9] drivers: core: Use fw_devlink_set_device() Herve Codina
2026-07-08 10:12   ` sashiko-bot
2026-07-08 10:02 ` [PATCH v9 3/9] pinctrl: cs42l43: " Herve Codina
2026-07-08 10:20   ` sashiko-bot
2026-07-08 10:02 ` [PATCH v9 4/9] cxl/test: Use device_set_node() Herve Codina
2026-07-08 10:15   ` sashiko-bot
2026-07-08 10:02 ` [PATCH v9 5/9] cxl/test: Use fw_devlink_set_device() Herve Codina
2026-07-08 10:16   ` sashiko-bot
2026-07-08 10:02 ` [PATCH v9 6/9] PCI: of: " Herve Codina
2026-07-08 10:29   ` sashiko-bot
2026-07-17 12:44     ` Herve Codina
2026-07-08 10:02 ` [PATCH v9 7/9] PCI: of: Clear fwnode->dev during root bridge node removal Herve Codina
2026-07-08 10:34   ` sashiko-bot
2026-07-17 12:57     ` Herve Codina [this message]
2026-07-08 10:02 ` [PATCH v9 8/9] PCI: of: Set fwnode device of newly created PCI device nodes Herve Codina
2026-07-08 10:43   ` sashiko-bot
2026-07-08 10:02 ` [PATCH v9 9/9] PCI: of: Remove fwnode_dev_initialized() call for a PCI root bridge node Herve Codina
2026-07-08 10:48   ` 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=20260717145731.0b878b27@bootlin.com \
    --to=herve.codina@bootlin.com \
    --cc=linux-cxl@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=sashiko-bot@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