Skip to contentSkip to conversation

thermal: fix cpu_cooling max_level behavior

PATCH Applied Eduardo Valentin 2 participants 5 messages Nov 13, 2013 – Jan 4, 2014 Thermal

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 thermal: fix cpu_cooling max_level behavior

As per Documentation/thermal/sysfs-api.txt, max_level is an index, not a counter. Thus, in case a CPU has
3 valid frequencies, max_level is expected to be 2, for instance.

The current code makes max_level == number of valid frequencies, which is bogus. This patch fix the cpu_cooling device by ranging max_level properly.

---
 drivers/thermal/cpu_cooling.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/thermal/cpu_cooling.c +4 -2
index d179028..d0f8f8b5 100644
@@ -173,6 +173,8 @@ static int get_property(unsigned int cpu, unsigned long input,
 		freq = table[i].frequency;
 		max_level++;
 	}
+	/* max_level is an index, not a counter */
+	max_level--;
 
 	/* get max level */
 	if (property == GET_MAXL) {
@@ -181,7 +183,7 @@ static int get_property(unsigned int cpu, unsigned long input,
 	}
 
 	if (property == GET_FREQ)
-		level = descend ? input : (max_level - input - 1);
+		level = descend ? input : (max_level - input);
 
 	for (i = 0, j = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
 		/* ignore invalid entry */
@@ -197,7 +199,7 @@ static int get_property(unsigned int cpu, unsigned long input,
 
 		if (property == GET_LEVEL && (unsigned int)input == freq) {
 			/* get level by frequency */
-			*output = descend ? j : (max_level - j - 1);
+			*output = descend ? j : (max_level - j);
 			return 0;
 		}
 		if (property == GET_FREQ && level == j) {
Signature · 1 line
1.8.2.1.342.gfa7285d
#1 lore raw
Quoted 10 lines · Eduardo Valentin

As per Documentation/thermal/sysfs-api.txt, max_level is an index, not a counter. Thus, in case a CPU has
3 valid frequencies, max_level is expected to be 2, for instance.

The current code makes max_level == number of valid frequencies, which is bogus. This patch fix the cpu_cooling device by ranging max_level properly.

Rui,

This fix is applicable even on 3.12.

Quoted 36 lines
---
 drivers/thermal/cpu_cooling.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/thermal/cpu_cooling.c +4 -2
index d179028..d0f8f8b5 100644
@@ -173,6 +173,8 @@ static int get_property(unsigned int cpu, unsigned long input,
 		freq = table[i].frequency;
 		max_level++;
 	}
+	/* max_level is an index, not a counter */
+	max_level--;
 
 	/* get max level */
 	if (property == GET_MAXL) {
@@ -181,7 +183,7 @@ static int get_property(unsigned int cpu, unsigned long input,
 	}
 
 	if (property == GET_FREQ)
-		level = descend ? input : (max_level - input - 1);
+		level = descend ? input : (max_level - input);
 
 	for (i = 0, j = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
 		/* ignore invalid entry */
@@ -197,7 +199,7 @@ static int get_property(unsigned int cpu, unsigned long input,
 
 		if (property == GET_LEVEL && (unsigned int)input == freq) {
 			/* get level by frequency */
-			*output = descend ? j : (max_level - j - 1);
+			*output = descend ? j : (max_level - j);
 			return 0;
 		}
 		if (property == GET_FREQ && level == j) {
Signature · 3 lines
You have got to be excited about what you are doing. (L. Lamport)

Eduardo Valentin
#2 lore raw
In reply to Eduardo Valentin
Quoted 10 lines · Eduardo Valentin

As per Documentation/thermal/sysfs-api.txt, max_level is an index, not a counter. Thus, in case a CPU has
3 valid frequencies, max_level is expected to be 2, for instance.

The current code makes max_level == number of valid frequencies, which is bogus. This patch fix the cpu_cooling device by ranging max_level properly.

Rui,

Can you please consider pushing this fix?

Quoted 36 lines
---
 drivers/thermal/cpu_cooling.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/thermal/cpu_cooling.c +4 -2
index d179028..d0f8f8b5 100644
@@ -173,6 +173,8 @@ static int get_property(unsigned int cpu, unsigned long input,
 		freq = table[i].frequency;
 		max_level++;
 	}
+	/* max_level is an index, not a counter */
+	max_level--;
 
 	/* get max level */
 	if (property == GET_MAXL) {
@@ -181,7 +183,7 @@ static int get_property(unsigned int cpu, unsigned long input,
 	}
 
 	if (property == GET_FREQ)
-		level = descend ? input : (max_level - input - 1);
+		level = descend ? input : (max_level - input);
 
 	for (i = 0, j = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
 		/* ignore invalid entry */
@@ -197,7 +199,7 @@ static int get_property(unsigned int cpu, unsigned long input,
 
 		if (property == GET_LEVEL && (unsigned int)input == freq) {
 			/* get level by frequency */
-			*output = descend ? j : (max_level - j - 1);
+			*output = descend ? j : (max_level - j);
 			return 0;
 		}
 		if (property == GET_FREQ && level == j) {
Signature · 3 lines
You have got to be excited about what you are doing. (L. Lamport)

Eduardo Valentin
reviewer Applied
#3 lore raw
Quoted 9 lines · Eduardo Valentin
Quoted 8 lines · Eduardo Valentin

As per Documentation/thermal/sysfs-api.txt, max_level is an index, not a counter. Thus, in case a CPU has
3 valid frequencies, max_level is expected to be 2, for instance.

The current code makes max_level == number of valid frequencies, which is bogus. This patch fix the cpu_cooling device by ranging max_level properly.

good catch.

Quoted 7 lines
Quoted 2 lines

Rui,

Can you please consider pushing this fix?

applied.

Quoted 4 lines
Quoted 4 lines
---
 drivers/thermal/cpu_cooling.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
Quoted 11 lines

drivers/thermal/cpu_cooling.c static int get_property(unsigned int cpu, unsigned long input, · line 178

Quoted 11 lines
drivers/thermal/cpu_cooling.c +2 -0
index d179028..d0f8f8b5 100644
@@ -173,6 +173,8 @@ static int get_property(unsigned int cpu, unsigned long input,
 		freq = table[i].frequency;
 		max_level++;
 	}
+	/* max_level is an index, not a counter */
+	max_level--;
 

I think we should check the max_level first, like the patch I attached below.

thanks,
rui

Quoted 1 line

From a116776f7b6052599df0c67db29c30ea9d69d7ee Mon Sep 17 00:00:00 2001

From: Zhang Rui <[email protected]>
Date: Thu, 2 Jan 2014 11:57:48 +0800
Subject: [PATCH] Thermal cpu cooling: return error if no valid cpu frequency
entry

---
 drivers/thermal/cpu_cooling.c |    5 +++++
 1 file changed, 5 insertions(+)
drivers/thermal/cpu_cooling.c +5 -0
index cc556a8..bb486b4 100644
@@ -173,6 +173,11 @@ static int get_property(unsigned int cpu, unsigned long input,
 		freq = table[i].frequency;
 		max_level++;
 	}
+
+	/* No valid cpu frequency entry */
+	if (max_level == 0)
+		return -EINVAL;
+
 	/* max_level is an index, not a counter */
 	max_level--;
 
Signature · 1 line
1.7.9.5
author Acked-by
#4 lore raw
Quoted 57 lines · Zhang Rui
Quoted 9 lines · Eduardo Valentin
Quoted 8 lines · Eduardo Valentin

As per Documentation/thermal/sysfs-api.txt, max_level is an index, not a counter. Thus, in case a CPU has
3 valid frequencies, max_level is expected to be 2, for instance.

The current code makes max_level == number of valid frequencies, which is bogus. This patch fix the cpu_cooling device by ranging max_level properly.

good catch.

Quoted 7 lines
Quoted 2 lines

Rui,

Can you please consider pushing this fix?

applied.

Quoted 4 lines
Quoted 4 lines
---
 drivers/thermal/cpu_cooling.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
Quoted 11 lines

drivers/thermal/cpu_cooling.c static int get_property(unsigned int cpu, unsigned long input, · line 178

Quoted 11 lines
drivers/thermal/cpu_cooling.c +2 -0
index d179028..d0f8f8b5 100644
@@ -173,6 +173,8 @@ static int get_property(unsigned int cpu, unsigned long input,
 		freq = table[i].frequency;
 		max_level++;
 	}
+	/* max_level is an index, not a counter */
+	max_level--;
 

I think we should check the max_level first, like the patch I attached below.

thanks,
rui

Quoted 1 line

From a116776f7b6052599df0c67db29c30ea9d69d7ee Mon Sep 17 00:00:00 2001

From: Zhang Rui <[email protected]>
Date: Thu, 2 Jan 2014 11:57:48 +0800
Subject: [PATCH] Thermal cpu cooling: return error if no valid cpu frequency
entry

---
 drivers/thermal/cpu_cooling.c |    5 +++++
 1 file changed, 5 insertions(+)
drivers/thermal/cpu_cooling.c +0 -0
index cc556a8..bb486b4 100644

drivers/thermal/cpu_cooling.c static int get_property(unsigned int cpu, unsigned long input, · line 180

Quoted 8 lines
drivers/thermal/cpu_cooling.c +5 -0
@@ -173,6 +173,11 @@ static int get_property(unsigned int cpu, unsigned long input,
 		freq = table[i].frequency;
 		max_level++;
 	}
+
+	/* No valid cpu frequency entry */
+	if (max_level == 0)
+		return -EINVAL;
+

Agreed. For the patch above:

Quoted 4 lines
drivers/thermal/cpu_cooling.c +0 -0
 	/* max_level is an index, not a counter */
 	max_level--;
 
Signature · 3 lines
You have got to be excited about what you are doing. (L. Lamport)

Eduardo Valentin