Skip to contentSkip to conversation

power: cpupower: utils: Optimize print_online_cpus and print_offline_cpus function

PATCH In review Li kunyu 2 participants 6 messages Oct 12, 2022 – Oct 14, 2022 CPU power monitoring

lore
Share & tools
lore

Li kunyu

author
#0 lore raw

PATCH 1/1 power: cpupower: utils: Optimize print_online_cpus and print_offline_cpus function

1. Remove the initialization assignment of variables, and they will be assigned first.
2. Remove the mandatory conversion of returned value of malloc function, which returns void* type.

---
 tools/power/cpupower/utils/helpers/misc.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
tools/power/cpupower/utils/helpers/misc.c +6 -6
index 9547b29254a7..36dbd0562240 100644
@@ -134,11 +134,11 @@ void get_cpustate(void)
  */
 void print_online_cpus(void)
 {
-	int str_len = 0;
-	char *online_cpus_str = NULL;
+	int str_len;
+	char *online_cpus_str;
 
 	str_len = online_cpus->size * 5;
-	online_cpus_str = (void *)malloc(sizeof(char) * str_len);
+	online_cpus_str = malloc(sizeof(char) * str_len);
 
 	if (!bitmask_isallclear(online_cpus)) {
 		bitmask_displaylist(online_cpus_str, str_len, online_cpus);
@@ -152,11 +152,11 @@ void print_online_cpus(void)
  */
 void print_offline_cpus(void)
 {
-	int str_len = 0;
-	char *offline_cpus_str = NULL;
+	int str_len;
+	char *offline_cpus_str;
 
 	str_len = offline_cpus->size * 5;
-	offline_cpus_str = (void *)malloc(sizeof(char) * str_len);
+	offline_cpus_str = malloc(sizeof(char) * str_len);
 
 	if (!bitmask_isallclear(offline_cpus)) {
 		bitmask_displaylist(offline_cpus_str, str_len, offline_cpus);
Signature · 1 line
2.18.2
maintainer
#1 lore raw
Quoted 6 lines · Li kunyu

1. Remove the initialization assignment of variables, and they will be assigned first.
2. Remove the mandatory conversion of returned value of malloc function, which returns void* type.

Can you tell me more about why this change is needed and how you determined the need for this change?

thanks,
-- Shuah

author
#2 lore raw

I'm glad to get your reply. In previous tests, it was found that variable initialization and assignment use mov related instructions. Therefore, when I analyze the code and find that removing some variable initialization and assignment does not affect the function and security, I will try to remove variable initialization.

Find the malloc function and find that its return value is void * type, so it does not need to cast.

thanks,
kunyu

author
#3 lore raw
In reply to Shuah Khan

I'm glad to get your reply. In previous tests, it was found that variable initialization and assignment use mov related instructions. Therefore, when I analyze the code and find that removing some variable initialization and assignment does not affect the function and security, I will try to remove variable initialization.

Find the malloc function and find that its return value is void * type, so it does not need to cast.

thanks,
kunyu

maintainer
#4 lore raw
Quoted 8 lines · Li kunyu

I'm glad to get your reply. In previous tests, it was found that variable initialization and assignment use mov related instructions. Therefore, when I analyze the code and find that removing some variable initialization and assignment does not affect the function and security, I will try to remove variable initialization.

Find the malloc function and find that its return value is void * type, so it does not need to cast.

thanks,
kunyu

I am not seeing any reasons for removing the initialization. There is no need to do that.

Missing error handling after malloc() call is the real problem that can be fixed in this code path.

If you would like to fix that, send me a patch for that.

Hmm. Your reply to list looks strange - please double check and fix it

"Re:[PATCH]"@lists.nfsmail.com etc...

thanks,
-- Shuah

author
#5 lore raw

OK, I will make a new patch to determine the malloc memory allocation.