From: Maxim Levitsky <mlevitsk@redhat.com>
To: "Naveen N Rao (AMD)" <naveen@kernel.org>,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>,
Vasant Hegde <vasant.hegde@amd.com>,
Vitaly Kuznetsov <vkuznets@redhat.com>
Subject: Re: [PATCH 1/3] KVM: x86: hyper-v: Convert synic_auto_eoi_used to an atomic
Date: Mon, 03 Feb 2025 20:30:13 -0500 [thread overview]
Message-ID: <dc784d6e4f6c4478fc18e0bc2d5df56af40d0019.camel@redhat.com> (raw)
In-Reply-To: <3d8ed6be41358c7635bd4e09ecdfd1bc77ce83df.1738595289.git.naveen@kernel.org>
On Mon, 2025-02-03 at 22:33 +0530, Naveen N Rao (AMD) wrote:
> apicv_update_lock is primarily meant for protecting updates to the apicv
> state, and is not necessary for guarding updates to synic_auto_eoi_used.
> Convert synic_auto_eoi_used to an atomic and use
> kvm_set_or_clear_apicv_inhibit() helper to simplify the logic.
>
> Signed-off-by: Naveen N Rao (AMD) <naveen@kernel.org>
> ---
> arch/x86/include/asm/kvm_host.h | 7 ++-----
> arch/x86/kvm/hyperv.c | 17 +++++------------
> 2 files changed, 7 insertions(+), 17 deletions(-)
>
> diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
> index 5193c3dfbce1..fb93563714c2 100644
> --- a/arch/x86/include/asm/kvm_host.h
> +++ b/arch/x86/include/asm/kvm_host.h
> @@ -1150,11 +1150,8 @@ struct kvm_hv {
> /* How many vCPUs have VP index != vCPU index */
> atomic_t num_mismatched_vp_indexes;
>
> - /*
> - * How many SynICs use 'AutoEOI' feature
> - * (protected by arch.apicv_update_lock)
> - */
> - unsigned int synic_auto_eoi_used;
> + /* How many SynICs use 'AutoEOI' feature */
> + atomic_t synic_auto_eoi_used;
>
> struct kvm_hv_syndbg hv_syndbg;
>
> diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c
> index 6a6dd5a84f22..7a4554ea1d16 100644
> --- a/arch/x86/kvm/hyperv.c
> +++ b/arch/x86/kvm/hyperv.c
> @@ -131,25 +131,18 @@ static void synic_update_vector(struct kvm_vcpu_hv_synic *synic,
> if (auto_eoi_old == auto_eoi_new)
> return;
>
> - if (!enable_apicv)
> - return;
> -
> - down_write(&vcpu->kvm->arch.apicv_update_lock);
> -
> if (auto_eoi_new)
> - hv->synic_auto_eoi_used++;
> + atomic_inc(&hv->synic_auto_eoi_used);
> else
> - hv->synic_auto_eoi_used--;
> + atomic_dec(&hv->synic_auto_eoi_used);
>
> /*
> * Inhibit APICv if any vCPU is using SynIC's AutoEOI, which relies on
> * the hypervisor to manually inject IRQs.
> */
> - __kvm_set_or_clear_apicv_inhibit(vcpu->kvm,
> - APICV_INHIBIT_REASON_HYPERV,
> - !!hv->synic_auto_eoi_used);
> -
> - up_write(&vcpu->kvm->arch.apicv_update_lock);
> + kvm_set_or_clear_apicv_inhibit(vcpu->kvm,
> + APICV_INHIBIT_REASON_HYPERV,
> + !!atomic_read(&hv->synic_auto_eoi_used));
Hi,
This introduces a race, because there is a race window between
the moment we read hv->synic_auto_eoi_used, and decide to set/clear the inhibit.
After we read hv->synic_auto_eoi_used, but before we call the kvm_set_or_clear_apicv_inhibit,
other core might also run synic_update_vector and change hv->synic_auto_eoi_used,
finish setting the inhibit in kvm_set_or_clear_apicv_inhibit,
and only then we will call kvm_set_or_clear_apicv_inhibit with the stale value of hv->synic_auto_eoi_used and clear it.
IMHO, knowing that this code is mostly a precaution and that modern windows doesn't use AutoEOI
(at least when AutoEOI deprecation bit is set), instead of counting, we can unconditionally
inhibit the APICv when the guest attempts to use AutoEOI once.
But as usual I won't be surprised that this breaks *some* old and/or odd windows versions.
Best regards,
Maxim Levitsky
> }
>
> static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint,
next prev parent reply other threads:[~2025-02-04 1:30 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-03 17:03 [PATCH 0/3] KVM: x86: Address performance degradation due to APICv inhibits Naveen N Rao (AMD)
2025-02-03 17:03 ` [PATCH 1/3] KVM: x86: hyper-v: Convert synic_auto_eoi_used to an atomic Naveen N Rao (AMD)
2025-02-04 1:30 ` Maxim Levitsky [this message]
2025-02-04 13:09 ` Naveen N Rao
2025-02-04 19:33 ` Sean Christopherson
2025-02-05 11:00 ` Naveen N Rao
2025-02-03 17:03 ` [PATCH 2/3] KVM: x86: Remove use of apicv_update_lock when toggling guest debug state Naveen N Rao (AMD)
2025-02-04 2:00 ` Maxim Levitsky
2025-02-04 13:10 ` Naveen N Rao
2025-02-04 14:25 ` Naveen N Rao
2025-02-04 17:51 ` Sean Christopherson
2025-02-04 17:58 ` Paolo Bonzini
2025-02-04 19:42 ` Maxim Levitsky
2025-02-05 11:13 ` Naveen N Rao
2025-02-03 17:03 ` [PATCH 3/3] KVM: x86: Decouple APICv activation state from apicv_inhibit_reasons Naveen N Rao (AMD)
2025-02-03 18:45 ` Sean Christopherson
2025-02-03 22:22 ` Paolo Bonzini
2025-02-03 23:46 ` Sean Christopherson
2025-02-04 1:23 ` Maxim Levitsky
2025-02-04 19:18 ` Sean Christopherson
2025-02-04 20:08 ` Maxim Levitsky
2025-02-05 1:41 ` Sean Christopherson
2025-02-05 10:54 ` Naveen N Rao
2025-02-05 11:36 ` Paolo Bonzini
2025-02-11 15:57 ` Naveen N Rao
2025-02-11 16:37 ` Sean Christopherson
2025-02-11 18:13 ` Naveen N Rao
2025-02-04 11:06 ` Naveen N Rao
2025-02-04 14:08 ` Paolo Bonzini
2025-02-11 14:37 ` Naveen N Rao
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=dc784d6e4f6c4478fc18e0bc2d5df56af40d0019.camel@redhat.com \
--to=mlevitsk@redhat.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=naveen@kernel.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=suravee.suthikulpanit@amd.com \
--cc=vasant.hegde@amd.com \
--cc=vkuznets@redhat.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 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®