Skip to contentSkip to conversation

balloon: Fix overflow when reporting actual memory size

PATCH Applied Adam Litke 2 participants 2 messages Mar 25, 2010 – Mar 27, 2010

lore
Share & tools
lore

Adam Litke

author
#0 lore raw

PATCH 1/1 balloon: Fix overflow when reporting actual memory size

Beginning with its introduction, the virtio balloon has had an overflow error that causes 'info balloon' to misreport the actual memory size when the balloon itself becomes larger than 4G. Use a cast when converting dev->actual from pages to kB to prevent overflows.

Before:
(qemu) info balloon
balloon: actual=5120
(qemu) balloon 1025
(qemu) info balloon
balloon: actual=1025
(qemu) balloon 1024
(qemu) info balloon
balloon: actual=5120

After:
(qemu) info balloon
balloon: actual=5120
(qemu) balloon 1025
(qemu) info balloon
balloon: actual=1025
(qemu) balloon 1024
(qemu) info balloon
balloon: actual=1024

---
hw/virtio-balloon.c | 3 ++-

1 files changed, 2 insertions(+), 1 deletions(-)
hw/virtio-balloon.c +0 -0
index 086d9d1..6eedab1 100644
@@ -78,7 +78,8 @@ static void stat_put(QDict *dict, const char *label, uint64_t val)

static QObject *get_stats_qobject(VirtIOBalloon *dev)

{
    QDict *dict = qdict_new();

- uint32_t actual = ram_size - (dev->actual << VIRTIO_BALLOON_PFN_SHIFT);
+ uint64_t actual = ram_size - ((uint64_t) dev->actual <<
+ VIRTIO_BALLOON_PFN_SHIFT);

    stat_put(dict, "actual", actual);
    stat_put(dict, "mem_swapped_in", dev->stats[VIRTIO_BALLOON_S_SWAP_IN]);
Signature · 1 line
1.6.3.3
#1 lore raw
Quoted 15 lines · Adam Litke

Beginning with its introduction, the virtio balloon has had an overflow error that causes 'info balloon' to misreport the actual memory size when the balloon itself becomes larger than 4G. Use a cast when converting dev->actual from pages to kB to prevent overflows.

Before:
(qemu) info balloon
balloon: actual=5120
(qemu) balloon 1025
(qemu) info balloon
balloon: actual=1025
(qemu) balloon 1024
(qemu) info balloon
balloon: actual=5120

Quoted 9 lines

After:
(qemu) info balloon
balloon: actual=5120
(qemu) balloon 1025
(qemu) info balloon
balloon: actual=1025
(qemu) balloon 1024
(qemu) info balloon
balloon: actual=1024

Thanks, applied.

Quoted 26 lines

---
hw/virtio-balloon.c | 3 ++-

1 files changed, 2 insertions(+), 1 deletions(-)
hw/virtio-balloon.c +2 -1
index 086d9d1..6eedab1 100644
@@ -78,7 +78,8 @@ static void stat_put(QDict *dict, const char *label, uint64_t val)
 static QObject *get_stats_qobject(VirtIOBalloon *dev)
 {
    QDict *dict = qdict_new();
-    uint32_t actual = ram_size - (dev->actual << VIRTIO_BALLOON_PFN_SHIFT);
+    uint64_t actual = ram_size - ((uint64_t) dev->actual <<
+                                  VIRTIO_BALLOON_PFN_SHIFT);
 
    stat_put(dict, "actual", actual);
    stat_put(dict, "mem_swapped_in", dev->stats[VIRTIO_BALLOON_S_SWAP_IN]);
Signature · 1 line
1.6.3.3
Signature · 2 lines
Aurelien Jarno	                        GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net