Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

xen/x86: fix initial memory balloon target

When adding extra memory regions as ballooned pages also adjust the balloon
target, otherwise when the balloon driver is started it will populate
memory to match the target value and consume all the extra memory regions
added.

This made the usage of the Xen `dom0_mem=,max:` command line parameter for
dom0 not work as expected, as the target won't be adjusted and when the
balloon is started it will populate memory straight to the 'max:' value.
It would equally affect domUs that have memory != maxmem.

Kernels built with CONFIG_XEN_UNPOPULATED_ALLOC are not affected, because
the extra memory regions are consumed by the unpopulated allocation driver,
and then balloon_add_regions() becomes a no-op.

Reported-by: John <jw@nuclearfallout.net>
Fixes: 87af633689ce ('x86/xen: fix balloon target initialization for PVH dom0')
Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
Reviewed-by: Juergen Gross <jgross@suse.com>
Tested-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
Message-ID: <20250514080427.28129-1-roger.pau@citrix.com>
Signed-off-by: Juergen Gross <jgross@suse.com>

authored by

Roger Pau Monne and committed by
Juergen Gross
74287971 d24c6b78

+8 -5
+8 -5
drivers/xen/balloon.c
··· 704 704 705 705 /* 706 706 * Extra regions are accounted for in the physmap, but need 707 - * decreasing from current_pages to balloon down the initial 708 - * allocation, because they are already accounted for in 709 - * total_pages. 707 + * decreasing from current_pages and target_pages to balloon 708 + * down the initial allocation, because they are already 709 + * accounted for in total_pages. 710 710 */ 711 - if (extra_pfn_end - start_pfn >= balloon_stats.current_pages) { 711 + pages = extra_pfn_end - start_pfn; 712 + if (pages >= balloon_stats.current_pages || 713 + pages >= balloon_stats.target_pages) { 712 714 WARN(1, "Extra pages underflow current target"); 713 715 return -ERANGE; 714 716 } 715 - balloon_stats.current_pages -= extra_pfn_end - start_pfn; 717 + balloon_stats.current_pages -= pages; 718 + balloon_stats.target_pages -= pages; 716 719 } 717 720 718 721 return 0;