Skip to contentSkip to conversation

PCI/pwrctrl: tc9563: take i2c adapter module reference

PATCH Applied Johan Hovold 5 participants 8 messages Jul 16 – Jul 20

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

PATCH 1/1 PCI/pwrctrl: tc9563: take i2c adapter module reference

The i2c subsystem currently blocks during adapter deregistration whenever there are consumers holding a reference.

Switch to using of_get_i2c_adapter_by_node() which also takes a reference to the adapter module so that an attempt to unload the module while in use fails gracefully instead of blocking uninterruptibly.

---
 drivers/pci/pwrctrl/pci-pwrctrl-tc9563.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/pci/pwrctrl/pci-pwrctrl-tc9563.c +4 -4
index 1555e8a9b3ca..e6a8abd9da8e 100644
@@ -548,7 +548,7 @@ static int tc9563_pwrctrl_probe(struct platform_device *pdev)
 		return dev_err_probe(dev, ret, "Failed to read i2c-parent property\n");
 
 	i2c_node = of_parse_phandle(dev->of_node, "i2c-parent", 0);
-	tc9563->adapter = of_find_i2c_adapter_by_node(i2c_node);
+	tc9563->adapter = of_get_i2c_adapter_by_node(i2c_node);
 	of_node_put(i2c_node);
 	if (!tc9563->adapter)
 		return dev_err_probe(dev, -EPROBE_DEFER, "Failed to find I2C adapter\n");
@@ -556,7 +556,7 @@ static int tc9563_pwrctrl_probe(struct platform_device *pdev)
 	tc9563->client = i2c_new_dummy_device(tc9563->adapter, addr);
 	if (IS_ERR(tc9563->client)) {
 		dev_err(dev, "Failed to create I2C client\n");
-		put_device(&tc9563->adapter->dev);
+		i2c_put_adapter(tc9563->adapter);
 		return PTR_ERR(tc9563->client);
 	}
 
@@ -624,7 +624,7 @@ static int tc9563_pwrctrl_probe(struct platform_device *pdev)
 	tc9563_pwrctrl_power_off(&tc9563->pwrctrl);
 remove_i2c:
 	i2c_unregister_device(tc9563->client);
-	put_device(&tc9563->adapter->dev);
+	i2c_put_adapter(tc9563->adapter);
 	return ret;
 }
 
@@ -636,7 +636,7 @@ static void tc9563_pwrctrl_remove(struct platform_device *pdev)
 
 	tc9563_pwrctrl_power_off(&tc9563->pwrctrl);
 	i2c_unregister_device(tc9563->client);
-	put_device(&tc9563->adapter->dev);
+	i2c_put_adapter(tc9563->adapter);
 }
 
 static const struct of_device_id tc9563_pwrctrl_of_match[] = {
Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Pre-existing issues:
- [High] Missing bounds check on port index allows out-of-bounds array access on malformed device tree.
- [High] Missing device link to I2C adapter leads to Use-After-Free if the adapter is unbound.
- [Medium] Unconditional calls to tc9563_pwrctrl_power_off() cause regulator usage count underflows. --

--- Patch [1]: [PATCH] PCI/pwrctrl: tc9563: take i2c adapter module reference --- commit ef55bbdfd2a346b4d8f7147c1d7a0c1ab090cb03
Author: Johan Hovold <[email protected]>
Subject: PCI/pwrctrl: tc9563: take i2c adapter module reference

This commit switches to using of_get_i2c_adapter_by_node() to take a module reference to the I2C adapter. This prevents the I2C subsystem from blocking uninterruptibly during adapter deregistration when consumers are holding a reference, allowing module unloads to fail gracefully instead.

Quoted 14 lines
drivers/pci/pwrctrl/pci-pwrctrl-tc9563.c +1 -1
index 488e1ec34a7fb..83ae9f256b69e 100644
@@ -549,7 +549,7 @@ static int tc9563_pwrctrl_probe(struct platform_device *pdev)
 		return dev_err_probe(dev, ret, "Failed to read i2c-parent property\n");
 
 	i2c_node = of_parse_phandle(dev->of_node, "i2c-parent", 0);
-	tc9563->adapter = of_find_i2c_adapter_by_node(i2c_node);
+	tc9563->adapter = of_get_i2c_adapter_by_node(i2c_node);
 	of_node_put(i2c_node);
 	if (!tc9563->adapter)
 		return dev_err_probe(dev, -EPROBE_DEFER, "Failed to find I2C adapter\n");

drivers/pci/pwrctrl/pci-pwrctrl-tc9563.c static int tc9563_pwrctrl_probe(struct platform_device *pdev) · line 557

Quoted 1 line
drivers/pci/pwrctrl/pci-pwrctrl-tc9563.c +0 -0
@@ -557,7 +557,7 @@ static int tc9563_pwrctrl_probe(struct platform_device *pdev)
 	tc9563->client = i2c_new_dummy_device(tc9563->adapter, addr);

[Severity: High]
This is a pre-existing issue, but does this code leave the platform device vulnerable to a use-after-free?

If a privileged user unbinds the I2C adapter via sysfs, the I2C core unregisters and frees all child clients, including this dummy client. Since no device link is created (such as with DL_FLAG_AUTOREMOVE_CONSUMER), the platform device remains bound and unaware, holding a dangling pointer to the freed client.

Could subsequent operations like device removal or PCI power callbacks dereference this freed client?

[Severity: High]
This is also a pre-existing issue, but could a malformed device tree cause an out-of-bounds access on the configuration array?

Looking further down in tc9563_pwrctrl_probe(), the port variable is incremented for each child node without bounds checking:

	for_each_child_of_node_scoped(node, child) {
		port++;
		ret = tc9563_pwrctrl_parse_device_dt(tc9563, child, port);
		...
	}

If the device tree provides more child nodes than expected, does the port variable increment beyond TC9563_MAX?

When passed to tc9563_pwrctrl_parse_device_dt(), this unvalidated index is directly used to access the statically sized array:

	struct tc9563_pwrctrl_cfg *cfg = &tc9563->cfg[port];

Could this lead to memory corruption if the loop exceeds the array bounds?

Quoted 6 lines
drivers/pci/pwrctrl/pci-pwrctrl-tc9563.c +1 -1
 	if (IS_ERR(tc9563->client)) {
 		dev_err(dev, "Failed to create I2C client\n");
-		put_device(&tc9563->adapter->dev);
+		i2c_put_adapter(tc9563->adapter);
 		return PTR_ERR(tc9563->client);
 	}

[ ... ]

drivers/pci/pwrctrl/pci-pwrctrl-tc9563.c static int tc9563_pwrctrl_probe(struct platform_device *pdev) · line 625

Quoted 2 lines
drivers/pci/pwrctrl/pci-pwrctrl-tc9563.c +0 -0
@@ -625,7 +625,7 @@ static int tc9563_pwrctrl_probe(struct platform_device *pdev)
 	tc9563_pwrctrl_power_off(&tc9563->pwrctrl);

[Severity: Medium]
This is a pre-existing issue, but could this unconditional call to power off cause a regulator usage count underflow?

Because the regulators are only enabled lazily when the PCI core invokes the .power_on callback, they may not have been enabled if devm_pci_pwrctrl_device_set_ready() fails during probe.

drivers/pci/pwrctrl/pci-pwrctrl-tc9563.c static void tc9563_pwrctrl_remove(struct platform_device *pdev) · line 638

Quoted 10 lines
drivers/pci/pwrctrl/pci-pwrctrl-tc9563.c +1 -1
 remove_i2c:
 	i2c_unregister_device(tc9563->client);
-	put_device(&tc9563->adapter->dev);
+	i2c_put_adapter(tc9563->adapter);
 	return ret;
 }
 
@@ -637,7 +637,7 @@ static void tc9563_pwrctrl_remove(struct platform_device *pdev)
 
 	tc9563_pwrctrl_power_off(&tc9563->pwrctrl);

[Severity: Medium]
Similarly, this is a pre-existing issue, but if the device is removed before being explicitly powered on by the PCI core, will this unconditional power off also trigger a regulator usage count underflow during removal?

Quoted 6 lines
drivers/pci/pwrctrl/pci-pwrctrl-tc9563.c +1 -1
 	i2c_unregister_device(tc9563->client);
-	put_device(&tc9563->adapter->dev);
+	i2c_put_adapter(tc9563->adapter);
 }
 
 static const struct of_device_id tc9563_pwrctrl_of_match[] = {
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Johan Hovold
Quoted 8 lines · Johan Hovold

The i2c subsystem currently blocks during adapter deregistration whenever there are consumers holding a reference.

Switch to using of_get_i2c_adapter_by_node() which also takes a reference to the adapter module so that an attempt to unload the module while in use fails gracefully instead of blocking uninterruptibly.

- Mani

Quoted 47 lines
---
 drivers/pci/pwrctrl/pci-pwrctrl-tc9563.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/pci/pwrctrl/pci-pwrctrl-tc9563.c +4 -4
index 1555e8a9b3ca..e6a8abd9da8e 100644
@@ -548,7 +548,7 @@ static int tc9563_pwrctrl_probe(struct platform_device *pdev)
 		return dev_err_probe(dev, ret, "Failed to read i2c-parent property\n");
 
 	i2c_node = of_parse_phandle(dev->of_node, "i2c-parent", 0);
-	tc9563->adapter = of_find_i2c_adapter_by_node(i2c_node);
+	tc9563->adapter = of_get_i2c_adapter_by_node(i2c_node);
 	of_node_put(i2c_node);
 	if (!tc9563->adapter)
 		return dev_err_probe(dev, -EPROBE_DEFER, "Failed to find I2C adapter\n");
@@ -556,7 +556,7 @@ static int tc9563_pwrctrl_probe(struct platform_device *pdev)
 	tc9563->client = i2c_new_dummy_device(tc9563->adapter, addr);
 	if (IS_ERR(tc9563->client)) {
 		dev_err(dev, "Failed to create I2C client\n");
-		put_device(&tc9563->adapter->dev);
+		i2c_put_adapter(tc9563->adapter);
 		return PTR_ERR(tc9563->client);
 	}
 
@@ -624,7 +624,7 @@ static int tc9563_pwrctrl_probe(struct platform_device *pdev)
 	tc9563_pwrctrl_power_off(&tc9563->pwrctrl);
 remove_i2c:
 	i2c_unregister_device(tc9563->client);
-	put_device(&tc9563->adapter->dev);
+	i2c_put_adapter(tc9563->adapter);
 	return ret;
 }
 
@@ -636,7 +636,7 @@ static void tc9563_pwrctrl_remove(struct platform_device *pdev)
 
 	tc9563_pwrctrl_power_off(&tc9563->pwrctrl);
 	i2c_unregister_device(tc9563->client);
-	put_device(&tc9563->adapter->dev);
+	i2c_put_adapter(tc9563->adapter);
 }
 
 static const struct of_device_id tc9563_pwrctrl_of_match[] = {
Signature · 1 line
2.54.0
Signature · 1 line
மணிவண்ணன் சதாசிவம்
maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to Johan Hovold
Quoted 9 lines · Johan Hovold

The i2c subsystem currently blocks during adapter deregistration whenever there are consumers holding a reference.

Switch to using of_get_i2c_adapter_by_node() which also takes a reference to the adapter module so that an attempt to unload the module while in use fails gracefully instead of blocking uninterruptibly.

---

#4 lore raw
In reply to Johan Hovold
Quoted 8 lines · Johan Hovold

The i2c subsystem currently blocks during adapter deregistration whenever there are consumers holding a reference.

Switch to using of_get_i2c_adapter_by_node() which also takes a reference to the adapter module so that an attempt to unload the module while in use fails gracefully instead of blocking uninterruptibly.

Applied to pci/pwrctrl for v7.3, thanks!

Gemini suggested minor commit log rewording, let me know if it makes it worse. I do like the impact being mentioned in the subject:

PCI/pwrctrl: tc9563: Avoid uninterruptible hang on in-use I2C module unload

The I2C subsystem blocks during adapter deregistration if there are consumers actively holding a reference.

Switch to of_get_i2c_adapter_by_node(), which also takes a reference to the adapter module, so an attempt to unload the module while in use will fail gracefully instead of blocking uninterruptibly.

Quoted 47 lines
---
 drivers/pci/pwrctrl/pci-pwrctrl-tc9563.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/pci/pwrctrl/pci-pwrctrl-tc9563.c +4 -4
index 1555e8a9b3ca..e6a8abd9da8e 100644
@@ -548,7 +548,7 @@ static int tc9563_pwrctrl_probe(struct platform_device *pdev)
 		return dev_err_probe(dev, ret, "Failed to read i2c-parent property\n");
 
 	i2c_node = of_parse_phandle(dev->of_node, "i2c-parent", 0);
-	tc9563->adapter = of_find_i2c_adapter_by_node(i2c_node);
+	tc9563->adapter = of_get_i2c_adapter_by_node(i2c_node);
 	of_node_put(i2c_node);
 	if (!tc9563->adapter)
 		return dev_err_probe(dev, -EPROBE_DEFER, "Failed to find I2C adapter\n");
@@ -556,7 +556,7 @@ static int tc9563_pwrctrl_probe(struct platform_device *pdev)
 	tc9563->client = i2c_new_dummy_device(tc9563->adapter, addr);
 	if (IS_ERR(tc9563->client)) {
 		dev_err(dev, "Failed to create I2C client\n");
-		put_device(&tc9563->adapter->dev);
+		i2c_put_adapter(tc9563->adapter);
 		return PTR_ERR(tc9563->client);
 	}
 
@@ -624,7 +624,7 @@ static int tc9563_pwrctrl_probe(struct platform_device *pdev)
 	tc9563_pwrctrl_power_off(&tc9563->pwrctrl);
 remove_i2c:
 	i2c_unregister_device(tc9563->client);
-	put_device(&tc9563->adapter->dev);
+	i2c_put_adapter(tc9563->adapter);
 	return ret;
 }
 
@@ -636,7 +636,7 @@ static void tc9563_pwrctrl_remove(struct platform_device *pdev)
 
 	tc9563_pwrctrl_power_off(&tc9563->pwrctrl);
 	i2c_unregister_device(tc9563->client);
-	put_device(&tc9563->adapter->dev);
+	i2c_put_adapter(tc9563->adapter);
 }
 
 static const struct of_device_id tc9563_pwrctrl_of_match[] = {
Signature · 1 line
2.54.0
#5 lore raw
Quoted 15 lines · Bjorn Helgaas
Quoted 8 lines · Johan Hovold

The i2c subsystem currently blocks during adapter deregistration whenever there are consumers holding a reference.

Switch to using of_get_i2c_adapter_by_node() which also takes a reference to the adapter module so that an attempt to unload the module while in use fails gracefully instead of blocking uninterruptibly.

Applied to pci/pwrctrl for v7.3, thanks!

Gemini suggested minor commit log rewording, let me know if it makes it worse. I do like the impact being mentioned in the subject:

Quoted 8 lines

PCI/pwrctrl: tc9563: Avoid uninterruptible hang on in-use I2C module unload

The I2C subsystem blocks during adapter deregistration if there are consumers actively holding a reference.

Switch to of_get_i2c_adapter_by_node(), which also takes a reference to the adapter module, so an attempt to unload the module while in use will fail gracefully instead of blocking uninterruptibly.

No, please don't rewrite my commit messages (and especially not without clearly documenting that you've done).

If you have any concerns about the wording, I can try to accommodate those in a v2.

If I had considered this to be a fix (the line is admittedly blurry) I would also have tried to be more explicit about the impact in the summary. But here I think the existing summary is sufficient.

Note that I've sent similar changes for other subsystems and there is some value in using the same patch summary (modulo subsystem preferences for capitalisation, etc).

Johan

#6 lore raw
Quoted 31 lines · Johan Hovold
Quoted 15 lines · Bjorn Helgaas
Quoted 8 lines · Johan Hovold

The i2c subsystem currently blocks during adapter deregistration whenever there are consumers holding a reference.

Switch to using of_get_i2c_adapter_by_node() which also takes a reference to the adapter module so that an attempt to unload the module while in use fails gracefully instead of blocking uninterruptibly.

Applied to pci/pwrctrl for v7.3, thanks!

Gemini suggested minor commit log rewording, let me know if it makes it worse. I do like the impact being mentioned in the subject:

Quoted 8 lines

PCI/pwrctrl: tc9563: Avoid uninterruptible hang on in-use I2C module unload

The I2C subsystem blocks during adapter deregistration if there are consumers actively holding a reference.

Switch to of_get_i2c_adapter_by_node(), which also takes a reference to the adapter module, so an attempt to unload the module while in use will fail gracefully instead of blocking uninterruptibly.

No, please don't rewrite my commit messages (and especially not without clearly documenting that you've done).

If you have any concerns about the wording, I can try to accommodate those in a v2.

Quoted 7 lines

If I had considered this to be a fix (the line is admittedly blurry) I would also have tried to be more explicit about the impact in the summary. But here I think the existing summary is sufficient.

Note that I've sent similar changes for other subsystems and there is some value in using the same patch summary (modulo subsystem preferences for capitalisation, etc).

You're right, I'm sorry. I used your original commit message. I assumed this was for v7.3, but let me know if it should be for v7.2.

#7 lore raw
Quoted 2 lines · Bjorn Helgaas

You're right, I'm sorry. I used your original commit message. I assumed this was for v7.3, but let me know if it should be for v7.2.

Thanks. And yes, this was intended for 7.3.

Johan