balloon.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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/errno.h>
  43. #include <linux/module.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 <asm/page.h>
  56. #include <asm/pgalloc.h>
  57. #include <asm/pgtable.h>
  58. #include <asm/tlb.h>
  59. #include <asm/xen/hypervisor.h>
  60. #include <asm/xen/hypercall.h>
  61. #include <xen/xen.h>
  62. #include <xen/interface/xen.h>
  63. #include <xen/interface/memory.h>
  64. #include <xen/balloon.h>
  65. #include <xen/features.h>
  66. #include <xen/page.h>
  67. /*
  68. * balloon_process() state:
  69. *
  70. * BP_DONE: done or nothing to do,
  71. * BP_EAGAIN: error, go to sleep,
  72. * BP_ECANCELED: error, balloon operation canceled.
  73. */
  74. enum bp_state {
  75. BP_DONE,
  76. BP_EAGAIN,
  77. BP_ECANCELED
  78. };
  79. static DEFINE_MUTEX(balloon_mutex);
  80. struct balloon_stats balloon_stats;
  81. EXPORT_SYMBOL_GPL(balloon_stats);
  82. /* We increase/decrease in batches which fit in a page */
  83. static xen_pfn_t frame_list[PAGE_SIZE / sizeof(unsigned long)];
  84. static DEFINE_PER_CPU(struct page *, balloon_scratch_page);
  85. /* List of ballooned pages, threaded through the mem_map array. */
  86. static LIST_HEAD(ballooned_pages);
  87. /* Main work function, always executed in process context. */
  88. static void balloon_process(struct work_struct *work);
  89. static DECLARE_DELAYED_WORK(balloon_worker, balloon_process);
  90. /* When ballooning out (allocating memory to return to Xen) we don't really
  91. want the kernel to try too hard since that can trigger the oom killer. */
  92. #define GFP_BALLOON \
  93. (GFP_HIGHUSER | __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC)
  94. static void scrub_page(struct page *page)
  95. {
  96. #ifdef CONFIG_XEN_SCRUB_PAGES
  97. clear_highpage(page);
  98. #endif
  99. }
  100. /* balloon_append: add the given page to the balloon. */
  101. static void __balloon_append(struct page *page)
  102. {
  103. /* Lowmem is re-populated first, so highmem pages go at list tail. */
  104. if (PageHighMem(page)) {
  105. list_add_tail(&page->lru, &ballooned_pages);
  106. balloon_stats.balloon_high++;
  107. } else {
  108. list_add(&page->lru, &ballooned_pages);
  109. balloon_stats.balloon_low++;
  110. }
  111. }
  112. static void balloon_append(struct page *page)
  113. {
  114. __balloon_append(page);
  115. adjust_managed_page_count(page, -1);
  116. }
  117. /* balloon_retrieve: rescue a page from the balloon, if it is not empty. */
  118. static struct page *balloon_retrieve(bool prefer_highmem)
  119. {
  120. struct page *page;
  121. if (list_empty(&ballooned_pages))
  122. return NULL;
  123. if (prefer_highmem)
  124. page = list_entry(ballooned_pages.prev, struct page, lru);
  125. else
  126. page = list_entry(ballooned_pages.next, struct page, lru);
  127. list_del(&page->lru);
  128. if (PageHighMem(page))
  129. balloon_stats.balloon_high--;
  130. else
  131. balloon_stats.balloon_low--;
  132. adjust_managed_page_count(page, 1);
  133. return page;
  134. }
  135. static struct page *balloon_next_page(struct page *page)
  136. {
  137. struct list_head *next = page->lru.next;
  138. if (next == &ballooned_pages)
  139. return NULL;
  140. return list_entry(next, struct page, lru);
  141. }
  142. static enum bp_state update_schedule(enum bp_state state)
  143. {
  144. if (state == BP_ECANCELED)
  145. return BP_ECANCELED;
  146. if (state == BP_DONE) {
  147. balloon_stats.schedule_delay = 1;
  148. balloon_stats.retry_count = 1;
  149. return BP_DONE;
  150. }
  151. ++balloon_stats.retry_count;
  152. if (balloon_stats.max_retry_count != RETRY_UNLIMITED &&
  153. balloon_stats.retry_count > balloon_stats.max_retry_count) {
  154. balloon_stats.schedule_delay = 1;
  155. balloon_stats.retry_count = 1;
  156. return BP_ECANCELED;
  157. }
  158. balloon_stats.schedule_delay <<= 1;
  159. if (balloon_stats.schedule_delay > balloon_stats.max_schedule_delay)
  160. balloon_stats.schedule_delay = balloon_stats.max_schedule_delay;
  161. return BP_EAGAIN;
  162. }
  163. #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
  164. static long current_credit(void)
  165. {
  166. return balloon_stats.target_pages - balloon_stats.current_pages -
  167. balloon_stats.hotplug_pages;
  168. }
  169. static bool balloon_is_inflated(void)
  170. {
  171. if (balloon_stats.balloon_low || balloon_stats.balloon_high ||
  172. balloon_stats.balloon_hotplug)
  173. return true;
  174. else
  175. return false;
  176. }
  177. /*
  178. * reserve_additional_memory() adds memory region of size >= credit above
  179. * max_pfn. New region is section aligned and size is modified to be multiple
  180. * of section size. Those features allow optimal use of address space and
  181. * establish proper alignment when this function is called first time after
  182. * boot (last section not fully populated at boot time contains unused memory
  183. * pages with PG_reserved bit not set; online_pages_range() does not allow page
  184. * onlining in whole range if first onlined page does not have PG_reserved
  185. * bit set). Real size of added memory is established at page onlining stage.
  186. */
  187. static enum bp_state reserve_additional_memory(long credit)
  188. {
  189. int nid, rc;
  190. u64 hotplug_start_paddr;
  191. unsigned long balloon_hotplug = credit;
  192. hotplug_start_paddr = PFN_PHYS(SECTION_ALIGN_UP(max_pfn));
  193. balloon_hotplug = round_up(balloon_hotplug, PAGES_PER_SECTION);
  194. nid = memory_add_physaddr_to_nid(hotplug_start_paddr);
  195. #ifdef CONFIG_XEN_HAVE_PVMMU
  196. /*
  197. * add_memory() will build page tables for the new memory so
  198. * the p2m must contain invalid entries so the correct
  199. * non-present PTEs will be written.
  200. *
  201. * If a failure occurs, the original (identity) p2m entries
  202. * are not restored since this region is now known not to
  203. * conflict with any devices.
  204. */
  205. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  206. unsigned long pfn, i;
  207. pfn = PFN_DOWN(hotplug_start_paddr);
  208. for (i = 0; i < balloon_hotplug; i++) {
  209. if (!set_phys_to_machine(pfn + i, INVALID_P2M_ENTRY)) {
  210. pr_warn("set_phys_to_machine() failed, no memory added\n");
  211. return BP_ECANCELED;
  212. }
  213. }
  214. }
  215. #endif
  216. rc = add_memory(nid, hotplug_start_paddr, balloon_hotplug << PAGE_SHIFT);
  217. if (rc) {
  218. pr_warn("Cannot add additional memory (%i)\n", rc);
  219. return BP_ECANCELED;
  220. }
  221. balloon_hotplug -= credit;
  222. balloon_stats.hotplug_pages += credit;
  223. balloon_stats.balloon_hotplug = balloon_hotplug;
  224. return BP_DONE;
  225. }
  226. static void xen_online_page(struct page *page)
  227. {
  228. __online_page_set_limits(page);
  229. mutex_lock(&balloon_mutex);
  230. __balloon_append(page);
  231. if (balloon_stats.hotplug_pages)
  232. --balloon_stats.hotplug_pages;
  233. else
  234. --balloon_stats.balloon_hotplug;
  235. mutex_unlock(&balloon_mutex);
  236. }
  237. static int xen_memory_notifier(struct notifier_block *nb, unsigned long val, void *v)
  238. {
  239. if (val == MEM_ONLINE)
  240. schedule_delayed_work(&balloon_worker, 0);
  241. return NOTIFY_OK;
  242. }
  243. static struct notifier_block xen_memory_nb = {
  244. .notifier_call = xen_memory_notifier,
  245. .priority = 0
  246. };
  247. #else
  248. static long current_credit(void)
  249. {
  250. unsigned long target = balloon_stats.target_pages;
  251. target = min(target,
  252. balloon_stats.current_pages +
  253. balloon_stats.balloon_low +
  254. balloon_stats.balloon_high);
  255. return target - balloon_stats.current_pages;
  256. }
  257. static bool balloon_is_inflated(void)
  258. {
  259. if (balloon_stats.balloon_low || balloon_stats.balloon_high)
  260. return true;
  261. else
  262. return false;
  263. }
  264. static enum bp_state reserve_additional_memory(long credit)
  265. {
  266. balloon_stats.target_pages = balloon_stats.current_pages;
  267. return BP_DONE;
  268. }
  269. #endif /* CONFIG_XEN_BALLOON_MEMORY_HOTPLUG */
  270. static enum bp_state increase_reservation(unsigned long nr_pages)
  271. {
  272. int rc;
  273. unsigned long pfn, i;
  274. struct page *page;
  275. struct xen_memory_reservation reservation = {
  276. .address_bits = 0,
  277. .extent_order = 0,
  278. .domid = DOMID_SELF
  279. };
  280. #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
  281. if (!balloon_stats.balloon_low && !balloon_stats.balloon_high) {
  282. nr_pages = min(nr_pages, balloon_stats.balloon_hotplug);
  283. balloon_stats.hotplug_pages += nr_pages;
  284. balloon_stats.balloon_hotplug -= nr_pages;
  285. return BP_DONE;
  286. }
  287. #endif
  288. if (nr_pages > ARRAY_SIZE(frame_list))
  289. nr_pages = ARRAY_SIZE(frame_list);
  290. page = list_first_entry_or_null(&ballooned_pages, struct page, lru);
  291. for (i = 0; i < nr_pages; i++) {
  292. if (!page) {
  293. nr_pages = i;
  294. break;
  295. }
  296. frame_list[i] = page_to_pfn(page);
  297. page = balloon_next_page(page);
  298. }
  299. set_xen_guest_handle(reservation.extent_start, frame_list);
  300. reservation.nr_extents = nr_pages;
  301. rc = HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation);
  302. if (rc <= 0)
  303. return BP_EAGAIN;
  304. for (i = 0; i < rc; i++) {
  305. page = balloon_retrieve(false);
  306. BUG_ON(page == NULL);
  307. pfn = page_to_pfn(page);
  308. #ifdef CONFIG_XEN_HAVE_PVMMU
  309. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  310. set_phys_to_machine(pfn, frame_list[i]);
  311. /* Link back into the page tables if not highmem. */
  312. if (!PageHighMem(page)) {
  313. int ret;
  314. ret = HYPERVISOR_update_va_mapping(
  315. (unsigned long)__va(pfn << PAGE_SHIFT),
  316. mfn_pte(frame_list[i], PAGE_KERNEL),
  317. 0);
  318. BUG_ON(ret);
  319. }
  320. }
  321. #endif
  322. /* Relinquish the page back to the allocator. */
  323. __free_reserved_page(page);
  324. }
  325. balloon_stats.current_pages += rc;
  326. return BP_DONE;
  327. }
  328. static enum bp_state decrease_reservation(unsigned long nr_pages, gfp_t gfp)
  329. {
  330. enum bp_state state = BP_DONE;
  331. unsigned long pfn, i;
  332. struct page *page;
  333. int ret;
  334. struct xen_memory_reservation reservation = {
  335. .address_bits = 0,
  336. .extent_order = 0,
  337. .domid = DOMID_SELF
  338. };
  339. #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
  340. if (balloon_stats.hotplug_pages) {
  341. nr_pages = min(nr_pages, balloon_stats.hotplug_pages);
  342. balloon_stats.hotplug_pages -= nr_pages;
  343. balloon_stats.balloon_hotplug += nr_pages;
  344. return BP_DONE;
  345. }
  346. #endif
  347. if (nr_pages > ARRAY_SIZE(frame_list))
  348. nr_pages = ARRAY_SIZE(frame_list);
  349. for (i = 0; i < nr_pages; i++) {
  350. page = alloc_page(gfp);
  351. if (page == NULL) {
  352. nr_pages = i;
  353. state = BP_EAGAIN;
  354. break;
  355. }
  356. scrub_page(page);
  357. frame_list[i] = page_to_pfn(page);
  358. }
  359. /*
  360. * Ensure that ballooned highmem pages don't have kmaps.
  361. *
  362. * Do this before changing the p2m as kmap_flush_unused()
  363. * reads PTEs to obtain pages (and hence needs the original
  364. * p2m entry).
  365. */
  366. kmap_flush_unused();
  367. /* Update direct mapping, invalidate P2M, and add to balloon. */
  368. for (i = 0; i < nr_pages; i++) {
  369. pfn = frame_list[i];
  370. frame_list[i] = pfn_to_mfn(pfn);
  371. page = pfn_to_page(pfn);
  372. #ifdef CONFIG_XEN_HAVE_PVMMU
  373. /*
  374. * Ballooned out frames are effectively replaced with
  375. * a scratch frame. Ensure direct mappings and the
  376. * p2m are consistent.
  377. */
  378. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  379. if (!PageHighMem(page)) {
  380. struct page *scratch_page = get_balloon_scratch_page();
  381. ret = HYPERVISOR_update_va_mapping(
  382. (unsigned long)__va(pfn << PAGE_SHIFT),
  383. pfn_pte(page_to_pfn(scratch_page),
  384. PAGE_KERNEL_RO), 0);
  385. BUG_ON(ret);
  386. put_balloon_scratch_page();
  387. }
  388. __set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
  389. }
  390. #endif
  391. balloon_append(page);
  392. }
  393. flush_tlb_all();
  394. set_xen_guest_handle(reservation.extent_start, frame_list);
  395. reservation.nr_extents = nr_pages;
  396. ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
  397. BUG_ON(ret != nr_pages);
  398. balloon_stats.current_pages -= nr_pages;
  399. return state;
  400. }
  401. /*
  402. * We avoid multiple worker processes conflicting via the balloon mutex.
  403. * We may of course race updates of the target counts (which are protected
  404. * by the balloon lock), or with changes to the Xen hard limit, but we will
  405. * recover from these in time.
  406. */
  407. static void balloon_process(struct work_struct *work)
  408. {
  409. enum bp_state state = BP_DONE;
  410. long credit;
  411. mutex_lock(&balloon_mutex);
  412. do {
  413. credit = current_credit();
  414. if (credit > 0) {
  415. if (balloon_is_inflated())
  416. state = increase_reservation(credit);
  417. else
  418. state = reserve_additional_memory(credit);
  419. }
  420. if (credit < 0)
  421. state = decrease_reservation(-credit, GFP_BALLOON);
  422. state = update_schedule(state);
  423. #ifndef CONFIG_PREEMPT
  424. if (need_resched())
  425. schedule();
  426. #endif
  427. } while (credit && state == BP_DONE);
  428. /* Schedule more work if there is some still to be done. */
  429. if (state == BP_EAGAIN)
  430. schedule_delayed_work(&balloon_worker, balloon_stats.schedule_delay * HZ);
  431. mutex_unlock(&balloon_mutex);
  432. }
  433. struct page *get_balloon_scratch_page(void)
  434. {
  435. struct page *ret = get_cpu_var(balloon_scratch_page);
  436. BUG_ON(ret == NULL);
  437. return ret;
  438. }
  439. void put_balloon_scratch_page(void)
  440. {
  441. put_cpu_var(balloon_scratch_page);
  442. }
  443. /* Resets the Xen limit, sets new target, and kicks off processing. */
  444. void balloon_set_new_target(unsigned long target)
  445. {
  446. /* No need for lock. Not read-modify-write updates. */
  447. balloon_stats.target_pages = target;
  448. schedule_delayed_work(&balloon_worker, 0);
  449. }
  450. EXPORT_SYMBOL_GPL(balloon_set_new_target);
  451. /**
  452. * alloc_xenballooned_pages - get pages that have been ballooned out
  453. * @nr_pages: Number of pages to get
  454. * @pages: pages returned
  455. * @highmem: allow highmem pages
  456. * @return 0 on success, error otherwise
  457. */
  458. int alloc_xenballooned_pages(int nr_pages, struct page **pages, bool highmem)
  459. {
  460. int pgno = 0;
  461. struct page *page;
  462. mutex_lock(&balloon_mutex);
  463. while (pgno < nr_pages) {
  464. page = balloon_retrieve(highmem);
  465. if (page && (highmem || !PageHighMem(page))) {
  466. pages[pgno++] = page;
  467. } else {
  468. enum bp_state st;
  469. if (page)
  470. balloon_append(page);
  471. st = decrease_reservation(nr_pages - pgno,
  472. highmem ? GFP_HIGHUSER : GFP_USER);
  473. if (st != BP_DONE)
  474. goto out_undo;
  475. }
  476. }
  477. mutex_unlock(&balloon_mutex);
  478. return 0;
  479. out_undo:
  480. while (pgno)
  481. balloon_append(pages[--pgno]);
  482. /* Free the memory back to the kernel soon */
  483. schedule_delayed_work(&balloon_worker, 0);
  484. mutex_unlock(&balloon_mutex);
  485. return -ENOMEM;
  486. }
  487. EXPORT_SYMBOL(alloc_xenballooned_pages);
  488. /**
  489. * free_xenballooned_pages - return pages retrieved with get_ballooned_pages
  490. * @nr_pages: Number of pages
  491. * @pages: pages to return
  492. */
  493. void free_xenballooned_pages(int nr_pages, struct page **pages)
  494. {
  495. int i;
  496. mutex_lock(&balloon_mutex);
  497. for (i = 0; i < nr_pages; i++) {
  498. if (pages[i])
  499. balloon_append(pages[i]);
  500. }
  501. /* The balloon may be too large now. Shrink it if needed. */
  502. if (current_credit())
  503. schedule_delayed_work(&balloon_worker, 0);
  504. mutex_unlock(&balloon_mutex);
  505. }
  506. EXPORT_SYMBOL(free_xenballooned_pages);
  507. static void __init balloon_add_region(unsigned long start_pfn,
  508. unsigned long pages)
  509. {
  510. unsigned long pfn, extra_pfn_end;
  511. struct page *page;
  512. /*
  513. * If the amount of usable memory has been limited (e.g., with
  514. * the 'mem' command line parameter), don't add pages beyond
  515. * this limit.
  516. */
  517. extra_pfn_end = min(max_pfn, start_pfn + pages);
  518. for (pfn = start_pfn; pfn < extra_pfn_end; pfn++) {
  519. page = pfn_to_page(pfn);
  520. /* totalram_pages and totalhigh_pages do not
  521. include the boot-time balloon extension, so
  522. don't subtract from it. */
  523. __balloon_append(page);
  524. }
  525. }
  526. static int alloc_balloon_scratch_page(int cpu)
  527. {
  528. if (per_cpu(balloon_scratch_page, cpu) != NULL)
  529. return 0;
  530. per_cpu(balloon_scratch_page, cpu) = alloc_page(GFP_KERNEL);
  531. if (per_cpu(balloon_scratch_page, cpu) == NULL) {
  532. pr_warn("Failed to allocate balloon_scratch_page for cpu %d\n", cpu);
  533. return -ENOMEM;
  534. }
  535. return 0;
  536. }
  537. static int balloon_cpu_notify(struct notifier_block *self,
  538. unsigned long action, void *hcpu)
  539. {
  540. int cpu = (long)hcpu;
  541. switch (action) {
  542. case CPU_UP_PREPARE:
  543. if (alloc_balloon_scratch_page(cpu))
  544. return NOTIFY_BAD;
  545. break;
  546. default:
  547. break;
  548. }
  549. return NOTIFY_OK;
  550. }
  551. static struct notifier_block balloon_cpu_notifier = {
  552. .notifier_call = balloon_cpu_notify,
  553. };
  554. static int __init balloon_init(void)
  555. {
  556. int i, cpu;
  557. if (!xen_domain())
  558. return -ENODEV;
  559. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  560. register_cpu_notifier(&balloon_cpu_notifier);
  561. get_online_cpus();
  562. for_each_online_cpu(cpu) {
  563. if (alloc_balloon_scratch_page(cpu)) {
  564. put_online_cpus();
  565. unregister_cpu_notifier(&balloon_cpu_notifier);
  566. return -ENOMEM;
  567. }
  568. }
  569. put_online_cpus();
  570. }
  571. pr_info("Initialising balloon driver\n");
  572. balloon_stats.current_pages = xen_pv_domain()
  573. ? min(xen_start_info->nr_pages - xen_released_pages, max_pfn)
  574. : get_num_physpages();
  575. balloon_stats.target_pages = balloon_stats.current_pages;
  576. balloon_stats.balloon_low = 0;
  577. balloon_stats.balloon_high = 0;
  578. balloon_stats.schedule_delay = 1;
  579. balloon_stats.max_schedule_delay = 32;
  580. balloon_stats.retry_count = 1;
  581. balloon_stats.max_retry_count = RETRY_UNLIMITED;
  582. #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
  583. balloon_stats.hotplug_pages = 0;
  584. balloon_stats.balloon_hotplug = 0;
  585. set_online_page_callback(&xen_online_page);
  586. register_memory_notifier(&xen_memory_nb);
  587. #endif
  588. /*
  589. * Initialize the balloon with pages from the extra memory
  590. * regions (see arch/x86/xen/setup.c).
  591. */
  592. for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++)
  593. if (xen_extra_mem[i].size)
  594. balloon_add_region(PFN_UP(xen_extra_mem[i].start),
  595. PFN_DOWN(xen_extra_mem[i].size));
  596. return 0;
  597. }
  598. subsys_initcall(balloon_init);
  599. static int __init balloon_clear(void)
  600. {
  601. int cpu;
  602. for_each_possible_cpu(cpu)
  603. per_cpu(balloon_scratch_page, cpu) = NULL;
  604. return 0;
  605. }
  606. early_initcall(balloon_clear);
  607. MODULE_LICENSE("GPL");