[PATCH] platform/mellanox: Fix i2c adapter reference leak in nvsw_sn2201_i2c_completion_notify()

Wentao Liang posted 1 patch 1 week ago
drivers/platform/mellanox/nvsw-sn2201.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
[PATCH] platform/mellanox: Fix i2c adapter reference leak in nvsw_sn2201_i2c_completion_notify()
Posted by Wentao Liang 1 week ago
nvsw_sn2201_i2c_completion_notify() takes a reference to the main mux
adapter with i2c_get_adapter(), but when the following
nvsw_sn2201_create_static_devices() call fails the adapter field has
already been cleared by the callee, so the i2c_put_adapter() in the
nvsw_sn2201_create_static_devices_fail path is a no-op and the
reference is leaked.

Keep the adapter in a local variable and release it on that path.

Fixes: 662f24826f95 ("platform/mellanox: Add support for new SN2201 system")
Cc: stable@vger.kernel.org
Signed-off-by: Wentao Liang <vulab@iscas.ac.cn>
---
 drivers/platform/mellanox/nvsw-sn2201.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/drivers/platform/mellanox/nvsw-sn2201.c b/drivers/platform/mellanox/nvsw-sn2201.c
index 92b58ba8f97b..b9d8074a5167 100644
--- a/drivers/platform/mellanox/nvsw-sn2201.c
+++ b/drivers/platform/mellanox/nvsw-sn2201.c
@@ -1172,12 +1172,14 @@ static void nvsw_sn2201_config_exit(struct nvsw_sn2201 *nvsw_sn2201)
 static int nvsw_sn2201_i2c_completion_notify(void *handle, int id)
 {
 	struct nvsw_sn2201 *nvsw_sn2201 = handle;
+	struct i2c_adapter *adapter;
 	void *regmap;
 	int i, err;
 
 	/* Create main mux. */
-	nvsw_sn2201->main_mux_devs->adapter = i2c_get_adapter(nvsw_sn2201->main_mux_devs->nr);
-	if (!nvsw_sn2201->main_mux_devs->adapter) {
+	adapter = i2c_get_adapter(nvsw_sn2201->main_mux_devs->nr);
+	nvsw_sn2201->main_mux_devs->adapter = adapter;
+	if (!adapter) {
 		err = -ENODEV;
 		dev_err(nvsw_sn2201->dev, "Failed to get adapter for bus %d\n",
 			nvsw_sn2201->main_mux_devs->nr);
@@ -1189,6 +1191,12 @@ static int nvsw_sn2201_i2c_completion_notify(void *handle, int id)
 						nvsw_sn2201->main_mux_devs_num);
 	if (err) {
 		dev_err(nvsw_sn2201->dev, "Failed to create main mux devices\n");
+		/*
+		 * nvsw_sn2201_create_static_devices() clears ->adapter on
+		 * failure, so release the reference taken above here.
+		 */
+		i2c_put_adapter(adapter);
+		nvsw_sn2201->main_mux_devs->adapter = NULL;
 		goto nvsw_sn2201_create_static_devices_fail;
 	}
 
-- 
2.34.1