public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Jonas Theisen <j.theisen@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH qemu-server 1/2] vmstatus: do not count memory reclaimed by the balloon as used
Date: Mon, 30 Nov 2026 11:20:52 +0100	[thread overview]
Message-ID: <20261130102217.70307-2-j.theisen@proxmox.com> (raw)
In-Reply-To: <20261130102217.70307-1-j.theisen@proxmox.com>

The guest memory usage is calculated as the total minus the free memory
reported by the guest's balloon driver. The Linux driver removes
inflated pages from the guest's total memory, but the Windows driver
keeps reporting the full physical memory and accounts the balloon pages
as allocated. With an inflated balloon, a Windows guest's usage thus
includes memory that the host already reclaimed, and can exceed the
memory the VM actually occupies on the host.

Subtract the part of the balloon that the guest still includes in its
total memory, derived from how far the reported total exceeds the
current balloon size. This works independently of the guest OS and
leaves guests that already shrink their total unaffected. Firmware-
reserved memory is missing from the guest's total, so the usage can
be overestimated by that amount.

Signed-off-by: Jonas Theisen <j.theisen@proxmox.com>
---
 src/PVE/QemuServer.pm | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm
index 1d665877..7de7a939 100644
--- a/src/PVE/QemuServer.pm
+++ b/src/PVE/QemuServer.pm
@@ -2362,7 +2362,8 @@ our $vmstatus_return_properties = {
     },
     mem => {
         description => "Currently used memory in bytes. Does not take into account kernel"
-            . " same-page merging (KSM). Uses information from ballooning when available.",
+            . " same-page merging (KSM). Uses information from ballooning when available and"
+            . " excludes memory reclaimed by the balloon device.",
         type => 'integer',
         optional => 1,
         renderer => 'bytes',
@@ -2674,7 +2675,15 @@ sub vmstatus {
         $d->{balloon} = $info->{actual};
 
         if (defined($info->{total_mem}) && defined($info->{free_mem})) {
-            $d->{mem} = $info->{total_mem} - $info->{free_mem};
+            # Some guests, like Windows, keep ballooned pages in their total and report them as
+            # allocated. The host already reclaimed these pages, so do not count them as used.
+            my $ballooned = $info->{max_mem} - $info->{actual};
+            my $ballooned_in_total = $info->{total_mem} - $info->{actual};
+            $ballooned_in_total = 0 if $ballooned_in_total < 0;
+            $ballooned_in_total = $ballooned if $ballooned_in_total > $ballooned;
+
+            my $used = $info->{total_mem} - $info->{free_mem} - $ballooned_in_total;
+            $d->{mem} = $used > 0 ? $used : 0;
             $d->{freemem} = $info->{free_mem};
         }
 
-- 
2.47.3





  reply	other threads:[~2026-11-30 10:22 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 10:20 [PATCH manager/qemu-server 0/2] fix memory usage reporting of VMs with an inflated balloon Jonas Theisen
2026-11-30 10:20 ` Jonas Theisen [this message]
2026-11-30 10:20 ` [PATCH manager 2/2] ui: guest status: show memory reclaimed by the balloon device Jonas Theisen

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=20261130102217.70307-2-j.theisen@proxmox.com \
    --to=j.theisen@proxmox.com \
    --cc=pve-devel@lists.proxmox.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
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal