From: Tzung-Bi Shih <tzungbi@kernel.org>
To: Benson Leung <bleung@chromium.org>,
Jason Gunthorpe <jgg@nvidia.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: tzungbi@kernel.org, chrome-platform@lists.linux.dev,
linux-kernel@vger.kernel.org
Subject: [PATCH v2 1/4] platform/chrome: cros_ec_chardev: Introduce chardev_data
Date: Sat, 16 May 2026 22:30:14 +0800 [thread overview]
Message-ID: <20260516143017.18560-2-tzungbi@kernel.org> (raw)
In-Reply-To: <20260516143017.18560-1-tzungbi@kernel.org>
Introduce struct chardev_pdata to hold platform driver data.
The platform driver data is allocated by kzalloc() instead of devm
variant, allowing for managed cleanup that can eventually extend beyond
device removal if files are still open.
Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org>
---
v2:
- No changes.
v1: https://lore.kernel.org/all/20260427134659.95181-6-tzungbi@kernel.org
drivers/platform/chrome/cros_ec_chardev.c | 51 +++++++++++++++++------
1 file changed, 39 insertions(+), 12 deletions(-)
diff --git a/drivers/platform/chrome/cros_ec_chardev.c b/drivers/platform/chrome/cros_ec_chardev.c
index 002be3352100..e7012e44a006 100644
--- a/drivers/platform/chrome/cros_ec_chardev.c
+++ b/drivers/platform/chrome/cros_ec_chardev.c
@@ -13,6 +13,7 @@
#include <linux/init.h>
#include <linux/device.h>
#include <linux/fs.h>
+#include <linux/kref.h>
#include <linux/miscdevice.h>
#include <linux/mod_devicetable.h>
#include <linux/module.h>
@@ -31,6 +32,21 @@
/* Arbitrary bounded size for the event queue */
#define CROS_MAX_EVENT_LEN PAGE_SIZE
+/*
+ * Platform device driver data.
+ */
+struct chardev_pdata {
+ struct miscdevice misc;
+ struct kref kref;
+};
+
+static void chardev_pdata_release(struct kref *kref)
+{
+ struct chardev_pdata *pdata = container_of(kref, typeof(*pdata), kref);
+
+ kfree(pdata);
+}
+
struct chardev_priv {
struct cros_ec_device *ec_dev;
struct notifier_block notifier;
@@ -374,28 +390,39 @@ static int cros_ec_chardev_probe(struct platform_device *pdev)
{
struct cros_ec_dev *ec = dev_get_drvdata(pdev->dev.parent);
struct cros_ec_platform *ec_platform = dev_get_platdata(ec->dev);
- struct miscdevice *misc;
+ struct chardev_pdata *pdata;
+ int ret;
- /* Create a char device: we want to create it anew */
- misc = devm_kzalloc(&pdev->dev, sizeof(*misc), GFP_KERNEL);
- if (!misc)
+ pdata = kzalloc_obj(*pdata);
+ if (!pdata)
return -ENOMEM;
- misc->minor = MISC_DYNAMIC_MINOR;
- misc->fops = &chardev_fops;
- misc->name = ec_platform->ec_name;
- misc->parent = pdev->dev.parent;
+ platform_set_drvdata(pdev, pdata);
+ kref_init(&pdata->kref);
- dev_set_drvdata(&pdev->dev, misc);
+ pdata->misc.minor = MISC_DYNAMIC_MINOR;
+ pdata->misc.fops = &chardev_fops;
+ pdata->misc.name = ec_platform->ec_name;
+ pdata->misc.parent = pdev->dev.parent;
- return misc_register(misc);
+ ret = misc_register(&pdata->misc);
+ if (ret) {
+ dev_err(&pdev->dev, "failed to register misc device\n");
+ goto err_put_pdata;
+ }
+
+ return 0;
+err_put_pdata:
+ kref_put(&pdata->kref, chardev_pdata_release);
+ return ret;
}
static void cros_ec_chardev_remove(struct platform_device *pdev)
{
- struct miscdevice *misc = dev_get_drvdata(&pdev->dev);
+ struct chardev_pdata *pdata = platform_get_drvdata(pdev);
- misc_deregister(misc);
+ misc_deregister(&pdata->misc);
+ kref_put(&pdata->kref, chardev_pdata_release);
}
static const struct platform_device_id cros_ec_chardev_id[] = {
--
2.51.0
next prev parent reply other threads:[~2026-05-16 14:30 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-16 14:30 [PATCH v2 0/4] platform/chrome: cros_ec_chardev: Fix a potential UAF Tzung-Bi Shih
2026-05-16 14:30 ` Tzung-Bi Shih [this message]
2026-05-16 14:30 ` [PATCH v2 2/4] platform/chrome: cros_ec_chardev: Move data to chardev_pdata Tzung-Bi Shih
2026-05-16 14:30 ` [PATCH v2 3/4] platform/chrome: cros_ec_chardev: Add event relayer Tzung-Bi Shih
2026-05-16 14:30 ` [PATCH v2 4/4] platform/chrome: cros_ec_chardev: Introduce rwsem for protecting ec_dev Tzung-Bi Shih
2026-05-21 13:58 ` Jason Gunthorpe
2026-05-25 5:43 ` Tzung-Bi Shih
2026-05-21 14:06 ` [PATCH v2 0/4] platform/chrome: cros_ec_chardev: Fix a potential UAF Jason Gunthorpe
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=20260516143017.18560-2-tzungbi@kernel.org \
--to=tzungbi@kernel.org \
--cc=bleung@chromium.org \
--cc=chrome-platform@lists.linux.dev \
--cc=gregkh@linuxfoundation.org \
--cc=jgg@nvidia.com \
--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