Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp3227625ybz; Sun, 3 May 2020 21:16:09 -0700 (PDT) X-Google-Smtp-Source: APiQypKZTeg8lhovQvIMxOOJjTgyTTcqkn3iy/2d9RpyKTbIiAwLUp9U9qHVUXfyP9Q6mtZn60IB X-Received: by 2002:aa7:d299:: with SMTP id w25mr4921296edq.88.1588565768914; Sun, 03 May 2020 21:16:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588565768; cv=none; d=google.com; s=arc-20160816; b=LN5tJ/ktNvIQ2O1ah6e4M2nXWuSk4PQtLdosWZgGYqpltYJC7wbLVcCmb1bLq6nbJL cazxh4M5kuMW+3F6U4jU+lEgwVczPi9VsRte2efMs5aQLv7NQ8o2rwCqMLDlnSO/SvXG +OFBPeDvVOZRDMxXUWhA9ef8rNdO1Lzi+DD2UL0sCu4ut114MbleGTDXysJor77Y6ZBq OjqY/OHGLB2LKg12puFlpWu2iibnhWhmW7XG17lko7eWLpsJDAG5Zia00wTnYn042h99 HvjDKOwXYFProrpx05MlEtkqEFwbCiJ/sxtEqZ0411OTTo21FTasNthpYZv7X+dmnMso 608A== 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=WTr2M3qkjzkc4EL59kozNmK2puQ92OMRX+u3Qm4TTBg=; b=dggfSQf8FZQFVd5rsyv7YlIsbqQP0bHIKssZ7vyGAphoi1OoLORwn+91j/iexqnFgh yctQdGgEmW3t2767pgnbEe0vTMKDlzXIAgJV4UfUSJxcbaTUKAFFfzRhmi7+WSQRAbha +7MTGAk4teQPba4bDAVZi6CeNsN7axQ55qmiAWm19LKoXXzzqWoDKpM36mPqBPjoCuat 27T6DVSntcBhEzJaBu5A1Co0x7kbK5i4L/f36/8uCXyUZ/8f2zcLAwArFsrcOWmE50ah h/N6uNBqzCrGciddKY/zq6uCVPDs+KvtF8Jt1sNo+sgrstgrZ0BVyZVTUr2TnRt6UouD 7DPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=rscZjN2v; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id i17si7357802edu.511.2020.05.03.21.15.46; Sun, 03 May 2020 21:16:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=rscZjN2v; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1727809AbgEDEN6 (ORCPT + 99 others); Mon, 4 May 2020 00:13:58 -0400 Received: from smtp-fw-33001.amazon.com ([207.171.190.10]:56565 "EHLO smtp-fw-33001.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727098AbgEDEN5 (ORCPT ); Mon, 4 May 2020 00:13:57 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1588565637; x=1620101637; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=WTr2M3qkjzkc4EL59kozNmK2puQ92OMRX+u3Qm4TTBg=; b=rscZjN2vqp/Pn8QXXJNr0pgA3+CUFgFaHQ0kuxXl00qamPYu1JHVIvNr cW5NURxqOAXfxvWksJcnKkGBGsi8Yjh8qZssrcvw22qqIaPaOt3tlu1tv bEx0KfYfTeeAnaiJvuU8YvNRDrsa3ZKZD3gy69EwRGVLOXELFJkIvKvwL M=; IronPort-SDR: a1RMtcOz8VesO8uaycQp233PpB1H/JN+ASoyyaf9ucImGrGfcfcPi2VRl8FNu1U/js+ktgfBJt CB+Ra1Vfk4QA== X-IronPort-AV: E=Sophos;i="5.73,350,1583193600"; d="scan'208";a="42389058" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-2b-a7fdc47a.us-west-2.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-33001.sea14.amazon.com with ESMTP; 04 May 2020 04:13:57 +0000 Received: from EX13MTAUWB001.ant.amazon.com (pdx4-ws-svc-p6-lb7-vlan2.pdx.amazon.com [10.170.41.162]) by email-inbound-relay-2b-a7fdc47a.us-west-2.amazon.com (Postfix) with ESMTPS id CA94DC5A5B; Mon, 4 May 2020 04:13:56 +0000 (UTC) Received: from EX13D01UWB001.ant.amazon.com (10.43.161.75) by EX13MTAUWB001.ant.amazon.com (10.43.161.207) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 4 May 2020 04:13:56 +0000 Received: from EX13MTAUEE002.ant.amazon.com (10.43.62.24) by EX13d01UWB001.ant.amazon.com (10.43.161.75) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 4 May 2020 04:13:55 +0000 Received: from localhost (10.85.6.15) by mail-relay.amazon.com (10.43.62.224) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Mon, 4 May 2020 04:13:54 +0000 From: Balbir Singh To: , CC: , , , , , , , Balbir Singh Subject: [PATCH v5 2/6] arch/x86/kvm: Refactor tlbflush and l1d flush Date: Mon, 4 May 2020 14:13:39 +1000 Message-ID: <20200504041343.9651-3-sblbir@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200504041343.9651-1-sblbir@amazon.com> References: <20200504041343.9651-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. Reviewed-by: Kees Cook Signed-off-by: Balbir Singh --- arch/x86/include/asm/cacheflush.h | 3 ++ arch/x86/kernel/l1d_flush.c | 54 +++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 29 ++--------------- 3 files changed, 60 insertions(+), 26 deletions(-) diff --git a/arch/x86/include/asm/cacheflush.h b/arch/x86/include/asm/cacheflush.h index bac56fcd9790..21cc3b28fa63 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 d605878c8f28..5871794f890d 100644 --- a/arch/x86/kernel/l1d_flush.c +++ b/arch/x86/kernel/l1d_flush.c @@ -34,3 +34,57 @@ 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); + +/* + * Not all users of l1d flush would want to populate the TLB first + * split out the function so that callers can optionally flush the L1D + * cache via sw without prefetching the TLB. + */ +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 f35654db904a..4f95927aad4c 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -6031,8 +6031,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' @@ -6061,32 +6059,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) -- 2.17.1