platform/chrome: cros_ec_chardev: Fix a possible UAF
Tzung-Bi Shih
maintainerv2 cover platform/chrome: cros_ec_chardev: Fix a possible UAF
This is a follow-up series of [1]. It tries to fix a possible UAF in the fops of cros_ec_chardev after the underlying protocol device has gone by using kref in struct cros_ec_device.
The 1st patch is a cleanup.
The 2nd patch removes the dependencies of fops to struct cros_ec_dev. Thus, it doesn't need to hold a reference to struct cros_ec_dev.
The 3rd patch returns an error when trying to send commands through an underlying protocol device which either hasn't been registered or has gone.
The 4th patch introduces a new helper cros_ec_device_alloc() for allocating struct cros_ec_device properly (including to initialize the kref).
The 5th patch lets the fops hold the kref when the file is opening.
The 6th patch starts to manage struct cros_ec_device's lifecycle by the kref.
[1] https://patchwork.kernel.org/project/chrome-platform/patch/[email protected]/
Tzung-Bi Shih (6):
platform/chrome: cros_ec_chardev: Remove redundant struct field platform/chrome: cros_ec_chardev: Decouple fops from struct
cros_ec_dev
platform/chrome: Disallow sending commands through unregistered ec_dev platform/chrome: Introduce cros_ec_device_alloc() platform/chrome: cros_ec_chardev: Hold refcount of struct
cros_ec_device
platform/chrome: Manage struct cros_ec_device lifecycle by its
refcount
drivers/platform/chrome/cros_ec.c | 13 +++- drivers/platform/chrome/cros_ec_chardev.c | 74 ++++++++++----------- drivers/platform/chrome/cros_ec_i2c.c | 4 +- drivers/platform/chrome/cros_ec_ishtp.c | 9 ++- drivers/platform/chrome/cros_ec_lpc.c | 11 +-- drivers/platform/chrome/cros_ec_proto.c | 53 +++++++++++++++ drivers/platform/chrome/cros_ec_rpmsg.c | 20 ++++-- drivers/platform/chrome/cros_ec_spi.c | 10 +-- drivers/platform/chrome/cros_ec_uart.c | 17 +++-- include/linux/platform_data/cros_ec_proto.h | 9 +++ 10 files changed, 153 insertions(+), 67 deletions(-)
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
Tzung-Bi Shih
maintainerPATCH v2 1/6 platform/chrome: cros_ec_chardev: Remove redundant struct field
ec_dev in the struct chardev_data is unused. Remove the field and the whole struct as well.
---
No changes since v1 (https://patchwork.kernel.org/project/chrome-platform/patch/[email protected]/).
drivers/platform/chrome/cros_ec_chardev.c | 28 +++++++++-------------- 1 file changed, 11 insertions(+), 17 deletions(-)
drivers/platform/chrome/cros_ec_chardev.c +11 -17
@@ -31,11 +31,6 @@/* Arbitrary bounded size for the event queue */ #define CROS_MAX_EVENT_LEN PAGE_SIZE -struct chardev_data { -struct cros_ec_dev *ec_dev; -struct miscdevice misc; -}; -struct chardev_priv {struct cros_ec_dev *ec_dev;struct notifier_block notifier;
@@ -379,29 +374,28 @@ static int cros_ec_chardev_probe(struct platform_device *pdev) {struct cros_ec_dev *ec_dev =dev_get_drvdata (pdev->dev.parent);struct cros_ec_platform *ec_platform =dev_get_platdata (ec_dev->dev); -struct chardev_data *data; +struct miscdevice *misc;/* Create a char device: we want to create it anew */ - data =devm_kzalloc (&pdev->dev,sizeof (*data),GFP_KERNEL ); -if (!data) + misc =devm_kzalloc (&pdev->dev,sizeof (*misc),GFP_KERNEL ); +if (!misc)return -ENOMEM ; - data->ec_dev = ec_dev; - data->misc.minor =MISC_DYNAMIC_MINOR ; - data->misc.fops = &chardev_fops; - data->misc.name = ec_platform->ec_name; - data->misc.parent = pdev->dev.parent; + misc->minor =MISC_DYNAMIC_MINOR ; + misc->fops = &chardev_fops; + misc->name = ec_platform->ec_name; + misc->parent = pdev->dev.parent; -dev_set_drvdata (&pdev->dev, data); +dev_set_drvdata (&pdev->dev, misc); -return misc_register (&data->misc); +return misc_register (misc); }static void cros_ec_chardev_remove (struct platform_device *pdev) { -struct chardev_data *data =dev_get_drvdata (&pdev->dev); +struct miscdevice *misc =dev_get_drvdata (&pdev->dev); -misc_deregister (&data->misc); +misc_deregister (misc); }static const struct platform_device_id cros_ec_chardev_id[] = {
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
Tzung-Bi Shih
maintainerPATCH v2 2/6 platform/chrome: cros_ec_chardev: Decouple fops from struct cros_ec_dev
In reply to Tzung-Bi ShihThe fops doesn't really need to hold a reference to struct cros_ec_dev. Remove the references from the fops.
No functional changes.
---
New patch in the v2 series.
drivers/platform/chrome/cros_ec_chardev.c | 44 +++++++++++------------ 1 file changed, 22 insertions(+), 22 deletions(-)
drivers/platform/chrome/cros_ec_chardev.c +22 -22
@@ -32,12 +32,13 @@#define CROS_MAX_EVENT_LEN PAGE_SIZEstruct chardev_priv { -struct cros_ec_dev *ec_dev; +struct cros_ec_device *ec_dev;struct notifier_block notifier;wait_queue_head_t wait_event;unsigned long event_mask;struct list_head events;size_t event_len; +u16 cmd_offset; };struct ec_event {
@@ -47,7 +48,7 @@ struct ec_event {u8 data[]; }; -static int ec_get_version (struct cros_ec_dev *ec,char *str,int maxlen) +static int ec_get_version (struct chardev_priv *priv,char *str,int maxlen) {static const char *const current_image_name[] = {"unknown" ,"read-only" ,"read-write" ,"invalid" ,
@@ -60,10 +61,10 @@ static int ec_get_version(struct cros_ec_dev *ec, char *str, int maxlen)if (!msg)return -ENOMEM ; - msg->command =EC_CMD_GET_VERSION + ec->cmd_offset; + msg->command =EC_CMD_GET_VERSION + priv->cmd_offset; msg->insize =sizeof (*resp); - ret =cros_ec_cmd_xfer_status (ec->ec_dev, msg); + ret =cros_ec_cmd_xfer_status (priv->ec_dev, msg);if (ret <0 ) { snprintf(str, maxlen, "Unknown EC version, returned error: %d\n",
@@ -91,7 +92,7 @@ static int cros_ec_chardev_mkbp_event(struct notifier_block *nb, { struct chardev_priv *priv = container_of(nb, struct chardev_priv, notifier); -struct cros_ec_device *ec_dev = priv->ec_dev->ec_dev; +struct cros_ec_device *ec_dev = priv->ec_dev;struct ec_event *event;unsigned long event_bit =1 << ec_dev->event_data.event_type;int total_size =sizeof (*event) + ec_dev->event_size;
@@ -156,7 +157,8 @@ static struct ec_event *cros_ec_chardev_fetch_event(struct chardev_priv *priv,static int cros_ec_chardev_open (struct inode *inode,struct file *filp) {struct miscdevice *mdev = filp->private_data; -struct cros_ec_dev *ec_dev =dev_get_drvdata (mdev->parent); +struct cros_ec_dev *ec =dev_get_drvdata (mdev->parent); +struct cros_ec_device *ec_dev = ec->ec_dev;struct chardev_priv *priv;int ret;
@@ -165,13 +167,14 @@ static int cros_ec_chardev_open(struct inode *inode, struct file *filp)return -ENOMEM ; priv->ec_dev = ec_dev; + priv->cmd_offset = ec->cmd_offset; filp->private_data = priv;INIT_LIST_HEAD (&priv->events);init_waitqueue_head (&priv->wait_event);nonseekable_open (inode, filp); priv->notifier.notifier_call = cros_ec_chardev_mkbp_event; - ret =blocking_notifier_chain_register (&ec_dev->ec_dev->event_notifier, + ret =blocking_notifier_chain_register (&ec_dev->event_notifier, &priv->notifier);if (ret) {dev_err (ec_dev->dev,"failed to register event notifier\n" );
@@ -199,7 +202,6 @@ static ssize_t cros_ec_chardev_read(struct file *filp, char __user *buffer,char msg[sizeof (struct ec_response_get_version ) +sizeof (CROS_EC_DEV_VERSION )];struct chardev_priv *priv = filp->private_data; -struct cros_ec_dev *ec_dev = priv->ec_dev;size_t count;int ret;
@@ -233,7 +235,7 @@ static ssize_t cros_ec_chardev_read(struct file *filp, char __user *buffer,if (*offset !=0 )return 0 ; - ret =ec_get_version (ec_dev, msg,sizeof (msg)); + ret =ec_get_version (priv, msg,sizeof (msg));if (ret)return ret;
@@ -249,10 +251,10 @@ static ssize_t cros_ec_chardev_read(struct file *filp, char __user *buffer,static int cros_ec_chardev_release (struct inode *inode,struct file *filp) {struct chardev_priv *priv = filp->private_data; -struct cros_ec_dev *ec_dev = priv->ec_dev; +struct cros_ec_device *ec_dev = priv->ec_dev;struct ec_event *event, *e; -blocking_notifier_chain_unregister (&ec_dev->ec_dev->event_notifier, +blocking_notifier_chain_unregister (&ec_dev->event_notifier, &priv->notifier);list_for_each_entry_safe (event, e, &priv->events, node) {
@@ -267,7 +269,7 @@ static int cros_ec_chardev_release(struct inode *inode, struct file *filp)/* * Ioctls */ -static long cros_ec_chardev_ioctl_xcmd (struct cros_ec_dev *ec,void __user *arg) +static long cros_ec_chardev_ioctl_xcmd (struct chardev_priv *priv,void __user *arg) {struct cros_ec_command *s_cmd;struct cros_ec_command u_cmd;
@@ -296,8 +298,8 @@ static long cros_ec_chardev_ioctl_xcmd(struct cros_ec_dev *ec, void __user *arg) goto exit; } - s_cmd->command += ec->cmd_offset; - ret =cros_ec_cmd_xfer (ec->ec_dev, s_cmd); + s_cmd->command += priv->cmd_offset; + ret =cros_ec_cmd_xfer (priv->ec_dev, s_cmd);/* Only copy data to userland if data was received. */ if (ret <0 ) goto exit;
@@ -309,10 +311,9 @@ static long cros_ec_chardev_ioctl_xcmd(struct cros_ec_dev *ec, void __user *arg)return ret; } -static long cros_ec_chardev_ioctl_readmem (struct cros_ec_dev *ec, -void __user *arg) +static long cros_ec_chardev_ioctl_readmem (struct chardev_priv *priv,void __user *arg) { -struct cros_ec_device *ec_dev = ec->ec_dev; +struct cros_ec_device *ec_dev = priv->ec_dev;struct cros_ec_readmem s_mem = { };long num;
@@ -341,16 +342,15 @@ static long cros_ec_chardev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) {struct chardev_priv *priv = filp->private_data; -struct cros_ec_dev *ec = priv->ec_dev;if (_IOC_TYPE (cmd) !=CROS_EC_DEV_IOC )return -ENOTTY ;switch (cmd) {case CROS_EC_DEV_IOCXCMD : -return cros_ec_chardev_ioctl_xcmd (ec, (void __user *)arg); +return cros_ec_chardev_ioctl_xcmd (priv, (void __user *)arg);case CROS_EC_DEV_IOCRDMEM : -return cros_ec_chardev_ioctl_readmem (ec, (void __user *)arg); +return cros_ec_chardev_ioctl_readmem (priv, (void __user *)arg);case CROS_EC_DEV_IOCEVENTMASK : priv->event_mask = arg;return 0 ;
@@ -372,8 +372,8 @@ static const struct file_operations chardev_fops = {static int cros_ec_chardev_probe (struct platform_device *pdev) { -struct cros_ec_dev *ec_dev =dev_get_drvdata (pdev->dev.parent); -struct cros_ec_platform *ec_platform =dev_get_platdata (ec_dev->dev); +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;/* Create a char device: we want to create it anew */
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
Tzung-Bi Shih
maintainerPATCH v2 3/6 platform/chrome: Disallow sending commands through unregistered ec_dev
In reply to Tzung-Bi ShihReturn earlier if attempting to send commands through an unregistered struct cros_ec_device.
---
New patch in the v2 series.
drivers/platform/chrome/cros_ec.c | 6 ++++++ drivers/platform/chrome/cros_ec_proto.c | 6 ++++++ include/linux/platform_data/cros_ec_proto.h | 2 ++ 3 files changed, 14 insertions(+)
drivers/platform/chrome/cros_ec.c +6 -0
@@ -200,6 +200,7 @@ int cros_ec_register(struct cros_ec_device *ec_dev)if (!ec_dev->dout)return -ENOMEM ; + ec_dev->registered = true;lockdep_register_key (&ec_dev->lockdep_key);mutex_init (&ec_dev->lock);lockdep_set_class (&ec_dev->lock, &ec_dev->lockdep_key);
@@ -300,6 +301,7 @@ int cros_ec_register(struct cros_ec_device *ec_dev)return 0 ; exit: + ec_dev->registered = false;platform_device_unregister (ec_dev->ec);platform_device_unregister (ec_dev->pd);mutex_destroy (&ec_dev->lock);
@@ -318,6 +320,10 @@ EXPORT_SYMBOL(cros_ec_register);*/ void cros_ec_unregister (struct cros_ec_device *ec_dev) { +mutex_lock (&ec_dev->lock); + ec_dev->registered = false; +mutex_unlock (&ec_dev->lock); +platform_device_unregister (ec_dev->pd);platform_device_unregister (ec_dev->ec);mutex_destroy (&ec_dev->lock);
drivers/platform/chrome/cros_ec_proto.c +6 -0
@@ -657,6 +657,12 @@ int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)int ret;mutex_lock (&ec_dev->lock); +if (!ec_dev->registered) { +dev_err (ec_dev->dev,"unregistered ec_dev; cannot send command\n" ); +mutex_unlock (&ec_dev->lock); +return -ENODEV ; + } +if (ec_dev->proto_version ==EC_PROTO_VERSION_UNKNOWN ) { ret =cros_ec_query_all (ec_dev);if (ret) {
include/linux/platform_data/cros_ec_proto.h +2 -0
@@ -122,6 +122,7 @@ struct cros_ec_command {* @dout_size: Size of dout buffer to allocate (zero to use static dout). * @wake_enabled: True if this device can wake the system from sleep. * @suspended: True if this device had been suspended. +* @registered: True if this device had been registered. * @cmd_xfer: Send command to EC and get response. * Returns the number of bytes received if the communication * succeeded, but that doesn't mean the EC was happy with the
@@ -180,6 +181,7 @@ struct cros_ec_device {int dout_size;bool wake_enabled;bool suspended; +bool registered;int (*cmd_xfer)(struct cros_ec_device *ec,struct cros_ec_command *msg); int (*pkt_xfer)(struct cros_ec_device *ec,
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
Tzung-Bi Shih
maintainerPATCH v2 4/6 platform/chrome: Introduce cros_ec_device_alloc()
In reply to Tzung-Bi ShihPrepare to decouple the lifecycle of struct cros_ec_device from specific device by introducing a kref.
Replace all occurrences of kzalloc for struct cros_ec_device to a new helper cros_ec_device_alloc() and initialize the kref in the helper.
No functional changes.
---
New patch in the v2 series.
drivers/platform/chrome/cros_ec.c | 7 +++- drivers/platform/chrome/cros_ec_i2c.c | 4 +- drivers/platform/chrome/cros_ec_ishtp.c | 9 +++-- drivers/platform/chrome/cros_ec_lpc.c | 11 +++-- drivers/platform/chrome/cros_ec_proto.c | 45 +++++++++++++++++++++ drivers/platform/chrome/cros_ec_rpmsg.c | 20 +++++---- drivers/platform/chrome/cros_ec_spi.c | 10 +++-- drivers/platform/chrome/cros_ec_uart.c | 17 +++++--- include/linux/platform_data/cros_ec_proto.h | 7 ++++ 9 files changed, 102 insertions(+), 28 deletions(-)
drivers/platform/chrome/cros_ec.c +5 -2
@@ -172,8 +172,10 @@ static int cros_ec_ready_event(struct notifier_block *nb,* cros_ec_register() - Register a new ChromeOS EC, using the provided info. * @ec_dev: Device to register. * -* Before calling this, allocate a pointer to a new device and then fill -* in all the fields up to the --private-- marker. +* Before calling this, allocate a pointer to a new device via +* cros_ec_device_alloc() and then fill in all the required fields. +* +* On success, the ownership of refcount of the ec_dev will be taken. * * Return: 0 on success or negative error code. */
@@ -328,6 +330,7 @@ void cros_ec_unregister(struct cros_ec_device *ec_dev)platform_device_unregister (ec_dev->ec);mutex_destroy (&ec_dev->lock);lockdep_unregister_key (&ec_dev->lockdep_key); +cros_ec_device_put (ec_dev); }EXPORT_SYMBOL (cros_ec_unregister);
drivers/platform/chrome/cros_ec_i2c.c +2 -2
@@ -292,12 +292,11 @@ static int cros_ec_i2c_probe(struct i2c_client *client)struct cros_ec_device *ec_dev =NULL ;int err; - ec_dev =devm_kzalloc (dev,sizeof (*ec_dev),GFP_KERNEL ); + ec_dev =cros_ec_device_alloc (dev);if (!ec_dev)return -ENOMEM ;i2c_set_clientdata (client, ec_dev); - ec_dev->dev = dev; ec_dev->priv = client; ec_dev->irq = client->irq; ec_dev->cmd_xfer = cros_ec_cmd_xfer_i2c;
@@ -311,6 +310,7 @@ static int cros_ec_i2c_probe(struct i2c_client *client) err =cros_ec_register (ec_dev);if (err) {dev_err (dev,"cannot register EC\n" ); +cros_ec_device_put (ec_dev);return err; }
drivers/platform/chrome/cros_ec_ishtp.c +6 -3
@@ -542,15 +542,15 @@ static int cros_ec_dev_init(struct ishtp_cl_data *client_data) {struct cros_ec_device *ec_dev;struct device *dev =cl_data_to_dev (client_data); +int ret; - ec_dev =devm_kzalloc (dev,sizeof (*ec_dev),GFP_KERNEL ); + ec_dev =cros_ec_device_alloc (dev);if (!ec_dev)return -ENOMEM ; client_data->ec_dev = ec_dev; dev->driver_data = ec_dev; - ec_dev->dev = dev; ec_dev->priv = client_data->cros_ish_cl; ec_dev->cmd_xfer =NULL ; ec_dev->pkt_xfer = cros_ec_pkt_xfer_ish;
@@ -559,7 +559,10 @@ static int cros_ec_dev_init(struct ishtp_cl_data *client_data)sizeof (struct ec_response_get_protocol_info ); ec_dev->dout_size =sizeof (struct cros_ish_out_msg ) +sizeof (struct ec_params_rwsig_action ); -return cros_ec_register (ec_dev); + ret =cros_ec_register (ec_dev); +if (ret) +cros_ec_device_put (ec_dev); +return ret; }static void reset_handler (struct work_struct *work)
drivers/platform/chrome/cros_ec_lpc.c +7 -4
@@ -637,12 +637,11 @@ static int cros_ec_lpc_probe(struct platform_device *pdev) } } - ec_dev =devm_kzalloc (dev,sizeof (*ec_dev),GFP_KERNEL ); + ec_dev =cros_ec_device_alloc (dev);if (!ec_dev)return -ENOMEM ;platform_set_drvdata (pdev, ec_dev); - ec_dev->dev = dev; ec_dev->phys_name =dev_name (dev); ec_dev->cmd_xfer = cros_ec_cmd_xfer_lpc; ec_dev->pkt_xfer = cros_ec_pkt_xfer_lpc;
@@ -661,13 +660,14 @@ static int cros_ec_lpc_probe(struct platform_device *pdev) ec_dev->irq = irq; else if (irq != -ENXIO) {dev_err (dev,"couldn't retrieve IRQ number (%d)\n" , irq); -return irq; + ret = irq; + goto err; } ret =cros_ec_register (ec_dev);if (ret) {dev_err (dev,"couldn't register ec_dev (%d)\n" , ret); -return ret; + goto err; }/*
@@ -685,6 +685,9 @@ static int cros_ec_lpc_probe(struct platform_device *pdev) }return 0 ; +err: +cros_ec_device_put (ec_dev); +return ret; }static void cros_ec_lpc_remove (struct platform_device *pdev)
drivers/platform/chrome/cros_ec_proto.c +45 -0
50 lines not on this page · raw message
drivers/platform/chrome/cros_ec_rpmsg.c +13 -7
@@ -216,15 +216,16 @@ static int cros_ec_rpmsg_probe(struct rpmsg_device *rpdev)struct cros_ec_device *ec_dev;int ret; - ec_dev =devm_kzalloc (dev,sizeof (*ec_dev),GFP_KERNEL ); + ec_dev =cros_ec_device_alloc (dev);if (!ec_dev)return -ENOMEM ; ec_rpmsg =devm_kzalloc (dev,sizeof (*ec_rpmsg),GFP_KERNEL ); -if (!ec_rpmsg) -return -ENOMEM ; +if (!ec_rpmsg) { + ret = -ENOMEM ; + goto err; + } - ec_dev->dev = dev; ec_dev->priv = ec_rpmsg; ec_dev->cmd_xfer = cros_ec_cmd_xfer_rpmsg; ec_dev->pkt_xfer = cros_ec_pkt_xfer_rpmsg;
@@ -240,14 +241,16 @@ static int cros_ec_rpmsg_probe(struct rpmsg_device *rpdev) cros_ec_rpmsg_host_event_function); ec_rpmsg->ept =cros_ec_rpmsg_create_ept (rpdev); -if (!ec_rpmsg->ept) -return -ENOMEM ; +if (!ec_rpmsg->ept) { + ret = -ENOMEM ; + goto err; + } ret =cros_ec_register (ec_dev);if (ret <0 ) {rpmsg_destroy_ept (ec_rpmsg->ept);cancel_work_sync (&ec_rpmsg->host_event_work); -return ret; + goto err; } ec_rpmsg->probe_done = true;
@@ -256,6 +259,9 @@ static int cros_ec_rpmsg_probe(struct rpmsg_device *rpdev)schedule_work (&ec_rpmsg->host_event_work);return 0 ; +err: +cros_ec_device_put (ec_dev); +return ret; }static void cros_ec_rpmsg_remove (struct rpmsg_device *rpdev)
drivers/platform/chrome/cros_ec_spi.c +6 -4
@@ -749,7 +749,7 @@ static int cros_ec_spi_probe(struct spi_device *spi)if (ec_spi ==NULL )return -ENOMEM ; ec_spi->spi = spi; - ec_dev =devm_kzalloc (dev,sizeof (*ec_dev),GFP_KERNEL ); + ec_dev =cros_ec_device_alloc (dev);if (!ec_dev)return -ENOMEM ;
@@ -757,7 +757,6 @@ static int cros_ec_spi_probe(struct spi_device *spi)cros_ec_spi_dt_probe (ec_spi, dev);spi_set_drvdata (spi, ec_dev); - ec_dev->dev = dev; ec_dev->priv = ec_spi; ec_dev->irq = spi->irq; ec_dev->cmd_xfer = cros_ec_cmd_xfer_spi;
@@ -772,17 +771,20 @@ static int cros_ec_spi_probe(struct spi_device *spi) err =cros_ec_spi_devm_high_pri_alloc (dev, ec_spi);if (err) -return err; + goto fail; err =cros_ec_register (ec_dev);if (err) {dev_err (dev,"cannot register EC\n" ); -return err; + goto fail; }device_init_wakeup (&spi->dev, true);return 0 ; +fail: +cros_ec_device_put (ec_dev); +return err; }static void cros_ec_spi_remove (struct spi_device *spi)
drivers/platform/chrome/cros_ec_uart.c +11 -6
@@ -259,7 +259,7 @@ static int cros_ec_uart_probe(struct serdev_device *serdev)if (!ec_uart)return -ENOMEM ; - ec_dev =devm_kzalloc (dev,sizeof (*ec_dev),GFP_KERNEL ); + ec_dev =cros_ec_device_alloc (dev);if (!ec_dev)return -ENOMEM ;
@@ -271,12 +271,11 @@ static int cros_ec_uart_probe(struct serdev_device *serdev) ret =cros_ec_uart_acpi_probe (ec_uart);if (ret <0 ) {dev_err (dev,"Failed to get ACPI info (%d)" , ret); -return ret; + goto err; }/* Initialize ec_dev for cros_ec */ ec_dev->phys_name =dev_name (dev); - ec_dev->dev = dev; ec_dev->priv = ec_uart; ec_dev->irq = ec_uart->irq; ec_dev->cmd_xfer =NULL ;
@@ -290,18 +289,24 @@ static int cros_ec_uart_probe(struct serdev_device *serdev) ret =devm_serdev_device_open (dev, serdev);if (ret) {dev_err (dev,"Unable to open UART device" ); -return ret; + goto err; } ret =serdev_device_set_baudrate (serdev, ec_uart->baudrate);if (ret <0 ) {dev_err (dev,"Failed to set up host baud rate (%d)" , ret); -return ret; + goto err; }serdev_device_set_flow_control (serdev, ec_uart->flowcontrol); -return cros_ec_register (ec_dev); + ret =cros_ec_register (ec_dev); +if (ret) + goto err; +return 0 ; +err: +cros_ec_device_put (ec_dev); +return ret; }static void cros_ec_uart_remove (struct serdev_device *serdev)
include/linux/platform_data/cros_ec_proto.h +7 -0
@@ -159,6 +159,7 @@ struct cros_ec_command {* @pd: The platform_device used by the mfd driver to interface with the * PD behind an EC. * @panic_notifier: EC panic notifier. +* @kref: The refcount. */ struct cros_ec_device {/* These are used by other drivers that want to talk to the EC */
@@ -205,6 +206,8 @@ struct cros_ec_device {struct platform_device *pd;struct blocking_notifier_head panic_notifier; + +struct kref kref; };/**
@@ -274,6 +277,10 @@ int cros_ec_cmd_readmem(struct cros_ec_device *ec_dev, u8 offset, u8 size, voidint cros_ec_get_cmd_versions (struct cros_ec_device *ec_dev,u16 cmd); +struct cros_ec_device *cros_ec_device_alloc (struct device *dev); +void cros_ec_device_get (struct cros_ec_device *ec_dev); +void cros_ec_device_put (struct cros_ec_device *ec_dev); +/** * cros_ec_get_time_ns() - Return time in ns. *
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
Tzung-Bi Shih
maintainerPATCH v2 5/6 platform/chrome: cros_ec_chardev: Hold refcount of struct cros_ec_device
In reply to Tzung-Bi ShihHold refcount of struct cros_ec_device to prevent the memory from releasing when the fops still depend on it.
---
New patch in the v2 series.
drivers/platform/chrome/cros_ec_chardev.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/platform/chrome/cros_ec_chardev.c +2 -0
@@ -181,6 +181,7 @@ static int cros_ec_chardev_open(struct inode *inode, struct file *filp)kfree (priv); } +cros_ec_device_get (ec_dev);return ret; }
@@ -256,6 +257,7 @@ static int cros_ec_chardev_release(struct inode *inode, struct file *filp)blocking_notifier_chain_unregister (&ec_dev->event_notifier, &priv->notifier); +cros_ec_device_put (ec_dev);list_for_each_entry_safe (event, e, &priv->events, node) {list_del (&event->node);
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
Tzung-Bi Shih
maintainerPATCH v2 6/6 platform/chrome: Manage struct cros_ec_device lifecycle by its refcount
In reply to Tzung-Bi ShihStop delegating the memory to dev-managed. Use its own refcount for managing the lifecycle.
---
New patch in the v2 series.
drivers/platform/chrome/cros_ec_proto.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/platform/chrome/cros_ec_proto.c +4 -2
@@ -1166,7 +1166,7 @@ EXPORT_SYMBOL_GPL(cros_ec_get_cmd_versions);*/ struct cros_ec_device *cros_ec_device_alloc (struct device *dev) { -struct cros_ec_device *ec_dev =devm_kzalloc (dev,sizeof (*ec_dev),GFP_KERNEL ); +struct cros_ec_device *ec_dev =kzalloc (sizeof (*ec_dev),GFP_KERNEL );if (ec_dev) { ec_dev->dev = dev;
@@ -1190,7 +1190,9 @@ EXPORT_SYMBOL_GPL(cros_ec_device_get);static void cros_ec_device_release (struct kref *kref) { -/* Do nothing as the cros_ec_device is still dev-managed. */ + struct cros_ec_device *ec_dev = container_of(kref, struct cros_ec_device, kref); + +kfree (ec_dev); }/**
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog