Skip to content

Commit b237158

Browse files
Oleksandr Tyshchenkojgross1
authored andcommitted
arm/xen: Read extended regions from DT and init Xen resource
This patch implements arch_xen_unpopulated_init() on Arm where the extended regions (if any) are gathered from DT and inserted into specific Xen resource to be used as unused address space for Xen scratch pages by unpopulated-alloc code. The extended region (safe range) is a region of guest physical address space which is unused and could be safely used to create grant/foreign mappings instead of wasting real RAM pages from the domain memory for establishing these mappings. The extended regions are chosen by the hypervisor at the domain creation time and advertised to it via "reg" property under hypervisor node in the guest device-tree. As region 0 is reserved for grant table space (always present), the indexes for extended regions are 1...N. If arch_xen_unpopulated_init() fails for some reason the default behaviour will be restored (allocate xenballooned pages). This patch also removes XEN_UNPOPULATED_ALLOC dependency on x86. Signed-off-by: Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com> Reviewed-by: Stefano Stabellini <sstabellini@kernel.org> Link: https://lore.kernel.org/r/1639080336-26573-6-git-send-email-olekstysh@gmail.com Signed-off-by: Juergen Gross <jgross@suse.com>
1 parent d1a928e commit b237158

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed

arch/arm/xen/enlighten.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ static __read_mostly unsigned int xen_events_irq;
6262
static __read_mostly phys_addr_t xen_grant_frames;
6363

6464
#define GRANT_TABLE_INDEX 0
65+
#define EXT_REGION_INDEX 1
6566

6667
uint32_t xen_start_flags;
6768
EXPORT_SYMBOL(xen_start_flags);
@@ -303,6 +304,111 @@ static void __init xen_acpi_guest_init(void)
303304
#endif
304305
}
305306

307+
#ifdef CONFIG_XEN_UNPOPULATED_ALLOC
308+
/*
309+
* A type-less specific Xen resource which contains extended regions
310+
* (unused regions of guest physical address space provided by the hypervisor).
311+
*/
312+
static struct resource xen_resource = {
313+
.name = "Xen unused space",
314+
};
315+
316+
int __init arch_xen_unpopulated_init(struct resource **res)
317+
{
318+
struct device_node *np;
319+
struct resource *regs, *tmp_res;
320+
uint64_t min_gpaddr = -1, max_gpaddr = 0;
321+
unsigned int i, nr_reg = 0;
322+
int rc;
323+
324+
if (!xen_domain())
325+
return -ENODEV;
326+
327+
if (!acpi_disabled)
328+
return -ENODEV;
329+
330+
np = of_find_compatible_node(NULL, NULL, "xen,xen");
331+
if (WARN_ON(!np))
332+
return -ENODEV;
333+
334+
/* Skip region 0 which is reserved for grant table space */
335+
while (of_get_address(np, nr_reg + EXT_REGION_INDEX, NULL, NULL))
336+
nr_reg++;
337+
338+
if (!nr_reg) {
339+
pr_err("No extended regions are found\n");
340+
return -EINVAL;
341+
}
342+
343+
regs = kcalloc(nr_reg, sizeof(*regs), GFP_KERNEL);
344+
if (!regs)
345+
return -ENOMEM;
346+
347+
/*
348+
* Create resource from extended regions provided by the hypervisor to be
349+
* used as unused address space for Xen scratch pages.
350+
*/
351+
for (i = 0; i < nr_reg; i++) {
352+
rc = of_address_to_resource(np, i + EXT_REGION_INDEX, &regs[i]);
353+
if (rc)
354+
goto err;
355+
356+
if (max_gpaddr < regs[i].end)
357+
max_gpaddr = regs[i].end;
358+
if (min_gpaddr > regs[i].start)
359+
min_gpaddr = regs[i].start;
360+
}
361+
362+
xen_resource.start = min_gpaddr;
363+
xen_resource.end = max_gpaddr;
364+
365+
/*
366+
* Mark holes between extended regions as unavailable. The rest of that
367+
* address space will be available for the allocation.
368+
*/
369+
for (i = 1; i < nr_reg; i++) {
370+
resource_size_t start, end;
371+
372+
/* There is an overlap between regions */
373+
if (regs[i - 1].end + 1 > regs[i].start) {
374+
rc = -EINVAL;
375+
goto err;
376+
}
377+
378+
/* There is no hole between regions */
379+
if (regs[i - 1].end + 1 == regs[i].start)
380+
continue;
381+
382+
start = regs[i - 1].end + 1;
383+
end = regs[i].start - 1;
384+
385+
tmp_res = kzalloc(sizeof(*tmp_res), GFP_KERNEL);
386+
if (!tmp_res) {
387+
rc = -ENOMEM;
388+
goto err;
389+
}
390+
391+
tmp_res->name = "Unavailable space";
392+
tmp_res->start = start;
393+
tmp_res->end = end;
394+
395+
rc = insert_resource(&xen_resource, tmp_res);
396+
if (rc) {
397+
pr_err("Cannot insert resource %pR (%d)\n", tmp_res, rc);
398+
kfree(tmp_res);
399+
goto err;
400+
}
401+
}
402+
403+
*res = &xen_resource;
404+
405+
err:
406+
kfree(regs);
407+
408+
return rc;
409+
}
410+
#endif
411+
306412
static void __init xen_dt_guest_init(void)
307413
{
308414
struct device_node *xen_node;

drivers/xen/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ config XEN_FRONT_PGDIR_SHBUF
327327

328328
config XEN_UNPOPULATED_ALLOC
329329
bool "Use unpopulated memory ranges for guest mappings"
330-
depends on X86 && ZONE_DEVICE
330+
depends on ZONE_DEVICE
331331
default XEN_BACKEND || XEN_GNTDEV || XEN_DOM0
332332
help
333333
Use unpopulated memory ranges in order to create mappings for guest

0 commit comments

Comments
 (0)