lan966x pci device: Add support for SFPs, PCI part
Subsystems (11)
v13 cover lan966x pci device: Add support for SFPs, PCI part
Hi,
Previously, I sent a big picture series adding support for SFP ports available on the LAN966x PCI device [0].
In this series patches touch several parts and sub-system in the kernel. Reviews have be done and it makes sense to split the series and send parts separately.
This current series is the extraction of patches related to issues in the PCI subsystem. It has to be seen as a continuation of the big picture series but related to this specific core part.
Patches 1 to 6 introduce and use fw_devlink_set_device() in already existing code. No functional change but the introduction of the fw_devlink_set_device() wrapper.
Patch 7 avoids a dangling pointer.
Patches 8 and 9 fix fw_devlink issues specific to PCI and the device-tree nodes created during enumeration.
Patch 10 avoids the usage of np->data and adds guards to prevent incorrect nodes removal.
[0] https://lore.kernel.org/all/[email protected]/
Best regards,
Hervé
Changes:
v12 -> v13
v12: https://lore.kernel.org/all/[email protected]/
- Patches 1 to 9
No changes
- Patch 10
Split a test condition into two distinct checks as suggested by Andy. No functional change but readability improvement. Split done in both of_pci_remove_node() and of_pci_remove_host_bridge_node().
v11 -> v12
v11: https://lore.kernel.org/all/[email protected]/
- Patches 1 to 7
Add 'Reviewed-by: Bartosz Golaszewski'
- Patches 8 and 9
No changes
- Patch 10
Add a NULL check for the retrieved OF node in removal functions to avoid any NULL pointer dereferences.
Robustify the guard setting cset to NULL after freeing it. Those issues have been reported by Shashiko:
https://lore.kernel.org/all/[email protected]/
v10 -> v11
v10: https://lore.kernel.org/all/[email protected]/
Rebase on top of v7.3-rc1
- Patches 1 to 5
Add 'Reviewed-by: Richard Cheng'
- Patches 6, 7 and 8
Add 'Reviewed-by: Richard Cheng'
Add 'Acked-by: Manivannan Sadhasivam'
- Patch 9
Add 'Reviewed-by: Richard Cheng'
- Patch 10
Rework removing np->data usage in the PCI devicetree node creation
and removal.v9 -> v10
v9: https://lore.kernel.org/all/[email protected]/
- Patches 1 to 6
No changes
- Patch 7
Clear fwnode->dev also on error path.
- Patch 8
Clear fwnode->dev also on error path.
- Patch 9
No changes
- Patch 10 (New patch in v10)
Guard against node removal with incorrect np->data.
v8 -> v9
v8: https://lore.kernel.org/all/[email protected]/
- Patch 7 (new patch in v9)
Clear fwnode->dev during root bridge node removal (avoid a dangling
pointer).- Patch 8 (patch 7 in v8)
Clear fwnode->dev during PCI device node removal.
v7 -> v8
v7: https://lore.kernel.org/all/[email protected]/
Rebase on top of v7.2-rc1
- patches 4 and 5
Add 'Reviewed-by: Andy Shevchenko'
- Other Patches
No other changes
v6 -> v7
Rebase on top of v7.1-rc1
- Patch 1
Add Cc stable. Needed for patches 11 and 12
- Patches 2 to 5
No changes
- Patch 6
Add Cc stable. Needed for patch 13
Add 'Acked-by: Bjorn Helgaas'
- Patch 7
Fix a typo in commit log
Add Fixes tag + Cc stable
Add 'Acked-by: Bjorn Helgaas'
- Patch 8
Add Fixes tag + Cc stable
Add 'Acked-by: Bjorn Helgaas'
Older iterations:
Patches 6 to 13 in the big picture series
https://lore.kernel.org/all/[email protected]/
Herve Codina (10):
driver core: fw_devlink: Introduce fw_devlink_set_device()
drivers: core: Use fw_devlink_set_device()
pinctrl: cs42l43: Use fw_devlink_set_device()
cxl/test: Use device_set_node()
cxl/test: Use fw_devlink_set_device()
PCI: of: Use fw_devlink_set_device()
PCI: of: Clear fwnode->dev during root bridge node removal and error
path
PCI: of: Set fwnode device of newly created PCI device nodes
PCI: of: Remove fwnode_dev_initialized() call for a PCI root bridge
node
PCI: of: Avoid np->data usage for the node changeset
drivers/base/core.c | 4 +- drivers/pci/of.c | 52 ++++++++++++++++-------- drivers/pinctrl/cirrus/pinctrl-cs42l43.c | 2 +- include/linux/fwnode.h | 6 +++ include/linux/pci.h | 11 +++++ tools/testing/cxl/test/cxl.c | 4 +- 6 files changed, 58 insertions(+), 21 deletions(-)
Signature · 1 line
2.55.0
PATCH v13 1/10 driver core: fw_devlink: Introduce fw_devlink_set_device()
Setting fwnode->dev is specific to fw_devlink.
In order to avoid having a direct 'fwnode->dev = dev;' in several place in the kernel, introduce fw_devlink_set_device() helper to perform this operation.
Having this helper allows to hide the fwnode devlink related stuff behind the helper.
---
Cc stable because used by other patches with Fixes + Cc stable
--- include/linux/fwnode.h | 6 ++++++ 1 file changed, 6 insertions(+)
include/linux/fwnode.h +6 -0
@@ -260,4 +260,10 @@ void fw_devlink_purge_absent_suppliers(struct fwnode_handle *fwnode);void fw_devlink_refresh_fwnode (struct fwnode_handle *fwnode);bool fw_devlink_is_strict (void ); +static inline void fw_devlink_set_device (struct fwnode_handle *fwnode, +struct device *dev) +{ + fwnode->dev = dev; +} +#endif
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v13 01/10] driver core: fw_devlink: Introduce fw_devlink_set_device()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines
Setting
fwnode->devis specific tofw_devlink.In order to avoid having a direct '
fwnode->dev= dev;' in several place in the kernel, introducefw_devlink_set_device()helper to perform this operation.Having this helper allows to hide the fwnode devlink related stuff behind the helper.
Signed-off-by Herve Codina <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v13 2/10 drivers: core: Use fw_devlink_set_device()
In reply to Herve CodinaThe code set directly fwnode->dev field.
Use the dedicated fw_devlink_set_device() helper to perform this operation.
--- drivers/base/core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/base/core.c +2 -2
@@ -3754,7 +3754,7 @@ int device_add(struct device *dev)* device and the driver sync_state callback is called for this device. */ if (dev->fwnode && !dev->fwnode->dev) { - dev->fwnode->dev = dev; +fw_devlink_set_device (dev->fwnode, dev);fw_devlink_link_device (dev); }
@@ -3929,7 +3929,7 @@ void device_del(struct device *dev)device_unlock (dev);if (dev->fwnode && dev->fwnode->dev == dev) - dev->fwnode->dev =NULL ; +fw_devlink_set_device (dev->fwnode,NULL );/* Notify clients of device removal. This call must come * before dpm_sysfs_remove().
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v13 02/10] drivers: core: Use fw_devlink_set_device()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines
The code set directly
fwnode->devfield.Use the dedicated
fw_devlink_set_device()helper to perform this operation.Signed-off-by Herve Codina <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v13 3/10 pinctrl: cs42l43: Use fw_devlink_set_device()
In reply to Herve CodinaThe code set directly fwnode->dev field.
Use the dedicated fw_devlink_set_device() helper to perform this operation.
--- drivers/pinctrl/cirrus/pinctrl-cs42l43.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pinctrl/cirrus/pinctrl-cs42l43.c +1 -1
@@ -576,7 +576,7 @@ static int cs42l43_pin_probe(struct platform_device *pdev)return ret;if (!child->dev) - child->dev = priv->dev; +fw_devlink_set_device (child, priv->dev); fwnode = child; } }
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v13 03/10] pinctrl: cs42l43: Use fw_devlink_set_device()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines
The code set directly
fwnode->devfield.Use the dedicated
fw_devlink_set_device()helper to perform this operation.Signed-off-by Herve Codina <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v13 4/10 cxl/test: Use device_set_node()
In reply to Herve CodinaThe code set directly dev->fwnode.
Use the dedicated helper to perform this operation.
--- tools/testing/cxl/test/cxl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/cxl/test/cxl.c +1 -1
@@ -1677,7 +1677,7 @@ static void mock_companion(struct acpi_device *adev, struct device *dev) {device_initialize (&adev->dev);fwnode_init (&adev->fwnode,NULL ); - dev->fwnode = &adev->fwnode; +device_set_node (dev, &adev->fwnode); adev->fwnode.dev = dev; }
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v13 04/10] cxl/test: Use device_set_node()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 5 lines
The code set directly
dev->fwnode.Use the dedicated helper to perform this operation.
Signed-off-by Herve Codina <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH v13 5/10 cxl/test: Use fw_devlink_set_device()
In reply to Herve CodinaThe code set directly fwnode.dev field.
Use the dedicated fw_devlink_set_device() helper to perform this operation.
--- tools/testing/cxl/test/cxl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/cxl/test/cxl.c +1 -1
@@ -1678,7 +1678,7 @@ static void mock_companion(struct acpi_device *adev, struct device *dev)device_initialize (&adev->dev);fwnode_init (&adev->fwnode,NULL );device_set_node (dev, &adev->fwnode); - adev->fwnode.dev = dev; +fw_devlink_set_device (&adev->fwnode, dev); }#ifndef SZ_64G
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v13 05/10] cxl/test: Use fw_devlink_set_device()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines
The code set directly fwnode.dev field.
Use the dedicated
fw_devlink_set_device()helper to perform this operation.Signed-off-by Herve Codina <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
PATCH v13 6/10 PCI: of: Use fw_devlink_set_device()
In reply to Herve CodinaThe code set directly fwnode.dev field.
Use the dedicated fw_devlink_set_device() helper to perform this operation.
---
Cc stable because used by other patches with Fixes + Cc stable
--- drivers/pci/of.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/of.c +1 -1
@@ -882,7 +882,7 @@ void of_pci_make_host_bridge_node(struct pci_host_bridge *bridge)* bus. Avoid any new device creation. */ of_node_set_flag (np,OF_POPULATED ); - np->fwnode.dev = &bridge->dev; +fw_devlink_set_device (&np->fwnode, &bridge->dev);fwnode_dev_initialized (&np->fwnode, true); ret =of_changeset_apply (cset);
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v13 06/10] PCI: of: Use fw_devlink_set_device()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines
The code set directly fwnode.dev field.
Use the dedicated
fw_devlink_set_device()helper to perform this operation.Signed-off-by Herve Codina <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
PATCH v13 7/10 PCI: of: Clear fwnode->dev during root bridge node removal and error path
In reply to Herve CodinaDuring the of_pci_make_host_bridge_node() call, an OF node is created dynamically and its fwnode device (fwnode->dev) is set to the PCI root bridge device using the fw_devlink_set_device(&np->fwnode, &bridge->dev) call.
On removal, of_pci_remove_host_bridge_node() is called and calls device_remove_of_node() which in turn set to NULL the related dev->fwnode.
Later in the removal sequence, device_del() is called and runs its cleanup logic:
if (dev->fwnode && dev->fwnode->dev == dev)
fw_devlink_set_device(dev->fwnode, NULL);Because dev->fwnode has been cleared earlier, fw_devlink_set_device() is not called and leaves fwnode->dev unchanged. This fwnode device (fwnode->dev) becomes an dangling pointer.
If any reference to the OF node is held after this removal, the pointer is still accessible using the OF node (np->fwnode.dev) but points to a freed area.
The exact same issue is present in the of_pci_make_host_bridge_node() error path leading to the exact same dangling fwnode->dev.
Avoid this dangling fwnode->dev pointer by clearing it in of_pci_remove_host_bridge_node() and in of_pci_make_host_bridge_node() error path.
--- drivers/pci/of.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/pci/of.c +4 -1
@@ -822,6 +822,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 );device_remove_of_node (&bridge->bus->dev);device_remove_of_node (&bridge->dev);of_changeset_revert (np->data);
@@ -887,7 +888,7 @@ void of_pci_make_host_bridge_node(struct pci_host_bridge *bridge) ret =of_changeset_apply (cset);if (ret) - goto out_free_node; + goto out_clear_devlink_dev; np->data = cset;
@@ -909,6 +910,8 @@ void of_pci_make_host_bridge_node(struct pci_host_bridge *bridge) out_revert_cset: np->data =NULL ;of_changeset_revert (cset); +out_clear_devlink_dev: +fw_devlink_set_device (&np->fwnode,NULL ); out_free_node:of_node_put (np); out_destroy_cset:
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v13 07/10] PCI: of: Clear fwnode->dev during root bridge node removal and error path
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 15 lines
During the
of_pci_make_host_bridge_node()call, an OF node is created dynamically and its fwnode device (fwnode->dev) is set to the PCI root bridge device using thefw_devlink_set_device(&np->fwnode, &bridge->dev)call.On removal,
of_pci_remove_host_bridge_node()is called and callsdevice_remove_of_node()which in turn set to NULL the relateddev->fwnode.Later in the removal sequence,
device_del()is called and runs its cleanup logic:if (dev->fwnode && dev->fwnode->dev == dev) fw_devlink_set_device(dev->fwnode, NULL);
Quoted 6 lines
[ ... ]
error path.Fixes 1f340724419e ("PCI: of: Create device tree PCI host bridge node")Signed-off-by Herve Codina <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
PATCH v13 8/10 PCI: of: Set fwnode device of newly created PCI device nodes
In reply to Herve CodinaDevice-tree node can be created when CONFIG_PCI_DYNAMIC_OF_NODES. Those nodes are created and filled based on PCI core information but the fwnode device field is not set.
When later an overlay is applied, this confuses fw_devlink. Indeed, without any device attached to the node, fw_devlink considers that this node will never become a device. When this node is pointed as a supplier, devlink looks at its ancestors in order to find a node with a device that could be used as the supplier.
In the PCI use case, this leads to links that wrongly use the PCI root bridge device as the supplier instead of the expected PCI device.
Setting the fwnode device to the device of the PCI device allows devlink to use this device as a supplier and so, correct links are created.
--- drivers/pci/of.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/pci/of.c +11 -1
@@ -736,6 +736,7 @@ void of_pci_remove_node(struct pci_dev *pdev)if (!np || !of_node_check_flag (np,OF_DYNAMIC ))return ; +fw_devlink_set_device (&np->fwnode,NULL );device_remove_of_node (&pdev->dev);of_changeset_revert (np->data);of_changeset_destroy (np->data);
@@ -788,9 +789,16 @@ void of_pci_make_dev_node(struct pci_dev *pdev)if (ret) goto out_free_node; +/* +* Set the fwnode device in order to have fw_devlink creating links +* pointing to this PCI device instead of walking up to the PCI host +* bridge. +*/ +fw_devlink_set_device (&np->fwnode, &pdev->dev); + ret =of_changeset_apply (cset);if (ret) - goto out_free_node; + goto out_clear_devlink_dev; np->data = cset;
@@ -805,6 +813,8 @@ void of_pci_make_dev_node(struct pci_dev *pdev) out_revert_cset: np->data =NULL ;of_changeset_revert (cset); +out_clear_devlink_dev: +fw_devlink_set_device (&np->fwnode,NULL ); out_free_node:of_node_put (np); out_destroy_cset:
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v13 08/10] PCI: of: Set fwnode device of newly created PCI device nodes
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] base: TOCTOU data race and UAF on fwnode->dev access --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] base: TOCTOU data race and UAF on fwnode->dev access
--
commit dc94be607770b564614555ac56ea83f070fa3dd4
Author: Herve Codina <[email protected]>
PCI: of: Set fwnode device of newly created PCI device nodes
Device-tree node can be created when CONFIG_PCI_DYNAMIC_OF_NODES. Those nodes are created and filled based on PCI core information but the fwnode device field is not set.
drivers/pci/of.c void of_pci_remove_node(struct pci_dev *pdev) · line 739
Quoted 9 lines
drivers/pci/of.c +1
-0@@ -736,6 +736,7 @@ void of_pci_remove_node(struct pci_dev *pdev)if (!np || !of_node_check_flag (np,OF_DYNAMIC ))return ; +fw_devlink_set_device (&np->fwnode,NULL );
[Severity: High]
Does this locklessly clear the fwnode device while another traversal might be reading it?
If a PCI device is concurrently removed while a driver is being bound to another device on the same system, of_pci_remove_node() clears fwnode->dev locklessly here.
Concurrently, __fw_devlink_pickup_dangling_consumers() in drivers/base/core.c can traverse the fwnode tree and read it locklessly:
if (fwnode->dev && fwnode->dev->bus) return;
Can this lead to a NULL pointer dereference if the compiler emits two loads and the pointer is cleared in between?
Can this also cause a use-after-free if the pointer is read, then the device is concurrently unregistered and freed before the bus field is accessed, as no reference count is held during the lockless read?
Quoted 3 lines
drivers/pci/of.c +0
-0device_remove_of_node (&pdev->dev);of_changeset_revert (np->data);of_changeset_destroy (np->data);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Re: [PATCH v13 08/10] PCI: of: Set fwnode device of newly created PCI device nodes
Quoted 31 lines · On Mon, 12 Oct 2026 14:44:48 +0000 [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] base: TOCTOU data race and UAF onfwnode->devaccess
--commit
dc94be607770b564614555ac56ea83f070fa3dd4
Author: Herve Codina <[email protected]>PCI: of: Set fwnode device of newly created PCI device nodes
Device-tree node can be created when
CONFIG_PCI_DYNAMIC_OF_NODES. Those nodes are created and filled based on PCI core information but the fwnode device field is not set.drivers/pci/of.c void of_pci_remove_node(struct pci_dev *pdev) · line 739
Quoted 9 lines
drivers/pci/of.c +1
-0@@ -736,6 +736,7 @@ void of_pci_remove_node(struct pci_dev *pdev)if (!np || !of_node_check_flag (np,OF_DYNAMIC ))return ; +fw_devlink_set_device (&np->fwnode,NULL );[Severity: High]
Does this locklessly clear the fwnode device while another traversal might be reading it?If a PCI device is concurrently removed while a driver is being bound to another device on the same system,
of_pci_remove_node()clearsfwnode->devlocklessly here.
Quoted 5 lines
Concurrently,
__fw_devlink_pickup_dangling_consumers()indrivers/base/core.ccan traverse the fwnode tree and read it locklessly:if (fwnode->dev && fwnode->dev->bus) return;
I don't think so.
__fw_devlink_pickup_dangling_consumers() can look at fwnode->dev but those fwnode->dev are not in the same branch in the tree as the one where the fwnode being modified is located.
I don't see any cases where __fw_devlink_pickup_dangling_consumers() can reach our fwnode.
In other word I don't see any race condition on my side and so I will not try to fix that point except if this "potential" issue is confirmed by other people. Indeed, I could be wrong in my analysis but without further details, I don't think the issue reported here can happen.
Best regards,
Hervé
PATCH v13 9/10 PCI: of: Remove fwnode_dev_initialized() call for a PCI root bridge node
In reply to Herve CodinaDuring the instantiation of devices described by a device-tree overlay applied on a PCI device, devlink displays the following kind of debug messages instead of creating the expected links:
'Not linking xxxx - might never become dev'
Without those expected links, the device removal order cannot be correct.
Those debug traces are printed by fw_devlink_create_devlink(). In our use case, they are all printed because the supplier of the link has at least one of its ancestor with its fwnode flag FWNODE_FLAG_INITIALIZED set.
The culprit ancestor is the PCI root bridge.
The fwnode related to the PCI root bridge is created dynamically by the of_pci_make_host_bridge_node() function. During this creation fwnode_dev_initialized() is called which set the FWNODE_FLAG_INITIALIZED flag.
Calling fwnode_dev_initialized() tells devlink that the device related to this node is handled out of the driver core. This is not correct in our case. Indeed the device related to this firmware node is handled using driver core mechanisms and is fully compliant devlink expectations.
Simply remove the fwnode_dev_initialized() call. With that done, the devlink debug messages are no more displayed and links that were missing are correctly created.
--- drivers/pci/of.c | 1 - 1 file changed, 1 deletion(-)
drivers/pci/of.c +0 -1
@@ -894,7 +894,6 @@ void of_pci_make_host_bridge_node(struct pci_host_bridge *bridge)*/ of_node_set_flag (np,OF_POPULATED );fw_devlink_set_device (&np->fwnode, &bridge->dev); -fwnode_dev_initialized (&np->fwnode, true); ret =of_changeset_apply (cset);if (ret)
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v13 09/10] PCI: of: Remove fwnode_dev_initialized() call for a PCI root bridge node
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines
During the instantiation of devices described by a device-tree overlay applied on a PCI device, devlink displays the following kind of debug messages instead of creating the expected links:
'Not linking xxxx - might never become dev'Without those expected links, the device removal order cannot be correct.
Those debug traces are printed by
fw_devlink_create_devlink(). In our use case, they are all printed because the supplier of the link has at least one of its ancestor with its fwnode flagFWNODE_FLAG_INITIALIZEDset.
Quoted 8 lines
The culprit ancestor is the PCI root bridge.
[ ... ]
are correctly created.Fixes 1f340724419e ("PCI: of: Create device tree PCI host bridge node")Signed-off-by Herve Codina <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
PATCH v13 10/10 PCI: of: Avoid np->data usage for the node changeset
In reply to Herve Codinaof_pci_remove_node() and of_pci_remove_host_bridge_node() check whether the node is dynamic but not whether it has valid private data.
During the node creation, an OF changeset is used and this changeset is stored in np->data to be available for removal functions.
If, for instance, a PCI host bridge is created using a device-tree overlay, the related node will have the dynamic flag set but np->data will be NULL. This leads to NULL pointer dereferences.
Checking for a non-NULL np->data pointer to determine if the node has been created by the PCI node creation process is not enough. Indeed, on some platforms like PowerPC, the OF_RECONFIG_ATTACH_NODE notifier (e.g., in the pci_dn_reconfig_notifier() function) intercepts node additions and populates np->data with its own structure, such as a struct pci_dn. In that case, np->data is not NULL but it is not related to our changeset stored during the PCI node process creation.
Avoid the usage of np->data to store the changeset used during the PCI node creation. Store our changeset in a more relevant structure: either struct pci_dev when the node is created for a PCI device or struct pci_host_bridge when the node is created for the PCI host bridge.
With that done, no ambiguity remains on removal. Indeed, this changeset, if non-NULL, is the one used during PCI node creation. Check and use this changeset on the removal process.
--- drivers/pci/of.c | 32 ++++++++++++++++++++------------ include/linux/pci.h | 11 +++++++++++ 2 files changed, 31 insertions(+), 12 deletions(-)
drivers/pci/of.c +20 -12
@@ -732,15 +732,20 @@ void of_pci_remove_node(struct pci_dev *pdev) {struct device_node *np; +if (!pdev->cset) +return ; + np =pci_device_to_OF_node (pdev); -if (!np || !of_node_check_flag (np,OF_DYNAMIC )) +if (!np)return ;fw_devlink_set_device (&np->fwnode,NULL );device_remove_of_node (&pdev->dev); -of_changeset_revert (np->data); -of_changeset_destroy (np->data); +of_changeset_revert (pdev->cset); +of_changeset_destroy (pdev->cset);of_node_put (np); +kfree (pdev->cset); + pdev->cset =NULL ; }void of_pci_make_dev_node (struct pci_dev *pdev)
@@ -800,18 +805,17 @@ void of_pci_make_dev_node(struct pci_dev *pdev)if (ret) goto out_clear_devlink_dev; - np->data = cset; - ret =device_add_of_node (&pdev->dev, np);if (ret) goto out_revert_cset; + pdev->cset = cset; +kfree (name);return ; out_revert_cset: - np->data =NULL ;of_changeset_revert (cset); out_clear_devlink_dev:fw_devlink_set_device (&np->fwnode,NULL );
@@ -828,16 +832,21 @@ void of_pci_remove_host_bridge_node(struct pci_host_bridge *bridge) {struct device_node *np; +if (!bridge->cset) +return ; + np =pci_bus_to_OF_node (bridge->bus); -if (!np || !of_node_check_flag (np,OF_DYNAMIC )) +if (!np)return ;fw_devlink_set_device (&np->fwnode,NULL );device_remove_of_node (&bridge->bus->dev);device_remove_of_node (&bridge->dev); -of_changeset_revert (np->data); -of_changeset_destroy (np->data); +of_changeset_revert (bridge->cset); +of_changeset_destroy (bridge->cset);of_node_put (np); +kfree (bridge->cset); + bridge->cset =NULL ; }void of_pci_make_host_bridge_node (struct pci_host_bridge *bridge)
@@ -899,8 +908,6 @@ void of_pci_make_host_bridge_node(struct pci_host_bridge *bridge)if (ret) goto out_clear_devlink_dev; - np->data = cset; -/* Add the of_node to host bridge and the root bus */ ret =device_add_of_node (&bridge->dev, np);if (ret)
@@ -910,6 +917,8 @@ void of_pci_make_host_bridge_node(struct pci_host_bridge *bridge)if (ret) goto out_remove_bridge_dev_of_node; + bridge->cset = cset; +kfree (name);return ;
@@ -917,7 +926,6 @@ void of_pci_make_host_bridge_node(struct pci_host_bridge *bridge) out_remove_bridge_dev_of_node:device_remove_of_node (&bridge->dev); out_revert_cset: - np->data =NULL ;of_changeset_revert (cset); out_clear_devlink_dev:fw_devlink_set_device (&np->fwnode,NULL );
include/linux/pci.h +11 -0
@@ -339,6 +339,9 @@ struct pcie_link_state;struct pci_sriov ;struct pci_p2pdma ;struct rcec_ea ; +#ifdef CONFIG_PCI_DYNAMIC_OF_NODES +struct of_changeset ; +#endif /* struct pci_dev - describes a PCI device *
@@ -598,6 +601,10 @@ struct pci_dev {u8 tph_mode;/* TPH mode */ u8 tph_req_type;/* TPH requester type */ #endif + +#ifdef CONFIG_PCI_DYNAMIC_OF_NODES +struct of_changeset *cset;/* Changeset used for OF node creation */ +#endif };static inline struct pci_dev *pci_physfn (struct pci_dev *dev)
@@ -670,6 +677,10 @@ struct pci_host_bridge { unsigned int broken_l1ss_resume:1; /* Resuming from L1SS duringsystem suspend is broken */ +#ifdef CONFIG_PCI_DYNAMIC_OF_NODES +struct of_changeset *cset;/* Changeset used for OF node creation */ +#endif +/* Resource alignment requirements */ resource_size_t (*align_resource)(struct pci_dev *dev, const struct resource *res,
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v13 10/10] PCI: of: Avoid np->data usage for the node changeset
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 17 lines
of_pci_remove_node()andof_pci_remove_host_bridge_node()check whether the node is dynamic but not whether it has valid private data.During the node creation, an OF changeset is used and this changeset is stored in
np->datato be available for removal functions.If, for instance, a PCI host bridge is created using a device-tree overlay, the related node will have the dynamic flag set but
np->datawill be NULL. This leads to NULL pointer dereferences.Checking for a non-NULL
np->datapointer to determine if the node has been created by the PCI node creation process is not enough. Indeed, on some platforms like PowerPC, theOF_RECONFIG_ATTACH_NODEnotifier (e.g., in thepci_dn_reconfig_notifier()function) intercepts node additions and populatesnp->datawith its own structure, such as a [ ... ]
Quoted 4 lines
Fixes 407d1a51921e ("PCI: Create device tree node for bridge")Fixes 1f340724419e ("PCI: of: Create device tree PCI host bridge node")Cc <[email protected]> # Issue not triggered but could be a problemSigned-off-by Herve Codina <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
Re: [PATCH v13 10/10] PCI: of: Avoid np->data usage for the node changeset
In reply to Herve CodinaQuoted 23 lines · Herve Codina
of_pci_remove_node()andof_pci_remove_host_bridge_node()check whether the node is dynamic but not whether it has valid private data.During the node creation, an OF changeset is used and this changeset is stored in
np->datato be available for removal functions.If, for instance, a PCI host bridge is created using a device-tree overlay, the related node will have the dynamic flag set but
np->datawill be NULL. This leads to NULL pointer dereferences.Checking for a non-NULL
np->datapointer to determine if the node has been created by the PCI node creation process is not enough. Indeed, on some platforms like PowerPC, theOF_RECONFIG_ATTACH_NODEnotifier (e.g., in thepci_dn_reconfig_notifier()function) intercepts node additions and populatesnp->datawith its own structure, such as a structpci_dn. In that case,np->datais not NULL but it is not related to our changeset stored during the PCI node process creation.Avoid the usage of
np->datato store the changeset used during the PCI node creation. Store our changeset in a more relevant structure: either structpci_devwhen the node is created for a PCI device or structpci_host_bridgewhen the node is created for the PCI host bridge.
Quoted 8 lines
With that done, no ambiguity remains on removal. Indeed, this changeset, if non-NULL, is the one used during PCI node creation. Check and use this changeset on the removal process.
Fixes 407d1a51921e ("PCI: Create device tree node for bridge")Fixes 1f340724419e ("PCI: of: Create device tree PCI host bridge node")Cc <[email protected]> # Issue not triggered but could be a problemSigned-off-by Herve Codina <[email protected]>
Quoted 144 lines
--- drivers/pci/of.c | 32 ++++++++++++++++++++------------ include/linux/pci.h | 11 +++++++++++ 2 files changed, 31 insertions(+), 12 deletions(-)drivers/pci/of.c +20
-12@@ -732,15 +732,20 @@ void of_pci_remove_node(struct pci_dev *pdev) {struct device_node *np; +if (!pdev->cset) +return ; + np =pci_device_to_OF_node (pdev); -if (!np || !of_node_check_flag (np,OF_DYNAMIC )) +if (!np)return ;fw_devlink_set_device (&np->fwnode,NULL );device_remove_of_node (&pdev->dev); -of_changeset_revert (np->data); -of_changeset_destroy (np->data); +of_changeset_revert (pdev->cset); +of_changeset_destroy (pdev->cset);of_node_put (np); +kfree (pdev->cset); + pdev->cset =NULL ; }void of_pci_make_dev_node (struct pci_dev *pdev)@@ -800,18 +805,17 @@ void of_pci_make_dev_node(struct pci_dev *pdev)if (ret) goto out_clear_devlink_dev; - np->data = cset; - ret =device_add_of_node (&pdev->dev, np);if (ret) goto out_revert_cset; + pdev->cset = cset; +kfree (name);return ; out_revert_cset: - np->data =NULL ;of_changeset_revert (cset); out_clear_devlink_dev:fw_devlink_set_device (&np->fwnode,NULL );@@ -828,16 +832,21 @@ void of_pci_remove_host_bridge_node(struct pci_host_bridge *bridge) {struct device_node *np; +if (!bridge->cset) +return ; + np =pci_bus_to_OF_node (bridge->bus); -if (!np || !of_node_check_flag (np,OF_DYNAMIC )) +if (!np)return ;fw_devlink_set_device (&np->fwnode,NULL );device_remove_of_node (&bridge->bus->dev);device_remove_of_node (&bridge->dev); -of_changeset_revert (np->data); -of_changeset_destroy (np->data); +of_changeset_revert (bridge->cset); +of_changeset_destroy (bridge->cset);of_node_put (np); +kfree (bridge->cset); + bridge->cset =NULL ; }void of_pci_make_host_bridge_node (struct pci_host_bridge *bridge)@@ -899,8 +908,6 @@ void of_pci_make_host_bridge_node(struct pci_host_bridge *bridge)if (ret) goto out_clear_devlink_dev; - np->data = cset; -/* Add the of_node to host bridge and the root bus */ ret =device_add_of_node (&bridge->dev, np);if (ret)@@ -910,6 +917,8 @@ void of_pci_make_host_bridge_node(struct pci_host_bridge *bridge)if (ret) goto out_remove_bridge_dev_of_node; + bridge->cset = cset; +kfree (name);return ;@@ -917,7 +926,6 @@ void of_pci_make_host_bridge_node(struct pci_host_bridge *bridge) out_remove_bridge_dev_of_node:device_remove_of_node (&bridge->dev); out_revert_cset: - np->data =NULL ;of_changeset_revert (cset); out_clear_devlink_dev:fw_devlink_set_device (&np->fwnode,NULL );include/linux/pci.h +11
-0@@ -339,6 +339,9 @@ struct pcie_link_state;struct pci_sriov ;struct pci_p2pdma ;struct rcec_ea ; +#ifdef CONFIG_PCI_DYNAMIC_OF_NODES +struct of_changeset ; +#endif /* struct pci_dev - describes a PCI device * @@ -598,6 +601,10 @@ struct pci_dev {u8 tph_mode;/* TPH mode */ u8 tph_req_type;/* TPH requester type */ #endif + +#ifdef CONFIG_PCI_DYNAMIC_OF_NODES +struct of_changeset *cset;/* Changeset used for OF node creation */ +#endif };static inline struct pci_dev *pci_physfn (struct pci_dev *dev)@@ -670,6 +677,10 @@ struct pci_host_bridge { unsigned int broken_l1ss_resume:1; /* Resuming from L1SS duringsystem suspend is broken */ +#ifdef CONFIG_PCI_DYNAMIC_OF_NODES +struct of_changeset *cset;/* Changeset used for OF node creation */ +#endif +/* Resource alignment requirements */ resource_size_t (*align_resource)(struct pci_dev *dev, const struct resource *res,Signature · 1 line
2.55.0
Hi Herve,
Quoted 2 lines · Herve Codina
Patches 8 and 9 fix
fw_devlinkissues specific to PCI and the device-tree nodes created during enumeration.
We hit exactly this on x86 switch platforms with CONFIG_PCI_DYNAMIC_OF_NODES. A driver applies a device-tree overlay onto each Xilinx FPGA PCI endpoint and populates I2C controllers (xiic), watchdogs and GPIO blocks. Their interrupts go through small chained irqchips that are themselves fed by the FPGA's MSI vectors.
Without this series, fw_devlink drops every supplier link under the synthesized PCI root bridge node ("Not linking ... might never become dev"), so the irqchips end up with no consumers. Unbinding one of them frees IRQ descriptors that the I2C controllers and the watchdog still hold. We get a remove_proc_entry "removing non-empty directory 'irq/N'" WARNING per IRQ, and the I2C controllers are dead until reboot.
With the series applied on v7.3, the device links are created (up to 32 consumers per irqchip), and unbinding an irqchip now unbinds its consumers first. I tested on two platforms, with 2 and 3 FPGAs (4 and 7 irqchips). Repeated unload/reload of the irqchip driver was clean every time, and everything probed again afterwards.
Thanks,
Abdurrahman