Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp400396ybb; Wed, 8 Apr 2020 02:03:44 -0700 (PDT) X-Google-Smtp-Source: APiQypKA+FXnnsEI9WKhy+f6M/xmVvGBQ3p3E222W0JOpbwQp0tSRvvDVlOSiFWevJsJAd7Igugz X-Received: by 2002:aca:130d:: with SMTP id e13mr1690692oii.98.1586336624370; Wed, 08 Apr 2020 02:03:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586336624; cv=none; d=google.com; s=arc-20160816; b=YhIhLhNv5Lz6H4jxZIwoPBMhNifYbhmsQw3gWpiNYOK4pMFa0hVcWlIlHox3564hfd yX4BE0OhMI2pZ+AEawpKQXkOPJaOMFu3nZ2INYrG0CW7fw42t0Zbgm1Fd86PDxa4yV2g oCuM7/YIcYgkTvTOMzE1uEY+ONU7Gnqbp5u6GY9FqYX5WH/aZsixqvZaJCX/DozqnWBj F5TDXSRj6nYHu8nkVKzqqro9aoO2lX3Pzt7GkgTmx2oydNQEGe2XuAjya59Xy3sqIImP 4dlHl7oaz+dJXAPzJnVklcZqN7jjEOhhoXyZ2qabyz8h/rrWLLJnJXLuk1wIijjJj4AP sqjw== 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=e5bUYjWbScbzjft6TEIZiCZRtgYcyyX63Xluh9NgdRM=; b=joZr1+JqCO8VewCpLTW/HGBQxdPVcVN/oyxiWZ04rIcE/6tQTyheCScs7O05wK/T3t M+ZOGJRC4xOHHnE5O1EczMG+RstH3lbp8fMBKmu6O7EUctO5X7v//SfkEtmCu0d83zkx UZi9f8v5YOmNsdMVHxU1ultUwEecGbrQBz3AuDwvGYXxG18HBgdr6jEbfNOt2OnfFDkk /8CrZ0AxSrCe97KQGtfbzDlBTghoL7w7b3MYf842BAZLV8Yl44Ha+F03QNA3nnO3yJpO NOkdIaBx51DoY/0QJ/KT+fS9M7P8vUtbZSvgMaAznBHUPm49Y+QOyuzzOsua4U/t6c6i 6G2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=sa3H+P6B; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u16si2457466oth.230.2020.04.08.02.03.29; Wed, 08 Apr 2020 02:03:44 -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=@amazon.com header.s=amazon201209 header.b=sa3H+P6B; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727505AbgDHJCu (ORCPT + 99 others); Wed, 8 Apr 2020 05:02:50 -0400 Received: from smtp-fw-9102.amazon.com ([207.171.184.29]:39808 "EHLO smtp-fw-9102.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726846AbgDHJCu (ORCPT ); Wed, 8 Apr 2020 05:02:50 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1586336569; x=1617872569; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=e5bUYjWbScbzjft6TEIZiCZRtgYcyyX63Xluh9NgdRM=; b=sa3H+P6BiPyoEDoYpnYEyRpglr28XA4GHBMR9exfOegcTlOXwE1iZ93t pSrjX+rtE1HHrjmxAlvhhGu6Of/pP9TwlUxd9jhMiyGbyBSLo3Lozs/Xc VEctFVYTnas/lV1AnPDcdS5JsGXS8jMMI1ml3yWGC3fpcXxK0YCfTR/Hm U=; IronPort-SDR: Qdq/WKgx+3/4KMPZauL9lu9cgebFhIPEsvD2PmfT9O2ur/M2RKK9iZUY2YmRXFmthaqLeslJLG fsqlZDZgJdFw== X-IronPort-AV: E=Sophos;i="5.72,357,1580774400"; d="scan'208";a="35938265" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-2a-22cc717f.us-west-2.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-9102.sea19.amazon.com with ESMTP; 08 Apr 2020 09:02:47 +0000 Received: from EX13MTAUWA001.ant.amazon.com (pdx4-ws-svc-p6-lb7-vlan3.pdx.amazon.com [10.170.41.166]) by email-inbound-relay-2a-22cc717f.us-west-2.amazon.com (Postfix) with ESMTPS id 3AB0DA18E5; Wed, 8 Apr 2020 09:02:47 +0000 (UTC) Received: from EX13D01UWA002.ant.amazon.com (10.43.160.74) by EX13MTAUWA001.ant.amazon.com (10.43.160.58) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Wed, 8 Apr 2020 09:02:46 +0000 Received: from EX13MTAUEA002.ant.amazon.com (10.43.61.77) by EX13d01UWA002.ant.amazon.com (10.43.160.74) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Wed, 8 Apr 2020 09:02:45 +0000 Received: from localhost (10.85.0.235) by mail-relay.amazon.com (10.43.61.169) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Wed, 8 Apr 2020 09:02:44 +0000 From: Balbir Singh To: , CC: , , , , , , "Balbir Singh" Subject: [PATCH v3 1/5] arch/x86/kvm: Refactor l1d flush lifecycle management Date: Wed, 8 Apr 2020 19:02:25 +1000 Message-ID: <20200408090229.16467-2-sblbir@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200408090229.16467-1-sblbir@amazon.com> References: <20200408090229.16467-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..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 91749f1254e8..f40c30f9b4d8 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; @@ -7999,7 +7980,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); + cleanup_l1d_flush_pages(vmx_l1d_flush_pages); vmx_l1d_flush_pages = NULL; } /* Restore state so sysfs ignores VMX */ -- 2.17.1