[PATCH v4 5/7] i2c: core: Do not dereference fwnode in struct device

Andy Shevchenko posted 7 patches 10 months ago
There is a newer version of this series
[PATCH v4 5/7] i2c: core: Do not dereference fwnode in struct device
Posted by Andy Shevchenko 10 months ago
In order to make the underneath API easier to change in the future,
prevent users from dereferencing fwnode from struct device.
Instead, use the specific device_set_node() API for that.

Tested-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
Reviewed-by: Sakari Ailus <sakari.ailus@linux.intel.com>
Acked-by: Sakari Ailus <sakari.ailus@linux.intel.com>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/i2c/i2c-core-base.c | 18 ++++++++++--------
 drivers/i2c/i2c-core-of.c   |  1 -
 2 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c
index 9ca1ade043ed..dc3c60a7d382 100644
--- a/drivers/i2c/i2c-core-base.c
+++ b/drivers/i2c/i2c-core-base.c
@@ -961,6 +961,7 @@ static void i2c_unlock_addr(struct i2c_adapter *adap, unsigned short addr,
 struct i2c_client *
 i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
 {
+	struct fwnode_handle *fwnode;
 	struct i2c_client *client;
 	bool need_put = false;
 	int status;
@@ -1001,18 +1002,19 @@ i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *inf
 	client->dev.parent = &client->adapter->dev;
 	client->dev.bus = &i2c_bus_type;
 	client->dev.type = &i2c_client_type;
-	client->dev.of_node = of_node_get(info->of_node);
-	client->dev.fwnode = info->fwnode;
 
 	device_enable_async_suspend(&client->dev);
 
+	fwnode = info->fwnode ?: of_fwnode_handle(info->of_node);
+	device_set_node(&client->dev, fwnode_handle_get(fwnode));
+
 	if (info->swnode) {
 		status = device_add_software_node(&client->dev, info->swnode);
 		if (status) {
 			dev_err(&adap->dev,
 				"Failed to add software node to client %s: %d\n",
 				client->name, status);
-			goto out_err_put_of_node;
+			goto out_err_put_fwnode;
 		}
 	}
 
@@ -1031,8 +1033,8 @@ i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *inf
 out_remove_swnode:
 	device_remove_software_node(&client->dev);
 	need_put = true;
-out_err_put_of_node:
-	of_node_put(info->of_node);
+out_err_put_fwnode:
+	fwnode_handle_put(fwnode);
 out_err:
 	dev_err(&adap->dev,
 		"Failed to register i2c client %s at 0x%02x (%d)\n",
@@ -1060,11 +1062,11 @@ void i2c_unregister_device(struct i2c_client *client)
 		return;
 
 	fwnode = dev_fwnode(&client->dev);
-	if (is_of_node(fwnode)) {
+	if (is_of_node(fwnode))
 		of_node_clear_flag(to_of_node(fwnode), OF_POPULATED);
-		of_node_put(client->dev.of_node);
-	} else if (is_acpi_device_node(fwnode))
+	else if (is_acpi_device_node(fwnode))
 		acpi_device_clear_enumerated(to_acpi_device_node(fwnode));
+	fwnode_handle_put(fwnode);
 
 	device_remove_software_node(&client->dev);
 	device_unregister(&client->dev);
diff --git a/drivers/i2c/i2c-core-of.c b/drivers/i2c/i2c-core-of.c
index 02feee6c9ba9..eb7fb202355f 100644
--- a/drivers/i2c/i2c-core-of.c
+++ b/drivers/i2c/i2c-core-of.c
@@ -49,7 +49,6 @@ int of_i2c_get_board_info(struct device *dev, struct device_node *node,
 	}
 
 	info->addr = addr;
-	info->of_node = node;
 	info->fwnode = of_fwnode_handle(node);
 
 	if (of_property_read_bool(node, "host-notify"))
-- 
2.47.2
Re: [PATCH v4 5/7] i2c: core: Do not dereference fwnode in struct device
Posted by Wolfram Sang 9 months, 4 weeks ago
On Mon, Apr 14, 2025 at 01:01:55PM +0300, Andy Shevchenko wrote:
> In order to make the underneath API easier to change in the future,
> prevent users from dereferencing fwnode from struct device.
> Instead, use the specific device_set_node() API for that.
> 
> Tested-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
> Reviewed-by: Sakari Ailus <sakari.ailus@linux.intel.com>
> Acked-by: Sakari Ailus <sakari.ailus@linux.intel.com>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

I'll check this patch later today. Rest of the series looks good to me
already.

Thanks!

Re: [PATCH v4 5/7] i2c: core: Do not dereference fwnode in struct device
Posted by Andy Shevchenko 9 months, 4 weeks ago
On Wed, Apr 16, 2025 at 08:55:27AM +0200, Wolfram Sang wrote:
> On Mon, Apr 14, 2025 at 01:01:55PM +0300, Andy Shevchenko wrote:
> > In order to make the underneath API easier to change in the future,
> > prevent users from dereferencing fwnode from struct device.
> > Instead, use the specific device_set_node() API for that.
> > 
> > Tested-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
> > Reviewed-by: Sakari Ailus <sakari.ailus@linux.intel.com>
> > Acked-by: Sakari Ailus <sakari.ailus@linux.intel.com>
> > Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> 
> I'll check this patch later today. Rest of the series looks good to me
> already.

Note, I'm planning to send a v5 shortly with the style fixes and commit
messages as you suggested.

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH v4 5/7] i2c: core: Do not dereference fwnode in struct device
Posted by Wolfram Sang 9 months, 4 weeks ago
On Wed, Apr 16, 2025 at 10:02:15AM +0300, Andy Shevchenko wrote:
> On Wed, Apr 16, 2025 at 08:55:27AM +0200, Wolfram Sang wrote:
> > On Mon, Apr 14, 2025 at 01:01:55PM +0300, Andy Shevchenko wrote:
> > > In order to make the underneath API easier to change in the future,
> > > prevent users from dereferencing fwnode from struct device.
> > > Instead, use the specific device_set_node() API for that.
> > > 
> > > Tested-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
> > > Reviewed-by: Sakari Ailus <sakari.ailus@linux.intel.com>
> > > Acked-by: Sakari Ailus <sakari.ailus@linux.intel.com>
> > > Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> > 
> > I'll check this patch later today. Rest of the series looks good to me
> > already.
> 
> Note, I'm planning to send a v5 shortly with the style fixes and commit
> messages as you suggested.

Please wait with that like half an hour.

Re: [PATCH v4 5/7] i2c: core: Do not dereference fwnode in struct device
Posted by Andy Shevchenko 9 months, 4 weeks ago
On Wed, Apr 16, 2025 at 09:20:52AM +0200, Wolfram Sang wrote:
> On Wed, Apr 16, 2025 at 10:02:15AM +0300, Andy Shevchenko wrote:
> > On Wed, Apr 16, 2025 at 08:55:27AM +0200, Wolfram Sang wrote:
> > > On Mon, Apr 14, 2025 at 01:01:55PM +0300, Andy Shevchenko wrote:
> > > > In order to make the underneath API easier to change in the future,
> > > > prevent users from dereferencing fwnode from struct device.
> > > > Instead, use the specific device_set_node() API for that.
> > > > 
> > > > Tested-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
> > > > Reviewed-by: Sakari Ailus <sakari.ailus@linux.intel.com>
> > > > Acked-by: Sakari Ailus <sakari.ailus@linux.intel.com>
> > > > Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> > > 
> > > I'll check this patch later today. Rest of the series looks good to me
> > > already.
> > 
> > Note, I'm planning to send a v5 shortly with the style fixes and commit
> > messages as you suggested.
> 
> Please wait with that like half an hour.

Too late, I have sent it already...

-- 
With Best Regards,
Andy Shevchenko