page_isolation.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 "internal.h"
  8. static inline struct page *
  9. __first_valid_page(unsigned long pfn, unsigned long nr_pages)
  10. {
  11. int i;
  12. for (i = 0; i < nr_pages; i++)
  13. if (pfn_valid_within(pfn + i))
  14. break;
  15. if (unlikely(i == nr_pages))
  16. return NULL;
  17. return pfn_to_page(pfn + i);
  18. }
  19. /*
  20. * start_isolate_page_range() -- make page-allocation-type of range of pages
  21. * to be MIGRATE_ISOLATE.
  22. * @start_pfn: The lower PFN of the range to be isolated.
  23. * @end_pfn: The upper PFN of the range to be isolated.
  24. * @migratetype: migrate type to set in error recovery.
  25. *
  26. * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in
  27. * the range will never be allocated. Any free pages and pages freed in the
  28. * future will not be allocated again.
  29. *
  30. * start_pfn/end_pfn must be aligned to pageblock_order.
  31. * Returns 0 on success and -EBUSY if any part of range cannot be isolated.
  32. */
  33. int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
  34. unsigned migratetype)
  35. {
  36. unsigned long pfn;
  37. unsigned long undo_pfn;
  38. struct page *page;
  39. BUG_ON((start_pfn) & (pageblock_nr_pages - 1));
  40. BUG_ON((end_pfn) & (pageblock_nr_pages - 1));
  41. for (pfn = start_pfn;
  42. pfn < end_pfn;
  43. pfn += pageblock_nr_pages) {
  44. page = __first_valid_page(pfn, pageblock_nr_pages);
  45. if (page && set_migratetype_isolate(page)) {
  46. undo_pfn = pfn;
  47. goto undo;
  48. }
  49. }
  50. return 0;
  51. undo:
  52. for (pfn = start_pfn;
  53. pfn < undo_pfn;
  54. pfn += pageblock_nr_pages)
  55. unset_migratetype_isolate(pfn_to_page(pfn), migratetype);
  56. return -EBUSY;
  57. }
  58. /*
  59. * Make isolated pages available again.
  60. */
  61. int undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
  62. unsigned migratetype)
  63. {
  64. unsigned long pfn;
  65. struct page *page;
  66. BUG_ON((start_pfn) & (pageblock_nr_pages - 1));
  67. BUG_ON((end_pfn) & (pageblock_nr_pages - 1));
  68. for (pfn = start_pfn;
  69. pfn < end_pfn;
  70. pfn += pageblock_nr_pages) {
  71. page = __first_valid_page(pfn, pageblock_nr_pages);
  72. if (!page || get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
  73. continue;
  74. unset_migratetype_isolate(page, migratetype);
  75. }
  76. return 0;
  77. }
  78. /*
  79. * Test all pages in the range is free(means isolated) or not.
  80. * all pages in [start_pfn...end_pfn) must be in the same zone.
  81. * zone->lock must be held before call this.
  82. *
  83. * Returns 1 if all pages in the range are isolated.
  84. */
  85. static int
  86. __test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn)
  87. {
  88. struct page *page;
  89. while (pfn < end_pfn) {
  90. if (!pfn_valid_within(pfn)) {
  91. pfn++;
  92. continue;
  93. }
  94. page = pfn_to_page(pfn);
  95. if (PageBuddy(page)) {
  96. /*
  97. * If race between isolatation and allocation happens,
  98. * some free pages could be in MIGRATE_MOVABLE list
  99. * although pageblock's migratation type of the page
  100. * is MIGRATE_ISOLATE. Catch it and move the page into
  101. * MIGRATE_ISOLATE list.
  102. */
  103. if (get_freepage_migratetype(page) != MIGRATE_ISOLATE) {
  104. struct page *end_page;
  105. end_page = page + (1 << page_order(page)) - 1;
  106. move_freepages(page_zone(page), page, end_page,
  107. MIGRATE_ISOLATE);
  108. }
  109. pfn += 1 << page_order(page);
  110. }
  111. else if (page_count(page) == 0 &&
  112. get_freepage_migratetype(page) == MIGRATE_ISOLATE)
  113. pfn += 1;
  114. else
  115. break;
  116. }
  117. if (pfn < end_pfn)
  118. return 0;
  119. return 1;
  120. }
  121. int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
  122. {
  123. unsigned long pfn, flags;
  124. struct page *page;
  125. struct zone *zone;
  126. int ret;
  127. /*
  128. * Note: pageblock_nr_page != MAX_ORDER. Then, chunks of free page
  129. * is not aligned to pageblock_nr_pages.
  130. * Then we just check pagetype fist.
  131. */
  132. for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
  133. page = __first_valid_page(pfn, pageblock_nr_pages);
  134. if (page && get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
  135. break;
  136. }
  137. page = __first_valid_page(start_pfn, end_pfn - start_pfn);
  138. if ((pfn < end_pfn) || !page)
  139. return -EBUSY;
  140. /* Check all pages are free or Marked as ISOLATED */
  141. zone = page_zone(page);
  142. spin_lock_irqsave(&zone->lock, flags);
  143. ret = __test_page_isolated_in_pageblock(start_pfn, end_pfn);
  144. spin_unlock_irqrestore(&zone->lock, flags);
  145. return ret ? 0 : -EBUSY;
  146. }