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