Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp4642903ybb; Tue, 7 Apr 2020 11:27:57 -0700 (PDT) X-Google-Smtp-Source: APiQypKuTn3y/ooBT3cTSl+dRASrj/AknYpUBUaziaQqD3Q1xgeQCuM+Vv7O4Vekz4GMLTcnzU2P X-Received: by 2002:a9d:7f19:: with SMTP id j25mr2788299otq.129.1586284076946; Tue, 07 Apr 2020 11:27:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586284076; cv=none; d=google.com; s=arc-20160816; b=PvwFaZhYGXBhq7E757SsaIELUX1qCcsR3AtOVpAQwbpUNpkbJcASaFUgnwHJNx8l11 g7Pv6k7597uCt03avHFSCkGr+Pc7TyEWXwrmsufHTGOzRXmJUGhJfw4eOrxvMt9DHK+2 olOcvHhZqAj7nxW+VmX8bDooP/Qe5BBUfddFroMqRXsyt0Wt/KEvXf9OMJvWvF41nnHD n5rSL2pDVB1+SC970PXr2VI+ur74WURp7vvMQqk21zmK493hYtcbWlokjpL8T8CG+l7T SdpcupE/lI9t83r99EVjw8scjn1U8MW1aMZ89eZKvYbgeF1kVwYNDCzCaBu83RlO5mNi K6qA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=ygAnMoK9ZaSsUWHOuK5PKPpZgadu5R9zemBpkbHj3s8=; b=GuMPRJA5c490C0XladzSj3nfVFLIuiuzZR2B4R9OsxYGISE46Ol2k7p7AMEA6YlHes eavXfwDM1JeBFdjoxdJrSoUmyYPjY5jmJXZ1+C7w7wwierubZBb0LW5ZvgNrs+8CMsPN qVYOMrZIPxb2GT4GP0cNvcGnuQ6EgR565aCkD7h/vIXFLFqV5FwYiL2tiFd186jFW9Sh erLWQxuGXJToo3tPubwAbfumZI3aQCv3NoIbLEI0jos2+pjAQFTc3ayu4XGSHyHAa/4D 1dpNuUYyPnAkU7NDr37v52JIz0R4qHW2ygrG2RjX35IbVkaoMuzUrLPNFSQKHkKbvYKM Bn1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=E7cqVk+b; 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=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x12si1465952otq.38.2020.04.07.11.27.42; Tue, 07 Apr 2020 11:27:56 -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=@chromium.org header.s=google header.b=E7cqVk+b; 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=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726559AbgDGSZD (ORCPT + 99 others); Tue, 7 Apr 2020 14:25:03 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:37310 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726277AbgDGSZD (ORCPT ); Tue, 7 Apr 2020 14:25:03 -0400 Received: by mail-pf1-f193.google.com with SMTP id u65so1181786pfb.4 for ; Tue, 07 Apr 2020 11:25:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=ygAnMoK9ZaSsUWHOuK5PKPpZgadu5R9zemBpkbHj3s8=; b=E7cqVk+bO8K6LyZp8VBhIZ4NqWt2n/3FdcKw6Qa5ggS7STrjUXsLTxRvrZbqiwcjKA IgitAKaWhENIy/cmBVf4pw/2H1n0iNnxFp7HpWNHfWLjeysDmyy14W4uwzaC2mHGoCk3 M9W+OV5zVl9jvz4ieBuePwZIA1kfNHXj1JEyI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=ygAnMoK9ZaSsUWHOuK5PKPpZgadu5R9zemBpkbHj3s8=; b=i+4ET70xA60PlmKBNABI6naVOnbusZXHr7cQLEt1AiaqKVR2nQVHTYtJbleloX577L d9pDdXN9TS88vuFdKPwKr5ZkfdUF4whXsytcye0BtB49m7Eyz/dvpGHAQmOc8ZCwFUaj 1Jp2iui08Wa9ChvMEAS06N1WU5wvzudQfdAkozWWRxUMVqLhbpfflylQtGlq65mdKo50 YH9v6nCk+vLcbUmRuGLz+g4ECY7lPYNvIf9mshRZ0/ELrHjhqFAuNveLeE/xixgxyzhS /wBTn9E4K+3dXk11QBe6FOQ0ZMOU+tP2rbBisja5VV5jQvnqq2UDZQ/pj3ulBupYgbed prEw== X-Gm-Message-State: AGi0PuaAZAr3LyCfGYjDk7B1TMuGpnuN9ZViEDlCnlO5nVDHGCydx1aX 6R6kboHEMSiDbX2x1uynEvw0mA== X-Received: by 2002:a63:60d:: with SMTP id 13mr3255921pgg.151.1586283902163; Tue, 07 Apr 2020 11:25:02 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id q15sm14511332pfn.89.2020.04.07.11.25.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Apr 2020 11:25:01 -0700 (PDT) Date: Tue, 7 Apr 2020 11:25:00 -0700 From: Kees Cook To: Balbir Singh Cc: tglx@linutronix.de, linux-kernel@vger.kernel.org, jpoimboe@redhat.com, tony.luck@intel.com, benh@kernel.crashing.org, x86@kernel.org, dave.hansen@intel.com Subject: Re: [PATCH v2 2/4] arch/x86: Refactor tlbflush and l1d flush Message-ID: <202004071122.379AB7D@keescook> References: <20200406031946.11815-1-sblbir@amazon.com> <20200406031946.11815-3-sblbir@amazon.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200406031946.11815-3-sblbir@amazon.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Apr 06, 2020 at 01:19:44PM +1000, Balbir Singh wrote: > 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; > +} This return value is backwards from the kernel's normal use of "int". I would expect 0 to mean "success" and non-zero to mean "failure". How about: 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 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; > - } Then this becomes: if (flush_l1d_cache_hw() == 0) return; (Or change it to a "bool" with and use true/false and leave the above call as-is.) Either way: Reviewed-by: Kees Cook -Kees > > - 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 > -- Kees Cook