Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp1408174ybb; Wed, 1 Apr 2020 23:35:34 -0700 (PDT) X-Google-Smtp-Source: APiQypLHbdXKVrftlawFLPoAutqFnpl6lW+gt792xmhDSGqKImILaSvDuVEluWKrLtazROqhuSC/ X-Received: by 2002:a4a:3306:: with SMTP id q6mr1508672ooq.81.1585809334309; Wed, 01 Apr 2020 23:35:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585809334; cv=none; d=google.com; s=arc-20160816; b=wdkv4uq6LH/c78VjwYvFEbOx76Dv111BnPK9bpfC0hwzaPT6UcbTetIgLbSQbQK7+d dYpPtmwWKIlVn4R4gnwNt1D44Hel61+wq60Z2pJi9r3RgHfXJxIfz6SF7O95JBvazZOs LkwCEv7HYfemcm6vex6ulXnZ2sGOHvo3NgODxBc+o1v7C7AJ+L0e9PNKCeA463uV3DD3 47/BufIstuC8VJ9EDUNWlVhFvTS0yYnLHSbah/2Pm4MpGjFjNd0ipimUWb2c3KDafmkj KXBuBHmf8kZ18+k6NbEzfmH54hCmcwRlzr3G7/+/C6yd0/R1IgC4yPRcXkXAL0e9ffZf GSmQ== 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=PAC33jTjkn1bt/6i6MXvdH7zVJo/4mG6UCzWWxpTL9g=; b=A1ItrZ7SWu7vVVPC+y1yMBytOw6uRTGzaZt5yVe/EiutBIZ/ox+mMfUsG3B0e0L5I0 g7StdAEqRGtupP8v2QFn+n8IYxGuePHMTm21/eriBG8bWfiSWagZSAS3EaQGrBogH9b/ R3wuYjHFsRDym+N/ciLic5otUqCEc1IjO78AmRppbvxPvYxvg3T4R31bQ0z4A7lhUpFH by2ZJsaAPf3TStZUT5b5tu7vrVzAMAq03VAUQ2wIDumE4x6Opl4cLgGk9Qlx4VBV+rjd mIfHq/2tw7UBNPEfx3hRUVGsMMzzalSgXoO/qmvhfos3FR8RnZN1QH8m6Nxw+B3C2g2W GhBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=sfjbF8lA; 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 184si1942889oif.56.2020.04.01.23.35.21; Wed, 01 Apr 2020 23:35:34 -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=sfjbF8lA; 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 S2387453AbgDBG0m (ORCPT + 99 others); Thu, 2 Apr 2020 02:26:42 -0400 Received: from smtp-fw-4101.amazon.com ([72.21.198.25]:31710 "EHLO smtp-fw-4101.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387431AbgDBG0k (ORCPT ); Thu, 2 Apr 2020 02:26:40 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1585808800; x=1617344800; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=PAC33jTjkn1bt/6i6MXvdH7zVJo/4mG6UCzWWxpTL9g=; b=sfjbF8lAOxFa8/mwOUa6cifhWBmz63UnSasSw2IFqn7hR5nXHw92c8tx zt5mfi4nckPAiVis34uzpWFlfBVMDq8kZMFihEsnQMl94XawEe1kcj7ej iESn0dd/Oj6Wz06jLGXI3soG8ry8ROuTCDWh9S1FSFBZ3kQfoQ0PXCTvT c=; IronPort-SDR: YJSCW8mY1qurpIyA3T0VEuTxeIIrWf/C09iz707cp6EWBwXrGltxo+h+SDkFnLlNcWm9CSVBhB 1+0L6yelw3hQ== X-IronPort-AV: E=Sophos;i="5.72,334,1580774400"; d="scan'208";a="23915798" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-1e-c7c08562.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-out-4101.iad4.amazon.com with ESMTP; 02 Apr 2020 06:26:28 +0000 Received: from EX13MTAUWB001.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan2.iad.amazon.com [10.40.159.162]) by email-inbound-relay-1e-c7c08562.us-east-1.amazon.com (Postfix) with ESMTPS id 2CFE7244159; Thu, 2 Apr 2020 06:26:25 +0000 (UTC) Received: from EX13D01UWB002.ant.amazon.com (10.43.161.136) by EX13MTAUWB001.ant.amazon.com (10.43.161.207) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Thu, 2 Apr 2020 06:26:25 +0000 Received: from EX13MTAUWB001.ant.amazon.com (10.43.161.207) by EX13d01UWB002.ant.amazon.com (10.43.161.136) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Thu, 2 Apr 2020 06:26:25 +0000 Received: from localhost (10.85.6.202) by mail-relay.amazon.com (10.43.161.249) with Microsoft SMTP Server id 15.0.1367.3 via Frontend Transport; Thu, 2 Apr 2020 06:26:24 +0000 From: Balbir Singh To: , CC: , , , , , Balbir Singh Subject: [PATCH 2/3] arch/x86: Refactor tlbflush and l1d flush Date: Thu, 2 Apr 2020 17:24:00 +1100 Message-ID: <20200402062401.29856-3-sblbir@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200402062401.29856-1-sblbir@amazon.com> References: <20200402062401.29856-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 209e63798435..29dc5a5bb6ab 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()) 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