2019-10-23 02:38:34

by Alexander Duyck

[permalink] [raw]
Subject: [PATCH v12 0/6] 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

--


2019-10-23 02:39:22

by Alexander Duyck

[permalink] [raw]
Subject: [PATCH v12 3/6] mm: Introduce Reported pages

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_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 <[email protected]>
---
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

diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index da289a3f8c5e..5ab34fab760e 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -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)
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
[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
diff --git a/mm/compaction.c b/mm/compaction.c
index 672d3c78c6ab..d20816b21b55 100644
--- a/mm/compaction.c
+++ b/mm/compaction.c
@@ -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;
diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
index 5e6b2a312362..aa86669ebcc5 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.
@@ -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);

diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index ea11c6f65157..f67846101bb6 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -74,6 +74,7 @@
#include <asm/div64.h>
#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);
@@ -1373,7 +1389,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);
@@ -1389,7 +1405,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);
}

@@ -2259,6 +2275,42 @@ 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
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 <linux/mmzone.h>
+#include <linux/pageblock-flags.h>
+#include <linux/page-isolation.h>
+#include <linux/jump_label.h>
+#include <linux/slab.h>
+#include <asm/pgtable.h>
+
+#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 */

2019-10-23 02:41:33

by Alexander Duyck

[permalink] [raw]
Subject: [PATCH v12 QEMU 1/3] virtio-ballon: Implement support for page poison tracking feature

From: 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.

Signed-off-by: Alexander Duyck <[email protected]>
---
hw/virtio/virtio-balloon.c | 25 +++++++++++++++++++++----
include/hw/virtio/virtio-balloon.h | 1 +
2 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/hw/virtio/virtio-balloon.c b/hw/virtio/virtio-balloon.c
index 40b04f518028..6ecfec422309 100644
--- a/hw/virtio/virtio-balloon.c
+++ b/hw/virtio/virtio-balloon.c
@@ -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.
diff --git a/include/hw/virtio/virtio-balloon.h b/include/hw/virtio/virtio-balloon.h
index d1c968d2376e..7fe78e5c14d7 100644
--- a/include/hw/virtio/virtio-balloon.h
+++ b/include/hw/virtio/virtio-balloon.h
@@ -70,6 +70,7 @@ typedef struct VirtIOBalloon {
uint32_t host_features;

bool qemu_4_0_config_size;
+ uint32_t poison_val;
} VirtIOBalloon;

#endif

2019-10-23 02:41:40

by Alexander Duyck

[permalink] [raw]
Subject: [PATCH v12 QEMU 2/3] virtio-balloon: Add bit to notify guest of unused page reporting

From: 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.

Signed-off-by: Alexander Duyck <[email protected]>
---
include/standard-headers/linux/virtio_balloon.h | 1 +
1 file changed, 1 insertion(+)

diff --git a/include/standard-headers/linux/virtio_balloon.h b/include/standard-headers/linux/virtio_balloon.h
index 9375ca2a70de..1c5f6d6f2de6 100644
--- a/include/standard-headers/linux/virtio_balloon.h
+++ b/include/standard-headers/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

2019-10-23 11:37:39

by Nitesh Narayan Lal

[permalink] [raw]
Subject: Re: [PATCH v12 0/6] mm / virtio: Provide support for unused page reporting


On 10/22/19 6:27 PM, Alexander Duyck wrote:
> 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
>
> --
>

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

2019-10-24 16:12:29

by Alexander Duyck

[permalink] [raw]
Subject: Re: [PATCH v12 0/6] mm / virtio: Provide support for unused page reporting

On Wed, 2019-10-23 at 07:35 -0400, Nitesh Narayan Lal wrote:
> On 10/22/19 6:27 PM, Alexander Duyck wrote:
> > 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>

> >
> 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]/

2019-10-28 21:09:41

by Alexander Duyck

[permalink] [raw]
Subject: Re: [PATCH v12 0/6] mm / virtio: Provide support for unused page reporting

On Mon, Oct 28, 2019 at 7:34 AM Nitesh Narayan Lal <[email protected]> wrote:
>
>
> On 10/22/19 6:27 PM, Alexander Duyck wrote:
>
>
> [...]
> > 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%
>
> 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.

2019-10-29 02:57:59

by Nitesh Narayan Lal

[permalink] [raw]
Subject: Re: [PATCH v12 0/6] mm / virtio: Provide support for unused page reporting


On 10/22/19 6:27 PM, Alexander Duyck wrote:


[...]
> 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%

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?

--
Thanks
Nitesh