From patchwork Fri May 19 15:50:38 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boris Ostrovsky X-Patchwork-Id: 9737503 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 93CB26020B for ; Fri, 19 May 2017 15:52:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 94B2926AE3 for ; Fri, 19 May 2017 15:52:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 889BA28179; Fri, 19 May 2017 15:52:20 +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 E1C6326AE3 for ; Fri, 19 May 2017 15:52:19 +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 1dBkA3-0000hc-KH; Fri, 19 May 2017 15:49:51 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dBkA2-0000g6-5d for xen-devel@lists.xen.org; Fri, 19 May 2017 15:49:50 +0000 Received: from [193.109.254.147] by server-7.bemta-6.messagelabs.com id 63/CF-03620-D141F195; Fri, 19 May 2017 15:49:49 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrLLMWRWlGSWpSXmKPExsUyZ7p8oK6siHy kwd5oiyUfF7M4MHoc3f2bKYAxijUzLym/IoE1Y3rHT9aCP64VWz8uZW1g3KXbxcjFISQwkUni 8+MnzBDOX0aJe683MUI4Gxgldu2bzA7h9DBK/Py1CKiMk4NNwEji7NHpjCC2iIC0xLXPl8E6m AUamCSenzsIlhAW8JI4uno1O4jNIqAq8XPvZjYQmxcovm5iOyuILSGgIDHl4XuwoZwC3hJvJs 0CiwsB1fya1QdVYyhx+uE2xgmMfAsYGVYxahSnFpWlFukameslFWWmZ5TkJmbm6BoamOnlphY XJ6an5iQmFesl5+duYgQGCwMQ7GBcvDbwEKMkB5OSKK/jYblIIb6k/JTKjMTijPii0pzU4kOM MhwcShK8lsLykUKCRanpqRVpmTnAsIVJS3DwKInwloKkeYsLEnOLM9MhUqcYFaXEeT8IASUEQ BIZpXlwbbBYucQoKyXMywh0iBBPQWpRbmYJqvwrRnEORiVh3gCQ8TyZeSVw018BLWYCWtz8QB pkcUkiQkqqgXHljpWe0ZErAq3NZE8blUodVW+1dBL3Kc4oOVyf4lK4dHWO7pdYvvkTVBMWZcw +5ff57dfXR16Zx0q/O/ktanOT3tIU5ofP3itPypzG/H+z9aWsyTVLwh53V97c9ZnHTZHhoUrt Ft61N7zr/z9g99xlXj/l88+rt39ufPZp869FzqlzO/ftafRXYinOSDTUYi4qTgQAheP2EJACA AA= X-Env-Sender: boris.ostrovsky@oracle.com X-Msg-Ref: server-15.tower-27.messagelabs.com!1495208987!50457895!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: 9.4.12; banners=-,-,- X-VirusChecked: Checked Received: (qmail 23296 invoked from network); 19 May 2017 15:49:48 -0000 Received: from userp1040.oracle.com (HELO userp1040.oracle.com) (156.151.31.81) by server-15.tower-27.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 19 May 2017 15:49:48 -0000 Received: from userv0021.oracle.com (userv0021.oracle.com [156.151.31.71]) by userp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id v4JFnfTQ032619 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 19 May 2017 15:49:41 GMT Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by userv0021.oracle.com (8.14.4/8.14.4) with ESMTP id v4JFnf6d026072 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 19 May 2017 15:49:41 GMT Received: from abhmp0001.oracle.com (abhmp0001.oracle.com [141.146.116.7]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id v4JFner2030860; Fri, 19 May 2017 15:49:40 GMT Received: from ovs104.us.oracle.com (/10.149.76.204) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 19 May 2017 08:49:40 -0700 From: Boris Ostrovsky To: xen-devel@lists.xen.org Date: Fri, 19 May 2017 11:50:38 -0400 Message-Id: <1495209040-11101-7-git-send-email-boris.ostrovsky@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1495209040-11101-1-git-send-email-boris.ostrovsky@oracle.com> References: <1495209040-11101-1-git-send-email-boris.ostrovsky@oracle.com> X-Source-IP: userv0021.oracle.com [156.151.31.71] Cc: sstabellini@kernel.org, wei.liu2@citrix.com, George.Dunlap@eu.citrix.com, andrew.cooper3@citrix.com, ian.jackson@eu.citrix.com, tim@xen.org, jbeulich@suse.com, Boris Ostrovsky Subject: [Xen-devel] [PATCH v4 6/8] mm: Keep heap accessible to others while scrubbing 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 while holding heap lock we can mark buddy's head as being scrubbed and drop the lock temporarily. If someone (most likely alloc_heap_pages()) tries to access this chunk it will signal the scrubber to abort scrub by setting head's PAGE_SCRUB_ABORT bit. The scrubber checks this bit after processing each page and stops its work as soon as it sees it. Signed-off-by: Boris Ostrovsky --- Changes in v4: * Drop unnecessary ACCESS_ONCE in scrub_free_pages, add smp_wmb() in scrub_continue() * Keep track of first_dirty in scrub_wait_state xen/common/page_alloc.c | 100 ++++++++++++++++++++++++++++++++++++++++++++--- xen/include/asm-arm/mm.h | 4 ++ xen/include/asm-x86/mm.h | 4 ++ 3 files changed, 103 insertions(+), 5 deletions(-) diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c index 6e505b1..b365305 100644 --- a/xen/common/page_alloc.c +++ b/xen/common/page_alloc.c @@ -694,6 +694,17 @@ static void page_list_add_scrub(struct page_info *pg, unsigned int node, page_list_add(pg, &heap(node, zone, order)); } +static void check_and_stop_scrub(struct page_info *head) +{ + if ( head->u.free.scrub_state & PAGE_SCRUBBING ) + { + head->u.free.scrub_state |= PAGE_SCRUB_ABORT; + spin_lock_kick(); + while ( ACCESS_ONCE(head->u.free.scrub_state) & PAGE_SCRUB_ABORT ) + cpu_relax(); + } +} + static struct page_info *get_free_buddy(unsigned int zone_lo, unsigned int zone_hi, unsigned int order, unsigned int memflags, @@ -738,9 +749,15 @@ static struct page_info *get_free_buddy(unsigned int zone_lo, { if ( (pg = page_list_remove_head(&heap(node, zone, j))) ) { - if ( (order == 0) || use_unscrubbed || - pg->u.free.first_dirty == INVALID_DIRTY_IDX) + if ( pg->u.free.first_dirty == INVALID_DIRTY_IDX ) return pg; + + if ( (order == 0) || use_unscrubbed ) + { + check_and_stop_scrub(pg); + return pg; + } + page_list_add_tail(pg, &heap(node, zone, j)); } } @@ -925,6 +942,7 @@ static int reserve_offlined_page(struct page_info *head) cur_head = head; + check_and_stop_scrub(head); /* * We may break the buddy so let's mark the head as clean. Then, when * merging chunks back into the heap, we will see whether the chunk has @@ -1069,6 +1087,29 @@ static unsigned int node_to_scrub(bool get_node) return closest; } +struct scrub_wait_state { + struct page_info *pg; + unsigned int first_dirty; + bool drop; +}; + +static void scrub_continue(void *data) +{ + struct scrub_wait_state *st = data; + + if ( st->drop ) + return; + + if ( st->pg->u.free.scrub_state & PAGE_SCRUB_ABORT ) + { + /* There is a waiter for this buddy. Release it. */ + st->drop = true; + st->pg->u.free.first_dirty = st->first_dirty; + smp_wmb(); + st->pg->u.free.scrub_state = 0; + } +} + bool scrub_free_pages(void) { struct page_info *pg; @@ -1090,24 +1131,51 @@ bool scrub_free_pages(void) do { while ( !page_list_empty(&heap(node, zone, order)) ) { - unsigned int i; + unsigned int i, dirty_cnt; + struct scrub_wait_state st; /* Unscrubbed pages are always at the end of the list. */ pg = page_list_last(&heap(node, zone, order)); if ( pg->u.free.first_dirty == INVALID_DIRTY_IDX ) break; + ASSERT(!pg->u.free.scrub_state); + pg->u.free.scrub_state = PAGE_SCRUBBING; + + spin_unlock(&heap_lock); + + dirty_cnt = 0; for ( i = pg->u.free.first_dirty; i < (1U << order); i++) { cnt++; if ( test_bit(_PGC_need_scrub, &pg[i].count_info) ) { scrub_one_page(&pg[i]); + /* + * We can modify count_info without holding heap + * lock since we effectively locked this buddy by + * setting its scrub_state. + */ pg[i].count_info &= ~PGC_need_scrub; - node_need_scrub[node]--; + dirty_cnt++; cnt += 100; /* scrubbed pages add heavier weight. */ } + if ( pg->u.free.scrub_state & PAGE_SCRUB_ABORT ) + { + /* Someone wants this chunk. Drop everything. */ + + pg->u.free.first_dirty = (i == (1U << order)) ? + INVALID_DIRTY_IDX : i + 1; + smp_wmb(); + pg->u.free.scrub_state = 0; + + spin_lock(&heap_lock); + node_need_scrub[node] -= dirty_cnt; + spin_unlock(&heap_lock); + goto out_nolock; + } + /* * Scrub a few (8) pages before becoming eligible for * preemtion. But also count non-scrubbing loop iteration @@ -1121,6 +1189,17 @@ bool scrub_free_pages(void) } } + st.pg = pg; + st.first_dirty = (i == (1UL << order)) ? + INVALID_DIRTY_IDX : i + 1; + st.drop = false; + spin_lock_cb(&heap_lock, scrub_continue, &st); + + node_need_scrub[node] -= dirty_cnt; + + if ( st.drop ) + goto out; + if ( i == (1U << order) ) { page_list_del(pg, &heap(node, zone, order)); @@ -1128,7 +1207,9 @@ bool scrub_free_pages(void) } else pg->u.free.first_dirty = i + 1; - + + pg->u.free.scrub_state = 0; + if ( preempt || (node_need_scrub[node] == 0) ) goto out; } @@ -1137,6 +1218,8 @@ bool scrub_free_pages(void) out: spin_unlock(&heap_lock); + + out_nolock: node_clear(node, node_scrubbing); return softirq_pending(cpu) || (node_to_scrub(false) != NUMA_NO_NODE); } @@ -1175,6 +1258,8 @@ static void free_heap_pages( if ( page_state_is(&pg[i], offlined) ) tainted = 1; + pg[i].u.free.scrub_state = 0; + /* If a page has no owner it will need no safety TLB flush. */ pg[i].u.free.need_tlbflush = (page_get_owner(&pg[i]) != NULL); if ( pg[i].u.free.need_tlbflush ) @@ -1218,6 +1303,8 @@ static void free_heap_pages( (phys_to_nid(page_to_maddr(predecessor)) != node) ) break; + check_and_stop_scrub(predecessor); + page_list_del(predecessor, &heap(node, zone, order)); if ( predecessor->u.free.first_dirty != INVALID_DIRTY_IDX ) @@ -1240,6 +1327,9 @@ static void free_heap_pages( (PFN_ORDER(successor) != order) || (phys_to_nid(page_to_maddr(successor)) != node) ) break; + + check_and_stop_scrub(successor); + page_list_del(successor, &heap(node, zone, order)); need_scrub |= (successor->u.free.first_dirty != INVALID_DIRTY_IDX); diff --git a/xen/include/asm-arm/mm.h b/xen/include/asm-arm/mm.h index 38d4fba..148d60b 100644 --- a/xen/include/asm-arm/mm.h +++ b/xen/include/asm-arm/mm.h @@ -46,6 +46,10 @@ struct page_info /* Index of the first *possibly* unscrubbed page in the buddy. */ #define INVALID_DIRTY_IDX -1U unsigned int first_dirty; +#define PAGE_SCRUBBING (1<<0) +#define PAGE_SCRUB_ABORT (1<<1) + unsigned char scrub_state; + /* Do TLBs need flushing for safety before next page use? */ bool_t need_tlbflush; } free; diff --git a/xen/include/asm-x86/mm.h b/xen/include/asm-x86/mm.h index e20f161..f46b242 100644 --- a/xen/include/asm-x86/mm.h +++ b/xen/include/asm-x86/mm.h @@ -90,6 +90,10 @@ struct page_info /* Index of the first *possibly* unscrubbed page in the buddy. */ #define INVALID_DIRTY_IDX -1U unsigned int first_dirty; +#define PAGE_SCRUBBING (1<<0) +#define PAGE_SCRUB_ABORT (1<<1) + unsigned char scrub_state; + /* Do TLBs need flushing for safety before next page use? */ bool_t need_tlbflush; } free;