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