balloon.c 19 KB

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