balloon_compaction.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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_page_alloc - allocates a new page for insertion into the balloon
  14. * page list.
  15. *
  16. * Driver must call it to properly allocate a new enlisted balloon page.
  17. * Driver must call balloon_page_enqueue before definitively removing it from
  18. * the guest system. This function returns the page address for the recently
  19. * allocated page or NULL in the case we fail to allocate a new page this turn.
  20. */
  21. struct page *balloon_page_alloc(void)
  22. {
  23. struct page *page = alloc_page(balloon_mapping_gfp_mask() |
  24. __GFP_NOMEMALLOC | __GFP_NORETRY);
  25. return page;
  26. }
  27. EXPORT_SYMBOL_GPL(balloon_page_alloc);
  28. /*
  29. * balloon_page_enqueue - allocates a new page and inserts it into the balloon
  30. * page list.
  31. * @b_dev_info: balloon device descriptor where we will insert a new page to
  32. * @page: new page to enqueue - allocated using balloon_page_alloc.
  33. *
  34. * Driver must call it to properly enqueue a new allocated balloon page
  35. * before definitively removing it from the guest system.
  36. * This function returns the page address for the recently enqueued page or
  37. * NULL in the case we fail to allocate a new page this turn.
  38. */
  39. void balloon_page_enqueue(struct balloon_dev_info *b_dev_info,
  40. struct page *page)
  41. {
  42. unsigned long flags;
  43. /*
  44. * Block others from accessing the 'page' when we get around to
  45. * establishing additional references. We should be the only one
  46. * holding a reference to the 'page' at this point.
  47. */
  48. BUG_ON(!trylock_page(page));
  49. spin_lock_irqsave(&b_dev_info->pages_lock, flags);
  50. balloon_page_insert(b_dev_info, page);
  51. __count_vm_event(BALLOON_INFLATE);
  52. spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
  53. unlock_page(page);
  54. }
  55. EXPORT_SYMBOL_GPL(balloon_page_enqueue);
  56. /*
  57. * balloon_page_dequeue - removes a page from balloon's page list and returns
  58. * the its address to allow the driver release the page.
  59. * @b_dev_info: balloon device decriptor where we will grab a page from.
  60. *
  61. * Driver must call it to properly de-allocate a previous enlisted balloon page
  62. * before definetively releasing it back to the guest system.
  63. * This function returns the page address for the recently dequeued page or
  64. * NULL in the case we find balloon's page list temporarily empty due to
  65. * compaction isolated pages.
  66. */
  67. struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info)
  68. {
  69. struct page *page, *tmp;
  70. unsigned long flags;
  71. bool dequeued_page;
  72. dequeued_page = false;
  73. spin_lock_irqsave(&b_dev_info->pages_lock, flags);
  74. list_for_each_entry_safe(page, tmp, &b_dev_info->pages, lru) {
  75. /*
  76. * Block others from accessing the 'page' while we get around
  77. * establishing additional references and preparing the 'page'
  78. * to be released by the balloon driver.
  79. */
  80. if (trylock_page(page)) {
  81. #ifdef CONFIG_BALLOON_COMPACTION
  82. if (PageIsolated(page)) {
  83. /* raced with isolation */
  84. unlock_page(page);
  85. continue;
  86. }
  87. #endif
  88. balloon_page_delete(page);
  89. __count_vm_event(BALLOON_DEFLATE);
  90. unlock_page(page);
  91. dequeued_page = true;
  92. break;
  93. }
  94. }
  95. spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
  96. if (!dequeued_page) {
  97. /*
  98. * If we are unable to dequeue a balloon page because the page
  99. * list is empty and there is no isolated pages, then something
  100. * went out of track and some balloon pages are lost.
  101. * BUG() here, otherwise the balloon driver may get stuck into
  102. * an infinite loop while attempting to release all its pages.
  103. */
  104. spin_lock_irqsave(&b_dev_info->pages_lock, flags);
  105. if (unlikely(list_empty(&b_dev_info->pages) &&
  106. !b_dev_info->isolated_pages))
  107. BUG();
  108. spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
  109. page = NULL;
  110. }
  111. return page;
  112. }
  113. EXPORT_SYMBOL_GPL(balloon_page_dequeue);
  114. #ifdef CONFIG_BALLOON_COMPACTION
  115. bool balloon_page_isolate(struct page *page, isolate_mode_t mode)
  116. {
  117. struct balloon_dev_info *b_dev_info = balloon_page_device(page);
  118. unsigned long flags;
  119. spin_lock_irqsave(&b_dev_info->pages_lock, flags);
  120. list_del(&page->lru);
  121. b_dev_info->isolated_pages++;
  122. spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
  123. return true;
  124. }
  125. void balloon_page_putback(struct page *page)
  126. {
  127. struct balloon_dev_info *b_dev_info = balloon_page_device(page);
  128. unsigned long flags;
  129. spin_lock_irqsave(&b_dev_info->pages_lock, flags);
  130. list_add(&page->lru, &b_dev_info->pages);
  131. b_dev_info->isolated_pages--;
  132. spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
  133. }
  134. /* move_to_new_page() counterpart for a ballooned page */
  135. int balloon_page_migrate(struct address_space *mapping,
  136. struct page *newpage, struct page *page,
  137. enum migrate_mode mode)
  138. {
  139. struct balloon_dev_info *balloon = balloon_page_device(page);
  140. /*
  141. * We can not easily support the no copy case here so ignore it as it
  142. * is unlikely to be use with ballon pages. See include/linux/hmm.h for
  143. * user of the MIGRATE_SYNC_NO_COPY mode.
  144. */
  145. if (mode == MIGRATE_SYNC_NO_COPY)
  146. return -EINVAL;
  147. VM_BUG_ON_PAGE(!PageLocked(page), page);
  148. VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
  149. return balloon->migratepage(balloon, newpage, page, mode);
  150. }
  151. const struct address_space_operations balloon_aops = {
  152. .migratepage = balloon_page_migrate,
  153. .isolate_page = balloon_page_isolate,
  154. .putback_page = balloon_page_putback,
  155. };
  156. EXPORT_SYMBOL_GPL(balloon_aops);
  157. #endif /* CONFIG_BALLOON_COMPACTION */