The following commit has been merged into the x86/mm branch of tip:

Commit-ID:     e3efae20ec69e9a8c9db1ad81b37de629219bbc4
Gitweb:        
https://git.kernel.org/tip/e3efae20ec69e9a8c9db1ad81b37de629219bbc4
Author:        Balbir Singh <sbl...@amazon.com>
AuthorDate:    Sun, 10 May 2020 11:47:59 +10:00
Committer:     Thomas Gleixner <t...@linutronix.de>
CommitterDate: Wed, 13 May 2020 18:12:19 +02:00

x86/kvm: Refactor L1D flush operations

Move the L1D flush functions into builtin code so they can be reused for
L1D flush on context switch.

Split them up into:
   - Hardware L1D flush
   - TLB pre-populating of L1D pages for software based flushing
   - Software based L1D flush

Adjust the KVM code accordingly.

[ tglx: Massaged changelog ]

Signed-off-by: Balbir Singh <sbl...@amazon.com>
Signed-off-by: Thomas Gleixner <t...@linutronix.de>
Reviewed-by: Kees Cook <keesc...@chromium.org>
Link: https://lkml.kernel.org/r/20200510014803.12190-3-sbl...@amazon.com

---
 arch/x86/include/asm/cacheflush.h |  3 ++-
 arch/x86/kernel/l1d_flush.c       | 49 ++++++++++++++++++++++++++++++-
 arch/x86/kvm/vmx/vmx.c            | 29 +-----------------
 3 files changed, 55 insertions(+), 26 deletions(-)

diff --git a/arch/x86/include/asm/cacheflush.h 
b/arch/x86/include/asm/cacheflush.h
index bac56fc..21cc3b2 100644
--- a/arch/x86/include/asm/cacheflush.h
+++ b/arch/x86/include/asm/cacheflush.h
@@ -8,7 +8,10 @@
 
 #define L1D_CACHE_ORDER 4
 void clflush_cache_range(void *addr, unsigned int size);
+void l1d_flush_populate_tlb(void *l1d_flush_pages);
 void *l1d_flush_alloc_pages(void);
 void l1d_flush_cleanup_pages(void *l1d_flush_pages);
+void l1d_flush_sw(void *l1d_flush_pages);
+int l1d_flush_hw(void);
 
 #endif /* _ASM_X86_CACHEFLUSH_H */
diff --git a/arch/x86/kernel/l1d_flush.c b/arch/x86/kernel/l1d_flush.c
index 4f298b7..32119ee 100644
--- a/arch/x86/kernel/l1d_flush.c
+++ b/arch/x86/kernel/l1d_flush.c
@@ -37,3 +37,52 @@ void l1d_flush_cleanup_pages(void *l1d_flush_pages)
        free_pages((unsigned long)l1d_flush_pages, L1D_CACHE_ORDER);
 }
 EXPORT_SYMBOL_GPL(l1d_flush_cleanup_pages);
+
+void l1d_flush_populate_tlb(void *l1d_flush_pages)
+{
+       int size = PAGE_SIZE << L1D_CACHE_ORDER;
+
+       asm volatile(
+               /* First ensure the pages are in the TLB */
+               "xorl   %%eax, %%eax\n"
+               ".Lpopulate_tlb:\n\t"
+               "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
+               "addl   $4096, %%eax\n\t"
+               "cmpl   %%eax, %[size]\n\t"
+               "jne    .Lpopulate_tlb\n\t"
+               "xorl   %%eax, %%eax\n\t"
+               "cpuid\n\t"
+               :: [flush_pages] "r" (l1d_flush_pages),
+                   [size] "r" (size)
+               : "eax", "ebx", "ecx", "edx");
+}
+EXPORT_SYMBOL_GPL(l1d_flush_populate_tlb);
+
+int l1d_flush_hw(void)
+{
+       if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
+               wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
+               return 0;
+       }
+       return -ENOTSUPP;
+}
+EXPORT_SYMBOL_GPL(l1d_flush_hw);
+
+void l1d_flush_sw(void *l1d_flush_pages)
+{
+       int size = PAGE_SIZE << L1D_CACHE_ORDER;
+
+       asm volatile(
+                       /* Fill the cache */
+                       "xorl   %%eax, %%eax\n"
+                       ".Lfill_cache:\n"
+                       "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
+                       "addl   $64, %%eax\n\t"
+                       "cmpl   %%eax, %[size]\n\t"
+                       "jne    .Lfill_cache\n\t"
+                       "lfence\n"
+                       :: [flush_pages] "r" (l1d_flush_pages),
+                       [size] "r" (size)
+                       : "eax", "ecx");
+}
+EXPORT_SYMBOL_GPL(l1d_flush_sw);
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 225aa82..786d161 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -5983,8 +5983,6 @@ unexpected_vmexit:
  */
 static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
 {
-       int size = PAGE_SIZE << L1D_CACHE_ORDER;
-
        /*
         * This code is only executed when the the flush mode is 'cond' or
         * 'always'
@@ -6013,32 +6011,11 @@ static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
 
        vcpu->stat.l1d_flush++;
 
-       if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
-               wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
+       if (!l1d_flush_hw())
                return;
-       }
 
-       asm volatile(
-               /* First ensure the pages are in the TLB */
-               "xorl   %%eax, %%eax\n"
-               ".Lpopulate_tlb:\n\t"
-               "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
-               "addl   $4096, %%eax\n\t"
-               "cmpl   %%eax, %[size]\n\t"
-               "jne    .Lpopulate_tlb\n\t"
-               "xorl   %%eax, %%eax\n\t"
-               "cpuid\n\t"
-               /* Now fill the cache */
-               "xorl   %%eax, %%eax\n"
-               ".Lfill_cache:\n"
-               "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
-               "addl   $64, %%eax\n\t"
-               "cmpl   %%eax, %[size]\n\t"
-               "jne    .Lfill_cache\n\t"
-               "lfence\n"
-               :: [flush_pages] "r" (vmx_l1d_flush_pages),
-                   [size] "r" (size)
-               : "eax", "ebx", "ecx", "edx");
+       l1d_flush_populate_tlb(vmx_l1d_flush_pages);
+       l1d_flush_sw(vmx_l1d_flush_pages);
 }
 
 static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)

Reply via email to