Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/mm/page_isolation.c |
| 4 | */ |
| 5 | |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 6 | #include <linux/mm.h> |
| 7 | #include <linux/page-isolation.h> |
| 8 | #include <linux/pageblock-flags.h> |
Minchan Kim | ee6f509 | 2012-07-31 16:43:50 -0700 | [diff] [blame] | 9 | #include <linux/memory.h> |
Naoya Horiguchi | c8721bb | 2013-09-11 14:22:09 -0700 | [diff] [blame] | 10 | #include <linux/hugetlb.h> |
Joonsoo Kim | 83358ec | 2016-07-26 15:23:43 -0700 | [diff] [blame] | 11 | #include <linux/page_owner.h> |
Michal Hocko | 8b91323 | 2017-07-10 15:48:47 -0700 | [diff] [blame] | 12 | #include <linux/migrate.h> |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 13 | #include "internal.h" |
| 14 | |
Joonsoo Kim | 0f0848e | 2016-01-14 15:18:42 -0800 | [diff] [blame] | 15 | #define CREATE_TRACE_POINTS |
| 16 | #include <trace/events/page_isolation.h> |
| 17 | |
Zi Yan | b48d8a8 | 2022-05-12 20:22:57 -0700 | [diff] [blame] | 18 | /* |
Zi Yan | 844fbae | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 19 | * This function checks whether the range [start_pfn, end_pfn) includes |
| 20 | * unmovable pages or not. The range must fall into a single pageblock and |
| 21 | * consequently belong to a single zone. |
Zi Yan | b48d8a8 | 2022-05-12 20:22:57 -0700 | [diff] [blame] | 22 | * |
| 23 | * PageLRU check without isolation or lru_lock could race so that |
| 24 | * MIGRATE_MOVABLE block might include unmovable pages. And __PageMovable |
| 25 | * check without lock_page also may miss some movable non-lru pages at |
| 26 | * race condition. So you can't expect this function should be exact. |
| 27 | * |
| 28 | * Returns a page without holding a reference. If the caller wants to |
| 29 | * dereference that page (e.g., dumping), it has to make sure that it |
| 30 | * cannot get removed (e.g., via memory unplug) concurrently. |
| 31 | * |
| 32 | */ |
Zi Yan | 844fbae | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 33 | static struct page *has_unmovable_pages(unsigned long start_pfn, unsigned long end_pfn, |
| 34 | int migratetype, int flags) |
Zi Yan | b48d8a8 | 2022-05-12 20:22:57 -0700 | [diff] [blame] | 35 | { |
Zi Yan | 844fbae | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 36 | struct page *page = pfn_to_page(start_pfn); |
| 37 | struct zone *zone = page_zone(page); |
| 38 | unsigned long pfn; |
| 39 | |
Kefeng Wang | 4f9bc69 | 2022-09-07 14:08:42 +0800 | [diff] [blame] | 40 | VM_BUG_ON(pageblock_start_pfn(start_pfn) != |
| 41 | pageblock_start_pfn(end_pfn - 1)); |
Zi Yan | b48d8a8 | 2022-05-12 20:22:57 -0700 | [diff] [blame] | 42 | |
| 43 | if (is_migrate_cma_page(page)) { |
| 44 | /* |
| 45 | * CMA allocations (alloc_contig_range) really need to mark |
| 46 | * isolate CMA pageblocks even when they are not movable in fact |
| 47 | * so consider them movable here. |
| 48 | */ |
| 49 | if (is_migrate_cma(migratetype)) |
| 50 | return NULL; |
| 51 | |
| 52 | return page; |
| 53 | } |
| 54 | |
Zi Yan | 844fbae | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 55 | for (pfn = start_pfn; pfn < end_pfn; pfn++) { |
| 56 | page = pfn_to_page(pfn); |
Zi Yan | b48d8a8 | 2022-05-12 20:22:57 -0700 | [diff] [blame] | 57 | |
| 58 | /* |
| 59 | * Both, bootmem allocations and memory holes are marked |
| 60 | * PG_reserved and are unmovable. We can even have unmovable |
| 61 | * allocations inside ZONE_MOVABLE, for example when |
| 62 | * specifying "movablecore". |
| 63 | */ |
| 64 | if (PageReserved(page)) |
| 65 | return page; |
| 66 | |
| 67 | /* |
| 68 | * If the zone is movable and we have ruled out all reserved |
| 69 | * pages then it should be reasonably safe to assume the rest |
| 70 | * is movable. |
| 71 | */ |
| 72 | if (zone_idx(zone) == ZONE_MOVABLE) |
| 73 | continue; |
| 74 | |
| 75 | /* |
| 76 | * Hugepages are not in LRU lists, but they're movable. |
| 77 | * THPs are on the LRU, but need to be counted as #small pages. |
| 78 | * We need not scan over tail pages because we don't |
| 79 | * handle each tail page individually in migration. |
| 80 | */ |
| 81 | if (PageHuge(page) || PageTransCompound(page)) { |
| 82 | struct page *head = compound_head(page); |
| 83 | unsigned int skip_pages; |
| 84 | |
| 85 | if (PageHuge(page)) { |
| 86 | if (!hugepage_migration_supported(page_hstate(head))) |
| 87 | return page; |
| 88 | } else if (!PageLRU(head) && !__PageMovable(head)) { |
| 89 | return page; |
| 90 | } |
| 91 | |
| 92 | skip_pages = compound_nr(head) - (page - head); |
Zi Yan | 844fbae | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 93 | pfn += skip_pages - 1; |
Zi Yan | b48d8a8 | 2022-05-12 20:22:57 -0700 | [diff] [blame] | 94 | continue; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * We can't use page_count without pin a page |
| 99 | * because another CPU can free compound page. |
| 100 | * This check already skips compound tails of THP |
| 101 | * because their page->_refcount is zero at all time. |
| 102 | */ |
| 103 | if (!page_ref_count(page)) { |
| 104 | if (PageBuddy(page)) |
Zi Yan | 844fbae | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 105 | pfn += (1 << buddy_order(page)) - 1; |
Zi Yan | b48d8a8 | 2022-05-12 20:22:57 -0700 | [diff] [blame] | 106 | continue; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * The HWPoisoned page may be not in buddy system, and |
| 111 | * page_count() is not 0. |
| 112 | */ |
| 113 | if ((flags & MEMORY_OFFLINE) && PageHWPoison(page)) |
| 114 | continue; |
| 115 | |
| 116 | /* |
| 117 | * We treat all PageOffline() pages as movable when offlining |
| 118 | * to give drivers a chance to decrement their reference count |
| 119 | * in MEM_GOING_OFFLINE in order to indicate that these pages |
| 120 | * can be offlined as there are no direct references anymore. |
| 121 | * For actually unmovable PageOffline() where the driver does |
| 122 | * not support this, we will fail later when trying to actually |
| 123 | * move these pages that still have a reference count > 0. |
| 124 | * (false negatives in this function only) |
| 125 | */ |
| 126 | if ((flags & MEMORY_OFFLINE) && PageOffline(page)) |
| 127 | continue; |
| 128 | |
| 129 | if (__PageMovable(page) || PageLRU(page)) |
| 130 | continue; |
| 131 | |
| 132 | /* |
| 133 | * If there are RECLAIMABLE pages, we need to check |
| 134 | * it. But now, memory offline itself doesn't call |
| 135 | * shrink_node_slabs() and it still to be fixed. |
| 136 | */ |
| 137 | return page; |
| 138 | } |
| 139 | return NULL; |
| 140 | } |
| 141 | |
Zi Yan | 844fbae | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 142 | /* |
| 143 | * This function set pageblock migratetype to isolate if no unmovable page is |
| 144 | * present in [start_pfn, end_pfn). The pageblock must intersect with |
| 145 | * [start_pfn, end_pfn). |
| 146 | */ |
| 147 | static int set_migratetype_isolate(struct page *page, int migratetype, int isol_flags, |
| 148 | unsigned long start_pfn, unsigned long end_pfn) |
Minchan Kim | ee6f509 | 2012-07-31 16:43:50 -0700 | [diff] [blame] | 149 | { |
David Hildenbrand | 1c31cb4 | 2020-10-13 16:55:28 -0700 | [diff] [blame] | 150 | struct zone *zone = page_zone(page); |
| 151 | struct page *unmovable; |
David Hildenbrand | 3f9903b9 | 2020-01-30 22:14:01 -0800 | [diff] [blame] | 152 | unsigned long flags; |
Zi Yan | 844fbae | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 153 | unsigned long check_unmovable_start, check_unmovable_end; |
Minchan Kim | ee6f509 | 2012-07-31 16:43:50 -0700 | [diff] [blame] | 154 | |
| 155 | spin_lock_irqsave(&zone->lock, flags); |
| 156 | |
Mike Kravetz | 2c7452a | 2018-04-05 16:25:26 -0700 | [diff] [blame] | 157 | /* |
| 158 | * We assume the caller intended to SET migrate type to isolate. |
| 159 | * If it is already set, then someone else must have raced and |
David Hildenbrand | 51030a5 | 2020-10-13 16:55:21 -0700 | [diff] [blame] | 160 | * set it before us. |
Mike Kravetz | 2c7452a | 2018-04-05 16:25:26 -0700 | [diff] [blame] | 161 | */ |
David Hildenbrand | 51030a5 | 2020-10-13 16:55:21 -0700 | [diff] [blame] | 162 | if (is_migrate_isolate_page(page)) { |
| 163 | spin_unlock_irqrestore(&zone->lock, flags); |
| 164 | return -EBUSY; |
| 165 | } |
Mike Kravetz | 2c7452a | 2018-04-05 16:25:26 -0700 | [diff] [blame] | 166 | |
Minchan Kim | ee6f509 | 2012-07-31 16:43:50 -0700 | [diff] [blame] | 167 | /* |
| 168 | * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself. |
| 169 | * We just check MOVABLE pages. |
Zi Yan | 844fbae | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 170 | * |
| 171 | * Pass the intersection of [start_pfn, end_pfn) and the page's pageblock |
| 172 | * to avoid redundant checks. |
Minchan Kim | ee6f509 | 2012-07-31 16:43:50 -0700 | [diff] [blame] | 173 | */ |
Zi Yan | 844fbae | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 174 | check_unmovable_start = max(page_to_pfn(page), start_pfn); |
Kefeng Wang | 4f9bc69 | 2022-09-07 14:08:42 +0800 | [diff] [blame] | 175 | check_unmovable_end = min(pageblock_end_pfn(page_to_pfn(page)), |
Zi Yan | 844fbae | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 176 | end_pfn); |
| 177 | |
| 178 | unmovable = has_unmovable_pages(check_unmovable_start, check_unmovable_end, |
| 179 | migratetype, isol_flags); |
Qian Cai | 4a55c04 | 2020-01-30 22:14:57 -0800 | [diff] [blame] | 180 | if (!unmovable) { |
Bartlomiej Zolnierkiewicz | 2139cbe | 2012-10-08 16:32:00 -0700 | [diff] [blame] | 181 | unsigned long nr_pages; |
Michal Hocko | 4da2ce2 | 2017-11-15 17:33:26 -0800 | [diff] [blame] | 182 | int mt = get_pageblock_migratetype(page); |
Bartlomiej Zolnierkiewicz | 2139cbe | 2012-10-08 16:32:00 -0700 | [diff] [blame] | 183 | |
Bartlomiej Zolnierkiewicz | a458431 | 2013-01-04 15:35:08 -0800 | [diff] [blame] | 184 | set_pageblock_migratetype(page, MIGRATE_ISOLATE); |
Joonsoo Kim | ad53f92 | 2014-11-13 15:19:11 -0800 | [diff] [blame] | 185 | zone->nr_isolate_pageblock++; |
Vlastimil Babka | 02aa0cd | 2017-05-08 15:54:40 -0700 | [diff] [blame] | 186 | nr_pages = move_freepages_block(zone, page, MIGRATE_ISOLATE, |
| 187 | NULL); |
Bartlomiej Zolnierkiewicz | 2139cbe | 2012-10-08 16:32:00 -0700 | [diff] [blame] | 188 | |
Michal Hocko | 4da2ce2 | 2017-11-15 17:33:26 -0800 | [diff] [blame] | 189 | __mod_zone_freepage_state(zone, -nr_pages, mt); |
David Hildenbrand | 1c31cb4 | 2020-10-13 16:55:28 -0700 | [diff] [blame] | 190 | spin_unlock_irqrestore(&zone->lock, flags); |
David Hildenbrand | 1c31cb4 | 2020-10-13 16:55:28 -0700 | [diff] [blame] | 191 | return 0; |
Minchan Kim | ee6f509 | 2012-07-31 16:43:50 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | spin_unlock_irqrestore(&zone->lock, flags); |
David Hildenbrand | 1c31cb4 | 2020-10-13 16:55:28 -0700 | [diff] [blame] | 195 | if (isol_flags & REPORT_FAILURE) { |
David Hildenbrand | 48381d7 | 2020-10-13 16:55:24 -0700 | [diff] [blame] | 196 | /* |
| 197 | * printk() with zone->lock held will likely trigger a |
| 198 | * lockdep splat, so defer it here. |
| 199 | */ |
| 200 | dump_page(unmovable, "unmovable page"); |
Qian Cai | 3d680bd | 2020-01-30 22:15:01 -0800 | [diff] [blame] | 201 | } |
Qian Cai | 4a55c04 | 2020-01-30 22:14:57 -0800 | [diff] [blame] | 202 | |
David Hildenbrand | 1c31cb4 | 2020-10-13 16:55:28 -0700 | [diff] [blame] | 203 | return -EBUSY; |
Minchan Kim | ee6f509 | 2012-07-31 16:43:50 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 206 | static void unset_migratetype_isolate(struct page *page, int migratetype) |
Minchan Kim | ee6f509 | 2012-07-31 16:43:50 -0700 | [diff] [blame] | 207 | { |
| 208 | struct zone *zone; |
Bartlomiej Zolnierkiewicz | 2139cbe | 2012-10-08 16:32:00 -0700 | [diff] [blame] | 209 | unsigned long flags, nr_pages; |
Joonsoo Kim | e3a2713 | 2016-07-26 15:24:01 -0700 | [diff] [blame] | 210 | bool isolated_page = false; |
Joonsoo Kim | 3c60509 | 2014-11-13 15:19:21 -0800 | [diff] [blame] | 211 | unsigned int order; |
Joonsoo Kim | 3c60509 | 2014-11-13 15:19:21 -0800 | [diff] [blame] | 212 | struct page *buddy; |
Bartlomiej Zolnierkiewicz | 2139cbe | 2012-10-08 16:32:00 -0700 | [diff] [blame] | 213 | |
Minchan Kim | ee6f509 | 2012-07-31 16:43:50 -0700 | [diff] [blame] | 214 | zone = page_zone(page); |
| 215 | spin_lock_irqsave(&zone->lock, flags); |
Xishi Qiu | bbf9ce9 | 2017-05-03 14:52:55 -0700 | [diff] [blame] | 216 | if (!is_migrate_isolate_page(page)) |
Minchan Kim | ee6f509 | 2012-07-31 16:43:50 -0700 | [diff] [blame] | 217 | goto out; |
Joonsoo Kim | 3c60509 | 2014-11-13 15:19:21 -0800 | [diff] [blame] | 218 | |
| 219 | /* |
| 220 | * Because freepage with more than pageblock_order on isolated |
| 221 | * pageblock is restricted to merge due to freepage counting problem, |
| 222 | * it is possible that there is free buddy page. |
| 223 | * move_freepages_block() doesn't care of merge so we need other |
| 224 | * approach in order to merge them. Isolation and free will make |
| 225 | * these pages to be merged. |
| 226 | */ |
| 227 | if (PageBuddy(page)) { |
Matthew Wilcox (Oracle) | ab130f91 | 2020-10-15 20:10:15 -0700 | [diff] [blame] | 228 | order = buddy_order(page); |
Muchun Song | 2484be0 | 2020-12-14 19:12:27 -0800 | [diff] [blame] | 229 | if (order >= pageblock_order && order < MAX_ORDER - 1) { |
Zi Yan | 8170ac4 | 2022-04-28 23:16:01 -0700 | [diff] [blame] | 230 | buddy = find_buddy_page_pfn(page, page_to_pfn(page), |
| 231 | order, NULL); |
| 232 | if (buddy && !is_migrate_isolate_page(buddy)) { |
Miaohe Lin | a500cb3 | 2021-11-05 13:42:19 -0700 | [diff] [blame] | 233 | isolated_page = !!__isolate_free_page(page, order); |
| 234 | /* |
| 235 | * Isolating a free page in an isolated pageblock |
| 236 | * is expected to always work as watermarks don't |
| 237 | * apply here. |
| 238 | */ |
| 239 | VM_WARN_ON(!isolated_page); |
Joonsoo Kim | 3c60509 | 2014-11-13 15:19:21 -0800 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * If we isolate freepage with more than pageblock_order, there |
| 246 | * should be no freepage in the range, so we could avoid costly |
| 247 | * pageblock scanning for freepage moving. |
David Hildenbrand | 293ffa5 | 2020-10-15 20:09:30 -0700 | [diff] [blame] | 248 | * |
| 249 | * We didn't actually touch any of the isolated pages, so place them |
| 250 | * to the tail of the freelist. This is an optimization for memory |
| 251 | * onlining - just onlined memory won't immediately be considered for |
| 252 | * allocation. |
Joonsoo Kim | 3c60509 | 2014-11-13 15:19:21 -0800 | [diff] [blame] | 253 | */ |
Chen Wandun | a85468b | 2022-02-03 20:49:06 -0800 | [diff] [blame] | 254 | if (!isolated_page) { |
Vlastimil Babka | 02aa0cd | 2017-05-08 15:54:40 -0700 | [diff] [blame] | 255 | nr_pages = move_freepages_block(zone, page, migratetype, NULL); |
Joonsoo Kim | 3c60509 | 2014-11-13 15:19:21 -0800 | [diff] [blame] | 256 | __mod_zone_freepage_state(zone, nr_pages, migratetype); |
| 257 | } |
Bartlomiej Zolnierkiewicz | a458431 | 2013-01-04 15:35:08 -0800 | [diff] [blame] | 258 | set_pageblock_migratetype(page, migratetype); |
Alexander Duyck | 624f58d | 2020-04-06 20:04:53 -0700 | [diff] [blame] | 259 | if (isolated_page) |
| 260 | __putback_isolated_page(page, order, migratetype); |
Joonsoo Kim | ad53f92 | 2014-11-13 15:19:11 -0800 | [diff] [blame] | 261 | zone->nr_isolate_pageblock--; |
Minchan Kim | ee6f509 | 2012-07-31 16:43:50 -0700 | [diff] [blame] | 262 | out: |
| 263 | spin_unlock_irqrestore(&zone->lock, flags); |
| 264 | } |
| 265 | |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 266 | static inline struct page * |
| 267 | __first_valid_page(unsigned long pfn, unsigned long nr_pages) |
| 268 | { |
| 269 | int i; |
Michal Hocko | 2ce1364 | 2017-07-06 15:38:04 -0700 | [diff] [blame] | 270 | |
| 271 | for (i = 0; i < nr_pages; i++) { |
| 272 | struct page *page; |
| 273 | |
Michal Hocko | 2ce1364 | 2017-07-06 15:38:04 -0700 | [diff] [blame] | 274 | page = pfn_to_online_page(pfn + i); |
| 275 | if (!page) |
| 276 | continue; |
| 277 | return page; |
| 278 | } |
| 279 | return NULL; |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Qian Cai | 9b7ea46 | 2019-03-28 20:43:34 -0700 | [diff] [blame] | 282 | /** |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 283 | * isolate_single_pageblock() -- tries to isolate a pageblock that might be |
| 284 | * within a free or in-use page. |
| 285 | * @boundary_pfn: pageblock-aligned pfn that a page might cross |
Zi Yan | 88ee134 | 2022-05-24 15:47:56 -0400 | [diff] [blame] | 286 | * @flags: isolation flags |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 287 | * @gfp_flags: GFP flags used for migrating pages |
| 288 | * @isolate_before: isolate the pageblock before the boundary_pfn |
Yang Li | 0429993 | 2022-06-02 14:21:16 +0800 | [diff] [blame] | 289 | * @skip_isolation: the flag to skip the pageblock isolation in second |
| 290 | * isolate_single_pageblock() |
Zi Yan | 80e2b58 | 2022-09-13 22:39:13 -0400 | [diff] [blame] | 291 | * @migratetype: migrate type to set in error recovery. |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 292 | * |
| 293 | * Free and in-use pages can be as big as MAX_ORDER-1 and contain more than one |
| 294 | * pageblock. When not all pageblocks within a page are isolated at the same |
| 295 | * time, free page accounting can go wrong. For example, in the case of |
| 296 | * MAX_ORDER-1 = pageblock_order + 1, a MAX_ORDER-1 page has two pagelbocks. |
| 297 | * [ MAX_ORDER-1 ] |
| 298 | * [ pageblock0 | pageblock1 ] |
| 299 | * When either pageblock is isolated, if it is a free page, the page is not |
| 300 | * split into separate migratetype lists, which is supposed to; if it is an |
| 301 | * in-use page and freed later, __free_one_page() does not split the free page |
| 302 | * either. The function handles this by splitting the free page or migrating |
| 303 | * the in-use page then splitting the free page. |
| 304 | */ |
Zi Yan | 88ee134 | 2022-05-24 15:47:56 -0400 | [diff] [blame] | 305 | static int isolate_single_pageblock(unsigned long boundary_pfn, int flags, |
Zi Yan | 80e2b58 | 2022-09-13 22:39:13 -0400 | [diff] [blame] | 306 | gfp_t gfp_flags, bool isolate_before, bool skip_isolation, |
| 307 | int migratetype) |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 308 | { |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 309 | unsigned long start_pfn; |
| 310 | unsigned long isolate_pageblock; |
| 311 | unsigned long pfn; |
| 312 | struct zone *zone; |
Zi Yan | 88ee134 | 2022-05-24 15:47:56 -0400 | [diff] [blame] | 313 | int ret; |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 314 | |
Kefeng Wang | ee0913c | 2022-09-07 14:08:44 +0800 | [diff] [blame] | 315 | VM_BUG_ON(!pageblock_aligned(boundary_pfn)); |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 316 | |
| 317 | if (isolate_before) |
| 318 | isolate_pageblock = boundary_pfn - pageblock_nr_pages; |
| 319 | else |
| 320 | isolate_pageblock = boundary_pfn; |
| 321 | |
| 322 | /* |
| 323 | * scan at the beginning of MAX_ORDER_NR_PAGES aligned range to avoid |
| 324 | * only isolating a subset of pageblocks from a bigger than pageblock |
| 325 | * free or in-use page. Also make sure all to-be-isolated pageblocks |
| 326 | * are within the same zone. |
| 327 | */ |
| 328 | zone = page_zone(pfn_to_page(isolate_pageblock)); |
| 329 | start_pfn = max(ALIGN_DOWN(isolate_pageblock, MAX_ORDER_NR_PAGES), |
| 330 | zone->zone_start_pfn); |
| 331 | |
Zi Yan | 80e2b58 | 2022-09-13 22:39:13 -0400 | [diff] [blame] | 332 | if (skip_isolation) { |
| 333 | int mt = get_pageblock_migratetype(pfn_to_page(isolate_pageblock)); |
Zi Yan | 88ee134 | 2022-05-24 15:47:56 -0400 | [diff] [blame] | 334 | |
Zi Yan | 80e2b58 | 2022-09-13 22:39:13 -0400 | [diff] [blame] | 335 | VM_BUG_ON(!is_migrate_isolate(mt)); |
| 336 | } else { |
| 337 | ret = set_migratetype_isolate(pfn_to_page(isolate_pageblock), migratetype, |
| 338 | flags, isolate_pageblock, isolate_pageblock + pageblock_nr_pages); |
Zi Yan | 9b209e5 | 2022-05-26 19:15:30 -0400 | [diff] [blame] | 339 | |
| 340 | if (ret) |
| 341 | return ret; |
| 342 | } |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 343 | |
| 344 | /* |
| 345 | * Bail out early when the to-be-isolated pageblock does not form |
| 346 | * a free or in-use page across boundary_pfn: |
| 347 | * |
| 348 | * 1. isolate before boundary_pfn: the page after is not online |
| 349 | * 2. isolate after boundary_pfn: the page before is not online |
| 350 | * |
| 351 | * This also ensures correctness. Without it, when isolate after |
| 352 | * boundary_pfn and [start_pfn, boundary_pfn) are not online, |
| 353 | * __first_valid_page() will return unexpected NULL in the for loop |
| 354 | * below. |
| 355 | */ |
| 356 | if (isolate_before) { |
| 357 | if (!pfn_to_online_page(boundary_pfn)) |
| 358 | return 0; |
| 359 | } else { |
| 360 | if (!pfn_to_online_page(boundary_pfn - 1)) |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | for (pfn = start_pfn; pfn < boundary_pfn;) { |
| 365 | struct page *page = __first_valid_page(pfn, boundary_pfn - pfn); |
| 366 | |
| 367 | VM_BUG_ON(!page); |
| 368 | pfn = page_to_pfn(page); |
| 369 | /* |
| 370 | * start_pfn is MAX_ORDER_NR_PAGES aligned, if there is any |
| 371 | * free pages in [start_pfn, boundary_pfn), its head page will |
| 372 | * always be in the range. |
| 373 | */ |
| 374 | if (PageBuddy(page)) { |
| 375 | int order = buddy_order(page); |
| 376 | |
Zi Yan | 86d28b07 | 2022-05-26 19:15:31 -0400 | [diff] [blame] | 377 | if (pfn + (1UL << order) > boundary_pfn) { |
| 378 | /* free page changed before split, check it again */ |
| 379 | if (split_free_page(page, order, boundary_pfn - pfn)) |
| 380 | continue; |
| 381 | } |
| 382 | |
| 383 | pfn += 1UL << order; |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 384 | continue; |
| 385 | } |
| 386 | /* |
| 387 | * migrate compound pages then let the free page handling code |
| 388 | * above do the rest. If migration is not possible, just fail. |
| 389 | */ |
| 390 | if (PageCompound(page)) { |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 391 | struct page *head = compound_head(page); |
| 392 | unsigned long head_pfn = page_to_pfn(head); |
Zi Yan | 547be96 | 2022-05-30 22:44:50 -0400 | [diff] [blame] | 393 | unsigned long nr_pages = compound_nr(head); |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 394 | |
Zi Yan | 88ee134 | 2022-05-24 15:47:56 -0400 | [diff] [blame] | 395 | if (head_pfn + nr_pages <= boundary_pfn) { |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 396 | pfn = head_pfn + nr_pages; |
| 397 | continue; |
| 398 | } |
| 399 | #if defined CONFIG_COMPACTION || defined CONFIG_CMA |
| 400 | /* |
| 401 | * hugetlb, lru compound (THP), and movable compound pages |
| 402 | * can be migrated. Otherwise, fail the isolation. |
| 403 | */ |
| 404 | if (PageHuge(page) || PageLRU(page) || __PageMovable(page)) { |
| 405 | int order; |
| 406 | unsigned long outer_pfn; |
Zi Yan | 88ee134 | 2022-05-24 15:47:56 -0400 | [diff] [blame] | 407 | int page_mt = get_pageblock_migratetype(page); |
| 408 | bool isolate_page = !is_migrate_isolate_page(page); |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 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); |
| 420 | |
Zi Yan | 88ee134 | 2022-05-24 15:47:56 -0400 | [diff] [blame] | 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 | |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 435 | ret = __alloc_contig_migrate_range(&cc, head_pfn, |
| 436 | head_pfn + nr_pages); |
| 437 | |
Zi Yan | 88ee134 | 2022-05-24 15:47:56 -0400 | [diff] [blame] | 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 | |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 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))) { |
Zi Yan | 88ee134 | 2022-05-24 15:47:56 -0400 | [diff] [blame] | 461 | /* stop if we cannot find the free page */ |
| 462 | if (++order >= MAX_ORDER) |
| 463 | goto failed; |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 464 | outer_pfn &= ~0UL << order; |
| 465 | } |
| 466 | pfn = outer_pfn; |
| 467 | continue; |
| 468 | } else |
| 469 | #endif |
| 470 | goto failed; |
| 471 | } |
| 472 | |
| 473 | pfn++; |
| 474 | } |
| 475 | return 0; |
| 476 | failed: |
| 477 | /* restore the original migratetype */ |
Zi Yan | 9b209e5 | 2022-05-26 19:15:30 -0400 | [diff] [blame] | 478 | if (!skip_isolation) |
Zi Yan | 80e2b58 | 2022-09-13 22:39:13 -0400 | [diff] [blame] | 479 | unset_migratetype_isolate(pfn_to_page(isolate_pageblock), migratetype); |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 480 | return -EBUSY; |
| 481 | } |
| 482 | |
| 483 | /** |
Qian Cai | 9b7ea46 | 2019-03-28 20:43:34 -0700 | [diff] [blame] | 484 | * start_isolate_page_range() - make page-allocation-type of range of pages to |
| 485 | * be MIGRATE_ISOLATE. |
| 486 | * @start_pfn: The lower PFN of the range to be isolated. |
| 487 | * @end_pfn: The upper PFN of the range to be isolated. |
Qian Cai | 9b7ea46 | 2019-03-28 20:43:34 -0700 | [diff] [blame] | 488 | * @migratetype: Migrate type to set in error recovery. |
| 489 | * @flags: The following flags are allowed (they can be combined in |
| 490 | * a bit mask) |
David Hildenbrand | 756d25b | 2019-11-30 17:54:07 -0800 | [diff] [blame] | 491 | * MEMORY_OFFLINE - isolate to offline (!allocate) memory |
| 492 | * e.g., skip over PageHWPoison() pages |
David Hildenbrand | aa218795 | 2020-05-07 16:01:30 +0200 | [diff] [blame] | 493 | * and PageOffline() pages. |
Qian Cai | 9b7ea46 | 2019-03-28 20:43:34 -0700 | [diff] [blame] | 494 | * REPORT_FAILURE - report details about the failure to |
| 495 | * isolate the range |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 496 | * @gfp_flags: GFP flags used for migrating pages that sit across the |
| 497 | * range boundaries. |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 498 | * |
| 499 | * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in |
| 500 | * the range will never be allocated. Any free pages and pages freed in the |
Qian Cai | 9b7ea46 | 2019-03-28 20:43:34 -0700 | [diff] [blame] | 501 | * future will not be allocated again. If specified range includes migrate types |
| 502 | * other than MOVABLE or CMA, this will fail with -EBUSY. For isolating all |
| 503 | * pages in the range finally, the caller have to free all pages in the range. |
| 504 | * test_page_isolated() can be used for test it. |
Mike Kravetz | 2c7452a | 2018-04-05 16:25:26 -0700 | [diff] [blame] | 505 | * |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 506 | * The function first tries to isolate the pageblocks at the beginning and end |
| 507 | * of the range, since there might be pages across the range boundaries. |
| 508 | * Afterwards, it isolates the rest of the range. |
| 509 | * |
Mike Kravetz | 2c7452a | 2018-04-05 16:25:26 -0700 | [diff] [blame] | 510 | * There is no high level synchronization mechanism that prevents two threads |
Qian Cai | 9b7ea46 | 2019-03-28 20:43:34 -0700 | [diff] [blame] | 511 | * from trying to isolate overlapping ranges. If this happens, one thread |
Mike Kravetz | 2c7452a | 2018-04-05 16:25:26 -0700 | [diff] [blame] | 512 | * will notice pageblocks in the overlapping range already set to isolate. |
| 513 | * This happens in set_migratetype_isolate, and set_migratetype_isolate |
Qian Cai | 9b7ea46 | 2019-03-28 20:43:34 -0700 | [diff] [blame] | 514 | * returns an error. We then clean up by restoring the migration type on |
| 515 | * pageblocks we may have modified and return -EBUSY to caller. This |
Mike Kravetz | 2c7452a | 2018-04-05 16:25:26 -0700 | [diff] [blame] | 516 | * prevents two threads from simultaneously working on overlapping ranges. |
Qian Cai | 9b7ea46 | 2019-03-28 20:43:34 -0700 | [diff] [blame] | 517 | * |
Pavel Tatashin | 9683182 | 2020-09-18 21:20:31 -0700 | [diff] [blame] | 518 | * Please note that there is no strong synchronization with the page allocator |
| 519 | * either. Pages might be freed while their page blocks are marked ISOLATED. |
Vlastimil Babka | 7612921 | 2020-12-14 19:10:56 -0800 | [diff] [blame] | 520 | * A call to drain_all_pages() after isolation can flush most of them. However |
| 521 | * in some cases pages might still end up on pcp lists and that would allow |
Pavel Tatashin | 9683182 | 2020-09-18 21:20:31 -0700 | [diff] [blame] | 522 | * for their allocation even when they are in fact isolated already. Depending |
Vlastimil Babka | ec6e8c7e | 2020-12-14 19:10:59 -0800 | [diff] [blame] | 523 | * on how strong of a guarantee the caller needs, zone_pcp_disable/enable() |
| 524 | * might be used to flush and disable pcplist before isolation and enable after |
| 525 | * unisolation. |
Pavel Tatashin | 9683182 | 2020-09-18 21:20:31 -0700 | [diff] [blame] | 526 | * |
David Hildenbrand | 3fa0c7c | 2020-10-15 20:08:07 -0700 | [diff] [blame] | 527 | * Return: 0 on success and -EBUSY if any part of range cannot be isolated. |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 528 | */ |
Michal Nazarewicz | 0815f3d | 2012-04-03 15:06:15 +0200 | [diff] [blame] | 529 | int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn, |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 530 | int migratetype, int flags, gfp_t gfp_flags) |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 531 | { |
| 532 | unsigned long pfn; |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 533 | struct page *page; |
Zi Yan | 6e263ff | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 534 | /* isolation is done at page block granularity */ |
Kefeng Wang | 4f9bc69 | 2022-09-07 14:08:42 +0800 | [diff] [blame] | 535 | unsigned long isolate_start = pageblock_start_pfn(start_pfn); |
Kefeng Wang | 5f7fa13 | 2022-09-07 14:08:43 +0800 | [diff] [blame] | 536 | unsigned long isolate_end = pageblock_align(end_pfn); |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 537 | int ret; |
Zi Yan | 9b209e5 | 2022-05-26 19:15:30 -0400 | [diff] [blame] | 538 | bool skip_isolation = false; |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 539 | |
Zi Yan | 6e263ff | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 540 | /* isolate [isolate_start, isolate_start + pageblock_nr_pages) pageblock */ |
Zi Yan | 80e2b58 | 2022-09-13 22:39:13 -0400 | [diff] [blame] | 541 | ret = isolate_single_pageblock(isolate_start, flags, gfp_flags, false, |
| 542 | skip_isolation, migratetype); |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 543 | if (ret) |
| 544 | return ret; |
| 545 | |
Zi Yan | 9b209e5 | 2022-05-26 19:15:30 -0400 | [diff] [blame] | 546 | if (isolate_start == isolate_end - pageblock_nr_pages) |
| 547 | skip_isolation = true; |
| 548 | |
Zi Yan | 6e263ff | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 549 | /* isolate [isolate_end - pageblock_nr_pages, isolate_end) pageblock */ |
Zi Yan | 80e2b58 | 2022-09-13 22:39:13 -0400 | [diff] [blame] | 550 | ret = isolate_single_pageblock(isolate_end, flags, gfp_flags, true, |
| 551 | skip_isolation, migratetype); |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 552 | if (ret) { |
Zi Yan | 6e263ff | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 553 | unset_migratetype_isolate(pfn_to_page(isolate_start), migratetype); |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 554 | return ret; |
| 555 | } |
| 556 | |
| 557 | /* skip isolated pageblocks at the beginning and end */ |
Zi Yan | 6e263ff | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 558 | for (pfn = isolate_start + pageblock_nr_pages; |
| 559 | pfn < isolate_end - pageblock_nr_pages; |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 560 | pfn += pageblock_nr_pages) { |
| 561 | page = __first_valid_page(pfn, pageblock_nr_pages); |
Zi Yan | 844fbae | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 562 | if (page && set_migratetype_isolate(page, migratetype, flags, |
| 563 | start_pfn, end_pfn)) { |
Zi Yan | 6e263ff | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 564 | undo_isolate_page_range(isolate_start, pfn, migratetype); |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 565 | unset_migratetype_isolate( |
Zi Yan | 6e263ff | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 566 | pfn_to_page(isolate_end - pageblock_nr_pages), |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 567 | migratetype); |
Miaohe Lin | e1d8c96 | 2021-11-05 13:42:16 -0700 | [diff] [blame] | 568 | return -EBUSY; |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 569 | } |
| 570 | } |
David Hildenbrand | 3fa0c7c | 2020-10-15 20:08:07 -0700 | [diff] [blame] | 571 | return 0; |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /* |
| 575 | * Make isolated pages available again. |
| 576 | */ |
Pingfan Liu | 1fcf0a5 | 2019-07-11 20:54:49 -0700 | [diff] [blame] | 577 | void undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn, |
Zi Yan | b2c9e2f | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 578 | int migratetype) |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 579 | { |
| 580 | unsigned long pfn; |
| 581 | struct page *page; |
Kefeng Wang | 4f9bc69 | 2022-09-07 14:08:42 +0800 | [diff] [blame] | 582 | unsigned long isolate_start = pageblock_start_pfn(start_pfn); |
Kefeng Wang | 5f7fa13 | 2022-09-07 14:08:43 +0800 | [diff] [blame] | 583 | unsigned long isolate_end = pageblock_align(end_pfn); |
Wang Xiaoqiang | 6f8d2b8 | 2016-01-15 16:57:13 -0800 | [diff] [blame] | 584 | |
Zi Yan | 6e263ff | 2022-05-12 20:22:58 -0700 | [diff] [blame] | 585 | for (pfn = isolate_start; |
| 586 | pfn < isolate_end; |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 587 | pfn += pageblock_nr_pages) { |
| 588 | page = __first_valid_page(pfn, pageblock_nr_pages); |
Xishi Qiu | bbf9ce9 | 2017-05-03 14:52:55 -0700 | [diff] [blame] | 589 | if (!page || !is_migrate_isolate_page(page)) |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 590 | continue; |
Michal Nazarewicz | 0815f3d | 2012-04-03 15:06:15 +0200 | [diff] [blame] | 591 | unset_migratetype_isolate(page, migratetype); |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 592 | } |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 593 | } |
| 594 | /* |
| 595 | * Test all pages in the range is free(means isolated) or not. |
| 596 | * all pages in [start_pfn...end_pfn) must be in the same zone. |
| 597 | * zone->lock must be held before call this. |
| 598 | * |
Neil Zhang | ec3b688 | 2016-04-01 14:31:37 -0700 | [diff] [blame] | 599 | * Returns the last tested pfn. |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 600 | */ |
Joonsoo Kim | fea85cf | 2016-01-14 15:18:39 -0800 | [diff] [blame] | 601 | static unsigned long |
Wen Congyang | b023f46 | 2012-12-11 16:00:45 -0800 | [diff] [blame] | 602 | __test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn, |
David Hildenbrand | 756d25b | 2019-11-30 17:54:07 -0800 | [diff] [blame] | 603 | int flags) |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 604 | { |
| 605 | struct page *page; |
| 606 | |
| 607 | while (pfn < end_pfn) { |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 608 | page = pfn_to_page(pfn); |
Vlastimil Babka | aa016d1 | 2015-09-08 15:01:22 -0700 | [diff] [blame] | 609 | if (PageBuddy(page)) |
Minchan Kim | 435b405 | 2012-10-08 16:32:16 -0700 | [diff] [blame] | 610 | /* |
Vlastimil Babka | aa016d1 | 2015-09-08 15:01:22 -0700 | [diff] [blame] | 611 | * If the page is on a free list, it has to be on |
| 612 | * the correct MIGRATE_ISOLATE freelist. There is no |
| 613 | * simple way to verify that as VM_BUG_ON(), though. |
Minchan Kim | 435b405 | 2012-10-08 16:32:16 -0700 | [diff] [blame] | 614 | */ |
Matthew Wilcox (Oracle) | ab130f91 | 2020-10-15 20:10:15 -0700 | [diff] [blame] | 615 | pfn += 1 << buddy_order(page); |
David Hildenbrand | 756d25b | 2019-11-30 17:54:07 -0800 | [diff] [blame] | 616 | else if ((flags & MEMORY_OFFLINE) && PageHWPoison(page)) |
Vlastimil Babka | aa016d1 | 2015-09-08 15:01:22 -0700 | [diff] [blame] | 617 | /* A HWPoisoned page cannot be also PageBuddy */ |
Wen Congyang | b023f46 | 2012-12-11 16:00:45 -0800 | [diff] [blame] | 618 | pfn++; |
David Hildenbrand | aa218795 | 2020-05-07 16:01:30 +0200 | [diff] [blame] | 619 | else if ((flags & MEMORY_OFFLINE) && PageOffline(page) && |
| 620 | !page_count(page)) |
| 621 | /* |
| 622 | * The responsible driver agreed to skip PageOffline() |
| 623 | * pages when offlining memory by dropping its |
| 624 | * reference in MEM_GOING_OFFLINE. |
| 625 | */ |
| 626 | pfn++; |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 627 | else |
| 628 | break; |
| 629 | } |
Joonsoo Kim | fea85cf | 2016-01-14 15:18:39 -0800 | [diff] [blame] | 630 | |
| 631 | return pfn; |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 632 | } |
| 633 | |
Joonsoo Kim | b9eb631 | 2016-05-19 17:12:06 -0700 | [diff] [blame] | 634 | /* Caller should ensure that requested range is in a single zone */ |
Wen Congyang | b023f46 | 2012-12-11 16:00:45 -0800 | [diff] [blame] | 635 | int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn, |
David Hildenbrand | 756d25b | 2019-11-30 17:54:07 -0800 | [diff] [blame] | 636 | int isol_flags) |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 637 | { |
Gerald Schaefer | 6c1b7f6 | 2008-10-02 14:50:16 -0700 | [diff] [blame] | 638 | unsigned long pfn, flags; |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 639 | struct page *page; |
Gerald Schaefer | 6c1b7f6 | 2008-10-02 14:50:16 -0700 | [diff] [blame] | 640 | struct zone *zone; |
George G. Davis | 1d09510 | 2021-09-02 14:58:16 -0700 | [diff] [blame] | 641 | int ret; |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 642 | |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 643 | /* |
Tang Chen | 85dbe70 | 2013-06-20 18:10:19 +0800 | [diff] [blame] | 644 | * Note: pageblock_nr_pages != MAX_ORDER. Then, chunks of free pages |
| 645 | * are not aligned to pageblock_nr_pages. |
| 646 | * Then we just check migratetype first. |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 647 | */ |
| 648 | for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) { |
| 649 | page = __first_valid_page(pfn, pageblock_nr_pages); |
Xishi Qiu | bbf9ce9 | 2017-05-03 14:52:55 -0700 | [diff] [blame] | 650 | if (page && !is_migrate_isolate_page(page)) |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 651 | break; |
| 652 | } |
Gerald Schaefer | a70dcb9 | 2008-11-06 12:53:36 -0800 | [diff] [blame] | 653 | page = __first_valid_page(start_pfn, end_pfn - start_pfn); |
George G. Davis | 1d09510 | 2021-09-02 14:58:16 -0700 | [diff] [blame] | 654 | if ((pfn < end_pfn) || !page) { |
| 655 | ret = -EBUSY; |
| 656 | goto out; |
| 657 | } |
| 658 | |
Tang Chen | 85dbe70 | 2013-06-20 18:10:19 +0800 | [diff] [blame] | 659 | /* Check all pages are free or marked as ISOLATED */ |
Gerald Schaefer | a70dcb9 | 2008-11-06 12:53:36 -0800 | [diff] [blame] | 660 | zone = page_zone(page); |
Gerald Schaefer | 6c1b7f6 | 2008-10-02 14:50:16 -0700 | [diff] [blame] | 661 | spin_lock_irqsave(&zone->lock, flags); |
David Hildenbrand | 756d25b | 2019-11-30 17:54:07 -0800 | [diff] [blame] | 662 | pfn = __test_page_isolated_in_pageblock(start_pfn, end_pfn, isol_flags); |
Gerald Schaefer | 6c1b7f6 | 2008-10-02 14:50:16 -0700 | [diff] [blame] | 663 | spin_unlock_irqrestore(&zone->lock, flags); |
Joonsoo Kim | fea85cf | 2016-01-14 15:18:39 -0800 | [diff] [blame] | 664 | |
George G. Davis | 1d09510 | 2021-09-02 14:58:16 -0700 | [diff] [blame] | 665 | ret = pfn < end_pfn ? -EBUSY : 0; |
| 666 | |
| 667 | out: |
Joonsoo Kim | 0f0848e | 2016-01-14 15:18:42 -0800 | [diff] [blame] | 668 | trace_test_pages_isolated(start_pfn, end_pfn, pfn); |
| 669 | |
George G. Davis | 1d09510 | 2021-09-02 14:58:16 -0700 | [diff] [blame] | 670 | return ret; |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 671 | } |