From patchwork Fri Jun 25 13:41:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: TungChen Shih X-Patchwork-Id: 12345139 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 75B0BC2B9F4 for ; Fri, 25 Jun 2021 13:42:24 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 3862E617ED for ; Fri, 25 Jun 2021 13:42:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3862E617ED Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=mediatek.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-mediatek-bounces+linux-mediatek=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Date:Subject:CC :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=C+bPW1JpcPvqQqoBGam12he/5Jb1lVdI2V5j3Ab425c=; b=yK6+/y+Lozz5Sr ZOgVF53ugR18I2wXZsQN2PSyB+rQ2gVlCpi+n2jtIOcnD4Ue9TrvGV8iBHbmPT81jB1ACRIq4UVjG 17riZxr4t23Nm4NpakAm4l8DdIrKEeNErkJkVQ2qN3zALAGwOKmT8DnE+PT+vxpswEgLeGULTGC5b G4rU8kDzVUJ62hnjk+kOkXSu2PBQ3zK2wmWRZ8jMgqkHEhx7E6IRUfX3uLWihlC+lon9eOOKQgS3g f4ac5cr6KFrwS66QEgrkgxjzzNXXePd7X1j3ehkv5SibLyU5pCxnqYJ9THeFbJXBaxKIrRka2BT38 aFUVwrE6aTRktLE0Tz7A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1lwm60-001kvu-TW; Fri, 25 Jun 2021 13:42:12 +0000 Received: from mailgw01.mediatek.com ([216.200.240.184]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1lwm5x-001kuN-PE; Fri, 25 Jun 2021 13:42:11 +0000 X-UUID: 7c8f65e23d4045658740cbed96545fd7-20210625 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Transfer-Encoding:Content-Type:MIME-Version:Message-ID:Date:Subject:CC:To:From; bh=rZ/W1Ql5R9otTRjtyF+YGQ07Zm+OUJKRFRGK53ia7Qw=; b=V00LQmAzg/lJyXsp3TFvhmTG46HCLzddUM165E8Nmkzn/5yaWdsXTi/a6W8J0hIyZAsmV8LLC3cZ1hL/QO1KQS9WeDEXVdLOhj/kDL5kDKNrQR0Xi37w6IJ9ny4eqrlVQk0eBURknHhF09HyG0ajOUQ+AqR804GlopGzQsI2auE=; X-UUID: 7c8f65e23d4045658740cbed96545fd7-20210625 Received: from mtkcas66.mediatek.inc [(172.29.193.44)] by mailgw01.mediatek.com (envelope-from ) (musrelay.mediatek.com ESMTP with TLSv1.2 ECDHE-RSA-AES256-SHA384 256/256) with ESMTP id 47739274; Fri, 25 Jun 2021 06:42:01 -0700 Received: from MTKMBS02N1.mediatek.inc (172.21.101.77) by MTKMBS62N2.mediatek.inc (172.29.193.42) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Fri, 25 Jun 2021 06:42:00 -0700 Received: from mtkcas11.mediatek.inc (172.21.101.40) by mtkmbs02n1.mediatek.inc (172.21.101.77) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Fri, 25 Jun 2021 21:41:46 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by mtkcas11.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Fri, 25 Jun 2021 21:41:46 +0800 From: TungChen Shih To: , , CC: , , , , , TungChen Shih Subject: [PATCH v1 1/1] cpufreq: fix the target freq not in the range of policy->min & max Date: Fri, 25 Jun 2021 21:41:30 +0800 Message-ID: <20210625134129.11885-1-tung-chen.shih@mediatek.com> X-Mailer: git-send-email 2.18.0 MIME-Version: 1.0 X-MTK: N X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210625_064209_890634_97ACD18B X-CRM114-Status: GOOD ( 14.56 ) X-BeenThere: linux-mediatek@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-mediatek" Errors-To: linux-mediatek-bounces+linux-mediatek=archiver.kernel.org@lists.infradead.org In cpufreq_frequency_table_target(), this function will try to find an index for @target_freq in freq_table, and the frequency of selected index should be in the range [policy->min, policy->max], which means: policy->min <= policy->freq_table[idx].frequency <= policy->max Though "clamp_val(target_freq, policy->min, policy->max);" would have been called to check this condition, when policy->max or min is not exactly one of the frequency in the frequency table, policy->freq_table[idx].frequency may still go out of the range For example, if our sorted freq_table is [3000, 2000, 1000], and suppose we have: @target_freq = 2500 @policy->min = 2000 @policy->max = 2200 @relation = CPUFREQ_RELATION_L 1. After clamp_val(target_freq, policy->min, policy->max); @target_freq becomes 2200 2. Since we use CPUFREQ_REALTION_L, final selected freq will be 3000 which beyonds policy->max Signed-off-by: TungChen Shih --- include/linux/cpufreq.h | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/include/linux/cpufreq.h b/include/linux/cpufreq.h index 353969c7acd3..60cb15740fdf 100644 --- a/include/linux/cpufreq.h +++ b/include/linux/cpufreq.h @@ -975,21 +975,40 @@ static inline int cpufreq_frequency_table_target(struct cpufreq_policy *policy, unsigned int target_freq, unsigned int relation) { + int idx = 0; if (unlikely(policy->freq_table_sorted == CPUFREQ_TABLE_UNSORTED)) return cpufreq_table_index_unsorted(policy, target_freq, relation); switch (relation) { case CPUFREQ_RELATION_L: - return cpufreq_table_find_index_l(policy, target_freq); + idx = cpufreq_table_find_index_l(policy, target_freq); + break; case CPUFREQ_RELATION_H: - return cpufreq_table_find_index_h(policy, target_freq); + idx = cpufreq_table_find_index_h(policy, target_freq); + break; case CPUFREQ_RELATION_C: - return cpufreq_table_find_index_c(policy, target_freq); + idx = cpufreq_table_find_index_c(policy, target_freq); + break; default: WARN_ON_ONCE(1); return 0; } + + /* target index verification */ + if (policy->freq_table[idx].frequency > policy->max) { + if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING) + idx--; + else + idx++; + } else if (policy->freq_table[idx].frequency < policy->min) { + if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING) + idx++; + else + idx--; + } + + return idx; } static inline int cpufreq_table_count_valid_entries(const struct cpufreq_policy *policy)