Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp2098943pxb; Fri, 25 Mar 2022 11:03:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwRwVRi/moC2zBobTHkpwF9sT2Bud3ooID7IVtwbWvKUqdPPV3kq2VyNuuEisrRkHlb3BmH X-Received: by 2002:a17:902:8506:b0:154:8692:a7ac with SMTP id bj6-20020a170902850600b001548692a7acmr13041234plb.10.1648231436780; Fri, 25 Mar 2022 11:03:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648231436; cv=none; d=google.com; s=arc-20160816; b=dR2AvKT7bjpKCe7/DWtcNlnH5EfBzYDUqRMVPt+EdhxJYDzvih1fSnTGSHsz+yoTJx 97gIzkOg0ufiCKp2hwMT03RxqlbQHlvapQtkFryaHXdZKd7s6E/uZ7LKLQXC6rvZ/nDh CtyMj1fp+oKXpQFfbo/VVwAWwn3q5wMCGFHO1YROfg5MFZWjwqBY4d7vkeDoYOuwqOut saO3aZVHOOCG3Aj/4NO2/vB17pMwEz5fLZ7EpNzsENryV2dFx+aIwq/9IOeDMowrYQmo PYQTwGSqAmxOKM+ItD65fguE59Pvg1Rhpa1qV/H6yV8yeHVZKaiChHnc6DXgBI6QjGBV L2wA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=oNdgroiW0MXjQ1AJ+7byePTTsvQ9RWf63r2qZjoIhzM=; b=mwzDWqD9mewui2wCfLRhOVFEGz0SFwbZTf+1Thz9JZor85QB4s5d9DnFptki0j3DR9 E7lUClZyOlwPELLPj95uSHiymRUlw0n+U/DfoncyQMwuyjp3xBuBaadIKf+IC8Zp1v3F sSo7VRz5EskotXnQb80c6iBl9g9bA0VcMlVRSe0WNG0zeQMct8joJZ8tItbz9MFs0rhH Jq8ALqrnmN6nWhKaxnjtVmC+3wpn0EqUQJkoh8dwBNHMN2naMf4E86Jyk03zOyhdYf2Y T3VuGlGKM9aZozdNdgxXNLXSA7TeEvz8/Ls41aRxeNh7+QtK/yjFkyMA7fOIvMlJxjFv 1fRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=BDixQbSm; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id n27-20020a63a51b000000b003816ff876bfsi3226396pgf.5.2022.03.25.11.03.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Mar 2022 11:03:56 -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=@google.com header.s=20210112 header.b=BDixQbSm; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 4BDE3129254; Fri, 25 Mar 2022 10:39:08 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245359AbiCWQHh (ORCPT + 99 others); Wed, 23 Mar 2022 12:07:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41794 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234639AbiCWQHg (ORCPT ); Wed, 23 Mar 2022 12:07:36 -0400 Received: from mail-yw1-x1135.google.com (mail-yw1-x1135.google.com [IPv6:2607:f8b0:4864:20::1135]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C09421706C for ; Wed, 23 Mar 2022 09:06:02 -0700 (PDT) Received: by mail-yw1-x1135.google.com with SMTP id 00721157ae682-2e5827a76f4so22583977b3.6 for ; Wed, 23 Mar 2022 09:06:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=oNdgroiW0MXjQ1AJ+7byePTTsvQ9RWf63r2qZjoIhzM=; b=BDixQbSmDLCatKCAqmxGMt8khsgiM9XapdUq+Gh3JZZv3gPFoi0P0fw0k42KHOE2H/ RwAYQyYcIZkdR36tf3ycyu1/5r+e/m4RQpQvneCZZfN4jkFOgngZXY/23sGoYLaiqv2p yFcfYkvkV21WjEOkWrQzKXPOF3HCapNpdpJlOq/Etx3bwae2mpI/m1JWC4H33F0FkfqG 05fGFRVSXuYZtZMiGxqM3X1Hh/O5U6tTn+z1FucZ9+l3LER1X89AAMZNeFu0znsrZvKq /NyKKJvrGZprsSZhDqoyFsaQBUkPZy7GDtaEBJ6hPSjFUa9hxiG2d7cueEq5o5Wq2Ju/ bP8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=oNdgroiW0MXjQ1AJ+7byePTTsvQ9RWf63r2qZjoIhzM=; b=sigysFJ6FIHznVEl8dAiTe3BgV3jTvGlyt542i7wXj/MO5LdgmHf8FXZjChSrGq8T2 f/ZTCpJxLeQWKdcC2kJqDzXd0juO9wq27xJWK2BPHQcUwa+sAAx5+qXqtRVPDNA/Q+1p TAX3PjTurd1xRi5lJOl65BFDFKxilUD8fd9AZOLzA4LHWeJSbMnxbywNgJrc7SGRP1zN BzGRbGKnW0aUKq/35RRZ1Dn+d8f++2TFls8BiaQHSJzMGJuAALLDlxo2PdJcOh7ZJSVr MRekEUCxQ7VVUmBnV7QcsJkBsSejBQx/erej3FafEKwwT9+5Yl6sLMmOv9Ha0RopeQfT 3Djw== X-Gm-Message-State: AOAM532JFyJ8bKJnpGZ5J3siY+n+G0AywVHiCWPRGB/rp/Qh7SnyQRWH tJfhdKURoOeZ1tCQSx6qHUEeHYV7v4XdgG6ttHeStg== X-Received: by 2002:a05:690c:809:b0:2e5:a6cb:bccd with SMTP id bx9-20020a05690c080900b002e5a6cbbccdmr605457ywb.47.1648051561524; Wed, 23 Mar 2022 09:06:01 -0700 (PDT) MIME-Version: 1.0 References: <20220311011715.2440601-1-eric.dumazet@gmail.com> <14cceb7c-f5f7-ee8e-18aa-4884ef212eb0@intel.com> In-Reply-To: <14cceb7c-f5f7-ee8e-18aa-4884ef212eb0@intel.com> From: Eric Dumazet Date: Wed, 23 Mar 2022 09:05:50 -0700 Message-ID: Subject: Re: [PATCH] x86/cpu: use smp_call_function_many() in arch_freq_prepare_all() To: "Rafael J. Wysocki" Cc: Eric Dumazet , "Paul E . McKenney" , linux-kernel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , "the arch/x86 maintainers" Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL 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 Fri, Mar 11, 2022 at 8:36 AM Rafael J. Wysocki wrote: > > On 3/11/2022 2:17 AM, 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. > > This looks reasonable to me. > > Acked-by: Rafael J. Wysocki > > or if you want me to pick it up, please resend the patch with a CC to > linux-pm@vger.kernel.org. I do not know what x86 maintainers prefer ? Let them give their advice here, thanks ! > > > Signed-off-by: Eric Dumazet > > 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); > >