Skip to content

Commit 080e2be

Browse files
dkipertorvalds
authored andcommitted
xen/balloon: memory hotplug support for Xen balloon driver
Memory hotplug support for Xen balloon driver. It should be mentioned that hotplugged memory is not onlined automatically. It should be onlined by user through standard sysfs interface. Memory could be hotplugged in following steps: 1) dom0: xl mem-max <domU> <maxmem> where <maxmem> is >= requested memory size, 2) dom0: xl mem-set <domU> <memory> where <memory> is requested memory size; alternatively memory could be added by writing proper value to /sys/devices/system/xen_memory/xen_memory0/target or /sys/devices/system/xen_memory/xen_memory0/target_kb on dumU, 3) domU: for i in /sys/devices/system/memory/memory*/state; do \ [ "`cat "$i"`" = offline ] && echo online > "$i"; done Memory could be onlined automatically on domU by adding following line to udev rules: SUBSYSTEM=="memory", ACTION=="add", RUN+="/bin/sh -c '[ -f /sys$devpath/state ] && echo online > /sys$devpath/state'" In that case step 3 should be omitted. Signed-off-by: Daniel Kiper <dkiper@net-space.pl> Acked-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com> Cc: Ian Campbell <ian.campbell@citrix.com> Cc: Jeremy Fitzhardinge <jeremy@goop.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 9d0ad8c commit 080e2be

3 files changed

Lines changed: 171 additions & 2 deletions

File tree

‎drivers/xen/Kconfig‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,36 @@ config XEN_SELFBALLOONING
2626
kernel boot parameter. Note that systems without a sufficiently
2727
large swap device should not enable self-ballooning.
2828

29+
config XEN_BALLOON_MEMORY_HOTPLUG
30+
bool "Memory hotplug support for Xen balloon driver"
31+
default n
32+
depends on XEN_BALLOON && MEMORY_HOTPLUG
33+
help
34+
Memory hotplug support for Xen balloon driver allows expanding memory
35+
available for the system above limit declared at system startup.
36+
It is very useful on critical systems which require long
37+
run without rebooting.
38+
39+
Memory could be hotplugged in following steps:
40+
41+
1) dom0: xl mem-max <domU> <maxmem>
42+
where <maxmem> is >= requested memory size,
43+
44+
2) dom0: xl mem-set <domU> <memory>
45+
where <memory> is requested memory size; alternatively memory
46+
could be added by writing proper value to
47+
/sys/devices/system/xen_memory/xen_memory0/target or
48+
/sys/devices/system/xen_memory/xen_memory0/target_kb on dumU,
49+
50+
3) domU: for i in /sys/devices/system/memory/memory*/state; do \
51+
[ "`cat "$i"`" = offline ] && echo online > "$i"; done
52+
53+
Memory could be onlined automatically on domU by adding following line to udev rules:
54+
55+
SUBSYSTEM=="memory", ACTION=="add", RUN+="/bin/sh -c '[ -f /sys$devpath/state ] && echo online > /sys$devpath/state'"
56+
57+
In that case step 3 should be omitted.
58+
2959
config XEN_SCRUB_PAGES
3060
bool "Scrub pages before returning them to system"
3161
depends on XEN_BALLOON

‎drivers/xen/balloon.c‎

Lines changed: 137 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
* Copyright (c) 2003, B Dragovic
55
* Copyright (c) 2003-2004, M Williamson, K Fraser
66
* Copyright (c) 2005 Dan M. Smith, IBM Corporation
7+
* Copyright (c) 2010 Daniel Kiper
8+
*
9+
* Memory hotplug support was written by Daniel Kiper. Work on
10+
* it was sponsored by Google under Google Summer of Code 2010
11+
* program. Jeremy Fitzhardinge from Citrix was the mentor for
12+
* this project.
713
*
814
* This program is free software; you can redistribute it and/or
915
* modify it under the terms of the GNU General Public License version 2
@@ -40,6 +46,9 @@
4046
#include <linux/mutex.h>
4147
#include <linux/list.h>
4248
#include <linux/gfp.h>
49+
#include <linux/notifier.h>
50+
#include <linux/memory.h>
51+
#include <linux/memory_hotplug.h>
4352

4453
#include <asm/page.h>
4554
#include <asm/pgalloc.h>
@@ -194,6 +203,87 @@ static enum bp_state update_schedule(enum bp_state state)
194203
return BP_EAGAIN;
195204
}
196205

206+
#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
207+
static long current_credit(void)
208+
{
209+
return balloon_stats.target_pages - balloon_stats.current_pages -
210+
balloon_stats.hotplug_pages;
211+
}
212+
213+
static bool balloon_is_inflated(void)
214+
{
215+
if (balloon_stats.balloon_low || balloon_stats.balloon_high ||
216+
balloon_stats.balloon_hotplug)
217+
return true;
218+
else
219+
return false;
220+
}
221+
222+
/*
223+
* reserve_additional_memory() adds memory region of size >= credit above
224+
* max_pfn. New region is section aligned and size is modified to be multiple
225+
* of section size. Those features allow optimal use of address space and
226+
* establish proper alignment when this function is called first time after
227+
* boot (last section not fully populated at boot time contains unused memory
228+
* pages with PG_reserved bit not set; online_pages_range() does not allow page
229+
* onlining in whole range if first onlined page does not have PG_reserved
230+
* bit set). Real size of added memory is established at page onlining stage.
231+
*/
232+
233+
static enum bp_state reserve_additional_memory(long credit)
234+
{
235+
int nid, rc;
236+
u64 hotplug_start_paddr;
237+
unsigned long balloon_hotplug = credit;
238+
239+
hotplug_start_paddr = PFN_PHYS(SECTION_ALIGN_UP(max_pfn));
240+
balloon_hotplug = round_up(balloon_hotplug, PAGES_PER_SECTION);
241+
nid = memory_add_physaddr_to_nid(hotplug_start_paddr);
242+
243+
rc = add_memory(nid, hotplug_start_paddr, balloon_hotplug << PAGE_SHIFT);
244+
245+
if (rc) {
246+
pr_info("xen_balloon: %s: add_memory() failed: %i\n", __func__, rc);
247+
return BP_EAGAIN;
248+
}
249+
250+
balloon_hotplug -= credit;
251+
252+
balloon_stats.hotplug_pages += credit;
253+
balloon_stats.balloon_hotplug = balloon_hotplug;
254+
255+
return BP_DONE;
256+
}
257+
258+
static void xen_online_page(struct page *page)
259+
{
260+
__online_page_set_limits(page);
261+
262+
mutex_lock(&balloon_mutex);
263+
264+
__balloon_append(page);
265+
266+
if (balloon_stats.hotplug_pages)
267+
--balloon_stats.hotplug_pages;
268+
else
269+
--balloon_stats.balloon_hotplug;
270+
271+
mutex_unlock(&balloon_mutex);
272+
}
273+
274+
static int xen_memory_notifier(struct notifier_block *nb, unsigned long val, void *v)
275+
{
276+
if (val == MEM_ONLINE)
277+
schedule_delayed_work(&balloon_worker, 0);
278+
279+
return NOTIFY_OK;
280+
}
281+
282+
static struct notifier_block xen_memory_nb = {
283+
.notifier_call = xen_memory_notifier,
284+
.priority = 0
285+
};
286+
#else
197287
static long current_credit(void)
198288
{
199289
unsigned long target = balloon_stats.target_pages;
@@ -206,6 +296,21 @@ static long current_credit(void)
206296
return target - balloon_stats.current_pages;
207297
}
208298

299+
static bool balloon_is_inflated(void)
300+
{
301+
if (balloon_stats.balloon_low || balloon_stats.balloon_high)
302+
return true;
303+
else
304+
return false;
305+
}
306+
307+
static enum bp_state reserve_additional_memory(long credit)
308+
{
309+
balloon_stats.target_pages = balloon_stats.current_pages;
310+
return BP_DONE;
311+
}
312+
#endif /* CONFIG_XEN_BALLOON_MEMORY_HOTPLUG */
313+
209314
static enum bp_state increase_reservation(unsigned long nr_pages)
210315
{
211316
int rc;
@@ -217,6 +322,15 @@ static enum bp_state increase_reservation(unsigned long nr_pages)
217322
.domid = DOMID_SELF
218323
};
219324

325+
#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
326+
if (!balloon_stats.balloon_low && !balloon_stats.balloon_high) {
327+
nr_pages = min(nr_pages, balloon_stats.balloon_hotplug);
328+
balloon_stats.hotplug_pages += nr_pages;
329+
balloon_stats.balloon_hotplug -= nr_pages;
330+
return BP_DONE;
331+
}
332+
#endif
333+
220334
if (nr_pages > ARRAY_SIZE(frame_list))
221335
nr_pages = ARRAY_SIZE(frame_list);
222336

@@ -279,6 +393,15 @@ static enum bp_state decrease_reservation(unsigned long nr_pages, gfp_t gfp)
279393
.domid = DOMID_SELF
280394
};
281395

396+
#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
397+
if (balloon_stats.hotplug_pages) {
398+
nr_pages = min(nr_pages, balloon_stats.hotplug_pages);
399+
balloon_stats.hotplug_pages -= nr_pages;
400+
balloon_stats.balloon_hotplug += nr_pages;
401+
return BP_DONE;
402+
}
403+
#endif
404+
282405
if (nr_pages > ARRAY_SIZE(frame_list))
283406
nr_pages = ARRAY_SIZE(frame_list);
284407

@@ -340,8 +463,12 @@ static void balloon_process(struct work_struct *work)
340463
do {
341464
credit = current_credit();
342465

343-
if (credit > 0)
344-
state = increase_reservation(credit);
466+
if (credit > 0) {
467+
if (balloon_is_inflated())
468+
state = increase_reservation(credit);
469+
else
470+
state = reserve_additional_memory(credit);
471+
}
345472

346473
if (credit < 0)
347474
state = decrease_reservation(-credit, GFP_BALLOON);
@@ -448,6 +575,14 @@ static int __init balloon_init(void)
448575
balloon_stats.retry_count = 1;
449576
balloon_stats.max_retry_count = RETRY_UNLIMITED;
450577

578+
#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
579+
balloon_stats.hotplug_pages = 0;
580+
balloon_stats.balloon_hotplug = 0;
581+
582+
set_online_page_callback(&xen_online_page);
583+
register_memory_notifier(&xen_memory_nb);
584+
#endif
585+
451586
/*
452587
* Initialise the balloon with excess memory space. We need
453588
* to make sure we don't add memory which doesn't exist or

‎include/xen/balloon.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ struct balloon_stats {
1515
unsigned long max_schedule_delay;
1616
unsigned long retry_count;
1717
unsigned long max_retry_count;
18+
#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
19+
unsigned long hotplug_pages;
20+
unsigned long balloon_hotplug;
21+
#endif
1822
};
1923

2024
extern struct balloon_stats balloon_stats;

0 commit comments

Comments
 (0)