Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp335341ybb; Wed, 25 Mar 2020 00:12:32 -0700 (PDT) X-Google-Smtp-Source: ADFU+vso2i5hYyNbaDkYJxZ0ajmKEd5+i2Jm9AoqJ98IFqcgbjUBWlaTHGS8RpUiQqOlqhuPA4Ul X-Received: by 2002:aca:3046:: with SMTP id w67mr1561567oiw.54.1585120351942; Wed, 25 Mar 2020 00:12:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585120351; cv=none; d=google.com; s=arc-20160816; b=Uv1MDyERLB0ckXQ3Ynn+DTKE2HiVuzB1tdUjc5XPOdP299eduCY4p506UYyBA4f2PA 9EEY5Mrh4wFN+DyH/fpSjGUl7UKoeqgBV1Z9CGdjjSaUs6E9eaA1McBRdsjcFrexXNjA vTtKO29MpUBaG8GybwSzJO+6p07NvVXOmn8pPAnfbhgzYKQntwBZaVDqUnyfMXQDQtDJ At0IksYbRNsOMMrT7hM2WLtDo/W4KeXaRqQajslEW/2lp5Gmq3A37N+RdwogPFxCvyZs lqs/l/p576qTSHJGh1FO69Zma2/+gPg34aMxmJnfprgh6t9+ERNqYnyxEIQSCIuVoFME zg+w== 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=ArfOi6mLMUEuRGDzzTgtWyGJbTJSiqOFkXGV33dSEk4=; b=yzViforyiRZCBhYLnqD8iOGCRYreNyYM+QfLxG8G5WPogtidW12t3CoBoalbswRIma Ga+eNPklrzooWgtWBat+eEt+/Ztzs8LWyIc2OKneRPCWBy+1YXaEsY7dRpy63vI0KUfJ ZqLKLhlJjFBUmAykPnZ/t0bDXpvnAD7uh47sfMoSIGceus+jqMbj5eWJDUhnmgZly/yZ EFnhL5X1UDgd/Kx86mkTSBhhoTyD2yWZptTWV2xuara+M72JKdm4kg+YwpI69N8zNr9I c3ub/Navg9ikwdGCTJPFJj0ww2WO8qC0hXuD9uAymMQoNboA6A1e4fjPwus3YRZLbh5n Sk7g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=cDsm79KS; 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 g1si1121838otp.268.2020.03.25.00.12.19; Wed, 25 Mar 2020 00:12:31 -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=cDsm79KS; 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 S1727328AbgCYHLe (ORCPT + 99 others); Wed, 25 Mar 2020 03:11:34 -0400 Received: from smtp-fw-9101.amazon.com ([207.171.184.25]:13131 "EHLO smtp-fw-9101.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727281AbgCYHLb (ORCPT ); Wed, 25 Mar 2020 03:11:31 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1585120290; x=1616656290; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=ArfOi6mLMUEuRGDzzTgtWyGJbTJSiqOFkXGV33dSEk4=; b=cDsm79KS6Krf6McdnoZ9xoShkuVNUJlnBs0pMYFoCpY+X+RkqLp0rUF4 a4r9uEuVoxHSwqMPRuvruwnGnUBQYWD4qbCoztWutityohTvJqJhv6+hs v0KJsVAOnUXr+rKz7kpMe4audRiUeI7Ut1j8qHUzLLqp7RdkrfCZOBffP 4=; IronPort-SDR: /O7sBhpfTayPHSgV1GemlZbxyH/TXiz3WZs5yr/WdWhb4OfBYi6liEZ1/EDgXvavjGN5Hzamwb i+C3C9c4jTfA== X-IronPort-AV: E=Sophos;i="5.72,303,1580774400"; d="scan'208";a="24948369" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-1d-98acfc19.us-east-1.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-9101.sea19.amazon.com with ESMTP; 25 Mar 2020 07:11:22 +0000 Received: from EX13MTAUWB001.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan3.iad.amazon.com [10.40.159.166]) by email-inbound-relay-1d-98acfc19.us-east-1.amazon.com (Postfix) with ESMTPS id EA29EA233B; Wed, 25 Mar 2020 07:11:19 +0000 (UTC) Received: from EX13D01UWB003.ant.amazon.com (10.43.161.94) by EX13MTAUWB001.ant.amazon.com (10.43.161.249) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Wed, 25 Mar 2020 07:11:19 +0000 Received: from EX13MTAUEA002.ant.amazon.com (10.43.61.77) by EX13d01UWB003.ant.amazon.com (10.43.161.94) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Wed, 25 Mar 2020 07:11:18 +0000 Received: from localhost (10.85.0.131) by mail-relay.amazon.com (10.43.61.169) with Microsoft SMTP Server id 15.0.1236.3 via Frontend Transport; Wed, 25 Mar 2020 07:11:17 +0000 From: Balbir Singh To: , CC: , , , , , Balbir Singh Subject: [RFC PATCH v2 2/4] arch/x86: Refactor tlbflush and l1d flush Date: Wed, 25 Mar 2020 18:10:59 +1100 Message-ID: <20200325071101.29556-3-sblbir@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200325071101.29556-1-sblbir@amazon.com> References: <20200325071101.29556-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 --- 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..60499f773046 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 1; + } + return 0; +} +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 66d798e1a9d8..9605589bb294 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -5963,8 +5963,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' @@ -5993,32 +5991,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()) 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