]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
net: pse-pd: pd692x0: Fix power budget leak in manager setup error path
authorKory Maincent <kory.maincent@bootlin.com>
Wed, 20 Aug 2025 13:27:07 +0000 (15:27 +0200)
committerJakub Kicinski <kuba@kernel.org>
Thu, 21 Aug 2025 14:55:40 +0000 (07:55 -0700)
Fix a resource leak where manager power budgets were freed on both
success and error paths during manager setup. Power budgets should
only be freed on error paths after regulator registration or during
driver removal.

Refactor cleanup logic by extracting OF node cleanup and power budget
freeing into separate helper functions for better maintainability.

Fixes: 359754013e6a ("net: pse-pd: pd692x0: Add support for PSE PI priority feature")
Signed-off-by: Kory Maincent <kory.maincent@bootlin.com>
Link: https://patch.msgid.link/20250820132708.837255-1-kory.maincent@bootlin.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
drivers/net/pse-pd/pd692x0.c

index 399ce9febda49321c6445d3bf4293261de2fcfb4..395f6c662175c07b51ce75b48041869e5ed5b6f5 100644 (file)
@@ -1162,12 +1162,44 @@ pd692x0_write_ports_matrix(struct pd692x0_priv *priv,
        return 0;
 }
 
+static void pd692x0_of_put_managers(struct pd692x0_priv *priv,
+                                   struct pd692x0_manager *manager,
+                                   int nmanagers)
+{
+       int i, j;
+
+       for (i = 0; i < nmanagers; i++) {
+               for (j = 0; j < manager[i].nports; j++)
+                       of_node_put(manager[i].port_node[j]);
+               of_node_put(manager[i].node);
+       }
+}
+
+static void pd692x0_managers_free_pw_budget(struct pd692x0_priv *priv)
+{
+       int i;
+
+       for (i = 0; i < PD692X0_MAX_MANAGERS; i++) {
+               struct regulator *supply;
+
+               if (!priv->manager_reg[i] || !priv->manager_pw_budget[i])
+                       continue;
+
+               supply = priv->manager_reg[i]->supply;
+               if (!supply)
+                       continue;
+
+               regulator_free_power_budget(supply,
+                                           priv->manager_pw_budget[i]);
+       }
+}
+
 static int pd692x0_setup_pi_matrix(struct pse_controller_dev *pcdev)
 {
        struct pd692x0_manager *manager __free(kfree) = NULL;
        struct pd692x0_priv *priv = to_pd692x0_priv(pcdev);
        struct pd692x0_matrix port_matrix[PD692X0_MAX_PIS];
-       int ret, i, j, nmanagers;
+       int ret, nmanagers;
 
        /* Should we flash the port matrix */
        if (priv->fw_state != PD692X0_FW_OK &&
@@ -1185,31 +1217,27 @@ static int pd692x0_setup_pi_matrix(struct pse_controller_dev *pcdev)
        nmanagers = ret;
        ret = pd692x0_register_managers_regulator(priv, manager, nmanagers);
        if (ret)
-               goto out;
+               goto err_of_managers;
 
        ret = pd692x0_configure_managers(priv, nmanagers);
        if (ret)
-               goto out;
+               goto err_of_managers;
 
        ret = pd692x0_set_ports_matrix(priv, manager, nmanagers, port_matrix);
        if (ret)
-               goto out;
+               goto err_managers_req_pw;
 
        ret = pd692x0_write_ports_matrix(priv, port_matrix);
        if (ret)
-               goto out;
+               goto err_managers_req_pw;
 
-out:
-       for (i = 0; i < nmanagers; i++) {
-               struct regulator *supply = priv->manager_reg[i]->supply;
-
-               regulator_free_power_budget(supply,
-                                           priv->manager_pw_budget[i]);
+       pd692x0_of_put_managers(priv, manager, nmanagers);
+       return 0;
 
-               for (j = 0; j < manager[i].nports; j++)
-                       of_node_put(manager[i].port_node[j]);
-               of_node_put(manager[i].node);
-       }
+err_managers_req_pw:
+       pd692x0_managers_free_pw_budget(priv);
+err_of_managers:
+       pd692x0_of_put_managers(priv, manager, nmanagers);
        return ret;
 }
 
@@ -1748,6 +1776,7 @@ static void pd692x0_i2c_remove(struct i2c_client *client)
 {
        struct pd692x0_priv *priv = i2c_get_clientdata(client);
 
+       pd692x0_managers_free_pw_budget(priv);
        firmware_upload_unregister(priv->fwl);
 }