From patchwork Tue Oct 1 15:29:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Duyck X-Patchwork-Id: 11169053 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E462415AB for ; Tue, 1 Oct 2019 15:29:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B9AE621855 for ; Tue, 1 Oct 2019 15:29:28 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="lx1oVk9a" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389604AbfJAP3Z (ORCPT ); Tue, 1 Oct 2019 11:29:25 -0400 Received: from mail-pg1-f193.google.com ([209.85.215.193]:39530 "EHLO mail-pg1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389599AbfJAP3Y (ORCPT ); Tue, 1 Oct 2019 11:29:24 -0400 Received: by mail-pg1-f193.google.com with SMTP id e1so4373550pgj.6; Tue, 01 Oct 2019 08:29:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:from:to:cc:date:message-id:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=Tg18ELn2yO5mcuA4hxXn6maR2nECizjJ7QIklFwPvZY=; b=lx1oVk9a+K28U2Q4cIxXEnv2szQ55+pVMEJbqitaU78Q9t6pOGpbqBFo9JgVDqkk7Y BAP8Gp/iLZZYowCaiahV22PwkOxuN2SrO/wFKvrzbgNB01XLsTdgW0AgCwXANCk5pd3U RnutPSXmj9y26VPc3eXfdJg0ScrTKXsFITcV6oJm3l4UNcRQz5iR+zi9LD+1JvG0uybr g/2qB0PbzVl9uRsJMGYbJL6E4PIC2Swqgeo8VuZkm9rTacz7n2xFI29IoyY01jbvRBBO ioQQmBVOopuw9V4ZUbAUBGKgUZCJDTUcZc+QOJUmZSRffxZTE2ys0U32nypNgZW/apUu dtUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=Tg18ELn2yO5mcuA4hxXn6maR2nECizjJ7QIklFwPvZY=; b=tanUmvmDMYCrPY+Q13b4xOBgVzPgqaEVeMxi3CKh/GJW5kCuaNOl17WU7d7dAvNqFU mOzaiLh++U7jF1VwqN+iPS5DaxBVAapYu2o5Yr9AO3hKl5kQ1aRZYkv8q+0OzO3tswrx DRukEZOJVpEXx3ddPD9IcRPbBR7DDbZp/VSXh6u3VMd3enOfL1S+T569L03siSZQcxPV xSkxOe2qkuJwUIibc+sDlpeauHeapDOLDHDTtD+Ut5aoi184sVmDwDoC5C2TXA9GOu/Z N7QW/JWK9LS3MAhob5/OMJDMGl5tzxxES/0YsFPqNRdxdunRuASLdJhz+crjWGDbKC9K XQKQ== X-Gm-Message-State: APjAAAX8m9olonet/1h9WGucvpYE/lllSFj5gQtHsouh0ZEGKVWkE6hc BrfKKEeWwtlOsh8Rey6VDIY= X-Google-Smtp-Source: APXvYqyHYHJwcWxzkd4NhdCH54CaB7vgm6krCM92IUZUhaDZPWi4HQ6ltB3UP1AlBMlsNjIktZRFTw== X-Received: by 2002:a17:90a:850c:: with SMTP id l12mr6347604pjn.9.1569943763533; Tue, 01 Oct 2019 08:29:23 -0700 (PDT) Received: from localhost.localdomain ([2001:470:b:9c3:9e5c:8eff:fe4f:f2d0]) by smtp.gmail.com with ESMTPSA id o64sm11445923pjb.24.2019.10.01.08.29.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Oct 2019 08:29:22 -0700 (PDT) Subject: [PATCH v11 1/6] mm: Adjust shuffle code to allow for future coalescing From: Alexander Duyck To: virtio-dev@lists.oasis-open.org, kvm@vger.kernel.org, mst@redhat.com, david@redhat.com, dave.hansen@intel.com, linux-kernel@vger.kernel.org, willy@infradead.org, mhocko@kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org, mgorman@techsingularity.net, vbabka@suse.cz, osalvador@suse.de Cc: yang.zhang.wz@gmail.com, pagupta@redhat.com, konrad.wilk@oracle.com, nitesh@redhat.com, riel@surriel.com, lcapitulino@redhat.com, wei.w.wang@intel.com, aarcange@redhat.com, pbonzini@redhat.com, dan.j.williams@intel.com, alexander.h.duyck@linux.intel.com Date: Tue, 01 Oct 2019 08:29:21 -0700 Message-ID: <20191001152921.27008.56300.stgit@localhost.localdomain> In-Reply-To: <20191001152441.27008.99285.stgit@localhost.localdomain> References: <20191001152441.27008.99285.stgit@localhost.localdomain> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Alexander Duyck Move the head/tail adding logic out of the shuffle code and into the __free_one_page function since ultimately that is where it is really needed anyway. By doing this we should be able to reduce the overhead and can consolidate all of the list addition bits in one spot. Acked-by: David Hildenbrand Reviewed-by: Dan Williams Signed-off-by: Alexander Duyck --- include/linux/mmzone.h | 12 -------- mm/page_alloc.c | 71 ++++++++++++++++++++++++++++-------------------- mm/shuffle.c | 12 ++++---- mm/shuffle.h | 6 ++++ 4 files changed, 54 insertions(+), 47 deletions(-) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index bda20282746b..125f300981c6 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -116,18 +116,6 @@ static inline void add_to_free_area_tail(struct page *page, struct free_area *ar area->nr_free++; } -#ifdef CONFIG_SHUFFLE_PAGE_ALLOCATOR -/* Used to preserve page allocation order entropy */ -void add_to_free_area_random(struct page *page, struct free_area *area, - int migratetype); -#else -static inline void add_to_free_area_random(struct page *page, - struct free_area *area, int migratetype) -{ - add_to_free_area(page, area, migratetype); -} -#endif - /* Used for pages which are on another list */ static inline void move_to_free_area(struct page *page, struct free_area *area, int migratetype) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 15c2050c629b..bc8de8d9416d 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -878,6 +878,36 @@ static inline struct capture_control *task_capc(struct zone *zone) #endif /* CONFIG_COMPACTION */ /* + * If this is not the largest possible page, check if the buddy + * of the next-highest order is free. If it is, it's possible + * that pages are being freed that will coalesce soon. In case, + * that is happening, add the free page to the tail of the list + * so it's less likely to be used soon and more likely to be merged + * as a higher order page + */ +static inline bool +buddy_merge_likely(unsigned long pfn, unsigned long buddy_pfn, + struct page *page, unsigned int order) +{ + struct page *higher_page, *higher_buddy; + unsigned long combined_pfn; + + if (order >= MAX_ORDER - 2) + return false; + + if (!pfn_valid_within(buddy_pfn)) + return false; + + combined_pfn = buddy_pfn & pfn; + higher_page = page + (combined_pfn - pfn); + buddy_pfn = __find_buddy_pfn(combined_pfn, order + 1); + higher_buddy = higher_page + (buddy_pfn - combined_pfn); + + return pfn_valid_within(buddy_pfn) && + page_is_buddy(higher_page, higher_buddy, order + 1); +} + +/* * Freeing function for a buddy system allocator. * * The concept of a buddy system is to maintain direct-mapped table @@ -906,11 +936,13 @@ static inline void __free_one_page(struct page *page, struct zone *zone, unsigned int order, int migratetype) { - unsigned long combined_pfn; + struct capture_control *capc = task_capc(zone); unsigned long uninitialized_var(buddy_pfn); - struct page *buddy; + unsigned long combined_pfn; + struct free_area *area; unsigned int max_order; - struct capture_control *capc = task_capc(zone); + struct page *buddy; + bool to_tail; max_order = min_t(unsigned int, MAX_ORDER, pageblock_order + 1); @@ -979,35 +1011,16 @@ static inline void __free_one_page(struct page *page, done_merging: set_page_order(page, order); - /* - * If this is not the largest possible page, check if the buddy - * of the next-highest order is free. If it is, it's possible - * that pages are being freed that will coalesce soon. In case, - * that is happening, add the free page to the tail of the list - * so it's less likely to be used soon and more likely to be merged - * as a higher order page - */ - if ((order < MAX_ORDER-2) && pfn_valid_within(buddy_pfn) - && !is_shuffle_order(order)) { - struct page *higher_page, *higher_buddy; - combined_pfn = buddy_pfn & pfn; - higher_page = page + (combined_pfn - pfn); - buddy_pfn = __find_buddy_pfn(combined_pfn, order + 1); - higher_buddy = higher_page + (buddy_pfn - combined_pfn); - if (pfn_valid_within(buddy_pfn) && - page_is_buddy(higher_page, higher_buddy, order + 1)) { - add_to_free_area_tail(page, &zone->free_area[order], - migratetype); - return; - } - } - + area = &zone->free_area[order]; if (is_shuffle_order(order)) - add_to_free_area_random(page, &zone->free_area[order], - migratetype); + to_tail = shuffle_pick_tail(); else - add_to_free_area(page, &zone->free_area[order], migratetype); + to_tail = buddy_merge_likely(pfn, buddy_pfn, page, order); + if (to_tail) + add_to_free_area_tail(page, area, migratetype); + else + add_to_free_area(page, area, migratetype); } /* diff --git a/mm/shuffle.c b/mm/shuffle.c index b3fe97fd6654..e65d57f39486 100644 --- a/mm/shuffle.c +++ b/mm/shuffle.c @@ -183,11 +183,11 @@ void __meminit __shuffle_free_memory(pg_data_t *pgdat) shuffle_zone(z); } -void add_to_free_area_random(struct page *page, struct free_area *area, - int migratetype) +bool shuffle_pick_tail(void) { static u64 rand; static u8 rand_bits; + bool ret; /* * The lack of locking is deliberate. If 2 threads race to @@ -198,10 +198,10 @@ void add_to_free_area_random(struct page *page, struct free_area *area, rand = get_random_u64(); } - if (rand & 1) - add_to_free_area(page, area, migratetype); - else - add_to_free_area_tail(page, area, migratetype); + ret = rand & 1; + rand_bits--; rand >>= 1; + + return ret; } diff --git a/mm/shuffle.h b/mm/shuffle.h index 777a257a0d2f..4d79f03b6658 100644 --- a/mm/shuffle.h +++ b/mm/shuffle.h @@ -22,6 +22,7 @@ enum mm_shuffle_ctl { DECLARE_STATIC_KEY_FALSE(page_alloc_shuffle_key); extern void page_alloc_shuffle(enum mm_shuffle_ctl ctl); extern void __shuffle_free_memory(pg_data_t *pgdat); +extern bool shuffle_pick_tail(void); static inline void shuffle_free_memory(pg_data_t *pgdat) { if (!static_branch_unlikely(&page_alloc_shuffle_key)) @@ -44,6 +45,11 @@ static inline bool is_shuffle_order(int order) return order >= SHUFFLE_ORDER; } #else +static inline bool shuffle_pick_tail(void) +{ + return false; +} + static inline void shuffle_free_memory(pg_data_t *pgdat) { } From patchwork Tue Oct 1 15:29:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Duyck X-Patchwork-Id: 11169057 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D50941709 for ; Tue, 1 Oct 2019 15:29:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A080A2133F for ; Tue, 1 Oct 2019 15:29:35 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="I96R73TB" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389617AbfJAP3c (ORCPT ); Tue, 1 Oct 2019 11:29:32 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:35377 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389599AbfJAP3c (ORCPT ); Tue, 1 Oct 2019 11:29:32 -0400 Received: by mail-pg1-f196.google.com with SMTP id a24so9916947pgj.2; Tue, 01 Oct 2019 08:29:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:from:to:cc:date:message-id:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=0K2FpVpCh6PKnNggfKA86tN6UUW5hrFsV1d9U5M4+WY=; b=I96R73TBFPYE44iKujpqzQWQK3J8yF4xWvXJLUZpYgjqZvdTkYZpFe7vlHJ4Fb7KI6 UuJK9c5wNUsBgx0I7WAQ3rgO0/SYkDjNjGftUOgU+zaW2J5MoetrZBgK+PWZTIBEt+wb XiaLuvVJSuHzlQFiqch4KFwvyEqunOOx0DgZ0tnzXtVjP7PS4bJBeISmaP3ilz4tSOgy iheasFrAUAnNQYx8TwPfST2krQ2e9g2C6u9oTdKptAPFJzkhs57QsKKQ7Dt1TlrAR3fb NDWVMDZG0hbf1+68yU177bjcto+r0FdQOXZLMzTGoineWqxyuCwuLFL7+kmHkR1Am6pn QpAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=0K2FpVpCh6PKnNggfKA86tN6UUW5hrFsV1d9U5M4+WY=; b=mlC0NM0buQhWI6WWE9XtmuLmY0RJJ/WXpqyqZ84MR1AAy64WszqUjixCx+xZ/vKrRg DLII1VhrEvakeCOUQwmLx+wy2XXoAN/cPz/tDyvzMFECrqQb1CyY0Xyb7spkla0VLJ5e 7156ct5A53ximv/wNdAM7gcj3pWBgcGBpYKcIRgmJfFYMwOOAzdGVfkgA8xh2p0xiicW LPVGA0iC2419WelarZYHG95Ic9G/K6p6KmJyzednzIXCfd8gbZRwkrIhk5AIaUy8mfks fVyrwmji7tLP3mOSkskXgT0nx0CkeJvJqAvfraJBLpVwFWPQLx7pL2l933OU+8V68WjO nIhQ== X-Gm-Message-State: APjAAAUwblJl+tA3vugaKC3mXK2pPqp7InQMo5u5FwhRKWOFVYMu7ZSY 2hE+7A57p5oZMSMu1ZRm8M4= X-Google-Smtp-Source: APXvYqzTm3Qna10SfQt1BUH9kl+jhh/w0RXiIoM5A7J0PLLmuOHpDv/3dxL7BXzT72+mpBUZS6KrjQ== X-Received: by 2002:a62:4e09:: with SMTP id c9mr29034017pfb.152.1569943769921; Tue, 01 Oct 2019 08:29:29 -0700 (PDT) Received: from localhost.localdomain ([2001:470:b:9c3:9e5c:8eff:fe4f:f2d0]) by smtp.gmail.com with ESMTPSA id z25sm15220217pfn.7.2019.10.01.08.29.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Oct 2019 08:29:29 -0700 (PDT) Subject: [PATCH v11 2/6] mm: Use zone and order instead of free area in free_list manipulators From: Alexander Duyck To: virtio-dev@lists.oasis-open.org, kvm@vger.kernel.org, mst@redhat.com, david@redhat.com, dave.hansen@intel.com, linux-kernel@vger.kernel.org, willy@infradead.org, mhocko@kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org, mgorman@techsingularity.net, vbabka@suse.cz, osalvador@suse.de Cc: yang.zhang.wz@gmail.com, pagupta@redhat.com, konrad.wilk@oracle.com, nitesh@redhat.com, riel@surriel.com, lcapitulino@redhat.com, wei.w.wang@intel.com, aarcange@redhat.com, pbonzini@redhat.com, dan.j.williams@intel.com, alexander.h.duyck@linux.intel.com Date: Tue, 01 Oct 2019 08:29:28 -0700 Message-ID: <20191001152928.27008.8178.stgit@localhost.localdomain> In-Reply-To: <20191001152441.27008.99285.stgit@localhost.localdomain> References: <20191001152441.27008.99285.stgit@localhost.localdomain> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Alexander Duyck In order to enable the use of the zone from the list manipulator functions I will need access to the zone pointer. As it turns out most of the accessors were always just being directly passed &zone->free_area[order] anyway so it would make sense to just fold that into the function itself and pass the zone and order as arguments instead of the free area. In order to be able to reference the zone we need to move the declaration of the functions down so that we have the zone defined before we define the list manipulation functions. Since the functions are only used in the file mm/page_alloc.c we can just move them there to reduce noise in the header. Reviewed-by: Dan Williams Reviewed-by: David Hildenbrand Reviewed-by: Pankaj Gupta Signed-off-by: Alexander Duyck --- include/linux/mmzone.h | 32 ----------------------- mm/page_alloc.c | 68 +++++++++++++++++++++++++++++++++++------------- 2 files changed, 49 insertions(+), 51 deletions(-) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 125f300981c6..270a7b493174 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -100,29 +100,6 @@ struct free_area { unsigned long nr_free; }; -/* Used for pages not on another list */ -static inline void add_to_free_area(struct page *page, struct free_area *area, - int migratetype) -{ - list_add(&page->lru, &area->free_list[migratetype]); - area->nr_free++; -} - -/* Used for pages not on another list */ -static inline void add_to_free_area_tail(struct page *page, struct free_area *area, - int migratetype) -{ - list_add_tail(&page->lru, &area->free_list[migratetype]); - area->nr_free++; -} - -/* Used for pages which are on another list */ -static inline void move_to_free_area(struct page *page, struct free_area *area, - int migratetype) -{ - list_move(&page->lru, &area->free_list[migratetype]); -} - static inline struct page *get_page_from_free_area(struct free_area *area, int migratetype) { @@ -130,15 +107,6 @@ static inline struct page *get_page_from_free_area(struct free_area *area, struct page, lru); } -static inline void del_page_from_free_area(struct page *page, - struct free_area *area) -{ - list_del(&page->lru); - __ClearPageBuddy(page); - set_page_private(page, 0); - area->nr_free--; -} - static inline bool free_area_empty(struct free_area *area, int migratetype) { return list_empty(&area->free_list[migratetype]); diff --git a/mm/page_alloc.c b/mm/page_alloc.c index bc8de8d9416d..5e142047f730 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -877,6 +877,44 @@ static inline struct capture_control *task_capc(struct zone *zone) } #endif /* CONFIG_COMPACTION */ +/* Used for pages not on another list */ +static inline void add_to_free_list(struct page *page, struct zone *zone, + unsigned int order, int migratetype) +{ + struct free_area *area = &zone->free_area[order]; + + list_add(&page->lru, &area->free_list[migratetype]); + area->nr_free++; +} + +/* Used for pages not on another list */ +static inline void add_to_free_list_tail(struct page *page, struct zone *zone, + unsigned int order, int migratetype) +{ + struct free_area *area = &zone->free_area[order]; + + list_add_tail(&page->lru, &area->free_list[migratetype]); + area->nr_free++; +} + +/* Used for pages which are on another list */ +static inline void move_to_free_list(struct page *page, struct zone *zone, + unsigned int order, int migratetype) +{ + struct free_area *area = &zone->free_area[order]; + + list_move(&page->lru, &area->free_list[migratetype]); +} + +static inline void del_page_from_free_list(struct page *page, struct zone *zone, + unsigned int order) +{ + list_del(&page->lru); + __ClearPageBuddy(page); + set_page_private(page, 0); + zone->free_area[order].nr_free--; +} + /* * If this is not the largest possible page, check if the buddy * of the next-highest order is free. If it is, it's possible @@ -939,7 +977,6 @@ static inline void __free_one_page(struct page *page, struct capture_control *capc = task_capc(zone); unsigned long uninitialized_var(buddy_pfn); unsigned long combined_pfn; - struct free_area *area; unsigned int max_order; struct page *buddy; bool to_tail; @@ -977,7 +1014,7 @@ static inline void __free_one_page(struct page *page, if (page_is_guard(buddy)) clear_page_guard(zone, buddy, order, migratetype); else - del_page_from_free_area(buddy, &zone->free_area[order]); + del_page_from_free_list(buddy, zone, order); combined_pfn = buddy_pfn & pfn; page = page + (combined_pfn - pfn); pfn = combined_pfn; @@ -1011,16 +1048,15 @@ static inline void __free_one_page(struct page *page, done_merging: set_page_order(page, order); - area = &zone->free_area[order]; if (is_shuffle_order(order)) to_tail = shuffle_pick_tail(); else to_tail = buddy_merge_likely(pfn, buddy_pfn, page, order); if (to_tail) - add_to_free_area_tail(page, area, migratetype); + add_to_free_list_tail(page, zone, order, migratetype); else - add_to_free_area(page, area, migratetype); + add_to_free_list(page, zone, order, migratetype); } /* @@ -2024,13 +2060,11 @@ void __init init_cma_reserved_pageblock(struct page *page) * -- nyc */ static inline void expand(struct zone *zone, struct page *page, - int low, int high, struct free_area *area, - int migratetype) + int low, int high, int migratetype) { unsigned long size = 1 << high; while (high > low) { - area--; high--; size >>= 1; VM_BUG_ON_PAGE(bad_range(zone, &page[size]), &page[size]); @@ -2044,7 +2078,7 @@ static inline void expand(struct zone *zone, struct page *page, if (set_page_guard(zone, &page[size], high, migratetype)) continue; - add_to_free_area(&page[size], area, migratetype); + add_to_free_list(&page[size], zone, high, migratetype); set_page_order(&page[size], high); } } @@ -2202,8 +2236,8 @@ struct page *__rmqueue_smallest(struct zone *zone, unsigned int order, page = get_page_from_free_area(area, migratetype); if (!page) continue; - del_page_from_free_area(page, area); - expand(zone, page, order, current_order, area, migratetype); + del_page_from_free_list(page, zone, current_order); + expand(zone, page, order, current_order, migratetype); set_pcppage_migratetype(page, migratetype); return page; } @@ -2211,7 +2245,6 @@ struct page *__rmqueue_smallest(struct zone *zone, unsigned int order, return NULL; } - /* * This array describes the order lists are fallen back to when * the free lists for the desirable migrate type are depleted @@ -2277,7 +2310,7 @@ static int move_freepages(struct zone *zone, VM_BUG_ON_PAGE(page_zone(page) != zone, page); order = page_order(page); - move_to_free_area(page, &zone->free_area[order], migratetype); + move_to_free_list(page, zone, order, migratetype); page += 1 << order; pages_moved += 1 << order; } @@ -2393,7 +2426,6 @@ static void steal_suitable_fallback(struct zone *zone, struct page *page, unsigned int alloc_flags, int start_type, bool whole_block) { unsigned int current_order = page_order(page); - struct free_area *area; int free_pages, movable_pages, alike_pages; int old_block_type; @@ -2464,8 +2496,7 @@ static void steal_suitable_fallback(struct zone *zone, struct page *page, return; single_page: - area = &zone->free_area[current_order]; - move_to_free_area(page, area, start_type); + move_to_free_list(page, zone, current_order, start_type); } /* @@ -3136,7 +3167,6 @@ void split_page(struct page *page, unsigned int order) int __isolate_free_page(struct page *page, unsigned int order) { - struct free_area *area = &page_zone(page)->free_area[order]; unsigned long watermark; struct zone *zone; int mt; @@ -3162,7 +3192,7 @@ int __isolate_free_page(struct page *page, unsigned int order) /* Remove page from free list */ - del_page_from_free_area(page, area); + del_page_from_free_list(page, zone, order); /* * Set the pageblock if the isolated page is at least half of a @@ -8605,7 +8635,7 @@ void zone_pcp_reset(struct zone *zone) pr_info("remove from free list %lx %d %lx\n", pfn, 1 << order, end_pfn); #endif - del_page_from_free_area(page, &zone->free_area[order]); + del_page_from_free_list(page, zone, order); for (i = 0; i < (1 << order); i++) SetPageReserved((page+i)); pfn += (1 << order); From patchwork Tue Oct 1 15:29:35 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Duyck X-Patchwork-Id: 11169061 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7E8961709 for ; Tue, 1 Oct 2019 15:29:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 3F0A32133F for ; Tue, 1 Oct 2019 15:29:42 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Yi/BDv/R" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389633AbfJAP3h (ORCPT ); Tue, 1 Oct 2019 11:29:37 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:34799 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389508AbfJAP3h (ORCPT ); Tue, 1 Oct 2019 11:29:37 -0400 Received: by mail-pg1-f196.google.com with SMTP id y35so9915336pgl.1; Tue, 01 Oct 2019 08:29:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:from:to:cc:date:message-id:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=e8YeMjhQY+ijjBNTlIg7BQh1fK0Fplybk8YAB8OnQhk=; b=Yi/BDv/Riwq60MtS+nhYnUAZbEBYBpv4V/gv2Sg59zkBgac3o41ROVKByPDLT4cr/k aFqafrNev5S7BVhjY3wBcL1wnF8fg5y/6+p1Bm3BwNd76igQVMCFXcwNk+FNkk4xCwG0 OD7auHpQZzB6nSIVImk2t6G6iS0Ynf2f1MVsVLrXmAXtN5YkW7BfZCH+CVjRjWlogCch mmkYpBm4rFD6DX5lwqrYczVFap2NMgbmTTz8Ipb172XzrpU2jWohUZbyeMvln5V1Wu9s 05TVM2bwbO5iRE2R6d8pd3lliieVxHQJeYQi5nef8BwgyxCVH4T7NHwSC8h/6WypybA8 ltfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=e8YeMjhQY+ijjBNTlIg7BQh1fK0Fplybk8YAB8OnQhk=; b=KOs4TyIwCNF6Kg18qzPnWaBLKIJnCxq1Uq1TyLzV4wn3vAzZSjKkrDFkL6r690s2Tg uUTg/Rh8bBSImGSxiY1jZMFZbtgdgFbmQqANYo+zrXnZlV3MvyrQEc4MQZDvFzUO+E3o XflCc1B9B4Few+vkJz7mWDzqbJnWPs3dBMvudZ57KbjzGQv8cvvtBYvZx28Yv+6qxH3G caN8hYeHR8LjcvPRkztsOTY0SLpXMmDQVC5/ZnqG7EY9jFsI73omqDDspIbxpxgkkiaY k/Hm8rsChu+sCKQslID2rv2cMc64GGQKY3xGmXPsgxQmNuX/xY0uLycIcO/cVhZ+0LVk mTMA== X-Gm-Message-State: APjAAAWVky7ELtDoMJBmeFGX7+3vg5U9ScSz7nTIMQHpA1bDRyT4kRhV TqcY8OwfWw8+bpyrJ1qK2Kc= X-Google-Smtp-Source: APXvYqxKk7zenPylPml2NcM0zdxPBidkQo9Pk9IxW7lap16R+taI0B25jkheM3hK2MbvxlXXF0NruQ== X-Received: by 2002:a17:90a:170e:: with SMTP id z14mr6197617pjd.119.1569943776312; Tue, 01 Oct 2019 08:29:36 -0700 (PDT) Received: from localhost.localdomain ([2001:470:b:9c3:9e5c:8eff:fe4f:f2d0]) by smtp.gmail.com with ESMTPSA id i37sm3032668pje.23.2019.10.01.08.29.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Oct 2019 08:29:35 -0700 (PDT) Subject: [PATCH v11 3/6] mm: Introduce Reported pages From: Alexander Duyck To: virtio-dev@lists.oasis-open.org, kvm@vger.kernel.org, mst@redhat.com, david@redhat.com, dave.hansen@intel.com, linux-kernel@vger.kernel.org, willy@infradead.org, mhocko@kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org, mgorman@techsingularity.net, vbabka@suse.cz, osalvador@suse.de Cc: yang.zhang.wz@gmail.com, pagupta@redhat.com, konrad.wilk@oracle.com, nitesh@redhat.com, riel@surriel.com, lcapitulino@redhat.com, wei.w.wang@intel.com, aarcange@redhat.com, pbonzini@redhat.com, dan.j.williams@intel.com, alexander.h.duyck@linux.intel.com Date: Tue, 01 Oct 2019 08:29:35 -0700 Message-ID: <20191001152934.27008.14328.stgit@localhost.localdomain> In-Reply-To: <20191001152441.27008.99285.stgit@localhost.localdomain> References: <20191001152441.27008.99285.stgit@localhost.localdomain> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Alexander Duyck In order to pave the way for free page reporting in virtualized environments we will need a way to get pages out of the free lists and identify those pages after they have been returned. To accomplish this, this patch adds the concept of a Reported Buddy, which is essentially meant to just be the Uptodate flag used in conjunction with the Buddy page type. It adds a set of pointers we shall call "reported_boundary" which represent the upper boundary between the unreported and reported pages. The general idea is that in order for a page to cross from one side of the boundary to the other it will need to verify that it went through the reporting process. Ultimately a free list has been fully processed when the boundary has been moved from the tail all they way up to occupying the first entry in the list. Without this we would have to manually walk the entire page list until we have find a page that hasn't been reported. In my testing this adds as much as 18% additional overhead which would make this unattractive as a solution. One limitation to this approach is that it is essentially a linear search and in the case of the free lists we can have pages added to either the head or the tail of the list. In order to place limits on this we only allow pages to be added before the reported_boundary instead of adding to the tail itself. An added advantage to this approach is that we should be reducing the overall memory footprint of the guest as it will be more likely to recycle warm pages versus trying to allocate the reported pages that were likely evicted from the guest memory. Since we will only be reporting one zone at a time we keep the boundary limited to being defined for just the zone we are currently reporting pages from. Doing this we can keep the number of additional pointers needed quite small. To flag that the boundaries are in place we use a single bit in the zone to indicate that reporting and the boundaries are active. We store the index of the boundary pointer used to track the reported page in the page->index value. Doing this we can avoid unnecessary computation to determine the index value again. There should be no issues with this as the value is unused when the page is in the buddy allocator, and is reset as soon as the page is removed from the free list. Signed-off-by: Alexander Duyck --- include/linux/mmzone.h | 16 ++++ include/linux/page-flags.h | 11 +++ mm/Kconfig | 11 +++ mm/compaction.c | 5 + mm/memory_hotplug.c | 2 + mm/page_alloc.c | 67 +++++++++++++++-- mm/page_reporting.h | 176 ++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 281 insertions(+), 7 deletions(-) create mode 100644 mm/page_reporting.h diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 270a7b493174..53922c30b8d8 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -463,6 +463,14 @@ struct zone { seqlock_t span_seqlock; #endif +#ifdef CONFIG_PAGE_REPORTING + /* + * Pointer to reported page tracking statistics array. The size of + * the array is MAX_ORDER - PAGE_REPORTING_MIN_ORDER. NULL when + * unused page reporting is not present. + */ + unsigned long *reported_pages; +#endif int initialized; /* Write-intensive fields used from the page allocator */ @@ -538,6 +546,14 @@ enum zone_flags { ZONE_BOOSTED_WATERMARK, /* zone recently boosted watermarks. * Cleared when kswapd is woken. */ + ZONE_PAGE_REPORTING_ACTIVE, /* zone enabled page reporting and is + * activly flushing the data out of + * higher order pages. + */ + ZONE_PAGE_REPORTING_REQUESTED, /* zone enabled page reporting and has + * requested flushing the data out of + * higher order pages. + */ }; static inline unsigned long zone_managed_pages(struct zone *zone) diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index f91cb8898ff0..759a3b3956f2 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -163,6 +163,9 @@ enum pageflags { /* non-lru isolated movable page */ PG_isolated = PG_reclaim, + + /* Buddy pages. Used to track which pages have been reported */ + PG_reported = PG_uptodate, }; #ifndef __GENERATING_BOUNDS_H @@ -432,6 +435,14 @@ static inline bool set_hwpoison_free_buddy_page(struct page *page) #endif /* + * PageReported() is used to track reported free pages within the Buddy + * allocator. We can use the non-atomic version of the test and set + * operations as both should be shielded with the zone lock to prevent + * any possible races on the setting or clearing of the bit. + */ +__PAGEFLAG(Reported, reported, PF_NO_COMPOUND) + +/* * On an anonymous page mapped into a user virtual memory area, * page->mapping points to its anon_vma, not to a struct address_space; * with the PAGE_MAPPING_ANON bit set to distinguish it. See rmap.h. diff --git a/mm/Kconfig b/mm/Kconfig index a5dae9a7eb51..0419b2a9be3e 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -237,6 +237,17 @@ config COMPACTION linux-mm@kvack.org. # +# support for unused page reporting +config PAGE_REPORTING + bool "Allow for reporting of unused pages" + def_bool n + help + Unused page reporting allows for the incremental acquisition of + unused pages from the buddy allocator for the purpose of reporting + those pages to another entity, such as a hypervisor, so that the + memory can be freed up for other uses. + +# # support for page migration # config MIGRATION diff --git a/mm/compaction.c b/mm/compaction.c index ce08b39d85d4..60e064330b3a 100644 --- a/mm/compaction.c +++ b/mm/compaction.c @@ -24,6 +24,7 @@ #include #include #include "internal.h" +#include "page_reporting.h" #ifdef CONFIG_COMPACTION static inline void count_compact_event(enum vm_event_item item) @@ -1325,6 +1326,8 @@ static int next_search_order(struct compact_control *cc, int order) continue; spin_lock_irqsave(&cc->zone->lock, flags); + page_reporting_free_area_release(cc->zone, order, + MIGRATE_MOVABLE); freelist = &area->free_list[MIGRATE_MOVABLE]; list_for_each_entry_reverse(freepage, freelist, lru) { unsigned long pfn; @@ -1681,6 +1684,8 @@ static unsigned long fast_find_migrateblock(struct compact_control *cc) continue; spin_lock_irqsave(&cc->zone->lock, flags); + page_reporting_free_area_release(cc->zone, order, + MIGRATE_MOVABLE); freelist = &area->free_list[MIGRATE_MOVABLE]; list_for_each_entry(freepage, freelist, lru) { unsigned long free_pfn; diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index 680b4b3e57d9..be9634819218 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -41,6 +41,7 @@ #include "internal.h" #include "shuffle.h" +#include "page_reporting.h" /* * online_page_callback contains pointer to current page onlining function. @@ -1624,6 +1625,7 @@ static int __ref __offline_pages(unsigned long start_pfn, if (!populated_zone(zone)) { zone_pcp_reset(zone); build_all_zonelists(NULL); + page_reporting_reset_zone(zone); } else zone_pcp_update(zone); diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 5e142047f730..c82c00ea1f5c 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -74,6 +74,7 @@ #include #include "internal.h" #include "shuffle.h" +#include "page_reporting.h" /* prevent >1 _updater_ of zone percpu pageset ->high and ->batch fields */ static DEFINE_MUTEX(pcp_batch_high_lock); @@ -891,10 +892,15 @@ static inline void add_to_free_list(struct page *page, struct zone *zone, static inline void add_to_free_list_tail(struct page *page, struct zone *zone, unsigned int order, int migratetype) { - struct free_area *area = &zone->free_area[order]; + struct list_head *tail = get_unreported_tail(zone, order, migratetype); - list_add_tail(&page->lru, &area->free_list[migratetype]); - area->nr_free++; + /* + * To prevent the unreported pages from slipping behind our iterator + * we will force them to be inserted in front of it. By doing this + * we should only need to make one pass through the freelist. + */ + list_add_tail(&page->lru, tail); + zone->free_area[order].nr_free++; } /* Used for pages which are on another list */ @@ -903,12 +909,20 @@ static inline void move_to_free_list(struct page *page, struct zone *zone, { struct free_area *area = &zone->free_area[order]; + /* Make certain the page isn't occupying the boundary */ + if (page_is_reported(page)) + __del_page_from_reported_list(page, zone); + list_move(&page->lru, &area->free_list[migratetype]); } static inline void del_page_from_free_list(struct page *page, struct zone *zone, unsigned int order) { + /* remove page from reported list, and clear reported state */ + if (page_is_reported(page)) + del_page_from_reported_list(page, zone, order); + list_del(&page->lru); __ClearPageBuddy(page); set_page_private(page, 0); @@ -972,7 +986,7 @@ static inline void del_page_from_free_list(struct page *page, struct zone *zone, static inline void __free_one_page(struct page *page, unsigned long pfn, struct zone *zone, unsigned int order, - int migratetype) + int migratetype, bool reported) { struct capture_control *capc = task_capc(zone); unsigned long uninitialized_var(buddy_pfn); @@ -1048,7 +1062,9 @@ static inline void __free_one_page(struct page *page, done_merging: set_page_order(page, order); - if (is_shuffle_order(order)) + if (reported) + to_tail = true; + else if (is_shuffle_order(order)) to_tail = shuffle_pick_tail(); else to_tail = buddy_merge_likely(pfn, buddy_pfn, page, order); @@ -1367,7 +1383,7 @@ static void free_pcppages_bulk(struct zone *zone, int count, if (unlikely(isolated_pageblocks)) mt = get_pageblock_migratetype(page); - __free_one_page(page, page_to_pfn(page), zone, 0, mt); + __free_one_page(page, page_to_pfn(page), zone, 0, mt, false); trace_mm_page_pcpu_drain(page, 0, mt); } spin_unlock(&zone->lock); @@ -1383,7 +1399,7 @@ static void free_one_page(struct zone *zone, is_migrate_isolate(migratetype))) { migratetype = get_pfnblock_migratetype(page, pfn); } - __free_one_page(page, pfn, zone, order, migratetype); + __free_one_page(page, pfn, zone, order, migratetype, false); spin_unlock(&zone->lock); } @@ -2245,6 +2261,43 @@ struct page *__rmqueue_smallest(struct zone *zone, unsigned int order, return NULL; } +#ifdef CONFIG_PAGE_REPORTING +struct list_head **reported_boundary __read_mostly; + +/** + * free_reported_page - Return a now-reported page back where we got it + * @page: Page that was reported + * @order: Order of the reported page + * + * This function will pull the migratetype and order information out + * of the page and attempt to return it where it found it. If the page + * is added to the free list without changes we will mark it as being + * reported. + */ +void free_reported_page(struct page *page, unsigned int order) +{ + struct zone *zone = page_zone(page); + unsigned long pfn; + unsigned int mt; + + /* zone lock should be held when this function is called */ + lockdep_assert_held(&zone->lock); + + pfn = page_to_pfn(page); + mt = get_pfnblock_migratetype(page, pfn); + __free_one_page(page, pfn, zone, order, mt, true); + + /* + * If page was not comingled with another page we can consider + * the result to be "reported" since part of the page hasn't been + * modified, otherwise we would need to report on the new larger + * page. + */ + if (PageBuddy(page) && page_order(page) == order) + add_page_to_reported_list(page, zone, order, mt); +} +#endif /* CONFIG_PAGE_REPORTING */ + /* * This array describes the order lists are fallen back to when * the free lists for the desirable migrate type are depleted diff --git a/mm/page_reporting.h b/mm/page_reporting.h new file mode 100644 index 000000000000..ee4d86daa089 --- /dev/null +++ b/mm/page_reporting.h @@ -0,0 +1,176 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _MM_PAGE_REPORTING_H +#define _MM_PAGE_REPORTING_H + +#include +#include +#include +#include +#include +#include + +#define PAGE_REPORTING_MIN_ORDER pageblock_order + +#ifdef CONFIG_PAGE_REPORTING +/* Reported page accessors, defined in page_alloc.c */ +void free_reported_page(struct page *page, unsigned int order); + +#define page_is_reported(_page) unlikely(PageReported(_page)) + +/* Free reported_pages and reset reported page tracking count to 0 */ +static inline void page_reporting_reset_zone(struct zone *zone) +{ + kfree(zone->reported_pages); + zone->reported_pages = NULL; +} + +/* Boundary functions */ +static inline pgoff_t +get_reporting_index(unsigned int order, unsigned int migratetype) +{ + /* + * We will only ever be dealing with pages greater-than or equal to + * PAGE_REPORTING_MIN_ORDER. Since that is the case we can avoid + * allocating unused space by limiting our index range to only the + * orders that are supported for page reporting. + */ + return (order - PAGE_REPORTING_MIN_ORDER) * MIGRATE_TYPES + migratetype; +} + +extern struct list_head **reported_boundary __read_mostly; + +static inline void +page_reporting_reset_boundary(struct zone *zone, unsigned int order, int mt) +{ + int index; + + if (order < PAGE_REPORTING_MIN_ORDER) + return; + if (!test_bit(ZONE_PAGE_REPORTING_ACTIVE, &zone->flags)) + return; + + index = get_reporting_index(order, mt); + reported_boundary[index] = &zone->free_area[order].free_list[mt]; +} + +static inline void page_reporting_disable_boundaries(struct zone *zone) +{ + /* zone lock should be held when this function is called */ + lockdep_assert_held(&zone->lock); + + __clear_bit(ZONE_PAGE_REPORTING_ACTIVE, &zone->flags); +} + +static inline void +page_reporting_free_area_release(struct zone *zone, unsigned int order, int mt) +{ + page_reporting_reset_boundary(zone, order, mt); +} + +/* + * Method for obtaining the tail of the free list. Using this allows for + * tail insertions of unreported pages into the region that is currently + * being scanned so as to avoid interleaving reported and unreported pages. + */ +static inline struct list_head * +get_unreported_tail(struct zone *zone, unsigned int order, int migratetype) +{ + if (order >= PAGE_REPORTING_MIN_ORDER && + test_bit(ZONE_PAGE_REPORTING_ACTIVE, &zone->flags)) + return reported_boundary[get_reporting_index(order, + migratetype)]; + + return &zone->free_area[order].free_list[migratetype]; +} + +/* + * Functions for adding/removing reported pages to the freelist. + * All of them expect the zone lock to be held to maintain + * consistency of the reported list as a subset of the free list. + */ +static inline void +add_page_to_reported_list(struct page *page, struct zone *zone, + unsigned int order, unsigned int mt) +{ + /* + * Default to using index 0, this will be updated later if the zone + * is still being processed. + */ + page->index = 0; + + /* flag page as reported */ + __SetPageReported(page); + + /* update areated page accounting */ + zone->reported_pages[order - PAGE_REPORTING_MIN_ORDER]++; +} + +static inline void page_reporting_pull_boundary(struct page *page) +{ + struct list_head **tail = &reported_boundary[page->index]; + + if (*tail == &page->lru) + *tail = page->lru.next; +} + +static inline void +__del_page_from_reported_list(struct page *page, struct zone *zone) +{ + /* + * Since the page is being pulled from the list we need to update + * the boundary, after that we can just update the index so that + * the correct boundary will be checked in the future. + */ + if (test_bit(ZONE_PAGE_REPORTING_ACTIVE, &zone->flags)) + page_reporting_pull_boundary(page); +} + +static inline void +del_page_from_reported_list(struct page *page, struct zone *zone, + unsigned int order) +{ + __del_page_from_reported_list(page, zone); + + /* page_private will contain the page order, so just use it directly */ + zone->reported_pages[order - PAGE_REPORTING_MIN_ORDER]--; + + /* clear the flag so we can report on it when it returns */ + __ClearPageReported(page); +} + +#else /* CONFIG_PAGE_REPORTING */ +#define page_is_reported(_page) false + +static inline void page_reporting_reset_zone(struct zone *zone) +{ +} + +static inline void +page_reporting_free_area_release(struct zone *zone, unsigned int order, int mt) +{ +} + +static inline struct list_head * +get_unreported_tail(struct zone *zone, unsigned int order, int migratetype) +{ + return &zone->free_area[order].free_list[migratetype]; +} + +static inline void +add_page_to_reported_list(struct page *page, struct zone *zone, + int order, int migratetype) +{ +} + +static inline void +__del_page_from_reported_list(struct page *page, struct zone *zone) +{ +} + +static inline void +del_page_from_reported_list(struct page *page, struct zone *zone, + unsigned int order) +{ +} +#endif /* CONFIG_PAGE_REPORTING */ +#endif /*_MM_PAGE_REPORTING_H */ From patchwork Tue Oct 1 15:29:41 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Duyck X-Patchwork-Id: 11169065 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 46D6B15AB for ; Tue, 1 Oct 2019 15:29:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 08DF821855 for ; Tue, 1 Oct 2019 15:29:48 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Zox6zQ+7" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389644AbfJAP3o (ORCPT ); Tue, 1 Oct 2019 11:29:44 -0400 Received: from mail-pf1-f196.google.com ([209.85.210.196]:36006 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727204AbfJAP3o (ORCPT ); Tue, 1 Oct 2019 11:29:44 -0400 Received: by mail-pf1-f196.google.com with SMTP id y22so8256100pfr.3; Tue, 01 Oct 2019 08:29:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:from:to:cc:date:message-id:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=I4N9O38zB69XEfD8MTqaEr/sXfkO/5S1ruLg/UfuQDk=; b=Zox6zQ+7psMCDObTdxSL5ciNIXLwJkDDEdzR64w4b7SM+LPgP6arNBrAO6F+eJfuXU 4TSI5FZtYqxLs9z7x+DuA54tcQSfJZ2AbqyuFlj7BPKGDwdiHFoOJ9RrbFHBaiiv+07m i77+M1xeRzyO/yKwxHI8/j8+i5t/rvB/Pm7gGwVatJ3O8qxlj3G+OTXkz/QtF3mGzFb7 pa1rkFkbUyQb4DSbpo1XvrgjDDqOv7Bltzk+xxKrYiD3lraZB430qHd38l8Z93IXOTYF cdvmKN4utSNNIysErRskQUoyjTb8htkc1l+uqVz778xmZmTR19jwJn+bFxbRC8vfqn5v lO3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=I4N9O38zB69XEfD8MTqaEr/sXfkO/5S1ruLg/UfuQDk=; b=cshTA2+vCj4CrsY1CmbkWfn1bbMs2ljfLiaHlQMjcJqrdSNFvcPpEQYTbEem+Os1Hr WNamZL50Vzs3BpNGKUqWR1ilDRSg3ogXt788kYtKxdXFUjd9+FGsQg+IhF915sJgs+YR valXgI/3NEo0IpnWu8vhUbFOjaLwRZi1qJIghmNUwI6UMU2sv78dgDM7dE4BX3693e4j G0MXtcJmriNFCIGEdzAmwkRM3ePOppjjF+a+9mbS4pM/A5UJ004I9KWTKpaseYsDB3X9 bTfxg+8Ve5HqaOEfu1KaodTUNtw3EV5TOWLE7xyDBPmZpm6cE6Ck6BryEzJE50+CDwj5 gG0g== X-Gm-Message-State: APjAAAXmQP/Hh4kNrPP/G+dV0beLHBKK613TL5fgmCO6X/1bcRiBUnrd jBusioztIfCDkOxFYn0PLhA= X-Google-Smtp-Source: APXvYqzAkQ8RRLDsS8SqLGkq+JKrMjkYeJV6fMZzEIRWqBJ1QFtDOl089tcXBzYnlUza8CVkThNaVA== X-Received: by 2002:a63:b102:: with SMTP id r2mr30191838pgf.370.1569943782872; Tue, 01 Oct 2019 08:29:42 -0700 (PDT) Received: from localhost.localdomain ([2001:470:b:9c3:9e5c:8eff:fe4f:f2d0]) by smtp.gmail.com with ESMTPSA id d76sm20569576pga.80.2019.10.01.08.29.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Oct 2019 08:29:42 -0700 (PDT) Subject: [PATCH v11 4/6] mm: Add device side and notifier for unused page reporting From: Alexander Duyck To: virtio-dev@lists.oasis-open.org, kvm@vger.kernel.org, mst@redhat.com, david@redhat.com, dave.hansen@intel.com, linux-kernel@vger.kernel.org, willy@infradead.org, mhocko@kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org, mgorman@techsingularity.net, vbabka@suse.cz, osalvador@suse.de Cc: yang.zhang.wz@gmail.com, pagupta@redhat.com, konrad.wilk@oracle.com, nitesh@redhat.com, riel@surriel.com, lcapitulino@redhat.com, wei.w.wang@intel.com, aarcange@redhat.com, pbonzini@redhat.com, dan.j.williams@intel.com, alexander.h.duyck@linux.intel.com Date: Tue, 01 Oct 2019 08:29:41 -0700 Message-ID: <20191001152941.27008.32740.stgit@localhost.localdomain> In-Reply-To: <20191001152441.27008.99285.stgit@localhost.localdomain> References: <20191001152441.27008.99285.stgit@localhost.localdomain> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Alexander Duyck With this patch we are adding the pieces needed to enable the reporting of pages to a specific device. That device needs to register a page reporting device that can be used to handle notifications that that pages are unused. Registering the device will in turn enable the notifications and allow page reporting to be active. When the the device is unregistered it will disable page reporting notifications. For now we only allow one page reporting device to be registered at a time. The determination of when to start reporting is based on the tracking of the number of free pages in a given area versus the number of reported pages in that area. We keep track of the number of reported pages per free_area in a separate zone specific area. We do this to avoid modifying the free_area structure as this can lead to false sharing for the highest order with the zone lock which leads to a noticeable performance degradation. Once reporting has started get_unreported_pages will use the reported_boundary pointers to track where it should resume processing the free lists. It will go through and either set the index if it finds a reported page, or it will attempt to isolate the page so that it can be reported. Signed-off-by: Alexander Duyck --- include/linux/page_reporting.h | 31 ++++ mm/Makefile | 1 mm/page_alloc.c | 10 + mm/page_reporting.c | 350 ++++++++++++++++++++++++++++++++++++++++ mm/page_reporting.h | 49 ++++++ 5 files changed, 439 insertions(+), 2 deletions(-) create mode 100644 include/linux/page_reporting.h create mode 100644 mm/page_reporting.c diff --git a/include/linux/page_reporting.h b/include/linux/page_reporting.h new file mode 100644 index 000000000000..155006fc9911 --- /dev/null +++ b/include/linux/page_reporting.h @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_PAGE_REPORTING_H +#define _LINUX_PAGE_REPORTING_H + +#include + +struct page_reporting_dev_info { + /* function that alters pages to make them "reported" */ + void (*report)(struct page_reporting_dev_info *phdev, + unsigned int nents); + + /* scatterlist containing pages to be processed */ + struct scatterlist *sg; + + /* + * Upper limit on the number of pages that the report function + * expects to be placed into the scatterlist to be processed. + */ + unsigned long capacity; + + /* work struct for processing reports */ + struct delayed_work work; + + /* The number of zones requesting reporting */ + atomic_t refcnt; +}; + +/* Tear-down and bring-up for page reporting devices */ +void page_reporting_unregister(struct page_reporting_dev_info *phdev); +int page_reporting_register(struct page_reporting_dev_info *phdev); +#endif /*_LINUX_PAGE_REPORTING_H */ diff --git a/mm/Makefile b/mm/Makefile index d996846697ef..fc4fa17b6c83 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -107,3 +107,4 @@ obj-$(CONFIG_PERCPU_STATS) += percpu-stats.o obj-$(CONFIG_ZONE_DEVICE) += memremap.o obj-$(CONFIG_HMM_MIRROR) += hmm.o obj-$(CONFIG_MEMFD_CREATE) += memfd.o +obj-$(CONFIG_PAGE_REPORTING) += page_reporting.o diff --git a/mm/page_alloc.c b/mm/page_alloc.c index c82c00ea1f5c..c3dc03d3095a 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1073,6 +1073,14 @@ static inline void __free_one_page(struct page *page, add_to_free_list_tail(page, zone, order, migratetype); else add_to_free_list(page, zone, order, migratetype); + + /* + * No need to notify on a reported page as the total count of + * unreported pages will not have increased since we have essentially + * merged the reported page with one or more unreported pages. + */ + if (!reported) + page_reporting_notify_free(zone, order); } /* @@ -2262,8 +2270,6 @@ struct page *__rmqueue_smallest(struct zone *zone, unsigned int order, } #ifdef CONFIG_PAGE_REPORTING -struct list_head **reported_boundary __read_mostly; - /** * free_reported_page - Return a now-reported page back where we got it * @page: Page that was reported diff --git a/mm/page_reporting.c b/mm/page_reporting.c new file mode 100644 index 000000000000..b7c9ed9289c2 --- /dev/null +++ b/mm/page_reporting.c @@ -0,0 +1,350 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include +#include +#include + +#include "page_reporting.h" +#include "internal.h" + +static struct page_reporting_dev_info __rcu *ph_dev_info __read_mostly; +struct list_head **reported_boundary __read_mostly; + +#define for_each_reporting_migratetype_order(_order, _type) \ + for (_order = MAX_ORDER; _order-- != PAGE_REPORTING_MIN_ORDER;) \ + for (_type = MIGRATE_TYPES; _type--;) \ + if (!is_migrate_isolate(_type)) + +static void page_reporting_populate_metadata(struct zone *zone) +{ + size_t size; + int node; + + /* + * We need to make sure we have somewhere to store the tracking + * data for how many reported pages are in the zone. To do that + * we need to make certain zone->reported_pages is populated. + */ + if (zone->reported_pages) + return; + + node = zone_to_nid(zone); + size = (MAX_ORDER - PAGE_REPORTING_MIN_ORDER) * sizeof(unsigned long); + zone->reported_pages = kzalloc_node(size, GFP_KERNEL, node); +} + +static void page_reporting_reset_all_boundaries(struct zone *zone) +{ + unsigned int order, mt; + + /* Update boundary data to reflect the zone we are currently working */ + for_each_reporting_migratetype_order(order, mt) + page_reporting_reset_boundary(zone, order, mt); +} + +static struct page * +get_unreported_page(struct zone *zone, unsigned int order, int mt) +{ + struct list_head *list = &zone->free_area[order].free_list[mt]; + struct list_head *tail = get_unreported_tail(zone, order, mt); + unsigned long index = get_reporting_index(order, mt); + struct page *page; + + /* Find a page of the appropriate size in the preferred list */ + page = list_last_entry(tail, struct page, lru); + list_for_each_entry_from_reverse(page, list, lru) { + /* If we entered this loop then the "raw" list isn't empty */ + + /* + * We are going to skip over the reported pages. Make + * certain that the index of those pages are correct + * as we will later be moving the boundary into place + * above them. + */ + if (PageReported(page)) { + page->index = index; + tail = &page->lru; + continue; + } + + /* Drop reference to page if isolate fails */ + if (__isolate_free_page(page, order)) + goto out; + + break; + } + + page = NULL; +out: + /* Update the boundary */ + reported_boundary[index] = tail; + + return page; +} + +static void +__page_reporting_cancel(struct zone *zone, + struct page_reporting_dev_info *phdev) +{ + /* processing of the zone is complete, we can disable boundaries */ + page_reporting_disable_boundaries(zone); + + /* + * If there are no longer enough free pages to fully populate + * the scatterlist, then we can just shut it down for this zone. + */ + __clear_bit(ZONE_PAGE_REPORTING_REQUESTED, &zone->flags); + atomic_dec(&phdev->refcnt); +} + +static unsigned int +page_reporting_fill(struct zone *zone, struct page_reporting_dev_info *phdev) +{ + struct scatterlist *sg = phdev->sg; + unsigned int order, mt, count = 0; + + sg_init_table(phdev->sg, phdev->capacity); + + /* Make sure the boundaries are enabled */ + if (!__test_and_set_bit(ZONE_PAGE_REPORTING_ACTIVE, &zone->flags)) + page_reporting_reset_all_boundaries(zone); + + for_each_reporting_migratetype_order(order, mt) { + struct page *page; + + /* + * Pull pages from free list until we have drained + * it or we have reached capacity. + */ + while ((page = get_unreported_page(zone, order, mt))) { + sg_set_page(&sg[count], page, PAGE_SIZE << order, 0); + + if (++count == phdev->capacity) + return phdev->capacity; + } + } + + /* mark end of scatterlist due to underflow */ + if (count) + sg_mark_end(&sg[count - 1]); + + /* We ran out of pages so we can stop now */ + __page_reporting_cancel(zone, phdev); + + return count; +} + +static void page_reporting_drain(struct page_reporting_dev_info *phdev) +{ + struct scatterlist *sg = phdev->sg; + + /* + * Drain the now reported pages back into their respective + * free lists/areas. We assume at least one page is populated. + */ + do { + free_reported_page(sg_page(sg), get_order(sg->length)); + } while (!sg_is_last(sg++)); +} + +/* + * The page reporting cycle consists of 4 stages, fill, report, drain, and + * idle. We will cycle through the first 3 stages until we fail to obtain any + * pages, in that case we will switch to idle. + */ +static void +page_reporting_cycle(struct zone *zone, struct page_reporting_dev_info *phdev) +{ + /* + * Guarantee boundaries and stats are populated before we + * start placing reported pages in the zone. + */ + page_reporting_populate_metadata(zone); + + spin_lock_irq(&zone->lock); + + /* Cancel the request if we failed to populate zone metadata */ + if (!zone->reported_pages) { + __page_reporting_cancel(zone, phdev); + goto zone_not_ready; + } + + do { + /* Pull pages out of allocator into a scaterlist */ + unsigned int nents = page_reporting_fill(zone, phdev); + + /* no pages were acquired, give up */ + if (!nents) + break; + + spin_unlock_irq(&zone->lock); + + /* begin processing pages in local list */ + phdev->report(phdev, nents); + + spin_lock_irq(&zone->lock); + + /* + * We should have a scatterlist of pages that have been + * processed. Return them to their original free lists. + */ + page_reporting_drain(phdev); + + /* keep pulling pages till there are none to pull */ + } while (test_bit(ZONE_PAGE_REPORTING_REQUESTED, &zone->flags)); +zone_not_ready: + spin_unlock_irq(&zone->lock); +} + +static void page_reporting_process(struct work_struct *work) +{ + struct delayed_work *d_work = to_delayed_work(work); + struct page_reporting_dev_info *phdev = + container_of(d_work, struct page_reporting_dev_info, work); + struct zone *zone = first_online_pgdat()->node_zones; + + do { + if (test_bit(ZONE_PAGE_REPORTING_REQUESTED, &zone->flags)) + page_reporting_cycle(zone, phdev); + + /* Move to next zone, if at end of list start over */ + zone = next_zone(zone) ? : first_online_pgdat()->node_zones; + + /* + * As long as refcnt has not reached zero there are still + * zones to be processed. + */ + } while (atomic_read(&phdev->refcnt)); +} + +/* request page reporting on this zone */ +void __page_reporting_request(struct zone *zone) +{ + struct page_reporting_dev_info *phdev; + + rcu_read_lock(); + + /* + * We use RCU to protect the ph_dev_info pointer. In almost all + * cases this should be present, however in the unlikely case of + * a shutdown this will be NULL and we should exit. + */ + phdev = rcu_dereference(ph_dev_info); + if (unlikely(!phdev)) + goto out; + + /* + * We can use separate test and set operations here as there + * is nothing else that can set or clear this bit while we are + * holding the zone lock. The advantage to doing it this way is + * that we don't have to dirty the cacheline unless we are + * changing the value. + */ + __set_bit(ZONE_PAGE_REPORTING_REQUESTED, &zone->flags); + + /* + * Delay the start of work to allow a sizable queue to + * build. For now we are limiting this to running no more + * than 10 times per second. + */ + if (!atomic_fetch_inc(&phdev->refcnt)) + schedule_delayed_work(&phdev->work, HZ / 10); +out: + rcu_read_unlock(); +} + +static DEFINE_MUTEX(page_reporting_mutex); +DEFINE_STATIC_KEY_FALSE(page_reporting_notify_enabled); + +void page_reporting_unregister(struct page_reporting_dev_info *phdev) +{ + mutex_lock(&page_reporting_mutex); + + if (rcu_access_pointer(ph_dev_info) == phdev) { + /* Disable page reporting notification */ + static_branch_disable(&page_reporting_notify_enabled); + RCU_INIT_POINTER(ph_dev_info, NULL); + synchronize_rcu(); + + /* Flush any existing work, and lock it out */ + cancel_delayed_work_sync(&phdev->work); + + /* Free scatterlist */ + kfree(phdev->sg); + phdev->sg = NULL; + + /* Free boundaries */ + kfree(reported_boundary); + reported_boundary = NULL; + } + + mutex_unlock(&page_reporting_mutex); +} +EXPORT_SYMBOL_GPL(page_reporting_unregister); + +int page_reporting_register(struct page_reporting_dev_info *phdev) +{ + struct zone *zone; + int err = 0; + + /* No point in enabling this if it cannot handle any pages */ + if (WARN_ON(!phdev->capacity || phdev->capacity > PAGE_REPORTING_HWM)) + return -EINVAL; + + mutex_lock(&page_reporting_mutex); + + /* nothing to do if already in use */ + if (rcu_access_pointer(ph_dev_info)) { + err = -EBUSY; + goto err_out; + } + + /* + * Allocate space to store the boundaries for the zone we are + * actively reporting on. We will need to store one boundary + * pointer per migratetype, and then we need to have one of these + * arrays per order for orders greater than or equal to + * PAGE_REPORTING_MIN_ORDER. + */ + reported_boundary = kcalloc(get_reporting_index(MAX_ORDER, 0), + sizeof(struct list_head *), GFP_KERNEL); + if (!reported_boundary) { + err = -ENOMEM; + goto err_out; + } + + /* allocate scatterlist to store pages being reported on */ + phdev->sg = kcalloc(phdev->capacity, sizeof(*phdev->sg), GFP_KERNEL); + if (!phdev->sg) { + err = -ENOMEM; + + kfree(reported_boundary); + reported_boundary = NULL; + + goto err_out; + } + + + /* initialize refcnt and work structures */ + atomic_set(&phdev->refcnt, 0); + INIT_DELAYED_WORK(&phdev->work, &page_reporting_process); + + /* assign device, and begin initial flush of populated zones */ + rcu_assign_pointer(ph_dev_info, phdev); + for_each_populated_zone(zone) { + spin_lock_irq(&zone->lock); + __page_reporting_request(zone); + spin_unlock_irq(&zone->lock); + } + + /* enable page reporting notification */ + static_branch_enable(&page_reporting_notify_enabled); +err_out: + mutex_unlock(&page_reporting_mutex); + + return err; +} +EXPORT_SYMBOL_GPL(page_reporting_register); diff --git a/mm/page_reporting.h b/mm/page_reporting.h index ee4d86daa089..7aaa1d9a42e8 100644 --- a/mm/page_reporting.h +++ b/mm/page_reporting.h @@ -10,6 +10,7 @@ #include #define PAGE_REPORTING_MIN_ORDER pageblock_order +#define PAGE_REPORTING_HWM 32 #ifdef CONFIG_PAGE_REPORTING /* Reported page accessors, defined in page_alloc.c */ @@ -24,6 +25,50 @@ static inline void page_reporting_reset_zone(struct zone *zone) zone->reported_pages = NULL; } +DECLARE_STATIC_KEY_FALSE(page_reporting_notify_enabled); +void __page_reporting_request(struct zone *zone); + +/** + * page_reporting_notify_free - Free page notification to start page processing + * @zone: Pointer to current zone of last page processed + * @order: Order of last page added to zone + * + * This function is meant to act as a screener for __page_reporting_request + * which will determine if a give zone has crossed over the high-water mark + * that will justify us beginning page treatment. If we have crossed that + * threshold then it will start the process of pulling some pages and + * placing them in the batch list for treatment. + */ +static inline void page_reporting_notify_free(struct zone *zone, int order) +{ + unsigned long next_report = PAGE_REPORTING_HWM; + int report_order; + + /* Called from hot path in __free_one_page() */ + if (!static_branch_unlikely(&page_reporting_notify_enabled)) + return; + + /* Limit notifications only to higher order pages */ + report_order = order - PAGE_REPORTING_MIN_ORDER; + if (report_order < 0) + return; + + /* Do not bother with tests if we have already requested reporting */ + if (test_bit(ZONE_PAGE_REPORTING_REQUESTED, &zone->flags)) + return; + + /* Add reported_pages count if it is present */ + if (zone->reported_pages) + next_report += zone->reported_pages[report_order]; + + /* Determine if we have crossed reporting threshold */ + if (zone->free_area[order].nr_free < next_report) + return; + + /* This is slow, but should be called very rarely */ + __page_reporting_request(zone); +} + /* Boundary functions */ static inline pgoff_t get_reporting_index(unsigned int order, unsigned int migratetype) @@ -145,6 +190,10 @@ static inline void page_reporting_reset_zone(struct zone *zone) { } +static inline void page_reporting_notify_free(struct zone *zone, int order) +{ +} + static inline void page_reporting_free_area_release(struct zone *zone, unsigned int order, int mt) { From patchwork Tue Oct 1 15:29:48 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Duyck X-Patchwork-Id: 11169069 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id F3F701709 for ; Tue, 1 Oct 2019 15:29:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D172721855 for ; Tue, 1 Oct 2019 15:29:54 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Y+MwPM0P" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389658AbfJAP3u (ORCPT ); Tue, 1 Oct 2019 11:29:50 -0400 Received: from mail-pg1-f193.google.com ([209.85.215.193]:46010 "EHLO mail-pg1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727204AbfJAP3u (ORCPT ); Tue, 1 Oct 2019 11:29:50 -0400 Received: by mail-pg1-f193.google.com with SMTP id q7so9868466pgi.12; Tue, 01 Oct 2019 08:29:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:from:to:cc:date:message-id:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=ywjGGrMf/PJ6pRbEzUf6NoarQ8dqbT7Q7Zl+POuvcuU=; b=Y+MwPM0PwyoQs6gNC+lR1Q5jB5E8k4nKxYlTqXp7RB0DriL/eunD9PtWjyD13XHLb5 PSjMcjNm4wTs/EsWyHr508chLopFHQy/Rr5L5sw+D1EHEyhOD05WhTyZPVgFU7LqdZam u6S0n5sGijNBUI27TPTXFGxH8GW7E4Ja7mvxvWAk8lcrel2TJJDk/p6EJrGUcpznoz6e VvMWNVdO2RINsTcVtYHT5uW+lM53GGdYkU0CD9FNd+OFrLsxIBj2vPmmDGpe98NnwZVh Cty1NRgNY1eJSRAZHdmKCezLgdIH74lf77h6TpGH2DJi1KJCQsDPfx0ylqaYeVOL+/1E 8pSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=ywjGGrMf/PJ6pRbEzUf6NoarQ8dqbT7Q7Zl+POuvcuU=; b=N9Z1ngnVRbzmLHfGsisSnry0Ruz5WGpyKi3N3dHVM7idu5HLXiNrTYi5mNL4qrKgkp HoE1OGzK5846qEtVsy3Uk4ZziQBfX4l6wRK3xur/W5CDRA0Jz7F35KNY7TndxloLe7XG 7J50L97LMZ6Db766V6TLhVE/zwWCRsrS2C6kL4Y3qCJWcV0xw0Gr3FQsDU/xxekJdM8Q 7KBkZKVF54GdLQMpN4lHYvCAVoZkim7sZeDqObph/jb5RWcOs92rGjddT7cgBdeQ2LsX SeKa5v3uxd6//zjKkRrA/txu1ePPtK6WduggEwW+dv3wfP5hMZuSVnOaw2HfPwnKzouj FNMw== X-Gm-Message-State: APjAAAUPzgkF5dSGtaPaVOQTf+L3g+DDb/JJzHrDg9LKgoPXnSCqav7w F3pi6SfybfSN/wDnv0t4fd0= X-Google-Smtp-Source: APXvYqwd/kCnyEC2V2itCDr9rRprR6IlLzz9BlpPrfcMUabGpgkedv6UUvLcNR7Y0XMtl684QLFcWw== X-Received: by 2002:a65:6488:: with SMTP id e8mr2541462pgv.192.1569943789337; Tue, 01 Oct 2019 08:29:49 -0700 (PDT) Received: from localhost.localdomain ([2001:470:b:9c3:9e5c:8eff:fe4f:f2d0]) by smtp.gmail.com with ESMTPSA id j22sm5658829pgg.16.2019.10.01.08.29.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Oct 2019 08:29:48 -0700 (PDT) Subject: [PATCH v11 5/6] virtio-balloon: Pull page poisoning config out of free page hinting From: Alexander Duyck To: virtio-dev@lists.oasis-open.org, kvm@vger.kernel.org, mst@redhat.com, david@redhat.com, dave.hansen@intel.com, linux-kernel@vger.kernel.org, willy@infradead.org, mhocko@kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org, mgorman@techsingularity.net, vbabka@suse.cz, osalvador@suse.de Cc: yang.zhang.wz@gmail.com, pagupta@redhat.com, konrad.wilk@oracle.com, nitesh@redhat.com, riel@surriel.com, lcapitulino@redhat.com, wei.w.wang@intel.com, aarcange@redhat.com, pbonzini@redhat.com, dan.j.williams@intel.com, alexander.h.duyck@linux.intel.com Date: Tue, 01 Oct 2019 08:29:48 -0700 Message-ID: <20191001152947.27008.56645.stgit@localhost.localdomain> In-Reply-To: <20191001152441.27008.99285.stgit@localhost.localdomain> References: <20191001152441.27008.99285.stgit@localhost.localdomain> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Alexander Duyck Currently the page poisoning setting wasn't being enabled unless free page hinting was enabled. However we will need the page poisoning tracking logic as well for unused page reporting. As such pull it out and make it a separate bit of config in the probe function. In addition we need to add support for the more recent init_on_free feature which expects a behavior similar to page poisoning in that we expect the page to be pre-zeroed. Reviewed-by: David Hildenbrand Signed-off-by: Alexander Duyck --- drivers/virtio/virtio_balloon.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c index 226fbb995fb0..92099298bc16 100644 --- a/drivers/virtio/virtio_balloon.c +++ b/drivers/virtio/virtio_balloon.c @@ -842,7 +842,6 @@ static int virtio_balloon_register_shrinker(struct virtio_balloon *vb) static int virtballoon_probe(struct virtio_device *vdev) { struct virtio_balloon *vb; - __u32 poison_val; int err; if (!vdev->config->get) { @@ -909,11 +908,20 @@ static int virtballoon_probe(struct virtio_device *vdev) VIRTIO_BALLOON_CMD_ID_STOP); spin_lock_init(&vb->free_page_list_lock); INIT_LIST_HEAD(&vb->free_page_list); - if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON)) { + } + if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON)) { + /* Start with poison val of 0 representing general init */ + __u32 poison_val = 0; + + /* + * Let the hypervisor know that we are expecting a + * specific value to be written back in unused pages. + */ + if (!want_init_on_free()) memset(&poison_val, PAGE_POISON, sizeof(poison_val)); - virtio_cwrite(vb->vdev, struct virtio_balloon_config, - poison_val, &poison_val); - } + + virtio_cwrite(vb->vdev, struct virtio_balloon_config, + poison_val, &poison_val); } /* * We continue to use VIRTIO_BALLOON_F_DEFLATE_ON_OOM to decide if a @@ -1014,7 +1022,10 @@ static int virtballoon_restore(struct virtio_device *vdev) static int virtballoon_validate(struct virtio_device *vdev) { - if (!page_poisoning_enabled()) + /* Tell the host whether we care about poisoned pages. */ + if (!want_init_on_free() && + (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY) || + !page_poisoning_enabled())) __virtio_clear_bit(vdev, VIRTIO_BALLOON_F_PAGE_POISON); __virtio_clear_bit(vdev, VIRTIO_F_IOMMU_PLATFORM); From patchwork Tue Oct 1 15:29:54 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Duyck X-Patchwork-Id: 11169073 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 037D013BD for ; Tue, 1 Oct 2019 15:30:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C13DC21855 for ; Tue, 1 Oct 2019 15:30:02 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EJWJX4Er" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389678AbfJAP35 (ORCPT ); Tue, 1 Oct 2019 11:29:57 -0400 Received: from mail-pg1-f195.google.com ([209.85.215.195]:33809 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727204AbfJAP35 (ORCPT ); Tue, 1 Oct 2019 11:29:57 -0400 Received: by mail-pg1-f195.google.com with SMTP id y35so9915934pgl.1; Tue, 01 Oct 2019 08:29:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:from:to:cc:date:message-id:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=JJfwPr63Q1M/xjYGQd4hOFUJFA0qW3vXAUWOtzOr/WI=; b=EJWJX4ErxRGwE7y76gGZzAEtqqiQrD5im/sxBJFAMkJ1URnMlwtA5H+OqB6GnDPL5C oAQgc41D8jPmoC06Nar05rkZGKOqvbi/XN82QJFufR7+tQLwpF8pY7Q9p6xhdB3crn4Y CS2lLOKTMmlRuxGioi/w7OidRP2aOP2myH5fZ/QHKI8g/pp1g69ebsDaD2blYzqXNv0+ 5YZ+4ajpCmN0QPy7LcXmaMB5cC+oiinhN4hBL8crarzP23+wVvd6sLOKaEWCvGDfcKDe twAWSY41t1AFC1Fb8PsDjN9aMPJE5iFgCOvWoGH27rMZkchZISLXhAgoVCrHR2NNUewi x8hA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=JJfwPr63Q1M/xjYGQd4hOFUJFA0qW3vXAUWOtzOr/WI=; b=tjAPn49YiVNSnwLerfZtnJ9bl9k+xInvrqdRwHpL1qTVPkO8HlHhDZO0AjqySeoScT 3sVpNEFz6n/sboM6xeta65LWlq0TVn0k3locBSIZ4iARycXi6d2zYjvPj6Vx3Usub4MU T+dslwlKgE/GfRoWHv/otxPwymMLUjZFG/M9UJ2NIBhCpcsnCp3P/fWKu1J9IBj0/Ht4 wqmtuRpkugyw5l2/MHgezmxJGPVXDmv8F7XcDs7XEOm30N73aZ/1HX8EJ9SF4ehK2K7p wueSu0gOU/v9fBvd90J4cInunF4WBCb6YDkqUrClx6kpkIoF985kv0Aiwz+aI6mznCIk yvNQ== X-Gm-Message-State: APjAAAXcVh805gEITNDCU7+ckhXHPoqKyoeZz2Lw3nM5za1uGBu4KMGC TrxUerrkOiTKzJNd/c6pKRs= X-Google-Smtp-Source: APXvYqwnIYGjdrTA4iU5gwI1UcD6Fg1tHESJxXf3TxmTyzoK6SJUvo0RI+T3UC5bIBA9r3X4CIHp3Q== X-Received: by 2002:a63:3c46:: with SMTP id i6mr19268875pgn.18.1569943795790; Tue, 01 Oct 2019 08:29:55 -0700 (PDT) Received: from localhost.localdomain ([2001:470:b:9c3:9e5c:8eff:fe4f:f2d0]) by smtp.gmail.com with ESMTPSA id b3sm2772447pjp.13.2019.10.01.08.29.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Oct 2019 08:29:55 -0700 (PDT) Subject: [PATCH v11 6/6] virtio-balloon: Add support for providing unused page reports to host From: Alexander Duyck To: virtio-dev@lists.oasis-open.org, kvm@vger.kernel.org, mst@redhat.com, david@redhat.com, dave.hansen@intel.com, linux-kernel@vger.kernel.org, willy@infradead.org, mhocko@kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org, mgorman@techsingularity.net, vbabka@suse.cz, osalvador@suse.de Cc: yang.zhang.wz@gmail.com, pagupta@redhat.com, konrad.wilk@oracle.com, nitesh@redhat.com, riel@surriel.com, lcapitulino@redhat.com, wei.w.wang@intel.com, aarcange@redhat.com, pbonzini@redhat.com, dan.j.williams@intel.com, alexander.h.duyck@linux.intel.com Date: Tue, 01 Oct 2019 08:29:54 -0700 Message-ID: <20191001152954.27008.95804.stgit@localhost.localdomain> In-Reply-To: <20191001152441.27008.99285.stgit@localhost.localdomain> References: <20191001152441.27008.99285.stgit@localhost.localdomain> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Alexander Duyck Add support for the page reporting feature provided by virtio-balloon. Reporting differs from the regular balloon functionality in that is is much less durable than a standard memory balloon. Instead of creating a list of pages that cannot be accessed the pages are only inaccessible while they are being indicated to the virtio interface. Once the interface has acknowledged them they are placed back into their respective free lists and are once again accessible by the guest system. Signed-off-by: Alexander Duyck --- drivers/virtio/Kconfig | 1 + drivers/virtio/virtio_balloon.c | 65 +++++++++++++++++++++++++++++++++++ include/uapi/linux/virtio_balloon.h | 1 + 3 files changed, 67 insertions(+) diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig index 078615cf2afc..4b2dd8259ff5 100644 --- a/drivers/virtio/Kconfig +++ b/drivers/virtio/Kconfig @@ -58,6 +58,7 @@ config VIRTIO_BALLOON tristate "Virtio balloon driver" depends on VIRTIO select MEMORY_BALLOON + select PAGE_REPORTING ---help--- This driver supports increasing and decreasing the amount of memory within a KVM guest. diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c index 92099298bc16..b56ca35482bc 100644 --- a/drivers/virtio/virtio_balloon.c +++ b/drivers/virtio/virtio_balloon.c @@ -19,6 +19,7 @@ #include #include #include +#include /* * Balloon device works in 4K page units. So each page is pointed to by @@ -37,6 +38,9 @@ #define VIRTIO_BALLOON_FREE_PAGE_SIZE \ (1 << (VIRTIO_BALLOON_FREE_PAGE_ORDER + PAGE_SHIFT)) +/* limit on the number of pages that can be on the reporting vq */ +#define VIRTIO_BALLOON_VRING_HINTS_MAX 16 + #ifdef CONFIG_BALLOON_COMPACTION static struct vfsmount *balloon_mnt; #endif @@ -46,6 +50,7 @@ enum virtio_balloon_vq { VIRTIO_BALLOON_VQ_DEFLATE, VIRTIO_BALLOON_VQ_STATS, VIRTIO_BALLOON_VQ_FREE_PAGE, + VIRTIO_BALLOON_VQ_REPORTING, VIRTIO_BALLOON_VQ_MAX }; @@ -113,6 +118,10 @@ struct virtio_balloon { /* To register a shrinker to shrink memory upon memory pressure */ struct shrinker shrinker; + + /* Unused page reporting device */ + struct virtqueue *reporting_vq; + struct page_reporting_dev_info ph_dev_info; }; static struct virtio_device_id id_table[] = { @@ -152,6 +161,32 @@ static void tell_host(struct virtio_balloon *vb, struct virtqueue *vq) } +void virtballoon_unused_page_report(struct page_reporting_dev_info *ph_dev_info, + unsigned int nents) +{ + struct virtio_balloon *vb = + container_of(ph_dev_info, struct virtio_balloon, ph_dev_info); + struct virtqueue *vq = vb->reporting_vq; + unsigned int unused, err; + + /* We should always be able to add these buffers to an empty queue. */ + err = virtqueue_add_inbuf(vq, ph_dev_info->sg, nents, vb, + GFP_NOWAIT | __GFP_NOWARN); + + /* + * In the extremely unlikely case that something has changed and we + * are able to trigger an error we will simply display a warning + * and exit without actually processing the pages. + */ + if (WARN_ON(err)) + return; + + virtqueue_kick(vq); + + /* When host has read buffer, this completes via balloon_ack */ + wait_event(vb->acked, virtqueue_get_buf(vq, &unused)); +} + static void set_page_pfns(struct virtio_balloon *vb, __virtio32 pfns[], struct page *page) { @@ -476,6 +511,7 @@ static int init_vqs(struct virtio_balloon *vb) names[VIRTIO_BALLOON_VQ_DEFLATE] = "deflate"; names[VIRTIO_BALLOON_VQ_STATS] = NULL; names[VIRTIO_BALLOON_VQ_FREE_PAGE] = NULL; + names[VIRTIO_BALLOON_VQ_REPORTING] = NULL; if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) { names[VIRTIO_BALLOON_VQ_STATS] = "stats"; @@ -487,11 +523,19 @@ static int init_vqs(struct virtio_balloon *vb) callbacks[VIRTIO_BALLOON_VQ_FREE_PAGE] = NULL; } + if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) { + names[VIRTIO_BALLOON_VQ_REPORTING] = "reporting_vq"; + callbacks[VIRTIO_BALLOON_VQ_REPORTING] = balloon_ack; + } + err = vb->vdev->config->find_vqs(vb->vdev, VIRTIO_BALLOON_VQ_MAX, vqs, callbacks, names, NULL, NULL); if (err) return err; + if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) + vb->reporting_vq = vqs[VIRTIO_BALLOON_VQ_REPORTING]; + vb->inflate_vq = vqs[VIRTIO_BALLOON_VQ_INFLATE]; vb->deflate_vq = vqs[VIRTIO_BALLOON_VQ_DEFLATE]; if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) { @@ -932,12 +976,30 @@ static int virtballoon_probe(struct virtio_device *vdev) if (err) goto out_del_balloon_wq; } + + vb->ph_dev_info.report = virtballoon_unused_page_report; + if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) { + unsigned int capacity; + + capacity = min_t(unsigned int, + virtqueue_get_vring_size(vb->reporting_vq), + VIRTIO_BALLOON_VRING_HINTS_MAX); + vb->ph_dev_info.capacity = capacity; + + err = page_reporting_register(&vb->ph_dev_info); + if (err) + goto out_unregister_shrinker; + } + virtio_device_ready(vdev); if (towards_target(vb)) virtballoon_changed(vdev); return 0; +out_unregister_shrinker: + if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM)) + virtio_balloon_unregister_shrinker(vb); out_del_balloon_wq: if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) destroy_workqueue(vb->balloon_wq); @@ -966,6 +1028,8 @@ static void virtballoon_remove(struct virtio_device *vdev) { struct virtio_balloon *vb = vdev->priv; + if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) + page_reporting_unregister(&vb->ph_dev_info); if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM)) virtio_balloon_unregister_shrinker(vb); spin_lock_irq(&vb->stop_update_lock); @@ -1038,6 +1102,7 @@ static int virtballoon_validate(struct virtio_device *vdev) VIRTIO_BALLOON_F_DEFLATE_ON_OOM, VIRTIO_BALLOON_F_FREE_PAGE_HINT, VIRTIO_BALLOON_F_PAGE_POISON, + VIRTIO_BALLOON_F_REPORTING, }; static struct virtio_driver virtio_balloon_driver = { diff --git a/include/uapi/linux/virtio_balloon.h b/include/uapi/linux/virtio_balloon.h index a1966cd7b677..19974392d324 100644 --- a/include/uapi/linux/virtio_balloon.h +++ b/include/uapi/linux/virtio_balloon.h @@ -36,6 +36,7 @@ #define VIRTIO_BALLOON_F_DEFLATE_ON_OOM 2 /* Deflate balloon on OOM */ #define VIRTIO_BALLOON_F_FREE_PAGE_HINT 3 /* VQ to report free pages */ #define VIRTIO_BALLOON_F_PAGE_POISON 4 /* Guest is using page poisoning */ +#define VIRTIO_BALLOON_F_REPORTING 5 /* Page reporting virtqueue */ /* Size of a PFN in the balloon interface. */ #define VIRTIO_BALLOON_PFN_SHIFT 12