balloon.c 16 KB

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