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