The patch titled Subject: mm: adjust shuffle code to allow for future coalescing has been added to the -mm tree. Its filename is mm-adjust-shuffle-code-to-allow-for-future-coalescing.patch This patch should soon appear at http://ozlabs.org/~akpm/mmots/broken-out/mm-adjust-shuffle-code-to-allow-for-future-coalescing.patch and later at http://ozlabs.org/~akpm/mmotm/broken-out/mm-adjust-shuffle-code-to-allow-for-future-coalescing.patch Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next and is updated there every 3-4 working days ------------------------------------------------------ From: Alexander Duyck <alexander.h.duyck@xxxxxxxxxxxxxxx> Subject: mm: adjust shuffle code to allow for future coalescing Patch series "mm / virtio: Provide support for unused page reporting", v13. This series provides an asynchronous means of reporting unused guest pages to a hypervisor so that the memory associated with those pages can be dropped and reused by other processes and/or guests on the host. Using this it is possible to avoid unnecessary I/O to disk and greatly improve performance in the case of memory overcommit on the host. When enabled it will allocate a set of statistics to track the number of reported pages. When the nr_free for a given free_area is greater than this by the high water mark we will schedule a worker to begin allocating the non-reported memory and to provide it to the reporting interface via a scatterlist. Currently this is only in use by virtio-balloon however there is the hope that at some point in the future other hypervisors might be able to make use of it. In the virtio-balloon/QEMU implementation the hypervisor is currently using MADV_DONTNEED to indicate to the host kernel that the page is currently unused. It will be faulted back into the guest the next time the page is accessed. To track if a page is reported or not the Uptodate flag was repurposed and used as a Reported flag for Buddy pages. While we are processing the pages in a given zone we have a set of pointers we track called reported_boundary that is used to keep our processing time to a minimum. Without these we would have to iterate through all of the reported pages which would become a significant burden. I measured as much as a 20% performance degradation without using the boundary pointers. In the event of something like compaction needing to process the zone at the same time it currently resorts to resetting the boundary if it is rearranging the list. However in the future it could choose to delay processing the zone if a flag is set indicating that a zone is being actively processed. Below are the results from various benchmarks. I primarily focused on two tests. The first is the will-it-scale/page_fault2 test, and the other is a modified version of will-it-scale/page_fault1 that was enabled to use THP. I did this as it allows for better visibility into different parts of the memory subsystem. The guest is running with 32G for RAM on one node of a E5-2630 v3. Test page_fault1 (THP) page_fault2 Baseline 1 1209281.00 +/-0.47% 411314.00 +/-0.42% 16 8804587.33 +/-1.80% 3419453.00 +/-1.80% Patches applied 1 1209369.67 +/-0.06% 412187.00 +/-0.10% 16 8812606.33 +/-0.06% 3435339.33 +/-1.82% Patches enabled 1 1209104.67 +/-0.11% 413067.67 +/-0.43% MADV disabled 16 8835481.67 +/-0.29% 3463485.67 +/-0.50% Patches enabled 1 1210367.67 +/-0.58% 416962.00 +/-0.14% 16 8433236.00 +/-0.58% 3437897.67 +/-0.34% The results above are for a baseline with a linux-next-20191031 kernel, that kernel with this patch set applied but page reporting disabled in virtio-balloon, patches applied but the madvise disabled by direct assigning a device, and the patches applied and page reporting fully enabled. These results include the deviation seen between the average value reported here versus the high and/or low value. I observed that during the test the memory usage for the first three tests never dropped whereas with the patches fully enabled the VM would drop to using only a few GB of the host's memory when switching from memhog to page fault tests. Most of the overhead seen with this patch set fully enabled is due to the fact that accessing the reported pages will cause a page fault and the host will have to zero the page before giving it back to the guest. The overall guest size is kept fairly small to only a few GB while the test is running. This overhead is much more visible when using THP than with standard 4K pages. As such for the case where the host memory is not oversubscribed this results in a performance regression, however if the host memory were oversubscribed this patch set should result in a performance improvement as swapping memory from the host can be avoided. A brief history on the background of unused page reporting can be found at: https://lore.kernel.org/lkml/29f43d5796feed0dec8e8bb98b187d9dac03b900.camel@xxxxxxxxxxxxxxx/ This patch (of 6): 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. Link: http://lkml.kernel.org/r/20191105220207.15144.8826.stgit@localhost.localdomain Signed-off-by: Alexander Duyck <alexander.h.duyck@xxxxxxxxxxxxxxx> Acked-by: David Hildenbrand <david@xxxxxxxxxx> Reviewed-by: Dan Williams <dan.j.williams@xxxxxxxxx> Cc: Michael S. Tsirkin <mst@xxxxxxxxxx> Cc: Matthew Wilcox <willy@xxxxxxxxxxxxx> Cc: Michal Hocko <mhocko@xxxxxxxxxx> Cc: Mel Gorman <mgorman@xxxxxxxxxxxxxxxxxxx> Cc: Vlastimil Babka <vbabka@xxxxxxx> Cc: Yang Zhang <yang.zhang.wz@xxxxxxxxx> Cc: <konrad.wilk@xxxxxxxxxx> Cc: Pankaj Gupta <pagupta@xxxxxxxxxx> Cc: Rik van Riel <riel@xxxxxxxxxxx> Cc: Luiz Capitulino <lcapitulino@xxxxxxxxxx> Cc: Dave Hansen <dave.hansen@xxxxxxxxx> Cc: Wei Wang <wei.w.wang@xxxxxxxxx> Cc: Andrea Arcangeli <aarcange@xxxxxxxxxx> Cc: Paolo Bonzini <pbonzini@xxxxxxxxxx> Cc: Dan Williams <dan.j.williams@xxxxxxxxx> Cc: Oscar Salvador <osalvador@xxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- 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(-) --- a/include/linux/mmzone.h~mm-adjust-shuffle-code-to-allow-for-future-coalescing +++ a/include/linux/mmzone.h @@ -116,18 +116,6 @@ static inline void add_to_free_area_tail 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) --- a/mm/page_alloc.c~mm-adjust-shuffle-code-to-allow-for-future-coalescing +++ a/mm/page_alloc.c @@ -878,6 +878,36 @@ compaction_capture(struct capture_contro #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(struc 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 @@ continue_merging: 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); } /* --- a/mm/shuffle.c~mm-adjust-shuffle-code-to-allow-for-future-coalescing +++ a/mm/shuffle.c @@ -183,11 +183,11 @@ void __meminit __shuffle_free_memory(pg_ 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 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; } --- a/mm/shuffle.h~mm-adjust-shuffle-code-to-allow-for-future-coalescing +++ a/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 return order >= SHUFFLE_ORDER; } #else +static inline bool shuffle_pick_tail(void) +{ + return false; +} + static inline void shuffle_free_memory(pg_data_t *pgdat) { } _ Patches currently in -mm which might be from alexander.h.duyck@xxxxxxxxxxxxxxx are mm-adjust-shuffle-code-to-allow-for-future-coalescing.patch mm-use-zone-and-order-instead-of-free-area-in-free_list-manipulators.patch mm-introduce-reported-pages.patch mm-add-device-side-and-notifier-for-unused-page-reporting.patch virtio-balloon-pull-page-poisoning-config-out-of-free-page-hinting.patch virtio-balloon-add-support-for-providing-unused-page-reports-to-host.patch