balloon.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version 2
  10. * as published by the Free Software Foundation; or, when distributed
  11. * separately from the Linux kernel or incorporated into other
  12. * software packages, subject to the following license:
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining a copy
  15. * of this source file (the "Software"), to deal in the Software without
  16. * restriction, including without limitation the rights to use, copy, modify,
  17. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  18. * and to permit persons to whom the Software is furnished to do so, subject to
  19. * the following conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be included in
  22. * all copies or substantial portions of the Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  29. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  30. * IN THE SOFTWARE.
  31. */
  32. #include <linux/kernel.h>
  33. #include <linux/sched.h>
  34. #include <linux/errno.h>
  35. #include <linux/mm.h>
  36. #include <linux/bootmem.h>
  37. #include <linux/pagemap.h>
  38. #include <linux/highmem.h>
  39. #include <linux/mutex.h>
  40. #include <linux/list.h>
  41. #include <linux/gfp.h>
  42. #include <asm/page.h>
  43. #include <asm/pgalloc.h>
  44. #include <asm/pgtable.h>
  45. #include <asm/tlb.h>
  46. #include <asm/e820.h>
  47. #include <asm/xen/hypervisor.h>
  48. #include <asm/xen/hypercall.h>
  49. #include <xen/xen.h>
  50. #include <xen/interface/xen.h>
  51. #include <xen/interface/memory.h>
  52. #include <xen/balloon.h>
  53. #include <xen/features.h>
  54. #include <xen/page.h>
  55. /*
  56. * balloon_process() state:
  57. *
  58. * BP_DONE: done or nothing to do,
  59. * BP_EAGAIN: error, go to sleep,
  60. * BP_ECANCELED: error, balloon operation canceled.
  61. */
  62. enum bp_state {
  63. BP_DONE,
  64. BP_EAGAIN,
  65. BP_ECANCELED
  66. };
  67. static DEFINE_MUTEX(balloon_mutex);
  68. struct balloon_stats balloon_stats;
  69. EXPORT_SYMBOL_GPL(balloon_stats);
  70. /* We increase/decrease in batches which fit in a page */
  71. static unsigned long frame_list[PAGE_SIZE / sizeof(unsigned long)];
  72. #ifdef CONFIG_HIGHMEM
  73. #define inc_totalhigh_pages() (totalhigh_pages++)
  74. #define dec_totalhigh_pages() (totalhigh_pages--)
  75. #else
  76. #define inc_totalhigh_pages() do {} while(0)
  77. #define dec_totalhigh_pages() do {} while(0)
  78. #endif
  79. /* List of ballooned pages, threaded through the mem_map array. */
  80. static LIST_HEAD(ballooned_pages);
  81. /* Main work function, always executed in process context. */
  82. static void balloon_process(struct work_struct *work);
  83. static DECLARE_DELAYED_WORK(balloon_worker, balloon_process);
  84. /* When ballooning out (allocating memory to return to Xen) we don't really
  85. want the kernel to try too hard since that can trigger the oom killer. */
  86. #define GFP_BALLOON \
  87. (GFP_HIGHUSER | __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC)
  88. static void scrub_page(struct page *page)
  89. {
  90. #ifdef CONFIG_XEN_SCRUB_PAGES
  91. clear_highpage(page);
  92. #endif
  93. }
  94. /* balloon_append: add the given page to the balloon. */
  95. static void __balloon_append(struct page *page)
  96. {
  97. /* Lowmem is re-populated first, so highmem pages go at list tail. */
  98. if (PageHighMem(page)) {
  99. list_add_tail(&page->lru, &ballooned_pages);
  100. balloon_stats.balloon_high++;
  101. dec_totalhigh_pages();
  102. } else {
  103. list_add(&page->lru, &ballooned_pages);
  104. balloon_stats.balloon_low++;
  105. }
  106. }
  107. static void balloon_append(struct page *page)
  108. {
  109. __balloon_append(page);
  110. totalram_pages--;
  111. }
  112. /* balloon_retrieve: rescue a page from the balloon, if it is not empty. */
  113. static struct page *balloon_retrieve(bool prefer_highmem)
  114. {
  115. struct page *page;
  116. if (list_empty(&ballooned_pages))
  117. return NULL;
  118. if (prefer_highmem)
  119. page = list_entry(ballooned_pages.prev, struct page, lru);
  120. else
  121. page = list_entry(ballooned_pages.next, struct page, lru);
  122. list_del(&page->lru);
  123. if (PageHighMem(page)) {
  124. balloon_stats.balloon_high--;
  125. inc_totalhigh_pages();
  126. }
  127. else
  128. balloon_stats.balloon_low--;
  129. totalram_pages++;
  130. return page;
  131. }
  132. static struct page *balloon_first_page(void)
  133. {
  134. if (list_empty(&ballooned_pages))
  135. return NULL;
  136. return list_entry(ballooned_pages.next, struct page, lru);
  137. }
  138. static struct page *balloon_next_page(struct page *page)
  139. {
  140. struct list_head *next = page->lru.next;
  141. if (next == &ballooned_pages)
  142. return NULL;
  143. return list_entry(next, struct page, lru);
  144. }
  145. static enum bp_state update_schedule(enum bp_state state)
  146. {
  147. if (state == BP_DONE) {
  148. balloon_stats.schedule_delay = 1;
  149. balloon_stats.retry_count = 1;
  150. return BP_DONE;
  151. }
  152. ++balloon_stats.retry_count;
  153. if (balloon_stats.max_retry_count != RETRY_UNLIMITED &&
  154. balloon_stats.retry_count > balloon_stats.max_retry_count) {
  155. balloon_stats.schedule_delay = 1;
  156. balloon_stats.retry_count = 1;
  157. return BP_ECANCELED;
  158. }
  159. balloon_stats.schedule_delay <<= 1;
  160. if (balloon_stats.schedule_delay > balloon_stats.max_schedule_delay)
  161. balloon_stats.schedule_delay = balloon_stats.max_schedule_delay;
  162. return BP_EAGAIN;
  163. }
  164. static unsigned long current_target(void)
  165. {
  166. unsigned long target = balloon_stats.target_pages;
  167. target = min(target,
  168. balloon_stats.current_pages +
  169. balloon_stats.balloon_low +
  170. balloon_stats.balloon_high);
  171. return target;
  172. }
  173. static enum bp_state increase_reservation(unsigned long nr_pages)
  174. {
  175. int rc;
  176. unsigned long pfn, i;
  177. struct page *page;
  178. struct xen_memory_reservation reservation = {
  179. .address_bits = 0,
  180. .extent_order = 0,
  181. .domid = DOMID_SELF
  182. };
  183. if (nr_pages > ARRAY_SIZE(frame_list))
  184. nr_pages = ARRAY_SIZE(frame_list);
  185. page = balloon_first_page();
  186. for (i = 0; i < nr_pages; i++) {
  187. if (!page) {
  188. nr_pages = i;
  189. break;
  190. }
  191. frame_list[i] = page_to_pfn(page);
  192. page = balloon_next_page(page);
  193. }
  194. set_xen_guest_handle(reservation.extent_start, frame_list);
  195. reservation.nr_extents = nr_pages;
  196. rc = HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation);
  197. if (rc <= 0)
  198. return BP_EAGAIN;
  199. for (i = 0; i < rc; i++) {
  200. page = balloon_retrieve(false);
  201. BUG_ON(page == NULL);
  202. pfn = page_to_pfn(page);
  203. BUG_ON(!xen_feature(XENFEAT_auto_translated_physmap) &&
  204. phys_to_machine_mapping_valid(pfn));
  205. set_phys_to_machine(pfn, frame_list[i]);
  206. /* Link back into the page tables if not highmem. */
  207. if (!xen_hvm_domain() && pfn < max_low_pfn) {
  208. int ret;
  209. ret = HYPERVISOR_update_va_mapping(
  210. (unsigned long)__va(pfn << PAGE_SHIFT),
  211. mfn_pte(frame_list[i], PAGE_KERNEL),
  212. 0);
  213. BUG_ON(ret);
  214. }
  215. /* Relinquish the page back to the allocator. */
  216. ClearPageReserved(page);
  217. init_page_count(page);
  218. __free_page(page);
  219. }
  220. balloon_stats.current_pages += rc;
  221. return BP_DONE;
  222. }
  223. static enum bp_state decrease_reservation(unsigned long nr_pages, gfp_t gfp)
  224. {
  225. enum bp_state state = BP_DONE;
  226. unsigned long pfn, i;
  227. struct page *page;
  228. int ret;
  229. struct xen_memory_reservation reservation = {
  230. .address_bits = 0,
  231. .extent_order = 0,
  232. .domid = DOMID_SELF
  233. };
  234. if (nr_pages > ARRAY_SIZE(frame_list))
  235. nr_pages = ARRAY_SIZE(frame_list);
  236. for (i = 0; i < nr_pages; i++) {
  237. if ((page = alloc_page(gfp)) == NULL) {
  238. nr_pages = i;
  239. state = BP_EAGAIN;
  240. break;
  241. }
  242. pfn = page_to_pfn(page);
  243. frame_list[i] = pfn_to_mfn(pfn);
  244. scrub_page(page);
  245. if (!xen_hvm_domain() && !PageHighMem(page)) {
  246. ret = HYPERVISOR_update_va_mapping(
  247. (unsigned long)__va(pfn << PAGE_SHIFT),
  248. __pte_ma(0), 0);
  249. BUG_ON(ret);
  250. }
  251. }
  252. /* Ensure that ballooned highmem pages don't have kmaps. */
  253. kmap_flush_unused();
  254. flush_tlb_all();
  255. /* No more mappings: invalidate P2M and add to balloon. */
  256. for (i = 0; i < nr_pages; i++) {
  257. pfn = mfn_to_pfn(frame_list[i]);
  258. __set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
  259. balloon_append(pfn_to_page(pfn));
  260. }
  261. set_xen_guest_handle(reservation.extent_start, frame_list);
  262. reservation.nr_extents = nr_pages;
  263. ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
  264. BUG_ON(ret != nr_pages);
  265. balloon_stats.current_pages -= nr_pages;
  266. return state;
  267. }
  268. /*
  269. * We avoid multiple worker processes conflicting via the balloon mutex.
  270. * We may of course race updates of the target counts (which are protected
  271. * by the balloon lock), or with changes to the Xen hard limit, but we will
  272. * recover from these in time.
  273. */
  274. static void balloon_process(struct work_struct *work)
  275. {
  276. enum bp_state state = BP_DONE;
  277. long credit;
  278. mutex_lock(&balloon_mutex);
  279. do {
  280. credit = current_target() - balloon_stats.current_pages;
  281. if (credit > 0)
  282. state = increase_reservation(credit);
  283. if (credit < 0)
  284. state = decrease_reservation(-credit, GFP_BALLOON);
  285. state = update_schedule(state);
  286. #ifndef CONFIG_PREEMPT
  287. if (need_resched())
  288. schedule();
  289. #endif
  290. } while (credit && state == BP_DONE);
  291. /* Schedule more work if there is some still to be done. */
  292. if (state == BP_EAGAIN)
  293. schedule_delayed_work(&balloon_worker, balloon_stats.schedule_delay * HZ);
  294. mutex_unlock(&balloon_mutex);
  295. }
  296. /* Resets the Xen limit, sets new target, and kicks off processing. */
  297. void balloon_set_new_target(unsigned long target)
  298. {
  299. /* No need for lock. Not read-modify-write updates. */
  300. balloon_stats.target_pages = target;
  301. schedule_delayed_work(&balloon_worker, 0);
  302. }
  303. EXPORT_SYMBOL_GPL(balloon_set_new_target);
  304. /**
  305. * alloc_xenballooned_pages - get pages that have been ballooned out
  306. * @nr_pages: Number of pages to get
  307. * @pages: pages returned
  308. * @return 0 on success, error otherwise
  309. */
  310. int alloc_xenballooned_pages(int nr_pages, struct page** pages)
  311. {
  312. int pgno = 0;
  313. struct page* page;
  314. mutex_lock(&balloon_mutex);
  315. while (pgno < nr_pages) {
  316. page = balloon_retrieve(true);
  317. if (page) {
  318. pages[pgno++] = page;
  319. } else {
  320. enum bp_state st;
  321. st = decrease_reservation(nr_pages - pgno, GFP_HIGHUSER);
  322. if (st != BP_DONE)
  323. goto out_undo;
  324. }
  325. }
  326. mutex_unlock(&balloon_mutex);
  327. return 0;
  328. out_undo:
  329. while (pgno)
  330. balloon_append(pages[--pgno]);
  331. /* Free the memory back to the kernel soon */
  332. schedule_delayed_work(&balloon_worker, 0);
  333. mutex_unlock(&balloon_mutex);
  334. return -ENOMEM;
  335. }
  336. EXPORT_SYMBOL(alloc_xenballooned_pages);
  337. /**
  338. * free_xenballooned_pages - return pages retrieved with get_ballooned_pages
  339. * @nr_pages: Number of pages
  340. * @pages: pages to return
  341. */
  342. void free_xenballooned_pages(int nr_pages, struct page** pages)
  343. {
  344. int i;
  345. mutex_lock(&balloon_mutex);
  346. for (i = 0; i < nr_pages; i++) {
  347. if (pages[i])
  348. balloon_append(pages[i]);
  349. }
  350. /* The balloon may be too large now. Shrink it if needed. */
  351. if (current_target() != balloon_stats.current_pages)
  352. schedule_delayed_work(&balloon_worker, 0);
  353. mutex_unlock(&balloon_mutex);
  354. }
  355. EXPORT_SYMBOL(free_xenballooned_pages);
  356. static int __init balloon_init(void)
  357. {
  358. unsigned long pfn, nr_pages, extra_pfn_end;
  359. struct page *page;
  360. if (!xen_domain())
  361. return -ENODEV;
  362. pr_info("xen/balloon: Initialising balloon driver.\n");
  363. if (xen_pv_domain())
  364. nr_pages = xen_start_info->nr_pages;
  365. else
  366. nr_pages = max_pfn;
  367. balloon_stats.current_pages = min(nr_pages, max_pfn);
  368. balloon_stats.target_pages = balloon_stats.current_pages;
  369. balloon_stats.balloon_low = 0;
  370. balloon_stats.balloon_high = 0;
  371. balloon_stats.schedule_delay = 1;
  372. balloon_stats.max_schedule_delay = 32;
  373. balloon_stats.retry_count = 1;
  374. balloon_stats.max_retry_count = RETRY_UNLIMITED;
  375. /*
  376. * Initialise the balloon with excess memory space. We need
  377. * to make sure we don't add memory which doesn't exist or
  378. * logically exist. The E820 map can be trimmed to be smaller
  379. * than the amount of physical memory due to the mem= command
  380. * line parameter. And if this is a 32-bit non-HIGHMEM kernel
  381. * on a system with memory which requires highmem to access,
  382. * don't try to use it.
  383. */
  384. extra_pfn_end = min(min(max_pfn, e820_end_of_ram_pfn()),
  385. (unsigned long)PFN_DOWN(xen_extra_mem_start + xen_extra_mem_size));
  386. for (pfn = PFN_UP(xen_extra_mem_start);
  387. pfn < extra_pfn_end;
  388. pfn++) {
  389. page = pfn_to_page(pfn);
  390. /* totalram_pages doesn't include the boot-time
  391. balloon extension, so don't subtract from it. */
  392. __balloon_append(page);
  393. }
  394. return 0;
  395. }
  396. subsys_initcall(balloon_init);
  397. MODULE_LICENSE("GPL");