Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp3531198ybl; Mon, 3 Feb 2020 02:00:13 -0800 (PST) X-Google-Smtp-Source: APXvYqyNRDSNSqIXE46tEsM+gbii/x6UW+0ENArwbpAO+CWd1AK9Ab8EwZFN9BO2pjmoyjNHkpLh X-Received: by 2002:a05:6830:1e72:: with SMTP id m18mr16115291otr.226.1580724012863; Mon, 03 Feb 2020 02:00:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580724012; cv=none; d=google.com; s=arc-20160816; b=izQZFBXXQ2OBDKWsbrKH5T+m207YtcJWlWyHhSbL0gizuUEjl+Aq2K1h3i/lcQP5Pk XnRcLAvyh5M0YdhLcks1sI/S/eseMh/LRhEd6xA9jq4YRNBsv2IFEp7mxe2bj6spsv+U glcQWUaXxcR3I8umyPcjRSz6/oq7f+L22aaVmaXP9Ul2c8Yuo5M8r/095Af2Snnv7DKm uf0D2jymLQXHMZbrMg7uR9geGr9KwnMs5EciEjqragBE49DZWp3MOFX6HvrjB7Z5KaZv TAeoMeJu/12Qj6vdApg0OjHkHjmqbTRze7p1yEC47zTGPucTXKfyrYyi4kYljRkOh4EY VEGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=vgFT7KCLvNSNXKaIMj+AMJeeoDZdmCntNB9bIaSyhR8=; b=1IyI882Kc5UcTG38gt349JefGC7bAZZr9JXOjd3pa+u48MdMiQuPMyG3Sx8+tsGf7k 8j1+TcE6tC/OvxAHt2G1mpaYMYnGIeg/Of8s7Q6Zq4KOW4gUjCgSTc0wfaz6hDamj87y BoQw5JhAeq91nTnfdRuBrOjCob+Fbu0PC5uox9dSw7okOPINnB+fRR+OPawpdVcLg21R 4CMUXxj5hFeibkAT007Eu37XfAaKc1AhB1rZ/zITbBeC90R9jgTRf6xCWqim/q8SG57Z 5/Q1yboeI7MC1Tvr7AA3i7AMkUk/RR1Aa+WUTLSyL8/lwLUrAu8FC7ocbAfDBHmZh6R5 yHAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=haYQFwfe; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p11si9360940ota.300.2020.02.03.02.00.01; Mon, 03 Feb 2020 02:00:12 -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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=haYQFwfe; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727793AbgBCIcF (ORCPT + 99 others); Mon, 3 Feb 2020 03:32:05 -0500 Received: from mail-oi1-f194.google.com ([209.85.167.194]:41410 "EHLO mail-oi1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726994AbgBCIcF (ORCPT ); Mon, 3 Feb 2020 03:32:05 -0500 Received: by mail-oi1-f194.google.com with SMTP id i1so13931481oie.8; Mon, 03 Feb 2020 00:32:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=vgFT7KCLvNSNXKaIMj+AMJeeoDZdmCntNB9bIaSyhR8=; b=haYQFwferv5+ZHvfqKEEV/YUXLdSYxjZlN0P4WdvN1Okw1asRKqpj1d86PDYFJ0bNa xbKaXhElGXZr/ely1gzZrQrCPFVTjVuXzwzXhE/ijyl7iQ3LoLSiswWk38Q253tqBw0q 1nw/VQSBppW6Lk0PvkCsBqDNGWtWJEcmhb1puBaNLNh9vBH58UgOLbDbbnoKpofIKRJj xFnaM4t2BEeo3hEnz+THrNEobj8VkLSIs7bjRb1vExJYwtzyJwskDSjlgvULeWME5Iji PYA4PfHdddbpHlCPtScYnFwV9mMQqUaPVWeQZt0ezObLlBgGIrTaE2+qmR9/nWgEykGc BkIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=vgFT7KCLvNSNXKaIMj+AMJeeoDZdmCntNB9bIaSyhR8=; b=EOl0CDIIY2/7YIbcHUNkN5gnaBypwjCyFGvU86Uu46s6wkgGmj519FbjWDL+DS2q40 3Ca7h3uqEJEZ2ruWWBtw09wSrJh/Gn4U4faF294EiFqWaWK/xdIHgAZBic5aw9tOvGTA vhf4Z5FSB0PNVPg64ki8I6FII0s98+zLfLr4wBhUAjXn8zMSw6MmLxBu/jACPdV3Px/b qeaAL0dSSMq3x14QpFkLQMT8lTrx1+gyJAlAkkQ9ZKmTNObYT8nL1cDHX948f7rjr0Ra C4OHxwB8Gz7r47QU3mZ9NDKrjuhcyQn/3Q1wfNkgVZAQoSI8sDV/qW35fz/GRnEJtnXx m9fg== X-Gm-Message-State: APjAAAWrohb13bDmGVSO7xheuxlJuHxF7nURN9vpGywJmY9MvrOuIRkb 9fmM4zD6LN+yW/I4illpths/vCwcEPdSyFQ3mEQ= X-Received: by 2002:aca:44d7:: with SMTP id r206mr5716393oia.33.1580718724066; Mon, 03 Feb 2020 00:32:04 -0800 (PST) MIME-Version: 1.0 References: <20200127071602.11460-1-nick.desaulniers@gmail.com> In-Reply-To: <20200127071602.11460-1-nick.desaulniers@gmail.com> From: Wanpeng Li Date: Mon, 3 Feb 2020 16:31:52 +0800 Message-ID: Subject: Re: [PATCH] dynamically allocate struct cpumask To: Nick Desaulniers Cc: Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , "H. Peter Anvin" , "the arch/x86 maintainers" , kvm , LKML , clang-built-linux@googlegroups.com Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Nick, On Mon, 27 Jan 2020 at 15:16, Nick Desaulniers wrote: > > This helps avoid avoid a potentially large stack allocation. > > 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. Could you help test the below untested patch? From 867753e2fa27906f15df7902ba1bce7f9cef6ebe Mon Sep 17 00:00:00 2001 From: Wanpeng Li Date: Mon, 3 Feb 2020 16:26:35 +0800 Subject: [PATCH] KVM: Pre-allocate 1 cpumask variable per cpu for both pv tlb and pv ipis Reported-by: 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 = 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); #ifdef CONFIG_PARAVIRT_SPINLOCKS -- 1.8.3.1