public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Dmitry Torokhov <dtor@vmware.com>
To: linux-kernel@vger.kernel.org
Cc: pv-drivers@vmware.com, Alok Kataria <akataria@vmware.com>,
	Saileshkumar Jain <sajain@vmware.com>,
	Rusty Russell <rusty@rustcorp.com.au>
Subject: [PATCH 1/4] virtio-balloon: allow hypervisor refuse locking some pages
Date: Fri, 19 Mar 2010 13:46:47 -0700	[thread overview]
Message-ID: <20100319204647.4418.23072.stgit@localhost.localdomain> (raw)
In-Reply-To: <20100319204628.4418.84674.stgit@localhost.localdomain>

Some hypervisors may refuse our request to lock pages in certain
circumstances. For example, VMware may decide that the target balloon
size decreased since it was last communicated to the guest and thus
current page should not be locked.

Reviewed-by: Alok Kataria <akataria@vmware.com>
Reviewed-by: Shaileshkumar Jain <sajain@vmware.com>
Signed-off-by: Dmitry Torokhov <dtor@vmware.com>
---

 drivers/virtio/virtio_balloon.c |   68 +++++++++++++++++++++++++++++++++++----
 include/linux/virtio_balloon.h  |    8 +++--
 2 files changed, 67 insertions(+), 9 deletions(-)

diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
index 369f2ee..444c929 100644
--- a/drivers/virtio/virtio_balloon.c
+++ b/drivers/virtio/virtio_balloon.c
@@ -25,6 +25,8 @@
 #include <linux/freezer.h>
 #include <linux/delay.h>
 
+#define VIRTIO_BALLOON_CHUNK_SIZE	256
+
 struct virtio_balloon
 {
 	struct virtio_device *vdev;
@@ -46,9 +48,13 @@ struct virtio_balloon
 	unsigned int num_pages;
 	struct list_head pages;
 
+	/* Pages that host refused to lock for one reason or another. */
+	struct list_head refused_pages;
+
 	/* The array of pfns we tell the Host about. */
 	unsigned int num_pfns;
-	u32 pfns[256];
+	u32 pfns[VIRTIO_BALLOON_CHUNK_SIZE];
+	u8 pfns_status[VIRTIO_BALLOON_CHUNK_SIZE];
 
 	/* Memory statistics */
 	int need_stats_update;
@@ -81,14 +87,21 @@ static void balloon_ack(struct virtqueue *vq)
 
 static void tell_host(struct virtio_balloon *vb, struct virtqueue *vq)
 {
-	struct scatterlist sg;
+	struct scatterlist sg[2];
+	int n_out = 0;
 
-	sg_init_one(&sg, vb->pfns, sizeof(vb->pfns[0]) * vb->num_pfns);
+	sg_init_table(sg, 2);
+	sg_set_buf(&sg[0], vb->pfns, sizeof(vb->pfns[0]) * vb->num_pfns);
+	sg_set_buf(&sg[1], vb->pfns_status,
+			sizeof(vb->pfns_status[0]) * vb->num_pfns);
 
 	init_completion(&vb->acked);
 
+	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_HOST_MAY_REFUSE))
+		n_out = 1;
+
 	/* We should always be able to add one buffer to an empty queue. */
-	if (vq->vq_ops->add_buf(vq, &sg, 1, 0, vb) < 0)
+	if (vq->vq_ops->add_buf(vq, sg, 1, n_out, vb) < 0)
 		BUG();
 	vq->vq_ops->kick(vq);
 
@@ -98,6 +111,8 @@ static void tell_host(struct virtio_balloon *vb, struct virtqueue *vq)
 
 static void fill_balloon(struct virtio_balloon *vb, size_t num)
 {
+	int i;
+
 	/* We can only do one array worth at a time. */
 	num = min(num, ARRAY_SIZE(vb->pfns));
 
@@ -113,9 +128,7 @@ static void fill_balloon(struct virtio_balloon *vb, size_t num)
 			break;
 		}
 		vb->pfns[vb->num_pfns] = page_to_balloon_pfn(page);
-		totalram_pages--;
-		vb->num_pages++;
-		list_add(&page->lru, &vb->pages);
+		vb->pfns_status[vb->num_pfns] = VIRTIO_BALLOON_PFN_OK;
 	}
 
 	/* Didn't get any?  Oh well. */
@@ -123,6 +136,32 @@ static void fill_balloon(struct virtio_balloon *vb, size_t num)
 		return;
 
 	tell_host(vb, vb->inflate_vq);
+
+	for (i = 0; i < vb->num_pfns; i++) {
+		struct page *page = pfn_to_page(vb->pfns[i]);
+		if (vb->pfns_status[i] == VIRTIO_BALLOON_PFN_OK) {
+			list_add(&page->lru, &vb->pages);
+			vb->num_pages++;
+			totalram_pages--;
+		} else {
+			dev_dbg(&vb->vdev->dev,
+				"adding page %p (pfn %x) to refused pages list\n",
+				page, vb->pfns[i]);
+			list_add(&page->lru, &vb->refused_pages);
+		}
+	}
+}
+
+static void release_refused_pages(struct virtio_balloon *vb)
+{
+	struct page *page, *next;
+
+	list_for_each_entry_safe(page, next, &vb->refused_pages, lru) {
+		dev_dbg(&vb->vdev->dev,
+			"releasing previously refused page %p", page);
+		list_del(&page->lru);
+		__free_page(page);
+	}
 }
 
 static void release_pages_by_pfn(const u32 pfns[], unsigned int num)
@@ -263,13 +302,26 @@ static int balloon(void *_vballoon)
 					 || vb->need_stats_update
 					 || kthread_should_stop()
 					 || freezing(current));
+
 		if (vb->need_stats_update)
 			stats_handle_request(vb);
+
 		if (diff > 0)
 			fill_balloon(vb, diff);
 		else if (diff < 0)
 			leak_balloon(vb, -diff);
+
 		update_balloon_size(vb);
+
+		if (diff > 0 &&
+		    !list_empty(&vb->refused_pages) &&
+		    towards_target(vb) <= 0) {
+			/*
+			 * We reached our goal so we now can release
+			 * pages that host refused to lock for us.
+			 */
+			release_refused_pages(vb);
+		}
 	}
 	return 0;
 }
@@ -289,6 +341,7 @@ static int virtballoon_probe(struct virtio_device *vdev)
 	}
 
 	INIT_LIST_HEAD(&vb->pages);
+	INIT_LIST_HEAD(&vb->refused_pages);
 	vb->num_pages = 0;
 	init_waitqueue_head(&vb->config_change);
 	vb->vdev = vdev;
@@ -357,6 +410,7 @@ static void __devexit virtballoon_remove(struct virtio_device *vdev)
 static unsigned int features[] = {
 	VIRTIO_BALLOON_F_MUST_TELL_HOST,
 	VIRTIO_BALLOON_F_STATS_VQ,
+	VIRTIO_BALLOON_F_HOST_MAY_REFUSE,
 };
 
 static struct virtio_driver virtio_balloon_driver = {
diff --git a/include/linux/virtio_balloon.h b/include/linux/virtio_balloon.h
index a50ecd1..4baad1c 100644
--- a/include/linux/virtio_balloon.h
+++ b/include/linux/virtio_balloon.h
@@ -6,12 +6,16 @@
 #include <linux/virtio_config.h>
 
 /* The feature bitmap for virtio balloon */
-#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_MUST_TELL_HOST		0 /* Tell before reclaiming pages */
+#define VIRTIO_BALLOON_F_STATS_VQ		1 /* Memory Stats virtqueue */
+#define VIRTIO_BALLOON_F_HOST_MAY_REFUSE	2 /* Host may refuse some pages */
 
 /* Size of a PFN in the balloon interface. */
 #define VIRTIO_BALLOON_PFN_SHIFT 12
 
+#define VIRTIO_BALLOON_PFN_OK		0
+#define VIRTIO_BALLOON_PFN_FAIL		1
+
 struct virtio_balloon_config
 {
 	/* Number of pages host wants Guest to give up. */


  reply	other threads:[~2010-03-19 20:46 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-19 20:46 [RFC/PATCH 0/4] VMware balloon driver over virtio Dmitry Torokhov
2010-03-19 20:46 ` Dmitry Torokhov [this message]
2010-03-20 13:01   ` [PATCH 1/4] virtio-balloon: allow hypervisor refuse locking some pages Avi Kivity
2010-03-19 20:46 ` [PATCH 2/4] virtio-balloon: implement balloon reset Dmitry Torokhov
2010-03-19 20:46 ` [PATCH 3/4] x86: export vmware_platform() symbol Dmitry Torokhov
2010-03-19 20:47 ` [PATCH 4/4] Add VMware memory balloon driver Dmitry Torokhov
2010-03-20 13:10   ` Avi Kivity
2010-03-20 19:54     ` Dmitry Torokhov
2010-03-21  9:19       ` Avi Kivity

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=20100319204647.4418.23072.stgit@localhost.localdomain \
    --to=dtor@vmware.com \
    --cc=akataria@vmware.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pv-drivers@vmware.com \
    --cc=rusty@rustcorp.com.au \
    --cc=sajain@vmware.com \
    /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