balloon.c 15 KB

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