balloon.c 16 KB

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