Chrome platform driver development
 help / color / mirror / Atom feed
From: Tzung-Bi Shih <tzungbi@kernel.org>
To: bleung@chromium.org, groeck@chromium.org
Cc: chrome-platform@lists.linux.dev, tzungbi@kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH v3 14/23] platform/chrome: don't use devm variants for `din` and `dout`
Date: Wed,  8 Jun 2022 11:07:25 +0000	[thread overview]
Message-ID: <20220608110734.2928245-15-tzungbi@kernel.org> (raw)
In-Reply-To: <20220608110734.2928245-1-tzungbi@kernel.org>

Don't use devm variants because the two buffers could be re-allocated
multiple times during runtime.  Their life cycles aren't quite aligned
to the device's.

Normally, free the memory if any when the ec_dev gets unregistered in
cros_ec_unregister().

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

Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org>
---
Changes from v2:
- Don't use realloc.

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 |  4 +--
 3 files changed, 17 insertions(+), 20 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..8a53e989c7e2 100644
--- a/drivers/platform/chrome/cros_ec_proto.c
+++ b/drivers/platform/chrome/cros_ec_proto.c
@@ -469,7 +469,6 @@ 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;
 
@@ -492,21 +491,18 @@ int cros_ec_query_all(struct cros_ec_device *ec_dev)
 		}
 	}
 
-	devm_kfree(dev, ec_dev->din);
-	devm_kfree(dev, ec_dev->dout);
+	kfree(ec_dev->din);
+	ec_dev->din = NULL;
+	kfree(ec_dev->dout);
+	ec_dev->dout = NULL;
 
-	ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
-	if (!ec_dev->din) {
-		ret = -ENOMEM;
-		goto exit;
-	}
+	ec_dev->din = kmalloc(ec_dev->din_size, GFP_KERNEL);
+	if (!ec_dev->din)
+		return -ENOMEM;
 
-	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;
-	}
+	ec_dev->dout = kmalloc(ec_dev->dout_size, GFP_KERNEL);
+	if (!ec_dev->dout)
+		return -ENOMEM;
 
 	/* Probe if MKBP event is supported */
 	ret = cros_ec_get_host_command_version_mask(ec_dev,
@@ -555,10 +551,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 63071af81c94..ec106d0f5648 100644
--- a/drivers/platform/chrome/cros_ec_proto_test.c
+++ b/drivers/platform/chrome/cros_ec_proto_test.c
@@ -180,8 +180,8 @@ 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 but allocated statically in struct cros_ec_proto_test_priv
+	 * calling kfree() and kmalloc().  Set them to NULL as they aren't allocated by kmalloc()
+	 * but allocated statically in struct cros_ec_proto_test_priv
 	 * (see cros_ec_proto_test_init()).
 	 */
 	ec_dev->din = NULL;
-- 
2.36.1.255.ge46751e96f-goog


  parent reply	other threads:[~2022-06-08 11:08 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-08 11:07 [PATCH v3 00/23] platform/chrome: Kunit tests and refactor for cros_ec_query_all() Tzung-Bi Shih
2022-06-08 11:07 ` [PATCH v3 01/23] platform/chrome: cros_ec_commands: fix compile errors Tzung-Bi Shih
2022-06-08 11:07 ` [PATCH v3 02/23] platform/chrome: cros_ec_proto: add Kunit tests for cros_ec_query_all() Tzung-Bi Shih
2022-06-08 16:12   ` Guenter Roeck
2022-06-08 11:07 ` [PATCH v3 03/23] platform/chrome: use macros for passthru indexes Tzung-Bi Shih
2022-06-08 11:07 ` [PATCH v3 04/23] platform/chrome: cros_ec_proto: assign buffer size from protocol info Tzung-Bi Shih
2022-06-08 11:07 ` [PATCH v3 05/23] platform/chrome: cros_ec_proto: remove redundant NULL check Tzung-Bi Shih
2022-06-08 11:07 ` [PATCH v3 06/23] platform/chrome: cros_ec_proto: use cros_ec_map_error() Tzung-Bi Shih
2022-06-08 11:07 ` [PATCH v3 07/23] platform/chrome: cros_ec_proto: separate cros_ec_get_proto_info() Tzung-Bi Shih
2022-06-08 11:07 ` [PATCH v3 08/23] platform/chrome: cros_ec_proto: add Kunit tests for getting proto info Tzung-Bi Shih
2022-06-08 16:13   ` Guenter Roeck
2022-06-08 11:07 ` [PATCH v3 09/23] platform/chrome: cros_ec_proto: handle empty payload in " Tzung-Bi Shih
2022-06-08 16:13   ` Guenter Roeck
2022-06-08 11:07 ` [PATCH v3 10/23] platform/chrome: cros_ec_proto: separate cros_ec_get_proto_info_legacy() Tzung-Bi Shih
2022-06-08 11:07 ` [PATCH v3 11/23] platform/chrome: cros_ec_proto: add Kunit test for getting legacy info Tzung-Bi Shih
2022-06-08 16:14   ` Guenter Roeck
2022-06-08 11:07 ` [PATCH v3 12/23] platform/chrome: cros_ec_proto: handle empty payload in getting info legacy Tzung-Bi Shih
2022-06-08 16:14   ` Guenter Roeck
2022-06-08 11:07 ` [PATCH v3 13/23] platform/chrome: cros_ec: don't allocate `din` and `dout` in cros_ec_register() Tzung-Bi Shih
2022-06-08 16:15   ` Guenter Roeck
2022-06-09  5:09     ` Tzung-Bi Shih
2022-06-08 11:07 ` Tzung-Bi Shih [this message]
2022-06-08 16:17   ` [PATCH v3 14/23] platform/chrome: don't use devm variants for `din` and `dout` Guenter Roeck
2022-06-08 11:07 ` [PATCH v3 15/23] platform/chrome: cros_ec_proto: don't show MKBP version if unsupported Tzung-Bi Shih
2022-06-08 11:07 ` [PATCH v3 16/23] platform/chrome: cros_ec_proto: return 0 on getting cmd mask success Tzung-Bi Shih
2022-06-08 16:20   ` Guenter Roeck
2022-06-09  8:44     ` Tzung-Bi Shih
2022-06-08 11:07 ` [PATCH v3 17/23] platform/chrome: cros_ec_proto: add Kunit test for getting cmd mask error Tzung-Bi Shih
2022-06-08 16:21   ` Guenter Roeck
2022-06-08 11:07 ` [PATCH v3 18/23] platform/chrome: cros_ec_proto: check `msg->result` in getting cmd mask Tzung-Bi Shih
2022-06-08 16:23   ` Guenter Roeck
2022-06-08 16:27     ` Guenter Roeck
2022-06-08 11:07 ` [PATCH v3 19/23] platform/chrome: cros_ec_proto: add Kunit tests for " Tzung-Bi Shih
2022-06-08 16:28   ` Guenter Roeck
2022-06-08 11:07 ` [PATCH v3 20/23] platform/chrome: cros_ec_proto: handle empty payload in " Tzung-Bi Shih
2022-06-08 16:24   ` Guenter Roeck
2022-06-08 11:07 ` [PATCH v3 21/23] platform/chrome: cros_ec_proto: return 0 on getting wake mask success Tzung-Bi Shih
2022-06-08 16:26   ` Guenter Roeck
2022-06-08 11:07 ` [PATCH v3 22/23] platform/chrome: cros_ec_proto: add Kunit test for getting wake mask Tzung-Bi Shih
2022-06-08 16:29   ` Guenter Roeck
2022-06-08 11:07 ` [PATCH v3 23/23] platform/chrome: cros_ec_proto: handle empty payload in " Tzung-Bi Shih
2022-06-08 16:30   ` Guenter Roeck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220608110734.2928245-15-tzungbi@kernel.org \
    --to=tzungbi@kernel.org \
    --cc=bleung@chromium.org \
    --cc=chrome-platform@lists.linux.dev \
    --cc=groeck@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox