[PATCH 4.14 04/33] Input: stmfts - fix reference leak in stmfts_input_open

Greg Kroah-Hartman posted 33 patches 3 years, 8 months ago
Only 32 patches received!
[PATCH 4.14 04/33] Input: stmfts - fix reference leak in stmfts_input_open
Posted by Greg Kroah-Hartman 3 years, 8 months ago
From: Zheng Yongjun <zhengyongjun3@huawei.com>

[ Upstream commit 26623eea0da3476446909af96c980768df07bbd9 ]

pm_runtime_get_sync() will increment pm usage counter even it
failed. Forgetting to call pm_runtime_put_noidle will result
in reference leak in stmfts_input_open, so we should fix it.

Signed-off-by: Zheng Yongjun <zhengyongjun3@huawei.com>
Link: https://lore.kernel.org/r/20220317131604.53538-1-zhengyongjun3@huawei.com
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 drivers/input/touchscreen/stmfts.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/input/touchscreen/stmfts.c b/drivers/input/touchscreen/stmfts.c
index d9e93dabbca2..9007027a7ad9 100644
--- a/drivers/input/touchscreen/stmfts.c
+++ b/drivers/input/touchscreen/stmfts.c
@@ -344,11 +344,11 @@ static int stmfts_input_open(struct input_dev *dev)
 
 	err = pm_runtime_get_sync(&sdata->client->dev);
 	if (err < 0)
-		return err;
+		goto out;
 
 	err = i2c_smbus_write_byte(sdata->client, STMFTS_MS_MT_SENSE_ON);
 	if (err)
-		return err;
+		goto out;
 
 	mutex_lock(&sdata->mutex);
 	sdata->running = true;
@@ -371,7 +371,9 @@ static int stmfts_input_open(struct input_dev *dev)
 				 "failed to enable touchkey\n");
 	}
 
-	return 0;
+out:
+	pm_runtime_put_noidle(&sdata->client->dev);
+	return err;
 }
 
 static void stmfts_input_close(struct input_dev *dev)
-- 
2.35.1
Re: [PATCH 4.14 04/33] Input: stmfts - fix reference leak in stmfts_input_open
Posted by Pavel Machek 3 years, 8 months ago
Hi!

> From: Zheng Yongjun <zhengyongjun3@huawei.com>
> 
> [ Upstream commit 26623eea0da3476446909af96c980768df07bbd9 ]
> 
> pm_runtime_get_sync() will increment pm usage counter even it
> failed. Forgetting to call pm_runtime_put_noidle will result
> in reference leak in stmfts_input_open, so we should fix it.

This is wrong, AFAICT.

>  drivers/input/touchscreen/stmfts.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/input/touchscreen/stmfts.c b/drivers/input/touchscreen/stmfts.c
> index d9e93dabbca2..9007027a7ad9 100644
> --- a/drivers/input/touchscreen/stmfts.c
> +++ b/drivers/input/touchscreen/stmfts.c
> @@ -344,11 +344,11 @@ static int stmfts_input_open(struct input_dev *dev)
>  
>  	err = pm_runtime_get_sync(&sdata->client->dev);
>  	if (err < 0)
> -		return err;
> +		goto out;
>  
>  	err = i2c_smbus_write_byte(sdata->client, STMFTS_MS_MT_SENSE_ON);
>  	if (err)
> -		return err;
> +		goto out;
>  
>  	mutex_lock(&sdata->mutex);
>  	sdata->running = true;
> @@ -371,7 +371,9 @@ static int stmfts_input_open(struct input_dev *dev)
>  				 "failed to enable touchkey\n");
>  	}
>  
> -	return 0;
> +out:
> +	pm_runtime_put_noidle(&sdata->client->dev);
> +	return err;
>  }
>  
>  static void stmfts_input_close(struct input_dev *dev)

We are now doing put even on the success path. That will break the
device... and will result in non-functional device and double put due
to the close path.

Best regards,
								Pavel
-- 
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Re: [PATCH 4.14 04/33] Input: stmfts - fix reference leak in stmfts_input_open
Posted by Dmitry Torokhov 3 years, 8 months ago
On Wed, May 25, 2022 at 12:52:48PM +0200, Pavel Machek wrote:
> Hi!
> 
> > From: Zheng Yongjun <zhengyongjun3@huawei.com>
> > 
> > [ Upstream commit 26623eea0da3476446909af96c980768df07bbd9 ]
> > 
> > pm_runtime_get_sync() will increment pm usage counter even it
> > failed. Forgetting to call pm_runtime_put_noidle will result
> > in reference leak in stmfts_input_open, so we should fix it.
> 
> This is wrong, AFAICT.

Yes, I think you are right. How about below?

Thanks.

-- 
Dmitry


Input: stmfts - do not leave device disabled in stmfts_input_open

From: Dmitry Torokhov <dmitry.torokhov@gmail.com>

The commit 26623eea0da3 attempted to deal with potential leak of runtime
PM counter when opening the touchscreen device, however it ended up
erroneously dropping the counter in the case of successfully enabling the
device.

Let's address this by using pm_runtime_resume_and_get() and then executing
pm_runtime_put_sync() only when we fail to send "sense on" command to the
device.

Fixes: 26623eea0da3 ("Input: stmfts - fix reference leak in stmfts_input_open")
Reported-by: Pavel Machek <pavel@denx.de>
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
 drivers/input/touchscreen/stmfts.c |   16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/drivers/input/touchscreen/stmfts.c b/drivers/input/touchscreen/stmfts.c
index 72e0b767e1ba..c175d44c52f3 100644
--- a/drivers/input/touchscreen/stmfts.c
+++ b/drivers/input/touchscreen/stmfts.c
@@ -337,13 +337,15 @@ static int stmfts_input_open(struct input_dev *dev)
 	struct stmfts_data *sdata = input_get_drvdata(dev);
 	int err;
 
-	err = pm_runtime_get_sync(&sdata->client->dev);
-	if (err < 0)
-		goto out;
+	err = pm_runtime_resume_and_get(&sdata->client->dev);
+	if (err)
+		return err;
 
 	err = i2c_smbus_write_byte(sdata->client, STMFTS_MS_MT_SENSE_ON);
-	if (err)
-		goto out;
+	if (err) {
+		pm_runtime_put_sync(&sdata->client->dev);
+		return err;
+	}
 
 	mutex_lock(&sdata->mutex);
 	sdata->running = true;
@@ -366,9 +368,7 @@ static int stmfts_input_open(struct input_dev *dev)
 				 "failed to enable touchkey\n");
 	}
 
-out:
-	pm_runtime_put_noidle(&sdata->client->dev);
-	return err;
+	return 0;
 }
 
 static void stmfts_input_close(struct input_dev *dev)
Re: [PATCH 4.14 04/33] Input: stmfts - fix reference leak in stmfts_input_open
Posted by Pavel Machek 3 years, 8 months ago
Hi!

> > > From: Zheng Yongjun <zhengyongjun3@huawei.com>
> > > 
> > > [ Upstream commit 26623eea0da3476446909af96c980768df07bbd9 ]
> > > 
> > > pm_runtime_get_sync() will increment pm usage counter even it
> > > failed. Forgetting to call pm_runtime_put_noidle will result
> > > in reference leak in stmfts_input_open, so we should fix it.
> > 
> > This is wrong, AFAICT.
> 
> Yes, I think you are right. How about below?

Looks good to me.

> Input: stmfts - do not leave device disabled in stmfts_input_open
> 
> From: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> 
> The commit 26623eea0da3 attempted to deal with potential leak of runtime
> PM counter when opening the touchscreen device, however it ended up
> erroneously dropping the counter in the case of successfully enabling the
> device.
> 
> Let's address this by using pm_runtime_resume_and_get() and then executing
> pm_runtime_put_sync() only when we fail to send "sense on" command to the
> device.
> 
> Fixes: 26623eea0da3 ("Input: stmfts - fix reference leak in stmfts_input_open")

Reviewed-by: Pavel Machek <pavel@denx.de>

Thank you,
								Pavel
-- 
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany