Skip to contentSkip to conversation

amdgpu/pm: Fix incorrect variable for size of clocks array

PATCH In review Darren Powell 2 participants 4 messages May 11, 2022 – Jun 16, 2022

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

PATCH v2 1/2 amdgpu/pm: Fix incorrect variable for size of clocks array

[v2]
No Changes, added RB
[v1]
Size of pp_clock_levels_with_latency is PP_MAX_CLOCK_LEVELS, not MAX_NUM_CLOCKS. Both are currently defined as 16, modifying in case one value is modified in future Changed code in both arcturus and aldabaran.

Also removed unneeded var count, and used min_t function

---
 drivers/gpu/drm/amd/pm/swsmu/smu11/arcturus_ppt.c  | 9 +++++----
 drivers/gpu/drm/amd/pm/swsmu/smu13/aldebaran_ppt.c | 9 +++++----
 2 files changed, 10 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/pm/swsmu/smu11/arcturus_ppt.c +5 -4
index 201563072189..bdd1e1a35a12 100644
@@ -573,12 +573,13 @@ static int arcturus_get_clk_table(struct smu_context *smu,
 			struct pp_clock_levels_with_latency *clocks,
 			struct smu_11_0_dpm_table *dpm_table)
 {
-	int i, count;
+	uint32_t i;
 
-	count = (dpm_table->count > MAX_NUM_CLOCKS) ? MAX_NUM_CLOCKS : dpm_table->count;
-	clocks->num_levels = count;
+	clocks->num_levels = min_t(uint32_t,
+				   dpm_table->count,
+				   (uint32_t)PP_MAX_CLOCK_LEVELS);
 
-	for (i = 0; i < count; i++) {
+	for (i = 0; i < clocks->num_levels; i++) {
 		clocks->data[i].clocks_in_khz =
 			dpm_table->dpm_levels[i].value * 1000;
 		clocks->data[i].latency_in_us = 0;
drivers/gpu/drm/amd/pm/swsmu/smu13/aldebaran_ppt.c +5 -4
index 38af648cb857..6a4fca47ae53 100644
@@ -549,12 +549,13 @@ static int aldebaran_get_clk_table(struct smu_context *smu,
 				   struct pp_clock_levels_with_latency *clocks,
 				   struct smu_13_0_dpm_table *dpm_table)
 {
-	int i, count;
+	uint32_t i;
 
-	count = (dpm_table->count > MAX_NUM_CLOCKS) ? MAX_NUM_CLOCKS : dpm_table->count;
-	clocks->num_levels = count;
+	clocks->num_levels = min_t(uint32_t,
+				   dpm_table->count,
+				   (uint32_t)PP_MAX_CLOCK_LEVELS);
 
-	for (i = 0; i < count; i++) {
+	for (i = 0; i < clocks->num_levels; i++) {
 		clocks->data[i].clocks_in_khz =
 			dpm_table->dpm_levels[i].value * 1000;
 		clocks->data[i].latency_in_us = 0;

base-commit: 8bb14fbec5ae45c31cbefe217db2418cc683612f

Signature · 1 line
2.35.1
#1 lore raw

PATCH v2 2/2 amdgpu/pm: Fix possible array out-of-bounds if SCLK levels != 2

[v2]
simplified fix after Lijo's feedback
removed clocks.num_levels from calculation of loop count
removed unsafe accesses to shim table freq_values
retained corner case output only min,now if
clocks.num_levels == 1 && now > min

[v1]
added a check to populate and use SCLK shim table freq_values only
if using dpm_level == AMD_DPM_FORCED_LEVEL_MANUAL or
AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM
removed clocks.num_levels from calculation of shim table size removed unsafe accesses to shim table freq_values
output gfx_table values if using other dpm levels
added check for freq_match when using freq_values for when now == min_clk

== Test ==
LOGFILE=aldebaran-sclk.test.log
AMDGPU_PCI_ADDR=lspci -nn | grep "VGA\|Display" | cut -d " " -f 1 AMDGPU_HWMON=ls -la /sys/class/hwmon | grep $AMDGPU_PCI_ADDR | awk '{print $9}' HWMON_DIR=/sys/class/hwmon/${AMDGPU_HWMON}

lspci -nn | grep "VGA\|Display" > $LOGFILE
FILES="pp_od_clk_voltage
pp_dpm_sclk"

for f in $FILES
do
echo === $f === >> $LOGFILE
cat $HWMON_DIR/device/$f >> $LOGFILE
done
cat $LOGFILE

---
 .../drm/amd/pm/swsmu/smu13/aldebaran_ppt.c    | 34 +++++++------------
 1 file changed, 12 insertions(+), 22 deletions(-)
drivers/gpu/drm/amd/pm/swsmu/smu13/aldebaran_ppt.c +12 -22
index 6a4fca47ae53..3eb82bc88890 100644
@@ -740,7 +740,7 @@ static int aldebaran_print_clk_levels(struct smu_context *smu,
 	struct smu_13_0_dpm_table *single_dpm_table;
 	struct smu_dpm_context *smu_dpm = &smu->smu_dpm;
 	struct smu_13_0_dpm_context *dpm_context = NULL;
-	uint32_t display_levels;
+	int display_levels;
 	uint32_t freq_values[3] = {0};
 	uint32_t min_clk, max_clk;
 
@@ -772,7 +772,7 @@ static int aldebaran_print_clk_levels(struct smu_context *smu,
 			return ret;
 		}
 
-		display_levels = clocks.num_levels;
+		display_levels = (clocks.num_levels == 1) ? 1 : 2;
 
 		min_clk = pstate_table->gfxclk_pstate.curr.min;
 		max_clk = pstate_table->gfxclk_pstate.curr.max;
@@ -782,30 +782,20 @@ static int aldebaran_print_clk_levels(struct smu_context *smu,
 
 		/* fine-grained dpm has only 2 levels */
 		if (now > min_clk && now < max_clk) {
-			display_levels = clocks.num_levels + 1;
+			display_levels++;
 			freq_values[2] = max_clk;
 			freq_values[1] = now;
 		}
 
-		/*
-		 * For DPM disabled case, there will be only one clock level.
-		 * And it's safe to assume that is always the current clock.
-		 */
-		if (display_levels == clocks.num_levels) {
-			for (i = 0; i < clocks.num_levels; i++)
-				size += sysfs_emit_at(buf, size, "%d: %uMhz %s\n", i,
-					freq_values[i],
-					(clocks.num_levels == 1) ?
-						"*" :
-						(aldebaran_freqs_in_same_level(
-							 freq_values[i], now) ?
-							 "*" :
-							 ""));
-		} else {
-			for (i = 0; i < display_levels; i++)
-				size += sysfs_emit_at(buf, size, "%d: %uMhz %s\n", i,
-						freq_values[i], i == 1 ? "*" : "");
-		}
+		for (i = 0; i < display_levels; i++)
+			size += sysfs_emit_at(buf, size, "%d: %uMhz %s\n", i,
+				freq_values[i],
+				(display_levels == 1) ?
+					"*" :
+					(aldebaran_freqs_in_same_level(
+						 freq_values[i], now) ?
+						 "*" :
+						 ""));
 
 		break;
 
Signature · 1 line
2.35.1
#2 lore raw

Re: [PATCH v2 2/2] amdgpu/pm: Fix possible array out-of-bounds if SCLK levels != 2

[AMD Official Use Only - General]

Ping 🙂
________________________________
From: Powell, Darren <[email protected]>
Sent: Wednesday, May 11, 2022 1:44 AM
To: [email protected] <[email protected]>
Cc: Lazar, Lijo <[email protected]>; Wang, Yang(Kevin) <[email protected]>; Feng, Kenneth <[email protected]>; Quan, Evan <[email protected]>; Ma, Le <[email protected]>; Powell, Darren <[email protected]>
Subject: [PATCH v2 2/2] amdgpu/pm: Fix possible array out-of-bounds if SCLK levels != 2

[v2]
simplified fix after Lijo's feedback
removed clocks.num_levels from calculation of loop count
removed unsafe accesses to shim table freq_values
retained corner case output only min,now if
clocks.num_levels == 1 && now > min

[v1]
added a check to populate and use SCLK shim table freq_values only
if using dpm_level == AMD_DPM_FORCED_LEVEL_MANUAL or
AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM
removed clocks.num_levels from calculation of shim table size removed unsafe accesses to shim table freq_values
output gfx_table values if using other dpm levels
added check for freq_match when using freq_values for when now == min_clk

== Test ==
LOGFILE=aldebaran-sclk.test.log
AMDGPU_PCI_ADDR=lspci -nn | grep "VGA\|Display" | cut -d " " -f 1 AMDGPU_HWMON=ls -la /sys/class/hwmon | grep $AMDGPU_PCI_ADDR | awk '{print $9}' HWMON_DIR=/sys/class/hwmon/${AMDGPU_HWMON}

lspci -nn | grep "VGA\|Display" > $LOGFILE
FILES="pp_od_clk_voltage
pp_dpm_sclk"

for f in $FILES
do
echo === $f === >> $LOGFILE
cat $HWMON_DIR/device/$f >> $LOGFILE
done
cat $LOGFILE

---
 .../drm/amd/pm/swsmu/smu13/aldebaran_ppt.c    | 34 +++++++------------
 1 file changed, 12 insertions(+), 22 deletions(-)
drivers/gpu/drm/amd/pm/swsmu/smu13/aldebaran_ppt.c +12 -22
index 6a4fca47ae53..3eb82bc88890 100644
@@ -740,7 +740,7 @@ static int aldebaran_print_clk_levels(struct smu_context *smu,
         struct smu_13_0_dpm_table *single_dpm_table;
         struct smu_dpm_context *smu_dpm = &smu->smu_dpm;
         struct smu_13_0_dpm_context *dpm_context = NULL;
-       uint32_t display_levels;
+       int display_levels;
         uint32_t freq_values[3] = {0};
         uint32_t min_clk, max_clk;
 
@@ -772,7 +772,7 @@ static int aldebaran_print_clk_levels(struct smu_context *smu,
                         return ret;
                 }
 
-               display_levels = clocks.num_levels;
+               display_levels = (clocks.num_levels == 1) ? 1 : 2;
 
                 min_clk = pstate_table->gfxclk_pstate.curr.min;
                 max_clk = pstate_table->gfxclk_pstate.curr.max;
@@ -782,30 +782,20 @@ static int aldebaran_print_clk_levels(struct smu_context *smu,
 
                 /* fine-grained dpm has only 2 levels */
                 if (now > min_clk && now < max_clk) {
-                       display_levels = clocks.num_levels + 1;
+                       display_levels++;
                         freq_values[2] = max_clk;
                         freq_values[1] = now;
                 }
 
-               /*
-                * For DPM disabled case, there will be only one clock level.
-                * And it's safe to assume that is always the current clock.
-                */
-               if (display_levels == clocks.num_levels) {
-                       for (i = 0; i < clocks.num_levels; i++)
-                               size += sysfs_emit_at(buf, size, "%d: %uMhz %s\n", i,
-                                       freq_values[i],
-                                       (clocks.num_levels == 1) ?
-                                               "*" :
-                                               (aldebaran_freqs_in_same_level(
-                                                        freq_values[i], now) ?
-                                                        "*" :
-                                                        ""));
-               } else {
-                       for (i = 0; i < display_levels; i++)
-                               size += sysfs_emit_at(buf, size, "%d: %uMhz %s\n", i,
-                                               freq_values[i], i == 1 ? "*" : "");
-               }
+               for (i = 0; i < display_levels; i++)
+                       size += sysfs_emit_at(buf, size, "%d: %uMhz %s\n", i,
+                               freq_values[i],
+                               (display_levels == 1) ?
+                                       "*" :
+                                       (aldebaran_freqs_in_same_level(
+                                                freq_values[i], now) ?
+                                                "*" :
+                                                ""));
 
                 break;
 

--
2.35.1

maintainer Reviewed-by Kenneth Feng
#3 lore raw

答复: [PATCH v2 2/2] amdgpu/pm: Fix possible array out-of-bounds if SCLK levels != 2

[AMD Official Use Only - General]

[AMD Official Use Only - General]

Best wishes
Kenneth Feng

发件人: Powell, Darren <[email protected]>
日期: 星期四, 2022年6月16日 05:00
收件人: [email protected] <[email protected]>
抄送: Lazar, Lijo <[email protected]>, Wang, Yang(Kevin) <[email protected]>, Feng, Kenneth <[email protected]>, Quan, Evan <[email protected]>, Ma, Le <[email protected]> 主题: Re: [PATCH v2 2/2] amdgpu/pm: Fix possible array out-of-bounds if SCLK levels != 2

[AMD Official Use Only - General]

Ping 🙂
________________________________
From: Powell, Darren <[email protected]>
Sent: Wednesday, May 11, 2022 1:44 AM
To: [email protected] <[email protected]>
Cc: Lazar, Lijo <[email protected]>; Wang, Yang(Kevin) <[email protected]>; Feng, Kenneth <[email protected]>; Quan, Evan <[email protected]>; Ma, Le <[email protected]>; Powell, Darren <[email protected]>
Subject: [PATCH v2 2/2] amdgpu/pm: Fix possible array out-of-bounds if SCLK levels != 2

[v2]
simplified fix after Lijo's feedback
removed clocks.num_levels from calculation of loop count
removed unsafe accesses to shim table freq_values
retained corner case output only min,now if
clocks.num_levels == 1 && now > min

[v1]
added a check to populate and use SCLK shim table freq_values only
if using dpm_level == AMD_DPM_FORCED_LEVEL_MANUAL or
AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM
removed clocks.num_levels from calculation of shim table size removed unsafe accesses to shim table freq_values
output gfx_table values if using other dpm levels
added check for freq_match when using freq_values for when now == min_clk

== Test ==
LOGFILE=aldebaran-sclk.test.log
AMDGPU_PCI_ADDR=lspci -nn | grep "VGA\|Display" | cut -d " " -f 1 AMDGPU_HWMON=ls -la /sys/class/hwmon | grep $AMDGPU_PCI_ADDR | awk '{print $9}' HWMON_DIR=/sys/class/hwmon/${AMDGPU_HWMON}

lspci -nn | grep "VGA\|Display" > $LOGFILE
FILES="pp_od_clk_voltage
pp_dpm_sclk"

for f in $FILES
do
echo === $f === >> $LOGFILE
cat $HWMON_DIR/device/$f >> $LOGFILE
done
cat $LOGFILE

---
 .../drm/amd/pm/swsmu/smu13/aldebaran_ppt.c    | 34 +++++++------------
 1 file changed, 12 insertions(+), 22 deletions(-)
drivers/gpu/drm/amd/pm/swsmu/smu13/aldebaran_ppt.c +12 -22
index 6a4fca47ae53..3eb82bc88890 100644
@@ -740,7 +740,7 @@ static int aldebaran_print_clk_levels(struct smu_context *smu,
         struct smu_13_0_dpm_table *single_dpm_table;
         struct smu_dpm_context *smu_dpm = &smu->smu_dpm;
         struct smu_13_0_dpm_context *dpm_context = NULL;
-       uint32_t display_levels;
+       int display_levels;
         uint32_t freq_values[3] = {0};
         uint32_t min_clk, max_clk;
 
@@ -772,7 +772,7 @@ static int aldebaran_print_clk_levels(struct smu_context *smu,
                         return ret;
                 }
 
-               display_levels = clocks.num_levels;
+               display_levels = (clocks.num_levels == 1) ? 1 : 2;
 
                 min_clk = pstate_table->gfxclk_pstate.curr.min;
                 max_clk = pstate_table->gfxclk_pstate.curr.max;
@@ -782,30 +782,20 @@ static int aldebaran_print_clk_levels(struct smu_context *smu,
 
                 /* fine-grained dpm has only 2 levels */
                 if (now > min_clk && now < max_clk) {
-                       display_levels = clocks.num_levels + 1;
+                       display_levels++;
                         freq_values[2] = max_clk;
                         freq_values[1] = now;
                 }
 
-               /*
-                * For DPM disabled case, there will be only one clock level.
-                * And it's safe to assume that is always the current clock.
-                */
-               if (display_levels == clocks.num_levels) {
-                       for (i = 0; i < clocks.num_levels; i++)
-                               size += sysfs_emit_at(buf, size, "%d: %uMhz %s\n", i,
-                                       freq_values[i],
-                                       (clocks.num_levels == 1) ?
-                                               "*" :
-                                               (aldebaran_freqs_in_same_level(
-                                                        freq_values[i], now) ?
-                                                        "*" :
-                                                        ""));
-               } else {
-                       for (i = 0; i < display_levels; i++)
-                               size += sysfs_emit_at(buf, size, "%d: %uMhz %s\n", i,
-                                               freq_values[i], i == 1 ? "*" : "");
-               }
+               for (i = 0; i < display_levels; i++)
+                       size += sysfs_emit_at(buf, size, "%d: %uMhz %s\n", i,
+                               freq_values[i],
+                               (display_levels == 1) ?
+                                       "*" :
+                                       (aldebaran_freqs_in_same_level(
+                                                freq_values[i], now) ?
+                                                "*" :
+                                                ""));
 
                 break;
 

--
2.35.1