dynamically allocate struct cpumask
Subsystems (3)
PATCH 1/1 dynamically allocate struct cpumask
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.--- arch/x86/kernel/kvm.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
arch/x86/kernel/kvm.c +6 -4
@@ -494,13 +494,15 @@ 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;const struct cpumask *local_mask; -cpumask_copy (&new_mask, mask); -cpumask_clear_cpu (this_cpu, &new_mask); - local_mask = &new_mask; + new_mask =kmalloc (sizeof (*new_mask),GFP_KERNEL ); +cpumask_copy (new_mask, mask); +cpumask_clear_cpu (this_cpu, new_mask); + local_mask = new_mask;__send_ipi_mask (local_mask, vector); +kfree (new_mask); }/*
Signature · 1 line
2.17.1
Quoted 31 lines · Nick Desaulniers
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.Signed-off-by Nick Desaulniers <[email protected]>--- arch/x86/kernel/kvm.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)arch/x86/kernel/kvm.c +0
-0
arch/x86/kernel/kvm.c static void kvm_send_ipi_mask(const struct cpumask *mask, int vector) · line 497
Quoted 4 lines
arch/x86/kernel/kvm.c +0
-1@@ -494,13 +494,15 @@ 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;
Right, on stack cpumask is definitely dodgy.
arch/x86/kernel/kvm.c static void kvm_send_ipi_mask(const struct cpumask *mask, int vector) · line 506
Quoted 13 lines
arch/x86/kernel/kvm.c +6
-3+struct cpumask *new_mask;const struct cpumask *local_mask; -cpumask_copy (&new_mask, mask); -cpumask_clear_cpu (this_cpu, &new_mask); - local_mask = &new_mask; + new_mask =kmalloc (sizeof (*new_mask),GFP_KERNEL ); +cpumask_copy (new_mask, mask); +cpumask_clear_cpu (this_cpu, new_mask); + local_mask = new_mask;__send_ipi_mask (local_mask, vector); +kfree (new_mask); }
One alternative approach is adding the inverse of cpu_bit_bitmap. I'm not entirely sure how often we need the all-but-self mask, but ISTR there were other places too.
Quoted 28 lines · Peter Zijlstra
Quoted 26 lines · Nick Desaulniers
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.Signed-off-by Nick Desaulniers <[email protected]>--- arch/x86/kernel/kvm.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
Quoted 19 lines
arch/x86/kernel/kvm.c static void kvm_send_ipi_mask(const struct cpumask *mask, int vector) · line 497
Quoted 9 lines
arch/x86/kernel/kvm.c +0
-1@@ -494,13 +494,15 @@ 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;Right, on stack cpumask is definitely dodgy.
Quoted 7 lines
arch/x86/kernel/kvm.c +2
-3+struct cpumask *new_mask;const struct cpumask *local_mask; -cpumask_copy (&new_mask, mask); -cpumask_clear_cpu (this_cpu, &new_mask); - local_mask = &new_mask; + new_mask =kmalloc (sizeof (*new_mask),GFP_KERNEL );
Probably should check for allocation failure, d'oh!
Quoted 10 lines
arch/x86/kernel/kvm.c static void kvm_send_ipi_mask(const struct cpumask *mask, int vector) · line 506
Quoted 6 lines
arch/x86/kernel/kvm.c +4
-0+cpumask_copy (new_mask, mask); +cpumask_clear_cpu (this_cpu, new_mask); + local_mask = new_mask;__send_ipi_mask (local_mask, vector); +kfree (new_mask); }One alternative approach is adding the inverse of
cpu_bit_bitmap. I'm not entirely sure how often we need the all-but-self mask, but ISTR there were other places too.
Vitaly Kuznetsov
reviewerQuoted 32 lines · Nick Desaulniers
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.Signed-off-by Nick Desaulniers <[email protected]>--- arch/x86/kernel/kvm.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)arch/x86/kernel/kvm.c +0
-0@@ -494,13 +494,15 @@ 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)
arch/x86/kernel/kvm.c static void kvm_send_ipi_mask(const struct cpumask *mask, int vector) · line 500
Quoted 10 lines
arch/x86/kernel/kvm.c +2
-4{unsigned int this_cpu =smp_processor_id (); -struct cpumask new_mask; +struct cpumask *new_mask;const struct cpumask *local_mask; -cpumask_copy (&new_mask, mask); -cpumask_clear_cpu (this_cpu, &new_mask); - local_mask = &new_mask; + new_mask =kmalloc (sizeof (*new_mask),GFP_KERNEL );
You could've used alloc_cpumask_var() instead, however, I think that memory allocation on this path is undesireable. We can always pre-allocate 1 cpumask variable per cpu and use it every time, e.g. we do this for Hyper-V.
Quoted 8 lines
arch/x86/kernel/kvm.c +4
-0+cpumask_copy (new_mask, mask); +cpumask_clear_cpu (this_cpu, new_mask); + local_mask = new_mask;__send_ipi_mask (local_mask, vector); +kfree (new_mask); }/*
Signature · 1 line
Vitaly
Hi Nick,
Quoted 13 lines · Nick Desaulniers
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
Quoted 8 lines
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 <[email protected]>
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
--- arch/x86/kernel/kvm.c | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-)
arch/x86/kernel/kvm.c +2 -0
@@ -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
Nick Desaulniers
Acked-byQuoted 54 lines · Wanpeng Li
Hi Nick,
Quoted 13 lines · Nick Desaulniers
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-thanQuoted 8 lines
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
867753e2fa27906f15df7902ba1bce7f9cef6ebeMon Sep 17 00:00:00 2001
From: Wanpeng Li <[email protected]>
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 ipisReported-by Nick Desaulniers <[email protected]>Signed-off-by Wanpeng Li <[email protected]>--- arch/x86/kernel/kvm.c | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-)arch/x86/kernel/kvm.c +2
-0@@ -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)
arch/x86/kernel/kvm.c static void kvm_send_ipi_mask(const struct · line 497
Quoted 7 lines
arch/x86/kernel/kvm.c +1
-1@@ -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);
Yes, this should help reduce the stack usage, thanks.
Quoted 81 lines
arch/x86/kernel/kvm.c +3
-3const 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);}
arch/x86/kernel/kvm.c +1
-2@@ -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 structcpumask *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);/*arch/x86/kernel/kvm.c +14
-7@@ -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
--
You received this message because you are subscribed to the Google Groups "Clang Built Linux" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/clang-built-linux/CANRm%2BCwK0Cg45mktda9Yz9fsjPCvtuB8O%2Bfma5L3tV725ki1qw%40mail.gmail.com.
Signature · 2 lines
Thanks, ~Nick Desaulniers