public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/6] driver core: Introduce device_{add,remove}_of_node()
       [not found] <20241104172001.165640-1-herve.codina@bootlin.com>
@ 2024-11-04 17:19 ` Herve Codina
  2024-11-04 17:19 ` [PATCH 2/6] PCI: of: Use device_{add,remove}_of_node() to attach of_node to existing device Herve Codina
  1 sibling, 0 replies; 4+ messages in thread
From: Herve Codina @ 2024-11-04 17:19 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Rob Herring,
	Saravana Kannan, Bjorn Helgaas, Lizhi Hou
  Cc: linux-kernel, devicetree, linux-pci, Allan Nielsen,
	Horatiu Vultur, Steen Hegelund, Thomas Petazzoni, Herve Codina,
	stable

An of_node can be set to a device using device_set_node().
This function cannot prevent any of_node and/or fwnode overwrites.

When adding an of_node on an already present device, the following
operations need to be done:
- Attach the of_node if no of_node were already attached
- Attach the of_node as a fwnode if no fwnode were already attached

This is the purpose of device_add_of_node().
device_remove_of_node() reverts the operations done by
device_add_of_node().

Cc: stable@vger.kernel.org
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
---
 drivers/base/core.c    | 52 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/device.h |  2 ++
 2 files changed, 54 insertions(+)

diff --git a/drivers/base/core.c b/drivers/base/core.c
index 24c572031403..0aa63371f55d 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -5118,6 +5118,58 @@ void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
 }
 EXPORT_SYMBOL_GPL(set_secondary_fwnode);
 
+/**
+ * device_remove_of_node - Remove an of_node from a device
+ * @dev: device whose device-tree node is being removed
+ */
+void device_remove_of_node(struct device *dev)
+{
+	dev = get_device(dev);
+	if (!dev)
+		return;
+
+	if (!dev->of_node)
+		goto end;
+
+	if (dev->fwnode == of_fwnode_handle(dev->of_node))
+		dev->fwnode = NULL;
+
+	of_node_put(dev->of_node);
+	dev->of_node = NULL;
+
+end:
+	put_device(dev);
+}
+EXPORT_SYMBOL_GPL(device_remove_of_node);
+
+/**
+ * device_add_of_node - Add an of_node to an existing device
+ * @dev: device whose device-tree node is being added
+ * @of_node: of_node to add
+ */
+void device_add_of_node(struct device *dev, struct device_node *of_node)
+{
+	if (!of_node)
+		return;
+
+	dev = get_device(dev);
+	if (!dev)
+		return;
+
+	if (WARN(dev->of_node, "%s: Cannot replace node %pOF with %pOF\n",
+		 dev_name(dev), dev->of_node, of_node))
+		goto end;
+
+	dev->of_node = of_node_get(of_node);
+
+	if (!dev->fwnode)
+		dev->fwnode = of_fwnode_handle(of_node);
+
+end:
+	put_device(dev);
+}
+EXPORT_SYMBOL_GPL(device_add_of_node);
+
 /**
  * device_set_of_node_from_dev - reuse device-tree node of another device
  * @dev: device whose device-tree node is being set
diff --git a/include/linux/device.h b/include/linux/device.h
index b4bde8d22697..e3aa25ce1f90 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -1146,6 +1146,8 @@ int device_online(struct device *dev);
 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
 void device_set_node(struct device *dev, struct fwnode_handle *fwnode);
+void device_add_of_node(struct device *dev, struct device_node *of_node);
+void device_remove_of_node(struct device *dev);
 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2);
 
 static inline struct device_node *dev_of_node(struct device *dev)
-- 
2.46.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/6] PCI: of: Use device_{add,remove}_of_node() to attach of_node to existing device
       [not found] <20241104172001.165640-1-herve.codina@bootlin.com>
  2024-11-04 17:19 ` [PATCH 1/6] driver core: Introduce device_{add,remove}_of_node() Herve Codina
@ 2024-11-04 17:19 ` Herve Codina
  2024-11-04 20:20   ` Rob Herring
  1 sibling, 1 reply; 4+ messages in thread
From: Herve Codina @ 2024-11-04 17:19 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Rob Herring,
	Saravana Kannan, Bjorn Helgaas, Lizhi Hou
  Cc: linux-kernel, devicetree, linux-pci, Allan Nielsen,
	Horatiu Vultur, Steen Hegelund, Thomas Petazzoni, Herve Codina,
	stable

The commit 407d1a51921e ("PCI: Create device tree node for bridge")
creates of_node for PCI devices. The newly created of_node is attached
to an existing device. This is done setting directly pdev->dev.of_node
in the code.

Even if pdev->dev.of_node cannot be previously set, this doesn't handle
the fwnode field of the struct device. Indeed, this field needs to be
set if it hasn't already been set.

device_{add,remove}_of_node() have been introduced to handle this case.

Use them instead of the direct setting.

Fixes: 407d1a51921e ("PCI: Create device tree node for bridge")
Cc: stable@vger.kernel.org
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
---
 drivers/pci/of.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/pci/of.c b/drivers/pci/of.c
index dacea3fc5128..141ffbb1b3e6 100644
--- a/drivers/pci/of.c
+++ b/drivers/pci/of.c
@@ -655,8 +655,8 @@ void of_pci_remove_node(struct pci_dev *pdev)
 	np = pci_device_to_OF_node(pdev);
 	if (!np || !of_node_check_flag(np, OF_DYNAMIC))
 		return;
-	pdev->dev.of_node = NULL;
 
+	device_remove_of_node(&pdev->dev);
 	of_changeset_revert(np->data);
 	of_changeset_destroy(np->data);
 	of_node_put(np);
@@ -713,7 +713,7 @@ void of_pci_make_dev_node(struct pci_dev *pdev)
 		goto out_free_node;
 
 	np->data = cset;
-	pdev->dev.of_node = np;
+	device_add_of_node(&pdev->dev, np);
 	kfree(name);
 
 	return;
-- 
2.46.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/6] PCI: of: Use device_{add,remove}_of_node() to attach of_node to existing device
  2024-11-04 17:19 ` [PATCH 2/6] PCI: of: Use device_{add,remove}_of_node() to attach of_node to existing device Herve Codina
@ 2024-11-04 20:20   ` Rob Herring
  2024-11-05 16:16     ` Herve Codina
  0 siblings, 1 reply; 4+ messages in thread
From: Rob Herring @ 2024-11-04 20:20 UTC (permalink / raw)
  To: Herve Codina
  Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Saravana Kannan,
	Bjorn Helgaas, Lizhi Hou, linux-kernel, devicetree, linux-pci,
	Allan Nielsen, Horatiu Vultur, Steen Hegelund, Thomas Petazzoni,
	stable

On Mon, Nov 04, 2024 at 06:19:56PM +0100, Herve Codina wrote:
> The commit 407d1a51921e ("PCI: Create device tree node for bridge")
> creates of_node for PCI devices. The newly created of_node is attached
> to an existing device. This is done setting directly pdev->dev.of_node
> in the code.
> 
> Even if pdev->dev.of_node cannot be previously set, this doesn't handle
> the fwnode field of the struct device. Indeed, this field needs to be
> set if it hasn't already been set.
> 
> device_{add,remove}_of_node() have been introduced to handle this case.
> 
> Use them instead of the direct setting.
> 
> Fixes: 407d1a51921e ("PCI: Create device tree node for bridge")
> Cc: stable@vger.kernel.org

I don't think this is stable material. What exactly would is broken 
which would be fixed by just the first 2 patches?


> Signed-off-by: Herve Codina <herve.codina@bootlin.com>
> ---
>  drivers/pci/of.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/pci/of.c b/drivers/pci/of.c
> index dacea3fc5128..141ffbb1b3e6 100644
> --- a/drivers/pci/of.c
> +++ b/drivers/pci/of.c
> @@ -655,8 +655,8 @@ void of_pci_remove_node(struct pci_dev *pdev)
>  	np = pci_device_to_OF_node(pdev);
>  	if (!np || !of_node_check_flag(np, OF_DYNAMIC))
>  		return;
> -	pdev->dev.of_node = NULL;
>  
> +	device_remove_of_node(&pdev->dev);
>  	of_changeset_revert(np->data);
>  	of_changeset_destroy(np->data);
>  	of_node_put(np);
> @@ -713,7 +713,7 @@ void of_pci_make_dev_node(struct pci_dev *pdev)
>  		goto out_free_node;
>  
>  	np->data = cset;
> -	pdev->dev.of_node = np;
> +	device_add_of_node(&pdev->dev, np);
>  	kfree(name);
>  
>  	return;
> -- 
> 2.46.2
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/6] PCI: of: Use device_{add,remove}_of_node() to attach of_node to existing device
  2024-11-04 20:20   ` Rob Herring
@ 2024-11-05 16:16     ` Herve Codina
  0 siblings, 0 replies; 4+ messages in thread
From: Herve Codina @ 2024-11-05 16:16 UTC (permalink / raw)
  To: Rob Herring
  Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Saravana Kannan,
	Bjorn Helgaas, Lizhi Hou, linux-kernel, devicetree, linux-pci,
	Allan Nielsen, Horatiu Vultur, Steen Hegelund, Thomas Petazzoni,
	stable

Hi Rob,

On Mon, 4 Nov 2024 14:20:08 -0600
Rob Herring <robh@kernel.org> wrote:

> On Mon, Nov 04, 2024 at 06:19:56PM +0100, Herve Codina wrote:
> > The commit 407d1a51921e ("PCI: Create device tree node for bridge")
> > creates of_node for PCI devices. The newly created of_node is attached
> > to an existing device. This is done setting directly pdev->dev.of_node
> > in the code.
> > 
> > Even if pdev->dev.of_node cannot be previously set, this doesn't handle
> > the fwnode field of the struct device. Indeed, this field needs to be
> > set if it hasn't already been set.
> > 
> > device_{add,remove}_of_node() have been introduced to handle this case.
> > 
> > Use them instead of the direct setting.
> > 
> > Fixes: 407d1a51921e ("PCI: Create device tree node for bridge")
> > Cc: stable@vger.kernel.org  
> 
> I don't think this is stable material. What exactly would is broken 
> which would be fixed by just the first 2 patches?

Hum indeed, I haven't observed a broken behavior in current kernel.
I will remove Fixes and Cc in the next iteration.

Best regards,
Hervé

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-11-05 16:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20241104172001.165640-1-herve.codina@bootlin.com>
2024-11-04 17:19 ` [PATCH 1/6] driver core: Introduce device_{add,remove}_of_node() Herve Codina
2024-11-04 17:19 ` [PATCH 2/6] PCI: of: Use device_{add,remove}_of_node() to attach of_node to existing device Herve Codina
2024-11-04 20:20   ` Rob Herring
2024-11-05 16:16     ` Herve Codina

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox