From patchwork Tue Sep 20 02:31:04 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dongli Zhang X-Patchwork-Id: 9340873 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 5D2FB6022E for ; Tue, 20 Sep 2016 02:34:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 49E1B29B90 for ; Tue, 20 Sep 2016 02:34:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3EC6F29B92; Tue, 20 Sep 2016 02:34:28 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id ACD0129B90 for ; Tue, 20 Sep 2016 02:34:27 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bmArH-0004Pj-Vw; Tue, 20 Sep 2016 02:32:31 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bmArG-0004PG-AZ for xen-devel@lists.xen.org; Tue, 20 Sep 2016 02:32:30 +0000 Received: from [85.158.139.211] by server-16.bemta-5.messagelabs.com id 09/5D-26103-DBF90E75; Tue, 20 Sep 2016 02:32:29 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrLLMWRWlGSWpSXmKPExsUyZ7p8oO6e+Q/ CDS5uNLVY8nExiwOjx9Hdv5kCGKNYM/OS8isSWDO+n3/MWHDEsGLGlUmsDYyNal2MXBxCAhOZ JBZPamGDcL4wSrxc/wXK2cgo8WnOZCYIp5tR4vj9HyxdjJwcbAI6EtMOnAKzRQSkJa59vswIU sQs0MUk8eLeFiaQhLBAksSUyTMYQWwWAVWJQ11X2EBsXgE3ifl7D4LVSAjISZw8NpkVxOYUcJ eY/7AZLC4EVHN43XxmiBpDic8blzJPYORbwMiwilG9OLWoLLVI10wvqSgzPaMkNzEzR9fQwFQ vN7W4ODE9NScxqVgvOT93EyMwWBiAYAfj1AbnQ4ySHExKorxyfA/ChfiS8lMqMxKLM+KLSnNS iw8xynBwKEnwhs8DygkWpaanVqRl5gDDFiYtwcGjJMK7ByTNW1yQmFucmQ6ROsWoKCXO6w2SE ABJZJTmwbXBYuUSo6yUMC8j0CFCPAWpRbmZJajyrxjFORiVhHlTQabwZOaVwE1/BbSYCWgxY8 99kMUliQgpqQbG3q9Bx+/Iv+qoWuXK+Yc7KP7rYfH4X2cZtmvYiD2w1U0uqnkoGOxocpV7v/k X87plh3772crbXaroKuzqFOh6mMH+qvvw9aQr8g2bPHyVn/cd/8nUOmtTxt2FqzRO9ZybmOB/ qDY39cInz4rvz28+X8924YH+lwkn3pUoZh09F60xSem7mliTEktxRqKhFnNRcSIACtnje5ACA AA= X-Env-Sender: dongli.zhang@oracle.com X-Msg-Ref: server-7.tower-206.messagelabs.com!1474338747!60231515!1 X-Originating-IP: [156.151.31.81] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogMTU2LjE1MS4zMS44MSA9PiAyODgzMzk=\n X-StarScan-Received: X-StarScan-Version: 8.84; banners=-,-,- X-VirusChecked: Checked Received: (qmail 29205 invoked from network); 20 Sep 2016 02:32:28 -0000 Received: from userp1040.oracle.com (HELO userp1040.oracle.com) (156.151.31.81) by server-7.tower-206.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 20 Sep 2016 02:32:28 -0000 Received: from userv0022.oracle.com (userv0022.oracle.com [156.151.31.74]) by userp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id u8K2WHLw027732 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 20 Sep 2016 02:32:17 GMT Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by userv0022.oracle.com (8.14.4/8.13.8) with ESMTP id u8K2WGqG005205 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 20 Sep 2016 02:32:16 GMT Received: from abhmp0009.oracle.com (abhmp0009.oracle.com [141.146.116.15]) by userv0121.oracle.com (8.13.8/8.13.8) with ESMTP id u8K2WFEL030517; Tue, 20 Sep 2016 02:32:15 GMT Received: from linux.cn.oracle.com (/10.182.70.252) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 19 Sep 2016 19:32:15 -0700 From: Dongli Zhang To: xen-devel@lists.xen.org Date: Tue, 20 Sep 2016 10:31:04 +0800 Message-Id: <1474338664-5054-2-git-send-email-dongli.zhang@oracle.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1474338664-5054-1-git-send-email-dongli.zhang@oracle.com> References: <1474338664-5054-1-git-send-email-dongli.zhang@oracle.com> X-Source-IP: userv0022.oracle.com [156.151.31.74] Cc: sstabellini@kernel.org, wei.liu2@citrix.com, George.Dunlap@eu.citrix.com, jinsong.liu@alibaba-inc.com, dario.faggioli@citrix.com, ian.jackson@eu.citrix.com, tim@xen.org, jbeulich@suse.com, andrew.cooper3@citrix.com Subject: [Xen-devel] [PATCH v6 2/2] xen: move TLB-flush filtering out into populate_physmap during vm creation X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP This patch implemented parts of TODO left in commit id a902c12ee45fc9389eb8fe54eeddaf267a555c58 (More efficient TLB-flush filtering in alloc_heap_pages()). It moved TLB-flush filtering out into populate_physmap. Because of TLB-flush in alloc_heap_pages, it's very slow to create a guest with memory size of more than 100GB on host with 100+ cpus. This patch introduced a "MEMF_no_tlbflush" bit to memflags to indicate whether TLB-flush should be done in alloc_heap_pages or its caller populate_physmap. Once this bit is set in memflags, alloc_heap_pages will ignore TLB-flush. To use this bit after vm is created might lead to security issue, that is, this would make pages accessible to the guest B, when guest A may still have a cached mapping to them. Therefore, this patch also introduced a "creation_finished" field to struct domain to indicate whether this domain has ever got unpaused by hypervisor. MEMF_no_tlbflush can be set only during vm creation phase when creation_finished is still false before this domain gets unpaused for the first time. Signed-off-by: Dongli Zhang Acked-by: Jan Beulich Acked-by: George Dunlap Reviewed-by: Dario Faggioli --- Changed since v5: * Remove conditional check before "d->creation_finished = true;". * Place "bool creation_finished;" next to the other group of booleans. * Remove duplicate "only" in comments. Changed since v4: * Rename is_ever_unpaused to creation_finished. * Change bool_t to bool. * Polish comments. Changed since v3: * Set the flag to true in domain_unpause_by_systemcontroller when unpausing the guest domain for the first time. * Use true/false for all boot_t variables. * Add unlikely to optimize "if statement". * Correct comment style. Changed since v2: * Limit this optimization to domain creation time. --- xen/common/domain.c | 7 +++++++ xen/common/memory.c | 22 ++++++++++++++++++++++ xen/common/page_alloc.c | 4 +++- xen/include/xen/mm.h | 2 ++ xen/include/xen/sched.h | 6 ++++++ 5 files changed, 40 insertions(+), 1 deletion(-) diff --git a/xen/common/domain.c b/xen/common/domain.c index a8804e4..3abaca9 100644 --- a/xen/common/domain.c +++ b/xen/common/domain.c @@ -1004,6 +1004,13 @@ int domain_unpause_by_systemcontroller(struct domain *d) { int old, new, prev = d->controller_pause_count; + /* + * We record this information here for populate_physmap to figure out + * that the domain has finished being created. In fact, we're only + * allowed to set the MEMF_no_tlbflush flag during VM creation. + */ + d->creation_finished = true; + do { old = prev; diff --git a/xen/common/memory.c b/xen/common/memory.c index cc0f69e..21797ca 100644 --- a/xen/common/memory.c +++ b/xen/common/memory.c @@ -141,6 +141,8 @@ static void populate_physmap(struct memop_args *a) unsigned int i, j; xen_pfn_t gpfn, mfn; struct domain *d = a->domain, *curr_d = current->domain; + bool need_tlbflush = false; + uint32_t tlbflush_timestamp = 0; if ( !guest_handle_subrange_okay(a->extent_list, a->nr_done, a->nr_extents-1) ) @@ -150,6 +152,17 @@ static void populate_physmap(struct memop_args *a) max_order(curr_d)) ) return; + /* + * With MEMF_no_tlbflush set, alloc_heap_pages() will ignore + * TLB-flushes. After VM creation, this is a security issue (it can + * make pages accessible to guest B, when guest A may still have a + * cached mapping to them). So we do this only during domain creation, + * when the domain itself has not yet been unpaused for the first + * time. + */ + if ( unlikely(!d->creation_finished) ) + a->memflags |= MEMF_no_tlbflush; + for ( i = a->nr_done; i < a->nr_extents; i++ ) { if ( i != a->nr_done && hypercall_preempt_check() ) @@ -214,6 +227,13 @@ static void populate_physmap(struct memop_args *a) goto out; } + if ( unlikely(a->memflags & MEMF_no_tlbflush) ) + { + for ( j = 0; j < (1U << a->extent_order); j++ ) + accumulate_tlbflush(&need_tlbflush, &page[j], + &tlbflush_timestamp); + } + mfn = page_to_mfn(page); } @@ -232,6 +252,8 @@ static void populate_physmap(struct memop_args *a) } out: + if ( need_tlbflush ) + filtered_flush_tlb_mask(tlbflush_timestamp); a->nr_done = i; } diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c index d7ca3a0..ae2476d 100644 --- a/xen/common/page_alloc.c +++ b/xen/common/page_alloc.c @@ -827,7 +827,9 @@ static struct page_info *alloc_heap_pages( BUG_ON(pg[i].count_info != PGC_state_free); pg[i].count_info = PGC_state_inuse; - accumulate_tlbflush(&need_tlbflush, &pg[i], &tlbflush_timestamp); + if ( !(memflags & MEMF_no_tlbflush) ) + accumulate_tlbflush(&need_tlbflush, &pg[i], + &tlbflush_timestamp); /* Initialise fields which have other uses for free pages. */ pg[i].u.inuse.type_info = 0; diff --git a/xen/include/xen/mm.h b/xen/include/xen/mm.h index 50db01d..76fbb82 100644 --- a/xen/include/xen/mm.h +++ b/xen/include/xen/mm.h @@ -222,6 +222,8 @@ struct npfec { #define MEMF_exact_node (1U<<_MEMF_exact_node) #define _MEMF_no_owner 5 #define MEMF_no_owner (1U<<_MEMF_no_owner) +#define _MEMF_no_tlbflush 6 +#define MEMF_no_tlbflush (1U<<_MEMF_no_tlbflush) #define _MEMF_node 8 #define MEMF_node_mask ((1U << (8 * sizeof(nodeid_t))) - 1) #define MEMF_node(n) ((((n) + 1) & MEMF_node_mask) << _MEMF_node) diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h index 2f9c15f..cd05e55 100644 --- a/xen/include/xen/sched.h +++ b/xen/include/xen/sched.h @@ -386,6 +386,12 @@ struct domain bool_t disable_migrate; /* Is this guest being debugged by dom0? */ bool_t debugger_attached; + /* + * Set to true at the very end of domain creation, when the domain is + * unpaused for the first time by the systemcontroller. + */ + bool creation_finished; + /* Which guest this guest has privileges on */ struct domain *target;