virtio: avoid implicit use of Linux page size in balloon interface
authorHollis Blanchard <hollisb@us.ibm.com>
Thu, 13 Nov 2008 21:48:33 +0000 (15:48 -0600)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 29 Dec 2008 22:56:04 +0000 (09:26 +1030)
Make the balloon interface always use 4K pages, and convert Linux pfns if
necessary. This patch assumes that Linux's PAGE_SHIFT will never be less than
12.

Signed-off-by: Hollis Blanchard <hollisb@us.ibm.com>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> (modified)
drivers/virtio/virtio_balloon.c
include/linux/virtio_balloon.h

index 62eab43152d21314826838080caed9ad3ee3eb1e..59268266b79a5b7c6b439c6e9b210bb25c4236a3 100644 (file)
@@ -56,6 +56,15 @@ static struct virtio_device_id id_table[] = {
        { 0 },
 };
 
+static u32 page_to_balloon_pfn(struct page *page)
+{
+       unsigned long pfn = page_to_pfn(page);
+
+       BUILD_BUG_ON(PAGE_SHIFT < VIRTIO_BALLOON_PFN_SHIFT);
+       /* Convert pfn from Linux page size to balloon page size. */
+       return pfn >> (PAGE_SHIFT - VIRTIO_BALLOON_PFN_SHIFT);
+}
+
 static void balloon_ack(struct virtqueue *vq)
 {
        struct virtio_balloon *vb;
@@ -99,7 +108,7 @@ static void fill_balloon(struct virtio_balloon *vb, size_t num)
                        msleep(200);
                        break;
                }
-               vb->pfns[vb->num_pfns] = page_to_pfn(page);
+               vb->pfns[vb->num_pfns] = page_to_balloon_pfn(page);
                totalram_pages--;
                vb->num_pages++;
                list_add(&page->lru, &vb->pages);
@@ -132,7 +141,7 @@ static void leak_balloon(struct virtio_balloon *vb, size_t num)
        for (vb->num_pfns = 0; vb->num_pfns < num; vb->num_pfns++) {
                page = list_first_entry(&vb->pages, struct page, lru);
                list_del(&page->lru);
-               vb->pfns[vb->num_pfns] = page_to_pfn(page);
+               vb->pfns[vb->num_pfns] = page_to_balloon_pfn(page);
                vb->num_pages--;
        }
 
index c30c7bfbf39bcb0f26507b88a9e82ec336d6eff6..8726ff77763e47389e0cb61eec678c85228b06c6 100644 (file)
@@ -10,6 +10,9 @@
 /* The feature bitmap for virtio balloon */
 #define VIRTIO_BALLOON_F_MUST_TELL_HOST        0 /* Tell before reclaiming pages */
 
+/* Size of a PFN in the balloon interface. */
+#define VIRTIO_BALLOON_PFN_SHIFT 12
+
 struct virtio_balloon_config
 {
        /* Number of pages host wants Guest to give up. */