balloon.c 15 KB

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