From: Maxim Levitsky <mlevitsk@redhat.com>
To: kvm@vger.kernel.org
Cc: Vitaly Kuznetsov <vkuznets@redhat.com>,
Ingo Molnar <mingo@redhat.com>,
Sean Christopherson <seanjc@google.com>,
Jani Nikula <jani.nikula@linux.intel.com>,
Daniel Vetter <daniel@ffwll.ch>, Zhi Wang <zhi.a.wang@intel.com>,
Dave Hansen <dave.hansen@linux.intel.com>,
Tvrtko Ursulin <tvrtko.ursulin@linux.intel.com>,
David Airlie <airlied@linux.ie>,
Wanpeng Li <wanpengli@tencent.com>,
Joonas Lahtinen <joonas.lahtinen@linux.intel.com>,
x86@kernel.org, intel-gvt-dev@lists.freedesktop.org,
Joerg Roedel <joro@8bytes.org>,
Zhenyu Wang <zhenyuw@linux.intel.com>,
Rodrigo Vivi <rodrigo.vivi@intel.com>,
Paolo Bonzini <pbonzini@redhat.com>,
"H. Peter Anvin" <hpa@zytor.com>, Borislav Petkov <bp@alien8.de>,
Jim Mattson <jmattson@google.com>,
intel-gfx@lists.freedesktop.org, linux-kernel@vger.kernel.org,
Thomas Gleixner <tglx@linutronix.de>,
dri-devel@lists.freedesktop.org,
Maxim Levitsky <mlevitsk@redhat.com>
Subject: [PATCH v3 11/11] KVM: SVM: allow to avoid not needed updates to is_running
Date: Tue, 1 Mar 2022 20:26:39 +0200 [thread overview]
Message-ID: <20220301182639.559568-12-mlevitsk@redhat.com> (raw)
In-Reply-To: <20220301182639.559568-1-mlevitsk@redhat.com>
Allow optionally to make KVM not update is_running unless it is
functionally needed which is only when a vCPU halts,
or is in the guest mode.
This means security wise that if a vCPU is scheduled out,
other vCPUs could still send doorbell messages to the
last physical CPU where this vCPU was last running.
This in theory can be considered less secure, thus
this option is not enabled by default.
The option is avic_doorbell_strict and is true by
default, setting it to false allows this relaxed
non strict mode.
Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com>
---
arch/x86/kvm/svm/avic.c | 39 +++++++++++++++++++++++++++------------
arch/x86/kvm/svm/svm.c | 7 +++++--
arch/x86/kvm/svm/svm.h | 1 +
virt/kvm/kvm_main.c | 3 ++-
4 files changed, 35 insertions(+), 15 deletions(-)
diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c
index dd13fd3588e2b..1d690a9d3952e 100644
--- a/arch/x86/kvm/svm/avic.c
+++ b/arch/x86/kvm/svm/avic.c
@@ -166,10 +166,13 @@ void avic_physid_shadow_table_update_vcpu_location(struct kvm_vcpu *vcpu, int cp
raw_spin_lock_irqsave(&kvm_svm->avic.table_entries_lock, flags);
list_for_each_entry(e, &vcpu_svm->nested.physid_ref_entries, link) {
- u64 sentry = READ_ONCE(*e->sentry);
+ u64 old_sentry = READ_ONCE(*e->sentry);
+ u64 new_sentry = old_sentry;
- physid_entry_set_apicid(&sentry, cpu);
- WRITE_ONCE(*e->sentry, sentry);
+ physid_entry_set_apicid(&new_sentry, cpu);
+
+ if (new_sentry != old_sentry)
+ WRITE_ONCE(*e->sentry, new_sentry);
nentries++;
}
@@ -1507,7 +1510,7 @@ avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r)
void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
{
- u64 entry;
+ u64 old_entry, new_entry;
/* ID = 0xff (broadcast), ID > 0xff (reserved) */
int h_physical_id = kvm_cpu_get_apicid(cpu);
struct vcpu_svm *svm = to_svm(vcpu);
@@ -1531,14 +1534,16 @@ void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
if (kvm_vcpu_is_blocking(vcpu))
return;
- entry = READ_ONCE(*(svm->avic_physical_id_cache));
- WARN_ON(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
+ old_entry = READ_ONCE(*(svm->avic_physical_id_cache));
+ new_entry = old_entry;
- entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
- entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK);
- entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
+ new_entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
+ new_entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK);
+ new_entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
+
+ if (old_entry != new_entry)
+ WRITE_ONCE(*(svm->avic_physical_id_cache), new_entry);
- WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
avic_update_iommu_vcpu_affinity(vcpu, h_physical_id, true);
}
@@ -1549,14 +1554,24 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu)
lockdep_assert_preemption_disabled();
+ avic_update_iommu_vcpu_affinity(vcpu, -1, 0);
+
+ /*
+ * It is only meaningful to intercept IPIs from the guest
+ * when either vCPU is blocked, or in guest mode.
+ * In all other cases (e.g userspace vmexit, or preemption
+ * by other task, the vCPU is guaranteed to return to guest mode
+ * as soon as it can
+ */
+ if (!avic_doorbell_strict && !kvm_vcpu_is_blocking(vcpu) && !is_guest_mode(vcpu))
+ return;
+
entry = READ_ONCE(*(svm->avic_physical_id_cache));
/* Nothing to do if IsRunning == '0' due to vCPU blocking. */
if (!(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK))
return;
- avic_update_iommu_vcpu_affinity(vcpu, -1, 0);
-
entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
}
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 0d6b715375a69..463b756f665ae 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -202,6 +202,9 @@ module_param(tsc_scaling, int, 0444);
static bool avic;
module_param(avic, bool, 0444);
+bool avic_doorbell_strict = true;
+module_param(avic_doorbell_strict, bool, 0444);
+
bool __read_mostly dump_invalid_vmcb;
module_param(dump_invalid_vmcb, bool, 0644);
@@ -1340,7 +1343,8 @@ static void svm_vcpu_put(struct kvm_vcpu *vcpu)
svm->loaded = false;
if (svm->nested.initialized && svm->avic_enabled)
- avic_physid_shadow_table_update_vcpu_location(vcpu, -1);
+ if (!avic_doorbell_strict || kvm_vcpu_is_blocking(vcpu))
+ avic_physid_shadow_table_update_vcpu_location(vcpu, -1);
svm_prepare_host_switch(vcpu);
@@ -4707,7 +4711,6 @@ static __init void svm_set_cpu_caps(void)
/* CPUID 0x80000001 and 0x8000000A (SVM features) */
if (nested) {
kvm_cpu_cap_set(X86_FEATURE_SVM);
- kvm_cpu_cap_set(X86_FEATURE_VMCBCLEAN);
if (nrips)
kvm_cpu_cap_set(X86_FEATURE_NRIPS);
diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h
index 8ebda12995abe..d0108bae2cdac 100644
--- a/arch/x86/kvm/svm/svm.h
+++ b/arch/x86/kvm/svm/svm.h
@@ -33,6 +33,7 @@
extern u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
extern bool npt_enabled;
extern bool intercept_smi;
+extern bool avic_doorbell_strict;
/*
* Clean bits in VMCB.
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index cdf1fa3c60ae2..67a29233e216b 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -3291,9 +3291,10 @@ bool kvm_vcpu_block(struct kvm_vcpu *vcpu)
vcpu->stat.generic.blocking = 1;
+ prepare_to_rcuwait(wait);
+
kvm_arch_vcpu_blocking(vcpu);
- prepare_to_rcuwait(wait);
for (;;) {
set_current_state(TASK_INTERRUPTIBLE);
--
2.26.3
prev parent reply other threads:[~2022-03-01 18:29 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-03-01 18:26 [PATCH v3 00/11] RFC: nested AVIC Maxim Levitsky
2022-03-01 18:26 ` [PATCH v3 01/11] KVM: x86: SVM: move nested_npt_enabled to svm.h Maxim Levitsky
2022-03-01 18:26 ` [PATCH v3 02/11] KVM: x86: SVM: allow AVIC to co-exist with a nested guest running Maxim Levitsky
2022-03-01 18:26 ` [PATCH v3 03/11] KVM: x86: mmu: allow to enable write tracking externally Maxim Levitsky
2022-03-01 18:26 ` [PATCH v3 04/11] x86: KVMGT: use kvm_page_track_write_tracking_enable Maxim Levitsky
2022-03-01 18:26 ` [PATCH v3 05/11] KVM: x86: mmu: add gfn_in_memslot helper Maxim Levitsky
2022-03-01 18:26 ` [PATCH v3 06/11] KVM: x86: lapic: don't allow to change APIC ID when apic acceleration is enabled Maxim Levitsky
2022-03-01 18:26 ` [PATCH v3 07/11] KVM: x86: SVM: remove avic's broken code that updated APIC ID Maxim Levitsky
2022-03-01 18:26 ` [PATCH v3 08/11] KVM: x86: SVM: move avic state to separate struct Maxim Levitsky
2022-03-01 18:26 ` [PATCH v3 09/11] KVM: x86: rename .set_apic_access_page_addr to reload_apic_access_page Maxim Levitsky
2022-03-01 18:26 ` [PATCH v3 10/11] KVM: nSVM: implement support for nested AVIC Maxim Levitsky
2022-03-01 18:26 ` Maxim Levitsky [this message]
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=20220301182639.559568-12-mlevitsk@redhat.com \
--to=mlevitsk@redhat.com \
--cc=airlied@linux.ie \
--cc=bp@alien8.de \
--cc=daniel@ffwll.ch \
--cc=dave.hansen@linux.intel.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=hpa@zytor.com \
--cc=intel-gfx@lists.freedesktop.org \
--cc=intel-gvt-dev@lists.freedesktop.org \
--cc=jani.nikula@linux.intel.com \
--cc=jmattson@google.com \
--cc=joonas.lahtinen@linux.intel.com \
--cc=joro@8bytes.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=pbonzini@redhat.com \
--cc=rodrigo.vivi@intel.com \
--cc=seanjc@google.com \
--cc=tglx@linutronix.de \
--cc=tvrtko.ursulin@linux.intel.com \
--cc=vkuznets@redhat.com \
--cc=wanpengli@tencent.com \
--cc=x86@kernel.org \
--cc=zhenyuw@linux.intel.com \
--cc=zhi.a.wang@intel.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®