Re: [virtio-dev] Re: [PATCH v7 5/6] virtio-balloon: Pull page poisoning config out of free page hinting

2019-09-04 Thread Alexander Duyck
On Wed, 2019-09-04 at 15:28 -0400, Michael S. Tsirkin wrote:
> On Wed, Sep 04, 2019 at 08:10:55AM -0700, Alexander Duyck wrote:
> > From: Alexander Duyck 
> > 
> > Currently the page poisoning setting wasn't being enabled unless free page
> > hinting was enabled. However we will need the page poisoning tracking logic
> > as well for unused page reporting. As such pull it out and make it a
> > separate bit of config in the probe function.
> > 
> > In addition we can actually wrap the code in a check for NO_SANITY. If we
> > don't care what is actually in the page we can just default to 0 and leave
> > it there.
> > 
> > Signed-off-by: Alexander Duyck 
> > ---
> >  drivers/virtio/virtio_balloon.c |   19 +--
> >  mm/page_reporting.c |4 
> >  2 files changed, 17 insertions(+), 6 deletions(-)
> > 
> > diff --git a/drivers/virtio/virtio_balloon.c 
> > b/drivers/virtio/virtio_balloon.c
> > index 226fbb995fb0..2c19457ab573 100644
> > --- a/drivers/virtio/virtio_balloon.c
> > +++ b/drivers/virtio/virtio_balloon.c
> > @@ -842,7 +842,6 @@ static int virtio_balloon_register_shrinker(struct 
> > virtio_balloon *vb)
> >  static int virtballoon_probe(struct virtio_device *vdev)
> >  {
> > struct virtio_balloon *vb;
> > -   __u32 poison_val;
> > int err;
> >  
> > if (!vdev->config->get) {
> > @@ -909,11 +908,19 @@ static int virtballoon_probe(struct virtio_device 
> > *vdev)
> >   VIRTIO_BALLOON_CMD_ID_STOP);
> > spin_lock_init(>free_page_list_lock);
> > INIT_LIST_HEAD(>free_page_list);
> > -   if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON)) {
> > -   memset(_val, PAGE_POISON, sizeof(poison_val));
> > -   virtio_cwrite(vb->vdev, struct virtio_balloon_config,
> > - poison_val, _val);
> > -   }
> > +   }
> > +   if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON)) {
> > +   __u32 poison_val = 0;
> > +
> > +#if !defined(CONFIG_PAGE_POISONING_NO_SANITY)
> > +   /*
> > +* Let hypervisor know that we are expecting a specific
> > +* value to be written back in unused pages.
> > +*/
> > +   memset(_val, PAGE_POISON, sizeof(poison_val));
> > +#endif
> > +   virtio_cwrite(vb->vdev, struct virtio_balloon_config,
> > + poison_val, _val);
> > }
> > /*
> >  * We continue to use VIRTIO_BALLOON_F_DEFLATE_ON_OOM to decide if a
> 
> I'm a bit confused by this part. Should we not just clear
> VIRTIO_BALLOON_F_PAGE_POISON completely?
> 
> In my mind the value written should be what guest puts in
> free pages - and possibly what it expects to find there later.

I thought it better to err on the side of more information rather than
less. With this the host knows that page poisoning is enabled, but that 0
is an acceptable value.

> If it doesn't expect anything there then it makes sense
> to clear VIRTIO_BALLOON_F_PAGE_POISON so that host does
> not try to put the poison value there.

That makes sense.

> But I think that it does not make sense to lie to host about the poison
> value - I think that if we do send poison value to
> host it's reasonable for host to expect free pages
> have that value - and even possibly to validate that.
> 
> So I think that the hack belongs in virtballoon_validate,
> near the page_poisoning_enabled check.

Yeah, I will move that for v8. I will just add an IS_ENABLED check to the
!page_poisoning_enabled check in virtballoon_validate and can drop the
#ifdef check.

Thanks.

- Alex


-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org



[virtio-dev] Re: [PATCH v7 6/6] virtio-balloon: Add support for providing unused page reports to host

2019-09-04 Thread Alexander Duyck
On Wed, 2019-09-04 at 15:17 -0400, Michael S. Tsirkin wrote:
> On Wed, Sep 04, 2019 at 08:11:02AM -0700, Alexander Duyck wrote:
> > From: Alexander Duyck 
> > 
> > Add support for the page reporting feature provided by virtio-balloon.
> > Reporting differs from the regular balloon functionality in that is is
> > much less durable than a standard memory balloon. Instead of creating a
> > list of pages that cannot be accessed the pages are only inaccessible
> > while they are being indicated to the virtio interface. Once the
> > interface has acknowledged them they are placed back into their respective
> > free lists and are once again accessible by the guest system.
> > 
> > Signed-off-by: Alexander Duyck 
> > ---
> >  drivers/virtio/Kconfig  |1 +
> >  drivers/virtio/virtio_balloon.c |   65 
> > +++
> >  include/uapi/linux/virtio_balloon.h |1 +
> >  3 files changed, 67 insertions(+)
> > 
> > diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig
> > index 078615cf2afc..4b2dd8259ff5 100644
> > --- a/drivers/virtio/Kconfig
> > +++ b/drivers/virtio/Kconfig
> > @@ -58,6 +58,7 @@ config VIRTIO_BALLOON
> > tristate "Virtio balloon driver"
> > depends on VIRTIO
> > select MEMORY_BALLOON
> > +   select PAGE_REPORTING
> > ---help---
> >  This driver supports increasing and decreasing the amount
> >  of memory within a KVM guest.
> > diff --git a/drivers/virtio/virtio_balloon.c 
> > b/drivers/virtio/virtio_balloon.c
> > index 2c19457ab573..0b400bb382c0 100644
> > --- a/drivers/virtio/virtio_balloon.c
> > +++ b/drivers/virtio/virtio_balloon.c
> > @@ -19,6 +19,7 @@
> >  #include 
> >  #include 
> >  #include 
> > +#include 
> >  
> >  /*
> >   * Balloon device works in 4K page units.  So each page is pointed to by
> > @@ -37,6 +38,9 @@
> >  #define VIRTIO_BALLOON_FREE_PAGE_SIZE \
> > (1 << (VIRTIO_BALLOON_FREE_PAGE_ORDER + PAGE_SHIFT))
> >  
> > +/*  limit on the number of pages that can be on the reporting vq */
> > +#define VIRTIO_BALLOON_VRING_HINTS_MAX 16
> > +
> >  #ifdef CONFIG_BALLOON_COMPACTION
> >  static struct vfsmount *balloon_mnt;
> >  #endif
> > @@ -46,6 +50,7 @@ enum virtio_balloon_vq {
> > VIRTIO_BALLOON_VQ_DEFLATE,
> > VIRTIO_BALLOON_VQ_STATS,
> > VIRTIO_BALLOON_VQ_FREE_PAGE,
> > +   VIRTIO_BALLOON_VQ_REPORTING,
> > VIRTIO_BALLOON_VQ_MAX
> >  };
> >  
> > @@ -113,6 +118,10 @@ struct virtio_balloon {
> >  
> > /* To register a shrinker to shrink memory upon memory pressure */
> > struct shrinker shrinker;
> > +
> > +   /* Unused page reporting device */
> > +   struct virtqueue *reporting_vq;
> > +   struct page_reporting_dev_info ph_dev_info;
> >  };
> >  
> >  static struct virtio_device_id id_table[] = {
> > @@ -152,6 +161,32 @@ static void tell_host(struct virtio_balloon *vb, 
> > struct virtqueue *vq)
> >  
> >  }
> >  
> > +void virtballoon_unused_page_report(struct page_reporting_dev_info 
> > *ph_dev_info,
> > +   unsigned int nents)
> > +{
> > +   struct virtio_balloon *vb =
> > +   container_of(ph_dev_info, struct virtio_balloon, ph_dev_info);
> > +   struct virtqueue *vq = vb->reporting_vq;
> > +   unsigned int unused, err;
> > +
> > +   /* We should always be able to add these buffers to an empty queue. */
> > +   err = virtqueue_add_inbuf(vq, ph_dev_info->sg, nents, vb,
> > + GFP_NOWAIT | __GFP_NOWARN);
> > +
> > +   /*
> > +* In the extremely unlikely case that something has changed and we
> > +* are able to trigger an error we will simply display a warning
> > +* and exit without actually processing the pages.
> > +*/
> > +   if (WARN_ON(err))
> > +   return;
> > +
> > +   virtqueue_kick(vq);
> > +
> > +   /* When host has read buffer, this completes via balloon_ack */
> > +   wait_event(vb->acked, virtqueue_get_buf(vq, ));
> > +}
> > +
> 
> So just to make sure I understand, this always passes a single
> buf to the vq and then waits until that completes, correct?

Correct.

> Thus there are never outstanding bufs on the vq and this
> is why we don't need e.g. any cleanup.

Yes. Basically this will wait until the queue has been processed by the
host before we process any additional pages. We can't have the pages in an
unknown state when we put them back so we have to wait here until we know
they have been fully reported to the host.


-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org



[virtio-dev] Re: [PATCH v7 5/6] virtio-balloon: Pull page poisoning config out of free page hinting

2019-09-04 Thread Michael S. Tsirkin
On Wed, Sep 04, 2019 at 08:10:55AM -0700, Alexander Duyck wrote:
> From: Alexander Duyck 
> 
> Currently the page poisoning setting wasn't being enabled unless free page
> hinting was enabled. However we will need the page poisoning tracking logic
> as well for unused page reporting. As such pull it out and make it a
> separate bit of config in the probe function.
> 
> In addition we can actually wrap the code in a check for NO_SANITY. If we
> don't care what is actually in the page we can just default to 0 and leave
> it there.
> 
> Signed-off-by: Alexander Duyck 
> ---
>  drivers/virtio/virtio_balloon.c |   19 +--
>  mm/page_reporting.c |4 
>  2 files changed, 17 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
> index 226fbb995fb0..2c19457ab573 100644
> --- a/drivers/virtio/virtio_balloon.c
> +++ b/drivers/virtio/virtio_balloon.c
> @@ -842,7 +842,6 @@ static int virtio_balloon_register_shrinker(struct 
> virtio_balloon *vb)
>  static int virtballoon_probe(struct virtio_device *vdev)
>  {
>   struct virtio_balloon *vb;
> - __u32 poison_val;
>   int err;
>  
>   if (!vdev->config->get) {
> @@ -909,11 +908,19 @@ static int virtballoon_probe(struct virtio_device *vdev)
> VIRTIO_BALLOON_CMD_ID_STOP);
>   spin_lock_init(>free_page_list_lock);
>   INIT_LIST_HEAD(>free_page_list);
> - if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON)) {
> - memset(_val, PAGE_POISON, sizeof(poison_val));
> - virtio_cwrite(vb->vdev, struct virtio_balloon_config,
> -   poison_val, _val);
> - }
> + }
> + if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON)) {
> + __u32 poison_val = 0;
> +
> +#if !defined(CONFIG_PAGE_POISONING_NO_SANITY)
> + /*
> +  * Let hypervisor know that we are expecting a specific
> +  * value to be written back in unused pages.
> +  */
> + memset(_val, PAGE_POISON, sizeof(poison_val));
> +#endif
> + virtio_cwrite(vb->vdev, struct virtio_balloon_config,
> +   poison_val, _val);
>   }
>   /*
>* We continue to use VIRTIO_BALLOON_F_DEFLATE_ON_OOM to decide if a

I'm a bit confused by this part. Should we not just clear
VIRTIO_BALLOON_F_PAGE_POISON completely?

In my mind the value written should be what guest puts in
free pages - and possibly what it expects to find there later.

If it doesn't expect anything there then it makes sense
to clear VIRTIO_BALLOON_F_PAGE_POISON so that host does
not try to put the poison value there.
But I think that it does not make sense to lie to host about the poison
value - I think that if we do send poison value to
host it's reasonable for host to expect free pages
have that value - and even possibly to validate that.

So I think that the hack belongs in virtballoon_validate,
near the page_poisoning_enabled check.



> diff --git a/mm/page_reporting.c b/mm/page_reporting.c
> index 5006b08d5eec..35c0fe4c4471 100644
> --- a/mm/page_reporting.c
> +++ b/mm/page_reporting.c
> @@ -299,6 +299,10 @@ int page_reporting_startup(struct 
> page_reporting_dev_info *phdev)
>   struct zone *zone;
>   int err = 0;
>  
> + /* No point in enabling this if it cannot handle any pages */
> + if (!phdev->capacity)
> + return -EINVAL;
> +
>   mutex_lock(_reporting_mutex);
>  
>   /* nothing to do if already in use */

-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org



[virtio-dev] Re: [PATCH v7 0/6] mm / virtio: Provide support for unused page reporting

2019-09-04 Thread Michael S. Tsirkin
On Wed, Sep 04, 2019 at 08:10:24AM -0700, Alexander Duyck wrote:
> This series provides an asynchronous means of reporting to a hypervisor
> that a guest page is no longer in use and can have the data associated
> with it dropped. To do this I have implemented functionality that allows
> for what I am referring to as unused page reporting
> 
> The functionality for this is fairly simple. When enabled it will allocate
> statistics to track the number of reported pages in a given free area.
> When the number of free pages exceeds this value plus a high water value,
> currently 32, it will begin performing page reporting which consists of
> pulling pages off of free list and placing them into a scatter list. The
> scatterlist is then given to the page reporting device and it will perform
> the required action to make the pages "reported", in the case of
> virtio-balloon this results in the pages being madvised as MADV_DONTNEED
> and as such they are forced out of the guest. After this they are placed
> back on the free list, and an additional bit is added if they are not
> merged indicating that they are a reported buddy page instead of a
> standard buddy page. The cycle then repeats with additional non-reported
> pages being pulled until the free areas all consist of reported pages.
> 
> I am leaving a number of things hard-coded such as limiting the lowest
> order processed to PAGEBLOCK_ORDER, and have left it up to the guest to
> determine what the limit is on how many pages it wants to allocate to
> process the hints. The upper limit for this is based on the size of the
> queue used to store the scattergather list.
> 
> My primary testing has just been to verify the memory is being freed after
> allocation by running memhog 40g on a 40g guest and watching the total
> free memory via /proc/meminfo on the host. With this I have verified most
> of the memory is freed after each iteration. As far as performance I have
> been mainly focusing on the will-it-scale/page_fault1 test running with
> 16 vcpus. I have modified it to use Transparent Huge Pages. With this I
> see almost no difference, -0.08%, with the patches applied and the feature
> disabled. I see a regression of -0.86% with the feature enabled, but the
> madvise disabled in the hypervisor due to a device being assigned. With
> the feature fully enabled I see a regression of -3.27% versus the baseline
> without these patches applied. In my testing I found that most of the
> overhead was due to the page zeroing that comes as a result of the pages
> having to be faulted back into the guest.
> 
> One side effect of these patches is that the guest becomes much more
> resilient in terms of NUMA locality. With the pages being freed and then
> reallocated when used it allows for the pages to be much closer to the
> active thread, and as a result there can be situations where this patch
> set will out-perform the stock kernel when the guest memory is not local
> to the guest vCPUs. To avoid that in my testing I set the affinity of all
> the vCPUs and QEMU instance to the same node.


So the virtio side looks good to me. But we need mm faulks to ack the mm
changes. And maybe merge all of this ...


> Changes from the RFC:
> https://lore.kernel.org/lkml/20190530215223.13974.22445.stgit@localhost.localdomain/
> Moved aeration requested flag out of aerator and into zone->flags.
> Moved boundary out of free_area and into local variables for aeration.
> Moved aeration cycle out of interrupt and into workqueue.
> Left nr_free as total pages instead of splitting it between raw and aerated.
> Combined size and physical address values in virtio ring into one 64b value.
> 
> Changes from v1:
> https://lore.kernel.org/lkml/20190619222922.1231.27432.stgit@localhost.localdomain/
> Dropped "waste page treatment" in favor of "page hinting"
> Renamed files and functions from "aeration" to "page_hinting"
> Moved from page->lru list to scatterlist
> Replaced wait on refcnt in shutdown with RCU and cancel_delayed_work_sync
> Virtio now uses scatterlist directly instead of intermediate array
> Moved stats out of free_area, now in separate area and pointed to from zone
> Merged patch 5 into patch 4 to improve review-ability
> Updated various code comments throughout
> 
> Changes from v2:
> https://lore.kernel.org/lkml/20190724165158.6685.87228.stgit@localhost.localdomain/
> Dropped "page hinting" in favor of "page reporting"
> Renamed files from "hinting" to "reporting"
> Replaced "Hinted" page type with "Reported" page flag
> Added support for page poisoning while hinting is active
> Add QEMU patch that implements PAGE_POISON feature
> 
> Changes from v3:
> https://lore.kernel.org/lkml/20190801222158.22190.96964.stgit@localhost.localdomain/
> Added mutex lock around page reporting startup and shutdown
> Fixed reference to "page aeration" in patch 2
> Split page reporting function bit out into separate QEMU patch
> Limited capacity of scatterlist to vq size - 1 instead of vq 

[virtio-dev] Re: [PATCH v7 6/6] virtio-balloon: Add support for providing unused page reports to host

2019-09-04 Thread Michael S. Tsirkin
On Wed, Sep 04, 2019 at 08:11:02AM -0700, Alexander Duyck wrote:
> From: Alexander Duyck 
> 
> Add support for the page reporting feature provided by virtio-balloon.
> Reporting differs from the regular balloon functionality in that is is
> much less durable than a standard memory balloon. Instead of creating a
> list of pages that cannot be accessed the pages are only inaccessible
> while they are being indicated to the virtio interface. Once the
> interface has acknowledged them they are placed back into their respective
> free lists and are once again accessible by the guest system.
> 
> Signed-off-by: Alexander Duyck 
> ---
>  drivers/virtio/Kconfig  |1 +
>  drivers/virtio/virtio_balloon.c |   65 
> +++
>  include/uapi/linux/virtio_balloon.h |1 +
>  3 files changed, 67 insertions(+)
> 
> diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig
> index 078615cf2afc..4b2dd8259ff5 100644
> --- a/drivers/virtio/Kconfig
> +++ b/drivers/virtio/Kconfig
> @@ -58,6 +58,7 @@ config VIRTIO_BALLOON
>   tristate "Virtio balloon driver"
>   depends on VIRTIO
>   select MEMORY_BALLOON
> + select PAGE_REPORTING
>   ---help---
>This driver supports increasing and decreasing the amount
>of memory within a KVM guest.
> diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
> index 2c19457ab573..0b400bb382c0 100644
> --- a/drivers/virtio/virtio_balloon.c
> +++ b/drivers/virtio/virtio_balloon.c
> @@ -19,6 +19,7 @@
>  #include 
>  #include 
>  #include 
> +#include 
>  
>  /*
>   * Balloon device works in 4K page units.  So each page is pointed to by
> @@ -37,6 +38,9 @@
>  #define VIRTIO_BALLOON_FREE_PAGE_SIZE \
>   (1 << (VIRTIO_BALLOON_FREE_PAGE_ORDER + PAGE_SHIFT))
>  
> +/*  limit on the number of pages that can be on the reporting vq */
> +#define VIRTIO_BALLOON_VRING_HINTS_MAX   16
> +
>  #ifdef CONFIG_BALLOON_COMPACTION
>  static struct vfsmount *balloon_mnt;
>  #endif
> @@ -46,6 +50,7 @@ enum virtio_balloon_vq {
>   VIRTIO_BALLOON_VQ_DEFLATE,
>   VIRTIO_BALLOON_VQ_STATS,
>   VIRTIO_BALLOON_VQ_FREE_PAGE,
> + VIRTIO_BALLOON_VQ_REPORTING,
>   VIRTIO_BALLOON_VQ_MAX
>  };
>  
> @@ -113,6 +118,10 @@ struct virtio_balloon {
>  
>   /* To register a shrinker to shrink memory upon memory pressure */
>   struct shrinker shrinker;
> +
> + /* Unused page reporting device */
> + struct virtqueue *reporting_vq;
> + struct page_reporting_dev_info ph_dev_info;
>  };
>  
>  static struct virtio_device_id id_table[] = {
> @@ -152,6 +161,32 @@ static void tell_host(struct virtio_balloon *vb, struct 
> virtqueue *vq)
>  
>  }
>  
> +void virtballoon_unused_page_report(struct page_reporting_dev_info 
> *ph_dev_info,
> + unsigned int nents)
> +{
> + struct virtio_balloon *vb =
> + container_of(ph_dev_info, struct virtio_balloon, ph_dev_info);
> + struct virtqueue *vq = vb->reporting_vq;
> + unsigned int unused, err;
> +
> + /* We should always be able to add these buffers to an empty queue. */
> + err = virtqueue_add_inbuf(vq, ph_dev_info->sg, nents, vb,
> +   GFP_NOWAIT | __GFP_NOWARN);
> +
> + /*
> +  * In the extremely unlikely case that something has changed and we
> +  * are able to trigger an error we will simply display a warning
> +  * and exit without actually processing the pages.
> +  */
> + if (WARN_ON(err))
> + return;
> +
> + virtqueue_kick(vq);
> +
> + /* When host has read buffer, this completes via balloon_ack */
> + wait_event(vb->acked, virtqueue_get_buf(vq, ));
> +}
> +

So just to make sure I understand, this always passes a single
buf to the vq and then waits until that completes, correct?
Thus there are never outstanding bufs on the vq and this
is why we don't need e.g. any cleanup.



>  static void set_page_pfns(struct virtio_balloon *vb,
> __virtio32 pfns[], struct page *page)
>  {
> @@ -476,6 +511,7 @@ static int init_vqs(struct virtio_balloon *vb)
>   names[VIRTIO_BALLOON_VQ_DEFLATE] = "deflate";
>   names[VIRTIO_BALLOON_VQ_STATS] = NULL;
>   names[VIRTIO_BALLOON_VQ_FREE_PAGE] = NULL;
> + names[VIRTIO_BALLOON_VQ_REPORTING] = NULL;
>  
>   if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) {
>   names[VIRTIO_BALLOON_VQ_STATS] = "stats";
> @@ -487,11 +523,19 @@ static int init_vqs(struct virtio_balloon *vb)
>   callbacks[VIRTIO_BALLOON_VQ_FREE_PAGE] = NULL;
>   }
>  
> + if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) {
> + names[VIRTIO_BALLOON_VQ_REPORTING] = "reporting_vq";
> + callbacks[VIRTIO_BALLOON_VQ_REPORTING] = balloon_ack;
> + }
> +
>   err = vb->vdev->config->find_vqs(vb->vdev, VIRTIO_BALLOON_VQ_MAX,
>vqs, callbacks, names, NULL, 

[virtio-dev] [PATCH v7 QEMU 3/3] virtio-balloon: Provide a interface for unused page reporting

2019-09-04 Thread Alexander Duyck
From: Alexander Duyck 

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

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

Signed-off-by: Alexander Duyck 
---
 hw/virtio/virtio-balloon.c |   46 ++--
 include/hw/virtio/virtio-balloon.h |2 +-
 2 files changed, 45 insertions(+), 3 deletions(-)

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

[virtio-dev] [PATCH v7 QEMU 1/3] virtio-ballon: Implement support for page poison tracking feature

2019-09-04 Thread Alexander Duyck
From: Alexander Duyck 

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

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

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

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


-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org



[virtio-dev] [PATCH v7 QEMU 2/3] virtio-balloon: Add bit to notify guest of unused page reporting

2019-09-04 Thread Alexander Duyck
From: Alexander Duyck 

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

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

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

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


-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org



[virtio-dev] [PATCH v7 5/6] virtio-balloon: Pull page poisoning config out of free page hinting

2019-09-04 Thread Alexander Duyck
From: Alexander Duyck 

Currently the page poisoning setting wasn't being enabled unless free page
hinting was enabled. However we will need the page poisoning tracking logic
as well for unused page reporting. As such pull it out and make it a
separate bit of config in the probe function.

In addition we can actually wrap the code in a check for NO_SANITY. If we
don't care what is actually in the page we can just default to 0 and leave
it there.

Signed-off-by: Alexander Duyck 
---
 drivers/virtio/virtio_balloon.c |   19 +--
 mm/page_reporting.c |4 
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
index 226fbb995fb0..2c19457ab573 100644
--- a/drivers/virtio/virtio_balloon.c
+++ b/drivers/virtio/virtio_balloon.c
@@ -842,7 +842,6 @@ static int virtio_balloon_register_shrinker(struct 
virtio_balloon *vb)
 static int virtballoon_probe(struct virtio_device *vdev)
 {
struct virtio_balloon *vb;
-   __u32 poison_val;
int err;
 
if (!vdev->config->get) {
@@ -909,11 +908,19 @@ static int virtballoon_probe(struct virtio_device *vdev)
  VIRTIO_BALLOON_CMD_ID_STOP);
spin_lock_init(>free_page_list_lock);
INIT_LIST_HEAD(>free_page_list);
-   if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON)) {
-   memset(_val, PAGE_POISON, sizeof(poison_val));
-   virtio_cwrite(vb->vdev, struct virtio_balloon_config,
- poison_val, _val);
-   }
+   }
+   if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON)) {
+   __u32 poison_val = 0;
+
+#if !defined(CONFIG_PAGE_POISONING_NO_SANITY)
+   /*
+* Let hypervisor know that we are expecting a specific
+* value to be written back in unused pages.
+*/
+   memset(_val, PAGE_POISON, sizeof(poison_val));
+#endif
+   virtio_cwrite(vb->vdev, struct virtio_balloon_config,
+ poison_val, _val);
}
/*
 * We continue to use VIRTIO_BALLOON_F_DEFLATE_ON_OOM to decide if a
diff --git a/mm/page_reporting.c b/mm/page_reporting.c
index 5006b08d5eec..35c0fe4c4471 100644
--- a/mm/page_reporting.c
+++ b/mm/page_reporting.c
@@ -299,6 +299,10 @@ int page_reporting_startup(struct page_reporting_dev_info 
*phdev)
struct zone *zone;
int err = 0;
 
+   /* No point in enabling this if it cannot handle any pages */
+   if (!phdev->capacity)
+   return -EINVAL;
+
mutex_lock(_reporting_mutex);
 
/* nothing to do if already in use */


-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org



[virtio-dev] [PATCH v7 4/6] mm: Introduce Reported pages

2019-09-04 Thread Alexander Duyck
From: Alexander Duyck 

In order to pave the way for free page reporting in virtualized
environments we will need a way to get pages out of the free lists and
identify those pages after they have been returned. To accomplish this,
this patch adds the concept of a Reported Buddy, which is essentially
meant to just be the Uptodate flag used in conjunction with the Buddy
page type.

It adds a set of pointers we shall call "boundary" which represents the
upper boundary between the unreported and reported pages. The general idea
is that in order for a page to cross from one side of the boundary to the
other it will need to go through the reporting process. Ultimately a
free_list has been fully processed when the boundary has been moved from
the tail all they way up to occupying the first entry in the list.

Doing this we should be able to make certain that we keep the reported
pages as one contiguous block in each free list. This will allow us to
efficiently manipulate the free lists whenever we need to go in and start
sending reports to the hypervisor that there are new pages that have been
freed and are no longer in use.

An added advantage to this approach is that we should be reducing the
overall memory footprint of the guest as it will be more likely to recycle
warm pages versus trying to allocate the reported pages that were likely
evicted from the guest memory.

Since we will only be reporting one zone at a time we keep the boundary
limited to being defined for just the zone we are currently reporting pages
from. Doing this we can keep the number of additional pointers needed quite
small. To flag that the boundaries are in place we use a single bit
in the zone to indicate that reporting and the boundaries are active.

The determination of when to start reporting is based on the tracking of
the number of free pages in a given area versus the number of reported
pages in that area. We keep track of the number of reported pages per
free_area in a separate zone specific area. We do this to avoid modifying
the free_area structure as this can lead to false sharing for the highest
order with the zone lock which leads to a noticeable performance
degradation.

Signed-off-by: Alexander Duyck 
---
 include/linux/mmzone.h |   52 ++
 include/linux/page-flags.h |   11 +
 include/linux/page_reporting.h |  177 +
 mm/Kconfig |5 +
 mm/Makefile|1 
 mm/memory_hotplug.c|1 
 mm/page_alloc.c|  114 +-
 mm/page_reporting.c|  336 
 8 files changed, 687 insertions(+), 10 deletions(-)
 create mode 100644 include/linux/page_reporting.h
 create mode 100644 mm/page_reporting.c

diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index 2ddf1f1971c0..4b2c44d7e266 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -463,6 +463,14 @@ struct zone {
seqlock_t   span_seqlock;
 #endif
 
+#ifdef CONFIG_PAGE_REPORTING
+   /*
+* Pointer to reported page tracking statistics array. The size of
+* the array is MAX_ORDER - PAGE_REPORTING_MIN_ORDER. NULL when
+* unused page reporting is not present.
+*/
+   unsigned long   *reported_pages;
+#endif
int initialized;
 
/* Write-intensive fields used from the page allocator */
@@ -538,6 +546,14 @@ enum zone_flags {
ZONE_BOOSTED_WATERMARK, /* zone recently boosted watermarks.
 * Cleared when kswapd is woken.
 */
+   ZONE_PAGE_REPORTING_REQUESTED,  /* zone enabled page reporting and has
+* requested flushing the data out of
+* higher order pages.
+*/
+   ZONE_PAGE_REPORTING_ACTIVE, /* zone enabled page reporting and is
+* activly flushing the data out of
+* higher order pages.
+*/
 };
 
 static inline unsigned long zone_managed_pages(struct zone *zone)
@@ -764,6 +780,8 @@ static inline bool pgdat_is_empty(pg_data_t *pgdat)
return !pgdat->node_start_pfn && !pgdat->node_spanned_pages;
 }
 
+#include 
+
 /* Used for pages not on another list */
 static inline void add_to_free_list(struct page *page, struct zone *zone,
unsigned int order, int migratetype)
@@ -778,24 +796,48 @@ static inline void add_to_free_list(struct page *page, 
struct zone *zone,
 static inline void add_to_free_list_tail(struct page *page, struct zone *zone,
 unsigned int order, int migratetype)
 {
-   struct free_area *area = >free_area[order];
+   struct list_head *tail = get_unreported_tail(zone, order, migratetype);
 
-  

[virtio-dev] [PATCH v7 6/6] virtio-balloon: Add support for providing unused page reports to host

2019-09-04 Thread Alexander Duyck
From: Alexander Duyck 

Add support for the page reporting feature provided by virtio-balloon.
Reporting differs from the regular balloon functionality in that is is
much less durable than a standard memory balloon. Instead of creating a
list of pages that cannot be accessed the pages are only inaccessible
while they are being indicated to the virtio interface. Once the
interface has acknowledged them they are placed back into their respective
free lists and are once again accessible by the guest system.

Signed-off-by: Alexander Duyck 
---
 drivers/virtio/Kconfig  |1 +
 drivers/virtio/virtio_balloon.c |   65 +++
 include/uapi/linux/virtio_balloon.h |1 +
 3 files changed, 67 insertions(+)

diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig
index 078615cf2afc..4b2dd8259ff5 100644
--- a/drivers/virtio/Kconfig
+++ b/drivers/virtio/Kconfig
@@ -58,6 +58,7 @@ config VIRTIO_BALLOON
tristate "Virtio balloon driver"
depends on VIRTIO
select MEMORY_BALLOON
+   select PAGE_REPORTING
---help---
 This driver supports increasing and decreasing the amount
 of memory within a KVM guest.
diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
index 2c19457ab573..0b400bb382c0 100644
--- a/drivers/virtio/virtio_balloon.c
+++ b/drivers/virtio/virtio_balloon.c
@@ -19,6 +19,7 @@
 #include 
 #include 
 #include 
+#include 
 
 /*
  * Balloon device works in 4K page units.  So each page is pointed to by
@@ -37,6 +38,9 @@
 #define VIRTIO_BALLOON_FREE_PAGE_SIZE \
(1 << (VIRTIO_BALLOON_FREE_PAGE_ORDER + PAGE_SHIFT))
 
+/*  limit on the number of pages that can be on the reporting vq */
+#define VIRTIO_BALLOON_VRING_HINTS_MAX 16
+
 #ifdef CONFIG_BALLOON_COMPACTION
 static struct vfsmount *balloon_mnt;
 #endif
@@ -46,6 +50,7 @@ enum virtio_balloon_vq {
VIRTIO_BALLOON_VQ_DEFLATE,
VIRTIO_BALLOON_VQ_STATS,
VIRTIO_BALLOON_VQ_FREE_PAGE,
+   VIRTIO_BALLOON_VQ_REPORTING,
VIRTIO_BALLOON_VQ_MAX
 };
 
@@ -113,6 +118,10 @@ struct virtio_balloon {
 
/* To register a shrinker to shrink memory upon memory pressure */
struct shrinker shrinker;
+
+   /* Unused page reporting device */
+   struct virtqueue *reporting_vq;
+   struct page_reporting_dev_info ph_dev_info;
 };
 
 static struct virtio_device_id id_table[] = {
@@ -152,6 +161,32 @@ static void tell_host(struct virtio_balloon *vb, struct 
virtqueue *vq)
 
 }
 
+void virtballoon_unused_page_report(struct page_reporting_dev_info 
*ph_dev_info,
+   unsigned int nents)
+{
+   struct virtio_balloon *vb =
+   container_of(ph_dev_info, struct virtio_balloon, ph_dev_info);
+   struct virtqueue *vq = vb->reporting_vq;
+   unsigned int unused, err;
+
+   /* We should always be able to add these buffers to an empty queue. */
+   err = virtqueue_add_inbuf(vq, ph_dev_info->sg, nents, vb,
+ GFP_NOWAIT | __GFP_NOWARN);
+
+   /*
+* In the extremely unlikely case that something has changed and we
+* are able to trigger an error we will simply display a warning
+* and exit without actually processing the pages.
+*/
+   if (WARN_ON(err))
+   return;
+
+   virtqueue_kick(vq);
+
+   /* When host has read buffer, this completes via balloon_ack */
+   wait_event(vb->acked, virtqueue_get_buf(vq, ));
+}
+
 static void set_page_pfns(struct virtio_balloon *vb,
  __virtio32 pfns[], struct page *page)
 {
@@ -476,6 +511,7 @@ static int init_vqs(struct virtio_balloon *vb)
names[VIRTIO_BALLOON_VQ_DEFLATE] = "deflate";
names[VIRTIO_BALLOON_VQ_STATS] = NULL;
names[VIRTIO_BALLOON_VQ_FREE_PAGE] = NULL;
+   names[VIRTIO_BALLOON_VQ_REPORTING] = NULL;
 
if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) {
names[VIRTIO_BALLOON_VQ_STATS] = "stats";
@@ -487,11 +523,19 @@ static int init_vqs(struct virtio_balloon *vb)
callbacks[VIRTIO_BALLOON_VQ_FREE_PAGE] = NULL;
}
 
+   if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) {
+   names[VIRTIO_BALLOON_VQ_REPORTING] = "reporting_vq";
+   callbacks[VIRTIO_BALLOON_VQ_REPORTING] = balloon_ack;
+   }
+
err = vb->vdev->config->find_vqs(vb->vdev, VIRTIO_BALLOON_VQ_MAX,
 vqs, callbacks, names, NULL, NULL);
if (err)
return err;
 
+   if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
+   vb->reporting_vq = vqs[VIRTIO_BALLOON_VQ_REPORTING];
+
vb->inflate_vq = vqs[VIRTIO_BALLOON_VQ_INFLATE];
vb->deflate_vq = vqs[VIRTIO_BALLOON_VQ_DEFLATE];
if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) {
@@ -931,12 +975,30 @@ static int virtballoon_probe(struct 

[virtio-dev] [PATCH v7 1/6] mm: Adjust shuffle code to allow for future coalescing

2019-09-04 Thread Alexander Duyck
From: Alexander Duyck 

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

While changing out the code I also opted to go for a bit more thread safe
approach to getting the boolean value. This way we can avoid possible cache
line bouncing of the batched entropy between CPUs.

Signed-off-by: Alexander Duyck 
---
 include/linux/mmzone.h |   12 
 mm/page_alloc.c|   70 +++-
 mm/shuffle.c   |   40 ---
 mm/shuffle.h   |   12 
 4 files changed, 75 insertions(+), 59 deletions(-)

diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index bda20282746b..125f300981c6 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -116,18 +116,6 @@ static inline void add_to_free_area_tail(struct page 
*page, struct free_area *ar
area->nr_free++;
 }
 
-#ifdef CONFIG_SHUFFLE_PAGE_ALLOCATOR
-/* Used to preserve page allocation order entropy */
-void add_to_free_area_random(struct page *page, struct free_area *area,
-   int migratetype);
-#else
-static inline void add_to_free_area_random(struct page *page,
-   struct free_area *area, int migratetype)
-{
-   add_to_free_area(page, area, migratetype);
-}
-#endif
-
 /* Used for pages which are on another list */
 static inline void move_to_free_area(struct page *page, struct free_area *area,
 int migratetype)
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index c5d62f1c2851..4e4356ba66c7 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -878,6 +878,36 @@ static inline struct capture_control *task_capc(struct 
zone *zone)
 #endif /* CONFIG_COMPACTION */
 
 /*
+ * If this is not the largest possible page, check if the buddy
+ * of the next-highest order is free. If it is, it's possible
+ * that pages are being freed that will coalesce soon. In case,
+ * that is happening, add the free page to the tail of the list
+ * so it's less likely to be used soon and more likely to be merged
+ * as a higher order page
+ */
+static inline bool
+buddy_merge_likely(unsigned long pfn, unsigned long buddy_pfn,
+  struct page *page, unsigned int order)
+{
+   struct page *higher_page, *higher_buddy;
+   unsigned long combined_pfn;
+
+   if (order >= MAX_ORDER - 2)
+   return false;
+
+   if (!pfn_valid_within(buddy_pfn))
+   return false;
+
+   combined_pfn = buddy_pfn & pfn;
+   higher_page = page + (combined_pfn - pfn);
+   buddy_pfn = __find_buddy_pfn(combined_pfn, order + 1);
+   higher_buddy = higher_page + (buddy_pfn - combined_pfn);
+
+   return pfn_valid_within(buddy_pfn) &&
+  page_is_buddy(higher_page, higher_buddy, order + 1);
+}
+
+/*
  * Freeing function for a buddy system allocator.
  *
  * The concept of a buddy system is to maintain direct-mapped table
@@ -906,11 +936,12 @@ static inline void __free_one_page(struct page *page,
struct zone *zone, unsigned int order,
int migratetype)
 {
-   unsigned long combined_pfn;
+   struct capture_control *capc = task_capc(zone);
unsigned long uninitialized_var(buddy_pfn);
-   struct page *buddy;
+   unsigned long combined_pfn;
+   struct free_area *area;
unsigned int max_order;
-   struct capture_control *capc = task_capc(zone);
+   struct page *buddy;
 
max_order = min_t(unsigned int, MAX_ORDER, pageblock_order + 1);
 
@@ -979,35 +1010,12 @@ static inline void __free_one_page(struct page *page,
 done_merging:
set_page_order(page, order);
 
-   /*
-* If this is not the largest possible page, check if the buddy
-* of the next-highest order is free. If it is, it's possible
-* that pages are being freed that will coalesce soon. In case,
-* that is happening, add the free page to the tail of the list
-* so it's less likely to be used soon and more likely to be merged
-* as a higher order page
-*/
-   if ((order < MAX_ORDER-2) && pfn_valid_within(buddy_pfn)
-   && !is_shuffle_order(order)) {
-   struct page *higher_page, *higher_buddy;
-   combined_pfn = buddy_pfn & pfn;
-   higher_page = page + (combined_pfn - pfn);
-   buddy_pfn = __find_buddy_pfn(combined_pfn, order + 1);
-   higher_buddy = higher_page + (buddy_pfn - combined_pfn);
-   if (pfn_valid_within(buddy_pfn) &&
-   page_is_buddy(higher_page, higher_buddy, order + 1)) {
-   add_to_free_area_tail(page, >free_area[order],
- migratetype);
-   return;
-  

[virtio-dev] [PATCH v7 3/6] mm: Use zone and order instead of free area in free_list manipulators

2019-09-04 Thread Alexander Duyck
From: Alexander Duyck 

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

In order to be able to reference the zone we need to move the declaration
of the functions down so that we have the zone defined before we define the
list manipulation functions.

Reviewed-by: Dan Williams 
Signed-off-by: Alexander Duyck 
---
 include/linux/mmzone.h |   70 ++--
 mm/page_alloc.c|   30 -
 2 files changed, 49 insertions(+), 51 deletions(-)

diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index 125f300981c6..2ddf1f1971c0 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -100,29 +100,6 @@ struct free_area {
unsigned long   nr_free;
 };
 
-/* Used for pages not on another list */
-static inline void add_to_free_area(struct page *page, struct free_area *area,
-int migratetype)
-{
-   list_add(>lru, >free_list[migratetype]);
-   area->nr_free++;
-}
-
-/* Used for pages not on another list */
-static inline void add_to_free_area_tail(struct page *page, struct free_area 
*area,
- int migratetype)
-{
-   list_add_tail(>lru, >free_list[migratetype]);
-   area->nr_free++;
-}
-
-/* Used for pages which are on another list */
-static inline void move_to_free_area(struct page *page, struct free_area *area,
-int migratetype)
-{
-   list_move(>lru, >free_list[migratetype]);
-}
-
 static inline struct page *get_page_from_free_area(struct free_area *area,
int migratetype)
 {
@@ -130,15 +107,6 @@ static inline struct page *get_page_from_free_area(struct 
free_area *area,
struct page, lru);
 }
 
-static inline void del_page_from_free_area(struct page *page,
-   struct free_area *area)
-{
-   list_del(>lru);
-   __ClearPageBuddy(page);
-   set_page_private(page, 0);
-   area->nr_free--;
-}
-
 static inline bool free_area_empty(struct free_area *area, int migratetype)
 {
return list_empty(>free_list[migratetype]);
@@ -796,6 +764,44 @@ static inline bool pgdat_is_empty(pg_data_t *pgdat)
return !pgdat->node_start_pfn && !pgdat->node_spanned_pages;
 }
 
+/* Used for pages not on another list */
+static inline void add_to_free_list(struct page *page, struct zone *zone,
+   unsigned int order, int migratetype)
+{
+   struct free_area *area = >free_area[order];
+
+   list_add(>lru, >free_list[migratetype]);
+   area->nr_free++;
+}
+
+/* Used for pages not on another list */
+static inline void add_to_free_list_tail(struct page *page, struct zone *zone,
+unsigned int order, int migratetype)
+{
+   struct free_area *area = >free_area[order];
+
+   list_add_tail(>lru, >free_list[migratetype]);
+   area->nr_free++;
+}
+
+/* Used for pages which are on another list */
+static inline void move_to_free_list(struct page *page, struct zone *zone,
+unsigned int order, int migratetype)
+{
+   struct free_area *area = >free_area[order];
+
+   list_move(>lru, >free_list[migratetype]);
+}
+
+static inline void del_page_from_free_list(struct page *page, struct zone 
*zone,
+  unsigned int order)
+{
+   list_del(>lru);
+   __ClearPageBuddy(page);
+   set_page_private(page, 0);
+   zone->free_area[order].nr_free--;
+}
+
 #include 
 
 void build_all_zonelists(pg_data_t *pgdat);
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index a791f2baeeeb..f85dc1561b85 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -921,7 +921,6 @@ static inline void __free_one_page(struct page *page,
struct capture_control *capc = task_capc(zone);
unsigned long uninitialized_var(buddy_pfn);
unsigned long combined_pfn;
-   struct free_area *area;
unsigned int max_order;
struct page *buddy;
 
@@ -958,7 +957,7 @@ static inline void __free_one_page(struct page *page,
if (page_is_guard(buddy))
clear_page_guard(zone, buddy, order, migratetype);
else
-   del_page_from_free_area(buddy, >free_area[order]);
+   del_page_from_free_list(buddy, zone, order);
combined_pfn = buddy_pfn & pfn;
page = page + (combined_pfn - pfn);
pfn = combined_pfn;
@@ -992,12 +991,11 @@ static inline void __free_one_page(struct page *page,
 done_merging:
set_page_order(page, order);
 
- 

[virtio-dev] [PATCH v7 2/6] mm: Move set/get_pcppage_migratetype to mmzone.h

2019-09-04 Thread Alexander Duyck
From: Alexander Duyck 

In order to support page reporting it will be necessary to store and
retrieve the migratetype of a page. To enable that I am moving the set and
get operations for pcppage_migratetype into the mm/internal.h header so
that they can be used outside of the page_alloc.c file.

Signed-off-by: Alexander Duyck 
---
 mm/internal.h   |   18 ++
 mm/page_alloc.c |   18 --
 2 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/mm/internal.h b/mm/internal.h
index 0d5f720c75ab..e4a1a57bbd40 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -549,6 +549,24 @@ static inline bool is_migrate_highatomic_page(struct page 
*page)
return get_pageblock_migratetype(page) == MIGRATE_HIGHATOMIC;
 }
 
+/*
+ * A cached value of the page's pageblock's migratetype, used when the page is
+ * put on a pcplist. Used to avoid the pageblock migratetype lookup when
+ * freeing from pcplists in most cases, at the cost of possibly becoming stale.
+ * Also the migratetype set in the page does not necessarily match the pcplist
+ * index, e.g. page might have MIGRATE_CMA set but be on a pcplist with any
+ * other index - this ensures that it will be put on the correct CMA freelist.
+ */
+static inline int get_pcppage_migratetype(struct page *page)
+{
+   return page->index;
+}
+
+static inline void set_pcppage_migratetype(struct page *page, int migratetype)
+{
+   page->index = migratetype;
+}
+
 void setup_zone_pageset(struct zone *zone);
 extern struct page *alloc_new_node_page(struct page *page, unsigned long node);
 #endif /* __MM_INTERNAL_H */
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 4e4356ba66c7..a791f2baeeeb 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -185,24 +185,6 @@ static int __init early_init_on_free(char *buf)
 }
 early_param("init_on_free", early_init_on_free);
 
-/*
- * A cached value of the page's pageblock's migratetype, used when the page is
- * put on a pcplist. Used to avoid the pageblock migratetype lookup when
- * freeing from pcplists in most cases, at the cost of possibly becoming stale.
- * Also the migratetype set in the page does not necessarily match the pcplist
- * index, e.g. page might have MIGRATE_CMA set but be on a pcplist with any
- * other index - this ensures that it will be put on the correct CMA freelist.
- */
-static inline int get_pcppage_migratetype(struct page *page)
-{
-   return page->index;
-}
-
-static inline void set_pcppage_migratetype(struct page *page, int migratetype)
-{
-   page->index = migratetype;
-}
-
 #ifdef CONFIG_PM_SLEEP
 /*
  * The following functions are used by the suspend/hibernate code to 
temporarily


-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org



[virtio-dev] [PATCH v7 0/6] mm / virtio: Provide support for unused page reporting

2019-09-04 Thread Alexander Duyck
This series provides an asynchronous means of reporting to a hypervisor
that a guest page is no longer in use and can have the data associated
with it dropped. To do this I have implemented functionality that allows
for what I am referring to as unused page reporting

The functionality for this is fairly simple. When enabled it will allocate
statistics to track the number of reported pages in a given free area.
When the number of free pages exceeds this value plus a high water value,
currently 32, it will begin performing page reporting which consists of
pulling pages off of free list and placing them into a scatter list. The
scatterlist is then given to the page reporting device and it will perform
the required action to make the pages "reported", in the case of
virtio-balloon this results in the pages being madvised as MADV_DONTNEED
and as such they are forced out of the guest. After this they are placed
back on the free list, and an additional bit is added if they are not
merged indicating that they are a reported buddy page instead of a
standard buddy page. The cycle then repeats with additional non-reported
pages being pulled until the free areas all consist of reported pages.

I am leaving a number of things hard-coded such as limiting the lowest
order processed to PAGEBLOCK_ORDER, and have left it up to the guest to
determine what the limit is on how many pages it wants to allocate to
process the hints. The upper limit for this is based on the size of the
queue used to store the scattergather list.

My primary testing has just been to verify the memory is being freed after
allocation by running memhog 40g on a 40g guest and watching the total
free memory via /proc/meminfo on the host. With this I have verified most
of the memory is freed after each iteration. As far as performance I have
been mainly focusing on the will-it-scale/page_fault1 test running with
16 vcpus. I have modified it to use Transparent Huge Pages. With this I
see almost no difference, -0.08%, with the patches applied and the feature
disabled. I see a regression of -0.86% with the feature enabled, but the
madvise disabled in the hypervisor due to a device being assigned. With
the feature fully enabled I see a regression of -3.27% versus the baseline
without these patches applied. In my testing I found that most of the
overhead was due to the page zeroing that comes as a result of the pages
having to be faulted back into the guest.

One side effect of these patches is that the guest becomes much more
resilient in terms of NUMA locality. With the pages being freed and then
reallocated when used it allows for the pages to be much closer to the
active thread, and as a result there can be situations where this patch
set will out-perform the stock kernel when the guest memory is not local
to the guest vCPUs. To avoid that in my testing I set the affinity of all
the vCPUs and QEMU instance to the same node.

Changes from the RFC:
https://lore.kernel.org/lkml/20190530215223.13974.22445.stgit@localhost.localdomain/
Moved aeration requested flag out of aerator and into zone->flags.
Moved boundary out of free_area and into local variables for aeration.
Moved aeration cycle out of interrupt and into workqueue.
Left nr_free as total pages instead of splitting it between raw and aerated.
Combined size and physical address values in virtio ring into one 64b value.

Changes from v1:
https://lore.kernel.org/lkml/20190619222922.1231.27432.stgit@localhost.localdomain/
Dropped "waste page treatment" in favor of "page hinting"
Renamed files and functions from "aeration" to "page_hinting"
Moved from page->lru list to scatterlist
Replaced wait on refcnt in shutdown with RCU and cancel_delayed_work_sync
Virtio now uses scatterlist directly instead of intermediate array
Moved stats out of free_area, now in separate area and pointed to from zone
Merged patch 5 into patch 4 to improve review-ability
Updated various code comments throughout

Changes from v2:
https://lore.kernel.org/lkml/20190724165158.6685.87228.stgit@localhost.localdomain/
Dropped "page hinting" in favor of "page reporting"
Renamed files from "hinting" to "reporting"
Replaced "Hinted" page type with "Reported" page flag
Added support for page poisoning while hinting is active
Add QEMU patch that implements PAGE_POISON feature

Changes from v3:
https://lore.kernel.org/lkml/20190801222158.22190.96964.stgit@localhost.localdomain/
Added mutex lock around page reporting startup and shutdown
Fixed reference to "page aeration" in patch 2
Split page reporting function bit out into separate QEMU patch
Limited capacity of scatterlist to vq size - 1 instead of vq size
Added exception handling for case of virtio descriptor allocation failure

Changes from v4:
https://lore.kernel.org/lkml/20190807224037.6891.53512.stgit@localhost.localdomain/
Replaced spin_(un)lock with spin_(un)lock_irq in page_reporting_cycle()
Dropped if/continue for ternary operator in page_reporting_process()
Added checks for 

[virtio-dev] Re: [PATCH v5 6/6] virtio-balloon: Add support for providing unused page reports to host

2019-09-04 Thread Alexander Duyck
On Wed, Sep 4, 2019 at 3:44 AM Michael S. Tsirkin  wrote:
>
> On Tue, Sep 03, 2019 at 07:13:32AM -0700, Alexander Duyck wrote:
> > On Tue, Sep 3, 2019 at 12:32 AM Michael S. Tsirkin  wrote:
> > >
> > > On Mon, Aug 12, 2019 at 02:33:56PM -0700, Alexander Duyck wrote:
> > > > From: Alexander Duyck 
> > > >
> > > > Add support for the page reporting feature provided by virtio-balloon.
> > > > Reporting differs from the regular balloon functionality in that is is
> > > > much less durable than a standard memory balloon. Instead of creating a
> > > > list of pages that cannot be accessed the pages are only inaccessible
> > > > while they are being indicated to the virtio interface. Once the
> > > > interface has acknowledged them they are placed back into their 
> > > > respective
> > > > free lists and are once again accessible by the guest system.
> > > >
> > > > Signed-off-by: Alexander Duyck 
> > > > ---
> > > >  drivers/virtio/Kconfig  |1 +
> > > >  drivers/virtio/virtio_balloon.c |   65 
> > > > +++
> > > >  include/uapi/linux/virtio_balloon.h |1 +
> > > >  3 files changed, 67 insertions(+)
> > > >
> > > > diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig
> > > > index 078615cf2afc..4b2dd8259ff5 100644
> > > > --- a/drivers/virtio/Kconfig
> > > > +++ b/drivers/virtio/Kconfig
> > > > @@ -58,6 +58,7 @@ config VIRTIO_BALLOON
> > > >   tristate "Virtio balloon driver"
> > > >   depends on VIRTIO
> > > >   select MEMORY_BALLOON
> > > > + select PAGE_REPORTING
> > > >   ---help---
> > > >This driver supports increasing and decreasing the amount
> > > >of memory within a KVM guest.
> > > > diff --git a/drivers/virtio/virtio_balloon.c 
> > > > b/drivers/virtio/virtio_balloon.c
> > > > index 2c19457ab573..52f9eeda1877 100644
> > > > --- a/drivers/virtio/virtio_balloon.c
> > > > +++ b/drivers/virtio/virtio_balloon.c
> > > > @@ -19,6 +19,7 @@
> > > >  #include 
> > > >  #include 
> > > >  #include 
> > > > +#include 
> > > >
> > > >  /*
> > > >   * Balloon device works in 4K page units.  So each page is pointed to 
> > > > by
> > > > @@ -37,6 +38,9 @@
> > > >  #define VIRTIO_BALLOON_FREE_PAGE_SIZE \
> > > >   (1 << (VIRTIO_BALLOON_FREE_PAGE_ORDER + PAGE_SHIFT))
> > > >
> > > > +/*  limit on the number of pages that can be on the reporting vq */
> > > > +#define VIRTIO_BALLOON_VRING_HINTS_MAX   16
> > > > +
> > > >  #ifdef CONFIG_BALLOON_COMPACTION
> > > >  static struct vfsmount *balloon_mnt;
> > > >  #endif
> > > > @@ -46,6 +50,7 @@ enum virtio_balloon_vq {
> > > >   VIRTIO_BALLOON_VQ_DEFLATE,
> > > >   VIRTIO_BALLOON_VQ_STATS,
> > > >   VIRTIO_BALLOON_VQ_FREE_PAGE,
> > > > + VIRTIO_BALLOON_VQ_REPORTING,
> > > >   VIRTIO_BALLOON_VQ_MAX
> > > >  };
> > > >
> > > > @@ -113,6 +118,10 @@ struct virtio_balloon {
> > > >
> > > >   /* To register a shrinker to shrink memory upon memory pressure */
> > > >   struct shrinker shrinker;
> > > > +
> > > > + /* Unused page reporting device */
> > > > + struct virtqueue *reporting_vq;
> > > > + struct page_reporting_dev_info ph_dev_info;
> > > >  };
> > > >
> > > >  static struct virtio_device_id id_table[] = {
> > > > @@ -152,6 +161,32 @@ static void tell_host(struct virtio_balloon *vb, 
> > > > struct virtqueue *vq)
> > > >
> > > >  }
> > > >
> > > > +void virtballoon_unused_page_report(struct page_reporting_dev_info 
> > > > *ph_dev_info,
> > > > + unsigned int nents)
> > > > +{
> > > > + struct virtio_balloon *vb =
> > > > + container_of(ph_dev_info, struct virtio_balloon, 
> > > > ph_dev_info);
> > > > + struct virtqueue *vq = vb->reporting_vq;
> > > > + unsigned int unused, err;
> > > > +
> > > > + /* We should always be able to add these buffers to an empty 
> > > > queue. */
> > > > + err = virtqueue_add_inbuf(vq, ph_dev_info->sg, nents, vb,
> > > > +   GFP_NOWAIT | __GFP_NOWARN);
> > > > +
> > > > + /*
> > > > +  * In the extremely unlikely case that something has changed and 
> > > > we
> > > > +  * are able to trigger an error we will simply display a warning
> > > > +  * and exit without actually processing the pages.
> > > > +  */
> > > > + if (WARN_ON(err))
> > > > + return;
> > > > +
> > > > + virtqueue_kick(vq);
> > > > +
> > > > + /* When host has read buffer, this completes via balloon_ack */
> > > > + wait_event(vb->acked, virtqueue_get_buf(vq, ));
> > > > +}
> > > > +
> > > >  static void set_page_pfns(struct virtio_balloon *vb,
> > > > __virtio32 pfns[], struct page *page)
> > > >  {
> > > > @@ -476,6 +511,7 @@ static int init_vqs(struct virtio_balloon *vb)
> > > >   names[VIRTIO_BALLOON_VQ_DEFLATE] = "deflate";
> > > >   names[VIRTIO_BALLOON_VQ_STATS] = NULL;
> > > >   names[VIRTIO_BALLOON_VQ_FREE_PAGE] = NULL;
> > > > + 

[virtio-dev] Re: [PATCH v5 6/6] virtio-balloon: Add support for providing unused page reports to host

2019-09-04 Thread Michael S. Tsirkin
On Tue, Sep 03, 2019 at 07:13:32AM -0700, Alexander Duyck wrote:
> On Tue, Sep 3, 2019 at 12:32 AM Michael S. Tsirkin  wrote:
> >
> > On Mon, Aug 12, 2019 at 02:33:56PM -0700, Alexander Duyck wrote:
> > > From: Alexander Duyck 
> > >
> > > Add support for the page reporting feature provided by virtio-balloon.
> > > Reporting differs from the regular balloon functionality in that is is
> > > much less durable than a standard memory balloon. Instead of creating a
> > > list of pages that cannot be accessed the pages are only inaccessible
> > > while they are being indicated to the virtio interface. Once the
> > > interface has acknowledged them they are placed back into their respective
> > > free lists and are once again accessible by the guest system.
> > >
> > > Signed-off-by: Alexander Duyck 
> > > ---
> > >  drivers/virtio/Kconfig  |1 +
> > >  drivers/virtio/virtio_balloon.c |   65 
> > > +++
> > >  include/uapi/linux/virtio_balloon.h |1 +
> > >  3 files changed, 67 insertions(+)
> > >
> > > diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig
> > > index 078615cf2afc..4b2dd8259ff5 100644
> > > --- a/drivers/virtio/Kconfig
> > > +++ b/drivers/virtio/Kconfig
> > > @@ -58,6 +58,7 @@ config VIRTIO_BALLOON
> > >   tristate "Virtio balloon driver"
> > >   depends on VIRTIO
> > >   select MEMORY_BALLOON
> > > + select PAGE_REPORTING
> > >   ---help---
> > >This driver supports increasing and decreasing the amount
> > >of memory within a KVM guest.
> > > diff --git a/drivers/virtio/virtio_balloon.c 
> > > b/drivers/virtio/virtio_balloon.c
> > > index 2c19457ab573..52f9eeda1877 100644
> > > --- a/drivers/virtio/virtio_balloon.c
> > > +++ b/drivers/virtio/virtio_balloon.c
> > > @@ -19,6 +19,7 @@
> > >  #include 
> > >  #include 
> > >  #include 
> > > +#include 
> > >
> > >  /*
> > >   * Balloon device works in 4K page units.  So each page is pointed to by
> > > @@ -37,6 +38,9 @@
> > >  #define VIRTIO_BALLOON_FREE_PAGE_SIZE \
> > >   (1 << (VIRTIO_BALLOON_FREE_PAGE_ORDER + PAGE_SHIFT))
> > >
> > > +/*  limit on the number of pages that can be on the reporting vq */
> > > +#define VIRTIO_BALLOON_VRING_HINTS_MAX   16
> > > +
> > >  #ifdef CONFIG_BALLOON_COMPACTION
> > >  static struct vfsmount *balloon_mnt;
> > >  #endif
> > > @@ -46,6 +50,7 @@ enum virtio_balloon_vq {
> > >   VIRTIO_BALLOON_VQ_DEFLATE,
> > >   VIRTIO_BALLOON_VQ_STATS,
> > >   VIRTIO_BALLOON_VQ_FREE_PAGE,
> > > + VIRTIO_BALLOON_VQ_REPORTING,
> > >   VIRTIO_BALLOON_VQ_MAX
> > >  };
> > >
> > > @@ -113,6 +118,10 @@ struct virtio_balloon {
> > >
> > >   /* To register a shrinker to shrink memory upon memory pressure */
> > >   struct shrinker shrinker;
> > > +
> > > + /* Unused page reporting device */
> > > + struct virtqueue *reporting_vq;
> > > + struct page_reporting_dev_info ph_dev_info;
> > >  };
> > >
> > >  static struct virtio_device_id id_table[] = {
> > > @@ -152,6 +161,32 @@ static void tell_host(struct virtio_balloon *vb, 
> > > struct virtqueue *vq)
> > >
> > >  }
> > >
> > > +void virtballoon_unused_page_report(struct page_reporting_dev_info 
> > > *ph_dev_info,
> > > + unsigned int nents)
> > > +{
> > > + struct virtio_balloon *vb =
> > > + container_of(ph_dev_info, struct virtio_balloon, 
> > > ph_dev_info);
> > > + struct virtqueue *vq = vb->reporting_vq;
> > > + unsigned int unused, err;
> > > +
> > > + /* We should always be able to add these buffers to an empty queue. 
> > > */
> > > + err = virtqueue_add_inbuf(vq, ph_dev_info->sg, nents, vb,
> > > +   GFP_NOWAIT | __GFP_NOWARN);
> > > +
> > > + /*
> > > +  * In the extremely unlikely case that something has changed and we
> > > +  * are able to trigger an error we will simply display a warning
> > > +  * and exit without actually processing the pages.
> > > +  */
> > > + if (WARN_ON(err))
> > > + return;
> > > +
> > > + virtqueue_kick(vq);
> > > +
> > > + /* When host has read buffer, this completes via balloon_ack */
> > > + wait_event(vb->acked, virtqueue_get_buf(vq, ));
> > > +}
> > > +
> > >  static void set_page_pfns(struct virtio_balloon *vb,
> > > __virtio32 pfns[], struct page *page)
> > >  {
> > > @@ -476,6 +511,7 @@ static int init_vqs(struct virtio_balloon *vb)
> > >   names[VIRTIO_BALLOON_VQ_DEFLATE] = "deflate";
> > >   names[VIRTIO_BALLOON_VQ_STATS] = NULL;
> > >   names[VIRTIO_BALLOON_VQ_FREE_PAGE] = NULL;
> > > + names[VIRTIO_BALLOON_VQ_REPORTING] = NULL;
> > >
> > >   if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) {
> > >   names[VIRTIO_BALLOON_VQ_STATS] = "stats";
> > > @@ -487,11 +523,19 @@ static int init_vqs(struct virtio_balloon *vb)
> > >   callbacks[VIRTIO_BALLOON_VQ_FREE_PAGE] = NULL;
> > > 

Re: [virtio-dev] Re: [RFC][Patch v12 1/2] mm: page_reporting: core infrastructure

2019-09-04 Thread David Hildenbrand
>>> For some reason, I am not seeing this work as I would have expected
>>> but I don't have solid reasoning to share yet. It could be simply
>>> because I am putting my hook at the wrong place. I will continue
>>> investigating this.
>>>
>>> In any case, I may be over complicating things here, so please let me
>>> if there is a better way to do this.
>> I have already been demonstrating the "better way" I think there is to
>> do this. I will push v7 of it early next week unless there is some
>> other feedback. By putting the bit in the page and controlling what
>> comes into and out of the lists it makes most of this quite a bit
>> easier. The only limitation is you have to modify where things get
>> placed in the lists so you don't create a "vapor lock" that would
>> stall the feed of pages into the reporting engine.
>>
>>> If this overhead is not significant we can probably live with it.
>> You have bigger issues you still have to overcome as I recall. Didn't
>> you still need to sort out hotplu
> 
> For memory hotplug, my impression is that it should
> not be a blocker for taking the first step (in case we do decide to
> go ahead with this approach). Another reason why I am considering
> this as future work is that memory hot(un)plug is still under
> development and requires fixing. (Specifically, issue such as zone
> shrinking which will directly impact the bitmap approach is still
> under discussion).

Memory hotplug is way more reliable than memory unplug - however, but
both are used in production. E.g. the zone shrinking you mention is
something that is not "optimal", but works in most scenarios. So both
features are rather in a "production/fixing" stage than a pure
"development" stage.

However, I do agree that memory hot(un)plug is not something
high-priority for free page hinting in the first shot. If it works out
of the box (Alexander's approach) - great - if not it is just one
additional scenario where free page hinting might not be optimal yet
(like vfio where we can't use it completely right now). After all, most
virtual environment (under KVM) I am aware of don't use DIMM-base memory
hot(un)plug at all.

The important part is that it will not break when memory is added/removed.

> 
>> g and a sparse map with a wide span
>> in a zone? Without those resolved the bitmap approach is still a no-go
>> regardless of performance.
> 
> For sparsity, the memory wastage should not be significant as I
> am tracking pages on the granularity of (MAX_ORDER - 2) and maintaining
> the bitmaps on a per-zone basis (which was not the case earlier).

To handle sparsity one would have to have separate bitmaps for each
section. Roughly 64 bit per 128MB section. Scanning the scattered bitmap
would get more expensive. Of course, one could have a hierarchical
bitmap to speed up the search etc.

But again, I think we should have a look how much of an issue sparse
zones/nodes actually are in virtual machines (KVM). The setups I am
aware of minimize sparsity (e.g., QEMU will place DIMMs consecutively in
memory, only aligning to e.g, 128MB on x86 if required). Usually all
memory in VMs is onlined to the NORMAL zone (except special cases of
course). The only "issue" would be if you start mixing DIMMs of
different nodes when hotplugging memory. The overhead for 1bit per 2MB
could be almost neglectable in most setups.

But I do agree that for the bitmap-based approach there might be more
scenarios where you'd rather not want to use free page hinting and
instead disable it.

> 
> However, if you do consider this as a block I will think about it and try to 
> fix it.
> In the worst case, if I don't find a solution I will add this as a known 
> limitation
> for this approach in my cover.
> 
>> - Alex


-- 

Thanks,

David / dhildenb

-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org