balloon.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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/cred.h>
  43. #include <linux/errno.h>
  44. #include <linux/freezer.h>
  45. #include <linux/kthread.h>
  46. #include <linux/mm.h>
  47. #include <linux/memblock.h>
  48. #include <linux/pagemap.h>
  49. #include <linux/highmem.h>
  50. #include <linux/mutex.h>
  51. #include <linux/list.h>
  52. #include <linux/gfp.h>
  53. #include <linux/notifier.h>
  54. #include <linux/memory.h>
  55. #include <linux/memory_hotplug.h>
  56. #include <linux/percpu-defs.h>
  57. #include <linux/slab.h>
  58. #include <linux/sysctl.h>
  59. #include <linux/moduleparam.h>
  60. #include <linux/jiffies.h>
  61. #include <asm/page.h>
  62. #include <asm/tlb.h>
  63. #include <asm/xen/hypervisor.h>
  64. #include <asm/xen/hypercall.h>
  65. #include <xen/xen.h>
  66. #include <xen/interface/xen.h>
  67. #include <xen/interface/memory.h>
  68. #include <xen/balloon.h>
  69. #include <xen/features.h>
  70. #include <xen/page.h>
  71. #include <xen/mem-reservation.h>
  72. #undef MODULE_PARAM_PREFIX
  73. #define MODULE_PARAM_PREFIX "xen."
  74. static uint __read_mostly balloon_boot_timeout = 180;
  75. module_param(balloon_boot_timeout, uint, 0444);
  76. #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
  77. static int xen_hotplug_unpopulated;
  78. static struct ctl_table balloon_table[] = {
  79. {
  80. .procname = "hotplug_unpopulated",
  81. .data = &xen_hotplug_unpopulated,
  82. .maxlen = sizeof(int),
  83. .mode = 0644,
  84. .proc_handler = proc_dointvec_minmax,
  85. .extra1 = SYSCTL_ZERO,
  86. .extra2 = SYSCTL_ONE,
  87. },
  88. { }
  89. };
  90. static struct ctl_table balloon_root[] = {
  91. {
  92. .procname = "balloon",
  93. .mode = 0555,
  94. .child = balloon_table,
  95. },
  96. { }
  97. };
  98. static struct ctl_table xen_root[] = {
  99. {
  100. .procname = "xen",
  101. .mode = 0555,
  102. .child = balloon_root,
  103. },
  104. { }
  105. };
  106. #else
  107. #define xen_hotplug_unpopulated 0
  108. #endif
  109. /*
  110. * Use one extent per PAGE_SIZE to avoid to break down the page into
  111. * multiple frame.
  112. */
  113. #define EXTENT_ORDER (fls(XEN_PFN_PER_PAGE) - 1)
  114. /*
  115. * balloon_thread() state:
  116. *
  117. * BP_DONE: done or nothing to do,
  118. * BP_WAIT: wait to be rescheduled,
  119. * BP_EAGAIN: error, go to sleep,
  120. * BP_ECANCELED: error, balloon operation canceled.
  121. */
  122. static enum bp_state {
  123. BP_DONE,
  124. BP_WAIT,
  125. BP_EAGAIN,
  126. BP_ECANCELED
  127. } balloon_state = BP_DONE;
  128. /* Main waiting point for xen-balloon thread. */
  129. static DECLARE_WAIT_QUEUE_HEAD(balloon_thread_wq);
  130. static DEFINE_MUTEX(balloon_mutex);
  131. struct balloon_stats balloon_stats;
  132. EXPORT_SYMBOL_GPL(balloon_stats);
  133. /* We increase/decrease in batches which fit in a page */
  134. static xen_pfn_t frame_list[PAGE_SIZE / sizeof(xen_pfn_t)];
  135. /* List of ballooned pages, threaded through the mem_map array. */
  136. static LIST_HEAD(ballooned_pages);
  137. static DECLARE_WAIT_QUEUE_HEAD(balloon_wq);
  138. /* When ballooning out (allocating memory to return to Xen) we don't really
  139. want the kernel to try too hard since that can trigger the oom killer. */
  140. #define GFP_BALLOON \
  141. (GFP_HIGHUSER | __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC)
  142. /* balloon_append: add the given page to the balloon. */
  143. static void balloon_append(struct page *page)
  144. {
  145. __SetPageOffline(page);
  146. /* Lowmem is re-populated first, so highmem pages go at list tail. */
  147. if (PageHighMem(page)) {
  148. list_add_tail(&page->lru, &ballooned_pages);
  149. balloon_stats.balloon_high++;
  150. } else {
  151. list_add(&page->lru, &ballooned_pages);
  152. balloon_stats.balloon_low++;
  153. }
  154. wake_up(&balloon_wq);
  155. }
  156. /* balloon_retrieve: rescue a page from the balloon, if it is not empty. */
  157. static struct page *balloon_retrieve(bool require_lowmem)
  158. {
  159. struct page *page;
  160. if (list_empty(&ballooned_pages))
  161. return NULL;
  162. page = list_entry(ballooned_pages.next, struct page, lru);
  163. if (require_lowmem && PageHighMem(page))
  164. return NULL;
  165. list_del(&page->lru);
  166. if (PageHighMem(page))
  167. balloon_stats.balloon_high--;
  168. else
  169. balloon_stats.balloon_low--;
  170. __ClearPageOffline(page);
  171. return page;
  172. }
  173. static struct page *balloon_next_page(struct page *page)
  174. {
  175. struct list_head *next = page->lru.next;
  176. if (next == &ballooned_pages)
  177. return NULL;
  178. return list_entry(next, struct page, lru);
  179. }
  180. static void update_schedule(void)
  181. {
  182. if (balloon_state == BP_WAIT || balloon_state == BP_ECANCELED)
  183. return;
  184. if (balloon_state == BP_DONE) {
  185. balloon_stats.schedule_delay = 1;
  186. balloon_stats.retry_count = 1;
  187. return;
  188. }
  189. ++balloon_stats.retry_count;
  190. if (balloon_stats.max_retry_count != RETRY_UNLIMITED &&
  191. balloon_stats.retry_count > balloon_stats.max_retry_count) {
  192. balloon_stats.schedule_delay = 1;
  193. balloon_stats.retry_count = 1;
  194. balloon_state = BP_ECANCELED;
  195. return;
  196. }
  197. balloon_stats.schedule_delay <<= 1;
  198. if (balloon_stats.schedule_delay > balloon_stats.max_schedule_delay)
  199. balloon_stats.schedule_delay = balloon_stats.max_schedule_delay;
  200. balloon_state = BP_EAGAIN;
  201. }
  202. #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
  203. static void release_memory_resource(struct resource *resource)
  204. {
  205. if (!resource)
  206. return;
  207. /*
  208. * No need to reset region to identity mapped since we now
  209. * know that no I/O can be in this region
  210. */
  211. release_resource(resource);
  212. kfree(resource);
  213. }
  214. static struct resource *additional_memory_resource(phys_addr_t size)
  215. {
  216. struct resource *res;
  217. int ret;
  218. res = kzalloc(sizeof(*res), GFP_KERNEL);
  219. if (!res)
  220. return NULL;
  221. res->name = "System RAM";
  222. res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  223. ret = allocate_resource(&iomem_resource, res,
  224. size, 0, -1,
  225. PAGES_PER_SECTION * PAGE_SIZE, NULL, NULL);
  226. if (ret < 0) {
  227. pr_err("Cannot allocate new System RAM resource\n");
  228. kfree(res);
  229. return NULL;
  230. }
  231. return res;
  232. }
  233. static enum bp_state reserve_additional_memory(void)
  234. {
  235. long credit;
  236. struct resource *resource;
  237. int nid, rc;
  238. unsigned long balloon_hotplug;
  239. credit = balloon_stats.target_pages + balloon_stats.target_unpopulated
  240. - balloon_stats.total_pages;
  241. /*
  242. * Already hotplugged enough pages? Wait for them to be
  243. * onlined.
  244. */
  245. if (credit <= 0)
  246. return BP_WAIT;
  247. balloon_hotplug = round_up(credit, PAGES_PER_SECTION);
  248. resource = additional_memory_resource(balloon_hotplug * PAGE_SIZE);
  249. if (!resource)
  250. goto err;
  251. nid = memory_add_physaddr_to_nid(resource->start);
  252. #ifdef CONFIG_XEN_HAVE_PVMMU
  253. /*
  254. * We don't support PV MMU when Linux and Xen is using
  255. * different page granularity.
  256. */
  257. BUILD_BUG_ON(XEN_PAGE_SIZE != PAGE_SIZE);
  258. /*
  259. * add_memory() will build page tables for the new memory so
  260. * the p2m must contain invalid entries so the correct
  261. * non-present PTEs will be written.
  262. *
  263. * If a failure occurs, the original (identity) p2m entries
  264. * are not restored since this region is now known not to
  265. * conflict with any devices.
  266. */
  267. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  268. unsigned long pfn, i;
  269. pfn = PFN_DOWN(resource->start);
  270. for (i = 0; i < balloon_hotplug; i++) {
  271. if (!set_phys_to_machine(pfn + i, INVALID_P2M_ENTRY)) {
  272. pr_warn("set_phys_to_machine() failed, no memory added\n");
  273. goto err;
  274. }
  275. }
  276. }
  277. #endif
  278. /*
  279. * add_memory_resource() will call online_pages() which in its turn
  280. * will call xen_online_page() callback causing deadlock if we don't
  281. * release balloon_mutex here. Unlocking here is safe because the
  282. * callers drop the mutex before trying again.
  283. */
  284. mutex_unlock(&balloon_mutex);
  285. /* add_memory_resource() requires the device_hotplug lock */
  286. lock_device_hotplug();
  287. rc = add_memory_resource(nid, resource, MHP_MERGE_RESOURCE);
  288. unlock_device_hotplug();
  289. mutex_lock(&balloon_mutex);
  290. if (rc) {
  291. pr_warn("Cannot add additional memory (%i)\n", rc);
  292. goto err;
  293. }
  294. balloon_stats.total_pages += balloon_hotplug;
  295. return BP_WAIT;
  296. err:
  297. release_memory_resource(resource);
  298. return BP_ECANCELED;
  299. }
  300. static void xen_online_page(struct page *page, unsigned int order)
  301. {
  302. unsigned long i, size = (1 << order);
  303. unsigned long start_pfn = page_to_pfn(page);
  304. struct page *p;
  305. pr_debug("Online %lu pages starting at pfn 0x%lx\n", size, start_pfn);
  306. mutex_lock(&balloon_mutex);
  307. for (i = 0; i < size; i++) {
  308. p = pfn_to_page(start_pfn + i);
  309. balloon_append(p);
  310. }
  311. mutex_unlock(&balloon_mutex);
  312. }
  313. static int xen_memory_notifier(struct notifier_block *nb, unsigned long val, void *v)
  314. {
  315. if (val == MEM_ONLINE)
  316. wake_up(&balloon_thread_wq);
  317. return NOTIFY_OK;
  318. }
  319. static struct notifier_block xen_memory_nb = {
  320. .notifier_call = xen_memory_notifier,
  321. .priority = 0
  322. };
  323. #else
  324. static enum bp_state reserve_additional_memory(void)
  325. {
  326. balloon_stats.target_pages = balloon_stats.current_pages +
  327. balloon_stats.target_unpopulated;
  328. return BP_ECANCELED;
  329. }
  330. #endif /* CONFIG_XEN_BALLOON_MEMORY_HOTPLUG */
  331. static long current_credit(void)
  332. {
  333. return balloon_stats.target_pages - balloon_stats.current_pages;
  334. }
  335. static bool balloon_is_inflated(void)
  336. {
  337. return balloon_stats.balloon_low || balloon_stats.balloon_high;
  338. }
  339. static enum bp_state increase_reservation(unsigned long nr_pages)
  340. {
  341. int rc;
  342. unsigned long i;
  343. struct page *page;
  344. if (nr_pages > ARRAY_SIZE(frame_list))
  345. nr_pages = ARRAY_SIZE(frame_list);
  346. page = list_first_entry_or_null(&ballooned_pages, struct page, lru);
  347. for (i = 0; i < nr_pages; i++) {
  348. if (!page) {
  349. nr_pages = i;
  350. break;
  351. }
  352. frame_list[i] = page_to_xen_pfn(page);
  353. page = balloon_next_page(page);
  354. }
  355. rc = xenmem_reservation_increase(nr_pages, frame_list);
  356. if (rc <= 0)
  357. return BP_EAGAIN;
  358. for (i = 0; i < rc; i++) {
  359. page = balloon_retrieve(false);
  360. BUG_ON(page == NULL);
  361. xenmem_reservation_va_mapping_update(1, &page, &frame_list[i]);
  362. /* Relinquish the page back to the allocator. */
  363. free_reserved_page(page);
  364. }
  365. balloon_stats.current_pages += rc;
  366. return BP_DONE;
  367. }
  368. static enum bp_state decrease_reservation(unsigned long nr_pages, gfp_t gfp)
  369. {
  370. enum bp_state state = BP_DONE;
  371. unsigned long i;
  372. struct page *page, *tmp;
  373. int ret;
  374. LIST_HEAD(pages);
  375. if (nr_pages > ARRAY_SIZE(frame_list))
  376. nr_pages = ARRAY_SIZE(frame_list);
  377. for (i = 0; i < nr_pages; i++) {
  378. page = alloc_page(gfp);
  379. if (page == NULL) {
  380. nr_pages = i;
  381. state = BP_EAGAIN;
  382. break;
  383. }
  384. adjust_managed_page_count(page, -1);
  385. xenmem_reservation_scrub_page(page);
  386. list_add(&page->lru, &pages);
  387. }
  388. /*
  389. * Ensure that ballooned highmem pages don't have kmaps.
  390. *
  391. * Do this before changing the p2m as kmap_flush_unused()
  392. * reads PTEs to obtain pages (and hence needs the original
  393. * p2m entry).
  394. */
  395. kmap_flush_unused();
  396. /*
  397. * Setup the frame, update direct mapping, invalidate P2M,
  398. * and add to balloon.
  399. */
  400. i = 0;
  401. list_for_each_entry_safe(page, tmp, &pages, lru) {
  402. frame_list[i++] = xen_page_to_gfn(page);
  403. xenmem_reservation_va_mapping_reset(1, &page);
  404. list_del(&page->lru);
  405. balloon_append(page);
  406. }
  407. flush_tlb_all();
  408. ret = xenmem_reservation_decrease(nr_pages, frame_list);
  409. BUG_ON(ret != nr_pages);
  410. balloon_stats.current_pages -= nr_pages;
  411. return state;
  412. }
  413. /*
  414. * Stop waiting if either state is BP_DONE and ballooning action is
  415. * needed, or if the credit has changed while state is not BP_DONE.
  416. */
  417. static bool balloon_thread_cond(long credit)
  418. {
  419. if (balloon_state == BP_DONE)
  420. credit = 0;
  421. return current_credit() != credit || kthread_should_stop();
  422. }
  423. /*
  424. * As this is a kthread it is guaranteed to run as a single instance only.
  425. * We may of course race updates of the target counts (which are protected
  426. * by the balloon lock), or with changes to the Xen hard limit, but we will
  427. * recover from these in time.
  428. */
  429. static int balloon_thread(void *unused)
  430. {
  431. long credit;
  432. unsigned long timeout;
  433. set_freezable();
  434. for (;;) {
  435. switch (balloon_state) {
  436. case BP_DONE:
  437. case BP_ECANCELED:
  438. timeout = 3600 * HZ;
  439. break;
  440. case BP_EAGAIN:
  441. timeout = balloon_stats.schedule_delay * HZ;
  442. break;
  443. case BP_WAIT:
  444. timeout = HZ;
  445. break;
  446. }
  447. credit = current_credit();
  448. wait_event_freezable_timeout(balloon_thread_wq,
  449. balloon_thread_cond(credit), timeout);
  450. if (kthread_should_stop())
  451. return 0;
  452. mutex_lock(&balloon_mutex);
  453. credit = current_credit();
  454. if (credit > 0) {
  455. if (balloon_is_inflated())
  456. balloon_state = increase_reservation(credit);
  457. else
  458. balloon_state = reserve_additional_memory();
  459. }
  460. if (credit < 0) {
  461. long n_pages;
  462. n_pages = min(-credit, si_mem_available());
  463. balloon_state = decrease_reservation(n_pages,
  464. GFP_BALLOON);
  465. if (balloon_state == BP_DONE && n_pages != -credit &&
  466. n_pages < totalreserve_pages)
  467. balloon_state = BP_EAGAIN;
  468. }
  469. update_schedule();
  470. mutex_unlock(&balloon_mutex);
  471. cond_resched();
  472. }
  473. }
  474. /* Resets the Xen limit, sets new target, and kicks off processing. */
  475. void balloon_set_new_target(unsigned long target)
  476. {
  477. /* No need for lock. Not read-modify-write updates. */
  478. balloon_stats.target_pages = target;
  479. wake_up(&balloon_thread_wq);
  480. }
  481. EXPORT_SYMBOL_GPL(balloon_set_new_target);
  482. static int add_ballooned_pages(unsigned int nr_pages)
  483. {
  484. enum bp_state st;
  485. if (xen_hotplug_unpopulated) {
  486. st = reserve_additional_memory();
  487. if (st != BP_ECANCELED) {
  488. int rc;
  489. mutex_unlock(&balloon_mutex);
  490. rc = wait_event_interruptible(balloon_wq,
  491. !list_empty(&ballooned_pages));
  492. mutex_lock(&balloon_mutex);
  493. return rc ? -ENOMEM : 0;
  494. }
  495. }
  496. if (si_mem_available() < nr_pages)
  497. return -ENOMEM;
  498. st = decrease_reservation(nr_pages, GFP_USER);
  499. if (st != BP_DONE)
  500. return -ENOMEM;
  501. return 0;
  502. }
  503. /**
  504. * xen_alloc_ballooned_pages - get pages that have been ballooned out
  505. * @nr_pages: Number of pages to get
  506. * @pages: pages returned
  507. * @return 0 on success, error otherwise
  508. */
  509. int xen_alloc_ballooned_pages(unsigned int nr_pages, struct page **pages)
  510. {
  511. unsigned int pgno = 0;
  512. struct page *page;
  513. int ret;
  514. mutex_lock(&balloon_mutex);
  515. balloon_stats.target_unpopulated += nr_pages;
  516. while (pgno < nr_pages) {
  517. page = balloon_retrieve(true);
  518. if (page) {
  519. pages[pgno++] = page;
  520. #ifdef CONFIG_XEN_HAVE_PVMMU
  521. /*
  522. * We don't support PV MMU when Linux and Xen is using
  523. * different page granularity.
  524. */
  525. BUILD_BUG_ON(XEN_PAGE_SIZE != PAGE_SIZE);
  526. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  527. ret = xen_alloc_p2m_entry(page_to_pfn(page));
  528. if (ret < 0)
  529. goto out_undo;
  530. }
  531. #endif
  532. } else {
  533. ret = add_ballooned_pages(nr_pages - pgno);
  534. if (ret < 0)
  535. goto out_undo;
  536. }
  537. }
  538. mutex_unlock(&balloon_mutex);
  539. return 0;
  540. out_undo:
  541. mutex_unlock(&balloon_mutex);
  542. xen_free_ballooned_pages(pgno, pages);
  543. /*
  544. * NB: xen_free_ballooned_pages will only subtract pgno pages, but since
  545. * target_unpopulated is incremented with nr_pages at the start we need
  546. * to remove the remaining ones also, or accounting will be screwed.
  547. */
  548. balloon_stats.target_unpopulated -= nr_pages - pgno;
  549. return ret;
  550. }
  551. EXPORT_SYMBOL(xen_alloc_ballooned_pages);
  552. /**
  553. * xen_free_ballooned_pages - return pages retrieved with get_ballooned_pages
  554. * @nr_pages: Number of pages
  555. * @pages: pages to return
  556. */
  557. void xen_free_ballooned_pages(unsigned int nr_pages, struct page **pages)
  558. {
  559. unsigned int i;
  560. mutex_lock(&balloon_mutex);
  561. for (i = 0; i < nr_pages; i++) {
  562. if (pages[i])
  563. balloon_append(pages[i]);
  564. }
  565. balloon_stats.target_unpopulated -= nr_pages;
  566. /* The balloon may be too large now. Shrink it if needed. */
  567. if (current_credit())
  568. wake_up(&balloon_thread_wq);
  569. mutex_unlock(&balloon_mutex);
  570. }
  571. EXPORT_SYMBOL(xen_free_ballooned_pages);
  572. static void __init balloon_add_regions(void)
  573. {
  574. #if defined(CONFIG_XEN_PV)
  575. unsigned long start_pfn, pages;
  576. unsigned long pfn, extra_pfn_end;
  577. unsigned int i;
  578. for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
  579. pages = xen_extra_mem[i].n_pfns;
  580. if (!pages)
  581. continue;
  582. start_pfn = xen_extra_mem[i].start_pfn;
  583. /*
  584. * If the amount of usable memory has been limited (e.g., with
  585. * the 'mem' command line parameter), don't add pages beyond
  586. * this limit.
  587. */
  588. extra_pfn_end = min(max_pfn, start_pfn + pages);
  589. for (pfn = start_pfn; pfn < extra_pfn_end; pfn++)
  590. balloon_append(pfn_to_page(pfn));
  591. balloon_stats.total_pages += extra_pfn_end - start_pfn;
  592. }
  593. #endif
  594. }
  595. static int __init balloon_init(void)
  596. {
  597. struct task_struct *task;
  598. if (!xen_domain())
  599. return -ENODEV;
  600. pr_info("Initialising balloon driver\n");
  601. #ifdef CONFIG_XEN_PV
  602. balloon_stats.current_pages = xen_pv_domain()
  603. ? min(xen_start_info->nr_pages - xen_released_pages, max_pfn)
  604. : get_num_physpages();
  605. #else
  606. balloon_stats.current_pages = get_num_physpages();
  607. #endif
  608. balloon_stats.target_pages = balloon_stats.current_pages;
  609. balloon_stats.balloon_low = 0;
  610. balloon_stats.balloon_high = 0;
  611. balloon_stats.total_pages = balloon_stats.current_pages;
  612. balloon_stats.schedule_delay = 1;
  613. balloon_stats.max_schedule_delay = 32;
  614. balloon_stats.retry_count = 1;
  615. balloon_stats.max_retry_count = 4;
  616. #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
  617. set_online_page_callback(&xen_online_page);
  618. register_memory_notifier(&xen_memory_nb);
  619. register_sysctl_table(xen_root);
  620. #endif
  621. balloon_add_regions();
  622. task = kthread_run(balloon_thread, NULL, "xen-balloon");
  623. if (IS_ERR(task)) {
  624. pr_err("xen-balloon thread could not be started, ballooning will not work!\n");
  625. return PTR_ERR(task);
  626. }
  627. /* Init the xen-balloon driver. */
  628. xen_balloon_init();
  629. return 0;
  630. }
  631. subsys_initcall(balloon_init);
  632. static int __init balloon_wait_finish(void)
  633. {
  634. long credit, last_credit = 0;
  635. unsigned long last_changed = 0;
  636. if (!xen_domain())
  637. return -ENODEV;
  638. /* PV guests don't need to wait. */
  639. if (xen_pv_domain() || !current_credit())
  640. return 0;
  641. pr_notice("Waiting for initial ballooning down having finished.\n");
  642. while ((credit = current_credit()) < 0) {
  643. if (credit != last_credit) {
  644. last_changed = jiffies;
  645. last_credit = credit;
  646. }
  647. if (balloon_state == BP_ECANCELED) {
  648. pr_warn_once("Initial ballooning failed, %ld pages need to be freed.\n",
  649. -credit);
  650. if (time_is_before_eq_jiffies(last_changed + HZ * balloon_boot_timeout))
  651. panic("Initial ballooning failed!\n");
  652. }
  653. schedule_timeout_interruptible(HZ / 10);
  654. }
  655. pr_notice("Initial ballooning down finished.\n");
  656. return 0;
  657. }
  658. late_initcall_sync(balloon_wait_finish);