From: Liang Li <liang.z.li@intel.com>
To: mst@redhat.com, viro@zeniv.linux.org.uk,
linux-kernel@vger.kernel.org, quintela@redhat.com,
amit.shah@redhat.com, pbonzini@redhat.com, dgilbert@redhat.com
Cc: linux-mm@kvack.org, kvm@vger.kernel.org, qemu-devel@nongnu.org,
agraf@suse.de, borntraeger@de.ibm.com,
Liang Li <liang.z.li@intel.com>
Subject: [PATCH kernel 2/2] virtio-balloon: extend balloon driver to support the new feature
Date: Tue, 19 Apr 2016 22:34:34 +0800 [thread overview]
Message-ID: <1461076474-3864-3-git-send-email-liang.z.li@intel.com> (raw)
In-Reply-To: <1461076474-3864-1-git-send-email-liang.z.li@intel.com>
Extend the virtio balloon to support the new feature
VIRTIO_BALLOON_F_GET_FREE_PAGES, so that we can use it to send
the free page bitmap from guest to QEMU, the free page bitmap will
be used for live migration optimization.
Signed-off-by: Liang Li <liang.z.li@intel.com>
---
drivers/virtio/virtio_balloon.c | 100 ++++++++++++++++++++++++++++++++++--
include/uapi/linux/virtio_balloon.h | 1 +
2 files changed, 96 insertions(+), 5 deletions(-)
diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
index 7b6d74f..cf17694 100644
--- a/drivers/virtio/virtio_balloon.c
+++ b/drivers/virtio/virtio_balloon.c
@@ -45,9 +45,17 @@ static int oom_pages = OOM_VBALLOON_DEFAULT_PAGES;
module_param(oom_pages, int, S_IRUSR | S_IWUSR);
MODULE_PARM_DESC(oom_pages, "pages to free on OOM");
+extern void get_free_pages(unsigned long *free_page_bitmap,
+ unsigned long len, int drop);
+extern unsigned long get_max_pfn(void);
+
+struct cache_drop_ctrl {
+ u64 ctrl;
+};
+
struct virtio_balloon {
struct virtio_device *vdev;
- struct virtqueue *inflate_vq, *deflate_vq, *stats_vq;
+ struct virtqueue *inflate_vq, *deflate_vq, *stats_vq, *free_pages_vq;
/* The balloon servicing is delegated to a freezable workqueue. */
struct work_struct update_balloon_stats_work;
@@ -77,6 +85,10 @@ struct virtio_balloon {
unsigned int num_pfns;
u32 pfns[VIRTIO_BALLOON_ARRAY_PFNS_MAX];
+ unsigned long *free_pages;
+ unsigned long bmap_len;
+ struct cache_drop_ctrl cache_drop;
+
/* Memory statistics */
struct virtio_balloon_stat stats[VIRTIO_BALLOON_S_NR];
@@ -256,6 +268,64 @@ static void update_balloon_stats(struct virtio_balloon *vb)
pages_to_bytes(available));
}
+static void update_free_pages_stats(struct virtio_balloon *vb)
+{
+ unsigned long bitmap_bytes, max_pfn;
+
+ max_pfn = get_max_pfn();
+ bitmap_bytes = ALIGN(max_pfn, BITS_PER_LONG) / 8;
+
+ if (!vb->free_pages)
+ vb->free_pages = kzalloc(bitmap_bytes, GFP_KERNEL);
+ else {
+ if (bitmap_bytes < vb->bmap_len)
+ memset(vb->free_pages, 0, bitmap_bytes);
+ else {
+ kfree(vb->free_pages);
+ vb->free_pages = kzalloc(bitmap_bytes, GFP_KERNEL);
+ }
+ }
+ if (!vb->free_pages) {
+ vb->bmap_len = 0;
+ return;
+ }
+
+ vb->bmap_len = bitmap_bytes;
+ get_free_pages(vb->free_pages, max_pfn, vb->cache_drop.ctrl);
+}
+
+static void free_pages_handle_rq(struct virtio_balloon *vb)
+{
+ struct virtqueue *vq;
+ struct scatterlist sg[2];
+ unsigned int len;
+ struct cache_drop_ctl *ptr_cache_drop;
+ struct scatterlist sg_in;
+
+ vq = vb->free_pages_vq;
+ ptr_cache_drop = virtqueue_get_buf(vq, &len);
+
+ if (!ptr_cache_drop || len != sizeof(vb->cache_drop))
+ return;
+ update_free_pages_stats(vb);
+ sg_init_table(sg, 2);
+ sg_set_buf(&sg[0], &(vb->bmap_len), sizeof(vb->bmap_len));
+ sg_set_buf(&sg[1], vb->free_pages, vb->bmap_len);
+
+ sg_init_one(&sg_in, &vb->cache_drop, sizeof(vb->cache_drop));
+
+ virtqueue_add_outbuf(vq, &sg[0], 2, vb, GFP_KERNEL);
+ virtqueue_add_inbuf(vq, &sg_in, 1, &vb->cache_drop, GFP_KERNEL);
+ virtqueue_kick(vq);
+}
+
+static void free_pages_rq(struct virtqueue *vq)
+{
+ struct virtio_balloon *vb = vq->vdev->priv;
+
+ free_pages_handle_rq(vb);
+}
+
/*
* While most virtqueues communicate guest-initiated requests to the hypervisor,
* the stats queue operates in reverse. The driver initializes the virtqueue
@@ -392,16 +462,22 @@ static void update_balloon_size_func(struct work_struct *work)
static int init_vqs(struct virtio_balloon *vb)
{
- struct virtqueue *vqs[3];
- vq_callback_t *callbacks[] = { balloon_ack, balloon_ack, stats_request };
- static const char * const names[] = { "inflate", "deflate", "stats" };
+ struct virtqueue *vqs[4];
+ vq_callback_t *callbacks[] = { balloon_ack, balloon_ack,
+ stats_request, free_pages_rq };
+ const char *names[] = { "inflate", "deflate", "stats", "free_pages" };
int err, nvqs;
/*
* We expect two virtqueues: inflate and deflate, and
* optionally stat.
*/
- nvqs = virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ) ? 3 : 2;
+ if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_GET_FREE_PAGES))
+ nvqs = 4;
+ else
+ nvqs = virtio_has_feature(vb->vdev,
+ VIRTIO_BALLOON_F_STATS_VQ) ? 3 : 2;
+
err = vb->vdev->config->find_vqs(vb->vdev, nvqs, vqs, callbacks, names);
if (err)
return err;
@@ -422,6 +498,16 @@ static int init_vqs(struct virtio_balloon *vb)
BUG();
virtqueue_kick(vb->stats_vq);
}
+ if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_GET_FREE_PAGES)) {
+ struct scatterlist sg_in;
+
+ vb->free_pages_vq = vqs[3];
+ sg_init_one(&sg_in, &vb->cache_drop, sizeof(vb->cache_drop));
+ if (virtqueue_add_inbuf(vb->free_pages_vq, &sg_in, 1,
+ &vb->cache_drop, GFP_KERNEL) < 0)
+ BUG();
+ virtqueue_kick(vb->free_pages_vq);
+ }
return 0;
}
@@ -505,6 +591,8 @@ static int virtballoon_probe(struct virtio_device *vdev)
goto out;
}
+ vb->bmap_len = 0;
+ vb->free_pages = NULL;
INIT_WORK(&vb->update_balloon_stats_work, update_balloon_stats_func);
INIT_WORK(&vb->update_balloon_size_work, update_balloon_size_func);
spin_lock_init(&vb->stop_update_lock);
@@ -567,6 +655,7 @@ static void virtballoon_remove(struct virtio_device *vdev)
cancel_work_sync(&vb->update_balloon_stats_work);
remove_common(vb);
+ kfree(vb->free_pages);
kfree(vb);
}
@@ -605,6 +694,7 @@ static unsigned int features[] = {
VIRTIO_BALLOON_F_MUST_TELL_HOST,
VIRTIO_BALLOON_F_STATS_VQ,
VIRTIO_BALLOON_F_DEFLATE_ON_OOM,
+ VIRTIO_BALLOON_F_GET_FREE_PAGES,
};
static struct virtio_driver virtio_balloon_driver = {
diff --git a/include/uapi/linux/virtio_balloon.h b/include/uapi/linux/virtio_balloon.h
index 343d7dd..2b41e4f 100644
--- a/include/uapi/linux/virtio_balloon.h
+++ b/include/uapi/linux/virtio_balloon.h
@@ -34,6 +34,7 @@
#define VIRTIO_BALLOON_F_MUST_TELL_HOST 0 /* Tell before reclaiming pages */
#define VIRTIO_BALLOON_F_STATS_VQ 1 /* Memory Stats virtqueue */
#define VIRTIO_BALLOON_F_DEFLATE_ON_OOM 2 /* Deflate balloon on OOM */
+#define VIRTIO_BALLOON_F_GET_FREE_PAGES 3 /* Get free page bitmap */
/* Size of a PFN in the balloon interface. */
#define VIRTIO_BALLOON_PFN_SHIFT 12
--
1.8.3.1
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
next prev parent reply other threads:[~2016-04-19 14:43 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-04-19 14:34 [PATCH kernel 0/2] speed up live migration by skipping free pages Liang Li
2016-04-19 14:34 ` [PATCH kernel 1/2] mm: add the related functions to build the free page bitmap Liang Li
2016-04-19 14:54 ` Rik van Riel
2016-04-19 15:02 ` Li, Liang Z
2016-04-19 15:26 ` Rik van Riel
2016-04-20 0:57 ` Li, Liang Z
2016-04-19 16:15 ` Michael S. Tsirkin
2016-04-20 1:41 ` Li, Liang Z
2016-04-21 13:48 ` Michael S. Tsirkin
2016-04-22 1:36 ` Li, Liang Z
2016-04-22 9:48 ` Dr. David Alan Gilbert
2016-04-22 13:58 ` Michael S. Tsirkin
2016-04-25 3:11 ` Li, Liang Z
2016-04-25 10:43 ` Michael S. Tsirkin
2016-04-26 3:21 ` Li, Liang Z
2016-04-19 14:56 ` kbuild test robot
2016-04-19 16:01 ` kbuild test robot
2016-04-19 16:14 ` kbuild test robot
2016-04-19 14:34 ` Liang Li [this message]
2016-04-19 16:18 ` [PATCH kernel 2/2] virtio-balloon: extend balloon driver to support the new feature Michael S. Tsirkin
2016-04-25 6:06 ` [PATCH kernel 0/2] speed up live migration by skipping free pages Amit Shah
2016-04-25 11:04 ` Michael S. Tsirkin
2016-04-25 12:08 ` Amit Shah
2016-04-25 12:51 ` Michael S. Tsirkin
2016-04-25 13:17 ` Dr. David Alan Gilbert
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1461076474-3864-3-git-send-email-liang.z.li@intel.com \
--to=liang.z.li@intel.com \
--cc=agraf@suse.de \
--cc=amit.shah@redhat.com \
--cc=borntraeger@de.ibm.com \
--cc=dgilbert@redhat.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mst@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=quintela@redhat.com \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).