From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id BAFD91FF0B7 for ; Mon, 30 Nov 2026 11:22:39 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 20DD221725; Mon, 30 Nov 2026 11:22:31 +0100 (CET) From: Jonas Theisen 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 Message-ID: <20261130102217.70307-2-j.theisen@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261130102217.70307-1-j.theisen@proxmox.com> References: <20261130102217.70307-1-j.theisen@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1796034147202 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.065 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: YAEAKFPE36KMEL4F7OV6BMB3RAX4CAXJ X-Message-ID-Hash: YAEAKFPE36KMEL4F7OV6BMB3RAX4CAXJ X-MailFrom: j.theisen@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 --- 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