public inbox for linux-input@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Input: edt-ft5x06 - fix regmap leak when probe fails
@ 2024-10-19  0:17 Dmitry Torokhov
  2024-10-23 11:46 ` Oliver Graute
  0 siblings, 1 reply; 2+ messages in thread
From: Dmitry Torokhov @ 2024-10-19  0:17 UTC (permalink / raw)
  To: linux-input
  Cc: Oliver Graute, Uwe Kleine-König, Felix Kaechele,
	Andreas Kemnade, Li Zetao, linux-kernel

The driver neglects to free the instance of I2C regmap constructed at
the beginning of the edt_ft5x06_ts_probe() method when probe fails.
Additionally edt_ft5x06_ts_remove() is freeing the regmap too early,
before the rest of the device resources that are managed by devm are
released.

Fix this by installing a custom devm action that will ensure that the
regmap is released at the right time during normal teardown as well as
in case of probe failure.

Note that devm_regmap_init_i2c() could not be used because the driver
may replace the original regmap with a regmap specific for M06 devices
in the middle of the probe, and using devm_regmap_init_i2c() would
result in releasing the M06 regmap too early.

Reported-by: Li Zetao <lizetao1@huawei.com>
Fixes: 9dfd9708ffba ("Input: edt-ft5x06 - convert to use regmap API")
Cc: stable@vger.kernel.org
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
 drivers/input/touchscreen/edt-ft5x06.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/drivers/input/touchscreen/edt-ft5x06.c b/drivers/input/touchscreen/edt-ft5x06.c
index ed9b71c11f71..2f4d66957969 100644
--- a/drivers/input/touchscreen/edt-ft5x06.c
+++ b/drivers/input/touchscreen/edt-ft5x06.c
@@ -1096,6 +1096,14 @@ static void edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data *tsdata)
 	}
 }
 
+static void edt_ft5x06_exit_regmap(void *arg)
+{
+	struct edt_ft5x06_ts_data *data = arg;
+
+	if (!IS_ERR_OR_NULL(data->regmap))
+		regmap_exit(data->regmap);
+}
+
 static void edt_ft5x06_disable_regulators(void *arg)
 {
 	struct edt_ft5x06_ts_data *data = arg;
@@ -1129,6 +1137,16 @@ static int edt_ft5x06_ts_probe(struct i2c_client *client)
 		return PTR_ERR(tsdata->regmap);
 	}
 
+	/*
+	 * We are not using devm_regmap_init_i2c() and instead install a
+	 * custom action because we may replace regmap with M06-specific one
+	 * and we need to make sure that it will not be released too early.
+	 */
+	error = devm_add_action_or_reset(&client->dev, edt_ft5x06_exit_regmap,
+					 tsdata);
+	if (error)
+		return error;
+
 	chip_data = device_get_match_data(&client->dev);
 	if (!chip_data)
 		chip_data = (const struct edt_i2c_chip_data *)id->driver_data;
@@ -1322,7 +1340,6 @@ static void edt_ft5x06_ts_remove(struct i2c_client *client)
 	struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
 
 	edt_ft5x06_ts_teardown_debugfs(tsdata);
-	regmap_exit(tsdata->regmap);
 }
 
 static int edt_ft5x06_ts_suspend(struct device *dev)
-- 
2.47.0.rc1.288.g06298d1525-goog


-- 
Dmitry

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] Input: edt-ft5x06 - fix regmap leak when probe fails
  2024-10-19  0:17 [PATCH] Input: edt-ft5x06 - fix regmap leak when probe fails Dmitry Torokhov
@ 2024-10-23 11:46 ` Oliver Graute
  0 siblings, 0 replies; 2+ messages in thread
From: Oliver Graute @ 2024-10-23 11:46 UTC (permalink / raw)
  To: Dmitry Torokhov
  Cc: linux-input, Uwe Kleine-König, Felix Kaechele,
	Andreas Kemnade, Li Zetao, linux-kernel

On 18/10/24, Dmitry Torokhov wrote:
> The driver neglects to free the instance of I2C regmap constructed at
> the beginning of the edt_ft5x06_ts_probe() method when probe fails.
> Additionally edt_ft5x06_ts_remove() is freeing the regmap too early,
> before the rest of the device resources that are managed by devm are
> released.
> 
> Fix this by installing a custom devm action that will ensure that the
> regmap is released at the right time during normal teardown as well as
> in case of probe failure.
> 
> Note that devm_regmap_init_i2c() could not be used because the driver
> may replace the original regmap with a regmap specific for M06 devices
> in the middle of the probe, and using devm_regmap_init_i2c() would
> result in releasing the M06 regmap too early.
> 
> Reported-by: Li Zetao <lizetao1@huawei.com>
> Fixes: 9dfd9708ffba ("Input: edt-ft5x06 - convert to use regmap API")
> Cc: stable@vger.kernel.org
> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

Reviewed-by: Oliver Graute <oliver.graute@kococonnector.com>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-10-23 11:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-19  0:17 [PATCH] Input: edt-ft5x06 - fix regmap leak when probe fails Dmitry Torokhov
2024-10-23 11:46 ` Oliver Graute

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox