thermal: core: Fix some error handling code in 'thermal_zone_device_register()'
Subsystems (2)
v2 cover thermal: core: Fix some error handling code in 'thermal_zone_device_register()'
These patches fix some issue in the error handling path in 'thermal_zone_device_register()'
The first patch adds some new helper function in order to ease the resource management.
Maybe using devm_ variant could also be a better choise to free manage these resources. As it would be a bigger change, I've not looked at it at all at the moment.
Also, note that this patch triggers a checkpatch warning about missing identifier name in function definition. I've left it as is to keep the style of the .h file.
The 2nd patch makes use of the new helper function.
The 3rd patch reorders the error handling path of 'thermal_zone_device_register()' in order to avoid the leaks.
*** These patches have been compiled tested only. ***
I also unsure about the right place for 'ida_simple_remove' in patch 3/3.
v1 -> v2:
- add some helper functions in order to release some resources more easily
- split the error handling path into 2. One before a successful call to 'device_register()', and one after
Christophe JAILLET (3):
thermal: core: Add some new helper functions to free resources
thermal: core: Use the new 'thermal_zone_destroy_device_groups()'
helper function
thermal: core: Fix resources release in error paths in
thermal_zone_device_register()
drivers/thermal/thermal_core.c | 29 ++++++++++++++--------------- drivers/thermal/thermal_core.h | 1 + drivers/thermal/thermal_sysfs.c | 29 +++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 15 deletions(-)
Signature · 1 line
2.11.0
PATCH v2 1/3 thermal: core: Add some new helper functions to free resources
In order to easily free resources allocated by 'thermal_zone_create_device_groups()' we need 2 new helper functions.
The first one undoes 'thermal_zone_create_device_groups()'. The 2nd one undoes 'create_trip_attrs()', which is a function called by 'thermal_zone_create_device_groups()'.
---
These functions will be used in patch 2/3 in order to simplify 'thermal_release()'
I've tried to implement it as close a possible as the way the resources have been allocated.
However, in 'thermal_release()', the code is simplier without some additionnal 'if'.
No sure if we should prefer consistancy with allocation or simplicity of code.
--- drivers/thermal/thermal_core.h | 1 + drivers/thermal/thermal_sysfs.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+)
drivers/thermal/thermal_core.h +1 -0
@@ -71,6 +71,7 @@ int thermal_build_list_of_policies(char *buf);/* sysfs I/F */ int thermal_zone_create_device_groups (struct thermal_zone_device *,int ); +void thermal_zone_destroy_device_groups (struct thermal_zone_device *);void thermal_cooling_device_setup_sysfs (struct thermal_cooling_device *);/* used only at binding time */ ssize_t
drivers/thermal/thermal_sysfs.c +29 -0
@@ -605,6 +605,24 @@ static int create_trip_attrs(struct thermal_zone_device *tz, int mask)return 0 ; } +/** +* destroy_trip_attrs() - create attributes for trip points +* @tz: the thermal zone device +* +* helper function to free resources alocated by create_trip_attrs() +*/ +static void destroy_trip_attrs (struct thermal_zone_device *tz) +{ +if (!tz) +return ; + +kfree (tz->trip_type_attrs); +kfree (tz->trip_temp_attrs); +if (tz->ops->get_trip_hyst) +kfree (tz->trip_hyst_attrs); +kfree (tz->trips_attribute_group.attrs); +} +int thermal_zone_create_device_groups (struct thermal_zone_device *tz,int mask) {
@@ -637,6 +655,17 @@ int thermal_zone_create_device_groups(struct thermal_zone_device *tz,return 0 ; } +void thermal_zone_destroy_device_groups (struct thermal_zone_device *tz) +{ +if (!tz) +return ; + +if (tz->trips) +destroy_trip_attrs (tz); + +kfree (tz->device.groups); +} +/* sys I/F for cooling device */ static ssize_t thermal_cooling_device_type_show(struct device *dev,
Signature · 1 line
2.11.0
PATCH v2 2/3 thermal: core: Use the new 'thermal_zone_destroy_device_groups()' helper function
In reply to Christophe JAILLETSimplify code by using the new 'thermal_zone_destroy_device_groups()' helper function.
--- drivers/thermal/thermal_core.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/thermal/thermal_core.c +1 -5
@@ -836,11 +836,7 @@ static void thermal_release(struct device *dev)if (!strncmp (dev_name (dev),"thermal_zone" ,sizeof ("thermal_zone" ) -1 )) { tz =to_thermal_zone (dev); -kfree (tz->trip_type_attrs); -kfree (tz->trip_temp_attrs); -kfree (tz->trip_hyst_attrs); -kfree (tz->trips_attribute_group.attrs); -kfree (tz->device.groups); +thermal_zone_destroy_device_groups (tz);kfree (tz); }else if (!strncmp (dev_name (dev),"cooling_device" ,sizeof ("cooling_device" ) -1 )) {
Signature · 1 line
2.11.0
PATCH v2 3/3 thermal: core: Fix resources release in error paths in thermal_zone_device_register()
In reply to Christophe JAILLETReorder error handling code in order to fix some resources leaks in some cases:
- 'tz' would leak if 'thermal_zone_create_device_groups()' fails
- memory allocated by 'thermal_zone_create_device_groups()' would leak if 'device_register()' fails
With this patch, we now have 2 error handling paths: one before 'device_register()', and one after it.
This is needed because some resources are released in 'thermal_release()'.
---
Should 'ida_simple_remove()' be folded somehow into 'thermal_release()'?
--- drivers/thermal/thermal_core.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-)
drivers/thermal/thermal_core.c +13 -10
@@ -1209,10 +1209,8 @@ thermal_zone_device_register(const char *type, int trips, int mask,ida_init (&tz->ida);mutex_init (&tz->lock); result =ida_simple_get (&thermal_tz_ida,0 ,0 ,GFP_KERNEL ); -if (result <0 ) { -kfree (tz); -return ERR_PTR (result); - } +if (result <0 ) + goto free_tz; tz->id = result;strlcpy (tz->type, type,sizeof (tz->type));
@@ -1228,18 +1226,15 @@ thermal_zone_device_register(const char *type, int trips, int mask,/* Add nodes that are always present via .groups */ result =thermal_zone_create_device_groups (tz, mask);if (result) - goto unregister; + goto remove_id;/* A new thermal zone needs to be updated anyway. */ atomic_set (&tz->need_update,1 );dev_set_name (&tz->device,"thermal_zone%d" , tz->id); result =device_register (&tz->device); -if (result) { -ida_simple_remove (&thermal_tz_ida, tz->id); -kfree (tz); -return ERR_PTR (result); - } +if (result) + goto remove_device_groups;for (count =0 ; count < trips; count++) {if (tz->ops->get_trip_type (tz, count, &trip_type))
@@ -1293,6 +1288,14 @@ thermal_zone_device_register(const char *type, int trips, int mask,ida_simple_remove (&thermal_tz_ida, tz->id);device_unregister (&tz->device);return ERR_PTR (result); + +remove_device_groups: +thermal_zone_destroy_device_groups (tz); +remove_id: +ida_simple_remove (&thermal_tz_ida, tz->id); +free_tz: +kfree (tz); +return ERR_PTR (result); }EXPORT_SYMBOL_GPL (thermal_zone_device_register);
Signature · 1 line
2.11.0
Zhang Rui
reviewerRe: [PATCH 1/3 v2] thermal: core: Add some new helper functions to free resources
In reply to Christophe JAILLETQuoted 49 lines · Christophe JAILLET
In order to easily free resources allocated by '
thermal_zone_create_device_groups()' we need 2 new helper functions.The first one undoes '
thermal_zone_create_device_groups()'. The 2nd one undoes 'create_trip_attrs()', which is a function called by
'thermal_zone_create_device_groups()'.Signed-off-by Christophe JAILLET <[email protected]>---
These functions will be used in patch 2/3 in order to simplify 'thermal_release()'
I've tried to implement it as close a possible as the way the resources have
been allocated.
However, in 'thermal_release()', the code is simplier without some additionnal 'if'.
No sure if we should prefer consistancy with allocation or simplicity of code.
---
drivers/thermal/thermal_core.h | 1 +drivers/thermal/thermal_sysfs.c| 29 +++++++++++++++++++++++++++++2 files changed, 30 insertions(+)drivers/thermal/thermal_core.h +0
-0@@ -71,6 +71,7 @@ int thermal_build_list_of_policies(char *buf);
/* sysfs I/F */
intthermal_zone_create_device_groups(struct thermal_zone_device *, int);
+voidthermal_zone_destroy_device_groups(struct thermal_zone_device *);
voidthermal_cooling_device_setup_sysfs(struct thermal_cooling_device *);
/* used only at binding time */
ssize_tdrivers/thermal/thermal_sysfs.c +0
-0@@ -605,6 +605,24 @@ static int create_trip_attrs(struct thermal_zone_device *tz, int mask) return 0; }
Quoted 3 lines
+/**
+ *destroy_trip_attrs()- create attributes for trip points
s/create/destroy
Quoted 3 lines
+ * @tz: the thermal zone device
+ *
+ * helper function to free resources alocated bycreate_trip_attrs()
s/alocated/allocated
thanks,
rui
Quoted 37 lines
+ */
drivers/thermal/thermal_sysfs.c +12
-0+static void destroy_trip_attrs (struct thermal_zone_device *tz) +{ +if (!tz) +return ; + +kfree (tz->trip_type_attrs); +kfree (tz->trip_temp_attrs); +if (tz->ops->get_trip_hyst) +kfree (tz->trip_hyst_attrs); +kfree (tz->trips_attribute_group.attrs); +} +int thermal_zone_create_device_groups(struct
thermal_zone_device*tz,
int mask)
{drivers/thermal/thermal_sysfs.c +0
-0@@ -637,6 +655,17 @@ int thermal_zone_create_device_groups(struct thermal_zone_device *tz, return 0; }
+voidthermal_zone_destroy_device_groups(struct thermal_zone_device *tz)drivers/thermal/thermal_sysfs.c +10
-0+{ +if (!tz) +return ; + +if (tz->trips) +destroy_trip_attrs (tz); + +kfree (tz->device.groups); +} +/* sys I/F for cooling device */
staticssize_tthermal_cooling_device_type_show(struct device *dev,
Zhang Rui
reviewerRe: [PATCH 1/3 v2] thermal: core: Add some new helper functions to free resources
Quoted 61 lines · Zhang Rui
Quoted 54 lines · Christophe JAILLET
In order to easily free resources allocated by '
thermal_zone_create_device_groups()' we need 2 new helper functions.The first one undoes '
thermal_zone_create_device_groups()'. The 2nd one undoes 'create_trip_attrs()', which is a function called
by
'thermal_zone_create_device_groups()'.Signed-off-by Christophe JAILLET <[email protected]>---
These functions will be used in patch 2/3 in order to simplify 'thermal_release()'
I've tried to implement it as close a possible as the way the resources have
been allocated.
However, in 'thermal_release()', the code is simplier without some additionnal 'if'.
No sure if we should prefer consistancy with allocation or simplicity
of code.
---
drivers/thermal/thermal_core.h | 1 +drivers/thermal/thermal_sysfs.c| 29 +++++++++++++++++++++++++++++2 files changed, 30 insertions(+)drivers/thermal/thermal_core.h +0
-0@@ -71,6 +71,7 @@ int thermal_build_list_of_policies(char *buf);
/* sysfs I/F */
int thermal_zone_create_device_groups(structthermal_zone_device*,
int);
+voidthermal_zone_destroy_device_groups(struct thermal_zone_device *);
voidthermal_cooling_device_setup_sysfs(struct thermal_cooling_device *);
/* used only at binding time */
ssize_tdrivers/thermal/thermal_sysfs.c +0
-0@@ -605,6 +605,24 @@ static int create_trip_attrs(struct thermal_zone_device *tz, int mask) return 0; }Quoted 3 lines
+/**
+ *destroy_trip_attrs()- create attributes for trip pointss/create/destroy
Quoted 1 line
Quoted 6 lines
Quoted 4 lines
+ * @tz: the thermal zone device
+ *
+ * helper function to free resources alocated bycreate_trip_attrs()s/alocated/allocated
as I have only two minor comments for this patch set, I will apply it directly, with these two typos fixed.
thanks,
rui
Quoted 40 lines
thanks,
ruiQuoted 38 lines
+ */
drivers/thermal/thermal_sysfs.c +12
-0+static void destroy_trip_attrs (struct thermal_zone_device *tz) +{ +if (!tz) +return ; + +kfree (tz->trip_type_attrs); +kfree (tz->trip_temp_attrs); +if (tz->ops->get_trip_hyst) +kfree (tz->trip_hyst_attrs); +kfree (tz->trips_attribute_group.attrs); +} +int thermal_zone_create_device_groups(struct
thermal_zone_device*tz,
int mask)
{drivers/thermal/thermal_sysfs.c +0
-0@@ -637,6 +655,17 @@ int thermal_zone_create_device_groups(struct thermal_zone_device *tz, return 0; }
+voidthermal_zone_destroy_device_groups(struct thermal_zone_device *tz)drivers/thermal/thermal_sysfs.c +10
-0+{ +if (!tz) +return ; + +if (tz->trips) +destroy_trip_attrs (tz); + +kfree (tz->device.groups); +} +/* sys I/F for cooling device */
staticssize_tthermal_cooling_device_type_show(struct device *dev,
Re: [PATCH 1/3 v2] thermal: core: Add some new helper functions to free resources
In reply to Christophe JAILLETAm 08.08.2017 16:39, schrieb Christophe JAILLET:
Quoted 63 lines
In order to easily free resources allocated by '
thermal_zone_create_device_groups()' we need 2 new helper functions.The first one undoes '
thermal_zone_create_device_groups()'. The 2nd one undoes 'create_trip_attrs()', which is a function called by 'thermal_zone_create_device_groups()'.Signed-off-by Christophe JAILLET <[email protected]>---
These functions will be used in patch 2/3 in order to simplify 'thermal_release()'
I've tried to implement it as close a possible as the way the resources have been allocated.
However, in 'thermal_release()', the code is simplier without some additionnal 'if'.
No sure if we should prefer consistancy with allocation or simplicity of code.--- drivers/thermal/thermal_core.h | 1 + drivers/thermal/thermal_sysfs.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+)drivers/thermal/thermal_core.h +1
-0@@ -71,6 +71,7 @@ int thermal_build_list_of_policies(char *buf);/* sysfs I/F */ int thermal_zone_create_device_groups (struct thermal_zone_device *,int ); +void thermal_zone_destroy_device_groups (struct thermal_zone_device *);void thermal_cooling_device_setup_sysfs (struct thermal_cooling_device *);/* used only at binding time */ ssize_t drivers/thermal/thermal_sysfs.c +18
-0@@ -605,6 +605,24 @@ static int create_trip_attrs(struct thermal_zone_device *tz, int mask)return 0 ; } +/** +* destroy_trip_attrs() - create attributes for trip points +* @tz: the thermal zone device +* +* helper function to free resources alocated by create_trip_attrs() +*/ +static void destroy_trip_attrs (struct thermal_zone_device *tz) +{ +if (!tz) +return ; + +kfree (tz->trip_type_attrs); +kfree (tz->trip_temp_attrs); +if (tz->ops->get_trip_hyst) +kfree (tz->trip_hyst_attrs); +kfree (tz->trips_attribute_group.attrs); +} +int thermal_zone_create_device_groups (struct thermal_zone_device *tz,int mask) {
drivers/thermal/thermal_sysfs.c int thermal_zone_create_device_groups(struct thermal_zone_device *tz, · line 664
Quoted 10 lines
drivers/thermal/thermal_sysfs.c +7
-0@@ -637,6 +655,17 @@ int thermal_zone_create_device_groups(struct thermal_zone_device *tz,return 0 ; } +void thermal_zone_destroy_device_groups (struct thermal_zone_device *tz) +{ +if (!tz) +return ; + +if (tz->trips) +destroy_trip_attrs (tz);
why the check for tz->trips ?destroy_trip_attrs does not access tz->trips->
re,
wh
Quoted 7 lines
drivers/thermal/thermal_sysfs.c +4
-0+ +kfree (tz->device.groups); +} +/* sys I/F for cooling device */ static ssize_t thermal_cooling_device_type_show(struct device *dev,
Zhang Rui
reviewerRe: [PATCH 1/3 v2] thermal: core: Add some new helper functions to free resources
Quoted 84 lines · walter harms
Am 08.08.2017 16:39, schrieb Christophe JAILLET:
Quoted 82 lines
In order to easily free resources allocated by '
thermal_zone_create_device_groups()' we need 2 new helper functions.The first one undoes '
thermal_zone_create_device_groups()'. The 2nd one undoes 'create_trip_attrs()', which is a function called by
'thermal_zone_create_device_groups()'.Signed-off-by Christophe JAILLET <[email protected]>---
These functions will be used in patch 2/3 in order to simplify 'thermal_release()'
I've tried to implement it as close a possible as the way the resources have
been allocated.
However, in 'thermal_release()', the code is simplier without some additionnal 'if'.
No sure if we should prefer consistancy with allocation or simplicity of code.
---
drivers/thermal/thermal_core.h | 1 +drivers/thermal/thermal_sysfs.c| 29 +++++++++++++++++++++++++++++2 files changed, 30 insertions(+)drivers/thermal/thermal_core.h +0
-0@@ -71,6 +71,7 @@ int thermal_build_list_of_policies(char *buf);
/* sysfs I/F */
intthermal_zone_create_device_groups(struct thermal_zone_device *, int);
+voidthermal_zone_destroy_device_groups(struct thermal_zone_device *);
voidthermal_cooling_device_setup_sysfs(struct thermal_cooling_device *);
/* used only at binding time */
ssize_tdrivers/thermal/thermal_sysfs.c +0
-0@@ -605,6 +605,24 @@ static int create_trip_attrs(struct thermal_zone_device *tz, int mask) return 0; }
+/**
+ *destroy_trip_attrs()- create attributes for trip points
+ * @tz: the thermal zone device
+ *
+ * helper function to free resources alocated bycreate_trip_attrs()
+ */drivers/thermal/thermal_sysfs.c +12
-0+static void(struct thermal_zone_device *tz) +{ +if (!tz) +return ; + +kfree (tz->trip_type_attrs); +kfree (tz->trip_temp_attrs); +if (tz->ops->get_trip_hyst) +kfree (tz->trip_hyst_attrs); +kfree (tz->trips_attribute_group.attrs); +} +int thermal_zone_create_device_groups(struct
thermal_zone_device*tz,
int mask)
{drivers/thermal/thermal_sysfs.c +0
-0@@ -637,6 +655,17 @@ int thermal_zone_create_device_groups(struct thermal_zone_device *tz, return 0; }
+voidthermal_zone_destroy_device_groups(struct thermal_zone_device *tz)
Quoted 9 lines
drivers/thermal/thermal_sysfs.c int thermal_zone_create_device_groups(struct thermal_zone_device *tz, · line 664
Quoted 6 lines
drivers/thermal/thermal_sysfs.c +6
-0+{ +if (!tz) +return ; + +if (tz->trips) +destroy_trip_attrs (tz);why the check for
tz->trips?destroy_trip_attrsdoes not access tz->trips->
tz->trips is an integer represents the number of trip points.
We add this check because there is not any trips attributes if we don't have any trip points.
It is true that the code also works if we don't have this check as destroy_trip_attrs() would be no-op when tz->trips equals 0. But I won't say this check is wrong.
thanks,
rui
Quoted 11 lines
re,
whQuoted 8 lines
drivers/thermal/thermal_sysfs.c +4
-0+ +kfree (tz->device.groups); +} +/* sys I/F for cooling device */
staticssize_tthermal_cooling_device_type_show(struct device *dev,
Re: [PATCH 1/3 v2] thermal: core: Add some new helper functions to free resources
In reply to Christophe JAILLETAm 11.08.2017 10:20, schrieb Zhang Rui:
Quoted 94 lines · walter harms
Quoted 82 lines
Am 08.08.2017 16:39, schrieb Christophe JAILLET:
Quoted 80 lines
In order to easily free resources allocated by '
thermal_zone_create_device_groups()' we need 2 new helper functions.The first one undoes '
thermal_zone_create_device_groups()'. The 2nd one undoes 'create_trip_attrs()', which is a function called by
'thermal_zone_create_device_groups()'.Signed-off-by Christophe JAILLET <[email protected]>---
These functions will be used in patch 2/3 in order to simplify 'thermal_release()'
I've tried to implement it as close a possible as the way the resources have
been allocated.
However, in 'thermal_release()', the code is simplier without some additionnal 'if'.
No sure if we should prefer consistancy with allocation or simplicity of code.--- drivers/thermal/thermal_core.h | 1 + drivers/thermal/thermal_sysfs.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+)drivers/thermal/thermal_core.h +1
-0@@ -71,6 +71,7 @@ int thermal_build_list_of_policies(char *buf);/* sysfs I/F */ int thermal_zone_create_device_groups (struct thermal_zone_device *,int ); +void thermal_zone_destroy_device_groups (struct thermal_zone_device *); void thermal_cooling_device_setup_sysfs(struct
thermal_cooling_device*);
/* used only at binding time */
ssize_tdrivers/thermal/thermal_sysfs.c +18
-0@@ -605,6 +605,24 @@ static int create_trip_attrs(struct thermal_zone_device *tz, int mask) return 0; } +/** +* destroy_trip_attrs() - create attributes for trip points +* @tz: the thermal zone device +* +* helper function to free resources alocated by create_trip_attrs() +*/ +static void(struct thermal_zone_device *tz) +{ +if (!tz) +return ; + +kfree (tz->trip_type_attrs); +kfree (tz->trip_temp_attrs); +if (tz->ops->get_trip_hyst) +kfree (tz->trip_hyst_attrs); +kfree (tz->trips_attribute_group.attrs); +} + int thermal_zone_create_device_groups(struct thermal_zone_device*tz,
drivers/thermal/thermal_sysfs.c +0
-0int mask) {@@ -637,6 +655,17 @@ int thermal_zone_create_device_groups(structthermal_zone_device *tz, return 0; }Quoted 11 lines
drivers/thermal/thermal_sysfs.c int thermal_zone_create_device_groups(struct thermal_zone_device *tz, · line 664
Quoted 8 lines
drivers/thermal/thermal_sysfs.c +7
-0+void thermal_zone_destroy_device_groups (struct thermal_zone_device *tz) +{ +if (!tz) +return ; + +if (tz->trips) +destroy_trip_attrs (tz);why the check for
tz->trips?destroy_trip_attrsdoes not access tz->trips->
Quoted 8 lines
tz->tripsis an integer represents the number of trip points.We add this check because there is not any trips attributes if we don't have any trip points.
It is true that the code also works if we don't have this check asdestroy_trip_attrs()would be no-op whentz->tripsequals 0. But I won't say this check is wrong.
I understand, then i would suggest to move the check into destroy_trip_attrs(). Without that knowledge it is confusing to read.
re,
wh
Quoted 14 lines
thanks,
ruiQuoted 11 lines
re,
whQuoted 8 lines
drivers/thermal/thermal_sysfs.c +4
-0+ +kfree (tz->device.groups); +} +/* sys I/F for cooling device */ static
ssize_tthermal_cooling_device_type_show(struct device *dev,