Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp400403ybb; Wed, 8 Apr 2020 02:03:44 -0700 (PDT) X-Google-Smtp-Source: APiQypJUbqTzrepUIeeFxjBuqMpdGJ4ACMoMCIQWUC7wqCfaQbRQIrLxrtXfJw8hWsITNY2QRiPt X-Received: by 2002:a9d:3b8b:: with SMTP id k11mr4685359otc.245.1586336624658; Wed, 08 Apr 2020 02:03:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586336624; cv=none; d=google.com; s=arc-20160816; b=PwAm+v4fkk+eDEP9t2vl1iF99hInU22wnwL3jBMNSoHVbtMthuuln0SMnxDokde74C sCkuWug4T/5z9OjGT4gTkLBFd8ko2xbplXvb9MNpm867gccxnMFZJnCl81DPoXbXU3Kr nBBOEPdgR2MtXNiJKEgNqN6wOwtiba0ZqX7nnUfbSJv+rN1IjRgXqhKrfmV3AycBtBPl AXpqNrOKP/mU/LrRQjmMU2S4Sy5A4MP0jIkX8drLMkmUzGrR9JKpd5NPQ2618VqWag4V y4XE9wOuiYURMLFry8e/i78JptOTcsjWGIgKqXICscVpHyNjwft77tO4W3j2sev5p1eh kFSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:ironport-sdr:dkim-signature; bh=w49rWDMGI638ZJLO3IGaQNtI2SFDquSkQvdPry+bnDA=; b=a5QchZG9guUjhz7QGdffdGWwCyyHnqkiPXwHlP2bvj3dfTrwuSVWLOuRg5hQ4/0gyy 3SQhpAWuOAanJYcc97hVv1KLZd164U7sqjf71gB263O+BVRojlWBTUAqM7PU0N408J7H lGV0pU97AnfVPJJrMvIY5zSCaBtMO1FKfnyW//4hfp2xUGIy1YQPMHB7nr/wtKhcwX+f b8ejU0f8eUa1ZiwATybBOEb/6mflfewTZW7ALccMpy86x5+XDQO9nSuDh0BuEqTNQMrU UoLjmOQMuiQeILVIp5PzqCMnBaxcV/8cR+PnuH/IhnpDcbkHGvxWHaVILPs9f1vAwlJn cJlg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=rutlUbue; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 186si2603628ooo.33.2020.04.08.02.03.31; Wed, 08 Apr 2020 02:03:44 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=rutlUbue; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727716AbgDHJCw (ORCPT + 99 others); Wed, 8 Apr 2020 05:02:52 -0400 Received: from smtp-fw-33001.amazon.com ([207.171.190.10]:26804 "EHLO smtp-fw-33001.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726846AbgDHJCv (ORCPT ); Wed, 8 Apr 2020 05:02:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1586336572; x=1617872572; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=w49rWDMGI638ZJLO3IGaQNtI2SFDquSkQvdPry+bnDA=; b=rutlUbueNq9bB6g/S3QFBGVEIPSdG+ELzv5WR5ABdBGCI1+SeqH75Y+h Q0spJl5Ez/MJYJnFRQoYJ26BN4HKtm7AZLs1wnLhJjM33rY7klhO2ATaz b8wi9SMsl0tawZjfsP+34WUU8RnikW8Ij66xUNFE85wrJMVlgXQifOuSE 4=; IronPort-SDR: zEsmko6yPQbfTAE5JbvqbjOzHl5ixzszwuk1G1FP/KgwOm8B/8iIESKuKD4Y9ZWNd1nJFB/Ixd 1HHkY95m9g0w== X-IronPort-AV: E=Sophos;i="5.72,357,1580774400"; d="scan'208";a="37342066" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-2c-397e131e.us-west-2.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-33001.sea14.amazon.com with ESMTP; 08 Apr 2020 09:02:50 +0000 Received: from EX13MTAUWB001.ant.amazon.com (pdx4-ws-svc-p6-lb7-vlan3.pdx.amazon.com [10.170.41.166]) by email-inbound-relay-2c-397e131e.us-west-2.amazon.com (Postfix) with ESMTPS id 844FCA071F; Wed, 8 Apr 2020 09:02:48 +0000 (UTC) Received: from EX13D01UWB004.ant.amazon.com (10.43.161.157) by EX13MTAUWB001.ant.amazon.com (10.43.161.207) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Wed, 8 Apr 2020 09:02:48 +0000 Received: from EX13MTAUWB001.ant.amazon.com (10.43.161.207) by EX13d01UWB004.ant.amazon.com (10.43.161.157) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Wed, 8 Apr 2020 09:02:47 +0000 Received: from localhost (10.85.0.235) by mail-relay.amazon.com (10.43.161.249) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Wed, 8 Apr 2020 09:02:47 +0000 From: Balbir Singh To: , CC: , , , , , , "Balbir Singh" Subject: [PATCH v3 2/5] arch/x86: Refactor tlbflush and l1d flush Date: Wed, 8 Apr 2020 19:02:26 +1000 Message-ID: <20200408090229.16467-3-sblbir@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200408090229.16467-1-sblbir@amazon.com> References: <20200408090229.16467-1-sblbir@amazon.com> MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Refactor the existing assembly bits into smaller helper functions and also abstract L1D_FLUSH into a helper function. Use these functions in kvm for L1D flushing. Signed-off-by: Balbir Singh Reviewed-by: Kees Cook --- arch/x86/include/asm/cacheflush.h | 3 ++ arch/x86/kernel/l1d_flush.c | 49 +++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 31 ++++--------------- 3 files changed, 57 insertions(+), 26 deletions(-) diff --git a/arch/x86/include/asm/cacheflush.h b/arch/x86/include/asm/cacheflush.h index 6419a4cef0e8..66a46db7aadd 100644 --- a/arch/x86/include/asm/cacheflush.h +++ b/arch/x86/include/asm/cacheflush.h @@ -10,5 +10,8 @@ void clflush_cache_range(void *addr, unsigned int size); void *alloc_l1d_flush_pages(void); void cleanup_l1d_flush_pages(void *l1d_flush_pages); +void populate_tlb_with_flush_pages(void *l1d_flush_pages); +void flush_l1d_cache_sw(void *l1d_flush_pages); +int flush_l1d_cache_hw(void); #endif /* _ASM_X86_CACHEFLUSH_H */ diff --git a/arch/x86/kernel/l1d_flush.c b/arch/x86/kernel/l1d_flush.c index 05f375c33423..0842369bac26 100644 --- a/arch/x86/kernel/l1d_flush.c +++ b/arch/x86/kernel/l1d_flush.c @@ -34,3 +34,52 @@ void cleanup_l1d_flush_pages(void *l1d_flush_pages) free_pages((unsigned long)l1d_flush_pages, L1D_CACHE_ORDER); } EXPORT_SYMBOL_GPL(cleanup_l1d_flush_pages); + +void populate_tlb_with_flush_pages(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(populate_tlb_with_flush_pages); + +int flush_l1d_cache_hw(void) +{ + if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) { + wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH); + return 0; + } + return -ENOTSUPP; +} +EXPORT_SYMBOL_GPL(flush_l1d_cache_hw); + +void flush_l1d_cache_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(flush_l1d_cache_sw); diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index f40c30f9b4d8..2bb91bfb8f53 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -5956,8 +5956,6 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu, */ 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' @@ -5986,32 +5984,13 @@ 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 (flush_l1d_cache_hw() == 0) 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"); + preempt_disable(); + populate_tlb_with_flush_pages(vmx_l1d_flush_pages); + flush_l1d_cache_sw(vmx_l1d_flush_pages); + preempt_enable(); } static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr) -- 2.17.1