Skip to contentSkip to conversation

Clean up and fix error handling in mdio_mux_init()

PATCH Superseded Saravana Kannan 2 participants 7 messages Aug 17, 2021 – Aug 18, 2021

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Clean up and fix error handling in mdio_mux_init()

This patch series was started due to -EPROBE_DEFER not being handled correctly in mdio_mux_init() and causing issues [1]. While at it, I also did some more error handling fixes and clean ups. The -EPROBE_DEFER fix is the last patch.

Ideally, in the last patch we'd treat any error similar to -EPROBE_DEFER but I'm not sure if it'll break any board/platforms where some child mdiobus never successfully registers. If we treated all errors similar to -EPROBE_DEFER, then none of the child mdiobus will work and that might be a regression. If people are sure this is not a real case, then I can fix up the last patch to always fail the entire mdio-mux init if any of the child mdiobus registration fails.

Cc: Marc Zyngier <[email protected]>
Cc: Neil Armstrong <[email protected]>
Cc: Kevin Hilman <[email protected]>
[1] - https://lore.kernel.org/lkml/CAGETcx95kHrv8wA-O+-JtfH7H9biJEGJtijuPVN0V5dUKUAB3A@mail.gmail.com/#t

v1 -> v2:
- Added Acked-by, Tested-by and Reviewed-by
- Fixing the subject so it goes to "net" tree

Saravana Kannan (3):
net: mdio-mux: Delete unnecessary devm_kfree
net: mdio-mux: Don't ignore memory allocation errors
net: mdio-mux: Handle -EPROBE_DEFER correctly

 drivers/net/mdio/mdio-mux.c | 37 ++++++++++++++++++++++++-------------
 1 file changed, 24 insertions(+), 13 deletions(-)
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
#1 lore raw

PATCH v2 1/3 net: mdio-mux: Delete unnecessary devm_kfree

The whole point of devm_* APIs is that you don't have to undo them if you are returning an error that's going to get propagated out of a probe() function. So delete unnecessary devm_kfree() call in the error return path.

---
 drivers/net/mdio/mdio-mux.c | 1 -
 1 file changed, 1 deletion(-)
drivers/net/mdio/mdio-mux.c +0 -1
index 110e4ee85785..5b37284f54d6 100644
@@ -181,7 +181,6 @@ int mdio_mux_init(struct device *dev,
 	}
 
 	dev_err(dev, "Error: No acceptable child buses found\n");
-	devm_kfree(dev, pb);
 err_pb_kz:
 	put_device(&parent_bus->dev);
 err_parent_bus:
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
maintainer
#2 lore raw

Re: [PATCH net v2 1/3] net: mdio-mux: Delete unnecessary devm_kfree

Quoted 10 lines · Saravana Kannan

The whole point of devm_* APIs is that you don't have to undo them if you are returning an error that's going to get propagated out of a probe() function. So delete unnecessary devm_kfree() call in the error return path.

Please add a Fixes: tag, since you want this in stable.

All three patches need fixes tags, possibly different for each patch?

Andrew

#3 lore raw

Re: [PATCH net v2 1/3] net: mdio-mux: Delete unnecessary devm_kfree

Quoted 13 lines · Andrew Lunn
Quoted 10 lines · Saravana Kannan

The whole point of devm_* APIs is that you don't have to undo them if you are returning an error that's going to get propagated out of a probe() function. So delete unnecessary devm_kfree() call in the error return path.

Quoted 3 lines

Please add a Fixes: tag, since you want this in stable.

All three patches need fixes tags, possibly different for each patch?

I generally ask for patches to be picked up by stable only if it fixes a bug that puts the kernel in a bad state or if it fixes an issue someone actually reported on the stable kernel. In this case, it's just failing device probes in some cases and I didn't think that met the bar for stable. But if you think they should, then that's fine by me.

I'll send out v3 patches with Fixes. I'm fairly sure these issues were present since the time mdio-mux was added. Hopefully v3 will be the last version I have to send out :)

-Saravana

maintainer
#4 lore raw

Re: [PATCH net v2 1/3] net: mdio-mux: Delete unnecessary devm_kfree

Quoted 14 lines · Saravana Kannan
Quoted 13 lines · Andrew Lunn
Quoted 10 lines · Saravana Kannan

The whole point of devm_* APIs is that you don't have to undo them if you are returning an error that's going to get propagated out of a probe() function. So delete unnecessary devm_kfree() call in the error return path.

Quoted 6 lines
Quoted 3 lines

Please add a Fixes: tag, since you want this in stable.

All three patches need fixes tags, possibly different for each patch?

I generally ask for patches to be picked up by stable only if it fixes a bug that puts the kernel in a bad state

Actually, you asked for stable. You set the subject to

[PATCH net v3 0/3] Clean up and fix error

where [PATCH net ] means stable. If you think this is just ongoing development work, use [PATCH net-next ]. Then the Fixes tags are optional.

Andrew

#5 lore raw

PATCH v2 2/3 net: mdio-mux: Don't ignore memory allocation errors

In reply to Saravana Kannan

If we are seeing memory allocation errors, don't try to continue registering child mdiobus devices. It's unlikely they'll succeed.

---
 drivers/net/mdio/mdio-mux.c | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)
drivers/net/mdio/mdio-mux.c +18 -10
index 5b37284f54d6..13035e2685c4 100644
@@ -82,6 +82,17 @@ static int mdio_mux_write(struct mii_bus *bus, int phy_id,
 
 static int parent_count;
 
+static void mdio_mux_uninit_children(struct mdio_mux_parent_bus *pb)
+{
+	struct mdio_mux_child_bus *cb = pb->children;
+
+	while (cb) {
+		mdiobus_unregister(cb->mii_bus);
+		mdiobus_free(cb->mii_bus);
+		cb = cb->next;
+	}
+}
+
 int mdio_mux_init(struct device *dev,
 		  struct device_node *mux_node,
 		  int (*switch_fn)(int cur, int desired, void *data),
@@ -144,7 +155,7 @@ int mdio_mux_init(struct device *dev,
 		cb = devm_kzalloc(dev, sizeof(*cb), GFP_KERNEL);
 		if (!cb) {
 			ret_val = -ENOMEM;
-			continue;
+			goto err_loop;
 		}
 		cb->bus_number = v;
 		cb->parent = pb;
@@ -152,8 +163,7 @@ int mdio_mux_init(struct device *dev,
 		cb->mii_bus = mdiobus_alloc();
 		if (!cb->mii_bus) {
 			ret_val = -ENOMEM;
-			devm_kfree(dev, cb);
-			continue;
+			goto err_loop;
 		}
 		cb->mii_bus->priv = cb;
 
@@ -181,6 +191,10 @@ int mdio_mux_init(struct device *dev,
 	}
 
 	dev_err(dev, "Error: No acceptable child buses found\n");
+
+err_loop:
+	mdio_mux_uninit_children(pb);
+	of_node_put(child_bus_node);
 err_pb_kz:
 	put_device(&parent_bus->dev);
 err_parent_bus:
@@ -192,14 +206,8 @@ EXPORT_SYMBOL_GPL(mdio_mux_init);
 void mdio_mux_uninit(void *mux_handle)
 {
 	struct mdio_mux_parent_bus *pb = mux_handle;
-	struct mdio_mux_child_bus *cb = pb->children;
-
-	while (cb) {
-		mdiobus_unregister(cb->mii_bus);
-		mdiobus_free(cb->mii_bus);
-		cb = cb->next;
-	}
 
+	mdio_mux_uninit_children(pb);
 	put_device(&pb->mii_bus->dev);
 }
 EXPORT_SYMBOL_GPL(mdio_mux_uninit);
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
#6 lore raw

PATCH v2 3/3 net: mdio-mux: Handle -EPROBE_DEFER correctly

In reply to Saravana Kannan

When registering mdiobus children, if we get an -EPROBE_DEFER, we shouldn't ignore it and continue registering the rest of the mdiobus children. This would permanently prevent the deferring child mdiobus from working instead of reattempting it in the future. So, if a child mdiobus needs to be reattempted in the future, defer the entire mdio-mux initialization.

This fixes the issue where PHYs sitting under the mdio-mux aren't initialized correctly if the PHY's interrupt controller is not yet ready when the mdio-mux is being probed. Additional context in the link below.

---
 drivers/net/mdio/mdio-mux.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/net/mdio/mdio-mux.c +6 -2
index 13035e2685c4..ebd001f0eece 100644
@@ -175,11 +175,15 @@ int mdio_mux_init(struct device *dev,
 		cb->mii_bus->write = mdio_mux_write;
 		r = of_mdiobus_register(cb->mii_bus, child_bus_node);
 		if (r) {
+			mdiobus_free(cb->mii_bus);
+			if (r == -EPROBE_DEFER) {
+				ret_val = r;
+				goto err_loop;
+			}
+			devm_kfree(dev, cb);
 			dev_err(dev,
 				"Error: Failed to register MDIO bus for child %pOF\n",
 				child_bus_node);
-			mdiobus_free(cb->mii_bus);
-			devm_kfree(dev, cb);
 		} else {
 			cb->next = pb->children;
 			pb->children = cb;
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog