From: Vitaly Kuznetsov <vkuznets@redhat.com>
To: Li RongQing <lirongqing@baidu.com>,
kvm@vger.kernel.org, pbonzini@redhat.com
Cc: seanjc@google.com, lirongqing@baidu.com
Subject: Re: [v3][PATCH 2/2] KVM: Clear pv eoi pending bit only when it is set
Date: Thu, 04 Nov 2021 12:32:41 +0100 [thread overview]
Message-ID: <87a6ikma06.fsf@vitty.brq.redhat.com> (raw)
In-Reply-To: <1636024059-53855-2-git-send-email-lirongqing@baidu.com>
Li RongQing <lirongqing@baidu.com> writes:
> merge pv_eoi_get_pending and pv_eoi_clr_pending into a single
> function pv_eoi_test_and_clear_pending, which returns and clear
> the value of the pending bit.
>
> and clear pv eoi pending bit only when it is set, to avoid calling
> pv_eoi_put_user(), this can speed about 300 nsec on AMD EPYC most
> of the time
>
> Suggested-by: Vitaly Kuznetsov <vkuznets@redhat.com>
> Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
> Signed-off-by: Li RongQing <lirongqing@baidu.com>
> ---
> diff v2: merge as pv_eoi_test_and_clear_pending
> diff v3: remove printk with a new patch
> arch/x86/kvm/lapic.c | 39 ++++++++++++++++++---------------------
> 1 files changed, 18 insertions(+), 21 deletions(-)
>
> diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
> index 752c48e..9c3b1b3 100644
> --- a/arch/x86/kvm/lapic.c
> +++ b/arch/x86/kvm/lapic.c
> @@ -673,15 +673,6 @@ static inline bool pv_eoi_enabled(struct kvm_vcpu *vcpu)
> return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED;
> }
>
> -static bool pv_eoi_get_pending(struct kvm_vcpu *vcpu)
> -{
> - u8 val;
> - if (pv_eoi_get_user(vcpu, &val) < 0)
> - return false;
> -
> - return val & KVM_PV_EOI_ENABLED;
> -}
> -
> static void pv_eoi_set_pending(struct kvm_vcpu *vcpu)
> {
> if (pv_eoi_put_user(vcpu, KVM_PV_EOI_ENABLED) < 0)
> @@ -690,12 +681,25 @@ static void pv_eoi_set_pending(struct kvm_vcpu *vcpu)
> __set_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
> }
>
> -static void pv_eoi_clr_pending(struct kvm_vcpu *vcpu)
> +static bool pv_eoi_test_and_clr_pending(struct kvm_vcpu *vcpu)
> {
> - if (pv_eoi_put_user(vcpu, KVM_PV_EOI_DISABLED) < 0)
> - return;
> + u8 val;
> +
> + if (pv_eoi_get_user(vcpu, &val) < 0)
> + return false;
> +
> + val &= KVM_PV_EOI_ENABLED;
>
> + /*
> + * Clear pending bit in any case: it will be set again on vmentry.
> + * While this might not be ideal from performance point of view,
> + * this makes sure pv eoi is only enabled when we know it's safe.
> + */
This comment is misplaced now, as one may read it as we're clearing the
bit in guest's memory while in fact it refers to a bit in
'vcpu->arch.apic_attention'. Moreover, we're now checking 'val' so 'in
any case' above is certainly misleading.
We can add a
"Disable PV EOI in guest's memory in case it was previously
enabled" (or something like that)
comment here (but I don't think it gives much value to be honest).
> + if (val && pv_eoi_put_user(vcpu, KVM_PV_EOI_DISABLED) < 0)
> + return false;
Let's move the original comment here.
> __clear_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
> +
> + return !!val;
('!!' is not really needed, the function returns bool)
> }
>
> static int apic_has_interrupt_for_ppr(struct kvm_lapic *apic, u32 ppr)
> @@ -2671,7 +2675,6 @@ void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
> static void apic_sync_pv_eoi_from_guest(struct kvm_vcpu *vcpu,
> struct kvm_lapic *apic)
> {
> - bool pending;
> int vector;
> /*
> * PV EOI state is derived from KVM_APIC_PV_EOI_PENDING in host
> @@ -2685,14 +2688,8 @@ static void apic_sync_pv_eoi_from_guest(struct kvm_vcpu *vcpu,
> * -> host enabled PV EOI, guest executed EOI.
> */
> BUG_ON(!pv_eoi_enabled(vcpu));
> - pending = pv_eoi_get_pending(vcpu);
> - /*
> - * Clear pending bit in any case: it will be set again on vmentry.
> - * While this might not be ideal from performance point of view,
> - * this makes sure pv eoi is only enabled when we know it's safe.
> - */
> - pv_eoi_clr_pending(vcpu);
> - if (pending)
> +
> + if (pv_eoi_test_and_clr_pending(vcpu))
> return;
> vector = apic_set_eoi(apic);
> trace_kvm_pv_eoi(apic, vector);
--
Vitaly
next prev parent reply other threads:[~2021-11-04 11:32 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-11-04 11:07 [v3][PATCH 1/2] KVM: x86: don't print when fail to read/write pv eoi memory Li RongQing
2021-11-04 11:07 ` [v3][PATCH 2/2] KVM: Clear pv eoi pending bit only when it is set Li RongQing
2021-11-04 11:32 ` Vitaly Kuznetsov [this message]
2021-11-04 11:22 ` [v3][PATCH 1/2] KVM: x86: don't print when fail to read/write pv eoi memory Vitaly Kuznetsov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87a6ikma06.fsf@vitty.brq.redhat.com \
--to=vkuznets@redhat.com \
--cc=kvm@vger.kernel.org \
--cc=lirongqing@baidu.com \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.