balloon.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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;
  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. page = balloon_first_page();
  177. for (i = 0; i < nr_pages; i++) {
  178. BUG_ON(page == NULL);
  179. frame_list[i] = page_to_pfn(page);
  180. page = balloon_next_page(page);
  181. }
  182. set_xen_guest_handle(reservation.extent_start, frame_list);
  183. reservation.nr_extents = nr_pages;
  184. rc = HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation);
  185. if (rc < 0)
  186. goto out;
  187. for (i = 0; i < rc; i++) {
  188. page = balloon_retrieve();
  189. BUG_ON(page == NULL);
  190. pfn = page_to_pfn(page);
  191. BUG_ON(!xen_feature(XENFEAT_auto_translated_physmap) &&
  192. phys_to_machine_mapping_valid(pfn));
  193. set_phys_to_machine(pfn, frame_list[i]);
  194. /* Link back into the page tables if not highmem. */
  195. if (pfn < max_low_pfn) {
  196. int ret;
  197. ret = HYPERVISOR_update_va_mapping(
  198. (unsigned long)__va(pfn << PAGE_SHIFT),
  199. mfn_pte(frame_list[i], PAGE_KERNEL),
  200. 0);
  201. BUG_ON(ret);
  202. }
  203. /* Relinquish the page back to the allocator. */
  204. ClearPageReserved(page);
  205. init_page_count(page);
  206. __free_page(page);
  207. }
  208. balloon_stats.current_pages += rc;
  209. out:
  210. return rc < 0 ? rc : rc != nr_pages;
  211. }
  212. static int decrease_reservation(unsigned long nr_pages)
  213. {
  214. unsigned long pfn, i;
  215. struct page *page;
  216. int need_sleep = 0;
  217. int ret;
  218. struct xen_memory_reservation reservation = {
  219. .address_bits = 0,
  220. .extent_order = 0,
  221. .domid = DOMID_SELF
  222. };
  223. if (nr_pages > ARRAY_SIZE(frame_list))
  224. nr_pages = ARRAY_SIZE(frame_list);
  225. for (i = 0; i < nr_pages; i++) {
  226. if ((page = alloc_page(GFP_BALLOON)) == NULL) {
  227. nr_pages = i;
  228. need_sleep = 1;
  229. break;
  230. }
  231. pfn = page_to_pfn(page);
  232. frame_list[i] = pfn_to_mfn(pfn);
  233. scrub_page(page);
  234. if (!PageHighMem(page)) {
  235. ret = HYPERVISOR_update_va_mapping(
  236. (unsigned long)__va(pfn << PAGE_SHIFT),
  237. __pte_ma(0), 0);
  238. BUG_ON(ret);
  239. }
  240. }
  241. /* Ensure that ballooned highmem pages don't have kmaps. */
  242. kmap_flush_unused();
  243. flush_tlb_all();
  244. /* No more mappings: invalidate P2M and add to balloon. */
  245. for (i = 0; i < nr_pages; i++) {
  246. pfn = mfn_to_pfn(frame_list[i]);
  247. set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
  248. balloon_append(pfn_to_page(pfn));
  249. }
  250. set_xen_guest_handle(reservation.extent_start, frame_list);
  251. reservation.nr_extents = nr_pages;
  252. ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
  253. BUG_ON(ret != nr_pages);
  254. balloon_stats.current_pages -= nr_pages;
  255. return need_sleep;
  256. }
  257. /*
  258. * We avoid multiple worker processes conflicting via the balloon mutex.
  259. * We may of course race updates of the target counts (which are protected
  260. * by the balloon lock), or with changes to the Xen hard limit, but we will
  261. * recover from these in time.
  262. */
  263. static void balloon_process(struct work_struct *work)
  264. {
  265. int need_sleep = 0;
  266. long credit;
  267. mutex_lock(&balloon_mutex);
  268. do {
  269. credit = current_target() - balloon_stats.current_pages;
  270. if (credit > 0)
  271. need_sleep = (increase_reservation(credit) != 0);
  272. if (credit < 0)
  273. need_sleep = (decrease_reservation(-credit) != 0);
  274. #ifndef CONFIG_PREEMPT
  275. if (need_resched())
  276. schedule();
  277. #endif
  278. } while ((credit != 0) && !need_sleep);
  279. /* Schedule more work if there is some still to be done. */
  280. if (current_target() != balloon_stats.current_pages)
  281. mod_timer(&balloon_timer, jiffies + HZ);
  282. mutex_unlock(&balloon_mutex);
  283. }
  284. /* Resets the Xen limit, sets new target, and kicks off processing. */
  285. static void balloon_set_new_target(unsigned long target)
  286. {
  287. /* No need for lock. Not read-modify-write updates. */
  288. balloon_stats.target_pages = target;
  289. schedule_work(&balloon_worker);
  290. }
  291. static struct xenbus_watch target_watch =
  292. {
  293. .node = "memory/target"
  294. };
  295. /* React to a change in the target key */
  296. static void watch_target(struct xenbus_watch *watch,
  297. const char **vec, unsigned int len)
  298. {
  299. unsigned long long new_target;
  300. int err;
  301. err = xenbus_scanf(XBT_NIL, "memory", "target", "%llu", &new_target);
  302. if (err != 1) {
  303. /* This is ok (for domain0 at least) - so just return */
  304. return;
  305. }
  306. /* The given memory/target value is in KiB, so it needs converting to
  307. * pages. PAGE_SHIFT converts bytes to pages, hence PAGE_SHIFT - 10.
  308. */
  309. balloon_set_new_target(new_target >> (PAGE_SHIFT - 10));
  310. }
  311. static int balloon_init_watcher(struct notifier_block *notifier,
  312. unsigned long event,
  313. void *data)
  314. {
  315. int err;
  316. err = register_xenbus_watch(&target_watch);
  317. if (err)
  318. printk(KERN_ERR "Failed to set balloon watcher\n");
  319. return NOTIFY_DONE;
  320. }
  321. static struct notifier_block xenstore_notifier;
  322. static int __init balloon_init(void)
  323. {
  324. unsigned long pfn;
  325. struct page *page;
  326. if (!xen_pv_domain())
  327. return -ENODEV;
  328. pr_info("xen_balloon: Initialising balloon driver.\n");
  329. balloon_stats.current_pages = min(xen_start_info->nr_pages, max_pfn);
  330. balloon_stats.target_pages = balloon_stats.current_pages;
  331. balloon_stats.balloon_low = 0;
  332. balloon_stats.balloon_high = 0;
  333. balloon_stats.driver_pages = 0UL;
  334. init_timer(&balloon_timer);
  335. balloon_timer.data = 0;
  336. balloon_timer.function = balloon_alarm;
  337. register_balloon(&balloon_sysdev);
  338. /* Initialise the balloon with excess memory space. */
  339. for (pfn = PFN_UP(xen_extra_mem_start);
  340. pfn < PFN_DOWN(xen_extra_mem_start + xen_extra_mem_size);
  341. pfn++) {
  342. page = pfn_to_page(pfn);
  343. /* totalram_pages doesn't include the boot-time
  344. balloon extension, so don't subtract from it. */
  345. __balloon_append(page);
  346. }
  347. target_watch.callback = watch_target;
  348. xenstore_notifier.notifier_call = balloon_init_watcher;
  349. register_xenstore_notifier(&xenstore_notifier);
  350. return 0;
  351. }
  352. subsys_initcall(balloon_init);
  353. static void balloon_exit(void)
  354. {
  355. /* XXX - release balloon here */
  356. return;
  357. }
  358. module_exit(balloon_exit);
  359. #define BALLOON_SHOW(name, format, args...) \
  360. static ssize_t show_##name(struct sys_device *dev, \
  361. struct sysdev_attribute *attr, \
  362. char *buf) \
  363. { \
  364. return sprintf(buf, format, ##args); \
  365. } \
  366. static SYSDEV_ATTR(name, S_IRUGO, show_##name, NULL)
  367. BALLOON_SHOW(current_kb, "%lu\n", PAGES2KB(balloon_stats.current_pages));
  368. BALLOON_SHOW(low_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_low));
  369. BALLOON_SHOW(high_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_high));
  370. BALLOON_SHOW(driver_kb, "%lu\n", PAGES2KB(balloon_stats.driver_pages));
  371. static ssize_t show_target_kb(struct sys_device *dev, struct sysdev_attribute *attr,
  372. char *buf)
  373. {
  374. return sprintf(buf, "%lu\n", PAGES2KB(balloon_stats.target_pages));
  375. }
  376. static ssize_t store_target_kb(struct sys_device *dev,
  377. struct sysdev_attribute *attr,
  378. const char *buf,
  379. size_t count)
  380. {
  381. char *endchar;
  382. unsigned long long target_bytes;
  383. if (!capable(CAP_SYS_ADMIN))
  384. return -EPERM;
  385. target_bytes = simple_strtoull(buf, &endchar, 0) * 1024;
  386. balloon_set_new_target(target_bytes >> PAGE_SHIFT);
  387. return count;
  388. }
  389. static SYSDEV_ATTR(target_kb, S_IRUGO | S_IWUSR,
  390. show_target_kb, store_target_kb);
  391. static ssize_t show_target(struct sys_device *dev, struct sysdev_attribute *attr,
  392. char *buf)
  393. {
  394. return sprintf(buf, "%llu\n",
  395. (unsigned long long)balloon_stats.target_pages
  396. << PAGE_SHIFT);
  397. }
  398. static ssize_t store_target(struct sys_device *dev,
  399. struct sysdev_attribute *attr,
  400. const char *buf,
  401. size_t count)
  402. {
  403. char *endchar;
  404. unsigned long long target_bytes;
  405. if (!capable(CAP_SYS_ADMIN))
  406. return -EPERM;
  407. target_bytes = memparse(buf, &endchar);
  408. balloon_set_new_target(target_bytes >> PAGE_SHIFT);
  409. return count;
  410. }
  411. static SYSDEV_ATTR(target, S_IRUGO | S_IWUSR,
  412. show_target, store_target);
  413. static struct sysdev_attribute *balloon_attrs[] = {
  414. &attr_target_kb,
  415. &attr_target,
  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_driver_kb.attr,
  422. NULL
  423. };
  424. static struct attribute_group balloon_info_group = {
  425. .name = "info",
  426. .attrs = balloon_info_attrs,
  427. };
  428. static struct sysdev_class balloon_sysdev_class = {
  429. .name = BALLOON_CLASS_NAME,
  430. };
  431. static int register_balloon(struct sys_device *sysdev)
  432. {
  433. int i, error;
  434. error = sysdev_class_register(&balloon_sysdev_class);
  435. if (error)
  436. return error;
  437. sysdev->id = 0;
  438. sysdev->cls = &balloon_sysdev_class;
  439. error = sysdev_register(sysdev);
  440. if (error) {
  441. sysdev_class_unregister(&balloon_sysdev_class);
  442. return error;
  443. }
  444. for (i = 0; i < ARRAY_SIZE(balloon_attrs); i++) {
  445. error = sysdev_create_file(sysdev, balloon_attrs[i]);
  446. if (error)
  447. goto fail;
  448. }
  449. error = sysfs_create_group(&sysdev->kobj, &balloon_info_group);
  450. if (error)
  451. goto fail;
  452. return 0;
  453. fail:
  454. while (--i >= 0)
  455. sysdev_remove_file(sysdev, balloon_attrs[i]);
  456. sysdev_unregister(sysdev);
  457. sysdev_class_unregister(&balloon_sysdev_class);
  458. return error;
  459. }
  460. MODULE_LICENSE("GPL");