* [PATCH 0/3] LoongArch: KVM: Some small enhanments
@ 2026-03-11 3:18 Bibo Mao
2026-03-11 3:18 ` [PATCH 1/3] LoongArch: KVM: Add kvm_request_pending checking in kvm_late_check_requests() Bibo Mao
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Bibo Mao @ 2026-03-11 3:18 UTC (permalink / raw)
To: Tianrui Zhao, Huacai Chen
Cc: kernel, kvm, loongarch, linux-kernel, Juergen Gross
There are some small enhancements about LoongArch KVM. There is
improvement about VM entry path in patch 1 and patch, add pending
request checking in advance and it is empty at most time.
Also change vcpu_is_preempted() from function to macro method, by actual
benchmark test such as unixbench. It is affected on physical machine
with function method. With the macro method, only one nop instruction
is added and there is no parameter parsing, there is no obvious
performance influence with physical machine.
Bibo Mao (3):
LoongArch: KVM: Add kvm_request_pending checking in
kvm_late_check_requests()
LoongArch: KVM: Move host CSR_EENTRY save in context switch
LoongArch: KVM: Set vcpu_is_preempted() macro rather than function
arch/loongarch/include/asm/qspinlock.h | 27 +++++++++++++++++++++-----
arch/loongarch/kernel/paravirt.c | 15 ++------------
arch/loongarch/kvm/vcpu.c | 24 +++++++++++------------
3 files changed, 36 insertions(+), 30 deletions(-)
base-commit: 1f318b96cc84d7c2ab792fcc0bfd42a7ca890681
--
2.39.3
^ permalink raw reply [flat|nested] 8+ messages in thread* [PATCH 1/3] LoongArch: KVM: Add kvm_request_pending checking in kvm_late_check_requests() 2026-03-11 3:18 [PATCH 0/3] LoongArch: KVM: Some small enhanments Bibo Mao @ 2026-03-11 3:18 ` Bibo Mao 2026-03-11 3:18 ` [PATCH 2/3] LoongArch: KVM: Move host CSR_EENTRY save in context switch Bibo Mao 2026-03-11 3:18 ` [PATCH 3/3] LoongArch: KVM: Set vcpu_is_preempted() macro rather than function Bibo Mao 2 siblings, 0 replies; 8+ messages in thread From: Bibo Mao @ 2026-03-11 3:18 UTC (permalink / raw) To: Tianrui Zhao, Huacai Chen Cc: kernel, kvm, loongarch, linux-kernel, Juergen Gross Add kvm_request_pending() checking firstly in kvm_late_check_requests(), at most time there is no pending request, the following pending bit checking can be skipped then. Also move function kvm_check_pmu() in kvm_late_check_requests(), and put it after kvm_request_pending() checking. Signed-off-by: Bibo Mao <maobibo@loongson.cn> --- arch/loongarch/kvm/vcpu.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c index 09e137f2f841..6104982b53c8 100644 --- a/arch/loongarch/kvm/vcpu.c +++ b/arch/loongarch/kvm/vcpu.c @@ -149,14 +149,6 @@ static void kvm_lose_pmu(struct kvm_vcpu *vcpu) kvm_restore_host_pmu(vcpu); } -static void kvm_check_pmu(struct kvm_vcpu *vcpu) -{ - if (kvm_check_request(KVM_REQ_PMU, vcpu)) { - kvm_own_pmu(vcpu); - vcpu->arch.aux_inuse |= KVM_LARCH_PMU; - } -} - static void kvm_update_stolen_time(struct kvm_vcpu *vcpu) { u32 version; @@ -232,6 +224,14 @@ static int kvm_check_requests(struct kvm_vcpu *vcpu) static void kvm_late_check_requests(struct kvm_vcpu *vcpu) { lockdep_assert_irqs_disabled(); + if (!kvm_request_pending(vcpu)) + return; + + if (kvm_check_request(KVM_REQ_PMU, vcpu)) { + kvm_own_pmu(vcpu); + vcpu->arch.aux_inuse |= KVM_LARCH_PMU; + } + if (kvm_check_request(KVM_REQ_TLB_FLUSH_GPA, vcpu)) if (vcpu->arch.flush_gpa != INVALID_GPA) { kvm_flush_tlb_gpa(vcpu, vcpu->arch.flush_gpa); @@ -312,7 +312,6 @@ static int kvm_pre_enter_guest(struct kvm_vcpu *vcpu) /* Make sure the vcpu mode has been written */ smp_store_mb(vcpu->mode, IN_GUEST_MODE); kvm_check_vpid(vcpu); - kvm_check_pmu(vcpu); /* * Called after function kvm_check_vpid() -- 2.39.3 ^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 2/3] LoongArch: KVM: Move host CSR_EENTRY save in context switch 2026-03-11 3:18 [PATCH 0/3] LoongArch: KVM: Some small enhanments Bibo Mao 2026-03-11 3:18 ` [PATCH 1/3] LoongArch: KVM: Add kvm_request_pending checking in kvm_late_check_requests() Bibo Mao @ 2026-03-11 3:18 ` Bibo Mao 2026-03-11 3:18 ` [PATCH 3/3] LoongArch: KVM: Set vcpu_is_preempted() macro rather than function Bibo Mao 2 siblings, 0 replies; 8+ messages in thread From: Bibo Mao @ 2026-03-11 3:18 UTC (permalink / raw) To: Tianrui Zhao, Huacai Chen Cc: kernel, kvm, loongarch, linux-kernel, Juergen Gross CSR register LOONGARCH_CSR_EENTRY is shared between host cpu and guest vCPU, KVM need save and restore LOONGARCH_CSR_EENTRY register. Here move CSR register LOONGARCH_CSR_EENTRY saving in context switch function, rather than VM enter entry. At most time VM enter/exit is much more frequent than vCPU thread context switch. Signed-off-by: Bibo Mao <maobibo@loongson.cn> --- arch/loongarch/kvm/vcpu.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c index 6104982b53c8..044627c6ddc7 100644 --- a/arch/loongarch/kvm/vcpu.c +++ b/arch/loongarch/kvm/vcpu.c @@ -319,7 +319,6 @@ static int kvm_pre_enter_guest(struct kvm_vcpu *vcpu) * and it may also clear KVM_REQ_TLB_FLUSH_GPA pending bit */ kvm_late_check_requests(vcpu); - vcpu->arch.host_eentry = csr_read64(LOONGARCH_CSR_EENTRY); /* Clear KVM_LARCH_SWCSR_LATEST as CSR will change when enter guest */ vcpu->arch.aux_inuse &= ~KVM_LARCH_SWCSR_LATEST; @@ -1624,9 +1623,11 @@ static int _kvm_vcpu_load(struct kvm_vcpu *vcpu, int cpu) * If not, any old guest state from this vCPU will have been clobbered. */ context = per_cpu_ptr(vcpu->kvm->arch.vmcs, cpu); - if (migrated || (context->last_vcpu != vcpu)) + if (migrated || (context->last_vcpu != vcpu)) { vcpu->arch.aux_inuse &= ~KVM_LARCH_HWCSR_USABLE; - context->last_vcpu = vcpu; + context->last_vcpu = vcpu; + vcpu->arch.host_eentry = csr_read64(LOONGARCH_CSR_EENTRY); + } /* Restore timer state regardless */ kvm_restore_timer(vcpu); -- 2.39.3 ^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 3/3] LoongArch: KVM: Set vcpu_is_preempted() macro rather than function 2026-03-11 3:18 [PATCH 0/3] LoongArch: KVM: Some small enhanments Bibo Mao 2026-03-11 3:18 ` [PATCH 1/3] LoongArch: KVM: Add kvm_request_pending checking in kvm_late_check_requests() Bibo Mao 2026-03-11 3:18 ` [PATCH 2/3] LoongArch: KVM: Move host CSR_EENTRY save in context switch Bibo Mao @ 2026-03-11 3:18 ` Bibo Mao 2026-03-11 3:35 ` Huacai Chen 2 siblings, 1 reply; 8+ messages in thread From: Bibo Mao @ 2026-03-11 3:18 UTC (permalink / raw) To: Tianrui Zhao, Huacai Chen Cc: kernel, kvm, loongarch, linux-kernel, Juergen Gross vcpu_is_preempted() is performance sensitive called in function osq_lock(), here set it as macro. So that parameter is not parsed at most time, it can avoid cache line thrashing across numa node. Here is part of unixbench result on 3C5000 DualWay machine with 32 Cores and 2 Numa node. origin with patch CONFIG_PARAVIRT disabled execl 6871.9 7134.2 7190.8 fstime 425.5 959.9 956.1 From the test result, with macro method it is almost the same with CONFIG_PARAVIRT disabled, and there is some improvment compared with function method. Signed-off-by: Bibo Mao <maobibo@loongson.cn> --- arch/loongarch/include/asm/qspinlock.h | 27 +++++++++++++++++++++----- arch/loongarch/kernel/paravirt.c | 15 ++------------ 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/arch/loongarch/include/asm/qspinlock.h b/arch/loongarch/include/asm/qspinlock.h index 66244801db67..305afd37cdcb 100644 --- a/arch/loongarch/include/asm/qspinlock.h +++ b/arch/loongarch/include/asm/qspinlock.h @@ -5,8 +5,10 @@ #include <linux/jump_label.h> #ifdef CONFIG_PARAVIRT - +#include <asm/kvm_para.h> DECLARE_STATIC_KEY_FALSE(virt_spin_lock_key); +DECLARE_STATIC_KEY_FALSE(virt_preempt_key); +DECLARE_PER_CPU(struct kvm_steal_time, steal_time); #define virt_spin_lock virt_spin_lock @@ -34,10 +36,25 @@ static inline bool virt_spin_lock(struct qspinlock *lock) return true; } -#define vcpu_is_preempted vcpu_is_preempted - -bool vcpu_is_preempted(int cpu); - +/* + * Macro is better than inline function here + * With inline function, parameter cpu is parsed even though it is not used. + * This may cause cache line thrashing across NUMA node. + * With macro method, parameter cpu is parsed only when it is used. + */ +#define vcpu_is_preempted(cpu) \ +({ \ + bool __val; \ + \ + if (!static_branch_unlikely(&virt_preempt_key)) \ + __val = false; \ + else { \ + struct kvm_steal_time *src; \ + src = &per_cpu(steal_time, cpu); \ + __val = !!(src->preempted & KVM_VCPU_PREEMPTED); \ + } \ + __val; \ +}) #endif /* CONFIG_PARAVIRT */ #include <asm-generic/qspinlock.h> diff --git a/arch/loongarch/kernel/paravirt.c b/arch/loongarch/kernel/paravirt.c index b74fe6db49ab..2d1206e486e2 100644 --- a/arch/loongarch/kernel/paravirt.c +++ b/arch/loongarch/kernel/paravirt.c @@ -10,8 +10,8 @@ #include <asm/paravirt.h> static int has_steal_clock; -static DEFINE_PER_CPU(struct kvm_steal_time, steal_time) __aligned(64); -static DEFINE_STATIC_KEY_FALSE(virt_preempt_key); +DEFINE_PER_CPU(struct kvm_steal_time, steal_time) __aligned(64); +DEFINE_STATIC_KEY_FALSE(virt_preempt_key); DEFINE_STATIC_KEY_FALSE(virt_spin_lock_key); static bool steal_acc = true; @@ -261,17 +261,6 @@ static int pv_time_cpu_down_prepare(unsigned int cpu) return 0; } -bool vcpu_is_preempted(int cpu) -{ - struct kvm_steal_time *src; - - if (!static_branch_unlikely(&virt_preempt_key)) - return false; - - src = &per_cpu(steal_time, cpu); - return !!(src->preempted & KVM_VCPU_PREEMPTED); -} -EXPORT_SYMBOL(vcpu_is_preempted); #endif static void pv_cpu_reboot(void *unused) -- 2.39.3 ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 3/3] LoongArch: KVM: Set vcpu_is_preempted() macro rather than function 2026-03-11 3:18 ` [PATCH 3/3] LoongArch: KVM: Set vcpu_is_preempted() macro rather than function Bibo Mao @ 2026-03-11 3:35 ` Huacai Chen 2026-03-11 3:47 ` Bibo Mao 0 siblings, 1 reply; 8+ messages in thread From: Huacai Chen @ 2026-03-11 3:35 UTC (permalink / raw) To: Bibo Mao Cc: Tianrui Zhao, kernel, kvm, loongarch, linux-kernel, Juergen Gross Hi, Bibo, On Wed, Mar 11, 2026 at 11:21 AM Bibo Mao <maobibo@loongson.cn> wrote: > > vcpu_is_preempted() is performance sensitive called in function > osq_lock(), here set it as macro. So that parameter is not parsed > at most time, it can avoid cache line thrashing across numa node. I remember that I had suggested you define vcpu_is_preempted() in the header file but you refused. Huacai > > Here is part of unixbench result on 3C5000 DualWay machine with 32 > Cores and 2 Numa node. > origin with patch CONFIG_PARAVIRT disabled > execl 6871.9 7134.2 7190.8 > fstime 425.5 959.9 956.1 > > From the test result, with macro method it is almost the same with > CONFIG_PARAVIRT disabled, and there is some improvment compared with > function method. > > Signed-off-by: Bibo Mao <maobibo@loongson.cn> > --- > arch/loongarch/include/asm/qspinlock.h | 27 +++++++++++++++++++++----- > arch/loongarch/kernel/paravirt.c | 15 ++------------ > 2 files changed, 24 insertions(+), 18 deletions(-) > > diff --git a/arch/loongarch/include/asm/qspinlock.h b/arch/loongarch/include/asm/qspinlock.h > index 66244801db67..305afd37cdcb 100644 > --- a/arch/loongarch/include/asm/qspinlock.h > +++ b/arch/loongarch/include/asm/qspinlock.h > @@ -5,8 +5,10 @@ > #include <linux/jump_label.h> > > #ifdef CONFIG_PARAVIRT > - > +#include <asm/kvm_para.h> > DECLARE_STATIC_KEY_FALSE(virt_spin_lock_key); > +DECLARE_STATIC_KEY_FALSE(virt_preempt_key); > +DECLARE_PER_CPU(struct kvm_steal_time, steal_time); > > #define virt_spin_lock virt_spin_lock > > @@ -34,10 +36,25 @@ static inline bool virt_spin_lock(struct qspinlock *lock) > return true; > } > > -#define vcpu_is_preempted vcpu_is_preempted > - > -bool vcpu_is_preempted(int cpu); > - > +/* > + * Macro is better than inline function here > + * With inline function, parameter cpu is parsed even though it is not used. > + * This may cause cache line thrashing across NUMA node. > + * With macro method, parameter cpu is parsed only when it is used. > + */ > +#define vcpu_is_preempted(cpu) \ > +({ \ > + bool __val; \ > + \ > + if (!static_branch_unlikely(&virt_preempt_key)) \ > + __val = false; \ > + else { \ > + struct kvm_steal_time *src; \ > + src = &per_cpu(steal_time, cpu); \ > + __val = !!(src->preempted & KVM_VCPU_PREEMPTED); \ > + } \ > + __val; \ > +}) > #endif /* CONFIG_PARAVIRT */ > > #include <asm-generic/qspinlock.h> > diff --git a/arch/loongarch/kernel/paravirt.c b/arch/loongarch/kernel/paravirt.c > index b74fe6db49ab..2d1206e486e2 100644 > --- a/arch/loongarch/kernel/paravirt.c > +++ b/arch/loongarch/kernel/paravirt.c > @@ -10,8 +10,8 @@ > #include <asm/paravirt.h> > > static int has_steal_clock; > -static DEFINE_PER_CPU(struct kvm_steal_time, steal_time) __aligned(64); > -static DEFINE_STATIC_KEY_FALSE(virt_preempt_key); > +DEFINE_PER_CPU(struct kvm_steal_time, steal_time) __aligned(64); > +DEFINE_STATIC_KEY_FALSE(virt_preempt_key); > DEFINE_STATIC_KEY_FALSE(virt_spin_lock_key); > > static bool steal_acc = true; > @@ -261,17 +261,6 @@ static int pv_time_cpu_down_prepare(unsigned int cpu) > return 0; > } > > -bool vcpu_is_preempted(int cpu) > -{ > - struct kvm_steal_time *src; > - > - if (!static_branch_unlikely(&virt_preempt_key)) > - return false; > - > - src = &per_cpu(steal_time, cpu); > - return !!(src->preempted & KVM_VCPU_PREEMPTED); > -} > -EXPORT_SYMBOL(vcpu_is_preempted); > #endif > > static void pv_cpu_reboot(void *unused) > -- > 2.39.3 > > ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 3/3] LoongArch: KVM: Set vcpu_is_preempted() macro rather than function 2026-03-11 3:35 ` Huacai Chen @ 2026-03-11 3:47 ` Bibo Mao 2026-03-11 4:38 ` Huacai Chen 0 siblings, 1 reply; 8+ messages in thread From: Bibo Mao @ 2026-03-11 3:47 UTC (permalink / raw) To: Huacai Chen Cc: Tianrui Zhao, kernel, kvm, loongarch, linux-kernel, Juergen Gross On 2026/3/11 上午11:35, Huacai Chen wrote: > Hi, Bibo, > > On Wed, Mar 11, 2026 at 11:21 AM Bibo Mao <maobibo@loongson.cn> wrote: >> >> vcpu_is_preempted() is performance sensitive called in function >> osq_lock(), here set it as macro. So that parameter is not parsed >> at most time, it can avoid cache line thrashing across numa node. > I remember that I had suggested you define vcpu_is_preempted() in the > header file but you refused. yes, I refuse then, that is a tragedy, I am sorry for that:( By some benchmarks, vcpu_is_preempted() is called in busy-loop method, more obvious with more CPUs, it may be a bottleneck. Regards Bibo Mao > > Huacai > >> >> Here is part of unixbench result on 3C5000 DualWay machine with 32 >> Cores and 2 Numa node. >> origin with patch CONFIG_PARAVIRT disabled >> execl 6871.9 7134.2 7190.8 >> fstime 425.5 959.9 956.1 >> >> From the test result, with macro method it is almost the same with >> CONFIG_PARAVIRT disabled, and there is some improvment compared with >> function method. >> >> Signed-off-by: Bibo Mao <maobibo@loongson.cn> >> --- >> arch/loongarch/include/asm/qspinlock.h | 27 +++++++++++++++++++++----- >> arch/loongarch/kernel/paravirt.c | 15 ++------------ >> 2 files changed, 24 insertions(+), 18 deletions(-) >> >> diff --git a/arch/loongarch/include/asm/qspinlock.h b/arch/loongarch/include/asm/qspinlock.h >> index 66244801db67..305afd37cdcb 100644 >> --- a/arch/loongarch/include/asm/qspinlock.h >> +++ b/arch/loongarch/include/asm/qspinlock.h >> @@ -5,8 +5,10 @@ >> #include <linux/jump_label.h> >> >> #ifdef CONFIG_PARAVIRT >> - >> +#include <asm/kvm_para.h> >> DECLARE_STATIC_KEY_FALSE(virt_spin_lock_key); >> +DECLARE_STATIC_KEY_FALSE(virt_preempt_key); >> +DECLARE_PER_CPU(struct kvm_steal_time, steal_time); >> >> #define virt_spin_lock virt_spin_lock >> >> @@ -34,10 +36,25 @@ static inline bool virt_spin_lock(struct qspinlock *lock) >> return true; >> } >> >> -#define vcpu_is_preempted vcpu_is_preempted >> - >> -bool vcpu_is_preempted(int cpu); >> - >> +/* >> + * Macro is better than inline function here >> + * With inline function, parameter cpu is parsed even though it is not used. >> + * This may cause cache line thrashing across NUMA node. >> + * With macro method, parameter cpu is parsed only when it is used. >> + */ >> +#define vcpu_is_preempted(cpu) \ >> +({ \ >> + bool __val; \ >> + \ >> + if (!static_branch_unlikely(&virt_preempt_key)) \ >> + __val = false; \ >> + else { \ >> + struct kvm_steal_time *src; \ >> + src = &per_cpu(steal_time, cpu); \ >> + __val = !!(src->preempted & KVM_VCPU_PREEMPTED); \ >> + } \ >> + __val; \ >> +}) >> #endif /* CONFIG_PARAVIRT */ >> >> #include <asm-generic/qspinlock.h> >> diff --git a/arch/loongarch/kernel/paravirt.c b/arch/loongarch/kernel/paravirt.c >> index b74fe6db49ab..2d1206e486e2 100644 >> --- a/arch/loongarch/kernel/paravirt.c >> +++ b/arch/loongarch/kernel/paravirt.c >> @@ -10,8 +10,8 @@ >> #include <asm/paravirt.h> >> >> static int has_steal_clock; >> -static DEFINE_PER_CPU(struct kvm_steal_time, steal_time) __aligned(64); >> -static DEFINE_STATIC_KEY_FALSE(virt_preempt_key); >> +DEFINE_PER_CPU(struct kvm_steal_time, steal_time) __aligned(64); >> +DEFINE_STATIC_KEY_FALSE(virt_preempt_key); >> DEFINE_STATIC_KEY_FALSE(virt_spin_lock_key); >> >> static bool steal_acc = true; >> @@ -261,17 +261,6 @@ static int pv_time_cpu_down_prepare(unsigned int cpu) >> return 0; >> } >> >> -bool vcpu_is_preempted(int cpu) >> -{ >> - struct kvm_steal_time *src; >> - >> - if (!static_branch_unlikely(&virt_preempt_key)) >> - return false; >> - >> - src = &per_cpu(steal_time, cpu); >> - return !!(src->preempted & KVM_VCPU_PREEMPTED); >> -} >> -EXPORT_SYMBOL(vcpu_is_preempted); >> #endif >> >> static void pv_cpu_reboot(void *unused) >> -- >> 2.39.3 >> >> ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 3/3] LoongArch: KVM: Set vcpu_is_preempted() macro rather than function 2026-03-11 3:47 ` Bibo Mao @ 2026-03-11 4:38 ` Huacai Chen 2026-03-11 6:30 ` Bibo Mao 0 siblings, 1 reply; 8+ messages in thread From: Huacai Chen @ 2026-03-11 4:38 UTC (permalink / raw) To: Bibo Mao Cc: Tianrui Zhao, kernel, kvm, loongarch, linux-kernel, Juergen Gross On Wed, Mar 11, 2026 at 11:51 AM Bibo Mao <maobibo@loongson.cn> wrote: > > > > On 2026/3/11 上午11:35, Huacai Chen wrote: > > Hi, Bibo, > > > > On Wed, Mar 11, 2026 at 11:21 AM Bibo Mao <maobibo@loongson.cn> wrote: > >> > >> vcpu_is_preempted() is performance sensitive called in function > >> osq_lock(), here set it as macro. So that parameter is not parsed > >> at most time, it can avoid cache line thrashing across numa node. > > I remember that I had suggested you define vcpu_is_preempted() in the > > header file but you refused. > yes, I refuse then, that is a tragedy, I am sorry for that:( > > By some benchmarks, vcpu_is_preempted() is called in busy-loop method, > more obvious with more CPUs, it may be a bottleneck. I also remember that you said the static key and percpu steal_time structure should be EXPORT_SYMBOL, but you haven't done this patch. Huacai > > Regards > Bibo Mao > > > > > Huacai > > > >> > >> Here is part of unixbench result on 3C5000 DualWay machine with 32 > >> Cores and 2 Numa node. > >> origin with patch CONFIG_PARAVIRT disabled > >> execl 6871.9 7134.2 7190.8 > >> fstime 425.5 959.9 956.1 > >> > >> From the test result, with macro method it is almost the same with > >> CONFIG_PARAVIRT disabled, and there is some improvment compared with > >> function method. > >> > >> Signed-off-by: Bibo Mao <maobibo@loongson.cn> > >> --- > >> arch/loongarch/include/asm/qspinlock.h | 27 +++++++++++++++++++++----- > >> arch/loongarch/kernel/paravirt.c | 15 ++------------ > >> 2 files changed, 24 insertions(+), 18 deletions(-) > >> > >> diff --git a/arch/loongarch/include/asm/qspinlock.h b/arch/loongarch/include/asm/qspinlock.h > >> index 66244801db67..305afd37cdcb 100644 > >> --- a/arch/loongarch/include/asm/qspinlock.h > >> +++ b/arch/loongarch/include/asm/qspinlock.h > >> @@ -5,8 +5,10 @@ > >> #include <linux/jump_label.h> > >> > >> #ifdef CONFIG_PARAVIRT > >> - > >> +#include <asm/kvm_para.h> > >> DECLARE_STATIC_KEY_FALSE(virt_spin_lock_key); > >> +DECLARE_STATIC_KEY_FALSE(virt_preempt_key); > >> +DECLARE_PER_CPU(struct kvm_steal_time, steal_time); > >> > >> #define virt_spin_lock virt_spin_lock > >> > >> @@ -34,10 +36,25 @@ static inline bool virt_spin_lock(struct qspinlock *lock) > >> return true; > >> } > >> > >> -#define vcpu_is_preempted vcpu_is_preempted > >> - > >> -bool vcpu_is_preempted(int cpu); > >> - > >> +/* > >> + * Macro is better than inline function here > >> + * With inline function, parameter cpu is parsed even though it is not used. > >> + * This may cause cache line thrashing across NUMA node. > >> + * With macro method, parameter cpu is parsed only when it is used. > >> + */ > >> +#define vcpu_is_preempted(cpu) \ > >> +({ \ > >> + bool __val; \ > >> + \ > >> + if (!static_branch_unlikely(&virt_preempt_key)) \ > >> + __val = false; \ > >> + else { \ > >> + struct kvm_steal_time *src; \ > >> + src = &per_cpu(steal_time, cpu); \ > >> + __val = !!(src->preempted & KVM_VCPU_PREEMPTED); \ > >> + } \ > >> + __val; \ > >> +}) > >> #endif /* CONFIG_PARAVIRT */ > >> > >> #include <asm-generic/qspinlock.h> > >> diff --git a/arch/loongarch/kernel/paravirt.c b/arch/loongarch/kernel/paravirt.c > >> index b74fe6db49ab..2d1206e486e2 100644 > >> --- a/arch/loongarch/kernel/paravirt.c > >> +++ b/arch/loongarch/kernel/paravirt.c > >> @@ -10,8 +10,8 @@ > >> #include <asm/paravirt.h> > >> > >> static int has_steal_clock; > >> -static DEFINE_PER_CPU(struct kvm_steal_time, steal_time) __aligned(64); > >> -static DEFINE_STATIC_KEY_FALSE(virt_preempt_key); > >> +DEFINE_PER_CPU(struct kvm_steal_time, steal_time) __aligned(64); > >> +DEFINE_STATIC_KEY_FALSE(virt_preempt_key); > >> DEFINE_STATIC_KEY_FALSE(virt_spin_lock_key); > >> > >> static bool steal_acc = true; > >> @@ -261,17 +261,6 @@ static int pv_time_cpu_down_prepare(unsigned int cpu) > >> return 0; > >> } > >> > >> -bool vcpu_is_preempted(int cpu) > >> -{ > >> - struct kvm_steal_time *src; > >> - > >> - if (!static_branch_unlikely(&virt_preempt_key)) > >> - return false; > >> - > >> - src = &per_cpu(steal_time, cpu); > >> - return !!(src->preempted & KVM_VCPU_PREEMPTED); > >> -} > >> -EXPORT_SYMBOL(vcpu_is_preempted); > >> #endif > >> > >> static void pv_cpu_reboot(void *unused) > >> -- > >> 2.39.3 > >> > >> > > ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 3/3] LoongArch: KVM: Set vcpu_is_preempted() macro rather than function 2026-03-11 4:38 ` Huacai Chen @ 2026-03-11 6:30 ` Bibo Mao 0 siblings, 0 replies; 8+ messages in thread From: Bibo Mao @ 2026-03-11 6:30 UTC (permalink / raw) To: Huacai Chen Cc: Tianrui Zhao, kernel, kvm, loongarch, linux-kernel, Juergen Gross On 2026/3/11 下午12:38, Huacai Chen wrote: > On Wed, Mar 11, 2026 at 11:51 AM Bibo Mao <maobibo@loongson.cn> wrote: >> >> >> >> On 2026/3/11 上午11:35, Huacai Chen wrote: >>> Hi, Bibo, >>> >>> On Wed, Mar 11, 2026 at 11:21 AM Bibo Mao <maobibo@loongson.cn> wrote: >>>> >>>> vcpu_is_preempted() is performance sensitive called in function >>>> osq_lock(), here set it as macro. So that parameter is not parsed >>>> at most time, it can avoid cache line thrashing across numa node. >>> I remember that I had suggested you define vcpu_is_preempted() in the >>> header file but you refused. >> yes, I refuse then, that is a tragedy, I am sorry for that:( >> >> By some benchmarks, vcpu_is_preempted() is called in busy-loop method, >> more obvious with more CPUs, it may be a bottleneck. > I also remember that you said the static key and percpu steal_time > structure should be EXPORT_SYMBOL, but you haven't done this patch. Currently API vcpu_is_preempted() and its caller owner_on_cpu() is only used on files in kernel/locking/mutex.c, rwsem.c etc, it seems that it is only used by kernel. And there is no obvious signals for module usage with this API. So I do not want to export the symbol this time, will export the symbol if there is actually usage for modules. Regards Bibo Mao > > Huacai > >> >> Regards >> Bibo Mao >> >>> >>> Huacai >>> >>>> >>>> Here is part of unixbench result on 3C5000 DualWay machine with 32 >>>> Cores and 2 Numa node. >>>> origin with patch CONFIG_PARAVIRT disabled >>>> execl 6871.9 7134.2 7190.8 >>>> fstime 425.5 959.9 956.1 >>>> >>>> From the test result, with macro method it is almost the same with >>>> CONFIG_PARAVIRT disabled, and there is some improvment compared with >>>> function method. >>>> >>>> Signed-off-by: Bibo Mao <maobibo@loongson.cn> >>>> --- >>>> arch/loongarch/include/asm/qspinlock.h | 27 +++++++++++++++++++++----- >>>> arch/loongarch/kernel/paravirt.c | 15 ++------------ >>>> 2 files changed, 24 insertions(+), 18 deletions(-) >>>> >>>> diff --git a/arch/loongarch/include/asm/qspinlock.h b/arch/loongarch/include/asm/qspinlock.h >>>> index 66244801db67..305afd37cdcb 100644 >>>> --- a/arch/loongarch/include/asm/qspinlock.h >>>> +++ b/arch/loongarch/include/asm/qspinlock.h >>>> @@ -5,8 +5,10 @@ >>>> #include <linux/jump_label.h> >>>> >>>> #ifdef CONFIG_PARAVIRT >>>> - >>>> +#include <asm/kvm_para.h> >>>> DECLARE_STATIC_KEY_FALSE(virt_spin_lock_key); >>>> +DECLARE_STATIC_KEY_FALSE(virt_preempt_key); >>>> +DECLARE_PER_CPU(struct kvm_steal_time, steal_time); >>>> >>>> #define virt_spin_lock virt_spin_lock >>>> >>>> @@ -34,10 +36,25 @@ static inline bool virt_spin_lock(struct qspinlock *lock) >>>> return true; >>>> } >>>> >>>> -#define vcpu_is_preempted vcpu_is_preempted >>>> - >>>> -bool vcpu_is_preempted(int cpu); >>>> - >>>> +/* >>>> + * Macro is better than inline function here >>>> + * With inline function, parameter cpu is parsed even though it is not used. >>>> + * This may cause cache line thrashing across NUMA node. >>>> + * With macro method, parameter cpu is parsed only when it is used. >>>> + */ >>>> +#define vcpu_is_preempted(cpu) \ >>>> +({ \ >>>> + bool __val; \ >>>> + \ >>>> + if (!static_branch_unlikely(&virt_preempt_key)) \ >>>> + __val = false; \ >>>> + else { \ >>>> + struct kvm_steal_time *src; \ >>>> + src = &per_cpu(steal_time, cpu); \ >>>> + __val = !!(src->preempted & KVM_VCPU_PREEMPTED); \ >>>> + } \ >>>> + __val; \ >>>> +}) >>>> #endif /* CONFIG_PARAVIRT */ >>>> >>>> #include <asm-generic/qspinlock.h> >>>> diff --git a/arch/loongarch/kernel/paravirt.c b/arch/loongarch/kernel/paravirt.c >>>> index b74fe6db49ab..2d1206e486e2 100644 >>>> --- a/arch/loongarch/kernel/paravirt.c >>>> +++ b/arch/loongarch/kernel/paravirt.c >>>> @@ -10,8 +10,8 @@ >>>> #include <asm/paravirt.h> >>>> >>>> static int has_steal_clock; >>>> -static DEFINE_PER_CPU(struct kvm_steal_time, steal_time) __aligned(64); >>>> -static DEFINE_STATIC_KEY_FALSE(virt_preempt_key); >>>> +DEFINE_PER_CPU(struct kvm_steal_time, steal_time) __aligned(64); >>>> +DEFINE_STATIC_KEY_FALSE(virt_preempt_key); >>>> DEFINE_STATIC_KEY_FALSE(virt_spin_lock_key); >>>> >>>> static bool steal_acc = true; >>>> @@ -261,17 +261,6 @@ static int pv_time_cpu_down_prepare(unsigned int cpu) >>>> return 0; >>>> } >>>> >>>> -bool vcpu_is_preempted(int cpu) >>>> -{ >>>> - struct kvm_steal_time *src; >>>> - >>>> - if (!static_branch_unlikely(&virt_preempt_key)) >>>> - return false; >>>> - >>>> - src = &per_cpu(steal_time, cpu); >>>> - return !!(src->preempted & KVM_VCPU_PREEMPTED); >>>> -} >>>> -EXPORT_SYMBOL(vcpu_is_preempted); >>>> #endif >>>> >>>> static void pv_cpu_reboot(void *unused) >>>> -- >>>> 2.39.3 >>>> >>>> >> >> ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-03-11 6:32 UTC | newest] Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2026-03-11 3:18 [PATCH 0/3] LoongArch: KVM: Some small enhanments Bibo Mao 2026-03-11 3:18 ` [PATCH 1/3] LoongArch: KVM: Add kvm_request_pending checking in kvm_late_check_requests() Bibo Mao 2026-03-11 3:18 ` [PATCH 2/3] LoongArch: KVM: Move host CSR_EENTRY save in context switch Bibo Mao 2026-03-11 3:18 ` [PATCH 3/3] LoongArch: KVM: Set vcpu_is_preempted() macro rather than function Bibo Mao 2026-03-11 3:35 ` Huacai Chen 2026-03-11 3:47 ` Bibo Mao 2026-03-11 4:38 ` Huacai Chen 2026-03-11 6:30 ` Bibo Mao
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®