linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v15 0/7] mm / virtio: Provide support for free page reporting
@ 2019-12-05 16:22 Alexander Duyck
  2019-12-05 16:22 ` [PATCH v15 1/7] mm: Adjust shuffle code to allow for future coalescing Alexander Duyck
                   ` (12 more replies)
  0 siblings, 13 replies; 39+ messages in thread
From: Alexander Duyck @ 2019-12-05 16:22 UTC (permalink / raw)
  To: kvm, mst, linux-kernel, willy, mhocko, linux-mm, akpm, mgorman, vbabka
  Cc: yang.zhang.wz, nitesh, konrad.wilk, david, pagupta, riel,
	lcapitulino, dave.hansen, wei.w.wang, aarcange, pbonzini,
	dan.j.williams, alexander.h.duyck, osalvador

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. Currently the order
used 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, processed as many pages as were listed in
nr_free prior to us starting, or have filled the scatterlist with pages to
be reported. 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 power saving features disabled
by setting the /dev/cpu_dma_latency value to 10ms.

Test                   page_fault1 (THP)    page_fault2
Name            tasks  Process Iter  STDEV  Process Iter  STDEV
Baseline            1    1208307.25  0.10%     408596.00  0.19%
                   16    8865204.75  0.16%    3344169.00  0.60%

Patches applied     1    1206809.00  0.26%     412558.25  0.32%
                   16    8814350.50  0.78%    3420102.00  1.16%

Patches enabled     1    1201386.25  0.21%     407903.75  0.32%
                   16    8880178.00  0.08%    3396700.50  0.54%

Patches enabled     1    1173529.00  1.04%     409006.50  0.45%
 page shuffle      16    8384540.25  0.74%    3288289.25  0.41%

Patches enabled     1    1193411.00  0.33%     406333.50  0.09%
 shuffle w/ RFC    16    8812639.75  0.73%    3321706.25  0.53%

The results above are for a baseline with a linux-next-20191203 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.
As seen in the data above, using MADV_FREE in QEMU mostly eliminates this
overhead.

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/29f43d5796feed0dec8e8bb98b187d9dac03b900.camel@linux.intel.com/

Changes from v13:
https://lore.kernel.org/lkml/20191105215940.15144.65968.stgit@localhost.localdomain/
Rewrote core reporting functionality
  Merged patches 3 & 4
  Dropped boundary list and related code
  Folded get_reported_page into page_reporting_fill
  Folded page_reporting_fill into page_reporting_cycle
Pulled reporting functionality out of free_reported_page
  Renamed it to __free_isolated_page
  Moved page reporting specific bits to page_reporting_drain
Renamed phdev to prdev since we aren't "hinting" we are "reporting"
Added documentation to describe the usage of unused page reporting
Updated cover page and patch descriptions to avoid mention of boundary

Changes from v14:
https://lore.kernel.org/lkml/20191119214454.24996.66289.stgit@localhost.localdomain/
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

---

Alexander Duyck (7):
      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: 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           |   25 ++
 include/uapi/linux/virtio_balloon.h      |    1 
 mm/Kconfig                               |   11 +
 mm/Makefile                              |    1 
 mm/internal.h                            |    1 
 mm/page_alloc.c                          |  169 +++++++++++----
 mm/page_isolation.c                      |    6 -
 mm/page_reporting.c                      |  336 ++++++++++++++++++++++++++++++
 mm/page_reporting.h                      |   54 +++++
 mm/shuffle.c                             |   12 +
 mm/shuffle.h                             |    6 +
 16 files changed, 700 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

--

^ permalink raw reply	[flat|nested] 39+ messages in thread

* [PATCH v15 1/7] mm: Adjust shuffle code to allow for future coalescing
  2019-12-05 16:22 [PATCH v15 0/7] mm / virtio: Provide support for free page reporting Alexander Duyck
@ 2019-12-05 16:22 ` Alexander Duyck
  2019-12-05 16:22 ` [PATCH v15 2/7] mm: Use zone and order instead of free area in free_list manipulators Alexander Duyck
                   ` (11 subsequent siblings)
  12 siblings, 0 replies; 39+ messages in thread
From: Alexander Duyck @ 2019-12-05 16:22 UTC (permalink / raw)
  To: kvm, mst, linux-kernel, willy, mhocko, linux-mm, akpm, mgorman, vbabka
  Cc: yang.zhang.wz, nitesh, konrad.wilk, david, pagupta, riel,
	lcapitulino, dave.hansen, wei.w.wang, aarcange, pbonzini,
	dan.j.williams, alexander.h.duyck, osalvador

From: Alexander Duyck <alexander.h.duyck@linux.intel.com>

Move the head/tail adding logic out of the shuffle code and into the
__free_one_page function since ultimately that is where it is really
needed anyway. By doing this we should be able to reduce the overhead
and can consolidate all of the list addition bits in one spot.

Acked-by: David Hildenbrand <david@redhat.com>
Reviewed-by: Dan Williams <dan.j.williams@intel.com>
Signed-off-by: Alexander Duyck <alexander.h.duyck@linux.intel.com>
---
 include/linux/mmzone.h |   12 --------
 mm/page_alloc.c        |   71 ++++++++++++++++++++++++++++--------------------
 mm/shuffle.c           |   12 ++++----
 mm/shuffle.h           |    6 ++++
 4 files changed, 54 insertions(+), 47 deletions(-)

diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index 89d8ff06c9ce..245010b24747 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -116,18 +116,6 @@ static inline void add_to_free_area_tail(struct page *page, struct free_area *ar
 	area->nr_free++;
 }
 
-#ifdef CONFIG_SHUFFLE_PAGE_ALLOCATOR
-/* Used to preserve page allocation order entropy */
-void add_to_free_area_random(struct page *page, struct free_area *area,
-		int migratetype);
-#else
-static inline void add_to_free_area_random(struct page *page,
-		struct free_area *area, int migratetype)
-{
-	add_to_free_area(page, area, migratetype);
-}
-#endif
-
 /* Used for pages which are on another list */
 static inline void move_to_free_area(struct page *page, struct free_area *area,
 			     int migratetype)
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 62dcd6b76c80..32e9cc092656 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -878,6 +878,36 @@ static inline struct capture_control *task_capc(struct zone *zone)
 #endif /* CONFIG_COMPACTION */
 
 /*
+ * If this is not the largest possible page, check if the buddy
+ * of the next-highest order is free. If it is, it's possible
+ * that pages are being freed that will coalesce soon. In case,
+ * that is happening, add the free page to the tail of the list
+ * so it's less likely to be used soon and more likely to be merged
+ * as a higher order page
+ */
+static inline bool
+buddy_merge_likely(unsigned long pfn, unsigned long buddy_pfn,
+		   struct page *page, unsigned int order)
+{
+	struct page *higher_page, *higher_buddy;
+	unsigned long combined_pfn;
+
+	if (order >= MAX_ORDER - 2)
+		return false;
+
+	if (!pfn_valid_within(buddy_pfn))
+		return false;
+
+	combined_pfn = buddy_pfn & pfn;
+	higher_page = page + (combined_pfn - pfn);
+	buddy_pfn = __find_buddy_pfn(combined_pfn, order + 1);
+	higher_buddy = higher_page + (buddy_pfn - combined_pfn);
+
+	return pfn_valid_within(buddy_pfn) &&
+	       page_is_buddy(higher_page, higher_buddy, order + 1);
+}
+
+/*
  * Freeing function for a buddy system allocator.
  *
  * The concept of a buddy system is to maintain direct-mapped table
@@ -906,11 +936,13 @@ static inline void __free_one_page(struct page *page,
 		struct zone *zone, unsigned int order,
 		int migratetype)
 {
-	unsigned long combined_pfn;
+	struct capture_control *capc = task_capc(zone);
 	unsigned long uninitialized_var(buddy_pfn);
-	struct page *buddy;
+	unsigned long combined_pfn;
+	struct free_area *area;
 	unsigned int max_order;
-	struct capture_control *capc = task_capc(zone);
+	struct page *buddy;
+	bool to_tail;
 
 	max_order = min_t(unsigned int, MAX_ORDER, pageblock_order + 1);
 
@@ -979,35 +1011,16 @@ static inline void __free_one_page(struct page *page,
 done_merging:
 	set_page_order(page, order);
 
-	/*
-	 * If this is not the largest possible page, check if the buddy
-	 * of the next-highest order is free. If it is, it's possible
-	 * that pages are being freed that will coalesce soon. In case,
-	 * that is happening, add the free page to the tail of the list
-	 * so it's less likely to be used soon and more likely to be merged
-	 * as a higher order page
-	 */
-	if ((order < MAX_ORDER-2) && pfn_valid_within(buddy_pfn)
-			&& !is_shuffle_order(order)) {
-		struct page *higher_page, *higher_buddy;
-		combined_pfn = buddy_pfn & pfn;
-		higher_page = page + (combined_pfn - pfn);
-		buddy_pfn = __find_buddy_pfn(combined_pfn, order + 1);
-		higher_buddy = higher_page + (buddy_pfn - combined_pfn);
-		if (pfn_valid_within(buddy_pfn) &&
-		    page_is_buddy(higher_page, higher_buddy, order + 1)) {
-			add_to_free_area_tail(page, &zone->free_area[order],
-					      migratetype);
-			return;
-		}
-	}
-
+	area = &zone->free_area[order];
 	if (is_shuffle_order(order))
-		add_to_free_area_random(page, &zone->free_area[order],
-				migratetype);
+		to_tail = shuffle_pick_tail();
 	else
-		add_to_free_area(page, &zone->free_area[order], migratetype);
+		to_tail = buddy_merge_likely(pfn, buddy_pfn, page, order);
 
+	if (to_tail)
+		add_to_free_area_tail(page, area, migratetype);
+	else
+		add_to_free_area(page, area, migratetype);
 }
 
 /*
diff --git a/mm/shuffle.c b/mm/shuffle.c
index b3fe97fd6654..e65d57f39486 100644
--- a/mm/shuffle.c
+++ b/mm/shuffle.c
@@ -183,11 +183,11 @@ void __meminit __shuffle_free_memory(pg_data_t *pgdat)
 		shuffle_zone(z);
 }
 
-void add_to_free_area_random(struct page *page, struct free_area *area,
-		int migratetype)
+bool shuffle_pick_tail(void)
 {
 	static u64 rand;
 	static u8 rand_bits;
+	bool ret;
 
 	/*
 	 * The lack of locking is deliberate. If 2 threads race to
@@ -198,10 +198,10 @@ void add_to_free_area_random(struct page *page, struct free_area *area,
 		rand = get_random_u64();
 	}
 
-	if (rand & 1)
-		add_to_free_area(page, area, migratetype);
-	else
-		add_to_free_area_tail(page, area, migratetype);
+	ret = rand & 1;
+
 	rand_bits--;
 	rand >>= 1;
+
+	return ret;
 }
diff --git a/mm/shuffle.h b/mm/shuffle.h
index 777a257a0d2f..4d79f03b6658 100644
--- a/mm/shuffle.h
+++ b/mm/shuffle.h
@@ -22,6 +22,7 @@ enum mm_shuffle_ctl {
 DECLARE_STATIC_KEY_FALSE(page_alloc_shuffle_key);
 extern void page_alloc_shuffle(enum mm_shuffle_ctl ctl);
 extern void __shuffle_free_memory(pg_data_t *pgdat);
+extern bool shuffle_pick_tail(void);
 static inline void shuffle_free_memory(pg_data_t *pgdat)
 {
 	if (!static_branch_unlikely(&page_alloc_shuffle_key))
@@ -44,6 +45,11 @@ static inline bool is_shuffle_order(int order)
 	return order >= SHUFFLE_ORDER;
 }
 #else
+static inline bool shuffle_pick_tail(void)
+{
+	return false;
+}
+
 static inline void shuffle_free_memory(pg_data_t *pgdat)
 {
 }


^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [PATCH v15 2/7] mm: Use zone and order instead of free area in free_list manipulators
  2019-12-05 16:22 [PATCH v15 0/7] mm / virtio: Provide support for free page reporting Alexander Duyck
  2019-12-05 16:22 ` [PATCH v15 1/7] mm: Adjust shuffle code to allow for future coalescing Alexander Duyck
@ 2019-12-05 16:22 ` Alexander Duyck
  2019-12-05 16:22 ` [PATCH v15 3/7] mm: Add function __putback_isolated_page Alexander Duyck
                   ` (10 subsequent siblings)
  12 siblings, 0 replies; 39+ messages in thread
From: Alexander Duyck @ 2019-12-05 16:22 UTC (permalink / raw)
  To: kvm, mst, linux-kernel, willy, mhocko, linux-mm, akpm, mgorman, vbabka
  Cc: yang.zhang.wz, nitesh, konrad.wilk, david, pagupta, riel,
	lcapitulino, dave.hansen, wei.w.wang, aarcange, pbonzini,
	dan.j.williams, alexander.h.duyck, osalvador

From: Alexander Duyck <alexander.h.duyck@linux.intel.com>

In order to enable the use of the zone from the list manipulator functions
I will need access to the zone pointer. As it turns out most of the
accessors were always just being directly passed &zone->free_area[order]
anyway so it would make sense to just fold that into the function itself
and pass the zone and order as arguments instead of the free area.

In order to be able to reference the zone we need to move the declaration
of the functions down so that we have the zone defined before we define the
list manipulation functions. Since the functions are only used in the file
mm/page_alloc.c we can just move them there to reduce noise in the header.

Reviewed-by: Dan Williams <dan.j.williams@intel.com>
Reviewed-by: David Hildenbrand <david@redhat.com>
Reviewed-by: Pankaj Gupta <pagupta@redhat.com>
Signed-off-by: Alexander Duyck <alexander.h.duyck@linux.intel.com>
---
 include/linux/mmzone.h |   32 -----------------------
 mm/page_alloc.c        |   67 +++++++++++++++++++++++++++++++++++-------------
 2 files changed, 49 insertions(+), 50 deletions(-)

diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index 245010b24747..8d93106490f3 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -100,29 +100,6 @@ struct free_area {
 	unsigned long		nr_free;
 };
 
-/* Used for pages not on another list */
-static inline void add_to_free_area(struct page *page, struct free_area *area,
-			     int migratetype)
-{
-	list_add(&page->lru, &area->free_list[migratetype]);
-	area->nr_free++;
-}
-
-/* Used for pages not on another list */
-static inline void add_to_free_area_tail(struct page *page, struct free_area *area,
-				  int migratetype)
-{
-	list_add_tail(&page->lru, &area->free_list[migratetype]);
-	area->nr_free++;
-}
-
-/* Used for pages which are on another list */
-static inline void move_to_free_area(struct page *page, struct free_area *area,
-			     int migratetype)
-{
-	list_move(&page->lru, &area->free_list[migratetype]);
-}
-
 static inline struct page *get_page_from_free_area(struct free_area *area,
 					    int migratetype)
 {
@@ -130,15 +107,6 @@ static inline struct page *get_page_from_free_area(struct free_area *area,
 					struct page, lru);
 }
 
-static inline void del_page_from_free_area(struct page *page,
-		struct free_area *area)
-{
-	list_del(&page->lru);
-	__ClearPageBuddy(page);
-	set_page_private(page, 0);
-	area->nr_free--;
-}
-
 static inline bool free_area_empty(struct free_area *area, int migratetype)
 {
 	return list_empty(&area->free_list[migratetype]);
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 32e9cc092656..e0a7895300fb 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -877,6 +877,44 @@ static inline struct capture_control *task_capc(struct zone *zone)
 }
 #endif /* CONFIG_COMPACTION */
 
+/* Used for pages not on another list */
+static inline void add_to_free_list(struct page *page, struct zone *zone,
+				    unsigned int order, int migratetype)
+{
+	struct free_area *area = &zone->free_area[order];
+
+	list_add(&page->lru, &area->free_list[migratetype]);
+	area->nr_free++;
+}
+
+/* Used for pages not on another list */
+static inline void add_to_free_list_tail(struct page *page, struct zone *zone,
+					 unsigned int order, int migratetype)
+{
+	struct free_area *area = &zone->free_area[order];
+
+	list_add_tail(&page->lru, &area->free_list[migratetype]);
+	area->nr_free++;
+}
+
+/* Used for pages which are on another list */
+static inline void move_to_free_list(struct page *page, struct zone *zone,
+				     unsigned int order, int migratetype)
+{
+	struct free_area *area = &zone->free_area[order];
+
+	list_move(&page->lru, &area->free_list[migratetype]);
+}
+
+static inline void del_page_from_free_list(struct page *page, struct zone *zone,
+					   unsigned int order)
+{
+	list_del(&page->lru);
+	__ClearPageBuddy(page);
+	set_page_private(page, 0);
+	zone->free_area[order].nr_free--;
+}
+
 /*
  * If this is not the largest possible page, check if the buddy
  * of the next-highest order is free. If it is, it's possible
@@ -939,7 +977,6 @@ static inline void __free_one_page(struct page *page,
 	struct capture_control *capc = task_capc(zone);
 	unsigned long uninitialized_var(buddy_pfn);
 	unsigned long combined_pfn;
-	struct free_area *area;
 	unsigned int max_order;
 	struct page *buddy;
 	bool to_tail;
@@ -977,7 +1014,7 @@ static inline void __free_one_page(struct page *page,
 		if (page_is_guard(buddy))
 			clear_page_guard(zone, buddy, order, migratetype);
 		else
-			del_page_from_free_area(buddy, &zone->free_area[order]);
+			del_page_from_free_list(buddy, zone, order);
 		combined_pfn = buddy_pfn & pfn;
 		page = page + (combined_pfn - pfn);
 		pfn = combined_pfn;
@@ -1011,16 +1048,15 @@ static inline void __free_one_page(struct page *page,
 done_merging:
 	set_page_order(page, order);
 
-	area = &zone->free_area[order];
 	if (is_shuffle_order(order))
 		to_tail = shuffle_pick_tail();
 	else
 		to_tail = buddy_merge_likely(pfn, buddy_pfn, page, order);
 
 	if (to_tail)
-		add_to_free_area_tail(page, area, migratetype);
+		add_to_free_list_tail(page, zone, order, migratetype);
 	else
-		add_to_free_area(page, area, migratetype);
+		add_to_free_list(page, zone, order, migratetype);
 }
 
 /*
@@ -2038,13 +2074,11 @@ void __init init_cma_reserved_pageblock(struct page *page)
  * -- nyc
  */
 static inline void expand(struct zone *zone, struct page *page,
-	int low, int high, struct free_area *area,
-	int migratetype)
+	int low, int high, int migratetype)
 {
 	unsigned long size = 1 << high;
 
 	while (high > low) {
-		area--;
 		high--;
 		size >>= 1;
 		VM_BUG_ON_PAGE(bad_range(zone, &page[size]), &page[size]);
@@ -2058,7 +2092,7 @@ static inline void expand(struct zone *zone, struct page *page,
 		if (set_page_guard(zone, &page[size], high, migratetype))
 			continue;
 
-		add_to_free_area(&page[size], area, migratetype);
+		add_to_free_list(&page[size], zone, high, migratetype);
 		set_page_order(&page[size], high);
 	}
 }
@@ -2216,8 +2250,8 @@ struct page *__rmqueue_smallest(struct zone *zone, unsigned int order,
 		page = get_page_from_free_area(area, migratetype);
 		if (!page)
 			continue;
-		del_page_from_free_area(page, area);
-		expand(zone, page, order, current_order, area, migratetype);
+		del_page_from_free_list(page, zone, current_order);
+		expand(zone, page, order, current_order, migratetype);
 		set_pcppage_migratetype(page, migratetype);
 		return page;
 	}
@@ -2291,7 +2325,7 @@ static int move_freepages(struct zone *zone,
 		VM_BUG_ON_PAGE(page_zone(page) != zone, page);
 
 		order = page_order(page);
-		move_to_free_area(page, &zone->free_area[order], migratetype);
+		move_to_free_list(page, zone, order, migratetype);
 		page += 1 << order;
 		pages_moved += 1 << order;
 	}
@@ -2407,7 +2441,6 @@ static void steal_suitable_fallback(struct zone *zone, struct page *page,
 		unsigned int alloc_flags, int start_type, bool whole_block)
 {
 	unsigned int current_order = page_order(page);
-	struct free_area *area;
 	int free_pages, movable_pages, alike_pages;
 	int old_block_type;
 
@@ -2478,8 +2511,7 @@ static void steal_suitable_fallback(struct zone *zone, struct page *page,
 	return;
 
 single_page:
-	area = &zone->free_area[current_order];
-	move_to_free_area(page, area, start_type);
+	move_to_free_list(page, zone, current_order, start_type);
 }
 
 /*
@@ -3150,7 +3182,6 @@ void split_page(struct page *page, unsigned int order)
 
 int __isolate_free_page(struct page *page, unsigned int order)
 {
-	struct free_area *area = &page_zone(page)->free_area[order];
 	unsigned long watermark;
 	struct zone *zone;
 	int mt;
@@ -3176,7 +3207,7 @@ int __isolate_free_page(struct page *page, unsigned int order)
 
 	/* Remove page from free list */
 
-	del_page_from_free_area(page, area);
+	del_page_from_free_list(page, zone, order);
 
 	/*
 	 * Set the pageblock if the isolated page is at least half of a
@@ -8703,7 +8734,7 @@ void zone_pcp_reset(struct zone *zone)
 		pr_info("remove from free list %lx %d %lx\n",
 			pfn, 1 << order, end_pfn);
 #endif
-		del_page_from_free_area(page, &zone->free_area[order]);
+		del_page_from_free_list(page, zone, order);
 		pfn += (1 << order);
 	}
 	spin_unlock_irqrestore(&zone->lock, flags);


^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [PATCH v15 3/7] mm: Add function __putback_isolated_page
  2019-12-05 16:22 [PATCH v15 0/7] mm / virtio: Provide support for free page reporting Alexander Duyck
  2019-12-05 16:22 ` [PATCH v15 1/7] mm: Adjust shuffle code to allow for future coalescing Alexander Duyck
  2019-12-05 16:22 ` [PATCH v15 2/7] mm: Use zone and order instead of free area in free_list manipulators Alexander Duyck
@ 2019-12-05 16:22 ` Alexander Duyck
  2019-12-16 11:36   ` David Hildenbrand
  2019-12-05 16:22 ` [PATCH v15 4/7] mm: Introduce Reported pages Alexander Duyck
                   ` (9 subsequent siblings)
  12 siblings, 1 reply; 39+ messages in thread
From: Alexander Duyck @ 2019-12-05 16:22 UTC (permalink / raw)
  To: kvm, mst, linux-kernel, willy, mhocko, linux-mm, akpm, mgorman, vbabka
  Cc: yang.zhang.wz, nitesh, konrad.wilk, david, pagupta, riel,
	lcapitulino, dave.hansen, wei.w.wang, aarcange, pbonzini,
	dan.j.williams, alexander.h.duyck, osalvador

From: Alexander Duyck <alexander.h.duyck@linux.intel.com>

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.

Signed-off-by: Alexander Duyck <alexander.h.duyck@linux.intel.com>
---
 mm/internal.h       |    1 +
 mm/page_alloc.c     |   24 ++++++++++++++++++++++++
 mm/page_isolation.c |    6 ++----
 3 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/mm/internal.h b/mm/internal.h
index 3cf20ab3ca01..e1c908d0bf83 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -157,6 +157,7 @@ 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);
 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);
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index e0a7895300fb..500b242c6f7f 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -3228,6 +3228,30 @@ 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)
+{
+	struct zone *zone = page_zone(page);
+	unsigned long pfn;
+	unsigned int mt;
+
+	/* zone lock should be held when this function is called */
+	lockdep_assert_held(&zone->lock);
+
+	pfn = page_to_pfn(page);
+	mt = get_pfnblock_migratetype(page, pfn);
+
+	/* Return isolated page to tail of freelist. */
+	__free_one_page(page, pfn, zone, order, mt);
+}
+
 /*
  * Update NUMA hit/miss statistics
  *
diff --git a/mm/page_isolation.c b/mm/page_isolation.c
index 04ee1663cdbe..d93d2be0070f 100644
--- a/mm/page_isolation.c
+++ b/mm/page_isolation.c
@@ -134,13 +134,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);
 	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 *


^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [PATCH v15 4/7] mm: Introduce Reported pages
  2019-12-05 16:22 [PATCH v15 0/7] mm / virtio: Provide support for free page reporting Alexander Duyck
                   ` (2 preceding siblings ...)
  2019-12-05 16:22 ` [PATCH v15 3/7] mm: Add function __putback_isolated_page Alexander Duyck
@ 2019-12-05 16:22 ` Alexander Duyck
  2019-12-16 10:17   ` Nitesh Narayan Lal
  2019-12-16 11:44   ` Nitesh Narayan Lal
  2019-12-05 16:22 ` [PATCH v15 5/7] virtio-balloon: Pull page poisoning config out of free page hinting Alexander Duyck
                   ` (8 subsequent siblings)
  12 siblings, 2 replies; 39+ messages in thread
From: Alexander Duyck @ 2019-12-05 16:22 UTC (permalink / raw)
  To: kvm, mst, linux-kernel, willy, mhocko, linux-mm, akpm, mgorman, vbabka
  Cc: yang.zhang.wz, nitesh, konrad.wilk, david, pagupta, riel,
	lcapitulino, dave.hansen, wei.w.wang, aarcange, pbonzini,
	dan.j.williams, alexander.h.duyck, osalvador

From: Alexander Duyck <alexander.h.duyck@linux.intel.com>

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.

Signed-off-by: Alexander Duyck <alexander.h.duyck@linux.intel.com>
---
 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            |  336 ++++++++++++++++++++++++++++++++++++++++
 mm/page_reporting.h            |   54 ++++++
 7 files changed, 451 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

diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h
index 1bf83c8fcaa7..49c2697046b9 100644
--- a/include/linux/page-flags.h
+++ b/include/linux/page-flags.h
@@ -163,6 +163,9 @@ enum pageflags {
 
 	/* non-lru isolated movable page */
 	PG_isolated = PG_reclaim,
+
+	/* 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.
diff --git a/include/linux/page_reporting.h b/include/linux/page_reporting.h
new file mode 100644
index 000000000000..32355486f572
--- /dev/null
+++ b/include/linux/page_reporting.h
@@ -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 */
diff --git a/mm/Kconfig b/mm/Kconfig
index ab80933be65f..d40a873402ff 100644
--- a/mm/Kconfig
+++ b/mm/Kconfig
@@ -237,6 +237,17 @@ config COMPACTION
 	  linux-mm@kvack.org.
 
 #
+# 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
diff --git a/mm/Makefile b/mm/Makefile
index 059115e6efb4..e55649063735 100644
--- a/mm/Makefile
+++ b/mm/Makefile
@@ -117,3 +117,4 @@ obj-$(CONFIG_ZONE_DEVICE) += memremap.o
 obj-$(CONFIG_HMM_MIRROR) += hmm.o
 obj-$(CONFIG_MEMFD_CREATE) += memfd.o
 obj-$(CONFIG_MAPPING_DIRTY_HELPERS) += mapping_dirty_helpers.o
+obj-$(CONFIG_PAGE_REPORTING) += page_reporting.o
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 500b242c6f7f..290148398c26 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -74,6 +74,7 @@
 #include <asm/div64.h>
 #include "internal.h"
 #include "shuffle.h"
+#include "page_reporting.h"
 
 /* prevent >1 _updater_ of zone percpu pageset ->high and ->batch fields */
 static DEFINE_MUTEX(pcp_batch_high_lock);
@@ -909,6 +910,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);
@@ -972,7 +977,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);
@@ -1057,6 +1062,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);
 }
 
 /*
@@ -1373,7 +1382,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);
@@ -1389,7 +1398,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);
 }
 
@@ -3249,7 +3258,7 @@ void __putback_isolated_page(struct page *page, unsigned int order)
 	mt = get_pfnblock_migratetype(page, pfn);
 
 	/* Return isolated page to tail of freelist. */
-	__free_one_page(page, pfn, zone, order, mt);
+	__free_one_page(page, pfn, zone, order, mt, false);
 }
 
 /*
diff --git a/mm/page_reporting.c b/mm/page_reporting.c
new file mode 100644
index 000000000000..90154e71d6f9
--- /dev/null
+++ b/mm/page_reporting.c
@@ -0,0 +1,336 @@
+// 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>
+
+#include "page_reporting.h"
+#include "internal.h"
+
+#define PAGE_REPORTING_DELAY	(2 * HZ)
+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);
+	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);
+}
+
+/* notify prdev of free page reporting request */
+void __page_reporting_notify(void)
+{
+	struct page_reporting_dev_info *prdev;
+
+	/*
+	 * We use RCU to protect the pr_dev_info pointer. In almost all
+	 * cases this should be present, however in the unlikely case of
+	 * a shutdown this will be NULL and we should exit.
+	 */
+	rcu_read_lock();
+	prdev = rcu_dereference(pr_dev_info);
+	if (likely(prdev))
+		__page_reporting_request(prdev);
+
+	rcu_read_unlock();
+}
+
+static void
+page_reporting_drain(struct page_reporting_dev_info *prdev,
+		     struct scatterlist *sgl, unsigned int nents, bool reported)
+{
+	struct scatterlist *sg = sgl;
+
+	/*
+	 * Drain the now reported pages back into their respective
+	 * free lists/areas. We assume at least one page is populated.
+	 */
+	do {
+		unsigned int order = get_order(sg->length);
+		struct page *page = sg_page(sg);
+
+		__putback_isolated_page(page, order);
+
+		/* If the pages were not reported due to error skip flagging */
+		if (!reported)
+			continue;
+
+		/*
+		 * If page was not comingled with another page we can
+		 * consider the result to be "reported" since the page
+		 * hasn't been modified, otherwise we will need to
+		 * report on the new larger page when we make our way
+		 * up to that higher order.
+		 */
+		if (PageBuddy(page) && page_order(page) == order)
+			__SetPageReported(page);
+	} while ((sg = sg_next(sg)));
+
+	/* reinitialize scatterlist now that it is empty */
+	sg_init_table(sgl, nents);
+}
+
+/*
+ * The page reporting cycle consists of 4 stages, fill, report, drain, and
+ * idle. We will cycle through the first 3 stages until we cannot obtain a
+ * full scatterlist of pages, in that case we will switch to idle.
+ */
+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;
+	unsigned long budget;
+	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);
+
+	/*
+	 * Only process as many pages are are present at the start of this
+	 * call. If additional pages are freed we can process them in a
+	 * couple seconds when we start the next pass.
+	 *
+	 * The assumption with this is that most of the pages are of one
+	 * migratetype so we should really only need this for one of the
+	 * lists in a given free area.
+	 */
+	budget = area->nr_free;
+
+	/* loop through free list adding unreported pages to sg list */
+	list_for_each_entry_safe(page, next, list, lru) {
+		/* If we consumed our budget we should go idle for now */
+		if (!budget--)
+			break;
+
+		/* We are going to skip over the reported pages. */
+		if (PageReported(page))
+			continue;
+
+		/* Attempt to add page to sg list */
+		if (*offset) {
+			if (!__isolate_free_page(page, order))
+				break;
+
+			sg_set_page(&sgl[--(*offset)], page, page_len, 0);
+			continue;
+		}
+
+		/*
+		 * Make the first non-reported entry in the free list
+		 * the new head of the free list before we exit.
+		 */
+		if (!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);
+
+		/* reset entries since the full list will be reported */
+		*offset = PAGE_REPORTING_CAPACITY;
+
+		/* begin processing pages in local list */
+		err = prdev->report(prdev, sgl, 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);
+
+		/* exit on error */
+		if (err)
+			break;
+
+		/*
+		 * 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);
+	}
+
+	spin_unlock_irq(&zone->lock);
+
+	return err;
+}
+
+static int
+page_reporting_process_zone(struct page_reporting_dev_info *prdev,
+			    struct scatterlist *sgl, struct zone *zone)
+{
+	unsigned int order, mt, leftover, offset = PAGE_REPORTING_CAPACITY;
+	unsigned long watermark;
+	int err = 0;
+
+	/* Generate minimum watermark to be able to guarantee progress */
+	watermark = low_wmark_pages(zone) +
+		    (PAGE_REPORTING_CAPACITY << PAGE_REPORTING_MIN_ORDER);
+
+	/*
+	 * Cancel request if insufficient free memory or if we failed
+	 * to allocate page reporting statistics for the zone.
+	 */
+	if (!zone_watermark_ok(zone, 0, watermark, 0, ALLOC_CMA))
+		return err;
+
+	/* Process each free list starting from lowest order/mt */
+	for (order = PAGE_REPORTING_MIN_ORDER; order < MAX_ORDER; order++) {
+		for (mt = 0; mt < MIGRATE_TYPES; mt++) {
+			/* We do not pull pages from the isolate free list */
+			if (is_migrate_isolate(mt))
+				continue;
+
+			err = page_reporting_cycle(prdev, zone, order, mt,
+						   sgl, &offset);
+			if (err)
+				return err;
+		}
+	}
+
+	/* report the leftover pages before going idle */
+	leftover = PAGE_REPORTING_CAPACITY - offset;
+	if (leftover) {
+		sgl = &sgl[offset];
+		err = prdev->report(prdev, sgl, leftover);
+
+		/* flush any remaining pages out from the last report */
+		spin_lock_irq(&zone->lock);
+		page_reporting_drain(prdev, sgl, leftover, !err);
+		spin_unlock_irq(&zone->lock);
+	}
+
+	return err;
+}
+
+static void page_reporting_process(struct work_struct *work)
+{
+	struct delayed_work *d_work = to_delayed_work(work);
+	struct page_reporting_dev_info *prdev =
+		container_of(d_work, struct page_reporting_dev_info, work);
+	int err = 0, state = PAGE_REPORTING_ACTIVE;
+	struct scatterlist *sgl;
+	struct zone *zone;
+
+	/*
+	 * Alter the state so we can detect any future requests while not
+	 * scheculing any additional work.
+	 */
+	atomic_set(&prdev->state, state);
+
+	/* allocate scatterlist to store pages being reported on */
+	sgl = kmalloc_array(PAGE_REPORTING_CAPACITY, sizeof(*sgl), GFP_KERNEL);
+	if (!sgl)
+		goto err_out;
+
+	sg_init_table(sgl, PAGE_REPORTING_CAPACITY);
+
+	for_each_zone(zone) {
+		err = page_reporting_process_zone(prdev, sgl, zone);
+		if (err)
+			break;
+	}
+
+	kfree(sgl);
+err_out:
+	/*
+	 * If the state has reverted back to requested then there may be
+	 * additional pages to be processed. We will defer for 200ms to allow
+	 * more pages to accumulate.
+	 */
+	state = atomic_cmpxchg(&prdev->state, state, PAGE_REPORTING_IDLE);
+	if (state == PAGE_REPORTING_REQUESTED)
+		schedule_delayed_work(&prdev->work, PAGE_REPORTING_DELAY);
+}
+
+static DEFINE_MUTEX(page_reporting_mutex);
+DEFINE_STATIC_KEY_FALSE(page_reporting_enabled);
+
+int page_reporting_register(struct page_reporting_dev_info *prdev)
+{
+	int err = 0;
+
+	mutex_lock(&page_reporting_mutex);
+
+	/* nothing to do if already in use */
+	if (rcu_access_pointer(pr_dev_info)) {
+		err = -EBUSY;
+		goto err_out;
+	}
+
+	/* initialize state and work structures */
+	atomic_set(&prdev->state, PAGE_REPORTING_IDLE);
+	INIT_DELAYED_WORK(&prdev->work, &page_reporting_process);
+
+	/* Begin initial flush of zones */
+	__page_reporting_request(prdev);
+
+	/* Assign device to allow notifications */
+	rcu_assign_pointer(pr_dev_info, prdev);
+
+	/* enable page reporting notification */
+	if (!static_key_enabled(&page_reporting_enabled)) {
+		static_branch_enable(&page_reporting_enabled);
+		pr_info("Free page reporting enabled\n");
+	}
+err_out:
+	mutex_unlock(&page_reporting_mutex);
+
+	return err;
+}
+EXPORT_SYMBOL_GPL(page_reporting_register);
+
+void page_reporting_unregister(struct page_reporting_dev_info *prdev)
+{
+	mutex_lock(&page_reporting_mutex);
+
+	if (rcu_access_pointer(pr_dev_info) == prdev) {
+		/* Disable page reporting notification */
+		RCU_INIT_POINTER(pr_dev_info, NULL);
+		synchronize_rcu();
+
+		/* Flush any existing work, and lock it out */
+		cancel_delayed_work_sync(&prdev->work);
+	}
+
+	mutex_unlock(&page_reporting_mutex);
+}
+EXPORT_SYMBOL_GPL(page_reporting_unregister);
diff --git a/mm/page_reporting.h b/mm/page_reporting.h
new file mode 100644
index 000000000000..aa6d37f4dc22
--- /dev/null
+++ b/mm/page_reporting.h
@@ -0,0 +1,54 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _MM_PAGE_REPORTING_H
+#define _MM_PAGE_REPORTING_H
+
+#include <linux/mmzone.h>
+#include <linux/pageblock-flags.h>
+#include <linux/page-isolation.h>
+#include <linux/jump_label.h>
+#include <linux/slab.h>
+#include <asm/pgtable.h>
+#include <linux/scatterlist.h>
+
+#define PAGE_REPORTING_MIN_ORDER	pageblock_order
+
+#ifdef CONFIG_PAGE_REPORTING
+DECLARE_STATIC_KEY_FALSE(page_reporting_enabled);
+void __page_reporting_notify(void);
+
+static inline bool page_reported(struct page *page)
+{
+	return static_branch_unlikely(&page_reporting_enabled) &&
+	       PageReported(page);
+}
+
+/**
+ * page_reporting_notify_free - Free page notification to start page processing
+ *
+ * This function is meant to act as a screener for __page_reporting_notify
+ * which will determine if a give zone has crossed over the high-water mark
+ * that will justify us beginning page treatment. If we have crossed that
+ * threshold then it will start the process of pulling some pages and
+ * placing them in the batch list for treatment.
+ */
+static inline void page_reporting_notify_free(unsigned int order)
+{
+	/* Called from hot path in __free_one_page() */
+	if (!static_branch_unlikely(&page_reporting_enabled))
+		return;
+
+	/* Determine if we have crossed reporting threshold */
+	if (order < PAGE_REPORTING_MIN_ORDER)
+		return;
+
+	/* This will add a few cycles, but should be called infrequently */
+	__page_reporting_notify();
+}
+#else /* CONFIG_PAGE_REPORTING */
+#define page_reported(_page)	false
+
+static inline void page_reporting_notify_free(unsigned int order)
+{
+}
+#endif /* CONFIG_PAGE_REPORTING */
+#endif /*_MM_PAGE_REPORTING_H */


^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [PATCH v15 5/7] virtio-balloon: Pull page poisoning config out of free page hinting
  2019-12-05 16:22 [PATCH v15 0/7] mm / virtio: Provide support for free page reporting Alexander Duyck
                   ` (3 preceding siblings ...)
  2019-12-05 16:22 ` [PATCH v15 4/7] mm: Introduce Reported pages Alexander Duyck
@ 2019-12-05 16:22 ` Alexander Duyck
  2019-12-13  7:03   ` Michael S. Tsirkin
  2019-12-05 16:22 ` [PATCH v15 6/7] virtio-balloon: Add support for providing free page reports to host Alexander Duyck
                   ` (7 subsequent siblings)
  12 siblings, 1 reply; 39+ messages in thread
From: Alexander Duyck @ 2019-12-05 16:22 UTC (permalink / raw)
  To: kvm, mst, linux-kernel, willy, mhocko, linux-mm, akpm, mgorman, vbabka
  Cc: yang.zhang.wz, nitesh, konrad.wilk, david, pagupta, riel,
	lcapitulino, dave.hansen, wei.w.wang, aarcange, pbonzini,
	dan.j.williams, alexander.h.duyck, osalvador

From: Alexander Duyck <alexander.h.duyck@linux.intel.com>

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.

Reviewed-by: David Hildenbrand <david@redhat.com>
Signed-off-by: Alexander Duyck <alexander.h.duyck@linux.intel.com>
---
 drivers/virtio/virtio_balloon.c |   23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
index 15b7f1d8c334..252591bc7e01 100644
--- a/drivers/virtio/virtio_balloon.c
+++ b/drivers/virtio/virtio_balloon.c
@@ -849,7 +849,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) {
@@ -916,11 +915,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
@@ -1021,7 +1029,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);


^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [PATCH v15 6/7] virtio-balloon: Add support for providing free page reports to host
  2019-12-05 16:22 [PATCH v15 0/7] mm / virtio: Provide support for free page reporting Alexander Duyck
                   ` (4 preceding siblings ...)
  2019-12-05 16:22 ` [PATCH v15 5/7] virtio-balloon: Pull page poisoning config out of free page hinting Alexander Duyck
@ 2019-12-05 16:22 ` Alexander Duyck
  2019-12-13  7:08   ` Michael S. Tsirkin
  2019-12-13 10:15   ` David Hildenbrand
  2019-12-05 16:23 ` [PATCH v15 7/7] mm: Add free page reporting documentation Alexander Duyck
                   ` (6 subsequent siblings)
  12 siblings, 2 replies; 39+ messages in thread
From: Alexander Duyck @ 2019-12-05 16:22 UTC (permalink / raw)
  To: kvm, mst, linux-kernel, willy, mhocko, linux-mm, akpm, mgorman, vbabka
  Cc: yang.zhang.wz, nitesh, konrad.wilk, david, pagupta, riel,
	lcapitulino, dave.hansen, wei.w.wang, aarcange, pbonzini,
	dan.j.williams, alexander.h.duyck, osalvador

From: Alexander Duyck <alexander.h.duyck@linux.intel.com>

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.

For this reason when I had originally introduced the patch set I referred
to this behavior as a "bubble" instead of a "balloon" since the duration
is short lived, and when the page is touched the "bubble" is popped and
the page is faulted back in.

Signed-off-by: Alexander Duyck <alexander.h.duyck@linux.intel.com>
---
 drivers/virtio/Kconfig              |    1 +
 drivers/virtio/virtio_balloon.c     |   64 +++++++++++++++++++++++++++++++++++
 include/uapi/linux/virtio_balloon.h |    1 +
 3 files changed, 66 insertions(+)

diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig
index 078615cf2afc..4b2dd8259ff5 100644
--- a/drivers/virtio/Kconfig
+++ b/drivers/virtio/Kconfig
@@ -58,6 +58,7 @@ config VIRTIO_BALLOON
 	tristate "Virtio balloon driver"
 	depends on VIRTIO
 	select MEMORY_BALLOON
+	select PAGE_REPORTING
 	---help---
 	 This driver supports increasing and decreasing the amount
 	 of memory within a KVM guest.
diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
index 252591bc7e01..ecd54edba968 100644
--- a/drivers/virtio/virtio_balloon.c
+++ b/drivers/virtio/virtio_balloon.c
@@ -19,6 +19,7 @@
 #include <linux/mount.h>
 #include <linux/magic.h>
 #include <linux/pseudo_fs.h>
+#include <linux/page_reporting.h>
 
 /*
  * Balloon device works in 4K page units.  So each page is pointed to by
@@ -47,6 +48,7 @@ enum virtio_balloon_vq {
 	VIRTIO_BALLOON_VQ_DEFLATE,
 	VIRTIO_BALLOON_VQ_STATS,
 	VIRTIO_BALLOON_VQ_FREE_PAGE,
+	VIRTIO_BALLOON_VQ_REPORTING,
 	VIRTIO_BALLOON_VQ_MAX
 };
 
@@ -114,6 +116,10 @@ struct virtio_balloon {
 
 	/* To register a shrinker to shrink memory upon memory pressure */
 	struct shrinker shrinker;
+
+	/* Free page reporting device */
+	struct virtqueue *reporting_vq;
+	struct page_reporting_dev_info pr_dev_info;
 };
 
 static struct virtio_device_id id_table[] = {
@@ -153,6 +159,33 @@ static void tell_host(struct virtio_balloon *vb, struct virtqueue *vq)
 
 }
 
+int virtballoon_free_page_report(struct page_reporting_dev_info *pr_dev_info,
+				   struct scatterlist *sg, unsigned int nents)
+{
+	struct virtio_balloon *vb =
+		container_of(pr_dev_info, struct virtio_balloon, pr_dev_info);
+	struct virtqueue *vq = vb->reporting_vq;
+	unsigned int unused, err;
+
+	/* We should always be able to add these buffers to an empty queue. */
+	err = virtqueue_add_inbuf(vq, sg, nents, vb, GFP_NOWAIT | __GFP_NOWARN);
+
+	/*
+	 * In the extremely unlikely case that something has occurred and we
+	 * are able to trigger an error we will simply display a warning
+	 * and exit without actually processing the pages.
+	 */
+	if (WARN_ON_ONCE(err))
+		return err;
+
+	virtqueue_kick(vq);
+
+	/* When host has read buffer, this completes via balloon_ack */
+	wait_event(vb->acked, virtqueue_get_buf(vq, &unused));
+
+	return 0;
+}
+
 static void set_page_pfns(struct virtio_balloon *vb,
 			  __virtio32 pfns[], struct page *page)
 {
@@ -477,6 +510,7 @@ static int init_vqs(struct virtio_balloon *vb)
 	names[VIRTIO_BALLOON_VQ_DEFLATE] = "deflate";
 	names[VIRTIO_BALLOON_VQ_STATS] = NULL;
 	names[VIRTIO_BALLOON_VQ_FREE_PAGE] = NULL;
+	names[VIRTIO_BALLOON_VQ_REPORTING] = NULL;
 
 	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) {
 		names[VIRTIO_BALLOON_VQ_STATS] = "stats";
@@ -488,6 +522,11 @@ static int init_vqs(struct virtio_balloon *vb)
 		callbacks[VIRTIO_BALLOON_VQ_FREE_PAGE] = NULL;
 	}
 
+	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) {
+		names[VIRTIO_BALLOON_VQ_REPORTING] = "reporting_vq";
+		callbacks[VIRTIO_BALLOON_VQ_REPORTING] = balloon_ack;
+	}
+
 	err = vb->vdev->config->find_vqs(vb->vdev, VIRTIO_BALLOON_VQ_MAX,
 					 vqs, callbacks, names, NULL, NULL);
 	if (err)
@@ -520,6 +559,9 @@ static int init_vqs(struct virtio_balloon *vb)
 	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
 		vb->free_page_vq = vqs[VIRTIO_BALLOON_VQ_FREE_PAGE];
 
+	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
+		vb->reporting_vq = vqs[VIRTIO_BALLOON_VQ_REPORTING];
+
 	return 0;
 }
 
@@ -939,12 +981,31 @@ static int virtballoon_probe(struct virtio_device *vdev)
 		if (err)
 			goto out_del_balloon_wq;
 	}
+
+	vb->pr_dev_info.report = virtballoon_free_page_report;
+	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) {
+		unsigned int capacity;
+
+		capacity = virtqueue_get_vring_size(vb->reporting_vq);
+		if (capacity < PAGE_REPORTING_CAPACITY) {
+			err = -ENOSPC;
+			goto out_unregister_shrinker;
+		}
+
+		err = page_reporting_register(&vb->pr_dev_info);
+		if (err)
+			goto out_unregister_shrinker;
+	}
+
 	virtio_device_ready(vdev);
 
 	if (towards_target(vb))
 		virtballoon_changed(vdev);
 	return 0;
 
+out_unregister_shrinker:
+	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
+		virtio_balloon_unregister_shrinker(vb);
 out_del_balloon_wq:
 	if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
 		destroy_workqueue(vb->balloon_wq);
@@ -973,6 +1034,8 @@ static void virtballoon_remove(struct virtio_device *vdev)
 {
 	struct virtio_balloon *vb = vdev->priv;
 
+	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
+		page_reporting_unregister(&vb->pr_dev_info);
 	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
 		virtio_balloon_unregister_shrinker(vb);
 	spin_lock_irq(&vb->stop_update_lock);
@@ -1045,6 +1108,7 @@ static int virtballoon_validate(struct virtio_device *vdev)
 	VIRTIO_BALLOON_F_DEFLATE_ON_OOM,
 	VIRTIO_BALLOON_F_FREE_PAGE_HINT,
 	VIRTIO_BALLOON_F_PAGE_POISON,
+	VIRTIO_BALLOON_F_REPORTING,
 };
 
 static struct virtio_driver virtio_balloon_driver = {
diff --git a/include/uapi/linux/virtio_balloon.h b/include/uapi/linux/virtio_balloon.h
index a1966cd7b677..19974392d324 100644
--- a/include/uapi/linux/virtio_balloon.h
+++ b/include/uapi/linux/virtio_balloon.h
@@ -36,6 +36,7 @@
 #define VIRTIO_BALLOON_F_DEFLATE_ON_OOM	2 /* Deflate balloon on OOM */
 #define VIRTIO_BALLOON_F_FREE_PAGE_HINT	3 /* VQ to report free pages */
 #define VIRTIO_BALLOON_F_PAGE_POISON	4 /* Guest is using page poisoning */
+#define VIRTIO_BALLOON_F_REPORTING	5 /* Page reporting virtqueue */
 
 /* Size of a PFN in the balloon interface. */
 #define VIRTIO_BALLOON_PFN_SHIFT 12


^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [PATCH v15 7/7] mm: Add free page reporting documentation
  2019-12-05 16:22 [PATCH v15 0/7] mm / virtio: Provide support for free page reporting Alexander Duyck
                   ` (5 preceding siblings ...)
  2019-12-05 16:22 ` [PATCH v15 6/7] virtio-balloon: Add support for providing free page reports to host Alexander Duyck
@ 2019-12-05 16:23 ` Alexander Duyck
  2019-12-05 16:24 ` [PATCH v15 QEMU 1/3] virtio-ballon: Implement support for page poison tracking feature Alexander Duyck
                   ` (5 subsequent siblings)
  12 siblings, 0 replies; 39+ messages in thread
From: Alexander Duyck @ 2019-12-05 16:23 UTC (permalink / raw)
  To: kvm, mst, linux-kernel, willy, mhocko, linux-mm, akpm, mgorman, vbabka
  Cc: yang.zhang.wz, nitesh, konrad.wilk, david, pagupta, riel,
	lcapitulino, dave.hansen, wei.w.wang, aarcange, pbonzini,
	dan.j.williams, alexander.h.duyck, osalvador

From: Alexander Duyck <alexander.h.duyck@linux.intel.com>

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.

Signed-off-by: Alexander Duyck <alexander.h.duyck@linux.intel.com>
---
 Documentation/vm/free_page_reporting.rst |   41 ++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
 create mode 100644 Documentation/vm/free_page_reporting.rst

diff --git a/Documentation/vm/free_page_reporting.rst b/Documentation/vm/free_page_reporting.rst
new file mode 100644
index 000000000000..33f54a450a4a
--- /dev/null
+++ b/Documentation/vm/free_page_reporting.rst
@@ -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
+


^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [PATCH v15 QEMU 1/3] virtio-ballon: Implement support for page poison tracking feature
  2019-12-05 16:22 [PATCH v15 0/7] mm / virtio: Provide support for free page reporting Alexander Duyck
                   ` (6 preceding siblings ...)
  2019-12-05 16:23 ` [PATCH v15 7/7] mm: Add free page reporting documentation Alexander Duyck
@ 2019-12-05 16:24 ` Alexander Duyck
  2019-12-05 16:24 ` [PATCH v15 QEMU 2/3] virtio-balloon: Add bit to notify guest of unused page reporting Alexander Duyck
                   ` (4 subsequent siblings)
  12 siblings, 0 replies; 39+ messages in thread
From: Alexander Duyck @ 2019-12-05 16:24 UTC (permalink / raw)
  To: virtio-dev, kvm, mst, linux-kernel, willy, mhocko, linux-mm,
	akpm, mgorman, vbabka
  Cc: yang.zhang.wz, nitesh, konrad.wilk, david, pagupta, riel,
	lcapitulino, dave.hansen, wei.w.wang, aarcange, pbonzini,
	dan.j.williams, alexander.h.duyck, osalvador

From: Alexander Duyck <alexander.h.duyck@linux.intel.com>

We need to make certain to advertise support for page poison tracking if
we want to actually get data on if the guest will be poisoning pages. So
if free page hinting is active we should add page poisoning support and
let the guest disable it if it isn't using it.

Page poisoning will result in a page being dirtied on free. As such we
cannot really avoid having to copy the page at least one more time since
we will need to write the poison value to the destination. As such we can
just ignore free page hinting if page poisoning is enabled as it will
actually reduce the work we have to do.

Signed-off-by: Alexander Duyck <alexander.h.duyck@linux.intel.com>
---
 hw/virtio/virtio-balloon.c         |   25 +++++++++++++++++++++----
 include/hw/virtio/virtio-balloon.h |    1 +
 2 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/hw/virtio/virtio-balloon.c b/hw/virtio/virtio-balloon.c
index 40b04f518028..6ecfec422309 100644
--- a/hw/virtio/virtio-balloon.c
+++ b/hw/virtio/virtio-balloon.c
@@ -531,6 +531,15 @@ static void virtio_balloon_free_page_start(VirtIOBalloon *s)
         return;
     }
 
+    /*
+     * If page poisoning is enabled then we probably shouldn't bother with
+     * the hinting since the poisoning will dirty the page and invalidate
+     * the work we are doing anyway.
+     */
+    if (virtio_vdev_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON)) {
+        return;
+    }
+
     if (s->free_page_report_cmd_id == UINT_MAX) {
         s->free_page_report_cmd_id =
                        VIRTIO_BALLOON_FREE_PAGE_REPORT_CMD_ID_MIN;
@@ -618,12 +627,10 @@ static size_t virtio_balloon_config_size(VirtIOBalloon *s)
     if (s->qemu_4_0_config_size) {
         return sizeof(struct virtio_balloon_config);
     }
-    if (virtio_has_feature(features, VIRTIO_BALLOON_F_PAGE_POISON)) {
+    if (virtio_has_feature(features, VIRTIO_BALLOON_F_PAGE_POISON) ||
+        virtio_has_feature(features, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
         return sizeof(struct virtio_balloon_config);
     }
-    if (virtio_has_feature(features, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
-        return offsetof(struct virtio_balloon_config, poison_val);
-    }
     return offsetof(struct virtio_balloon_config, free_page_report_cmd_id);
 }
 
@@ -634,6 +641,7 @@ static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data)
 
     config.num_pages = cpu_to_le32(dev->num_pages);
     config.actual = cpu_to_le32(dev->actual);
+    config.poison_val = cpu_to_le32(dev->poison_val);
 
     if (dev->free_page_report_status == FREE_PAGE_REPORT_S_REQUESTED) {
         config.free_page_report_cmd_id =
@@ -697,6 +705,8 @@ static void virtio_balloon_set_config(VirtIODevice *vdev,
         qapi_event_send_balloon_change(vm_ram_size -
                         ((ram_addr_t) dev->actual << VIRTIO_BALLOON_PFN_SHIFT));
     }
+    dev->poison_val = virtio_vdev_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON) ? 
+                      le32_to_cpu(config.poison_val) : 0;
     trace_virtio_balloon_set_config(dev->actual, oldactual);
 }
 
@@ -706,6 +716,9 @@ static uint64_t virtio_balloon_get_features(VirtIODevice *vdev, uint64_t f,
     VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
     f |= dev->host_features;
     virtio_add_feature(&f, VIRTIO_BALLOON_F_STATS_VQ);
+    if (virtio_has_feature(f, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
+        virtio_add_feature(&f, VIRTIO_BALLOON_F_PAGE_POISON);
+    }
 
     return f;
 }
@@ -847,6 +860,8 @@ static void virtio_balloon_device_reset(VirtIODevice *vdev)
         g_free(s->stats_vq_elem);
         s->stats_vq_elem = NULL;
     }
+
+    s->poison_val = 0;
 }
 
 static void virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status)
@@ -909,6 +924,8 @@ static Property virtio_balloon_properties[] = {
                     VIRTIO_BALLOON_F_DEFLATE_ON_OOM, false),
     DEFINE_PROP_BIT("free-page-hint", VirtIOBalloon, host_features,
                     VIRTIO_BALLOON_F_FREE_PAGE_HINT, false),
+    DEFINE_PROP_BIT("x-page-poison", VirtIOBalloon, host_features,
+                    VIRTIO_BALLOON_F_PAGE_POISON, false),
     /* QEMU 4.0 accidentally changed the config size even when free-page-hint
      * is disabled, resulting in QEMU 3.1 migration incompatibility.  This
      * property retains this quirk for QEMU 4.1 machine types.
diff --git a/include/hw/virtio/virtio-balloon.h b/include/hw/virtio/virtio-balloon.h
index d1c968d2376e..7fe78e5c14d7 100644
--- a/include/hw/virtio/virtio-balloon.h
+++ b/include/hw/virtio/virtio-balloon.h
@@ -70,6 +70,7 @@ typedef struct VirtIOBalloon {
     uint32_t host_features;
 
     bool qemu_4_0_config_size;
+    uint32_t poison_val;
 } VirtIOBalloon;
 
 #endif


^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [PATCH v15 QEMU 2/3] virtio-balloon: Add bit to notify guest of unused page reporting
  2019-12-05 16:22 [PATCH v15 0/7] mm / virtio: Provide support for free page reporting Alexander Duyck
                   ` (7 preceding siblings ...)
  2019-12-05 16:24 ` [PATCH v15 QEMU 1/3] virtio-ballon: Implement support for page poison tracking feature Alexander Duyck
@ 2019-12-05 16:24 ` Alexander Duyck
  2019-12-05 16:24 ` [PATCH v15 QEMU 3/3] virtio-balloon: Provide a interface for " Alexander Duyck
                   ` (3 subsequent siblings)
  12 siblings, 0 replies; 39+ messages in thread
From: Alexander Duyck @ 2019-12-05 16:24 UTC (permalink / raw)
  To: virtio-dev, kvm, mst, linux-kernel, willy, mhocko, linux-mm,
	akpm, mgorman, vbabka
  Cc: yang.zhang.wz, nitesh, konrad.wilk, david, pagupta, riel,
	lcapitulino, dave.hansen, wei.w.wang, aarcange, pbonzini,
	dan.j.williams, alexander.h.duyck, osalvador

From: Alexander Duyck <alexander.h.duyck@linux.intel.com>

Add a bit for the page reporting feature provided by virtio-balloon.

This patch should be replaced once the feature is added to the Linux kernel
and the bit is backported into this exported kernel header.

Signed-off-by: Alexander Duyck <alexander.h.duyck@linux.intel.com>
---
 include/standard-headers/linux/virtio_balloon.h |    1 +
 1 file changed, 1 insertion(+)

diff --git a/include/standard-headers/linux/virtio_balloon.h b/include/standard-headers/linux/virtio_balloon.h
index 9375ca2a70de..1c5f6d6f2de6 100644
--- a/include/standard-headers/linux/virtio_balloon.h
+++ b/include/standard-headers/linux/virtio_balloon.h
@@ -36,6 +36,7 @@
 #define VIRTIO_BALLOON_F_DEFLATE_ON_OOM	2 /* Deflate balloon on OOM */
 #define VIRTIO_BALLOON_F_FREE_PAGE_HINT	3 /* VQ to report free pages */
 #define VIRTIO_BALLOON_F_PAGE_POISON	4 /* Guest is using page poisoning */
+#define VIRTIO_BALLOON_F_REPORTING	5 /* Page reporting virtqueue */
 
 /* Size of a PFN in the balloon interface. */
 #define VIRTIO_BALLOON_PFN_SHIFT 12


^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [PATCH v15 QEMU 3/3] virtio-balloon: Provide a interface for unused page reporting
  2019-12-05 16:22 [PATCH v15 0/7] mm / virtio: Provide support for free page reporting Alexander Duyck
                   ` (8 preceding siblings ...)
  2019-12-05 16:24 ` [PATCH v15 QEMU 2/3] virtio-balloon: Add bit to notify guest of unused page reporting Alexander Duyck
@ 2019-12-05 16:24 ` Alexander Duyck
  2019-12-05 16:26 ` [PATCH v15 QEMU 4/3 RFC] memory: Add support for MADV_FREE as mechanism to lazy discard pages Alexander Duyck
                   ` (2 subsequent siblings)
  12 siblings, 0 replies; 39+ messages in thread
From: Alexander Duyck @ 2019-12-05 16:24 UTC (permalink / raw)
  To: virtio-dev, kvm, mst, linux-kernel, willy, mhocko, linux-mm,
	akpm, mgorman, vbabka
  Cc: yang.zhang.wz, nitesh, konrad.wilk, david, pagupta, riel,
	lcapitulino, dave.hansen, wei.w.wang, aarcange, pbonzini,
	dan.j.williams, alexander.h.duyck, osalvador

From: Alexander Duyck <alexander.h.duyck@linux.intel.com>

Add support for what I am referring to as "unused page reporting".
Basically the idea is to function very similar to how the balloon works
in that we basically end up madvising the page as not being used. However
we don't really need to bother with any deflate type logic since the page
will be faulted back into the guest when it is read or written to.

This is meant to be a simplification of the existing balloon interface
to use for providing hints to what memory needs to be freed. I am assuming
this is safe to do as the deflate logic does not actually appear to do very
much other than tracking what subpages have been released and which ones
haven't.

Signed-off-by: Alexander Duyck <alexander.h.duyck@linux.intel.com>
---
 hw/virtio/virtio-balloon.c         |   46 ++++++++++++++++++++++++++++++++++--
 include/hw/virtio/virtio-balloon.h |    2 +-
 2 files changed, 45 insertions(+), 3 deletions(-)

diff --git a/hw/virtio/virtio-balloon.c b/hw/virtio/virtio-balloon.c
index 6ecfec422309..47f253d016db 100644
--- a/hw/virtio/virtio-balloon.c
+++ b/hw/virtio/virtio-balloon.c
@@ -321,6 +321,40 @@ static void balloon_stats_set_poll_interval(Object *obj, Visitor *v,
     balloon_stats_change_timer(s, 0);
 }
 
+static void virtio_balloon_handle_report(VirtIODevice *vdev, VirtQueue *vq)
+{
+    VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
+    VirtQueueElement *elem;
+
+    while ((elem = virtqueue_pop(vq, sizeof(VirtQueueElement)))) {
+    	unsigned int i;
+
+        for (i = 0; i < elem->in_num; i++) {
+            void *addr = elem->in_sg[i].iov_base;
+            size_t size = elem->in_sg[i].iov_len;
+            ram_addr_t ram_offset;
+            size_t rb_page_size;
+            RAMBlock *rb;
+
+            if (qemu_balloon_is_inhibited() || dev->poison_val)
+                continue;
+
+            rb = qemu_ram_block_from_host(addr, false, &ram_offset);
+            rb_page_size = qemu_ram_pagesize(rb);
+
+            /* For now we will simply ignore unaligned memory regions */
+            if ((ram_offset | size) & (rb_page_size - 1))
+                continue;
+
+            ram_block_discard_range(rb, ram_offset, size);
+        }
+
+        virtqueue_push(vq, elem, 0);
+        virtio_notify(vdev, vq);
+        g_free(elem);
+    }
+}
+
 static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
 {
     VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
@@ -628,7 +662,8 @@ static size_t virtio_balloon_config_size(VirtIOBalloon *s)
         return sizeof(struct virtio_balloon_config);
     }
     if (virtio_has_feature(features, VIRTIO_BALLOON_F_PAGE_POISON) ||
-        virtio_has_feature(features, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
+        virtio_has_feature(features, VIRTIO_BALLOON_F_FREE_PAGE_HINT) ||
+        virtio_has_feature(features, VIRTIO_BALLOON_F_REPORTING)) {
         return sizeof(struct virtio_balloon_config);
     }
     return offsetof(struct virtio_balloon_config, free_page_report_cmd_id);
@@ -716,7 +751,8 @@ static uint64_t virtio_balloon_get_features(VirtIODevice *vdev, uint64_t f,
     VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
     f |= dev->host_features;
     virtio_add_feature(&f, VIRTIO_BALLOON_F_STATS_VQ);
-    if (virtio_has_feature(f, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
+    if (virtio_has_feature(f, VIRTIO_BALLOON_F_FREE_PAGE_HINT) ||
+        virtio_has_feature(f, VIRTIO_BALLOON_F_REPORTING)) {
         virtio_add_feature(&f, VIRTIO_BALLOON_F_PAGE_POISON);
     }
 
@@ -806,6 +842,10 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
     s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
     s->svq = virtio_add_queue(vdev, 128, virtio_balloon_receive_stats);
 
+    if (virtio_has_feature(s->host_features, VIRTIO_BALLOON_F_REPORTING)) {
+        s->rvq = virtio_add_queue(vdev, 32, virtio_balloon_handle_report);
+    }
+
     if (virtio_has_feature(s->host_features,
                            VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
         s->free_page_vq = virtio_add_queue(vdev, VIRTQUEUE_MAX_SIZE,
@@ -932,6 +972,8 @@ static Property virtio_balloon_properties[] = {
      */
     DEFINE_PROP_BOOL("qemu-4-0-config-size", VirtIOBalloon,
                      qemu_4_0_config_size, false),
+    DEFINE_PROP_BIT("unused-page-reporting", VirtIOBalloon, host_features,
+                    VIRTIO_BALLOON_F_REPORTING, true),
     DEFINE_PROP_LINK("iothread", VirtIOBalloon, iothread, TYPE_IOTHREAD,
                      IOThread *),
     DEFINE_PROP_END_OF_LIST(),
diff --git a/include/hw/virtio/virtio-balloon.h b/include/hw/virtio/virtio-balloon.h
index 7fe78e5c14d7..db5bf7127112 100644
--- a/include/hw/virtio/virtio-balloon.h
+++ b/include/hw/virtio/virtio-balloon.h
@@ -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;


^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [PATCH v15 QEMU 4/3 RFC] memory: Add support for MADV_FREE as mechanism to lazy discard pages
  2019-12-05 16:22 [PATCH v15 0/7] mm / virtio: Provide support for free page reporting Alexander Duyck
                   ` (9 preceding siblings ...)
  2019-12-05 16:24 ` [PATCH v15 QEMU 3/3] virtio-balloon: Provide a interface for " Alexander Duyck
@ 2019-12-05 16:26 ` Alexander Duyck
  2019-12-12 23:47 ` [PATCH v15 0/7] mm / virtio: Provide support for free page reporting Alexander Duyck
  2019-12-13 10:00 ` David Hildenbrand
  12 siblings, 0 replies; 39+ messages in thread
From: Alexander Duyck @ 2019-12-05 16:26 UTC (permalink / raw)
  To: virtio-dev, kvm, mst, linux-kernel, willy, mhocko, linux-mm,
	akpm, mgorman, vbabka
  Cc: yang.zhang.wz, nitesh, konrad.wilk, david, pagupta, riel,
	lcapitulino, dave.hansen, wei.w.wang, aarcange, pbonzini,
	dan.j.williams, alexander.h.duyck, osalvador

From: Alexander Duyck <alexander.h.duyck@linux.intel.com>

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.

Signed-off-by: Alexander Duyck <alexander.h.duyck@linux.intel.com>
---
 exec.c                     |   39 +++++++++++++++++++++++++++------------
 hw/virtio/virtio-balloon.c |    2 +-
 include/exec/cpu-common.h  |    1 +
 3 files changed, 29 insertions(+), 13 deletions(-)

diff --git a/exec.c b/exec.c
index ffdb5185353b..14eda993058c 100644
--- a/exec.c
+++ b/exec.c
@@ -3843,15 +3843,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;
 
@@ -3904,13 +3897,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 "
@@ -3938,6 +3936,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;
diff --git a/hw/virtio/virtio-balloon.c b/hw/virtio/virtio-balloon.c
index 47f253d016db..b904bdde8b1b 100644
--- a/hw/virtio/virtio-balloon.c
+++ b/hw/virtio/virtio-balloon.c
@@ -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);
diff --git a/include/exec/cpu-common.h b/include/exec/cpu-common.h
index 81753bbb3431..2bbd26784c63 100644
--- a/include/exec/cpu-common.h
+++ b/include/exec/cpu-common.h
@@ -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
 


^ permalink raw reply related	[flat|nested] 39+ messages in thread

* Re: [PATCH v15 0/7] mm / virtio: Provide support for free page reporting
  2019-12-05 16:22 [PATCH v15 0/7] mm / virtio: Provide support for free page reporting Alexander Duyck
                   ` (10 preceding siblings ...)
  2019-12-05 16:26 ` [PATCH v15 QEMU 4/3 RFC] memory: Add support for MADV_FREE as mechanism to lazy discard pages Alexander Duyck
@ 2019-12-12 23:47 ` Alexander Duyck
  2019-12-13 10:00 ` David Hildenbrand
  12 siblings, 0 replies; 39+ messages in thread
From: Alexander Duyck @ 2019-12-12 23:47 UTC (permalink / raw)
  To: Alexander Duyck, kvm, mst, linux-kernel, willy, mhocko, linux-mm,
	akpm, mgorman, vbabka
  Cc: yang.zhang.wz, nitesh, konrad.wilk, david, pagupta, riel,
	lcapitulino, dave.hansen, wei.w.wang, aarcange, pbonzini,
	dan.j.williams, osalvador

On Thu, 2019-12-05 at 08:22 -0800, Alexander Duyck wrote:
> 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.

<snip>

> Changes from v14:
> https://lore.kernel.org/lkml/20191119214454.24996.66289.stgit@localhost.localdomain/
> 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

It has been about a week since I posted v15 and haven't heard anything.
Consider this a gentle ping.

I'm looking for input on patches 3 and 4 in this set as I updated them to
address most of the concerns Mel had. Just wondering if the set needs
additional work or if we are good with this as a starting point for this
feature?

Thanks.

- Alex


^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v15 5/7] virtio-balloon: Pull page poisoning config out of free page hinting
  2019-12-05 16:22 ` [PATCH v15 5/7] virtio-balloon: Pull page poisoning config out of free page hinting Alexander Duyck
@ 2019-12-13  7:03   ` Michael S. Tsirkin
  0 siblings, 0 replies; 39+ messages in thread
From: Michael S. Tsirkin @ 2019-12-13  7:03 UTC (permalink / raw)
  To: Alexander Duyck
  Cc: kvm, linux-kernel, willy, mhocko, linux-mm, akpm, mgorman,
	vbabka, yang.zhang.wz, nitesh, konrad.wilk, david, pagupta, riel,
	lcapitulino, dave.hansen, wei.w.wang, aarcange, pbonzini,
	dan.j.williams, alexander.h.duyck, osalvador

On Thu, Dec 05, 2019 at 08:22:47AM -0800, Alexander Duyck wrote:
> From: Alexander Duyck <alexander.h.duyck@linux.intel.com>
> 
> 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.
> 
> Reviewed-by: David Hildenbrand <david@redhat.com>
> Signed-off-by: Alexander Duyck <alexander.h.duyck@linux.intel.com>

Acked-by: Michael S. Tsirkin <mst@redhat.com>

> ---
>  drivers/virtio/virtio_balloon.c |   23 +++++++++++++++++------
>  1 file changed, 17 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
> index 15b7f1d8c334..252591bc7e01 100644
> --- a/drivers/virtio/virtio_balloon.c
> +++ b/drivers/virtio/virtio_balloon.c
> @@ -849,7 +849,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) {
> @@ -916,11 +915,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
> @@ -1021,7 +1029,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);


^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v15 6/7] virtio-balloon: Add support for providing free page reports to host
  2019-12-05 16:22 ` [PATCH v15 6/7] virtio-balloon: Add support for providing free page reports to host Alexander Duyck
@ 2019-12-13  7:08   ` Michael S. Tsirkin
  2019-12-13 16:35     ` Alexander Duyck
  2019-12-13 10:15   ` David Hildenbrand
  1 sibling, 1 reply; 39+ messages in thread
From: Michael S. Tsirkin @ 2019-12-13  7:08 UTC (permalink / raw)
  To: Alexander Duyck
  Cc: kvm, linux-kernel, willy, mhocko, linux-mm, akpm, mgorman,
	vbabka, yang.zhang.wz, nitesh, konrad.wilk, david, pagupta, riel,
	lcapitulino, dave.hansen, wei.w.wang, aarcange, pbonzini,
	dan.j.williams, alexander.h.duyck, osalvador

On Thu, Dec 05, 2019 at 08:22:55AM -0800, Alexander Duyck wrote:
> From: Alexander Duyck <alexander.h.duyck@linux.intel.com>
> 
> 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.
> 
> For this reason when I had originally introduced the patch set I referred
> to this behavior as a "bubble" instead of a "balloon" since the duration
> is short lived, and when the page is touched the "bubble" is popped and
> the page is faulted back in.
> 
> Signed-off-by: Alexander Duyck <alexander.h.duyck@linux.intel.com>


virtio POV is fine here:

Acked-by: Michael S. Tsirkin <mst@redhat.com>

However please copy virtio-comment on UAPI changes.
If possible isolate the last chunk in a patch by itself
to make it easier for non-kernel developers to review.

> ---
>  drivers/virtio/Kconfig              |    1 +
>  drivers/virtio/virtio_balloon.c     |   64 +++++++++++++++++++++++++++++++++++
>  include/uapi/linux/virtio_balloon.h |    1 +
>  3 files changed, 66 insertions(+)
> 
> diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig
> index 078615cf2afc..4b2dd8259ff5 100644
> --- a/drivers/virtio/Kconfig
> +++ b/drivers/virtio/Kconfig
> @@ -58,6 +58,7 @@ config VIRTIO_BALLOON
>  	tristate "Virtio balloon driver"
>  	depends on VIRTIO
>  	select MEMORY_BALLOON
> +	select PAGE_REPORTING
>  	---help---
>  	 This driver supports increasing and decreasing the amount
>  	 of memory within a KVM guest.
> diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
> index 252591bc7e01..ecd54edba968 100644
> --- a/drivers/virtio/virtio_balloon.c
> +++ b/drivers/virtio/virtio_balloon.c
> @@ -19,6 +19,7 @@
>  #include <linux/mount.h>
>  #include <linux/magic.h>
>  #include <linux/pseudo_fs.h>
> +#include <linux/page_reporting.h>
>  
>  /*
>   * Balloon device works in 4K page units.  So each page is pointed to by
> @@ -47,6 +48,7 @@ enum virtio_balloon_vq {
>  	VIRTIO_BALLOON_VQ_DEFLATE,
>  	VIRTIO_BALLOON_VQ_STATS,
>  	VIRTIO_BALLOON_VQ_FREE_PAGE,
> +	VIRTIO_BALLOON_VQ_REPORTING,
>  	VIRTIO_BALLOON_VQ_MAX
>  };
>  
> @@ -114,6 +116,10 @@ struct virtio_balloon {
>  
>  	/* To register a shrinker to shrink memory upon memory pressure */
>  	struct shrinker shrinker;
> +
> +	/* Free page reporting device */
> +	struct virtqueue *reporting_vq;
> +	struct page_reporting_dev_info pr_dev_info;
>  };
>  
>  static struct virtio_device_id id_table[] = {
> @@ -153,6 +159,33 @@ static void tell_host(struct virtio_balloon *vb, struct virtqueue *vq)
>  
>  }
>  
> +int virtballoon_free_page_report(struct page_reporting_dev_info *pr_dev_info,
> +				   struct scatterlist *sg, unsigned int nents)
> +{
> +	struct virtio_balloon *vb =
> +		container_of(pr_dev_info, struct virtio_balloon, pr_dev_info);
> +	struct virtqueue *vq = vb->reporting_vq;
> +	unsigned int unused, err;
> +
> +	/* We should always be able to add these buffers to an empty queue. */
> +	err = virtqueue_add_inbuf(vq, sg, nents, vb, GFP_NOWAIT | __GFP_NOWARN);
> +
> +	/*
> +	 * In the extremely unlikely case that something has occurred and we
> +	 * are able to trigger an error we will simply display a warning
> +	 * and exit without actually processing the pages.
> +	 */
> +	if (WARN_ON_ONCE(err))
> +		return err;
> +
> +	virtqueue_kick(vq);
> +
> +	/* When host has read buffer, this completes via balloon_ack */
> +	wait_event(vb->acked, virtqueue_get_buf(vq, &unused));
> +
> +	return 0;
> +}
> +
>  static void set_page_pfns(struct virtio_balloon *vb,
>  			  __virtio32 pfns[], struct page *page)
>  {
> @@ -477,6 +510,7 @@ static int init_vqs(struct virtio_balloon *vb)
>  	names[VIRTIO_BALLOON_VQ_DEFLATE] = "deflate";
>  	names[VIRTIO_BALLOON_VQ_STATS] = NULL;
>  	names[VIRTIO_BALLOON_VQ_FREE_PAGE] = NULL;
> +	names[VIRTIO_BALLOON_VQ_REPORTING] = NULL;
>  
>  	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) {
>  		names[VIRTIO_BALLOON_VQ_STATS] = "stats";
> @@ -488,6 +522,11 @@ static int init_vqs(struct virtio_balloon *vb)
>  		callbacks[VIRTIO_BALLOON_VQ_FREE_PAGE] = NULL;
>  	}
>  
> +	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) {
> +		names[VIRTIO_BALLOON_VQ_REPORTING] = "reporting_vq";
> +		callbacks[VIRTIO_BALLOON_VQ_REPORTING] = balloon_ack;
> +	}
> +
>  	err = vb->vdev->config->find_vqs(vb->vdev, VIRTIO_BALLOON_VQ_MAX,
>  					 vqs, callbacks, names, NULL, NULL);
>  	if (err)
> @@ -520,6 +559,9 @@ static int init_vqs(struct virtio_balloon *vb)
>  	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
>  		vb->free_page_vq = vqs[VIRTIO_BALLOON_VQ_FREE_PAGE];
>  
> +	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
> +		vb->reporting_vq = vqs[VIRTIO_BALLOON_VQ_REPORTING];
> +
>  	return 0;
>  }
>  
> @@ -939,12 +981,31 @@ static int virtballoon_probe(struct virtio_device *vdev)
>  		if (err)
>  			goto out_del_balloon_wq;
>  	}
> +
> +	vb->pr_dev_info.report = virtballoon_free_page_report;
> +	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) {
> +		unsigned int capacity;
> +
> +		capacity = virtqueue_get_vring_size(vb->reporting_vq);
> +		if (capacity < PAGE_REPORTING_CAPACITY) {
> +			err = -ENOSPC;
> +			goto out_unregister_shrinker;
> +		}
> +
> +		err = page_reporting_register(&vb->pr_dev_info);
> +		if (err)
> +			goto out_unregister_shrinker;
> +	}
> +
>  	virtio_device_ready(vdev);
>  
>  	if (towards_target(vb))
>  		virtballoon_changed(vdev);
>  	return 0;
>  
> +out_unregister_shrinker:
> +	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
> +		virtio_balloon_unregister_shrinker(vb);
>  out_del_balloon_wq:
>  	if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
>  		destroy_workqueue(vb->balloon_wq);
> @@ -973,6 +1034,8 @@ static void virtballoon_remove(struct virtio_device *vdev)
>  {
>  	struct virtio_balloon *vb = vdev->priv;
>  
> +	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
> +		page_reporting_unregister(&vb->pr_dev_info);
>  	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
>  		virtio_balloon_unregister_shrinker(vb);
>  	spin_lock_irq(&vb->stop_update_lock);
> @@ -1045,6 +1108,7 @@ static int virtballoon_validate(struct virtio_device *vdev)
>  	VIRTIO_BALLOON_F_DEFLATE_ON_OOM,
>  	VIRTIO_BALLOON_F_FREE_PAGE_HINT,
>  	VIRTIO_BALLOON_F_PAGE_POISON,
> +	VIRTIO_BALLOON_F_REPORTING,
>  };
>  
>  static struct virtio_driver virtio_balloon_driver = {
> diff --git a/include/uapi/linux/virtio_balloon.h b/include/uapi/linux/virtio_balloon.h
> index a1966cd7b677..19974392d324 100644
> --- a/include/uapi/linux/virtio_balloon.h
> +++ b/include/uapi/linux/virtio_balloon.h
> @@ -36,6 +36,7 @@
>  #define VIRTIO_BALLOON_F_DEFLATE_ON_OOM	2 /* Deflate balloon on OOM */
>  #define VIRTIO_BALLOON_F_FREE_PAGE_HINT	3 /* VQ to report free pages */
>  #define VIRTIO_BALLOON_F_PAGE_POISON	4 /* Guest is using page poisoning */
> +#define VIRTIO_BALLOON_F_REPORTING	5 /* Page reporting virtqueue */
>  
>  /* Size of a PFN in the balloon interface. */
>  #define VIRTIO_BALLOON_PFN_SHIFT 12


^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v15 0/7] mm / virtio: Provide support for free page reporting
  2019-12-05 16:22 [PATCH v15 0/7] mm / virtio: Provide support for free page reporting Alexander Duyck
                   ` (11 preceding siblings ...)
  2019-12-12 23:47 ` [PATCH v15 0/7] mm / virtio: Provide support for free page reporting Alexander Duyck
@ 2019-12-13 10:00 ` David Hildenbrand
  2019-12-13 11:08   ` Mel Gorman
  2019-12-13 16:46   ` Alexander Duyck
  12 siblings, 2 replies; 39+ messages in thread
From: David Hildenbrand @ 2019-12-13 10:00 UTC (permalink / raw)
  To: Alexander Duyck, kvm, mst, linux-kernel, willy, mhocko, linux-mm,
	akpm, mgorman, vbabka
  Cc: yang.zhang.wz, nitesh, konrad.wilk, pagupta, riel, lcapitulino,
	dave.hansen, wei.w.wang, aarcange, pbonzini, dan.j.williams,
	alexander.h.duyck, osalvador

On 05.12.19 17:22, Alexander Duyck wrote:
> 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. Currently the order
> used 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, processed as many pages as were listed in
> nr_free prior to us starting, or have filled the scatterlist with pages to
> be reported. 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 power saving features disabled
> by setting the /dev/cpu_dma_latency value to 10ms.
> 
> Test                   page_fault1 (THP)    page_fault2
> Name            tasks  Process Iter  STDEV  Process Iter  STDEV
> Baseline            1    1208307.25  0.10%     408596.00  0.19%
>                    16    8865204.75  0.16%    3344169.00  0.60%
> 
> Patches applied     1    1206809.00  0.26%     412558.25  0.32%
>                    16    8814350.50  0.78%    3420102.00  1.16%
> 
> Patches enabled     1    1201386.25  0.21%     407903.75  0.32%
>                    16    8880178.00  0.08%    3396700.50  0.54%
> 
> Patches enabled     1    1173529.00  1.04%     409006.50  0.45%
>  page shuffle      16    8384540.25  0.74%    3288289.25  0.41%
> 
> Patches enabled     1    1193411.00  0.33%     406333.50  0.09%
>  shuffle w/ RFC    16    8812639.75  0.73%    3321706.25  0.53%
> 
> The results above are for a baseline with a linux-next-20191203 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.
> As seen in the data above, using MADV_FREE in QEMU mostly eliminates this
> overhead.
> 
> 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/29f43d5796feed0dec8e8bb98b187d9dac03b900.camel@linux.intel.com/
> 
> Changes from v13:
> https://lore.kernel.org/lkml/20191105215940.15144.65968.stgit@localhost.localdomain/
> Rewrote core reporting functionality
>   Merged patches 3 & 4
>   Dropped boundary list and related code
>   Folded get_reported_page into page_reporting_fill
>   Folded page_reporting_fill into page_reporting_cycle
> Pulled reporting functionality out of free_reported_page
>   Renamed it to __free_isolated_page
>   Moved page reporting specific bits to page_reporting_drain
> Renamed phdev to prdev since we aren't "hinting" we are "reporting"
> Added documentation to describe the usage of unused page reporting
> Updated cover page and patch descriptions to avoid mention of boundary
> 
> Changes from v14:
> https://lore.kernel.org/lkml/20191119214454.24996.66289.stgit@localhost.localdomain/
> 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

Last time Mel said

"Ok, I'm ok with how this hooks into the allocator as the overhead is
minimal. However, the patch itself still includes a number of
optimisations instead of being a bare-boned implementation of the
feature with optimisations layered on top."

and

"Either way, the separate patch could have supporting data on how much
it improves the speed of reporting pages so it can be compared to any
other optimisation that may be proposed. Supporting data would also help
make the case that any complexity introduced by the optimisation is
worthwhile."

But I was only partially following that discussion.

I can see that there is only one additional patch (before the reporting
one) compared to the previous series on the MM side. Does that comment
no longer apply (I can see e.g., "Removed REPORTING_REQUESTED bit used
in zone flags" in the changelog) - IOW, did you drop all the
optimizations in question for now? If so, can you share some performance
differences with and without the previous optimizations? (just out of
personal interest :) )

Christmas is getting closer, and at least in Europe/Germany that usually
means that things will slow down ... or however you want to call that.
So I wouldn't expect too much review happening before next year (but I
might be wrong of course).

Cheers!

-- 
Thanks,

David / dhildenb


^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v15 6/7] virtio-balloon: Add support for providing free page reports to host
  2019-12-05 16:22 ` [PATCH v15 6/7] virtio-balloon: Add support for providing free page reports to host Alexander Duyck
  2019-12-13  7:08   ` Michael S. Tsirkin
@ 2019-12-13 10:15   ` David Hildenbrand
  2019-12-13 16:37     ` Alexander Duyck
  1 sibling, 1 reply; 39+ messages in thread
From: David Hildenbrand @ 2019-12-13 10:15 UTC (permalink / raw)
  To: Alexander Duyck, kvm, mst, linux-kernel, willy, mhocko, linux-mm,
	akpm, mgorman, vbabka
  Cc: yang.zhang.wz, nitesh, konrad.wilk, pagupta, riel, lcapitulino,
	dave.hansen, wei.w.wang, aarcange, pbonzini, dan.j.williams,
	alexander.h.duyck, osalvador

On 05.12.19 17:22, Alexander Duyck wrote:
> From: Alexander Duyck <alexander.h.duyck@linux.intel.com>
> 
> 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.
> 
> For this reason when I had originally introduced the patch set I referred
> to this behavior as a "bubble" instead of a "balloon" since the duration
> is short lived, and when the page is touched the "bubble" is popped and
> the page is faulted back in.

While an interesting read, I would drop that comment as it isn't really
of value for the code/codebase itself.

[...]

> +
> +	vb->pr_dev_info.report = virtballoon_free_page_report;
> +	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) {
> +		unsigned int capacity;
> +
> +		capacity = virtqueue_get_vring_size(vb->reporting_vq);
> +		if (capacity < PAGE_REPORTING_CAPACITY) {
> +			err = -ENOSPC;
> +			goto out_unregister_shrinker;

It's somewhat strange to fail loading the balloon completely here.
Wouldn't it be better to print e.g. a warning but continue without free
page reporting?

(I guess splitting up the list can be done in an addon patch if ever
really needed for virtio-balloon)

Apart from that

Reviewed-by: David Hildenbrand <david@redhat.com>

Thanks!

-- 
Thanks,

David / dhildenb


^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v15 0/7] mm / virtio: Provide support for free page reporting
  2019-12-13 10:00 ` David Hildenbrand
@ 2019-12-13 11:08   ` Mel Gorman
  2019-12-13 16:59     ` Alexander Duyck
  2019-12-13 16:46   ` Alexander Duyck
  1 sibling, 1 reply; 39+ messages in thread
From: Mel Gorman @ 2019-12-13 11:08 UTC (permalink / raw)
  To: David Hildenbrand
  Cc: Alexander Duyck, kvm, mst, linux-kernel, willy, mhocko, linux-mm,
	akpm, vbabka, yang.zhang.wz, nitesh, konrad.wilk, pagupta, riel,
	lcapitulino, dave.hansen, wei.w.wang, aarcange, pbonzini,
	dan.j.williams, alexander.h.duyck, osalvador

On Fri, Dec 13, 2019 at 11:00:42AM +0100, David Hildenbrand wrote:
> > A brief history on the background of free page reporting can be found at:
> > https://lore.kernel.org/lkml/29f43d5796feed0dec8e8bb98b187d9dac03b900.camel@linux.intel.com/
> > 
> > Changes from v13:
> > https://lore.kernel.org/lkml/20191105215940.15144.65968.stgit@localhost.localdomain/
> > Rewrote core reporting functionality
> >   Merged patches 3 & 4
> >   Dropped boundary list and related code
> >   Folded get_reported_page into page_reporting_fill
> >   Folded page_reporting_fill into page_reporting_cycle
> > Pulled reporting functionality out of free_reported_page
> >   Renamed it to __free_isolated_page
> >   Moved page reporting specific bits to page_reporting_drain
> > Renamed phdev to prdev since we aren't "hinting" we are "reporting"
> > Added documentation to describe the usage of unused page reporting
> > Updated cover page and patch descriptions to avoid mention of boundary
> > 
> > Changes from v14:
> > https://lore.kernel.org/lkml/20191119214454.24996.66289.stgit@localhost.localdomain/
> > 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
> 
> Last time Mel said
> 
> "Ok, I'm ok with how this hooks into the allocator as the overhead is
> minimal. However, the patch itself still includes a number of
> optimisations instead of being a bare-boned implementation of the
> feature with optimisations layered on top."
> 

I didn't get the chance to take a close look as I'm trying to clear as
much as possible from my table on the run-up to Christmas so I don't come
back to a disaster inbox. I also noted that the Acks for earlier patches
were not included so I was uncertain if doing a full review would still
be a good use of time when time was tight.

That said, some optimisations are still included but much reduced. For
example, list rotations are still there but it's very straight-forward.
The refcount is gone which is good and replaced by a state, which could be
be better documented, but is more straight forward and the zone->lock is
back protecting the free lists primarily and not zone metadata or prdev
metadata (at least not obviously). I didn't put in the time to see if
the atomic_set in page_reporting_process() is ok or whether state could
be lost but I *think* it's ok because it should be called from just one
workqueue request and they shouldn't be stacked. A comment there explaining
why atomic_set is definitely correct would be helpful.

I'm inclined to decide that yes, this version is potentially ok as a
bare minimum but didn't put in the time to be 100% sure.
 
-- 
Mel Gorman
SUSE Labs

^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v15 6/7] virtio-balloon: Add support for providing free page reports to host
  2019-12-13  7:08   ` Michael S. Tsirkin
@ 2019-12-13 16:35     ` Alexander Duyck
  2019-12-15  9:29       ` Michael S. Tsirkin
  0 siblings, 1 reply; 39+ messages in thread
From: Alexander Duyck @ 2019-12-13 16:35 UTC (permalink / raw)
  To: Michael S. Tsirkin, Alexander Duyck
  Cc: kvm, linux-kernel, willy, mhocko, linux-mm, akpm, mgorman,
	vbabka, yang.zhang.wz, nitesh, konrad.wilk, david, pagupta, riel,
	lcapitulino, dave.hansen, wei.w.wang, aarcange, pbonzini,
	dan.j.williams, osalvador

On Fri, 2019-12-13 at 02:08 -0500, Michael S. Tsirkin wrote:
> On Thu, Dec 05, 2019 at 08:22:55AM -0800, Alexander Duyck wrote:
> > From: Alexander Duyck <alexander.h.duyck@linux.intel.com>
> > 
> > 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.
> > 
> > For this reason when I had originally introduced the patch set I referred
> > to this behavior as a "bubble" instead of a "balloon" since the duration
> > is short lived, and when the page is touched the "bubble" is popped and
> > the page is faulted back in.
> > 
> > Signed-off-by: Alexander Duyck <alexander.h.duyck@linux.intel.com>
> 
> virtio POV is fine here:
> 
> Acked-by: Michael S. Tsirkin <mst@redhat.com>
> 
> However please copy virtio-comment on UAPI changes.

So I have been avoiding copying virtio-dev on the kernel changes as I had
gotten feedback that it was annoying some people as they were getting
bounces since they were not subscribed. Will the same type of thing happen
with virtio-comment?

> If possible isolate the last chunk in a patch by itself
> to make it easier for non-kernel developers to review.

Are you talking about the change in "include/uapi/linux/virtio_balloon.h"?

I have it as a standalone patch in the QEMU set, and for the QEMU set I
had included virtio-dev. Would you prefer I include virtio-comment instead
or in addition to virtio-dev? My thought is that I would prefer to keep
the virtio people focused on the QEMU code since they are probably more
comfortable with that, and the kernel people focused on the kernel code.

> > ---
> >  drivers/virtio/Kconfig              |    1 +
> >  drivers/virtio/virtio_balloon.c     |   64 +++++++++++++++++++++++++++++++++++
> >  include/uapi/linux/virtio_balloon.h |    1 +
> >  3 files changed, 66 insertions(+)

<snip>

> > 
> > diff --git a/include/uapi/linux/virtio_balloon.h b/include/uapi/linux/virtio_balloon.h
> > index a1966cd7b677..19974392d324 100644
> > --- a/include/uapi/linux/virtio_balloon.h
> > +++ b/include/uapi/linux/virtio_balloon.h
> > @@ -36,6 +36,7 @@
> >  #define VIRTIO_BALLOON_F_DEFLATE_ON_OOM	2 /* Deflate balloon on OOM */
> >  #define VIRTIO_BALLOON_F_FREE_PAGE_HINT	3 /* VQ to report free pages */
> >  #define VIRTIO_BALLOON_F_PAGE_POISON	4 /* Guest is using page poisoning */
> > +#define VIRTIO_BALLOON_F_REPORTING	5 /* Page reporting virtqueue */
> >  
> >  /* Size of a PFN in the balloon interface. */
> >  #define VIRTIO_BALLOON_PFN_SHIFT 12

If this is the bit we are talking about I have it split out already into a
QEMU specific patch as well, it can be found here:
https://lore.kernel.org/lkml/20191205162422.19737.57728.stgit@localhost.localdomain/

If needed I could probably add a cover page and/or update the comments in
that patch if that is needed to better explain the change.

Thanks.

- Alex


^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v15 6/7] virtio-balloon: Add support for providing free page reports to host
  2019-12-13 10:15   ` David Hildenbrand
@ 2019-12-13 16:37     ` Alexander Duyck
  0 siblings, 0 replies; 39+ messages in thread
From: Alexander Duyck @ 2019-12-13 16:37 UTC (permalink / raw)
  To: David Hildenbrand, Alexander Duyck, kvm, mst, linux-kernel,
	willy, mhocko, linux-mm, akpm, mgorman, vbabka
  Cc: yang.zhang.wz, nitesh, konrad.wilk, pagupta, riel, lcapitulino,
	dave.hansen, wei.w.wang, aarcange, pbonzini, dan.j.williams,
	osalvador

On Fri, 2019-12-13 at 11:15 +0100, David Hildenbrand wrote:
> On 05.12.19 17:22, Alexander Duyck wrote:
> > From: Alexander Duyck <alexander.h.duyck@linux.intel.com>
> > 
> > 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.
> > 
> > For this reason when I had originally introduced the patch set I referred
> > to this behavior as a "bubble" instead of a "balloon" since the duration
> > is short lived, and when the page is touched the "bubble" is popped and
> > the page is faulted back in.
> 
> While an interesting read, I would drop that comment as it isn't really
> of value for the code/codebase itself.

Okay, I can drop the comment.

> [...]
> 
> > +
> > +	vb->pr_dev_info.report = virtballoon_free_page_report;
> > +	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) {
> > +		unsigned int capacity;
> > +
> > +		capacity = virtqueue_get_vring_size(vb->reporting_vq);
> > +		if (capacity < PAGE_REPORTING_CAPACITY) {
> > +			err = -ENOSPC;
> > +			goto out_unregister_shrinker;
> 
> It's somewhat strange to fail loading the balloon completely here.
> Wouldn't it be better to print e.g. a warning but continue without free
> page reporting?
> 
> (I guess splitting up the list can be done in an addon patch if ever
> really needed for virtio-balloon)

That was kind of my thought. Odds are it probably is unlikely to come up.
At least with the code I have now I think the virtqueue size is something
like 128 and the capacity is only 32 as I wanted to limit the number of
pages that were being reported at the time.

> Apart from that
> 
> Reviewed-by: David Hildenbrand <david@redhat.com>
> 
> Thanks!
> 


Thanks for the review.

- Alex


^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v15 0/7] mm / virtio: Provide support for free page reporting
  2019-12-13 10:00 ` David Hildenbrand
  2019-12-13 11:08   ` Mel Gorman
@ 2019-12-13 16:46   ` Alexander Duyck
  2019-12-16 12:21     ` David Hildenbrand
  1 sibling, 1 reply; 39+ messages in thread
From: Alexander Duyck @ 2019-12-13 16:46 UTC (permalink / raw)
  To: David Hildenbrand, Alexander Duyck, kvm, mst, linux-kernel,
	willy, mhocko, linux-mm, akpm, mgorman, vbabka
  Cc: yang.zhang.wz, nitesh, konrad.wilk, pagupta, riel, lcapitulino,
	dave.hansen, wei.w.wang, aarcange, pbonzini, dan.j.williams,
	osalvador

On Fri, 2019-12-13 at 11:00 +0100, David Hildenbrand wrote:
> On 05.12.19 17:22, Alexander Duyck wrote:
> > 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. Currently the order
> > used 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, processed as many pages as were listed in
> > nr_free prior to us starting, or have filled the scatterlist with pages to
> > be reported. 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 power saving features disabled
> > by setting the /dev/cpu_dma_latency value to 10ms.
> > 
> > Test                   page_fault1 (THP)    page_fault2
> > Name            tasks  Process Iter  STDEV  Process Iter  STDEV
> > Baseline            1    1208307.25  0.10%     408596.00  0.19%
> >                    16    8865204.75  0.16%    3344169.00  0.60%
> > 
> > Patches applied     1    1206809.00  0.26%     412558.25  0.32%
> >                    16    8814350.50  0.78%    3420102.00  1.16%
> > 
> > Patches enabled     1    1201386.25  0.21%     407903.75  0.32%
> >                    16    8880178.00  0.08%    3396700.50  0.54%
> > 
> > Patches enabled     1    1173529.00  1.04%     409006.50  0.45%
> >  page shuffle      16    8384540.25  0.74%    3288289.25  0.41%
> > 
> > Patches enabled     1    1193411.00  0.33%     406333.50  0.09%
> >  shuffle w/ RFC    16    8812639.75  0.73%    3321706.25  0.53%
> > 
> > The results above are for a baseline with a linux-next-20191203 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.
> > As seen in the data above, using MADV_FREE in QEMU mostly eliminates this
> > overhead.
> > 
> > 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/29f43d5796feed0dec8e8bb98b187d9dac03b900.camel@linux.intel.com/
> > 
> > Changes from v13:
> > https://lore.kernel.org/lkml/20191105215940.15144.65968.stgit@localhost.localdomain/
> > Rewrote core reporting functionality
> >   Merged patches 3 & 4
> >   Dropped boundary list and related code
> >   Folded get_reported_page into page_reporting_fill
> >   Folded page_reporting_fill into page_reporting_cycle
> > Pulled reporting functionality out of free_reported_page
> >   Renamed it to __free_isolated_page
> >   Moved page reporting specific bits to page_reporting_drain
> > Renamed phdev to prdev since we aren't "hinting" we are "reporting"
> > Added documentation to describe the usage of unused page reporting
> > Updated cover page and patch descriptions to avoid mention of boundary
> > 
> > Changes from v14:
> > https://lore.kernel.org/lkml/20191119214454.24996.66289.stgit@localhost.localdomain/
> > 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
> 
> Last time Mel said
> 
> "Ok, I'm ok with how this hooks into the allocator as the overhead is
> minimal. However, the patch itself still includes a number of
> optimisations instead of being a bare-boned implementation of the
> feature with optimisations layered on top."
> 
> and
> 
> "Either way, the separate patch could have supporting data on how much
> it improves the speed of reporting pages so it can be compared to any
> other optimisation that may be proposed. Supporting data would also help
> make the case that any complexity introduced by the optimisation is
> worthwhile."
> 
> But I was only partially following that discussion.
> 
> I can see that there is only one additional patch (before the reporting
> one) compared to the previous series on the MM side. Does that comment
> no longer apply (I can see e.g., "Removed REPORTING_REQUESTED bit used
> in zone flags" in the changelog) - IOW, did you drop all the
> optimizations in question for now? If so, can you share some performance
> differences with and without the previous optimizations? (just out of
> personal interest :) )

I found alternative approaches and did away with most of them. What I had
done is replaced the REPORTING_REQUESTED and reference count logic with
the state in order to guarantee that we will make our way through the list
and rearm the reporting thread if the work isn't completed.

One thing I still think I need to split out based on Mel's comments is the
list rotation and probably the new budget value I added.

As far as performance this new patch set performs better than the old one
did. Most of that is due to the fact that I increased the delay between
passes and dropped any optimizations for the shuffling code.

> Christmas is getting closer, and at least in Europe/Germany that usually
> means that things will slow down ... or however you want to call that.
> So I wouldn't expect too much review happening before next year (but I
> might be wrong of course).
> 
> Cheers!

That is one of the reasons why I wanted to see if there were any comments
I could get before the break. It gives me a chance to address them and
push one last patch set before I head out on Christmas break myself.


^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v15 0/7] mm / virtio: Provide support for free page reporting
  2019-12-13 11:08   ` Mel Gorman
@ 2019-12-13 16:59     ` Alexander Duyck
  0 siblings, 0 replies; 39+ messages in thread
From: Alexander Duyck @ 2019-12-13 16:59 UTC (permalink / raw)
  To: Mel Gorman, David Hildenbrand
  Cc: Alexander Duyck, kvm, mst, linux-kernel, willy, mhocko, linux-mm,
	akpm, vbabka, yang.zhang.wz, nitesh, konrad.wilk, pagupta, riel,
	lcapitulino, dave.hansen, wei.w.wang, aarcange, pbonzini,
	dan.j.williams, osalvador

On Fri, 2019-12-13 at 11:08 +0000, Mel Gorman wrote:
> On Fri, Dec 13, 2019 at 11:00:42AM +0100, David Hildenbrand wrote:
> > > A brief history on the background of free page reporting can be found at:
> > > https://lore.kernel.org/lkml/29f43d5796feed0dec8e8bb98b187d9dac03b900.camel@linux.intel.com/
> > > 
> > > Changes from v13:
> > > https://lore.kernel.org/lkml/20191105215940.15144.65968.stgit@localhost.localdomain/
> > > Rewrote core reporting functionality
> > >   Merged patches 3 & 4
> > >   Dropped boundary list and related code
> > >   Folded get_reported_page into page_reporting_fill
> > >   Folded page_reporting_fill into page_reporting_cycle
> > > Pulled reporting functionality out of free_reported_page
> > >   Renamed it to __free_isolated_page
> > >   Moved page reporting specific bits to page_reporting_drain
> > > Renamed phdev to prdev since we aren't "hinting" we are "reporting"
> > > Added documentation to describe the usage of unused page reporting
> > > Updated cover page and patch descriptions to avoid mention of boundary
> > > 
> > > Changes from v14:
> > > https://lore.kernel.org/lkml/20191119214454.24996.66289.stgit@localhost.localdomain/
> > > 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
> > 
> > Last time Mel said
> > 
> > "Ok, I'm ok with how this hooks into the allocator as the overhead is
> > minimal. However, the patch itself still includes a number of
> > optimisations instead of being a bare-boned implementation of the
> > feature with optimisations layered on top."
> > 
> 
> I didn't get the chance to take a close look as I'm trying to clear as
> much as possible from my table on the run-up to Christmas so I don't come
> back to a disaster inbox. I also noted that the Acks for earlier patches
> were not included so I was uncertain if doing a full review would still
> be a good use of time when time was tight.

Sorry about that. I will go back through and make sure to collect the Acks
on the earlier patches. I guess I had overlooked them while focusing on
rewriting the core functionality.

> That said, some optimisations are still included but much reduced. For
> example, list rotations are still there but it's very straight-forward.

I will go ahead and split the rotations out into a separate patch for v16.
I can probably do that and pull the budget bit I had added out and put it
together as a "work conserving/limiting" optimization for the patch set.

> The refcount is gone which is good and replaced by a state, which could be
> be better documented, but is more straight forward and the zone->lock is
> back protecting the free lists primarily and not zone metadata or prdev
> metadata (at least not obviously). I didn't put in the time to see if
> the atomic_set in page_reporting_process() is ok or whether state could
> be lost but I *think* it's ok because it should be called from just one
> workqueue request and they shouldn't be stacked. A comment there explaining
> why atomic_set is definitely correct would be helpful.

I will go though and add some more documentation about the state.

> I'm inclined to decide that yes, this version is potentially ok as a
> bare minimum but didn't put in the time to be 100% sure.

Sounds good. I will go through and address the concerns you brought up,
and probably post a v16 by the end of next week.

Thanks for the feedback.

- Alex


^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v15 6/7] virtio-balloon: Add support for providing free page reports to host
  2019-12-13 16:35     ` Alexander Duyck
@ 2019-12-15  9:29       ` Michael S. Tsirkin
  0 siblings, 0 replies; 39+ messages in thread
From: Michael S. Tsirkin @ 2019-12-15  9:29 UTC (permalink / raw)
  To: Alexander Duyck
  Cc: Alexander Duyck, kvm, linux-kernel, willy, mhocko, linux-mm,
	akpm, mgorman, vbabka, yang.zhang.wz, nitesh, konrad.wilk, david,
	pagupta, riel, lcapitulino, dave.hansen, wei.w.wang, aarcange,
	pbonzini, dan.j.williams, osalvador

On Fri, Dec 13, 2019 at 08:35:13AM -0800, Alexander Duyck wrote:
> On Fri, 2019-12-13 at 02:08 -0500, Michael S. Tsirkin wrote:
> > On Thu, Dec 05, 2019 at 08:22:55AM -0800, Alexander Duyck wrote:
> > > From: Alexander Duyck <alexander.h.duyck@linux.intel.com>
> > > 
> > > 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.
> > > 
> > > For this reason when I had originally introduced the patch set I referred
> > > to this behavior as a "bubble" instead of a "balloon" since the duration
> > > is short lived, and when the page is touched the "bubble" is popped and
> > > the page is faulted back in.
> > > 
> > > Signed-off-by: Alexander Duyck <alexander.h.duyck@linux.intel.com>
> > 
> > virtio POV is fine here:
> > 
> > Acked-by: Michael S. Tsirkin <mst@redhat.com>
> > 
> > However please copy virtio-comment on UAPI changes.
> 
> So I have been avoiding copying virtio-dev on the kernel changes as I had
> gotten feedback that it was annoying some people as they were getting
> bounces since they were not subscribed. Will the same type of thing happen
> with virtio-comment?

Same thing.

> > If possible isolate the last chunk in a patch by itself
> > to make it easier for non-kernel developers to review.
> 
> Are you talking about the change in "include/uapi/linux/virtio_balloon.h"?
> 
> I have it as a standalone patch in the QEMU set, and for the QEMU set I
> had included virtio-dev.

That's enough then.

> Would you prefer I include virtio-comment instead
> or in addition to virtio-dev? My thought is that I would prefer to keep
> the virtio people focused on the QEMU code since they are probably more
> comfortable with that, and the kernel people focused on the kernel code.

virtio-dev is enough too.


> > > ---
> > >  drivers/virtio/Kconfig              |    1 +
> > >  drivers/virtio/virtio_balloon.c     |   64 +++++++++++++++++++++++++++++++++++
> > >  include/uapi/linux/virtio_balloon.h |    1 +
> > >  3 files changed, 66 insertions(+)
> 
> <snip>
> 
> > > 
> > > diff --git a/include/uapi/linux/virtio_balloon.h b/include/uapi/linux/virtio_balloon.h
> > > index a1966cd7b677..19974392d324 100644
> > > --- a/include/uapi/linux/virtio_balloon.h
> > > +++ b/include/uapi/linux/virtio_balloon.h
> > > @@ -36,6 +36,7 @@
> > >  #define VIRTIO_BALLOON_F_DEFLATE_ON_OOM	2 /* Deflate balloon on OOM */
> > >  #define VIRTIO_BALLOON_F_FREE_PAGE_HINT	3 /* VQ to report free pages */
> > >  #define VIRTIO_BALLOON_F_PAGE_POISON	4 /* Guest is using page poisoning */
> > > +#define VIRTIO_BALLOON_F_REPORTING	5 /* Page reporting virtqueue */
> > >  
> > >  /* Size of a PFN in the balloon interface. */
> > >  #define VIRTIO_BALLOON_PFN_SHIFT 12
> 
> If this is the bit we are talking about I have it split out already into a
> QEMU specific patch as well, it can be found here:
> https://lore.kernel.org/lkml/20191205162422.19737.57728.stgit@localhost.localdomain/
> 
> If needed I could probably add a cover page and/or update the comments in
> that patch if that is needed to better explain the change.
> 
> Thanks.
> 
> - Alex

Updating comment in this patch can't hurt.
But yes, this is OK as is, too, I just missed that you did it.

-- 
MST


^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v15 4/7] mm: Introduce Reported pages
  2019-12-05 16:22 ` [PATCH v15 4/7] mm: Introduce Reported pages Alexander Duyck
@ 2019-12-16 10:17   ` Nitesh Narayan Lal
  2019-12-16 16:28     ` Alexander Duyck
  2019-12-16 11:44   ` Nitesh Narayan Lal
  1 sibling, 1 reply; 39+ messages in thread
From: Nitesh Narayan Lal @ 2019-12-16 10:17 UTC (permalink / raw)
  To: Alexander Duyck, kvm, mst, linux-kernel, willy, mhocko, linux-mm,
	akpm, mgorman, vbabka
  Cc: yang.zhang.wz, konrad.wilk, david, pagupta, riel, lcapitulino,
	dave.hansen, wei.w.wang, aarcange, pbonzini, dan.j.williams,
	alexander.h.duyck, osalvador


On 12/5/19 11:22 AM, Alexander Duyck wrote:
> From: Alexander Duyck <alexander.h.duyck@linux.intel.com>
>
> 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.

[...]

> +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);
> +	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);
> +}
> +

I think you recently switched to using an atomic variable for maintaining page
reporting status as I was doing in v12.
Which is good, as we will not have a disagreement on it now.

On a side note, apologies for not getting involved actively in the
discussions/reviews as I am on PTO.

-- 

Nitesh


^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v15 3/7] mm: Add function __putback_isolated_page
  2019-12-05 16:22 ` [PATCH v15 3/7] mm: Add function __putback_isolated_page Alexander Duyck
@ 2019-12-16 11:36   ` David Hildenbrand
  2019-12-16 16:22     ` Alexander Duyck
  0 siblings, 1 reply; 39+ messages in thread
From: David Hildenbrand @ 2019-12-16 11:36 UTC (permalink / raw)
  To: Alexander Duyck, kvm, mst, linux-kernel, willy, mhocko, linux-mm,
	akpm, mgorman, vbabka
  Cc: yang.zhang.wz, nitesh, konrad.wilk, pagupta, riel, lcapitulino,
	dave.hansen, wei.w.wang, aarcange, pbonzini, dan.j.williams,
	alexander.h.duyck, osalvador

[...]
> +/**
> + * __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)
> +{
> +	struct zone *zone = page_zone(page);
> +	unsigned long pfn;
> +	unsigned int mt;
> +
> +	/* zone lock should be held when this function is called */
> +	lockdep_assert_held(&zone->lock);
> +
> +	pfn = page_to_pfn(page);
> +	mt = get_pfnblock_migratetype(page, pfn);

IMHO get_pageblock_migratetype() would be nicer - I guess the compiler
will optimize out the double page_to_pfn().

> +
> +	/* Return isolated page to tail of freelist. */
> +	__free_one_page(page, pfn, zone, order, mt);
> +}
> +
>  /*
>   * Update NUMA hit/miss statistics
>   *
> diff --git a/mm/page_isolation.c b/mm/page_isolation.c
> index 04ee1663cdbe..d93d2be0070f 100644
> --- a/mm/page_isolation.c
> +++ b/mm/page_isolation.c
> @@ -134,13 +134,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);
>  	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);
> -	}

So If I get it right:

post_alloc_hook() does quite some stuff like
- arch_alloc_page(page, order);
- kernel_map_pages(page, 1 << order, 1)
- kasan_alloc_pages()
- kernel_poison_pages(1)
- set_page_owner()

Which free_pages_prepare() will undo, like
- reset_page_owner()
- kernel_poison_pages(0)
- arch_free_page()
- kernel_map_pages()
- kasan_free_nondeferred_pages()

Both would be skipped now - which sounds like the right thing to do IMHO
(and smells like quite a performance improvement). I haven't verified if
actually everything we skip in free_pages_prepare() is safe (I think it
is, it seems to be mostly relevant for actually used/allocated pages).

-- 
Thanks,

David / dhildenb


^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v15 4/7] mm: Introduce Reported pages
  2019-12-05 16:22 ` [PATCH v15 4/7] mm: Introduce Reported pages Alexander Duyck
  2019-12-16 10:17   ` Nitesh Narayan Lal
@ 2019-12-16 11:44   ` Nitesh Narayan Lal
  2019-12-16 16:10     ` Alexander Duyck
  1 sibling, 1 reply; 39+ messages in thread
From: Nitesh Narayan Lal @ 2019-12-16 11:44 UTC (permalink / raw)
  To: Alexander Duyck, kvm, mst, linux-kernel, willy, mhocko, linux-mm,
	akpm, mgorman, vbabka
  Cc: yang.zhang.wz, konrad.wilk, david, pagupta, riel, lcapitulino,
	dave.hansen, wei.w.wang, aarcange, pbonzini, dan.j.williams,
	alexander.h.duyck, osalvador


On 12/5/19 11:22 AM, Alexander Duyck wrote:
> From: Alexander Duyck <alexander.h.duyck@linux.intel.com>
>
> 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.

[...]

> k);
> +
> +	return err;
> +}
> +
> +static int
> +page_reporting_process_zone(struct page_reporting_dev_info *prdev,
> +			    struct scatterlist *sgl, struct zone *zone)
> +{
> +	unsigned int order, mt, leftover, offset = PAGE_REPORTING_CAPACITY;
> +	unsigned long watermark;
> +	int err = 0;
> +
> +	/* Generate minimum watermark to be able to guarantee progress */
> +	watermark = low_wmark_pages(zone) +
> +		    (PAGE_REPORTING_CAPACITY << PAGE_REPORTING_MIN_ORDER);
> +
> +	/*
> +	 * Cancel request if insufficient free memory or if we failed
> +	 * to allocate page reporting statistics for the zone.
> +	 */
> +	if (!zone_watermark_ok(zone, 0, watermark, 0, ALLOC_CMA))
> +		return err;
> +


Will it not make more sense to check the low watermark condition before every
reporting request generated for a bunch of 32 isolated pages?
or will that be too costly?

> +	/* Process each free list starting from lowest order/mt */
> +	for (order = PAGE_REPORTING_MIN_ORDER; order < MAX_ORDER; order++) {
> +		for (mt = 0; mt < MIGRATE_TYPES; mt++) {
> +			/* We do not pull pages from the isolate free list */
> +			if (is_migrate_isolate(mt))
> +				continue;
> +
> +			err = page_reporting_cycle(prdev, zone, order, mt,
> +						   sgl, &offset);
> +			if (err)
> +				return err;
> +		}
> +	}
> +
> +	/* report the leftover pages before going idle */
> +	leftover = PAGE_REPORTING_CAPACITY - offset;
> +	if (leftover) {
> +		sgl = &sgl[offset];
> +		err = prdev->report(prdev, sgl, leftover);
> +
> +		/* flush any remaining pages out from the last report */
> +		spin_lock_irq(&zone->lock);
> +		page_reporting_drain(prdev, sgl, leftover, !err);
> +		spin_unlock_irq(&zone->lock);
> +	}
> +
> +	return err;
> +}
-- 
Nitesh


^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v15 0/7] mm / virtio: Provide support for free page reporting
  2019-12-13 16:46   ` Alexander Duyck
@ 2019-12-16 12:21     ` David Hildenbrand
  0 siblings, 0 replies; 39+ messages in thread
From: David Hildenbrand @ 2019-12-16 12:21 UTC (permalink / raw)
  To: Alexander Duyck, Alexander Duyck, kvm, mst, linux-kernel, willy,
	mhocko, linux-mm, akpm, mgorman, vbabka
  Cc: yang.zhang.wz, nitesh, konrad.wilk, pagupta, riel, lcapitulino,
	dave.hansen, wei.w.wang, aarcange, pbonzini, dan.j.williams,
	osalvador

> I found alternative approaches and did away with most of them. What I had
> done is replaced the REPORTING_REQUESTED and reference count logic with
> the state in order to guarantee that we will make our way through the list
> and rearm the reporting thread if the work isn't completed.
> 
> One thing I still think I need to split out based on Mel's comments is the
> list rotation and probably the new budget value I added.

Yeah, both probably make sense.

> 
> As far as performance this new patch set performs better than the old one
> did. Most of that is due to the fact that I increased the delay between
> passes and dropped any optimizations for the shuffling code.

That's interesting - and good :)

I probably won't have enough time to look into #4 before Christmas - so
I might wait for your resend and review after Christmas.

-- 
Thanks,

David / dhildenb


^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v15 4/7] mm: Introduce Reported pages
  2019-12-16 11:44   ` Nitesh Narayan Lal
@ 2019-12-16 16:10     ` Alexander Duyck
  0 siblings, 0 replies; 39+ messages in thread
From: Alexander Duyck @ 2019-12-16 16:10 UTC (permalink / raw)
  To: Nitesh Narayan Lal, Alexander Duyck, kvm, mst, linux-kernel,
	willy, mhocko, linux-mm, akpm, mgorman, vbabka
  Cc: yang.zhang.wz, konrad.wilk, david, pagupta, riel, lcapitulino,
	dave.hansen, wei.w.wang, aarcange, pbonzini, dan.j.williams,
	osalvador

On Mon, 2019-12-16 at 06:44 -0500, Nitesh Narayan Lal wrote:
> On 12/5/19 11:22 AM, Alexander Duyck wrote:
> > From: Alexander Duyck <alexander.h.duyck@linux.intel.com>
> > 
> > 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.
> 
> [...]
> 
> > k);
> > +
> > +	return err;
> > +}
> > +
> > +static int
> > +page_reporting_process_zone(struct page_reporting_dev_info *prdev,
> > +			    struct scatterlist *sgl, struct zone *zone)
> > +{
> > +	unsigned int order, mt, leftover, offset = PAGE_REPORTING_CAPACITY;
> > +	unsigned long watermark;
> > +	int err = 0;
> > +
> > +	/* Generate minimum watermark to be able to guarantee progress */
> > +	watermark = low_wmark_pages(zone) +
> > +		    (PAGE_REPORTING_CAPACITY << PAGE_REPORTING_MIN_ORDER);
> > +
> > +	/*
> > +	 * Cancel request if insufficient free memory or if we failed
> > +	 * to allocate page reporting statistics for the zone.
> > +	 */
> > +	if (!zone_watermark_ok(zone, 0, watermark, 0, ALLOC_CMA))
> > +		return err;
> > +
> 
> Will it not make more sense to check the low watermark condition before every
> reporting request generated for a bunch of 32 isolated pages?
> or will that be too costly?

My thought is to wait until we are actually processing the request. That
way we are only performing this check once every 2 seconds instead of
every time we are thinking about requesting page reporting.

Keep in mind I removed the reported_pages tracking statistics so we now
are requesting as soon as we free any page. So if we moved the check tot
he request itself it would mean that a low memory condition would result
in us repeatedly checking the low water mark and failing the test.


^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v15 3/7] mm: Add function __putback_isolated_page
  2019-12-16 11:36   ` David Hildenbrand
@ 2019-12-16 16:22     ` Alexander Duyck
  2019-12-17 10:58       ` David Hildenbrand
  0 siblings, 1 reply; 39+ messages in thread
From: Alexander Duyck @ 2019-12-16 16:22 UTC (permalink / raw)
  To: David Hildenbrand, Alexander Duyck, kvm, mst, linux-kernel,
	willy, mhocko, linux-mm, akpm, mgorman, vbabka
  Cc: yang.zhang.wz, nitesh, konrad.wilk, pagupta, riel, lcapitulino,
	dave.hansen, wei.w.wang, aarcange, pbonzini, dan.j.williams,
	osalvador

On Mon, 2019-12-16 at 12:36 +0100, David Hildenbrand wrote:
> [...]
> > +/**
> > + * __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)
> > +{
> > +	struct zone *zone = page_zone(page);
> > +	unsigned long pfn;
> > +	unsigned int mt;
> > +
> > +	/* zone lock should be held when this function is called */
> > +	lockdep_assert_held(&zone->lock);
> > +
> > +	pfn = page_to_pfn(page);
> > +	mt = get_pfnblock_migratetype(page, pfn);
> 
> IMHO get_pageblock_migratetype() would be nicer - I guess the compiler
> will optimize out the double page_to_pfn().

The thing is I need the page_to_pfn call already in order to pass the
value to __free_one_page. With that being the case why not juse use
get_pfnblock_migratetype?

Also there are some scenarios where __page_to_pfn is not that simple a
call with us having to get the node ID so we can find the pgdat structure
to perform the calculation. I'm not sure the compiler would be ble to
figure out that the result is the same for both calls, so it is better to
make it explicit.

> > +
> > +	/* Return isolated page to tail of freelist. */
> > +	__free_one_page(page, pfn, zone, order, mt);
> > +}
> > +
> >  /*
> >   * Update NUMA hit/miss statistics
> >   *
> > diff --git a/mm/page_isolation.c b/mm/page_isolation.c
> > index 04ee1663cdbe..d93d2be0070f 100644
> > --- a/mm/page_isolation.c
> > +++ b/mm/page_isolation.c
> > @@ -134,13 +134,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);
> >  	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);
> > -	}
> 
> So If I get it right:
> 
> post_alloc_hook() does quite some stuff like
> - arch_alloc_page(page, order);
> - kernel_map_pages(page, 1 << order, 1)
> - kasan_alloc_pages()
> - kernel_poison_pages(1)
> - set_page_owner()
> 
> Which free_pages_prepare() will undo, like
> - reset_page_owner()
> - kernel_poison_pages(0)
> - arch_free_page()
> - kernel_map_pages()
> - kasan_free_nondeferred_pages()
> 
> Both would be skipped now - which sounds like the right thing to do IMHO
> (and smells like quite a performance improvement). I haven't verified if
> actually everything we skip in free_pages_prepare() is safe (I think it
> is, it seems to be mostly relevant for actually used/allocated pages).

That was kind of my thought on this. Basically the logic I was following
was that the code path will call move_freepages_block that bypasses all of
the above mentioned calls if the pages it is moving will not be merged. If
it is safe in that case my assumption is that it should be safe to just
call __putback_isolated_page in such a case as it also bypasses the block
above, but it supports merging the page with other pages that are already
on the freelist.


^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v15 4/7] mm: Introduce Reported pages
  2019-12-16 10:17   ` Nitesh Narayan Lal
@ 2019-12-16 16:28     ` Alexander Duyck
  2019-12-17  8:55       ` Nitesh Narayan Lal
  0 siblings, 1 reply; 39+ messages in thread
From: Alexander Duyck @ 2019-12-16 16:28 UTC (permalink / raw)
  To: Nitesh Narayan Lal, Alexander Duyck, kvm, mst, linux-kernel,
	willy, mhocko, linux-mm, akpm, mgorman, vbabka
  Cc: yang.zhang.wz, konrad.wilk, david, pagupta, riel, lcapitulino,
	dave.hansen, wei.w.wang, aarcange, pbonzini, dan.j.williams,
	osalvador

On Mon, 2019-12-16 at 05:17 -0500, Nitesh Narayan Lal wrote:
> On 12/5/19 11:22 AM, Alexander Duyck wrote:
> > From: Alexander Duyck <alexander.h.duyck@linux.intel.com>
> > 
> > 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.
> 
> [...]
> 
> > +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);
> > +	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);
> > +}
> > +
> 
> I think you recently switched to using an atomic variable for maintaining page
> reporting status as I was doing in v12.
> Which is good, as we will not have a disagreement on it now.

There is still some differences between our approaches if I am not
mistaken. Specifically I have code in place so that any requests to report
while we are actively working on reporting will trigger another pass being
scheduled after we completed. I still believe you were lacking any logic
like that as I recall.

> On a side note, apologies for not getting involved actively in the
> discussions/reviews as I am on PTO.

No problem. I've been mostly looking for input from the core MM
maintainers anyway as we sorted most of the virtualization pieces some
time ago.


^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v15 4/7] mm: Introduce Reported pages
  2019-12-16 16:28     ` Alexander Duyck
@ 2019-12-17  8:55       ` Nitesh Narayan Lal
  2019-12-17 16:31         ` Alexander Duyck
  0 siblings, 1 reply; 39+ messages in thread
From: Nitesh Narayan Lal @ 2019-12-17  8:55 UTC (permalink / raw)
  To: Alexander Duyck, Alexander Duyck, kvm, mst, linux-kernel, willy,
	mhocko, linux-mm, akpm, mgorman, vbabka
  Cc: yang.zhang.wz, konrad.wilk, david, pagupta, riel, lcapitulino,
	dave.hansen, wei.w.wang, aarcange, pbonzini, dan.j.williams,
	osalvador


On 12/16/19 11:28 AM, Alexander Duyck wrote:
> On Mon, 2019-12-16 at 05:17 -0500, Nitesh Narayan Lal wrote:
>> On 12/5/19 11:22 AM, Alexander Duyck wrote:
>>> From: Alexander Duyck <alexander.h.duyck@linux.intel.com>
>>>
>>> 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.
>> [...]
>>
>>> +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);
>>> +	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);
>>> +}
>>> +
>> I think you recently switched to using an atomic variable for maintaining page
>> reporting status as I was doing in v12.
>> Which is good, as we will not have a disagreement on it now.
> There is still some differences between our approaches if I am not
> mistaken. Specifically I have code in place so that any requests to report
> while we are actively working on reporting will trigger another pass being
> scheduled after we completed. I still believe you were lacking any logic
> like that as I recall.
>

Yes, I was specifically referring to the atomic state variable.
Though I am wondering if having an atomic variable to track page reporting state
is better than having a page reporting specific unsigned long flag, which we can
manipulate via __set_bit() and __clear_bit().

>> On a side note, apologies for not getting involved actively in the
>> discussions/reviews as I am on PTO.
> No problem. I've been mostly looking for input from the core MM
> maintainers anyway as we sorted most of the virtualization pieces some
> time ago.
>
-- 
Thanks
Nitesh


^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v15 3/7] mm: Add function __putback_isolated_page
  2019-12-16 16:22     ` Alexander Duyck
@ 2019-12-17 10:58       ` David Hildenbrand
  2019-12-17 16:26         ` Alexander Duyck
  0 siblings, 1 reply; 39+ messages in thread
From: David Hildenbrand @ 2019-12-17 10:58 UTC (permalink / raw)
  To: Alexander Duyck, Alexander Duyck, kvm, mst, linux-kernel, willy,
	mhocko, linux-mm, akpm, mgorman, vbabka
  Cc: yang.zhang.wz, nitesh, konrad.wilk, pagupta, riel, lcapitulino,
	dave.hansen, wei.w.wang, aarcange, pbonzini, dan.j.williams,
	osalvador

On 16.12.19 17:22, Alexander Duyck wrote:
> On Mon, 2019-12-16 at 12:36 +0100, David Hildenbrand wrote:
>> [...]
>>> +/**
>>> + * __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)
>>> +{
>>> +	struct zone *zone = page_zone(page);
>>> +	unsigned long pfn;
>>> +	unsigned int mt;
>>> +
>>> +	/* zone lock should be held when this function is called */
>>> +	lockdep_assert_held(&zone->lock);
>>> +
>>> +	pfn = page_to_pfn(page);
>>> +	mt = get_pfnblock_migratetype(page, pfn);
>>
>> IMHO get_pageblock_migratetype() would be nicer - I guess the compiler
>> will optimize out the double page_to_pfn().
> 
> The thing is I need the page_to_pfn call already in order to pass the
> value to __free_one_page. With that being the case why not juse use
> get_pfnblock_migratetype?

I was reading
	set_pageblock_migratetype(page, migratetype);
And wondered why we don't use straight forward
	get_pageblock_migratetype()
but instead something that looks like a micro-optimization

> 
> Also there are some scenarios where __page_to_pfn is not that simple a
> call with us having to get the node ID so we can find the pgdat structure
> to perform the calculation. I'm not sure the compiler would be ble to
> figure out that the result is the same for both calls, so it is better to
> make it explicit.

Only in case of CONFIG_SPARSEMEM we have to go via the section - but I
doubt this is really worth optimizing here.

But yeah, I'm fine with this change, only "IMHO
get_pageblock_migratetype() would be nicer" :)

> 
>>> +
>>> +	/* Return isolated page to tail of freelist. */
>>> +	__free_one_page(page, pfn, zone, order, mt);
>>> +}
>>> +
>>>  /*
>>>   * Update NUMA hit/miss statistics
>>>   *
>>> diff --git a/mm/page_isolation.c b/mm/page_isolation.c
>>> index 04ee1663cdbe..d93d2be0070f 100644
>>> --- a/mm/page_isolation.c
>>> +++ b/mm/page_isolation.c
>>> @@ -134,13 +134,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);
>>>  	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);
>>> -	}
>>
>> So If I get it right:
>>
>> post_alloc_hook() does quite some stuff like
>> - arch_alloc_page(page, order);
>> - kernel_map_pages(page, 1 << order, 1)
>> - kasan_alloc_pages()
>> - kernel_poison_pages(1)
>> - set_page_owner()
>>
>> Which free_pages_prepare() will undo, like
>> - reset_page_owner()
>> - kernel_poison_pages(0)
>> - arch_free_page()
>> - kernel_map_pages()
>> - kasan_free_nondeferred_pages()
>>
>> Both would be skipped now - which sounds like the right thing to do IMHO
>> (and smells like quite a performance improvement). I haven't verified if
>> actually everything we skip in free_pages_prepare() is safe (I think it
>> is, it seems to be mostly relevant for actually used/allocated pages).
> 
> That was kind of my thought on this. Basically the logic I was following
> was that the code path will call move_freepages_block that bypasses all of
> the above mentioned calls if the pages it is moving will not be merged. If
> it is safe in that case my assumption is that it should be safe to just
> call __putback_isolated_page in such a case as it also bypasses the block
> above, but it supports merging the page with other pages that are already
> on the freelist.

Makes sense to me

Acked-by: David Hildenbrand <david@redhat.com>


-- 
Thanks,

David / dhildenb


^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v15 3/7] mm: Add function __putback_isolated_page
  2019-12-17 10:58       ` David Hildenbrand
@ 2019-12-17 16:26         ` Alexander Duyck
  2019-12-17 17:24           ` David Hildenbrand
  0 siblings, 1 reply; 39+ messages in thread
From: Alexander Duyck @ 2019-12-17 16:26 UTC (permalink / raw)
  To: David Hildenbrand, Alexander Duyck, kvm, mst, linux-kernel,
	willy, mhocko, linux-mm, akpm, mgorman, vbabka
  Cc: yang.zhang.wz, nitesh, konrad.wilk, pagupta, riel, lcapitulino,
	dave.hansen, wei.w.wang, aarcange, pbonzini, dan.j.williams,
	osalvador

On Tue, 2019-12-17 at 11:58 +0100, David Hildenbrand wrote:
> On 16.12.19 17:22, Alexander Duyck wrote:
> > On Mon, 2019-12-16 at 12:36 +0100, David Hildenbrand wrote:
> > > [...]
> > > > +/**
> > > > + * __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)
> > > > +{
> > > > +	struct zone *zone = page_zone(page);
> > > > +	unsigned long pfn;
> > > > +	unsigned int mt;
> > > > +
> > > > +	/* zone lock should be held when this function is called */
> > > > +	lockdep_assert_held(&zone->lock);
> > > > +
> > > > +	pfn = page_to_pfn(page);
> > > > +	mt = get_pfnblock_migratetype(page, pfn);
> > > 
> > > IMHO get_pageblock_migratetype() would be nicer - I guess the compiler
> > > will optimize out the double page_to_pfn().
> > 
> > The thing is I need the page_to_pfn call already in order to pass the
> > value to __free_one_page. With that being the case why not juse use
> > get_pfnblock_migratetype?
> 
> I was reading
> 	set_pageblock_migratetype(page, migratetype);
> And wondered why we don't use straight forward
> 	get_pageblock_migratetype()
> but instead something that looks like a micro-optimization

So there ends up being a some other optimizations you may not have
noticed.

For instance, the fact that get_pfnblock_migratetype is an inline
function, whereas get_pageblock_migratetype calls get_pfnblock_flags_mask
which is not an inline function. So you end up having to take the overhead
for a call/return.

I hadn't noticed that myself until taking a look at the code.

> > Also there are some scenarios where __page_to_pfn is not that simple a
> > call with us having to get the node ID so we can find the pgdat structure
> > to perform the calculation. I'm not sure the compiler would be ble to
> > figure out that the result is the same for both calls, so it is better to
> > make it explicit.
> 
> Only in case of CONFIG_SPARSEMEM we have to go via the section - but I
> doubt this is really worth optimizing here.
> 
> But yeah, I'm fine with this change, only "IMHO
> get_pageblock_migratetype() would be nicer" :)

Aren't most distros running with CONFIG_SPARSEMEM enabled? If that is the
case why not optimize for it?

As I stated earlier, in my case I already have to pull out the PFN as a
part of freeing the page anyway, so why not reuse the value instead of
having it be computed twice? It is in keeping with how the other handlers
are dealing with this such as free_one_page, __free_pages_ok, and
free_unref_page_prepare. I suspect it has to do with the fact that it is
an inline like I pointed out above.

> > > > +
> > > > +	/* Return isolated page to tail of freelist. */
> > > > +	__free_one_page(page, pfn, zone, order, mt);
> > > > +}
> > > > +
> > > >  /*
> > > >   * Update NUMA hit/miss statistics
> > > >   *
> > > > diff --git a/mm/page_isolation.c b/mm/page_isolation.c
> > > > index 04ee1663cdbe..d93d2be0070f 100644
> > > > --- a/mm/page_isolation.c
> > > > +++ b/mm/page_isolation.c
> > > > @@ -134,13 +134,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);
> > > >  	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);
> > > > -	}
> > > 
> > > So If I get it right:
> > > 
> > > post_alloc_hook() does quite some stuff like
> > > - arch_alloc_page(page, order);
> > > - kernel_map_pages(page, 1 << order, 1)
> > > - kasan_alloc_pages()
> > > - kernel_poison_pages(1)
> > > - set_page_owner()
> > > 
> > > Which free_pages_prepare() will undo, like
> > > - reset_page_owner()
> > > - kernel_poison_pages(0)
> > > - arch_free_page()
> > > - kernel_map_pages()
> > > - kasan_free_nondeferred_pages()
> > > 
> > > Both would be skipped now - which sounds like the right thing to do IMHO
> > > (and smells like quite a performance improvement). I haven't verified if
> > > actually everything we skip in free_pages_prepare() is safe (I think it
> > > is, it seems to be mostly relevant for actually used/allocated pages).
> > 
> > That was kind of my thought on this. Basically the logic I was following
> > was that the code path will call move_freepages_block that bypasses all of
> > the above mentioned calls if the pages it is moving will not be merged. If
> > it is safe in that case my assumption is that it should be safe to just
> > call __putback_isolated_page in such a case as it also bypasses the block
> > above, but it supports merging the page with other pages that are already
> > on the freelist.
> 
> Makes sense to me
> 
> Acked-by: David Hildenbrand <david@redhat.com>

Thanks. I will add the Ack to the patch for v16.


^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v15 4/7] mm: Introduce Reported pages
  2019-12-17  8:55       ` Nitesh Narayan Lal
@ 2019-12-17 16:31         ` Alexander Duyck
  2019-12-18  7:31           ` Mel Gorman
  0 siblings, 1 reply; 39+ messages in thread
From: Alexander Duyck @ 2019-12-17 16:31 UTC (permalink / raw)
  To: Nitesh Narayan Lal, Alexander Duyck, kvm, mst, linux-kernel,
	willy, mhocko, linux-mm, akpm, mgorman, vbabka
  Cc: yang.zhang.wz, konrad.wilk, david, pagupta, riel, lcapitulino,
	dave.hansen, wei.w.wang, aarcange, pbonzini, dan.j.williams,
	osalvador

On Tue, 2019-12-17 at 03:55 -0500, Nitesh Narayan Lal wrote:
> On 12/16/19 11:28 AM, Alexander Duyck wrote:
> > On Mon, 2019-12-16 at 05:17 -0500, Nitesh Narayan Lal wrote:
> > > On 12/5/19 11:22 AM, Alexander Duyck wrote:
> > > > From: Alexander Duyck <alexander.h.duyck@linux.intel.com>
> > > > 
> > > > 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.
> > > [...]
> > > 
> > > > +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);
> > > > +	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);
> > > > +}
> > > > +
> > > I think you recently switched to using an atomic variable for maintaining page
> > > reporting status as I was doing in v12.
> > > Which is good, as we will not have a disagreement on it now.
> > There is still some differences between our approaches if I am not
> > mistaken. Specifically I have code in place so that any requests to report
> > while we are actively working on reporting will trigger another pass being
> > scheduled after we completed. I still believe you were lacking any logic
> > like that as I recall.
> > 
> 
> Yes, I was specifically referring to the atomic state variable.
> Though I am wondering if having an atomic variable to track page reporting state
> is better than having a page reporting specific unsigned long flag, which we can
> manipulate via __set_bit() and __clear_bit().

So the reason for using an atomic state variable is because I only really
have 3 possible states; idle, active, and requested. It allows for a
pretty simple state machine as any transition from idle indicates that we
need to schedule the worker, transition from requested to active when the
worker starts, and if at the end of a pass if we are still in the active
state it means we can transition back to idle, otherwise we reschedule the
worker.

In order to do the same sort of thing using the bitops would require at
least 2 bits. In addition with the requirement that I cannot use the zone
lock for protection of the state I cannot use the non-atomic versions of
things such as __set_bit and __clear_bit so they would require additional
locking protections.


^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v15 3/7] mm: Add function __putback_isolated_page
  2019-12-17 16:26         ` Alexander Duyck
@ 2019-12-17 17:24           ` David Hildenbrand
  2019-12-17 18:24             ` Alexander Duyck
  0 siblings, 1 reply; 39+ messages in thread
From: David Hildenbrand @ 2019-12-17 17:24 UTC (permalink / raw)
  To: Alexander Duyck, Alexander Duyck, kvm, mst, linux-kernel, willy,
	mhocko, linux-mm, akpm, mgorman, vbabka
  Cc: yang.zhang.wz, nitesh, konrad.wilk, pagupta, riel, lcapitulino,
	dave.hansen, wei.w.wang, aarcange, pbonzini, dan.j.williams,
	osalvador

 >>> Also there are some scenarios where __page_to_pfn is not that simple a
>>> call with us having to get the node ID so we can find the pgdat structure
>>> to perform the calculation. I'm not sure the compiler would be ble to
>>> figure out that the result is the same for both calls, so it is better to
>>> make it explicit.
>>
>> Only in case of CONFIG_SPARSEMEM we have to go via the section - but I
>> doubt this is really worth optimizing here.
>>
>> But yeah, I'm fine with this change, only "IMHO
>> get_pageblock_migratetype() would be nicer" :)
> 
> Aren't most distros running with CONFIG_SPARSEMEM enabled? If that is the
> case why not optimize for it?

Because I tend to dislike micro-optimizations without performance
numbers for code that is not on a hot path. But I mean in this case, as
you said, you need the pfn either way, so it's completely fine with.

I do wonder, however, if you should just pass in the migratetype from
the caller. That would be even faster ;)

-- 
Thanks,

David / dhildenb


^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v15 3/7] mm: Add function __putback_isolated_page
  2019-12-17 17:24           ` David Hildenbrand
@ 2019-12-17 18:24             ` Alexander Duyck
  2019-12-17 18:46               ` David Hildenbrand
  0 siblings, 1 reply; 39+ messages in thread
From: Alexander Duyck @ 2019-12-17 18:24 UTC (permalink / raw)
  To: David Hildenbrand, Alexander Duyck, kvm, mst, linux-kernel,
	willy, mhocko, linux-mm, akpm, mgorman, vbabka
  Cc: yang.zhang.wz, nitesh, konrad.wilk, pagupta, riel, lcapitulino,
	dave.hansen, wei.w.wang, aarcange, pbonzini, dan.j.williams,
	osalvador

On Tue, 2019-12-17 at 18:24 +0100, David Hildenbrand wrote:
>  >>> Also there are some scenarios where __page_to_pfn is not that simple a
> > > > call with us having to get the node ID so we can find the pgdat structure
> > > > to perform the calculation. I'm not sure the compiler would be ble to
> > > > figure out that the result is the same for both calls, so it is better to
> > > > make it explicit.
> > > 
> > > Only in case of CONFIG_SPARSEMEM we have to go via the section - but I
> > > doubt this is really worth optimizing here.
> > > 
> > > But yeah, I'm fine with this change, only "IMHO
> > > get_pageblock_migratetype() would be nicer" :)
> > 
> > Aren't most distros running with CONFIG_SPARSEMEM enabled? If that is the
> > case why not optimize for it?
> 
> Because I tend to dislike micro-optimizations without performance
> numbers for code that is not on a hot path. But I mean in this case, as
> you said, you need the pfn either way, so it's completely fine with.
> 
> I do wonder, however, if you should just pass in the migratetype from
> the caller. That would be even faster ;)

The problem is page isolation. We can end up with a page being moved to an
isolate pageblock while we aren't holding the zone lock, and as such we
likely need to test it again anyway. So there isn't value in storing and
reusing the value for cases like page reporting.

In addition, the act of isolating the page can cause the migratetype to
change as __isolate_free_page will attempt to change the migratetype to
movable if it is one of the standard percpu types and we are pulling at
least half a pageblock out. So storing the value before we isolate it
would be problematic as well.

Undoing page isolation is the exception to the issues pointed out above,
but in that case we are overwriting the pageblock migratetype anyway so
the cache lines involved should all be warm from having just set the
value.


^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v15 3/7] mm: Add function __putback_isolated_page
  2019-12-17 18:24             ` Alexander Duyck
@ 2019-12-17 18:46               ` David Hildenbrand
  2019-12-17 21:50                 ` Alexander Duyck
  0 siblings, 1 reply; 39+ messages in thread
From: David Hildenbrand @ 2019-12-17 18:46 UTC (permalink / raw)
  To: Alexander Duyck
  Cc: David Hildenbrand, Alexander Duyck, kvm, mst, linux-kernel,
	willy, mhocko, linux-mm, akpm, mgorman, vbabka, yang.zhang.wz,
	nitesh, konrad.wilk, pagupta, riel, lcapitulino, dave.hansen,
	wei.w.wang, aarcange, pbonzini, dan.j.williams, osalvador



> Am 17.12.2019 um 19:25 schrieb Alexander Duyck <alexander.h.duyck@linux.intel.com>:
> 
> On Tue, 2019-12-17 at 18:24 +0100, David Hildenbrand wrote:
>>>>> Also there are some scenarios where __page_to_pfn is not that simple a
>>>>> call with us having to get the node ID so we can find the pgdat structure
>>>>> to perform the calculation. I'm not sure the compiler would be ble to
>>>>> figure out that the result is the same for both calls, so it is better to
>>>>> make it explicit.
>>>> 
>>>> Only in case of CONFIG_SPARSEMEM we have to go via the section - but I
>>>> doubt this is really worth optimizing here.
>>>> 
>>>> But yeah, I'm fine with this change, only "IMHO
>>>> get_pageblock_migratetype() would be nicer" :)
>>> 
>>> Aren't most distros running with CONFIG_SPARSEMEM enabled? If that is the
>>> case why not optimize for it?
>> 
>> Because I tend to dislike micro-optimizations without performance
>> numbers for code that is not on a hot path. But I mean in this case, as
>> you said, you need the pfn either way, so it's completely fine with.
>> 
>> I do wonder, however, if you should just pass in the migratetype from
>> the caller. That would be even faster ;)
> 
> The problem is page isolation. We can end up with a page being moved to an
> isolate pageblock while we aren't holding the zone lock, and as such we
> likely need to test it again anyway. So there isn't value in storing and
> reusing the value for cases like page reporting.
> 
> In addition, the act of isolating the page can cause the migratetype to
> change as __isolate_free_page will attempt to change the migratetype to
> movable if it is one of the standard percpu types and we are pulling at
> least half a pageblock out. So storing the value before we isolate it
> would be problematic as well.
> 
> Undoing page isolation is the exception to the issues pointed out above,
> but in that case we are overwriting the pageblock migratetype anyway so
> the cache lines involved should all be warm from having just set the
> value.

Nothing would speak against querying the migratetype in the caller and passing it on. After all you‘re holding the zone lock, so it can‘t change.
> 


^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v15 3/7] mm: Add function __putback_isolated_page
  2019-12-17 18:46               ` David Hildenbrand
@ 2019-12-17 21:50                 ` Alexander Duyck
  0 siblings, 0 replies; 39+ messages in thread
From: Alexander Duyck @ 2019-12-17 21:50 UTC (permalink / raw)
  To: David Hildenbrand
  Cc: Alexander Duyck, kvm, mst, linux-kernel, willy, mhocko, linux-mm,
	akpm, mgorman, vbabka, yang.zhang.wz, nitesh, konrad.wilk,
	pagupta, riel, lcapitulino, dave.hansen, wei.w.wang, aarcange,
	pbonzini, dan.j.williams, osalvador

On Tue, 2019-12-17 at 19:46 +0100, David Hildenbrand wrote:
> > Am 17.12.2019 um 19:25 schrieb Alexander Duyck <alexander.h.duyck@linux.intel.com>:
> > 
> > On Tue, 2019-12-17 at 18:24 +0100, David Hildenbrand wrote:
> > > > > > Also there are some scenarios where __page_to_pfn is not that simple a
> > > > > > call with us having to get the node ID so we can find the pgdat structure
> > > > > > to perform the calculation. I'm not sure the compiler would be ble to
> > > > > > figure out that the result is the same for both calls, so it is better to
> > > > > > make it explicit.
> > > > > 
> > > > > Only in case of CONFIG_SPARSEMEM we have to go via the section - but I
> > > > > doubt this is really worth optimizing here.
> > > > > 
> > > > > But yeah, I'm fine with this change, only "IMHO
> > > > > get_pageblock_migratetype() would be nicer" :)
> > > > 
> > > > Aren't most distros running with CONFIG_SPARSEMEM enabled? If that is the
> > > > case why not optimize for it?
> > > 
> > > Because I tend to dislike micro-optimizations without performance
> > > numbers for code that is not on a hot path. But I mean in this case, as
> > > you said, you need the pfn either way, so it's completely fine with.
> > > 
> > > I do wonder, however, if you should just pass in the migratetype from
> > > the caller. That would be even faster ;)
> > 
> > The problem is page isolation. We can end up with a page being moved to an
> > isolate pageblock while we aren't holding the zone lock, and as such we
> > likely need to test it again anyway. So there isn't value in storing and
> > reusing the value for cases like page reporting.
> > 
> > In addition, the act of isolating the page can cause the migratetype to
> > change as __isolate_free_page will attempt to change the migratetype to
> > movable if it is one of the standard percpu types and we are pulling at
> > least half a pageblock out. So storing the value before we isolate it
> > would be problematic as well.
> > 
> > Undoing page isolation is the exception to the issues pointed out above,
> > but in that case we are overwriting the pageblock migratetype anyway so
> > the cache lines involved should all be warm from having just set the
> > value.
> 
> Nothing would speak against querying the migratetype in the caller and
> passing it on. After all you‘re holding the zone lock, so it can‘t
> change.

That's a fair argument. I will go ahead and make that change since it only
really adds one line to patch 4 and allows us to drop several lines from
patch 3.


^ permalink raw reply	[flat|nested] 39+ messages in thread

* Re: [PATCH v15 4/7] mm: Introduce Reported pages
  2019-12-17 16:31         ` Alexander Duyck
@ 2019-12-18  7:31           ` Mel Gorman
  0 siblings, 0 replies; 39+ messages in thread
From: Mel Gorman @ 2019-12-18  7:31 UTC (permalink / raw)
  To: Alexander Duyck
  Cc: Nitesh Narayan Lal, Alexander Duyck, kvm, mst, linux-kernel,
	willy, mhocko, linux-mm, akpm, vbabka, yang.zhang.wz,
	konrad.wilk, david, pagupta, riel, lcapitulino, dave.hansen,
	wei.w.wang, aarcange, pbonzini, dan.j.williams, osalvador

On Tue, Dec 17, 2019 at 08:31:59AM -0800, Alexander Duyck wrote:
> > > > I think you recently switched to using an atomic variable for maintaining page
> > > > reporting status as I was doing in v12.
> > > > Which is good, as we will not have a disagreement on it now.
> > >
> > > There is still some differences between our approaches if I am not
> > > mistaken. Specifically I have code in place so that any requests to report
> > > while we are actively working on reporting will trigger another pass being
> > > scheduled after we completed. I still believe you were lacking any logic
> > > like that as I recall.
> > > 
> > 
> > Yes, I was specifically referring to the atomic state variable.
> > Though I am wondering if having an atomic variable to track page reporting state
> > is better than having a page reporting specific unsigned long flag, which we can
> > manipulate via __set_bit() and __clear_bit().
> 
> So the reason for using an atomic state variable is because I only really
> have 3 possible states; idle, active, and requested. It allows for a
> pretty simple state machine as any transition from idle indicates that we
> need to schedule the worker, transition from requested to active when the
> worker starts, and if at the end of a pass if we are still in the active
> state it means we can transition back to idle, otherwise we reschedule the
> worker.
> 
> In order to do the same sort of thing using the bitops would require at
> least 2 bits. In addition with the requirement that I cannot use the zone
> lock for protection of the state I cannot use the non-atomic versions of
> things such as __set_bit and __clear_bit so they would require additional
> locking protections.
> 

I completely agree with this. I had pointed out in an earlier review
that expanding the scope of the zone lock was inappropriate, the
non-atomic operations on separate flags potentially misses updates and
in general, I prefer the atomic variable approach a lot more than the
previous zone->flag based approach.

-- 
Mel Gorman
SUSE Labs

^ permalink raw reply	[flat|nested] 39+ messages in thread

end of thread, other threads:[~2019-12-18  7:31 UTC | newest]

Thread overview: 39+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-05 16:22 [PATCH v15 0/7] mm / virtio: Provide support for free page reporting Alexander Duyck
2019-12-05 16:22 ` [PATCH v15 1/7] mm: Adjust shuffle code to allow for future coalescing Alexander Duyck
2019-12-05 16:22 ` [PATCH v15 2/7] mm: Use zone and order instead of free area in free_list manipulators Alexander Duyck
2019-12-05 16:22 ` [PATCH v15 3/7] mm: Add function __putback_isolated_page Alexander Duyck
2019-12-16 11:36   ` David Hildenbrand
2019-12-16 16:22     ` Alexander Duyck
2019-12-17 10:58       ` David Hildenbrand
2019-12-17 16:26         ` Alexander Duyck
2019-12-17 17:24           ` David Hildenbrand
2019-12-17 18:24             ` Alexander Duyck
2019-12-17 18:46               ` David Hildenbrand
2019-12-17 21:50                 ` Alexander Duyck
2019-12-05 16:22 ` [PATCH v15 4/7] mm: Introduce Reported pages Alexander Duyck
2019-12-16 10:17   ` Nitesh Narayan Lal
2019-12-16 16:28     ` Alexander Duyck
2019-12-17  8:55       ` Nitesh Narayan Lal
2019-12-17 16:31         ` Alexander Duyck
2019-12-18  7:31           ` Mel Gorman
2019-12-16 11:44   ` Nitesh Narayan Lal
2019-12-16 16:10     ` Alexander Duyck
2019-12-05 16:22 ` [PATCH v15 5/7] virtio-balloon: Pull page poisoning config out of free page hinting Alexander Duyck
2019-12-13  7:03   ` Michael S. Tsirkin
2019-12-05 16:22 ` [PATCH v15 6/7] virtio-balloon: Add support for providing free page reports to host Alexander Duyck
2019-12-13  7:08   ` Michael S. Tsirkin
2019-12-13 16:35     ` Alexander Duyck
2019-12-15  9:29       ` Michael S. Tsirkin
2019-12-13 10:15   ` David Hildenbrand
2019-12-13 16:37     ` Alexander Duyck
2019-12-05 16:23 ` [PATCH v15 7/7] mm: Add free page reporting documentation Alexander Duyck
2019-12-05 16:24 ` [PATCH v15 QEMU 1/3] virtio-ballon: Implement support for page poison tracking feature Alexander Duyck
2019-12-05 16:24 ` [PATCH v15 QEMU 2/3] virtio-balloon: Add bit to notify guest of unused page reporting Alexander Duyck
2019-12-05 16:24 ` [PATCH v15 QEMU 3/3] virtio-balloon: Provide a interface for " Alexander Duyck
2019-12-05 16:26 ` [PATCH v15 QEMU 4/3 RFC] memory: Add support for MADV_FREE as mechanism to lazy discard pages Alexander Duyck
2019-12-12 23:47 ` [PATCH v15 0/7] mm / virtio: Provide support for free page reporting Alexander Duyck
2019-12-13 10:00 ` David Hildenbrand
2019-12-13 11:08   ` Mel Gorman
2019-12-13 16:59     ` Alexander Duyck
2019-12-13 16:46   ` Alexander Duyck
2019-12-16 12:21     ` David Hildenbrand

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).