page_isolation.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. *
  25. * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in
  26. * the range will never be allocated. Any free pages and pages freed in the
  27. * future will not be allocated again.
  28. *
  29. * start_pfn/end_pfn must be aligned to pageblock_order.
  30. * Returns 0 on success and -EBUSY if any part of range cannot be isolated.
  31. */
  32. int
  33. start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn)
  34. {
  35. unsigned long pfn;
  36. unsigned long undo_pfn;
  37. struct page *page;
  38. BUG_ON((start_pfn) & (pageblock_nr_pages - 1));
  39. BUG_ON((end_pfn) & (pageblock_nr_pages - 1));
  40. for (pfn = start_pfn;
  41. pfn < end_pfn;
  42. pfn += pageblock_nr_pages) {
  43. page = __first_valid_page(pfn, pageblock_nr_pages);
  44. if (page && set_migratetype_isolate(page)) {
  45. undo_pfn = pfn;
  46. goto undo;
  47. }
  48. }
  49. return 0;
  50. undo:
  51. for (pfn = start_pfn;
  52. pfn < undo_pfn;
  53. pfn += pageblock_nr_pages)
  54. unset_migratetype_isolate(pfn_to_page(pfn));
  55. return -EBUSY;
  56. }
  57. /*
  58. * Make isolated pages available again.
  59. */
  60. int
  61. undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn)
  62. {
  63. unsigned long pfn;
  64. struct page *page;
  65. BUG_ON((start_pfn) & (pageblock_nr_pages - 1));
  66. BUG_ON((end_pfn) & (pageblock_nr_pages - 1));
  67. for (pfn = start_pfn;
  68. pfn < end_pfn;
  69. pfn += pageblock_nr_pages) {
  70. page = __first_valid_page(pfn, pageblock_nr_pages);
  71. if (!page || get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
  72. continue;
  73. unset_migratetype_isolate(page);
  74. }
  75. return 0;
  76. }
  77. /*
  78. * Test all pages in the range is free(means isolated) or not.
  79. * all pages in [start_pfn...end_pfn) must be in the same zone.
  80. * zone->lock must be held before call this.
  81. *
  82. * Returns 1 if all pages in the range is isolated.
  83. */
  84. static int
  85. __test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn)
  86. {
  87. struct page *page;
  88. while (pfn < end_pfn) {
  89. if (!pfn_valid_within(pfn)) {
  90. pfn++;
  91. continue;
  92. }
  93. page = pfn_to_page(pfn);
  94. if (PageBuddy(page))
  95. pfn += 1 << page_order(page);
  96. else if (page_count(page) == 0 &&
  97. page_private(page) == MIGRATE_ISOLATE)
  98. pfn += 1;
  99. else
  100. break;
  101. }
  102. if (pfn < end_pfn)
  103. return 0;
  104. return 1;
  105. }
  106. int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
  107. {
  108. unsigned long pfn, flags;
  109. struct page *page;
  110. struct zone *zone;
  111. int ret;
  112. /*
  113. * Note: pageblock_nr_page != MAX_ORDER. Then, chunks of free page
  114. * is not aligned to pageblock_nr_pages.
  115. * Then we just check pagetype fist.
  116. */
  117. for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
  118. page = __first_valid_page(pfn, pageblock_nr_pages);
  119. if (page && get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
  120. break;
  121. }
  122. page = __first_valid_page(start_pfn, end_pfn - start_pfn);
  123. if ((pfn < end_pfn) || !page)
  124. return -EBUSY;
  125. /* Check all pages are free or Marked as ISOLATED */
  126. zone = page_zone(page);
  127. spin_lock_irqsave(&zone->lock, flags);
  128. ret = __test_page_isolated_in_pageblock(start_pfn, end_pfn);
  129. spin_unlock_irqrestore(&zone->lock, flags);
  130. return ret ? 0 : -EBUSY;
  131. }