balloon.c 19 KB

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