Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp763450pxp; Fri, 11 Mar 2022 14:29:18 -0800 (PST) X-Google-Smtp-Source: ABdhPJyql1cR2bAwtLfpNIOuK8QMnEPdq0XpFlyjSNFevZXeITc5LVE1SCVgtD4K2VKmhHoAV2EX X-Received: by 2002:a17:90a:10d6:b0:1bc:48ad:c8c8 with SMTP id b22-20020a17090a10d600b001bc48adc8c8mr13142931pje.149.1647037757674; Fri, 11 Mar 2022 14:29:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1647037757; cv=none; d=google.com; s=arc-20160816; b=KMDPuuXxIKWTk0HuujfCGiOJJqMiu6PLMcyh9BMQufVn6PKABVlApd0WU31fIle2yv Q6UTXt1EmRmU7LM1+g+MnVJ5MRMymgcCM0lrcajFmiQr+zfT9IWF1NDbW9nzCqpU8tTJ R9Wf0gwLInILPjgUMENezU1yZ3PpEwMOlVIR5lYtIko9DDjZXSRd/fQvX+EQhMzPiFer 1oBfly3L6gEFwivhOjaWPkOb45ksEc/DFxUOmmIRxnjDf6AvXU1czbsLq+2vCnOHKh3S 7mu2FrZaD1zL7D4ZevFn+yS6iuQbKKAho30D0+78+EuWHbw+MoN/OJsGha0zZAzFT6GH GTKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=xAPmmMdvQVAdWSHdda/tQmqd/l6ufuFQnvJSs9srOYw=; b=cITHIrtWXx6SDeZV8oWANHWiTRNp2ZwdeF/tuf0TPC8gupRLPHrEpv7VEtPulJuC+I FgWpEUNX+Y76FI15wJzRSURTOc59Udwd3nD5NvviwLYoYac5Y7lzjSYXOKhEKlgKQxH1 X9YlVJIoyabg+vdx7/GOLRK/tMpFbrb3AUR52/eZ5mUlwos6LYW0NLS5Cd0F5VZCqNmp EuFfHB6EDnLj2CRtLXoyUjUa0uLSHIaSDVb8Ba5CcicBqySKOAtHmdJA42/NRYgCq41Q mEmdEQ4CIv7+NQ2Bv/gbUoUI6R9OdyGE2eByVPlrrUWILb4T84ixenhAcHa1kX9Xw9Sc XjsQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=DsT5G4Aw; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id o187-20020a6341c4000000b0038057d376f8si9404050pga.249.2022.03.11.14.29.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Mar 2022 14:29:17 -0800 (PST) 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; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=DsT5G4Aw; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 15FC02EDE53; Fri, 11 Mar 2022 13:38:26 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240521AbiCKQdL (ORCPT + 99 others); Fri, 11 Mar 2022 11:33:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345185AbiCKQcx (ORCPT ); Fri, 11 Mar 2022 11:32:53 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 770591AD976 for ; Fri, 11 Mar 2022 08:31:49 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 1065A61C0D for ; Fri, 11 Mar 2022 16:31:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 63396C340E9; Fri, 11 Mar 2022 16:31:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1647016308; bh=zZYK+aZkPSFlvOSU6TMUdz/2r2Ym7F/rak/o3ntO1Kw=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=DsT5G4AwPGYZggMhMinXiAQxRQLkviiEPW/oixIIkQUgglQ6S2dyNtUpOz6PLI9wC V8CxUNAugBoZupYKqyY+Hsruv8P0CPfXYpQOCx4Rzkb4LZge4RXZLxDSWO1rvX0Ol9 T/5vfLZ0vQce5XgG0lTNBuY5unZw6XWMApfPVBvnrTPGmvxTgbL5l0WHhj3Lxcp2j/ PysvciDiFFlHDJ8/ZY/jjeENE4tHKhTENxaYIg80GtYY/P9Vb0BsJVBAOXluSBidMD F9MPJMcrLnjUKP8fmwpr9KR0niAkSsn5JccMhFTTkPBg0WiKwv3d6/CQlX4Mmfe9ja G3qE3VBgQ/YSg== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id ED0195C0140; Fri, 11 Mar 2022 08:31:47 -0800 (PST) Date: Fri, 11 Mar 2022 08:31:47 -0800 From: "Paul E. McKenney" To: Eric Dumazet Cc: linux-kernel , Eric Dumazet , "Rafael J . Wysocki" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , x86@kernel.org Subject: Re: [PATCH] x86/cpu: use smp_call_function_many() in arch_freq_prepare_all() Message-ID: <20220311163147.GI4285@paulmck-ThinkPad-P17-Gen-1> Reply-To: paulmck@kernel.org References: <20220311011715.2440601-1-eric.dumazet@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220311011715.2440601-1-eric.dumazet@gmail.com> X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 Thu, Mar 10, 2022 at 05:17:15PM -0800, Eric Dumazet wrote: > From: Eric Dumazet > > Opening /proc/cpuinfo can have a big latency on hosts with many cpus, > mostly because it is essentially doing: > > for_each_online_cpu(cpu) > smp_call_function_single(cpu, aperfmperf_snapshot_khz, ...) > > smp_call_function_single() is reusing a common csd, meaning that > each invocation needs to wait for completion of the prior one. > > Paul recent patches have lowered number of cpus receiving the IPI, > but there are still cases where the latency of the above loop can > reach 10 ms, then an extra msleep(10) is performed, for a total of 20ms. > > Using smp_call_function_many() allows for full parallelism, > and latency is down to ~80 usec, on a host with 256 cpus. > > Signed-off-by: Eric Dumazet Nice!!! Acked-by: Paul E. McKenney > Cc: Paul E. McKenney > Cc: Rafael J. Wysocki > Cc: Thomas Gleixner > Cc: Ingo Molnar > Cc: Borislav Petkov > Cc: "H. Peter Anvin" > Cc: > --- > arch/x86/kernel/cpu/aperfmperf.c | 32 +++++++++++++++++++++++--------- > 1 file changed, 23 insertions(+), 9 deletions(-) > > diff --git a/arch/x86/kernel/cpu/aperfmperf.c b/arch/x86/kernel/cpu/aperfmperf.c > index 22911deacb6e441ad60ddb57190ef3772afb3cf0..a305310ceb44784a0ad9be7c196061d98fa1adbc 100644 > --- a/arch/x86/kernel/cpu/aperfmperf.c > +++ b/arch/x86/kernel/cpu/aperfmperf.c > @@ -67,7 +67,8 @@ static void aperfmperf_snapshot_khz(void *dummy) > atomic_set_release(&s->scfpending, 0); > } > > -static bool aperfmperf_snapshot_cpu(int cpu, ktime_t now, bool wait) > +static bool aperfmperf_snapshot_cpu(int cpu, ktime_t now, bool wait, > + struct cpumask *mask) > { > s64 time_delta = ktime_ms_delta(now, per_cpu(samples.time, cpu)); > struct aperfmperf_sample *s = per_cpu_ptr(&samples, cpu); > @@ -76,9 +77,13 @@ static bool aperfmperf_snapshot_cpu(int cpu, ktime_t now, bool wait) > if (time_delta < APERFMPERF_CACHE_THRESHOLD_MS) > return true; > > - if (!atomic_xchg(&s->scfpending, 1) || wait) > - smp_call_function_single(cpu, aperfmperf_snapshot_khz, NULL, wait); > - > + if (!atomic_xchg(&s->scfpending, 1) || wait) { > + if (mask) > + __cpumask_set_cpu(cpu, mask); > + else > + smp_call_function_single(cpu, aperfmperf_snapshot_khz, > + NULL, wait); > + } > /* Return false if the previous iteration was too long ago. */ > return time_delta <= APERFMPERF_STALE_THRESHOLD_MS; > } > @@ -97,13 +102,14 @@ unsigned int aperfmperf_get_khz(int cpu) > if (rcu_is_idle_cpu(cpu)) > return 0; /* Idle CPUs are completely uninteresting. */ > > - aperfmperf_snapshot_cpu(cpu, ktime_get(), true); > + aperfmperf_snapshot_cpu(cpu, ktime_get(), true, NULL); > return per_cpu(samples.khz, cpu); > } > > void arch_freq_prepare_all(void) > { > ktime_t now = ktime_get(); > + cpumask_var_t mask; > bool wait = false; > int cpu; > > @@ -113,17 +119,25 @@ void arch_freq_prepare_all(void) > if (!boot_cpu_has(X86_FEATURE_APERFMPERF)) > return; > > + if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) > + return; > + > + cpus_read_lock(); > for_each_online_cpu(cpu) { > if (!housekeeping_cpu(cpu, HK_FLAG_MISC)) > continue; > if (rcu_is_idle_cpu(cpu)) > continue; /* Idle CPUs are completely uninteresting. */ > - if (!aperfmperf_snapshot_cpu(cpu, now, false)) > + if (!aperfmperf_snapshot_cpu(cpu, now, false, mask)) > wait = true; > } > > - if (wait) > - msleep(APERFMPERF_REFRESH_DELAY_MS); > + preempt_disable(); > + smp_call_function_many(mask, aperfmperf_snapshot_khz, NULL, wait); > + preempt_enable(); > + cpus_read_unlock(); > + > + free_cpumask_var(mask); > } > > unsigned int arch_freq_get_on_cpu(int cpu) > @@ -139,7 +153,7 @@ unsigned int arch_freq_get_on_cpu(int cpu) > if (!housekeeping_cpu(cpu, HK_FLAG_MISC)) > return 0; > > - if (aperfmperf_snapshot_cpu(cpu, ktime_get(), true)) > + if (aperfmperf_snapshot_cpu(cpu, ktime_get(), true, NULL)) > return per_cpu(samples.khz, cpu); > > msleep(APERFMPERF_REFRESH_DELAY_MS); > -- > 2.35.1.723.g4982287a31-goog >