Skip to content

Commit 55b3da9

Browse files
author
David Vrabel
committed
xen/balloon: find non-conflicting regions to place hotplugged memory
Instead of placing hotplugged memory at the end of RAM (which may conflict with PCI devices or reserved regions) use allocate_resource() to get a new, suitably aligned resource that does not conflict. Signed-off-by: David Vrabel <[email protected]> Reviewed-by: Daniel Kiper <[email protected]> --- v3: - Remove stale comment.
1 parent f5775e0 commit 55b3da9

File tree

1 file changed

+53
-20
lines changed

1 file changed

+53
-20
lines changed

drivers/xen/balloon.c

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include <linux/memory.h>
5555
#include <linux/memory_hotplug.h>
5656
#include <linux/percpu-defs.h>
57+
#include <linux/slab.h>
5758

5859
#include <asm/page.h>
5960
#include <asm/pgalloc.h>
@@ -208,26 +209,56 @@ static bool balloon_is_inflated(void)
208209
return false;
209210
}
210211

211-
/*
212-
* reserve_additional_memory() adds memory region of size >= credit above
213-
* max_pfn. New region is section aligned and size is modified to be multiple
214-
* of section size. Those features allow optimal use of address space and
215-
* establish proper alignment when this function is called first time after
216-
* boot (last section not fully populated at boot time contains unused memory
217-
* pages with PG_reserved bit not set; online_pages_range() does not allow page
218-
* onlining in whole range if first onlined page does not have PG_reserved
219-
* bit set). Real size of added memory is established at page onlining stage.
220-
*/
212+
static struct resource *additional_memory_resource(phys_addr_t size)
213+
{
214+
struct resource *res;
215+
int ret;
216+
217+
res = kzalloc(sizeof(*res), GFP_KERNEL);
218+
if (!res)
219+
return NULL;
220+
221+
res->name = "System RAM";
222+
res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
223+
224+
ret = allocate_resource(&iomem_resource, res,
225+
size, 0, -1,
226+
PAGES_PER_SECTION * PAGE_SIZE, NULL, NULL);
227+
if (ret < 0) {
228+
pr_err("Cannot allocate new System RAM resource\n");
229+
kfree(res);
230+
return NULL;
231+
}
232+
233+
return res;
234+
}
235+
236+
static void release_memory_resource(struct resource *resource)
237+
{
238+
if (!resource)
239+
return;
240+
241+
/*
242+
* No need to reset region to identity mapped since we now
243+
* know that no I/O can be in this region
244+
*/
245+
release_resource(resource);
246+
kfree(resource);
247+
}
221248

222249
static enum bp_state reserve_additional_memory(long credit)
223250
{
251+
struct resource *resource;
224252
int nid, rc;
225-
u64 hotplug_start_paddr;
226-
unsigned long balloon_hotplug = credit;
253+
unsigned long balloon_hotplug;
254+
255+
balloon_hotplug = round_up(credit, PAGES_PER_SECTION);
256+
257+
resource = additional_memory_resource(balloon_hotplug * PAGE_SIZE);
258+
if (!resource)
259+
goto err;
227260

228-
hotplug_start_paddr = PFN_PHYS(SECTION_ALIGN_UP(max_pfn));
229-
balloon_hotplug = round_up(balloon_hotplug, PAGES_PER_SECTION);
230-
nid = memory_add_physaddr_to_nid(hotplug_start_paddr);
261+
nid = memory_add_physaddr_to_nid(resource->start);
231262

232263
#ifdef CONFIG_XEN_HAVE_PVMMU
233264
/*
@@ -242,21 +273,20 @@ static enum bp_state reserve_additional_memory(long credit)
242273
if (!xen_feature(XENFEAT_auto_translated_physmap)) {
243274
unsigned long pfn, i;
244275

245-
pfn = PFN_DOWN(hotplug_start_paddr);
276+
pfn = PFN_DOWN(resource->start);
246277
for (i = 0; i < balloon_hotplug; i++) {
247278
if (!set_phys_to_machine(pfn + i, INVALID_P2M_ENTRY)) {
248279
pr_warn("set_phys_to_machine() failed, no memory added\n");
249-
return BP_ECANCELED;
280+
goto err;
250281
}
251282
}
252283
}
253284
#endif
254285

255-
rc = add_memory(nid, hotplug_start_paddr, balloon_hotplug << PAGE_SHIFT);
256-
286+
rc = add_memory_resource(nid, resource);
257287
if (rc) {
258288
pr_warn("Cannot add additional memory (%i)\n", rc);
259-
return BP_ECANCELED;
289+
goto err;
260290
}
261291

262292
balloon_hotplug -= credit;
@@ -265,6 +295,9 @@ static enum bp_state reserve_additional_memory(long credit)
265295
balloon_stats.balloon_hotplug = balloon_hotplug;
266296

267297
return BP_DONE;
298+
err:
299+
release_memory_resource(resource);
300+
return BP_ECANCELED;
268301
}
269302

270303
static void xen_online_page(struct page *page)

0 commit comments

Comments
 (0)