Skip to content

Commit 8a9442f

Browse files
Wanpeng Libonzini
authored andcommitted
KVM: Pre-allocate 1 cpumask variable per cpu for both pv tlb and pv ipis
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 <[email protected]> Acked-by: Nick Desaulniers <[email protected]> Reviewed-by: Vitaly Kuznetsov <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Nick Desaulniers <[email protected]> Signed-off-by: Wanpeng Li <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent a262bca commit 8a9442f

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

arch/x86/kernel/kvm.c

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,8 @@ static bool pv_tlb_flush_supported(void)
432432
kvm_para_has_feature(KVM_FEATURE_STEAL_TIME));
433433
}
434434

435+
static DEFINE_PER_CPU(cpumask_var_t, __pv_cpu_mask);
436+
435437
#ifdef CONFIG_SMP
436438

437439
static bool pv_ipi_supported(void)
@@ -510,12 +512,12 @@ static void kvm_send_ipi_mask(const struct cpumask *mask, int vector)
510512
static void kvm_send_ipi_mask_allbutself(const struct cpumask *mask, int vector)
511513
{
512514
unsigned int this_cpu = smp_processor_id();
513-
struct cpumask new_mask;
515+
struct cpumask *new_mask = this_cpu_cpumask_var_ptr(__pv_cpu_mask);
514516
const struct cpumask *local_mask;
515517

516-
cpumask_copy(&new_mask, mask);
517-
cpumask_clear_cpu(this_cpu, &new_mask);
518-
local_mask = &new_mask;
518+
cpumask_copy(new_mask, mask);
519+
cpumask_clear_cpu(this_cpu, new_mask);
520+
local_mask = new_mask;
519521
__send_ipi_mask(local_mask, vector);
520522
}
521523

@@ -595,15 +597,14 @@ static void __init kvm_apf_trap_init(void)
595597
update_intr_gate(X86_TRAP_PF, async_page_fault);
596598
}
597599

598-
static DEFINE_PER_CPU(cpumask_var_t, __pv_tlb_mask);
599600

600601
static void kvm_flush_tlb_others(const struct cpumask *cpumask,
601602
const struct flush_tlb_info *info)
602603
{
603604
u8 state;
604605
int cpu;
605606
struct kvm_steal_time *src;
606-
struct cpumask *flushmask = this_cpu_cpumask_var_ptr(__pv_tlb_mask);
607+
struct cpumask *flushmask = this_cpu_cpumask_var_ptr(__pv_cpu_mask);
607608

608609
cpumask_copy(flushmask, cpumask);
609610
/*
@@ -642,6 +643,7 @@ static void __init kvm_guest_init(void)
642643
if (pv_tlb_flush_supported()) {
643644
pv_ops.mmu.flush_tlb_others = kvm_flush_tlb_others;
644645
pv_ops.mmu.tlb_remove_table = tlb_remove_table;
646+
pr_info("KVM setup pv remote TLB flush\n");
645647
}
646648

647649
if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
@@ -748,24 +750,31 @@ static __init int activate_jump_labels(void)
748750
}
749751
arch_initcall(activate_jump_labels);
750752

751-
static __init int kvm_setup_pv_tlb_flush(void)
753+
static __init int kvm_alloc_cpumask(void)
752754
{
753755
int cpu;
756+
bool alloc = false;
754757

755758
if (!kvm_para_available() || nopv)
756759
return 0;
757760

758-
if (pv_tlb_flush_supported()) {
761+
if (pv_tlb_flush_supported())
762+
alloc = true;
763+
764+
#if defined(CONFIG_SMP)
765+
if (pv_ipi_supported())
766+
alloc = true;
767+
#endif
768+
769+
if (alloc)
759770
for_each_possible_cpu(cpu) {
760-
zalloc_cpumask_var_node(per_cpu_ptr(&__pv_tlb_mask, cpu),
771+
zalloc_cpumask_var_node(per_cpu_ptr(&__pv_cpu_mask, cpu),
761772
GFP_KERNEL, cpu_to_node(cpu));
762773
}
763-
pr_info("KVM setup pv remote TLB flush\n");
764-
}
765774

766775
return 0;
767776
}
768-
arch_initcall(kvm_setup_pv_tlb_flush);
777+
arch_initcall(kvm_alloc_cpumask);
769778

770779
#ifdef CONFIG_PARAVIRT_SPINLOCKS
771780

0 commit comments

Comments
 (0)