Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp935713pxp; Wed, 16 Mar 2022 21:53:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJws+5ToBx5s/R8iQ8ef1uKGLWKOHAB1QKwRborW/A/2tYw6Blwpbx4t6R4AumjUfuMyoM4P X-Received: by 2002:a63:ec16:0:b0:381:6a51:6231 with SMTP id j22-20020a63ec16000000b003816a516231mr2255875pgh.189.1647492805364; Wed, 16 Mar 2022 21:53:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647492805; cv=none; d=google.com; s=arc-20160816; b=UEO1LFTWRDvmWTbSomF8qjg9oOkXontCPUVYoKvoPjXm912ofWto5hBqIo4EY4D69i aWtf5tk/RSmpWVr+XRm1MyVdMJ/D+RE49wXSf/Hi07ROiR2UvzHmZs//oBwvVOOsUmcP FnwQM/+Q2pFmISEHMUBq6XCoM6fc7aiE3NQYOEobYqzTRQbsmLfVV+eryjE2CC+13NRU X2piVI1XfJ40rUulRwVahA8D2yJyyyk+XSJyHgYUpVDPpebN+jY8LabE26clsw01mYKI FfDvNo9j4W4O0YDriuGhlYJXrWyOWo/4ZPfbLiM9WLn49s3ikJL2NwTjHAyIJU1OctTT KALg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:cc:references:to :subject; bh=R4uSeffHdjQuUwl/BUP7f7V0yCxRZILg8x19xIoaong=; b=GMPyWZbqEHCPmRpV0U0TzTCq8b//uzmkXLsZho5c1750NAIhtrUCSDc3GlOTQPdUdx +blm3oVe5XA7VclqTL5sptjWrdOqn+3FIXtgqn7Tgx4SCutvlPGJNvWayHpm8SNAQDEZ TXLYYRsMfOVg4pSKbQWFFluL76Og8EWow+wWsM+hWnDH3DjkdQwbCv+t/Wn8BN4YoXiM NRO02xMvNcHq9FUonyZ6Vk1FVXyf9PmNG+HI8PQbAH1r2MHF/kAw2WRNZ3DMOWH3gVGn DCxjsIGvFeNFwfbTUnHwUcMyu6Z5yu23tQwlrxFUVxJL/EApOVNBc20KTCRUoyNv9PT4 rYpA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id r38-20020a635d26000000b003816043f15esi1006147pgb.851.2022.03.16.21.53.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Mar 2022 21:53:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9EB8F16BFAE; Wed, 16 Mar 2022 21:11:41 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344451AbiCOCbl (ORCPT + 99 others); Mon, 14 Mar 2022 22:31:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43494 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344432AbiCOCbh (ORCPT ); Mon, 14 Mar 2022 22:31:37 -0400 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 957AB47079; Mon, 14 Mar 2022 19:30:26 -0700 (PDT) Received: from canpemm500006.china.huawei.com (unknown [172.30.72.53]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4KHcjT0DVNz9snV; Tue, 15 Mar 2022 10:26:37 +0800 (CST) Received: from [10.67.109.61] (10.67.109.61) by canpemm500006.china.huawei.com (7.192.105.130) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Tue, 15 Mar 2022 10:30:24 +0800 Subject: Re: [PATCH] cpufreq: fix cpufreq_get() can't get correct CPU frequency To: "Rafael J. Wysocki" References: <20220311081111.159639-1-zhengzucheng@huawei.com> CC: Viresh Kumar , Thomas Gleixner , Len Brown , Linux PM , Linux Kernel Mailing List , Stable From: zhengzucheng Message-ID: <100a5228-a941-0ff7-8133-14273472b6f5@huawei.com> Date: Tue, 15 Mar 2022 10:30:23 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.7.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [10.67.109.61] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To canpemm500006.china.huawei.com (7.192.105.130) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,NICE_REPLY_A, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2022/3/11 23:52, Rafael J. Wysocki wrote: > On Fri, Mar 11, 2022 at 9:11 AM z00314508 wrote: >> From: Zucheng Zheng >> >> On some specific platforms, the cpufreq driver does not define >> cpufreq_driver.get() routine (eg:x86 intel_pstate driver), as a > I guess you mean the cpufreq driver ->get callback. > > No, intel_pstate doesn't implement it, because it cannot reliably > return the current CPU frequency. > >> result, the cpufreq_get() can't get the correct CPU frequency. > No, it can't, if intel_pstate is the driver, but what's the problem? > This function is only called in one place in the kernel and not on x8 > even. > >> Modern x86 processors include the hardware needed to accurately >> calculate frequency over an interval -- APERF, MPERF and the TSC. > You can compute the average frequency over an interval, but ->get is > expected to return the actual current frequency at the time call time. > >> Here we use arch_freq_get_on_cpu() in preference to any driver >> driver-specific cpufreq_driver.get() routine to get CPU frequency. >> >> Fixes: f8475cef9008 ("x86: use common aperfmperf_khz_on_cpu() to calculate KHz using APERF/MPERF") > No kidding. > >> Signed-off-by: Zucheng Zheng >> --- >> drivers/cpufreq/cpufreq.c | 6 +++++- >> 1 file changed, 5 insertions(+), 1 deletion(-) >> >> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c >> index 80f535cc8a75..d777257b4454 100644 >> --- a/drivers/cpufreq/cpufreq.c >> +++ b/drivers/cpufreq/cpufreq.c >> @@ -1806,10 +1806,14 @@ unsigned int cpufreq_get(unsigned int cpu) >> { >> struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); >> unsigned int ret_freq = 0; >> + unsigned int freq; >> >> if (policy) { >> down_read(&policy->rwsem); >> - if (cpufreq_driver->get) >> + freq = arch_freq_get_on_cpu(policy->cpu); >> + if (freq) >> + ret_freq = freq; >> + else if (cpufreq_driver->get) > Again, what problem exactly does this address? Thank you for review. In some scenarios, cpufreq driver ->get is not defined, some driver get the CPU frequency by calling cpufreq_get() will return 0. The modification to this problem is inspired by the implementation of the show_scaling_cur_freq(). > >> ret_freq = __cpufreq_get(policy); >> up_read(&policy->rwsem); >> >> -- > . >