mm / virtio: Provide support for unused page reporting
Subsystems (8)
v12 cover mm / virtio: Provide support for unused page reporting
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.
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 on one node of a E5-2630 v3 CPU with 48G of RAM that I split up into two logical nodes in the guest in order to test with NUMA as well.
Test page_fault1 (THP) page_fault2
Baseline 1 1256106.33 +/-0.09% 482202.67 +/-0.46%
16 8864441.67 +/-0.09% 3734692.00 +/-1.23%
Patches applied 1 1257096.00 +/-0.06% 477436.00 +/-0.16%
16 8864677.33 +/-0.06% 3800037.00 +/-0.19%Patches enabled 1 1258420.00 +/-0.04% 480080.00 +/-0.07% MADV disabled 16 8753840.00 +/-1.27% 3782764.00 +/-0.37%
Patches enabled 1 1267916.33 +/-0.08% 472075.67 +/-0.39%
16 8287050.33 +/-0.67% 3774500.33 +/-0.11%The results above are for a baseline with a linux-next-20191021 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.
There is currently an alternative patch set[1] that has been under work for some time however the v12 version of that patch set could not be tested as it triggered a kernel panic when I attempted to test it. It requires multiple modifications to get up and running with performance comparable to this patch set. A follow-on set has yet to be posted. As such I have not included results from that patch set, and I would appreciate it if we could keep this patch set the focus of any discussion on this thread.
For info on earlier versions you will need to follow the links provided with the respective versions.
[1]: https://lore.kernel.org/lkml/[email protected]/
Changes from v10:
https://lore.kernel.org/lkml/[email protected]/
Rebased on "Add linux-next specific files for 20190930"
Added page_is_reported() macro to prevent unneeded testing of PageReported bit Fixed several spots where comments referred to older aeration naming Set upper limit for phdev->capacity to page reporting high water mark Updated virtio page poison detection logic to also cover init_on_free Tweaked page_reporting_notify_free to reduce code size
Removed dead code in non-reporting path
Changes from v11:
https://lore.kernel.org/lkml/[email protected]/
Removed unnecessary whitespace change from patch 2
Minor tweak to get_unreported_page to avoid excess writes to boundary Rewrote cover page to lay out additional performance info.
---
Alexander Duyck (6):
mm: Adjust shuffle code to allow for future coalescing
mm: Use zone and order instead of free area in free_list manipulators
mm: Introduce Reported pages
mm: Add device side and notifier for unused page reporting
virtio-balloon: Pull page poisoning config out of free page hinting
virtio-balloon: Add support for providing unused page reports to host
drivers/virtio/Kconfig | 1 drivers/virtio/virtio_balloon.c | 88 ++++++++- include/linux/mmzone.h | 60 ++---- include/linux/page-flags.h | 11 + include/linux/page_reporting.h | 31 +++ include/uapi/linux/virtio_balloon.h | 1 mm/Kconfig | 11 + mm/Makefile | 1 mm/compaction.c | 5 mm/memory_hotplug.c | 2 mm/page_alloc.c | 194 +++++++++++++++---- mm/page_reporting.c | 353 +++++++++++++++++++++++++++++++++++ mm/page_reporting.h | 225 ++++++++++++++++++++++ mm/shuffle.c | 12 + mm/shuffle.h | 6 + 15 files changed, 899 insertions(+), 102 deletions(-) create mode 100644 include/linux/page_reporting.h create mode 100644 mm/page_reporting.c create mode 100644 mm/page_reporting.h
--
PATCH v12 1/6 mm: Adjust shuffle code to allow for future coalescing
From: Alexander Duyck <[email protected]>
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.
--- 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(-)
include/linux/mmzone.h +0 -12
@@ -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)
mm/page_alloc.c +42 -29
94 lines not on this page · raw message
mm/shuffle.c +6 -6
@@ -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; }
mm/shuffle.h +6 -0
@@ -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) { }
PATCH v12 2/6 mm: Use zone and order instead of free area in free_list manipulators
In reply to Alexander DuyckFrom: Alexander Duyck <[email protected]>
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.
--- include/linux/mmzone.h | 32 ----------------------- mm/page_alloc.c | 67 +++++++++++++++++++++++++++++++++++------------- 2 files changed, 49 insertions(+), 50 deletions(-)
include/linux/mmzone.h +0 -32
@@ -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]);
mm/page_alloc.c +49 -18
156 lines not on this page · raw message
PATCH v12 3/6 mm: Introduce Reported pages
In reply to Alexander DuyckFrom: Alexander Duyck <[email protected]>
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.
--- 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 | 66 +++++++++++++++-- mm/page_reporting.h | 176 ++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 280 insertions(+), 7 deletions(-) create mode 100644 mm/page_reporting.h
include/linux/mmzone.h +16 -0
@@ -470,6 +470,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 */
@@ -545,6 +553,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 +* actively 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)
include/linux/page-flags.h +11 -0
@@ -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.
mm/Kconfig +11 -0
@@ -237,6 +237,17 @@ config COMPACTION [email protected]. # +# 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
mm/compaction.c +5 -0
@@ -24,6 +24,7 @@#include <linux/page_owner.h> #include <linux/psi.h> #include "internal.h" +#include "page_reporting.h" #ifdef CONFIG_COMPACTION static inline void count_compact_event (enum vm_event_item item)
@@ -1326,6 +1327,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;
@@ -1682,6 +1685,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;
mm/memory_hotplug.c +2 -0
@@ -41,6 +41,7 @@#include "internal.h" #include "shuffle.h" +#include "page_reporting.h" /* * online_page_callback contains pointer to current page onlining function.
@@ -1558,6 +1559,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);
mm/page_alloc.c +59 -7
121 lines not on this page · raw message
mm/page_reporting.h new file +176 -0
176 lines not on this page · raw message
PATCH v12 4/6 mm: Add device side and notifier for unused page reporting
In reply to Alexander DuyckFrom: Alexander Duyck <[email protected]>
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.
--- include/linux/page_reporting.h | 31 ++++ mm/Makefile | 1 mm/page_alloc.c | 10 + mm/page_reporting.c | 353 ++++++++++++++++++++++++++++++++++++++++ mm/page_reporting.h | 49 ++++++ 5 files changed, 442 insertions(+), 2 deletions(-) create mode 100644 include/linux/page_reporting.h create mode 100644 mm/page_reporting.c
include/linux/page_reporting.h new file +31 -0
@@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_PAGE_REPORTING_H +#define _LINUX_PAGE_REPORTING_H + +#include <linux/mmzone.h> + +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 */
mm/Makefile +1 -0
@@ -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
mm/page_alloc.c +8 -2
@@ -1073,6 +1073,14 @@ static inline void __free_one_page(struct page *page,add_to_free_list_tail (page, zone, order, migratetype); elseadd_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); }/*
@@ -2276,8 +2284,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
mm/page_reporting.c new file +353 -0
353 lines not on this page · raw message
mm/page_reporting.h +49 -0
67 lines not on this page · raw message
PATCH v12 5/6 virtio-balloon: Pull page poisoning config out of free page hinting
In reply to Alexander DuyckFrom: Alexander Duyck <[email protected]>
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.
--- drivers/virtio/virtio_balloon.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-)
drivers/virtio/virtio_balloon.c +17 -6
@@ -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 );
PATCH v12 6/6 virtio-balloon: Add support for providing unused page reports to host
In reply to Alexander DuyckFrom: Alexander Duyck <[email protected]>
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.
--- drivers/virtio/Kconfig | 1 + drivers/virtio/virtio_balloon.c | 65 +++++++++++++++++++++++++++++++++++ include/uapi/linux/virtio_balloon.h | 1 + 3 files changed, 67 insertions(+)
drivers/virtio/Kconfig +1 -0
@@ -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.
drivers/virtio/virtio_balloon.c +65 -0
136 lines not on this page · raw message
include/uapi/linux/virtio_balloon.h +1 -0
@@ -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
PATCH v12 1/3 virtio-ballon: Implement support for page poison tracking feature
In reply to Alexander DuyckFrom: Alexander Duyck <[email protected]>
We need to make certain to advertise support for page poison tracking if we want to actually get data on if the guest will be poisoning pages. So if free page hinting is active we should add page poisoning support and let the guest disable it if it isn't using it.
Page poisoning will result in a page being dirtied on free. As such we cannot really avoid having to copy the page at least one more time since we will need to write the poison value to the destination. As such we can just ignore free page hinting if page poisoning is enabled as it will actually reduce the work we have to do.
--- hw/virtio/virtio-balloon.c | 25 +++++++++++++++++++++---- include/hw/virtio/virtio-balloon.h | 1 + 2 files changed, 22 insertions(+), 4 deletions(-)
hw/virtio/virtio-balloon.c +21 -4
@@ -531,6 +531,15 @@ static void virtio_balloon_free_page_start(VirtIOBalloon *s)return ; } +/* +* If page poisoning is enabled then we probably shouldn't bother with +* the hinting since the poisoning will dirty the page and invalidate +* the work we are doing anyway. +*/ +if (virtio_vdev_has_feature (vdev,VIRTIO_BALLOON_F_PAGE_POISON )) { +return ; + } +if (s->free_page_report_cmd_id ==UINT_MAX ) { s->free_page_report_cmd_id =VIRTIO_BALLOON_FREE_PAGE_REPORT_CMD_ID_MIN ;
@@ -618,12 +627,10 @@ static size_t virtio_balloon_config_size(VirtIOBalloon *s)if (s->qemu_4_0_config_size) {return sizeof (struct virtio_balloon_config ); } -if (virtio_has_feature (features,VIRTIO_BALLOON_F_PAGE_POISON )) { +if (virtio_has_feature (features,VIRTIO_BALLOON_F_PAGE_POISON ) || +virtio_has_feature (features,VIRTIO_BALLOON_F_FREE_PAGE_HINT )) {return sizeof (struct virtio_balloon_config ); } -if (virtio_has_feature (features,VIRTIO_BALLOON_F_FREE_PAGE_HINT )) { -return offsetof(struct virtio_balloon_config , poison_val); - }return offsetof(struct virtio_balloon_config , free_page_report_cmd_id); }
@@ -634,6 +641,7 @@ static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data) config.num_pages =cpu_to_le32 (dev->num_pages); config.actual =cpu_to_le32 (dev->actual); + config.poison_val =cpu_to_le32 (dev->poison_val);if (dev->free_page_report_status ==FREE_PAGE_REPORT_S_REQUESTED ) { config.free_page_report_cmd_id =
@@ -697,6 +705,8 @@ static void virtio_balloon_set_config(VirtIODevice *vdev,qapi_event_send_balloon_change (vm_ram_size - ((ram_addr_t ) dev->actual <<VIRTIO_BALLOON_PFN_SHIFT )); } + dev->poison_val =virtio_vdev_has_feature (vdev,VIRTIO_BALLOON_F_PAGE_POISON ) ? +le32_to_cpu (config.poison_val) :0 ;trace_virtio_balloon_set_config (dev->actual, oldactual); }
@@ -706,6 +716,9 @@ static uint64_t virtio_balloon_get_features(VirtIODevice *vdev, uint64_t f,VirtIOBalloon *dev =VIRTIO_BALLOON (vdev); f |= dev->host_features;virtio_add_feature (&f,VIRTIO_BALLOON_F_STATS_VQ ); +if (virtio_has_feature (f,VIRTIO_BALLOON_F_FREE_PAGE_HINT )) { +virtio_add_feature (&f,VIRTIO_BALLOON_F_PAGE_POISON ); + }return f; }
@@ -847,6 +860,8 @@ static void virtio_balloon_device_reset(VirtIODevice *vdev)g_free (s->stats_vq_elem); s->stats_vq_elem =NULL ; } + + s->poison_val =0 ; }static void virtio_balloon_set_status (VirtIODevice *vdev,uint8_t status)
@@ -909,6 +924,8 @@ static Property virtio_balloon_properties[] = { VIRTIO_BALLOON_F_DEFLATE_ON_OOM, false),DEFINE_PROP_BIT ("free-page-hint" , VirtIOBalloon, host_features,VIRTIO_BALLOON_F_FREE_PAGE_HINT , false), +DEFINE_PROP_BIT ("x-page-poison" , VirtIOBalloon, host_features, + VIRTIO_BALLOON_F_PAGE_POISON, false),/* QEMU 4.0 accidentally changed the config size even when free-page-hint * is disabled, resulting in QEMU 3.1 migration incompatibility. This * property retains this quirk for QEMU 4.1 machine types.
include/hw/virtio/virtio-balloon.h +1 -0
@@ -70,6 +70,7 @@ typedef struct VirtIOBalloon {uint32_t host_features;bool qemu_4_0_config_size; + uint32_t poison_val; } VirtIOBalloon;#endif
PATCH v12 2/3 virtio-balloon: Add bit to notify guest of unused page reporting
In reply to Alexander DuyckFrom: Alexander Duyck <[email protected]>
Add a bit for the page reporting feature provided by virtio-balloon.
This patch should be replaced once the feature is added to the Linux kernel and the bit is backported into this exported kernel header.
--- include/standard-headers/linux/virtio_balloon.h | 1 + 1 file changed, 1 insertion(+)
include/standard-headers/linux/virtio_balloon.h +1 -0
@@ -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
PATCH v12 3/3 virtio-balloon: Provide a interface for unused page reporting
In reply to Alexander DuyckFrom: Alexander Duyck <[email protected]>
Add support for what I am referring to as "unused page reporting". Basically the idea is to function very similar to how the balloon works in that we basically end up madvising the page as not being used. However we don't really need to bother with any deflate type logic since the page will be faulted back into the guest when it is read or written to.
This is meant to be a simplification of the existing balloon interface to use for providing hints to what memory needs to be freed. I am assuming this is safe to do as the deflate logic does not actually appear to do very much other than tracking what subpages have been released and which ones haven't.
--- hw/virtio/virtio-balloon.c | 46 ++++++++++++++++++++++++++++++++++-- include/hw/virtio/virtio-balloon.h | 2 +- 2 files changed, 45 insertions(+), 3 deletions(-)
hw/virtio/virtio-balloon.c +44 -2
@@ -321,6 +321,40 @@ static void balloon_stats_set_poll_interval(Object *obj, Visitor *v,balloon_stats_change_timer (s,0 ); } +static void virtio_balloon_handle_report (VirtIODevice *vdev,VirtQueue *vq) +{ +VirtIOBalloon *dev =VIRTIO_BALLOON (vdev); +VirtQueueElement *elem; + +while ((elem =virtqueue_pop (vq,sizeof (VirtQueueElement)))) { +unsigned int i; + +for (i =0 ; i < elem->in_num; i++) { +void *addr = elem->in_sg[i].iov_base; +size_t size = elem->in_sg[i].iov_len; +ram_addr_t ram_offset; +size_t rb_page_size; +RAMBlock *rb; + +if (qemu_balloon_is_inhibited () || dev->poison_val) +continue ; + + rb =qemu_ram_block_from_host (addr, false, &ram_offset); + rb_page_size =qemu_ram_pagesize (rb); + +/* For now we will simply ignore unaligned memory regions */ +if ((ram_offset | size) & (rb_page_size -1 )) +continue ; + +ram_block_discard_range (rb, ram_offset, size); + } + +virtqueue_push (vq, elem,0 ); +virtio_notify (vdev, vq); +g_free (elem); + } +} +static void virtio_balloon_handle_output (VirtIODevice *vdev,VirtQueue *vq) {VirtIOBalloon *s =VIRTIO_BALLOON (vdev);
@@ -628,7 +662,8 @@ static size_t virtio_balloon_config_size(VirtIOBalloon *s)return sizeof (struct virtio_balloon_config ); }if (virtio_has_feature (features,VIRTIO_BALLOON_F_PAGE_POISON ) || -virtio_has_feature (features,VIRTIO_BALLOON_F_FREE_PAGE_HINT )) { +virtio_has_feature (features,VIRTIO_BALLOON_F_FREE_PAGE_HINT ) || +virtio_has_feature (features,VIRTIO_BALLOON_F_REPORTING )) {return sizeof (struct virtio_balloon_config ); }return offsetof(struct virtio_balloon_config , free_page_report_cmd_id);
@@ -716,7 +751,8 @@ static uint64_t virtio_balloon_get_features(VirtIODevice *vdev, uint64_t f,VirtIOBalloon *dev =VIRTIO_BALLOON (vdev); f |= dev->host_features;virtio_add_feature (&f,VIRTIO_BALLOON_F_STATS_VQ ); -if (virtio_has_feature (f,VIRTIO_BALLOON_F_FREE_PAGE_HINT )) { +if (virtio_has_feature (f,VIRTIO_BALLOON_F_FREE_PAGE_HINT ) || +virtio_has_feature (f,VIRTIO_BALLOON_F_REPORTING )) {virtio_add_feature (&f,VIRTIO_BALLOON_F_PAGE_POISON ); }
@@ -806,6 +842,10 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp) s->dvq =virtio_add_queue (vdev,128 , virtio_balloon_handle_output); s->svq =virtio_add_queue (vdev,128 , virtio_balloon_receive_stats); +if (virtio_has_feature (s->host_features,VIRTIO_BALLOON_F_REPORTING )) { + s->rvq =virtio_add_queue (vdev,32 , virtio_balloon_handle_report); + } +if (virtio_has_feature (s->host_features,VIRTIO_BALLOON_F_FREE_PAGE_HINT )) { s->free_page_vq = virtio_add_queue(vdev, VIRTQUEUE_MAX_SIZE,
@@ -932,6 +972,8 @@ static Property virtio_balloon_properties[] = {*/ DEFINE_PROP_BOOL ("qemu-4-0-config-size" , VirtIOBalloon, qemu_4_0_config_size, false), +DEFINE_PROP_BIT ("unused-page-reporting" , VirtIOBalloon, host_features, +VIRTIO_BALLOON_F_REPORTING , true),DEFINE_PROP_LINK ("iothread" , VirtIOBalloon, iothread,TYPE_IOTHREAD , IOThread *), DEFINE_PROP_END_OF_LIST(),
include/hw/virtio/virtio-balloon.h +1 -1
@@ -42,7 +42,7 @@ enum virtio_balloon_free_page_report_status { typedef struct VirtIOBalloon { VirtIODevice parent_obj; - VirtQueue *ivq, *dvq, *svq, *free_page_vq; + VirtQueue *ivq, *dvq, *svq, *free_page_vq, *rvq; uint32_t free_page_report_status; uint32_t num_pages; uint32_t actual;
Andrew Morton
maintainerQuoted 31 lines · On Tue, 22 Oct 2019 15:27:52 -0700 Alexander Duyck <[email protected]>…
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 on one node of a E5-2630 v3 CPU with 48G of RAM that I split up into two logical nodes in the guest in order to test with NUMA as well.
Test
page_fault1(THP)page_fault2
Baseline 1 1256106.33 +/-0.09% 482202.67 +/-0.46%16 8864441.67 +/-0.09% 3734692.00 +/-1.23%Patches applied 1 1257096.00 +/-0.06% 477436.00 +/-0.16% 16 8864677.33 +/-0.06% 3800037.00 +/-0.19%Patches enabled 1 1258420.00 +/-0.04% 480080.00 +/-0.07% MADV disabled 16 8753840.00 +/-1.27% 3782764.00 +/-0.37%Patches enabled 1 1267916.33 +/-0.08% 472075.67 +/-0.39% 16 8287050.33 +/-0.67% 3774500.33 +/-0.11%The results above are for a baseline with a linux-next-20191021 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.
Quoted 9 lines
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.
I'm trying to understand "how valuable is this patchset" and the above resulted in some headscratching.
Overall, how valuable is this patchset? To real users running real workloads?
Quoted 8 lines
There is currently an alternative patch set[1] that has been under work for some time however the v12 version of that patch set could not be tested as it triggered a kernel panic when I attempted to test it. It requires multiple modifications to get up and running with performance comparable to this patch set. A follow-on set has yet to be posted. As such I have not included results from that patch set, and I would appreciate it if we could keep this patch set the focus of any discussion on this thread.
Actually, the rest of us would be interested in a comparison ;)
Andrew Morton
maintainerRe: [PATCH v12 3/6] mm: Introduce Reported pages
In reply to Alexander DuyckQuoted 35 lines · On Tue, 22 Oct 2019 15:28:12 -0700 Alexander Duyck <[email protected]>…
From: Alexander Duyck <[email protected]>
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_boundaryinstead 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.
Quoted 5 lines
We store the index of the boundary pointer used to track the reported page in the
page->indexvalue. 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.
This looks like quite a lot of new code in code MM. Hence previous "how valuable is this patchset" question!
Some silly trivia which I noticed while perusing:
Quoted 6 lines
...
include/linux/mmzone.h struct zone { · line 479
Quoted 10 lines
include/linux/mmzone.h +7
-0@@ -470,6 +470,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;
Dumb question. Why not
unsigned long reported_pages[MAX_ORDER - PAGE_REPORTING_MIN_ORDER];
include/linux/mmzone.h struct zone {
Quoted 8 lines
include/linux/mmzone.h +2
-0+#endif int initialized;/* Write-intensive fields used from the page allocator */ +#define page_is_reported (_page) unlikely(PageReported(_page))
page_reported() would be more consistent.
Quoted 8 lines
...
include/linux/mmzone.h +5
-0+static inline void +add_page_to_reported_list (struct page *page,struct zone *zone, +unsigned int order,unsigned int mt) +{ +/*
include/linux/mmzone.h
Quoted 10 lines
include/linux/mmzone.h +10
-0+* 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 ]++;
nit. This is an array, not a list. The function name is a bit screwy.
Quoted 5 lines
include/linux/mmzone.h +2
-0+} +
Re: [PATCH v12 3/6] mm: Introduce Reported pages
Quoted 43 lines · Andrew Morton
Quoted 35 lines · On Tue, 22 Oct 2019 15:28:12 -0700 Alexander Duyck <[email protected]>…
From: Alexander Duyck <[email protected]>
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_boundaryinstead 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.
Quoted 5 lines
We store the index of the boundary pointer used to track the reported page in the
page->indexvalue. 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.
Quoted 4 lines
This looks like quite a lot of new code in code MM. Hence previous "how valuable is this patchset" question!
Some silly trivia which I noticed while perusing:
I'll try to answer it here.
My understanding is that this can be very valuable in the case where a host is oversubscribing guest memory. What I have seen is that memory overcommit can quickly cause certain workloads to take minutes versus just seconds depending on the speed at which memory is swapped out and in.
What this patch set is providing is a form of auto-ballooning that allows the guest to shink its memory footprint so that it can be packed more tightly with other guests, especially in the case where guests are often inactive.
Quoted 18 lines
Quoted 5 lines
...
include/linux/mmzone.h struct zone { · line 479
Quoted 10 lines
include/linux/mmzone.h +7
-0@@ -470,6 +470,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;Dumb question. Why not
Quoted 1 line
unsigned long reported_pages[MAX_ORDER - PAGE_REPORTING_MIN_ORDER];
It was mostly to avoid causing too much change to the zone structure. By placing it where I did I was essentially just making use of unused space that would have otherwise been padding. In addition, since this is only going to be used when in a virtualized environment we keep the size of the zone smaller on systems that won't be making use of page reporting.
Quoted 10 lines
include/linux/mmzone.h struct zone {
Quoted 8 lines
include/linux/mmzone.h +2
-0+#endif int initialized;/* Write-intensive fields used from the page allocator */ +#define page_is_reported (_page) unlikely(PageReported(_page))
page_reported()would be more consistent.
Okay, I can do that.
Quoted 11 lines
Quoted 11 lines
...
include/linux/mmzone.h +9
-0+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 ;
Quoted 8 lines
include/linux/mmzone.h
Quoted 6 lines
include/linux/mmzone.h +6
-0+ +/* flag page as reported */ +__SetPageReported (page); + +/* update areated page accounting */ + zone->reported_pages[order -PAGE_REPORTING_MIN_ORDER ]++;nit. This is an array, not a list. The function name is a bit screwy.
Yeah. Maybe I should rename this to mark_page_reported(). I think at some point it was updating the reported_boundary so that the page was pulled into the list. I gave up on that when we had to start supporting the boundary being pulled out from under us. The array is just for tracking the statistics and wasn't a consideration in the naming.
Quoted 43 lines · Andrew Morton
Quoted 31 lines · On Tue, 22 Oct 2019 15:27:52 -0700 Alexander Duyck <[email protected]>…
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 on one node of a E5-2630 v3 CPU with 48G of RAM that I split up into two logical nodes in the guest in order to test with NUMA as well.
Test
page_fault1(THP)page_fault2
Baseline 1 1256106.33 +/-0.09% 482202.67 +/-0.46%16 8864441.67 +/-0.09% 3734692.00 +/-1.23%Patches applied 1 1257096.00 +/-0.06% 477436.00 +/-0.16% 16 8864677.33 +/-0.06% 3800037.00 +/-0.19%Patches enabled 1 1258420.00 +/-0.04% 480080.00 +/-0.07% MADV disabled 16 8753840.00 +/-1.27% 3782764.00 +/-0.37%Patches enabled 1 1267916.33 +/-0.08% 472075.67 +/-0.39% 16 8287050.33 +/-0.67% 3774500.33 +/-0.11%The results above are for a baseline with a linux-next-20191021 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.
Quoted 9 lines
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.
Quoted 5 lines
I'm trying to understand "how valuable is this patchset" and the above resulted in some headscratching.
Overall, how valuable is this patchset? To real users running real workloads?
A more detailed reply is in my response to your comments on patch 3. Basically the value is for host memory overcommit in that we can avoid having to go to swap nearly as often and can potentially pack the guests even tighter with better performance.
Quoted 10 lines
Quoted 8 lines
There is currently an alternative patch set[1] that has been under work for some time however the v12 version of that patch set could not be tested as it triggered a kernel panic when I attempted to test it. It requires multiple modifications to get up and running with performance comparable to this patch set. A follow-on set has yet to be posted. As such I have not included results from that patch set, and I would appreciate it if we could keep this patch set the focus of any discussion on this thread.
Actually, the rest of us would be interested in a comparison ;)
I understand that. However, the last time I tried benchmarking that patch set it blew up into a thread where we kept having to fix things on that patch set and by the time we were done we weren't benchmarking the v12 patch set anymore since we had made so many modifications to it, and that assumes Nitesh and I were in sync. Also I don't know what the current state of his patch set is as he was working on some additional changes when we last discussed things.
Ideally that patch set can be reposted with the necessary fixes and then we can go through any necessary debug, repair, and addressing limitations there.
Re: [PATCH v12 2/6] mm: Use zone and order instead of free area in free_list manipulators
In reply to Alexander DuyckQuoted 13 lines · Alexander Duyck
From: Alexander Duyck <[email protected]>
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.cwe can just move them there to reduce noise in the header.
Quoted 8 lines
Reviewed-by Dan Williams <[email protected]>Reviewed-by David Hildenbrand <[email protected]>Reviewed-by Pankaj Gupta <[email protected]>Signed-off-by Alexander Duyck <[email protected]>--- include/linux/mmzone.h | 32 ----------------------- mm/page_alloc.c | 67 +++++++++++++++++++++++++++++++++++------------- 2 files changed, 49 insertions(+), 50 deletions(-)
Did you see
https://lore.kernel.org/lkml/[email protected]/T/#m4d2bc2f37bd7bdc3ae35c4f197905c275d0ad2f9
this time?
And the difference to the old patch is only an empty line.
Signature · 3 lines
Thanks, David / dhildenb
Quoted 3 lines · Alexander Duyck
Quoted 2 lines · Andrew Morton
On Tue, 22 Oct 2019 15:27:52 -0700 Alexander Duyck <[email protected]> wrote:
[...]
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
There is currently an alternative patch set[1] that has been under work for some time however the v12 version of that patch set could not be tested as it triggered a kernel panic when I attempted to test it. It requires multiple modifications to get up and running with performance comparable to this patch set. A follow-on set has yet to be posted. As such I have not included results from that patch set, and I would appreciate it if we could keep this patch set the focus of any discussion on this thread.
Quoted 8 lines
Quoted 1 line
Actually, the rest of us would be interested in a comparison ;)
I understand that. However, the last time I tried benchmarking that patch set it blew up into a thread where we kept having to fix things on that patch set and by the time we were done we weren't benchmarking the v12 patch set anymore since we had made so many modifications to it, and that assumes Nitesh and I were in sync. Also I don't know what the current state of his patch set is as he was working on some additional changes when we last discussed things.
Just an update about the current state of my patch-series:
As we last discussed I was going to try implementing Michal Hock's suggestion of using page-isolation APIs. To do that I have replaced __isolate_free_page() with start/undo_isolate_free_page_range().
However, I am running into some issues which I am currently investigating.
After this, I will be investigating the reason why I was seeing degradation specifically with (MAX_ORDER - 2) as the reporting order.
Quoted 6 lines
Ideally that patch set can be reposted with the necessary fixes and then we can go through any necessary debug, repair, and addressing limitations there.
Quoted 131 lines · Alexander Duyck
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.
When enabled it will allocate a set of statistics to track the number of reported pages. When the
nr_freefor a givenfree_areais 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_DONTNEEDto 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_boundarythat 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 on one node of a E5-2630 v3 CPU with 48G of RAM that I split up into two logical nodes in the guest in order to test with NUMA as well.
Test
page_fault1(THP)page_fault2
Baseline 1 1256106.33 +/-0.09% 482202.67 +/-0.46%16 8864441.67 +/-0.09% 3734692.00 +/-1.23%Patches applied 1 1257096.00 +/-0.06% 477436.00 +/-0.16% 16 8864677.33 +/-0.06% 3800037.00 +/-0.19%Patches enabled 1 1258420.00 +/-0.04% 480080.00 +/-0.07% MADV disabled 16 8753840.00 +/-1.27% 3782764.00 +/-0.37%Patches enabled 1 1267916.33 +/-0.08% 472075.67 +/-0.39% 16 8287050.33 +/-0.67% 3774500.33 +/-0.11%The results above are for a baseline with a linux-next-20191021 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.
There is currently an alternative patch set[1] that has been under work for some time however the v12 version of that patch set could not be tested as it triggered a kernel panic when I attempted to test it. It requires multiple modifications to get up and running with performance comparable to this patch set. A follow-on set has yet to be posted. As such I have not included results from that patch set, and I would appreciate it if we could keep this patch set the focus of any discussion on this thread.
For info on earlier versions you will need to follow the links provided with the respective versions.
[1]: https://lore.kernel.org/lkml/[email protected]/
Changes from v10:
https://lore.kernel.org/lkml/[email protected]/
Rebased on "Add linux-next specific files for 20190930"
Addedpage_is_reported()macro to prevent unneeded testing of PageReported bit Fixed several spots where comments referred to older aeration naming Set upper limit forphdev->capacityto page reporting high water mark Updated virtio page poison detection logic to also coverinit_on_freeTweakedpage_reporting_notify_freeto reduce code size
Removed dead code in non-reporting pathChanges from v11:
https://lore.kernel.org/lkml/[email protected]/
Removed unnecessary whitespace change from patch 2
Minor tweak toget_unreported_pageto avoid excess writes to boundary Rewrote cover page to lay out additional performance info.---
Alexander Duyck (6):
mm: Adjust shuffle code to allow for future coalescing
mm: Use zone and order instead of free area infree_listmanipulators
mm: Introduce Reported pages
mm: Add device side and notifier for unused page reporting
virtio-balloon: Pull page poisoning config out of free page hinting
virtio-balloon: Add support for providing unused page reports to hostdrivers/virtio/Kconfig | 1 drivers/virtio/virtio_balloon.c | 88 ++++++++- include/linux/mmzone.h | 60 ++---- include/linux/page-flags.h | 11 + include/linux/page_reporting.h | 31 +++ include/uapi/linux/virtio_balloon.h | 1 mm/Kconfig | 11 + mm/Makefile | 1 mm/compaction.c | 5 mm/memory_hotplug.c | 2 mm/page_alloc.c | 194 +++++++++++++++---- mm/page_reporting.c | 353 +++++++++++++++++++++++++++++++++++ mm/page_reporting.h | 225 ++++++++++++++++++++++ mm/shuffle.c | 12 + mm/shuffle.h | 6 + 15 files changed, 899 insertions(+), 102 deletions(-) create mode 100644 include/linux/page_reporting.h create mode 100644 mm/page_reporting.c create mode 100644 mm/page_reporting.h
Quoted 2 lines
--
I think Michal Hocko suggested us to include a brief detail about the background explaining how we ended up with the current approach and what all things we have already tried.
That would help someone reviewing the patch-series for the first time to understand it in a better way.
--
Nitesh
Re: [PATCH v12 2/6] mm: Use zone and order instead of free area in free_list manipulators
In reply to David HildenbrandQuoted 23 lines · David Hildenbrand
Quoted 13 lines · Alexander Duyck
From: Alexander Duyck <[email protected]>
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.cwe can just move them there to reduce noise in the header.Quoted 8 lines
Reviewed-by Dan Williams <[email protected]>Reviewed-by David Hildenbrand <[email protected]>Reviewed-by Pankaj Gupta <[email protected]>Signed-off-by Alexander Duyck <[email protected]>--- include/linux/mmzone.h | 32 ----------------------- mm/page_alloc.c | 67 +++++++++++++++++++++++++++++++++++------------- 2 files changed, 49 insertions(+), 50 deletions(-)
Quoted 8 lines
Did you see
https://lore.kernel.org/lkml/[email protected]/T/#m4d2bc2f37bd7bdc3ae35c4f197905c275d0ad2f9
this time?
And the difference to the old patch is only an empty line.
I saw the report. However I have not had much luck reproducing it in order to get root cause. Here are my results for linux-next 20191021 with that patch running page_fault2 over an average of 3 runs:
Baseline: 3734692.00 This patch: 3739878.67
Also I am not so sure about these results as the same patch had passed previously before and instead it was patch 3 that was reported as having a -1.2% regression[1]. All I changed in response to that report was to add page_is_reported() which just wrapped the bit test for the reported flag in a #ifdef to avoid testing it for the blocks that were already #ifdef wrapped anyway.
I am still trying to see if I can get access to a system that would be a better match for the one that reported the issue. My working theory is that maybe it requires a high core count per node to reproduce. Either that or it is some combination of the kernel being tested on and the patch is causing some loop to go out of alignment and become more expensive.
I also included the page_fault2 results in my cover page as that seems to show a slight improvement with all of the patches applied.
Thanks.
- Alex
[1]: https://lore.kernel.org/lkml/20190921152522.GU15734@shao2-debian/
Quoted 5 lines · Nitesh Narayan Lal
Quoted 4 lines · Alexander Duyck
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.
<snip>
Quoted 6 lines
Quoted 1 line
I think Michal Hocko suggested us to include a brief detail about the background explaining how we ended up with the current approach and what all things we have already tried.
That would help someone reviewing the patch-series for the first time to understand it in a better way.
I'm not entirely sure it helps. The problem is that even the "brief" version will probably be pretty long.
From what I know the first real public discussion of guest memory overcommit and free page hinting dates back to the 2011 KVM forum and a presentation by Rik van Riel[0].
Before I got started in the code there was already virtio-balloon free page hinting[1]. However it was meant to be an all-at-once reporting of the free pages in the system at a given point in time, and used only for VM migration. All it does is inflate a balloon until it encounters an OOM and then it frees the memory back to the guest. One interesting piece that came out of the work on that patch set was the suggestion by Linus to use an array based incremental approach[2] which is what I based my later implementation on.
I believe Nitesh had already been working on his own approach for unused page hinting for some time at that point. Prior to submitting my RFC there was already a v7 that had been submitted by Nitesh back in mid 2018[3]. The solution was an array based approach which appeared to instrument arch_alloc_page and arch_free_page and would prevent allocations while hinting was occurring.
The first RFC I had written[4] was a synchronous approach that made use of arch_free_page to make a hypercall that would immediately flag the page as being unused. However a hypercall per page can be expensive and we ideally don't want the guest vCPU potentially being hung up while waiting on the host mmap_sem.
At about this time I believe Nitesh's solution[5] was still trying to keep an array of pages that were unused and tracking that via arch_free_page. In the synchronous case it could cause OOM errors, and in the asynchronous approach it had issues with being overrun and not being able to track unused pages.
Later I switched to an asynchronous approach[6], originally calling it "bubble hinting". With the asynchronous approach it is necessary to have a way to track what pages have been reported and what haven't. I originally was using the page type to track it as I had a Buddy and a TreatedBuddy, but ultimately that moved to a "Reported" page flag. In addition I pulled the counters and pointers out of the free_area/free_list and instead now have a stand-alone set of pointers and keep the reported statistics in a separate dynamic allocation.
Then Nitesh's solution had changed to the bitmap approach[7]. However it has been pointed out that this solution doesn't deal with sparse memory, hotplug, and various other issues.
Since then both my approach and Nitesh's approach have been iterating with mostly minor changes.
[0]: https://www.linux-kvm.org/images/f/ff/2011-forum-memory-overcommit.pdf
[1]: https://lore.kernel.org/lkml/[email protected]/
[2]: https://lore.kernel.org/lkml/CA+55aFzqj8wxXnHAdUTiOomipgFONVbqKMjL_tfk7e5ar1FziQ@mail.gmail.com/
[3]: https://www.spinics.net/lists/kvm/msg170113.html
[4]: https://lore.kernel.org/lkml/[email protected]/
[5]: https://lore.kernel.org/lkml/[email protected]/
[6]: https://lore.kernel.org/lkml/[email protected]/
[7]: https://lore.kernel.org/lkml/[email protected]/
Re: [PATCH v12 2/6] mm: Use zone and order instead of free area in free_list manipulators
In reply to Alexander DuyckQuoted 28 lines · Alexander Duyck
Quoted 27 lines · David Hildenbrand
Quoted 13 lines · Alexander Duyck
From: Alexander Duyck <[email protected]>
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.cwe can just move them there to reduce noise in the header.Quoted 8 lines
Reviewed-by Dan Williams <[email protected]>Reviewed-by David Hildenbrand <[email protected]>Reviewed-by Pankaj Gupta <[email protected]>Signed-off-by Alexander Duyck <[email protected]>--- include/linux/mmzone.h | 32 ----------------------- mm/page_alloc.c | 67 +++++++++++++++++++++++++++++++++++------------- 2 files changed, 49 insertions(+), 50 deletions(-)Did you see
https://lore.kernel.org/lkml/[email protected]/T/#m4d2bc2f37bd7bdc3ae35c4f197905c275d0ad2f9
Quoted 8 lines
Quoted 4 lines
this time?
And the difference to the old patch is only an empty line.
I saw the report. However I have not had much luck reproducing it in order to get root cause. Here are my results for linux-next 20191021 with that patch running
page_fault2over an average of 3 runs:
It would have been good if you'd reply to the report or sth. like that. Then people (including me) are aware that you looked into it and what your results of your investigation were.
Quoted 7 lines
Baseline: 3734692.00 This patch: 3739878.67Also I am not so sure about these results as the same patch had passed previously before and instead it was patch 3 that was reported as having a -1.2% regression[1]. All I changed in response to that report was to add
Well, previously there was also a regression in the successor PageReported() patch, not sure how they bisect in this case.
Quoted 6 lines
page_is_reported()which just wrapped the bit test for the reported flag in a #ifdef to avoid testing it for the blocks that were already #ifdef wrapped anyway.I am still trying to see if I can get access to a system that would be a better match for the one that reported the issue. My working theory is
I barely see false positives (well, I also barely see reports at all) on MM, that's why I asked.
Quoted 3 lines
that maybe it requires a high core count per node to reproduce. Either that or it is some combination of the kernel being tested on and the patch is causing some loop to go out of alignment and become more expensive.
Yes, double check that the config and the setup roughly matches what has been reported.
Quoted 10 lines
I also included the
page_fault2results in my cover page as that seems to show a slight improvement with all of the patches applied.Thanks.
- Alex
[1]: https://lore.kernel.org/lkml/20190921152522.GU15734@shao2-debian/
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH v12 2/6] mm: Use zone and order instead of free area in free_list manipulators
Quoted 34 lines · David Hildenbrand
Quoted 33 lines · Alexander Duyck
Quoted 31 lines · David Hildenbrand
Quoted 13 lines · Alexander Duyck
From: Alexander Duyck <[email protected]>
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.cwe can just move them there to reduce noise in the header.Quoted 8 lines
Reviewed-by Dan Williams <[email protected]>Reviewed-by David Hildenbrand <[email protected]>Reviewed-by Pankaj Gupta <[email protected]>Signed-off-by Alexander Duyck <[email protected]>--- include/linux/mmzone.h | 32 ----------------------- mm/page_alloc.c | 67 +++++++++++++++++++++++++++++++++++------------- 2 files changed, 49 insertions(+), 50 deletions(-)Did you see
https://lore.kernel.org/lkml/[email protected]/T/#m4d2bc2f37bd7bdc3ae35c4f197905c275d0ad2f9
this time?
And the difference to the old patch is only an empty line.
Quoted 7 lines
Quoted 3 lines
I saw the report. However I have not had much luck reproducing it in order to get root cause. Here are my results for linux-next 20191021 with that patch running
page_fault2over an average of 3 runs:It would have been good if you'd reply to the report or sth. like that. Then people (including me) are aware that you looked into it and what your results of your investigation were.
I'll try to be more careful to remember to do that in the future.
Quoted 9 lines
Quoted 6 lines
Baseline: 3734692.00 This patch: 3739878.67Also I am not so sure about these results as the same patch had passed previously before and instead it was patch 3 that was reported as having a -1.2% regression[1]. All I changed in response to that report was to add
Well, previously there was also a regression in the successor
PageReported()patch, not sure how they bisect in this case.
This is one of the things that has me thinking this is a possible code alignment type issue. In the past when chasing down network performance issues I would see these kind of things when a loop went from being cache line aligned to not being aligned.
Quoted 9 lines
Quoted 6 lines
page_is_reported()which just wrapped the bit test for the reported flag in a #ifdef to avoid testing it for the blocks that were already #ifdef wrapped anyway.I am still trying to see if I can get access to a system that would be a better match for the one that reported the issue. My working theory is
I barely see false positives (well, I also barely see reports at all) on MM, that's why I asked.
Like I said, I will dig into this.
Quoted 6 lines
Quoted 3 lines
that maybe it requires a high core count per node to reproduce. Either that or it is some combination of the kernel being tested on and the patch is causing some loop to go out of alignment and become more expensive.
Yes, double check that the config and the setup roughly matches what has been reported.
I've been testing with the same .config and version of gcc. The last bit I am trying now is to test with the same exact kernel source. I figure if I can reproduce the issue with that then I can figure out exact root cause, even if there isn't much we can do since the issue doesn't appear with the latest patch set.
On 10/22/19 6:27 PM, Alexander Duyck wrote:
[...]
Quoted 12 lines
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 on one node of a E5-2630 v3 CPU with 48G of RAM that I split up into two logical nodes in the guest in order to test with NUMA as well.
Test
page_fault1(THP)page_fault2
Baseline 1 1256106.33 +/-0.09% 482202.67 +/-0.46%16 8864441.67 +/-0.09% 3734692.00 +/-1.23%
Quoted 8 lines
Patches applied 1 1257096.00 +/-0.06% 477436.00 +/-0.16% 16 8864677.33 +/-0.06% 3800037.00 +/-0.19%Patches enabled 1 1258420.00 +/-0.04% 480080.00 +/-0.07% MADV disabled 16 8753840.00 +/-1.27% 3782764.00 +/-0.37%Patches enabled 1 1267916.33 +/-0.08% 472075.67 +/-0.39% 16 8287050.33 +/-0.67% 3774500.33 +/-0.11%
If I am not mistaken then you are only observing the number of processes (and not the number of threads) launched over the 1st and the 16th vcpu reported by will-it-scale?
Signature · 2 lines
Thanks Nitesh
Quoted 24 lines · Nitesh Narayan Lal
On 10/22/19 6:27 PM, Alexander Duyck wrote:
[...]
Quoted 18 lines
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 on one node of a E5-2630 v3 CPU with 48G of RAM that I split up into two logical nodes in the guest in order to test with NUMA as well.
Test
page_fault1(THP)page_fault2
Baseline 1 1256106.33 +/-0.09% 482202.67 +/-0.46%16 8864441.67 +/-0.09% 3734692.00 +/-1.23%Patches applied 1 1257096.00 +/-0.06% 477436.00 +/-0.16% 16 8864677.33 +/-0.06% 3800037.00 +/-0.19%Patches enabled 1 1258420.00 +/-0.04% 480080.00 +/-0.07%
MADV disabled 16 8753840.00 +/-1.27% 3782764.00 +/-0.37%
Quoted 6 lines
Quoted 2 lines
Patches enabled 1 1267916.33 +/-0.08% 472075.67 +/-0.39%
16 8287050.33 +/-0.67% 3774500.33 +/-0.11%If I am not mistaken then you are only observing the number of processes (and not the number of threads) launched over the 1st and the 16th vcpu reported by will-it-scale?
You are correct these results are for the processes. I monitored them for 1 - 16, but only included the results for 1 and 16 since those seem to be the most relevant data points.