From patchwork Tue Apr 9 21:56:56 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Bresticker X-Patchwork-Id: 2417941 X-Patchwork-Delegate: rui.zhang@intel.com Return-Path: X-Original-To: patchwork-linux-pm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id AFA7B3FD8C for ; Tue, 9 Apr 2013 21:57:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1762666Ab3DIV5D (ORCPT ); Tue, 9 Apr 2013 17:57:03 -0400 Received: from mail-ie0-f201.google.com ([209.85.223.201]:45089 "EHLO mail-ie0-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751100Ab3DIV5B (ORCPT ); Tue, 9 Apr 2013 17:57:01 -0400 Received: by mail-ie0-f201.google.com with SMTP id a11so1887407iee.0 for ; Tue, 09 Apr 2013 14:57:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer :x-gm-message-state; bh=nuILHiOPRfekT88266Xyn69jwwYfpPJV3fNVqJp3Kus=; b=pSk4hcFUTajv93WEn7Rr7RIV+DyQOp6VMCaVH5NQ5DlsYPniI2MgdMmBYXyTAAKSrj K3ocOwuDxn/O6R7faNUE6nRCrI/ZrwFPEFmvvqbQwKPL14U9ZoK9ppiqN7pjAkGOeTKj ygA2D/r/aRBK+5B/pBhH5/+s1aYNLWYovq3/QrmuFIMlfpNn4ERwQh2MmMJZU0x9TidV wnyoOkIcRe0a/S5iHHm791Sy2nhNczlp3FhAB2kJ9BqeELsoGpQSzG0v0rhAna25/fCb BUWzY2xDhTL1baC5cNv95TSUojb8eKrLPc8n5B/m5wMaPJbJLF4Kml4NwBuamZ1CLsUb SBBg== X-Received: by 10.50.183.164 with SMTP id en4mr13678149igc.2.1365544621414; Tue, 09 Apr 2013 14:57:01 -0700 (PDT) Received: from corp2gmr1-1.hot.corp.google.com (corp2gmr1-1.hot.corp.google.com [172.24.189.92]) by gmr-mx.google.com with ESMTPS id o6si2342456igh.2.2013.04.09.14.57.01 (version=TLSv1.1 cipher=AES128-SHA bits=128/128); Tue, 09 Apr 2013 14:57:01 -0700 (PDT) Received: from abrestic.mtv.corp.google.com (abrestic.mtv.corp.google.com [172.22.72.111]) by corp2gmr1-1.hot.corp.google.com (Postfix) with ESMTP id BE2DA31C1BD; Tue, 9 Apr 2013 14:57:00 -0700 (PDT) Received: by abrestic.mtv.corp.google.com (Postfix, from userid 137652) id 4B71C22146F; Tue, 9 Apr 2013 14:57:00 -0700 (PDT) From: Andrew Bresticker To: Zhang Rui , Eduardo Valentin Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Andrew Bresticker Subject: [PATCH] thermal: cpu_cooling: fix handling of invalid frequency table entries Date: Tue, 9 Apr 2013 14:56:56 -0700 Message-Id: <1365544616-10003-1-git-send-email-abrestic@chromium.org> X-Mailer: git-send-email 1.8.1.3 X-Gm-Message-State: ALoCoQnQPT0BYz1DlRQMf3OF3PYVAlQ98PdN9J4eDcPEDI4rB4BMyTpY9wCcXgVFQ6UySMwVY/qNu/TNryrpqdrjVsjWvvb+l3jFFIot+rZc0l2nr7MbSjBYdjAnFe4c9lJZk8oFMmaGaHewwxC74VurUGA/nAuv01lv4XBmbbycdO0e/eQqLPeYREnpHKY+GVQFcFr5/TyK Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org 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 --- 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; }