balloon.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. /******************************************************************************
  2. * Xen balloon driver - enables returning/claiming memory to/from Xen.
  3. *
  4. * Copyright (c) 2003, B Dragovic
  5. * Copyright (c) 2003-2004, M Williamson, K Fraser
  6. * 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.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License version 2
  16. * as published by the Free Software Foundation; or, when distributed
  17. * separately from the Linux kernel or incorporated into other
  18. * software packages, subject to the following license:
  19. *
  20. * Permission is hereby granted, free of charge, to any person obtaining a copy
  21. * of this source file (the "Software"), to deal in the Software without
  22. * restriction, including without limitation the rights to use, copy, modify,
  23. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  24. * and to permit persons to whom the Software is furnished to do so, subject to
  25. * the following conditions:
  26. *
  27. * The above copyright notice and this permission notice shall be included in
  28. * all copies or substantial portions of the Software.
  29. *
  30. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  31. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  32. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  33. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  34. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  35. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  36. * IN THE SOFTWARE.
  37. */
  38. #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
  39. #include <linux/cpu.h>
  40. #include <linux/kernel.h>
  41. #include <linux/sched.h>
  42. #include <linux/cred.h>
  43. #include <linux/errno.h>
  44. #include <linux/mm.h>
  45. #include <linux/bootmem.h>
  46. #include <linux/pagemap.h>
  47. #include <linux/highmem.h>
  48. #include <linux/mutex.h>
  49. #include <linux/list.h>
  50. #include <linux/gfp.h>
  51. #include <linux/notifier.h>
  52. #include <linux/memory.h>
  53. #include <linux/memory_hotplug.h>
  54. #include <linux/percpu-defs.h>
  55. #include <linux/slab.h>
  56. #include <linux/sysctl.h>
  57. #include <asm/page.h>
  58. #include <asm/pgalloc.h>
  59. #include <asm/pgtable.h>
  60. #include <asm/tlb.h>
  61. #include <asm/xen/hypervisor.h>
  62. #include <asm/xen/hypercall.h>
  63. #include <xen/xen.h>
  64. #include <xen/interface/xen.h>
  65. #include <xen/interface/memory.h>
  66. #include <xen/balloon.h>
  67. #include <xen/features.h>
  68. #include <xen/page.h>
  69. #include <xen/mem-reservation.h>
  70. static int xen_hotplug_unpopulated;
  71. #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
  72. static int zero;
  73. static int one = 1;
  74. static struct ctl_table balloon_table[] = {
  75. {
  76. .procname = "hotplug_unpopulated",
  77. .data = &xen_hotplug_unpopulated,
  78. .maxlen = sizeof(int),
  79. .mode = 0644,
  80. .proc_handler = proc_dointvec_minmax,
  81. .extra1 = &zero,
  82. .extra2 = &one,
  83. },
  84. { }
  85. };
  86. static struct ctl_table balloon_root[] = {
  87. {
  88. .procname = "balloon",
  89. .mode = 0555,
  90. .child = balloon_table,
  91. },
  92. { }
  93. };
  94. static struct ctl_table xen_root[] = {
  95. {
  96. .procname = "xen",
  97. .mode = 0555,
  98. .child = balloon_root,
  99. },
  100. { }
  101. };
  102. #endif
  103. /*
  104. * Use one extent per PAGE_SIZE to avoid to break down the page into
  105. * multiple frame.
  106. */
  107. #define EXTENT_ORDER (fls(XEN_PFN_PER_PAGE) - 1)
  108. /*
  109. * balloon_process() state:
  110. *
  111. * BP_DONE: done or nothing to do,
  112. * BP_WAIT: wait to be rescheduled,
  113. * BP_EAGAIN: error, go to sleep,
  114. * BP_ECANCELED: error, balloon operation canceled.
  115. */
  116. enum bp_state {
  117. BP_DONE,
  118. BP_WAIT,
  119. BP_EAGAIN,
  120. BP_ECANCELED
  121. };
  122. static DEFINE_MUTEX(balloon_mutex);
  123. struct balloon_stats balloon_stats;
  124. EXPORT_SYMBOL_GPL(balloon_stats);
  125. /* We increase/decrease in batches which fit in a page */
  126. static xen_pfn_t frame_list[PAGE_SIZE / sizeof(xen_pfn_t)];
  127. /* List of ballooned pages, threaded through the mem_map array. */
  128. static LIST_HEAD(ballooned_pages);
  129. static DECLARE_WAIT_QUEUE_HEAD(balloon_wq);
  130. /* Main work function, always executed in process context. */
  131. static void balloon_process(struct work_struct *work);
  132. static DECLARE_DELAYED_WORK(balloon_worker, balloon_process);
  133. /* When ballooning out (allocating memory to return to Xen) we don't really
  134. want the kernel to try too hard since that can trigger the oom killer. */
  135. #define GFP_BALLOON \
  136. (GFP_HIGHUSER | __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC)
  137. /* balloon_append: add the given page to the balloon. */
  138. static void __balloon_append(struct page *page)
  139. {
  140. /* Lowmem is re-populated first, so highmem pages go at list tail. */
  141. if (PageHighMem(page)) {
  142. list_add_tail(&page->lru, &ballooned_pages);
  143. balloon_stats.balloon_high++;
  144. } else {
  145. list_add(&page->lru, &ballooned_pages);
  146. balloon_stats.balloon_low++;
  147. }
  148. wake_up(&balloon_wq);
  149. }
  150. static void balloon_append(struct page *page)
  151. {
  152. __balloon_append(page);
  153. }
  154. /* balloon_retrieve: rescue a page from the balloon, if it is not empty. */
  155. static struct page *balloon_retrieve(bool require_lowmem)
  156. {
  157. struct page *page;
  158. if (list_empty(&ballooned_pages))
  159. return NULL;
  160. page = list_entry(ballooned_pages.next, struct page, lru);
  161. if (require_lowmem && PageHighMem(page))
  162. return NULL;
  163. list_del(&page->lru);
  164. if (PageHighMem(page))
  165. balloon_stats.balloon_high--;
  166. else
  167. balloon_stats.balloon_low--;
  168. return page;
  169. }
  170. static struct page *balloon_next_page(struct page *page)
  171. {
  172. struct list_head *next = page->lru.next;
  173. if (next == &ballooned_pages)
  174. return NULL;
  175. return list_entry(next, struct page, lru);
  176. }
  177. static enum bp_state update_schedule(enum bp_state state)
  178. {
  179. if (state == BP_WAIT)
  180. return BP_WAIT;
  181. if (state == BP_ECANCELED)
  182. return BP_ECANCELED;
  183. if (state == BP_DONE) {
  184. balloon_stats.schedule_delay = 1;
  185. balloon_stats.retry_count = 1;
  186. return BP_DONE;
  187. }
  188. ++balloon_stats.retry_count;
  189. if (balloon_stats.max_retry_count != RETRY_UNLIMITED &&
  190. balloon_stats.retry_count > balloon_stats.max_retry_count) {
  191. balloon_stats.schedule_delay = 1;
  192. balloon_stats.retry_count = 1;
  193. return BP_ECANCELED;
  194. }
  195. balloon_stats.schedule_delay <<= 1;
  196. if (balloon_stats.schedule_delay > balloon_stats.max_schedule_delay)
  197. balloon_stats.schedule_delay = balloon_stats.max_schedule_delay;
  198. return BP_EAGAIN;
  199. }
  200. #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
  201. static void release_memory_resource(struct resource *resource)
  202. {
  203. if (!resource)
  204. return;
  205. /*
  206. * No need to reset region to identity mapped since we now
  207. * know that no I/O can be in this region
  208. */
  209. release_resource(resource);
  210. kfree(resource);
  211. }
  212. static struct resource *additional_memory_resource(phys_addr_t size)
  213. {
  214. struct resource *res;
  215. int ret;
  216. res = kzalloc(sizeof(*res), GFP_KERNEL);
  217. if (!res)
  218. return NULL;
  219. res->name = "System RAM";
  220. res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  221. ret = allocate_resource(&iomem_resource, res,
  222. size, 0, -1,
  223. PAGES_PER_SECTION * PAGE_SIZE, NULL, NULL);
  224. if (ret < 0) {
  225. pr_err("Cannot allocate new System RAM resource\n");
  226. kfree(res);
  227. return NULL;
  228. }
  229. #ifdef CONFIG_SPARSEMEM
  230. {
  231. unsigned long limit = 1UL << (MAX_PHYSMEM_BITS - PAGE_SHIFT);
  232. unsigned long pfn = res->start >> PAGE_SHIFT;
  233. if (pfn > limit) {
  234. pr_err("New System RAM resource outside addressable RAM (%lu > %lu)\n",
  235. pfn, limit);
  236. release_memory_resource(res);
  237. return NULL;
  238. }
  239. }
  240. #endif
  241. return res;
  242. }
  243. static enum bp_state reserve_additional_memory(void)
  244. {
  245. long credit;
  246. struct resource *resource;
  247. int nid, rc;
  248. unsigned long balloon_hotplug;
  249. credit = balloon_stats.target_pages + balloon_stats.target_unpopulated
  250. - balloon_stats.total_pages;
  251. /*
  252. * Already hotplugged enough pages? Wait for them to be
  253. * onlined.
  254. */
  255. if (credit <= 0)
  256. return BP_WAIT;
  257. balloon_hotplug = round_up(credit, PAGES_PER_SECTION);
  258. resource = additional_memory_resource(balloon_hotplug * PAGE_SIZE);
  259. if (!resource)
  260. goto err;
  261. nid = memory_add_physaddr_to_nid(resource->start);
  262. #ifdef CONFIG_XEN_HAVE_PVMMU
  263. /*
  264. * We don't support PV MMU when Linux and Xen is using
  265. * different page granularity.
  266. */
  267. BUILD_BUG_ON(XEN_PAGE_SIZE != PAGE_SIZE);
  268. /*
  269. * add_memory() will build page tables for the new memory so
  270. * the p2m must contain invalid entries so the correct
  271. * non-present PTEs will be written.
  272. *
  273. * If a failure occurs, the original (identity) p2m entries
  274. * are not restored since this region is now known not to
  275. * conflict with any devices.
  276. */
  277. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  278. unsigned long pfn, i;
  279. pfn = PFN_DOWN(resource->start);
  280. for (i = 0; i < balloon_hotplug; i++) {
  281. if (!set_phys_to_machine(pfn + i, INVALID_P2M_ENTRY)) {
  282. pr_warn("set_phys_to_machine() failed, no memory added\n");
  283. goto err;
  284. }
  285. }
  286. }
  287. #endif
  288. /*
  289. * add_memory_resource() will call online_pages() which in its turn
  290. * will call xen_online_page() callback causing deadlock if we don't
  291. * release balloon_mutex here. Unlocking here is safe because the
  292. * callers drop the mutex before trying again.
  293. */
  294. mutex_unlock(&balloon_mutex);
  295. /* add_memory_resource() requires the device_hotplug lock */
  296. lock_device_hotplug();
  297. rc = add_memory_resource(nid, resource, memhp_auto_online);
  298. unlock_device_hotplug();
  299. mutex_lock(&balloon_mutex);
  300. if (rc) {
  301. pr_warn("Cannot add additional memory (%i)\n", rc);
  302. goto err;
  303. }
  304. balloon_stats.total_pages += balloon_hotplug;
  305. return BP_WAIT;
  306. err:
  307. release_memory_resource(resource);
  308. return BP_ECANCELED;
  309. }
  310. static void xen_online_page(struct page *page)
  311. {
  312. __online_page_set_limits(page);
  313. mutex_lock(&balloon_mutex);
  314. __balloon_append(page);
  315. mutex_unlock(&balloon_mutex);
  316. }
  317. static int xen_memory_notifier(struct notifier_block *nb, unsigned long val, void *v)
  318. {
  319. if (val == MEM_ONLINE)
  320. schedule_delayed_work(&balloon_worker, 0);
  321. return NOTIFY_OK;
  322. }
  323. static struct notifier_block xen_memory_nb = {
  324. .notifier_call = xen_memory_notifier,
  325. .priority = 0
  326. };
  327. #else
  328. static enum bp_state reserve_additional_memory(void)
  329. {
  330. balloon_stats.target_pages = balloon_stats.current_pages +
  331. balloon_stats.target_unpopulated;
  332. return BP_ECANCELED;
  333. }
  334. #endif /* CONFIG_XEN_BALLOON_MEMORY_HOTPLUG */
  335. static long current_credit(void)
  336. {
  337. return balloon_stats.target_pages - balloon_stats.current_pages;
  338. }
  339. static bool balloon_is_inflated(void)
  340. {
  341. return balloon_stats.balloon_low || balloon_stats.balloon_high;
  342. }
  343. static enum bp_state increase_reservation(unsigned long nr_pages)
  344. {
  345. int rc;
  346. unsigned long i;
  347. struct page *page;
  348. if (nr_pages > ARRAY_SIZE(frame_list))
  349. nr_pages = ARRAY_SIZE(frame_list);
  350. page = list_first_entry_or_null(&ballooned_pages, struct page, lru);
  351. for (i = 0; i < nr_pages; i++) {
  352. if (!page) {
  353. nr_pages = i;
  354. break;
  355. }
  356. frame_list[i] = page_to_xen_pfn(page);
  357. page = balloon_next_page(page);
  358. }
  359. rc = xenmem_reservation_increase(nr_pages, frame_list);
  360. if (rc <= 0)
  361. return BP_EAGAIN;
  362. for (i = 0; i < rc; i++) {
  363. page = balloon_retrieve(false);
  364. BUG_ON(page == NULL);
  365. xenmem_reservation_va_mapping_update(1, &page, &frame_list[i]);
  366. /* Relinquish the page back to the allocator. */
  367. free_reserved_page(page);
  368. }
  369. balloon_stats.current_pages += rc;
  370. return BP_DONE;
  371. }
  372. static enum bp_state decrease_reservation(unsigned long nr_pages, gfp_t gfp)
  373. {
  374. enum bp_state state = BP_DONE;
  375. unsigned long i;
  376. struct page *page, *tmp;
  377. int ret;
  378. LIST_HEAD(pages);
  379. if (nr_pages > ARRAY_SIZE(frame_list))
  380. nr_pages = ARRAY_SIZE(frame_list);
  381. for (i = 0; i < nr_pages; i++) {
  382. page = alloc_page(gfp);
  383. if (page == NULL) {
  384. nr_pages = i;
  385. state = BP_EAGAIN;
  386. break;
  387. }
  388. adjust_managed_page_count(page, -1);
  389. xenmem_reservation_scrub_page(page);
  390. list_add(&page->lru, &pages);
  391. }
  392. /*
  393. * Ensure that ballooned highmem pages don't have kmaps.
  394. *
  395. * Do this before changing the p2m as kmap_flush_unused()
  396. * reads PTEs to obtain pages (and hence needs the original
  397. * p2m entry).
  398. */
  399. kmap_flush_unused();
  400. /*
  401. * Setup the frame, update direct mapping, invalidate P2M,
  402. * and add to balloon.
  403. */
  404. i = 0;
  405. list_for_each_entry_safe(page, tmp, &pages, lru) {
  406. frame_list[i++] = xen_page_to_gfn(page);
  407. xenmem_reservation_va_mapping_reset(1, &page);
  408. list_del(&page->lru);
  409. balloon_append(page);
  410. }
  411. flush_tlb_all();
  412. ret = xenmem_reservation_decrease(nr_pages, frame_list);
  413. BUG_ON(ret != nr_pages);
  414. balloon_stats.current_pages -= nr_pages;
  415. return state;
  416. }
  417. /*
  418. * As this is a work item it is guaranteed to run as a single instance only.
  419. * We may of course race updates of the target counts (which are protected
  420. * by the balloon lock), or with changes to the Xen hard limit, but we will
  421. * recover from these in time.
  422. */
  423. static void balloon_process(struct work_struct *work)
  424. {
  425. enum bp_state state = BP_DONE;
  426. long credit;
  427. do {
  428. mutex_lock(&balloon_mutex);
  429. credit = current_credit();
  430. if (credit > 0) {
  431. if (balloon_is_inflated())
  432. state = increase_reservation(credit);
  433. else
  434. state = reserve_additional_memory();
  435. }
  436. if (credit < 0) {
  437. long n_pages;
  438. n_pages = min(-credit, si_mem_available());
  439. state = decrease_reservation(n_pages, GFP_BALLOON);
  440. if (state == BP_DONE && n_pages != -credit &&
  441. n_pages < totalreserve_pages)
  442. state = BP_EAGAIN;
  443. }
  444. state = update_schedule(state);
  445. mutex_unlock(&balloon_mutex);
  446. cond_resched();
  447. } while (credit && state == BP_DONE);
  448. /* Schedule more work if there is some still to be done. */
  449. if (state == BP_EAGAIN)
  450. schedule_delayed_work(&balloon_worker, balloon_stats.schedule_delay * HZ);
  451. }
  452. /* Resets the Xen limit, sets new target, and kicks off processing. */
  453. void balloon_set_new_target(unsigned long target)
  454. {
  455. /* No need for lock. Not read-modify-write updates. */
  456. balloon_stats.target_pages = target;
  457. schedule_delayed_work(&balloon_worker, 0);
  458. }
  459. EXPORT_SYMBOL_GPL(balloon_set_new_target);
  460. static int add_ballooned_pages(int nr_pages)
  461. {
  462. enum bp_state st;
  463. if (xen_hotplug_unpopulated) {
  464. st = reserve_additional_memory();
  465. if (st != BP_ECANCELED) {
  466. int rc;
  467. mutex_unlock(&balloon_mutex);
  468. rc = wait_event_interruptible(balloon_wq,
  469. !list_empty(&ballooned_pages));
  470. mutex_lock(&balloon_mutex);
  471. return rc ? -ENOMEM : 0;
  472. }
  473. }
  474. if (si_mem_available() < nr_pages)
  475. return -ENOMEM;
  476. st = decrease_reservation(nr_pages, GFP_USER);
  477. if (st != BP_DONE)
  478. return -ENOMEM;
  479. return 0;
  480. }
  481. /**
  482. * alloc_xenballooned_pages - get pages that have been ballooned out
  483. * @nr_pages: Number of pages to get
  484. * @pages: pages returned
  485. * @return 0 on success, error otherwise
  486. */
  487. int alloc_xenballooned_pages(int nr_pages, struct page **pages)
  488. {
  489. int pgno = 0;
  490. struct page *page;
  491. int ret;
  492. mutex_lock(&balloon_mutex);
  493. balloon_stats.target_unpopulated += nr_pages;
  494. while (pgno < nr_pages) {
  495. page = balloon_retrieve(true);
  496. if (page) {
  497. pages[pgno++] = page;
  498. #ifdef CONFIG_XEN_HAVE_PVMMU
  499. /*
  500. * We don't support PV MMU when Linux and Xen is using
  501. * different page granularity.
  502. */
  503. BUILD_BUG_ON(XEN_PAGE_SIZE != PAGE_SIZE);
  504. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  505. ret = xen_alloc_p2m_entry(page_to_pfn(page));
  506. if (ret < 0)
  507. goto out_undo;
  508. }
  509. #endif
  510. } else {
  511. ret = add_ballooned_pages(nr_pages - pgno);
  512. if (ret < 0)
  513. goto out_undo;
  514. }
  515. }
  516. mutex_unlock(&balloon_mutex);
  517. return 0;
  518. out_undo:
  519. mutex_unlock(&balloon_mutex);
  520. free_xenballooned_pages(pgno, pages);
  521. /*
  522. * NB: free_xenballooned_pages will only subtract pgno pages, but since
  523. * target_unpopulated is incremented with nr_pages at the start we need
  524. * to remove the remaining ones also, or accounting will be screwed.
  525. */
  526. balloon_stats.target_unpopulated -= nr_pages - pgno;
  527. return ret;
  528. }
  529. EXPORT_SYMBOL(alloc_xenballooned_pages);
  530. /**
  531. * free_xenballooned_pages - return pages retrieved with get_ballooned_pages
  532. * @nr_pages: Number of pages
  533. * @pages: pages to return
  534. */
  535. void free_xenballooned_pages(int nr_pages, struct page **pages)
  536. {
  537. int i;
  538. mutex_lock(&balloon_mutex);
  539. for (i = 0; i < nr_pages; i++) {
  540. if (pages[i])
  541. balloon_append(pages[i]);
  542. }
  543. balloon_stats.target_unpopulated -= nr_pages;
  544. /* The balloon may be too large now. Shrink it if needed. */
  545. if (current_credit())
  546. schedule_delayed_work(&balloon_worker, 0);
  547. mutex_unlock(&balloon_mutex);
  548. }
  549. EXPORT_SYMBOL(free_xenballooned_pages);
  550. #ifdef CONFIG_XEN_PV
  551. static void __init balloon_add_region(unsigned long start_pfn,
  552. unsigned long pages)
  553. {
  554. unsigned long pfn, extra_pfn_end;
  555. struct page *page;
  556. /*
  557. * If the amount of usable memory has been limited (e.g., with
  558. * the 'mem' command line parameter), don't add pages beyond
  559. * this limit.
  560. */
  561. extra_pfn_end = min(max_pfn, start_pfn + pages);
  562. for (pfn = start_pfn; pfn < extra_pfn_end; pfn++) {
  563. page = pfn_to_page(pfn);
  564. /* totalram_pages and totalhigh_pages do not
  565. include the boot-time balloon extension, so
  566. don't subtract from it. */
  567. __balloon_append(page);
  568. }
  569. balloon_stats.total_pages += extra_pfn_end - start_pfn;
  570. }
  571. #endif
  572. static int __init balloon_init(void)
  573. {
  574. if (!xen_domain())
  575. return -ENODEV;
  576. pr_info("Initialising balloon driver\n");
  577. #ifdef CONFIG_XEN_PV
  578. balloon_stats.current_pages = xen_pv_domain()
  579. ? min(xen_start_info->nr_pages - xen_released_pages, max_pfn)
  580. : get_num_physpages();
  581. #else
  582. balloon_stats.current_pages = get_num_physpages();
  583. #endif
  584. balloon_stats.target_pages = balloon_stats.current_pages;
  585. balloon_stats.balloon_low = 0;
  586. balloon_stats.balloon_high = 0;
  587. balloon_stats.total_pages = balloon_stats.current_pages;
  588. balloon_stats.schedule_delay = 1;
  589. balloon_stats.max_schedule_delay = 32;
  590. balloon_stats.retry_count = 1;
  591. balloon_stats.max_retry_count = 4;
  592. #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
  593. set_online_page_callback(&xen_online_page);
  594. register_memory_notifier(&xen_memory_nb);
  595. register_sysctl_table(xen_root);
  596. #endif
  597. #ifdef CONFIG_XEN_PV
  598. {
  599. int i;
  600. /*
  601. * Initialize the balloon with pages from the extra memory
  602. * regions (see arch/x86/xen/setup.c).
  603. */
  604. for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++)
  605. if (xen_extra_mem[i].n_pfns)
  606. balloon_add_region(xen_extra_mem[i].start_pfn,
  607. xen_extra_mem[i].n_pfns);
  608. }
  609. #endif
  610. /* Init the xen-balloon driver. */
  611. xen_balloon_init();
  612. return 0;
  613. }
  614. subsys_initcall(balloon_init);