page_isolation.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * linux/mm/page_isolation.c
  3. */
  4. #include <linux/mm.h>
  5. #include <linux/page-isolation.h>
  6. #include <linux/pageblock-flags.h>
  7. #include <linux/memory.h>
  8. #include <linux/hugetlb.h>
  9. #include "internal.h"
  10. int set_migratetype_isolate(struct page *page, bool skip_hwpoisoned_pages)
  11. {
  12. struct zone *zone;
  13. unsigned long flags, pfn;
  14. struct memory_isolate_notify arg;
  15. int notifier_ret;
  16. int ret = -EBUSY;
  17. zone = page_zone(page);
  18. spin_lock_irqsave(&zone->lock, flags);
  19. pfn = page_to_pfn(page);
  20. arg.start_pfn = pfn;
  21. arg.nr_pages = pageblock_nr_pages;
  22. arg.pages_found = 0;
  23. /*
  24. * It may be possible to isolate a pageblock even if the
  25. * migratetype is not MIGRATE_MOVABLE. The memory isolation
  26. * notifier chain is used by balloon drivers to return the
  27. * number of pages in a range that are held by the balloon
  28. * driver to shrink memory. If all the pages are accounted for
  29. * by balloons, are free, or on the LRU, isolation can continue.
  30. * Later, for example, when memory hotplug notifier runs, these
  31. * pages reported as "can be isolated" should be isolated(freed)
  32. * by the balloon driver through the memory notifier chain.
  33. */
  34. notifier_ret = memory_isolate_notify(MEM_ISOLATE_COUNT, &arg);
  35. notifier_ret = notifier_to_errno(notifier_ret);
  36. if (notifier_ret)
  37. goto out;
  38. /*
  39. * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
  40. * We just check MOVABLE pages.
  41. */
  42. if (!has_unmovable_pages(zone, page, arg.pages_found,
  43. skip_hwpoisoned_pages))
  44. ret = 0;
  45. /*
  46. * immobile means "not-on-lru" paes. If immobile is larger than
  47. * removable-by-driver pages reported by notifier, we'll fail.
  48. */
  49. out:
  50. if (!ret) {
  51. unsigned long nr_pages;
  52. int migratetype = get_pageblock_migratetype(page);
  53. set_pageblock_migratetype(page, MIGRATE_ISOLATE);
  54. nr_pages = move_freepages_block(zone, page, MIGRATE_ISOLATE);
  55. __mod_zone_freepage_state(zone, -nr_pages, migratetype);
  56. }
  57. spin_unlock_irqrestore(&zone->lock, flags);
  58. if (!ret)
  59. drain_all_pages();
  60. return ret;
  61. }
  62. void unset_migratetype_isolate(struct page *page, unsigned migratetype)
  63. {
  64. struct zone *zone;
  65. unsigned long flags, nr_pages;
  66. zone = page_zone(page);
  67. spin_lock_irqsave(&zone->lock, flags);
  68. if (get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
  69. goto out;
  70. nr_pages = move_freepages_block(zone, page, migratetype);
  71. __mod_zone_freepage_state(zone, nr_pages, migratetype);
  72. set_pageblock_migratetype(page, migratetype);
  73. out:
  74. spin_unlock_irqrestore(&zone->lock, flags);
  75. }
  76. static inline struct page *
  77. __first_valid_page(unsigned long pfn, unsigned long nr_pages)
  78. {
  79. int i;
  80. for (i = 0; i < nr_pages; i++)
  81. if (pfn_valid_within(pfn + i))
  82. break;
  83. if (unlikely(i == nr_pages))
  84. return NULL;
  85. return pfn_to_page(pfn + i);
  86. }
  87. /*
  88. * start_isolate_page_range() -- make page-allocation-type of range of pages
  89. * to be MIGRATE_ISOLATE.
  90. * @start_pfn: The lower PFN of the range to be isolated.
  91. * @end_pfn: The upper PFN of the range to be isolated.
  92. * @migratetype: migrate type to set in error recovery.
  93. *
  94. * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in
  95. * the range will never be allocated. Any free pages and pages freed in the
  96. * future will not be allocated again.
  97. *
  98. * start_pfn/end_pfn must be aligned to pageblock_order.
  99. * Returns 0 on success and -EBUSY if any part of range cannot be isolated.
  100. */
  101. int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
  102. unsigned migratetype, bool skip_hwpoisoned_pages)
  103. {
  104. unsigned long pfn;
  105. unsigned long undo_pfn;
  106. struct page *page;
  107. BUG_ON((start_pfn) & (pageblock_nr_pages - 1));
  108. BUG_ON((end_pfn) & (pageblock_nr_pages - 1));
  109. for (pfn = start_pfn;
  110. pfn < end_pfn;
  111. pfn += pageblock_nr_pages) {
  112. page = __first_valid_page(pfn, pageblock_nr_pages);
  113. if (page &&
  114. set_migratetype_isolate(page, skip_hwpoisoned_pages)) {
  115. undo_pfn = pfn;
  116. goto undo;
  117. }
  118. }
  119. return 0;
  120. undo:
  121. for (pfn = start_pfn;
  122. pfn < undo_pfn;
  123. pfn += pageblock_nr_pages)
  124. unset_migratetype_isolate(pfn_to_page(pfn), migratetype);
  125. return -EBUSY;
  126. }
  127. /*
  128. * Make isolated pages available again.
  129. */
  130. int undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
  131. unsigned migratetype)
  132. {
  133. unsigned long pfn;
  134. struct page *page;
  135. BUG_ON((start_pfn) & (pageblock_nr_pages - 1));
  136. BUG_ON((end_pfn) & (pageblock_nr_pages - 1));
  137. for (pfn = start_pfn;
  138. pfn < end_pfn;
  139. pfn += pageblock_nr_pages) {
  140. page = __first_valid_page(pfn, pageblock_nr_pages);
  141. if (!page || get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
  142. continue;
  143. unset_migratetype_isolate(page, migratetype);
  144. }
  145. return 0;
  146. }
  147. /*
  148. * Test all pages in the range is free(means isolated) or not.
  149. * all pages in [start_pfn...end_pfn) must be in the same zone.
  150. * zone->lock must be held before call this.
  151. *
  152. * Returns 1 if all pages in the range are isolated.
  153. */
  154. static int
  155. __test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn,
  156. bool skip_hwpoisoned_pages)
  157. {
  158. struct page *page;
  159. while (pfn < end_pfn) {
  160. if (!pfn_valid_within(pfn)) {
  161. pfn++;
  162. continue;
  163. }
  164. page = pfn_to_page(pfn);
  165. if (PageBuddy(page)) {
  166. /*
  167. * If race between isolatation and allocation happens,
  168. * some free pages could be in MIGRATE_MOVABLE list
  169. * although pageblock's migratation type of the page
  170. * is MIGRATE_ISOLATE. Catch it and move the page into
  171. * MIGRATE_ISOLATE list.
  172. */
  173. if (get_freepage_migratetype(page) != MIGRATE_ISOLATE) {
  174. struct page *end_page;
  175. end_page = page + (1 << page_order(page)) - 1;
  176. move_freepages(page_zone(page), page, end_page,
  177. MIGRATE_ISOLATE);
  178. }
  179. pfn += 1 << page_order(page);
  180. }
  181. else if (page_count(page) == 0 &&
  182. get_freepage_migratetype(page) == MIGRATE_ISOLATE)
  183. pfn += 1;
  184. else if (skip_hwpoisoned_pages && PageHWPoison(page)) {
  185. /*
  186. * The HWPoisoned page may be not in buddy
  187. * system, and page_count() is not 0.
  188. */
  189. pfn++;
  190. continue;
  191. }
  192. else
  193. break;
  194. }
  195. if (pfn < end_pfn)
  196. return 0;
  197. return 1;
  198. }
  199. int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn,
  200. bool skip_hwpoisoned_pages)
  201. {
  202. unsigned long pfn, flags;
  203. struct page *page;
  204. struct zone *zone;
  205. int ret;
  206. /*
  207. * Note: pageblock_nr_pages != MAX_ORDER. Then, chunks of free pages
  208. * are not aligned to pageblock_nr_pages.
  209. * Then we just check migratetype first.
  210. */
  211. for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
  212. page = __first_valid_page(pfn, pageblock_nr_pages);
  213. if (page && get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
  214. break;
  215. }
  216. page = __first_valid_page(start_pfn, end_pfn - start_pfn);
  217. if ((pfn < end_pfn) || !page)
  218. return -EBUSY;
  219. /* Check all pages are free or marked as ISOLATED */
  220. zone = page_zone(page);
  221. spin_lock_irqsave(&zone->lock, flags);
  222. ret = __test_page_isolated_in_pageblock(start_pfn, end_pfn,
  223. skip_hwpoisoned_pages);
  224. spin_unlock_irqrestore(&zone->lock, flags);
  225. return ret ? 0 : -EBUSY;
  226. }
  227. struct page *alloc_migrate_target(struct page *page, unsigned long private,
  228. int **resultp)
  229. {
  230. gfp_t gfp_mask = GFP_USER | __GFP_MOVABLE;
  231. /*
  232. * TODO: allocate a destination hugepage from a nearest neighbor node,
  233. * accordance with memory policy of the user process if possible. For
  234. * now as a simple work-around, we use the next node for destination.
  235. */
  236. if (PageHuge(page)) {
  237. nodemask_t src = nodemask_of_node(page_to_nid(page));
  238. nodemask_t dst;
  239. nodes_complement(dst, src);
  240. return alloc_huge_page_node(page_hstate(compound_head(page)),
  241. next_node(page_to_nid(page), dst));
  242. }
  243. if (PageHighMem(page))
  244. gfp_mask |= __GFP_HIGHMEM;
  245. return alloc_page(gfp_mask);
  246. }