]> git.itanic.dy.fi Git - linux-stable/commitdiff
KVM: x86: hyper-v: Introduce TLB flush fifo
authorVitaly Kuznetsov <vkuznets@redhat.com>
Tue, 1 Nov 2022 14:53:47 +0000 (15:53 +0100)
committerPaolo Bonzini <pbonzini@redhat.com>
Fri, 18 Nov 2022 17:59:04 +0000 (12:59 -0500)
To allow flushing individual GVAs instead of always flushing the whole
VPID a per-vCPU structure to pass the requests is needed. Use standard
'kfifo' to queue two types of entries: individual GVA (GFN + up to 4095
following GFNs in the lower 12 bits) and 'flush all'.

The size of the fifo is arbitrarily set to '16'.

Note, kvm_hv_flush_tlb() only queues 'flush all' entries for now and
kvm_hv_vcpu_flush_tlb() doesn't actually read the fifo just resets the
queue before returning -EOPNOTSUPP (which triggers full TLB flush) so
the functional change is very small but the infrastructure is prepared
to handle individual GVA flush requests.

Reviewed-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20221101145426.251680-10-vkuznets@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/include/asm/kvm_host.h
arch/x86/kvm/hyperv.c
arch/x86/kvm/hyperv.h
arch/x86/kvm/svm/svm.c
arch/x86/kvm/x86.c

index 0b85230a0e0a7a126a57b54b36601d16c7a734b9..3e35dcf40dc7cc59afcdf2a1811bf60e6fb541a4 100644 (file)
@@ -25,6 +25,7 @@
 #include <linux/clocksource.h>
 #include <linux/irqbypass.h>
 #include <linux/hyperv.h>
+#include <linux/kfifo.h>
 
 #include <asm/apic.h>
 #include <asm/pvclock-abi.h>
@@ -618,6 +619,23 @@ struct kvm_vcpu_hv_synic {
        bool dont_zero_synic_pages;
 };
 
+/* The maximum number of entries on the TLB flush fifo. */
+#define KVM_HV_TLB_FLUSH_FIFO_SIZE (16)
+/*
+ * Note: the following 'magic' entry is made up by KVM to avoid putting
+ * anything besides GVA on the TLB flush fifo. It is theoretically possible
+ * to observe a request to flush 4095 PFNs starting from 0xfffffffffffff000
+ * which will look identical. KVM's action to 'flush everything' instead of
+ * flushing these particular addresses is, however, fully legitimate as
+ * flushing more than requested is always OK.
+ */
+#define KVM_HV_TLB_FLUSHALL_ENTRY  ((u64)-1)
+
+struct kvm_vcpu_hv_tlb_flush_fifo {
+       spinlock_t write_lock;
+       DECLARE_KFIFO(entries, u64, KVM_HV_TLB_FLUSH_FIFO_SIZE);
+};
+
 /* Hyper-V per vcpu emulation context */
 struct kvm_vcpu_hv {
        struct kvm_vcpu *vcpu;
@@ -639,6 +657,8 @@ struct kvm_vcpu_hv {
                u32 nested_eax; /* HYPERV_CPUID_NESTED_FEATURES.EAX */
                u32 nested_ebx; /* HYPERV_CPUID_NESTED_FEATURES.EBX */
        } cpuid_cache;
+
+       struct kvm_vcpu_hv_tlb_flush_fifo tlb_flush_fifo;
 };
 
 /* Xen HVM per vcpu emulation context */
index 3c0f639f6a05e7ac2fe077277c132e2e01f33f22..9d9a5ff2d54bd9175e8301d6df7913be2dc2a1f9 100644 (file)
@@ -29,6 +29,7 @@
 #include <linux/kvm_host.h>
 #include <linux/highmem.h>
 #include <linux/sched/cputime.h>
+#include <linux/spinlock.h>
 #include <linux/eventfd.h>
 
 #include <asm/apicdef.h>
@@ -954,6 +955,9 @@ int kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)
 
        hv_vcpu->vp_index = vcpu->vcpu_idx;
 
+       INIT_KFIFO(hv_vcpu->tlb_flush_fifo.entries);
+       spin_lock_init(&hv_vcpu->tlb_flush_fifo.write_lock);
+
        return 0;
 }
 
@@ -1783,6 +1787,37 @@ static u64 kvm_get_sparse_vp_set(struct kvm *kvm, struct kvm_hv_hcall *hc,
                              var_cnt * sizeof(*sparse_banks));
 }
 
+static void hv_tlb_flush_enqueue(struct kvm_vcpu *vcpu)
+{
+       struct kvm_vcpu_hv_tlb_flush_fifo *tlb_flush_fifo;
+       struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
+       u64 flush_all_entry = KVM_HV_TLB_FLUSHALL_ENTRY;
+
+       if (!hv_vcpu)
+               return;
+
+       tlb_flush_fifo = &hv_vcpu->tlb_flush_fifo;
+
+       kfifo_in_spinlocked_noirqsave(&tlb_flush_fifo->entries, &flush_all_entry,
+                                     1, &tlb_flush_fifo->write_lock);
+}
+
+int kvm_hv_vcpu_flush_tlb(struct kvm_vcpu *vcpu)
+{
+       struct kvm_vcpu_hv_tlb_flush_fifo *tlb_flush_fifo;
+       struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
+
+       if (!hv_vcpu)
+               return -EINVAL;
+
+       tlb_flush_fifo = &hv_vcpu->tlb_flush_fifo;
+
+       kfifo_reset_out(&tlb_flush_fifo->entries);
+
+       /* Precise flushing isn't implemented yet. */
+       return -EOPNOTSUPP;
+}
+
 static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)
 {
        struct kvm *kvm = vcpu->kvm;
@@ -1791,6 +1826,8 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)
        DECLARE_BITMAP(vcpu_mask, KVM_MAX_VCPUS);
        u64 valid_bank_mask;
        u64 sparse_banks[KVM_HV_MAX_SPARSE_VCPU_SET_BITS];
+       struct kvm_vcpu *v;
+       unsigned long i;
        bool all_cpus;
 
        /*
@@ -1870,10 +1907,20 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)
         * analyze it here, flush TLB regardless of the specified address space.
         */
        if (all_cpus) {
+               kvm_for_each_vcpu(i, v, kvm)
+                       hv_tlb_flush_enqueue(v);
+
                kvm_make_all_cpus_request(kvm, KVM_REQ_HV_TLB_FLUSH);
        } else {
                sparse_set_to_vcpu_mask(kvm, sparse_banks, valid_bank_mask, vcpu_mask);
 
+               for_each_set_bit(i, vcpu_mask, KVM_MAX_VCPUS) {
+                       v = kvm_get_vcpu(kvm, i);
+                       if (!v)
+                               continue;
+                       hv_tlb_flush_enqueue(v);
+               }
+
                kvm_make_vcpus_request_mask(kvm, KVM_REQ_HV_TLB_FLUSH, vcpu_mask);
        }
 
index 1030b1b50552373666a59d371e99202389abfab0..f79edf9234cd4c13ecb08eb99c2cae68940932f2 100644 (file)
@@ -151,4 +151,19 @@ int kvm_vm_ioctl_hv_eventfd(struct kvm *kvm, struct kvm_hyperv_eventfd *args);
 int kvm_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid,
                     struct kvm_cpuid_entry2 __user *entries);
 
+static inline void kvm_hv_vcpu_purge_flush_tlb(struct kvm_vcpu *vcpu)
+{
+       struct kvm_vcpu_hv_tlb_flush_fifo *tlb_flush_fifo;
+       struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
+
+       if (!hv_vcpu || !kvm_check_request(KVM_REQ_HV_TLB_FLUSH, vcpu))
+               return;
+
+       tlb_flush_fifo = &hv_vcpu->tlb_flush_fifo;
+
+       kfifo_reset_out(&tlb_flush_fifo->entries);
+}
+
+int kvm_hv_vcpu_flush_tlb(struct kvm_vcpu *vcpu);
+
 #endif
index 4ea6ddd9989947eadf5376702112c9727328d787..91352d69284524c36ab0012a1c6ad2827ca45ffe 100644 (file)
@@ -3727,7 +3727,7 @@ static void svm_flush_tlb_current(struct kvm_vcpu *vcpu)
         * A TLB flush for the current ASID flushes both "host" and "guest" TLB
         * entries, and thus is a superset of Hyper-V's fine grained flushing.
         */
-       kvm_clear_request(KVM_REQ_HV_TLB_FLUSH, vcpu);
+       kvm_hv_vcpu_purge_flush_tlb(vcpu);
 
        /*
         * Flush only the current ASID even if the TLB flush was invoked via
index 12e49e8566d498229fd84fefd01e7b209a39bc1d..72ac6bf05c8b4836d164ba2c61c18cd2826f6f41 100644 (file)
@@ -3425,7 +3425,7 @@ static void kvm_vcpu_flush_tlb_guest(struct kvm_vcpu *vcpu)
         * Flushing all "guest" TLB is always a superset of Hyper-V's fine
         * grained flushing.
         */
-       kvm_clear_request(KVM_REQ_HV_TLB_FLUSH, vcpu);
+       kvm_hv_vcpu_purge_flush_tlb(vcpu);
 }
 
 
@@ -10256,7 +10256,14 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 
                kvm_service_local_tlb_flush_requests(vcpu);
 
-               if (kvm_check_request(KVM_REQ_HV_TLB_FLUSH, vcpu))
+               /*
+                * Fall back to a "full" guest flush if Hyper-V's precise
+                * flushing fails.  Note, Hyper-V's flushing is per-vCPU, but
+                * the flushes are considered "remote" and not "local" because
+                * the requests can be initiated from other vCPUs.
+                */
+               if (kvm_check_request(KVM_REQ_HV_TLB_FLUSH, vcpu) &&
+                   kvm_hv_vcpu_flush_tlb(vcpu))
                        kvm_vcpu_flush_tlb_guest(vcpu);
 
                if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {