Skip to contentSkip to conversation

cpufreq: powernv: fix stack bloat and NR_CPUS limitation

PATCH In review John Hubbard 4 participants 6 messages Oct 18, 2019 – Nov 6, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 cpufreq: powernv: fix stack bloat and NR_CPUS limitation

The following build warning occurred on powerpc 64-bit builds:

drivers/cpufreq/powernv-cpufreq.c: In function 'init_chip_info':
drivers/cpufreq/powernv-cpufreq.c:1070:1: warning: the frame size of 1040 bytes is larger than 1024 bytes [-Wframe-larger-than=]

This is due to putting 1024 bytes on the stack:

    unsigned int chip[256];

...and while looking at this, it also has a bug: it fails with a stack overrun, if CONFIG_NR_CPUS > 256.

Fix both problems by dynamically allocating based on CONFIG_NR_CPUS.

Fixes 053819e0bf840 ("cpufreq: powernv: Handle throttling due to Pmax capping at chip level")
Cc Shilpasri G Bhat <[email protected]>
Cc Preeti U Murthy <[email protected]>
Cc Viresh Kumar <[email protected]>
Cc Rafael J. Wysocki <[email protected]>

---

Changes since v1: includes Viresh's review commit fixes.

 drivers/cpufreq/powernv-cpufreq.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
drivers/cpufreq/powernv-cpufreq.c +13 -4
index 6061850e59c9..5b2e968cb5ea 100644
@@ -1041,9 +1041,14 @@ static struct cpufreq_driver powernv_cpufreq_driver = {
 
 static int init_chip_info(void)
 {
-	unsigned int chip[256];
+	unsigned int *chip;
 	unsigned int cpu, i;
 	unsigned int prev_chip_id = UINT_MAX;
+	int ret = 0;
+
+	chip = kcalloc(CONFIG_NR_CPUS, sizeof(*chip), GFP_KERNEL);
+	if (!chip)
+		return -ENOMEM;
 
 	for_each_possible_cpu(cpu) {
 		unsigned int id = cpu_to_chip_id(cpu);
@@ -1055,8 +1060,10 @@ static int init_chip_info(void)
 	}
 
 	chips = kcalloc(nr_chips, sizeof(struct chip), GFP_KERNEL);
-	if (!chips)
-		return -ENOMEM;
+	if (!chips) {
+		ret = -ENOMEM;
+		goto free_and_return;
+	}
 
 	for (i = 0; i < nr_chips; i++) {
 		chips[i].id = chip[i];
@@ -1066,7 +1073,9 @@ static int init_chip_info(void)
 			per_cpu(chip_info, cpu) =  &chips[i];
 	}
 
-	return 0;
+free_and_return:
+	kfree(chip);
+	return ret;
 }
 
 static inline void clean_chip_info(void)
Signature · 1 line
2.23.0
maintainer Acked-by on v2 1/1
#1 lore raw
Quoted 22 lines · John Hubbard

The following build warning occurred on powerpc 64-bit builds:

drivers/cpufreq/powernv-cpufreq.c: In function 'init_chip_info':
drivers/cpufreq/powernv-cpufreq.c:1070:1: warning: the frame size of 1040 bytes is larger than 1024 bytes [-Wframe-larger-than=]

This is due to putting 1024 bytes on the stack:

    unsigned int chip[256];

...and while looking at this, it also has a bug: it fails with a stack overrun, if CONFIG_NR_CPUS > 256.

Fix both problems by dynamically allocating based on CONFIG_NR_CPUS.

Fixes 053819e0bf840 ("cpufreq: powernv: Handle throttling due to Pmax capping at chip level")
Cc Shilpasri G Bhat <[email protected]>
Cc Preeti U Murthy <[email protected]>
Cc Viresh Kumar <[email protected]>
Cc Rafael J. Wysocki <[email protected]>
Quoted 6 lines

---

Changes since v1: includes Viresh's review commit fixes.

 drivers/cpufreq/powernv-cpufreq.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
Signature · 1 line
viresh
#2 lore raw
Quoted 25 lines · CET Viresh Kumar
Quoted 24 lines · John Hubbard

The following build warning occurred on powerpc 64-bit builds:

drivers/cpufreq/powernv-cpufreq.c: In function 'init_chip_info':
drivers/cpufreq/powernv-cpufreq.c:1070:1: warning: the frame size of 1040 bytes is larger than 1024 bytes [-Wframe-larger-than=]

This is due to putting 1024 bytes on the stack:

    unsigned int chip[256];

...and while looking at this, it also has a bug: it fails with a stack overrun, if CONFIG_NR_CPUS > 256.

Fix both problems by dynamically allocating based on CONFIG_NR_CPUS.

Fixes 053819e0bf840 ("cpufreq: powernv: Handle throttling due to Pmax capping at chip level")
Cc Shilpasri G Bhat <[email protected]>
Cc Preeti U Murthy <[email protected]>
Cc Viresh Kumar <[email protected]>
Cc Rafael J. Wysocki <[email protected]>

---

Quoted 8 lines
Quoted 4 lines

Changes since v1: includes Viresh's review commit fixes.

 drivers/cpufreq/powernv-cpufreq.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

Applying as 5.5 material, thanks!

#3 lore raw
In reply to John Hubbard

Hi John,

Sorry I didn't reply to this sooner, too many patches :/

Quoted 4 lines · John Hubbard

The following build warning occurred on powerpc 64-bit builds:

drivers/cpufreq/powernv-cpufreq.c: In function 'init_chip_info':
drivers/cpufreq/powernv-cpufreq.c:1070:1: warning: the frame size of 1040 bytes is larger than 1024 bytes [-Wframe-larger-than=]

Oddly I don't see that warning in my builds, eg with GCC9:

https://travis-ci.org/linuxppc/linux/jobs/604870722

Quoted 6 lines

This is due to putting 1024 bytes on the stack:

    unsigned int chip[256];

...and while looking at this, it also has a bug: it fails with a stack overrun, if CONFIG_NR_CPUS > 256.

It _probably_ doesn't, because it only increments the index when the chip_id of the CPU changes, ie. it doesn't create a chip for every CPU. But I agree it's flaky the way it's written.

Quoted 1 line

Fix both problems by dynamically allocating based on CONFIG_NR_CPUS.

Shouldn't it use num_possible_cpus() ?

Given the for loop is over possible CPUs that seems like the upper bound. In practice it should be lower because some CPUs will share a chip.

cheers

Quoted 61 lines
Fixes 053819e0bf840 ("cpufreq: powernv: Handle throttling due to Pmax capping at chip level")
Cc Shilpasri G Bhat <[email protected]>
Cc Preeti U Murthy <[email protected]>
Cc Viresh Kumar <[email protected]>
Cc Rafael J. Wysocki <[email protected]>

---

Changes since v1: includes Viresh's review commit fixes.

 drivers/cpufreq/powernv-cpufreq.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
drivers/cpufreq/powernv-cpufreq.c +13 -4
index 6061850e59c9..5b2e968cb5ea 100644
@@ -1041,9 +1041,14 @@ static struct cpufreq_driver powernv_cpufreq_driver = {
 
 static int init_chip_info(void)
 {
-	unsigned int chip[256];
+	unsigned int *chip;
 	unsigned int cpu, i;
 	unsigned int prev_chip_id = UINT_MAX;
+	int ret = 0;
+
+	chip = kcalloc(CONFIG_NR_CPUS, sizeof(*chip), GFP_KERNEL);
+	if (!chip)
+		return -ENOMEM;
 
 	for_each_possible_cpu(cpu) {
 		unsigned int id = cpu_to_chip_id(cpu);
@@ -1055,8 +1060,10 @@ static int init_chip_info(void)
 	}
 
 	chips = kcalloc(nr_chips, sizeof(struct chip), GFP_KERNEL);
-	if (!chips)
-		return -ENOMEM;
+	if (!chips) {
+		ret = -ENOMEM;
+		goto free_and_return;
+	}
 
 	for (i = 0; i < nr_chips; i++) {
 		chips[i].id = chip[i];
@@ -1066,7 +1073,9 @@ static int init_chip_info(void)
 			per_cpu(chip_info, cpu) =  &chips[i];
 	}
 
-	return 0;
+free_and_return:
+	kfree(chip);
+	return ret;
 }
 
 static inline void clean_chip_info(void)
Signature · 1 line
2.23.0
#4 lore raw
Quoted 5 lines · Michael Ellerman

Hi John,

Sorry I didn't reply to this sooner, too many patches :/

John Hubbard <[email protected]> writes:

Quoted 8 lines
Quoted 4 lines

The following build warning occurred on powerpc 64-bit builds:

drivers/cpufreq/powernv-cpufreq.c: In function 'init_chip_info':
drivers/cpufreq/powernv-cpufreq.c:1070:1: warning: the frame size of 1040 bytes is larger than 1024 bytes [-Wframe-larger-than=]

Oddly I don't see that warning in my builds, eg with GCC9:

   https://travis-ci.org/linuxppc/linux/jobs/604870722

This is with a cross-compiler based on gcc 8.1.0, which I got from:
https://mirrors.edge.kernel.org/pub/tools/crosstool/files/bin/x86_64/8.1.0/

I'll put that in the v3 commit description.

Quoted 11 lines
Quoted 7 lines

This is due to putting 1024 bytes on the stack:

     unsigned int chip[256];

...and while looking at this, it also has a bug: it fails with a stack overrun, if CONFIG_NR_CPUS > 256.

It _probably_ doesn't, because it only increments the index when the chip_id of the CPU changes, ie. it doesn't create a chip for every CPU. But I agree it's flaky the way it's written.

I'll soften up the wording accordingly.

Quoted 9 lines
Quoted 2 lines

Fix both problems by dynamically allocating based on CONFIG_NR_CPUS.

Shouldn't it use num_possible_cpus() ?

Given the for loop is over possible CPUs that seems like the upper bound. In practice it should be lower because some CPUs will share a chip.

OK, I see, that's more consistent with the code, I'll change to that.

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
Quoted 63 lines
Quoted 63 lines
Fixes 053819e0bf840 ("cpufreq: powernv: Handle throttling due to Pmax capping at chip level")
Cc Shilpasri G Bhat <[email protected]>
Cc Preeti U Murthy <[email protected]>
Cc Viresh Kumar <[email protected]>
Cc Rafael J. Wysocki <[email protected]>

---

Changes since v1: includes Viresh's review commit fixes.

  drivers/cpufreq/powernv-cpufreq.c | 17 +++++++++++++----
  1 file changed, 13 insertions(+), 4 deletions(-)
drivers/cpufreq/powernv-cpufreq.c +13 -4
index 6061850e59c9..5b2e968cb5ea 100644
@@ -1041,9 +1041,14 @@ static struct cpufreq_driver powernv_cpufreq_driver = {
 
  static int init_chip_info(void)
  {
-	unsigned int chip[256];
+	unsigned int *chip;
  	unsigned int cpu, i;
  	unsigned int prev_chip_id = UINT_MAX;
+	int ret = 0;
+
+	chip = kcalloc(CONFIG_NR_CPUS, sizeof(*chip), GFP_KERNEL);
+	if (!chip)
+		return -ENOMEM;
 
  	for_each_possible_cpu(cpu) {
  		unsigned int id = cpu_to_chip_id(cpu);
@@ -1055,8 +1060,10 @@ static int init_chip_info(void)
  	}
 
  	chips = kcalloc(nr_chips, sizeof(struct chip), GFP_KERNEL);
-	if (!chips)
-		return -ENOMEM;
+	if (!chips) {
+		ret = -ENOMEM;
+		goto free_and_return;
+	}
 
  	for (i = 0; i < nr_chips; i++) {
  		chips[i].id = chip[i];
@@ -1066,7 +1073,9 @@ static int init_chip_info(void)
  			per_cpu(chip_info, cpu) =  &chips[i];
  	}
 
-	return 0;
+free_and_return:
+	kfree(chip);
+	return ret;
  }
 
  static inline void clean_chip_info(void)
Signature · 1 line
2.23.0
#5 lore raw
Quoted 37 lines · John Hubbard
Quoted 5 lines · Michael Ellerman

Hi John,

Sorry I didn't reply to this sooner, too many patches :/

John Hubbard <[email protected]> writes:

Quoted 8 lines
Quoted 4 lines

The following build warning occurred on powerpc 64-bit builds:

drivers/cpufreq/powernv-cpufreq.c: In function 'init_chip_info':
drivers/cpufreq/powernv-cpufreq.c:1070:1: warning: the frame size of 1040 bytes is larger than 1024 bytes [-Wframe-larger-than=]

Oddly I don't see that warning in my builds, eg with GCC9:

   https://travis-ci.org/linuxppc/linux/jobs/604870722

This is with a cross-compiler based on gcc 8.1.0, which I got from:

   https://mirrors.edge.kernel.org/pub/tools/crosstool/files/bin/x86_64/8.1.0/

I'll put that in the v3 commit description.

Quoted 11 lines
Quoted 7 lines

This is due to putting 1024 bytes on the stack:

     unsigned int chip[256];

...and while looking at this, it also has a bug: it fails with a stack overrun, if CONFIG_NR_CPUS > 256.

It _probably_ doesn't, because it only increments the index when the chip_id of the CPU changes, ie. it doesn't create a chip for every CPU. But I agree it's flaky the way it's written.

I'll soften up the wording accordingly.

Quoted 3 lines
Quoted 2 lines

Fix both problems by dynamically allocating based on CONFIG_NR_CPUS.

Quoted 8 lines
Quoted 6 lines

Shouldn't it use num_possible_cpus() ?

Given the for loop is over possible CPUs that seems like the upper bound. In practice it should be lower because some CPUs will share a chip.

OK, I see, that's more consistent with the code, I'll change to that.

Thanks.

cheers