From patchwork Fri Aug 4 17:05:42 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boris Ostrovsky X-Patchwork-Id: 9881749 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 4702D603F4 for ; Fri, 4 Aug 2017 17:05:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 31705288F7 for ; Fri, 4 Aug 2017 17:05:56 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2626028962; Fri, 4 Aug 2017 17:05:56 +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 567622896D for ; Fri, 4 Aug 2017 17:05:55 +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 1ddg0d-0006iO-7K; Fri, 04 Aug 2017 17:03:35 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ddg0c-0006hW-JH for xen-devel@lists.xen.org; Fri, 04 Aug 2017 17:03:34 +0000 Received: from [85.158.137.68] by server-14.bemta-3.messagelabs.com id 7C/1D-01862-5E8A4895; Fri, 04 Aug 2017 17:03:33 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFupikeJIrShJLcpLzFFi42LpnVTnqvt0RUu kweVXIhZLPi5mcWD0OLr7N1MAYxRrZl5SfkUCa8bcu4/YCn64VBx60cfUwLjWoIuRi0NIYBKT xPI9fawQzi9GiU3fTrBBOBsYJRbdeM8C4fQwSnw5sIu5i5GTg03ASOLs0emMILaIgLTEtc+XG UGKmAW2MEn86drOApIQFjCXWHX6PpjNIqAq8WzeV7BmXgFPiZvTnjCB2BICChJTHr4Hi3MKeE k0t75gB7GFgGpO3v8BVWMs0f72ItsERr4FjAyrGDWKU4vKUot0jcz0kooy0zNKchMzc3QNDYz 1clOLixPTU3MSk4r1kvNzNzECw6WegYFxB2PDXr9DjJIcTEqivNXHmiKF+JLyUyozEosz4otK c1KLDzHKcHAoSfCuX94SKSRYlJqeWpGWmQMMXJi0BAePkgjvA5A0b3FBYm5xZjpE6hSjLserC f+/MQmx5OXnpUqJ8y4GKRIAKcoozYMbAYuiS4yyUsK8jAwMDEI8BalFuZklqPKvGMU5GJWEeb eATOHJzCuB2/QK6AgmoCP+1DWCHFGSiJCSamCs9bI6sTozWmZ+uXhrw9ywf41XEg7W3NaIz3j r6X9Tp6s3d6v82tlmrMWsWm5NhToxJ4U1bWKtpumc8HkssXzHM86aZ6++NK3ckPml+sEtxaJ1 y/7rbXTMdePf0rB2xSQFzTgOtjUMcYsmPVzdILJ685Y7TzQ3m4ldCq5wDV/wut5IuO7952gll uKMREMt5qLiRABcB0H1nQIAAA== X-Env-Sender: boris.ostrovsky@oracle.com X-Msg-Ref: server-5.tower-31.messagelabs.com!1501866211!105416129!1 X-Originating-IP: [141.146.126.69] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogMTQxLjE0Ni4xMjYuNjkgPT4gMjc3MjE4\n X-StarScan-Received: X-StarScan-Version: 9.4.25; banners=-,-,- X-VirusChecked: Checked Received: (qmail 10080 invoked from network); 4 Aug 2017 17:03:33 -0000 Received: from aserp1040.oracle.com (HELO aserp1040.oracle.com) (141.146.126.69) by server-5.tower-31.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 4 Aug 2017 17:03:33 -0000 Received: from aserv0022.oracle.com (aserv0022.oracle.com [141.146.126.234]) by aserp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id v74H3Rhg028061 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 4 Aug 2017 17:03:27 GMT Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by aserv0022.oracle.com (8.14.4/8.14.4) with ESMTP id v74H3QpD015043 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 4 Aug 2017 17:03:27 GMT Received: from abhmp0013.oracle.com (abhmp0013.oracle.com [141.146.116.19]) by userv0121.oracle.com (8.14.4/8.13.8) with ESMTP id v74H3QFL000649; Fri, 4 Aug 2017 17:03:26 GMT Received: from ovs104.us.oracle.com (/10.149.76.204) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 04 Aug 2017 10:03:25 -0700 From: Boris Ostrovsky To: xen-devel@lists.xen.org Date: Fri, 4 Aug 2017 13:05:42 -0400 Message-Id: <1501866346-9774-5-git-send-email-boris.ostrovsky@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1501866346-9774-1-git-send-email-boris.ostrovsky@oracle.com> References: <1501866346-9774-1-git-send-email-boris.ostrovsky@oracle.com> X-Source-IP: aserv0022.oracle.com [141.146.126.234] Cc: sstabellini@kernel.org, wei.liu2@citrix.com, George.Dunlap@eu.citrix.com, andrew.cooper3@citrix.com, Dario Faggioli , ian.jackson@eu.citrix.com, tim@xen.org, jbeulich@suse.com, Boris Ostrovsky Subject: [Xen-devel] [PATCH v6 4/8] mm: Scrub memory from idle loop 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 Instead of scrubbing pages during guest destruction (from free_heap_pages()) do this opportunistically, from the idle loop. We might come to scrub_free_pages()from idle loop while another CPU uses mapcache override, resulting in a fault while trying to do __map_domain_page() in scrub_one_page(). To avoid this, make mapcache vcpu override a per-cpu variable. Signed-off-by: Boris Ostrovsky Reviewed-by: Jan Beulich Reviewed-by: Dario Faggioli --- CC: Dario Faggioli --- Changes in v6: * Moved final softirq_pending() test from scrub_free_pages() to idle loop. xen/arch/arm/domain.c | 8 ++- xen/arch/x86/domain.c | 8 ++- xen/arch/x86/domain_page.c | 6 +-- xen/common/page_alloc.c | 119 ++++++++++++++++++++++++++++++++++++++++----- xen/include/xen/mm.h | 1 + 5 files changed, 124 insertions(+), 18 deletions(-) diff --git a/xen/arch/arm/domain.c b/xen/arch/arm/domain.c index 2dc8b0a..d7961bb 100644 --- a/xen/arch/arm/domain.c +++ b/xen/arch/arm/domain.c @@ -51,7 +51,13 @@ void idle_loop(void) /* Are we here for running vcpu context tasklets, or for idling? */ if ( unlikely(tasklet_work_to_do(cpu)) ) do_tasklet(); - else + /* + * Test softirqs twice --- first to see if should even try scrubbing + * and then, after it is done, whether softirqs became pending + * while we were scrubbing. + */ + else if ( !softirq_pending(cpu) && !scrub_free_pages() && + !softirq_pending(cpu) ) { local_irq_disable(); if ( cpu_is_haltable(cpu) ) diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index baaf815..9b4b959 100644 --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -122,7 +122,13 @@ static void idle_loop(void) /* Are we here for running vcpu context tasklets, or for idling? */ if ( unlikely(tasklet_work_to_do(cpu)) ) do_tasklet(); - else + /* + * Test softirqs twice --- first to see if should even try scrubbing + * and then, after it is done, whether softirqs became pending + * while we were scrubbing. + */ + else if ( !softirq_pending(cpu) && !scrub_free_pages() && + !softirq_pending(cpu) ) pm_idle(); do_softirq(); /* diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c index 71baede..0783c1e 100644 --- a/xen/arch/x86/domain_page.c +++ b/xen/arch/x86/domain_page.c @@ -18,12 +18,12 @@ #include #include -static struct vcpu *__read_mostly override; +static DEFINE_PER_CPU(struct vcpu *, override); static inline struct vcpu *mapcache_current_vcpu(void) { /* In the common case we use the mapcache of the running VCPU. */ - struct vcpu *v = override ?: current; + struct vcpu *v = this_cpu(override) ?: current; /* * When current isn't properly set up yet, this is equivalent to @@ -59,7 +59,7 @@ static inline struct vcpu *mapcache_current_vcpu(void) void __init mapcache_override_current(struct vcpu *v) { - override = v; + this_cpu(override) = v; } #define mapcache_l2_entry(e) ((e) >> PAGETABLE_ORDER) diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c index eedff2d..3f04f16 100644 --- a/xen/common/page_alloc.c +++ b/xen/common/page_alloc.c @@ -1024,15 +1024,86 @@ static int reserve_offlined_page(struct page_info *head) return count; } -static void scrub_free_pages(unsigned int node) +static nodemask_t node_scrubbing; + +/* + * If get_node is true this will return closest node that needs to be scrubbed, + * with appropriate bit in node_scrubbing set. + * If get_node is not set, this will return *a* node that needs to be scrubbed. + * node_scrubbing bitmask will no be updated. + * If no node needs scrubbing then NUMA_NO_NODE is returned. + */ +static unsigned int node_to_scrub(bool get_node) { - struct page_info *pg; - unsigned int zone; + nodeid_t node = cpu_to_node(smp_processor_id()), local_node; + nodeid_t closest = NUMA_NO_NODE; + u8 dist, shortest = 0xff; - ASSERT(spin_is_locked(&heap_lock)); + if ( node == NUMA_NO_NODE ) + node = 0; - if ( !node_need_scrub[node] ) - return; + if ( node_need_scrub[node] && + (!get_node || !node_test_and_set(node, node_scrubbing)) ) + return node; + + /* + * See if there are memory-only nodes that need scrubbing and choose + * the closest one. + */ + local_node = node; + for ( ; ; ) + { + do { + node = cycle_node(node, node_online_map); + } while ( !cpumask_empty(&node_to_cpumask(node)) && + (node != local_node) ); + + if ( node == local_node ) + break; + + if ( node_need_scrub[node] ) + { + if ( !get_node ) + return node; + + dist = __node_distance(local_node, node); + + /* + * Grab the node right away. If we find a closer node later we will + * release this one. While there is a chance that another CPU will + * not be able to scrub that node when it is searching for scrub work + * at the same time it will be able to do so next time it wakes up. + * The alternative would be to perform this search under a lock but + * then we'd need to take this lock every time we come in here. + */ + if ( (dist < shortest || closest == NUMA_NO_NODE) && + !node_test_and_set(node, node_scrubbing) ) + { + if ( closest != NUMA_NO_NODE ) + node_clear(closest, node_scrubbing); + shortest = dist; + closest = node; + } + } + } + + return closest; +} + +bool scrub_free_pages(void) +{ + struct page_info *pg; + unsigned int zone; + unsigned int cpu = smp_processor_id(); + bool preempt = false; + nodeid_t node; + unsigned int cnt = 0; + + node = node_to_scrub(true); + if ( node == NUMA_NO_NODE ) + return false; + + spin_lock(&heap_lock); for ( zone = 0; zone < NR_ZONES; zone++ ) { @@ -1055,17 +1126,42 @@ static void scrub_free_pages(unsigned int node) scrub_one_page(&pg[i]); pg[i].count_info &= ~PGC_need_scrub; node_need_scrub[node]--; + cnt += 100; /* scrubbed pages add heavier weight. */ + } + else + cnt++; + + /* + * Scrub a few (8) pages before becoming eligible for + * preemption. But also count non-scrubbing loop iterations + * so that we don't get stuck here with an almost clean + * heap. + */ + if ( cnt > 800 && softirq_pending(cpu) ) + { + preempt = true; + break; } } - page_list_del(pg, &heap(node, zone, order)); - page_list_add_scrub(pg, node, zone, order, INVALID_DIRTY_IDX); + if ( i >= (1U << order) - 1 ) + { + page_list_del(pg, &heap(node, zone, order)); + page_list_add_scrub(pg, node, zone, order, INVALID_DIRTY_IDX); + } + else + pg->u.free.first_dirty = i + 1; - if ( node_need_scrub[node] == 0 ) - return; + if ( preempt || (node_need_scrub[node] == 0) ) + goto out; } } while ( order-- != 0 ); } + + out: + spin_unlock(&heap_lock); + node_clear(node, node_scrubbing); + return node_to_scrub(false) != NUMA_NO_NODE; } /* Free 2^@order set of pages. */ @@ -1176,9 +1272,6 @@ static void free_heap_pages( if ( tainted ) reserve_offlined_page(pg); - if ( need_scrub ) - scrub_free_pages(node); - spin_unlock(&heap_lock); } diff --git a/xen/include/xen/mm.h b/xen/include/xen/mm.h index e1f9c42..ddc3fb3 100644 --- a/xen/include/xen/mm.h +++ b/xen/include/xen/mm.h @@ -160,6 +160,7 @@ void init_xenheap_pages(paddr_t ps, paddr_t pe); void xenheap_max_mfn(unsigned long mfn); void *alloc_xenheap_pages(unsigned int order, unsigned int memflags); void free_xenheap_pages(void *v, unsigned int order); +bool scrub_free_pages(void); #define alloc_xenheap_page() (alloc_xenheap_pages(0,0)) #define free_xenheap_page(v) (free_xenheap_pages(v,0)) /* Map machine page range in Xen virtual address space. */