Skip to content

Commit 0949c64

Browse files
roygerjgross1
authored andcommitted
Partial revert "x86/xen: fix balloon target initialization for PVH dom0"
This partially reverts commit 87af633 so the current memory target for PV guests is still fetched from start_info->nr_pages, which matches exactly what the toolstack sets the initial memory target to. Using get_num_physpages() is possible on PV also, but needs adjusting to take into account the ISA hole and the PFN at 0 not considered usable memory despite being populated, and hence would need extra adjustments. Instead of carrying those extra adjustments switch back to the previous code. That leaves Linux with a difference in how current memory target is obtained for HVM vs PV, but that's better than adding extra logic just for PV. However if switching to start_info->nr_pages for PV domains we need to differentiate between released pages (freed back to the hypervisor) as opposed to pages in the physmap which are not populated to start with. Introduce a new xen_unpopulated_pages to account for papges that have never been populated, and hence in the PV case don't need subtracting. Fixes: 87af633 ("x86/xen: fix balloon target initialization for PVH dom0") Reported-by: James Dingwall <james@dingwall.me.uk> Signed-off-by: Roger Pau Monné <roger.pau@citrix.com> Reviewed-by: Juergen Gross <jgross@suse.com> Signed-off-by: Juergen Gross <jgross@suse.com> Message-ID: <20260128110510.46425-2-roger.pau@citrix.com>
1 parent 763baca commit 0949c64

4 files changed

Lines changed: 21 additions & 5 deletions

File tree

arch/x86/xen/enlighten.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ int __init arch_xen_unpopulated_init(struct resource **res)
470470
* driver to know how much of the physmap is unpopulated and
471471
* set an accurate initial memory target.
472472
*/
473-
xen_released_pages += xen_extra_mem[i].n_pfns;
473+
xen_unpopulated_pages += xen_extra_mem[i].n_pfns;
474474
/* Zero so region is not also added to the balloon driver. */
475475
xen_extra_mem[i].n_pfns = 0;
476476
}

drivers/xen/balloon.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -724,19 +724,26 @@ static int __init balloon_add_regions(void)
724724
static int __init balloon_init(void)
725725
{
726726
struct task_struct *task;
727+
unsigned long current_pages;
727728
int rc;
728729

729730
if (!xen_domain())
730731
return -ENODEV;
731732

732733
pr_info("Initialising balloon driver\n");
733734

734-
if (xen_released_pages >= get_num_physpages()) {
735-
WARN(1, "Released pages underflow current target");
736-
return -ERANGE;
735+
if (xen_pv_domain()) {
736+
if (xen_released_pages >= xen_start_info->nr_pages)
737+
goto underflow;
738+
current_pages = min(xen_start_info->nr_pages -
739+
xen_released_pages, max_pfn);
740+
} else {
741+
if (xen_unpopulated_pages >= get_num_physpages())
742+
goto underflow;
743+
current_pages = get_num_physpages() - xen_unpopulated_pages;
737744
}
738745

739-
balloon_stats.current_pages = get_num_physpages() - xen_released_pages;
746+
balloon_stats.current_pages = current_pages;
740747
balloon_stats.target_pages = balloon_stats.current_pages;
741748
balloon_stats.balloon_low = 0;
742749
balloon_stats.balloon_high = 0;
@@ -767,6 +774,10 @@ static int __init balloon_init(void)
767774
xen_balloon_init();
768775

769776
return 0;
777+
778+
underflow:
779+
WARN(1, "Released pages underflow current target");
780+
return -ERANGE;
770781
}
771782
subsys_initcall(balloon_init);
772783

drivers/xen/unpopulated-alloc.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ static unsigned int list_count;
1818

1919
static struct resource *target_resource;
2020

21+
/* Pages to subtract from the memory count when setting balloon target. */
22+
unsigned long xen_unpopulated_pages __initdata;
23+
2124
/*
2225
* If arch is not happy with system "iomem_resource" being used for
2326
* the region allocation it can provide it's own view by creating specific

include/xen/xen.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,13 @@ extern u64 xen_saved_max_mem_size;
6969
#endif
7070

7171
#ifdef CONFIG_XEN_UNPOPULATED_ALLOC
72+
extern unsigned long xen_unpopulated_pages;
7273
int xen_alloc_unpopulated_pages(unsigned int nr_pages, struct page **pages);
7374
void xen_free_unpopulated_pages(unsigned int nr_pages, struct page **pages);
7475
#include <linux/ioport.h>
7576
int arch_xen_unpopulated_init(struct resource **res);
7677
#else
78+
#define xen_unpopulated_pages 0UL
7779
#include <xen/balloon.h>
7880
static inline int xen_alloc_unpopulated_pages(unsigned int nr_pages,
7981
struct page **pages)

0 commit comments

Comments
 (0)