diff mbox series

[v7,1/5] driver core: Introduce device_{add,remove}_of_node()

Message ID 20250204073501.278248-2-herve.codina@bootlin.com (mailing list archive)
State Superseded
Delegated to: Bjorn Helgaas
Headers show
Series Add support for the PCI host bridge device-tree node creation. | expand

Commit Message

Herve Codina Feb. 4, 2025, 7:34 a.m. UTC
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().

Signed-off-by: Herve Codina <herve.codina@bootlin.com>
---
 drivers/base/core.c    | 61 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/device.h |  2 ++
 2 files changed, 63 insertions(+)

Comments

Greg KH Feb. 19, 2025, 8:27 a.m. UTC | #1
On Tue, Feb 04, 2025 at 08:34:56AM +0100, Herve Codina wrote:
> 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().
> 
> Signed-off-by: Herve Codina <herve.codina@bootlin.com>
> ---
>  drivers/base/core.c    | 61 ++++++++++++++++++++++++++++++++++++++++++
>  include/linux/device.h |  2 ++
>  2 files changed, 63 insertions(+)
> 

Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Jonathan Cameron Feb. 19, 2025, 3:59 p.m. UTC | #2
On Tue,  4 Feb 2025 08:34:56 +0100
Herve Codina <herve.codina@bootlin.com> wrote:

> 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().
> 
> Signed-off-by: Herve Codina <herve.codina@bootlin.com>
A few passing comments. Not suggestions to actually change anything
at this stage though. Maybe a potential follow up if you think it's
a good idea.

> ---
>  drivers/base/core.c    | 61 ++++++++++++++++++++++++++++++++++++++++++
>  include/linux/device.h |  2 ++
>  2 files changed, 63 insertions(+)
> 
> diff --git a/drivers/base/core.c b/drivers/base/core.c
> index 5a1f05198114..d1b044af64de 100644
> --- a/drivers/base/core.c
> +++ b/drivers/base/core.c
> @@ -5170,6 +5170,67 @@ 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;
Maybe use
	struct device *d __free(put_device) = get_device(dev);

	if (!d->of_node);
		return;

Not a reason to respin though!


> +
> +	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
> + *
> + * Return: 0 on success or error code on failure.
> + */
> +int device_add_of_node(struct device *dev, struct device_node *of_node)
> +{
> +	int ret;
> +
> +	if (!of_node)
> +		return -EINVAL;
> +
> +	dev = get_device(dev);

Likewise could use __free() magic here as well for slight simpliciations.

> +	if (!dev)
> +		return -EINVAL;
> +
> +	if (dev->of_node) {
> +		dev_err(dev, "Cannot replace node %pOF with %pOF\n",
> +			dev->of_node, of_node);
> +		ret = -EBUSY;
> +		goto end;
> +	}
> +
> +	dev->of_node = of_node_get(of_node);
> +
> +	if (!dev->fwnode)
> +		dev->fwnode = of_fwnode_handle(of_node);
> +
> +	ret = 0;
> +end:
> +	put_device(dev);
> +	return ret;
> +}
> +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 80a5b3268986..1244e5892292 100644
> --- a/include/linux/device.h
> +++ b/include/linux/device.h
> @@ -1191,6 +1191,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);
> +int 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)
Herve Codina Feb. 20, 2025, 8:12 a.m. UTC | #3
Hi Jonathan,

On Wed, 19 Feb 2025 15:59:01 +0000
Jonathan Cameron <Jonathan.Cameron@huawei.com> wrote:

...

> > Signed-off-by: Herve Codina <herve.codina@bootlin.com>  
> A few passing comments. Not suggestions to actually change anything
> at this stage though. Maybe a potential follow up if you think it's
> a good idea.
> 
...

> > +void device_remove_of_node(struct device *dev)
> > +{
> > +	dev = get_device(dev);
> > +	if (!dev)
> > +		return;  
> Maybe use
> 	struct device *d __free(put_device) = get_device(dev);
> 
> 	if (!d->of_node);
> 		return;
> 
> Not a reason to respin though!
> 
> 
...

> > +int device_add_of_node(struct device *dev, struct device_node *of_node)
> > +{
> > +	int ret;
> > +
> > +	if (!of_node)
> > +		return -EINVAL;
> > +
> > +	dev = get_device(dev);  
> 
> Likewise could use __free() magic here as well for slight simpliciations.
> 

I see. Indeed, the __free(put_device) can be an improvement in core.c

I think that this has to be done out of this series in a more globally way
because put_device() is used in several place in this file and having a mix
between __free(put_device) and put_device() calls in a goto label is not the
best solution.

For this reason, as you proposed except if someone else pushes in the
__free(put_device) direction in functions introduced in this patch, I
prefer to keep this patch as it is.

Thanks for your feedback,
Hervé
diff mbox series

Patch

diff --git a/drivers/base/core.c b/drivers/base/core.c
index 5a1f05198114..d1b044af64de 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -5170,6 +5170,67 @@  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
+ *
+ * Return: 0 on success or error code on failure.
+ */
+int device_add_of_node(struct device *dev, struct device_node *of_node)
+{
+	int ret;
+
+	if (!of_node)
+		return -EINVAL;
+
+	dev = get_device(dev);
+	if (!dev)
+		return -EINVAL;
+
+	if (dev->of_node) {
+		dev_err(dev, "Cannot replace node %pOF with %pOF\n",
+			dev->of_node, of_node);
+		ret = -EBUSY;
+		goto end;
+	}
+
+	dev->of_node = of_node_get(of_node);
+
+	if (!dev->fwnode)
+		dev->fwnode = of_fwnode_handle(of_node);
+
+	ret = 0;
+end:
+	put_device(dev);
+	return ret;
+}
+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 80a5b3268986..1244e5892292 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -1191,6 +1191,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);
+int 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)