From patchwork Tue Aug 8 21:45:05 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boris Ostrovsky X-Patchwork-Id: 9889131 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 86B9660352 for ; Tue, 8 Aug 2017 21:46:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 750902890F for ; Tue, 8 Aug 2017 21:46:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 699CF2892F; Tue, 8 Aug 2017 21:46:13 +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 16551289C0 for ; Tue, 8 Aug 2017 21:46:10 +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 1dfCHC-00082q-DE; Tue, 08 Aug 2017 21:42:58 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dfCHA-00081e-Jk for xen-devel@lists.xen.org; Tue, 08 Aug 2017 21:42:56 +0000 Received: from [85.158.139.211] by server-1.bemta-5.messagelabs.com id B3/43-01993-F503A895; Tue, 08 Aug 2017 21:42:55 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFupikeJIrShJLcpLzFFi42LpnVTnqhtv0BV pMPMBu8WSj4tZHBg9ju7+zRTAGMWamZeUX5HAmrHzyGzWgt7IilN71jM1MPZYdDFycQgJTGCS uHW3gR3C+cUo8XrCJjYIZz2jRNP6hcwQTg+jxNt7b5i6GDk52ASMJM4enc4IYosISEtc+3yZE aSIWWAak8Teb+/AEsICXhI9i1axgtgsAqoSb66/YwaxeYHij+Y+AquREFCQmPLwPVicU8BbYu aFvywgthBQzYzJU5khagwlTj/cxjiBkW8BI8MqRo3i1KKy1CJdI0O9pKLM9IyS3MTMHF1DA1O 93NTi4sT01JzEpGK95PzcTYzAcKlnYGDcwXh3st8hRkkOJiVR3k3anZFCfEn5KZUZicUZ8UWl OanFhxhlODiUJHiT9boihQSLUtNTK9Iyc4CBC5OW4OBREuHl1gdK8xYXJOYWZ6ZDpE4x6nK8m vD/G5MQS15+XqqUOO9dkBkCIEUZpXlwI2BRdIlRVkqYl5GBgUGIpyC1KDezBFX+FaM4B6OSMO 9zkCk8mXklcJteAR3BBHREhG8nyBEliQgpqQbG7PlsJt/TJ1h9dNpRWBvx9tbj/GbX65KZDic /nLwh9HJZm01UCp/30nc5r4+vnPViwZtTv4pSnnaHn/rffGvzw9jdK0/cV6+Yd/H8FSmV24cN /guGsU6cvLLhaGbPtFjN1WLPYr5mBrT+Kg9s8Y1z36S7ZI5ntyHTFJlpCcJed2UvX2dxtzhfp cRSnJFoqMVcVJwIAK5vtNydAgAA X-Env-Sender: boris.ostrovsky@oracle.com X-Msg-Ref: server-9.tower-206.messagelabs.com!1502228572!106531279!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.45; banners=-,-,- X-VirusChecked: Checked Received: (qmail 64096 invoked from network); 8 Aug 2017 21:42:54 -0000 Received: from aserp1040.oracle.com (HELO aserp1040.oracle.com) (141.146.126.69) by server-9.tower-206.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 8 Aug 2017 21:42:54 -0000 Received: from userv0021.oracle.com (userv0021.oracle.com [156.151.31.71]) by aserp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id v78LgimC022562 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 8 Aug 2017 21:42:44 GMT Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by userv0021.oracle.com (8.14.4/8.14.4) with ESMTP id v78Lgiiv007311 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 8 Aug 2017 21:42:44 GMT Received: from abhmp0003.oracle.com (abhmp0003.oracle.com [141.146.116.9]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id v78LghOj026708; Tue, 8 Aug 2017 21:42:43 GMT Received: from ovs104.us.oracle.com (/10.149.76.204) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 08 Aug 2017 14:42:43 -0700 From: Boris Ostrovsky To: xen-devel@lists.xen.org Date: Tue, 8 Aug 2017 17:45:05 -0400 Message-Id: <1502228707-31883-8-git-send-email-boris.ostrovsky@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1502228707-31883-1-git-send-email-boris.ostrovsky@oracle.com> References: <1502228707-31883-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, julien.grall@arm.com, jbeulich@suse.com, Boris Ostrovsky Subject: [Xen-devel] [PATCH v7 7/9] 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 BUDDY_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 Reviewed-by: Jan Beulich --- Changes in v7: * Replaced page_info with typeof(head->u.free) in check_and_stop_scrub() * Replaced 1UL with 1U in scrub_free_pages() * Fixed formatting in asm-*/mm.h xen/common/page_alloc.c | 110 +++++++++++++++++++++++++++++++++++++++++++++-- xen/include/asm-arm/mm.h | 28 +++++++----- xen/include/asm-x86/mm.h | 30 ++++++++----- 3 files changed, 143 insertions(+), 25 deletions(-) diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c index b886983..726f857 100644 --- a/xen/common/page_alloc.c +++ b/xen/common/page_alloc.c @@ -687,6 +687,7 @@ static void page_list_add_scrub(struct page_info *pg, unsigned int node, { PFN_ORDER(pg) = order; pg->u.free.first_dirty = first_dirty; + pg->u.free.scrub_state = BUDDY_NOT_SCRUBBING; if ( first_dirty != INVALID_DIRTY_IDX ) { @@ -697,6 +698,25 @@ 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 == BUDDY_SCRUBBING ) + { + typeof(head->u.free) pgfree; + + head->u.free.scrub_state = BUDDY_SCRUB_ABORT; + spin_lock_kick(); + for ( ; ; ) + { + /* Can't ACCESS_ONCE() a bitfield. */ + pgfree.val = ACCESS_ONCE(head->u.free.val); + if ( pgfree.scrub_state != BUDDY_SCRUB_ABORT ) + break; + cpu_relax(); + } + } +} + static struct page_info *get_free_buddy(unsigned int zone_lo, unsigned int zone_hi, unsigned int order, unsigned int memflags, @@ -741,14 +761,19 @@ static struct page_info *get_free_buddy(unsigned int zone_lo, { if ( (pg = page_list_remove_head(&heap(node, zone, j))) ) { + if ( pg->u.free.first_dirty == INVALID_DIRTY_IDX ) + return pg; /* * We grab single pages (order=0) even if they are * unscrubbed. Given that scrubbing one page is fairly quick * it is not worth breaking higher orders. */ - if ( (order == 0) || use_unscrubbed || - pg->u.free.first_dirty == INVALID_DIRTY_IDX) + if ( (order == 0) || use_unscrubbed ) + { + check_and_stop_scrub(pg); return pg; + } + page_list_add_tail(pg, &heap(node, zone, j)); } } @@ -929,6 +954,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 @@ -1079,6 +1105,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 == BUDDY_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 = BUDDY_NOT_SCRUBBING; + } +} + bool scrub_free_pages(void) { struct page_info *pg; @@ -1101,25 +1150,53 @@ 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 == BUDDY_NOT_SCRUBBING); + pg->u.free.scrub_state = BUDDY_SCRUBBING; + + spin_unlock(&heap_lock); + + dirty_cnt = 0; + for ( i = pg->u.free.first_dirty; i < (1U << order); i++) { 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. */ } else cnt++; + if ( pg->u.free.scrub_state == BUDDY_SCRUB_ABORT ) + { + /* Someone wants this chunk. Drop everything. */ + + pg->u.free.first_dirty = (i == (1U << order) - 1) ? + INVALID_DIRTY_IDX : i + 1; + smp_wmb(); + pg->u.free.scrub_state = BUDDY_NOT_SCRUBBING; + + 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 * preemption. But also count non-scrubbing loop iterations @@ -1133,6 +1210,23 @@ bool scrub_free_pages(void) } } + st.pg = pg; + /* + * get_free_buddy() grabs a buddy with first_dirty set to + * INVALID_DIRTY_IDX so we can't set pg's first_dirty here. + * It will be set either below or in the lock callback (in + * scrub_continue()). + */ + st.first_dirty = (i >= (1U << order) - 1) ? + 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) - 1 ) { page_list_del(pg, &heap(node, zone, order)); @@ -1141,6 +1235,8 @@ bool scrub_free_pages(void) else pg->u.free.first_dirty = i + 1; + pg->u.free.scrub_state = BUDDY_NOT_SCRUBBING; + if ( preempt || (node_need_scrub[node] == 0) ) goto out; } @@ -1149,6 +1245,8 @@ bool scrub_free_pages(void) out: spin_unlock(&heap_lock); + + out_nolock: node_clear(node, node_scrubbing); return node_to_scrub(false) != NUMA_NO_NODE; } @@ -1230,6 +1328,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)); /* Keep predecessor's first_dirty if it is already set. */ @@ -1251,6 +1351,8 @@ static void free_heap_pages( (phys_to_nid(page_to_maddr(successor)) != node) ) break; + check_and_stop_scrub(successor); + page_list_del(successor, &heap(node, zone, order)); } diff --git a/xen/include/asm-arm/mm.h b/xen/include/asm-arm/mm.h index 572337c..d42b070 100644 --- a/xen/include/asm-arm/mm.h +++ b/xen/include/asm-arm/mm.h @@ -42,18 +42,26 @@ struct page_info unsigned long type_info; } inuse; /* Page is on a free list: ((count_info & PGC_count_mask) == 0). */ - struct { - /* Do TLBs need flushing for safety before next page use? */ - bool need_tlbflush:1; - - /* - * Index of the first *possibly* unscrubbed page in the buddy. - * One more bit than maximum possible order to accommodate - * INVALID_DIRTY_IDX. - */ + union { + struct { + /* Do TLBs need flushing for safety before next page use? */ + bool need_tlbflush:1; + + /* + * Index of the first *possibly* unscrubbed page in the buddy. + * One more bit than maximum possible order to accommodate + * INVALID_DIRTY_IDX. + */ #define INVALID_DIRTY_IDX ((1UL << (MAX_ORDER + 1)) - 1) - unsigned long first_dirty:MAX_ORDER + 1; + unsigned long first_dirty:MAX_ORDER + 1; + +#define BUDDY_NOT_SCRUBBING 0 +#define BUDDY_SCRUBBING 1 +#define BUDDY_SCRUB_ABORT 2 + unsigned long scrub_state:2; + }; + unsigned long val; } free; } u; diff --git a/xen/include/asm-x86/mm.h b/xen/include/asm-x86/mm.h index 07dc963..c9cf6c3 100644 --- a/xen/include/asm-x86/mm.h +++ b/xen/include/asm-x86/mm.h @@ -86,19 +86,27 @@ struct page_info } sh; /* Page is on a free list: ((count_info & PGC_count_mask) == 0). */ - struct { - /* Do TLBs need flushing for safety before next page use? */ - bool need_tlbflush:1; - - /* - * Index of the first *possibly* unscrubbed page in the buddy. - * One more bit than maximum possible order to accommodate - * INVALID_DIRTY_IDX. - */ + union { + struct { + /* Do TLBs need flushing for safety before next page use? */ + bool need_tlbflush:1; + + /* + * Index of the first *possibly* unscrubbed page in the buddy. + * One more bit than maximum possible order to accommodate + * INVALID_DIRTY_IDX. + */ #define INVALID_DIRTY_IDX ((1UL << (MAX_ORDER + 1)) - 1) - unsigned long first_dirty:MAX_ORDER + 1; - } free; + unsigned long first_dirty:MAX_ORDER + 1; +#define BUDDY_NOT_SCRUBBING 0 +#define BUDDY_SCRUBBING 1 +#define BUDDY_SCRUB_ABORT 2 + unsigned long scrub_state:2; + }; + + unsigned long val; + } free; } u; union {