Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp3227867ybz; Sun, 3 May 2020 21:16:42 -0700 (PDT) X-Google-Smtp-Source: APiQypL380o/B1goYz5nQNT/Q3yCRUATCnyF6wJkkgLT2Gu+Sakb3feRVsR3nLtpxSIhBa9TOUxG X-Received: by 2002:a17:906:3e96:: with SMTP id a22mr13362151ejj.232.1588565802043; Sun, 03 May 2020 21:16:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588565802; cv=none; d=google.com; s=arc-20160816; b=BO636ypj3490QOE1P0pBQW32WQBeElD+nel6ruw3JINWpGOb2akGADPfmJQQzqRMfX p6Z0CrF7xgTh8HfXtwphVibhxhhXq9ri89yZIrOLiwGumYgRZ0w04XOVTcE4ZBGF/gxU C1hLURnZeJVSJLUJt6uvW1wGJ9axdgENFBP4rey0HR0b6mzyJ0pAGmbYJRdozf3qdaMM 5we5BSZMI3AJAwaQNslnCYum6uHB+1i1cBb1S0xBZ4maY/dhCaEUwR38OU46kTnDzPhl uY2whyK5nXzEQwHd66BILUQ+Is49eVB12YnPo7fXzIrqBHbmlw8GZLDQFjMfUabSx3JD f+0g== 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=gsU3ZiJEUiwE3rLF98AlNJeYC+Nque4SiUZzK6YC91U=; b=NKBB5fniAodhwiD4WAB8axRO3COlBch0V+Daq4ZubhHnKQO5kk+d4JOtpjIyREcysw DTkHia0SpVhB8rGCszbGu72pzsdBCTkvvfU6xNx9e+EhCk6uPProoT5KGq2aN5Q1D0tr sN+hPrfzpexvIcE/URF/RkdUKF/okL0oOj+v28iQoo2nJ8GT2UIeRWEjJ4kxQFYpRVsi upLQ80ByM3ge0uVZJKdtKD1aRFsdEtCxOASUlZ/aDmjYEp37X+9+pIzd1xPy/xjhp8Xz NxWBY3cXP6H7BbVqB5n42HUst6L4TRPWdmIJhHk86oOKCPKyj9sHkOXTBw7EZwascypx +uLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=OW2LDIZ1; 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 a11si6113883edv.573.2020.05.03.21.16.19; Sun, 03 May 2020 21:16:42 -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=OW2LDIZ1; 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 S1727118AbgEDEN5 (ORCPT + 99 others); Mon, 4 May 2020 00:13:57 -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 S1726441AbgEDEN5 (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=1588565636; x=1620101636; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=gsU3ZiJEUiwE3rLF98AlNJeYC+Nque4SiUZzK6YC91U=; b=OW2LDIZ1mKMyl/8XnY/zh68/EQysv6fhksjMwhOh3hNpfTIAH0bD3xxH jYYhjc7IgXGkoucgkawlaRRIzcW0DF39uRnZxR/IybOOzAI8rLQ6+z2EE CUKW3NXosHJHKnLU5L9dbDZ3WgYXvANZCXwKx9pRUxFmbOIXXGXsL9IWH 4=; IronPort-SDR: G3f/HVSmZ0JBariigHKhrU++7O2N28174zrHddXZbJD2+dz2QEHYHGsQhCxns5gpkCtZxKQ9J+ Oc6h2LqZ8Xdw== X-IronPort-AV: E=Sophos;i="5.73,350,1583193600"; d="scan'208";a="42389056" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-2c-4e7c8266.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:54 +0000 Received: from EX13MTAUWB001.ant.amazon.com (pdx4-ws-svc-p6-lb7-vlan2.pdx.amazon.com [10.170.41.162]) by email-inbound-relay-2c-4e7c8266.us-west-2.amazon.com (Postfix) with ESMTPS id 68AA7A1DF0; Mon, 4 May 2020 04:13:54 +0000 (UTC) Received: from EX13D01UWB004.ant.amazon.com (10.43.161.157) by EX13MTAUWB001.ant.amazon.com (10.43.161.249) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 4 May 2020 04:13:53 +0000 Received: from EX13MTAUEB002.ant.amazon.com (10.43.60.12) by EX13d01UWB004.ant.amazon.com (10.43.161.157) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 4 May 2020 04:13:53 +0000 Received: from localhost (10.85.6.15) by mail-relay.amazon.com (10.43.60.234) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Mon, 4 May 2020 04:13:51 +0000 From: Balbir Singh To: , CC: , , , , , , , Balbir Singh Subject: [PATCH v5 1/6] arch/x86/kvm: Refactor l1d flush lifecycle management Date: Mon, 4 May 2020 14:13:38 +1000 Message-ID: <20200504041343.9651-2-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 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 00d31a5e0089..f35654db904a 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; @@ -8099,7 +8080,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