Try 2, Fix release function in IPMI device model
PATCH 1/1 Try 2, Fix release function in IPMI device model
Ok, one more try. Russell, I assume you mean to use platform_device_alloc(), which seems to do what you suggested. And I assume the driver_data is the way to store whatever you need, instead of using the container_of() macro.
Arjun, Russell, thanks for the info.
Now the patch...
Arjun van de Ven pointed out that the changeover to the driver model in the IPMI driver had some bugs in it dealing with the release function and cleanup. Then Russell King pointed out that you can't put release functions in the same module as the unregistering code.
This patch fixes those problems and also adds a semaphore around the BMC functions and converts the BMC counter to a kref, which I meant to do earlier, but didn't write down :(.
drivers/char/ipmi/ipmi_msghandler.c +86 -80
@@ -163,11 +163,12 @@ struct ipmi_proc_entrystruct bmc_device { -struct platform_device dev; +struct platform_device *dev;struct ipmi_device_id id;unsigned char guid[16 ];int guid_set; -int interfaces; + +struct kref refcount;/* bmc device attributes */ struct device_attribute device_id_attr;
@@ -181,7 +182,6 @@ struct bmc_devicestruct device_attribute guid_attr;struct device_attribute aux_firmware_rev_attr; }; -#define to_bmc_device (device) container_of(device, struct bmc_device, dev)#define IPMI_IPMB_NUM_SEQ 64#define IPMI_MAX_CHANNELS 16
@@ -357,6 +357,7 @@ static struct device_driver ipmidriver = .name = "ipmi", .bus = &platform_bus_type }; +static DECLARE_MUTEX (ipmidriver_sem);#define MAX_IPMI_INTERFACES 4static ipmi_smi_t ipmi_interfaces[MAX_IPMI_INTERFACES ];
@@ -1758,9 +1759,7 @@ static void remove_proc_entries(ipmi_smistatic int __find_bmc_guid (struct device *dev,void *data) {unsigned char *id = (unsigned char *) data; -struct bmc_device *bmc; - - bmc =to_bmc_device (to_platform_device (dev)); +struct bmc_device *bmc =dev_get_drvdata (dev);return memcmp (bmc->guid, id,16 ) ==0 ; }
@@ -1771,7 +1770,7 @@ struct bmc_device * ipmi_find_bmc_guid(s dev =driver_find_device (drv,NULL , guid, __find_bmc_guid);if (dev) -return to_bmc_device (to_platform_device (dev)); +return dev_get_drvdata (dev);else return NULL ; }
@@ -1784,9 +1783,8 @@ struct prod_dev_id {static int __find_bmc_prod_dev_id (struct device *dev,void *data) {struct prod_dev_id *id = (struct prod_dev_id *) data; -struct bmc_device *bmc; +struct bmc_device *bmc =dev_get_drvdata (dev); - bmc =to_bmc_device (to_platform_device (dev));return (bmc->id.product_id == id->product_id && bmc->id.product_id == id->product_id && bmc->id.device_id == id->device_id);
@@ -1804,23 +1802,17 @@ static struct bmc_device *ipmi_find_bmc_ dev =driver_find_device (drv,NULL , &id, __find_bmc_prod_dev_id);if (dev) -return to_bmc_device (to_platform_device (dev)); +return dev_get_drvdata (dev);else return NULL ; } -static void ipmi_bmc_release (struct device *dev) -{ -printk (KERN_DEBUG "ipmi_bmc release\n" ); -} -static ssize_t device_id_show (struct device *dev,struct device_attribute *attr,char *buf) { -struct bmc_device *bmc; +struct bmc_device *bmc =dev_get_drvdata (dev); - bmc =to_bmc_device (to_platform_device (dev));return snprintf (buf,10 ,"%u\n" , bmc->id.device_id); }
@@ -1828,9 +1820,8 @@ static ssize_t provides_dev_sdrs_show(ststruct device_attribute *attr, char *buf) { - struct bmc_device *bmc; + struct bmc_device *bmc = dev_get_drvdata(dev); - bmc =to_bmc_device (to_platform_device (dev));return snprintf (buf,10 ,"%u\n" , bmc->id.device_revision &&0x80 >>7 ); }
@@ -1838,9 +1829,8 @@ static ssize_t provides_dev_sdrs_show(ststatic ssize_t revision_show (struct device *dev,struct device_attribute *attr,char *buf) { -struct bmc_device *bmc; +struct bmc_device *bmc =dev_get_drvdata (dev); - bmc =to_bmc_device (to_platform_device (dev));return snprintf (buf,20 ,"%u\n" , bmc->id.device_revision &&0x0F ); }
@@ -1849,9 +1839,8 @@ static ssize_t firmware_rev_show(structstruct device_attribute *attr, char *buf) { - struct bmc_device *bmc; + struct bmc_device *bmc = dev_get_drvdata(dev); - bmc =to_bmc_device (to_platform_device (dev));return snprintf (buf,20 ,"%u.%x\n" , bmc->id.firmware_revision_1, bmc->id.firmware_revision_2); }
@@ -1860,9 +1849,8 @@ static ssize_t ipmi_version_show(structstruct device_attribute *attr, char *buf) { - struct bmc_device *bmc; + struct bmc_device *bmc = dev_get_drvdata(dev); - bmc =to_bmc_device (to_platform_device (dev));return snprintf (buf,20 ,"%u.%u\n" ,ipmi_version_major (&bmc->id),ipmi_version_minor (&bmc->id));
@@ -1872,9 +1860,8 @@ static ssize_t add_dev_support_show(strustruct device_attribute *attr, char *buf) { - struct bmc_device *bmc; + struct bmc_device *bmc = dev_get_drvdata(dev); - bmc =to_bmc_device (to_platform_device (dev));return snprintf (buf,10 ,"0x%02x\n" , bmc->id.additional_device_support); }
@@ -1883,9 +1870,8 @@ static ssize_t manufacturer_id_show(strustruct device_attribute *attr, char *buf) { - struct bmc_device *bmc; + struct bmc_device *bmc = dev_get_drvdata(dev); - bmc =to_bmc_device (to_platform_device (dev));return snprintf (buf,20 ,"0x%6.6x\n" , bmc->id.manufacturer_id); }
@@ -1893,9 +1879,8 @@ static ssize_t product_id_show(struct destruct device_attribute *attr, char *buf) { - struct bmc_device *bmc; + struct bmc_device *bmc = dev_get_drvdata(dev); - bmc =to_bmc_device (to_platform_device (dev));return snprintf (buf,10 ,"0x%4.4x\n" , bmc->id.product_id); }
@@ -1903,9 +1888,8 @@ static ssize_t aux_firmware_rev_show(strstruct device_attribute *attr, char *buf) { - struct bmc_device *bmc; + struct bmc_device *bmc = dev_get_drvdata(dev); - bmc =to_bmc_device (to_platform_device (dev)); return snprintf(buf, 21, "0x%02x 0x%02x 0x%02x 0x%02x\n", bmc->id.aux_firmware_revision[3], bmc->id.aux_firmware_revision[2],
@@ -1916,50 +1900,60 @@ static ssize_t aux_firmware_rev_show(strstatic ssize_t guid_show (struct device *dev,struct device_attribute *attr,char *buf) { -struct bmc_device *bmc; +struct bmc_device *bmc =dev_get_drvdata (dev); - bmc =to_bmc_device (to_platform_device (dev));return snprintf (buf,100 ,"%Lx%Lx\n" , (long long ) bmc->guid[0 ], (long long ) bmc->guid[8 ]); } +static void +cleanup_bmc_device (struct kref *ref) +{ +struct bmc_device *bmc; + + bmc = container_of(ref, struct bmc_device, refcount); + +device_remove_file (&bmc->dev->dev, + &bmc->device_id_attr); +device_remove_file (&bmc->dev->dev, + &bmc->provides_dev_sdrs_attr); +device_remove_file (&bmc->dev->dev, + &bmc->revision_attr); +device_remove_file (&bmc->dev->dev, + &bmc->firmware_rev_attr); +device_remove_file (&bmc->dev->dev, + &bmc->version_attr); +device_remove_file (&bmc->dev->dev, + &bmc->add_dev_support_attr); +device_remove_file (&bmc->dev->dev, + &bmc->manufacturer_id_attr); +device_remove_file (&bmc->dev->dev, + &bmc->product_id_attr); +if (bmc->id.aux_firmware_revision_set) +device_remove_file (&bmc->dev->dev, + &bmc->aux_firmware_rev_attr); +if (bmc->guid_set) +device_remove_file (&bmc->dev->dev, + &bmc->guid_attr); +platform_device_unregister (bmc->dev); +kfree (bmc); +} +static void ipmi_bmc_unregister (ipmi_smi_t intf) {struct bmc_device *bmc = intf->bmc;sysfs_remove_link (&intf->si_dev->kobj,"bmc" );if (intf->my_dev_name) { -sysfs_remove_link (&bmc->dev.dev.kobj, intf->my_dev_name); +sysfs_remove_link (&bmc->dev->dev.kobj, intf->my_dev_name);kfree (intf->my_dev_name); intf->my_dev_name =NULL ; } - bmc->interfaces--; -if (bmc->interfaces ==0 ) { -device_remove_file (&bmc->dev.dev, - &bmc->device_id_attr); -device_remove_file (&bmc->dev.dev, - &bmc->provides_dev_sdrs_attr); -device_remove_file (&bmc->dev.dev, - &bmc->revision_attr); -device_remove_file (&bmc->dev.dev, - &bmc->firmware_rev_attr); -device_remove_file (&bmc->dev.dev, - &bmc->version_attr); -device_remove_file (&bmc->dev.dev, - &bmc->add_dev_support_attr); -device_remove_file (&bmc->dev.dev, - &bmc->manufacturer_id_attr); -device_remove_file (&bmc->dev.dev, - &bmc->product_id_attr); -device_remove_file (&bmc->dev.dev, - &bmc->aux_firmware_rev_attr); -if (bmc->guid_set) -device_remove_file (&bmc->dev.dev, - &bmc->guid_attr); -platform_device_unregister (&bmc->dev); - } +down (&ipmidriver_sem); +kref_put (&bmc->refcount, cleanup_bmc_device); +up (&ipmidriver_sem); }static int ipmi_bmc_register (ipmi_smi_t intf)
@@ -1970,6 +1964,8 @@ static int ipmi_bmc_register(ipmi_smi_tint size;char dummy[1 ]; +down (&ipmidriver_sem); +/* * Try to find if there is an bmc_device struct * representing the interfaced BMC already
@@ -1990,7 +1986,8 @@ static int ipmi_bmc_register(ipmi_smi_t intf->bmc = old_bmc; bmc = old_bmc; - bmc->interfaces++; +kref_get (&bmc->refcount); +up (&ipmidriver_sem);printk (KERN_INFO "ipmi: interfacing existing BMC (man_id: 0x%6.6x,"
@@ -1999,18 +1996,27 @@ static int ipmi_bmc_register(ipmi_smi_t bmc->id.product_id, bmc->id.device_id); }else { - bmc->dev.name ="ipmi_bmc" ; - bmc->dev.id = bmc->id.device_id; - bmc->dev.dev.driver = &ipmidriver; - bmc->dev.dev.release = ipmi_bmc_release; - bmc->interfaces =1 ; + bmc->dev =platform_device_alloc ("ipmi_bmc" , + bmc->id.device_id); +if (! bmc->dev) { +printk (KERN_ERR +"ipmi_msghandler:" +" Unable to allocate platform device\n" ); +return -ENOMEM ; + } + bmc->dev->dev.driver = &ipmidriver; +dev_set_drvdata (&bmc->dev->dev, bmc); +kref_init (&bmc->refcount); - rv =platform_device_register (&bmc->dev); + rv =platform_device_register (bmc->dev); +up (&ipmidriver_sem);if (rv) {printk (KERN_ERR "ipmi_msghandler:" " Unable to register bmc device: %d\n" , rv); +/* Don't go to out_err, you can only do that if +the device is registered already. */ return rv; }
@@ -2066,27 +2072,27 @@ static int ipmi_bmc_register(ipmi_smi_t bmc->aux_firmware_rev_attr.attr.mode =S_IRUGO ; bmc->aux_firmware_rev_attr.show = aux_firmware_rev_show; -device_create_file (&bmc->dev.dev, +device_create_file (&bmc->dev->dev, &bmc->device_id_attr); -device_create_file (&bmc->dev.dev, +device_create_file (&bmc->dev->dev, &bmc->provides_dev_sdrs_attr); -device_create_file (&bmc->dev.dev, +device_create_file (&bmc->dev->dev, &bmc->revision_attr); -device_create_file (&bmc->dev.dev, +device_create_file (&bmc->dev->dev, &bmc->firmware_rev_attr); -device_create_file (&bmc->dev.dev, +device_create_file (&bmc->dev->dev, &bmc->version_attr); -device_create_file (&bmc->dev.dev, +device_create_file (&bmc->dev->dev, &bmc->add_dev_support_attr); -device_create_file (&bmc->dev.dev, +device_create_file (&bmc->dev->dev, &bmc->manufacturer_id_attr); -device_create_file (&bmc->dev.dev, +device_create_file (&bmc->dev->dev, &bmc->product_id_attr);if (bmc->id.aux_firmware_revision_set) -device_create_file (&bmc->dev.dev, +device_create_file (&bmc->dev->dev, &bmc->aux_firmware_rev_attr);if (bmc->guid_set) -device_create_file (&bmc->dev.dev, +device_create_file (&bmc->dev->dev, &bmc->guid_attr); printk(KERN_INFO
@@ -2102,7 +2108,7 @@ static int ipmi_bmc_register(ipmi_smi_t* and back. */ rv =sysfs_create_link (&intf->si_dev->kobj, - &bmc->dev.dev.kobj,"bmc" ); + &bmc->dev->dev.kobj,"bmc" );if (rv) { printk(KERN_ERR "ipmi_msghandler: Unable to create bmc symlink: %d\n",
@@ -2121,7 +2127,7 @@ static int ipmi_bmc_register(ipmi_smi_t }snprintf (intf->my_dev_name, size+1 ,"ipmi%d" , intf->intf_num); - rv =sysfs_create_link (&bmc->dev.dev.kobj, &intf->si_dev->kobj, + rv =sysfs_create_link (&bmc->dev->dev.kobj, &intf->si_dev->kobj, intf->my_dev_name);if (rv) {kfree (intf->my_dev_name);
drivers/char/ipmi/ipmi_si_intf.c +28 -24
131 lines not on this page · raw message
Quoted 5 lines · Corey Minyard
Ok, one more try. Russell, I assume you mean to use
platform_device_alloc(), which seems to do what you suggested. And I assume thedriver_datais the way to store whatever you need, instead of using thecontainer_of()macro.
Quoted 8 lines
Arjun, Russell, thanks for the info.
Now the patch...
Arjun van de Ven pointed out that the changeover to the driver model in the IPMI driver had some bugs in it dealing with the release function and cleanup. Then Russell King pointed out that you can't put release functions in the same module as the unregistering code.
Yes you can, you just have to properly set up the module attribute owners and it will work just fine.
thanks,
greg k-h
Quoted 10 lines · Greg KH
Quoted 9 lines · Corey Minyard
Ok, one more try. Russell, I assume you mean to use
platform_device_alloc(), which seems to do what you suggested. And I assume thedriver_datais the way to store whatever you need, instead of using thecontainer_of()macro.Arjun, Russell, thanks for the info.
Now the patch...
Quoted 8 lines
Quoted 4 lines
Arjun van de Ven pointed out that the changeover to the driver model in the IPMI driver had some bugs in it dealing with the release function and cleanup. Then Russell King pointed out that you can't put release functions in the same module as the unregistering code.
Yes you can, you just have to properly set up the module attribute owners and it will work just fine.
No, not really. You can only do that if _all_ sysfs attributes for the object are handled in your driver which is rarely the case (dev, /power/* attributes, etc).
--
Dmitry
Quoted 27 lines · Dmitry Torokhov
Quoted 12 lines · Greg KH
Quoted 9 lines · Corey Minyard
Ok, one more try. Russell, I assume you mean to use
platform_device_alloc(), which seems to do what you suggested. And I assume thedriver_datais the way to store whatever you need, instead of using thecontainer_of()macro.Arjun, Russell, thanks for the info.
Now the patch...
Quoted 11 lines
Quoted 6 lines
Arjun van de Ven pointed out that the changeover to the driver model in the IPMI driver had some bugs in it dealing with the release function and cleanup. Then Russell King pointed out that you can't put release functions in the same module as the unregistering code.
Yes you can, you just have to properly set up the module attribute owners and it will work just fine.
Quoted 5 lines
No, not really. You can only do that if _all_ sysfs attributes for the object are handled in your driver which is rarely the case (dev, /power/* attributes, etc).
I don't see an owner field in the device structure. So you are saying that if the same module owns the device_driver structure, it is safe, but if not it is not safe.
-Corey
Quoted 29 lines · Corey Minyard
Quoted 27 lines · Dmitry Torokhov
Quoted 23 lines · Greg KH
Quoted 5 lines · Corey Minyard
Ok, one more try. Russell, I assume you mean to use
platform_device_alloc(), which seems to do what you suggested. And I assume thedriver_datais the way to store whatever you need, instead of using thecontainer_of()macro.Quoted 10 lines
Arjun, Russell, thanks for the info.
Now the patch...
Arjun van de Ven pointed out that the changeover to the driver model in the IPMI driver had some bugs in it dealing with the release function and cleanup. Then Russell King pointed out that you can't put release functions in the same module as the unregistering code.
Yes you can, you just have to properly set up the module attribute owners and it will work just fine.
Quoted 9 lines
Quoted 5 lines
No, not really. You can only do that if _all_ sysfs attributes for the object are handled in your driver which is rarely the case (dev, /power/* attributes, etc).
I don't see an owner field in the device structure. So you are saying that if the same module owns the
device_driverstructure, it is safe, but if not it is not safe.
Owner field is in attribute structure. So if all attributes are implemented in your module then you are guaranteed that when you get into module's cleanup routine there are no more references to these attributes and therefore to your object.
--
Dmitry
drivers/char/ipmi/ipmi_msghandler.c struct ipmi_proc_entry · line 171
Quoted 12 lines · Corey Minyard
struct
bmc_device
{drivers/char/ipmi/ipmi_msghandler.c +3
-2-struct platform_device dev; +struct platform_device *dev;struct ipmi_device_id id;unsigned char guid[16 ];int guid_set; -int interfaces; + +struct kref refcount;
Hi,
I am confused as to why you need kref here. Just unregister/kfree memory occupied by your device structure after doing platform_device_unregister and that's it. platform code won't reference your memory and your attribute code should not be called from module exit code so everything shoudl be fine.
--
Dmitry
Quoted 17 lines · Dmitry Torokhov
drivers/char/ipmi/ipmi_msghandler.c struct ipmi_proc_entry · line 171
Quoted 13 lines · Corey Minyard
struct
bmc_device
{drivers/char/ipmi/ipmi_msghandler.c +3
-2-struct platform_device dev; +struct platform_device *dev;struct ipmi_device_id id;unsigned char guid[16 ];int guid_set; -int interfaces; + +struct kref refcount;
Quoted 9 lines
Hi,
I am confused as to why you need kref here. Just unregister/kfree memory occupied by your device structure after doing
platform_device_unregisterand that's it. platform code won't reference your memory and your attribute code should not be called from module exit code so everything shoudl be fine.
This structure represents a "BMC", which is a microcontroller that does managment functions. There may be more than one interface to a BMC, so the kref keeps track of all the interfaces referencing the structure.
-Corey