mbox series

[v16.1,0/9] mm / virtio: Provide support for free page reporting

Message ID 20200122173040.6142.39116.stgit@localhost.localdomain (mailing list archive)
Headers show
Series mm / virtio: Provide support for free page reporting | expand

Message

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

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

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

Currently this is only in use by virtio-balloon however there is the hope
that at some point in the future other hypervisors might be able to make
use of it. In the virtio-balloon/QEMU implementation the hypervisor is
currently using MADV_DONTNEED to indicate to the host kernel that the page
is currently free. It will be zeroed and faulted back into the guest the
next time the page is accessed.

To track if a page is reported or not the Uptodate flag was repurposed and
used as a Reported flag for Buddy pages. We walk though the free list
isolating pages and adding them to the scatterlist until we either
encounter the end of the list, 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 features such as CPU turbo
disabled in the BIOS.

Test                   page_fault1 (THP)    page_fault2
Name            tasks  Process Iter  STDEV  Process Iter  STDEV
Baseline            1    1012402.50  0.14%     361855.25  0.81%
                   16    8827457.25  0.09%    3282347.00  0.34%

Patches Applied     1    1007897.00  0.23%     361887.00  0.26%
                   16    8784741.75  0.39%    3240669.25  0.48%

Patches Enabled     1    1010227.50  0.39%     359749.25  0.56%
                   16    8756219.00  0.24%    3226608.75  0.97%

Patches Enabled     1    1050982.00  4.26%     357966.25  0.14%
 page shuffle      16    8672601.25  0.49%    3223177.75  0.40%

Patches enabled     1    1003238.00  0.22%     360211.00  0.22%
 shuffle w/ RFC    16    8767010.50  0.32%    3199874.00  0.71%

The results above are for a baseline with a linux-next-20191219 kernel,
that kernel with this patch set applied but page reporting disabled in
virtio-balloon, the patches applied and page reporting fully enabled, the
patches enabled with page shuffling enabled, and the patches applied with
page shuffling enabled and an RFC patch that makes used of MADV_FREE in
QEMU. These results include the deviation seen between the average value
reported here versus the high and/or low value. I observed that during the
test memory usage for the first three tests never dropped whereas with the
patches fully enabled the VM would drop to using only a few GB of the
host's memory when switching from memhog to page fault tests.

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

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

A brief history on the background of free page reporting can be found at:
https://lore.kernel.org/lkml/29f43d5796feed0dec8e8bb98b187d9dac03b900.camel@linux.intel.com/

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

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

Changes from v16:
https://lore.kernel.org/lkml/20200103210509.29237.18426.stgit@localhost.localdomain/
Rebased on linux-next-20200122
  Updated patch 2 to to account for removal of pr_info in __isolate_free_page
Updated patch title for patches 7, 8, and 9 to use prefix mm/page_reporting
No code changes other than conflict resolution for patch 2

---

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


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

--

Comments

Alexander Graf Jan. 23, 2020, 10:20 a.m. UTC | #1
Hi Alex,

On 22.01.20 18:43, 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. In each pass at
> least one sixteenth of each free list will be reported. By doing this we
> avoid racing against other threads that may be causing a high amount of
> memory churn.
> 
> The lowest page order currently scanned when reporting pages is
> pageblock_order so that this feature will not interfere with the use of
> Transparent Huge Pages in the case of virtualization.
> 
> Currently this is only in use by virtio-balloon however there is the hope
> that at some point in the future other hypervisors might be able to make
> use of it. In the virtio-balloon/QEMU implementation the hypervisor is
> currently using MADV_DONTNEED to indicate to the host kernel that the page
> is currently free. It will be zeroed and faulted back into the guest the
> next time the page is accessed.
> 
> To track if a page is reported or not the Uptodate flag was repurposed and
> used as a Reported flag for Buddy pages. We walk though the free list
> isolating pages and adding them to the scatterlist until we either
> encounter the end of the list, 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 features such as CPU turbo
> disabled in the BIOS.
> 
> Test                   page_fault1 (THP)    page_fault2
> Name            tasks  Process Iter  STDEV  Process Iter  STDEV
> Baseline            1    1012402.50  0.14%     361855.25  0.81%
>                     16    8827457.25  0.09%    3282347.00  0.34%
> 
> Patches Applied     1    1007897.00  0.23%     361887.00  0.26%
>                     16    8784741.75  0.39%    3240669.25  0.48%
> 
> Patches Enabled     1    1010227.50  0.39%     359749.25  0.56%
>                     16    8756219.00  0.24%    3226608.75  0.97%
> 
> Patches Enabled     1    1050982.00  4.26%     357966.25  0.14%
>   page shuffle      16    8672601.25  0.49%    3223177.75  0.40%
> 
> Patches enabled     1    1003238.00  0.22%     360211.00  0.22%
>   shuffle w/ RFC    16    8767010.50  0.32%    3199874.00  0.71%
> 
> The results above are for a baseline with a linux-next-20191219 kernel,
> that kernel with this patch set applied but page reporting disabled in
> virtio-balloon, the patches applied and page reporting fully enabled, the
> patches enabled with page shuffling enabled, and the patches applied with
> page shuffling enabled and an RFC patch that makes used of MADV_FREE in
> QEMU. These results include the deviation seen between the average value
> reported here versus the high and/or low value. I observed that during the
> test memory usage for the first three tests never dropped whereas with the
> patches fully enabled the VM would drop to using only a few GB of the
> host's memory when switching from memhog to page fault tests.
> 
> Any of the overhead visible with this patch set enabled seems due to page
> faults caused by accessing the reported pages and the host zeroing the page
> before giving it back to the guest. This overhead is much more visible when
> using THP than with standard 4K pages. In addition page shuffling seemed to
> increase the amount of faults generated due to an increase in memory churn.
> The overhead is reduced when using MADV_FREE as we can avoid the extra
> zeroing of the pages when they are reintroduced to the host, as can be seen
> when the RFC is applied with shuffling enabled.
> 
> The overall guest size is kept fairly small to only a few GB while the test
> is running. If the host memory were oversubscribed this patch set should
> result in a performance improvement as swapping memory in the host can be
> avoided.


I really like the approach overall. Voluntarily propagating free memory 
from a guest to the host has been a sore point ever since KVM was 
around. This solution looks like a very elegant way to do so.

The big piece I'm missing is the page cache. Linux will by default try 
to keep the free list as small as it can in favor of page cache, so most 
of the benefit of this patch set will be void in real world scenarios.

Traditionally, this was solved by creating pressure from the host 
through virtio-balloon: Exactly the piece that this patch set gets away 
with. I never liked "ballooning", because the host has very limited 
visibility into the actual memory utility of its guests. So leaving the 
decision on how much memory is actually needed at a given point in time 
should ideally stay with the guest.

What would keep us from applying the page hinting approach to inactive, 
clean page cache pages? With writeback in place as well, we would slowly 
propagate pages from

   dirty -> clean -> clean, inactive -> free -> host owned

which gives a guest a natural path to give up "not important" memory.

The big problem I see is that what I really want from a user's point of 
view is a tuneable that says "Automatically free clean page cache pages 
that were not accessed in the last X minutes". Otherwise we may run into 
the risk of evicting some times in use page cache pages.

I have a hard time grasping the mm code to understand how hard that 
would be to implement that though :).


Alex



Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
David Hildenbrand Jan. 23, 2020, 2:05 p.m. UTC | #2
On 23.01.20 11:20, Alexander Graf wrote:
> Hi Alex,
> 
> On 22.01.20 18:43, 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. In each pass at
>> least one sixteenth of each free list will be reported. By doing this we
>> avoid racing against other threads that may be causing a high amount of
>> memory churn.
>>
>> The lowest page order currently scanned when reporting pages is
>> pageblock_order so that this feature will not interfere with the use of
>> Transparent Huge Pages in the case of virtualization.
>>
>> Currently this is only in use by virtio-balloon however there is the hope
>> that at some point in the future other hypervisors might be able to make
>> use of it. In the virtio-balloon/QEMU implementation the hypervisor is
>> currently using MADV_DONTNEED to indicate to the host kernel that the page
>> is currently free. It will be zeroed and faulted back into the guest the
>> next time the page is accessed.
>>
>> To track if a page is reported or not the Uptodate flag was repurposed and
>> used as a Reported flag for Buddy pages. We walk though the free list
>> isolating pages and adding them to the scatterlist until we either
>> encounter the end of the list, 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 features such as CPU turbo
>> disabled in the BIOS.
>>
>> Test                   page_fault1 (THP)    page_fault2
>> Name            tasks  Process Iter  STDEV  Process Iter  STDEV
>> Baseline            1    1012402.50  0.14%     361855.25  0.81%
>>                     16    8827457.25  0.09%    3282347.00  0.34%
>>
>> Patches Applied     1    1007897.00  0.23%     361887.00  0.26%
>>                     16    8784741.75  0.39%    3240669.25  0.48%
>>
>> Patches Enabled     1    1010227.50  0.39%     359749.25  0.56%
>>                     16    8756219.00  0.24%    3226608.75  0.97%
>>
>> Patches Enabled     1    1050982.00  4.26%     357966.25  0.14%
>>   page shuffle      16    8672601.25  0.49%    3223177.75  0.40%
>>
>> Patches enabled     1    1003238.00  0.22%     360211.00  0.22%
>>   shuffle w/ RFC    16    8767010.50  0.32%    3199874.00  0.71%
>>
>> The results above are for a baseline with a linux-next-20191219 kernel,
>> that kernel with this patch set applied but page reporting disabled in
>> virtio-balloon, the patches applied and page reporting fully enabled, the
>> patches enabled with page shuffling enabled, and the patches applied with
>> page shuffling enabled and an RFC patch that makes used of MADV_FREE in
>> QEMU. These results include the deviation seen between the average value
>> reported here versus the high and/or low value. I observed that during the
>> test memory usage for the first three tests never dropped whereas with the
>> patches fully enabled the VM would drop to using only a few GB of the
>> host's memory when switching from memhog to page fault tests.
>>
>> Any of the overhead visible with this patch set enabled seems due to page
>> faults caused by accessing the reported pages and the host zeroing the page
>> before giving it back to the guest. This overhead is much more visible when
>> using THP than with standard 4K pages. In addition page shuffling seemed to
>> increase the amount of faults generated due to an increase in memory churn.
>> The overhead is reduced when using MADV_FREE as we can avoid the extra
>> zeroing of the pages when they are reintroduced to the host, as can be seen
>> when the RFC is applied with shuffling enabled.
>>
>> The overall guest size is kept fairly small to only a few GB while the test
>> is running. If the host memory were oversubscribed this patch set should
>> result in a performance improvement as swapping memory in the host can be
>> avoided.
> 
> 
> I really like the approach overall. Voluntarily propagating free memory 
> from a guest to the host has been a sore point ever since KVM was 
> around. This solution looks like a very elegant way to do so.
> 
> The big piece I'm missing is the page cache. Linux will by default try 
> to keep the free list as small as it can in favor of page cache, so most 
> of the benefit of this patch set will be void in real world scenarios.

One approach is to move (parts of) the page cache from the guest to the
hypervisor - e.g., using emulated NVDIMM or virtio-pmem.
Alexander Graf Jan. 23, 2020, 2:52 p.m. UTC | #3
On 23.01.20 15:05, David Hildenbrand wrote:
> On 23.01.20 11:20, Alexander Graf wrote:
>> Hi Alex,
>>
>> On 22.01.20 18:43, 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. In each pass at
>>> least one sixteenth of each free list will be reported. By doing this we
>>> avoid racing against other threads that may be causing a high amount of
>>> memory churn.
>>>
>>> The lowest page order currently scanned when reporting pages is
>>> pageblock_order so that this feature will not interfere with the use of
>>> Transparent Huge Pages in the case of virtualization.
>>>
>>> Currently this is only in use by virtio-balloon however there is the hope
>>> that at some point in the future other hypervisors might be able to make
>>> use of it. In the virtio-balloon/QEMU implementation the hypervisor is
>>> currently using MADV_DONTNEED to indicate to the host kernel that the page
>>> is currently free. It will be zeroed and faulted back into the guest the
>>> next time the page is accessed.
>>>
>>> To track if a page is reported or not the Uptodate flag was repurposed and
>>> used as a Reported flag for Buddy pages. We walk though the free list
>>> isolating pages and adding them to the scatterlist until we either
>>> encounter the end of the list, 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 features such as CPU turbo
>>> disabled in the BIOS.
>>>
>>> Test                   page_fault1 (THP)    page_fault2
>>> Name            tasks  Process Iter  STDEV  Process Iter  STDEV
>>> Baseline            1    1012402.50  0.14%     361855.25  0.81%
>>>                      16    8827457.25  0.09%    3282347.00  0.34%
>>>
>>> Patches Applied     1    1007897.00  0.23%     361887.00  0.26%
>>>                      16    8784741.75  0.39%    3240669.25  0.48%
>>>
>>> Patches Enabled     1    1010227.50  0.39%     359749.25  0.56%
>>>                      16    8756219.00  0.24%    3226608.75  0.97%
>>>
>>> Patches Enabled     1    1050982.00  4.26%     357966.25  0.14%
>>>    page shuffle      16    8672601.25  0.49%    3223177.75  0.40%
>>>
>>> Patches enabled     1    1003238.00  0.22%     360211.00  0.22%
>>>    shuffle w/ RFC    16    8767010.50  0.32%    3199874.00  0.71%
>>>
>>> The results above are for a baseline with a linux-next-20191219 kernel,
>>> that kernel with this patch set applied but page reporting disabled in
>>> virtio-balloon, the patches applied and page reporting fully enabled, the
>>> patches enabled with page shuffling enabled, and the patches applied with
>>> page shuffling enabled and an RFC patch that makes used of MADV_FREE in
>>> QEMU. These results include the deviation seen between the average value
>>> reported here versus the high and/or low value. I observed that during the
>>> test memory usage for the first three tests never dropped whereas with the
>>> patches fully enabled the VM would drop to using only a few GB of the
>>> host's memory when switching from memhog to page fault tests.
>>>
>>> Any of the overhead visible with this patch set enabled seems due to page
>>> faults caused by accessing the reported pages and the host zeroing the page
>>> before giving it back to the guest. This overhead is much more visible when
>>> using THP than with standard 4K pages. In addition page shuffling seemed to
>>> increase the amount of faults generated due to an increase in memory churn.
>>> The overhead is reduced when using MADV_FREE as we can avoid the extra
>>> zeroing of the pages when they are reintroduced to the host, as can be seen
>>> when the RFC is applied with shuffling enabled.
>>>
>>> The overall guest size is kept fairly small to only a few GB while the test
>>> is running. If the host memory were oversubscribed this patch set should
>>> result in a performance improvement as swapping memory in the host can be
>>> avoided.
>>
>>
>> I really like the approach overall. Voluntarily propagating free memory
>> from a guest to the host has been a sore point ever since KVM was
>> around. This solution looks like a very elegant way to do so.
>>
>> The big piece I'm missing is the page cache. Linux will by default try
>> to keep the free list as small as it can in favor of page cache, so most
>> of the benefit of this patch set will be void in real world scenarios.
> 
> One approach is to move (parts of) the page cache from the guest to the
> hypervisor - e.g., using emulated NVDIMM or virtio-pmem.

Whether you can do that depends heavily on your virtualization 
environment. On a host with single tenant VMs, that's definitely 
feasible. In a Kubernetes environment, it might also be feasible.

But when you have VMs that assume that the host is interfering with them 
as little as possible, it becomes harder:

How do you ensure fairness across different VMs' page cache that is 
munged into a single big host one?

Do you even have host page cache or are you using SR-IOV / mdev for 
storage for performance reasons?


The puzzle is still incomplete, even with NVDIMM exposure to the guest 
as an option unfortunately :).


Alex



Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
Alexander Duyck Jan. 23, 2020, 4:26 p.m. UTC | #4
On Thu, 2020-01-23 at 11:20 +0100, Alexander Graf wrote:
> Hi Alex,
> 
> On 22.01.20 18:43, 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. In each pass at
> > least one sixteenth of each free list will be reported. By doing this we
> > avoid racing against other threads that may be causing a high amount of
> > memory churn.
> > 
> > The lowest page order currently scanned when reporting pages is
> > pageblock_order so that this feature will not interfere with the use of
> > Transparent Huge Pages in the case of virtualization.
> > 
> > Currently this is only in use by virtio-balloon however there is the hope
> > that at some point in the future other hypervisors might be able to make
> > use of it. In the virtio-balloon/QEMU implementation the hypervisor is
> > currently using MADV_DONTNEED to indicate to the host kernel that the page
> > is currently free. It will be zeroed and faulted back into the guest the
> > next time the page is accessed.
> > 
> > To track if a page is reported or not the Uptodate flag was repurposed and
> > used as a Reported flag for Buddy pages. We walk though the free list
> > isolating pages and adding them to the scatterlist until we either
> > encounter the end of the list, 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 features such as CPU turbo
> > disabled in the BIOS.
> > 
> > Test                   page_fault1 (THP)    page_fault2
> > Name            tasks  Process Iter  STDEV  Process Iter  STDEV
> > Baseline            1    1012402.50  0.14%     361855.25  0.81%
> >                     16    8827457.25  0.09%    3282347.00  0.34%
> > 
> > Patches Applied     1    1007897.00  0.23%     361887.00  0.26%
> >                     16    8784741.75  0.39%    3240669.25  0.48%
> > 
> > Patches Enabled     1    1010227.50  0.39%     359749.25  0.56%
> >                     16    8756219.00  0.24%    3226608.75  0.97%
> > 
> > Patches Enabled     1    1050982.00  4.26%     357966.25  0.14%
> >   page shuffle      16    8672601.25  0.49%    3223177.75  0.40%
> > 
> > Patches enabled     1    1003238.00  0.22%     360211.00  0.22%
> >   shuffle w/ RFC    16    8767010.50  0.32%    3199874.00  0.71%
> > 
> > The results above are for a baseline with a linux-next-20191219 kernel,
> > that kernel with this patch set applied but page reporting disabled in
> > virtio-balloon, the patches applied and page reporting fully enabled, the
> > patches enabled with page shuffling enabled, and the patches applied with
> > page shuffling enabled and an RFC patch that makes used of MADV_FREE in
> > QEMU. These results include the deviation seen between the average value
> > reported here versus the high and/or low value. I observed that during the
> > test memory usage for the first three tests never dropped whereas with the
> > patches fully enabled the VM would drop to using only a few GB of the
> > host's memory when switching from memhog to page fault tests.
> > 
> > Any of the overhead visible with this patch set enabled seems due to page
> > faults caused by accessing the reported pages and the host zeroing the page
> > before giving it back to the guest. This overhead is much more visible when
> > using THP than with standard 4K pages. In addition page shuffling seemed to
> > increase the amount of faults generated due to an increase in memory churn.
> > The overhead is reduced when using MADV_FREE as we can avoid the extra
> > zeroing of the pages when they are reintroduced to the host, as can be seen
> > when the RFC is applied with shuffling enabled.
> > 
> > The overall guest size is kept fairly small to only a few GB while the test
> > is running. If the host memory were oversubscribed this patch set should
> > result in a performance improvement as swapping memory in the host can be
> > avoided.
> 
> I really like the approach overall. Voluntarily propagating free memory 
> from a guest to the host has been a sore point ever since KVM was 
> around. This solution looks like a very elegant way to do so.
> 
> The big piece I'm missing is the page cache. Linux will by default try 
> to keep the free list as small as it can in favor of page cache, so most 
> of the benefit of this patch set will be void in real world scenarios.

Agreed. This is a the next piece of this I plan to work on once this is
accepted. For now the quick and dirty approach is to essentially make use
of the /proc/sys/vm/drop_caches interface in the guest by either putting
it in a cronjob somewhere or to have it after memory intensive workloads.

> Traditionally, this was solved by creating pressure from the host 
> through virtio-balloon: Exactly the piece that this patch set gets away 
> with. I never liked "ballooning", because the host has very limited 
> visibility into the actual memory utility of its guests. So leaving the 
> decision on how much memory is actually needed at a given point in time 
> should ideally stay with the guest.
> 
> What would keep us from applying the page hinting approach to inactive, 
> clean page cache pages? With writeback in place as well, we would slowly 
> propagate pages from
> 
>    dirty -> clean -> clean, inactive -> free -> host owned
> 
> which gives a guest a natural path to give up "not important" memory.

I considered something similar. Basically one thought I had was to
essentially look at putting together some sort of epoch. When the host is
under memory pressure it would need to somehow notify the guest and then
the guest would start moving the epoch forward so that we start evicting
pages out of the page cache when the host is under memory pressure.

> The big problem I see is that what I really want from a user's point of 
> view is a tuneable that says "Automatically free clean page cache pages 
> that were not accessed in the last X minutes". Otherwise we may run into 
> the risk of evicting some times in use page cache pages.
> 
> I have a hard time grasping the mm code to understand how hard that 
> would be to implement that though :).
> 
> 
> Alex

Yeah, I am not exactly an expert on this either as I have only been
working int he MM tree for about a year now.

I have submitted this as a topic for LSF/MM summit[1] and I am hoping to
get some feedback on the best way to apply proactive memory pressure as
one of the subtopics if iti s selected.

Thanks.

- Alex

[1]: https://lore.kernel.org/linux-mm/4b8671d16573307da09afc56030c2a5f5a9c45bf.camel@linux.intel.com/
Alexander Graf Jan. 23, 2020, 4:54 p.m. UTC | #5
On 23.01.20 17:26, Alexander Duyck wrote:
> On Thu, 2020-01-23 at 11:20 +0100, Alexander Graf wrote:
>> Hi Alex,
>>
>> On 22.01.20 18:43, Alexander Duyck wrote:
[...]
>>> 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.
>>
>> I really like the approach overall. Voluntarily propagating free memory
>> from a guest to the host has been a sore point ever since KVM was
>> around. This solution looks like a very elegant way to do so.
>>
>> The big piece I'm missing is the page cache. Linux will by default try
>> to keep the free list as small as it can in favor of page cache, so most
>> of the benefit of this patch set will be void in real world scenarios.
> 
> Agreed. This is a the next piece of this I plan to work on once this is
> accepted. For now the quick and dirty approach is to essentially make use
> of the /proc/sys/vm/drop_caches interface in the guest by either putting
> it in a cronjob somewhere or to have it after memory intensive workloads.
> 
>> Traditionally, this was solved by creating pressure from the host
>> through virtio-balloon: Exactly the piece that this patch set gets away
>> with. I never liked "ballooning", because the host has very limited
>> visibility into the actual memory utility of its guests. So leaving the
>> decision on how much memory is actually needed at a given point in time
>> should ideally stay with the guest.
>>
>> What would keep us from applying the page hinting approach to inactive,
>> clean page cache pages? With writeback in place as well, we would slowly
>> propagate pages from
>>
>>     dirty -> clean -> clean, inactive -> free -> host owned
>>
>> which gives a guest a natural path to give up "not important" memory.
> 
> I considered something similar. Basically one thought I had was to
> essentially look at putting together some sort of epoch. When the host is
> under memory pressure it would need to somehow notify the guest and then
> the guest would start moving the epoch forward so that we start evicting
> pages out of the page cache when the host is under memory pressure.

I think we want to consider an interface in which the host actively asks 
guests to purge pages to be on the same line as swapping: The last line 
of defense.

In the normal mode of operation, you still want to shrink down 
voluntarily, so that everyone cooperatively tries to make free for new 
guests you could potentially run on the same host.

If you start to apply pressure to guests to find out of they might have 
some pages to spare, we're almost back to the old style ballooning approach.

Btw, have you ever looked at CMM2 [1]? With that, the host can 
essentially just "steal" pages from the guest when it needs any, without 
the need to execute the guest meanwhile. That means inside the host 
swapping path, CMM2 can just evict guest page cache pages as easily as 
we evict host page cache pages. To me, that's even more attractive in 
the swap / emergency case than an interface which requires the guest to 
proactively execute while we are in a low mem situation.

>> The big problem I see is that what I really want from a user's point of
>> view is a tuneable that says "Automatically free clean page cache pages
>> that were not accessed in the last X minutes". Otherwise we may run into
>> the risk of evicting some times in use page cache pages.
>>
>> I have a hard time grasping the mm code to understand how hard that
>> would be to implement that though :).
>>
>>
>> Alex
> 
> Yeah, I am not exactly an expert on this either as I have only been
> working int he MM tree for about a year now.
> 
> I have submitted this as a topic for LSF/MM summit[1] and I am hoping to
> get some feedback on the best way to apply proactive memory pressure as
> one of the subtopics if it is selected.

That's a great idea! Hannes just mentioned LSF/MM as a good forum to 
discuss this at last night, I'm glad to see you already picked up on it :).


Alex

[1] https://www.kernel.org/doc/ols/2006/ols2006v2-pages-321-336.pdf



Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
Dave Hansen Jan. 23, 2020, 5:20 p.m. UTC | #6
On 1/23/20 8:26 AM, Alexander Duyck wrote:
>> The big piece I'm missing is the page cache. Linux will by default try 
>> to keep the free list as small as it can in favor of page cache, so most 
>> of the benefit of this patch set will be void in real world scenarios.
> Agreed. This is a the next piece of this I plan to work on once this is
> accepted. For now the quick and dirty approach is to essentially make use
> of the /proc/sys/vm/drop_caches interface in the guest by either putting
> it in a cronjob somewhere or to have it after memory intensive workloads.

There was an implementation in "Clear Linux" that used this sysctl:

> https://github.com/Conan-Kudo/omv-kernel-rc/blob/master/0154-sysctl-vm-Fine-grained-cache-shrinking.patch

(I can't find it in the Clear repos at the moment, must not be used
currently).  But the idea was to have a little daemon in the host that
periodically applied some artificial pressure with this sysctl.  This
sysctl is a smaller hammer than /proc/sys/vm/drop_caches and lets you
drop small amounts of cache.

The right way to do it is probably to do real, generic reclaim instead
of drop_caches.

This isn't conceptually *that* far away from the "proactive reclaim"
that other folks have proposed:

	https://lwn.net/Articles/787611/
Alexander Duyck Jan. 23, 2020, 6:33 p.m. UTC | #7
On Thu, 2020-01-23 at 17:54 +0100, Alexander Graf wrote:
> 
> On 23.01.20 17:26, Alexander Duyck wrote:
> > On Thu, 2020-01-23 at 11:20 +0100, Alexander Graf wrote:
> > > Hi Alex,
> > > 
> > > On 22.01.20 18:43, Alexander Duyck wrote:
> [...]
> > > > 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.
> > > 
> > > I really like the approach overall. Voluntarily propagating free memory
> > > from a guest to the host has been a sore point ever since KVM was
> > > around. This solution looks like a very elegant way to do so.
> > > 
> > > The big piece I'm missing is the page cache. Linux will by default try
> > > to keep the free list as small as it can in favor of page cache, so most
> > > of the benefit of this patch set will be void in real world scenarios.
> > 
> > Agreed. This is a the next piece of this I plan to work on once this is
> > accepted. For now the quick and dirty approach is to essentially make use
> > of the /proc/sys/vm/drop_caches interface in the guest by either putting
> > it in a cronjob somewhere or to have it after memory intensive workloads.
> > 
> > > Traditionally, this was solved by creating pressure from the host
> > > through virtio-balloon: Exactly the piece that this patch set gets away
> > > with. I never liked "ballooning", because the host has very limited
> > > visibility into the actual memory utility of its guests. So leaving the
> > > decision on how much memory is actually needed at a given point in time
> > > should ideally stay with the guest.
> > > 
> > > What would keep us from applying the page hinting approach to inactive,
> > > clean page cache pages? With writeback in place as well, we would slowly
> > > propagate pages from
> > > 
> > >     dirty -> clean -> clean, inactive -> free -> host owned
> > > 
> > > which gives a guest a natural path to give up "not important" memory.
> > 
> > I considered something similar. Basically one thought I had was to
> > essentially look at putting together some sort of epoch. When the host is
> > under memory pressure it would need to somehow notify the guest and then
> > the guest would start moving the epoch forward so that we start evicting
> > pages out of the page cache when the host is under memory pressure.
> 
> I think we want to consider an interface in which the host actively asks 
> guests to purge pages to be on the same line as swapping: The last line 
> of defense.

I suppose. The only reason I was thinking that we may want to look at
doing something like that was to avoid putting pressure on the guest when
the host doesn't need us to.

> In the normal mode of operation, you still want to shrink down 
> voluntarily, so that everyone cooperatively tries to make free for new 
> guests you could potentially run on the same host.
> 
> If you start to apply pressure to guests to find out of they might have 
> some pages to spare, we're almost back to the old style ballooning approach.

Thats true. In addition we avoid possible issues with us trying to flush
out a bunch of memory from multiple guests as once since they would be
proactively freeing the memory.

I'm thinking the inactive state could be something similar to MADV_FREE in
terms of behavior.  If it sits in the queue for long enough we decide
nobody is using it anymore so it is freed, but if it is accessed it is
cheap for us to just put it back without much in the way of overhead.

> Btw, have you ever looked at CMM2 [1]? With that, the host can 
> essentially just "steal" pages from the guest when it needs any, without 
> the need to execute the guest meanwhile. That means inside the host 
> swapping path, CMM2 can just evict guest page cache pages as easily as 
> we evict host page cache pages. To me, that's even more attractive in 
> the swap / emergency case than an interface which requires the guest to 
> proactively execute while we are in a low mem situation.

<snip>

> [1] https://www.kernel.org/doc/ols/2006/ols2006v2-pages-321-336.pdf

I hadn't read through this before. If nothing else the verbiage is useful
since what we are discussing is essentially how to deal with the
"volatile" pages within the system, the "unused" pages are the ones we
have reported to the host with the page reporting, and the "stable" pages
are those pages that have been faulted back into the guest when it
accessed them.

I can see there would be some advantages to CMM2, however it seems like it
is adding a significant amount of state to pages since it has to support a
fairly significant number of states and then there is the added complexity
for all the transitions in and out of stable from the various states
depending on how things are being changed.

Do you happen to know if anyone has done any research into how much
overhead is added with CMM2 enabled? I'd be curious since it seems like
the paper mentions having to track a signficant number of state
transitions for the memory throughout the kernel.
Alexander Graf Jan. 23, 2020, 6:47 p.m. UTC | #8
>> Am 23.01.2020 um 19:34 schrieb Alexander Duyck <alexander.h.duyck@linux.intel.com>:
>> 
>> On Thu, 2020-01-23 at 17:54 +0100, Alexander Graf wrote:
>>> On 23.01.20 17:26, Alexander Duyck wrote:
>>> On Thu, 2020-01-23 at 11:20 +0100, Alexander Graf wrote:
>>>> Hi Alex,
>>>>> On 22.01.20 18:43, Alexander Duyck wrote:
>> [...]
>>>>> 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.
>>>> I really like the approach overall. Voluntarily propagating free memory
>>>> from a guest to the host has been a sore point ever since KVM was
>>>> around. This solution looks like a very elegant way to do so.
>>>> The big piece I'm missing is the page cache. Linux will by default try
>>>> to keep the free list as small as it can in favor of page cache, so most
>>>> of the benefit of this patch set will be void in real world scenarios.
>>> Agreed. This is a the next piece of this I plan to work on once this is
>>> accepted. For now the quick and dirty approach is to essentially make use
>>> of the /proc/sys/vm/drop_caches interface in the guest by either putting
>>> it in a cronjob somewhere or to have it after memory intensive workloads.
>>>> Traditionally, this was solved by creating pressure from the host
>>>> through virtio-balloon: Exactly the piece that this patch set gets away
>>>> with. I never liked "ballooning", because the host has very limited
>>>> visibility into the actual memory utility of its guests. So leaving the
>>>> decision on how much memory is actually needed at a given point in time
>>>> should ideally stay with the guest.
>>>> What would keep us from applying the page hinting approach to inactive,
>>>> clean page cache pages? With writeback in place as well, we would slowly
>>>> propagate pages from
>>>>   dirty -> clean -> clean, inactive -> free -> host owned
>>>> which gives a guest a natural path to give up "not important" memory.
>>> I considered something similar. Basically one thought I had was to
>>> essentially look at putting together some sort of epoch. When the host is
>>> under memory pressure it would need to somehow notify the guest and then
>>> the guest would start moving the epoch forward so that we start evicting
>>> pages out of the page cache when the host is under memory pressure.
>> I think we want to consider an interface in which the host actively asks
>> guests to purge pages to be on the same line as swapping: The last line
>> of defense.
> 
> I suppose. The only reason I was thinking that we may want to look at
> doing something like that was to avoid putting pressure on the guest when
> the host doesn't need us to.
> 
>> In the normal mode of operation, you still want to shrink down
>> voluntarily, so that everyone cooperatively tries to make free for new
>> guests you could potentially run on the same host.
>> If you start to apply pressure to guests to find out of they might have
>> some pages to spare, we're almost back to the old style ballooning approach.
> 
> Thats true. In addition we avoid possible issues with us trying to flush
> out a bunch of memory from multiple guests as once since they would be
> proactively freeing the memory.
> 
> I'm thinking the inactive state could be something similar to MADV_FREE in
> terms of behavior.  If it sits in the queue for long enough we decide
> nobody is using it anymore so it is freed, but if it is accessed it is
> cheap for us to just put it back without much in the way of overhead.

I think the main difference between the MADV_FREE and what we want is that we also want to pull the page into active state on read.

But sure, that's a possible interface. What I'd like to make sure of is that we can have different host policies: discard the page straight away, keep it for a fixed amount of time or discard it lazily on pressure. As long as the guest gives the host its clean pages voluntarily, I'm happy.

Btw, have you already given thought to the faulting interface when a page was evicted? That's where it gets especially tricky. With a simple "discard the page straight away" style interface, we would not have to fault.

> 
>> Btw, have you ever looked at CMM2 [1]? With that, the host can
>> essentially just "steal" pages from the guest when it needs any, without
>> the need to execute the guest meanwhile. That means inside the host
>> swapping path, CMM2 can just evict guest page cache pages as easily as
>> we evict host page cache pages. To me, that's even more attractive in
>> the swap / emergency case than an interface which requires the guest to
>> proactively execute while we are in a low mem situation.
> 
> <snip>
> 
>> [1] https://www.kernel.org/doc/ols/2006/ols2006v2-pages-321-336.pdf
> 
> I hadn't read through this before. If nothing else the verbiage is useful
> since what we are discussing is essentially how to deal with the
> "volatile" pages within the system, the "unused" pages are the ones we
> have reported to the host with the page reporting, and the "stable" pages
> are those pages that have been faulted back into the guest when it
> accessed them.
> 
> I can see there would be some advantages to CMM2, however it seems like it
> is adding a significant amount of state to pages since it has to support a
> fairly significant number of states and then there is the added complexity
> for all the transitions in and out of stable from the various states
> depending on how things are being changed.
> 
> Do you happen to know if anyone has done any research into how much
> overhead is added with CMM2 enabled? I'd be curious since it seems like
> the paper mentions having to track a signficant number of state
> transitions for the memory throughout the kernel.

Let me add Christian Borntraeger to the thread. He can definitely help on that side. I asked him earlier today and he confirmed that cmm2 is in active use on s390.

Alex




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
Johannes Weiner Jan. 23, 2020, 7:17 p.m. UTC | #9
On Thu, Jan 23, 2020 at 08:26:39AM -0800, Alexander Duyck wrote:
> On Thu, 2020-01-23 at 11:20 +0100, Alexander Graf wrote:
> > Hi Alex,
> > 
> > On 22.01.20 18:43, 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. In each pass at
> > > least one sixteenth of each free list will be reported. By doing this we
> > > avoid racing against other threads that may be causing a high amount of
> > > memory churn.
> > > 
> > > The lowest page order currently scanned when reporting pages is
> > > pageblock_order so that this feature will not interfere with the use of
> > > Transparent Huge Pages in the case of virtualization.
> > > 
> > > Currently this is only in use by virtio-balloon however there is the hope
> > > that at some point in the future other hypervisors might be able to make
> > > use of it. In the virtio-balloon/QEMU implementation the hypervisor is
> > > currently using MADV_DONTNEED to indicate to the host kernel that the page
> > > is currently free. It will be zeroed and faulted back into the guest the
> > > next time the page is accessed.
> > > 
> > > To track if a page is reported or not the Uptodate flag was repurposed and
> > > used as a Reported flag for Buddy pages. We walk though the free list
> > > isolating pages and adding them to the scatterlist until we either
> > > encounter the end of the list, 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 features such as CPU turbo
> > > disabled in the BIOS.
> > > 
> > > Test                   page_fault1 (THP)    page_fault2
> > > Name            tasks  Process Iter  STDEV  Process Iter  STDEV
> > > Baseline            1    1012402.50  0.14%     361855.25  0.81%
> > >                     16    8827457.25  0.09%    3282347.00  0.34%
> > > 
> > > Patches Applied     1    1007897.00  0.23%     361887.00  0.26%
> > >                     16    8784741.75  0.39%    3240669.25  0.48%
> > > 
> > > Patches Enabled     1    1010227.50  0.39%     359749.25  0.56%
> > >                     16    8756219.00  0.24%    3226608.75  0.97%
> > > 
> > > Patches Enabled     1    1050982.00  4.26%     357966.25  0.14%
> > >   page shuffle      16    8672601.25  0.49%    3223177.75  0.40%
> > > 
> > > Patches enabled     1    1003238.00  0.22%     360211.00  0.22%
> > >   shuffle w/ RFC    16    8767010.50  0.32%    3199874.00  0.71%
> > > 
> > > The results above are for a baseline with a linux-next-20191219 kernel,
> > > that kernel with this patch set applied but page reporting disabled in
> > > virtio-balloon, the patches applied and page reporting fully enabled, the
> > > patches enabled with page shuffling enabled, and the patches applied with
> > > page shuffling enabled and an RFC patch that makes used of MADV_FREE in
> > > QEMU. These results include the deviation seen between the average value
> > > reported here versus the high and/or low value. I observed that during the
> > > test memory usage for the first three tests never dropped whereas with the
> > > patches fully enabled the VM would drop to using only a few GB of the
> > > host's memory when switching from memhog to page fault tests.
> > > 
> > > Any of the overhead visible with this patch set enabled seems due to page
> > > faults caused by accessing the reported pages and the host zeroing the page
> > > before giving it back to the guest. This overhead is much more visible when
> > > using THP than with standard 4K pages. In addition page shuffling seemed to
> > > increase the amount of faults generated due to an increase in memory churn.
> > > The overhead is reduced when using MADV_FREE as we can avoid the extra
> > > zeroing of the pages when they are reintroduced to the host, as can be seen
> > > when the RFC is applied with shuffling enabled.
> > > 
> > > The overall guest size is kept fairly small to only a few GB while the test
> > > is running. If the host memory were oversubscribed this patch set should
> > > result in a performance improvement as swapping memory in the host can be
> > > avoided.
> > 
> > I really like the approach overall. Voluntarily propagating free memory 
> > from a guest to the host has been a sore point ever since KVM was 
> > around. This solution looks like a very elegant way to do so.
> > 
> > The big piece I'm missing is the page cache. Linux will by default try 
> > to keep the free list as small as it can in favor of page cache, so most 
> > of the benefit of this patch set will be void in real world scenarios.
> 
> Agreed. This is a the next piece of this I plan to work on once this is
> accepted. For now the quick and dirty approach is to essentially make use
> of the /proc/sys/vm/drop_caches interface in the guest by either putting
> it in a cronjob somewhere or to have it after memory intensive workloads.
> 
> > Traditionally, this was solved by creating pressure from the host 
> > through virtio-balloon: Exactly the piece that this patch set gets away 
> > with. I never liked "ballooning", because the host has very limited 
> > visibility into the actual memory utility of its guests. So leaving the 
> > decision on how much memory is actually needed at a given point in time 
> > should ideally stay with the guest.
> > 
> > What would keep us from applying the page hinting approach to inactive, 
> > clean page cache pages? With writeback in place as well, we would slowly 
> > propagate pages from
> > 
> >    dirty -> clean -> clean, inactive -> free -> host owned
> > 
> > which gives a guest a natural path to give up "not important" memory.
> 
> I considered something similar. Basically one thought I had was to
> essentially look at putting together some sort of epoch. When the host is
> under memory pressure it would need to somehow notify the guest and then
> the guest would start moving the epoch forward so that we start evicting
> pages out of the page cache when the host is under memory pressure.
> 
> > The big problem I see is that what I really want from a user's point of 
> > view is a tuneable that says "Automatically free clean page cache pages 
> > that were not accessed in the last X minutes". Otherwise we may run into 
> > the risk of evicting some times in use page cache pages.
> > 
> > I have a hard time grasping the mm code to understand how hard that 
> > would be to implement that though :).
> > 
> > 
> > Alex
> 
> Yeah, I am not exactly an expert on this either as I have only been
> working int he MM tree for about a year now.
> 
> I have submitted this as a topic for LSF/MM summit[1] and I am hoping to
> get some feedback on the best way to apply proactive memory pressure as
> one of the subtopics if iti s selected.

I've been working on a proactive reclaim project that shrinks
workloads to their smallest, still healthy, memory footprint.

Because we (FB) have a similar problem with containers: in order to
know how many workloads can be safely combined on a host, we first
need to know how much memory a given workload truly requires - as
opposed to how many pages it would gobble up for one-off cache and
cold anon regions if it had the whole machine to itself.

This userspace tool uses cgroups and psi to adjust the memory limits
of workloads in a pressure feedback loop. It targets a minimal rate of
refaults/swapping/reclaim activity to identify the point where all the
cold pages have been evicted and we're *just* about to start eating
into warmer memory.

With SSDs, control over pressure is fine-grained enough that we can
run it on even highly latency-sensitive things like our web servers
without impacting response time meaningfully.

It harnesses the VM's existing LRU/clock algorithm to identify the
pages which are most likely to be cold, so the approach scales to
large memory sizes (256G+) with only minor CPU overhead.

https://github.com/facebookincubator/senpai

The same concept could be applicable to shrinking guests proactively
in virtualized environments?
Konrad Rzeszutek Wilk Jan. 23, 2020, 7:23 p.m. UTC | #10
On Thu, Jan 23, 2020 at 09:20:15AM -0800, Dave Hansen wrote:
> On 1/23/20 8:26 AM, Alexander Duyck wrote:
> >> The big piece I'm missing is the page cache. Linux will by default try 
> >> to keep the free list as small as it can in favor of page cache, so most 
> >> of the benefit of this patch set will be void in real world scenarios.
> > Agreed. This is a the next piece of this I plan to work on once this is
> > accepted. For now the quick and dirty approach is to essentially make use
> > of the /proc/sys/vm/drop_caches interface in the guest by either putting
> > it in a cronjob somewhere or to have it after memory intensive workloads.
> 
> There was an implementation in "Clear Linux" that used this sysctl:
> 
> > https://github.com/Conan-Kudo/omv-kernel-rc/blob/master/0154-sysctl-vm-Fine-grained-cache-shrinking.patch
> 
> (I can't find it in the Clear repos at the moment, must not be used
> currently).  But the idea was to have a little daemon in the host that
> periodically applied some artificial pressure with this sysctl.  This
> sysctl is a smaller hammer than /proc/sys/vm/drop_caches and lets you
> drop small amounts of cache.
> 
> The right way to do it is probably to do real, generic reclaim instead
> of drop_caches.

This  sounds like Transcendent Memory (https://www.linux-kvm.org/images/d/d7/TmemNotVirt-Linuxcon2011-Final.pdf)
which has (which is in the Linux kernel) a driver to push on the swapper
and everything else to evict pages to the hypervisor. 

Look at cleancache and frontswap and xen-selfballoon.c (was removed by
by 814bbf49dcd0ad642e7ceb8991e57555c5472cce)

Avi Kivity pointed out one big issue with all of this - customers have
to be nicely behaved - which they don't seem to be.

But I would recommend you look at cleancache for the page cache.

> 
> This isn't conceptually *that* far away from the "proactive reclaim"
> that other folks have proposed:
> 
> 	https://lwn.net/Articles/787611/
Alexander Duyck Jan. 23, 2020, 10:05 p.m. UTC | #11
On Thu, 2020-01-23 at 18:47 +0000, Graf (AWS), Alexander wrote:
> > > Am 23.01.2020 um 19:34 schrieb Alexander Duyck <alexander.h.duyck@linux.intel.com>:
> > > 
> > > On Thu, 2020-01-23 at 17:54 +0100, Alexander Graf wrote:
> > > > On 23.01.20 17:26, Alexander Duyck wrote:
> > > > On Thu, 2020-01-23 at 11:20 +0100, Alexander Graf wrote:
> > > > > Hi Alex,
> > > > > > On 22.01.20 18:43, Alexander Duyck wrote:
> > > [...]
> > > > > > 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.
> > > > > I really like the approach overall. Voluntarily propagating free memory
> > > > > from a guest to the host has been a sore point ever since KVM was
> > > > > around. This solution looks like a very elegant way to do so.
> > > > > The big piece I'm missing is the page cache. Linux will by default try
> > > > > to keep the free list as small as it can in favor of page cache, so most
> > > > > of the benefit of this patch set will be void in real world scenarios.
> > > > Agreed. This is a the next piece of this I plan to work on once this is
> > > > accepted. For now the quick and dirty approach is to essentially make use
> > > > of the /proc/sys/vm/drop_caches interface in the guest by either putting
> > > > it in a cronjob somewhere or to have it after memory intensive workloads.
> > > > > Traditionally, this was solved by creating pressure from the host
> > > > > through virtio-balloon: Exactly the piece that this patch set gets away
> > > > > with. I never liked "ballooning", because the host has very limited
> > > > > visibility into the actual memory utility of its guests. So leaving the
> > > > > decision on how much memory is actually needed at a given point in time
> > > > > should ideally stay with the guest.
> > > > > What would keep us from applying the page hinting approach to inactive,
> > > > > clean page cache pages? With writeback in place as well, we would slowly
> > > > > propagate pages from
> > > > >   dirty -> clean -> clean, inactive -> free -> host owned
> > > > > which gives a guest a natural path to give up "not important" memory.
> > > > I considered something similar. Basically one thought I had was to
> > > > essentially look at putting together some sort of epoch. When the host is
> > > > under memory pressure it would need to somehow notify the guest and then
> > > > the guest would start moving the epoch forward so that we start evicting
> > > > pages out of the page cache when the host is under memory pressure.
> > > I think we want to consider an interface in which the host actively asks
> > > guests to purge pages to be on the same line as swapping: The last line
> > > of defense.
> > 
> > I suppose. The only reason I was thinking that we may want to look at
> > doing something like that was to avoid putting pressure on the guest when
> > the host doesn't need us to.
> > 
> > > In the normal mode of operation, you still want to shrink down
> > > voluntarily, so that everyone cooperatively tries to make free for new
> > > guests you could potentially run on the same host.
> > > If you start to apply pressure to guests to find out of they might have
> > > some pages to spare, we're almost back to the old style ballooning approach.
> > 
> > Thats true. In addition we avoid possible issues with us trying to flush
> > out a bunch of memory from multiple guests as once since they would be
> > proactively freeing the memory.
> > 
> > I'm thinking the inactive state could be something similar to MADV_FREE in
> > terms of behavior.  If it sits in the queue for long enough we decide
> > nobody is using it anymore so it is freed, but if it is accessed it is
> > cheap for us to just put it back without much in the way of overhead.
> 
> I think the main difference between the MADV_FREE and what we want is
> that we also want to pull the page into active state on read.
> 
> But sure, that's a possible interface. What I'd like to make sure of is
> that we can have different host policies: discard the page straight
> away, keep it for a fixed amount of time or discard it lazily on
> pressure. As long as the guest gives the host its clean pages
> voluntarily, I'm happy.

Well the current model I am working with has us using MAD_DONTNEED from
the hypervisor if the unsued page is reported. So it will still have to be
pulled back in, but it will start out as a zeroed page.

> Btw, have you already given thought to the faulting interface when a
> page was evicted? That's where it gets especially tricky. With a simple
> "discard the page straight away" style interface, we would not have to
> fault.

So the fault I was referring to would be inside the guest only. Basically
we would keep the page for a little while longer while it is inactive and
just let the mapping go. Then if something accesses it before we finally
release it we don't pay the heavy cost of having to get it back from the
host and then copying the memory back in from swap or the file.

I'm just loosely basing that on the "proactive reclaim" idea that was
proposed back at the last lsf/mm summit (https://lwn.net/Articles/787611/)
. I still haven't even started work on any of those pieces yet nor looked
at it too closely. I'm still in the information gathering phase.


> > > Btw, have you ever looked at CMM2 [1]? With that, the host can
> > > essentially just "steal" pages from the guest when it needs any, without
> > > the need to execute the guest meanwhile. That means inside the host
> > > swapping path, CMM2 can just evict guest page cache pages as easily as
> > > we evict host page cache pages. To me, that's even more attractive in
> > > the swap / emergency case than an interface which requires the guest to
> > > proactively execute while we are in a low mem situation.
> > 
> > <snip>
> > 
> > > [1] https://www.kernel.org/doc/ols/2006/ols2006v2-pages-321-336.pdf
> > 
> > I hadn't read through this before. If nothing else the verbiage is useful
> > since what we are discussing is essentially how to deal with the
> > "volatile" pages within the system, the "unused" pages are the ones we
> > have reported to the host with the page reporting, and the "stable" pages
> > are those pages that have been faulted back into the guest when it
> > accessed them.
> > 
> > I can see there would be some advantages to CMM2, however it seems like it
> > is adding a significant amount of state to pages since it has to support a
> > fairly significant number of states and then there is the added complexity
> > for all the transitions in and out of stable from the various states
> > depending on how things are being changed.
> > 
> > Do you happen to know if anyone has done any research into how much
> > overhead is added with CMM2 enabled? I'd be curious since it seems like
> > the paper mentions having to track a signficant number of state
> > transitions for the memory throughout the kernel.
> 
> Let me add Christian Borntraeger to the thread. He can definitely help
> on that side. I asked him earlier today and he confirmed that cmm2 is in
> active use on s390.
> 
> Alex

Okay, sounds good.

- Alex
Alexander Duyck Jan. 23, 2020, 10:29 p.m. UTC | #12
On Thu, 2020-01-23 at 14:17 -0500, Johannes Weiner wrote:
> On Thu, Jan 23, 2020 at 08:26:39AM -0800, Alexander Duyck wrote:
> > On Thu, 2020-01-23 at 11:20 +0100, Alexander Graf wrote:
> > > Hi Alex,
> > > 
> > > On 22.01.20 18:43, 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. In each pass at
> > > > least one sixteenth of each free list will be reported. By doing this we
> > > > avoid racing against other threads that may be causing a high amount of
> > > > memory churn.
> > > > 
> > > > The lowest page order currently scanned when reporting pages is
> > > > pageblock_order so that this feature will not interfere with the use of
> > > > Transparent Huge Pages in the case of virtualization.
> > > > 
> > > > Currently this is only in use by virtio-balloon however there is the hope
> > > > that at some point in the future other hypervisors might be able to make
> > > > use of it. In the virtio-balloon/QEMU implementation the hypervisor is
> > > > currently using MADV_DONTNEED to indicate to the host kernel that the page
> > > > is currently free. It will be zeroed and faulted back into the guest the
> > > > next time the page is accessed.
> > > > 
> > > > To track if a page is reported or not the Uptodate flag was repurposed and
> > > > used as a Reported flag for Buddy pages. We walk though the free list
> > > > isolating pages and adding them to the scatterlist until we either
> > > > encounter the end of the list, 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 features such as CPU turbo
> > > > disabled in the BIOS.
> > > > 
> > > > Test                   page_fault1 (THP)    page_fault2
> > > > Name            tasks  Process Iter  STDEV  Process Iter  STDEV
> > > > Baseline            1    1012402.50  0.14%     361855.25  0.81%
> > > >                     16    8827457.25  0.09%    3282347.00  0.34%
> > > > 
> > > > Patches Applied     1    1007897.00  0.23%     361887.00  0.26%
> > > >                     16    8784741.75  0.39%    3240669.25  0.48%
> > > > 
> > > > Patches Enabled     1    1010227.50  0.39%     359749.25  0.56%
> > > >                     16    8756219.00  0.24%    3226608.75  0.97%
> > > > 
> > > > Patches Enabled     1    1050982.00  4.26%     357966.25  0.14%
> > > >   page shuffle      16    8672601.25  0.49%    3223177.75  0.40%
> > > > 
> > > > Patches enabled     1    1003238.00  0.22%     360211.00  0.22%
> > > >   shuffle w/ RFC    16    8767010.50  0.32%    3199874.00  0.71%
> > > > 
> > > > The results above are for a baseline with a linux-next-20191219 kernel,
> > > > that kernel with this patch set applied but page reporting disabled in
> > > > virtio-balloon, the patches applied and page reporting fully enabled, the
> > > > patches enabled with page shuffling enabled, and the patches applied with
> > > > page shuffling enabled and an RFC patch that makes used of MADV_FREE in
> > > > QEMU. These results include the deviation seen between the average value
> > > > reported here versus the high and/or low value. I observed that during the
> > > > test memory usage for the first three tests never dropped whereas with the
> > > > patches fully enabled the VM would drop to using only a few GB of the
> > > > host's memory when switching from memhog to page fault tests.
> > > > 
> > > > Any of the overhead visible with this patch set enabled seems due to page
> > > > faults caused by accessing the reported pages and the host zeroing the page
> > > > before giving it back to the guest. This overhead is much more visible when
> > > > using THP than with standard 4K pages. In addition page shuffling seemed to
> > > > increase the amount of faults generated due to an increase in memory churn.
> > > > The overhead is reduced when using MADV_FREE as we can avoid the extra
> > > > zeroing of the pages when they are reintroduced to the host, as can be seen
> > > > when the RFC is applied with shuffling enabled.
> > > > 
> > > > The overall guest size is kept fairly small to only a few GB while the test
> > > > is running. If the host memory were oversubscribed this patch set should
> > > > result in a performance improvement as swapping memory in the host can be
> > > > avoided.
> > > 
> > > I really like the approach overall. Voluntarily propagating free memory 
> > > from a guest to the host has been a sore point ever since KVM was 
> > > around. This solution looks like a very elegant way to do so.
> > > 
> > > The big piece I'm missing is the page cache. Linux will by default try 
> > > to keep the free list as small as it can in favor of page cache, so most 
> > > of the benefit of this patch set will be void in real world scenarios.
> > 
> > Agreed. This is a the next piece of this I plan to work on once this is
> > accepted. For now the quick and dirty approach is to essentially make use
> > of the /proc/sys/vm/drop_caches interface in the guest by either putting
> > it in a cronjob somewhere or to have it after memory intensive workloads.
> > 
> > > Traditionally, this was solved by creating pressure from the host 
> > > through virtio-balloon: Exactly the piece that this patch set gets away 
> > > with. I never liked "ballooning", because the host has very limited 
> > > visibility into the actual memory utility of its guests. So leaving the 
> > > decision on how much memory is actually needed at a given point in time 
> > > should ideally stay with the guest.
> > > 
> > > What would keep us from applying the page hinting approach to inactive, 
> > > clean page cache pages? With writeback in place as well, we would slowly 
> > > propagate pages from
> > > 
> > >    dirty -> clean -> clean, inactive -> free -> host owned
> > > 
> > > which gives a guest a natural path to give up "not important" memory.
> > 
> > I considered something similar. Basically one thought I had was to
> > essentially look at putting together some sort of epoch. When the host is
> > under memory pressure it would need to somehow notify the guest and then
> > the guest would start moving the epoch forward so that we start evicting
> > pages out of the page cache when the host is under memory pressure.
> > 
> > > The big problem I see is that what I really want from a user's point of 
> > > view is a tuneable that says "Automatically free clean page cache pages 
> > > that were not accessed in the last X minutes". Otherwise we may run into 
> > > the risk of evicting some times in use page cache pages.
> > > 
> > > I have a hard time grasping the mm code to understand how hard that 
> > > would be to implement that though :).
> > > 
> > > 
> > > Alex
> > 
> > Yeah, I am not exactly an expert on this either as I have only been
> > working int he MM tree for about a year now.
> > 
> > I have submitted this as a topic for LSF/MM summit[1] and I am hoping to
> > get some feedback on the best way to apply proactive memory pressure as
> > one of the subtopics if iti s selected.
> 
> I've been working on a proactive reclaim project that shrinks
> workloads to their smallest, still healthy, memory footprint.
> 
> Because we (FB) have a similar problem with containers: in order to
> know how many workloads can be safely combined on a host, we first
> need to know how much memory a given workload truly requires - as
> opposed to how many pages it would gobble up for one-off cache and
> cold anon regions if it had the whole machine to itself.
> 
> This userspace tool uses cgroups and psi to adjust the memory limits
> of workloads in a pressure feedback loop. It targets a minimal rate of
> refaults/swapping/reclaim activity to identify the point where all the
> cold pages have been evicted and we're *just* about to start eating
> into warmer memory.
> 
> With SSDs, control over pressure is fine-grained enough that we can
> run it on even highly latency-sensitive things like our web servers
> without impacting response time meaningfully.
> 
> It harnesses the VM's existing LRU/clock algorithm to identify the
> pages which are most likely to be cold, so the approach scales to
> large memory sizes (256G+) with only minor CPU overhead.
> 
> https://github.com/facebookincubator/senpai
> 
> The same concept could be applicable to shrinking guests proactively
> in virtualized environments?

Looking it over this kind of does what we would want to do, however we
would need to find a way to have this work without the cgroup requirement.
Essentially we would have the guest running this and then proactively
keeping its own resources in check.

- Alex
Dave Hansen Jan. 23, 2020, 11:24 p.m. UTC | #13
On 1/23/20 2:29 PM, Alexander Duyck wrote:
> Looking it over this kind of does what we would want to do, however we
> would need to find a way to have this work without the cgroup requirement.
> Essentially we would have the guest running this and then proactively
> keeping its own resources in check.

It's also worth noting that for Clear Linux, the guests are doing
container-like things (https://katacontainers.io/) but inside virtual
machines.  The VM content in this case is known and relatively trusted,
so generally isn't a stretch to assume that it can run a daemon and will
mostly play nice.
David Hildenbrand Jan. 24, 2020, 1:25 p.m. UTC | #14
On 23.01.20 15:52, Alexander Graf wrote:
> 
> 
> On 23.01.20 15:05, David Hildenbrand wrote:
>> On 23.01.20 11:20, Alexander Graf wrote:
>>> Hi Alex,
>>>
>>> On 22.01.20 18:43, 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. In each pass at
>>>> least one sixteenth of each free list will be reported. By doing this we
>>>> avoid racing against other threads that may be causing a high amount of
>>>> memory churn.
>>>>
>>>> The lowest page order currently scanned when reporting pages is
>>>> pageblock_order so that this feature will not interfere with the use of
>>>> Transparent Huge Pages in the case of virtualization.
>>>>
>>>> Currently this is only in use by virtio-balloon however there is the hope
>>>> that at some point in the future other hypervisors might be able to make
>>>> use of it. In the virtio-balloon/QEMU implementation the hypervisor is
>>>> currently using MADV_DONTNEED to indicate to the host kernel that the page
>>>> is currently free. It will be zeroed and faulted back into the guest the
>>>> next time the page is accessed.
>>>>
>>>> To track if a page is reported or not the Uptodate flag was repurposed and
>>>> used as a Reported flag for Buddy pages. We walk though the free list
>>>> isolating pages and adding them to the scatterlist until we either
>>>> encounter the end of the list, 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 features such as CPU turbo
>>>> disabled in the BIOS.
>>>>
>>>> Test                   page_fault1 (THP)    page_fault2
>>>> Name            tasks  Process Iter  STDEV  Process Iter  STDEV
>>>> Baseline            1    1012402.50  0.14%     361855.25  0.81%
>>>>                      16    8827457.25  0.09%    3282347.00  0.34%
>>>>
>>>> Patches Applied     1    1007897.00  0.23%     361887.00  0.26%
>>>>                      16    8784741.75  0.39%    3240669.25  0.48%
>>>>
>>>> Patches Enabled     1    1010227.50  0.39%     359749.25  0.56%
>>>>                      16    8756219.00  0.24%    3226608.75  0.97%
>>>>
>>>> Patches Enabled     1    1050982.00  4.26%     357966.25  0.14%
>>>>    page shuffle      16    8672601.25  0.49%    3223177.75  0.40%
>>>>
>>>> Patches enabled     1    1003238.00  0.22%     360211.00  0.22%
>>>>    shuffle w/ RFC    16    8767010.50  0.32%    3199874.00  0.71%
>>>>
>>>> The results above are for a baseline with a linux-next-20191219 kernel,
>>>> that kernel with this patch set applied but page reporting disabled in
>>>> virtio-balloon, the patches applied and page reporting fully enabled, the
>>>> patches enabled with page shuffling enabled, and the patches applied with
>>>> page shuffling enabled and an RFC patch that makes used of MADV_FREE in
>>>> QEMU. These results include the deviation seen between the average value
>>>> reported here versus the high and/or low value. I observed that during the
>>>> test memory usage for the first three tests never dropped whereas with the
>>>> patches fully enabled the VM would drop to using only a few GB of the
>>>> host's memory when switching from memhog to page fault tests.
>>>>
>>>> Any of the overhead visible with this patch set enabled seems due to page
>>>> faults caused by accessing the reported pages and the host zeroing the page
>>>> before giving it back to the guest. This overhead is much more visible when
>>>> using THP than with standard 4K pages. In addition page shuffling seemed to
>>>> increase the amount of faults generated due to an increase in memory churn.
>>>> The overhead is reduced when using MADV_FREE as we can avoid the extra
>>>> zeroing of the pages when they are reintroduced to the host, as can be seen
>>>> when the RFC is applied with shuffling enabled.
>>>>
>>>> The overall guest size is kept fairly small to only a few GB while the test
>>>> is running. If the host memory were oversubscribed this patch set should
>>>> result in a performance improvement as swapping memory in the host can be
>>>> avoided.
>>>
>>>
>>> I really like the approach overall. Voluntarily propagating free memory
>>> from a guest to the host has been a sore point ever since KVM was
>>> around. This solution looks like a very elegant way to do so.
>>>
>>> The big piece I'm missing is the page cache. Linux will by default try
>>> to keep the free list as small as it can in favor of page cache, so most
>>> of the benefit of this patch set will be void in real world scenarios.
>>
>> One approach is to move (parts of) the page cache from the guest to the
>> hypervisor - e.g., using emulated NVDIMM or virtio-pmem.
> 
> Whether you can do that depends heavily on your virtualization 
> environment. On a host with single tenant VMs, that's definitely 
> feasible. In a Kubernetes environment, it might also be feasible.

I would be interesting in which environments this is an actual problem
that can't be solved in the hypervisor (e.g., see below).

> 
> But when you have VMs that assume that the host is interfering with them 
> as little as possible, it becomes harder:

... then you don't want free page reporting or even ballooning I suppose :)

> 
> How do you ensure fairness across different VMs' page cache that is 
> munged into a single big host one?

Interesting question. I would assume that this problem (e.g.,
partitioning the page cache/priorities/etc..) either

a) Already has been solved in the kernel for different processes/process
groups/containers. So for VMs as well.
b) Still is an open problem to solve for all these units.

But: Not a cgroup/pagecache expert

> 
> Do you even have host page cache or are you using SR-IOV / mdev for 
> storage for performance reasons?

E.g., with virtio-mem, a file-backed file will be mmaped into your VM
just like a NVDIMM. So there will be host page cache. But I am -
unfortunately - not an expert on that matter as well (and not sure if it
answers your question) :)

> 
> 
> The puzzle is still incomplete, even with NVDIMM exposure to the guest 
> as an option unfortunately :).

I think it is worth to note that free page reporting won't apply to all
environments either way. There is a steady overhead to do the reporting
and reporting only happens on >= 2MB chunks. There are setups where
virtio-balloon might still be desirable.

(e.g., fragmented guest, no runtime overhead, ...)

All this fiddeling with the guest page cache feels wrong to me ... but
there seem to be interesting approaches being discussed.

So I do agree that the puzzle is still incomplete for some use cases /
environments.
David Hildenbrand Jan. 24, 2020, 4:20 p.m. UTC | #15
On 24.01.20 14:25, David Hildenbrand wrote:
> On 23.01.20 15:52, Alexander Graf wrote:
>>
>>
>> On 23.01.20 15:05, David Hildenbrand wrote:
>>> On 23.01.20 11:20, Alexander Graf wrote:
>>>> Hi Alex,
>>>>
>>>> On 22.01.20 18:43, 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. In each pass at
>>>>> least one sixteenth of each free list will be reported. By doing this we
>>>>> avoid racing against other threads that may be causing a high amount of
>>>>> memory churn.
>>>>>
>>>>> The lowest page order currently scanned when reporting pages is
>>>>> pageblock_order so that this feature will not interfere with the use of
>>>>> Transparent Huge Pages in the case of virtualization.
>>>>>
>>>>> Currently this is only in use by virtio-balloon however there is the hope
>>>>> that at some point in the future other hypervisors might be able to make
>>>>> use of it. In the virtio-balloon/QEMU implementation the hypervisor is
>>>>> currently using MADV_DONTNEED to indicate to the host kernel that the page
>>>>> is currently free. It will be zeroed and faulted back into the guest the
>>>>> next time the page is accessed.
>>>>>
>>>>> To track if a page is reported or not the Uptodate flag was repurposed and
>>>>> used as a Reported flag for Buddy pages. We walk though the free list
>>>>> isolating pages and adding them to the scatterlist until we either
>>>>> encounter the end of the list, 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 features such as CPU turbo
>>>>> disabled in the BIOS.
>>>>>
>>>>> Test                   page_fault1 (THP)    page_fault2
>>>>> Name            tasks  Process Iter  STDEV  Process Iter  STDEV
>>>>> Baseline            1    1012402.50  0.14%     361855.25  0.81%
>>>>>                      16    8827457.25  0.09%    3282347.00  0.34%
>>>>>
>>>>> Patches Applied     1    1007897.00  0.23%     361887.00  0.26%
>>>>>                      16    8784741.75  0.39%    3240669.25  0.48%
>>>>>
>>>>> Patches Enabled     1    1010227.50  0.39%     359749.25  0.56%
>>>>>                      16    8756219.00  0.24%    3226608.75  0.97%
>>>>>
>>>>> Patches Enabled     1    1050982.00  4.26%     357966.25  0.14%
>>>>>    page shuffle      16    8672601.25  0.49%    3223177.75  0.40%
>>>>>
>>>>> Patches enabled     1    1003238.00  0.22%     360211.00  0.22%
>>>>>    shuffle w/ RFC    16    8767010.50  0.32%    3199874.00  0.71%
>>>>>
>>>>> The results above are for a baseline with a linux-next-20191219 kernel,
>>>>> that kernel with this patch set applied but page reporting disabled in
>>>>> virtio-balloon, the patches applied and page reporting fully enabled, the
>>>>> patches enabled with page shuffling enabled, and the patches applied with
>>>>> page shuffling enabled and an RFC patch that makes used of MADV_FREE in
>>>>> QEMU. These results include the deviation seen between the average value
>>>>> reported here versus the high and/or low value. I observed that during the
>>>>> test memory usage for the first three tests never dropped whereas with the
>>>>> patches fully enabled the VM would drop to using only a few GB of the
>>>>> host's memory when switching from memhog to page fault tests.
>>>>>
>>>>> Any of the overhead visible with this patch set enabled seems due to page
>>>>> faults caused by accessing the reported pages and the host zeroing the page
>>>>> before giving it back to the guest. This overhead is much more visible when
>>>>> using THP than with standard 4K pages. In addition page shuffling seemed to
>>>>> increase the amount of faults generated due to an increase in memory churn.
>>>>> The overhead is reduced when using MADV_FREE as we can avoid the extra
>>>>> zeroing of the pages when they are reintroduced to the host, as can be seen
>>>>> when the RFC is applied with shuffling enabled.
>>>>>
>>>>> The overall guest size is kept fairly small to only a few GB while the test
>>>>> is running. If the host memory were oversubscribed this patch set should
>>>>> result in a performance improvement as swapping memory in the host can be
>>>>> avoided.
>>>>
>>>>
>>>> I really like the approach overall. Voluntarily propagating free memory
>>>> from a guest to the host has been a sore point ever since KVM was
>>>> around. This solution looks like a very elegant way to do so.
>>>>
>>>> The big piece I'm missing is the page cache. Linux will by default try
>>>> to keep the free list as small as it can in favor of page cache, so most
>>>> of the benefit of this patch set will be void in real world scenarios.
>>>
>>> One approach is to move (parts of) the page cache from the guest to the
>>> hypervisor - e.g., using emulated NVDIMM or virtio-pmem.
>>
>> Whether you can do that depends heavily on your virtualization 
>> environment. On a host with single tenant VMs, that's definitely 
>> feasible. In a Kubernetes environment, it might also be feasible.
> 
> I would be interesting in which environments this is an actual problem
> that can't be solved in the hypervisor (e.g., see below).

Okay, as Alex told me offline, (somewhat obvious) environments are where
the hypervisor page cache is not involved (e.g., vfio etc.)
Alexander Graf Jan. 24, 2020, 4:40 p.m. UTC | #16
On 24.01.20 14:23, Hillf Danton wrote:
> 
> On Thu, 23 Jan 2020 11:20:07 +0100 Alexander Graf wrote:
>>
>> The big problem I see is that what I really want from a user's point of
>> view is a tuneable that says "Automatically free clean page cache pages
>> that were not accessed in the last X minutes".
> 
> A diff is made on top of 1a4e58cce84e ("mm: introduce MADV_PAGEOUT") without
> test in any form, assuming it goes in line with the tunable above but without
> "X minutes" taken into account.
> 
> [BTW, please take a look at
> Content-Type: text/plain; charset="utf-8"; format="flowed"
> Content-Transfer-Encoding: base64
Thanks, looks like Exchange doesn't pass 8bit data on, I've changed the 
default to ascii now, please just notify me in private if you see it 
broken again.

> 
> and ensure pure text message.]
> 
> 
> --- a/include/uapi/asm-generic/mman-common.h
> +++ b/include/uapi/asm-generic/mman-common.h
> @@ -69,6 +69,7 @@
>   
>   #define MADV_COLD	20		/* deactivate these pages */
>   #define MADV_PAGEOUT	21		/* reclaim these pages */
> +#define MADV_CCPC	22		/* reclaim cold & clean page cache pages */

This patch adds a new madvise flag. I have a hard time seeing how that 
would help with the "full system expiry" of pages?

The basic point that I tried to make above was that I would ideally like 
to have a coldness cutoff date at which you can be pretty confident that 
page cache data is no longer needed.

To work properly, this needs to be transparent to any normal process on 
the system :).


Alex



Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
Alexander Duyck Feb. 3, 2020, 10:05 p.m. UTC | #17
On Wed, 2020-01-22 at 09:43 -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>

> 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 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
> 
> Changes from v15:
> https://lore.kernel.org/lkml/20191205161928.19548.41654.stgit@localhost.localdomain/
> Rebased on linux-next-20191219
> Split out patches for budget and moving head to last page processed
> Updated budget code to reduce how much memory is reported per pass
> Added logic to also rotate the list if we exit due a page isolation failure
> Added migratetype as argument in __putback_isolated_page
> 
> Changes from v16:
> https://lore.kernel.org/lkml/20200103210509.29237.18426.stgit@localhost.localdomain/
> Rebased on linux-next-20200122
>   Updated patch 2 to to account for removal of pr_info in __isolate_free_page
> Updated patch title for patches 7, 8, and 9 to use prefix mm/page_reporting
> No code changes other than conflict resolution for patch 2

So I thought I would put out a gentle nudge since it has been about 4
weeks since v16 was submitted, a little over a week and a half for v16.1,
and I have yet to get any feedback on the code contained in the patchset.
Codewise nothing has changed from the v16 patchset other than rebasing it
off of the linux-next tree to resolve some merge conflicts that I saw
recently, and discussion around v16.1 was mostly about next steps and how
to deal with the page cache instead of discussing the code itself.

The full patchset can be found at:
https://lore.kernel.org/lkml/20200122173040.6142.39116.stgit@localhost.localdomain/

I believe I still need review feedback for patches 3, 4, 7, 8, and 9.

Thanks.

- Alex
Alexander Duyck Feb. 10, 2020, 7:18 p.m. UTC | #18
On Mon, 2020-02-03 at 14:05 -0800, Alexander Duyck wrote:
> On Wed, 2020-01-22 at 09:43 -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>
> 
> > 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 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
> > 
> > Changes from v15:
> > https://lore.kernel.org/lkml/20191205161928.19548.41654.stgit@localhost.localdomain/
> > Rebased on linux-next-20191219
> > Split out patches for budget and moving head to last page processed
> > Updated budget code to reduce how much memory is reported per pass
> > Added logic to also rotate the list if we exit due a page isolation failure
> > Added migratetype as argument in __putback_isolated_page
> > 
> > Changes from v16:
> > https://lore.kernel.org/lkml/20200103210509.29237.18426.stgit@localhost.localdomain/
> > Rebased on linux-next-20200122
> >   Updated patch 2 to to account for removal of pr_info in __isolate_free_page
> > Updated patch title for patches 7, 8, and 9 to use prefix mm/page_reporting
> > No code changes other than conflict resolution for patch 2
> 
> So I thought I would put out a gentle nudge since it has been about 4
> weeks since v16 was submitted, a little over a week and a half for v16.1,
> and I have yet to get any feedback on the code contained in the patchset.
> Codewise nothing has changed from the v16 patchset other than rebasing it
> off of the linux-next tree to resolve some merge conflicts that I saw
> recently, and discussion around v16.1 was mostly about next steps and how
> to deal with the page cache instead of discussing the code itself.
> 
> The full patchset can be found at:
> https://lore.kernel.org/lkml/20200122173040.6142.39116.stgit@localhost.localdomain/
> 
> I believe I still need review feedback for patches 3, 4, 7, 8, and 9.
> 
> Thanks.
> 
> - Alex

So I had posted this patch set a few days before Linus's merge window
opened. When I posted it the discussion was about what the follow-up on
this patch set will be in terms of putting pressure on the page cache to
force it to shrink. However I didn't get any review comments on the code
itself.

My last understanding on this patch set is that I am waiting on patch
feedback from Mel Gorman as he had the remaining requests that led to most
of the changes in v15 and v16. I believe I have addressed them, but I
don't believe he has had a chance to review them.

I am wondering now if it is still possible to either get it reviewed
and/or applied without reposting, or do I need to repost it since it has
been several weeks since I submitted it? The patch set still applies to
the linux-next tree without any issues.

Thanks.

- Alex
Mel Gorman Feb. 11, 2020, 10:40 a.m. UTC | #19
On Mon, Feb 10, 2020 at 11:18:59AM -0800, Alexander Duyck wrote:
> > So I thought I would put out a gentle nudge since it has been about 4
> > weeks since v16 was submitted, a little over a week and a half for v16.1,
> > and I have yet to get any feedback on the code contained in the patchset.
> > Codewise nothing has changed from the v16 patchset other than rebasing it
> > off of the linux-next tree to resolve some merge conflicts that I saw
> > recently, and discussion around v16.1 was mostly about next steps and how
> > to deal with the page cache instead of discussing the code itself.
> > 
> > The full patchset can be found at:
> > https://lore.kernel.org/lkml/20200122173040.6142.39116.stgit@localhost.localdomain/
> > 
> > I believe I still need review feedback for patches 3, 4, 7, 8, and 9.
> > 
> > Thanks.
> > 
> > - Alex
> 
> So I had posted this patch set a few days before Linus's merge window
> opened. When I posted it the discussion was about what the follow-up on
> this patch set will be in terms of putting pressure on the page cache to
> force it to shrink. However I didn't get any review comments on the code
> itself.
> 
> My last understanding on this patch set is that I am waiting on patch
> feedback from Mel Gorman as he had the remaining requests that led to most
> of the changes in v15 and v16. I believe I have addressed them, but I
> don't believe he has had a chance to review them.
> 
> I am wondering now if it is still possible to either get it reviewed
> and/or applied without reposting, or do I need to repost it since it has
> been several weeks since I submitted it? The patch set still applies to
> the linux-next tree without any issues.
> 

Please repost to take into account that this is confirmed to be working
as expected after the merge window and has not conflicted with anything
else that got merged in the meantime. This fell off my radar because of the
timing when it was posted and the volume of mail I was receiving. I simply
noted a large amount of traffic in response to the series and assumed
others had issues that would get resolved without looking closely. Now
I see that it was all comments on future work instead of the series itself.

Sorry.
Alexander Duyck Feb. 11, 2020, 10:57 p.m. UTC | #20
On Tue, 2020-02-11 at 10:40 +0000, Mel Gorman wrote:
> On Mon, Feb 10, 2020 at 11:18:59AM -0800, Alexander Duyck wrote:
> > > So I thought I would put out a gentle nudge since it has been about 4
> > > weeks since v16 was submitted, a little over a week and a half for v16.1,
> > > and I have yet to get any feedback on the code contained in the patchset.
> > > Codewise nothing has changed from the v16 patchset other than rebasing it
> > > off of the linux-next tree to resolve some merge conflicts that I saw
> > > recently, and discussion around v16.1 was mostly about next steps and how
> > > to deal with the page cache instead of discussing the code itself.
> > > 
> > > The full patchset can be found at:
> > > https://lore.kernel.org/lkml/20200122173040.6142.39116.stgit@localhost.localdomain/
> > > 
> > > I believe I still need review feedback for patches 3, 4, 7, 8, and 9.
> > > 
> > > Thanks.
> > > 
> > > - Alex
> > 
> > So I had posted this patch set a few days before Linus's merge window
> > opened. When I posted it the discussion was about what the follow-up on
> > this patch set will be in terms of putting pressure on the page cache to
> > force it to shrink. However I didn't get any review comments on the code
> > itself.
> > 
> > My last understanding on this patch set is that I am waiting on patch
> > feedback from Mel Gorman as he had the remaining requests that led to most
> > of the changes in v15 and v16. I believe I have addressed them, but I
> > don't believe he has had a chance to review them.
> > 
> > I am wondering now if it is still possible to either get it reviewed
> > and/or applied without reposting, or do I need to repost it since it has
> > been several weeks since I submitted it? The patch set still applies to
> > the linux-next tree without any issues.
> > 
> 
> Please repost to take into account that this is confirmed to be working
> as expected after the merge window and has not conflicted with anything
> else that got merged in the meantime. This fell off my radar because of the
> timing when it was posted and the volume of mail I was receiving. I simply
> noted a large amount of traffic in response to the series and assumed
> others had issues that would get resolved without looking closely. Now
> I see that it was all comments on future work instead of the series itself.
> 
> Sorry.
> 

No problem.

I have reposted as v17. I made a slight tweak to the cover page, rebased
on today's linux-next and QEMU, rebuilt and reran some of the tests to
verify the functionality and performance are still running about the same.

The full patch set with QEMU patches can be found here:
https://lore.kernel.org/lkml/20200211224416.29318.44077.stgit@localhost.localdomain/

Thanks.

- Alex