balloon.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. } else {
  102. list_add(&page->lru, &ballooned_pages);
  103. balloon_stats.balloon_low++;
  104. }
  105. }
  106. static void balloon_append(struct page *page)
  107. {
  108. __balloon_append(page);
  109. if (PageHighMem(page))
  110. dec_totalhigh_pages();
  111. totalram_pages--;
  112. }
  113. /* balloon_retrieve: rescue a page from the balloon, if it is not empty. */
  114. static struct page *balloon_retrieve(bool prefer_highmem)
  115. {
  116. struct page *page;
  117. if (list_empty(&ballooned_pages))
  118. return NULL;
  119. if (prefer_highmem)
  120. page = list_entry(ballooned_pages.prev, struct page, lru);
  121. else
  122. page = list_entry(ballooned_pages.next, struct page, lru);
  123. list_del(&page->lru);
  124. if (PageHighMem(page)) {
  125. balloon_stats.balloon_high--;
  126. inc_totalhigh_pages();
  127. }
  128. else
  129. balloon_stats.balloon_low--;
  130. totalram_pages++;
  131. return page;
  132. }
  133. static struct page *balloon_first_page(void)
  134. {
  135. if (list_empty(&ballooned_pages))
  136. return NULL;
  137. return list_entry(ballooned_pages.next, struct page, lru);
  138. }
  139. static struct page *balloon_next_page(struct page *page)
  140. {
  141. struct list_head *next = page->lru.next;
  142. if (next == &ballooned_pages)
  143. return NULL;
  144. return list_entry(next, struct page, lru);
  145. }
  146. static enum bp_state update_schedule(enum bp_state state)
  147. {
  148. if (state == BP_DONE) {
  149. balloon_stats.schedule_delay = 1;
  150. balloon_stats.retry_count = 1;
  151. return BP_DONE;
  152. }
  153. ++balloon_stats.retry_count;
  154. if (balloon_stats.max_retry_count != RETRY_UNLIMITED &&
  155. balloon_stats.retry_count > balloon_stats.max_retry_count) {
  156. balloon_stats.schedule_delay = 1;
  157. balloon_stats.retry_count = 1;
  158. return BP_ECANCELED;
  159. }
  160. balloon_stats.schedule_delay <<= 1;
  161. if (balloon_stats.schedule_delay > balloon_stats.max_schedule_delay)
  162. balloon_stats.schedule_delay = balloon_stats.max_schedule_delay;
  163. return BP_EAGAIN;
  164. }
  165. static long current_credit(void)
  166. {
  167. unsigned long target = balloon_stats.target_pages;
  168. target = min(target,
  169. balloon_stats.current_pages +
  170. balloon_stats.balloon_low +
  171. balloon_stats.balloon_high);
  172. return target - balloon_stats.current_pages;
  173. }
  174. static enum bp_state increase_reservation(unsigned long nr_pages)
  175. {
  176. int rc;
  177. unsigned long pfn, i;
  178. struct page *page;
  179. struct xen_memory_reservation reservation = {
  180. .address_bits = 0,
  181. .extent_order = 0,
  182. .domid = DOMID_SELF
  183. };
  184. if (nr_pages > ARRAY_SIZE(frame_list))
  185. nr_pages = ARRAY_SIZE(frame_list);
  186. page = balloon_first_page();
  187. for (i = 0; i < nr_pages; i++) {
  188. if (!page) {
  189. nr_pages = i;
  190. break;
  191. }
  192. frame_list[i] = page_to_pfn(page);
  193. page = balloon_next_page(page);
  194. }
  195. set_xen_guest_handle(reservation.extent_start, frame_list);
  196. reservation.nr_extents = nr_pages;
  197. rc = HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation);
  198. if (rc <= 0)
  199. return BP_EAGAIN;
  200. for (i = 0; i < rc; i++) {
  201. page = balloon_retrieve(false);
  202. BUG_ON(page == NULL);
  203. pfn = page_to_pfn(page);
  204. BUG_ON(!xen_feature(XENFEAT_auto_translated_physmap) &&
  205. phys_to_machine_mapping_valid(pfn));
  206. set_phys_to_machine(pfn, frame_list[i]);
  207. /* Link back into the page tables if not highmem. */
  208. if (xen_pv_domain() && !PageHighMem(page)) {
  209. int ret;
  210. ret = HYPERVISOR_update_va_mapping(
  211. (unsigned long)__va(pfn << PAGE_SHIFT),
  212. mfn_pte(frame_list[i], PAGE_KERNEL),
  213. 0);
  214. BUG_ON(ret);
  215. }
  216. /* Relinquish the page back to the allocator. */
  217. ClearPageReserved(page);
  218. init_page_count(page);
  219. __free_page(page);
  220. }
  221. balloon_stats.current_pages += rc;
  222. return BP_DONE;
  223. }
  224. static enum bp_state decrease_reservation(unsigned long nr_pages, gfp_t gfp)
  225. {
  226. enum bp_state state = BP_DONE;
  227. unsigned long pfn, i;
  228. struct page *page;
  229. int ret;
  230. struct xen_memory_reservation reservation = {
  231. .address_bits = 0,
  232. .extent_order = 0,
  233. .domid = DOMID_SELF
  234. };
  235. if (nr_pages > ARRAY_SIZE(frame_list))
  236. nr_pages = ARRAY_SIZE(frame_list);
  237. for (i = 0; i < nr_pages; i++) {
  238. if ((page = alloc_page(gfp)) == NULL) {
  239. nr_pages = i;
  240. state = BP_EAGAIN;
  241. break;
  242. }
  243. pfn = page_to_pfn(page);
  244. frame_list[i] = pfn_to_mfn(pfn);
  245. scrub_page(page);
  246. if (xen_pv_domain() && !PageHighMem(page)) {
  247. ret = HYPERVISOR_update_va_mapping(
  248. (unsigned long)__va(pfn << PAGE_SHIFT),
  249. __pte_ma(0), 0);
  250. BUG_ON(ret);
  251. }
  252. }
  253. /* Ensure that ballooned highmem pages don't have kmaps. */
  254. kmap_flush_unused();
  255. flush_tlb_all();
  256. /* No more mappings: invalidate P2M and add to balloon. */
  257. for (i = 0; i < nr_pages; i++) {
  258. pfn = mfn_to_pfn(frame_list[i]);
  259. __set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
  260. balloon_append(pfn_to_page(pfn));
  261. }
  262. set_xen_guest_handle(reservation.extent_start, frame_list);
  263. reservation.nr_extents = nr_pages;
  264. ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
  265. BUG_ON(ret != nr_pages);
  266. balloon_stats.current_pages -= nr_pages;
  267. return state;
  268. }
  269. /*
  270. * We avoid multiple worker processes conflicting via the balloon mutex.
  271. * We may of course race updates of the target counts (which are protected
  272. * by the balloon lock), or with changes to the Xen hard limit, but we will
  273. * recover from these in time.
  274. */
  275. static void balloon_process(struct work_struct *work)
  276. {
  277. enum bp_state state = BP_DONE;
  278. long credit;
  279. mutex_lock(&balloon_mutex);
  280. do {
  281. credit = current_credit();
  282. if (credit > 0)
  283. state = increase_reservation(credit);
  284. if (credit < 0)
  285. state = decrease_reservation(-credit, GFP_BALLOON);
  286. state = update_schedule(state);
  287. #ifndef CONFIG_PREEMPT
  288. if (need_resched())
  289. schedule();
  290. #endif
  291. } while (credit && state == BP_DONE);
  292. /* Schedule more work if there is some still to be done. */
  293. if (state == BP_EAGAIN)
  294. schedule_delayed_work(&balloon_worker, balloon_stats.schedule_delay * HZ);
  295. mutex_unlock(&balloon_mutex);
  296. }
  297. /* Resets the Xen limit, sets new target, and kicks off processing. */
  298. void balloon_set_new_target(unsigned long target)
  299. {
  300. /* No need for lock. Not read-modify-write updates. */
  301. balloon_stats.target_pages = target;
  302. schedule_delayed_work(&balloon_worker, 0);
  303. }
  304. EXPORT_SYMBOL_GPL(balloon_set_new_target);
  305. /**
  306. * alloc_xenballooned_pages - get pages that have been ballooned out
  307. * @nr_pages: Number of pages to get
  308. * @pages: pages returned
  309. * @return 0 on success, error otherwise
  310. */
  311. int alloc_xenballooned_pages(int nr_pages, struct page** pages)
  312. {
  313. int pgno = 0;
  314. struct page* page;
  315. mutex_lock(&balloon_mutex);
  316. while (pgno < nr_pages) {
  317. page = balloon_retrieve(true);
  318. if (page) {
  319. pages[pgno++] = page;
  320. } else {
  321. enum bp_state st;
  322. st = decrease_reservation(nr_pages - pgno, GFP_HIGHUSER);
  323. if (st != BP_DONE)
  324. goto out_undo;
  325. }
  326. }
  327. mutex_unlock(&balloon_mutex);
  328. return 0;
  329. out_undo:
  330. while (pgno)
  331. balloon_append(pages[--pgno]);
  332. /* Free the memory back to the kernel soon */
  333. schedule_delayed_work(&balloon_worker, 0);
  334. mutex_unlock(&balloon_mutex);
  335. return -ENOMEM;
  336. }
  337. EXPORT_SYMBOL(alloc_xenballooned_pages);
  338. /**
  339. * free_xenballooned_pages - return pages retrieved with get_ballooned_pages
  340. * @nr_pages: Number of pages
  341. * @pages: pages to return
  342. */
  343. void free_xenballooned_pages(int nr_pages, struct page** pages)
  344. {
  345. int i;
  346. mutex_lock(&balloon_mutex);
  347. for (i = 0; i < nr_pages; i++) {
  348. if (pages[i])
  349. balloon_append(pages[i]);
  350. }
  351. /* The balloon may be too large now. Shrink it if needed. */
  352. if (current_credit())
  353. schedule_delayed_work(&balloon_worker, 0);
  354. mutex_unlock(&balloon_mutex);
  355. }
  356. EXPORT_SYMBOL(free_xenballooned_pages);
  357. static int __init balloon_init(void)
  358. {
  359. unsigned long pfn, extra_pfn_end;
  360. struct page *page;
  361. if (!xen_domain())
  362. return -ENODEV;
  363. pr_info("xen/balloon: Initialising balloon driver.\n");
  364. balloon_stats.current_pages = xen_pv_domain() ? min(xen_start_info->nr_pages, max_pfn) : max_pfn;
  365. balloon_stats.target_pages = balloon_stats.current_pages;
  366. balloon_stats.balloon_low = 0;
  367. balloon_stats.balloon_high = 0;
  368. balloon_stats.schedule_delay = 1;
  369. balloon_stats.max_schedule_delay = 32;
  370. balloon_stats.retry_count = 1;
  371. balloon_stats.max_retry_count = RETRY_UNLIMITED;
  372. /*
  373. * Initialise the balloon with excess memory space. We need
  374. * to make sure we don't add memory which doesn't exist or
  375. * logically exist. The E820 map can be trimmed to be smaller
  376. * than the amount of physical memory due to the mem= command
  377. * line parameter. And if this is a 32-bit non-HIGHMEM kernel
  378. * on a system with memory which requires highmem to access,
  379. * don't try to use it.
  380. */
  381. extra_pfn_end = min(min(max_pfn, e820_end_of_ram_pfn()),
  382. (unsigned long)PFN_DOWN(xen_extra_mem_start + xen_extra_mem_size));
  383. for (pfn = PFN_UP(xen_extra_mem_start);
  384. pfn < extra_pfn_end;
  385. pfn++) {
  386. page = pfn_to_page(pfn);
  387. /* totalram_pages and totalhigh_pages do not include the boot-time
  388. balloon extension, so don't subtract from it. */
  389. __balloon_append(page);
  390. }
  391. return 0;
  392. }
  393. subsys_initcall(balloon_init);
  394. MODULE_LICENSE("GPL");