page_isolation.c 8.3 KB

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