Skip to contentSkip to conversation

i2c: provide and use i2c_adapter_set_node()

PATCH Applied Bartosz Golaszewski 4 participants 14 messages Feb 23 – Feb 26

Subsystems (11)
lore
Share & tools
lore
#0 lore raw

v2 cover i2c: provide and use i2c_adapter_set_node()

It's been another year of discussing the object life-time problems at conferences. I2C is one of the offenders and its problems are more complex than those of some other subsystems. It seems the revocable[1] API may make its way into the kernel this year but even with it in place, I2C won't be able to use it as there's currently nothing to *revoke*. The struct device is embedded within the i2c_adapter struct whose lifetime is tied to the provider device being bound to its driver.

Fixing this won't be fast and easy but nothing's going to happen if we don't start chipping away at it. The ultimate goal in order to be able to use an SRCU-based solution (revocable or otherwise) is to convert the embedded struct device in struct i2c_adapter into an __rcu pointer that can be *revoked*. To that end we need to hide all dereferences of adap->dev in drivers.

I2C drivers use device_set_node() to assign a firmware node directly to the struct device embedded in i2c_adapter. In order to hide the direct
dereferencing: provide a dedicated interface that wraps the call to device_set_node() but takes the adapter as argument instead.

---
Changes in v2:
- add patches replacing ACPI_COMPANION_SET() with i2c_adapter_set_node()
- rebased on top of v7.0-rc1
- collected tags
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Bartosz Golaszewski (9):
i2c: provide i2c_adapter_set_node()
i2c: designware-common: use i2c_adapter_set_node()
i2c: gpio: use i2c_adapter_set_node()
i2c: ljca: use i2c_adapter_set_node()
i2c: ls2x: use i2c_adapter_set_node()
i2c: rzv2m: use i2c_adapter_set_node()
i2c: usbio: use i2c_adapter_set_node()
i2c: designware-amdisp: use i2c_adapter_set_node()
i2c: viai2c-zhaoxin: use i2c_adapter_set_node()

 drivers/i2c/busses/i2c-designware-amdisp.c | 2 +-
 drivers/i2c/busses/i2c-designware-common.c | 2 +-
 drivers/i2c/busses/i2c-gpio.c              | 2 +-
 drivers/i2c/busses/i2c-ljca.c              | 2 +-
 drivers/i2c/busses/i2c-ls2x.c              | 2 +-
 drivers/i2c/busses/i2c-rzv2m.c             | 2 +-
 drivers/i2c/busses/i2c-usbio.c             | 2 +-
 drivers/i2c/busses/i2c-viai2c-zhaoxin.c    | 2 +-
 include/linux/i2c.h                        | 7 +++++++
 9 files changed, 15 insertions(+), 8 deletions(-)

---
base-commit: 6de23f81a5e08be8fbf5e8d7e9febc72a5b5f27f
change-id: 20251223-i2c-ada-dev-set-node-0636a8d3666c

Best regards,

Signature · 1 line
Bartosz Golaszewski <[email protected]>
#1 lore raw

PATCH v2 1/9 i2c: provide i2c_adapter_set_node()

Provide a wrapper around device_set_node() that takes the i2c_adapter as argument. This allows us to hide more dereferencing of the embedded struct device as part of working towards fixing the object lifetime issues in i2c.

---
 include/linux/i2c.h | 7 +++++++
 1 file changed, 7 insertions(+)
include/linux/i2c.h +7 -0
index 20fd41b51d5c85ee1665395c07345faafd8e2fca..38448bdfac671adcdf341adbe2f52341e5aec41a 100644
@@ -29,6 +29,7 @@ extern const struct device_type i2c_client_type;
 
 /* --- General options ------------------------------------------------	*/
 
+struct fwnode_handle;
 struct i2c_msg;
 struct i2c_adapter;
 struct i2c_client;
@@ -778,6 +779,12 @@ static inline void i2c_set_adapdata(struct i2c_adapter *adap, void *data)
 	dev_set_drvdata(&adap->dev, data);
 }
 
+static inline void i2c_adapter_set_node(struct i2c_adapter *adap,
+					struct fwnode_handle *fwnode)
+{
+	device_set_node(&adap->dev, fwnode);
+}
+
 static inline struct i2c_adapter *
 i2c_parent_is_i2c_adapter(const struct i2c_adapter *adapter)
 {
Signature · 1 line
2.47.3
#2 lore raw

PATCH v2 2/9 i2c: designware-common: use i2c_adapter_set_node()

In reply to Bartosz Golaszewski

Use the dedicated wrapper for setting the fwnode of the i2c_adapter. This allows us to hide the dereferencing of the embedded struct device.

---
 drivers/i2c/busses/i2c-designware-common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i2c/busses/i2c-designware-common.c +1 -1
index 4dc57fd561703bc12a0c40889841571fd2943e70..3c761c4d818c12eb9721893d43a443374b8766d9 100644
@@ -882,7 +882,7 @@ int i2c_dw_probe(struct dw_i2c_dev *dev)
 	unsigned long irq_flags;
 	int ret;
 
-	device_set_node(&dev->adapter.dev, dev_fwnode(dev->dev));
+	i2c_adapter_set_node(adap, dev_fwnode(dev->dev));
 
 	ret = i2c_dw_init_regmap(dev);
 	if (ret)
Signature · 1 line
2.47.3
#3 lore raw

PATCH v2 3/9 i2c: gpio: use i2c_adapter_set_node()

In reply to Bartosz Golaszewski

Use the dedicated wrapper for setting the fwnode of the i2c_adapter. This allows us to hide the dereferencing of the embedded struct device.

---
 drivers/i2c/busses/i2c-gpio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i2c/busses/i2c-gpio.c +1 -1
index f4355b17bfbf15c402e71096ccbb99f5a35bb479..4984736969e1bf2d11620772214cb13ceb411b6a 100644
@@ -426,7 +426,7 @@ static int i2c_gpio_probe(struct platform_device *pdev)
 	adap->algo_data = bit_data;
 	adap->class = I2C_CLASS_HWMON;
 	adap->dev.parent = dev;
-	device_set_node(&adap->dev, fwnode);
+	i2c_adapter_set_node(adap, fwnode);
 
 	adap->nr = pdev->id;
 	ret = i2c_bit_add_numbered_bus(adap);
Signature · 1 line
2.47.3
#4 lore raw

PATCH v2 4/9 i2c: ljca: use i2c_adapter_set_node()

In reply to Bartosz Golaszewski

Use the dedicated wrapper for setting the fwnode of the i2c_adapter. This allows us to hide the dereferencing of the embedded struct device.

---
 drivers/i2c/busses/i2c-ljca.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i2c/busses/i2c-ljca.c +1 -1
index 93274f0c2d72166bf017f65af797f418743b6a57..fb45019b1a0c583f19bce3523983a79d632da3da 100644
@@ -294,7 +294,7 @@ static int ljca_i2c_probe(struct auxiliary_device *auxdev,
 		 dev_name(&auxdev->dev), dev_name(auxdev->dev.parent),
 		 ljca_i2c->i2c_info->id);
 
-	device_set_node(&ljca_i2c->adap.dev, dev_fwnode(&auxdev->dev));
+	i2c_adapter_set_node(&ljca_i2c->adap, dev_fwnode(&auxdev->dev));
 
 	i2c_set_adapdata(&ljca_i2c->adap, ljca_i2c);
 	auxiliary_set_drvdata(auxdev, ljca_i2c);
Signature · 1 line
2.47.3
#5 lore raw

PATCH v2 5/9 i2c: ls2x: use i2c_adapter_set_node()

In reply to Bartosz Golaszewski

Use the dedicated wrapper for setting the fwnode of the i2c_adapter. This allows us to hide the dereferencing of the embedded struct device.

---
 drivers/i2c/busses/i2c-ls2x.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i2c/busses/i2c-ls2x.c +1 -1
index b475dd27b7af94a22537f1000e95606774d6c7f2..217a55d7d90b2e515ef33e9014263958d9258b00 100644
@@ -312,7 +312,7 @@ static int ls2x_i2c_probe(struct platform_device *pdev)
 	adap->owner = THIS_MODULE;
 	adap->algo = &ls2x_i2c_algo;
 	adap->timeout = msecs_to_jiffies(100);
-	device_set_node(&adap->dev, dev_fwnode(dev));
+	i2c_adapter_set_node(adap, dev_fwnode(dev));
 	i2c_set_adapdata(adap, priv);
 	strscpy(adap->name, pdev->name, sizeof(adap->name));
 	init_completion(&priv->cmd_complete);
Signature · 1 line
2.47.3
#6 lore raw

PATCH v2 6/9 i2c: rzv2m: use i2c_adapter_set_node()

In reply to Bartosz Golaszewski

Use the dedicated wrapper for setting the fwnode of the i2c_adapter. This allows us to hide the dereferencing of the embedded struct device.

---
 drivers/i2c/busses/i2c-rzv2m.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i2c/busses/i2c-rzv2m.c +1 -1
index 2387148506736a65378809971bfbddfb436b6c4d..f31d5b90802477a3d27557a4a92ca2b26023e343 100644
@@ -451,7 +451,7 @@ static int rzv2m_i2c_probe(struct platform_device *pdev)
 	adap->quirks = &rzv2m_i2c_quirks;
 	adap->dev.parent = dev;
 	adap->owner = THIS_MODULE;
-	device_set_node(&adap->dev, dev_fwnode(dev));
+	i2c_adapter_set_node(adap, dev_fwnode(dev));
 	i2c_set_adapdata(adap, priv);
 	strscpy(adap->name, pdev->name, sizeof(adap->name));
 	init_completion(&priv->msg_tia_done);
Signature · 1 line
2.47.3
#7 lore raw

PATCH v2 7/9 i2c: usbio: use i2c_adapter_set_node()

In reply to Bartosz Golaszewski

Use the dedicated wrapper for setting the fwnode of the i2c_adapter. This allows us to hide the dereferencing of the embedded struct device.

---
 drivers/i2c/busses/i2c-usbio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i2c/busses/i2c-usbio.c +1 -1
index e7799abf67877906c7787235bddb28ac23168077..4824d61fc85d7b23fd7f6a8c2be5809ad9fe2613 100644
@@ -278,7 +278,7 @@ static int usbio_i2c_probe(struct auxiliary_device *adev,
 	snprintf(i2c->adap.name, sizeof(i2c->adap.name), "%s.%d",
 		 USBIO_I2C_CLIENT, i2c->adev->id);
 
-	device_set_node(&i2c->adap.dev, dev_fwnode(&adev->dev));
+	i2c_adapter_set_node(&i2c->adap, dev_fwnode(&adev->dev));
 
 	auxiliary_set_drvdata(adev, i2c);
 	i2c_set_adapdata(&i2c->adap, i2c);
Signature · 1 line
2.47.3
#8 lore raw

PATCH v2 8/9 i2c: designware-amdisp: use i2c_adapter_set_node()

In reply to Bartosz Golaszewski

Use the dedicated wrapper for setting the fwnode of the i2c_adapter. This allows us to hide the dereferencing of the embedded struct device.

---
 drivers/i2c/busses/i2c-designware-amdisp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i2c/busses/i2c-designware-amdisp.c +1 -1
index c48728ad9f6f25174515defe55603d67b2491b72..7cbfd441bb16ec1045a2546d55af3cce774b462a 100644
@@ -61,7 +61,7 @@ static int amd_isp_dw_i2c_plat_probe(struct platform_device *pdev)
 	adap = &isp_i2c_dev->adapter;
 	adap->owner = THIS_MODULE;
 	scnprintf(adap->name, sizeof(adap->name), AMDISP_I2C_ADAP_NAME);
-	ACPI_COMPANION_SET(&adap->dev, ACPI_COMPANION(&pdev->dev));
+	i2c_adapter_set_node(adap, dev_fwnode(&pdev->dev));
 	adap->dev.of_node = pdev->dev.of_node;
 	/* use dynamically allocated adapter id */
 	adap->nr = -1;
Signature · 1 line
2.47.3
#9 lore raw

Re: [PATCH v2 8/9] i2c: designware-amdisp: use i2c_adapter_set_node()

Quoted 2 lines · Bartosz Golaszewski

Use the dedicated wrapper for setting the fwnode of the i2c_adapter. This allows us to hide the dereferencing of the embedded struct device.

This should be united with patch 2.
For this kind of patches I would not make the split like this.

But if Andi is okay to take as is, I won't object (this is minor thing).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#10 lore raw

Re: [PATCH v2 8/9] i2c: designware-amdisp: use i2c_adapter_set_node()

Quoted 10 lines · Andy Shevchenko
Quoted 2 lines · Bartosz Golaszewski

Use the dedicated wrapper for setting the fwnode of the i2c_adapter. This allows us to hide the dereferencing of the embedded struct device.

This should be united with patch 2.
For this kind of patches I would not make the split like this.

But if Andi is okay to take as is, I won't object (this is minor thing).

Gah, I folded this into this series but forgot to squash it into the original designware patch. It can be squashed in-tree by Wolfram or I can resend it.

Bart

maintainer
#11 lore raw

Re: [PATCH v2 8/9] i2c: designware-amdisp: use i2c_adapter_set_node()

Quoted 4 lines

Gah, I folded this into this series but forgot to squash it into the original designware patch. It can be squashed in-tree by Wolfram or I can resend it.

Everything got squashed anyhow ;)

#12 lore raw

PATCH v2 9/9 i2c: viai2c-zhaoxin: use i2c_adapter_set_node()

In reply to Bartosz Golaszewski

Use the dedicated wrapper for setting the fwnode of the i2c_adapter. This allows us to hide the dereferencing of the embedded struct device.

---
 drivers/i2c/busses/i2c-viai2c-zhaoxin.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i2c/busses/i2c-viai2c-zhaoxin.c +1 -1
index 95dc64902b7cdb01beea04acf7bad8f20133d965..87ba7367473d117f8cb7d4bd422ae49f0bfb6881 100644
@@ -323,7 +323,7 @@ static int zxi2c_probe(struct platform_device *pdev)
 	adap->algo = &zxi2c_algorithm;
 	adap->quirks = &zxi2c_quirks;
 	adap->dev.parent = &pdev->dev;
-	ACPI_COMPANION_SET(&adap->dev, ACPI_COMPANION(&pdev->dev));
+	i2c_adapter_set_node(adap, dev_fwnode(&pdev->dev));
 	snprintf(adap->name, sizeof(adap->name), "zhaoxin-%s-%s",
 		 dev_name(pdev->dev.parent), dev_name(i2c->dev));
 	i2c_set_adapdata(adap, i2c);
Signature · 1 line
2.47.3
maintainer Applied
#13 lore raw
In reply to Bartosz Golaszewski
Quoted 15 lines · Bartosz Golaszewski

It's been another year of discussing the object life-time problems at conferences. I2C is one of the offenders and its problems are more complex than those of some other subsystems. It seems the revocable[1] API may make its way into the kernel this year but even with it in place, I2C won't be able to use it as there's currently nothing to *revoke*. The struct device is embedded within the i2c_adapter struct whose lifetime is tied to the provider device being bound to its driver.

Fixing this won't be fast and easy but nothing's going to happen if we don't start chipping away at it. The ultimate goal in order to be able to use an SRCU-based solution (revocable or otherwise) is to convert the embedded struct device in struct i2c_adapter into an __rcu pointer that can be *revoked*. To that end we need to hide all dereferences of adap->dev in drivers.

Quoted 7 lines

I2C drivers use device_set_node() to assign a firmware node directly to the struct device embedded in i2c_adapter. In order to hide the direct
dereferencing: provide a dedicated interface that wraps the call to device_set_node() but takes the adapter as argument instead.

Applied to for-current, squashed...

Quoted 4 lines

---
Changes in v2:
- add patches replacing ACPI_COMPANION_SET() with i2c_adapter_set_node()
- rebased on top of v7.0-rc1

... and resolved another merge conflict :/

But despite this minor hickup, thanks a ton for your work! Looking forward to your follow-up patches.

Happy hacking!