balloon.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /******************************************************************************
  2. * balloon.c
  3. *
  4. * Xen balloon driver - enables returning/claiming memory to/from Xen.
  5. *
  6. * Copyright (c) 2003, B Dragovic
  7. * Copyright (c) 2003-2004, M Williamson, K Fraser
  8. * Copyright (c) 2005 Dan M. Smith, IBM Corporation
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License version 2
  12. * as published by the Free Software Foundation; or, when distributed
  13. * separately from the Linux kernel or incorporated into other
  14. * software packages, subject to the following license:
  15. *
  16. * Permission is hereby granted, free of charge, to any person obtaining a copy
  17. * of this source file (the "Software"), to deal in the Software without
  18. * restriction, including without limitation the rights to use, copy, modify,
  19. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  20. * and to permit persons to whom the Software is furnished to do so, subject to
  21. * the following conditions:
  22. *
  23. * The above copyright notice and this permission notice shall be included in
  24. * all copies or substantial portions of the Software.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  29. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  30. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  31. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  32. * IN THE SOFTWARE.
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/sched.h>
  37. #include <linux/errno.h>
  38. #include <linux/mm.h>
  39. #include <linux/bootmem.h>
  40. #include <linux/pagemap.h>
  41. #include <linux/highmem.h>
  42. #include <linux/mutex.h>
  43. #include <linux/list.h>
  44. #include <linux/sysdev.h>
  45. #include <asm/page.h>
  46. #include <asm/pgalloc.h>
  47. #include <asm/pgtable.h>
  48. #include <asm/uaccess.h>
  49. #include <asm/tlb.h>
  50. #include <asm/xen/hypervisor.h>
  51. #include <asm/xen/hypercall.h>
  52. #include <xen/interface/xen.h>
  53. #include <xen/interface/memory.h>
  54. #include <xen/xenbus.h>
  55. #include <xen/features.h>
  56. #include <xen/page.h>
  57. #define PAGES2KB(_p) ((_p)<<(PAGE_SHIFT-10))
  58. #define BALLOON_CLASS_NAME "xen_memory"
  59. struct balloon_stats {
  60. /* We aim for 'current allocation' == 'target allocation'. */
  61. unsigned long current_pages;
  62. unsigned long target_pages;
  63. /* We may hit the hard limit in Xen. If we do then we remember it. */
  64. unsigned long hard_limit;
  65. /*
  66. * Drivers may alter the memory reservation independently, but they
  67. * must inform the balloon driver so we avoid hitting the hard limit.
  68. */
  69. unsigned long driver_pages;
  70. /* Number of pages in high- and low-memory balloons. */
  71. unsigned long balloon_low;
  72. unsigned long balloon_high;
  73. };
  74. static DEFINE_MUTEX(balloon_mutex);
  75. static struct sys_device balloon_sysdev;
  76. static int register_balloon(struct sys_device *sysdev);
  77. /*
  78. * Protects atomic reservation decrease/increase against concurrent increases.
  79. * Also protects non-atomic updates of current_pages and driver_pages, and
  80. * balloon lists.
  81. */
  82. static DEFINE_SPINLOCK(balloon_lock);
  83. static struct balloon_stats balloon_stats;
  84. /* We increase/decrease in batches which fit in a page */
  85. static unsigned long frame_list[PAGE_SIZE / sizeof(unsigned long)];
  86. /* VM /proc information for memory */
  87. extern unsigned long totalram_pages;
  88. #ifdef CONFIG_HIGHMEM
  89. extern unsigned long totalhigh_pages;
  90. #define inc_totalhigh_pages() (totalhigh_pages++)
  91. #define dec_totalhigh_pages() (totalhigh_pages--)
  92. #else
  93. #define inc_totalhigh_pages() do {} while(0)
  94. #define dec_totalhigh_pages() do {} while(0)
  95. #endif
  96. /* List of ballooned pages, threaded through the mem_map array. */
  97. static LIST_HEAD(ballooned_pages);
  98. /* Main work function, always executed in process context. */
  99. static void balloon_process(struct work_struct *work);
  100. static DECLARE_WORK(balloon_worker, balloon_process);
  101. static struct timer_list balloon_timer;
  102. /* When ballooning out (allocating memory to return to Xen) we don't really
  103. want the kernel to try too hard since that can trigger the oom killer. */
  104. #define GFP_BALLOON \
  105. (GFP_HIGHUSER | __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC)
  106. static void scrub_page(struct page *page)
  107. {
  108. #ifdef CONFIG_XEN_SCRUB_PAGES
  109. clear_highpage(page);
  110. #endif
  111. }
  112. /* balloon_append: add the given page to the balloon. */
  113. static void balloon_append(struct page *page)
  114. {
  115. /* Lowmem is re-populated first, so highmem pages go at list tail. */
  116. if (PageHighMem(page)) {
  117. list_add_tail(&page->lru, &ballooned_pages);
  118. balloon_stats.balloon_high++;
  119. dec_totalhigh_pages();
  120. } else {
  121. list_add(&page->lru, &ballooned_pages);
  122. balloon_stats.balloon_low++;
  123. }
  124. }
  125. /* balloon_retrieve: rescue a page from the balloon, if it is not empty. */
  126. static struct page *balloon_retrieve(void)
  127. {
  128. struct page *page;
  129. if (list_empty(&ballooned_pages))
  130. return NULL;
  131. page = list_entry(ballooned_pages.next, struct page, lru);
  132. list_del(&page->lru);
  133. if (PageHighMem(page)) {
  134. balloon_stats.balloon_high--;
  135. inc_totalhigh_pages();
  136. }
  137. else
  138. balloon_stats.balloon_low--;
  139. return page;
  140. }
  141. static struct page *balloon_first_page(void)
  142. {
  143. if (list_empty(&ballooned_pages))
  144. return NULL;
  145. return list_entry(ballooned_pages.next, struct page, lru);
  146. }
  147. static struct page *balloon_next_page(struct page *page)
  148. {
  149. struct list_head *next = page->lru.next;
  150. if (next == &ballooned_pages)
  151. return NULL;
  152. return list_entry(next, struct page, lru);
  153. }
  154. static void balloon_alarm(unsigned long unused)
  155. {
  156. schedule_work(&balloon_worker);
  157. }
  158. static unsigned long current_target(void)
  159. {
  160. unsigned long target = min(balloon_stats.target_pages, balloon_stats.hard_limit);
  161. target = min(target,
  162. balloon_stats.current_pages +
  163. balloon_stats.balloon_low +
  164. balloon_stats.balloon_high);
  165. return target;
  166. }
  167. static int increase_reservation(unsigned long nr_pages)
  168. {
  169. unsigned long pfn, i, flags;
  170. struct page *page;
  171. long rc;
  172. struct xen_memory_reservation reservation = {
  173. .address_bits = 0,
  174. .extent_order = 0,
  175. .domid = DOMID_SELF
  176. };
  177. if (nr_pages > ARRAY_SIZE(frame_list))
  178. nr_pages = ARRAY_SIZE(frame_list);
  179. spin_lock_irqsave(&balloon_lock, flags);
  180. page = balloon_first_page();
  181. for (i = 0; i < nr_pages; i++) {
  182. BUG_ON(page == NULL);
  183. frame_list[i] = page_to_pfn(page);;
  184. page = balloon_next_page(page);
  185. }
  186. set_xen_guest_handle(reservation.extent_start, frame_list);
  187. reservation.nr_extents = nr_pages;
  188. rc = HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation);
  189. if (rc < nr_pages) {
  190. if (rc > 0) {
  191. int ret;
  192. /* We hit the Xen hard limit: reprobe. */
  193. reservation.nr_extents = rc;
  194. ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
  195. &reservation);
  196. BUG_ON(ret != rc);
  197. }
  198. if (rc >= 0)
  199. balloon_stats.hard_limit = (balloon_stats.current_pages + rc -
  200. balloon_stats.driver_pages);
  201. goto out;
  202. }
  203. for (i = 0; i < nr_pages; i++) {
  204. page = balloon_retrieve();
  205. BUG_ON(page == NULL);
  206. pfn = page_to_pfn(page);
  207. BUG_ON(!xen_feature(XENFEAT_auto_translated_physmap) &&
  208. phys_to_machine_mapping_valid(pfn));
  209. set_phys_to_machine(pfn, frame_list[i]);
  210. /* Link back into the page tables if not highmem. */
  211. if (pfn < max_low_pfn) {
  212. int ret;
  213. ret = HYPERVISOR_update_va_mapping(
  214. (unsigned long)__va(pfn << PAGE_SHIFT),
  215. mfn_pte(frame_list[i], PAGE_KERNEL),
  216. 0);
  217. BUG_ON(ret);
  218. }
  219. /* Relinquish the page back to the allocator. */
  220. ClearPageReserved(page);
  221. init_page_count(page);
  222. __free_page(page);
  223. }
  224. balloon_stats.current_pages += nr_pages;
  225. totalram_pages = balloon_stats.current_pages;
  226. out:
  227. spin_unlock_irqrestore(&balloon_lock, flags);
  228. return 0;
  229. }
  230. static int decrease_reservation(unsigned long nr_pages)
  231. {
  232. unsigned long pfn, i, flags;
  233. struct page *page;
  234. int need_sleep = 0;
  235. int ret;
  236. struct xen_memory_reservation reservation = {
  237. .address_bits = 0,
  238. .extent_order = 0,
  239. .domid = DOMID_SELF
  240. };
  241. if (nr_pages > ARRAY_SIZE(frame_list))
  242. nr_pages = ARRAY_SIZE(frame_list);
  243. for (i = 0; i < nr_pages; i++) {
  244. if ((page = alloc_page(GFP_BALLOON)) == NULL) {
  245. nr_pages = i;
  246. need_sleep = 1;
  247. break;
  248. }
  249. pfn = page_to_pfn(page);
  250. frame_list[i] = pfn_to_mfn(pfn);
  251. scrub_page(page);
  252. }
  253. /* Ensure that ballooned highmem pages don't have kmaps. */
  254. kmap_flush_unused();
  255. flush_tlb_all();
  256. spin_lock_irqsave(&balloon_lock, flags);
  257. /* No more mappings: invalidate P2M and add to balloon. */
  258. for (i = 0; i < nr_pages; i++) {
  259. pfn = mfn_to_pfn(frame_list[i]);
  260. set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
  261. balloon_append(pfn_to_page(pfn));
  262. }
  263. set_xen_guest_handle(reservation.extent_start, frame_list);
  264. reservation.nr_extents = nr_pages;
  265. ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
  266. BUG_ON(ret != nr_pages);
  267. balloon_stats.current_pages -= nr_pages;
  268. totalram_pages = balloon_stats.current_pages;
  269. spin_unlock_irqrestore(&balloon_lock, flags);
  270. return need_sleep;
  271. }
  272. /*
  273. * We avoid multiple worker processes conflicting via the balloon mutex.
  274. * We may of course race updates of the target counts (which are protected
  275. * by the balloon lock), or with changes to the Xen hard limit, but we will
  276. * recover from these in time.
  277. */
  278. static void balloon_process(struct work_struct *work)
  279. {
  280. int need_sleep = 0;
  281. long credit;
  282. mutex_lock(&balloon_mutex);
  283. do {
  284. credit = current_target() - balloon_stats.current_pages;
  285. if (credit > 0)
  286. need_sleep = (increase_reservation(credit) != 0);
  287. if (credit < 0)
  288. need_sleep = (decrease_reservation(-credit) != 0);
  289. #ifndef CONFIG_PREEMPT
  290. if (need_resched())
  291. schedule();
  292. #endif
  293. } while ((credit != 0) && !need_sleep);
  294. /* Schedule more work if there is some still to be done. */
  295. if (current_target() != balloon_stats.current_pages)
  296. mod_timer(&balloon_timer, jiffies + HZ);
  297. mutex_unlock(&balloon_mutex);
  298. }
  299. /* Resets the Xen limit, sets new target, and kicks off processing. */
  300. static void balloon_set_new_target(unsigned long target)
  301. {
  302. /* No need for lock. Not read-modify-write updates. */
  303. balloon_stats.hard_limit = ~0UL;
  304. balloon_stats.target_pages = target;
  305. schedule_work(&balloon_worker);
  306. }
  307. static struct xenbus_watch target_watch =
  308. {
  309. .node = "memory/target"
  310. };
  311. /* React to a change in the target key */
  312. static void watch_target(struct xenbus_watch *watch,
  313. const char **vec, unsigned int len)
  314. {
  315. unsigned long long new_target;
  316. int err;
  317. err = xenbus_scanf(XBT_NIL, "memory", "target", "%llu", &new_target);
  318. if (err != 1) {
  319. /* This is ok (for domain0 at least) - so just return */
  320. return;
  321. }
  322. /* The given memory/target value is in KiB, so it needs converting to
  323. * pages. PAGE_SHIFT converts bytes to pages, hence PAGE_SHIFT - 10.
  324. */
  325. balloon_set_new_target(new_target >> (PAGE_SHIFT - 10));
  326. }
  327. static int balloon_init_watcher(struct notifier_block *notifier,
  328. unsigned long event,
  329. void *data)
  330. {
  331. int err;
  332. err = register_xenbus_watch(&target_watch);
  333. if (err)
  334. printk(KERN_ERR "Failed to set balloon watcher\n");
  335. return NOTIFY_DONE;
  336. }
  337. static struct notifier_block xenstore_notifier;
  338. static int __init balloon_init(void)
  339. {
  340. unsigned long pfn;
  341. struct page *page;
  342. if (!xen_pv_domain())
  343. return -ENODEV;
  344. pr_info("xen_balloon: Initialising balloon driver.\n");
  345. balloon_stats.current_pages = min(xen_start_info->nr_pages, max_pfn);
  346. totalram_pages = balloon_stats.current_pages;
  347. balloon_stats.target_pages = balloon_stats.current_pages;
  348. balloon_stats.balloon_low = 0;
  349. balloon_stats.balloon_high = 0;
  350. balloon_stats.driver_pages = 0UL;
  351. balloon_stats.hard_limit = ~0UL;
  352. init_timer(&balloon_timer);
  353. balloon_timer.data = 0;
  354. balloon_timer.function = balloon_alarm;
  355. register_balloon(&balloon_sysdev);
  356. /* Initialise the balloon with excess memory space. */
  357. for (pfn = xen_start_info->nr_pages; pfn < max_pfn; pfn++) {
  358. page = pfn_to_page(pfn);
  359. if (!PageReserved(page))
  360. balloon_append(page);
  361. }
  362. target_watch.callback = watch_target;
  363. xenstore_notifier.notifier_call = balloon_init_watcher;
  364. register_xenstore_notifier(&xenstore_notifier);
  365. return 0;
  366. }
  367. subsys_initcall(balloon_init);
  368. static void balloon_exit(void)
  369. {
  370. /* XXX - release balloon here */
  371. return;
  372. }
  373. module_exit(balloon_exit);
  374. #define BALLOON_SHOW(name, format, args...) \
  375. static ssize_t show_##name(struct sys_device *dev, \
  376. struct sysdev_attribute *attr, \
  377. char *buf) \
  378. { \
  379. return sprintf(buf, format, ##args); \
  380. } \
  381. static SYSDEV_ATTR(name, S_IRUGO, show_##name, NULL)
  382. BALLOON_SHOW(current_kb, "%lu\n", PAGES2KB(balloon_stats.current_pages));
  383. BALLOON_SHOW(low_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_low));
  384. BALLOON_SHOW(high_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_high));
  385. BALLOON_SHOW(hard_limit_kb,
  386. (balloon_stats.hard_limit!=~0UL) ? "%lu\n" : "???\n",
  387. (balloon_stats.hard_limit!=~0UL) ? PAGES2KB(balloon_stats.hard_limit) : 0);
  388. BALLOON_SHOW(driver_kb, "%lu\n", PAGES2KB(balloon_stats.driver_pages));
  389. static ssize_t show_target_kb(struct sys_device *dev, struct sysdev_attribute *attr,
  390. char *buf)
  391. {
  392. return sprintf(buf, "%lu\n", PAGES2KB(balloon_stats.target_pages));
  393. }
  394. static ssize_t store_target_kb(struct sys_device *dev,
  395. struct sysdev_attribute *attr,
  396. const char *buf,
  397. size_t count)
  398. {
  399. char *endchar;
  400. unsigned long long target_bytes;
  401. if (!capable(CAP_SYS_ADMIN))
  402. return -EPERM;
  403. target_bytes = memparse(buf, &endchar);
  404. balloon_set_new_target(target_bytes >> PAGE_SHIFT);
  405. return count;
  406. }
  407. static SYSDEV_ATTR(target_kb, S_IRUGO | S_IWUSR,
  408. show_target_kb, store_target_kb);
  409. static struct sysdev_attribute *balloon_attrs[] = {
  410. &attr_target_kb,
  411. };
  412. static struct attribute *balloon_info_attrs[] = {
  413. &attr_current_kb.attr,
  414. &attr_low_kb.attr,
  415. &attr_high_kb.attr,
  416. &attr_hard_limit_kb.attr,
  417. &attr_driver_kb.attr,
  418. NULL
  419. };
  420. static struct attribute_group balloon_info_group = {
  421. .name = "info",
  422. .attrs = balloon_info_attrs,
  423. };
  424. static struct sysdev_class balloon_sysdev_class = {
  425. .name = BALLOON_CLASS_NAME,
  426. };
  427. static int register_balloon(struct sys_device *sysdev)
  428. {
  429. int i, error;
  430. error = sysdev_class_register(&balloon_sysdev_class);
  431. if (error)
  432. return error;
  433. sysdev->id = 0;
  434. sysdev->cls = &balloon_sysdev_class;
  435. error = sysdev_register(sysdev);
  436. if (error) {
  437. sysdev_class_unregister(&balloon_sysdev_class);
  438. return error;
  439. }
  440. for (i = 0; i < ARRAY_SIZE(balloon_attrs); i++) {
  441. error = sysdev_create_file(sysdev, balloon_attrs[i]);
  442. if (error)
  443. goto fail;
  444. }
  445. error = sysfs_create_group(&sysdev->kobj, &balloon_info_group);
  446. if (error)
  447. goto fail;
  448. return 0;
  449. fail:
  450. while (--i >= 0)
  451. sysdev_remove_file(sysdev, balloon_attrs[i]);
  452. sysdev_unregister(sysdev);
  453. sysdev_class_unregister(&balloon_sysdev_class);
  454. return error;
  455. }
  456. MODULE_LICENSE("GPL");