Skip to contentSkip to conversation

virtio: Report new guest memory statistics pertinent to memory ballooning (V3)

Qemu-devel Adam Litke 4 participants 11 messages Nov 17, 2009 – Nov 19, 2009

Subsystems (2)
lore
Share & tools
lore

Adam Litke

author
#0 lore raw

virtio: Report new guest memory statistics pertinent to memory ballooning (V3)

Changes since V2:
- Use a virtqueue for communication instead of the device config space

Changes since V1:
- In the monitor, print all stats on one line with less abbreviated names
- Coding style changes

When using ballooning to manage overcommitted memory on a host, a system for guests to communicate their memory usage to the host can provide information that will minimize the impact of ballooning on the guests. The current method employs a daemon running in each guest that communicates memory statistics to a host daemon at a specified time interval. The host daemon aggregates this information and inflates and/or deflates balloons according to the level of host memory pressure. This approach is effective but overly complex since a daemon must be installed inside each guest and coordinated to communicate with the host. A simpler approach is to collect memory statistics in the virtio balloon driver and communicate them directly to the hypervisor.

This patch implements the qemu side of the communication channel. I will post the kernel driver modifications in-reply to this message.

balloon.h +2 -2
index 60b4a5d..def4c56 100644
@@ -16,12 +16,12 @@
 
 #include "cpu-defs.h"
 
-typedef ram_addr_t (QEMUBalloonEvent)(void *opaque, ram_addr_t target);
+typedef int (QEMUBalloonEvent)(void *opaque, ram_addr_t target);
 
 void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque);
 
 void qemu_balloon(ram_addr_t target);
 
-ram_addr_t qemu_balloon_status(void);
+int qemu_balloon_status(void);
 
 #endif
hw/virtio-balloon.c +83 -4

147 lines not on this page · raw message

hw/virtio-balloon.h +16 -0
index 9a0d119..005ddeb 100644
@@ -25,6 +25,7 @@
 
 /* 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 */
 
 /* Size of a PFN in the balloon interface. */
 #define VIRTIO_BALLOON_PFN_SHIFT 12
@@ -37,4 +38,19 @@ struct virtio_balloon_config
     uint32_t actual;
 };
 
+/* Memory Statistics */
+#define VIRTIO_BALLOON_S_SWAP_IN  0   /* Number of pages swapped in */
+#define VIRTIO_BALLOON_S_SWAP_OUT 1   /* Number of pages swapped out */
+#define VIRTIO_BALLOON_S_ANON     2   /* Number of anonymous pages in use */
+#define VIRTIO_BALLOON_S_MAJFLT   3   /* Number of major faults */
+#define VIRTIO_BALLOON_S_MINFLT   4   /* Number of minor faults */
+#define VIRTIO_BALLOON_S_MEMFREE  5   /* Total amount of free memory */
+#define VIRTIO_BALLOON_S_MEMTOT   6   /* Total amount of memory */
+#define VIRTIO_BALLOON_S_NR       7
+
+typedef struct VirtIOBalloonStat {
+    uint16_t tag;
+    uint32_t val;
+} VirtIOBalloonStat;
+
 #endif
monitor.c +3 -5
index ed1ce6e..73484c8 100644
@@ -1583,16 +1583,14 @@ static void do_balloon(Monitor *mon, int value)
 
 static void do_info_balloon(Monitor *mon)
 {
-    ram_addr_t actual;
+    int ret;
 
-    actual = qemu_balloon_status();
     if (kvm_enabled() && !kvm_has_sync_mmu())
         monitor_printf(mon, "Using KVM without synchronous MMU, "
                        "ballooning disabled\n");
-    else if (actual == 0)
+    ret = qemu_balloon_status();
+    if (ret == -1)
         monitor_printf(mon, "Ballooning not activated in VM\n");
-    else
-        monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
 }
 
 static qemu_acl *find_acl(Monitor *mon, const char *name)
vl.c +2 -2
index 710d52e..aebceab 100644
@@ -337,11 +337,11 @@ void qemu_balloon(ram_addr_t target)
         qemu_balloon_event(qemu_balloon_event_opaque, target);
 }
 
-ram_addr_t qemu_balloon_status(void)
+int qemu_balloon_status(void)
 {
     if (qemu_balloon_event)
         return qemu_balloon_event(qemu_balloon_event_opaque, 0);
-    return 0;
+    return -1;
 }
 
 /***********************************************************/
Signature · 2 lines
Thanks,
Adam
author
#1 lore raw

virtio: Add memory statistics reporting to the balloon driver (V2)

virtio: Add memory statistics reporting to the balloon driver (V2)

Changes since V1:
- Use a virtqueue instead of the device config space

When using ballooning to manage overcommitted memory on a host, a system for guests to communicate their memory usage to the host can provide information that will minimize the impact of ballooning on the guests. The current method employs a daemon running in each guest that communicates memory statistics to a host daemon at a specified time interval. The host daemon aggregates this information and inflates and/or deflates balloons according to the level of host memory pressure. This approach is effective but overly complex since a daemon must be installed inside each guest and coordinated to communicate with the host. A simpler approach is to collect memory statistics in the virtio balloon driver and communicate them directly to the hypervisor.

This patch enables the guest-side support by adding stats collection and reporting to the virtio balloon driver.

drivers/virtio/virtio_balloon.c +82 -8
index 200c22f..59b9533 100644
@@ -29,7 +29,7 @@
 struct virtio_balloon
 {
 	struct virtio_device *vdev;
-	struct virtqueue *inflate_vq, *deflate_vq;
+	struct virtqueue *inflate_vq, *deflate_vq, *stats_vq;
 
 	/* Where the ballooning thread waits for config to change. */
 	wait_queue_head_t config_change;
@@ -50,6 +50,9 @@ struct virtio_balloon
 	/* The array of pfns we tell the Host about. */
 	unsigned int num_pfns;
 	u32 pfns[256];
+
+	/* Memory statistics */
+	struct virtio_balloon_stat stats[VIRTIO_BALLOON_S_NR];
 };
 
 static struct virtio_device_id id_table[] = {
@@ -155,6 +158,60 @@ static void leak_balloon(struct virtio_balloon *vb, size_t num)
 	}
 }
 
+static inline void update_stat(struct virtio_balloon *vb, int idx,
+				unsigned int tag, unsigned long val)
+{
+	BUG_ON(idx >= VIRTIO_BALLOON_S_NR);
+	vb->stats[idx].tag = tag;
+	vb->stats[idx].val = cpu_to_le32(val);
+}
+
+#define K(x) ((x) << (PAGE_SHIFT - 10))
+static void update_balloon_stats(struct virtio_balloon *vb)
+{
+	unsigned long events[NR_VM_EVENT_ITEMS];
+	struct sysinfo i;
+	unsigned long anon_pages;
+	int idx = 0;
+
+	all_vm_events(events);
+	si_meminfo(&i);
+	anon_pages = K(global_page_state(NR_ANON_PAGES));
+
+	update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_IN, events[PSWPIN]);
+	update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_OUT, events[PSWPOUT]);
+	update_stat(vb, idx++, VIRTIO_BALLOON_S_ANON, anon_pages);
+	update_stat(vb, idx++, VIRTIO_BALLOON_S_MAJFLT, events[PGMAJFAULT]);
+	update_stat(vb, idx++, VIRTIO_BALLOON_S_MINFLT, events[PGFAULT]);
+	update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMFREE, K(i.freeram));
+	update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMTOT, K(i.totalram));
+}
+
+/*
+ * While most virtqueues communicate guest-initiated requests to the hypervisor,
+ * the stats queue operates in reverse.  The driver initializes the virtqueue
+ * with a single buffer.  From that point forward, all conversations consist of
+ * a hypervisor request (a call to this function) which directs us to refill
+ * the virtqueue with a fresh stats buffer.
+ */
+static void stats_ack(struct virtqueue *vq)
+{
+	struct virtio_balloon *vb;
+	unsigned int len;
+	struct scatterlist sg;
+
+	vb = vq->vq_ops->get_buf(vq, &len);
+	if (!vb)
+		return;
+
+	update_balloon_stats(vb);
+
+	sg_init_one(&sg, vb->stats, sizeof(vb->stats));
+	if (vq->vq_ops->add_buf(vq, &sg, 1, 0, vb) < 0)
+		BUG();
+	vq->vq_ops->kick(vq);
+}
+
 static void virtballoon_changed(struct virtio_device *vdev)
 {
 	struct virtio_balloon *vb = vdev->priv;
@@ -205,10 +262,10 @@ static int balloon(void *_vballoon)
 static int virtballoon_probe(struct virtio_device *vdev)
 {
 	struct virtio_balloon *vb;
-	struct virtqueue *vqs[2];
-	vq_callback_t *callbacks[] = { balloon_ack, balloon_ack };
-	const char *names[] = { "inflate", "deflate" };
-	int err;
+	struct virtqueue *vqs[3];
+	vq_callback_t *callbacks[] = { balloon_ack, balloon_ack, stats_ack };
+	const char *names[] = { "inflate", "deflate", "stats" };
+	int err, nvqs;
 
 	vdev->priv = vb = kmalloc(sizeof(*vb), GFP_KERNEL);
 	if (!vb) {
@@ -221,13 +278,28 @@ static int virtballoon_probe(struct virtio_device *vdev)
 	init_waitqueue_head(&vb->config_change);
 	vb->vdev = vdev;
 
-	/* We expect two virtqueues. */
-	err = vdev->config->find_vqs(vdev, 2, vqs, callbacks, names);
+	/* We expect two virtqueues: inflate and deflate,
+	 * and optionally stat. */
+	nvqs = virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ) ? 3 : 2;
+	err = vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names);
 	if (err)
 		goto out_free_vb;
 
 	vb->inflate_vq = vqs[0];
 	vb->deflate_vq = vqs[1];
+	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) {
+		struct scatterlist sg;
+		vb->stats_vq = vqs[2];
+
+		/*
+		 * Prime this virtqueue with one buffer so the hypervisor can
+		 * use it to signal us later.
+		 */
+		sg_init_one(&sg, vb->stats, sizeof(vb->stats));
+		if (vb->stats_vq->vq_ops->add_buf(vb->stats_vq, &sg, 1, 0, vb) < 0)
+			BUG();
+		vb->stats_vq->vq_ops->kick(vb->stats_vq);
+	}
 
 	vb->thread = kthread_run(balloon, vb, "vballoon");
 	if (IS_ERR(vb->thread)) {
@@ -265,7 +337,9 @@ static void virtballoon_remove(struct virtio_device *vdev)
 	kfree(vb);
 }
 
-static unsigned int features[] = { VIRTIO_BALLOON_F_MUST_TELL_HOST };
+static unsigned int features[] = {
+	VIRTIO_BALLOON_F_MUST_TELL_HOST, VIRTIO_BALLOON_F_STATS_VQ,
+};
 
 static struct virtio_driver virtio_balloon = {
 	.feature_table = features,
include/linux/virtio_balloon.h +17 -0
index 09d7300..3d21b9f 100644
@@ -6,6 +6,7 @@
 
 /* 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 */
 
 /* Size of a PFN in the balloon interface. */
 #define VIRTIO_BALLOON_PFN_SHIFT 12
@@ -17,4 +18,20 @@ struct virtio_balloon_config
 	/* Number of pages we've actually got in balloon. */
 	__le32 actual;
 };
+
+#define VIRTIO_BALLOON_S_SWAP_IN  0   /* Number of pages swapped in */
+#define VIRTIO_BALLOON_S_SWAP_OUT 1   /* Number of pages swapped out */
+#define VIRTIO_BALLOON_S_ANON     2   /* Number of anonymous pages in use */
+#define VIRTIO_BALLOON_S_MAJFLT   3   /* Number of major faults */
+#define VIRTIO_BALLOON_S_MINFLT   4   /* Number of minor faults */
+#define VIRTIO_BALLOON_S_MEMFREE  5   /* Total amount of free memory */
+#define VIRTIO_BALLOON_S_MEMTOT   6   /* Total amount of memory */
+#define VIRTIO_BALLOON_S_NR       7
+
+struct virtio_balloon_stat
+{
+	__le16 tag;
+	__le32 val;
+};
+
 #endif /* _LINUX_VIRTIO_BALLOON_H */
Signature · 2 lines
Thanks,
Adam
author
#2 lore raw

virtio: Add memory statistics reporting to the balloon driver (V2)

In reply to Adam Litke

virtio: Add memory statistics reporting to the balloon driver (V2)

Changes since V1:
- Use a virtqueue instead of the device config space

When using ballooning to manage overcommitted memory on a host, a system for guests to communicate their memory usage to the host can provide information that will minimize the impact of ballooning on the guests. The current method employs a daemon running in each guest that communicates memory statistics to a host daemon at a specified time interval. The host daemon aggregates this information and inflates and/or deflates balloons according to the level of host memory pressure. This approach is effective but overly complex since a daemon must be installed inside each guest and coordinated to communicate with the host. A simpler approach is to collect memory statistics in the virtio balloon driver and communicate them directly to the hypervisor.

This patch enables the guest-side support by adding stats collection and reporting to the virtio balloon driver.

drivers/virtio/virtio_balloon.c +82 -8
index 200c22f..59b9533 100644
@@ -29,7 +29,7 @@
 struct virtio_balloon
 {
 	struct virtio_device *vdev;
-	struct virtqueue *inflate_vq, *deflate_vq;
+	struct virtqueue *inflate_vq, *deflate_vq, *stats_vq;
 
 	/* Where the ballooning thread waits for config to change. */
 	wait_queue_head_t config_change;
@@ -50,6 +50,9 @@ struct virtio_balloon
 	/* The array of pfns we tell the Host about. */
 	unsigned int num_pfns;
 	u32 pfns[256];
+
+	/* Memory statistics */
+	struct virtio_balloon_stat stats[VIRTIO_BALLOON_S_NR];
 };
 
 static struct virtio_device_id id_table[] = {
@@ -155,6 +158,60 @@ static void leak_balloon(struct virtio_balloon *vb, size_t num)
 	}
 }
 
+static inline void update_stat(struct virtio_balloon *vb, int idx,
+				unsigned int tag, unsigned long val)
+{
+	BUG_ON(idx >= VIRTIO_BALLOON_S_NR);
+	vb->stats[idx].tag = tag;
+	vb->stats[idx].val = cpu_to_le32(val);
+}
+
+#define K(x) ((x) << (PAGE_SHIFT - 10))
+static void update_balloon_stats(struct virtio_balloon *vb)
+{
+	unsigned long events[NR_VM_EVENT_ITEMS];
+	struct sysinfo i;
+	unsigned long anon_pages;
+	int idx = 0;
+
+	all_vm_events(events);
+	si_meminfo(&i);
+	anon_pages = K(global_page_state(NR_ANON_PAGES));
+
+	update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_IN, events[PSWPIN]);
+	update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_OUT, events[PSWPOUT]);
+	update_stat(vb, idx++, VIRTIO_BALLOON_S_ANON, anon_pages);
+	update_stat(vb, idx++, VIRTIO_BALLOON_S_MAJFLT, events[PGMAJFAULT]);
+	update_stat(vb, idx++, VIRTIO_BALLOON_S_MINFLT, events[PGFAULT]);
+	update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMFREE, K(i.freeram));
+	update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMTOT, K(i.totalram));
+}
+
+/*
+ * While most virtqueues communicate guest-initiated requests to the hypervisor,
+ * the stats queue operates in reverse.  The driver initializes the virtqueue
+ * with a single buffer.  From that point forward, all conversations consist of
+ * a hypervisor request (a call to this function) which directs us to refill
+ * the virtqueue with a fresh stats buffer.
+ */
+static void stats_ack(struct virtqueue *vq)
+{
+	struct virtio_balloon *vb;
+	unsigned int len;
+	struct scatterlist sg;
+
+	vb = vq->vq_ops->get_buf(vq, &len);
+	if (!vb)
+		return;
+
+	update_balloon_stats(vb);
+
+	sg_init_one(&sg, vb->stats, sizeof(vb->stats));
+	if (vq->vq_ops->add_buf(vq, &sg, 1, 0, vb) < 0)
+		BUG();
+	vq->vq_ops->kick(vq);
+}
+
 static void virtballoon_changed(struct virtio_device *vdev)
 {
 	struct virtio_balloon *vb = vdev->priv;
@@ -205,10 +262,10 @@ static int balloon(void *_vballoon)
 static int virtballoon_probe(struct virtio_device *vdev)
 {
 	struct virtio_balloon *vb;
-	struct virtqueue *vqs[2];
-	vq_callback_t *callbacks[] = { balloon_ack, balloon_ack };
-	const char *names[] = { "inflate", "deflate" };
-	int err;
+	struct virtqueue *vqs[3];
+	vq_callback_t *callbacks[] = { balloon_ack, balloon_ack, stats_ack };
+	const char *names[] = { "inflate", "deflate", "stats" };
+	int err, nvqs;
 
 	vdev->priv = vb = kmalloc(sizeof(*vb), GFP_KERNEL);
 	if (!vb) {
@@ -221,13 +278,28 @@ static int virtballoon_probe(struct virtio_device *vdev)
 	init_waitqueue_head(&vb->config_change);
 	vb->vdev = vdev;
 
-	/* We expect two virtqueues. */
-	err = vdev->config->find_vqs(vdev, 2, vqs, callbacks, names);
+	/* We expect two virtqueues: inflate and deflate,
+	 * and optionally stat. */
+	nvqs = virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ) ? 3 : 2;
+	err = vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names);
 	if (err)
 		goto out_free_vb;
 
 	vb->inflate_vq = vqs[0];
 	vb->deflate_vq = vqs[1];
+	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) {
+		struct scatterlist sg;
+		vb->stats_vq = vqs[2];
+
+		/*
+		 * Prime this virtqueue with one buffer so the hypervisor can
+		 * use it to signal us later.
+		 */
+		sg_init_one(&sg, vb->stats, sizeof(vb->stats));
+		if (vb->stats_vq->vq_ops->add_buf(vb->stats_vq, &sg, 1, 0, vb) < 0)
+			BUG();
+		vb->stats_vq->vq_ops->kick(vb->stats_vq);
+	}
 
 	vb->thread = kthread_run(balloon, vb, "vballoon");
 	if (IS_ERR(vb->thread)) {
@@ -265,7 +337,9 @@ static void virtballoon_remove(struct virtio_device *vdev)
 	kfree(vb);
 }
 
-static unsigned int features[] = { VIRTIO_BALLOON_F_MUST_TELL_HOST };
+static unsigned int features[] = {
+	VIRTIO_BALLOON_F_MUST_TELL_HOST, VIRTIO_BALLOON_F_STATS_VQ,
+};
 
 static struct virtio_driver virtio_balloon = {
 	.feature_table = features,
include/linux/virtio_balloon.h +17 -0
index 09d7300..3d21b9f 100644
@@ -6,6 +6,7 @@
 
 /* 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 */
 
 /* Size of a PFN in the balloon interface. */
 #define VIRTIO_BALLOON_PFN_SHIFT 12
@@ -17,4 +18,20 @@ struct virtio_balloon_config
 	/* Number of pages we've actually got in balloon. */
 	__le32 actual;
 };
+
+#define VIRTIO_BALLOON_S_SWAP_IN  0   /* Number of pages swapped in */
+#define VIRTIO_BALLOON_S_SWAP_OUT 1   /* Number of pages swapped out */
+#define VIRTIO_BALLOON_S_ANON     2   /* Number of anonymous pages in use */
+#define VIRTIO_BALLOON_S_MAJFLT   3   /* Number of major faults */
+#define VIRTIO_BALLOON_S_MINFLT   4   /* Number of minor faults */
+#define VIRTIO_BALLOON_S_MEMFREE  5   /* Total amount of free memory */
+#define VIRTIO_BALLOON_S_MEMTOT   6   /* Total amount of memory */
+#define VIRTIO_BALLOON_S_NR       7
+
+struct virtio_balloon_stat
+{
+	__le16 tag;
+	__le32 val;
+};
+
 #endif /* _LINUX_VIRTIO_BALLOON_H */
Signature · 2 lines
Thanks,
Adam
#3 lore raw

Re: virtio: Add memory statistics reporting to the balloon driver (V2)

Quoted 4 lines · Adam Litke

virtio: Add memory statistics reporting to the balloon driver (V2)

Changes since V1:
- Use a virtqueue instead of the device config space

Hi Adam,

If Anthony's happy, I'm happy with this approach.

Couple of minor points:

drivers/virtio/virtio_balloon.c static void leak_balloon(struct virtio_balloon *vb, size_t num) · line 167

Quoted 7 lines
drivers/virtio/virtio_balloon.c +7 -0
+static inline void update_stat(struct virtio_balloon *vb, int idx,
+				unsigned int tag, unsigned long val)
+{
+	BUG_ON(idx >= VIRTIO_BALLOON_S_NR);
+	vb->stats[idx].tag = tag;
+	vb->stats[idx].val = cpu_to_le32(val);
+}

The little-endian conversion of the balloon driver is a historical mistake (no other driver does this). Let's not extend it to the stats.

Here you've done one and not the other, which is even worse. (Sparse would have found this, I assume).

include/linux/virtio_balloon.h struct virtio_balloon_config · line 35

Quoted 5 lines
include/linux/virtio_balloon.h +5 -0
+struct virtio_balloon_stat
+{
+	__le16 tag;
+	__le32 val;
+};

Is 32 bits sufficient? A big machine might get over 4bn faults, and certainly 4 TB of memory isn't that far away.

Thanks,
Rusty.

#4 lore raw

Re: [Qemu-devel] Re: virtio: Add memory statistics reporting to the balloon driver (V2)

Quoted 15 lines · Rusty Russell
Quoted 5 lines · Adam Litke

virtio: Add memory statistics reporting to the balloon driver (V2)

Changes since V1:
- Use a virtqueue instead of the device config space

Hi Adam,

If Anthony's happy, I'm happy with this approach.

Couple of minor points:

Quoted 12 lines

drivers/virtio/virtio_balloon.c static void leak_balloon(struct virtio_balloon *vb, size_t num) · line 167

Quoted 8 lines
drivers/virtio/virtio_balloon.c +7 -0
+static inline void update_stat(struct virtio_balloon *vb, int idx,
+				unsigned int tag, unsigned long val)
+{
+	BUG_ON(idx >= VIRTIO_BALLOON_S_NR);
+	vb->stats[idx].tag = tag;
+	vb->stats[idx].val = cpu_to_le32(val);
+}

The little-endian conversion of the balloon driver is a historical mistake (no other driver does this). Let's not extend it to the stats.

I think the mistake is that the other drivers don't do that.

We cheat in qemu and assume that the guest is always in a fixed endianness but this is not always the case for all architectures.

That said, since we make this mistake everywhere, I guess I understand the argument to have consistency and to just admit that we're broken here. But this is where the endianness bits come from.

Quoted 3 lines

Here you've done one and not the other, which is even worse. (Sparse would have found this, I assume).

Yup, that's definitely wrong.

Quoted 10 lines

include/linux/virtio_balloon.h struct virtio_balloon_config · line 35

Quoted 6 lines
include/linux/virtio_balloon.h +5 -0
+struct virtio_balloon_stat
+{
+	__le16 tag;
+	__le32 val;
+};

Is 32 bits sufficient? A big machine might get over 4bn faults, and certainly 4 TB of memory isn't that far away.

I think making the interface u64 and byte based would be the best solution. Making assumptions about page size across guest and host is another thing we should try to avoid.

Regards,

Anthony Liguori

#5 lore raw

Re: [Qemu-devel] Re: virtio: Add memory statistics reporting to the balloon driver (V2)

Quoted 8 lines · Anthony Liguori
Quoted 2 lines · Rusty Russell

The little-endian conversion of the balloon driver is a historical mistake (no other driver does this). Let's not extend it to the stats.

I think the mistake is that the other drivers don't do that.

We cheat in qemu and assume that the guest is always in a fixed endianness but this is not always the case for all architectures.

If guests can have different endianness (reasonable on some CPUs where it's switchable - some even have more than 2 options), then I guess the *host* on those systems have different endianness too.

Is the host's endianness signalled to the guest anywhere, so that guest drivers can do cpu_to_qemuhost32(), when someone eventually finds that necessary?

-- Jamie

#6 lore raw

Re: [Qemu-devel] Re: virtio: Add memory statistics reporting to the balloon driver (V2)

In reply to Anthony Liguori
Quoted 8 lines · Anthony Liguori
Quoted 2 lines · Rusty Russell

The little-endian conversion of the balloon driver is a historical mistake (no other driver does this). Let's not extend it to the stats.

I think the mistake is that the other drivers don't do that.

We cheat in qemu and assume that the guest is always in a fixed endianness but this is not always the case for all architectures.

If guests can have different endianness (reasonable on some CPUs where it's switchable - some even have more than 2 options), then I guess the *host* on those systems have different endianness too.

Is the host's endianness signalled to the guest anywhere, so that guest drivers can do cpu_to_qemuhost32(), when someone eventually finds that necessary?

-- Jamie

#7 lore raw

Re: [Qemu-devel] Re: virtio: Add memory statistics reporting to the balloon driver (V2)

In reply to Anthony Liguori
Quoted 8 lines · Anthony Liguori
Quoted 2 lines · Rusty Russell

The little-endian conversion of the balloon driver is a historical mistake (no other driver does this). Let's not extend it to the stats.

I think the mistake is that the other drivers don't do that.

We cheat in qemu and assume that the guest is always in a fixed endianness but this is not always the case for all architectures.

Perhaps, but it's documented in the spec. My assertion remains that to do any virtualization you need to know what the guest endian is anyway, so endian converts throughout the drivers just add pain for driver authors.

Quoted 3 lines

I think making the interface u64 and byte based would be the best solution. Making assumptions about page size across guest and host is another thing we should try to avoid.

Yep, just report the raw byte counts as u64.

Cheers,
Rusty.

#8 lore raw

Re: [Qemu-devel] Re: virtio: Add memory statistics reporting to the balloon driver (V2)

In reply to Anthony Liguori
Quoted 8 lines · Anthony Liguori
Quoted 2 lines · Rusty Russell

The little-endian conversion of the balloon driver is a historical mistake (no other driver does this). Let's not extend it to the stats.

I think the mistake is that the other drivers don't do that.

We cheat in qemu and assume that the guest is always in a fixed endianness but this is not always the case for all architectures.

Perhaps, but it's documented in the spec. My assertion remains that to do any virtualization you need to know what the guest endian is anyway, so endian converts throughout the drivers just add pain for driver authors.

Quoted 3 lines

I think making the interface u64 and byte based would be the best solution. Making assumptions about page size across guest and host is another thing we should try to avoid.

Yep, just report the raw byte counts as u64.

Cheers,
Rusty.

#9 lore raw

Re: [Qemu-devel] Re: virtio: Add memory statistics reporting to the balloon driver (V2)

In reply to Rusty Russell
Quoted 15 lines · Rusty Russell
Quoted 5 lines · Adam Litke

virtio: Add memory statistics reporting to the balloon driver (V2)

Changes since V1:
- Use a virtqueue instead of the device config space

Hi Adam,

If Anthony's happy, I'm happy with this approach.

Couple of minor points:

Quoted 12 lines

drivers/virtio/virtio_balloon.c static void leak_balloon(struct virtio_balloon *vb, size_t num) · line 167

Quoted 8 lines
drivers/virtio/virtio_balloon.c +7 -0
+static inline void update_stat(struct virtio_balloon *vb, int idx,
+				unsigned int tag, unsigned long val)
+{
+	BUG_ON(idx >= VIRTIO_BALLOON_S_NR);
+	vb->stats[idx].tag = tag;
+	vb->stats[idx].val = cpu_to_le32(val);
+}

The little-endian conversion of the balloon driver is a historical mistake (no other driver does this). Let's not extend it to the stats.

I think the mistake is that the other drivers don't do that.

We cheat in qemu and assume that the guest is always in a fixed endianness but this is not always the case for all architectures.

That said, since we make this mistake everywhere, I guess I understand the argument to have consistency and to just admit that we're broken here. But this is where the endianness bits come from.

Quoted 3 lines

Here you've done one and not the other, which is even worse. (Sparse would have found this, I assume).

Yup, that's definitely wrong.

Quoted 10 lines

include/linux/virtio_balloon.h struct virtio_balloon_config · line 35

Quoted 6 lines
include/linux/virtio_balloon.h +5 -0
+struct virtio_balloon_stat
+{
+	__le16 tag;
+	__le32 val;
+};

Is 32 bits sufficient? A big machine might get over 4bn faults, and certainly 4 TB of memory isn't that far away.

I think making the interface u64 and byte based would be the best solution. Making assumptions about page size across guest and host is another thing we should try to avoid.

Regards,

Anthony Liguori

#10 lore raw

Re: virtio: Add memory statistics reporting to the balloon driver (V2)

In reply to Adam Litke
Quoted 4 lines · Adam Litke

virtio: Add memory statistics reporting to the balloon driver (V2)

Changes since V1:
- Use a virtqueue instead of the device config space

Hi Adam,

If Anthony's happy, I'm happy with this approach.

Couple of minor points:

drivers/virtio/virtio_balloon.c static void leak_balloon(struct virtio_balloon *vb, size_t num) · line 167

Quoted 7 lines
drivers/virtio/virtio_balloon.c +7 -0
+static inline void update_stat(struct virtio_balloon *vb, int idx,
+				unsigned int tag, unsigned long val)
+{
+	BUG_ON(idx >= VIRTIO_BALLOON_S_NR);
+	vb->stats[idx].tag = tag;
+	vb->stats[idx].val = cpu_to_le32(val);
+}

The little-endian conversion of the balloon driver is a historical mistake (no other driver does this). Let's not extend it to the stats.

Here you've done one and not the other, which is even worse. (Sparse would have found this, I assume).

include/linux/virtio_balloon.h struct virtio_balloon_config · line 35

Quoted 5 lines
include/linux/virtio_balloon.h +5 -0
+struct virtio_balloon_stat
+{
+	__le16 tag;
+	__le32 val;
+};

Is 32 bits sufficient? A big machine might get over 4bn faults, and certainly 4 TB of memory isn't that far away.

Thanks,
Rusty.