balloon_compaction.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * mm/balloon_compaction.c
  4. *
  5. * Common interface for making balloon pages movable by compaction.
  6. *
  7. * Copyright (C) 2012, Red Hat, Inc. Rafael Aquini <aquini@redhat.com>
  8. */
  9. #include <linux/mm.h>
  10. #include <linux/slab.h>
  11. #include <linux/export.h>
  12. #include <linux/balloon_compaction.h>
  13. static void balloon_page_enqueue_one(struct balloon_dev_info *b_dev_info,
  14. struct page *page)
  15. {
  16. /*
  17. * Block others from accessing the 'page' when we get around to
  18. * establishing additional references. We should be the only one
  19. * holding a reference to the 'page' at this point. If we are not, then
  20. * memory corruption is possible and we should stop execution.
  21. */
  22. BUG_ON(!trylock_page(page));
  23. balloon_page_insert(b_dev_info, page);
  24. unlock_page(page);
  25. __count_vm_event(BALLOON_INFLATE);
  26. }
  27. /**
  28. * balloon_page_list_enqueue() - inserts a list of pages into the balloon page
  29. * list.
  30. * @b_dev_info: balloon device descriptor where we will insert a new page to
  31. * @pages: pages to enqueue - allocated using balloon_page_alloc.
  32. *
  33. * Driver must call this function to properly enqueue balloon pages before
  34. * definitively removing them from the guest system.
  35. *
  36. * Return: number of pages that were enqueued.
  37. */
  38. size_t balloon_page_list_enqueue(struct balloon_dev_info *b_dev_info,
  39. struct list_head *pages)
  40. {
  41. struct page *page, *tmp;
  42. unsigned long flags;
  43. size_t n_pages = 0;
  44. spin_lock_irqsave(&b_dev_info->pages_lock, flags);
  45. list_for_each_entry_safe(page, tmp, pages, lru) {
  46. list_del(&page->lru);
  47. balloon_page_enqueue_one(b_dev_info, page);
  48. n_pages++;
  49. }
  50. spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
  51. return n_pages;
  52. }
  53. EXPORT_SYMBOL_GPL(balloon_page_list_enqueue);
  54. /**
  55. * balloon_page_list_dequeue() - removes pages from balloon's page list and
  56. * returns a list of the pages.
  57. * @b_dev_info: balloon device decriptor where we will grab a page from.
  58. * @pages: pointer to the list of pages that would be returned to the caller.
  59. * @n_req_pages: number of requested pages.
  60. *
  61. * Driver must call this function to properly de-allocate a previous enlisted
  62. * balloon pages before definitively releasing it back to the guest system.
  63. * This function tries to remove @n_req_pages from the ballooned pages and
  64. * return them to the caller in the @pages list.
  65. *
  66. * Note that this function may fail to dequeue some pages even if the balloon
  67. * isn't empty - since the page list can be temporarily empty due to compaction
  68. * of isolated pages.
  69. *
  70. * Return: number of pages that were added to the @pages list.
  71. */
  72. size_t balloon_page_list_dequeue(struct balloon_dev_info *b_dev_info,
  73. struct list_head *pages, size_t n_req_pages)
  74. {
  75. struct page *page, *tmp;
  76. unsigned long flags;
  77. size_t n_pages = 0;
  78. spin_lock_irqsave(&b_dev_info->pages_lock, flags);
  79. list_for_each_entry_safe(page, tmp, &b_dev_info->pages, lru) {
  80. if (n_pages == n_req_pages)
  81. break;
  82. /*
  83. * Block others from accessing the 'page' while we get around to
  84. * establishing additional references and preparing the 'page'
  85. * to be released by the balloon driver.
  86. */
  87. if (!trylock_page(page))
  88. continue;
  89. if (IS_ENABLED(CONFIG_BALLOON_COMPACTION) &&
  90. PageIsolated(page)) {
  91. /* raced with isolation */
  92. unlock_page(page);
  93. continue;
  94. }
  95. balloon_page_delete(page);
  96. __count_vm_event(BALLOON_DEFLATE);
  97. list_add(&page->lru, pages);
  98. unlock_page(page);
  99. n_pages++;
  100. }
  101. spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
  102. return n_pages;
  103. }
  104. EXPORT_SYMBOL_GPL(balloon_page_list_dequeue);
  105. /*
  106. * balloon_page_alloc - allocates a new page for insertion into the balloon
  107. * page list.
  108. *
  109. * Driver must call this function to properly allocate a new balloon page.
  110. * Driver must call balloon_page_enqueue before definitively removing the page
  111. * from the guest system.
  112. *
  113. * Return: struct page for the allocated page or NULL on allocation failure.
  114. */
  115. struct page *balloon_page_alloc(void)
  116. {
  117. struct page *page = alloc_page(balloon_mapping_gfp_mask() |
  118. __GFP_NOMEMALLOC | __GFP_NORETRY |
  119. __GFP_NOWARN);
  120. return page;
  121. }
  122. EXPORT_SYMBOL_GPL(balloon_page_alloc);
  123. /*
  124. * balloon_page_enqueue - inserts a new page into the balloon page list.
  125. *
  126. * @b_dev_info: balloon device descriptor where we will insert a new page
  127. * @page: new page to enqueue - allocated using balloon_page_alloc.
  128. *
  129. * Drivers must call this function to properly enqueue a new allocated balloon
  130. * page before definitively removing the page from the guest system.
  131. *
  132. * Drivers must not call balloon_page_enqueue on pages that have been pushed to
  133. * a list with balloon_page_push before removing them with balloon_page_pop. To
  134. * enqueue a list of pages, use balloon_page_list_enqueue instead.
  135. */
  136. void balloon_page_enqueue(struct balloon_dev_info *b_dev_info,
  137. struct page *page)
  138. {
  139. unsigned long flags;
  140. spin_lock_irqsave(&b_dev_info->pages_lock, flags);
  141. balloon_page_enqueue_one(b_dev_info, page);
  142. spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
  143. }
  144. EXPORT_SYMBOL_GPL(balloon_page_enqueue);
  145. /*
  146. * balloon_page_dequeue - removes a page from balloon's page list and returns
  147. * its address to allow the driver to release the page.
  148. * @b_dev_info: balloon device decriptor where we will grab a page from.
  149. *
  150. * Driver must call this function to properly dequeue a previously enqueued page
  151. * before definitively releasing it back to the guest system.
  152. *
  153. * Caller must perform its own accounting to ensure that this
  154. * function is called only if some pages are actually enqueued.
  155. *
  156. * Note that this function may fail to dequeue some pages even if there are
  157. * some enqueued pages - since the page list can be temporarily empty due to
  158. * the compaction of isolated pages.
  159. *
  160. * TODO: remove the caller accounting requirements, and allow caller to wait
  161. * until all pages can be dequeued.
  162. *
  163. * Return: struct page for the dequeued page, or NULL if no page was dequeued.
  164. */
  165. struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info)
  166. {
  167. unsigned long flags;
  168. LIST_HEAD(pages);
  169. int n_pages;
  170. n_pages = balloon_page_list_dequeue(b_dev_info, &pages, 1);
  171. if (n_pages != 1) {
  172. /*
  173. * If we are unable to dequeue a balloon page because the page
  174. * list is empty and there are no isolated pages, then something
  175. * went out of track and some balloon pages are lost.
  176. * BUG() here, otherwise the balloon driver may get stuck in
  177. * an infinite loop while attempting to release all its pages.
  178. */
  179. spin_lock_irqsave(&b_dev_info->pages_lock, flags);
  180. if (unlikely(list_empty(&b_dev_info->pages) &&
  181. !b_dev_info->isolated_pages))
  182. BUG();
  183. spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
  184. return NULL;
  185. }
  186. return list_first_entry(&pages, struct page, lru);
  187. }
  188. EXPORT_SYMBOL_GPL(balloon_page_dequeue);
  189. #ifdef CONFIG_BALLOON_COMPACTION
  190. bool balloon_page_isolate(struct page *page, isolate_mode_t mode)
  191. {
  192. struct balloon_dev_info *b_dev_info = balloon_page_device(page);
  193. unsigned long flags;
  194. spin_lock_irqsave(&b_dev_info->pages_lock, flags);
  195. list_del(&page->lru);
  196. b_dev_info->isolated_pages++;
  197. spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
  198. return true;
  199. }
  200. void balloon_page_putback(struct page *page)
  201. {
  202. struct balloon_dev_info *b_dev_info = balloon_page_device(page);
  203. unsigned long flags;
  204. spin_lock_irqsave(&b_dev_info->pages_lock, flags);
  205. list_add(&page->lru, &b_dev_info->pages);
  206. b_dev_info->isolated_pages--;
  207. spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
  208. }
  209. /* move_to_new_page() counterpart for a ballooned page */
  210. int balloon_page_migrate(struct address_space *mapping,
  211. struct page *newpage, struct page *page,
  212. enum migrate_mode mode)
  213. {
  214. struct balloon_dev_info *balloon = balloon_page_device(page);
  215. /*
  216. * We can not easily support the no copy case here so ignore it as it
  217. * is unlikely to be used with balloon pages. See include/linux/hmm.h
  218. * for a user of the MIGRATE_SYNC_NO_COPY mode.
  219. */
  220. if (mode == MIGRATE_SYNC_NO_COPY)
  221. return -EINVAL;
  222. VM_BUG_ON_PAGE(!PageLocked(page), page);
  223. VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
  224. return balloon->migratepage(balloon, newpage, page, mode);
  225. }
  226. const struct address_space_operations balloon_aops = {
  227. .migratepage = balloon_page_migrate,
  228. .isolate_page = balloon_page_isolate,
  229. .putback_page = balloon_page_putback,
  230. };
  231. EXPORT_SYMBOL_GPL(balloon_aops);
  232. #endif /* CONFIG_BALLOON_COMPACTION */