diff mbox series

KVM: x86: directly call wbinvd for local cpu when emulate wbinvd

Message ID 1632821269-52969-1-git-send-email-lirongqing@baidu.com (mailing list archive)
State New, archived
Headers show
Series KVM: x86: directly call wbinvd for local cpu when emulate wbinvd | expand

Commit Message

Li,Rongqing Sept. 28, 2021, 9:27 a.m. UTC
directly call wbinvd for local pCPU, which can avoid ipi for
itself and calling of get_cpu/on_each_cpu_mask/etc.

In fact, This change reverts commit 2eec73437487 ("KVM: x86: Avoid
issuing wbinvd twice"), since smp_call_function_many is skiping the
local cpu (as description of c2162e13d6e2f), wbinvd is not issued
twice

and reverts commit c2162e13d6e2f ("KVM: X86: Fix missing local pCPU
when executing wbinvd on all dirty pCPUs") too, which fixed the
previous patch, when revert previous patch, it is not needed.

Signed-off-by: Li RongQing <lirongqing@baidu.com>
---
 arch/x86/kvm/x86.c |   13 ++++++-------
 1 files changed, 6 insertions(+), 7 deletions(-)

Comments

Andy Lutomirski Oct. 4, 2021, 10:56 p.m. UTC | #1
On 9/28/21 02:27, Li RongQing wrote:
> directly call wbinvd for local pCPU, which can avoid ipi for
> itself and calling of get_cpu/on_each_cpu_mask/etc.
> 

Why is this an improvement?  Trading get_cpu() vs preempt_disable() 
seems like a negligible difference, and it makes the code more complicated.

> In fact, This change reverts commit 2eec73437487 ("KVM: x86: Avoid
> issuing wbinvd twice"), since smp_call_function_many is skiping the
> local cpu (as description of c2162e13d6e2f), wbinvd is not issued
> twice
> 
> and reverts commit c2162e13d6e2f ("KVM: X86: Fix missing local pCPU
> when executing wbinvd on all dirty pCPUs") too, which fixed the
> previous patch, when revert previous patch, it is not needed.
> 
> Signed-off-by: Li RongQing <lirongqing@baidu.com>
> ---
>   arch/x86/kvm/x86.c |   13 ++++++-------
>   1 files changed, 6 insertions(+), 7 deletions(-)
> 
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 28ef141..ee65941 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -6984,15 +6984,14 @@ static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu)
>   		return X86EMUL_CONTINUE;
>   
>   	if (static_call(kvm_x86_has_wbinvd_exit)()) {
> -		int cpu = get_cpu();
> -
> -		cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
> -		on_each_cpu_mask(vcpu->arch.wbinvd_dirty_mask,
> +		preempt_disable();
> +		smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
>   				wbinvd_ipi, NULL, 1);
> -		put_cpu();
> +		preempt_enable();
>   		cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
> -	} else
> -		wbinvd();
> +	}
> +
> +	wbinvd();
>   	return X86EMUL_CONTINUE;
>   }
>   
>
Li,Rongqing Oct. 5, 2021, 8:04 a.m. UTC | #2
> -----邮件原件-----
> 发件人: Andy Lutomirski <luto@kernel.org>
> 发送时间: 2021年10月5日 6:57
> 收件人: Li,Rongqing <lirongqing@baidu.com>; kvm@vger.kernel.org;
> wanpengli@tencent.com; jan.kiszka@siemens.com; x86@kernel.org
> 主题: Re: [PATCH] KVM: x86: directly call wbinvd for local cpu when emulate
> wbinvd
> 
> On 9/28/21 02:27, Li RongQing wrote:
> > directly call wbinvd for local pCPU, which can avoid ipi for itself
> > and calling of get_cpu/on_each_cpu_mask/etc.
> >
> 
> Why is this an improvement?  Trading get_cpu() vs preempt_disable() seems
> like a negligible difference, and it makes the code more complicated.
> 

First: to local pCpu, this reduces a ipi to itself, ipi will trigger context switch between irq and thread, it is expensive.

Second, preempt_disable/preempt_enable vs get_cpu/ cpumask_set_cpu/put_cpu, the preempt_disable/ preempt_enable is more slight. 

And this can avoid the atomic cpumask_set_cpu

-Li  


> > In fact, This change reverts commit 2eec73437487 ("KVM: x86: Avoid
> > issuing wbinvd twice"), since smp_call_function_many is skiping the
> > local cpu (as description of c2162e13d6e2f), wbinvd is not issued
> > twice
> >
> > and reverts commit c2162e13d6e2f ("KVM: X86: Fix missing local pCPU
> > when executing wbinvd on all dirty pCPUs") too, which fixed the
> > previous patch, when revert previous patch, it is not needed.
> >
> > Signed-off-by: Li RongQing <lirongqing@baidu.com>
> > ---
> >   arch/x86/kvm/x86.c |   13 ++++++-------
> >   1 files changed, 6 insertions(+), 7 deletions(-)
> >
> > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index
> > 28ef141..ee65941 100644
> > --- a/arch/x86/kvm/x86.c
> > +++ b/arch/x86/kvm/x86.c
> > @@ -6984,15 +6984,14 @@ static int kvm_emulate_wbinvd_noskip(struct
> kvm_vcpu *vcpu)
> >   		return X86EMUL_CONTINUE;
> >
> >   	if (static_call(kvm_x86_has_wbinvd_exit)()) {
> > -		int cpu = get_cpu();
> > -
> > -		cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
> > -		on_each_cpu_mask(vcpu->arch.wbinvd_dirty_mask,
> > +		preempt_disable();
> > +		smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
> >   				wbinvd_ipi, NULL, 1);
> > -		put_cpu();
> > +		preempt_enable();
> >   		cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
> > -	} else
> > -		wbinvd();
> > +	}
> > +
> > +	wbinvd();
> >   	return X86EMUL_CONTINUE;
> >   }
> >
> >
Li,Rongqing Oct. 12, 2021, 8:48 a.m. UTC | #3
> -----邮件原件-----
> 发件人: Li,Rongqing
> 发送时间: 2021年10月5日 16:05
> 收件人: 'Andy Lutomirski' <luto@kernel.org>; kvm@vger.kernel.org;
> wanpengli@tencent.com; jan.kiszka@siemens.com; x86@kernel.org
> 主题: 答复: [PATCH] KVM: x86: directly call wbinvd for local cpu when emulate
> wbinvd
> 
> 
> 
> > -----邮件原件-----
> > 发件人: Andy Lutomirski <luto@kernel.org>
> > 发送时间: 2021年10月5日 6:57
> > 收件人: Li,Rongqing <lirongqing@baidu.com>; kvm@vger.kernel.org;
> > wanpengli@tencent.com; jan.kiszka@siemens.com; x86@kernel.org
> > 主题: Re: [PATCH] KVM: x86: directly call wbinvd for local cpu when
> > emulate wbinvd
> >
> > On 9/28/21 02:27, Li RongQing wrote:
> > > directly call wbinvd for local pCPU, which can avoid ipi for itself
> > > and calling of get_cpu/on_each_cpu_mask/etc.
> > >
> >
> > Why is this an improvement?  Trading get_cpu() vs preempt_disable()
> > seems like a negligible difference, and it makes the code more complicated.
> >
> 
> First: to local pCpu, this reduces a ipi to itself, ipi will trigger context switch
> between irq and thread, it is expensive.
> 

on_each_cpu_mask will not send ipi to local cpu, the callback function will be executed directly for local cpu

But this patch is useful still, Using smp_call_function_many and directly calling wbinvd can reduce the unnecessary cpumask_set_cpu and cpumask_test_cpu(), and some duplicate preempt disable

-Li
> Second, preempt_disable/preempt_enable vs get_cpu/
> cpumask_set_cpu/put_cpu, the preempt_disable/ preempt_enable is more
> slight.
> 
> And this can avoid the atomic cpumask_set_cpu
> 
> -Li
> 
> 
> > > In fact, This change reverts commit 2eec73437487 ("KVM: x86: Avoid
> > > issuing wbinvd twice"), since smp_call_function_many is skiping the
> > > local cpu (as description of c2162e13d6e2f), wbinvd is not issued
> > > twice
> > >
> > > and reverts commit c2162e13d6e2f ("KVM: X86: Fix missing local pCPU
> > > when executing wbinvd on all dirty pCPUs") too, which fixed the
> > > previous patch, when revert previous patch, it is not needed.
> > >
> > > Signed-off-by: Li RongQing <lirongqing@baidu.com>
> > > ---
> > >   arch/x86/kvm/x86.c |   13 ++++++-------
> > >   1 files changed, 6 insertions(+), 7 deletions(-)
> > >
> > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index
> > > 28ef141..ee65941 100644
> > > --- a/arch/x86/kvm/x86.c
> > > +++ b/arch/x86/kvm/x86.c
> > > @@ -6984,15 +6984,14 @@ static int kvm_emulate_wbinvd_noskip(struct
> > kvm_vcpu *vcpu)
> > >   		return X86EMUL_CONTINUE;
> > >
> > >   	if (static_call(kvm_x86_has_wbinvd_exit)()) {
> > > -		int cpu = get_cpu();
> > > -
> > > -		cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
> > > -		on_each_cpu_mask(vcpu->arch.wbinvd_dirty_mask,
> > > +		preempt_disable();
> > > +		smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
> > >   				wbinvd_ipi, NULL, 1);
> > > -		put_cpu();
> > > +		preempt_enable();
> > >   		cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
> > > -	} else
> > > -		wbinvd();
> > > +	}
> > > +
> > > +	wbinvd();
> > >   	return X86EMUL_CONTINUE;
> > >   }
> > >
> > >
diff mbox series

Patch

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 28ef141..ee65941 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -6984,15 +6984,14 @@  static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu)
 		return X86EMUL_CONTINUE;
 
 	if (static_call(kvm_x86_has_wbinvd_exit)()) {
-		int cpu = get_cpu();
-
-		cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
-		on_each_cpu_mask(vcpu->arch.wbinvd_dirty_mask,
+		preempt_disable();
+		smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
 				wbinvd_ipi, NULL, 1);
-		put_cpu();
+		preempt_enable();
 		cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
-	} else
-		wbinvd();
+	}
+
+	wbinvd();
 	return X86EMUL_CONTINUE;
 }