diff mbox series

[v2,1/6] platform/chrome: cros_ec: fix error handling in cros_ec_register()

Message ID 20220209045035.380615-2-tzungbi@google.com (mailing list archive)
State Superseded
Headers show
Series platform/chrome: cros_ec: miscellaneous cleanups | expand

Commit Message

Tzung-Bi Shih Feb. 9, 2022, 4:50 a.m. UTC
Fix cros_ec_register() doesn't unregister platform devices if
blocking_notifier_chain_register() fails.

Also use the single exit path to handle the platform device
unregistration.

This fix depends on the fact that all the callers of cros_ec_register()
allocate zeroed memory.

Fixes: 42cd0ab476e2 ("platform/chrome: cros_ec: Query EC protocol version if EC transitions between RO/RW")
Signed-off-by: Tzung-Bi Shih <tzungbi@google.com>
---
 drivers/platform/chrome/cros_ec.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

Comments

Prashant Malani Feb. 9, 2022, 5:38 a.m. UTC | #1
Hi,

On Tue, Feb 8, 2022 at 8:50 PM Tzung-Bi Shih <tzungbi@google.com> wrote:
>
> Fix cros_ec_register() doesn't unregister platform devices if
> blocking_notifier_chain_register() fails.

This isn't grammatically correct. Instead:
Fix cros_ec_register() to unregister platform devices if blocking....

>
> Also use the single exit path to handle the platform device
> unregistration.
>
> This fix depends on the fact that all the callers of cros_ec_register()
> allocate zeroed memory.

Is that a fair assumption? What happens if a future driver calls
cros_ec_register()
without zeroed memory?

>
> Fixes: 42cd0ab476e2 ("platform/chrome: cros_ec: Query EC protocol version if EC transitions between RO/RW")
> Signed-off-by: Tzung-Bi Shih <tzungbi@google.com>
> ---
What has changed since v2? Please add a change log of the versions here.

>  drivers/platform/chrome/cros_ec.c | 16 ++++++++++------
>  1 file changed, 10 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/platform/chrome/cros_ec.c b/drivers/platform/chrome/cros_ec.c
> index fc5aa1525d13..7ce667ff08e0 100644
> --- a/drivers/platform/chrome/cros_ec.c
> +++ b/drivers/platform/chrome/cros_ec.c
> @@ -245,18 +245,16 @@ int cros_ec_register(struct cros_ec_device *ec_dev)
>                 if (IS_ERR(ec_dev->pd)) {
>                         dev_err(ec_dev->dev,
>                                 "Failed to create CrOS PD platform device\n");
> -                       platform_device_unregister(ec_dev->ec);
> -                       return PTR_ERR(ec_dev->pd);
> +                       err = PTR_ERR(ec_dev->pd);
> +                       goto exit;
>                 }
>         }
>
>         if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
>                 err = devm_of_platform_populate(dev);
>                 if (err) {
> -                       platform_device_unregister(ec_dev->pd);
> -                       platform_device_unregister(ec_dev->ec);
>                         dev_err(dev, "Failed to register sub-devices\n");
> -                       return err;
> +                       goto exit;
>                 }
>         }
>
> @@ -278,7 +276,7 @@ int cros_ec_register(struct cros_ec_device *ec_dev)
>                 err = blocking_notifier_chain_register(&ec_dev->event_notifier,
>                                                       &ec_dev->notifier_ready);
>                 if (err)
> -                       return err;
> +                       goto exit;
>         }
>
>         dev_info(dev, "Chrome EC device registered\n");
> @@ -291,6 +289,12 @@ int cros_ec_register(struct cros_ec_device *ec_dev)
>                 cros_ec_irq_thread(0, ec_dev);
>
>         return 0;
> +exit:
> +       if (!IS_ERR_OR_NULL(ec_dev->pd))
> +               platform_device_unregister(ec_dev->pd);
> +       if (!IS_ERR_OR_NULL(ec_dev->ec))
> +               platform_device_unregister(ec_dev->ec);
> +       return err;
You don't need these "if" checks. They are already performed by
platform_device_unregister(), or rather, the functions that it calls [1][2].

[1] https://elixir.bootlin.com/linux/latest/source/drivers/base/platform.c#L756
[2] https://elixir.bootlin.com/linux/latest/source/drivers/base/platform.c#L553
Tzung-Bi Shih Feb. 9, 2022, 9:35 a.m. UTC | #2
On Tue, Feb 08, 2022 at 09:38:55PM -0800, Prashant Malani wrote:
> On Tue, Feb 8, 2022 at 8:50 PM Tzung-Bi Shih <tzungbi@google.com> wrote:
> >
> > Fix cros_ec_register() doesn't unregister platform devices if
> > blocking_notifier_chain_register() fails.
> 
> This isn't grammatically correct. Instead:
> Fix cros_ec_register() to unregister platform devices if blocking....

Thanks, will fix in next version.

> > Also use the single exit path to handle the platform device
> > unregistration.
> >
> > This fix depends on the fact that all the callers of cros_ec_register()
> > allocate zeroed memory.
> 
> Is that a fair assumption? What happens if a future driver calls
> cros_ec_register()
> without zeroed memory?

Suppose ec_dev->pd is garbage data, platform_device_unregister() will operate
on invalid memory address.

Let's use another guard condition in next version instead of relying on
zeroed memory.

> > ---
> What has changed since v2? Please add a change log of the versions here.

They are in the cover letter actually.  Will copy them to each patch.

> > @@ -291,6 +289,12 @@ int cros_ec_register(struct cros_ec_device *ec_dev)
> >                 cros_ec_irq_thread(0, ec_dev);
> >
> >         return 0;
> > +exit:
> > +       if (!IS_ERR_OR_NULL(ec_dev->pd))
> > +               platform_device_unregister(ec_dev->pd);
> > +       if (!IS_ERR_OR_NULL(ec_dev->ec))
> > +               platform_device_unregister(ec_dev->ec);
> > +       return err;
> You don't need these "if" checks. They are already performed by
> platform_device_unregister(), or rather, the functions that it calls [1][2].

Ack, will remove the check in next version.
diff mbox series

Patch

diff --git a/drivers/platform/chrome/cros_ec.c b/drivers/platform/chrome/cros_ec.c
index fc5aa1525d13..7ce667ff08e0 100644
--- a/drivers/platform/chrome/cros_ec.c
+++ b/drivers/platform/chrome/cros_ec.c
@@ -245,18 +245,16 @@  int cros_ec_register(struct cros_ec_device *ec_dev)
 		if (IS_ERR(ec_dev->pd)) {
 			dev_err(ec_dev->dev,
 				"Failed to create CrOS PD platform device\n");
-			platform_device_unregister(ec_dev->ec);
-			return PTR_ERR(ec_dev->pd);
+			err = PTR_ERR(ec_dev->pd);
+			goto exit;
 		}
 	}
 
 	if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
 		err = devm_of_platform_populate(dev);
 		if (err) {
-			platform_device_unregister(ec_dev->pd);
-			platform_device_unregister(ec_dev->ec);
 			dev_err(dev, "Failed to register sub-devices\n");
-			return err;
+			goto exit;
 		}
 	}
 
@@ -278,7 +276,7 @@  int cros_ec_register(struct cros_ec_device *ec_dev)
 		err = blocking_notifier_chain_register(&ec_dev->event_notifier,
 						      &ec_dev->notifier_ready);
 		if (err)
-			return err;
+			goto exit;
 	}
 
 	dev_info(dev, "Chrome EC device registered\n");
@@ -291,6 +289,12 @@  int cros_ec_register(struct cros_ec_device *ec_dev)
 		cros_ec_irq_thread(0, ec_dev);
 
 	return 0;
+exit:
+	if (!IS_ERR_OR_NULL(ec_dev->pd))
+		platform_device_unregister(ec_dev->pd);
+	if (!IS_ERR_OR_NULL(ec_dev->ec))
+		platform_device_unregister(ec_dev->ec);
+	return err;
 }
 EXPORT_SYMBOL(cros_ec_register);