Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp5031363ybl; Tue, 4 Feb 2020 06:30:07 -0800 (PST) X-Google-Smtp-Source: APXvYqyqSJ3R4nh2uiijf4cKUQVTWwF6X4wT9I8KldRTG+aRBmpVzS5E2wgKEBLFTD2Aum8ien+C X-Received: by 2002:aca:ea43:: with SMTP id i64mr3690386oih.30.1580826607249; Tue, 04 Feb 2020 06:30:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580826607; cv=none; d=google.com; s=arc-20160816; b=viIY+OzpSJVbWlxh5ge9rB55ybCYLvSLF29okP6E+rje+T4KG7HcuQ8e56w4HAwm7K gBi0nVOfXtcXemvDr0Jdb96Kup6v4lTO9YWHkwfXxFIJe1gJZpgCYRp6iAektQt3Om63 uIzNJE/Lbphg5t8iyeCYwt7WnAOdwLcWSDc92rd3v8ecTVGr4R7ml62ROYNJDZYpxA/g buVgZJD9DU2v/+lVyj5IAciuxRgwOuiFtCZeW4yyjPkFrloR2THy1VZrlyn10/qfXpBX VgD7wSVSNCzTK/lyRreEau9OE89X39/eMRdaNjOMOrcbKKUMtx/1uq57APzX1edmm8vb a9CA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=h2P5Toi+LgLIeMkvgZXesgrSwA0XeYA3h4RfUN7dBBQ=; b=KqT3lm5bxSNUIf9Zt/IKsDsEy4sJrC/gv/y+/cGiJFe+p6Ap7Ho8SJGJnTEYA4S7XS SyL2QMP0xsRMSFC8bxhTdB2AD+vGDHRIVdlazMSIRyyHbVikwRRXLmhQ4dWq304OrvFS RPvUU/nqSk9QKyt5vRIZUJ4P9HRtgjkTP0fhJq2TWM2eIum2kysZYd6xpgdvYBgF4ClJ v4RpXkqgTNCJn6jUU5hmc/4c2jIIW+yJRmB4X19oF1nun1JK+9n5n4/Zd9pONFdlC7M0 WTaQRh49t2Mr+mlZfamaJGZZi3mzvHWcrr26ncAiJfO6Vw3VgGhP/NIqRfcSp/15nndS iR9A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=canonical.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t127si9636894oih.45.2020.02.04.06.29.54; Tue, 04 Feb 2020 06:30:07 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=canonical.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727321AbgBDO1t (ORCPT + 99 others); Tue, 4 Feb 2020 09:27:49 -0500 Received: from youngberry.canonical.com ([91.189.89.112]:53185 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727230AbgBDO1s (ORCPT ); Tue, 4 Feb 2020 09:27:48 -0500 Received: from [187.32.88.249] (helo=calabresa) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1iyzB0-0001Ay-EI; Tue, 04 Feb 2020 14:27:44 +0000 Date: Tue, 4 Feb 2020 11:27:33 -0300 From: Thadeu Lima de Souza Cascardo To: Wanpeng Li Cc: Vitaly Kuznetsov , LKML , kvm , Paolo Bonzini , Sean Christopherson , Wanpeng Li , Jim Mattson , Joerg Roedel Subject: Re: [PATCH] KVM: Pre-allocate 1 cpumask variable per cpu for both pv tlb and pv ipis Message-ID: <20200204142733.GI40679@calabresa> References: <878slio6hp.fsf@vitty.brq.redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.12.2 (2019-09-21) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Feb 04, 2020 at 09:09:59PM +0800, Wanpeng Li wrote: > Cc Thadeu, > On Tue, 4 Feb 2020 at 20:57, Vitaly Kuznetsov wrote: > > > > Wanpeng Li writes: > > > > > From: Wanpeng Li > > > > > > Nick Desaulniers Reported: > > > > > > When building with: > > > $ make CC=clang arch/x86/ CFLAGS=-Wframe-larger-than=1000 > > > The following warning is observed: > > > arch/x86/kernel/kvm.c:494:13: warning: stack frame size of 1064 bytes in > > > function 'kvm_send_ipi_mask_allbutself' [-Wframe-larger-than=] > > > static void kvm_send_ipi_mask_allbutself(const struct cpumask *mask, int > > > vector) > > > ^ > > > Debugging with: > > > https://github.com/ClangBuiltLinux/frame-larger-than > > > via: > > > $ python3 frame_larger_than.py arch/x86/kernel/kvm.o \ > > > kvm_send_ipi_mask_allbutself > > > points to the stack allocated `struct cpumask newmask` in > > > `kvm_send_ipi_mask_allbutself`. The size of a `struct cpumask` is > > > potentially large, as it's CONFIG_NR_CPUS divided by BITS_PER_LONG for > > > the target architecture. CONFIG_NR_CPUS for X86_64 can be as high as > > > 8192, making a single instance of a `struct cpumask` 1024 B. > > > > > > This patch fixes it by pre-allocate 1 cpumask variable per cpu and use it for > > > both pv tlb and pv ipis.. > > > > > > Reported-by: Nick Desaulniers > > > Acked-by: Nick Desaulniers > > > Cc: Peter Zijlstra > > > Cc: Nick Desaulniers > > > Signed-off-by: Wanpeng Li > > > --- > > > arch/x86/kernel/kvm.c | 33 +++++++++++++++++++++------------ > > > 1 file changed, 21 insertions(+), 12 deletions(-) > > > > > > diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c > > > index 81045aab..b1e8efa 100644 > > > --- a/arch/x86/kernel/kvm.c > > > +++ b/arch/x86/kernel/kvm.c > > > @@ -425,6 +425,8 @@ static void __init sev_map_percpu_data(void) > > > } > > > } > > > > > > +static DEFINE_PER_CPU(cpumask_var_t, __pv_cpu_mask); > > > + > > > #ifdef CONFIG_SMP > > > #define KVM_IPI_CLUSTER_SIZE (2 * BITS_PER_LONG) > > > > > > @@ -490,12 +492,12 @@ static void kvm_send_ipi_mask(const struct > > > cpumask *mask, int vector) > > > static void kvm_send_ipi_mask_allbutself(const struct cpumask *mask, > > > int vector) > > > { > > > unsigned int this_cpu = smp_processor_id(); > > > - struct cpumask new_mask; > > > + struct cpumask *new_mask = this_cpu_cpumask_var_ptr(__pv_cpu_mask); > > > const struct cpumask *local_mask; > > > > > > - cpumask_copy(&new_mask, mask); > > > - cpumask_clear_cpu(this_cpu, &new_mask); > > > - local_mask = &new_mask; > > > + cpumask_copy(new_mask, mask); > > > + cpumask_clear_cpu(this_cpu, new_mask); > > > + local_mask = new_mask; > > > __send_ipi_mask(local_mask, vector); > > > } > > > > > > @@ -575,7 +577,6 @@ static void __init kvm_apf_trap_init(void) > > > update_intr_gate(X86_TRAP_PF, async_page_fault); > > > } > > > > > > -static DEFINE_PER_CPU(cpumask_var_t, __pv_tlb_mask); > > > > > > static void kvm_flush_tlb_others(const struct cpumask *cpumask, > > > const struct flush_tlb_info *info) > > > @@ -583,7 +584,7 @@ static void kvm_flush_tlb_others(const struct > > > cpumask *cpumask, > > > u8 state; > > > int cpu; > > > struct kvm_steal_time *src; > > > - struct cpumask *flushmask = this_cpu_cpumask_var_ptr(__pv_tlb_mask); > > > + struct cpumask *flushmask = this_cpu_cpumask_var_ptr(__pv_cpu_mask); > > > > > > cpumask_copy(flushmask, cpumask); > > > /* > > > @@ -624,6 +625,7 @@ static void __init kvm_guest_init(void) > > > kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) { > > > pv_ops.mmu.flush_tlb_others = kvm_flush_tlb_others; > > > pv_ops.mmu.tlb_remove_table = tlb_remove_table; > > > + pr_info("KVM setup pv remote TLB flush\n"); > > > } > > > > > > if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) > > > @@ -732,23 +734,30 @@ static __init int activate_jump_labels(void) > > > } > > > arch_initcall(activate_jump_labels); > > > > > > -static __init int kvm_setup_pv_tlb_flush(void) > > > +static __init int kvm_alloc_cpumask(void) > > > { > > > int cpu; > > > + bool alloc = false; > > > > > > if (kvm_para_has_feature(KVM_FEATURE_PV_TLB_FLUSH) && > > > !kvm_para_has_hint(KVM_HINTS_REALTIME) && > > > - kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) { > > > + kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) > > > + alloc = true; > > > + > > > +#if defined(CONFIG_SMP) > > > + if (!alloc && kvm_para_has_feature(KVM_FEATURE_PV_SEND_IPI)) > > > > '!alloc' check is superfluous. > > > > > + alloc = true; > > > +#endif > > > + > > > + if (alloc) > > > for_each_possible_cpu(cpu) { > > > - zalloc_cpumask_var_node(per_cpu_ptr(&__pv_tlb_mask, cpu), > > > + zalloc_cpumask_var_node(per_cpu_ptr(&__pv_cpu_mask, cpu), > > > GFP_KERNEL, cpu_to_node(cpu)); > > > } > > > - pr_info("KVM setup pv remote TLB flush\n"); > > > - } > > > > > > return 0; > > > } > > > -arch_initcall(kvm_setup_pv_tlb_flush); > > > +arch_initcall(kvm_alloc_cpumask); > > > > Honestly, I'd simplify the check in kvm_alloc_cpumask() as > > > > if (!kvm_para_available()) > > return; > > > > and allocated masks for all other cases. > > This will waste the memory if pv tlb and pv ipis are not exposed which > are the only users currently. > > Wanpeng I am more concerned about printing the "KVM setup pv remote TLB flush" message, not only when KVM pv is used, but pv TLB flush is not going to be used, but also when the system is not even paravirtualized. Cascardo.