diff mbox series

USB: apple-mfi-fastcharge: fix reference leak in apple_mfi_fc_set_property

Message ID 20201030154534.98294-1-zhangqilong3@huawei.com (mailing list archive)
State Superseded
Headers show
Series USB: apple-mfi-fastcharge: fix reference leak in apple_mfi_fc_set_property | expand

Commit Message

Zhang Qilong Oct. 30, 2020, 3:45 p.m. UTC
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 apple_mfi_fc_set_property, so
we should fix it.

Signed-off-by: Zhang Qilong <zhangqilong3@huawei.com>
---
 drivers/usb/misc/apple-mfi-fastcharge.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

Bastien Nocera Oct. 30, 2020, 3:56 p.m. UTC | #1
On Fri, 2020-10-30 at 23:45 +0800, Zhang Qilong wrote:
> 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 apple_mfi_fc_set_property, so
> we should fix it.
> 
> Signed-off-by: Zhang Qilong <zhangqilong3@huawei.com>

It looks correct, but I don't know whether it's necessary.
There's a boatload of users of that API that don't even check for the
get_sync() retval, and loads more where it's checked but never acted
upon.

Do you intend to fix all those as well?

> ---
>  drivers/usb/misc/apple-mfi-fastcharge.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c
> b/drivers/usb/misc/apple-mfi-fastcharge.c
> index b403094a6b3a..9e1ad4536e36 100644
> --- a/drivers/usb/misc/apple-mfi-fastcharge.c
> +++ b/drivers/usb/misc/apple-mfi-fastcharge.c
> @@ -120,8 +120,10 @@ static int apple_mfi_fc_set_property(struct
> power_supply *psy,
>         dev_dbg(&mfi->udev->dev, "prop: %d\n", psp);
>  
>         ret = pm_runtime_get_sync(&mfi->udev->dev);
> -       if (ret < 0)
> +       if (ret < 0) {
> +               pm_runtime_put_noidle(&mfi->udev->dev);
>                 return ret;
> +       }
>  
>         switch (psp) {
>         case POWER_SUPPLY_PROP_CHARGE_TYPE:
Sergei Shtylyov Oct. 30, 2020, 6:11 p.m. UTC | #2
On 10/30/20 6:45 PM, Zhang Qilong wrote:

> pm_runtime_get_sync() will increment pm usage counter even

   You missed when/if in this (and the following) patch.

> it failed. Forgetting to call pm_runtime_put_noidle will
> result in reference leak in apple_mfi_fc_set_property, so
> we should fix it.
> 
> Signed-off-by: Zhang Qilong <zhangqilong3@huawei.com>
[...]

MBR, Sergei
Zhang Qilong Oct. 31, 2020, 3:29 a.m. UTC | #3
> 
> On Fri, 2020-10-30 at 23:45 +0800, Zhang Qilong wrote:
> > 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 apple_mfi_fc_set_property, so we should fix it.
> >
> > Signed-off-by: Zhang Qilong <zhangqilong3@huawei.com>
> 
> It looks correct, but I don't know whether it's necessary.
> There's a boatload of users of that API that don't even check for the
> get_sync() retval, and loads more where it's checked but never acted upon.
> 
> Do you intend to fix all those as well?
> 

pm_runtime_get_sync will increase the reference count at first, and it will resume the device
later. If runtime of the device is active or has error(something else....), resume will failed. If we
do not call put operation to decrease the reference, the result is that this device cannot enter
the idle state and always stay busy or other non-idle state, maybe it consumes more power or
raise additional errors... And I'll try to fix them : )

Thanks, best wish!

Zhang Qilong

> > ---
> >  drivers/usb/misc/apple-mfi-fastcharge.c | 4 +++-
> >  1 file changed, 3 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c
> > b/drivers/usb/misc/apple-mfi-fastcharge.c
> > index b403094a6b3a..9e1ad4536e36 100644
> > --- a/drivers/usb/misc/apple-mfi-fastcharge.c
> > +++ b/drivers/usb/misc/apple-mfi-fastcharge.c
> > @@ -120,8 +120,10 @@ static int apple_mfi_fc_set_property(struct
> > power_supply *psy,
> >         dev_dbg(&mfi->udev->dev, "prop: %d\n", psp);
> >
> >         ret = pm_runtime_get_sync(&mfi->udev->dev);
> > -       if (ret < 0)
> > +       if (ret < 0) {
> > +               pm_runtime_put_noidle(&mfi->udev->dev);
> >                 return ret;
> > +       }
> >
> >         switch (psp) {
> >         case POWER_SUPPLY_PROP_CHARGE_TYPE:
>
diff mbox series

Patch

diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c b/drivers/usb/misc/apple-mfi-fastcharge.c
index b403094a6b3a..9e1ad4536e36 100644
--- a/drivers/usb/misc/apple-mfi-fastcharge.c
+++ b/drivers/usb/misc/apple-mfi-fastcharge.c
@@ -120,8 +120,10 @@  static int apple_mfi_fc_set_property(struct power_supply *psy,
 	dev_dbg(&mfi->udev->dev, "prop: %d\n", psp);
 
 	ret = pm_runtime_get_sync(&mfi->udev->dev);
-	if (ret < 0)
+	if (ret < 0) {
+		pm_runtime_put_noidle(&mfi->udev->dev);
 		return ret;
+	}
 
 	switch (psp) {
 	case POWER_SUPPLY_PROP_CHARGE_TYPE: