linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] thermal: cpu_cooling: fix handling of invalid frequency table entries
@ 2013-04-09 21:56 Andrew Bresticker
  0 siblings, 0 replies; only message in thread
From: Andrew Bresticker @ 2013-04-09 21:56 UTC (permalink / raw)
  To: Zhang Rui, Eduardo Valentin; +Cc: linux-pm, linux-kernel, Andrew Bresticker

get_cpu_frequency() goes into an infinite loop if any of the entries
in the CPU frequency table are invalid.  This patch fixes that case
and, with a separate counter for frequency levels, ensures that the
n-th valid frequency level is returned.

Signed-off-by: Andrew Bresticker <abrestic@chromium.org>
---
 drivers/thermal/cpu_cooling.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/drivers/thermal/cpu_cooling.c b/drivers/thermal/cpu_cooling.c
index 836828e..e6db441 100644
--- a/drivers/thermal/cpu_cooling.c
+++ b/drivers/thermal/cpu_cooling.c
@@ -124,14 +124,14 @@ static int is_cpufreq_valid(int cpu)
 static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
 {
 	int ret = 0, i = 0;
-	unsigned long level_index;
+	unsigned long level_index = 0;
 	bool descend = false;
 	struct cpufreq_frequency_table *table =
 					cpufreq_frequency_get_table(cpu);
 	if (!table)
 		return ret;
 
-	while (table[i].frequency != CPUFREQ_TABLE_END) {
+	for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
 		if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
 			continue;
 
@@ -143,24 +143,25 @@ static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
 		}
 
 		/*return if level matched and table in descending order*/
-		if (descend && i == level)
+		if (descend && level_index == level)
 			return table[i].frequency;
-		i++;
+		level_index++;
 	}
 	i--;
+	level_index--;
 
-	if (level > i || descend)
+	if (level > level_index || descend)
 		return ret;
-	level_index = i - level;
+	level = level_index - level;
 
 	/*Scan the table in reverse order and match the level*/
-	while (i >= 0) {
+	for (; i >= 0; i--) {
 		if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
 			continue;
 		/*return if level matched*/
-		if (i == level_index)
+		if (level_index == level)
 			return table[i].frequency;
-		i--;
+		level_index--;
 	}
 	return ret;
 }
-- 
1.8.1.3


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2013-04-09 21:57 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-04-09 21:56 [PATCH] thermal: cpu_cooling: fix handling of invalid frequency table entries Andrew Bresticker

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).