diff mbox

[PATCHv5,01/10] Thermal: Do kfree in _unregister functions

Message ID 1389916587-2541-2-git-send-email-durgadoss.r@intel.com (mailing list archive)
State Superseded, archived
Delegated to: Zhang Rui
Headers show

Commit Message

durgadoss.r@intel.com Jan. 16, 2014, 11:56 p.m. UTC
Currently the thermal_release function does
kfree for all devices when they call device_unregister.
This makes code scattering i.e whenever we add new
devices (to thermal class) we need to add its
corresponding kfree in thermal_release function.
And the if-else is also growing lengthy.

Instead, do all kfree() of devices in their own
_unregister functions. This makes the code look
clean and easy to maintain.

Signed-off-by: Durgadoss R <durgadoss.r@intel.com>
---
 drivers/thermal/thermal_core.c |   17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)

Comments

Dmitry Torokhov Jan. 21, 2014, 7:46 p.m. UTC | #1
Hi Durgadoss,

On Fri, Jan 17, 2014 at 05:26:18AM +0530, Durgadoss R wrote:
> Currently the thermal_release function does
> kfree for all devices when they call device_unregister.
> This makes code scattering i.e whenever we add new
> devices (to thermal class) we need to add its
> corresponding kfree in thermal_release function.
> And the if-else is also growing lengthy.
> 
> Instead, do all kfree() of devices in their own
> _unregister functions. This makes the code look
> clean and easy to maintain.
> 
> Signed-off-by: Durgadoss R <durgadoss.r@intel.com>
> ---
>  drivers/thermal/thermal_core.c |   17 +++++------------
>  1 file changed, 5 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
> index 338a88b..165afc6 100644
> --- a/drivers/thermal/thermal_core.c
> +++ b/drivers/thermal/thermal_core.c
> @@ -1042,18 +1042,7 @@ EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
>  
>  static void thermal_release(struct device *dev)
>  {
> -	struct thermal_zone_device *tz;
> -	struct thermal_cooling_device *cdev;
> -
> -	if (!strncmp(dev_name(dev), "thermal_zone",
> -		     sizeof("thermal_zone") - 1)) {
> -		tz = to_thermal_zone(dev);
> -		kfree(tz);
> -	} else if(!strncmp(dev_name(dev), "cooling_device",
> -			sizeof("cooling_device") - 1)){
> -		cdev = to_cooling_device(dev);
> -		kfree(cdev);
> -	}
> +	/* No-op since kfree(dev) is done in _unregister functions */
>  }
>  
>  static struct class thermal_class = {
> @@ -1146,6 +1135,7 @@ __thermal_cooling_device_register(struct device_node *np,
>  unregister:
>  	release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
>  	device_unregister(&cdev->device);
> +	kfree(cdev);

This is broken, you can not assume that you are holding the last
reference. Please observe help text of DEBUG_KOBJECT_RELEASE config
option, activate said option and try unregistering thermal device.

Thanks.
Zhang Rui March 3, 2014, 1:44 p.m. UTC | #2
On Tue, 2014-01-21 at 11:46 -0800, Dmitry Torokhov wrote:
> Hi Durgadoss,
> 
> On Fri, Jan 17, 2014 at 05:26:18AM +0530, Durgadoss R wrote:
> > Currently the thermal_release function does
> > kfree for all devices when they call device_unregister.
> > This makes code scattering i.e whenever we add new
> > devices (to thermal class) we need to add its
> > corresponding kfree in thermal_release function.
> > And the if-else is also growing lengthy.
> > 
> > Instead, do all kfree() of devices in their own
> > _unregister functions. This makes the code look
> > clean and easy to maintain.
> > 
> > Signed-off-by: Durgadoss R <durgadoss.r@intel.com>
> > ---
> >  drivers/thermal/thermal_core.c |   17 +++++------------
> >  1 file changed, 5 insertions(+), 12 deletions(-)
> > 
> > diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
> > index 338a88b..165afc6 100644
> > --- a/drivers/thermal/thermal_core.c
> > +++ b/drivers/thermal/thermal_core.c
> > @@ -1042,18 +1042,7 @@ EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
> >  
> >  static void thermal_release(struct device *dev)
> >  {
> > -	struct thermal_zone_device *tz;
> > -	struct thermal_cooling_device *cdev;
> > -
> > -	if (!strncmp(dev_name(dev), "thermal_zone",
> > -		     sizeof("thermal_zone") - 1)) {
> > -		tz = to_thermal_zone(dev);
> > -		kfree(tz);
> > -	} else if(!strncmp(dev_name(dev), "cooling_device",
> > -			sizeof("cooling_device") - 1)){
> > -		cdev = to_cooling_device(dev);
> > -		kfree(cdev);
> > -	}
> > +	/* No-op since kfree(dev) is done in _unregister functions */
> >  }
> >  
> >  static struct class thermal_class = {
> > @@ -1146,6 +1135,7 @@ __thermal_cooling_device_register(struct device_node *np,
> >  unregister:
> >  	release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
> >  	device_unregister(&cdev->device);
> > +	kfree(cdev);
> 
> This is broken, you can not assume that you are holding the last
> reference. Please observe help text of DEBUG_KOBJECT_RELEASE config
> option, activate said option and try unregistering thermal device.
> 
agreed.
If you think it is too complicated to support the different thermal
components in thermal_release(), we can introduce per device .release()
callback as well.

thanks,
rui
> Thanks.
> 


--
To unsubscribe from this list: send the line "unsubscribe linux-pm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
index 338a88b..165afc6 100644
--- a/drivers/thermal/thermal_core.c
+++ b/drivers/thermal/thermal_core.c
@@ -1042,18 +1042,7 @@  EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
 
 static void thermal_release(struct device *dev)
 {
-	struct thermal_zone_device *tz;
-	struct thermal_cooling_device *cdev;
-
-	if (!strncmp(dev_name(dev), "thermal_zone",
-		     sizeof("thermal_zone") - 1)) {
-		tz = to_thermal_zone(dev);
-		kfree(tz);
-	} else if(!strncmp(dev_name(dev), "cooling_device",
-			sizeof("cooling_device") - 1)){
-		cdev = to_cooling_device(dev);
-		kfree(cdev);
-	}
+	/* No-op since kfree(dev) is done in _unregister functions */
 }
 
 static struct class thermal_class = {
@@ -1146,6 +1135,7 @@  __thermal_cooling_device_register(struct device_node *np,
 unregister:
 	release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
 	device_unregister(&cdev->device);
+	kfree(cdev);
 	return ERR_PTR(result);
 }
 
@@ -1250,6 +1240,7 @@  void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
 
 	release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
 	device_unregister(&cdev->device);
+	kfree(cdev);
 	return;
 }
 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
@@ -1563,6 +1554,7 @@  struct thermal_zone_device *thermal_zone_device_register(const char *type,
 unregister:
 	release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
 	device_unregister(&tz->device);
+	kfree(tz);
 	return ERR_PTR(result);
 }
 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
@@ -1630,6 +1622,7 @@  void thermal_zone_device_unregister(struct thermal_zone_device *tz)
 	idr_destroy(&tz->idr);
 	mutex_destroy(&tz->lock);
 	device_unregister(&tz->device);
+	kfree(tz);
 	return;
 }
 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);