page_isolation.c 7.2 KB

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