cpupower: fix resource leaks and unit labels
cover cpupower: fix resource leaks and unit labels
This series addresses multiple resource management issues in cpupower utilities:
- Patch 1 corrects swapped unit descriptions in powercap output where power_uw and energy_uj labels were interchanged.
- Patch 2 centralizes cleanup logic to ensure bitmask resources are freed on all exit paths in the main program.
- Patch 3 fixes a memory leak in print_man_page() when execlp fails.
Kaushlendra Kumar (3):
cpupower: fix swapped power/energy unit labels
cpupower: Fix memory leaks on early exit paths
cpupower: fix memory leak on execlp failure
tools/power/cpupower/utils/cpupower.c | 25 ++++++++++++++++------ tools/power/cpupower/utils/powercap-info.c | 4 ++-- 2 files changed, 21 insertions(+), 8 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/3 cpupower: fix swapped power/energy unit labels
Fix error where microWatts and microJoules units were interchanged.
--- tools/power/cpupower/utils/powercap-info.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/power/cpupower/utils/powercap-info.c +2 -2
@@ -38,11 +38,11 @@ static int powercap_print_one_zone(struct powercap_zone *zone)printf (" (%s)\n" , mode ?"enabled" :"disabled" );if (zone->has_power_uw) -printf (_ ("%sPower can be monitored in micro Jules\n" ), +printf (_ ("%sPower can be monitored in micro Watts\n" ), pr_prefix);if (zone->has_energy_uj) -printf (_ ("%sPower can be monitored in micro Watts\n" ), +printf (_ ("%sPower can be monitored in micro Jules\n" ), pr_prefix);printf ("\n" );
Signature · 1 line
2.34.1
Shuah Khan
maintainerRe: [PATCH 1/3] cpupower: fix swapped power/energy unit labels
Quoted 13 lines · Kaushlendra Kumar
Fix error where microWatts and microJoules units were interchanged.
Signed-off-by Kaushlendra Kumar <[email protected]>--- tools/power/cpupower/utils/powercap-info.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)tools/power/cpupower/utils/powercap-info.c +0
-0@@ -38,11 +38,11 @@ static int powercap_print_one_zone(struct powercap_zone *zone)printf (" (%s)\n" , mode ?"enabled" :"disabled" );
tools/power/cpupower/utils/powercap-info.c static int powercap_print_one_zone(struct powercap_zone *zone) · line 46
Quoted 10 lines
tools/power/cpupower/utils/powercap-info.c +2
-2if (zone->has_power_uw) -printf (_ ("%sPower can be monitored in micro Jules\n" ), +printf (_ ("%sPower can be monitored in micro Watts\n" ), pr_prefix);if (zone->has_energy_uj) -printf (_ ("%sPower can be monitored in micro Watts\n" ), +printf (_ ("%sPower can be monitored in micro Jules\n" ), pr_prefix);
Good find. I applied this to
https://web.git.kernel.org/pub/scm/linux/kernel/git/shuah/linux.git/?h=cpupower
I will send this to PM maintainer after rc1 comes out.
thanks,
-- Shuah
PATCH 2/3 cpupower: Fix memory leaks on early exit paths
In reply to Kaushlendra KumarAdd cleanup_resources() helper function to properly free globally allocated bitmasks (cpus_chosen, online_cpus, offline_cpus) before calling exit().
--- tools/power/cpupower/utils/cpupower.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)
tools/power/cpupower/utils/cpupower.c +16 -6
@@ -172,6 +172,17 @@ static void handle_options(int *argc, const char ***argv) *argv += new_argc; } +static void cleanup_resources (void ) { +if (cpus_chosen) +bitmask_free (cpus_chosen); +if (online_cpus) +bitmask_free (online_cpus); +if (offline_cpus) +bitmask_free (offline_cpus); + cpus_chosen =NULL ; + online_cpus =NULL ; + offline_cpus =NULL ; +}int main (int argc,const char *argv[]) {const char *cmd;
@@ -193,6 +204,7 @@ int main(int argc, const char *argv[])if (argc <1 ) {print_help (); +cleanup_resources ();return EXIT_FAILURE ; }
@@ -208,6 +220,7 @@ int main(int argc, const char *argv[]) base_cpu =sched_getcpu ();if (base_cpu <0 ) {fprintf (stderr,_ ("No valid cpus found.\n" )); +cleanup_resources ();return EXIT_FAILURE ; }
@@ -230,17 +243,14 @@ int main(int argc, const char *argv[])if (!run_as_root && p->needs_root) {fprintf (stderr,_ ("Subcommand %s needs root " "privileges\n" ), cmd); +cleanup_resources ();return EXIT_FAILURE ; } ret = p->main (argc, argv); -if (cpus_chosen) -bitmask_free (cpus_chosen); -if (online_cpus) -bitmask_free (online_cpus); -if (offline_cpus) -bitmask_free (offline_cpus); +cleanup_resources ();return ret; }print_help (); +cleanup_resources ();return EXIT_FAILURE ; }
Signature · 1 line
2.34.1
Shuah Khan
maintainerRe: [PATCH 2/3] cpupower: Fix memory leaks on early exit paths
Quoted 5 lines · Kaushlendra Kumar
Add
cleanup_resources()helper function to properly free globally allocated bitmasks (cpus_chosen,online_cpus,offline_cpus) before callingexit().Signed-off-by Kaushlendra Kumar <[email protected]>
Memory gets releases when process exits - there is no need to free.
thanks,
-- Shuah
RE: [PATCH 2/3] cpupower: Fix memory leaks on early exit paths
Quoted 8 lines · Shuah Khan
Quoted 5 lines · Kaushlendra Kumar
Add
cleanup_resources()helper function to properly free globally allocated bitmasks (cpus_chosen,online_cpus,offline_cpus) before callingexit().Signed-off-by Kaushlendra Kumar <[email protected]>Memory gets releases when process exits - there is no need to free.
Hi Shuah,
You're right that the OS reclaims memory on process exit.
BR,
Kaushlendra
PATCH 3/3 cpupower: fix memory leak on execlp failure
In reply to Kaushlendra KumarFree allocated page string when execlp fails to execute.
--- tools/power/cpupower/utils/cpupower.c | 3 +++ 1 file changed, 3 insertions(+)
tools/power/cpupower/utils/cpupower.c +3 -0
@@ -99,6 +99,9 @@ static int print_man_page(const char *subpage)execlp ("man" ,"man" , page,NULL ); +/* execlp failed */ +free (page); +/* should not be reached */ return -EINVAL ; }
Signature · 1 line
2.34.1
Shuah Khan
maintainerRe: [PATCH 3/3] cpupower: fix memory leak on execlp failure
Quoted 12 lines · Kaushlendra Kumar
Free allocated page string when execlp fails to execute.
Signed-off-by Kaushlendra Kumar <[email protected]>--- tools/power/cpupower/utils/cpupower.c | 3 +++ 1 file changed, 3 insertions(+)tools/power/cpupower/utils/cpupower.c +0
-0
tools/power/cpupower/utils/cpupower.c static int print_man_page(const char *subpage) · line 107
Quoted 9 lines
tools/power/cpupower/utils/cpupower.c +3
-0@@ -99,6 +99,9 @@ static int print_man_page(const char *subpage)execlp ("man" ,"man" , page,NULL ); +/* execlp failed */ +free (page); +/* should not be reached */ return -EINVAL ; }
This is unnecessary - how did you test this code?
thanks,
-- Shuah
PATCH 1/1 tools/cpupower: Avoid out-of-bounds access on empty strings
In reply to Kaushlendra KumarIf sysfs_get_idlestate_string() or sysfs_get_cpuidle_driver() returns an empty string, accessing result[strlen(result) - 1] causes an out-of-bounds access at result[-1].
Add length check before removing the trailing newline to prevent this. Also avoid redundant strlen() calls by caching the length.
--- tools/power/cpupower/lib/cpuidle.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
tools/power/cpupower/lib/cpuidle.c +6 -4
@@ -193,8 +193,9 @@ static char *cpuidle_state_get_one_string(unsigned int cpu,if (result ==NULL )return NULL ; -if (result[strlen (result) -1 ] =='\n' ) - result[strlen (result) -1 ] ='\0' ; + len =strlen (result); +if (len >0 && result[len -1 ] =='\n' ) + result[len -1 ] ='\0' ;return result; }
@@ -366,8 +367,9 @@ static char *sysfs_cpuidle_get_one_string(enum cpuidle_string which)if (result ==NULL )return NULL ; -if (result[strlen (result) -1 ] =='\n' ) - result[strlen (result) -1 ] ='\0' ; + len =strlen (result); +if (len >0 && result[len -1 ] =='\n' ) + result[len -1 ] ='\0' ;return result; }
Signature · 1 line
2.34.1
RE: [PATCH] tools/cpupower: Avoid out-of-bounds access on empty strings
Please ignore this patch. It was sent by mistake.
Sorry for the noise.
BR,
Kaushlendra
-----Original Message-----
From: Kumar, Kaushlendra <[email protected]>
Sent: Wednesday, February 4, 2026 10:52 AM
To: [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; Kumar, Kaushlendra <[email protected]>
Subject: [PATCH] tools/cpupower: Avoid out-of-bounds access on empty strings
If sysfs_get_idlestate_string() or sysfs_get_cpuidle_driver() returns an empty string, accessing result[strlen(result) - 1] causes an out-of-bounds access at result[-1].
Add length check before removing the trailing newline to prevent this. Also avoid redundant strlen() calls by caching the length.
--- tools/power/cpupower/lib/cpuidle.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
tools/power/cpupower/lib/cpuidle.c +6 -4
@@ -193,8 +193,9 @@ static char *cpuidle_state_get_one_string(unsigned int cpu,if (result ==NULL )return NULL ; -if (result[strlen (result) -1 ] =='\n' ) - result[strlen (result) -1 ] ='\0' ; + len =strlen (result); +if (len >0 && result[len -1 ] =='\n' ) + result[len -1 ] ='\0' ;return result; }
@@ -366,8 +367,9 @@ static char *sysfs_cpuidle_get_one_string(enum cpuidle_string which)if (result ==NULL )return NULL ; -if (result[strlen (result) -1 ] =='\n' ) - result[strlen (result) -1 ] ='\0' ; + len =strlen (result); +if (len >0 && result[len -1 ] =='\n' ) + result[len -1 ] ='\0' ;return result; }
--
2.34.1