page_isolation.c 8.3 KB

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