Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp4638770ybb; Tue, 7 Apr 2020 11:22:53 -0700 (PDT) X-Google-Smtp-Source: APiQypKcaFcfWebKts31umTCWe/UpODnOrVA88FHnQ7uIkuHy25MyaXw8l2FD7XYv9iA1tlweh/9 X-Received: by 2002:a4a:e08b:: with SMTP id w11mr3013393oos.62.1586283773791; Tue, 07 Apr 2020 11:22:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586283773; cv=none; d=google.com; s=arc-20160816; b=mZk7TLC1u8EW05jisHoRBgPy/giSuO5egI9CXl41v3KQl3ccpxBS9VXWDbZQ+bpb6G XjIGqdlh45nvOGPDxGhgXO0YQDIMAwWJSx3+dXLNTjJ4pHJq59RW4fs0sKhRT72YF0AS rGgYkFIo1P8zLyv4JAM7ZJbaHPeC57k+o3KGWbRLsz6arbzbQB8s/HD7y8WatSCmhvjm 1bZ3oKqtulw3j4uo14EGW3N4k5eAGRG1gawqRZxNnhvAnuAQHIzJxa8xikQPHvZrqmwx 5t50U3MdE1P8niFVMrLWJNDw7TjzopqK1cAilLbcv7x2dbuR2NWZT+Nde8iwopUM7QPy tsPQ== 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=NbpMH6Skwho9hvVAqKiUbM06OygWWr7Vk5Zim+uGaqg=; b=Gr0mAneNF0ap/t3Jq8irPaKJ1P8cxx+rxQlv6Le6kBjlJj2J6NTOtBUFDPnFaPK8Mj zlnK7MUPzwJzHQRup0drBgqXNsmZ9p46dQwR/hHQfDcZRlPhCZrUODmZfHTrGnRjouPi 03ECgE1sHM4cD9nxj9VuifKXJDSt+du1zH1AKfIazQQSdSL+v+O3+XXSrWmZYbTIMAcU Zd0hIMgYT28E4Lc0MAy6OX1XorHVdm+uqblm4eybMCvKakcRAvm4A8nRGwAOF4xLLFkE PMUrwqyXhX6nU0sW05NdqF6rvasqKo93G1g/OrasSEc/No4kMS06sElWsRszlE/cJf0S WghA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=cgSUi1SA; 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 u7si1597055oth.8.2020.04.07.11.22.39; Tue, 07 Apr 2020 11:22:53 -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=cgSUi1SA; 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 S1726421AbgDGSVO (ORCPT + 99 others); Tue, 7 Apr 2020 14:21:14 -0400 Received: from mail-pl1-f194.google.com ([209.85.214.194]:42573 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726277AbgDGSVO (ORCPT ); Tue, 7 Apr 2020 14:21:14 -0400 Received: by mail-pl1-f194.google.com with SMTP id v2so168055plp.9 for ; Tue, 07 Apr 2020 11:21:13 -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=NbpMH6Skwho9hvVAqKiUbM06OygWWr7Vk5Zim+uGaqg=; b=cgSUi1SAh6agTPfqTbY+5k6q/YsLJGf+XzamQcja0uNiJ6BlWmlYLPRDLsuX2eq8Gy mu4deeE2J/KrbhxQrS8ROpVCTWffePnZsFLzDeq91mXwwQm+pcl37YXRP70lEc+Frd5G UqQnBC7Uj9lMYSubJbaaL+PQ3eZX/DDue6UNM= 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=NbpMH6Skwho9hvVAqKiUbM06OygWWr7Vk5Zim+uGaqg=; b=NzqkXSDV6OEVCg6kTlOVIKaprIY9WqxfQom0d6p6bof5O1558bo2g4XPWDDw6au30g N1mjiYPyerLxDyxBsy9d+21pN2fvdMK2gMEpVOtyWEGf8j835TYF1bmP9FbiP82u9dWK U9Ksujm1+Fkg9avjaLtuUl8zoyjMqkYF9ucOoYkK1ovC4SkJhkkOfGEb6LfgAPOcaeuj A9t4WY1VgnPV9pa5zU+RWpUnw5bqSBRjZbgmOgjRV8bp94vTpFe7KTtv6fkSK+1Lqbko EewHywCaqnIkNqHzwBRkeBVfTx9A1qyfeFEiJD6WkjVa7b7x/GGpeFkQqfda3VwhoTm9 jmlw== X-Gm-Message-State: AGi0PuZ9kS6IBm9D2OHX4fSkz6Ymv53CRAPhJ4atwSjB5M4VyzR+Wgjk o3yUvEwDWf8dnCMxtlr4fUjV7Q== X-Received: by 2002:a17:902:8d8d:: with SMTP id v13mr3665354plo.260.1586283673049; Tue, 07 Apr 2020 11:21:13 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id x11sm14631526pfp.204.2020.04.07.11.21.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Apr 2020 11:21:12 -0700 (PDT) Date: Tue, 7 Apr 2020 11:21:11 -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 1/4] arch/x86/kvm: Refactor l1d flush lifecycle management Message-ID: <202004071121.AE5534C@keescook> References: <20200406031946.11815-1-sblbir@amazon.com> <20200406031946.11815-2-sblbir@amazon.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200406031946.11815-2-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:43PM +1000, Balbir Singh wrote: > Split out the allocation and free routines to be used in a follow > up set of patches (to reuse for L1D flushing). > > Signed-off-by: Balbir Singh Reviewed-by: Kees Cook -Kees > --- > arch/x86/include/asm/cacheflush.h | 3 +++ > arch/x86/kernel/Makefile | 1 + > arch/x86/kernel/l1d_flush.c | 36 +++++++++++++++++++++++++++++++ > arch/x86/kvm/vmx/vmx.c | 25 +++------------------ > 4 files changed, 43 insertions(+), 22 deletions(-) > create mode 100644 arch/x86/kernel/l1d_flush.c > > diff --git a/arch/x86/include/asm/cacheflush.h b/arch/x86/include/asm/cacheflush.h > index 63feaf2a5f93..6419a4cef0e8 100644 > --- a/arch/x86/include/asm/cacheflush.h > +++ b/arch/x86/include/asm/cacheflush.h > @@ -6,6 +6,9 @@ > #include > #include > > +#define L1D_CACHE_ORDER 4 > void clflush_cache_range(void *addr, unsigned int size); > +void *alloc_l1d_flush_pages(void); > +void cleanup_l1d_flush_pages(void *l1d_flush_pages); > > #endif /* _ASM_X86_CACHEFLUSH_H */ > diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile > index d6d61c4455fa..48f443e6c2de 100644 > --- a/arch/x86/kernel/Makefile > +++ b/arch/x86/kernel/Makefile > @@ -160,3 +160,4 @@ ifeq ($(CONFIG_X86_64),y) > endif > > obj-$(CONFIG_IMA_SECURE_AND_OR_TRUSTED_BOOT) += ima_arch.o > +obj-y += l1d_flush.o > diff --git a/arch/x86/kernel/l1d_flush.c b/arch/x86/kernel/l1d_flush.c > new file mode 100644 > index 000000000000..05f375c33423 > --- /dev/null > +++ b/arch/x86/kernel/l1d_flush.c > @@ -0,0 +1,36 @@ > +#include > +#include > + > +void *alloc_l1d_flush_pages(void) > +{ > + struct page *page; > + void *l1d_flush_pages = NULL; > + int i; > + > + /* > + * This allocation for l1d_flush_pages is not tied to a VM/task's > + * lifetime and so should not be charged to a memcg. > + */ > + page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER); > + if (!page) > + return NULL; > + l1d_flush_pages = page_address(page); > + > + /* > + * Initialize each page with a different pattern in > + * order to protect against KSM in the nested > + * virtualization case. > + */ > + for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) { > + memset(l1d_flush_pages + i * PAGE_SIZE, i + 1, > + PAGE_SIZE); > + } > + return l1d_flush_pages; > +} > +EXPORT_SYMBOL_GPL(alloc_l1d_flush_pages); > + > +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); > diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c > index 9eaccf92d616..209e63798435 100644 > --- a/arch/x86/kvm/vmx/vmx.c > +++ b/arch/x86/kvm/vmx/vmx.c > @@ -203,14 +203,10 @@ static const struct { > [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false}, > }; > > -#define L1D_CACHE_ORDER 4 > static void *vmx_l1d_flush_pages; > > static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf) > { > - struct page *page; > - unsigned int i; > - > if (!boot_cpu_has_bug(X86_BUG_L1TF)) { > l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED; > return 0; > @@ -253,24 +249,9 @@ static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf) > > if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages && > !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) { > - /* > - * This allocation for vmx_l1d_flush_pages is not tied to a VM > - * lifetime and so should not be charged to a memcg. > - */ > - page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER); > - if (!page) > + vmx_l1d_flush_pages = alloc_l1d_flush_pages(); > + if (!vmx_l1d_flush_pages) > return -ENOMEM; > - vmx_l1d_flush_pages = page_address(page); > - > - /* > - * Initialize each page with a different pattern in > - * order to protect against KSM in the nested > - * virtualization case. > - */ > - for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) { > - memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1, > - PAGE_SIZE); > - } > } > > l1tf_vmx_mitigation = l1tf; > @@ -7992,7 +7973,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { > static void vmx_cleanup_l1d_flush(void) > { > if (vmx_l1d_flush_pages) { > - free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER); > + cleanup_l1d_flush_pages(vmx_l1d_flush_pages); > vmx_l1d_flush_pages = NULL; > } > /* Restore state so sysfs ignores VMX */ > -- > 2.17.1 > -- Kees Cook