diff mbox series

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

Message ID 20220209095703.517608-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, 9:56 a.m. UTC
Fix cros_ec_register() to unregister platform devices if
blocking_notifier_chain_register() fails.

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

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>
---
Changes from v2:
(https://patchwork.kernel.org/project/chrome-platform/patch/20220209045035.380615-2-tzungbi@google.com/)
- Fix grammar error in commit message.
- Change the code that don't rely on zeroed memory.
- Remove unnecessary `if` checks before calling platform_device_unregister().

Changes from v1:
(https://lore.kernel.org/lkml/20220125101527.1812887-1-tzungbi@google.com/T/#u)
- Use imperative mood in commit message.
- Use IS_ERR_OR_NULL() in 1st patch.

 drivers/platform/chrome/cros_ec.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

Comments

Prashant Malani Feb. 15, 2022, 9:39 p.m. UTC | #1
On Feb 09 17:56, Tzung-Bi Shih wrote:
> Fix cros_ec_register() to unregister platform devices if
> blocking_notifier_chain_register() fails.
>
> Also use the single exit path to handle the platform device
> unregistration.
>
> 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>
> ---
> Changes from v2:
> (https://patchwork.kernel.org/project/chrome-platform/patch/20220209045035.380615-2-tzungbi@google.com/)
> - Fix grammar error in commit message.
> - Change the code that don't rely on zeroed memory.
> - Remove unnecessary `if` checks before calling platform_device_unregister().
>
> Changes from v1:
> (https://lore.kernel.org/lkml/20220125101527.1812887-1-tzungbi@google.com/T/#u)
> - Use imperative mood in commit message.
> - Use IS_ERR_OR_NULL() in 1st patch.
>
>  drivers/platform/chrome/cros_ec.c | 15 +++++++++------
>  1 file changed, 9 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/platform/chrome/cros_ec.c b/drivers/platform/chrome/cros_ec.c
> index fc5aa1525d13..15f599d721a1 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,11 @@ int cros_ec_register(struct cros_ec_device *ec_dev)
>               cros_ec_irq_thread(0, ec_dev);
>
>       return 0;
> +exit:
> +     if (ec_dev->max_passthru)

This just complicates things, by depending on another variable. Just initialize
ec_dev->pd and ec_dev->ec to NULL at the beginning instead.


> +             platform_device_unregister(ec_dev->pd);
> +     platform_device_unregister(ec_dev->ec);
> +     return err;
>  }
>  EXPORT_SYMBOL(cros_ec_register);
>
> --
> 2.35.0.263.gb82422642f-goog
>

-Prashant
Tzung-Bi Shih Feb. 16, 2022, 4:11 a.m. UTC | #2
On Tue, Feb 15, 2022 at 01:39:51PM -0800, Prashant Malani wrote:
> On Feb 09 17:56, Tzung-Bi Shih wrote:
> > +exit:
> > +     if (ec_dev->max_passthru)
> 
> This just complicates things, by depending on another variable. Just initialize
> ec_dev->pd and ec_dev->ec to NULL at the beginning instead.

Ack, will fix in the next version.
diff mbox series

Patch

diff --git a/drivers/platform/chrome/cros_ec.c b/drivers/platform/chrome/cros_ec.c
index fc5aa1525d13..15f599d721a1 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,11 @@  int cros_ec_register(struct cros_ec_device *ec_dev)
 		cros_ec_irq_thread(0, ec_dev);
 
 	return 0;
+exit:
+	if (ec_dev->max_passthru)
+		platform_device_unregister(ec_dev->pd);
+	platform_device_unregister(ec_dev->ec);
+	return err;
 }
 EXPORT_SYMBOL(cros_ec_register);