1 // SPDX-License-Identifier: GPL-2.0 1 // SPDX-License-Identifier: GPL-2.0 2 /* 2 /* 3 * linux/mm/page_isolation.c 3 * linux/mm/page_isolation.c 4 */ 4 */ 5 5 6 #include <linux/mm.h> 6 #include <linux/mm.h> 7 #include <linux/page-isolation.h> 7 #include <linux/page-isolation.h> 8 #include <linux/pageblock-flags.h> 8 #include <linux/pageblock-flags.h> 9 #include <linux/memory.h> 9 #include <linux/memory.h> 10 #include <linux/hugetlb.h> 10 #include <linux/hugetlb.h> 11 #include <linux/page_owner.h> 11 #include <linux/page_owner.h> 12 #include <linux/migrate.h> 12 #include <linux/migrate.h> 13 #include "internal.h" 13 #include "internal.h" 14 14 15 #define CREATE_TRACE_POINTS 15 #define CREATE_TRACE_POINTS 16 #include <trace/events/page_isolation.h> 16 #include <trace/events/page_isolation.h> 17 17 18 /* 18 /* 19 * This function checks whether the range [sta 19 * This function checks whether the range [start_pfn, end_pfn) includes 20 * unmovable pages or not. The range must fall 20 * unmovable pages or not. The range must fall into a single pageblock and 21 * consequently belong to a single zone. 21 * consequently belong to a single zone. 22 * 22 * 23 * PageLRU check without isolation or lru_lock 23 * PageLRU check without isolation or lru_lock could race so that 24 * MIGRATE_MOVABLE block might include unmovab 24 * MIGRATE_MOVABLE block might include unmovable pages. And __PageMovable 25 * check without lock_page also may miss some 25 * check without lock_page also may miss some movable non-lru pages at 26 * race condition. So you can't expect this fu 26 * race condition. So you can't expect this function should be exact. 27 * 27 * 28 * Returns a page without holding a reference. 28 * Returns a page without holding a reference. If the caller wants to 29 * dereference that page (e.g., dumping), it h 29 * dereference that page (e.g., dumping), it has to make sure that it 30 * cannot get removed (e.g., via memory unplug 30 * cannot get removed (e.g., via memory unplug) concurrently. 31 * 31 * 32 */ 32 */ 33 static struct page *has_unmovable_pages(unsign 33 static struct page *has_unmovable_pages(unsigned long start_pfn, unsigned long end_pfn, 34 int migratetyp 34 int migratetype, int flags) 35 { 35 { 36 struct page *page = pfn_to_page(start_ 36 struct page *page = pfn_to_page(start_pfn); 37 struct zone *zone = page_zone(page); 37 struct zone *zone = page_zone(page); 38 unsigned long pfn; 38 unsigned long pfn; 39 39 40 VM_BUG_ON(pageblock_start_pfn(start_pf !! 40 VM_BUG_ON(ALIGN_DOWN(start_pfn, pageblock_nr_pages) != 41 pageblock_start_pfn(end_pfn !! 41 ALIGN_DOWN(end_pfn - 1, pageblock_nr_pages)); 42 42 43 if (is_migrate_cma_page(page)) { 43 if (is_migrate_cma_page(page)) { 44 /* 44 /* 45 * CMA allocations (alloc_cont 45 * CMA allocations (alloc_contig_range) really need to mark 46 * isolate CMA pageblocks even 46 * isolate CMA pageblocks even when they are not movable in fact 47 * so consider them movable he 47 * so consider them movable here. 48 */ 48 */ 49 if (is_migrate_cma(migratetype 49 if (is_migrate_cma(migratetype)) 50 return NULL; 50 return NULL; 51 51 52 return page; 52 return page; 53 } 53 } 54 54 55 for (pfn = start_pfn; pfn < end_pfn; p 55 for (pfn = start_pfn; pfn < end_pfn; pfn++) { 56 page = pfn_to_page(pfn); 56 page = pfn_to_page(pfn); 57 57 58 /* 58 /* 59 * Both, bootmem allocations a 59 * Both, bootmem allocations and memory holes are marked 60 * PG_reserved and are unmovab 60 * PG_reserved and are unmovable. We can even have unmovable 61 * allocations inside ZONE_MOV 61 * allocations inside ZONE_MOVABLE, for example when 62 * specifying "movablecore". 62 * specifying "movablecore". 63 */ 63 */ 64 if (PageReserved(page)) 64 if (PageReserved(page)) 65 return page; 65 return page; 66 66 67 /* 67 /* 68 * If the zone is movable and 68 * If the zone is movable and we have ruled out all reserved 69 * pages then it should be rea 69 * pages then it should be reasonably safe to assume the rest 70 * is movable. 70 * is movable. 71 */ 71 */ 72 if (zone_idx(zone) == ZONE_MOV 72 if (zone_idx(zone) == ZONE_MOVABLE) 73 continue; 73 continue; 74 74 75 /* 75 /* 76 * Hugepages are not in LRU li 76 * Hugepages are not in LRU lists, but they're movable. 77 * THPs are on the LRU, but ne 77 * THPs are on the LRU, but need to be counted as #small pages. 78 * We need not scan over tail 78 * We need not scan over tail pages because we don't 79 * handle each tail page indiv 79 * handle each tail page individually in migration. 80 */ 80 */ 81 if (PageHuge(page) || PageTran 81 if (PageHuge(page) || PageTransCompound(page)) { 82 struct folio *folio = !! 82 struct page *head = compound_head(page); 83 unsigned int skip_page 83 unsigned int skip_pages; 84 84 85 if (PageHuge(page)) { 85 if (PageHuge(page)) { 86 if (!hugepage_ !! 86 if (!hugepage_migration_supported(page_hstate(head))) 87 return 87 return page; 88 } else if (!folio_test !! 88 } else if (!PageLRU(head) && !__PageMovable(head)) { 89 return page; 89 return page; 90 } 90 } 91 91 92 skip_pages = folio_nr_ !! 92 skip_pages = compound_nr(head) - (page - head); 93 pfn += skip_pages - 1; 93 pfn += skip_pages - 1; 94 continue; 94 continue; 95 } 95 } 96 96 97 /* 97 /* 98 * We can't use page_count wit 98 * We can't use page_count without pin a page 99 * because another CPU can fre 99 * because another CPU can free compound page. 100 * This check already skips co 100 * This check already skips compound tails of THP 101 * because their page->_refcou 101 * because their page->_refcount is zero at all time. 102 */ 102 */ 103 if (!page_ref_count(page)) { 103 if (!page_ref_count(page)) { 104 if (PageBuddy(page)) 104 if (PageBuddy(page)) 105 pfn += (1 << b 105 pfn += (1 << buddy_order(page)) - 1; 106 continue; 106 continue; 107 } 107 } 108 108 109 /* 109 /* 110 * The HWPoisoned page may be 110 * The HWPoisoned page may be not in buddy system, and 111 * page_count() is not 0. 111 * page_count() is not 0. 112 */ 112 */ 113 if ((flags & MEMORY_OFFLINE) & 113 if ((flags & MEMORY_OFFLINE) && PageHWPoison(page)) 114 continue; 114 continue; 115 115 116 /* 116 /* 117 * We treat all PageOffline() 117 * We treat all PageOffline() pages as movable when offlining 118 * to give drivers a chance to 118 * to give drivers a chance to decrement their reference count 119 * in MEM_GOING_OFFLINE in ord 119 * in MEM_GOING_OFFLINE in order to indicate that these pages 120 * can be offlined as there ar 120 * can be offlined as there are no direct references anymore. 121 * For actually unmovable Page 121 * For actually unmovable PageOffline() where the driver does 122 * not support this, we will f 122 * not support this, we will fail later when trying to actually 123 * move these pages that still 123 * move these pages that still have a reference count > 0. 124 * (false negatives in this fu 124 * (false negatives in this function only) 125 */ 125 */ 126 if ((flags & MEMORY_OFFLINE) & 126 if ((flags & MEMORY_OFFLINE) && PageOffline(page)) 127 continue; 127 continue; 128 128 129 if (__PageMovable(page) || Pag 129 if (__PageMovable(page) || PageLRU(page)) 130 continue; 130 continue; 131 131 132 /* 132 /* 133 * If there are RECLAIMABLE pa 133 * If there are RECLAIMABLE pages, we need to check 134 * it. But now, memory offlin 134 * it. But now, memory offline itself doesn't call 135 * shrink_node_slabs() and it 135 * shrink_node_slabs() and it still to be fixed. 136 */ 136 */ 137 return page; 137 return page; 138 } 138 } 139 return NULL; 139 return NULL; 140 } 140 } 141 141 142 /* 142 /* 143 * This function set pageblock migratetype to 143 * This function set pageblock migratetype to isolate if no unmovable page is 144 * present in [start_pfn, end_pfn). The pagebl 144 * present in [start_pfn, end_pfn). The pageblock must intersect with 145 * [start_pfn, end_pfn). 145 * [start_pfn, end_pfn). 146 */ 146 */ 147 static int set_migratetype_isolate(struct page 147 static int set_migratetype_isolate(struct page *page, int migratetype, int isol_flags, 148 unsigned long start_pf 148 unsigned long start_pfn, unsigned long end_pfn) 149 { 149 { 150 struct zone *zone = page_zone(page); 150 struct zone *zone = page_zone(page); 151 struct page *unmovable; 151 struct page *unmovable; 152 unsigned long flags; 152 unsigned long flags; 153 unsigned long check_unmovable_start, c 153 unsigned long check_unmovable_start, check_unmovable_end; 154 154 155 if (PageUnaccepted(page)) << 156 accept_page(page); << 157 << 158 spin_lock_irqsave(&zone->lock, flags); 155 spin_lock_irqsave(&zone->lock, flags); 159 156 160 /* 157 /* 161 * We assume the caller intended to SE 158 * We assume the caller intended to SET migrate type to isolate. 162 * If it is already set, then someone 159 * If it is already set, then someone else must have raced and 163 * set it before us. 160 * set it before us. 164 */ 161 */ 165 if (is_migrate_isolate_page(page)) { 162 if (is_migrate_isolate_page(page)) { 166 spin_unlock_irqrestore(&zone-> 163 spin_unlock_irqrestore(&zone->lock, flags); 167 return -EBUSY; 164 return -EBUSY; 168 } 165 } 169 166 170 /* 167 /* 171 * FIXME: Now, memory hotplug doesn't 168 * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself. 172 * We just check MOVABLE pages. 169 * We just check MOVABLE pages. 173 * 170 * 174 * Pass the intersection of [start_pfn 171 * Pass the intersection of [start_pfn, end_pfn) and the page's pageblock 175 * to avoid redundant checks. 172 * to avoid redundant checks. 176 */ 173 */ 177 check_unmovable_start = max(page_to_pf 174 check_unmovable_start = max(page_to_pfn(page), start_pfn); 178 check_unmovable_end = min(pageblock_en !! 175 check_unmovable_end = min(ALIGN(page_to_pfn(page) + 1, pageblock_nr_pages), 179 end_pfn); 176 end_pfn); 180 177 181 unmovable = has_unmovable_pages(check_ 178 unmovable = has_unmovable_pages(check_unmovable_start, check_unmovable_end, 182 migratetype, isol_flag 179 migratetype, isol_flags); 183 if (!unmovable) { 180 if (!unmovable) { 184 if (!move_freepages_block_isol !! 181 unsigned long nr_pages; 185 spin_unlock_irqrestore !! 182 int mt = get_pageblock_migratetype(page); 186 return -EBUSY; !! 183 187 } !! 184 set_pageblock_migratetype(page, MIGRATE_ISOLATE); 188 zone->nr_isolate_pageblock++; 185 zone->nr_isolate_pageblock++; >> 186 nr_pages = move_freepages_block(zone, page, MIGRATE_ISOLATE, >> 187 NULL); >> 188 >> 189 __mod_zone_freepage_state(zone, -nr_pages, mt); 189 spin_unlock_irqrestore(&zone-> 190 spin_unlock_irqrestore(&zone->lock, flags); 190 return 0; 191 return 0; 191 } 192 } 192 193 193 spin_unlock_irqrestore(&zone->lock, fl 194 spin_unlock_irqrestore(&zone->lock, flags); 194 if (isol_flags & REPORT_FAILURE) { 195 if (isol_flags & REPORT_FAILURE) { 195 /* 196 /* 196 * printk() with zone->lock he 197 * printk() with zone->lock held will likely trigger a 197 * lockdep splat, so defer it 198 * lockdep splat, so defer it here. 198 */ 199 */ 199 dump_page(unmovable, "unmovabl 200 dump_page(unmovable, "unmovable page"); 200 } 201 } 201 202 202 return -EBUSY; 203 return -EBUSY; 203 } 204 } 204 205 205 static void unset_migratetype_isolate(struct p 206 static void unset_migratetype_isolate(struct page *page, int migratetype) 206 { 207 { 207 struct zone *zone; 208 struct zone *zone; 208 unsigned long flags; !! 209 unsigned long flags, nr_pages; 209 bool isolated_page = false; 210 bool isolated_page = false; 210 unsigned int order; 211 unsigned int order; 211 struct page *buddy; 212 struct page *buddy; 212 213 213 zone = page_zone(page); 214 zone = page_zone(page); 214 spin_lock_irqsave(&zone->lock, flags); 215 spin_lock_irqsave(&zone->lock, flags); 215 if (!is_migrate_isolate_page(page)) 216 if (!is_migrate_isolate_page(page)) 216 goto out; 217 goto out; 217 218 218 /* 219 /* 219 * Because freepage with more than pag 220 * Because freepage with more than pageblock_order on isolated 220 * pageblock is restricted to merge du 221 * pageblock is restricted to merge due to freepage counting problem, 221 * it is possible that there is free b 222 * it is possible that there is free buddy page. 222 * move_freepages_block() doesn't care 223 * move_freepages_block() doesn't care of merge so we need other 223 * approach in order to merge them. Is 224 * approach in order to merge them. Isolation and free will make 224 * these pages to be merged. 225 * these pages to be merged. 225 */ 226 */ 226 if (PageBuddy(page)) { 227 if (PageBuddy(page)) { 227 order = buddy_order(page); 228 order = buddy_order(page); 228 if (order >= pageblock_order & !! 229 if (order >= pageblock_order && order < MAX_ORDER - 1) { 229 buddy = find_buddy_pag 230 buddy = find_buddy_page_pfn(page, page_to_pfn(page), 230 231 order, NULL); 231 if (buddy && !is_migra 232 if (buddy && !is_migrate_isolate_page(buddy)) { 232 isolated_page 233 isolated_page = !!__isolate_free_page(page, order); 233 /* 234 /* 234 * Isolating a 235 * Isolating a free page in an isolated pageblock 235 * is expected 236 * is expected to always work as watermarks don't 236 * apply here. 237 * apply here. 237 */ 238 */ 238 VM_WARN_ON(!is 239 VM_WARN_ON(!isolated_page); 239 } 240 } 240 } 241 } 241 } 242 } 242 243 243 /* 244 /* 244 * If we isolate freepage with more th 245 * If we isolate freepage with more than pageblock_order, there 245 * should be no freepage in the range, 246 * should be no freepage in the range, so we could avoid costly 246 * pageblock scanning for freepage mov 247 * pageblock scanning for freepage moving. 247 * 248 * 248 * We didn't actually touch any of the 249 * We didn't actually touch any of the isolated pages, so place them 249 * to the tail of the freelist. This i 250 * to the tail of the freelist. This is an optimization for memory 250 * onlining - just onlined memory won' 251 * onlining - just onlined memory won't immediately be considered for 251 * allocation. 252 * allocation. 252 */ 253 */ 253 if (!isolated_page) { 254 if (!isolated_page) { 254 /* !! 255 nr_pages = move_freepages_block(zone, page, migratetype, NULL); 255 * Isolating this block alread !! 256 __mod_zone_freepage_state(zone, nr_pages, migratetype); 256 * should not fail on zone bou << 257 */ << 258 WARN_ON_ONCE(!move_freepages_b << 259 } else { << 260 set_pageblock_migratetype(page << 261 __putback_isolated_page(page, << 262 } 257 } >> 258 set_pageblock_migratetype(page, migratetype); >> 259 if (isolated_page) >> 260 __putback_isolated_page(page, order, migratetype); 263 zone->nr_isolate_pageblock--; 261 zone->nr_isolate_pageblock--; 264 out: 262 out: 265 spin_unlock_irqrestore(&zone->lock, fl 263 spin_unlock_irqrestore(&zone->lock, flags); 266 } 264 } 267 265 268 static inline struct page * 266 static inline struct page * 269 __first_valid_page(unsigned long pfn, unsigned 267 __first_valid_page(unsigned long pfn, unsigned long nr_pages) 270 { 268 { 271 int i; 269 int i; 272 270 273 for (i = 0; i < nr_pages; i++) { 271 for (i = 0; i < nr_pages; i++) { 274 struct page *page; 272 struct page *page; 275 273 276 page = pfn_to_online_page(pfn 274 page = pfn_to_online_page(pfn + i); 277 if (!page) 275 if (!page) 278 continue; 276 continue; 279 return page; 277 return page; 280 } 278 } 281 return NULL; 279 return NULL; 282 } 280 } 283 281 284 /** 282 /** 285 * isolate_single_pageblock() -- tries to isol 283 * isolate_single_pageblock() -- tries to isolate a pageblock that might be 286 * within a free or in-use page. 284 * within a free or in-use page. 287 * @boundary_pfn: pageblock-alig 285 * @boundary_pfn: pageblock-aligned pfn that a page might cross 288 * @flags: isolation flag 286 * @flags: isolation flags 289 * @gfp_flags: GFP flags used 287 * @gfp_flags: GFP flags used for migrating pages 290 * @isolate_before: isolate the pageblock 288 * @isolate_before: isolate the pageblock before the boundary_pfn 291 * @skip_isolation: the flag to skip the p 289 * @skip_isolation: the flag to skip the pageblock isolation in second 292 * isolate_single_pageblo 290 * isolate_single_pageblock() 293 * @migratetype: migrate type to set in 291 * @migratetype: migrate type to set in error recovery. 294 * 292 * 295 * Free and in-use pages can be as big as MAX_ !! 293 * Free and in-use pages can be as big as MAX_ORDER-1 and contain more than one 296 * pageblock. When not all pageblocks within a 294 * pageblock. When not all pageblocks within a page are isolated at the same 297 * time, free page accounting can go wrong. Fo 295 * time, free page accounting can go wrong. For example, in the case of 298 * MAX_PAGE_ORDER = pageblock_order + 1, a MAX !! 296 * MAX_ORDER-1 = pageblock_order + 1, a MAX_ORDER-1 page has two pagelbocks. 299 * pagelbocks. !! 297 * [ MAX_ORDER-1 ] 300 * [ MAX_PAGE_ORDER ] << 301 * [ pageblock0 | pageblock1 ] 298 * [ pageblock0 | pageblock1 ] 302 * When either pageblock is isolated, if it is 299 * When either pageblock is isolated, if it is a free page, the page is not 303 * split into separate migratetype lists, whic 300 * split into separate migratetype lists, which is supposed to; if it is an 304 * in-use page and freed later, __free_one_pag 301 * in-use page and freed later, __free_one_page() does not split the free page 305 * either. The function handles this by splitt 302 * either. The function handles this by splitting the free page or migrating 306 * the in-use page then splitting the free pag 303 * the in-use page then splitting the free page. 307 */ 304 */ 308 static int isolate_single_pageblock(unsigned l 305 static int isolate_single_pageblock(unsigned long boundary_pfn, int flags, 309 gfp_t gfp_flags, bool 306 gfp_t gfp_flags, bool isolate_before, bool skip_isolation, 310 int migratetype) 307 int migratetype) 311 { 308 { 312 unsigned long start_pfn; 309 unsigned long start_pfn; 313 unsigned long isolate_pageblock; 310 unsigned long isolate_pageblock; 314 unsigned long pfn; 311 unsigned long pfn; 315 struct zone *zone; 312 struct zone *zone; 316 int ret; 313 int ret; 317 314 318 VM_BUG_ON(!pageblock_aligned(boundary_ !! 315 VM_BUG_ON(!IS_ALIGNED(boundary_pfn, pageblock_nr_pages)); 319 316 320 if (isolate_before) 317 if (isolate_before) 321 isolate_pageblock = boundary_p 318 isolate_pageblock = boundary_pfn - pageblock_nr_pages; 322 else 319 else 323 isolate_pageblock = boundary_p 320 isolate_pageblock = boundary_pfn; 324 321 325 /* 322 /* 326 * scan at the beginning of MAX_ORDER_ 323 * scan at the beginning of MAX_ORDER_NR_PAGES aligned range to avoid 327 * only isolating a subset of pagebloc 324 * only isolating a subset of pageblocks from a bigger than pageblock 328 * free or in-use page. Also make sure 325 * free or in-use page. Also make sure all to-be-isolated pageblocks 329 * are within the same zone. 326 * are within the same zone. 330 */ 327 */ 331 zone = page_zone(pfn_to_page(isolate_ 328 zone = page_zone(pfn_to_page(isolate_pageblock)); 332 start_pfn = max(ALIGN_DOWN(isolate_pa 329 start_pfn = max(ALIGN_DOWN(isolate_pageblock, MAX_ORDER_NR_PAGES), 333 zone->zo 330 zone->zone_start_pfn); 334 331 335 if (skip_isolation) { 332 if (skip_isolation) { 336 int mt __maybe_unused = get_pa !! 333 int mt = get_pageblock_migratetype(pfn_to_page(isolate_pageblock)); 337 334 338 VM_BUG_ON(!is_migrate_isolate( 335 VM_BUG_ON(!is_migrate_isolate(mt)); 339 } else { 336 } else { 340 ret = set_migratetype_isolate( 337 ret = set_migratetype_isolate(pfn_to_page(isolate_pageblock), migratetype, 341 flags, isolate 338 flags, isolate_pageblock, isolate_pageblock + pageblock_nr_pages); 342 339 343 if (ret) 340 if (ret) 344 return ret; 341 return ret; 345 } 342 } 346 343 347 /* 344 /* 348 * Bail out early when the to-be-isola 345 * Bail out early when the to-be-isolated pageblock does not form 349 * a free or in-use page across bounda 346 * a free or in-use page across boundary_pfn: 350 * 347 * 351 * 1. isolate before boundary_pfn: the 348 * 1. isolate before boundary_pfn: the page after is not online 352 * 2. isolate after boundary_pfn: the 349 * 2. isolate after boundary_pfn: the page before is not online 353 * 350 * 354 * This also ensures correctness. With 351 * This also ensures correctness. Without it, when isolate after 355 * boundary_pfn and [start_pfn, bounda 352 * boundary_pfn and [start_pfn, boundary_pfn) are not online, 356 * __first_valid_page() will return un 353 * __first_valid_page() will return unexpected NULL in the for loop 357 * below. 354 * below. 358 */ 355 */ 359 if (isolate_before) { 356 if (isolate_before) { 360 if (!pfn_to_online_page(bounda 357 if (!pfn_to_online_page(boundary_pfn)) 361 return 0; 358 return 0; 362 } else { 359 } else { 363 if (!pfn_to_online_page(bounda 360 if (!pfn_to_online_page(boundary_pfn - 1)) 364 return 0; 361 return 0; 365 } 362 } 366 363 367 for (pfn = start_pfn; pfn < boundary_p 364 for (pfn = start_pfn; pfn < boundary_pfn;) { 368 struct page *page = __first_va 365 struct page *page = __first_valid_page(pfn, boundary_pfn - pfn); 369 366 370 VM_BUG_ON(!page); 367 VM_BUG_ON(!page); 371 pfn = page_to_pfn(page); 368 pfn = page_to_pfn(page); 372 !! 369 /* 373 if (PageUnaccepted(page)) { !! 370 * start_pfn is MAX_ORDER_NR_PAGES aligned, if there is any 374 pfn += MAX_ORDER_NR_PA !! 371 * free pages in [start_pfn, boundary_pfn), its head page will 375 continue; !! 372 * always be in the range. 376 } !! 373 */ 377 << 378 if (PageBuddy(page)) { 374 if (PageBuddy(page)) { 379 int order = buddy_orde 375 int order = buddy_order(page); 380 376 381 /* move_freepages_bloc !! 377 if (pfn + (1UL << order) > boundary_pfn) { 382 VM_WARN_ON_ONCE(pfn + !! 378 /* free page changed before split, check it again */ >> 379 if (split_free_page(page, order, boundary_pfn - pfn)) >> 380 continue; >> 381 } 383 382 384 pfn += 1UL << order; 383 pfn += 1UL << order; 385 continue; 384 continue; 386 } 385 } 387 << 388 /* 386 /* 389 * If a compound page is strad !! 387 * migrate compound pages then let the free page handling code 390 * to migrate it out of the wa !! 388 * above do the rest. If migration is not possible, just fail. 391 * << 392 * We don't have to worry abou << 393 * free page that straddles in << 394 * pages are freed as order-0 << 395 * (currently) do not exceed p << 396 * << 397 * The block of interest has a << 398 * MIGRATE_ISOLATE above, so w << 399 * will free its pages onto th << 400 */ 389 */ 401 if (PageCompound(page)) { 390 if (PageCompound(page)) { 402 struct page *head = co 391 struct page *head = compound_head(page); 403 unsigned long head_pfn 392 unsigned long head_pfn = page_to_pfn(head); 404 unsigned long nr_pages 393 unsigned long nr_pages = compound_nr(head); 405 394 406 if (head_pfn + nr_page !! 395 if (head_pfn + nr_pages <= boundary_pfn) { 407 PageHuge(page)) { << 408 pfn = head_pfn 396 pfn = head_pfn + nr_pages; 409 continue; 397 continue; 410 } 398 } 411 !! 399 #if defined CONFIG_COMPACTION || defined CONFIG_CMA 412 /* 400 /* 413 * These pages are mov !! 401 * hugetlb, lru compound (THP), and movable compound pages 414 * not expected to exc !! 402 * can be migrated. Otherwise, fail the isolation. 415 * << 416 * Let us know when th << 417 * proper free and spl << 418 */ 403 */ 419 VM_WARN_ON_ONCE_PAGE(P !! 404 if (PageHuge(page) || PageLRU(page) || __PageMovable(page)) { 420 VM_WARN_ON_ONCE_PAGE(_ !! 405 int order; >> 406 unsigned long outer_pfn; >> 407 int page_mt = get_pageblock_migratetype(page); >> 408 bool isolate_page = !is_migrate_isolate_page(page); >> 409 struct compact_control cc = { >> 410 .nr_migratepages = 0, >> 411 .order = -1, >> 412 .zone = page_zone(pfn_to_page(head_pfn)), >> 413 .mode = MIGRATE_SYNC, >> 414 .ignore_skip_hint = true, >> 415 .no_set_skip_hint = true, >> 416 .gfp_mask = gfp_flags, >> 417 .alloc_contig = true, >> 418 }; >> 419 INIT_LIST_HEAD(&cc.migratepages); 421 420 422 goto failed; !! 421 /* >> 422 * XXX: mark the page as MIGRATE_ISOLATE so that >> 423 * no one else can grab the freed page after migration. >> 424 * Ideally, the page should be freed as two separate >> 425 * pages to be added into separate migratetype free >> 426 * lists. >> 427 */ >> 428 if (isolate_page) { >> 429 ret = set_migratetype_isolate(page, page_mt, >> 430 flags, head_pfn, head_pfn + nr_pages); >> 431 if (ret) >> 432 goto failed; >> 433 } >> 434 >> 435 ret = __alloc_contig_migrate_range(&cc, head_pfn, >> 436 head_pfn + nr_pages); >> 437 >> 438 /* >> 439 * restore the page's migratetype so that it can >> 440 * be split into separate migratetype free lists >> 441 * later. >> 442 */ >> 443 if (isolate_page) >> 444 unset_migratetype_isolate(page, page_mt); >> 445 >> 446 if (ret) >> 447 goto failed; >> 448 /* >> 449 * reset pfn to the head of the free page, so >> 450 * that the free page handling code above can split >> 451 * the free page to the right migratetype list. >> 452 * >> 453 * head_pfn is not used here as a hugetlb page order >> 454 * can be bigger than MAX_ORDER-1, but after it is >> 455 * freed, the free page order is not. Use pfn within >> 456 * the range to find the head of the free page. >> 457 */ >> 458 order = 0; >> 459 outer_pfn = pfn; >> 460 while (!PageBuddy(pfn_to_page(outer_pfn))) { >> 461 /* stop if we cannot find the free page */ >> 462 if (++order >= MAX_ORDER) >> 463 goto failed; >> 464 outer_pfn &= ~0UL << order; >> 465 } >> 466 pfn = outer_pfn; >> 467 continue; >> 468 } else >> 469 #endif >> 470 goto failed; 423 } 471 } 424 472 425 pfn++; 473 pfn++; 426 } 474 } 427 return 0; 475 return 0; 428 failed: 476 failed: 429 /* restore the original migratetype */ 477 /* restore the original migratetype */ 430 if (!skip_isolation) 478 if (!skip_isolation) 431 unset_migratetype_isolate(pfn_ 479 unset_migratetype_isolate(pfn_to_page(isolate_pageblock), migratetype); 432 return -EBUSY; 480 return -EBUSY; 433 } 481 } 434 482 435 /** 483 /** 436 * start_isolate_page_range() - mark page rang !! 484 * start_isolate_page_range() - make page-allocation-type of range of pages to 437 * @start_pfn: The first PFN of the r !! 485 * be MIGRATE_ISOLATE. 438 * @end_pfn: The last PFN of the ra !! 486 * @start_pfn: The lower PFN of the range to be isolated. >> 487 * @end_pfn: The upper PFN of the range to be isolated. 439 * @migratetype: Migrate type to set in 488 * @migratetype: Migrate type to set in error recovery. 440 * @flags: The following flags ar 489 * @flags: The following flags are allowed (they can be combined in 441 * a bit mask) 490 * a bit mask) 442 * MEMORY_OFFLINE - isola 491 * MEMORY_OFFLINE - isolate to offline (!allocate) memory 443 * e.g., 492 * e.g., skip over PageHWPoison() pages 444 * and P 493 * and PageOffline() pages. 445 * REPORT_FAILURE - repor 494 * REPORT_FAILURE - report details about the failure to 446 * isolate the range 495 * isolate the range 447 * @gfp_flags: GFP flags used for mig 496 * @gfp_flags: GFP flags used for migrating pages that sit across the 448 * range boundaries. 497 * range boundaries. 449 * 498 * 450 * Making page-allocation-type to be MIGRATE_I 499 * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in 451 * the range will never be allocated. Any free 500 * the range will never be allocated. Any free pages and pages freed in the 452 * future will not be allocated again. If spec 501 * future will not be allocated again. If specified range includes migrate types 453 * other than MOVABLE or CMA, this will fail w 502 * other than MOVABLE or CMA, this will fail with -EBUSY. For isolating all 454 * pages in the range finally, the caller have 503 * pages in the range finally, the caller have to free all pages in the range. 455 * test_page_isolated() can be used for test i 504 * test_page_isolated() can be used for test it. 456 * 505 * 457 * The function first tries to isolate the pag 506 * The function first tries to isolate the pageblocks at the beginning and end 458 * of the range, since there might be pages ac 507 * of the range, since there might be pages across the range boundaries. 459 * Afterwards, it isolates the rest of the ran 508 * Afterwards, it isolates the rest of the range. 460 * 509 * 461 * There is no high level synchronization mech 510 * There is no high level synchronization mechanism that prevents two threads 462 * from trying to isolate overlapping ranges. 511 * from trying to isolate overlapping ranges. If this happens, one thread 463 * will notice pageblocks in the overlapping r 512 * will notice pageblocks in the overlapping range already set to isolate. 464 * This happens in set_migratetype_isolate, an 513 * This happens in set_migratetype_isolate, and set_migratetype_isolate 465 * returns an error. We then clean up by resto 514 * returns an error. We then clean up by restoring the migration type on 466 * pageblocks we may have modified and return 515 * pageblocks we may have modified and return -EBUSY to caller. This 467 * prevents two threads from simultaneously wo 516 * prevents two threads from simultaneously working on overlapping ranges. 468 * 517 * 469 * Please note that there is no strong synchro 518 * Please note that there is no strong synchronization with the page allocator 470 * either. Pages might be freed while their pa 519 * either. Pages might be freed while their page blocks are marked ISOLATED. 471 * A call to drain_all_pages() after isolation 520 * A call to drain_all_pages() after isolation can flush most of them. However 472 * in some cases pages might still end up on p 521 * in some cases pages might still end up on pcp lists and that would allow 473 * for their allocation even when they are in 522 * for their allocation even when they are in fact isolated already. Depending 474 * on how strong of a guarantee the caller nee 523 * on how strong of a guarantee the caller needs, zone_pcp_disable/enable() 475 * might be used to flush and disable pcplist 524 * might be used to flush and disable pcplist before isolation and enable after 476 * unisolation. 525 * unisolation. 477 * 526 * 478 * Return: 0 on success and -EBUSY if any part 527 * Return: 0 on success and -EBUSY if any part of range cannot be isolated. 479 */ 528 */ 480 int start_isolate_page_range(unsigned long sta 529 int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn, 481 int migratetype, 530 int migratetype, int flags, gfp_t gfp_flags) 482 { 531 { 483 unsigned long pfn; 532 unsigned long pfn; 484 struct page *page; 533 struct page *page; 485 /* isolation is done at page block gra 534 /* isolation is done at page block granularity */ 486 unsigned long isolate_start = pagebloc !! 535 unsigned long isolate_start = ALIGN_DOWN(start_pfn, pageblock_nr_pages); 487 unsigned long isolate_end = pageblock_ !! 536 unsigned long isolate_end = ALIGN(end_pfn, pageblock_nr_pages); 488 int ret; 537 int ret; 489 bool skip_isolation = false; 538 bool skip_isolation = false; 490 539 491 /* isolate [isolate_start, isolate_sta 540 /* isolate [isolate_start, isolate_start + pageblock_nr_pages) pageblock */ 492 ret = isolate_single_pageblock(isolate 541 ret = isolate_single_pageblock(isolate_start, flags, gfp_flags, false, 493 skip_isolation, migrat 542 skip_isolation, migratetype); 494 if (ret) 543 if (ret) 495 return ret; 544 return ret; 496 545 497 if (isolate_start == isolate_end - pag 546 if (isolate_start == isolate_end - pageblock_nr_pages) 498 skip_isolation = true; 547 skip_isolation = true; 499 548 500 /* isolate [isolate_end - pageblock_nr 549 /* isolate [isolate_end - pageblock_nr_pages, isolate_end) pageblock */ 501 ret = isolate_single_pageblock(isolate 550 ret = isolate_single_pageblock(isolate_end, flags, gfp_flags, true, 502 skip_isolation, migrat 551 skip_isolation, migratetype); 503 if (ret) { 552 if (ret) { 504 unset_migratetype_isolate(pfn_ 553 unset_migratetype_isolate(pfn_to_page(isolate_start), migratetype); 505 return ret; 554 return ret; 506 } 555 } 507 556 508 /* skip isolated pageblocks at the beg 557 /* skip isolated pageblocks at the beginning and end */ 509 for (pfn = isolate_start + pageblock_n 558 for (pfn = isolate_start + pageblock_nr_pages; 510 pfn < isolate_end - pageblock_nr_ 559 pfn < isolate_end - pageblock_nr_pages; 511 pfn += pageblock_nr_pages) { 560 pfn += pageblock_nr_pages) { 512 page = __first_valid_page(pfn, 561 page = __first_valid_page(pfn, pageblock_nr_pages); 513 if (page && set_migratetype_is 562 if (page && set_migratetype_isolate(page, migratetype, flags, 514 start_ 563 start_pfn, end_pfn)) { 515 undo_isolate_page_rang 564 undo_isolate_page_range(isolate_start, pfn, migratetype); 516 unset_migratetype_isol 565 unset_migratetype_isolate( 517 pfn_to_page(is 566 pfn_to_page(isolate_end - pageblock_nr_pages), 518 migratetype); 567 migratetype); 519 return -EBUSY; 568 return -EBUSY; 520 } 569 } 521 } 570 } 522 return 0; 571 return 0; 523 } 572 } 524 573 525 /** !! 574 /* 526 * undo_isolate_page_range - undo effects of s !! 575 * Make isolated pages available again. 527 * @start_pfn: The first PFN of the i << 528 * @end_pfn: The last PFN of the is << 529 * @migratetype: New migrate type to se << 530 * << 531 * This finds every MIGRATE_ISOLATE page block << 532 * and switches it to @migratetype. << 533 */ 576 */ 534 void undo_isolate_page_range(unsigned long sta 577 void undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn, 535 int migratetype) 578 int migratetype) 536 { 579 { 537 unsigned long pfn; 580 unsigned long pfn; 538 struct page *page; 581 struct page *page; 539 unsigned long isolate_start = pagebloc !! 582 unsigned long isolate_start = ALIGN_DOWN(start_pfn, pageblock_nr_pages); 540 unsigned long isolate_end = pageblock_ !! 583 unsigned long isolate_end = ALIGN(end_pfn, pageblock_nr_pages); >> 584 541 585 542 for (pfn = isolate_start; 586 for (pfn = isolate_start; 543 pfn < isolate_end; 587 pfn < isolate_end; 544 pfn += pageblock_nr_pages) { 588 pfn += pageblock_nr_pages) { 545 page = __first_valid_page(pfn, 589 page = __first_valid_page(pfn, pageblock_nr_pages); 546 if (!page || !is_migrate_isola 590 if (!page || !is_migrate_isolate_page(page)) 547 continue; 591 continue; 548 unset_migratetype_isolate(page 592 unset_migratetype_isolate(page, migratetype); 549 } 593 } 550 } 594 } 551 /* 595 /* 552 * Test all pages in the range is free(means i 596 * Test all pages in the range is free(means isolated) or not. 553 * all pages in [start_pfn...end_pfn) must be 597 * all pages in [start_pfn...end_pfn) must be in the same zone. 554 * zone->lock must be held before call this. 598 * zone->lock must be held before call this. 555 * 599 * 556 * Returns the last tested pfn. 600 * Returns the last tested pfn. 557 */ 601 */ 558 static unsigned long 602 static unsigned long 559 __test_page_isolated_in_pageblock(unsigned lon 603 __test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn, 560 int flags) 604 int flags) 561 { 605 { 562 struct page *page; 606 struct page *page; 563 607 564 while (pfn < end_pfn) { 608 while (pfn < end_pfn) { 565 page = pfn_to_page(pfn); 609 page = pfn_to_page(pfn); 566 if (PageBuddy(page)) 610 if (PageBuddy(page)) 567 /* 611 /* 568 * If the page is on a 612 * If the page is on a free list, it has to be on 569 * the correct MIGRATE 613 * the correct MIGRATE_ISOLATE freelist. There is no 570 * simple way to verif 614 * simple way to verify that as VM_BUG_ON(), though. 571 */ 615 */ 572 pfn += 1 << buddy_orde 616 pfn += 1 << buddy_order(page); 573 else if ((flags & MEMORY_OFFLI 617 else if ((flags & MEMORY_OFFLINE) && PageHWPoison(page)) 574 /* A HWPoisoned page c 618 /* A HWPoisoned page cannot be also PageBuddy */ 575 pfn++; 619 pfn++; 576 else if ((flags & MEMORY_OFFLI 620 else if ((flags & MEMORY_OFFLINE) && PageOffline(page) && 577 !page_count(page)) 621 !page_count(page)) 578 /* 622 /* 579 * The responsible dri 623 * The responsible driver agreed to skip PageOffline() 580 * pages when offlinin 624 * pages when offlining memory by dropping its 581 * reference in MEM_GO 625 * reference in MEM_GOING_OFFLINE. 582 */ 626 */ 583 pfn++; 627 pfn++; 584 else 628 else 585 break; 629 break; 586 } 630 } 587 631 588 return pfn; 632 return pfn; 589 } 633 } 590 634 591 /** !! 635 /* Caller should ensure that requested range is in a single zone */ 592 * test_pages_isolated - check if pageblocks i << 593 * @start_pfn: The first PFN of the i << 594 * @end_pfn: The first PFN *after* << 595 * @isol_flags: Testing mode flags << 596 * << 597 * This tests if all in the specified range ar << 598 * << 599 * If %MEMORY_OFFLINE is specified in @flags, << 600 * poisoned and offlined pages free as well. << 601 * << 602 * Caller must ensure the requested range does << 603 * << 604 * Returns 0 if true, -EBUSY if one or more pa << 605 */ << 606 int test_pages_isolated(unsigned long start_pf 636 int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn, 607 int isol_flags) 637 int isol_flags) 608 { 638 { 609 unsigned long pfn, flags; 639 unsigned long pfn, flags; 610 struct page *page; 640 struct page *page; 611 struct zone *zone; 641 struct zone *zone; 612 int ret; 642 int ret; 613 643 614 /* 644 /* 615 * Note: pageblock_nr_pages != MAX_PAG !! 645 * Note: pageblock_nr_pages != MAX_ORDER. Then, chunks of free pages 616 * pages are not aligned to pageblock_ !! 646 * are not aligned to pageblock_nr_pages. 617 * Then we just check migratetype firs 647 * Then we just check migratetype first. 618 */ 648 */ 619 for (pfn = start_pfn; pfn < end_pfn; p 649 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) { 620 page = __first_valid_page(pfn, 650 page = __first_valid_page(pfn, pageblock_nr_pages); 621 if (page && !is_migrate_isolat 651 if (page && !is_migrate_isolate_page(page)) 622 break; 652 break; 623 } 653 } 624 page = __first_valid_page(start_pfn, e 654 page = __first_valid_page(start_pfn, end_pfn - start_pfn); 625 if ((pfn < end_pfn) || !page) { 655 if ((pfn < end_pfn) || !page) { 626 ret = -EBUSY; 656 ret = -EBUSY; 627 goto out; 657 goto out; 628 } 658 } 629 659 630 /* Check all pages are free or marked 660 /* Check all pages are free or marked as ISOLATED */ 631 zone = page_zone(page); 661 zone = page_zone(page); 632 spin_lock_irqsave(&zone->lock, flags); 662 spin_lock_irqsave(&zone->lock, flags); 633 pfn = __test_page_isolated_in_pagebloc 663 pfn = __test_page_isolated_in_pageblock(start_pfn, end_pfn, isol_flags); 634 spin_unlock_irqrestore(&zone->lock, fl 664 spin_unlock_irqrestore(&zone->lock, flags); 635 665 636 ret = pfn < end_pfn ? -EBUSY : 0; 666 ret = pfn < end_pfn ? -EBUSY : 0; 637 667 638 out: 668 out: 639 trace_test_pages_isolated(start_pfn, e 669 trace_test_pages_isolated(start_pfn, end_pfn, pfn); 640 670 641 return ret; 671 return ret; 642 } 672 } 643 673
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.