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