balloon.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Common interface for implementing a memory balloon, including support
  4. * for migration of pages inflated in a memory balloon.
  5. *
  6. * Copyright (C) 2012, Red Hat, Inc. Rafael Aquini <aquini@redhat.com>
  7. */
  8. #include <linux/mm.h>
  9. #include <linux/slab.h>
  10. #include <linux/export.h>
  11. #include <linux/balloon.h>
  12. /*
  13. * Lock protecting the balloon_dev_info of all devices. We don't really
  14. * expect more than one device.
  15. */
  16. static DEFINE_SPINLOCK(balloon_pages_lock);
  17. /**
  18. * balloon_page_insert - insert a page into the balloon's page list and make
  19. * the page->private assignment accordingly.
  20. * @balloon : pointer to balloon device
  21. * @page : page to be assigned as a 'balloon page'
  22. *
  23. * Caller must ensure the balloon_pages_lock is held.
  24. */
  25. static void balloon_page_insert(struct balloon_dev_info *balloon,
  26. struct page *page)
  27. {
  28. lockdep_assert_held(&balloon_pages_lock);
  29. __SetPageOffline(page);
  30. if (IS_ENABLED(CONFIG_BALLOON_MIGRATION)) {
  31. SetPageMovableOps(page);
  32. set_page_private(page, (unsigned long)balloon);
  33. }
  34. list_add(&page->lru, &balloon->pages);
  35. }
  36. /**
  37. * balloon_page_finalize - prepare a balloon page that was removed from the
  38. * balloon list for release to the page allocator
  39. * @page: page to be released to the page allocator
  40. *
  41. * Caller must ensure the balloon_pages_lock is held.
  42. */
  43. static void balloon_page_finalize(struct page *page)
  44. {
  45. lockdep_assert_held(&balloon_pages_lock);
  46. if (IS_ENABLED(CONFIG_BALLOON_MIGRATION))
  47. set_page_private(page, 0);
  48. /* PageOffline is sticky until the page is freed to the buddy. */
  49. }
  50. static void balloon_page_enqueue_one(struct balloon_dev_info *b_dev_info,
  51. struct page *page)
  52. {
  53. balloon_page_insert(b_dev_info, page);
  54. if (b_dev_info->adjust_managed_page_count)
  55. adjust_managed_page_count(page, -1);
  56. __count_vm_event(BALLOON_INFLATE);
  57. inc_node_page_state(page, NR_BALLOON_PAGES);
  58. }
  59. /**
  60. * balloon_page_list_enqueue() - inserts a list of pages into the balloon page
  61. * list.
  62. * @b_dev_info: balloon device descriptor where we will insert a new page to
  63. * @pages: pages to enqueue - allocated using balloon_page_alloc.
  64. *
  65. * Driver must call this function to properly enqueue balloon pages before
  66. * definitively removing them from the guest system.
  67. *
  68. * Return: number of pages that were enqueued.
  69. */
  70. size_t balloon_page_list_enqueue(struct balloon_dev_info *b_dev_info,
  71. struct list_head *pages)
  72. {
  73. struct page *page, *tmp;
  74. unsigned long flags;
  75. size_t n_pages = 0;
  76. spin_lock_irqsave(&balloon_pages_lock, flags);
  77. list_for_each_entry_safe(page, tmp, pages, lru) {
  78. list_del(&page->lru);
  79. balloon_page_enqueue_one(b_dev_info, page);
  80. n_pages++;
  81. }
  82. spin_unlock_irqrestore(&balloon_pages_lock, flags);
  83. return n_pages;
  84. }
  85. EXPORT_SYMBOL_GPL(balloon_page_list_enqueue);
  86. /**
  87. * balloon_page_list_dequeue() - removes pages from balloon's page list and
  88. * returns a list of the pages.
  89. * @b_dev_info: balloon device descriptor where we will grab a page from.
  90. * @pages: pointer to the list of pages that would be returned to the caller.
  91. * @n_req_pages: number of requested pages.
  92. *
  93. * Driver must call this function to properly de-allocate a previous enlisted
  94. * balloon pages before definitively releasing it back to the guest system.
  95. * This function tries to remove @n_req_pages from the ballooned pages and
  96. * return them to the caller in the @pages list.
  97. *
  98. * Note that this function may fail to dequeue some pages even if the balloon
  99. * isn't empty - since the page list can be temporarily empty due to compaction
  100. * of isolated pages.
  101. *
  102. * Return: number of pages that were added to the @pages list.
  103. */
  104. size_t balloon_page_list_dequeue(struct balloon_dev_info *b_dev_info,
  105. struct list_head *pages, size_t n_req_pages)
  106. {
  107. struct page *page, *tmp;
  108. unsigned long flags;
  109. size_t n_pages = 0;
  110. spin_lock_irqsave(&balloon_pages_lock, flags);
  111. list_for_each_entry_safe(page, tmp, &b_dev_info->pages, lru) {
  112. if (n_pages == n_req_pages)
  113. break;
  114. list_del(&page->lru);
  115. if (b_dev_info->adjust_managed_page_count)
  116. adjust_managed_page_count(page, 1);
  117. balloon_page_finalize(page);
  118. __count_vm_event(BALLOON_DEFLATE);
  119. list_add(&page->lru, pages);
  120. dec_node_page_state(page, NR_BALLOON_PAGES);
  121. n_pages++;
  122. }
  123. spin_unlock_irqrestore(&balloon_pages_lock, flags);
  124. return n_pages;
  125. }
  126. EXPORT_SYMBOL_GPL(balloon_page_list_dequeue);
  127. /**
  128. * balloon_page_alloc - allocates a new page for insertion into the balloon
  129. * page list.
  130. *
  131. * Driver must call this function to properly allocate a new balloon page.
  132. * Driver must call balloon_page_enqueue before definitively removing the page
  133. * from the guest system.
  134. *
  135. * Return: struct page for the allocated page or NULL on allocation failure.
  136. */
  137. struct page *balloon_page_alloc(void)
  138. {
  139. gfp_t gfp_flags = __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
  140. if (IS_ENABLED(CONFIG_BALLOON_MIGRATION))
  141. gfp_flags |= GFP_HIGHUSER_MOVABLE;
  142. else
  143. gfp_flags |= GFP_HIGHUSER;
  144. return alloc_page(gfp_flags);
  145. }
  146. EXPORT_SYMBOL_GPL(balloon_page_alloc);
  147. /**
  148. * balloon_page_enqueue - inserts a new page into the balloon page list.
  149. *
  150. * @b_dev_info: balloon device descriptor where we will insert a new page
  151. * @page: new page to enqueue - allocated using balloon_page_alloc.
  152. *
  153. * Drivers must call this function to properly enqueue a new allocated balloon
  154. * page before definitively removing the page from the guest system.
  155. *
  156. * Drivers must not enqueue pages while page->lru is still in
  157. * use, and must not use page->lru until a page was unqueued again.
  158. */
  159. void balloon_page_enqueue(struct balloon_dev_info *b_dev_info,
  160. struct page *page)
  161. {
  162. unsigned long flags;
  163. spin_lock_irqsave(&balloon_pages_lock, flags);
  164. balloon_page_enqueue_one(b_dev_info, page);
  165. spin_unlock_irqrestore(&balloon_pages_lock, flags);
  166. }
  167. EXPORT_SYMBOL_GPL(balloon_page_enqueue);
  168. /**
  169. * balloon_page_dequeue - removes a page from balloon's page list and returns
  170. * its address to allow the driver to release the page.
  171. * @b_dev_info: balloon device descriptor where we will grab a page from.
  172. *
  173. * Driver must call this function to properly dequeue a previously enqueued page
  174. * before definitively releasing it back to the guest system.
  175. *
  176. * Caller must perform its own accounting to ensure that this
  177. * function is called only if some pages are actually enqueued.
  178. *
  179. * Note that this function may fail to dequeue some pages even if there are
  180. * some enqueued pages - since the page list can be temporarily empty due to
  181. * the compaction of isolated pages.
  182. *
  183. * TODO: remove the caller accounting requirements, and allow caller to wait
  184. * until all pages can be dequeued.
  185. *
  186. * Return: struct page for the dequeued page, or NULL if no page was dequeued.
  187. */
  188. struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info)
  189. {
  190. unsigned long flags;
  191. LIST_HEAD(pages);
  192. int n_pages;
  193. n_pages = balloon_page_list_dequeue(b_dev_info, &pages, 1);
  194. if (n_pages != 1) {
  195. /*
  196. * If we are unable to dequeue a balloon page because the page
  197. * list is empty and there are no isolated pages, then something
  198. * went out of track and some balloon pages are lost.
  199. * BUG() here, otherwise the balloon driver may get stuck in
  200. * an infinite loop while attempting to release all its pages.
  201. */
  202. spin_lock_irqsave(&balloon_pages_lock, flags);
  203. if (unlikely(list_empty(&b_dev_info->pages) &&
  204. !b_dev_info->isolated_pages))
  205. BUG();
  206. spin_unlock_irqrestore(&balloon_pages_lock, flags);
  207. return NULL;
  208. }
  209. return list_first_entry(&pages, struct page, lru);
  210. }
  211. EXPORT_SYMBOL_GPL(balloon_page_dequeue);
  212. #ifdef CONFIG_BALLOON_MIGRATION
  213. static struct balloon_dev_info *balloon_page_device(struct page *page)
  214. {
  215. return (struct balloon_dev_info *)page_private(page);
  216. }
  217. static bool balloon_page_isolate(struct page *page, isolate_mode_t mode)
  218. {
  219. struct balloon_dev_info *b_dev_info;
  220. unsigned long flags;
  221. spin_lock_irqsave(&balloon_pages_lock, flags);
  222. b_dev_info = balloon_page_device(page);
  223. if (!b_dev_info) {
  224. /*
  225. * The page already got deflated and removed from the
  226. * balloon list.
  227. */
  228. spin_unlock_irqrestore(&balloon_pages_lock, flags);
  229. return false;
  230. }
  231. list_del(&page->lru);
  232. b_dev_info->isolated_pages++;
  233. spin_unlock_irqrestore(&balloon_pages_lock, flags);
  234. return true;
  235. }
  236. static void balloon_page_putback(struct page *page)
  237. {
  238. struct balloon_dev_info *b_dev_info = balloon_page_device(page);
  239. unsigned long flags;
  240. /*
  241. * When we isolated the page, the page was still inflated in a balloon
  242. * device. As isolated balloon pages cannot get deflated, we still have
  243. * a balloon device here.
  244. */
  245. if (WARN_ON_ONCE(!b_dev_info))
  246. return;
  247. spin_lock_irqsave(&balloon_pages_lock, flags);
  248. list_add(&page->lru, &b_dev_info->pages);
  249. b_dev_info->isolated_pages--;
  250. spin_unlock_irqrestore(&balloon_pages_lock, flags);
  251. }
  252. static int balloon_page_migrate(struct page *newpage, struct page *page,
  253. enum migrate_mode mode)
  254. {
  255. struct balloon_dev_info *b_dev_info = balloon_page_device(page);
  256. unsigned long flags;
  257. int rc;
  258. /*
  259. * When we isolated the page, the page was still inflated in a balloon
  260. * device. As isolated balloon pages cannot get deflated, we still have
  261. * a balloon device here.
  262. */
  263. if (WARN_ON_ONCE(!b_dev_info))
  264. return -EAGAIN;
  265. rc = b_dev_info->migratepage(b_dev_info, newpage, page, mode);
  266. if (rc < 0 && rc != -ENOENT)
  267. return rc;
  268. spin_lock_irqsave(&balloon_pages_lock, flags);
  269. if (!rc) {
  270. /* Insert the new page into the balloon list. */
  271. get_page(newpage);
  272. balloon_page_insert(b_dev_info, newpage);
  273. __count_vm_event(BALLOON_MIGRATE);
  274. if (b_dev_info->adjust_managed_page_count &&
  275. page_zone(page) != page_zone(newpage)) {
  276. /*
  277. * When we migrate a page to a different zone we
  278. * have to fixup the count of both involved zones.
  279. */
  280. adjust_managed_page_count(page, 1);
  281. adjust_managed_page_count(newpage, -1);
  282. }
  283. } else {
  284. /* Old page was deflated but new page not inflated. */
  285. __count_vm_event(BALLOON_DEFLATE);
  286. if (b_dev_info->adjust_managed_page_count)
  287. adjust_managed_page_count(page, 1);
  288. }
  289. b_dev_info->isolated_pages--;
  290. /* Free the now-deflated page we isolated in balloon_page_isolate(). */
  291. balloon_page_finalize(page);
  292. spin_unlock_irqrestore(&balloon_pages_lock, flags);
  293. put_page(page);
  294. return 0;
  295. }
  296. static const struct movable_operations balloon_mops = {
  297. .migrate_page = balloon_page_migrate,
  298. .isolate_page = balloon_page_isolate,
  299. .putback_page = balloon_page_putback,
  300. };
  301. static int __init balloon_init(void)
  302. {
  303. return set_movable_ops(&balloon_mops, PGTY_offline);
  304. }
  305. core_initcall(balloon_init);
  306. #endif /* CONFIG_BALLOON_MIGRATION */