diff mbox series

[v2,12/15] platform/chrome: use krealloc() for `din` and `dout`

Message ID 20220607145639.2362750-13-tzungbi@kernel.org (mailing list archive)
State Superseded
Headers show
Series platform/chrome: Kunit tests and refactor for cros_ec_query_all() | expand

Commit Message

Tzung-Bi Shih June 7, 2022, 2:56 p.m. UTC
Use krealloc() to re-allocate `din` and `dout`.  Don't use devm variant
because the two buffers could be re-allocated multiple times during
runtime.  Their life cycles aren't quite aligned to the device's.

Free the memory in cros_ec_unregister() if any.

No need to free memory if krealloc() fails.  They will be freed
eventually in either of the following:
- Error handling path in cros_ec_register().
- In cros_ec_unregister().
- Next krealloc() in cros_ec_query_all().

Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org>
---
Changes from v1:
- Don't use devm.
- Free in cros_ec_unregister().

 drivers/platform/chrome/cros_ec.c            |  4 +++
 drivers/platform/chrome/cros_ec_proto.c      | 29 +++++++-------------
 drivers/platform/chrome/cros_ec_proto_test.c |  3 +-
 3 files changed, 15 insertions(+), 21 deletions(-)

Comments

Guenter Roeck June 7, 2022, 7:06 p.m. UTC | #1
On Tue, Jun 7, 2022 at 7:57 AM Tzung-Bi Shih <tzungbi@kernel.org> wrote:
>
> Use krealloc() to re-allocate `din` and `dout`.  Don't use devm variant
> because the two buffers could be re-allocated multiple times during
> runtime.  Their life cycles aren't quite aligned to the device's.

While this saves a few lines of code, it is runtime-expensive:
krealloc() copies the old data, which is a waste of time/resources.
Maybe it would be better to just use kfree() followed by kzalloc().

>
> Free the memory in cros_ec_unregister() if any.
>
> No need to free memory if krealloc() fails.  They will be freed
> eventually in either of the following:
> - Error handling path in cros_ec_register().
> - In cros_ec_unregister().
> - Next krealloc() in cros_ec_query_all().
>
> Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org>
> ---
> Changes from v1:
> - Don't use devm.
> - Free in cros_ec_unregister().
>
>  drivers/platform/chrome/cros_ec.c            |  4 +++
>  drivers/platform/chrome/cros_ec_proto.c      | 29 +++++++-------------
>  drivers/platform/chrome/cros_ec_proto_test.c |  3 +-
>  3 files changed, 15 insertions(+), 21 deletions(-)
>
> diff --git a/drivers/platform/chrome/cros_ec.c b/drivers/platform/chrome/cros_ec.c
> index 29d3b544dafb..fb8cb8a73295 100644
> --- a/drivers/platform/chrome/cros_ec.c
> +++ b/drivers/platform/chrome/cros_ec.c
> @@ -285,6 +285,8 @@ int cros_ec_register(struct cros_ec_device *ec_dev)
>  exit:
>         platform_device_unregister(ec_dev->ec);
>         platform_device_unregister(ec_dev->pd);
> +       kfree(ec_dev->din);
> +       kfree(ec_dev->dout);
>         return err;
>  }
>  EXPORT_SYMBOL(cros_ec_register);
> @@ -302,6 +304,8 @@ void cros_ec_unregister(struct cros_ec_device *ec_dev)
>         if (ec_dev->pd)
>                 platform_device_unregister(ec_dev->pd);
>         platform_device_unregister(ec_dev->ec);
> +       kfree(ec_dev->din);
> +       kfree(ec_dev->dout);
>  }
>  EXPORT_SYMBOL(cros_ec_unregister);
>
> diff --git a/drivers/platform/chrome/cros_ec_proto.c b/drivers/platform/chrome/cros_ec_proto.c
> index 473654f50bca..de6bc457e496 100644
> --- a/drivers/platform/chrome/cros_ec_proto.c
> +++ b/drivers/platform/chrome/cros_ec_proto.c
> @@ -469,9 +469,9 @@ static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev,
>   */
>  int cros_ec_query_all(struct cros_ec_device *ec_dev)
>  {
> -       struct device *dev = ec_dev->dev;
>         u32 ver_mask = 0;
>         int ret;
> +       u8 *din, *dout;
>
>         /* First try sending with proto v3. */
>         if (!cros_ec_get_proto_info(ec_dev, CROS_EC_DEV_EC_INDEX)) {
> @@ -492,21 +492,15 @@ int cros_ec_query_all(struct cros_ec_device *ec_dev)
>                 }
>         }
>
> -       devm_kfree(dev, ec_dev->din);
> -       devm_kfree(dev, ec_dev->dout);
> -
> -       ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
> -       if (!ec_dev->din) {
> -               ret = -ENOMEM;
> -               goto exit;
> -       }
> +       din = krealloc(ec_dev->din, ec_dev->din_size, GFP_KERNEL);
> +       if (!din)
> +               return -ENOMEM;

I would suggest assigning the values directly; the new variables don't
really add value.

Thanks,
Guenter

> +       ec_dev->din = din;
>
> -       ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
> -       if (!ec_dev->dout) {
> -               devm_kfree(dev, ec_dev->din);
> -               ret = -ENOMEM;
> -               goto exit;
> -       }
> +       dout = krealloc(ec_dev->dout, ec_dev->dout_size, GFP_KERNEL);
> +       if (!dout)
> +               return -ENOMEM;
> +       ec_dev->dout = dout;
>
>         /* Probe if MKBP event is supported */
>         ret = cros_ec_get_host_command_version_mask(ec_dev,
> @@ -555,10 +549,7 @@ int cros_ec_query_all(struct cros_ec_device *ec_dev)
>                                 "failed to retrieve wake mask: %d\n", ret);
>         }
>
> -       ret = 0;
> -
> -exit:
> -       return ret;
> +       return 0;
>  }
>  EXPORT_SYMBOL(cros_ec_query_all);
>
> diff --git a/drivers/platform/chrome/cros_ec_proto_test.c b/drivers/platform/chrome/cros_ec_proto_test.c
> index 730248be42a7..27b81a5a9880 100644
> --- a/drivers/platform/chrome/cros_ec_proto_test.c
> +++ b/drivers/platform/chrome/cros_ec_proto_test.c
> @@ -180,8 +180,7 @@ static void cros_ec_proto_test_query_all_pretest(struct kunit *test)
>
>         /*
>          * cros_ec_query_all() will free din and dout and allocate them again to fit the usage by
> -        * calling devm_kfree() and devm_kzalloc().  Set them to NULL as they aren't managed by
> -        * ec_dev->dev.
> +        * calling krealloc().  Set them to NULL as they aren't allocated by kalloc().
>          */
>         ec_dev->din = NULL;
>         ec_dev->dout = NULL;
> --
> 2.36.1.255.ge46751e96f-goog
>
Tzung-Bi Shih June 8, 2022, 2:17 a.m. UTC | #2
On Tue, Jun 07, 2022 at 12:06:23PM -0700, Guenter Roeck wrote:
> On Tue, Jun 7, 2022 at 7:57 AM Tzung-Bi Shih <tzungbi@kernel.org> wrote:
> >
> > Use krealloc() to re-allocate `din` and `dout`.  Don't use devm variant
> > because the two buffers could be re-allocated multiple times during
> > runtime.  Their life cycles aren't quite aligned to the device's.
> 
> While this saves a few lines of code, it is runtime-expensive:
> krealloc() copies the old data, which is a waste of time/resources.
> Maybe it would be better to just use kfree() followed by kzalloc().

Ack.  I wasn't aware of the memcpy().  Let's don't use it.

> > @@ -469,9 +469,9 @@ static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev,
> >   */
> >  int cros_ec_query_all(struct cros_ec_device *ec_dev)
> >  {
> > -       struct device *dev = ec_dev->dev;
> >         u32 ver_mask = 0;
> >         int ret;
> > +       u8 *din, *dout;
> >
> >         /* First try sending with proto v3. */
> >         if (!cros_ec_get_proto_info(ec_dev, CROS_EC_DEV_EC_INDEX)) {
> > @@ -492,21 +492,15 @@ int cros_ec_query_all(struct cros_ec_device *ec_dev)
> >                 }
> >         }
> >
> > -       devm_kfree(dev, ec_dev->din);
> > -       devm_kfree(dev, ec_dev->dout);
> > -
> > -       ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
> > -       if (!ec_dev->din) {
> > -               ret = -ENOMEM;
> > -               goto exit;
> > -       }
> > +       din = krealloc(ec_dev->din, ec_dev->din_size, GFP_KERNEL);
> > +       if (!din)
> > +               return -ENOMEM;
> 
> I would suggest assigning the values directly; the new variables don't
> really add value.

checkpatch.pl generated a warning for it.  See [1].

Will remove the krealloc()s anyway.

[1]: https://github.com/torvalds/linux/commit/972fdea2e6ece7578915d386a5447bc78d3fb8b8
diff mbox series

Patch

diff --git a/drivers/platform/chrome/cros_ec.c b/drivers/platform/chrome/cros_ec.c
index 29d3b544dafb..fb8cb8a73295 100644
--- a/drivers/platform/chrome/cros_ec.c
+++ b/drivers/platform/chrome/cros_ec.c
@@ -285,6 +285,8 @@  int cros_ec_register(struct cros_ec_device *ec_dev)
 exit:
 	platform_device_unregister(ec_dev->ec);
 	platform_device_unregister(ec_dev->pd);
+	kfree(ec_dev->din);
+	kfree(ec_dev->dout);
 	return err;
 }
 EXPORT_SYMBOL(cros_ec_register);
@@ -302,6 +304,8 @@  void cros_ec_unregister(struct cros_ec_device *ec_dev)
 	if (ec_dev->pd)
 		platform_device_unregister(ec_dev->pd);
 	platform_device_unregister(ec_dev->ec);
+	kfree(ec_dev->din);
+	kfree(ec_dev->dout);
 }
 EXPORT_SYMBOL(cros_ec_unregister);
 
diff --git a/drivers/platform/chrome/cros_ec_proto.c b/drivers/platform/chrome/cros_ec_proto.c
index 473654f50bca..de6bc457e496 100644
--- a/drivers/platform/chrome/cros_ec_proto.c
+++ b/drivers/platform/chrome/cros_ec_proto.c
@@ -469,9 +469,9 @@  static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev,
  */
 int cros_ec_query_all(struct cros_ec_device *ec_dev)
 {
-	struct device *dev = ec_dev->dev;
 	u32 ver_mask = 0;
 	int ret;
+	u8 *din, *dout;
 
 	/* First try sending with proto v3. */
 	if (!cros_ec_get_proto_info(ec_dev, CROS_EC_DEV_EC_INDEX)) {
@@ -492,21 +492,15 @@  int cros_ec_query_all(struct cros_ec_device *ec_dev)
 		}
 	}
 
-	devm_kfree(dev, ec_dev->din);
-	devm_kfree(dev, ec_dev->dout);
-
-	ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
-	if (!ec_dev->din) {
-		ret = -ENOMEM;
-		goto exit;
-	}
+	din = krealloc(ec_dev->din, ec_dev->din_size, GFP_KERNEL);
+	if (!din)
+		return -ENOMEM;
+	ec_dev->din = din;
 
-	ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
-	if (!ec_dev->dout) {
-		devm_kfree(dev, ec_dev->din);
-		ret = -ENOMEM;
-		goto exit;
-	}
+	dout = krealloc(ec_dev->dout, ec_dev->dout_size, GFP_KERNEL);
+	if (!dout)
+		return -ENOMEM;
+	ec_dev->dout = dout;
 
 	/* Probe if MKBP event is supported */
 	ret = cros_ec_get_host_command_version_mask(ec_dev,
@@ -555,10 +549,7 @@  int cros_ec_query_all(struct cros_ec_device *ec_dev)
 				"failed to retrieve wake mask: %d\n", ret);
 	}
 
-	ret = 0;
-
-exit:
-	return ret;
+	return 0;
 }
 EXPORT_SYMBOL(cros_ec_query_all);
 
diff --git a/drivers/platform/chrome/cros_ec_proto_test.c b/drivers/platform/chrome/cros_ec_proto_test.c
index 730248be42a7..27b81a5a9880 100644
--- a/drivers/platform/chrome/cros_ec_proto_test.c
+++ b/drivers/platform/chrome/cros_ec_proto_test.c
@@ -180,8 +180,7 @@  static void cros_ec_proto_test_query_all_pretest(struct kunit *test)
 
 	/*
 	 * cros_ec_query_all() will free din and dout and allocate them again to fit the usage by
-	 * calling devm_kfree() and devm_kzalloc().  Set them to NULL as they aren't managed by
-	 * ec_dev->dev.
+	 * calling krealloc().  Set them to NULL as they aren't allocated by kalloc().
 	 */
 	ec_dev->din = NULL;
 	ec_dev->dout = NULL;