Skip to contentSkip to conversation

mm / virtio: Provide support for free page reporting

PATCH Applied Alexander Duyck 5 participants 31 messages Feb 11, 2020 – Mar 11, 2021

Subsystems (8)
lore
Share & tools
lore
#0 lore raw

v17 cover mm / virtio: Provide support for free page reporting

This series provides an asynchronous means of reporting free guest pages to a hypervisor so that the memory associated with those pages can be dropped and reused by other processes and/or guests on the host. Using this it is possible to avoid unnecessary I/O to disk and greatly improve performance in the case of memory overcommit on the host.

When enabled we will be performing a scan of free memory every 2 seconds while pages of sufficiently high order are being freed. In each pass at least one sixteenth of each free list will be reported. By doing this we avoid racing against other threads that may be causing a high amount of memory churn.

The lowest page order currently scanned when reporting pages is pageblock_order so that this feature will not interfere with the use of Transparent Huge Pages in the case of virtualization.

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 free. It will be zeroed and 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. We walk though the free list isolating pages and adding them to the scatterlist until we either encounter the end of the list or have processed at least one sixteenth of the pages that were listed in nr_free prior to us starting. If we fill the scatterlist before we reach the end of the list we rotate the list so that the first unreported page we encounter is moved to the head of the list as that is where we will resume after we have freed the reported pages back into the tail of the list.

Below are the results from various benchmarks. I primarily focused on two tests. The first is the will-it-scale/page_fault2 test, and the other is a modified version of will-it-scale/page_fault1 that was enabled to use THP. I did this as it allows for better visibility into different parts of the memory subsystem. The guest is running with 32G for RAM on one node of a E5-2630 v3. The host has had some features such as CPU turbo disabled in the BIOS.

Test                   page_fault1 (THP)    page_fault2
Name            tasks  Process Iter  STDEV  Process Iter  STDEV
Baseline            1    1012402.50  0.14%     361855.25  0.81%
                   16    8827457.25  0.09%    3282347.00  0.34%
Patches Applied     1    1007897.00  0.23%     361887.00  0.26%
                   16    8784741.75  0.39%    3240669.25  0.48%
Patches Enabled     1    1010227.50  0.39%     359749.25  0.56%
                   16    8756219.00  0.24%    3226608.75  0.97%
Patches Enabled     1    1050982.00  4.26%     357966.25  0.14%
 page shuffle      16    8672601.25  0.49%    3223177.75  0.40%
Patches enabled     1    1003238.00  0.22%     360211.00  0.22%
 shuffle w/ RFC    16    8767010.50  0.32%    3199874.00  0.71%

The results above are for a baseline with a linux-next-20191219 kernel, that kernel with this patch set applied but page reporting disabled in virtio-balloon, the patches applied and page reporting fully enabled, the patches enabled with page shuffling enabled, and the patches applied with page shuffling enabled and an RFC patch that makes used of MADV_FREE in QEMU. 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 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.

Any of the overhead visible with this patch set enabled seems due to page faults caused by accessing the reported pages and the host zeroing the page before giving it back to the guest. This overhead is much more visible when using THP than with standard 4K pages. In addition page shuffling seemed to increase the amount of faults generated due to an increase in memory churn. The overehad is reduced when using MADV_FREE as we can avoid the extra zeroing of the pages when they are reintroduced to the host, as can be seen when the RFC is applied with shuffling enabled.

The overall guest size is kept fairly small to only a few GB while the test is running. If the host memory were oversubscribed this patch set should result in a performance improvement as swapping memory in the host can be avoided.

A brief history on the background of free page reporting can be found at:
https://lore.kernel.org/lkml/[email protected]/

Changes from v14:
https://lore.kernel.org/lkml/[email protected]/
Renamed "unused page reporting" to "free page reporting"
Updated code, kconfig, and patch descriptions
Split out patch for __free_isolated_page
Renamed function to __putback_isolated_page
Rewrote core reporting functionality
Added logic to reschedule worker in 2 seconds instead of run to completion Removed reported_pages statistics
Removed REPORTING_REQUESTED bit used in zone flags
Replaced page_reporting_dev_info refcount with state variable Removed scatterlist from page_reporting_dev_info
Removed capacity from page reporting device
Added dynamic scatterlist allocation/free at start/end of reporting process Updated __free_one_page so that reported pages are not always added to tail Added logic to handle error from report function
Updated virtio-balloon patch that adds support for page reporting
Updated patch description to try and highlight differences in approaches Updated logic to reflect that we cannot limit the scatterlist from device Added logic to return error from report function
Moved documentation patch to end of patch set

Changes from v15:
https://lore.kernel.org/lkml/[email protected]/
Rebased on linux-next-20191219
Split out patches for budget and moving head to last page processed Updated budget code to reduce how much memory is reported per pass Added logic to also rotate the list if we exit due a page isolation failure Added migratetype as argument in __putback_isolated_page

Changes from v16:
https://lore.kernel.org/lkml/[email protected]/
Rebased on linux-next-20200122
Updated patch 2 to to account for removal of pr_info in __isolate_free_page
Updated patch title for patches 7, 8, and 9 to included mm/page_reporting

Changes from v16.1:
https://lore.kernel.org/lkml/[email protected]/
Rebased QEMU patches to latest
Rebased on linux-next-20200211
Tweaked cover page to more accurately describe hinting process Verified results have not changed in significant way

---

Alexander Duyck (9):
mm: Adjust shuffle code to allow for future coalescing
mm: Use zone and order instead of free area in free_list manipulators
mm: Add function __putback_isolated_page
mm: Introduce Reported pages
virtio-balloon: Pull page poisoning config out of free page hinting
virtio-balloon: Add support for providing free page reports to host mm/page_reporting: Rotate reported pages to the tail of the list mm/page_reporting: Add budget limit on how many pages can be reported per pass mm/page_reporting: Add free page reporting documentation

 Documentation/vm/free_page_reporting.rst |   41 +++
 drivers/virtio/Kconfig                   |    1 
 drivers/virtio/virtio_balloon.c          |   87 +++++++
 include/linux/mmzone.h                   |   44 ----
 include/linux/page-flags.h               |   11 +
 include/linux/page_reporting.h           |   26 ++
 include/uapi/linux/virtio_balloon.h      |    1 
 mm/Kconfig                               |   11 +
 mm/Makefile                              |    1 
 mm/internal.h                            |    2 
 mm/page_alloc.c                          |  164 ++++++++++----
 mm/page_isolation.c                      |    6 
 mm/page_reporting.c                      |  364 ++++++++++++++++++++++++++++++
 mm/page_reporting.h                      |   54 ++++
 mm/shuffle.c                             |   12 -
 mm/shuffle.h                             |    6 
 16 files changed, 725 insertions(+), 106 deletions(-)
 create mode 100644 Documentation/vm/free_page_reporting.rst
 create mode 100644 include/linux/page_reporting.h
 create mode 100644 mm/page_reporting.c
 create mode 100644 mm/page_reporting.h

--

#1 lore raw

PATCH v17 1/9 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
index 462f6873905a..bdcd071ab67f 100644
@@ -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
index b3fe97fd6654..e65d57f39486 100644
@@ -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
index 777a257a0d2f..4d79f03b6658 100644
@@ -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)
 {
 }
#2 lore raw

Re: [PATCH v17 1/9] mm: Adjust shuffle code to allow for future coalescing

Hi Alexander,

My team was evaluating FPR and observed that these patches don’t report memory for deallocated hugeapages directly and need to cycle through buddy allocator. For example, say we need to allocate a maximum of 12 * 1G hugepages (by setting nr_hugepages), use 8 * 1G hugepages, and then deallocate 4 * 1G hugepages. Unlike regular 4K pages, this 4G worth of memory will not be reported until we set nr_hugepages to 8 (wait sometime(?) for FPR to do its work) and set it back again to 12. While this works fine in theory, in practice, setting nr_hugepages to 12 could fail too due to fragmentation (this could depend on other processes memory usage behavior).

If FPR could report this free memory without cycling through buddy allocator, it makes the solution more robust. I am looking for advice on how feasible this approach is and what would be the effort for building this functionality. In general, if there are other thoughts on how we can address this, please do let me know.

Thanks,
bala

#3 lore raw

Re: [PATCH v17 1/9] mm: Adjust shuffle code to allow for future coalescing

Hi Bala,

There was a similar effort several months ago that was trying to do this in conjunction with pre-zeroing of pages. I suspect if you wanted to you could probably pick up some of their patch set and work with that. It can be found at:
https://www.spinics.net/lists/linux-mm/msg239735.html

Thanks.

- Alex

Quoted 16 lines · Balasubramaniam

Hi Alexander,

My team was evaluating FPR and observed that these patches don’t report memory for deallocated hugeapages directly and need to cycle through buddy allocator. For example, say we need to allocate a maximum of 12 * 1G hugepages (by setting nr_hugepages), use 8 * 1G hugepages, and then deallocate 4 * 1G hugepages. Unlike regular 4K pages, this 4G worth of memory will not be reported until we set nr_hugepages to 8 (wait sometime(?) for FPR to do its work) and set it back again to 12. While this works fine in theory, in practice, setting nr_hugepages to 12 could fail too due to fragmentation (this could depend on other processes memory usage behavior).

If FPR could report this free memory without cycling through buddy allocator, it makes the solution more robust. I am looking for advice on how feasible this approach is and what would be the effort for building this functionality. In general, if there are other thoughts on how we can address this, please do let me know.

Thanks,

bala

#4 lore raw

PATCH v17 2/9 mm: Use zone and order instead of free area in free_list manipulators

In reply to 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.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
index bdcd071ab67f..a32bd503b9fc 100644
@@ -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

#5 lore raw

PATCH v17 3/9 mm: Add function __putback_isolated_page

In reply to Alexander Duyck

From: Alexander Duyck <[email protected]>

There are cases where we would benefit from avoiding having to go through the allocation and free cycle to return an isolated page.

Examples for this might include page poisoning in which we isolate a page and then put it back in the free list without ever having actually allocated it.

This will enable us to also avoid notifiers for the future free page reporting which will need to avoid retriggering page reporting when returning pages that have been reported on.

---
 mm/internal.h       |    2 ++
 mm/page_alloc.c     |   19 +++++++++++++++++++
 mm/page_isolation.c |    6 ++----
 3 files changed, 23 insertions(+), 4 deletions(-)
mm/internal.h +2 -0
index 3cf20ab3ca01..7b108222e5f4 100644
@@ -157,6 +157,8 @@ static inline struct page *pageblock_pfn_to_page(unsigned long start_pfn,
 }
 
 extern int __isolate_free_page(struct page *page, unsigned int order);
+extern void __putback_isolated_page(struct page *page, unsigned int order,
+				    int mt);
 extern void memblock_free_pages(struct page *page, unsigned long pfn,
 					unsigned int order);
 extern void __free_pages_core(struct page *page, unsigned int order);
mm/page_alloc.c +19 -0
index de916aa06a27..b711fc0159d9 100644
@@ -3217,6 +3217,25 @@ int __isolate_free_page(struct page *page, unsigned int order)
 	return 1UL << order;
 }
 
+/**
+ * __putback_isolated_page - Return a now-isolated page back where we got it
+ * @page: Page that was isolated
+ * @order: Order of the isolated page
+ *
+ * This function is meant to return a page pulled from the free lists via
+ * __isolate_free_page back to the free lists they were pulled from.
+ */
+void __putback_isolated_page(struct page *page, unsigned int order, int mt)
+{
+	struct zone *zone = page_zone(page);
+
+	/* zone lock should be held when this function is called */
+	lockdep_assert_held(&zone->lock);
+
+	/* Return isolated page to tail of freelist. */
+	__free_one_page(page, page_to_pfn(page), zone, order, mt);
+}
+
 /*
  * Update NUMA hit/miss statistics
  *
mm/page_isolation.c +2 -4
index a9fd7c740c23..2c11a38d6e87 100644
@@ -117,13 +117,11 @@ static void unset_migratetype_isolate(struct page *page, unsigned migratetype)
 		__mod_zone_freepage_state(zone, nr_pages, migratetype);
 	}
 	set_pageblock_migratetype(page, migratetype);
+	if (isolated_page)
+		__putback_isolated_page(page, order, migratetype);
 	zone->nr_isolate_pageblock--;
 out:
 	spin_unlock_irqrestore(&zone->lock, flags);
-	if (isolated_page) {
-		post_alloc_hook(page, order, __GFP_MOVABLE);
-		__free_pages(page, order);
-	}
 }
 
 static inline struct page *
Acked-by on v17 3/9
#6 lore raw

Re: [PATCH v17 3/9] mm: Add function __putback_isolated_page

Quoted 9 lines · Alexander Duyck

From: Alexander Duyck <[email protected]>

There are cases where we would benefit from avoiding having to go through the allocation and free cycle to return an isolated page.

Examples for this might include page poisoning in which we isolate a page and then put it back in the free list without ever having actually allocated it.

Quoted 6 lines

This will enable us to also avoid notifiers for the future free page reporting which will need to avoid retriggering page reporting when returning pages that have been reported on.

Ok, the prior code that used post_alloc_hook to make the isolated page seem like a normally allocated page followed by a free seems strange anyway. As well as being expensive, isolated pages can end up on the per-cpu lists which is probably not what is desired. I *think* what you've done is ok so

Signature · 2 lines
Mel Gorman
SUSE Labs
#7 lore raw

PATCH v17 4/9 mm: Introduce Reported pages

In reply to Alexander Duyck

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.

To prevent the reported pages from leaking outside of the buddy lists I added a check to clear the PageReported bit in the del_page_from_free_list function. As a result any reported page that is split, merged, or allocated will have the flag cleared prior to the PageBuddy value being cleared.

The process for reporting pages is fairly simple. Once we free a page that meets the minimum order for page reporting we will schedule a worker thread to start 2s or more in the future. That worker thread will begin working from the lowest supported page reporting order up to MAX_ORDER - 1 pulling unreported pages from the free list and storing them in the scatterlist.

When processing each individual free list it is necessary for the worker thread to release the zone lock when it needs to stop and report the full scatterlist of pages. To reduce the work of the next iteration the worker thread will rotate the free list so that the first unreported page in the free list becomes the first entry in the list.

It will then call a reporting function providing information on how many entries are in the scatterlist. Once the function completes it will return the pages to the free area from which they were allocated and start over pulling more pages from the free areas until there are no longer enough pages to report on to keep the worker busy, or we have processed as many pages as were contained in the free area when we started processing the list.

The worker thread will work in a round-robin fashion making its way though each zone requesting reporting, and through each reportable free list within that zone. Once all free areas within the zone have been processed it will check to see if there have been any requests for reporting while it was processing. If so it will reschedule the worker thread to start up again in roughly 2s and exit.

---
 include/linux/page-flags.h     |   11 +
 include/linux/page_reporting.h |   25 +++
 mm/Kconfig                     |   11 +
 mm/Makefile                    |    1 
 mm/page_alloc.c                |   17 ++
 mm/page_reporting.c            |  319 ++++++++++++++++++++++++++++++++++++++++
 mm/page_reporting.h            |   54 +++++++
 7 files changed, 434 insertions(+), 4 deletions(-)
 create mode 100644 include/linux/page_reporting.h
 create mode 100644 mm/page_reporting.c
 create mode 100644 mm/page_reporting.h
include/linux/page-flags.h +11 -0
index 1bf83c8fcaa7..49c2697046b9 100644
@@ -163,6 +163,9 @@ enum pageflags {
 
 	/* non-lru isolated movable page */
 	PG_isolated = PG_reclaim,
+
+	/* Only valid for buddy pages. Used to track pages that are 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.
include/linux/page_reporting.h new file +25 -0
new file mode 100644
index 000000000000..32355486f572
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_PAGE_REPORTING_H
+#define _LINUX_PAGE_REPORTING_H
+
+#include <linux/mmzone.h>
+#include <linux/scatterlist.h>
+
+#define PAGE_REPORTING_CAPACITY		32
+
+struct page_reporting_dev_info {
+	/* function that alters pages to make them "reported" */
+	int (*report)(struct page_reporting_dev_info *prdev,
+		      struct scatterlist *sg, unsigned int nents);
+
+	/* work struct for processing reports */
+	struct delayed_work work;
+
+	/* Current state of page reporting */
+	atomic_t state;
+};
+
+/* Tear-down and bring-up for page reporting devices */
+void page_reporting_unregister(struct page_reporting_dev_info *prdev);
+int page_reporting_register(struct page_reporting_dev_info *prdev);
+#endif /*_LINUX_PAGE_REPORTING_H */
mm/Kconfig +11 -0
index ab80933be65f..d40a873402ff 100644
@@ -237,6 +237,17 @@ config COMPACTION
 	  [email protected].
 
 #
+# support for free page reporting
+config PAGE_REPORTING
+	bool "Free page reporting"
+	def_bool n
+	help
+	  Free page reporting allows for the incremental acquisition of
+	  free 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 within the host for other uses.
+
+#
 # support for page migration
 #
 config MIGRATION
mm/Makefile +1 -0
index c9696f3ec840..7b5eec34d0e9 100644
@@ -118,3 +118,4 @@ obj-$(CONFIG_HMM_MIRROR) += hmm.o
 obj-$(CONFIG_MEMFD_CREATE) += memfd.o
 obj-$(CONFIG_MAPPING_DIRTY_HELPERS) += mapping_dirty_helpers.o
 obj-$(CONFIG_PTDUMP_CORE) += ptdump.o
+obj-$(CONFIG_PAGE_REPORTING) += page_reporting.o
mm/page_alloc.c +13 -4
index b711fc0159d9..1142b2f91377 100644
@@ -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);
@@ -902,6 +903,10 @@ static inline void move_to_free_list(struct page *page, struct zone *zone,
 static inline void del_page_from_free_list(struct page *page, struct zone *zone,
 					   unsigned int order)
 {
+	/* clear reported state and update reported page count */
+	if (page_reported(page))
+		__ClearPageReported(page);
+
 	list_del(&page->lru);
 	__ClearPageBuddy(page);
 	set_page_private(page, 0);
@@ -965,7 +970,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 report)
 {
 	struct capture_control *capc = task_capc(zone);
 	unsigned long uninitialized_var(buddy_pfn);
@@ -1050,6 +1055,10 @@ static inline void __free_one_page(struct page *page,
 		add_to_free_list_tail(page, zone, order, migratetype);
 	else
 		add_to_free_list(page, zone, order, migratetype);
+
+	/* Notify page reporting subsystem of freed page */
+	if (report)
+		page_reporting_notify_free(order);
 }
 
 /*
@@ -1366,7 +1375,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, true);
 		trace_mm_page_pcpu_drain(page, 0, mt);
 	}
 	spin_unlock(&zone->lock);
@@ -1382,7 +1391,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, true);
 	spin_unlock(&zone->lock);
 }
 
@@ -3233,7 +3242,7 @@ void __putback_isolated_page(struct page *page, unsigned int order, int mt)
 	lockdep_assert_held(&zone->lock);
 
 	/* Return isolated page to tail of freelist. */
-	__free_one_page(page, page_to_pfn(page), zone, order, mt);
+	__free_one_page(page, page_to_pfn(page), zone, order, mt, false);
 }
 
 /*
mm/page_reporting.c new file +319 -0

319 lines not on this page · raw message

mm/page_reporting.h new file +54 -0

54 lines not on this page · raw message

Acked-by on v17 4/9
#8 lore raw

Re: [PATCH v17 4/9] mm: Introduce Reported pages

Quoted 9 lines · Alexander Duyck
mm/page_reporting.c new file +3 -0
new file mode 100644
index 000000000000..1047c6872d4f
@@ -0,0 +1,319 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/mm.h>
+#include <linux/mmzone.h>

mm/page_reporting.c line 13

Quoted 10 lines
mm/page_reporting.c new file +10 -0
+#include <linux/page_reporting.h>
+#include <linux/gfp.h>
+#include <linux/export.h>
+#include <linux/delay.h>
+#include <linux/scatterlist.h>
+
+#include "page_reporting.h"
+#include "internal.h"
+
+#define PAGE_REPORTING_DELAY	(2 * HZ)

I assume there is nothing special about 2 seconds other than "do some progress every so often".

Quoted 24 lines
mm/page_reporting.c new file +24 -0
+static struct page_reporting_dev_info __rcu *pr_dev_info __read_mostly;
+
+enum {
+	PAGE_REPORTING_IDLE = 0,
+	PAGE_REPORTING_REQUESTED,
+	PAGE_REPORTING_ACTIVE
+};
+
+/* request page reporting */
+static void
+__page_reporting_request(struct page_reporting_dev_info *prdev)
+{
+	unsigned int state;
+
+	/* Check to see if we are in desired state */
+	state = atomic_read(&prdev->state);
+	if (state == PAGE_REPORTING_REQUESTED)
+		return;
+
+	/*
+	 *  If reporting is already active there is nothing we need to do.
+	 *  Test against 0 as that represents PAGE_REPORTING_IDLE.
+	 */
+	state = atomic_xchg(&prdev->state, PAGE_REPORTING_REQUESTED);

mm/page_reporting.c line 47

Quoted 10 lines
mm/page_reporting.c new file +10 -0
+	if (state != PAGE_REPORTING_IDLE)
+		return;
+
+	/*
+	 * Delay the start of work to allow a sizable queue to build. For
+	 * now we are limiting this to running no more than once every
+	 * couple of seconds.
+	 */
+	schedule_delayed_work(&prdev->work, PAGE_REPORTING_DELAY);
+}

Seems a fair use of atomics.

Quoted 20 lines
mm/page_reporting.c new file +20 -0
+static int
+page_reporting_cycle(struct page_reporting_dev_info *prdev, struct zone *zone,
+		     unsigned int order, unsigned int mt,
+		     struct scatterlist *sgl, unsigned int *offset)
+{
+	struct free_area *area = &zone->free_area[order];
+	struct list_head *list = &area->free_list[mt];
+	unsigned int page_len = PAGE_SIZE << order;
+	struct page *page, *next;
+	int err = 0;
+
+	/*
+	 * Perform early check, if free area is empty there is
+	 * nothing to process so we can skip this free_list.
+	 */
+	if (list_empty(list))
+		return err;
+
+	spin_lock_irq(&zone->lock);
+

mm/page_reporting.c line 137

Quoted 10 lines
mm/page_reporting.c new file +10 -0
+	/* loop through free list adding unreported pages to sg list */
+	list_for_each_entry_safe(page, next, list, lru) {
+		/* We are going to skip over the reported pages. */
+		if (PageReported(page))
+			continue;
+
+		/* Attempt to pull page from list */
+		if (!__isolate_free_page(page, order))
+			break;
+

Might want to note that you are breaking because the only reason to fail the isolation is that watermarks are not met and we are likely under memory pressure. It's not a big issue.

However, while I think this is correct, it's hard to follow. This loop can be broken out of with pages still on the scatter gather list. The current flow guarantees that err will not be set at this point so the caller cleans it up so we always drain the list either here or in the caller.

While I think it works, it's a bit fragile. I recommend putting a comment above this noting why it's safe and put a VM_WARN_ON_ONCE(err) before the break in case someone tries to change this in a years time and does not spot that the flow to reach page_reporting_drain *somewhere* is critical.

Quoted 28 lines
mm/page_reporting.c new file +28 -0
+		/* Add page to scatter list */
+		--(*offset);
+		sg_set_page(&sgl[*offset], page, page_len, 0);
+
+		/* If scatterlist isn't full grab more pages */
+		if (*offset)
+			continue;
+
+		/* release lock before waiting on report processing */
+		spin_unlock_irq(&zone->lock);
+
+		/* begin processing pages in local list */
+		err = prdev->report(prdev, sgl, PAGE_REPORTING_CAPACITY);
+
+		/* reset offset since the full list was reported */
+		*offset = PAGE_REPORTING_CAPACITY;
+
+		/* reacquire zone lock and resume processing */
+		spin_lock_irq(&zone->lock);
+
+		/* flush reported pages from the sg list */
+		page_reporting_drain(prdev, sgl, PAGE_REPORTING_CAPACITY, !err);
+
+		/*
+		 * Reset next to first entry, the old next isn't valid
+		 * since we dropped the lock to report the pages
+		 */
+		next = list_first_entry(list, struct page, lru);

mm/page_reporting.c line 175

Quoted 10 lines
mm/page_reporting.c new file +10 -0
+
+		/* exit on error */
+		if (err)
+			break;
+	}
+
+	spin_unlock_irq(&zone->lock);
+
+	return err;
+}

I complained about the use of zone lock before but in this version, I think I'm ok with it. The lock is held for the free list manipulations which is what it's for. The state management with atomics seems reasonable.

Otherwise I think this is ok and I think the implementation right. Of great importance to me was the allocator fast paths but they seem to be adequately protected by a static branch so

The ack applies regardless of whether you decide to document and defensively protect page_reporting_cycle against losing pages on the scatter/gather list but I do recommend it.

Signature · 2 lines
Mel Gorman
SUSE Labs
#9 lore raw

Re: [PATCH v17 4/9] mm: Introduce Reported pages

Quoted 15 lines · Mel Gorman
Quoted 14 lines · Alexander Duyck
mm/page_reporting.c new file +8 -0
new file mode 100644
index 000000000000..1047c6872d4f
@@ -0,0 +1,319 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/mm.h>
+#include <linux/mmzone.h>
+#include <linux/page_reporting.h>
+#include <linux/gfp.h>
+#include <linux/export.h>
+#include <linux/delay.h>
+#include <linux/scatterlist.h>
Quoted 8 lines

mm/page_reporting.c line 13

Quoted 5 lines
mm/page_reporting.c new file +5 -0
+
+#include "page_reporting.h"
+#include "internal.h"
+
+#define PAGE_REPORTING_DELAY	(2 * HZ)

I assume there is nothing special about 2 seconds other than "do some progress every so often".

Yes, nothing special. I played around with a few different values. I just settled on 2 seconds as I figured with that and 1/16 of the list per pass it came out to about 30 seconds which I felt is about the right time for a fully utilized system to settle back to the inactive state.

Quoted 36 lines
Quoted 21 lines
mm/page_reporting.c new file +20 -0
+static int
+page_reporting_cycle(struct page_reporting_dev_info *prdev, struct zone *zone,
+		     unsigned int order, unsigned int mt,
+		     struct scatterlist *sgl, unsigned int *offset)
+{
+	struct free_area *area = &zone->free_area[order];
+	struct list_head *list = &area->free_list[mt];
+	unsigned int page_len = PAGE_SIZE << order;
+	struct page *page, *next;
+	int err = 0;
+
+	/*
+	 * Perform early check, if free area is empty there is
+	 * nothing to process so we can skip this free_list.
+	 */
+	if (list_empty(list))
+		return err;
+
+	spin_lock_irq(&zone->lock);
+

mm/page_reporting.c line 137

Quoted 10 lines
mm/page_reporting.c new file +10 -0
+	/* loop through free list adding unreported pages to sg list */
+	list_for_each_entry_safe(page, next, list, lru) {
+		/* We are going to skip over the reported pages. */
+		if (PageReported(page))
+			continue;
+
+		/* Attempt to pull page from list */
+		if (!__isolate_free_page(page, order))
+			break;
+

Might want to note that you are breaking because the only reason to fail the isolation is that watermarks are not met and we are likely under memory pressure. It's not a big issue.

Quoted 4 lines

However, while I think this is correct, it's hard to follow. This loop can be broken out of with pages still on the scatter gather list. The current flow guarantees that err will not be set at this point so the caller cleans it up so we always drain the list either here or in the caller.

I can probably submit a follow-up patch to update the comments. The reason for not returning an error is because I didn't consider it an error that we encountered the watermark and were not able to pull any more pages. Instead I considered that the "stop" point for this pass and have it just exit out of the loop and flush the data.

At the start of the next pass we will check against the low watermark instead of the minimum watermark and if that check fails we will simply stop reporting pages for the zone until additional pages are freed.

I can probably also update the description for page_reporting_cycle since it may not be clear that the output for this is a partially filled in- progress scatterlist so we always have to reporting any remaining entries at the end of processing a given zone. It might make more sense if I move the bits related to "leftover" in page_reporting_process_zone into their own function.

Quoted 4 lines

While I think it works, it's a bit fragile. I recommend putting a comment above this noting why it's safe and put a VM_WARN_ON_ONCE(err) before the break in case someone tries to change this in a years time and does not spot that the flow to reach page_reporting_drain *somewhere* is critical.

I assume this isn't about this section, but the section below?

Quoted 6 lines
Quoted 6 lines
mm/page_reporting.c new file +6 -0
+		/* Add page to scatter list */
+		--(*offset);
+		sg_set_page(&sgl[*offset], page, page_len, 0);
+
+		/* If scatterlist isn't full grab more pages */
+		if (*offset)
Quoted 8 lines

mm/page_reporting.c line 151

Quoted 8 lines
mm/page_reporting.c new file +8 -0
+			continue;
+
+		/* release lock before waiting on report processing */
+		spin_unlock_irq(&zone->lock);
+
+		/* begin processing pages in local list */
+		err = prdev->report(prdev, sgl, PAGE_REPORTING_CAPACITY);
+

So one thing I can do is probably add a comment here as well to more thoroughly explain the reason why we wait to call the break until we are in the block below.

Quoted 10 lines
Quoted 10 lines
mm/page_reporting.c new file +10 -0
+		/* reset offset since the full list was reported */
+		*offset = PAGE_REPORTING_CAPACITY;
+
+		/* reacquire zone lock and resume processing */
+		spin_lock_irq(&zone->lock);
+
+		/* flush reported pages from the sg list */
+		page_reporting_drain(prdev, sgl, PAGE_REPORTING_CAPACITY, !err);
+
+		/*
Quoted 8 lines

mm/page_reporting.c line 169

Quoted 8 lines
mm/page_reporting.c new file +8 -0
+		 * Reset next to first entry, the old next isn't valid
+		 * since we dropped the lock to report the pages
+		 */
+		next = list_first_entry(list, struct page, lru);
+
+		/* exit on error */
+		if (err)
+			break;

And I assume you meant to add the VM_WARN_ON_ONCE here? The statement above wouldn't make much sense since err would always be 0.

Quoted 16 lines

mm/page_reporting.c line 175

Quoted 6 lines
mm/page_reporting.c new file +6 -0
+	}
+
+	spin_unlock_irq(&zone->lock);
+
+	return err;
+}

I complained about the use of zone lock before but in this version, I think I'm ok with it. The lock is held for the free list manipulations which is what it's for. The state management with atomics seems reasonable.

Otherwise I think this is ok and I think the implementation right. Of great importance to me was the allocator fast paths but they seem to be adequately protected by a static branch so

Quoted 5 lines

The ack applies regardless of whether you decide to document and defensively protect page_reporting_cycle against losing pages on the scatter/gather list but I do recommend it.

Thanks for reviewing this. I appreciate the feedback.

- Alex

#10 lore raw

Re: [PATCH v17 4/9] mm: Introduce Reported pages

Quoted 40 lines · Alexander Duyck
Quoted 35 lines
Quoted 20 lines
mm/page_reporting.c new file +20 -0
+static int
+page_reporting_cycle(struct page_reporting_dev_info *prdev, struct zone *zone,
+		     unsigned int order, unsigned int mt,
+		     struct scatterlist *sgl, unsigned int *offset)
+{
+	struct free_area *area = &zone->free_area[order];
+	struct list_head *list = &area->free_list[mt];
+	unsigned int page_len = PAGE_SIZE << order;
+	struct page *page, *next;
+	int err = 0;
+
+	/*
+	 * Perform early check, if free area is empty there is
+	 * nothing to process so we can skip this free_list.
+	 */
+	if (list_empty(list))
+		return err;
+
+	spin_lock_irq(&zone->lock);
+

mm/page_reporting.c line 137

Quoted 10 lines
mm/page_reporting.c new file +10 -0
+	/* loop through free list adding unreported pages to sg list */
+	list_for_each_entry_safe(page, next, list, lru) {
+		/* We are going to skip over the reported pages. */
+		if (PageReported(page))
+			continue;
+
+		/* Attempt to pull page from list */
+		if (!__isolate_free_page(page, order))
+			break;
+

Might want to note that you are breaking because the only reason to fail the isolation is that watermarks are not met and we are likely under memory pressure. It's not a big issue.

Quoted 4 lines

However, while I think this is correct, it's hard to follow. This loop can be broken out of with pages still on the scatter gather list. The current flow guarantees that err will not be set at this point so the caller cleans it up so we always drain the list either here or in the caller.

Quoted 6 lines

I can probably submit a follow-up patch to update the comments. The reason for not returning an error is because I didn't consider it an error that we encountered the watermark and were not able to pull any more pages. Instead I considered that the "stop" point for this pass and have it just exit out of the loop and flush the data.

I don't consider it an error and I don't think you should return an error. The comment just needs to explain that the draining happens in the caller in this case. That should be enough of a warning to a future developer to double check the flow after any changes to make sure the drain is reached.

Quoted 7 lines
Quoted 4 lines

While I think it works, it's a bit fragile. I recommend putting a comment above this noting why it's safe and put a VM_WARN_ON_ONCE(err) before the break in case someone tries to change this in a years time and does not spot that the flow to reach page_reporting_drain *somewhere* is critical.

I assume this isn't about this section, but the section below?

I meant something like

if (!__isolate_free_page(page, order)) {
	VM_WARN_ON_ONCE(err);
	break;
}

Because at this point it's possible there are entries that should go through page_reporting_drain() but the caller will not call page_reporting_drain() in the event of an error.

Signature · 2 lines
Mel Gorman
SUSE Labs
#11 lore raw

Re: [PATCH v17 4/9] mm: Introduce Reported pages

Quoted 48 lines · Mel Gorman
Quoted 40 lines · Alexander Duyck
Quoted 35 lines
Quoted 20 lines
mm/page_reporting.c new file +20 -0
+static int
+page_reporting_cycle(struct page_reporting_dev_info *prdev, struct zone *zone,
+		     unsigned int order, unsigned int mt,
+		     struct scatterlist *sgl, unsigned int *offset)
+{
+	struct free_area *area = &zone->free_area[order];
+	struct list_head *list = &area->free_list[mt];
+	unsigned int page_len = PAGE_SIZE << order;
+	struct page *page, *next;
+	int err = 0;
+
+	/*
+	 * Perform early check, if free area is empty there is
+	 * nothing to process so we can skip this free_list.
+	 */
+	if (list_empty(list))
+		return err;
+
+	spin_lock_irq(&zone->lock);
+

mm/page_reporting.c line 137

Quoted 10 lines
mm/page_reporting.c new file +10 -0
+	/* loop through free list adding unreported pages to sg list */
+	list_for_each_entry_safe(page, next, list, lru) {
+		/* We are going to skip over the reported pages. */
+		if (PageReported(page))
+			continue;
+
+		/* Attempt to pull page from list */
+		if (!__isolate_free_page(page, order))
+			break;
+

Might want to note that you are breaking because the only reason to fail the isolation is that watermarks are not met and we are likely under memory pressure. It's not a big issue.

Quoted 4 lines

However, while I think this is correct, it's hard to follow. This loop can be broken out of with pages still on the scatter gather list. The current flow guarantees that err will not be set at this point so the caller cleans it up so we always drain the list either here or in the caller.

Quoted 6 lines

I can probably submit a follow-up patch to update the comments. The reason for not returning an error is because I didn't consider it an error that we encountered the watermark and were not able to pull any more pages. Instead I considered that the "stop" point for this pass and have it just exit out of the loop and flush the data.

Quoted 5 lines

I don't consider it an error and I don't think you should return an error. The comment just needs to explain that the draining happens in the caller in this case. That should be enough of a warning to a future developer to double check the flow after any changes to make sure the drain is reached.

The comment I can do, that shouldn't be an issue. The point I was getting at is that a separate drain call is expected for this any time the function is not returning an error, and the only way it can return an error is if there was a reporting issue.

Quoted 10 lines
Quoted 7 lines
Quoted 4 lines

While I think it works, it's a bit fragile. I recommend putting a comment above this noting why it's safe and put a VM_WARN_ON_ONCE(err) before the break in case someone tries to change this in a years time and does not spot that the flow to reach page_reporting_drain *somewhere* is critical.

I assume this isn't about this section, but the section below?

I meant something like

Quoted 8 lines
if (!__isolate_free_page(page, order)) {
	VM_WARN_ON_ONCE(err);
	break;
}

Because at this point it's possible there are entries that should go through page_reporting_drain() but the caller will not call page_reporting_drain() in the event of an error.

I would think adding that would confuse things even more. There is a break statement at the end of the loop that will break out if err is set. So we should never hit the VM_WARN_ON_ONCE because err should always be 0 before we even attempt to isolate the page. I think something like the following would probably make more sense:

        err = page_reporting_cycle(prdev, zone, order, mt,
                                   sgl, &offset);
        if (err) {
                /*
                 * We should have drained the scatterlist
                 * prior to exiting page_reporting_cycle if
                 * we encountered an error. If we did not
                 * then this could result in a memory leak.
                 * Verify that the end of the scatterlist
                 * was cleared prior to us getting here.
                 */
                sgl = &sgl[PAGE_REPORTING_CAPACITY - 1];
                VM_WARN_ON_ONCE(sg_page(sgl));
                return err;
        }

With that we are more-or-less making certain that they called page_reporting_drain which will zero the scatterlist.

#12 lore raw

Re: [PATCH v17 4/9] mm: Introduce Reported pages

Quoted 55 lines · Alexander Duyck
Quoted 48 lines · Mel Gorman
Quoted 40 lines · Alexander Duyck
Quoted 35 lines
Quoted 20 lines
mm/page_reporting.c new file +20 -0
+static int
+page_reporting_cycle(struct page_reporting_dev_info *prdev, struct zone *zone,
+		     unsigned int order, unsigned int mt,
+		     struct scatterlist *sgl, unsigned int *offset)
+{
+	struct free_area *area = &zone->free_area[order];
+	struct list_head *list = &area->free_list[mt];
+	unsigned int page_len = PAGE_SIZE << order;
+	struct page *page, *next;
+	int err = 0;
+
+	/*
+	 * Perform early check, if free area is empty there is
+	 * nothing to process so we can skip this free_list.
+	 */
+	if (list_empty(list))
+		return err;
+
+	spin_lock_irq(&zone->lock);
+

mm/page_reporting.c line 137

Quoted 10 lines
mm/page_reporting.c new file +10 -0
+	/* loop through free list adding unreported pages to sg list */
+	list_for_each_entry_safe(page, next, list, lru) {
+		/* We are going to skip over the reported pages. */
+		if (PageReported(page))
+			continue;
+
+		/* Attempt to pull page from list */
+		if (!__isolate_free_page(page, order))
+			break;
+

Might want to note that you are breaking because the only reason to fail the isolation is that watermarks are not met and we are likely under memory pressure. It's not a big issue.

Quoted 4 lines

However, while I think this is correct, it's hard to follow. This loop can be broken out of with pages still on the scatter gather list. The current flow guarantees that err will not be set at this point so the caller cleans it up so we always drain the list either here or in the caller.

Quoted 6 lines

I can probably submit a follow-up patch to update the comments. The reason for not returning an error is because I didn't consider it an error that we encountered the watermark and were not able to pull any more pages. Instead I considered that the "stop" point for this pass and have it just exit out of the loop and flush the data.

Quoted 5 lines

I don't consider it an error and I don't think you should return an error. The comment just needs to explain that the draining happens in the caller in this case. That should be enough of a warning to a future developer to double check the flow after any changes to make sure the drain is reached.

Quoted 5 lines

The comment I can do, that shouldn't be an issue. The point I was getting at is that a separate drain call is expected for this any time the function is not returning an error, and the only way it can return an error is if there was a reporting issue.

I'm not suggesting you return an error. I'm suggesting you put a warn in before you break due to watermarks *if* there is an error. It should *never* trigger unless someone modifies the flow and breaks it in which case the warning will not kill the system but give a strong hint to the developer that they need to think a bit more.

It's ok to leave it out because at this point, it's a distraction and I do not see a problem with the current code.

Signature · 2 lines
Mel Gorman
SUSE Labs
#13 lore raw

PATCH v17 5/9 virtio-balloon: Pull page poisoning config out of free page hinting

In reply to Alexander Duyck

From: 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 free 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
index 7bfe365d9372..98d09825f2e2 100644
@@ -864,7 +864,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) {
@@ -930,11 +929,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 balloon 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
@@ -1045,7 +1053,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);
#14 lore raw

PATCH v17 6/9 virtio-balloon: Add support for providing free page reports to host

In reply to Alexander Duyck

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

Unlike a standard balloon we don't inflate and deflate the pages. Instead we perform the reporting, and once the reporting is completed it is assumed that the page has been dropped from the guest and will be faulted back in the next time the page is accessed.

---
 drivers/virtio/Kconfig              |    1 +
 drivers/virtio/virtio_balloon.c     |   64 +++++++++++++++++++++++++++++++++++
 include/uapi/linux/virtio_balloon.h |    1 +
 3 files changed, 66 insertions(+)
drivers/virtio/Kconfig +1 -0
index 078615cf2afc..4b2dd8259ff5 100644
@@ -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 +64 -0

130 lines not on this page · raw message

include/uapi/linux/virtio_balloon.h +1 -0
index a1966cd7b677..19974392d324 100644
@@ -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
#15 lore raw

PATCH v17 7/9 mm/page_reporting: Rotate reported pages to the tail of the list

In reply to Alexander Duyck

From: Alexander Duyck <[email protected]>

Rather than walking over the same pages again and again to get to the pages that have yet to be reported we can save ourselves a significant amount of time by simply rotating the list so that when we have a full list of reported pages the head of the list is pointing to the next non-reported page. Doing this should save us some significant time when processing each free list.

This doesn't gain us much in the standard case as all of the non-reported pages should be near the top of the list already. However in the case of page shuffling this results in a noticeable improvement. Below are the will-it-scale page_fault1 w/ THP numbers for 16 tasks with and without this patch.

Without:

tasks   processes       processes_idle  threads         threads_idle
16      8093776.25      0.17            5393242.00      38.20

With:

tasks   processes       processes_idle  threads         threads_idle
16      8283274.75      0.17            5594261.00      38.15
---
 mm/page_reporting.c |   30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)
mm/page_reporting.c +22 -8
index 1047c6872d4f..6885e74c2367 100644
@@ -131,17 +131,27 @@ void __page_reporting_notify(void)
 		if (PageReported(page))
 			continue;
 
-		/* Attempt to pull page from list */
-		if (!__isolate_free_page(page, order))
-			break;
+		/* Attempt to pull page from list and place in scatterlist */
+		if (*offset) {
+			if (!__isolate_free_page(page, order)) {
+				next = page;
+				break;
+			}
 
-		/* Add page to scatter list */
-		--(*offset);
-		sg_set_page(&sgl[*offset], page, page_len, 0);
+			/* Add page to scatter list */
+			--(*offset);
+			sg_set_page(&sgl[*offset], page, page_len, 0);
 
-		/* If scatterlist isn't full grab more pages */
-		if (*offset)
 			continue;
+		}
+
+		/*
+		 * Make the first non-processed page in the free list
+		 * the new head of the free list before we release the
+		 * zone lock.
+		 */
+		if (&page->lru != list && !list_is_first(&page->lru, list))
+			list_rotate_to_front(&page->lru, list);
 
 		/* release lock before waiting on report processing */
 		spin_unlock_irq(&zone->lock);
@@ -169,6 +179,10 @@ void __page_reporting_notify(void)
 			break;
 	}
 
+	/* Rotate any leftover pages to the head of the freelist */
+	if (&next->lru != list && !list_is_first(&next->lru, list))
+		list_rotate_to_front(&next->lru, list);
+
 	spin_unlock_irq(&zone->lock);
 
 	return err;
Acked-by on v17 7/9
#16 lore raw

Re: [PATCH v17 7/9] mm/page_reporting: Rotate reported pages to the tail of the list

Quoted 16 lines · Alexander Duyck

From: Alexander Duyck <[email protected]>

Rather than walking over the same pages again and again to get to the pages that have yet to be reported we can save ourselves a significant amount of time by simply rotating the list so that when we have a full list of reported pages the head of the list is pointing to the next non-reported page. Doing this should save us some significant time when processing each free list.

This doesn't gain us much in the standard case as all of the non-reported pages should be near the top of the list already. However in the case of page shuffling this results in a noticeable improvement. Below are the will-it-scale page_fault1 w/ THP numbers for 16 tasks with and without this patch.

Without:

Quoted 8 lines
tasks   processes       processes_idle  threads         threads_idle
16      8093776.25      0.17            5393242.00      38.20

With:

tasks   processes       processes_idle  threads         threads_idle
16      8283274.75      0.17            5594261.00      38.15

Thanks for pulling this patch out and noting its impact. I think the rotation is ok and if it turns out I missed something, it'll be relatively easy to back out just the optimisation and leave the rest of the feature intact.

Signature · 2 lines
Mel Gorman
SUSE Labs
#17 lore raw

PATCH v17 8/9 mm/page_reporting: Add budget limit on how many pages can be reported per pass

In reply to Alexander Duyck

From: Alexander Duyck <[email protected]>

In order to keep ourselves from reporting pages that are just going to be reused again in the case of heavy churn we can put a limit on how many total pages we will process per pass. Doing this will allow the worker thread to go into idle much more quickly so that we avoid competing with other threads that might be allocating or freeing pages.

The logic added here will limit the worker thread to no more than one sixteenth of the total free pages in a given area per list. Once that limit is reached it will update the state so that at the end of the pass we will reschedule the worker to try again in 2 seconds when the memory churn has hopefully settled down.

Again this optimization doesn't show much of a benefit in the standard case as the memory churn is minmal. However with page allocator shuffling enabled the gain is quite noticeable. Below are the results with a THP enabled version of the will-it-scale page_fault1 test showing the improvement in iterations for 16 processes or threads.

Without:

tasks   processes       processes_idle  threads         threads_idle
16      8283274.75      0.17            5594261.00      38.15

With:

tasks   processes       processes_idle  threads         threads_idle
16      8767010.50      0.21            5791312.75      36.98
---
 include/linux/page_reporting.h |    1 +
 mm/page_reporting.c            |   33 ++++++++++++++++++++++++++++++++-
 2 files changed, 33 insertions(+), 1 deletion(-)
include/linux/page_reporting.h +1 -0
index 32355486f572..3b99e0ec24f2 100644
@@ -5,6 +5,7 @@
 #include <linux/mmzone.h>
 #include <linux/scatterlist.h>
 
+/* This value should always be a power of 2, see page_reporting_cycle() */
 #define PAGE_REPORTING_CAPACITY		32
 
 struct page_reporting_dev_info {
mm/page_reporting.c +32 -1
index 6885e74c2367..3bbd471cfc81 100644
@@ -114,6 +114,7 @@ void __page_reporting_notify(void)
 	struct list_head *list = &area->free_list[mt];
 	unsigned int page_len = PAGE_SIZE << order;
 	struct page *page, *next;
+	long budget;
 	int err = 0;
 
 	/*
@@ -125,12 +126,39 @@ void __page_reporting_notify(void)
 
 	spin_lock_irq(&zone->lock);
 
+	/*
+	 * Limit how many calls we will be making to the page reporting
+	 * device for this list. By doing this we avoid processing any
+	 * given list for too long.
+	 *
+	 * The current value used allows us enough calls to process over a
+	 * sixteenth of the current list plus one additional call to handle
+	 * any pages that may have already been present from the previous
+	 * list processed. This should result in us reporting all pages on
+	 * an idle system in about 30 seconds.
+	 *
+	 * The division here should be cheap since PAGE_REPORTING_CAPACITY
+	 * should always be a power of 2.
+	 */
+	budget = DIV_ROUND_UP(area->nr_free, PAGE_REPORTING_CAPACITY * 16);
+
 	/* loop through free list adding unreported pages to sg list */
 	list_for_each_entry_safe(page, next, list, lru) {
 		/* We are going to skip over the reported pages. */
 		if (PageReported(page))
 			continue;
 
+		/*
+		 * If we fully consumed our budget then update our
+		 * state to indicate that we are requesting additional
+		 * processing and exit this list.
+		 */
+		if (budget < 0) {
+			atomic_set(&prdev->state, PAGE_REPORTING_REQUESTED);
+			next = page;
+			break;
+		}
+
 		/* Attempt to pull page from list and place in scatterlist */
 		if (*offset) {
 			if (!__isolate_free_page(page, order)) {
@@ -146,7 +174,7 @@ void __page_reporting_notify(void)
 		}
 
 		/*
-		 * Make the first non-processed page in the free list
+		 * Make the first non-reported page in the free list
 		 * the new head of the free list before we release the
 		 * zone lock.
 		 */
@@ -162,6 +190,9 @@ void __page_reporting_notify(void)
 		/* reset offset since the full list was reported */
 		*offset = PAGE_REPORTING_CAPACITY;
 
+		/* update budget to reflect call to report function */
+		budget--;
+
 		/* reacquire zone lock and resume processing */
 		spin_lock_irq(&zone->lock);
 
Acked-by on v17 8/9
#18 lore raw

Re: [PATCH v17 8/9] mm/page_reporting: Add budget limit on how many pages can be reported per pass

Quoted 21 lines · Alexander Duyck

From: Alexander Duyck <[email protected]>

In order to keep ourselves from reporting pages that are just going to be reused again in the case of heavy churn we can put a limit on how many total pages we will process per pass. Doing this will allow the worker thread to go into idle much more quickly so that we avoid competing with other threads that might be allocating or freeing pages.

The logic added here will limit the worker thread to no more than one sixteenth of the total free pages in a given area per list. Once that limit is reached it will update the state so that at the end of the pass we will reschedule the worker to try again in 2 seconds when the memory churn has hopefully settled down.

Again this optimization doesn't show much of a benefit in the standard case as the memory churn is minmal. However with page allocator shuffling enabled the gain is quite noticeable. Below are the results with a THP enabled version of the will-it-scale page_fault1 test showing the improvement in iterations for 16 processes or threads.

Without:

Quoted 8 lines
tasks   processes       processes_idle  threads         threads_idle
16      8283274.75      0.17            5594261.00      38.15

With:

tasks   processes       processes_idle  threads         threads_idle
16      8767010.50      0.21            5791312.75      36.98

Seems fair. The test case you used would have been pounding on the zone lock at fairly high frequency so it represents a worst-case scenario but not necessarily an unrealistic one

Signature · 2 lines
Mel Gorman
SUSE Labs
#19 lore raw

PATCH v17 9/9 mm/page_reporting: Add free page reporting documentation

In reply to Alexander Duyck

From: Alexander Duyck <[email protected]>

Add documentation for free page reporting. Currently the only consumer is virtio-balloon, however it is possible that other drivers might make use of this so it is best to add a bit of documetation explaining at a high level how to use the API.

---
 Documentation/vm/free_page_reporting.rst |   41 ++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
 create mode 100644 Documentation/vm/free_page_reporting.rst
Documentation/vm/free_page_reporting.rst new file +41 -0
new file mode 100644
index 000000000000..33f54a450a4a
@@ -0,0 +1,41 @@
+.. _free_page_reporting:
+
+=====================
+Free Page Reporting
+=====================
+
+Free page reporting is an API by which a device can register to receive
+lists of pages that are currently unused by the system. This is useful in
+the case of virtualization where a guest is then able to use this data to
+notify the hypervisor that it is no longer using certain pages in memory.
+
+For the driver, typically a balloon driver, to use of this functionality
+it will allocate and initialize a page_reporting_dev_info structure. The
+field within the structure it will populate is the "report" function
+pointer used to process the scatterlist. It must also guarantee that it can
+handle at least PAGE_REPORTING_CAPACITY worth of scatterlist entries per
+call to the function. A call to page_reporting_register will register the
+page reporting interface with the reporting framework assuming no other
+page reporting devices are already registered.
+
+Once registered the page reporting API will begin reporting batches of
+pages to the driver. The API will start reporting pages 2 seconds after
+the interface is registered and will continue to do so 2 seconds after any
+page of a sufficiently high order is freed.
+
+Pages reported will be stored in the scatterlist passed to the reporting
+function with the final entry having the end bit set in entry nent - 1.
+While pages are being processed by the report function they will not be
+accessible to the allocator. Once the report function has been completed
+the pages will be returned to the free area from which they were obtained.
+
+Prior to removing a driver that is making use of free page reporting it
+is necessary to call page_reporting_unregister to have the
+page_reporting_dev_info structure that is currently in use by free page
+reporting removed. Doing this will prevent further reports from being
+issued via the interface. If another driver or the same driver is
+registered it is possible for it to resume where the previous driver had
+left off in terms of reporting free pages.
+
+Alexander Duyck, Dec 04, 2019
+
#20 lore raw

PATCH v17 1/3 virtio-ballon: Implement support for page poison tracking feature

In reply to Alexander Duyck

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.

---
 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
index a4729f7fc930..455d85b7082f 100644
@@ -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;
 }
@@ -854,6 +867,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)
@@ -916,6 +931,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
index d1c968d2376e..7fe78e5c14d7 100644
@@ -70,6 +70,7 @@ typedef struct VirtIOBalloon {
     uint32_t host_features;
 
     bool qemu_4_0_config_size;
+    uint32_t poison_val;
 } VirtIOBalloon;
 
 #endif

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

#21 lore raw

PATCH v17 2/3 virtio-balloon: Add support for providing free page reports to host

In reply to Alexander Duyck

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

Unlike a standard balloon we don't inflate and deflate the pages. Instead we perform the reporting, and once the reporting is completed it is assumed that the page has been dropped from the guest and will be faulted back in the next time the page is accessed.

This patch is a subset of the UAPI patch that was submitted for the Linux kernel. The original patch can be found at:
https://lore.kernel.org/lkml/[email protected]/

---
 include/standard-headers/linux/virtio_balloon.h |    1 +
 1 file changed, 1 insertion(+)
include/standard-headers/linux/virtio_balloon.h +1 -0
index 9375ca2a70de..1c5f6d6f2de6 100644
@@ -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

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

#22 lore raw

PATCH v17 3/3 virtio-balloon: Provide a interface for free page reporting

In reply to Alexander Duyck

From: Alexander Duyck <[email protected]>

Add support for what I am referring to as "free 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
index 455d85b7082f..5faafd2f62ac 100644
@@ -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,
@@ -939,6 +979,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
index 7fe78e5c14d7..db5bf7127112 100644
@@ -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;

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

#23 lore raw

[PATCH v17 QEMU 4/3 RFC] memory: Add support for MADV_FREE as mechanism to lazy discard pages

In reply to Alexander Duyck

From: Alexander Duyck <[email protected]>

Add support for the MADV_FREE advice argument when discarding pages. Specifically we add an option to perform a lazy discard for use with free page reporting as this allows us to avoid expensive page zeroing in the case that the system is not under memory pressure.

To enable this I simply extended the ram_block_discard_range function to add an extra parameter for "lazy" freeing. I then renamed the function, wrapped it in a function defined using the original name and defaulting lazy to false. From there I created a second wrapper for ram_block_free_range and updated the page reporting code to use that.

---
 exec.c                     |   39 +++++++++++++++++++++++++++------------
 hw/virtio/virtio-balloon.c |    2 +-
 include/exec/cpu-common.h  |    1 +
 3 files changed, 29 insertions(+), 13 deletions(-)
exec.c +27 -12
index 67e520d18ea5..2266574eb06e 100644
@@ -3881,15 +3881,8 @@ int qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque)
     return ret;
 }
 
-/*
- * Unmap pages of memory from start to start+length such that
- * they a) read as 0, b) Trigger whatever fault mechanism
- * the OS provides for postcopy.
- * The pages must be unmapped by the end of the function.
- * Returns: 0 on success, none-0 on failure
- *
- */
-int ram_block_discard_range(RAMBlock *rb, uint64_t start, size_t length)
+static int __ram_block_discard_range(RAMBlock *rb, uint64_t start,
+                                     size_t length, bool lazy)
 {
     int ret = -1;
 
@@ -3941,13 +3934,18 @@ int ram_block_discard_range(RAMBlock *rb, uint64_t start, size_t length)
 #endif
         }
         if (need_madvise) {
-            /* For normal RAM this causes it to be unmapped,
+#ifdef CONFIG_MADVISE
+#ifdef MADV_FREE
+            int advice = (lazy && !need_fallocate) ? MADV_FREE : MADV_DONTNEED;
+#else
+            int advice = MADV_DONTNEED;
+#endif
+            /* For normal RAM this causes it to be lazy freed or unmapped,
              * for shared memory it causes the local mapping to disappear
              * and to fall back on the file contents (which we just
              * fallocate'd away).
              */
-#if defined(CONFIG_MADVISE)
-            ret =  madvise(host_startaddr, length, MADV_DONTNEED);
+            ret =  madvise(host_startaddr, length, advice);
             if (ret) {
                 ret = -errno;
                 error_report("ram_block_discard_range: Failed to discard range "
@@ -3975,6 +3973,23 @@ err:
     return ret;
 }
 
+/*
+ * Unmap pages of memory from start to start+length such that
+ * they a) read as 0, b) Trigger whatever fault mechanism
+ * the OS provides for postcopy.
+ * The pages must be unmapped by the end of the function.
+ * Returns: 0 on success, none-0 on failure
+ *
+ */
+int ram_block_discard_range(RAMBlock *rb, uint64_t start, size_t length)
+{
+    return __ram_block_discard_range(rb, start, length, false);
+}
+
+int ram_block_free_range(RAMBlock *rb, uint64_t start, size_t length)
+{
+    return __ram_block_discard_range(rb, start, length, true);
+}
 bool ramblock_is_pmem(RAMBlock *rb)
 {
     return rb->flags & RAM_PMEM;
hw/virtio/virtio-balloon.c +1 -1
index 5faafd2f62ac..7df92af73792 100644
@@ -346,7 +346,7 @@ static void virtio_balloon_handle_report(VirtIODevice *vdev, VirtQueue *vq)
             if ((ram_offset | size) & (rb_page_size - 1))
                 continue;
 
-            ram_block_discard_range(rb, ram_offset, size);
+            ram_block_free_range(rb, ram_offset, size);
         }
 
         virtqueue_push(vq, elem, 0);
include/exec/cpu-common.h +1 -0
index 81753bbb3431..2bbd26784c63 100644
@@ -104,6 +104,7 @@ typedef int (RAMBlockIterFunc)(RAMBlock *rb, void *opaque);
 
 int qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque);
 int ram_block_discard_range(RAMBlock *rb, uint64_t start, size_t length);
+int ram_block_free_range(RAMBlock *rb, uint64_t start, size_t length);
 
 #endif
 

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

maintainer
#24 lore raw
In reply to Alexander Duyck
Quoted 5 lines · On Tue, 11 Feb 2020 14:45:51 -0800 Alexander Duyck <[email protected]>…

This series provides an asynchronous means of reporting free guest pages to a hypervisor so that the memory associated with those pages can be dropped and reused by other processes and/or guests on the host. Using this it is possible to avoid unnecessary I/O to disk and greatly improve performance in the case of memory overcommit on the host.

"greatly improve" sounds nice.

Quoted 43 lines

When enabled we will be performing a scan of free memory every 2 seconds while pages of sufficiently high order are being freed. In each pass at least one sixteenth of each free list will be reported. By doing this we avoid racing against other threads that may be causing a high amount of memory churn.

The lowest page order currently scanned when reporting pages is pageblock_order so that this feature will not interfere with the use of Transparent Huge Pages in the case of virtualization.

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 free. It will be zeroed and 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. We walk though the free list isolating pages and adding them to the scatterlist until we either encounter the end of the list or have processed at least one sixteenth of the pages that were listed in nr_free prior to us starting. If we fill the scatterlist before we reach the end of the list we rotate the list so that the first unreported page we encounter is moved to the head of the list as that is where we will resume after we have freed the reported pages back into the tail of the list.

Below are the results from various benchmarks. I primarily focused on two tests. The first is the will-it-scale/page_fault2 test, and the other is a modified version of will-it-scale/page_fault1 that was enabled to use THP. I did this as it allows for better visibility into different parts of the memory subsystem. The guest is running with 32G for RAM on one node of a E5-2630 v3. The host has had some features such as CPU turbo disabled in the BIOS.

Test                   page_fault1 (THP)    page_fault2
Name            tasks  Process Iter  STDEV  Process Iter  STDEV
Baseline            1    1012402.50  0.14%     361855.25  0.81%
                   16    8827457.25  0.09%    3282347.00  0.34%
Patches Applied     1    1007897.00  0.23%     361887.00  0.26%
                   16    8784741.75  0.39%    3240669.25  0.48%
Quoted 8 lines
Patches Enabled     1    1010227.50  0.39%     359749.25  0.56%
                   16    8756219.00  0.24%    3226608.75  0.97%
Patches Enabled     1    1050982.00  4.26%     357966.25  0.14%
 page shuffle      16    8672601.25  0.49%    3223177.75  0.40%
Patches enabled     1    1003238.00  0.22%     360211.00  0.22%
 shuffle w/ RFC    16    8767010.50  0.32%    3199874.00  0.71%

But these differences seem really small - around 1%? I think we're just showing not much harm was caused?

Quoted 10 lines

The results above are for a baseline with a linux-next-20191219 kernel, that kernel with this patch set applied but page reporting disabled in virtio-balloon, the patches applied and page reporting fully enabled, the patches enabled with page shuffling enabled, and the patches applied with page shuffling enabled and an RFC patch that makes used of MADV_FREE in QEMU. 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 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.

And this is the "great improvement", yes?

Is it possible to measure the end-user-visible benefits of this?

Quoted 9 lines

Any of the overhead visible with this patch set enabled seems due to page faults caused by accessing the reported pages and the host zeroing the page before giving it back to the guest. This overhead is much more visible when using THP than with standard 4K pages. In addition page shuffling seemed to increase the amount of faults generated due to an increase in memory churn. The overehad is reduced when using MADV_FREE as we can avoid the extra zeroing of the pages when they are reintroduced to the host, as can be seen when the RFC is applied with shuffling enabled.

Quoted 4 lines

The overall guest size is kept fairly small to only a few GB while the test is running. If the host memory were oversubscribed this patch set should result in a performance improvement as swapping memory in the host can be avoided.

"should result". Can we firm this up a lot?

#25 lore raw
Quoted 56 lines · Andrew Morton
Quoted 5 lines · On Tue, 11 Feb 2020 14:45:51 -0800 Alexander Duyck <[email protected]>…

This series provides an asynchronous means of reporting free guest pages to a hypervisor so that the memory associated with those pages can be dropped and reused by other processes and/or guests on the host. Using this it is possible to avoid unnecessary I/O to disk and greatly improve performance in the case of memory overcommit on the host.

"greatly improve" sounds nice.

Quoted 46 lines

When enabled we will be performing a scan of free memory every 2 seconds while pages of sufficiently high order are being freed. In each pass at least one sixteenth of each free list will be reported. By doing this we avoid racing against other threads that may be causing a high amount of memory churn.

The lowest page order currently scanned when reporting pages is pageblock_order so that this feature will not interfere with the use of Transparent Huge Pages in the case of virtualization.

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 free. It will be zeroed and 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. We walk though the free list isolating pages and adding them to the scatterlist until we either encounter the end of the list or have processed at least one sixteenth of the pages that were listed in nr_free prior to us starting. If we fill the scatterlist before we reach the end of the list we rotate the list so that the first unreported page we encounter is moved to the head of the list as that is where we will resume after we have freed the reported pages back into the tail of the list.

Below are the results from various benchmarks. I primarily focused on two tests. The first is the will-it-scale/page_fault2 test, and the other is a modified version of will-it-scale/page_fault1 that was enabled to use THP. I did this as it allows for better visibility into different parts of the memory subsystem. The guest is running with 32G for RAM on one node of a E5-2630 v3. The host has had some features such as CPU turbo disabled in the BIOS.

Test                   page_fault1 (THP)    page_fault2
Name            tasks  Process Iter  STDEV  Process Iter  STDEV
Baseline            1    1012402.50  0.14%     361855.25  0.81%
                   16    8827457.25  0.09%    3282347.00  0.34%
Patches Applied     1    1007897.00  0.23%     361887.00  0.26%
                   16    8784741.75  0.39%    3240669.25  0.48%
Patches Enabled     1    1010227.50  0.39%     359749.25  0.56%
                   16    8756219.00  0.24%    3226608.75  0.97%
Quoted 8 lines
Quoted 5 lines
Patches Enabled     1    1050982.00  4.26%     357966.25  0.14%
 page shuffle      16    8672601.25  0.49%    3223177.75  0.40%
Patches enabled     1    1003238.00  0.22%     360211.00  0.22%
 shuffle w/ RFC    16    8767010.50  0.32%    3199874.00  0.71%

But these differences seem really small - around 1%? I think we're just showing not much harm was caused?

Yes. Basically I am just showing the iterations are not negatively impacted. The big difference between the cases where it is enabled versus the cases where it is not is that the guest memory footprint is much smaller in the enabled cases than in the baseline or "Applied" cases.

Quoted 11 lines
Quoted 10 lines

The results above are for a baseline with a linux-next-20191219 kernel, that kernel with this patch set applied but page reporting disabled in virtio-balloon, the patches applied and page reporting fully enabled, the patches enabled with page shuffling enabled, and the patches applied with page shuffling enabled and an RFC patch that makes used of MADV_FREE in QEMU. 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 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 1 line

And this is the "great improvement", yes?

Yes, this is the great improvement. Basically what we get is effectively auto-ballooning so the guests aren't having to go to swap when they start to get loaded up since they are returning the memory to the host when they are done with it.

Quoted 1 line

Is it possible to measure the end-user-visible benefits of this?

If I clear the page cache on my host via drop_caches, fire up my 32G VM, and run the following commands:
memhog 32g
echo 3 > /proc/sys/vm/drop_caches

On the host I just have to monitor /proc/meminfo and I can see the difference. I get the following results on the host, in the enabled case it takes about 30 seconds for it to settle into the final state since I only report page a bit at a time:
Baseline/Applied
MemTotal: 131963012 kB
MemFree: 95189740 kB

Enabled:
MemTotal: 131963012 kB
MemFree: 126459472 kB

This is what I was referring to with the comment above. I had a test I was running back around the first RFC that consisted of bringing up enough VMs so that there was a bit of memory overcommit and then having the VMs in turn run memhog. As I recall the difference between the two was something like a couple minutes to run through all the VMs as the memhog would take up to 40+ seconds for one that was having to pull from swap while it took only 5 to 7 seconds for the VMs that were all running the page hinting.

I had referenced it here in the RFC:
https://lore.kernel.org/lkml/[email protected]/

I have been verifying the memory has been getting freed but didn't feel like the test added much value so I haven't added it to the cover page for a while since the time could vary widely and is dependent on things like the disk type used for the host swap since my SSD is likely faster than spinning rust, but may not be as fast as other SSDs on the market. Since the disk speed can play such a huge role I wasn't comfortable posting numbers since the benefits could vary so widely.

Quoted 9 lines
Quoted 9 lines

Any of the overhead visible with this patch set enabled seems due to page faults caused by accessing the reported pages and the host zeroing the page before giving it back to the guest. This overhead is much more visible when using THP than with standard 4K pages. In addition page shuffling seemed to increase the amount of faults generated due to an increase in memory churn. The overehad is reduced when using MADV_FREE as we can avoid the extra zeroing of the pages when they are reintroduced to the host, as can be seen when the RFC is applied with shuffling enabled.

Quoted 6 lines
Quoted 4 lines

The overall guest size is kept fairly small to only a few GB while the test is running. If the host memory were oversubscribed this patch set should result in a performance improvement as swapping memory in the host can be avoided.

"should result". Can we firm this up a lot?

I said "should result" here because if the guests are using all of their memory then the free page reporting won't make a difference since you have to have free pages before they can be reported. Also we cannot use free page reporting in the cases such as when a device is direct assigned into the guest as that currently prevents us from disassociating a page from the guest.

maintainer
#26 lore raw
Quoted 23 lines · On Tue, 11 Feb 2020 15:55:31 -0800 Alexander Duyck…

On the host I just have to monitor /proc/meminfo and I can see the difference. I get the following results on the host, in the enabled case it takes about 30 seconds for it to settle into the final state since I only report page a bit at a time:
Baseline/Applied
MemTotal: 131963012 kB
MemFree: 95189740 kB

Enabled:
MemTotal: 131963012 kB
MemFree: 126459472 kB

This is what I was referring to with the comment above. I had a test I was running back around the first RFC that consisted of bringing up enough VMs so that there was a bit of memory overcommit and then having the VMs in turn run memhog. As I recall the difference between the two was something like a couple minutes to run through all the VMs as the memhog would take up to 40+ seconds for one that was having to pull from swap while it took only 5 to 7 seconds for the VMs that were all running the page hinting.

I had referenced it here in the RFC:
https://lore.kernel.org/lkml/[email protected]/

Quoted 7 lines

I have been verifying the memory has been getting freed but didn't feel like the test added much value so I haven't added it to the cover page for a while since the time could vary widely and is dependent on things like the disk type used for the host swap since my SSD is likely faster than spinning rust, but may not be as fast as other SSDs on the market. Since the disk speed can play such a huge role I wasn't comfortable posting numbers since the benefits could vary so widely.

OK, thanks. I'll add the patches to the mm pile. The new mm/page_reporting.c is unreviewed afaict, so I guess you own that for now ;)

It would be very nice to get some feedback from testers asserting "yes, this really helped my workload" but I understand this sort of testing is hard to obtain at this stage.

#27 lore raw
Quoted 34 lines · Andrew Morton
Quoted 23 lines · On Tue, 11 Feb 2020 15:55:31 -0800 Alexander Duyck…

On the host I just have to monitor /proc/meminfo and I can see the difference. I get the following results on the host, in the enabled case it takes about 30 seconds for it to settle into the final state since I only report page a bit at a time:
Baseline/Applied
MemTotal: 131963012 kB
MemFree: 95189740 kB

Enabled:
MemTotal: 131963012 kB
MemFree: 126459472 kB

This is what I was referring to with the comment above. I had a test I was running back around the first RFC that consisted of bringing up enough VMs so that there was a bit of memory overcommit and then having the VMs in turn run memhog. As I recall the difference between the two was something like a couple minutes to run through all the VMs as the memhog would take up to 40+ seconds for one that was having to pull from swap while it took only 5 to 7 seconds for the VMs that were all running the page hinting.

I had referenced it here in the RFC:
https://lore.kernel.org/lkml/[email protected]/

Quoted 7 lines

I have been verifying the memory has been getting freed but didn't feel like the test added much value so I haven't added it to the cover page for a while since the time could vary widely and is dependent on things like the disk type used for the host swap since my SSD is likely faster than spinning rust, but may not be as fast as other SSDs on the market. Since the disk speed can play such a huge role I wasn't comfortable posting numbers since the benefits could vary so widely.

Quoted 3 lines

OK, thanks. I'll add the patches to the mm pile. The new mm/page_reporting.c is unreviewed afaict, so I guess you own that for now ;)

I will see what I can do to get some additional review of those patches. There has been some review, but I rewrote that block after suggestions as I had to split it out over several patches to account for the gains from the changes in patches 7 and 8.

Quoted 3 lines

It would be very nice to get some feedback from testers asserting "yes, this really helped my workload" but I understand this sort of testing is hard to obtain at this stage.

Without the QEMU patches applied there isn't much that this patch set can do on its own, so that is another piece I have to work on. Yet another reason to make sure it does no harm if it is not enabled.

So far the one that surprised me the most is that somebody from Huawei was working to add device pass-thru support to it already.
https://lore.kernel.org/lkml/[email protected]/

Thanks.

- Alex

#28 lore raw
In reply to Andrew Morton
Quoted 33 lines · Andrew Morton
Quoted 23 lines · On Tue, 11 Feb 2020 15:55:31 -0800 Alexander Duyck…

On the host I just have to monitor /proc/meminfo and I can see the difference. I get the following results on the host, in the enabled case it takes about 30 seconds for it to settle into the final state since I only report page a bit at a time:
Baseline/Applied
MemTotal: 131963012 kB
MemFree: 95189740 kB

Enabled:
MemTotal: 131963012 kB
MemFree: 126459472 kB

This is what I was referring to with the comment above. I had a test I was running back around the first RFC that consisted of bringing up enough VMs so that there was a bit of memory overcommit and then having the VMs in turn run memhog. As I recall the difference between the two was something like a couple minutes to run through all the VMs as the memhog would take up to 40+ seconds for one that was having to pull from swap while it took only 5 to 7 seconds for the VMs that were all running the page hinting.

I had referenced it here in the RFC:
https://lore.kernel.org/lkml/[email protected]/

Quoted 7 lines

I have been verifying the memory has been getting freed but didn't feel like the test added much value so I haven't added it to the cover page for a while since the time could vary widely and is dependent on things like the disk type used for the host swap since my SSD is likely faster than spinning rust, but may not be as fast as other SSDs on the market. Since the disk speed can play such a huge role I wasn't comfortable posting numbers since the benefits could vary so widely.

Quoted 8 lines

OK, thanks. I'll add the patches to the mm pile. The new mm/page_reporting.c is unreviewed afaict, so I guess you own that for now ;)

It would be very nice to get some feedback from testers asserting "yes, this really helped my workload" but I understand this sort of testing is hard to obtain at this stage.

Mel,

Any ETA on when you would be available to review these patches? They are now in Andrew's tree and in linux-next. I am hoping to get any remaining review from the community sorted out in the next few weeks so I can move onto focusing on how best to exert pressure on the page cache so that we can keep the guest memory footprint small.

Thanks.

- Alex

#29 lore raw
Quoted 43 lines · Alexander Duyck
Quoted 33 lines · Andrew Morton
Quoted 23 lines · On Tue, 11 Feb 2020 15:55:31 -0800 Alexander Duyck…

On the host I just have to monitor /proc/meminfo and I can see the difference. I get the following results on the host, in the enabled case it takes about 30 seconds for it to settle into the final state since I only report page a bit at a time:
Baseline/Applied
MemTotal: 131963012 kB
MemFree: 95189740 kB

Enabled:
MemTotal: 131963012 kB
MemFree: 126459472 kB

This is what I was referring to with the comment above. I had a test I was running back around the first RFC that consisted of bringing up enough VMs so that there was a bit of memory overcommit and then having the VMs in turn run memhog. As I recall the difference between the two was something like a couple minutes to run through all the VMs as the memhog would take up to 40+ seconds for one that was having to pull from swap while it took only 5 to 7 seconds for the VMs that were all running the page hinting.

I had referenced it here in the RFC:
https://lore.kernel.org/lkml/[email protected]/

Quoted 7 lines

I have been verifying the memory has been getting freed but didn't feel like the test added much value so I haven't added it to the cover page for a while since the time could vary widely and is dependent on things like the disk type used for the host swap since my SSD is likely faster than spinning rust, but may not be as fast as other SSDs on the market. Since the disk speed can play such a huge role I wasn't comfortable posting numbers since the benefits could vary so widely.

Quoted 8 lines

OK, thanks. I'll add the patches to the mm pile. The new mm/page_reporting.c is unreviewed afaict, so I guess you own that for now ;)

It would be very nice to get some feedback from testers asserting "yes, this really helped my workload" but I understand this sort of testing is hard to obtain at this stage.

Quoted 8 lines

Mel,

Any ETA on when you would be available to review these patches? They are now in Andrew's tree and in linux-next. I am hoping to get any remaining review from the community sorted out in the next few weeks so I can move onto focusing on how best to exert pressure on the page cache so that we can keep the guest memory footprint small.

I hope to get to it soon. I'm trying to finalise a scheduler-related series that reconciles NUMA and CPU balancing and it's occupying much of my attention available for mainline development :(

Signature · 2 lines
Mel Gorman
SUSE Labs
#30 lore raw
In reply to Alexander Duyck
Quoted 43 lines · Alexander Duyck
Quoted 33 lines · Andrew Morton
Quoted 23 lines · On Tue, 11 Feb 2020 15:55:31 -0800 Alexander Duyck…

On the host I just have to monitor /proc/meminfo and I can see the difference. I get the following results on the host, in the enabled case it takes about 30 seconds for it to settle into the final state since I only report page a bit at a time:
Baseline/Applied
MemTotal: 131963012 kB
MemFree: 95189740 kB

Enabled:
MemTotal: 131963012 kB
MemFree: 126459472 kB

This is what I was referring to with the comment above. I had a test I was running back around the first RFC that consisted of bringing up enough VMs so that there was a bit of memory overcommit and then having the VMs in turn run memhog. As I recall the difference between the two was something like a couple minutes to run through all the VMs as the memhog would take up to 40+ seconds for one that was having to pull from swap while it took only 5 to 7 seconds for the VMs that were all running the page hinting.

I had referenced it here in the RFC:
https://lore.kernel.org/lkml/[email protected]/

Quoted 7 lines

I have been verifying the memory has been getting freed but didn't feel like the test added much value so I haven't added it to the cover page for a while since the time could vary widely and is dependent on things like the disk type used for the host swap since my SSD is likely faster than spinning rust, but may not be as fast as other SSDs on the market. Since the disk speed can play such a huge role I wasn't comfortable posting numbers since the benefits could vary so widely.

Quoted 8 lines

OK, thanks. I'll add the patches to the mm pile. The new mm/page_reporting.c is unreviewed afaict, so I guess you own that for now ;)

It would be very nice to get some feedback from testers asserting "yes, this really helped my workload" but I understand this sort of testing is hard to obtain at this stage.

Quoted 8 lines

Mel,

Any ETA on when you would be available to review these patches? They are now in Andrew's tree and in linux-next. I am hoping to get any remaining review from the community sorted out in the next few weeks so I can move onto focusing on how best to exert pressure on the page cache so that we can keep the guest memory footprint small.

Sorry for the delay but I got through most of the patches -- I ignored the qemu ones. Overall I think it's fine, while I made a suggestion on how one section can be a bit more defensive, I don't consider it mandatory to update the series.

Last time I got upset by the use of zone lock, premature optimisation and some potential page allocator overhead. Now how you use the zone lock is justified and I think the state managed by atomics is ok. The optimisations are split out and so if there is a bug lurking in there, they can be backed out relatively easily and it was easier to review the series in this way. Finally, the overhead to the page allocator when the feature is disabled should be non-existent as it's protected by a static branch so I have no further objections.

Thanks!

Signature · 2 lines
Mel Gorman
SUSE Labs