balloon_compaction.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * mm/balloon_compaction.c
  3. *
  4. * Common interface for making balloon pages movable by compaction.
  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_compaction.h>
  12. /*
  13. * balloon_devinfo_alloc - allocates a balloon device information descriptor.
  14. * @balloon_dev_descriptor: pointer to reference the balloon device which
  15. * this struct balloon_dev_info will be servicing.
  16. *
  17. * Driver must call it to properly allocate and initialize an instance of
  18. * struct balloon_dev_info which will be used to reference a balloon device
  19. * as well as to keep track of the balloon device page list.
  20. */
  21. struct balloon_dev_info *balloon_devinfo_alloc(void *balloon_dev_descriptor)
  22. {
  23. struct balloon_dev_info *b_dev_info;
  24. b_dev_info = kmalloc(sizeof(*b_dev_info), GFP_KERNEL);
  25. if (!b_dev_info)
  26. return ERR_PTR(-ENOMEM);
  27. b_dev_info->balloon_device = balloon_dev_descriptor;
  28. b_dev_info->mapping = NULL;
  29. b_dev_info->isolated_pages = 0;
  30. spin_lock_init(&b_dev_info->pages_lock);
  31. INIT_LIST_HEAD(&b_dev_info->pages);
  32. return b_dev_info;
  33. }
  34. EXPORT_SYMBOL_GPL(balloon_devinfo_alloc);
  35. /*
  36. * balloon_page_enqueue - allocates a new page and inserts it into the balloon
  37. * page list.
  38. * @b_dev_info: balloon device decriptor where we will insert a new page to
  39. *
  40. * Driver must call it to properly allocate a new enlisted balloon page
  41. * before definetively removing it from the guest system.
  42. * This function returns the page address for the recently enqueued page or
  43. * NULL in the case we fail to allocate a new page this turn.
  44. */
  45. struct page *balloon_page_enqueue(struct balloon_dev_info *b_dev_info)
  46. {
  47. unsigned long flags;
  48. struct page *page = alloc_page(balloon_mapping_gfp_mask() |
  49. __GFP_NOMEMALLOC | __GFP_NORETRY);
  50. if (!page)
  51. return NULL;
  52. /*
  53. * Block others from accessing the 'page' when we get around to
  54. * establishing additional references. We should be the only one
  55. * holding a reference to the 'page' at this point.
  56. */
  57. BUG_ON(!trylock_page(page));
  58. spin_lock_irqsave(&b_dev_info->pages_lock, flags);
  59. balloon_page_insert(page, b_dev_info->mapping, &b_dev_info->pages);
  60. spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
  61. unlock_page(page);
  62. return page;
  63. }
  64. EXPORT_SYMBOL_GPL(balloon_page_enqueue);
  65. /*
  66. * balloon_page_dequeue - removes a page from balloon's page list and returns
  67. * the its address to allow the driver release the page.
  68. * @b_dev_info: balloon device decriptor where we will grab a page from.
  69. *
  70. * Driver must call it to properly de-allocate a previous enlisted balloon page
  71. * before definetively releasing it back to the guest system.
  72. * This function returns the page address for the recently dequeued page or
  73. * NULL in the case we find balloon's page list temporarily empty due to
  74. * compaction isolated pages.
  75. */
  76. struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info)
  77. {
  78. struct page *page, *tmp;
  79. unsigned long flags;
  80. bool dequeued_page;
  81. dequeued_page = false;
  82. list_for_each_entry_safe(page, tmp, &b_dev_info->pages, lru) {
  83. /*
  84. * Block others from accessing the 'page' while we get around
  85. * establishing additional references and preparing the 'page'
  86. * to be released by the balloon driver.
  87. */
  88. if (trylock_page(page)) {
  89. spin_lock_irqsave(&b_dev_info->pages_lock, flags);
  90. /*
  91. * Raise the page refcount here to prevent any wrong
  92. * attempt to isolate this page, in case of coliding
  93. * with balloon_page_isolate() just after we release
  94. * the page lock.
  95. *
  96. * balloon_page_free() will take care of dropping
  97. * this extra refcount later.
  98. */
  99. get_page(page);
  100. balloon_page_delete(page);
  101. spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
  102. unlock_page(page);
  103. dequeued_page = true;
  104. break;
  105. }
  106. }
  107. if (!dequeued_page) {
  108. /*
  109. * If we are unable to dequeue a balloon page because the page
  110. * list is empty and there is no isolated pages, then something
  111. * went out of track and some balloon pages are lost.
  112. * BUG() here, otherwise the balloon driver may get stuck into
  113. * an infinite loop while attempting to release all its pages.
  114. */
  115. spin_lock_irqsave(&b_dev_info->pages_lock, flags);
  116. if (unlikely(list_empty(&b_dev_info->pages) &&
  117. !b_dev_info->isolated_pages))
  118. BUG();
  119. spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
  120. page = NULL;
  121. }
  122. return page;
  123. }
  124. EXPORT_SYMBOL_GPL(balloon_page_dequeue);
  125. #ifdef CONFIG_BALLOON_COMPACTION
  126. /*
  127. * balloon_mapping_alloc - allocates a special ->mapping for ballooned pages.
  128. * @b_dev_info: holds the balloon device information descriptor.
  129. * @a_ops: balloon_mapping address_space_operations descriptor.
  130. *
  131. * Driver must call it to properly allocate and initialize an instance of
  132. * struct address_space which will be used as the special page->mapping for
  133. * balloon device enlisted page instances.
  134. */
  135. struct address_space *balloon_mapping_alloc(struct balloon_dev_info *b_dev_info,
  136. const struct address_space_operations *a_ops)
  137. {
  138. struct address_space *mapping;
  139. mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
  140. if (!mapping)
  141. return ERR_PTR(-ENOMEM);
  142. /*
  143. * Give a clean 'zeroed' status to all elements of this special
  144. * balloon page->mapping struct address_space instance.
  145. */
  146. address_space_init_once(mapping);
  147. /*
  148. * Set mapping->flags appropriately, to allow balloon pages
  149. * ->mapping identification.
  150. */
  151. mapping_set_balloon(mapping);
  152. mapping_set_gfp_mask(mapping, balloon_mapping_gfp_mask());
  153. /* balloon's page->mapping->a_ops callback descriptor */
  154. mapping->a_ops = a_ops;
  155. /*
  156. * Establish a pointer reference back to the balloon device descriptor
  157. * this particular page->mapping will be servicing.
  158. * This is used by compaction / migration procedures to identify and
  159. * access the balloon device pageset while isolating / migrating pages.
  160. *
  161. * As some balloon drivers can register multiple balloon devices
  162. * for a single guest, this also helps compaction / migration to
  163. * properly deal with multiple balloon pagesets, when required.
  164. */
  165. mapping->private_data = b_dev_info;
  166. b_dev_info->mapping = mapping;
  167. return mapping;
  168. }
  169. EXPORT_SYMBOL_GPL(balloon_mapping_alloc);
  170. static inline void __isolate_balloon_page(struct page *page)
  171. {
  172. struct balloon_dev_info *b_dev_info = page->mapping->private_data;
  173. unsigned long flags;
  174. spin_lock_irqsave(&b_dev_info->pages_lock, flags);
  175. list_del(&page->lru);
  176. b_dev_info->isolated_pages++;
  177. spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
  178. }
  179. static inline void __putback_balloon_page(struct page *page)
  180. {
  181. struct balloon_dev_info *b_dev_info = page->mapping->private_data;
  182. unsigned long flags;
  183. spin_lock_irqsave(&b_dev_info->pages_lock, flags);
  184. list_add(&page->lru, &b_dev_info->pages);
  185. b_dev_info->isolated_pages--;
  186. spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
  187. }
  188. static inline int __migrate_balloon_page(struct address_space *mapping,
  189. struct page *newpage, struct page *page, enum migrate_mode mode)
  190. {
  191. return page->mapping->a_ops->migratepage(mapping, newpage, page, mode);
  192. }
  193. /* __isolate_lru_page() counterpart for a ballooned page */
  194. bool balloon_page_isolate(struct page *page)
  195. {
  196. /*
  197. * Avoid burning cycles with pages that are yet under __free_pages(),
  198. * or just got freed under us.
  199. *
  200. * In case we 'win' a race for a balloon page being freed under us and
  201. * raise its refcount preventing __free_pages() from doing its job
  202. * the put_page() at the end of this block will take care of
  203. * release this page, thus avoiding a nasty leakage.
  204. */
  205. if (likely(get_page_unless_zero(page))) {
  206. /*
  207. * As balloon pages are not isolated from LRU lists, concurrent
  208. * compaction threads can race against page migration functions
  209. * as well as race against the balloon driver releasing a page.
  210. *
  211. * In order to avoid having an already isolated balloon page
  212. * being (wrongly) re-isolated while it is under migration,
  213. * or to avoid attempting to isolate pages being released by
  214. * the balloon driver, lets be sure we have the page lock
  215. * before proceeding with the balloon page isolation steps.
  216. */
  217. if (likely(trylock_page(page))) {
  218. /*
  219. * A ballooned page, by default, has just one refcount.
  220. * Prevent concurrent compaction threads from isolating
  221. * an already isolated balloon page by refcount check.
  222. */
  223. if (__is_movable_balloon_page(page) &&
  224. page_count(page) == 2) {
  225. __isolate_balloon_page(page);
  226. unlock_page(page);
  227. return true;
  228. }
  229. unlock_page(page);
  230. }
  231. put_page(page);
  232. }
  233. return false;
  234. }
  235. /* putback_lru_page() counterpart for a ballooned page */
  236. void balloon_page_putback(struct page *page)
  237. {
  238. /*
  239. * 'lock_page()' stabilizes the page and prevents races against
  240. * concurrent isolation threads attempting to re-isolate it.
  241. */
  242. lock_page(page);
  243. if (__is_movable_balloon_page(page)) {
  244. __putback_balloon_page(page);
  245. /* drop the extra ref count taken for page isolation */
  246. put_page(page);
  247. } else {
  248. WARN_ON(1);
  249. dump_page(page);
  250. }
  251. unlock_page(page);
  252. }
  253. /* move_to_new_page() counterpart for a ballooned page */
  254. int balloon_page_migrate(struct page *newpage,
  255. struct page *page, enum migrate_mode mode)
  256. {
  257. struct address_space *mapping;
  258. int rc = -EAGAIN;
  259. /*
  260. * Block others from accessing the 'newpage' when we get around to
  261. * establishing additional references. We should be the only one
  262. * holding a reference to the 'newpage' at this point.
  263. */
  264. BUG_ON(!trylock_page(newpage));
  265. if (WARN_ON(!__is_movable_balloon_page(page))) {
  266. dump_page(page);
  267. unlock_page(newpage);
  268. return rc;
  269. }
  270. mapping = page->mapping;
  271. if (mapping)
  272. rc = __migrate_balloon_page(mapping, newpage, page, mode);
  273. unlock_page(newpage);
  274. return rc;
  275. }
  276. #endif /* CONFIG_BALLOON_COMPACTION */