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

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>

authored by

Oleksandr Tyshchenko and committed by
Juergen Gross
b2371587 d1a928ea

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