Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp2902707ybb; Sun, 5 Apr 2020 20:22:47 -0700 (PDT) X-Google-Smtp-Source: APiQypJzTkBt8B3LcONw68buDfghZDEHV4LHzPg6bUe5xyLAEz/LYYrNREmg+25d6LVziRGjFRSe X-Received: by 2002:a05:6808:7c4:: with SMTP id f4mr11178167oij.31.1586143367227; Sun, 05 Apr 2020 20:22:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586143367; cv=none; d=google.com; s=arc-20160816; b=0TlkUXlytulI5SsalW1G4i4iMawMCNIYiYo1OG9I0m2vwOlnKPc16Z6fxE64XFV4q+ R5nXgYI6kdsqkhKMrMKZwfi6yh/PLRVDwPt51qdNj+QOYUz2Ch3oQRyN/Ov/uKNjGUv0 v+xpOXLaZnuhRnXaGOsHYGs9YkivD/gZ2vtd9VkuVh5z4jokSBacf16tRrifTtIU69et TNGtuCv7gyqXfh5AYj0FzKOPXaryZLSHowbUkPgP+wgqVg0o95qHSkzLg7Jn7qgVOFAQ D0qfHMqb0zddMf7syEywp5y+2xM4Q+Xg5VnHFA3+sAxX7tBlc5I/5vsAmXC0757/1glB Yh8A== 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=SnijxemXc4pda/xxVBuPeAunmLkl+JjNGg7itXnbx+p1y+SqgebS0ogxgZzUy2xxhE sGQHzgpu7GUMjjcWm8y+6zYG9WJU0Bdu7WlvwkOp9Zyar5vElDCr9KfdoOFZllC6Tovf 9rRetLyJ8mh2oDrDlYTwgOeSjltTO5wt4pxt2458l0PZdYcmiOCJGH4cpzxpnn1/tb/F Uc0Z5l1/XBQD//jTaYPR5MWWC9lMAi45FTbBF4y7okgrrKiU3ArjrO9ehcRQqa7JgkT1 /Vly+XiARZyTIgOFG6KgJ9H7Jfrj0OI54foNUjWSgCFlPuou7JRUjv5wmKQrFeE/bxnf vJuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=Qmgi8AdT; 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 r3si7542479otc.58.2020.04.05.20.22.34; Sun, 05 Apr 2020 20:22:47 -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=Qmgi8AdT; 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 S1726491AbgDFDWR (ORCPT + 99 others); Sun, 5 Apr 2020 23:22:17 -0400 Received: from smtp-fw-6001.amazon.com ([52.95.48.154]:26123 "EHLO smtp-fw-6001.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726436AbgDFDWR (ORCPT ); Sun, 5 Apr 2020 23:22:17 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1586143337; x=1617679337; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=PAC33jTjkn1bt/6i6MXvdH7zVJo/4mG6UCzWWxpTL9g=; b=Qmgi8AdTr0BZOs7wjr0WuvBaBL2sB9yE8hJuo6zQdSQdJMs2vBVW/tRs iqLZpxvqgTGyJHJUZTO7+wwdU9YUKmhOK3k0qkA75CUZnglCx9mZ1DCwk ieUfXIIuGGxKsZL9e3VlS1XJpGY3T/HyZPM8ReEPSjfHPvDLJ4uwrQ1VQ s=; IronPort-SDR: YtYsFooOZBS28nwr8jibC6SZrr1VO09Jpg3PLYNNYQ0PNJg531H3yN0pMPjN1l7kpqLcK+dRso Es4v4mMQlu3A== X-IronPort-AV: E=Sophos;i="5.72,349,1580774400"; d="scan'208";a="25641511" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-1e-303d0b0e.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-out-6001.iad6.amazon.com with ESMTP; 06 Apr 2020 03:22:17 +0000 Received: from EX13MTAUWA001.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan3.iad.amazon.com [10.40.159.166]) by email-inbound-relay-1e-303d0b0e.us-east-1.amazon.com (Postfix) with ESMTPS id E3723A23E2; Mon, 6 Apr 2020 03:22:13 +0000 (UTC) Received: from EX13D01UWA004.ant.amazon.com (10.43.160.99) by EX13MTAUWA001.ant.amazon.com (10.43.160.118) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 6 Apr 2020 03:22:12 +0000 Received: from EX13MTAUEB002.ant.amazon.com (10.43.60.12) by EX13d01UWA004.ant.amazon.com (10.43.160.99) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 6 Apr 2020 03:22:12 +0000 Received: from localhost (10.85.6.171) by mail-relay.amazon.com (10.43.60.234) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Mon, 6 Apr 2020 03:22:10 +0000 From: Balbir Singh To: , CC: , , , , , , "Balbir Singh" Subject: [PATCH v2 2/4] arch/x86: Refactor tlbflush and l1d flush Date: Mon, 6 Apr 2020 13:19:44 +1000 Message-ID: <20200406031946.11815-3-sblbir@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200406031946.11815-1-sblbir@amazon.com> References: <20200406031946.11815-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