mei: fix mei_device lifetime
Subsystems (2)
Alexander Usyskin
maintainerv2 cover mei: fix mei_device lifetime
mei_device data structure is allocated using devm_* functions and hooked to the parent device.
It works for client systems where parent device is pci one. When parent device is auxiliary bus device produced by graphics driver, the parent can be destroyed before child and on the way it cleans all allocated memory.
This leads to use-after-free if mei character device is opened when parent device is destroyed.
Introduce device to hook the mei_device lifetime on it and decouple cdev from main structure to allow all memory to be released when it is unused and not before.
V2: Make cdev patches first to avoid double refcounting
in the middle of the series.
Replace manual refcount with special device.
Alexander Usyskin (5):
mei: set parent for char device
mei: make char device control its own lifetime
mei: bus: use cldev in prints
mei: more prints with client prefix
mei: create dedicated device object
drivers/misc/mei/bus-fixup.c | 16 ++-- drivers/misc/mei/bus.c | 42 +++++----- drivers/misc/mei/client.c | 82 +++++++++---------- drivers/misc/mei/client.h | 6 +- drivers/misc/mei/dma-ring.c | 8 +- drivers/misc/mei/gsc-me.c | 13 +-- drivers/misc/mei/hbm.c | 135 +++++++++++++++----------------- drivers/misc/mei/hw-me.c | 101 ++++++++++++------------ drivers/misc/mei/hw-txe.c | 62 +++++++-------- drivers/misc/mei/init.c | 85 ++++++++++++++------ drivers/misc/mei/interrupt.c | 47 ++++++----- drivers/misc/mei/main.c | 99 ++++++++++++----------- drivers/misc/mei/mei_dev.h | 15 ++-- drivers/misc/mei/pci-me.c | 12 ++- drivers/misc/mei/pci-txe.c | 10 ++- drivers/misc/mei/platform-vsc.c | 18 +++-- 16 files changed, 404 insertions(+), 347 deletions(-)
Signature · 1 line
2.43.0
Alexander Usyskin
maintainerPATCH v2 1/5 mei: set parent for char device
Connect char device to parent device to avoid
parent device unload while char device is
still held open by user-space.
--- drivers/misc/mei/main.c | 1 + 1 file changed, 1 insertion(+)
drivers/misc/mei/main.c +1 -0
@@ -1225,6 +1225,7 @@ int mei_register(struct mei_device *dev, struct device *parent) 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);/* Add the device */ ret =cdev_add (&dev->cdev, devno,1 );
Signature · 1 line
2.43.0
Greg Kroah-Hartman
maintainerRe: [char-misc-next v2 1/5] mei: set parent for char device
Quoted 3 lines · Alexander Usyskin
Connect char device to parent device to avoid
parent device unload while char device is
still held open by user-space.
No, that's not what cdev_set_parent() does.
Also, why not wrap your lines at 72 columns?
thanks,
greg k-h
Usyskin, Alexander
maintainerRE: [char-misc-next v2 1/5] mei: set parent for char device
Quoted 9 lines
Subject: Re: [char-misc-next v2 1/5] mei: set parent for char device
Quoted 3 lines · Alexander Usyskin
Connect char device to parent device to avoid
parent device unload while char device is
still held open by user-space.No, that's not what
cdev_set_parent()does.
https://elixir.bootlin.com/linux/v6.16-rc3/source/fs/char_dev.c#L511
* cdev_set_parent() sets a parent kobject which will be referenced
* appropriately so the parent is not freed before the cdev. This
* should be called before cdev_add.
This is what written there. Is my interpretation wrong?
Quoted 1 line
Also, why not wrap your lines at 72 columns?
Ops, missed this while preparing series, will fix.
Quoted 4 lines
thanks,
greg k-h
- -
Thanks,
Sasha
Greg Kroah-Hartman
maintainerRe: [char-misc-next v2 1/5] mei: set parent for char device
Quoted 7 lines · Alexander
Quoted 7 lines
Subject: Re: [char-misc-next v2 1/5] mei: set parent for char device
Quoted 3 lines · Alexander Usyskin
Connect char device to parent device to avoid
parent device unload while char device is
still held open by user-space.
Quoted 8 lines
Quoted 2 lines
No, that's not what
cdev_set_parent()does.https://elixir.bootlin.com/linux/v6.16-rc3/source/fs/char_dev.c#L511
*cdev_set_parent()sets a parent kobject which will be referenced
* appropriately so the parent is not freed before the cdev. This
* should be called beforecdev_add.This is what written there. Is my interpretation wrong?
It has nothing to do with a char device being open or closed from userspace, it has to deal with properly placing the device in the kobject tree and the object references being correct.
Surely the mei device had this set up before, right? If not, how did sysfs look with these char devices with no real parent?
thanks,
greg k-h
Usyskin, Alexander
maintainerRE: [char-misc-next v2 1/5] mei: set parent for char device
Quoted 17 lines
Subject: Re: [char-misc-next v2 1/5] mei: set parent for char device
Quoted 14 lines · Alexander
Quoted 9 lines
Subject: Re: [char-misc-next v2 1/5] mei: set parent for char device
Quoted 3 lines · Alexander Usyskin
Connect char device to parent device to avoid
parent device unload while char device is
still held open by user-space.No, that's not what
cdev_set_parent()does.https://elixir.bootlin.com/linux/v6.16-rc3/source/fs/char_dev.c#L511
*cdev_set_parent()sets a parent kobject which will be referenced
* appropriately so the parent is not freed before the cdev. This
* should be called beforecdev_add.
Quoted 9 lines
Quoted 1 line
This is what written there. Is my interpretation wrong?
It has nothing to do with a char device being open or closed from userspace, it has to deal with properly placing the device in the kobject tree and the object references being correct.
Surely the mei device had this set up before, right? If not, how did sysfs look with these char devices with no real parent?
Mei device never had this setup.
I've looked at other drivers - some of them do not have this setup.
- -
Thanks,
Sasha
Quoted 3 lines
thanks,
greg k-h
Greg Kroah-Hartman
maintainerRe: [char-misc-next v2 1/5] mei: set parent for char device
Quoted 23 lines · Alexander
Quoted 23 lines
Subject: Re: [char-misc-next v2 1/5] mei: set parent for char device
Quoted 7 lines · Alexander
Quoted 7 lines
Subject: Re: [char-misc-next v2 1/5] mei: set parent for char device
Quoted 3 lines · Alexander Usyskin
Connect char device to parent device to avoid
parent device unload while char device is
still held open by user-space.Quoted 8 lines
Quoted 2 lines
No, that's not what
cdev_set_parent()does.https://elixir.bootlin.com/linux/v6.16-rc3/source/fs/char_dev.c#L511
*cdev_set_parent()sets a parent kobject which will be referenced
* appropriately so the parent is not freed before the cdev. This
* should be called beforecdev_add.This is what written there. Is my interpretation wrong?
It has nothing to do with a char device being open or closed from userspace, it has to deal with properly placing the device in the kobject tree and the object references being correct.
Quoted 5 lines
Quoted 3 lines
Surely the mei device had this set up before, right? If not, how did sysfs look with these char devices with no real parent?
Mei device never had this setup.
So what is the "parent" of a mei device today?
Quoted 1 line
I've looked at other drivers - some of them do not have this setup.
Then they are broken, don't copy them :)
thanks,
greg k-h
Usyskin, Alexander
maintainerRE: [char-misc-next v2 1/5] mei: set parent for char device
Quoted 27 lines
Subject: Re: [char-misc-next v2 1/5] mei: set parent for char device
Quoted 6 lines · Alexander
Quoted 6 lines
Subject: Re: [char-misc-next v2 1/5] mei: set parent for char device
Quoted 3 lines · Alexander
Quoted 3 lines
Subject: Re: [char-misc-next v2 1/5] mei: set parent for char device
On Mon, Jun 30, 2025 at 12:19:38PM +0300, Alexander Usyskin
Quoted 17 lines · wrote:
Quoted 17 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Connect char device to parent device to avoid
parent device unload while char device is
still held open by user-space.Quoted 8 lines
Quoted 2 lines
No, that's not what
cdev_set_parent()does.https://elixir.bootlin.com/linux/v6.16-rc3/source/fs/char_dev.c#L511
*cdev_set_parent()sets a parent kobject which will be referenced
* appropriately so the parent is not freed before the cdev. This
* should be called beforecdev_add.This is what written there. Is my interpretation wrong?
It has nothing to do with a char device being open or closed from userspace, it has to deal with properly placing the device in the kobject tree and the object references being correct.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Surely the mei device had this set up before, right? If not, how did sysfs look with these char devices with no real parent?
Mei device never had this setup.
So what is the "parent" of a mei device today?
PCI device in CSME case and auxiliary one for discrete graphics.
Quoted 3 lines
Quoted 1 line
I've looked at other drivers - some of them do not have this setup.
Then they are broken, don't copy them :)
Yep, I'm trying to fix this mess.
I've found a way to use existing mei class device to anchor the mei_device. With this we can use mei class device as parent for the cdev. Pushed the series (v3), looking for reactions whatever this is the right way.
- -
Thanks,
Sasha
Alexander Usyskin
maintainerPATCH v2 2/5 mei: make char device control its own lifetime
In reply to Alexander UsyskinAllocate 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.
--- drivers/misc/mei/main.c | 36 +++++++++++++++++++++++------------- drivers/misc/mei/mei_dev.h | 4 ++-- 2 files changed, 25 insertions(+), 15 deletions(-)
drivers/misc/mei/main.c +23 -13
@@ -51,7 +51,9 @@ static int mei_open(struct inode *inode, struct file *file)int err; - dev = container_of(inode->i_cdev, struct mei_device, cdev); + dev =idr_find (&mei_idr,iminor (inode)); +if (!dev) +return -ENODEV ;mutex_lock (&dev->device_lock);
@@ -1118,7 +1120,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);
@@ -1223,16 +1228,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,
@@ -1243,16 +1253,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; }
@@ -1262,8 +1272,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);
drivers/misc/mei/mei_dev.h +2 -2
@@ -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 * * @write_list : write pending list
@@ -557,7 +557,7 @@ struct mei_dev_timeouts {*/ struct mei_device {struct device *dev; -struct cdev cdev; +struct cdev *cdev;int minor;struct list_head write_list;
Signature · 1 line
2.43.0
Greg Kroah-Hartman
maintainerRe: [char-misc-next v2 2/5] mei: make char device control its own lifetime
Quoted 27 lines · Alexander Usyskin
Allocate character device dynamically and allow to control its own lifetime as it may outlive
mei_devicestructure while character device closes after parent device is removed from the system.Signed-off-by Alexander Usyskin <[email protected]>--- drivers/misc/mei/main.c | 36 +++++++++++++++++++++++------------- drivers/misc/mei/mei_dev.h | 4 ++-- 2 files changed, 25 insertions(+), 15 deletions(-)drivers/misc/mei/main.c +3
-1@@ -51,7 +51,9 @@ static int mei_open(struct inode *inode, struct file *file)int err; - dev = container_of(inode->i_cdev, struct mei_device, cdev); + dev =idr_find (&mei_idr,iminor (inode)); +if (!dev) +return -ENODEV ;mutex_lock (&dev->device_lock);
drivers/misc/mei/main.c void mei_set_devstate(struct mei_device *dev, enum mei_dev_state state) · line 1123
Quoted 5 lines
drivers/misc/mei/main.c +1
-1@@ -1118,7 +1120,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)
How can this happen?
drivers/misc/mei/main.c void mei_set_devstate(struct mei_device *dev, enum mei_dev_state state) · line 1124
Quoted 1 line
drivers/misc/mei/main.c +1
-0+return ;
No error reported?
Quoted 14 lines
drivers/misc/mei/main.c +4
-3+ + clsdev =class_find_device_by_devt (&mei_class, dev->cdev->dev);if (clsdev) {sysfs_notify (&clsdev->kobj,NULL ,"dev_state" );put_device (clsdev);@@ -1223,16 +1228,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) {
drivers/misc/mei/main.c int mei_register(struct mei_device *dev, struct device *parent) · line 1241
Quoted 10 lines
drivers/misc/mei/main.c +7
-1+ 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 );
Shouldn't this be cdev_device_add()? If not, who frees the cdev when it is done?
drivers/misc/mei/main.c int mei_register(struct mei_device *dev, struct device *parent) · line 1245
Quoted 5 lines
drivers/misc/mei/main.c +1
-1if (ret) {dev_err (parent,"unable to add device %d:%d\n" ,MAJOR (mei_devt), dev->minor); - goto err_dev_add; + goto err_del_cdev;
The error name was correct, why change it?
Quoted 25 lines
drivers/misc/mei/main.c +4
-4} clsdev = device_create_with_groups(&mei_class, parent, devno,@@ -1243,16 +1253,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; }
drivers/misc/mei/main.c void mei_deregister(struct mei_device *dev) · line 1276
Quoted 7 lines
drivers/misc/mei/main.c +2
-2@@ -1262,8 +1272,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);
Was this tested? What happens when cdev_del() is called like this? Is the memory really freed?
Quoted 17 lines
drivers/misc/mei/main.c +0
-0mei_dbgfs_deregister (dev);drivers/misc/mei/mei_dev.h +1
-1@@ -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 * * @write_list : write pending list
drivers/misc/mei/mei_dev.h struct mei_dev_timeouts { · line 560
Quoted 5 lines
drivers/misc/mei/mei_dev.h +1
-1@@ -557,7 +557,7 @@ struct mei_dev_timeouts {*/ struct mei_device {struct device *dev; -struct cdev cdev; +struct cdev *cdev;
So now nothing owns the lifetime of the mei_device object? Do things still work at this point in time in the patch series?
thanks,
greg k-h
Alexander Usyskin
maintainerPATCH v2 3/5 mei: bus: use cldev in prints
In reply to Alexander UsyskinFor unifomity, print using client device on bus
where possible.
--- drivers/misc/mei/bus-fixup.c | 10 +++++----- drivers/misc/mei/bus.c | 18 +++++++++--------- 2 files changed, 14 insertions(+), 14 deletions(-)
drivers/misc/mei/bus-fixup.c +5 -5
@@ -463,14 +463,14 @@ static void mei_nfc(struct mei_cl_device *cldev)if (IS_ERR (cl)) { ret =PTR_ERR (cl); cl =NULL ; -dev_err (bus->dev,"nfc hook alloc failed %d\n" , ret); +dev_err (&cldev->dev,"nfc hook alloc failed %d\n" , ret); goto out; } me_cl =mei_me_cl_by_uuid (bus, &mei_nfc_info_guid);if (!me_cl) { ret = -ENOTTY ; -dev_err (bus->dev,"Cannot find nfc info %d\n" , ret); +dev_err (&cldev->dev,"Cannot find nfc info %d\n" , ret); goto out; }
@@ -496,13 +496,13 @@ static void mei_nfc(struct mei_cl_device *cldev) goto disconnect; } -dev_dbg (bus->dev,"nfc radio %s\n" , radio_name); +dev_dbg (&cldev->dev,"nfc radio %s\n" , radio_name);strscpy (cldev->name, radio_name,sizeof (cldev->name)); disconnect:mutex_lock (&bus->device_lock);if (mei_cl_disconnect (cl) <0 ) -dev_err (bus->dev,"Can't disconnect the NFC INFO ME\n" ); +dev_err (&cldev->dev,"Can't disconnect the NFC INFO ME\n" );mei_cl_flush_queues (cl,NULL );
@@ -515,7 +515,7 @@ static void mei_nfc(struct mei_cl_device *cldev)if (ret) cldev->do_match =0 ; -dev_dbg (bus->dev,"end of fixup match = %d\n" , cldev->do_match); +dev_dbg (&cldev->dev,"end of fixup match = %d\n" , cldev->do_match); }/**
drivers/misc/mei/bus.c +9 -9
@@ -875,14 +875,14 @@ int mei_cldev_disable(struct mei_cl_device *cldev)mei_cl_bus_vtag_free (cldev);if (!mei_cl_is_connected (cl)) { -dev_dbg (bus->dev,"Already disconnected\n" ); +dev_dbg (&cldev->dev,"Already disconnected\n" ); err =0 ; goto out; } err =mei_cl_disconnect (cl);if (err <0 ) -dev_err (bus->dev,"Could not disconnect from the ME client\n" ); +dev_err (&cldev->dev,"Could not disconnect from the ME client\n" ); out:/* Flush queues and remove any pending read unless we have mapped DMA */
@@ -935,7 +935,7 @@ ssize_t mei_cldev_send_gsc_command(struct mei_cl_device *cldev, cl = cldev->cl; bus = cldev->bus; -dev_dbg (bus->dev,"client_id %u, fence_id %u\n" , client_id, fence_id); +dev_dbg (&cldev->dev,"client_id %u, fence_id %u\n" , client_id, fence_id);if (!bus->hbm_f_gsc_supported)return -EOPNOTSUPP ;
@@ -983,11 +983,11 @@ ssize_t mei_cldev_send_gsc_command(struct mei_cl_device *cldev,/* send the message to GSC */ ret =__mei_cl_send (cl, (u8 *)ext_hdr, buf_sz,0 ,MEI_CL_IO_SGL );if (ret <0 ) { -dev_err (bus->dev,"__mei_cl_send failed, returned %zd\n" , ret); +dev_err (&cldev->dev,"__mei_cl_send failed, returned %zd\n" , ret); goto end; }if (ret != buf_sz) { -dev_err (bus->dev,"__mei_cl_send returned %zd instead of expected %zd\n" , +dev_err (&cldev->dev,"__mei_cl_send returned %zd instead of expected %zd\n" , ret, buf_sz); ret = -EIO ; goto end;
@@ -997,7 +997,7 @@ ssize_t mei_cldev_send_gsc_command(struct mei_cl_device *cldev, ret =__mei_cl_recv (cl, (u8 *)&rx_msg,sizeof (rx_msg),NULL ,MEI_CL_IO_SGL ,0 );if (ret !=sizeof (rx_msg)) { -dev_err (bus->dev,"__mei_cl_recv returned %zd instead of expected %zd\n" , +dev_err (&cldev->dev,"__mei_cl_recv returned %zd instead of expected %zd\n" , ret,sizeof (rx_msg));if (ret >=0 ) ret = -EIO ;
@@ -1006,13 +1006,13 @@ ssize_t mei_cldev_send_gsc_command(struct mei_cl_device *cldev,/* check rx_msg.client_id and rx_msg.fence_id match the ones we send */ if (rx_msg.client_id != client_id || rx_msg.fence_id != fence_id) { -dev_err (bus->dev,"received client_id/fence_id %u/%u instead of %u/%u sent\n" , +dev_err (&cldev->dev,"received client_id/fence_id %u/%u instead of %u/%u sent\n" , rx_msg.client_id, rx_msg.fence_id, client_id, fence_id); ret = -EFAULT ; goto end; } -dev_dbg (bus->dev,"gsc command: successfully written %u bytes\n" , rx_msg.written); +dev_dbg (&cldev->dev,"gsc command: successfully written %u bytes\n" , rx_msg.written); ret = rx_msg.written; end:
@@ -1399,7 +1399,7 @@ static int mei_cl_bus_dev_add(struct mei_cl_device *cldev) {int ret; -dev_dbg (cldev->bus->dev,"adding %pUL:%02X\n" , +dev_dbg (&cldev->dev,"adding %pUL:%02X\n" ,mei_me_cl_uuid (cldev->me_cl),mei_me_cl_ver (cldev->me_cl)); ret =device_add (&cldev->dev);
Signature · 1 line
2.43.0
Alexander Usyskin
maintainerPATCH v2 4/5 mei: more prints with client prefix
In reply to Alexander UsyskinUse client-aware print macro instead of
usual device print in more places.
--- drivers/misc/mei/hbm.c | 14 ++++----- drivers/misc/mei/interrupt.c | 2 +- drivers/misc/mei/main.c | 55 +++++++++++++++++------------------- 3 files changed, 34 insertions(+), 37 deletions(-)
drivers/misc/mei/hbm.c +7 -7
@@ -510,7 +510,7 @@ int mei_hbm_cl_notify_req(struct mei_device *dev, ret =mei_hbm_write_message (dev, &mei_hdr, &req);if (ret) -dev_err (dev->dev,"notify request failed: ret = %d\n" , ret); +cl_err (dev, cl,"notify request failed: ret = %d\n" , ret);return ret; }
@@ -626,7 +626,7 @@ int mei_hbm_cl_dma_map_req(struct mei_device *dev, struct mei_cl *cl) ret =mei_hbm_write_message (dev, &mei_hdr, &req);if (ret) -dev_err (dev->dev,"dma map request failed: ret = %d\n" , ret); +cl_err (dev, cl,"dma map request failed: ret = %d\n" , ret);return ret; }
@@ -654,7 +654,7 @@ int mei_hbm_cl_dma_unmap_req(struct mei_device *dev, struct mei_cl *cl) ret =mei_hbm_write_message (dev, &mei_hdr, &req);if (ret) -dev_err (dev->dev,"dma unmap request failed: ret = %d\n" , ret); +cl_err (dev, cl,"dma unmap request failed: ret = %d\n" , ret);return ret; }
@@ -679,10 +679,10 @@ static void mei_hbm_cl_dma_map_res(struct mei_device *dev,return ;if (res->status) { -dev_err (dev->dev,"cl dma map failed %d\n" , res->status); +cl_err (dev, cl,"cl dma map failed %d\n" , res->status); cl->status = -EFAULT ; }else { -dev_dbg (dev->dev,"cl dma map succeeded\n" ); +cl_dbg (dev, cl,"cl dma map succeeded\n" ); cl->dma_mapped =1 ; cl->status =0 ; }
@@ -709,10 +709,10 @@ static void mei_hbm_cl_dma_unmap_res(struct mei_device *dev,return ;if (res->status) { -dev_err (dev->dev,"cl dma unmap failed %d\n" , res->status); +cl_err (dev, cl,"cl dma unmap failed %d\n" , res->status); cl->status = -EFAULT ; }else { -dev_dbg (dev->dev,"cl dma unmap succeeded\n" ); +cl_dbg (dev, cl,"cl dma unmap succeeded\n" ); cl->dma_mapped =0 ; cl->status =0 ; }
drivers/misc/mei/interrupt.c +1 -1
@@ -35,7 +35,7 @@ void mei_irq_compl_handler(struct mei_device *dev, struct list_head *cmpl_list) cl = cb->cl;list_del_init (&cb->list); -dev_dbg (dev->dev,"completing call back.\n" ); +cl_dbg (dev, cl,"completing call back.\n" );mei_cl_complete (cl, cb); } }
drivers/misc/mei/main.c +26 -29
175 lines not on this page · raw message
Signature · 1 line
2.43.0
Greg Kroah-Hartman
maintainerRe: [char-misc-next v2 4/5] mei: more prints with client prefix
Quoted 2 lines · Alexander Usyskin
Use client-aware print macro instead of
usual device print in more places.
Why? This changes the output of the driver.
Quoted 13 lines
Signed-off-by Alexander Usyskin <[email protected]>--- drivers/misc/mei/hbm.c | 14 ++++----- drivers/misc/mei/interrupt.c | 2 +- drivers/misc/mei/main.c | 55 +++++++++++++++++------------------- 3 files changed, 34 insertions(+), 37 deletions(-)drivers/misc/mei/hbm.c +0
-0
drivers/misc/mei/hbm.c int mei_hbm_cl_notify_req(struct mei_device *dev, · line 513
Quoted 5 lines
drivers/misc/mei/hbm.c +1
-1@@ -510,7 +510,7 @@ int mei_hbm_cl_notify_req(struct mei_device *dev, ret =mei_hbm_write_message (dev, &mei_hdr, &req);if (ret) -dev_err (dev->dev,"notify request failed: ret = %d\n" , ret); +cl_err (dev, cl,"notify request failed: ret = %d\n" , ret);
Why do you have a mei-specific debug printk macro anyway? You really shouldn't do that :(
This feels like a step backwards.
thanks,
greg k-h
Usyskin, Alexander
maintainerRE: [char-misc-next v2 4/5] mei: more prints with client prefix
Quoted 8 lines
Subject: Re: [char-misc-next v2 4/5] mei: more prints with client prefix
Quoted 2 lines · Alexander Usyskin
Use client-aware print macro instead of
usual device print in more places.Why? This changes the output of the driver.
This adds prefix to the existing outputs, below I've added more explanations.
Quoted 22 lines
Quoted 13 lines
Signed-off-by Alexander Usyskin <[email protected]>--- drivers/misc/mei/hbm.c | 14 ++++----- drivers/misc/mei/interrupt.c | 2 +- drivers/misc/mei/main.c | 55 +++++++++++++++++------------------- 3 files changed, 34 insertions(+), 37 deletions(-)drivers/misc/mei/hbm.c +0
-0drivers/misc/mei/hbm.c int mei_hbm_cl_notify_req(struct mei_device *dev, · line 513
Quoted 5 lines
drivers/misc/mei/hbm.c +1
-1@@ -510,7 +510,7 @@ int mei_hbm_cl_notify_req(struct mei_device *dev, ret =mei_hbm_write_message (dev, &mei_hdr, &req);if (ret) -dev_err (dev->dev,"notify request failed: ret = %d\n" , ret); +cl_err (dev, cl,"notify request failed: ret = %d\n" , ret);Why do you have a mei-specific debug printk macro anyway? You really shouldn't do that :(
Quoted 2 lines
This feels like a step backwards.
cl_err is the wrapper for dev_err with additional prefix of current transaction. This helps to understand to what flow this print belongs when multiple transactions are executed in parallel.
Same for cl_dbg.
This greatly increases driver debuggability in complex scenarios. Some of these prints are introduced before specific macro was invented, some are misses from the newer patches.
This patch helps to normalize output and makes consecutive patches smaller.
Quoted 3 lines
thanks,
greg k-h
- -
Thanks,
Sasha
Alexander Usyskin
maintainerPATCH v2 5/5 mei: create dedicated device object
In reply to Alexander Usyskinmei_device lifetime is managed by devm procedure of parent device. But such memory is freed on device_del.Mei_device object is used by client object that may be alive after parent device is removed.
It may lead to use-after-free if discrete graphics driver unloads mei_gsc auxiliary device while user-space holds open handle to mei character device.
Add dedicated device object to control driver
private data lifetime.
Rename exising parent device pointer from
dev to parent to avoid misuse.
Leave power management on parent device as
user-space is expecting it there.
--- drivers/misc/mei/bus-fixup.c | 6 +- drivers/misc/mei/bus.c | 24 ++++--- drivers/misc/mei/client.c | 82 +++++++++++----------- drivers/misc/mei/client.h | 6 +- drivers/misc/mei/dma-ring.c | 8 +-- drivers/misc/mei/gsc-me.c | 13 ++-- drivers/misc/mei/hbm.c | 121 +++++++++++++++----------------- drivers/misc/mei/hw-me.c | 101 +++++++++++++------------- drivers/misc/mei/hw-txe.c | 62 ++++++++-------- drivers/misc/mei/init.c | 85 +++++++++++++++------- drivers/misc/mei/interrupt.c | 45 ++++++------ drivers/misc/mei/main.c | 9 ++- drivers/misc/mei/mei_dev.h | 11 +-- drivers/misc/mei/pci-me.c | 12 ++-- drivers/misc/mei/pci-txe.c | 10 ++- drivers/misc/mei/platform-vsc.c | 18 ++--- 16 files changed, 331 insertions(+), 282 deletions(-)
drivers/misc/mei/bus-fixup.c +3 -3
@@ -386,7 +386,7 @@ static int mei_nfc_if_version(struct mei_cl *cl, ret =__mei_cl_send (cl, (u8 *)&cmd,sizeof (cmd),0 ,MEI_CL_IO_TX_BLOCKING );if (ret <0 ) { -dev_err (bus->dev,"Could not send IF version cmd ret = %d\n" , ret); +dev_err (&bus->dev,"Could not send IF version cmd ret = %d\n" , ret);return ret; }
@@ -401,14 +401,14 @@ static int mei_nfc_if_version(struct mei_cl *cl, bytes_recv =__mei_cl_recv (cl, (u8 *)reply, if_version_length, &vtag,0 ,0 );if (bytes_recv <0 || (size_t )bytes_recv < if_version_length) { -dev_err (bus->dev,"Could not read IF version ret = %d\n" , bytes_recv); +dev_err (&bus->dev,"Could not read IF version ret = %d\n" , bytes_recv); ret = -EIO ; goto err; }memcpy (ver, reply->data,sizeof (*ver)); -dev_info (bus->dev,"NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n" , +dev_info (&bus->dev,"NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n" , ver->fw_ivn, ver->vendor_id, ver->radio_type); err:
drivers/misc/mei/bus.c +14 -10
@@ -637,7 +637,7 @@ EXPORT_SYMBOL_GPL(mei_cldev_enabled);*/ static bool mei_cl_bus_module_get (struct mei_cl_device *cldev) { -return try_module_get (cldev->bus->dev->driver->owner); +return try_module_get (cldev->bus->parent->driver->owner); }/**
@@ -647,7 +647,7 @@ static bool mei_cl_bus_module_get(struct mei_cl_device *cldev)*/ static void mei_cl_bus_module_put (struct mei_cl_device *cldev) { -module_put (cldev->bus->dev->driver->owner); +module_put (cldev->bus->parent->driver->owner); }/**
@@ -1286,16 +1286,20 @@ static const struct bus_type mei_cl_bus_type = {static struct mei_device *mei_dev_bus_get (struct mei_device *bus) { -if (bus) -get_device (bus->dev); +if (bus) { +get_device (&bus->dev); +get_device (bus->parent); + }return bus; }static void mei_dev_bus_put (struct mei_device *bus) { -if (bus) -put_device (bus->dev); +if (bus) { +put_device (bus->parent); +put_device (&bus->dev); + } }static void mei_cl_bus_dev_release (struct device *dev)
@@ -1323,7 +1327,7 @@ static const struct device_type mei_cl_device_type = {static inline void mei_cl_bus_set_name (struct mei_cl_device *cldev) {dev_set_name (&cldev->dev,"%s-%pUl" , -dev_name (cldev->bus->dev), +dev_name (cldev->bus->parent),mei_me_cl_uuid (cldev->me_cl)); }
@@ -1352,7 +1356,7 @@ static struct mei_cl_device *mei_cl_bus_dev_alloc(struct mei_device *bus, }device_initialize (&cldev->dev); - cldev->dev.parent = bus->dev; + cldev->dev.parent = bus->parent; cldev->dev.bus = &mei_cl_bus_type; cldev->dev.type = &mei_cl_device_type; cldev->bus =mei_dev_bus_get (bus);
@@ -1487,7 +1491,7 @@ static void mei_cl_bus_dev_init(struct mei_device *bus,WARN_ON (!mutex_is_locked (&bus->cl_bus_lock)); -dev_dbg (bus->dev,"initializing %pUl" ,mei_me_cl_uuid (me_cl)); +dev_dbg (&bus->dev,"initializing %pUl" ,mei_me_cl_uuid (me_cl));if (me_cl->bus_added)return ;
@@ -1538,7 +1542,7 @@ static void mei_cl_bus_rescan(struct mei_device *bus) }mutex_unlock (&bus->cl_bus_lock); -dev_dbg (bus->dev,"rescan end" ); +dev_dbg (&bus->dev,"rescan end" ); }void mei_cl_bus_rescan_work (struct work_struct *work)
drivers/misc/mei/client.c +41 -41
229 lines not on this page · raw message
drivers/misc/mei/client.h +3 -3
@@ -275,12 +275,12 @@ int mei_cl_dma_unmap(struct mei_cl *cl, const struct file *fp);#define MEI_CL_PRM (cl) (cl)->host_client_id, mei_cl_me_id(cl) #define cl_dbg(dev, cl, format, arg...) \ - dev_dbg((dev)->dev, MEI_CL_FMT format, MEI_CL_PRM(cl), ##arg) + dev_dbg(&(dev)->dev, MEI_CL_FMT format, MEI_CL_PRM(cl), ##arg) #define cl_warn(dev, cl, format, arg...) \ - dev_warn((dev)->dev, MEI_CL_FMT format, MEI_CL_PRM(cl), ##arg) + dev_warn(&(dev)->dev, MEI_CL_FMT format, MEI_CL_PRM(cl), ##arg) #define cl_err(dev, cl, format, arg...) \ - dev_err((dev)->dev, MEI_CL_FMT format, MEI_CL_PRM(cl), ##arg) + dev_err(&(dev)->dev, MEI_CL_FMT format, MEI_CL_PRM(cl), ##arg)#endif /* _MEI_CLIENT_H_ */
drivers/misc/mei/dma-ring.c +4 -4
@@ -30,7 +30,7 @@ static int mei_dmam_dscr_alloc(struct mei_device *dev,if (dscr->vaddr)return 0 ; - dscr->vaddr =dmam_alloc_coherent (dev->dev, dscr->size, &dscr->daddr, + dscr->vaddr =dmam_alloc_coherent (dev->parent, dscr->size, &dscr->daddr,GFP_KERNEL );if (!dscr->vaddr)return -ENOMEM ;
@@ -50,7 +50,7 @@ static void mei_dmam_dscr_free(struct mei_device *dev,if (!dscr->vaddr)return ; -dmam_free_coherent (dev->dev, dscr->size, dscr->vaddr, dscr->daddr); +dmam_free_coherent (dev->parent, dscr->size, dscr->vaddr, dscr->daddr); dscr->vaddr =NULL ; }
@@ -177,7 +177,7 @@ void mei_dma_ring_read(struct mei_device *dev, unsigned char *buf, u32 len)if (WARN_ON (!ctrl))return ; -dev_dbg (dev->dev,"reading from dma %u bytes\n" , len); +dev_dbg (&dev->dev,"reading from dma %u bytes\n" , len);if (!len)return ;
@@ -254,7 +254,7 @@ void mei_dma_ring_write(struct mei_device *dev, unsigned char *buf, u32 len)if (WARN_ON (!ctrl))return ; -dev_dbg (dev->dev,"writing to dma %u bytes\n" , len); +dev_dbg (&dev->dev,"writing to dma %u bytes\n" , len); hbuf_depth =mei_dma_ring_hbuf_depth (dev); wr_idx =READ_ONCE (ctrl->hbuf_wr_idx) & (hbuf_depth -1 ); slots =mei_data2slots (len);
drivers/misc/mei/gsc-me.c +8 -5
@@ -69,7 +69,7 @@ static int mei_gsc_probe(struct auxiliary_device *aux_dev, hw->mem_addr =devm_ioremap_resource (device, &adev->bar);if (IS_ERR (hw->mem_addr)) { ret =PTR_ERR (hw->mem_addr); - goto err; + goto deinit; } hw->irq = adev->irq;
@@ -93,7 +93,7 @@ static int mei_gsc_probe(struct auxiliary_device *aux_dev,if (IS_ERR (hw->polling_thread)) { ret =PTR_ERR (hw->polling_thread);dev_err (device,"unable to create kernel thread: %d\n" , ret); - goto err; + goto deinit; } }else { ret = devm_request_threaded_irq(device, hw->irq,
@@ -102,7 +102,7 @@ static int mei_gsc_probe(struct auxiliary_device *aux_dev, IRQF_ONESHOT, KBUILD_MODNAME, dev);if (ret) {dev_err (device,"irq register failed %d\n" , ret); - goto err; + goto deinit; } }
@@ -131,7 +131,8 @@ static int mei_gsc_probe(struct auxiliary_device *aux_dev,mei_stop (dev);if (!mei_me_hw_use_polling (hw))devm_free_irq (device, hw->irq, dev); - +deinit: +mei_device_deinit (dev); err:dev_err (device,"probe failed: %d\n" , ret);dev_set_drvdata (device,NULL );
@@ -159,6 +160,8 @@ static void mei_gsc_remove(struct auxiliary_device *aux_dev)mei_disable_interrupts (dev);if (!mei_me_hw_use_polling (hw))devm_free_irq (&aux_dev->dev, hw->irq, dev); + +mei_device_deinit (dev); }static int __maybe_unused mei_gsc_pm_suspend (struct device *device)
@@ -252,7 +255,7 @@ static int __maybe_unused mei_gsc_pm_runtime_resume(struct device *device) irq_ret =mei_me_irq_thread_handler (1 , dev);if (irq_ret !=IRQ_HANDLED ) -dev_err (dev->dev,"thread handler fail %d\n" , irq_ret); +dev_err (&dev->dev,"thread handler fail %d\n" , irq_ret);return 0 ; }
drivers/misc/mei/hbm.c +58 -63
410 lines not on this page · raw message
drivers/misc/mei/hw-me.c +50 -51
379 lines not on this page · raw message
drivers/misc/mei/hw-txe.c +30 -32
233 lines not on this page · raw message
drivers/misc/mei/init.c +60 -25
212 lines not on this page · raw message
drivers/misc/mei/interrupt.c +22 -23
155 lines not on this page · raw message
drivers/misc/mei/main.c +6 -3
@@ -54,11 +54,12 @@ static int mei_open(struct inode *inode, struct file *file) dev =idr_find (&mei_idr,iminor (inode));if (!dev)return -ENODEV ; +get_device (&dev->dev);mutex_lock (&dev->device_lock);if (dev->dev_state !=MEI_DEV_ENABLED ) { -dev_dbg (dev->dev,"dev_state != MEI_ENABLED dev_state = %s\n" , +dev_dbg (&dev->dev,"dev_state != MEI_ENABLED dev_state = %s\n" ,mei_dev_state_str (dev->dev_state)); err = -ENODEV ; goto err_unlock;
@@ -79,6 +80,7 @@ static int mei_open(struct inode *inode, struct file *file) err_unlock:mutex_unlock (&dev->device_lock); +put_device (&dev->dev);return err; }
@@ -154,6 +156,7 @@ static int mei_release(struct inode *inode, struct file *file) file->private_data =NULL ;mutex_unlock (&dev->device_lock); +put_device (&dev->dev);return rets; }
@@ -479,7 +482,7 @@ static int mei_vt_support_check(struct mei_device *dev, const uuid_le *uuid) me_cl =mei_me_cl_by_uuid (dev, uuid);if (!me_cl) { -dev_dbg (dev->dev,"Cannot connect to FW Client UUID = %pUl\n" , +dev_dbg (&dev->dev,"Cannot connect to FW Client UUID = %pUl\n" , uuid);return -ENOTTY ; }
@@ -1196,7 +1199,7 @@ static int mei_minor_get(struct mei_device *dev)if (ret >=0 ) dev->minor = ret;else if (ret == -ENOSPC ) -dev_err (dev->dev,"too many mei devices\n" ); +dev_err (&dev->dev,"too many mei devices\n" );mutex_unlock (&mei_minor_lock);return ret;
drivers/misc/mei/mei_dev.h +7 -4
@@ -470,7 +470,8 @@ struct mei_dev_timeouts {/** * struct mei_device - MEI private device struct * -* @dev : device on a bus +* @parent : device on a bus +* @dev : device object * @cdev : character device pointer * @minor : minor number allocated for device *
@@ -556,7 +557,8 @@ struct mei_dev_timeouts {* @hw : hw specific data */ struct mei_device { -struct device *dev; +struct device *parent; +struct device dev;struct cdev *cdev;int minor;
@@ -702,10 +704,11 @@ static inline u32 mei_slots2data(int slots)/* * mei init function prototypes */ -void mei_device_init (struct mei_device *dev, -struct device *device, +struct mei_device *mei_device_init (struct device *device, +size_t hw_size,bool slow_fw,const struct mei_hw_ops *hw_ops); +void mei_device_deinit (struct mei_device *dev);int mei_reset (struct mei_device *dev);int mei_start (struct mei_device *dev);int mei_restart (struct mei_device *dev);
drivers/misc/mei/pci-me.c +8 -4
@@ -143,7 +143,7 @@ static inline void mei_me_unset_pm_domain(struct mei_device *dev) {}static int mei_me_read_fws (const struct mei_device *dev,int where,u32 *val) { -struct pci_dev *pdev =to_pci_dev (dev->dev); +struct pci_dev *pdev =to_pci_dev (dev->parent);return pci_read_config_dword (pdev, where, val); }
@@ -235,7 +235,7 @@ static int mei_me_probe(struct pci_dev *pdev, const struct pci_device_id *ent)if (err) {dev_err (&pdev->dev,"request_threaded_irq failure. irq = %d\n" , pdev->irq); - goto end; + goto deinit; }if (mei_start (dev)) {
@@ -286,6 +286,8 @@ static int mei_me_probe(struct pci_dev *pdev, const struct pci_device_id *ent)mei_cancel_work (dev);mei_disable_interrupts (dev);free_irq (pdev->irq, dev); +deinit: +mei_device_deinit (dev); end:dev_err (&pdev->dev,"initialization failed.\n" );return err;
@@ -338,6 +340,8 @@ static void mei_me_remove(struct pci_dev *pdev)free_irq (pdev->irq, dev);mei_deregister (dev); + +mei_device_deinit (dev); }#ifdef CONFIG_PM_SLEEP
@@ -475,7 +479,7 @@ static int mei_me_pm_runtime_resume(struct device *device)*/ static inline void mei_me_set_pm_domain (struct mei_device *dev) { -struct pci_dev *pdev =to_pci_dev (dev->dev); +struct pci_dev *pdev =to_pci_dev (dev->parent);if (pdev->dev.bus && pdev->dev.bus->pm) { dev->pg_domain.ops = *pdev->dev.bus->pm;
@@ -496,7 +500,7 @@ static inline void mei_me_set_pm_domain(struct mei_device *dev)static inline void mei_me_unset_pm_domain (struct mei_device *dev) {/* stop using pm callbacks if any */ -dev_pm_domain_set (dev->dev,NULL ); +dev_pm_domain_set (dev->parent,NULL ); }static const struct dev_pm_ops mei_me_pm_ops = {
drivers/misc/mei/pci-txe.c +7 -3
@@ -106,7 +106,7 @@ static int mei_txe_probe(struct pci_dev *pdev, const struct pci_device_id *ent)if (err) {dev_err (&pdev->dev,"mei: request_threaded_irq failure. irq = %d\n" , pdev->irq); - goto end; + goto deinit; }if (mei_start (dev)) {
@@ -150,6 +150,8 @@ static int mei_txe_probe(struct pci_dev *pdev, const struct pci_device_id *ent)mei_cancel_work (dev);mei_disable_interrupts (dev);free_irq (pdev->irq, dev); +deinit: +mei_device_deinit (dev); end:dev_err (&pdev->dev,"initialization failed.\n" );return err;
@@ -199,6 +201,8 @@ static void mei_txe_remove(struct pci_dev *pdev)free_irq (pdev->irq, dev);mei_deregister (dev); + +mei_device_deinit (dev); }
@@ -321,7 +325,7 @@ static int mei_txe_pm_runtime_resume(struct device *device)*/ static inline void mei_txe_set_pm_domain (struct mei_device *dev) { -struct pci_dev *pdev =to_pci_dev (dev->dev); +struct pci_dev *pdev =to_pci_dev (dev->parent);if (pdev->dev.bus && pdev->dev.bus->pm) { dev->pg_domain.ops = *pdev->dev.bus->pm;
@@ -342,7 +346,7 @@ static inline void mei_txe_set_pm_domain(struct mei_device *dev)static inline void mei_txe_unset_pm_domain (struct mei_device *dev) {/* stop using pm callbacks if any */ -dev_pm_domain_set (dev->dev,NULL ); +dev_pm_domain_set (dev->parent,NULL ); }static const struct dev_pm_ops mei_txe_pm_ops = {
drivers/misc/mei/platform-vsc.c +10 -8
@@ -152,7 +152,7 @@ static int mei_vsc_hw_start(struct mei_device *mei_dev)MEI_VSC_POLL_TIMEOUT_US , true, hw, &buf, sizeof(buf));if (ret) { -dev_err (mei_dev->dev,"wait fw ready failed: %d\n" , ret); +dev_err (&mei_dev->dev,"wait fw ready failed: %d\n" , ret);return ret; }
@@ -256,7 +256,7 @@ static int mei_vsc_hw_reset(struct mei_device *mei_dev, bool intr_enable)vsc_tp_reset (hw->tp); -return vsc_tp_init (hw->tp, mei_dev->dev); +return vsc_tp_init (hw->tp, mei_dev->parent); }static const struct mei_hw_ops mei_vsc_hw_ops = {
@@ -322,7 +322,7 @@ static void mei_vsc_event_cb(void *context) mei_dev->hbuf_is_ready =mei_hbuf_is_ready (mei_dev); ret =mei_irq_write_handler (mei_dev, &cmpl_list);if (ret) -dev_err (mei_dev->dev,"dispatch write request failed: %d\n" , ret); +dev_err (&mei_dev->dev,"dispatch write request failed: %d\n" , ret); mei_dev->hbuf_is_ready =mei_hbuf_is_ready (mei_dev);mei_irq_compl_handler (mei_dev, &cmpl_list);
@@ -340,12 +340,10 @@ static int mei_vsc_probe(struct platform_device *pdev)if (!tp)return dev_err_probe (dev, -ENODEV ,"no platform data\n" ); - mei_dev =devm_kzalloc (dev,size_add (sizeof (*mei_dev),sizeof (*hw)), -GFP_KERNEL ); + mei_dev =mei_device_init (dev,sizeof (*hw), false, &mei_vsc_hw_ops);if (!mei_dev)return -ENOMEM ; -mei_device_init (mei_dev, dev, false, &mei_vsc_hw_ops); mei_dev->fw_f_fw_ver_supported =0 ; mei_dev->kind ="ivsc" ;
@@ -367,7 +365,7 @@ static int mei_vsc_probe(struct platform_device *pdev)if (ret) goto err_stop; -pm_runtime_enable (mei_dev->dev); +pm_runtime_enable (mei_dev->parent);return 0 ;
@@ -379,6 +377,8 @@ static int mei_vsc_probe(struct platform_device *pdev)mei_disable_interrupts (mei_dev); +mei_device_deinit (mei_dev); +return ret; }
@@ -386,13 +386,15 @@ static void mei_vsc_remove(struct platform_device *pdev) {struct mei_device *mei_dev =platform_get_drvdata (pdev); -pm_runtime_disable (mei_dev->dev); +pm_runtime_disable (mei_dev->parent);mei_stop (mei_dev);mei_disable_interrupts (mei_dev);mei_deregister (mei_dev); + +mei_device_deinit (mei_dev); }static int mei_vsc_suspend (struct device *dev)
Signature · 1 line
2.43.0
Greg Kroah-Hartman
maintainerRe: [char-misc-next v2 5/5] mei: create dedicated device object
Quoted 1 line · Alexander Usyskin
mei_devicelifetime is managed by devm procedure of parent device.
Ick, what could go wrong...
Hint, devm is not a good thing to ever use for other reference counted structures that can be incremented/decremented independently. This is probably never going to work.
Quoted 6 lines
But such memory is freed on
device_del.Mei_deviceobject is used by client object that may be alive after parent device is removed.
It may lead to use-after-free if discrete graphics driver unloadsmei_gscauxiliary device while user-space holds open handle to mei character device.
Ah, are you trying to explain what happens today? This isn't obvious.
Quoted 2 lines
Add dedicated device object to control driver
private data lifetime.
But where is that device in sysfs now?
Quoted 2 lines
Rename exising parent device pointer from
dev to parent to avoid misuse.
Why not do this rename first?
Quoted 2 lines
Leave power management on parent device as
user-space is expecting it there.
Why? That feels totally wrong.
How does sysfs look before/after this change? Is the bus still addressed properly?
Quoted 39 lines
Signed-off-by Alexander Usyskin <[email protected]>--- drivers/misc/mei/bus-fixup.c | 6 +- drivers/misc/mei/bus.c | 24 ++++--- drivers/misc/mei/client.c | 82 +++++++++++----------- drivers/misc/mei/client.h | 6 +- drivers/misc/mei/dma-ring.c | 8 +-- drivers/misc/mei/gsc-me.c | 13 ++-- drivers/misc/mei/hbm.c | 121 +++++++++++++++----------------- drivers/misc/mei/hw-me.c | 101 +++++++++++++------------- drivers/misc/mei/hw-txe.c | 62 ++++++++-------- drivers/misc/mei/init.c | 85 +++++++++++++++------- drivers/misc/mei/interrupt.c | 45 ++++++------ drivers/misc/mei/main.c | 9 ++- drivers/misc/mei/mei_dev.h | 11 +-- drivers/misc/mei/pci-me.c | 12 ++-- drivers/misc/mei/pci-txe.c | 10 ++- drivers/misc/mei/platform-vsc.c | 18 ++--- 16 files changed, 331 insertions(+), 282 deletions(-)drivers/misc/mei/bus-fixup.c +1
-2@@ -386,7 +386,7 @@ static int mei_nfc_if_version(struct mei_cl *cl, ret =__mei_cl_send (cl, (u8 *)&cmd,sizeof (cmd),0 ,MEI_CL_IO_TX_BLOCKING );if (ret <0 ) { -dev_err (bus->dev,"Could not send IF version cmd ret = %d\n" , ret); +dev_err (&bus->dev,"Could not send IF version cmd ret = %d\n" , ret);return ret; }@@ -401,14 +401,14 @@ static int mei_nfc_if_version(struct mei_cl *cl, bytes_recv =__mei_cl_recv (cl, (u8 *)reply, if_version_length, &vtag,0 ,0 );if (bytes_recv <0 || (size_t )bytes_recv < if_version_length) { -dev_err (bus->dev,"Could not read IF version ret = %d\n" , bytes_recv);
drivers/misc/mei/bus-fixup.c static int mei_nfc_if_version(struct mei_cl *cl, · line 412
Quoted 10 lines
drivers/misc/mei/bus-fixup.c +2
-1+dev_err (&bus->dev,"Could not read IF version ret = %d\n" , bytes_recv); ret = -EIO ; goto err; }memcpy (ver, reply->data,sizeof (*ver)); -dev_info (bus->dev,"NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n" , +dev_info (&bus->dev,"NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n" , ver->fw_ivn, ver->vendor_id, ver->radio_type);
When drivers are working, they should be quiet :)
thanks,
greg k-h
Usyskin, Alexander
maintainerRE: [char-misc-next v2 5/5] mei: create dedicated device object
Quoted 11 lines
Subject: Re: [char-misc-next v2 5/5] mei: create dedicated device object
Quoted 1 line · Alexander Usyskin
mei_devicelifetime is managed by devm procedure of parent device.Ick, what could go wrong...
Hint, devm is not a good thing to ever use for other reference counted structures that can be incremented/decremented independently. This is probably never going to work.
Quoted 9 lines
Quoted 6 lines
But such memory is freed on
device_del.Mei_deviceobject is used by client object that may be alive after parent device is removed.
It may lead to use-after-free if discrete graphics driver unloadsmei_gscauxiliary device while user-space holds open handle to mei character device.Ah, are you trying to explain what happens today? This isn't obvious.
Will rephrase.
Quoted 10 lines
Quoted 2 lines
Add dedicated device object to control driver
private data lifetime.But where is that device in sysfs now?
Quoted 2 lines
Rename exising parent device pointer from
dev to parent to avoid misuse.Why not do this rename first?
Ok, will split to separate patch.
Quoted 8 lines
Quoted 2 lines
Leave power management on parent device as
user-space is expecting it there.Why? That feels totally wrong.
How does sysfs look before/after this change? Is the bus still addressed properly?
The sysfs is like: pci device (0000:00:16.0 usually) -> mei0 And it is unchanged with the patch.
New device is at pci device (0000:00:16.0) -> meid.0000:00:16.0
This is from platform with patches:
user@Platform:~$ ls -l /sys/bus/pci/devices/0000\:80\:16.0/ ... drwxr-xr-x 3 root root 0 Jun 23 14:42 0000:80:16.0-05b79a6f-4628-4d7f-899d-a91514cb32ab ... drwxr-xr-x 3 root root 0 Jun 23 14:42 0000:80:16.0-efa2aaa6-0bb6-4194-aba2-9f59a675eeba ... drwxr-xr-x 3 root root 0 Jun 23 14:42 mei drwxr-xr-x 3 root root 0 Jul 1 13:34 meid.0000:80:16.0 ...
The runtime power management is on PCI device.
We override usual PCI runtime pm as CSME has its own power management mechanism.
I'm looking if we can use mei class device created with device_create_with_groups, but it is created too late in the flow to be usefull.
- -
Thanks,
Sasha
Quoted 61 lines
Quoted 26 lines
Signed-off-by Alexander Usyskin <[email protected]>--- drivers/misc/mei/bus-fixup.c | 6 +- drivers/misc/mei/bus.c | 24 ++++--- drivers/misc/mei/client.c | 82 +++++++++++----------- drivers/misc/mei/client.h | 6 +- drivers/misc/mei/dma-ring.c | 8 +-- drivers/misc/mei/gsc-me.c | 13 ++-- drivers/misc/mei/hbm.c | 121 +++++++++++++++----------------- drivers/misc/mei/hw-me.c | 101 +++++++++++++------------- drivers/misc/mei/hw-txe.c | 62 ++++++++-------- drivers/misc/mei/init.c | 85 +++++++++++++++------- drivers/misc/mei/interrupt.c | 45 ++++++------ drivers/misc/mei/main.c | 9 ++- drivers/misc/mei/mei_dev.h | 11 +-- drivers/misc/mei/pci-me.c | 12 ++-- drivers/misc/mei/pci-txe.c | 10 ++- drivers/misc/mei/platform-vsc.c | 18 ++--- 16 files changed, 331 insertions(+), 282 deletions(-)drivers/misc/mei/bus-fixup.c +0
-0drivers/misc/mei/bus-fixup.c static int mei_nfc_if_version(struct mei_cl *cl, · line 389
Quoted 4 lines
drivers/misc/mei/bus-fixup.c +0
-1@@ -386,7 +386,7 @@ static int mei_nfc_if_version(struct mei_cl *cl, ret =__mei_cl_send (cl, (u8 *)&cmd,sizeof (cmd),0 ,MEI_CL_IO_TX_BLOCKING );if (ret <0 ) { - dev_err(bus->dev, "Could not send IF version cmd ret =%d\n", ret);
drivers/misc/mei/bus-fixup.c static int mei_nfc_if_version(struct mei_cl *cl, · line 389
Quoted 1 line
drivers/misc/mei/bus-fixup.c +1
-0+ dev_err(&bus->dev, "Could not send IF version cmd ret =%d\n", ret);
drivers/misc/mei/bus-fixup.c static int mei_nfc_if_version(struct mei_cl *cl, · line 404
Quoted 8 lines
drivers/misc/mei/bus-fixup.c +0
-1return ret; }@@ -401,14 +401,14 @@ static int mei_nfc_if_version(struct mei_cl *cl, bytes_recv =__mei_cl_recv (cl, (u8 *)reply, if_version_length, &vtag,0 ,0 );if (bytes_recv <0 || (size_t )bytes_recv < if_version_length) { - dev_err(bus->dev, "Could not read IF version ret = %d\n",
bytes_recv);drivers/misc/mei/bus-fixup.c static int mei_nfc_if_version(struct mei_cl *cl, · line 404
Quoted 1 line
drivers/misc/mei/bus-fixup.c +1
-0+ dev_err(&bus->dev, "Could not read IF version ret = %d\n",
bytes_recv);drivers/misc/mei/bus-fixup.c static int mei_nfc_if_version(struct mei_cl *cl,
Quoted 7 lines
drivers/misc/mei/bus-fixup.c +0
-1ret = -EIO ; goto err; }memcpy (ver, reply->data,sizeof (*ver)); - dev_info(bus->dev, "NFC MEI VERSION: IVN 0x%x Vendor ID 0x%xType 0x%x\n",
drivers/misc/mei/bus-fixup.c static int mei_nfc_if_version(struct mei_cl *cl, · line 411
Quoted 1 line
drivers/misc/mei/bus-fixup.c +1
-0+ dev_info(&bus->dev, "NFC MEI VERSION: IVN 0x%x Vendor ID 0x%xType 0x%x\n",
drivers/misc/mei/bus-fixup.c static int mei_nfc_if_version(struct mei_cl *cl, · line 412
Quoted 1 line
drivers/misc/mei/bus-fixup.c +0
-0ver->fw_ivn, ver->vendor_id, ver->radio_type);When drivers are working, they should be quiet :)
thanks,
greg k-h
Greg Kroah-Hartman
maintainerQuoted 11 lines · Alexander Usyskin
mei_devicedata structure is allocated using devm_* functions and hooked to the parent device.
It works for client systems where parent device is pci one. When parent device is auxiliary bus device produced by graphics driver, the parent can be destroyed before child and on the way it cleans all allocated memory.
This leads to use-after-free if mei character device is opened when parent device is destroyed.
Introduce device to hook themei_devicelifetime on it and decouple cdev from main structure to allow all memory to be released when it is unused and not before.
How was any of this tested?