Skip to content

Commit d1a928e

Browse files
Oleksandr Tyshchenkojgross1
authored andcommitted
xen/unpopulated-alloc: Add mechanism to use Xen resource
The main reason of this change is that unpopulated-alloc code cannot be used in its current form on Arm, but there is a desire to reuse it to avoid wasting real RAM pages for the grant/foreign mappings. The problem is that system "iomem_resource" is used for the address space allocation, but the really unallocated space can't be figured out precisely by the domain on Arm without hypervisor involvement. For example, not all device I/O regions are known by the time domain starts creating grant/foreign mappings. And following the advise from "iomem_resource" we might end up reusing these regions by a mistake. So, the hypervisor which maintains the P2M for the domain is in the best position to provide unused regions of guest physical address space which could be safely used to create grant/foreign mappings. Introduce new helper arch_xen_unpopulated_init() which purpose is to create specific Xen resource based on the memory regions provided by the hypervisor to be used as unused space for Xen scratch pages. If arch doesn't define arch_xen_unpopulated_init() the default "iomem_resource" will be used. Update the arguments list of allocate_resource() in fill_list() to always allocate a region from the hotpluggable range (maximum possible addressable physical memory range for which the linear mapping could be created). If arch doesn't define arch_get_mappable_range() the default range (0,-1) will be used. The behaviour on x86 won't be changed by current patch as both arch_xen_unpopulated_init() and arch_get_mappable_range() are not implemented for it. Also fallback to allocate xenballooned pages (balloon out RAM pages) if we do not have any suitable resource to work with (target_resource is invalid) and as the result we won't be able to provide unpopulated pages on a request. Signed-off-by: Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com> Reviewed-by: Stefano Stabellini <sstabellini@kernel.org> Link: https://lore.kernel.org/r/1639080336-26573-5-git-send-email-olekstysh@gmail.com Signed-off-by: Juergen Gross <jgross@suse.com>
1 parent 9dd060a commit d1a928e

File tree

2 files changed

+84
-4
lines changed

2 files changed

+84
-4
lines changed

drivers/xen/unpopulated-alloc.c

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,37 @@
88

99
#include <asm/page.h>
1010

11+
#include <xen/balloon.h>
1112
#include <xen/page.h>
1213
#include <xen/xen.h>
1314

1415
static DEFINE_MUTEX(list_lock);
1516
static struct page *page_list;
1617
static unsigned int list_count;
1718

19+
static struct resource *target_resource;
20+
21+
/*
22+
* If arch is not happy with system "iomem_resource" being used for
23+
* the region allocation it can provide it's own view by creating specific
24+
* Xen resource with unused regions of guest physical address space provided
25+
* by the hypervisor.
26+
*/
27+
int __weak __init arch_xen_unpopulated_init(struct resource **res)
28+
{
29+
*res = &iomem_resource;
30+
31+
return 0;
32+
}
33+
1834
static int fill_list(unsigned int nr_pages)
1935
{
2036
struct dev_pagemap *pgmap;
21-
struct resource *res;
37+
struct resource *res, *tmp_res = NULL;
2238
void *vaddr;
2339
unsigned int i, alloc_pages = round_up(nr_pages, PAGES_PER_SECTION);
24-
int ret = -ENOMEM;
40+
struct range mhp_range;
41+
int ret;
2542

2643
res = kzalloc(sizeof(*res), GFP_KERNEL);
2744
if (!res)
@@ -30,14 +47,40 @@ static int fill_list(unsigned int nr_pages)
3047
res->name = "Xen scratch";
3148
res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
3249

33-
ret = allocate_resource(&iomem_resource, res,
34-
alloc_pages * PAGE_SIZE, 0, -1,
50+
mhp_range = mhp_get_pluggable_range(true);
51+
52+
ret = allocate_resource(target_resource, res,
53+
alloc_pages * PAGE_SIZE, mhp_range.start, mhp_range.end,
3554
PAGES_PER_SECTION * PAGE_SIZE, NULL, NULL);
3655
if (ret < 0) {
3756
pr_err("Cannot allocate new IOMEM resource\n");
3857
goto err_resource;
3958
}
4059

60+
/*
61+
* Reserve the region previously allocated from Xen resource to avoid
62+
* re-using it by someone else.
63+
*/
64+
if (target_resource != &iomem_resource) {
65+
tmp_res = kzalloc(sizeof(*tmp_res), GFP_KERNEL);
66+
if (!tmp_res) {
67+
ret = -ENOMEM;
68+
goto err_insert;
69+
}
70+
71+
tmp_res->name = res->name;
72+
tmp_res->start = res->start;
73+
tmp_res->end = res->end;
74+
tmp_res->flags = res->flags;
75+
76+
ret = request_resource(&iomem_resource, tmp_res);
77+
if (ret < 0) {
78+
pr_err("Cannot request resource %pR (%d)\n", tmp_res, ret);
79+
kfree(tmp_res);
80+
goto err_insert;
81+
}
82+
}
83+
4184
pgmap = kzalloc(sizeof(*pgmap), GFP_KERNEL);
4285
if (!pgmap) {
4386
ret = -ENOMEM;
@@ -95,6 +138,11 @@ static int fill_list(unsigned int nr_pages)
95138
err_memremap:
96139
kfree(pgmap);
97140
err_pgmap:
141+
if (tmp_res) {
142+
release_resource(tmp_res);
143+
kfree(tmp_res);
144+
}
145+
err_insert:
98146
release_resource(res);
99147
err_resource:
100148
kfree(res);
@@ -112,6 +160,14 @@ int xen_alloc_unpopulated_pages(unsigned int nr_pages, struct page **pages)
112160
unsigned int i;
113161
int ret = 0;
114162

163+
/*
164+
* Fallback to default behavior if we do not have any suitable resource
165+
* to allocate required region from and as the result we won't be able to
166+
* construct pages.
167+
*/
168+
if (!target_resource)
169+
return xen_alloc_ballooned_pages(nr_pages, pages);
170+
115171
mutex_lock(&list_lock);
116172
if (list_count < nr_pages) {
117173
ret = fill_list(nr_pages - list_count);
@@ -159,6 +215,11 @@ void xen_free_unpopulated_pages(unsigned int nr_pages, struct page **pages)
159215
{
160216
unsigned int i;
161217

218+
if (!target_resource) {
219+
xen_free_ballooned_pages(nr_pages, pages);
220+
return;
221+
}
222+
162223
mutex_lock(&list_lock);
163224
for (i = 0; i < nr_pages; i++) {
164225
pages[i]->zone_device_data = page_list;
@@ -201,3 +262,20 @@ static int __init init(void)
201262
}
202263
subsys_initcall(init);
203264
#endif
265+
266+
static int __init unpopulated_init(void)
267+
{
268+
int ret;
269+
270+
if (!xen_domain())
271+
return -ENODEV;
272+
273+
ret = arch_xen_unpopulated_init(&target_resource);
274+
if (ret) {
275+
pr_err("xen:unpopulated: Cannot initialize target resource\n");
276+
target_resource = NULL;
277+
}
278+
279+
return ret;
280+
}
281+
early_initcall(unpopulated_init);

include/xen/xen.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ extern u64 xen_saved_max_mem_size;
5555
#ifdef CONFIG_XEN_UNPOPULATED_ALLOC
5656
int xen_alloc_unpopulated_pages(unsigned int nr_pages, struct page **pages);
5757
void xen_free_unpopulated_pages(unsigned int nr_pages, struct page **pages);
58+
#include <linux/ioport.h>
59+
int arch_xen_unpopulated_init(struct resource **res);
5860
#else
5961
#include <xen/balloon.h>
6062
static inline int xen_alloc_unpopulated_pages(unsigned int nr_pages,

0 commit comments

Comments
 (0)