| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * linux/mm/page_isolation.c |
| 4 | */ |
| 5 | |
| 6 | #include <linux/mm.h> |
| 7 | #include <linux/page-isolation.h> |
| 8 | #include <linux/pageblock-flags.h> |
| 9 | #include <linux/memory.h> |
| 10 | #include <linux/hugetlb.h> |
| 11 | #include <linux/page_owner.h> |
| 12 | #include <linux/migrate.h> |
| 13 | #include "internal.h" |
| 14 | |
| 15 | #define CREATE_TRACE_POINTS |
| 16 | #include <trace/events/page_isolation.h> |
| 17 | |
| 18 | static int set_migratetype_isolate(struct page *page, int migratetype, int isol_flags) |
| 19 | { |
| 20 | struct zone *zone; |
| 21 | unsigned long flags, pfn; |
| 22 | struct memory_isolate_notify arg; |
| 23 | int notifier_ret; |
| 24 | int ret = -EBUSY; |
| 25 | |
| 26 | zone = page_zone(page); |
| 27 | |
| 28 | spin_lock_irqsave(&zone->lock, flags); |
| 29 | |
| 30 | /* |
| 31 | * We assume the caller intended to SET migrate type to isolate. |
| 32 | * If it is already set, then someone else must have raced and |
| 33 | * set it before us. Return -EBUSY |
| 34 | */ |
| 35 | if (is_migrate_isolate_page(page)) |
| 36 | goto out; |
| 37 | |
| 38 | pfn = page_to_pfn(page); |
| 39 | arg.start_pfn = pfn; |
| 40 | arg.nr_pages = pageblock_nr_pages; |
| 41 | arg.pages_found = 0; |
| 42 | |
| 43 | /* |
| 44 | * It may be possible to isolate a pageblock even if the |
| 45 | * migratetype is not MIGRATE_MOVABLE. The memory isolation |
| 46 | * notifier chain is used by balloon drivers to return the |
| 47 | * number of pages in a range that are held by the balloon |
| 48 | * driver to shrink memory. If all the pages are accounted for |
| 49 | * by balloons, are free, or on the LRU, isolation can continue. |
| 50 | * Later, for example, when memory hotplug notifier runs, these |
| 51 | * pages reported as "can be isolated" should be isolated(freed) |
| 52 | * by the balloon driver through the memory notifier chain. |
| 53 | */ |
| 54 | notifier_ret = memory_isolate_notify(MEM_ISOLATE_COUNT, &arg); |
| 55 | notifier_ret = notifier_to_errno(notifier_ret); |
| 56 | if (notifier_ret) |
| 57 | goto out; |
| 58 | /* |
| 59 | * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself. |
| 60 | * We just check MOVABLE pages. |
| 61 | */ |
| 62 | if (!has_unmovable_pages(zone, page, arg.pages_found, migratetype, flags)) |
| 63 | ret = 0; |
| 64 | |
| 65 | /* |
| 66 | * immobile means "not-on-lru" pages. If immobile is larger than |
| 67 | * removable-by-driver pages reported by notifier, we'll fail. |
| 68 | */ |
| 69 | |
| 70 | out: |
| 71 | if (!ret) { |
| 72 | unsigned long nr_pages; |
| 73 | int mt = get_pageblock_migratetype(page); |
| 74 | |
| 75 | set_pageblock_migratetype(page, MIGRATE_ISOLATE); |
| 76 | zone->nr_isolate_pageblock++; |
| 77 | nr_pages = move_freepages_block(zone, page, MIGRATE_ISOLATE, |
| 78 | NULL); |
| 79 | |
| 80 | __mod_zone_freepage_state(zone, -nr_pages, mt); |
| 81 | } |
| 82 | |
| 83 | spin_unlock_irqrestore(&zone->lock, flags); |
| 84 | if (!ret) |
| 85 | drain_all_pages(zone); |
| 86 | return ret; |
| 87 | } |
| 88 | |
| 89 | static void unset_migratetype_isolate(struct page *page, unsigned migratetype) |
| 90 | { |
| 91 | struct zone *zone; |
| 92 | unsigned long flags, nr_pages; |
| 93 | bool isolated_page = false; |
| 94 | unsigned int order; |
| 95 | unsigned long pfn, buddy_pfn; |
| 96 | struct page *buddy; |
| 97 | |
| 98 | zone = page_zone(page); |
| 99 | spin_lock_irqsave(&zone->lock, flags); |
| 100 | if (!is_migrate_isolate_page(page)) |
| 101 | goto out; |
| 102 | |
| 103 | /* |
| 104 | * Because freepage with more than pageblock_order on isolated |
| 105 | * pageblock is restricted to merge due to freepage counting problem, |
| 106 | * it is possible that there is free buddy page. |
| 107 | * move_freepages_block() doesn't care of merge so we need other |
| 108 | * approach in order to merge them. Isolation and free will make |
| 109 | * these pages to be merged. |
| 110 | */ |
| 111 | if (PageBuddy(page)) { |
| 112 | order = page_order(page); |
| 113 | if (order >= pageblock_order) { |
| 114 | pfn = page_to_pfn(page); |
| 115 | buddy_pfn = __find_buddy_pfn(pfn, order); |
| 116 | buddy = page + (buddy_pfn - pfn); |
| 117 | |
| 118 | if (pfn_valid_within(buddy_pfn) && |
| 119 | !is_migrate_isolate_page(buddy)) { |
| 120 | __isolate_free_page(page, order); |
| 121 | isolated_page = true; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * If we isolate freepage with more than pageblock_order, there |
| 128 | * should be no freepage in the range, so we could avoid costly |
| 129 | * pageblock scanning for freepage moving. |
| 130 | */ |
| 131 | if (!isolated_page) { |
| 132 | nr_pages = move_freepages_block(zone, page, migratetype, NULL); |
| 133 | __mod_zone_freepage_state(zone, nr_pages, migratetype); |
| 134 | } |
| 135 | set_pageblock_migratetype(page, migratetype); |
| 136 | zone->nr_isolate_pageblock--; |
| 137 | out: |
| 138 | spin_unlock_irqrestore(&zone->lock, flags); |
| 139 | if (isolated_page) { |
| 140 | post_alloc_hook(page, order, __GFP_MOVABLE); |
| 141 | __free_pages(page, order); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | static inline struct page * |
| 146 | __first_valid_page(unsigned long pfn, unsigned long nr_pages) |
| 147 | { |
| 148 | int i; |
| 149 | |
| 150 | for (i = 0; i < nr_pages; i++) { |
| 151 | struct page *page; |
| 152 | |
| 153 | if (!pfn_valid_within(pfn + i)) |
| 154 | continue; |
| 155 | page = pfn_to_online_page(pfn + i); |
| 156 | if (!page) |
| 157 | continue; |
| 158 | return page; |
| 159 | } |
| 160 | return NULL; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * start_isolate_page_range() -- make page-allocation-type of range of pages |
| 165 | * to be MIGRATE_ISOLATE. |
| 166 | * @start_pfn: The lower PFN of the range to be isolated. |
| 167 | * @end_pfn: The upper PFN of the range to be isolated. |
| 168 | * @migratetype: migrate type to set in error recovery. |
| 169 | * |
| 170 | * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in |
| 171 | * the range will never be allocated. Any free pages and pages freed in the |
| 172 | * future will not be allocated again. |
| 173 | * |
| 174 | * start_pfn/end_pfn must be aligned to pageblock_order. |
| 175 | * Return 0 on success and -EBUSY if any part of range cannot be isolated. |
| 176 | * |
| 177 | * There is no high level synchronization mechanism that prevents two threads |
| 178 | * from trying to isolate overlapping ranges. If this happens, one thread |
| 179 | * will notice pageblocks in the overlapping range already set to isolate. |
| 180 | * This happens in set_migratetype_isolate, and set_migratetype_isolate |
| 181 | * returns an error. We then clean up by restoring the migration type on |
| 182 | * pageblocks we may have modified and return -EBUSY to caller. This |
| 183 | * prevents two threads from simultaneously working on overlapping ranges. |
| 184 | */ |
| 185 | int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn, |
| 186 | unsigned migratetype, int flags) |
| 187 | { |
| 188 | unsigned long pfn; |
| 189 | unsigned long undo_pfn; |
| 190 | struct page *page; |
| 191 | |
| 192 | BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages)); |
| 193 | BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages)); |
| 194 | |
| 195 | for (pfn = start_pfn; |
| 196 | pfn < end_pfn; |
| 197 | pfn += pageblock_nr_pages) { |
| 198 | page = __first_valid_page(pfn, pageblock_nr_pages); |
| 199 | if (page && |
| 200 | set_migratetype_isolate(page, migratetype, flags)) { |
| 201 | undo_pfn = pfn; |
| 202 | goto undo; |
| 203 | } |
| 204 | } |
| 205 | return 0; |
| 206 | undo: |
| 207 | for (pfn = start_pfn; |
| 208 | pfn < undo_pfn; |
| 209 | pfn += pageblock_nr_pages) { |
| 210 | struct page *page = pfn_to_online_page(pfn); |
| 211 | if (!page) |
| 212 | continue; |
| 213 | unset_migratetype_isolate(page, migratetype); |
| 214 | } |
| 215 | |
| 216 | return -EBUSY; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * Make isolated pages available again. |
| 221 | */ |
| 222 | int undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn, |
| 223 | unsigned migratetype) |
| 224 | { |
| 225 | unsigned long pfn; |
| 226 | struct page *page; |
| 227 | |
| 228 | BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages)); |
| 229 | BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages)); |
| 230 | |
| 231 | for (pfn = start_pfn; |
| 232 | pfn < end_pfn; |
| 233 | pfn += pageblock_nr_pages) { |
| 234 | page = __first_valid_page(pfn, pageblock_nr_pages); |
| 235 | if (!page || !is_migrate_isolate_page(page)) |
| 236 | continue; |
| 237 | unset_migratetype_isolate(page, migratetype); |
| 238 | } |
| 239 | return 0; |
| 240 | } |
| 241 | /* |
| 242 | * Test all pages in the range is free(means isolated) or not. |
| 243 | * all pages in [start_pfn...end_pfn) must be in the same zone. |
| 244 | * zone->lock must be held before call this. |
| 245 | * |
| 246 | * Returns the last tested pfn. |
| 247 | */ |
| 248 | static unsigned long |
| 249 | __test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn, |
| 250 | bool skip_hwpoisoned_pages) |
| 251 | { |
| 252 | struct page *page; |
| 253 | |
| 254 | while (pfn < end_pfn) { |
| 255 | if (!pfn_valid_within(pfn)) { |
| 256 | pfn++; |
| 257 | continue; |
| 258 | } |
| 259 | page = pfn_to_page(pfn); |
| 260 | if (PageBuddy(page)) |
| 261 | /* |
| 262 | * If the page is on a free list, it has to be on |
| 263 | * the correct MIGRATE_ISOLATE freelist. There is no |
| 264 | * simple way to verify that as VM_BUG_ON(), though. |
| 265 | */ |
| 266 | pfn += 1 << page_order(page); |
| 267 | else if (skip_hwpoisoned_pages && PageHWPoison(page)) |
| 268 | /* A HWPoisoned page cannot be also PageBuddy */ |
| 269 | pfn++; |
| 270 | else |
| 271 | break; |
| 272 | } |
| 273 | |
| 274 | return pfn; |
| 275 | } |
| 276 | |
| 277 | /* Caller should ensure that requested range is in a single zone */ |
| 278 | int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn, |
| 279 | bool skip_hwpoisoned_pages) |
| 280 | { |
| 281 | unsigned long pfn, flags; |
| 282 | struct page *page; |
| 283 | struct zone *zone; |
| 284 | |
| 285 | /* |
| 286 | * Note: pageblock_nr_pages != MAX_ORDER. Then, chunks of free pages |
| 287 | * are not aligned to pageblock_nr_pages. |
| 288 | * Then we just check migratetype first. |
| 289 | */ |
| 290 | for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) { |
| 291 | page = __first_valid_page(pfn, pageblock_nr_pages); |
| 292 | if (page && !is_migrate_isolate_page(page)) |
| 293 | break; |
| 294 | } |
| 295 | page = __first_valid_page(start_pfn, end_pfn - start_pfn); |
| 296 | if ((pfn < end_pfn) || !page) |
| 297 | return -EBUSY; |
| 298 | /* Check all pages are free or marked as ISOLATED */ |
| 299 | zone = page_zone(page); |
| 300 | spin_lock_irqsave(&zone->lock, flags); |
| 301 | pfn = __test_page_isolated_in_pageblock(start_pfn, end_pfn, |
| 302 | skip_hwpoisoned_pages); |
| 303 | spin_unlock_irqrestore(&zone->lock, flags); |
| 304 | |
| 305 | trace_test_pages_isolated(start_pfn, end_pfn, pfn); |
| 306 | |
| 307 | return pfn < end_pfn ? -EBUSY : 0; |
| 308 | } |
| 309 | |
| 310 | struct page *alloc_migrate_target(struct page *page, unsigned long private) |
| 311 | { |
| 312 | return new_page_nodemask(page, numa_node_id(), &node_states[N_MEMORY]); |
| 313 | } |