Received: by 2002:a05:6602:2086:0:0:0:0 with SMTP id a6csp4610265ioa; Wed, 27 Apr 2022 07:26:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwCGdwl9auYurALyhIUaYqkoinFvGNQ7e6iqvIVVaA2bQV5kfJpElWQ21YB+ER/PC9A6oDt X-Received: by 2002:a63:6b87:0:b0:3aa:e0eb:d157 with SMTP id g129-20020a636b87000000b003aae0ebd157mr19995054pgc.23.1651069605082; Wed, 27 Apr 2022 07:26:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651069605; cv=none; d=google.com; s=arc-20160816; b=Qe6rDR3dsKdKwCcV67fQVuBeOVklK6gVWQ009TsLUYRuPJy709I1Nl9OAd/O5ENNjy ++79qReO7+7Njo/v3m2ys7S+d3LpYdQD6JFOh6oDGbk7lwdB4w4EMtF2itxUTS9UgEpX Mdzyeqqc/0hkPmOn0LK73Y+8kHju3LVZPpVfaYR4gjdbd5Ep1Aiznb2GwXbE1CQicSG/ 2ZsUjXbgiTRzV2HLqmtc6KuCRQj5IfgfClfC1k9RXMAu7yup36ZX+Nz6/NGM+wYcT3VR qF5sJbCZS6p8zs4P/uPcZ/xE8z1x9ZVy6cLnQaMGn2Eh7xApzG4VZnhmmZInCvEH0fW6 MT/w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=N+7h99kKlMUh7JJHN1Dn+s8h8ohgzthst2O6eL4ZICc=; b=k5/xMpY0yaTlMK0t7U0Y+SxOb16gR5IPee+rjJyyU2Qr0zmfm32X2z2SkhHm0AHXkL gck3/2bv+su0IN6y0QOU64vDDIckiJz3lRoOTo0+rFUOd8fvOEtSSOuVSpS/3usV1+67 2JFQO42bjU6f14ux8a5lAVf9xvxew7dnpfGCAtK84LG3Bqn+jxmN0i9d2k3PGzUKsEoB rbhDpoyaBjMN6RmIAHWOxMqsXT0VzmT4CZT5I5c/8k9RacStxWLS7N+FfxapCC1lhdSM 9QJ5DymGfMth5ikmcGczTJHKL0H5S5JuHURZps9TFsU7nZDcmO9iQj3YuUpvq+raLTHL 9goA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=2dhv0yAg; dkim=neutral (no key) header.i=@linutronix.de; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id p10-20020a170902eaca00b001590b345716si1639770pld.281.2022.04.27.07.26.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Apr 2022 07:26:45 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=2dhv0yAg; dkim=neutral (no key) header.i=@linutronix.de; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0435742EE3; Wed, 27 Apr 2022 06:57:01 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237020AbiD0N7m (ORCPT + 99 others); Wed, 27 Apr 2022 09:59:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237262AbiD0N70 (ORCPT ); Wed, 27 Apr 2022 09:59:26 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 827AA63A2; Wed, 27 Apr 2022 06:56:09 -0700 (PDT) Date: Wed, 27 Apr 2022 13:56:06 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1651067768; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=N+7h99kKlMUh7JJHN1Dn+s8h8ohgzthst2O6eL4ZICc=; b=2dhv0yAgkThgpo3hqBSiO5MOCkzNZ9agIlY80McZDmPfShlxgndTDr47ZXhwrxLAYZ6d5q pg8VPEGPe0YDUa5y+aCKOpCl9uRD+jAZNIh1Xn8jxDEzfPgTlUEUoevdFylnQyLfn4/o+p nbiiaeSLDQD5rUbOzP/KlNkkGMcpZY1brSgkYjfHWOD9/jn5IuDSrn6u35uSwuiwlBdiaP oHsEbpp7qJn8rGCTnwzs+JPoVC1/M69CyTBarE1+ugZ6I6uZ1DXgPqWlHsCkyaB7hCAwFW O/E5YlgPWDbLpDsmBC365AH/+GmzjZ1ShvG8TiMxk2pqJYKXVxp+9aamAR8q0w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1651067768; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=N+7h99kKlMUh7JJHN1Dn+s8h8ohgzthst2O6eL4ZICc=; b=08NK86gHGDpi+LXN/iwpEN2TUTDfNTE7KPgWuzhlbmsZ6gvaqC+HPan52pzbunMF+LGec7 jRAA2AuWV3SxsYDA== From: "tip-bot2 for Thomas Gleixner" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/cleanups] x86/aperfmperf: Replace aperfmperf_get_khz() Cc: Eric Dumazet , Thomas Gleixner , Eric Dumazet , "Rafael J. Wysocki" , "Peter Zijlstra (Intel)" , "Paul E. McKenney" , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20220415161206.875029458@linutronix.de> References: <20220415161206.875029458@linutronix.de> MIME-Version: 1.0 Message-ID: <165106776682.4207.10283880979080175202.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE 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 The following commit has been merged into the x86/cleanups branch of tip: Commit-ID: 61551f094837f77952eba2fdf8b913bb5b191ced Gitweb: https://git.kernel.org/tip/61551f094837f77952eba2fdf8b913bb5b191ced Author: Thomas Gleixner AuthorDate: Fri, 15 Apr 2022 21:20:02 +02:00 Committer: Thomas Gleixner CommitterDate: Wed, 27 Apr 2022 15:51:08 +02:00 x86/aperfmperf: Replace aperfmperf_get_khz() The frequency invariance infrastructure provides the APERF/MPERF samples already. Utilize them for the cpu frequency display in /proc/cpuinfo. The sample is considered valid for 20ms. So for idle or isolated NOHZ full CPUs the function returns 0, which is matching the previous behaviour. This gets rid of the mass IPIs and a delay of 20ms for stabilizing observed by Eric when reading /proc/cpuinfo. Reported-by: Eric Dumazet Signed-off-by: Thomas Gleixner Tested-by: Eric Dumazet Reviewed-by: Rafael J. Wysocki Acked-by: Peter Zijlstra (Intel) Acked-by: Paul E. McKenney Link: https://lore.kernel.org/r/20220415161206.875029458@linutronix.de --- arch/x86/kernel/cpu/aperfmperf.c | 77 +++++++++++++------------------ fs/proc/cpuinfo.c | 6 +-- include/linux/cpufreq.h | 1 +- 3 files changed, 35 insertions(+), 49 deletions(-) diff --git a/arch/x86/kernel/cpu/aperfmperf.c b/arch/x86/kernel/cpu/aperfmperf.c index 963c069..e9d2da7 100644 --- a/arch/x86/kernel/cpu/aperfmperf.c +++ b/arch/x86/kernel/cpu/aperfmperf.c @@ -101,49 +101,6 @@ static bool aperfmperf_snapshot_cpu(int cpu, ktime_t now, bool wait) return time_delta <= APERFMPERF_STALE_THRESHOLD_MS; } -unsigned int aperfmperf_get_khz(int cpu) -{ - if (!cpu_khz) - return 0; - - if (!boot_cpu_has(X86_FEATURE_APERFMPERF)) - return 0; - - if (!housekeeping_cpu(cpu, HK_TYPE_MISC)) - return 0; - - if (rcu_is_idle_cpu(cpu)) - return 0; /* Idle CPUs are completely uninteresting. */ - - aperfmperf_snapshot_cpu(cpu, ktime_get(), true); - return per_cpu(samples.khz, cpu); -} - -void arch_freq_prepare_all(void) -{ - ktime_t now = ktime_get(); - bool wait = false; - int cpu; - - if (!cpu_khz) - return; - - if (!boot_cpu_has(X86_FEATURE_APERFMPERF)) - return; - - for_each_online_cpu(cpu) { - if (!housekeeping_cpu(cpu, HK_TYPE_MISC)) - continue; - if (rcu_is_idle_cpu(cpu)) - continue; /* Idle CPUs are completely uninteresting. */ - if (!aperfmperf_snapshot_cpu(cpu, now, false)) - wait = true; - } - - if (wait) - msleep(APERFMPERF_REFRESH_DELAY_MS); -} - unsigned int arch_freq_get_on_cpu(int cpu) { struct aperfmperf_sample *s = per_cpu_ptr(&samples, cpu); @@ -530,6 +487,40 @@ void arch_scale_freq_tick(void) scale_freq_tick(acnt, mcnt); } +/* + * Discard samples older than the define maximum sample age of 20ms. There + * is no point in sending IPIs in such a case. If the scheduler tick was + * not running then the CPU is either idle or isolated. + */ +#define MAX_SAMPLE_AGE ((unsigned long)HZ / 50) + +unsigned int aperfmperf_get_khz(int cpu) +{ + struct aperfmperf *s = per_cpu_ptr(&cpu_samples, cpu); + unsigned long last; + unsigned int seq; + u64 acnt, mcnt; + + if (!cpu_feature_enabled(X86_FEATURE_APERFMPERF)) + return 0; + + do { + seq = raw_read_seqcount_begin(&s->seq); + last = s->last_update; + acnt = s->acnt; + mcnt = s->mcnt; + } while (read_seqcount_retry(&s->seq, seq)); + + /* + * Bail on invalid count and when the last update was too long ago, + * which covers idle and NOHZ full CPUs. + */ + if (!mcnt || (jiffies - last) > MAX_SAMPLE_AGE) + return 0; + + return div64_u64((cpu_khz * acnt), mcnt); +} + static int __init bp_init_aperfmperf(void) { if (!cpu_feature_enabled(X86_FEATURE_APERFMPERF)) diff --git a/fs/proc/cpuinfo.c b/fs/proc/cpuinfo.c index 419760f..f38bda5 100644 --- a/fs/proc/cpuinfo.c +++ b/fs/proc/cpuinfo.c @@ -5,14 +5,10 @@ #include #include -__weak void arch_freq_prepare_all(void) -{ -} - extern const struct seq_operations cpuinfo_op; + static int cpuinfo_open(struct inode *inode, struct file *file) { - arch_freq_prepare_all(); return seq_open(file, &cpuinfo_op); } diff --git a/include/linux/cpufreq.h b/include/linux/cpufreq.h index 35c7d6d..d5595d5 100644 --- a/include/linux/cpufreq.h +++ b/include/linux/cpufreq.h @@ -1199,7 +1199,6 @@ static inline void sched_cpufreq_governor_change(struct cpufreq_policy *policy, struct cpufreq_governor *old_gov) { } #endif -extern void arch_freq_prepare_all(void); extern unsigned int arch_freq_get_on_cpu(int cpu); #ifndef arch_set_freq_scale