Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp780996ybk; Sat, 9 May 2020 18:50:13 -0700 (PDT) X-Google-Smtp-Source: APiQypJRJSf6IoszvDttHGf87RlB1TO14IyswTrDoCjp3SxEHmx3JODXK0wmBcwjn7yHce6jBvrJ X-Received: by 2002:a17:907:b11:: with SMTP id h17mr8289952ejl.371.1589075412978; Sat, 09 May 2020 18:50:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589075412; cv=none; d=google.com; s=arc-20160816; b=Uwsw1eAWqXb1W/PsnyJa8p+ko1AJ4T9des4wcu9HGcq2PCf3w8bKjcykExTTZekVRL Oaa00J43EZgIWaQ5CpcAdv7ePT6zCwqedrEFq29pxL+AfE2tofz7l+sSsrrFuQFyHQ3h J17EKAKuReFXy5DCz6LRlIrxr8+h11l6CWWCTDU4L9z0GuX+W4P4bLRSRxrEhQElAIXx HY7c0phsV1wLxFfu0vNVHFJYpdfVxG12wL7qAKDcuRdQzwRzsc1ATfGGX8Eaa5bjNsAa d8qsIjMM7i8WXx4ZwsSPDmRBCQn+wIw0Ff3cHCy6V5AJAL37T9zEsOIKvn7DruUPJvbf NbZQ== 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=03x4F3LkIBs0IXi5t1iV4y2G0l/zpWiZkIMN7L6NQV4=; b=lEB61K+pzZOEZaouQvlBxXt25up81840ixvsdDD7odDW99Mz+x9AknJp63CnYl1oBP BzWmtVzPP5/NsJxz5kuyb36sB3yhWpvtgprhZggaHHO1qPOsyqBok16Ghm4iaMn/PLv8 MKqlWN7Fg2j+ioV89xExdJ9ql7zrNgWy/g0c/+/YUnX4yfSaAv/gQNwmt83/krEV2hrL yEMjm0vazqbYv68uBlHrgLhOQYO+5OyJBnZYu+M1LjjK8QhQ1Z11DZvMgp//7SiOI/h/ nyrGYrScPoIaansy2pddlwvI9Arl7hwCvAzW5G2PzIDOAX5WGheURBJX2y48CrlhIpnO i68g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=lYJdy1SX; 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 h2si3667932edz.585.2020.05.09.18.49.47; Sat, 09 May 2020 18:50:12 -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=lYJdy1SX; 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 S1728932AbgEJBs1 (ORCPT + 99 others); Sat, 9 May 2020 21:48:27 -0400 Received: from smtp-fw-6002.amazon.com ([52.95.49.90]:59360 "EHLO smtp-fw-6002.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726630AbgEJBs1 (ORCPT ); Sat, 9 May 2020 21:48:27 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1589075306; x=1620611306; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=03x4F3LkIBs0IXi5t1iV4y2G0l/zpWiZkIMN7L6NQV4=; b=lYJdy1SXlARFoUS1ZMf3ipR1OWEsCfUO3kT5fKxbFQzGILzl6NTW27XV yLACSvWyI8S2kFYkvIyFjKxNwKTFHjtpDBnlTqTmAcsF4IxcHONRGlDA8 SC0Hyy5HzicytVtn5BQ+dEZr8j89nGK1BqsAagO+ih58vbgD8nJtZC0Da 8=; IronPort-SDR: ZjHW5QOC0Mi760T3TTCB5q1DzE4+elDBfqWAwm1ArhaW6OAHUGRJOhxs1x8C7/plufWoVwM1U8 t9D2+pBTLd0A== X-IronPort-AV: E=Sophos;i="5.73,373,1583193600"; d="scan'208";a="29400602" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-1e-a70de69e.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-out-6002.iad6.amazon.com with ESMTP; 10 May 2020 01:48:13 +0000 Received: from EX13MTAUWB001.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan2.iad.amazon.com [10.40.159.162]) by email-inbound-relay-1e-a70de69e.us-east-1.amazon.com (Postfix) with ESMTPS id A132BA1FC4; Sun, 10 May 2020 01:48:12 +0000 (UTC) Received: from EX13D01UWB002.ant.amazon.com (10.43.161.136) by EX13MTAUWB001.ant.amazon.com (10.43.161.207) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Sun, 10 May 2020 01:48:11 +0000 Received: from EX13MTAUEA001.ant.amazon.com (10.43.61.82) by EX13d01UWB002.ant.amazon.com (10.43.161.136) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Sun, 10 May 2020 01:48:11 +0000 Received: from localhost (10.85.6.121) by mail-relay.amazon.com (10.43.61.243) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Sun, 10 May 2020 01:48:10 +0000 From: Balbir Singh To: , CC: , , , , , , , Balbir Singh Subject: [PATCH v6 1/6] arch/x86/kvm: Refactor l1d flush lifecycle management Date: Sun, 10 May 2020 11:47:58 +1000 Message-ID: <20200510014803.12190-2-sblbir@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200510014803.12190-1-sblbir@amazon.com> References: <20200510014803.12190-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 e77261db2391..c17c1e3c1a0b 100644 --- a/arch/x86/kernel/Makefile +++ b/arch/x86/kernel/Makefile @@ -157,3 +157,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