From nobody Thu Aug 13 09:15:46 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1785922864; cv=none; d=zohomail.com; s=zohoarc; b=V+VGFSLqmaRny395UWwpbA6c8jcXq6Ppx1OArXBVDqfLCfrmADsVVS3JgVvYn6AjUqldJBpNxxxkVld+WV5i5e4dsS7WkHTpGtuzKXINVuwIdv5BInQsd+bxwOGfHrjDYdbfeO5DB7tDGBuXOfpP8tZEqBOVQUmDOhrDHzBvLW0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1785922864; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=jqWNmZY06DVMjdGxIRpnMRf2z1reaDJOSpTrbBkvv8c=; b=GqQ/RdI9LTzwwLK6i//QOrn1rJvPhUAg00ZUl/EJnEMoBcKH6ImKPqT6Imif7TNfY+7D6O4b5oTE53xwpCyaBAwJ9EK6WVwLO67HHeRaJTI1z4gYF3OiuiYhosNIsgPrFALQqDppIDvE7AfwT6Yk0OiRMYOQXEzW8iNSKqst+sI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1785922864172719.1255059814175; Wed, 5 Aug 2026 02:41:04 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1383193.1626449 (Exim 4.92) (envelope-from ) id 1wrY76-0003eh-AE; Wed, 05 Aug 2026 09:40:40 +0000 Received: by outflank-mailman (output) from mailman id 1383193.1626449; Wed, 05 Aug 2026 09:40:40 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wrY76-0003ea-7f; Wed, 05 Aug 2026 09:40:40 +0000 Received: by outflank-mailman (input) for mailman id 1383193; Wed, 05 Aug 2026 09:40:39 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wrY75-0003eU-Hq for xen-devel@lists.xenproject.org; Wed, 05 Aug 2026 09:40:39 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.96) (envelope-from ) id 1wrY72-006IBs-32; Wed, 05 Aug 2026 09:40:36 +0000 Received: from 224.pool85-54-217.dynamic.orange.es ([85.54.217.224] helo=localhost) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wrY72-00FGrj-18; Wed, 05 Aug 2026 09:40:36 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type: MIME-Version:Message-ID:Date:Subject:Cc:To:From; bh=jqWNmZY06DVMjdGxIRpnMRf2z1reaDJOSpTrbBkvv8c=; b=Rz/Fywb9AY5sDD9He27t1Qc5k2 RRLgVyzgKt6l6h3C7jNH0drpNd+kl0uZZKYtPt3ybteq3VJImA/JPSCDOwYFsKpWvwIvFmKquewtp ud3QY0S6HTbjv2yVc3EgHFTnekJymgHrZpge7sFIVCqzIjlfT0KP6S7FcjlRKbpMe30Y=; From: Roger Pau Monne To: Juergen Gross , Roger Pau Monne , xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org Cc: Roger Pau Monne , stable@vger.kernel.org, Yannick Martin , "Thorsten Leemhuis" , Matthias Goergens , Stefano Stabellini , Oleksandr Tyshchenko Subject: [PATCH v2] x86/xen: fix init of balloon stats again Date: Wed, 5 Aug 2026 11:40:07 +0200 Message-ID: <20260805094008.95778-1-roger@xenproject.org> X-Mailer: git-send-email 2.53.0 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1785922867199158500 The handling of extra memory regions done in balloon_add_regions() is not correct for PV guests, since the initial target is set to reflect the real memory the system has, not what's described on the memory map, which can be higher if memory !=3D maxmem. Introduce separate logic for addition vs subtraction in balloon_add_regions() and handle extra regions correctly by adding them to the total amount of pages, instead of subtracting from the current and target pages amounts. In the common case PV domU/dom0 and PVH dom0 will use the addition path, since the initial target reflects the real assigned memory. HVM and PVH domUs use the subtraction path, since the target is set based on the amount of memory reported in the memory map, without accounting for released regions. Fixes: 87af633689ce ("x86/xen: fix balloon target initialization for PVH do= m0") Fixes: 0949c646d646 ("Partial revert "x86/xen: fix balloon target initializ= ation for PVH dom0"") Signed-off-by: Roger Pau Monn=C3=A9 Cc: stable@vger.kernel.org Reviewed-by: Juergen Gross Tested-by: Matthias Goergens --- Cc: Yannick Martin Cc: "Thorsten Leemhuis" Cc: Matthias Goergens --- Changes since v1: - Also fix PVH dom0 without unpopulated pages support. - Account for XENMEM_current_reservation possibly failing. --- drivers/xen/balloon.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/drivers/xen/balloon.c b/drivers/xen/balloon.c index e7f1d4ca6d75..e7f74ea7cd5e 100644 --- a/drivers/xen/balloon.c +++ b/drivers/xen/balloon.c @@ -679,7 +679,7 @@ void xen_free_ballooned_pages(unsigned int nr_pages, st= ruct page **pages) } EXPORT_SYMBOL(xen_free_ballooned_pages); =20 -static int __init balloon_add_regions(void) +static int __init balloon_add_regions(bool append) { unsigned long start_pfn, pages; unsigned long pfn, extra_pfn_end; @@ -703,19 +703,26 @@ static int __init balloon_add_regions(void) balloon_append(pfn_to_page(pfn)); =20 /* - * Extra regions are accounted for in the physmap, but need - * decreasing from current_pages and target_pages to balloon - * down the initial allocation, because they are already - * accounted for in total_pages. + * There are two different use-cases depending on how the + * initial memory target is fetched. For PVH dom0 and PV the + * target is usually set to reflect the domain assigned memory, + * and hence extra regions need adding. + * + * OTOH for HVM and PVH domU the target is set to the amount of + * RAM reported in the memory map, and hence extra regions need + * subtracting to reflect the real memory usage. */ pages =3D extra_pfn_end - start_pfn; - if (pages >=3D balloon_stats.current_pages || - pages >=3D balloon_stats.target_pages) { + if (append) { + balloon_stats.total_pages +=3D pages; + } else if (pages >=3D balloon_stats.current_pages || + pages >=3D balloon_stats.target_pages) { WARN(1, "Extra pages underflow current target"); return -ERANGE; + } else { + balloon_stats.current_pages -=3D pages; + balloon_stats.target_pages -=3D pages; } - balloon_stats.current_pages -=3D pages; - balloon_stats.target_pages -=3D pages; } =20 return 0; @@ -726,6 +733,7 @@ static int __init balloon_init(void) struct task_struct *task; long current_pages =3D 0; domid_t domid =3D DOMID_SELF; + bool append =3D true; int rc; =20 if (!xen_domain()) @@ -745,6 +753,7 @@ static int __init balloon_init(void) } else { if (xen_unpopulated_pages >=3D get_num_physpages()) goto underflow; + append =3D false; current_pages =3D get_num_physpages() - xen_unpopulated_pages; } @@ -767,7 +776,7 @@ static int __init balloon_init(void) register_sysctl_init("xen/balloon", balloon_table); #endif =20 - rc =3D balloon_add_regions(); + rc =3D balloon_add_regions(append); if (rc) return rc; =20 --=20 2.53.0