Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp1865904ybz; Thu, 23 Apr 2020 07:23:10 -0700 (PDT) X-Google-Smtp-Source: APiQypLfUeCubLGctB085KJd4zUl94bzpImuOb1W8NMW6h0vTY3yAvxKUVYlCkfK9D/P3Y9fiMzl X-Received: by 2002:a05:6402:b2a:: with SMTP id bo10mr2985574edb.366.1587651790075; Thu, 23 Apr 2020 07:23:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587651790; cv=none; d=google.com; s=arc-20160816; b=HOXpo1d8eSEjNg8DmPZgFIIc0whNDfQHL2827pIem8RBvJPiG6En/xY59INw+KMIto pe997rT2deW77+9/HKIDayoZ/gb6MFtKXOlu+Sj7K6EiqOywbHTdrRnTC2v5VLDOGYOW 6u7imVs+brT3Qd2WWp0OOq5MnPw3YmKvSXOTpy73Lt6LYlA6gOpGwyGyGfBveCj1N6Vn 7OwNDZL7x+DDilDAo6EG9VNwZYapkOQpNmBJUAMKMzNe7hBDoGjlBBlPX2Gd2Vdck6am y7pOQ1lP438xsxUwW5rWpIca276QFeQtMHTarDMoSnAlTy9CGC0LE6wKAPQUkk2I/vfy Yklg== 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=V3PAQ7nQXZVIgwNUUZw2X7xg6e90LhPmlNElU1fY3aU=; b=p0bbzZecXgdczsJDfAv0KPW1IPvZQSmmk/FqOWYoZVbZJtJbG6tjzsd1rB3cZeiIXC mD3djfrirlmgymLdUBelWPySJzlE7EJJBnfvpymvNzw2UzH7K03oTRG6KUTwiJjp2xkf KoC5ZSHZnvyKVXvE/G+TRdlRABLqXoquXYL6NWAXGZ+17yiVtA0Z5dq241xAombqRu3A xqBmLcgbTwSRu3f9SqkSXxEcOxVpOBHTrVmBtZA/vkPf+o/8MFyyfFNYQVU4VeSpsSrd 3Z2h3cbETyAbidNmvz8c57bmJSRXq1UC/AQNu5kix/UmF0kDf9JnCRw0i0Ave5EePTKt TY+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=ZYTRbRXE; 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 n10si1231823edt.563.2020.04.23.07.22.46; Thu, 23 Apr 2020 07:23:10 -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=ZYTRbRXE; 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 S1728220AbgDWOBk (ORCPT + 99 others); Thu, 23 Apr 2020 10:01:40 -0400 Received: from smtp-fw-9102.amazon.com ([207.171.184.29]:8602 "EHLO smtp-fw-9102.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727053AbgDWOBj (ORCPT ); Thu, 23 Apr 2020 10:01:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1587650498; x=1619186498; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=V3PAQ7nQXZVIgwNUUZw2X7xg6e90LhPmlNElU1fY3aU=; b=ZYTRbRXEy3YecKFA/BpC1OUMHLwq7SgvNNn5FJj5ZSTWxEI5WBOKjopn rcoGnRrfRkT02d505pCgACTs/wOa6+bvzzlR9uZVgxbgy5v1Duk+wOnr2 Msb7sR7PwjLpGeF520Q5duRjDSOoiJpSw7YOcZjnIi4jRp1xoTbmdyxWi o=; IronPort-SDR: FGFyUWhJKmiyc8n79v03um984ho8eE5mBG8UHvNdcLcGQlQuinSchZPmmdIuJWkxNV09abtBhm oEALJFRbmMXg== X-IronPort-AV: E=Sophos;i="5.73,307,1583193600"; d="scan'208";a="39036391" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-2a-e7be2041.us-west-2.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-9102.sea19.amazon.com with ESMTP; 23 Apr 2020 14:01:36 +0000 Received: from EX13MTAUWA001.ant.amazon.com (pdx4-ws-svc-p6-lb7-vlan2.pdx.amazon.com [10.170.41.162]) by email-inbound-relay-2a-e7be2041.us-west-2.amazon.com (Postfix) with ESMTPS id 86903A1C62; Thu, 23 Apr 2020 14:01:35 +0000 (UTC) Received: from EX13D01UWA001.ant.amazon.com (10.43.160.60) by EX13MTAUWA001.ant.amazon.com (10.43.160.58) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Thu, 23 Apr 2020 14:01:35 +0000 Received: from EX13MTAUWA001.ant.amazon.com (10.43.160.58) by EX13d01UWA001.ant.amazon.com (10.43.160.60) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Thu, 23 Apr 2020 14:01:34 +0000 Received: from localhost (10.143.207.46) by mail-relay.amazon.com (10.43.160.118) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Thu, 23 Apr 2020 14:01:34 +0000 From: Balbir Singh To: , CC: , , , , , , "Balbir Singh" Subject: [PATCH v4 1/6] arch/x86/kvm: Refactor l1d flush lifecycle management Date: Fri, 24 Apr 2020 00:01:20 +1000 Message-ID: <20200423140125.7332-2-sblbir@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200423140125.7332-1-sblbir@amazon.com> References: <20200423140125.7332-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 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 --- 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..bac56fcd9790 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 *l1d_flush_alloc_pages(void); +void l1d_flush_cleanup_pages(void *l1d_flush_pages); #endif /* _ASM_X86_CACHEFLUSH_H */ diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile index 92e1261ec4ec..42c11ca85f1c 100644 --- a/arch/x86/kernel/Makefile +++ b/arch/x86/kernel/Makefile @@ -158,3 +158,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..d605878c8f28 --- /dev/null +++ b/arch/x86/kernel/l1d_flush.c @@ -0,0 +1,36 @@ +#include +#include + +void *l1d_flush_alloc_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(l1d_flush_alloc_pages); + +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); diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 83050977490c..225aa8219bac 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 = l1d_flush_alloc_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; @@ -8026,7 +8007,7 @@ static struct kvm_x86_init_ops vmx_init_ops __initdata = { static void vmx_cleanup_l1d_flush(void) { if (vmx_l1d_flush_pages) { - free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER); + l1d_flush_cleanup_pages(vmx_l1d_flush_pages); vmx_l1d_flush_pages = NULL; } /* Restore state so sysfs ignores VMX */ -- 2.17.1