From: Alexander Usyskin <alexander.usyskin@intel.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Reuven Abliyev <reuven.abliyev@intel.com>,
Alexander Usyskin <alexander.usyskin@intel.com>,
linux-kernel@vger.kernel.org
Subject: [char-misc-next 3/3] mei: make char device control its own lifetime
Date: Wed, 18 Jun 2025 12:54:33 +0300 [thread overview]
Message-ID: <20250618095433.3943546-4-alexander.usyskin@intel.com> (raw)
In-Reply-To: <20250618095433.3943546-1-alexander.usyskin@intel.com>
Allocate character device dynamically and allow to
control its own lifetime as it may outlive mei_device
structure while character device closes after parent
device is removed from the system.
Signed-off-by: Alexander Usyskin <alexander.usyskin@intel.com>
---
drivers/misc/mei/main.c | 34 +++++++++++++++++++++-------------
drivers/misc/mei/mei_dev.h | 4 ++--
2 files changed, 23 insertions(+), 15 deletions(-)
diff --git a/drivers/misc/mei/main.c b/drivers/misc/mei/main.c
index c160703d0782..2162a8de9e6a 100644
--- a/drivers/misc/mei/main.c
+++ b/drivers/misc/mei/main.c
@@ -51,7 +51,7 @@ static int mei_open(struct inode *inode, struct file *file)
int err;
- dev = mei_device_get(container_of(inode->i_cdev, struct mei_device, cdev));
+ dev = mei_device_get(idr_find(&mei_idr, iminor(inode)));
if (!dev)
return -ENODEV;
@@ -1122,7 +1122,10 @@ void mei_set_devstate(struct mei_device *dev, enum mei_dev_state state)
dev->dev_state = state;
- clsdev = class_find_device_by_devt(&mei_class, dev->cdev.dev);
+ if (!dev->cdev)
+ return;
+
+ clsdev = class_find_device_by_devt(&mei_class, dev->cdev->dev);
if (clsdev) {
sysfs_notify(&clsdev->kobj, NULL, "dev_state");
put_device(clsdev);
@@ -1227,16 +1230,21 @@ int mei_register(struct mei_device *dev, struct device *parent)
/* Fill in the data structures */
devno = MKDEV(MAJOR(mei_devt), dev->minor);
- cdev_init(&dev->cdev, &mei_fops);
- dev->cdev.owner = parent->driver->owner;
- cdev_set_parent(&dev->cdev, &parent->kobj);
+ dev->cdev = cdev_alloc();
+ if (!dev->cdev) {
+ ret = -ENOMEM;
+ goto err;
+ }
+ dev->cdev->ops = &mei_fops;
+ dev->cdev->owner = parent->driver->owner;
+ cdev_set_parent(dev->cdev, &parent->kobj);
/* Add the device */
- ret = cdev_add(&dev->cdev, devno, 1);
+ ret = cdev_add(dev->cdev, devno, 1);
if (ret) {
dev_err(parent, "unable to add device %d:%d\n",
MAJOR(mei_devt), dev->minor);
- goto err_dev_add;
+ goto err_del_cdev;
}
clsdev = device_create_with_groups(&mei_class, parent, devno,
@@ -1247,16 +1255,16 @@ int mei_register(struct mei_device *dev, struct device *parent)
dev_err(parent, "unable to create device %d:%d\n",
MAJOR(mei_devt), dev->minor);
ret = PTR_ERR(clsdev);
- goto err_dev_create;
+ goto err_del_cdev;
}
mei_dbgfs_register(dev, dev_name(clsdev));
return 0;
-err_dev_create:
- cdev_del(&dev->cdev);
-err_dev_add:
+err_del_cdev:
+ cdev_del(dev->cdev);
+err:
mei_minor_free(dev);
return ret;
}
@@ -1266,8 +1274,8 @@ void mei_deregister(struct mei_device *dev)
{
int devno;
- devno = dev->cdev.dev;
- cdev_del(&dev->cdev);
+ devno = dev->cdev->dev;
+ cdev_del(dev->cdev);
mei_dbgfs_deregister(dev);
diff --git a/drivers/misc/mei/mei_dev.h b/drivers/misc/mei/mei_dev.h
index b745cde80662..8f081c6735c4 100644
--- a/drivers/misc/mei/mei_dev.h
+++ b/drivers/misc/mei/mei_dev.h
@@ -471,7 +471,7 @@ struct mei_dev_timeouts {
* struct mei_device - MEI private device struct
*
* @dev : device on a bus
- * @cdev : character device
+ * @cdev : character device pointer
* @minor : minor number allocated for device
*
* @refcnt : struct reference count
@@ -559,7 +559,7 @@ struct mei_dev_timeouts {
*/
struct mei_device {
struct device *dev;
- struct cdev cdev;
+ struct cdev *cdev;
int minor;
struct kref refcnt;
--
2.43.0
prev parent reply other threads:[~2025-06-18 10:09 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-18 9:54 [char-misc-next 0/3] mei: fix mei_device lifetime Alexander Usyskin
2025-06-18 9:54 ` [char-misc-next 1/3] mei: refcount mei_device Alexander Usyskin
2025-06-21 9:07 ` Greg Kroah-Hartman
2025-06-22 9:14 ` Usyskin, Alexander
2025-06-22 9:21 ` Greg Kroah-Hartman
2025-06-18 9:54 ` [char-misc-next 2/3] mei: set parent for char device Alexander Usyskin
2025-06-18 9:54 ` Alexander Usyskin [this message]
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=20250618095433.3943546-4-alexander.usyskin@intel.com \
--to=alexander.usyskin@intel.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=reuven.abliyev@intel.com \
/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
all inboxes | Powered by JetHome®