balloon.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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/highmem.h>
  44. #include <linux/list.h>
  45. #include <linux/sysdev.h>
  46. #include <asm/xen/hypervisor.h>
  47. #include <asm/page.h>
  48. #include <asm/pgalloc.h>
  49. #include <asm/pgtable.h>
  50. #include <asm/uaccess.h>
  51. #include <asm/tlb.h>
  52. #include <xen/interface/memory.h>
  53. #include <xen/balloon.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. if (PageHighMem(page)) {
  110. void *v = kmap(page);
  111. clear_page(v);
  112. kunmap(v);
  113. } else {
  114. void *v = page_address(page);
  115. clear_page(v);
  116. }
  117. #endif
  118. }
  119. /* balloon_append: add the given page to the balloon. */
  120. static void balloon_append(struct page *page)
  121. {
  122. /* Lowmem is re-populated first, so highmem pages go at list tail. */
  123. if (PageHighMem(page)) {
  124. list_add_tail(&page->lru, &ballooned_pages);
  125. balloon_stats.balloon_high++;
  126. dec_totalhigh_pages();
  127. } else {
  128. list_add(&page->lru, &ballooned_pages);
  129. balloon_stats.balloon_low++;
  130. }
  131. }
  132. /* balloon_retrieve: rescue a page from the balloon, if it is not empty. */
  133. static struct page *balloon_retrieve(void)
  134. {
  135. struct page *page;
  136. if (list_empty(&ballooned_pages))
  137. return NULL;
  138. page = list_entry(ballooned_pages.next, struct page, lru);
  139. list_del(&page->lru);
  140. if (PageHighMem(page)) {
  141. balloon_stats.balloon_high--;
  142. inc_totalhigh_pages();
  143. }
  144. else
  145. balloon_stats.balloon_low--;
  146. return page;
  147. }
  148. static struct page *balloon_first_page(void)
  149. {
  150. if (list_empty(&ballooned_pages))
  151. return NULL;
  152. return list_entry(ballooned_pages.next, struct page, lru);
  153. }
  154. static struct page *balloon_next_page(struct page *page)
  155. {
  156. struct list_head *next = page->lru.next;
  157. if (next == &ballooned_pages)
  158. return NULL;
  159. return list_entry(next, struct page, lru);
  160. }
  161. static void balloon_alarm(unsigned long unused)
  162. {
  163. schedule_work(&balloon_worker);
  164. }
  165. static unsigned long current_target(void)
  166. {
  167. unsigned long target = min(balloon_stats.target_pages, balloon_stats.hard_limit);
  168. target = min(target,
  169. balloon_stats.current_pages +
  170. balloon_stats.balloon_low +
  171. balloon_stats.balloon_high);
  172. return target;
  173. }
  174. static int increase_reservation(unsigned long nr_pages)
  175. {
  176. unsigned long pfn, i, flags;
  177. struct page *page;
  178. long rc;
  179. struct xen_memory_reservation reservation = {
  180. .address_bits = 0,
  181. .extent_order = 0,
  182. .domid = DOMID_SELF
  183. };
  184. if (nr_pages > ARRAY_SIZE(frame_list))
  185. nr_pages = ARRAY_SIZE(frame_list);
  186. spin_lock_irqsave(&balloon_lock, flags);
  187. page = balloon_first_page();
  188. for (i = 0; i < nr_pages; i++) {
  189. BUG_ON(page == NULL);
  190. frame_list[i] = page_to_pfn(page);;
  191. page = balloon_next_page(page);
  192. }
  193. set_xen_guest_handle(reservation.extent_start, frame_list);
  194. reservation.nr_extents = nr_pages;
  195. rc = HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation);
  196. if (rc < nr_pages) {
  197. if (rc > 0) {
  198. int ret;
  199. /* We hit the Xen hard limit: reprobe. */
  200. reservation.nr_extents = rc;
  201. ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
  202. &reservation);
  203. BUG_ON(ret != rc);
  204. }
  205. if (rc >= 0)
  206. balloon_stats.hard_limit = (balloon_stats.current_pages + rc -
  207. balloon_stats.driver_pages);
  208. goto out;
  209. }
  210. for (i = 0; i < nr_pages; i++) {
  211. page = balloon_retrieve();
  212. BUG_ON(page == NULL);
  213. pfn = page_to_pfn(page);
  214. BUG_ON(!xen_feature(XENFEAT_auto_translated_physmap) &&
  215. phys_to_machine_mapping_valid(pfn));
  216. set_phys_to_machine(pfn, frame_list[i]);
  217. /* Link back into the page tables if not highmem. */
  218. if (pfn < max_low_pfn) {
  219. int ret;
  220. ret = HYPERVISOR_update_va_mapping(
  221. (unsigned long)__va(pfn << PAGE_SHIFT),
  222. mfn_pte(frame_list[i], PAGE_KERNEL),
  223. 0);
  224. BUG_ON(ret);
  225. }
  226. /* Relinquish the page back to the allocator. */
  227. ClearPageReserved(page);
  228. init_page_count(page);
  229. __free_page(page);
  230. }
  231. balloon_stats.current_pages += nr_pages;
  232. totalram_pages = balloon_stats.current_pages;
  233. out:
  234. spin_unlock_irqrestore(&balloon_lock, flags);
  235. return 0;
  236. }
  237. static int decrease_reservation(unsigned long nr_pages)
  238. {
  239. unsigned long pfn, i, flags;
  240. struct page *page;
  241. int need_sleep = 0;
  242. int ret;
  243. struct xen_memory_reservation reservation = {
  244. .address_bits = 0,
  245. .extent_order = 0,
  246. .domid = DOMID_SELF
  247. };
  248. if (nr_pages > ARRAY_SIZE(frame_list))
  249. nr_pages = ARRAY_SIZE(frame_list);
  250. for (i = 0; i < nr_pages; i++) {
  251. if ((page = alloc_page(GFP_BALLOON)) == NULL) {
  252. nr_pages = i;
  253. need_sleep = 1;
  254. break;
  255. }
  256. pfn = page_to_pfn(page);
  257. frame_list[i] = pfn_to_mfn(pfn);
  258. scrub_page(page);
  259. }
  260. /* Ensure that ballooned highmem pages don't have kmaps. */
  261. kmap_flush_unused();
  262. flush_tlb_all();
  263. spin_lock_irqsave(&balloon_lock, flags);
  264. /* No more mappings: invalidate P2M and add to balloon. */
  265. for (i = 0; i < nr_pages; i++) {
  266. pfn = mfn_to_pfn(frame_list[i]);
  267. set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
  268. balloon_append(pfn_to_page(pfn));
  269. }
  270. set_xen_guest_handle(reservation.extent_start, frame_list);
  271. reservation.nr_extents = nr_pages;
  272. ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
  273. BUG_ON(ret != nr_pages);
  274. balloon_stats.current_pages -= nr_pages;
  275. totalram_pages = balloon_stats.current_pages;
  276. spin_unlock_irqrestore(&balloon_lock, flags);
  277. return need_sleep;
  278. }
  279. /*
  280. * We avoid multiple worker processes conflicting via the balloon mutex.
  281. * We may of course race updates of the target counts (which are protected
  282. * by the balloon lock), or with changes to the Xen hard limit, but we will
  283. * recover from these in time.
  284. */
  285. static void balloon_process(struct work_struct *work)
  286. {
  287. int need_sleep = 0;
  288. long credit;
  289. mutex_lock(&balloon_mutex);
  290. do {
  291. credit = current_target() - balloon_stats.current_pages;
  292. if (credit > 0)
  293. need_sleep = (increase_reservation(credit) != 0);
  294. if (credit < 0)
  295. need_sleep = (decrease_reservation(-credit) != 0);
  296. #ifndef CONFIG_PREEMPT
  297. if (need_resched())
  298. schedule();
  299. #endif
  300. } while ((credit != 0) && !need_sleep);
  301. /* Schedule more work if there is some still to be done. */
  302. if (current_target() != balloon_stats.current_pages)
  303. mod_timer(&balloon_timer, jiffies + HZ);
  304. mutex_unlock(&balloon_mutex);
  305. }
  306. /* Resets the Xen limit, sets new target, and kicks off processing. */
  307. static void balloon_set_new_target(unsigned long target)
  308. {
  309. /* No need for lock. Not read-modify-write updates. */
  310. balloon_stats.hard_limit = ~0UL;
  311. balloon_stats.target_pages = target;
  312. schedule_work(&balloon_worker);
  313. }
  314. static struct xenbus_watch target_watch =
  315. {
  316. .node = "memory/target"
  317. };
  318. /* React to a change in the target key */
  319. static void watch_target(struct xenbus_watch *watch,
  320. const char **vec, unsigned int len)
  321. {
  322. unsigned long long new_target;
  323. int err;
  324. err = xenbus_scanf(XBT_NIL, "memory", "target", "%llu", &new_target);
  325. if (err != 1) {
  326. /* This is ok (for domain0 at least) - so just return */
  327. return;
  328. }
  329. /* The given memory/target value is in KiB, so it needs converting to
  330. * pages. PAGE_SHIFT converts bytes to pages, hence PAGE_SHIFT - 10.
  331. */
  332. balloon_set_new_target(new_target >> (PAGE_SHIFT - 10));
  333. }
  334. static int balloon_init_watcher(struct notifier_block *notifier,
  335. unsigned long event,
  336. void *data)
  337. {
  338. int err;
  339. err = register_xenbus_watch(&target_watch);
  340. if (err)
  341. printk(KERN_ERR "Failed to set balloon watcher\n");
  342. return NOTIFY_DONE;
  343. }
  344. static struct notifier_block xenstore_notifier;
  345. static int __init balloon_init(void)
  346. {
  347. unsigned long pfn;
  348. struct page *page;
  349. if (!xen_pv_domain())
  350. return -ENODEV;
  351. pr_info("xen_balloon: Initialising balloon driver.\n");
  352. balloon_stats.current_pages = min(xen_start_info->nr_pages, max_pfn);
  353. totalram_pages = balloon_stats.current_pages;
  354. balloon_stats.target_pages = balloon_stats.current_pages;
  355. balloon_stats.balloon_low = 0;
  356. balloon_stats.balloon_high = 0;
  357. balloon_stats.driver_pages = 0UL;
  358. balloon_stats.hard_limit = ~0UL;
  359. init_timer(&balloon_timer);
  360. balloon_timer.data = 0;
  361. balloon_timer.function = balloon_alarm;
  362. register_balloon(&balloon_sysdev);
  363. /* Initialise the balloon with excess memory space. */
  364. for (pfn = xen_start_info->nr_pages; pfn < max_pfn; pfn++) {
  365. page = pfn_to_page(pfn);
  366. if (!PageReserved(page))
  367. balloon_append(page);
  368. }
  369. target_watch.callback = watch_target;
  370. xenstore_notifier.notifier_call = balloon_init_watcher;
  371. register_xenstore_notifier(&xenstore_notifier);
  372. return 0;
  373. }
  374. subsys_initcall(balloon_init);
  375. static void balloon_exit(void)
  376. {
  377. /* XXX - release balloon here */
  378. return;
  379. }
  380. module_exit(balloon_exit);
  381. #define BALLOON_SHOW(name, format, args...) \
  382. static ssize_t show_##name(struct sys_device *dev, \
  383. struct sysdev_attribute *attr, \
  384. char *buf) \
  385. { \
  386. return sprintf(buf, format, ##args); \
  387. } \
  388. static SYSDEV_ATTR(name, S_IRUGO, show_##name, NULL)
  389. BALLOON_SHOW(current_kb, "%lu\n", PAGES2KB(balloon_stats.current_pages));
  390. BALLOON_SHOW(low_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_low));
  391. BALLOON_SHOW(high_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_high));
  392. BALLOON_SHOW(hard_limit_kb,
  393. (balloon_stats.hard_limit!=~0UL) ? "%lu\n" : "???\n",
  394. (balloon_stats.hard_limit!=~0UL) ? PAGES2KB(balloon_stats.hard_limit) : 0);
  395. BALLOON_SHOW(driver_kb, "%lu\n", PAGES2KB(balloon_stats.driver_pages));
  396. static ssize_t show_target_kb(struct sys_device *dev, struct sysdev_attribute *attr,
  397. char *buf)
  398. {
  399. return sprintf(buf, "%lu\n", PAGES2KB(balloon_stats.target_pages));
  400. }
  401. static ssize_t store_target_kb(struct sys_device *dev,
  402. struct sysdev_attribute *attr,
  403. const char *buf,
  404. size_t count)
  405. {
  406. char *endchar;
  407. unsigned long long target_bytes;
  408. if (!capable(CAP_SYS_ADMIN))
  409. return -EPERM;
  410. target_bytes = memparse(buf, &endchar);
  411. balloon_set_new_target(target_bytes >> PAGE_SHIFT);
  412. return count;
  413. }
  414. static SYSDEV_ATTR(target_kb, S_IRUGO | S_IWUSR,
  415. show_target_kb, store_target_kb);
  416. static struct sysdev_attribute *balloon_attrs[] = {
  417. &attr_target_kb,
  418. };
  419. static struct attribute *balloon_info_attrs[] = {
  420. &attr_current_kb.attr,
  421. &attr_low_kb.attr,
  422. &attr_high_kb.attr,
  423. &attr_hard_limit_kb.attr,
  424. &attr_driver_kb.attr,
  425. NULL
  426. };
  427. static struct attribute_group balloon_info_group = {
  428. .name = "info",
  429. .attrs = balloon_info_attrs,
  430. };
  431. static struct sysdev_class balloon_sysdev_class = {
  432. .name = BALLOON_CLASS_NAME,
  433. };
  434. static int register_balloon(struct sys_device *sysdev)
  435. {
  436. int i, error;
  437. error = sysdev_class_register(&balloon_sysdev_class);
  438. if (error)
  439. return error;
  440. sysdev->id = 0;
  441. sysdev->cls = &balloon_sysdev_class;
  442. error = sysdev_register(sysdev);
  443. if (error) {
  444. sysdev_class_unregister(&balloon_sysdev_class);
  445. return error;
  446. }
  447. for (i = 0; i < ARRAY_SIZE(balloon_attrs); i++) {
  448. error = sysdev_create_file(sysdev, balloon_attrs[i]);
  449. if (error)
  450. goto fail;
  451. }
  452. error = sysfs_create_group(&sysdev->kobj, &balloon_info_group);
  453. if (error)
  454. goto fail;
  455. return 0;
  456. fail:
  457. while (--i >= 0)
  458. sysdev_remove_file(sysdev, balloon_attrs[i]);
  459. sysdev_unregister(sysdev);
  460. sysdev_class_unregister(&balloon_sysdev_class);
  461. return error;
  462. }
  463. MODULE_LICENSE("GPL");