Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/mm/compaction.c |
| 4 | * |
| 5 | * Memory compaction for the reduction of external fragmentation. Note that |
| 6 | * this heavily depends upon page migration to do all the real heavy |
| 7 | * lifting |
| 8 | * |
| 9 | * Copyright IBM Corp. 2007-2010 Mel Gorman <mel@csn.ul.ie> |
| 10 | */ |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 11 | #include <linux/cpu.h> |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 12 | #include <linux/swap.h> |
| 13 | #include <linux/migrate.h> |
| 14 | #include <linux/compaction.h> |
| 15 | #include <linux/mm_inline.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 16 | #include <linux/sched/signal.h> |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 17 | #include <linux/backing-dev.h> |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 18 | #include <linux/sysctl.h> |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 19 | #include <linux/sysfs.h> |
Minchan Kim | 194159f | 2013-02-22 16:33:58 -0800 | [diff] [blame] | 20 | #include <linux/page-isolation.h> |
Andrey Ryabinin | b8c73fc | 2015-02-13 14:39:28 -0800 | [diff] [blame] | 21 | #include <linux/kasan.h> |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 22 | #include <linux/kthread.h> |
| 23 | #include <linux/freezer.h> |
Joonsoo Kim | 83358ec | 2016-07-26 15:23:43 -0700 | [diff] [blame] | 24 | #include <linux/page_owner.h> |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 25 | #include <linux/psi.h> |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 26 | #include "internal.h" |
Tao Zeng | a1db7dc | 2022-01-20 15:09:43 +0800 | [diff] [blame] | 27 | #ifdef CONFIG_AMLOGIC_PAGE_TRACE |
| 28 | #include <linux/amlogic/page_trace.h> |
| 29 | #endif |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 30 | #ifdef CONFIG_AMLOGIC_CMA |
| 31 | #include <linux/amlogic/aml_cma.h> |
| 32 | #endif |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 33 | |
Minchan Kim | 010fc29 | 2012-12-20 15:05:06 -0800 | [diff] [blame] | 34 | #ifdef CONFIG_COMPACTION |
| 35 | static inline void count_compact_event(enum vm_event_item item) |
| 36 | { |
| 37 | count_vm_event(item); |
| 38 | } |
| 39 | |
| 40 | static inline void count_compact_events(enum vm_event_item item, long delta) |
| 41 | { |
| 42 | count_vm_events(item, delta); |
| 43 | } |
| 44 | #else |
| 45 | #define count_compact_event(item) do { } while (0) |
| 46 | #define count_compact_events(item, delta) do { } while (0) |
| 47 | #endif |
| 48 | |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 49 | #if defined CONFIG_COMPACTION || defined CONFIG_CMA |
| 50 | |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 51 | #define CREATE_TRACE_POINTS |
| 52 | #include <trace/events/compaction.h> |
Robin Hsu | 1eeadb4 | 2022-05-13 10:19:50 +0800 | [diff] [blame] | 53 | #undef CREATE_TRACE_POINTS |
| 54 | #include <trace/hooks/mm.h> |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 55 | |
Vlastimil Babka | 06b6640 | 2016-05-19 17:11:48 -0700 | [diff] [blame] | 56 | #define block_start_pfn(pfn, order) round_down(pfn, 1UL << (order)) |
| 57 | #define block_end_pfn(pfn, order) ALIGN((pfn) + 1, 1UL << (order)) |
| 58 | #define pageblock_start_pfn(pfn) block_start_pfn(pfn, pageblock_order) |
| 59 | #define pageblock_end_pfn(pfn) block_end_pfn(pfn, pageblock_order) |
| 60 | |
Nitin Gupta | facdaa9 | 2020-08-11 18:31:00 -0700 | [diff] [blame] | 61 | /* |
| 62 | * Fragmentation score check interval for proactive compaction purposes. |
| 63 | */ |
Nitin Gupta | d34c0a7 | 2020-08-11 18:31:07 -0700 | [diff] [blame] | 64 | static const unsigned int HPAGE_FRAG_CHECK_INTERVAL_MSEC = 500; |
Nitin Gupta | facdaa9 | 2020-08-11 18:31:00 -0700 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * Page order with-respect-to which proactive compaction |
| 68 | * calculates external fragmentation, which is used as |
| 69 | * the "fragmentation score" of a node/zone. |
| 70 | */ |
| 71 | #if defined CONFIG_TRANSPARENT_HUGEPAGE |
| 72 | #define COMPACTION_HPAGE_ORDER HPAGE_PMD_ORDER |
Nitin Gupta | 2578873 | 2020-08-11 18:31:04 -0700 | [diff] [blame] | 73 | #elif defined CONFIG_HUGETLBFS |
Nitin Gupta | facdaa9 | 2020-08-11 18:31:00 -0700 | [diff] [blame] | 74 | #define COMPACTION_HPAGE_ORDER HUGETLB_PAGE_ORDER |
| 75 | #else |
| 76 | #define COMPACTION_HPAGE_ORDER (PMD_SHIFT - PAGE_SHIFT) |
| 77 | #endif |
| 78 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 79 | static unsigned long release_freepages(struct list_head *freelist) |
| 80 | { |
| 81 | struct page *page, *next; |
Vlastimil Babka | 6bace09 | 2014-12-10 15:43:31 -0800 | [diff] [blame] | 82 | unsigned long high_pfn = 0; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 83 | |
| 84 | list_for_each_entry_safe(page, next, freelist, lru) { |
Vlastimil Babka | 6bace09 | 2014-12-10 15:43:31 -0800 | [diff] [blame] | 85 | unsigned long pfn = page_to_pfn(page); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 86 | list_del(&page->lru); |
| 87 | __free_page(page); |
Vlastimil Babka | 6bace09 | 2014-12-10 15:43:31 -0800 | [diff] [blame] | 88 | if (pfn > high_pfn) |
| 89 | high_pfn = pfn; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Vlastimil Babka | 6bace09 | 2014-12-10 15:43:31 -0800 | [diff] [blame] | 92 | return high_pfn; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Mel Gorman | 4469ab9 | 2019-03-05 15:44:39 -0800 | [diff] [blame] | 95 | static void split_map_pages(struct list_head *list) |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 96 | { |
Joonsoo Kim | 66c6422 | 2016-07-26 15:23:40 -0700 | [diff] [blame] | 97 | unsigned int i, order, nr_pages; |
| 98 | struct page *page, *next; |
| 99 | LIST_HEAD(tmp_list); |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 100 | |
Joonsoo Kim | 66c6422 | 2016-07-26 15:23:40 -0700 | [diff] [blame] | 101 | list_for_each_entry_safe(page, next, list, lru) { |
| 102 | list_del(&page->lru); |
| 103 | |
| 104 | order = page_private(page); |
| 105 | nr_pages = 1 << order; |
Joonsoo Kim | 66c6422 | 2016-07-26 15:23:40 -0700 | [diff] [blame] | 106 | |
Joonsoo Kim | 46f24fd | 2016-07-26 15:23:58 -0700 | [diff] [blame] | 107 | post_alloc_hook(page, order, __GFP_MOVABLE); |
Joonsoo Kim | 66c6422 | 2016-07-26 15:23:40 -0700 | [diff] [blame] | 108 | if (order) |
| 109 | split_page(page, order); |
| 110 | |
| 111 | for (i = 0; i < nr_pages; i++) { |
| 112 | list_add(&page->lru, &tmp_list); |
| 113 | page++; |
| 114 | } |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 115 | } |
Joonsoo Kim | 66c6422 | 2016-07-26 15:23:40 -0700 | [diff] [blame] | 116 | |
| 117 | list_splice(&tmp_list, list); |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 120 | #ifdef CONFIG_COMPACTION |
Joonsoo Kim | 24e2716 | 2015-02-11 15:27:09 -0800 | [diff] [blame] | 121 | |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 122 | int PageMovable(struct page *page) |
| 123 | { |
| 124 | struct address_space *mapping; |
| 125 | |
| 126 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
| 127 | if (!__PageMovable(page)) |
| 128 | return 0; |
| 129 | |
| 130 | mapping = page_mapping(page); |
| 131 | if (mapping && mapping->a_ops && mapping->a_ops->isolate_page) |
| 132 | return 1; |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | EXPORT_SYMBOL(PageMovable); |
| 137 | |
| 138 | void __SetPageMovable(struct page *page, struct address_space *mapping) |
| 139 | { |
| 140 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
| 141 | VM_BUG_ON_PAGE((unsigned long)mapping & PAGE_MAPPING_MOVABLE, page); |
| 142 | page->mapping = (void *)((unsigned long)mapping | PAGE_MAPPING_MOVABLE); |
| 143 | } |
| 144 | EXPORT_SYMBOL(__SetPageMovable); |
| 145 | |
| 146 | void __ClearPageMovable(struct page *page) |
| 147 | { |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 148 | VM_BUG_ON_PAGE(!PageMovable(page), page); |
| 149 | /* |
| 150 | * Clear registered address_space val with keeping PAGE_MAPPING_MOVABLE |
| 151 | * flag so that VM can catch up released page by driver after isolation. |
| 152 | * With it, VM migration doesn't try to put it back. |
| 153 | */ |
| 154 | page->mapping = (void *)((unsigned long)page->mapping & |
| 155 | PAGE_MAPPING_MOVABLE); |
| 156 | } |
| 157 | EXPORT_SYMBOL(__ClearPageMovable); |
| 158 | |
Joonsoo Kim | 24e2716 | 2015-02-11 15:27:09 -0800 | [diff] [blame] | 159 | /* Do not skip compaction more than 64 times */ |
| 160 | #define COMPACT_MAX_DEFER_SHIFT 6 |
| 161 | |
| 162 | /* |
| 163 | * Compaction is deferred when compaction fails to result in a page |
Alex Shi | 860b327 | 2020-08-11 18:31:10 -0700 | [diff] [blame] | 164 | * allocation success. 1 << compact_defer_shift, compactions are skipped up |
Joonsoo Kim | 24e2716 | 2015-02-11 15:27:09 -0800 | [diff] [blame] | 165 | * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT |
| 166 | */ |
Hui Su | 2271b01 | 2020-12-14 19:12:46 -0800 | [diff] [blame] | 167 | static void defer_compaction(struct zone *zone, int order) |
Joonsoo Kim | 24e2716 | 2015-02-11 15:27:09 -0800 | [diff] [blame] | 168 | { |
| 169 | zone->compact_considered = 0; |
| 170 | zone->compact_defer_shift++; |
| 171 | |
| 172 | if (order < zone->compact_order_failed) |
| 173 | zone->compact_order_failed = order; |
| 174 | |
| 175 | if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT) |
| 176 | zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT; |
| 177 | |
| 178 | trace_mm_compaction_defer_compaction(zone, order); |
| 179 | } |
| 180 | |
| 181 | /* Returns true if compaction should be skipped this time */ |
Hui Su | 2271b01 | 2020-12-14 19:12:46 -0800 | [diff] [blame] | 182 | static bool compaction_deferred(struct zone *zone, int order) |
Joonsoo Kim | 24e2716 | 2015-02-11 15:27:09 -0800 | [diff] [blame] | 183 | { |
| 184 | unsigned long defer_limit = 1UL << zone->compact_defer_shift; |
| 185 | |
| 186 | if (order < zone->compact_order_failed) |
| 187 | return false; |
| 188 | |
| 189 | /* Avoid possible overflow */ |
Mateusz Nosek | 62b35fe | 2020-10-13 16:56:58 -0700 | [diff] [blame] | 190 | if (++zone->compact_considered >= defer_limit) { |
Joonsoo Kim | 24e2716 | 2015-02-11 15:27:09 -0800 | [diff] [blame] | 191 | zone->compact_considered = defer_limit; |
Joonsoo Kim | 24e2716 | 2015-02-11 15:27:09 -0800 | [diff] [blame] | 192 | return false; |
Mateusz Nosek | 62b35fe | 2020-10-13 16:56:58 -0700 | [diff] [blame] | 193 | } |
Joonsoo Kim | 24e2716 | 2015-02-11 15:27:09 -0800 | [diff] [blame] | 194 | |
| 195 | trace_mm_compaction_deferred(zone, order); |
| 196 | |
| 197 | return true; |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * Update defer tracking counters after successful compaction of given order, |
| 202 | * which means an allocation either succeeded (alloc_success == true) or is |
| 203 | * expected to succeed. |
| 204 | */ |
| 205 | void compaction_defer_reset(struct zone *zone, int order, |
| 206 | bool alloc_success) |
| 207 | { |
| 208 | if (alloc_success) { |
| 209 | zone->compact_considered = 0; |
| 210 | zone->compact_defer_shift = 0; |
| 211 | } |
| 212 | if (order >= zone->compact_order_failed) |
| 213 | zone->compact_order_failed = order + 1; |
| 214 | |
| 215 | trace_mm_compaction_defer_reset(zone, order); |
| 216 | } |
| 217 | |
| 218 | /* Returns true if restarting compaction after many failures */ |
Hui Su | 2271b01 | 2020-12-14 19:12:46 -0800 | [diff] [blame] | 219 | static bool compaction_restarting(struct zone *zone, int order) |
Joonsoo Kim | 24e2716 | 2015-02-11 15:27:09 -0800 | [diff] [blame] | 220 | { |
| 221 | if (order < zone->compact_order_failed) |
| 222 | return false; |
| 223 | |
| 224 | return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT && |
| 225 | zone->compact_considered >= 1UL << zone->compact_defer_shift; |
| 226 | } |
| 227 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 228 | /* Returns true if the pageblock should be scanned for pages to isolate. */ |
| 229 | static inline bool isolation_suitable(struct compact_control *cc, |
| 230 | struct page *page) |
| 231 | { |
| 232 | if (cc->ignore_skip_hint) |
| 233 | return true; |
| 234 | |
| 235 | return !get_pageblock_skip(page); |
| 236 | } |
| 237 | |
Vlastimil Babka | 02333641 | 2015-09-08 15:02:42 -0700 | [diff] [blame] | 238 | static void reset_cached_positions(struct zone *zone) |
| 239 | { |
| 240 | zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn; |
| 241 | zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn; |
Joonsoo Kim | 623446e | 2016-03-15 14:57:45 -0700 | [diff] [blame] | 242 | zone->compact_cached_free_pfn = |
Vlastimil Babka | 06b6640 | 2016-05-19 17:11:48 -0700 | [diff] [blame] | 243 | pageblock_start_pfn(zone_end_pfn(zone) - 1); |
Vlastimil Babka | 02333641 | 2015-09-08 15:02:42 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 246 | /* |
Hui Su | 2271b01 | 2020-12-14 19:12:46 -0800 | [diff] [blame] | 247 | * Compound pages of >= pageblock_order should consistently be skipped until |
Vlastimil Babka | b527cfe | 2017-11-17 15:26:34 -0800 | [diff] [blame] | 248 | * released. It is always pointless to compact pages of such order (if they are |
| 249 | * migratable), and the pageblocks they occupy cannot contain any free pages. |
David Rientjes | 21dc7e0 | 2017-11-17 15:26:30 -0800 | [diff] [blame] | 250 | */ |
Vlastimil Babka | b527cfe | 2017-11-17 15:26:34 -0800 | [diff] [blame] | 251 | static bool pageblock_skip_persistent(struct page *page) |
David Rientjes | 21dc7e0 | 2017-11-17 15:26:30 -0800 | [diff] [blame] | 252 | { |
Vlastimil Babka | b527cfe | 2017-11-17 15:26:34 -0800 | [diff] [blame] | 253 | if (!PageCompound(page)) |
David Rientjes | 21dc7e0 | 2017-11-17 15:26:30 -0800 | [diff] [blame] | 254 | return false; |
Vlastimil Babka | b527cfe | 2017-11-17 15:26:34 -0800 | [diff] [blame] | 255 | |
| 256 | page = compound_head(page); |
| 257 | |
| 258 | if (compound_order(page) >= pageblock_order) |
| 259 | return true; |
| 260 | |
| 261 | return false; |
David Rientjes | 21dc7e0 | 2017-11-17 15:26:30 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Mel Gorman | e332f74 | 2019-03-05 15:45:38 -0800 | [diff] [blame] | 264 | static bool |
| 265 | __reset_isolation_pfn(struct zone *zone, unsigned long pfn, bool check_source, |
| 266 | bool check_target) |
| 267 | { |
| 268 | struct page *page = pfn_to_online_page(pfn); |
Mel Gorman | 6b0868c | 2019-04-04 11:54:09 +0100 | [diff] [blame] | 269 | struct page *block_page; |
Mel Gorman | e332f74 | 2019-03-05 15:45:38 -0800 | [diff] [blame] | 270 | struct page *end_page; |
| 271 | unsigned long block_pfn; |
| 272 | |
| 273 | if (!page) |
| 274 | return false; |
| 275 | if (zone != page_zone(page)) |
| 276 | return false; |
| 277 | if (pageblock_skip_persistent(page)) |
| 278 | return false; |
| 279 | |
| 280 | /* |
| 281 | * If skip is already cleared do no further checking once the |
| 282 | * restart points have been set. |
| 283 | */ |
| 284 | if (check_source && check_target && !get_pageblock_skip(page)) |
| 285 | return true; |
| 286 | |
| 287 | /* |
| 288 | * If clearing skip for the target scanner, do not select a |
| 289 | * non-movable pageblock as the starting point. |
| 290 | */ |
| 291 | if (!check_source && check_target && |
| 292 | get_pageblock_migratetype(page) != MIGRATE_MOVABLE) |
| 293 | return false; |
| 294 | |
Mel Gorman | 6b0868c | 2019-04-04 11:54:09 +0100 | [diff] [blame] | 295 | /* Ensure the start of the pageblock or zone is online and valid */ |
| 296 | block_pfn = pageblock_start_pfn(pfn); |
Vlastimil Babka | a2e9a5a | 2019-10-14 14:12:07 -0700 | [diff] [blame] | 297 | block_pfn = max(block_pfn, zone->zone_start_pfn); |
| 298 | block_page = pfn_to_online_page(block_pfn); |
Mel Gorman | 6b0868c | 2019-04-04 11:54:09 +0100 | [diff] [blame] | 299 | if (block_page) { |
| 300 | page = block_page; |
| 301 | pfn = block_pfn; |
| 302 | } |
| 303 | |
| 304 | /* Ensure the end of the pageblock or zone is online and valid */ |
Vlastimil Babka | a2e9a5a | 2019-10-14 14:12:07 -0700 | [diff] [blame] | 305 | block_pfn = pageblock_end_pfn(pfn) - 1; |
Mel Gorman | 6b0868c | 2019-04-04 11:54:09 +0100 | [diff] [blame] | 306 | block_pfn = min(block_pfn, zone_end_pfn(zone) - 1); |
| 307 | end_page = pfn_to_online_page(block_pfn); |
| 308 | if (!end_page) |
| 309 | return false; |
| 310 | |
Mel Gorman | e332f74 | 2019-03-05 15:45:38 -0800 | [diff] [blame] | 311 | /* |
| 312 | * Only clear the hint if a sample indicates there is either a |
| 313 | * free page or an LRU page in the block. One or other condition |
| 314 | * is necessary for the block to be a migration source/target. |
| 315 | */ |
Mel Gorman | e332f74 | 2019-03-05 15:45:38 -0800 | [diff] [blame] | 316 | do { |
Mike Rapoport | 859a85d | 2021-09-07 19:54:52 -0700 | [diff] [blame] | 317 | if (check_source && PageLRU(page)) { |
| 318 | clear_pageblock_skip(page); |
| 319 | return true; |
| 320 | } |
Mel Gorman | e332f74 | 2019-03-05 15:45:38 -0800 | [diff] [blame] | 321 | |
Mike Rapoport | 859a85d | 2021-09-07 19:54:52 -0700 | [diff] [blame] | 322 | if (check_target && PageBuddy(page)) { |
| 323 | clear_pageblock_skip(page); |
| 324 | return true; |
Mel Gorman | e332f74 | 2019-03-05 15:45:38 -0800 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | page += (1 << PAGE_ALLOC_COSTLY_ORDER); |
| 328 | pfn += (1 << PAGE_ALLOC_COSTLY_ORDER); |
Vlastimil Babka | a2e9a5a | 2019-10-14 14:12:07 -0700 | [diff] [blame] | 329 | } while (page <= end_page); |
Mel Gorman | e332f74 | 2019-03-05 15:45:38 -0800 | [diff] [blame] | 330 | |
| 331 | return false; |
| 332 | } |
| 333 | |
David Rientjes | 21dc7e0 | 2017-11-17 15:26:30 -0800 | [diff] [blame] | 334 | /* |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 335 | * This function is called to clear all cached information on pageblocks that |
| 336 | * should be skipped for page isolation when the migrate and free page scanner |
| 337 | * meet. |
| 338 | */ |
Mel Gorman | 6299702 | 2012-10-08 16:32:47 -0700 | [diff] [blame] | 339 | static void __reset_isolation_suitable(struct zone *zone) |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 340 | { |
Mel Gorman | e332f74 | 2019-03-05 15:45:38 -0800 | [diff] [blame] | 341 | unsigned long migrate_pfn = zone->zone_start_pfn; |
Mel Gorman | 6b0868c | 2019-04-04 11:54:09 +0100 | [diff] [blame] | 342 | unsigned long free_pfn = zone_end_pfn(zone) - 1; |
Mel Gorman | e332f74 | 2019-03-05 15:45:38 -0800 | [diff] [blame] | 343 | unsigned long reset_migrate = free_pfn; |
| 344 | unsigned long reset_free = migrate_pfn; |
| 345 | bool source_set = false; |
| 346 | bool free_set = false; |
| 347 | |
| 348 | if (!zone->compact_blockskip_flush) |
| 349 | return; |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 350 | |
Mel Gorman | 6299702 | 2012-10-08 16:32:47 -0700 | [diff] [blame] | 351 | zone->compact_blockskip_flush = false; |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 352 | |
Mel Gorman | e332f74 | 2019-03-05 15:45:38 -0800 | [diff] [blame] | 353 | /* |
| 354 | * Walk the zone and update pageblock skip information. Source looks |
| 355 | * for PageLRU while target looks for PageBuddy. When the scanner |
| 356 | * is found, both PageBuddy and PageLRU are checked as the pageblock |
| 357 | * is suitable as both source and target. |
| 358 | */ |
| 359 | for (; migrate_pfn < free_pfn; migrate_pfn += pageblock_nr_pages, |
| 360 | free_pfn -= pageblock_nr_pages) { |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 361 | cond_resched(); |
| 362 | |
Mel Gorman | e332f74 | 2019-03-05 15:45:38 -0800 | [diff] [blame] | 363 | /* Update the migrate PFN */ |
| 364 | if (__reset_isolation_pfn(zone, migrate_pfn, true, source_set) && |
| 365 | migrate_pfn < reset_migrate) { |
| 366 | source_set = true; |
| 367 | reset_migrate = migrate_pfn; |
| 368 | zone->compact_init_migrate_pfn = reset_migrate; |
| 369 | zone->compact_cached_migrate_pfn[0] = reset_migrate; |
| 370 | zone->compact_cached_migrate_pfn[1] = reset_migrate; |
| 371 | } |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 372 | |
Mel Gorman | e332f74 | 2019-03-05 15:45:38 -0800 | [diff] [blame] | 373 | /* Update the free PFN */ |
| 374 | if (__reset_isolation_pfn(zone, free_pfn, free_set, true) && |
| 375 | free_pfn > reset_free) { |
| 376 | free_set = true; |
| 377 | reset_free = free_pfn; |
| 378 | zone->compact_init_free_pfn = reset_free; |
| 379 | zone->compact_cached_free_pfn = reset_free; |
| 380 | } |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 381 | } |
Vlastimil Babka | 02333641 | 2015-09-08 15:02:42 -0700 | [diff] [blame] | 382 | |
Mel Gorman | e332f74 | 2019-03-05 15:45:38 -0800 | [diff] [blame] | 383 | /* Leave no distance if no suitable block was reset */ |
| 384 | if (reset_migrate >= reset_free) { |
| 385 | zone->compact_cached_migrate_pfn[0] = migrate_pfn; |
| 386 | zone->compact_cached_migrate_pfn[1] = migrate_pfn; |
| 387 | zone->compact_cached_free_pfn = free_pfn; |
| 388 | } |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Mel Gorman | 6299702 | 2012-10-08 16:32:47 -0700 | [diff] [blame] | 391 | void reset_isolation_suitable(pg_data_t *pgdat) |
| 392 | { |
| 393 | int zoneid; |
| 394 | |
| 395 | for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { |
| 396 | struct zone *zone = &pgdat->node_zones[zoneid]; |
| 397 | if (!populated_zone(zone)) |
| 398 | continue; |
| 399 | |
| 400 | /* Only flush if a full compaction finished recently */ |
| 401 | if (zone->compact_blockskip_flush) |
| 402 | __reset_isolation_suitable(zone); |
| 403 | } |
| 404 | } |
| 405 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 406 | /* |
Mel Gorman | e380beb | 2019-03-05 15:44:58 -0800 | [diff] [blame] | 407 | * Sets the pageblock skip bit if it was clear. Note that this is a hint as |
| 408 | * locks are not required for read/writers. Returns true if it was already set. |
| 409 | */ |
| 410 | static bool test_and_set_skip(struct compact_control *cc, struct page *page, |
| 411 | unsigned long pfn) |
| 412 | { |
| 413 | bool skip; |
| 414 | |
| 415 | /* Do no update if skip hint is being ignored */ |
| 416 | if (cc->ignore_skip_hint) |
| 417 | return false; |
| 418 | |
| 419 | if (!IS_ALIGNED(pfn, pageblock_nr_pages)) |
| 420 | return false; |
| 421 | |
| 422 | skip = get_pageblock_skip(page); |
| 423 | if (!skip && !cc->no_set_skip_hint) |
| 424 | set_pageblock_skip(page); |
| 425 | |
| 426 | return skip; |
| 427 | } |
| 428 | |
| 429 | static void update_cached_migrate(struct compact_control *cc, unsigned long pfn) |
| 430 | { |
| 431 | struct zone *zone = cc->zone; |
| 432 | |
| 433 | pfn = pageblock_end_pfn(pfn); |
| 434 | |
| 435 | /* Set for isolation rather than compaction */ |
| 436 | if (cc->no_set_skip_hint) |
| 437 | return; |
| 438 | |
| 439 | if (pfn > zone->compact_cached_migrate_pfn[0]) |
| 440 | zone->compact_cached_migrate_pfn[0] = pfn; |
| 441 | if (cc->mode != MIGRATE_ASYNC && |
| 442 | pfn > zone->compact_cached_migrate_pfn[1]) |
| 443 | zone->compact_cached_migrate_pfn[1] = pfn; |
| 444 | } |
| 445 | |
| 446 | /* |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 447 | * If no pages were isolated then mark this pageblock to be skipped in the |
Mel Gorman | 6299702 | 2012-10-08 16:32:47 -0700 | [diff] [blame] | 448 | * future. The information is later cleared by __reset_isolation_suitable(). |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 449 | */ |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 450 | static void update_pageblock_skip(struct compact_control *cc, |
Mel Gorman | d097a6f | 2019-03-05 15:45:28 -0800 | [diff] [blame] | 451 | struct page *page, unsigned long pfn) |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 452 | { |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 453 | struct zone *zone = cc->zone; |
Joonsoo Kim | 6815bf3 | 2013-12-18 17:08:52 -0800 | [diff] [blame] | 454 | |
Vlastimil Babka | 2583d67 | 2017-11-17 15:26:38 -0800 | [diff] [blame] | 455 | if (cc->no_set_skip_hint) |
Joonsoo Kim | 6815bf3 | 2013-12-18 17:08:52 -0800 | [diff] [blame] | 456 | return; |
| 457 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 458 | if (!page) |
| 459 | return; |
| 460 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 461 | set_pageblock_skip(page); |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 462 | |
David Rientjes | 35979ef | 2014-06-04 16:08:27 -0700 | [diff] [blame] | 463 | /* Update where async and sync compaction should restart */ |
Mel Gorman | e380beb | 2019-03-05 15:44:58 -0800 | [diff] [blame] | 464 | if (pfn < zone->compact_cached_free_pfn) |
| 465 | zone->compact_cached_free_pfn = pfn; |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 466 | } |
| 467 | #else |
| 468 | static inline bool isolation_suitable(struct compact_control *cc, |
| 469 | struct page *page) |
| 470 | { |
| 471 | return true; |
| 472 | } |
| 473 | |
Vlastimil Babka | b527cfe | 2017-11-17 15:26:34 -0800 | [diff] [blame] | 474 | static inline bool pageblock_skip_persistent(struct page *page) |
David Rientjes | 21dc7e0 | 2017-11-17 15:26:30 -0800 | [diff] [blame] | 475 | { |
| 476 | return false; |
| 477 | } |
| 478 | |
| 479 | static inline void update_pageblock_skip(struct compact_control *cc, |
Mel Gorman | d097a6f | 2019-03-05 15:45:28 -0800 | [diff] [blame] | 480 | struct page *page, unsigned long pfn) |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 481 | { |
| 482 | } |
Mel Gorman | e380beb | 2019-03-05 15:44:58 -0800 | [diff] [blame] | 483 | |
| 484 | static void update_cached_migrate(struct compact_control *cc, unsigned long pfn) |
| 485 | { |
| 486 | } |
| 487 | |
| 488 | static bool test_and_set_skip(struct compact_control *cc, struct page *page, |
| 489 | unsigned long pfn) |
| 490 | { |
| 491 | return false; |
| 492 | } |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 493 | #endif /* CONFIG_COMPACTION */ |
| 494 | |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 495 | /* |
| 496 | * Compaction requires the taking of some coarse locks that are potentially |
Mel Gorman | cb2dcaf | 2019-03-05 15:45:11 -0800 | [diff] [blame] | 497 | * very heavily contended. For async compaction, trylock and record if the |
| 498 | * lock is contended. The lock will still be acquired but compaction will |
| 499 | * abort when the current block is finished regardless of success rate. |
| 500 | * Sync compaction acquires the lock. |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 501 | * |
Mel Gorman | cb2dcaf | 2019-03-05 15:45:11 -0800 | [diff] [blame] | 502 | * Always returns true which makes it easier to track lock state in callers. |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 503 | */ |
Mel Gorman | cb2dcaf | 2019-03-05 15:45:11 -0800 | [diff] [blame] | 504 | static bool compact_lock_irqsave(spinlock_t *lock, unsigned long *flags, |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 505 | struct compact_control *cc) |
Jules Irenge | 77337ed | 2020-04-06 20:08:06 -0700 | [diff] [blame] | 506 | __acquires(lock) |
Mel Gorman | 2a1402a | 2012-10-08 16:32:33 -0700 | [diff] [blame] | 507 | { |
Mel Gorman | cb2dcaf | 2019-03-05 15:45:11 -0800 | [diff] [blame] | 508 | /* Track if the lock is contended in async mode */ |
| 509 | if (cc->mode == MIGRATE_ASYNC && !cc->contended) { |
| 510 | if (spin_trylock_irqsave(lock, *flags)) |
| 511 | return true; |
| 512 | |
| 513 | cc->contended = true; |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 514 | } |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame] | 515 | |
Mel Gorman | cb2dcaf | 2019-03-05 15:45:11 -0800 | [diff] [blame] | 516 | spin_lock_irqsave(lock, *flags); |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 517 | return true; |
Mel Gorman | 2a1402a | 2012-10-08 16:32:33 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 520 | /* |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 521 | * Compaction requires the taking of some coarse locks that are potentially |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 522 | * very heavily contended. The lock should be periodically unlocked to avoid |
| 523 | * having disabled IRQs for a long time, even when there is nobody waiting on |
| 524 | * the lock. It might also be that allowing the IRQs will result in |
| 525 | * need_resched() becoming true. If scheduling is needed, async compaction |
| 526 | * aborts. Sync compaction schedules. |
| 527 | * Either compaction type will also abort if a fatal signal is pending. |
| 528 | * In either case if the lock was locked, it is dropped and not regained. |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 529 | * |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 530 | * Returns true if compaction should abort due to fatal signal pending, or |
| 531 | * async compaction due to need_resched() |
| 532 | * Returns false when compaction can continue (sync compaction might have |
| 533 | * scheduled) |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 534 | */ |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 535 | static bool compact_unlock_should_abort(spinlock_t *lock, |
| 536 | unsigned long flags, bool *locked, struct compact_control *cc) |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 537 | { |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 538 | if (*locked) { |
| 539 | spin_unlock_irqrestore(lock, flags); |
| 540 | *locked = false; |
| 541 | } |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame] | 542 | |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 543 | if (fatal_signal_pending(current)) { |
Vlastimil Babka | c3486f5 | 2016-07-28 15:49:30 -0700 | [diff] [blame] | 544 | cc->contended = true; |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 545 | return true; |
| 546 | } |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 547 | |
Mel Gorman | cf66f07 | 2019-03-05 15:45:24 -0800 | [diff] [blame] | 548 | cond_resched(); |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 549 | |
| 550 | return false; |
| 551 | } |
| 552 | |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 553 | /* |
Jerome Marchand | 9e4be47 | 2013-11-12 15:07:12 -0800 | [diff] [blame] | 554 | * Isolate free pages onto a private freelist. If @strict is true, will abort |
| 555 | * returning 0 on any invalid PFNs or non-free pages inside of the pageblock |
| 556 | * (even though it may still end up isolating some pages). |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 557 | */ |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 558 | static unsigned long isolate_freepages_block(struct compact_control *cc, |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 559 | unsigned long *start_pfn, |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 560 | unsigned long end_pfn, |
| 561 | struct list_head *freelist, |
Mel Gorman | 4fca973 | 2019-03-05 15:45:34 -0800 | [diff] [blame] | 562 | unsigned int stride, |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 563 | bool strict) |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 564 | { |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 565 | int nr_scanned = 0, total_isolated = 0; |
Mel Gorman | d097a6f | 2019-03-05 15:45:28 -0800 | [diff] [blame] | 566 | struct page *cursor; |
Xiubo Li | b8b2d82 | 2014-10-09 15:28:21 -0700 | [diff] [blame] | 567 | unsigned long flags = 0; |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 568 | bool locked = false; |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 569 | unsigned long blockpfn = *start_pfn; |
Joonsoo Kim | 66c6422 | 2016-07-26 15:23:40 -0700 | [diff] [blame] | 570 | unsigned int order; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 571 | |
Mel Gorman | 4fca973 | 2019-03-05 15:45:34 -0800 | [diff] [blame] | 572 | /* Strict mode is for isolation, speed is secondary */ |
| 573 | if (strict) |
| 574 | stride = 1; |
| 575 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 576 | cursor = pfn_to_page(blockpfn); |
| 577 | |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 578 | /* Isolate free pages. */ |
Mel Gorman | 4fca973 | 2019-03-05 15:45:34 -0800 | [diff] [blame] | 579 | for (; blockpfn < end_pfn; blockpfn += stride, cursor += stride) { |
Joonsoo Kim | 66c6422 | 2016-07-26 15:23:40 -0700 | [diff] [blame] | 580 | int isolated; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 581 | struct page *page = cursor; |
| 582 | |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 583 | /* |
| 584 | * Periodically drop the lock (if held) regardless of its |
| 585 | * contention, to give chance to IRQs. Abort if fatal signal |
| 586 | * pending or async compaction detects need_resched() |
| 587 | */ |
| 588 | if (!(blockpfn % SWAP_CLUSTER_MAX) |
| 589 | && compact_unlock_should_abort(&cc->zone->lock, flags, |
| 590 | &locked, cc)) |
| 591 | break; |
| 592 | |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 593 | nr_scanned++; |
Laura Abbott | 2af120b | 2014-03-10 15:49:44 -0700 | [diff] [blame] | 594 | |
Vlastimil Babka | 9fcd6d2 | 2015-09-08 15:02:49 -0700 | [diff] [blame] | 595 | /* |
| 596 | * For compound pages such as THP and hugetlbfs, we can save |
| 597 | * potentially a lot of iterations if we skip them at once. |
| 598 | * The check is racy, but we can consider only valid values |
| 599 | * and the only danger is skipping too much. |
| 600 | */ |
| 601 | if (PageCompound(page)) { |
David Rientjes | 21dc7e0 | 2017-11-17 15:26:30 -0800 | [diff] [blame] | 602 | const unsigned int order = compound_order(page); |
Vlastimil Babka | 9fcd6d2 | 2015-09-08 15:02:49 -0700 | [diff] [blame] | 603 | |
Vlastimil Babka | d3c85ba | 2017-11-17 15:26:41 -0800 | [diff] [blame] | 604 | if (likely(order < MAX_ORDER)) { |
David Rientjes | 21dc7e0 | 2017-11-17 15:26:30 -0800 | [diff] [blame] | 605 | blockpfn += (1UL << order) - 1; |
| 606 | cursor += (1UL << order) - 1; |
Vlastimil Babka | 9fcd6d2 | 2015-09-08 15:02:49 -0700 | [diff] [blame] | 607 | } |
Vlastimil Babka | 9fcd6d2 | 2015-09-08 15:02:49 -0700 | [diff] [blame] | 608 | goto isolate_fail; |
| 609 | } |
| 610 | |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 611 | if (!PageBuddy(page)) |
Laura Abbott | 2af120b | 2014-03-10 15:49:44 -0700 | [diff] [blame] | 612 | goto isolate_fail; |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 613 | |
| 614 | /* |
Vlastimil Babka | 69b7189 | 2014-10-09 15:27:18 -0700 | [diff] [blame] | 615 | * If we already hold the lock, we can skip some rechecking. |
| 616 | * Note that if we hold the lock now, checked_pageblock was |
| 617 | * already set in some previous iteration (or strict is true), |
| 618 | * so it is correct to skip the suitable migration target |
| 619 | * recheck as well. |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 620 | */ |
Vlastimil Babka | 69b7189 | 2014-10-09 15:27:18 -0700 | [diff] [blame] | 621 | if (!locked) { |
Mel Gorman | cb2dcaf | 2019-03-05 15:45:11 -0800 | [diff] [blame] | 622 | locked = compact_lock_irqsave(&cc->zone->lock, |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 623 | &flags, cc); |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 624 | |
Vlastimil Babka | 69b7189 | 2014-10-09 15:27:18 -0700 | [diff] [blame] | 625 | /* Recheck this is a buddy page under lock */ |
| 626 | if (!PageBuddy(page)) |
| 627 | goto isolate_fail; |
| 628 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 629 | |
Joonsoo Kim | 66c6422 | 2016-07-26 15:23:40 -0700 | [diff] [blame] | 630 | /* Found a free page, will break it into order-0 pages */ |
Matthew Wilcox (Oracle) | ab130f91 | 2020-10-15 20:10:15 -0700 | [diff] [blame] | 631 | order = buddy_order(page); |
Joonsoo Kim | 66c6422 | 2016-07-26 15:23:40 -0700 | [diff] [blame] | 632 | isolated = __isolate_free_page(page, order); |
David Rientjes | a4f04f2 | 2016-06-24 14:50:10 -0700 | [diff] [blame] | 633 | if (!isolated) |
| 634 | break; |
Joonsoo Kim | 66c6422 | 2016-07-26 15:23:40 -0700 | [diff] [blame] | 635 | set_page_private(page, order); |
David Rientjes | a4f04f2 | 2016-06-24 14:50:10 -0700 | [diff] [blame] | 636 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 637 | total_isolated += isolated; |
David Rientjes | a4f04f2 | 2016-06-24 14:50:10 -0700 | [diff] [blame] | 638 | cc->nr_freepages += isolated; |
Joonsoo Kim | 66c6422 | 2016-07-26 15:23:40 -0700 | [diff] [blame] | 639 | list_add_tail(&page->lru, freelist); |
| 640 | |
David Rientjes | a4f04f2 | 2016-06-24 14:50:10 -0700 | [diff] [blame] | 641 | if (!strict && cc->nr_migratepages <= cc->nr_freepages) { |
| 642 | blockpfn += isolated; |
| 643 | break; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 644 | } |
David Rientjes | a4f04f2 | 2016-06-24 14:50:10 -0700 | [diff] [blame] | 645 | /* Advance to the end of split page */ |
| 646 | blockpfn += isolated - 1; |
| 647 | cursor += isolated - 1; |
| 648 | continue; |
Laura Abbott | 2af120b | 2014-03-10 15:49:44 -0700 | [diff] [blame] | 649 | |
| 650 | isolate_fail: |
| 651 | if (strict) |
| 652 | break; |
| 653 | else |
| 654 | continue; |
| 655 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 656 | } |
| 657 | |
David Rientjes | a4f04f2 | 2016-06-24 14:50:10 -0700 | [diff] [blame] | 658 | if (locked) |
| 659 | spin_unlock_irqrestore(&cc->zone->lock, flags); |
| 660 | |
Vlastimil Babka | 9fcd6d2 | 2015-09-08 15:02:49 -0700 | [diff] [blame] | 661 | /* |
| 662 | * There is a tiny chance that we have read bogus compound_order(), |
| 663 | * so be careful to not go outside of the pageblock. |
| 664 | */ |
| 665 | if (unlikely(blockpfn > end_pfn)) |
| 666 | blockpfn = end_pfn; |
| 667 | |
Joonsoo Kim | e34d85f | 2015-02-11 15:27:04 -0800 | [diff] [blame] | 668 | trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn, |
| 669 | nr_scanned, total_isolated); |
| 670 | |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 671 | /* Record how far we have got within the block */ |
| 672 | *start_pfn = blockpfn; |
| 673 | |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 674 | /* |
| 675 | * If strict isolation is requested by CMA then check that all the |
| 676 | * pages requested were isolated. If there were any failures, 0 is |
| 677 | * returned and CMA will fail. |
| 678 | */ |
Laura Abbott | 2af120b | 2014-03-10 15:49:44 -0700 | [diff] [blame] | 679 | if (strict && blockpfn < end_pfn) |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 680 | total_isolated = 0; |
| 681 | |
David Rientjes | 7f354a5 | 2017-02-22 15:44:50 -0800 | [diff] [blame] | 682 | cc->total_free_scanned += nr_scanned; |
Mel Gorman | 397487d | 2012-10-19 12:00:10 +0100 | [diff] [blame] | 683 | if (total_isolated) |
Minchan Kim | 010fc29 | 2012-12-20 15:05:06 -0800 | [diff] [blame] | 684 | count_compact_events(COMPACTISOLATED, total_isolated); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 685 | return total_isolated; |
| 686 | } |
| 687 | |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 688 | /** |
| 689 | * isolate_freepages_range() - isolate free pages. |
Mike Rapoport | e8b098f | 2018-04-05 16:24:57 -0700 | [diff] [blame] | 690 | * @cc: Compaction control structure. |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 691 | * @start_pfn: The first PFN to start isolating. |
| 692 | * @end_pfn: The one-past-last PFN. |
| 693 | * |
| 694 | * Non-free pages, invalid PFNs, or zone boundaries within the |
| 695 | * [start_pfn, end_pfn) range are considered errors, cause function to |
| 696 | * undo its actions and return zero. |
| 697 | * |
| 698 | * Otherwise, function returns one-past-the-last PFN of isolated page |
| 699 | * (which may be greater then end_pfn if end fell in a middle of |
| 700 | * a free page). |
| 701 | */ |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 702 | unsigned long |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 703 | isolate_freepages_range(struct compact_control *cc, |
| 704 | unsigned long start_pfn, unsigned long end_pfn) |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 705 | { |
Joonsoo Kim | e1409c3 | 2016-03-15 14:57:48 -0700 | [diff] [blame] | 706 | unsigned long isolated, pfn, block_start_pfn, block_end_pfn; |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 707 | LIST_HEAD(freelist); |
| 708 | |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 709 | pfn = start_pfn; |
Vlastimil Babka | 06b6640 | 2016-05-19 17:11:48 -0700 | [diff] [blame] | 710 | block_start_pfn = pageblock_start_pfn(pfn); |
Joonsoo Kim | e1409c3 | 2016-03-15 14:57:48 -0700 | [diff] [blame] | 711 | if (block_start_pfn < cc->zone->zone_start_pfn) |
| 712 | block_start_pfn = cc->zone->zone_start_pfn; |
Vlastimil Babka | 06b6640 | 2016-05-19 17:11:48 -0700 | [diff] [blame] | 713 | block_end_pfn = pageblock_end_pfn(pfn); |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 714 | |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 715 | for (; pfn < end_pfn; pfn += isolated, |
Joonsoo Kim | e1409c3 | 2016-03-15 14:57:48 -0700 | [diff] [blame] | 716 | block_start_pfn = block_end_pfn, |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 717 | block_end_pfn += pageblock_nr_pages) { |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 718 | /* Protect pfn from changing by isolate_freepages_block */ |
| 719 | unsigned long isolate_start_pfn = pfn; |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 720 | |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 721 | block_end_pfn = min(block_end_pfn, end_pfn); |
| 722 | |
Joonsoo Kim | 5842001 | 2014-11-13 15:19:07 -0800 | [diff] [blame] | 723 | /* |
| 724 | * pfn could pass the block_end_pfn if isolated freepage |
| 725 | * is more than pageblock order. In this case, we adjust |
| 726 | * scanning range to right one. |
| 727 | */ |
| 728 | if (pfn >= block_end_pfn) { |
Vlastimil Babka | 06b6640 | 2016-05-19 17:11:48 -0700 | [diff] [blame] | 729 | block_start_pfn = pageblock_start_pfn(pfn); |
| 730 | block_end_pfn = pageblock_end_pfn(pfn); |
Joonsoo Kim | 5842001 | 2014-11-13 15:19:07 -0800 | [diff] [blame] | 731 | block_end_pfn = min(block_end_pfn, end_pfn); |
| 732 | } |
| 733 | |
Joonsoo Kim | e1409c3 | 2016-03-15 14:57:48 -0700 | [diff] [blame] | 734 | if (!pageblock_pfn_to_page(block_start_pfn, |
| 735 | block_end_pfn, cc->zone)) |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 736 | break; |
| 737 | |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 738 | isolated = isolate_freepages_block(cc, &isolate_start_pfn, |
Mel Gorman | 4fca973 | 2019-03-05 15:45:34 -0800 | [diff] [blame] | 739 | block_end_pfn, &freelist, 0, true); |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 740 | |
| 741 | /* |
| 742 | * In strict mode, isolate_freepages_block() returns 0 if |
| 743 | * there are any holes in the block (ie. invalid PFNs or |
| 744 | * non-free pages). |
| 745 | */ |
| 746 | if (!isolated) |
| 747 | break; |
| 748 | |
| 749 | /* |
| 750 | * If we managed to isolate pages, it is always (1 << n) * |
| 751 | * pageblock_nr_pages for some non-negative n. (Max order |
| 752 | * page may span two pageblocks). |
| 753 | */ |
| 754 | } |
| 755 | |
Joonsoo Kim | 66c6422 | 2016-07-26 15:23:40 -0700 | [diff] [blame] | 756 | /* __isolate_free_page() does not map the pages */ |
Mel Gorman | 4469ab9 | 2019-03-05 15:44:39 -0800 | [diff] [blame] | 757 | split_map_pages(&freelist); |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 758 | |
| 759 | if (pfn < end_pfn) { |
| 760 | /* Loop terminated early, cleanup. */ |
| 761 | release_freepages(&freelist); |
| 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | /* We don't use freelists for anything. */ |
| 766 | return pfn; |
| 767 | } |
| 768 | |
Carlos Llamas | 061e34c | 2021-04-23 19:20:54 +0000 | [diff] [blame] | 769 | #ifdef CONFIG_COMPACTION |
Charan Teja Reddy | f47b852 | 2021-02-16 13:59:45 +0530 | [diff] [blame] | 770 | unsigned long isolate_and_split_free_page(struct page *page, |
| 771 | struct list_head *list) |
| 772 | { |
| 773 | unsigned long isolated; |
| 774 | unsigned int order; |
| 775 | |
| 776 | if (!PageBuddy(page)) |
| 777 | return 0; |
| 778 | |
| 779 | order = buddy_order(page); |
| 780 | isolated = __isolate_free_page(page, order); |
| 781 | if (!isolated) |
| 782 | return 0; |
| 783 | |
| 784 | set_page_private(page, order); |
| 785 | list_add(&page->lru, list); |
| 786 | |
| 787 | split_map_pages(list); |
| 788 | |
| 789 | return isolated; |
| 790 | } |
| 791 | EXPORT_SYMBOL_GPL(isolate_and_split_free_page); |
Carlos Llamas | 061e34c | 2021-04-23 19:20:54 +0000 | [diff] [blame] | 792 | #endif |
Charan Teja Reddy | f47b852 | 2021-02-16 13:59:45 +0530 | [diff] [blame] | 793 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 794 | /* Similar to reclaim, but different enough that they don't share logic */ |
Andrey Ryabinin | 5f438ee | 2019-03-05 15:49:42 -0800 | [diff] [blame] | 795 | static bool too_many_isolated(pg_data_t *pgdat) |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 796 | { |
Minchan Kim | bc69304 | 2010-09-09 16:38:00 -0700 | [diff] [blame] | 797 | unsigned long active, inactive, isolated; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 798 | |
Andrey Ryabinin | 5f438ee | 2019-03-05 15:49:42 -0800 | [diff] [blame] | 799 | inactive = node_page_state(pgdat, NR_INACTIVE_FILE) + |
| 800 | node_page_state(pgdat, NR_INACTIVE_ANON); |
| 801 | active = node_page_state(pgdat, NR_ACTIVE_FILE) + |
| 802 | node_page_state(pgdat, NR_ACTIVE_ANON); |
| 803 | isolated = node_page_state(pgdat, NR_ISOLATED_FILE) + |
| 804 | node_page_state(pgdat, NR_ISOLATED_ANON); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 805 | |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 806 | #ifdef CONFIG_AMLOGIC_CMA |
| 807 | check_cma_isolated(&isolated, active, inactive); |
| 808 | #endif |
Minchan Kim | bc69304 | 2010-09-09 16:38:00 -0700 | [diff] [blame] | 809 | return isolated > (inactive + active) / 2; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 810 | } |
| 811 | |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 812 | /** |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 813 | * isolate_migratepages_block() - isolate all migrate-able pages within |
| 814 | * a single pageblock |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 815 | * @cc: Compaction control structure. |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 816 | * @low_pfn: The first PFN to isolate |
| 817 | * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock |
Hugh Dickins | c5eda60 | 2022-03-22 14:45:41 -0700 | [diff] [blame] | 818 | * @mode: Isolation mode to be used. |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 819 | * |
| 820 | * Isolate all pages that can be migrated from the range specified by |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 821 | * [low_pfn, end_pfn). The range is expected to be within same pageblock. |
Oscar Salvador | c2ad7a1 | 2021-05-04 18:35:17 -0700 | [diff] [blame] | 822 | * Returns errno, like -EAGAIN or -EINTR in case e.g signal pending or congestion, |
Oscar Salvador | 369fa22 | 2021-05-04 18:35:26 -0700 | [diff] [blame] | 823 | * -ENOMEM in case we could not allocate a page, or 0. |
Oscar Salvador | c2ad7a1 | 2021-05-04 18:35:17 -0700 | [diff] [blame] | 824 | * cc->migrate_pfn will contain the next pfn to scan. |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 825 | * |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 826 | * The pages are isolated on cc->migratepages list (not required to be empty), |
Oscar Salvador | c2ad7a1 | 2021-05-04 18:35:17 -0700 | [diff] [blame] | 827 | * and cc->nr_migratepages is updated accordingly. |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 828 | */ |
Oscar Salvador | c2ad7a1 | 2021-05-04 18:35:17 -0700 | [diff] [blame] | 829 | static int |
Minchan Kim | a934b923 | 2023-01-12 09:52:49 -0800 | [diff] [blame] | 830 | isolate_migratepages_block(struct compact_control_ext *cc_ext, unsigned long low_pfn, |
Hugh Dickins | c5eda60 | 2022-03-22 14:45:41 -0700 | [diff] [blame] | 831 | unsigned long end_pfn, isolate_mode_t mode) |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 832 | { |
Minchan Kim | a934b923 | 2023-01-12 09:52:49 -0800 | [diff] [blame] | 833 | struct compact_control *cc = cc_ext->cc; |
Andrey Ryabinin | 5f438ee | 2019-03-05 15:49:42 -0800 | [diff] [blame] | 834 | pg_data_t *pgdat = cc->zone->zone_pgdat; |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 835 | unsigned long nr_scanned = 0, nr_isolated = 0; |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 836 | struct lruvec *lruvec; |
Xiubo Li | b8b2d82 | 2014-10-09 15:28:21 -0700 | [diff] [blame] | 837 | unsigned long flags = 0; |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 838 | struct lruvec *locked = NULL; |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 839 | struct page *page = NULL, *valid_page = NULL; |
Hugh Dickins | c5eda60 | 2022-03-22 14:45:41 -0700 | [diff] [blame] | 840 | struct address_space *mapping; |
Joonsoo Kim | e34d85f | 2015-02-11 15:27:04 -0800 | [diff] [blame] | 841 | unsigned long start_pfn = low_pfn; |
Vlastimil Babka | fdd048e | 2016-05-19 17:11:55 -0700 | [diff] [blame] | 842 | bool skip_on_failure = false; |
| 843 | unsigned long next_skip_pfn = 0; |
Mel Gorman | e380beb | 2019-03-05 15:44:58 -0800 | [diff] [blame] | 844 | bool skip_updated = false; |
Oscar Salvador | c2ad7a1 | 2021-05-04 18:35:17 -0700 | [diff] [blame] | 845 | int ret = 0; |
| 846 | |
| 847 | cc->migrate_pfn = low_pfn; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 848 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 849 | /* |
| 850 | * Ensure that there are not too many pages isolated from the LRU |
| 851 | * list by either parallel reclaimers or compaction. If there are, |
| 852 | * delay for some time until fewer pages are isolated |
| 853 | */ |
Andrey Ryabinin | 5f438ee | 2019-03-05 15:49:42 -0800 | [diff] [blame] | 854 | while (unlikely(too_many_isolated(pgdat))) { |
Zi Yan | d20bdd57 | 2020-11-13 22:51:43 -0800 | [diff] [blame] | 855 | /* stop isolation if there are still pages not migrated */ |
| 856 | if (cc->nr_migratepages) |
Oscar Salvador | c2ad7a1 | 2021-05-04 18:35:17 -0700 | [diff] [blame] | 857 | return -EAGAIN; |
Zi Yan | d20bdd57 | 2020-11-13 22:51:43 -0800 | [diff] [blame] | 858 | |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 859 | /* async migration should just abort */ |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 860 | if (cc->mode == MIGRATE_ASYNC) |
Oscar Salvador | c2ad7a1 | 2021-05-04 18:35:17 -0700 | [diff] [blame] | 861 | return -EAGAIN; |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 862 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 863 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
| 864 | |
| 865 | if (fatal_signal_pending(current)) |
Oscar Salvador | c2ad7a1 | 2021-05-04 18:35:17 -0700 | [diff] [blame] | 866 | return -EINTR; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 867 | } |
| 868 | |
Mel Gorman | cf66f07 | 2019-03-05 15:45:24 -0800 | [diff] [blame] | 869 | cond_resched(); |
David Rientjes | aeef4b8 | 2014-06-04 16:08:31 -0700 | [diff] [blame] | 870 | |
Vlastimil Babka | fdd048e | 2016-05-19 17:11:55 -0700 | [diff] [blame] | 871 | if (cc->direct_compaction && (cc->mode == MIGRATE_ASYNC)) { |
| 872 | skip_on_failure = true; |
| 873 | next_skip_pfn = block_end_pfn(low_pfn, cc->order); |
| 874 | } |
| 875 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 876 | /* Time to isolate some pages for migration */ |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 877 | for (; low_pfn < end_pfn; low_pfn++) { |
Vlastimil Babka | 29c0dde | 2015-09-08 15:02:46 -0700 | [diff] [blame] | 878 | |
Vlastimil Babka | fdd048e | 2016-05-19 17:11:55 -0700 | [diff] [blame] | 879 | if (skip_on_failure && low_pfn >= next_skip_pfn) { |
| 880 | /* |
| 881 | * We have isolated all migration candidates in the |
| 882 | * previous order-aligned block, and did not skip it due |
| 883 | * to failure. We should migrate the pages now and |
| 884 | * hopefully succeed compaction. |
| 885 | */ |
| 886 | if (nr_isolated) |
| 887 | break; |
| 888 | |
| 889 | /* |
| 890 | * We failed to isolate in the previous order-aligned |
| 891 | * block. Set the new boundary to the end of the |
| 892 | * current block. Note we can't simply increase |
| 893 | * next_skip_pfn by 1 << order, as low_pfn might have |
| 894 | * been incremented by a higher number due to skipping |
| 895 | * a compound or a high-order buddy page in the |
| 896 | * previous loop iteration. |
| 897 | */ |
| 898 | next_skip_pfn = block_end_pfn(low_pfn, cc->order); |
| 899 | } |
| 900 | |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 901 | /* |
| 902 | * Periodically drop the lock (if held) regardless of its |
Mel Gorman | 670105a | 2019-08-02 21:48:51 -0700 | [diff] [blame] | 903 | * contention, to give chance to IRQs. Abort completely if |
| 904 | * a fatal signal is pending. |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 905 | */ |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 906 | if (!(low_pfn % SWAP_CLUSTER_MAX)) { |
| 907 | if (locked) { |
| 908 | unlock_page_lruvec_irqrestore(locked, flags); |
| 909 | locked = NULL; |
| 910 | } |
| 911 | |
| 912 | if (fatal_signal_pending(current)) { |
| 913 | cc->contended = true; |
Oscar Salvador | c2ad7a1 | 2021-05-04 18:35:17 -0700 | [diff] [blame] | 914 | ret = -EINTR; |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 915 | |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 916 | #ifdef CONFIG_AMLOGIC_CMA |
| 917 | if (cc->alloc_contig) |
| 918 | cma_debug(1, page, "abort by sig, low_pfn:%lx, swap:%ld\n", |
| 919 | low_pfn, SWAP_CLUSTER_MAX); |
| 920 | #endif |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 921 | goto fatal_pending; |
| 922 | } |
| 923 | |
| 924 | cond_resched(); |
Mel Gorman | 670105a | 2019-08-02 21:48:51 -0700 | [diff] [blame] | 925 | } |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 926 | |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 927 | nr_scanned++; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 928 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 929 | page = pfn_to_page(low_pfn); |
Mel Gorman | dc90860 | 2012-02-08 17:13:38 -0800 | [diff] [blame] | 930 | |
Mel Gorman | e380beb | 2019-03-05 15:44:58 -0800 | [diff] [blame] | 931 | /* |
| 932 | * Check if the pageblock has already been marked skipped. |
| 933 | * Only the aligned PFN is checked as the caller isolates |
| 934 | * COMPACT_CLUSTER_MAX at a time so the second call must |
| 935 | * not falsely conclude that the block should be skipped. |
| 936 | */ |
| 937 | if (!valid_page && IS_ALIGNED(low_pfn, pageblock_nr_pages)) { |
| 938 | if (!cc->ignore_skip_hint && get_pageblock_skip(page)) { |
| 939 | low_pfn = end_pfn; |
Alex Shi | 9df4131 | 2020-12-15 12:34:20 -0800 | [diff] [blame] | 940 | page = NULL; |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 941 | #ifdef CONFIG_AMLOGIC_CMA |
| 942 | if (cc->alloc_contig) |
| 943 | cma_debug(1, page, "abort by skip, low_pfn:%lx\n", |
| 944 | low_pfn); |
| 945 | #endif |
Mel Gorman | e380beb | 2019-03-05 15:44:58 -0800 | [diff] [blame] | 946 | goto isolate_abort; |
| 947 | } |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 948 | valid_page = page; |
Mel Gorman | e380beb | 2019-03-05 15:44:58 -0800 | [diff] [blame] | 949 | } |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 950 | |
Oscar Salvador | 369fa22 | 2021-05-04 18:35:26 -0700 | [diff] [blame] | 951 | if (PageHuge(page) && cc->alloc_contig) { |
Oscar Salvador | ae37c7f | 2021-05-04 18:35:29 -0700 | [diff] [blame] | 952 | ret = isolate_or_dissolve_huge_page(page, &cc->migratepages); |
Oscar Salvador | 369fa22 | 2021-05-04 18:35:26 -0700 | [diff] [blame] | 953 | |
| 954 | /* |
| 955 | * Fail isolation in case isolate_or_dissolve_huge_page() |
| 956 | * reports an error. In case of -ENOMEM, abort right away. |
| 957 | */ |
| 958 | if (ret < 0) { |
| 959 | /* Do not report -EBUSY down the chain */ |
| 960 | if (ret == -EBUSY) |
| 961 | ret = 0; |
| 962 | low_pfn += (1UL << compound_order(page)) - 1; |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 963 | #ifdef CONFIG_AMLOGIC_CMA |
| 964 | if (cc->alloc_contig) |
| 965 | cma_debug(1, page, "abort by huge, low_pfn:%lx\n", |
| 966 | low_pfn); |
| 967 | #endif |
Oscar Salvador | 369fa22 | 2021-05-04 18:35:26 -0700 | [diff] [blame] | 968 | goto isolate_fail; |
| 969 | } |
| 970 | |
Oscar Salvador | ae37c7f | 2021-05-04 18:35:29 -0700 | [diff] [blame] | 971 | if (PageHuge(page)) { |
| 972 | /* |
| 973 | * Hugepage was successfully isolated and placed |
| 974 | * on the cc->migratepages list. |
| 975 | */ |
| 976 | low_pfn += compound_nr(page) - 1; |
| 977 | goto isolate_success_no_list; |
| 978 | } |
| 979 | |
Oscar Salvador | 369fa22 | 2021-05-04 18:35:26 -0700 | [diff] [blame] | 980 | /* |
| 981 | * Ok, the hugepage was dissolved. Now these pages are |
| 982 | * Buddy and cannot be re-allocated because they are |
| 983 | * isolated. Fall-through as the check below handles |
| 984 | * Buddy pages. |
| 985 | */ |
| 986 | } |
| 987 | |
Mel Gorman | 6c14466 | 2014-01-23 15:53:38 -0800 | [diff] [blame] | 988 | /* |
Vlastimil Babka | 99c0fd5 | 2014-10-09 15:27:23 -0700 | [diff] [blame] | 989 | * Skip if free. We read page order here without zone lock |
| 990 | * which is generally unsafe, but the race window is small and |
| 991 | * the worst thing that can happen is that we skip some |
| 992 | * potential isolation targets. |
Mel Gorman | 6c14466 | 2014-01-23 15:53:38 -0800 | [diff] [blame] | 993 | */ |
Vlastimil Babka | 99c0fd5 | 2014-10-09 15:27:23 -0700 | [diff] [blame] | 994 | if (PageBuddy(page)) { |
Matthew Wilcox (Oracle) | ab130f91 | 2020-10-15 20:10:15 -0700 | [diff] [blame] | 995 | unsigned long freepage_order = buddy_order_unsafe(page); |
Vlastimil Babka | 99c0fd5 | 2014-10-09 15:27:23 -0700 | [diff] [blame] | 996 | |
| 997 | /* |
| 998 | * Without lock, we cannot be sure that what we got is |
| 999 | * a valid page order. Consider only values in the |
| 1000 | * valid order range to prevent low_pfn overflow. |
| 1001 | */ |
| 1002 | if (freepage_order > 0 && freepage_order < MAX_ORDER) |
| 1003 | low_pfn += (1UL << freepage_order) - 1; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1004 | continue; |
Vlastimil Babka | 99c0fd5 | 2014-10-09 15:27:23 -0700 | [diff] [blame] | 1005 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1006 | |
Mel Gorman | 9927af74 | 2011-01-13 15:45:59 -0800 | [diff] [blame] | 1007 | /* |
Vlastimil Babka | 29c0dde | 2015-09-08 15:02:46 -0700 | [diff] [blame] | 1008 | * Regardless of being on LRU, compound pages such as THP and |
Rik van Riel | 1da2f32 | 2020-04-01 21:10:31 -0700 | [diff] [blame] | 1009 | * hugetlbfs are not to be compacted unless we are attempting |
| 1010 | * an allocation much larger than the huge page size (eg CMA). |
| 1011 | * We can potentially save a lot of iterations if we skip them |
| 1012 | * at once. The check is racy, but we can consider only valid |
| 1013 | * values and the only danger is skipping too much. |
Andrea Arcangeli | bc83501 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 1014 | */ |
Rik van Riel | 1da2f32 | 2020-04-01 21:10:31 -0700 | [diff] [blame] | 1015 | if (PageCompound(page) && !cc->alloc_contig) { |
David Rientjes | 21dc7e0 | 2017-11-17 15:26:30 -0800 | [diff] [blame] | 1016 | const unsigned int order = compound_order(page); |
Vlastimil Babka | 29c0dde | 2015-09-08 15:02:46 -0700 | [diff] [blame] | 1017 | |
Vlastimil Babka | d3c85ba | 2017-11-17 15:26:41 -0800 | [diff] [blame] | 1018 | if (likely(order < MAX_ORDER)) |
David Rientjes | 21dc7e0 | 2017-11-17 15:26:30 -0800 | [diff] [blame] | 1019 | low_pfn += (1UL << order) - 1; |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 1020 | #ifdef CONFIG_AMLOGIC_CMA |
| 1021 | if (cc->alloc_contig) |
| 1022 | cma_debug(1, page, "abort by compound, low_pfn:%lx\n", |
| 1023 | low_pfn); |
| 1024 | #endif |
Vlastimil Babka | fdd048e | 2016-05-19 17:11:55 -0700 | [diff] [blame] | 1025 | goto isolate_fail; |
Mel Gorman | 2a1402a | 2012-10-08 16:32:33 -0700 | [diff] [blame] | 1026 | } |
| 1027 | |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 1028 | /* |
| 1029 | * Check may be lockless but that's ok as we recheck later. |
| 1030 | * It's possible to migrate LRU and non-lru movable pages. |
| 1031 | * Skip any other type of page |
| 1032 | */ |
| 1033 | if (!PageLRU(page)) { |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 1034 | /* |
| 1035 | * __PageMovable can return false positive so we need |
| 1036 | * to verify it under page_lock. |
| 1037 | */ |
| 1038 | if (unlikely(__PageMovable(page)) && |
| 1039 | !PageIsolated(page)) { |
| 1040 | if (locked) { |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 1041 | unlock_page_lruvec_irqrestore(locked, flags); |
| 1042 | locked = NULL; |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
Hugh Dickins | c5eda60 | 2022-03-22 14:45:41 -0700 | [diff] [blame] | 1045 | if (!isolate_movable_page(page, mode)) |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 1046 | goto isolate_success; |
| 1047 | } |
| 1048 | |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 1049 | #ifdef CONFIG_AMLOGIC_CMA |
| 1050 | if (cc->alloc_contig && page_count(page)) |
| 1051 | cma_debug(1, page, "abort by LRU, low_pfn:%lx\n", |
| 1052 | low_pfn); |
| 1053 | #endif |
Vlastimil Babka | fdd048e | 2016-05-19 17:11:55 -0700 | [diff] [blame] | 1054 | goto isolate_fail; |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 1055 | } |
Vlastimil Babka | 29c0dde | 2015-09-08 15:02:46 -0700 | [diff] [blame] | 1056 | |
David Rientjes | 119d6d5 | 2014-04-03 14:48:00 -0700 | [diff] [blame] | 1057 | /* |
Alex Shi | 9df4131 | 2020-12-15 12:34:20 -0800 | [diff] [blame] | 1058 | * Be careful not to clear PageLRU until after we're |
| 1059 | * sure the page is not being freed elsewhere -- the |
| 1060 | * page release code relies on it. |
| 1061 | */ |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 1062 | #ifdef CONFIG_AMLOGIC_CMA |
| 1063 | if (unlikely(!get_page_unless_zero(page))) { |
| 1064 | if (cc->alloc_contig) |
| 1065 | cma_debug(1, page, "none zero ref, low_pfn:%lx\n", |
| 1066 | low_pfn); |
| 1067 | goto isolate_fail; |
| 1068 | } |
| 1069 | #else |
Alex Shi | 9df4131 | 2020-12-15 12:34:20 -0800 | [diff] [blame] | 1070 | if (unlikely(!get_page_unless_zero(page))) |
| 1071 | goto isolate_fail; |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 1072 | #endif |
Alex Shi | 9df4131 | 2020-12-15 12:34:20 -0800 | [diff] [blame] | 1073 | |
Gavin Shan | b951ab4 | 2022-11-24 17:55:23 +0800 | [diff] [blame] | 1074 | /* |
| 1075 | * Migration will fail if an anonymous page is pinned in memory, |
| 1076 | * so avoid taking lru_lock and isolating it unnecessarily in an |
| 1077 | * admittedly racy check. |
| 1078 | */ |
| 1079 | mapping = page_mapping(page); |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 1080 | #ifdef CONFIG_AMLOGIC_CMA |
| 1081 | if (!mapping && (page_count(page) - 1) > total_mapcount(page)) { |
| 1082 | if (cc->alloc_contig) |
| 1083 | cma_debug(1, page, "mc/rc miss match, low_pfn:%lx\n", |
| 1084 | low_pfn); |
| 1085 | goto isolate_fail_put; |
| 1086 | } |
| 1087 | check_page_to_cma(cc, mapping, page); |
| 1088 | #else |
Gavin Shan | b951ab4 | 2022-11-24 17:55:23 +0800 | [diff] [blame] | 1089 | if (!mapping && (page_count(page) - 1) > total_mapcount(page)) |
Alex Shi | 9df4131 | 2020-12-15 12:34:20 -0800 | [diff] [blame] | 1090 | goto isolate_fail_put; |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 1091 | #endif |
Alex Shi | 9df4131 | 2020-12-15 12:34:20 -0800 | [diff] [blame] | 1092 | |
Gavin Shan | b951ab4 | 2022-11-24 17:55:23 +0800 | [diff] [blame] | 1093 | /* |
| 1094 | * Only allow to migrate anonymous pages in GFP_NOFS context |
| 1095 | * because those do not depend on fs locks. |
| 1096 | */ |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 1097 | #ifdef CONFIG_AMLOGIC_CMA |
| 1098 | if (!(cc->gfp_mask & __GFP_FS) && mapping) { |
| 1099 | if (cc->alloc_contig) |
| 1100 | cma_debug(1, page, "no fs ctx, low_pfn:%lx\n", |
| 1101 | low_pfn); |
| 1102 | goto isolate_fail_put; |
| 1103 | } |
| 1104 | #else |
Gavin Shan | b951ab4 | 2022-11-24 17:55:23 +0800 | [diff] [blame] | 1105 | if (!(cc->gfp_mask & __GFP_FS) && mapping) |
| 1106 | goto isolate_fail_put; |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 1107 | #endif |
Gavin Shan | b951ab4 | 2022-11-24 17:55:23 +0800 | [diff] [blame] | 1108 | |
Hugh Dickins | c5eda60 | 2022-03-22 14:45:41 -0700 | [diff] [blame] | 1109 | /* Only take pages on LRU: a check now makes later tests safe */ |
| 1110 | if (!PageLRU(page)) |
Alex Shi | 9df4131 | 2020-12-15 12:34:20 -0800 | [diff] [blame] | 1111 | goto isolate_fail_put; |
| 1112 | |
Hugh Dickins | c5eda60 | 2022-03-22 14:45:41 -0700 | [diff] [blame] | 1113 | /* Compaction might skip unevictable pages but CMA takes them */ |
| 1114 | if (!(mode & ISOLATE_UNEVICTABLE) && PageUnevictable(page)) |
| 1115 | goto isolate_fail_put; |
| 1116 | |
| 1117 | /* |
| 1118 | * To minimise LRU disruption, the caller can indicate with |
| 1119 | * ISOLATE_ASYNC_MIGRATE that it only wants to isolate pages |
| 1120 | * it will be able to migrate without blocking - clean pages |
| 1121 | * for the most part. PageWriteback would require blocking. |
| 1122 | */ |
| 1123 | if ((mode & ISOLATE_ASYNC_MIGRATE) && PageWriteback(page)) |
| 1124 | goto isolate_fail_put; |
| 1125 | |
| 1126 | if ((mode & ISOLATE_ASYNC_MIGRATE) && PageDirty(page)) { |
| 1127 | bool migrate_dirty; |
| 1128 | |
| 1129 | /* |
| 1130 | * Only pages without mappings or that have a |
| 1131 | * ->migratepage callback are possible to migrate |
| 1132 | * without blocking. However, we can be racing with |
| 1133 | * truncation so it's necessary to lock the page |
| 1134 | * to stabilise the mapping as truncation holds |
| 1135 | * the page lock until after the page is removed |
| 1136 | * from the page cache. |
| 1137 | */ |
| 1138 | if (!trylock_page(page)) |
| 1139 | goto isolate_fail_put; |
| 1140 | |
| 1141 | mapping = page_mapping(page); |
| 1142 | migrate_dirty = !mapping || mapping->a_ops->migratepage; |
| 1143 | unlock_page(page); |
| 1144 | if (!migrate_dirty) |
| 1145 | goto isolate_fail_put; |
| 1146 | } |
| 1147 | |
Alex Shi | 9df4131 | 2020-12-15 12:34:20 -0800 | [diff] [blame] | 1148 | /* Try isolate the page */ |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 1149 | #ifdef CONFIG_AMLOGIC_CMA |
| 1150 | if (!TestClearPageLRU(page)) { |
| 1151 | if (cc->alloc_contig) |
| 1152 | cma_debug(1, page, "clear lru fail, low_pfn:%lx, mode:%x\n", |
| 1153 | low_pfn, mode); |
| 1154 | goto isolate_fail_put; |
| 1155 | } |
| 1156 | #else |
Alex Shi | 9df4131 | 2020-12-15 12:34:20 -0800 | [diff] [blame] | 1157 | if (!TestClearPageLRU(page)) |
| 1158 | goto isolate_fail_put; |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 1159 | #endif |
Alex Shi | 9df4131 | 2020-12-15 12:34:20 -0800 | [diff] [blame] | 1160 | |
Muchun Song | a984226 | 2021-06-28 19:37:53 -0700 | [diff] [blame] | 1161 | lruvec = mem_cgroup_page_lruvec(page); |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 1162 | |
Vlastimil Babka | 69b7189 | 2014-10-09 15:27:18 -0700 | [diff] [blame] | 1163 | /* If we already hold the lock, we can skip some rechecking */ |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 1164 | if (lruvec != locked) { |
| 1165 | if (locked) |
| 1166 | unlock_page_lruvec_irqrestore(locked, flags); |
| 1167 | |
| 1168 | compact_lock_irqsave(&lruvec->lru_lock, &flags, cc); |
| 1169 | locked = lruvec; |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 1170 | |
| 1171 | lruvec_memcg_debug(lruvec, page); |
Mel Gorman | e380beb | 2019-03-05 15:44:58 -0800 | [diff] [blame] | 1172 | |
Mel Gorman | e380beb | 2019-03-05 15:44:58 -0800 | [diff] [blame] | 1173 | /* Try get exclusive access under lock */ |
| 1174 | if (!skip_updated) { |
| 1175 | skip_updated = true; |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 1176 | #ifdef CONFIG_AMLOGIC_CMA |
| 1177 | if (test_and_set_skip(cc, page, low_pfn)) { |
| 1178 | if (cc->alloc_contig) |
| 1179 | cma_debug(1, page, "skip fail, low_pfn:%lx, mode:%x\n", |
| 1180 | low_pfn, mode); |
| 1181 | goto isolate_abort; |
| 1182 | } |
| 1183 | #else |
Mel Gorman | e380beb | 2019-03-05 15:44:58 -0800 | [diff] [blame] | 1184 | if (test_and_set_skip(cc, page, low_pfn)) |
| 1185 | goto isolate_abort; |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 1186 | #endif |
Mel Gorman | e380beb | 2019-03-05 15:44:58 -0800 | [diff] [blame] | 1187 | } |
Mel Gorman | 2a1402a | 2012-10-08 16:32:33 -0700 | [diff] [blame] | 1188 | |
Vlastimil Babka | 29c0dde | 2015-09-08 15:02:46 -0700 | [diff] [blame] | 1189 | /* |
| 1190 | * Page become compound since the non-locked check, |
| 1191 | * and it's on LRU. It can only be a THP so the order |
| 1192 | * is safe to read and it's 0 for tail pages. |
| 1193 | */ |
Rik van Riel | 1da2f32 | 2020-04-01 21:10:31 -0700 | [diff] [blame] | 1194 | if (unlikely(PageCompound(page) && !cc->alloc_contig)) { |
Matthew Wilcox (Oracle) | d8c6546 | 2019-09-23 15:34:30 -0700 | [diff] [blame] | 1195 | low_pfn += compound_nr(page) - 1; |
Alex Shi | 9df4131 | 2020-12-15 12:34:20 -0800 | [diff] [blame] | 1196 | SetPageLRU(page); |
| 1197 | goto isolate_fail_put; |
Vlastimil Babka | 69b7189 | 2014-10-09 15:27:18 -0700 | [diff] [blame] | 1198 | } |
Alex Shi | d99fd5f | 2021-02-24 12:09:25 -0800 | [diff] [blame] | 1199 | } |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1200 | |
Rik van Riel | 1da2f32 | 2020-04-01 21:10:31 -0700 | [diff] [blame] | 1201 | /* The whole page is taken off the LRU; skip the tail pages. */ |
| 1202 | if (PageCompound(page)) |
| 1203 | low_pfn += compound_nr(page) - 1; |
Andrea Arcangeli | bc83501 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 1204 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1205 | /* Successfully isolated */ |
Yu Zhao | 46ae6b2 | 2021-02-24 12:08:25 -0800 | [diff] [blame] | 1206 | del_page_from_lru_list(page, lruvec); |
Rik van Riel | 1da2f32 | 2020-04-01 21:10:31 -0700 | [diff] [blame] | 1207 | mod_node_page_state(page_pgdat(page), |
Huang Ying | 9de4f22 | 2020-04-06 20:04:41 -0700 | [diff] [blame] | 1208 | NR_ISOLATED_ANON + page_is_file_lru(page), |
Matthew Wilcox (Oracle) | 6c35784 | 2020-08-14 17:30:37 -0700 | [diff] [blame] | 1209 | thp_nr_pages(page)); |
Joonsoo Kim | b6c7501 | 2014-04-07 15:37:07 -0700 | [diff] [blame] | 1210 | |
| 1211 | isolate_success: |
Vlastimil Babka | fdd048e | 2016-05-19 17:11:55 -0700 | [diff] [blame] | 1212 | list_add(&page->lru, &cc->migratepages); |
Oscar Salvador | ae37c7f | 2021-05-04 18:35:29 -0700 | [diff] [blame] | 1213 | isolate_success_no_list: |
Zi Yan | 3893586 | 2020-11-13 22:51:40 -0800 | [diff] [blame] | 1214 | cc->nr_migratepages += compound_nr(page); |
Minchan Kim | a934b923 | 2023-01-12 09:52:49 -0800 | [diff] [blame] | 1215 | if (!PageAnon(page)) |
| 1216 | cc_ext->nr_migrate_file_pages += compound_nr(page); |
Zi Yan | 3893586 | 2020-11-13 22:51:40 -0800 | [diff] [blame] | 1217 | nr_isolated += compound_nr(page); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1218 | |
Mel Gorman | 804d312 | 2019-03-05 15:45:07 -0800 | [diff] [blame] | 1219 | /* |
| 1220 | * Avoid isolating too much unless this block is being |
Mel Gorman | cb2dcaf | 2019-03-05 15:45:11 -0800 | [diff] [blame] | 1221 | * rescanned (e.g. dirty/writeback pages, parallel allocation) |
| 1222 | * or a lock is contended. For contention, isolate quickly to |
| 1223 | * potentially remove one source of contention. |
Mel Gorman | 804d312 | 2019-03-05 15:45:07 -0800 | [diff] [blame] | 1224 | */ |
Zi Yan | 3893586 | 2020-11-13 22:51:40 -0800 | [diff] [blame] | 1225 | if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX && |
Mel Gorman | cb2dcaf | 2019-03-05 15:45:11 -0800 | [diff] [blame] | 1226 | !cc->rescan && !cc->contended) { |
Hillf Danton | 31b8384 | 2012-01-10 15:07:59 -0800 | [diff] [blame] | 1227 | ++low_pfn; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1228 | break; |
Hillf Danton | 31b8384 | 2012-01-10 15:07:59 -0800 | [diff] [blame] | 1229 | } |
Vlastimil Babka | fdd048e | 2016-05-19 17:11:55 -0700 | [diff] [blame] | 1230 | |
| 1231 | continue; |
Alex Shi | 9df4131 | 2020-12-15 12:34:20 -0800 | [diff] [blame] | 1232 | |
| 1233 | isolate_fail_put: |
| 1234 | /* Avoid potential deadlock in freeing page under lru_lock */ |
| 1235 | if (locked) { |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 1236 | unlock_page_lruvec_irqrestore(locked, flags); |
| 1237 | locked = NULL; |
Alex Shi | 9df4131 | 2020-12-15 12:34:20 -0800 | [diff] [blame] | 1238 | } |
| 1239 | put_page(page); |
| 1240 | |
Vlastimil Babka | fdd048e | 2016-05-19 17:11:55 -0700 | [diff] [blame] | 1241 | isolate_fail: |
Oscar Salvador | 369fa22 | 2021-05-04 18:35:26 -0700 | [diff] [blame] | 1242 | if (!skip_on_failure && ret != -ENOMEM) |
Vlastimil Babka | fdd048e | 2016-05-19 17:11:55 -0700 | [diff] [blame] | 1243 | continue; |
| 1244 | |
| 1245 | /* |
| 1246 | * We have isolated some pages, but then failed. Release them |
| 1247 | * instead of migrating, as we cannot form the cc->order buddy |
| 1248 | * page anyway. |
| 1249 | */ |
| 1250 | if (nr_isolated) { |
| 1251 | if (locked) { |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 1252 | unlock_page_lruvec_irqrestore(locked, flags); |
| 1253 | locked = NULL; |
Vlastimil Babka | fdd048e | 2016-05-19 17:11:55 -0700 | [diff] [blame] | 1254 | } |
Vlastimil Babka | fdd048e | 2016-05-19 17:11:55 -0700 | [diff] [blame] | 1255 | putback_movable_pages(&cc->migratepages); |
| 1256 | cc->nr_migratepages = 0; |
Minchan Kim | a934b923 | 2023-01-12 09:52:49 -0800 | [diff] [blame] | 1257 | cc_ext->nr_migrate_file_pages = 0; |
Vlastimil Babka | fdd048e | 2016-05-19 17:11:55 -0700 | [diff] [blame] | 1258 | nr_isolated = 0; |
| 1259 | } |
| 1260 | |
| 1261 | if (low_pfn < next_skip_pfn) { |
| 1262 | low_pfn = next_skip_pfn - 1; |
| 1263 | /* |
| 1264 | * The check near the loop beginning would have updated |
| 1265 | * next_skip_pfn too, but this is a bit simpler. |
| 1266 | */ |
| 1267 | next_skip_pfn += 1UL << cc->order; |
| 1268 | } |
Oscar Salvador | 369fa22 | 2021-05-04 18:35:26 -0700 | [diff] [blame] | 1269 | |
| 1270 | if (ret == -ENOMEM) |
| 1271 | break; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1272 | } |
| 1273 | |
Vlastimil Babka | 99c0fd5 | 2014-10-09 15:27:23 -0700 | [diff] [blame] | 1274 | /* |
| 1275 | * The PageBuddy() check could have potentially brought us outside |
| 1276 | * the range to be scanned. |
| 1277 | */ |
| 1278 | if (unlikely(low_pfn > end_pfn)) |
| 1279 | low_pfn = end_pfn; |
| 1280 | |
Alex Shi | 9df4131 | 2020-12-15 12:34:20 -0800 | [diff] [blame] | 1281 | page = NULL; |
| 1282 | |
Mel Gorman | e380beb | 2019-03-05 15:44:58 -0800 | [diff] [blame] | 1283 | isolate_abort: |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 1284 | if (locked) |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 1285 | unlock_page_lruvec_irqrestore(locked, flags); |
Alex Shi | 9df4131 | 2020-12-15 12:34:20 -0800 | [diff] [blame] | 1286 | if (page) { |
| 1287 | SetPageLRU(page); |
| 1288 | put_page(page); |
| 1289 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1290 | |
Vlastimil Babka | 50b5b09 | 2014-01-21 15:51:10 -0800 | [diff] [blame] | 1291 | /* |
Mel Gorman | 804d312 | 2019-03-05 15:45:07 -0800 | [diff] [blame] | 1292 | * Updated the cached scanner pfn once the pageblock has been scanned |
| 1293 | * Pages will either be migrated in which case there is no point |
| 1294 | * scanning in the near future or migration failed in which case the |
| 1295 | * failure reason may persist. The block is marked for skipping if |
| 1296 | * there were no pages isolated in the block or if the block is |
| 1297 | * rescanned twice in a row. |
Vlastimil Babka | 50b5b09 | 2014-01-21 15:51:10 -0800 | [diff] [blame] | 1298 | */ |
Mel Gorman | 804d312 | 2019-03-05 15:45:07 -0800 | [diff] [blame] | 1299 | if (low_pfn == end_pfn && (!nr_isolated || cc->rescan)) { |
Mel Gorman | e380beb | 2019-03-05 15:44:58 -0800 | [diff] [blame] | 1300 | if (valid_page && !skip_updated) |
| 1301 | set_pageblock_skip(valid_page); |
| 1302 | update_cached_migrate(cc, low_pfn); |
| 1303 | } |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 1304 | |
Joonsoo Kim | e34d85f | 2015-02-11 15:27:04 -0800 | [diff] [blame] | 1305 | trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn, |
| 1306 | nr_scanned, nr_isolated); |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 1307 | |
Mel Gorman | 670105a | 2019-08-02 21:48:51 -0700 | [diff] [blame] | 1308 | fatal_pending: |
David Rientjes | 7f354a5 | 2017-02-22 15:44:50 -0800 | [diff] [blame] | 1309 | cc->total_migrate_scanned += nr_scanned; |
Mel Gorman | 397487d | 2012-10-19 12:00:10 +0100 | [diff] [blame] | 1310 | if (nr_isolated) |
Minchan Kim | 010fc29 | 2012-12-20 15:05:06 -0800 | [diff] [blame] | 1311 | count_compact_events(COMPACTISOLATED, nr_isolated); |
Mel Gorman | 397487d | 2012-10-19 12:00:10 +0100 | [diff] [blame] | 1312 | |
Oscar Salvador | c2ad7a1 | 2021-05-04 18:35:17 -0700 | [diff] [blame] | 1313 | cc->migrate_pfn = low_pfn; |
| 1314 | |
| 1315 | return ret; |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 1316 | } |
| 1317 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1318 | /** |
| 1319 | * isolate_migratepages_range() - isolate migrate-able pages in a PFN range |
| 1320 | * @cc: Compaction control structure. |
| 1321 | * @start_pfn: The first PFN to start isolating. |
| 1322 | * @end_pfn: The one-past-last PFN. |
| 1323 | * |
Oscar Salvador | 369fa22 | 2021-05-04 18:35:26 -0700 | [diff] [blame] | 1324 | * Returns -EAGAIN when contented, -EINTR in case of a signal pending, -ENOMEM |
| 1325 | * in case we could not allocate a page, or 0. |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1326 | */ |
Oscar Salvador | c2ad7a1 | 2021-05-04 18:35:17 -0700 | [diff] [blame] | 1327 | int |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1328 | isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn, |
| 1329 | unsigned long end_pfn) |
| 1330 | { |
Minchan Kim | a934b923 | 2023-01-12 09:52:49 -0800 | [diff] [blame] | 1331 | struct compact_control_ext cc_ext = { .cc = cc }; |
Joonsoo Kim | e1409c3 | 2016-03-15 14:57:48 -0700 | [diff] [blame] | 1332 | unsigned long pfn, block_start_pfn, block_end_pfn; |
Oscar Salvador | c2ad7a1 | 2021-05-04 18:35:17 -0700 | [diff] [blame] | 1333 | int ret = 0; |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1334 | |
| 1335 | /* Scan block by block. First and last block may be incomplete */ |
| 1336 | pfn = start_pfn; |
Vlastimil Babka | 06b6640 | 2016-05-19 17:11:48 -0700 | [diff] [blame] | 1337 | block_start_pfn = pageblock_start_pfn(pfn); |
Joonsoo Kim | e1409c3 | 2016-03-15 14:57:48 -0700 | [diff] [blame] | 1338 | if (block_start_pfn < cc->zone->zone_start_pfn) |
| 1339 | block_start_pfn = cc->zone->zone_start_pfn; |
Vlastimil Babka | 06b6640 | 2016-05-19 17:11:48 -0700 | [diff] [blame] | 1340 | block_end_pfn = pageblock_end_pfn(pfn); |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1341 | |
| 1342 | for (; pfn < end_pfn; pfn = block_end_pfn, |
Joonsoo Kim | e1409c3 | 2016-03-15 14:57:48 -0700 | [diff] [blame] | 1343 | block_start_pfn = block_end_pfn, |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1344 | block_end_pfn += pageblock_nr_pages) { |
| 1345 | |
| 1346 | block_end_pfn = min(block_end_pfn, end_pfn); |
| 1347 | |
Joonsoo Kim | e1409c3 | 2016-03-15 14:57:48 -0700 | [diff] [blame] | 1348 | if (!pageblock_pfn_to_page(block_start_pfn, |
| 1349 | block_end_pfn, cc->zone)) |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1350 | continue; |
| 1351 | |
Suren Baghdasaryan | 3fd32dc17 | 2024-03-14 15:53:20 -0700 | [diff] [blame] | 1352 | ret = isolate_migratepages_block(&cc_ext, pfn, block_end_pfn, |
Oscar Salvador | c2ad7a1 | 2021-05-04 18:35:17 -0700 | [diff] [blame] | 1353 | ISOLATE_UNEVICTABLE); |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1354 | |
Oscar Salvador | c2ad7a1 | 2021-05-04 18:35:17 -0700 | [diff] [blame] | 1355 | if (ret) |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1356 | break; |
Joonsoo Kim | 6ea41c0 | 2014-10-29 14:50:20 -0700 | [diff] [blame] | 1357 | |
Zi Yan | 3893586 | 2020-11-13 22:51:40 -0800 | [diff] [blame] | 1358 | if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX) |
Joonsoo Kim | 6ea41c0 | 2014-10-29 14:50:20 -0700 | [diff] [blame] | 1359 | break; |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1360 | } |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1361 | |
Oscar Salvador | c2ad7a1 | 2021-05-04 18:35:17 -0700 | [diff] [blame] | 1362 | return ret; |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1365 | #endif /* CONFIG_COMPACTION || CONFIG_CMA */ |
| 1366 | #ifdef CONFIG_COMPACTION |
Andrew Morton | 018e9a4 | 2015-04-15 16:15:20 -0700 | [diff] [blame] | 1367 | |
Vlastimil Babka | b682deb | 2017-05-08 15:54:43 -0700 | [diff] [blame] | 1368 | static bool suitable_migration_source(struct compact_control *cc, |
| 1369 | struct page *page) |
| 1370 | { |
Vlastimil Babka | 282722b | 2017-05-08 15:54:49 -0700 | [diff] [blame] | 1371 | int block_mt; |
| 1372 | |
Mel Gorman | 9bebefd | 2019-03-05 15:45:14 -0800 | [diff] [blame] | 1373 | if (pageblock_skip_persistent(page)) |
| 1374 | return false; |
| 1375 | |
Vlastimil Babka | 282722b | 2017-05-08 15:54:49 -0700 | [diff] [blame] | 1376 | if ((cc->mode != MIGRATE_ASYNC) || !cc->direct_compaction) |
Vlastimil Babka | b682deb | 2017-05-08 15:54:43 -0700 | [diff] [blame] | 1377 | return true; |
| 1378 | |
Vlastimil Babka | 282722b | 2017-05-08 15:54:49 -0700 | [diff] [blame] | 1379 | block_mt = get_pageblock_migratetype(page); |
| 1380 | |
| 1381 | if (cc->migratetype == MIGRATE_MOVABLE) |
| 1382 | return is_migrate_movable(block_mt); |
| 1383 | else |
| 1384 | return block_mt == cc->migratetype; |
Vlastimil Babka | b682deb | 2017-05-08 15:54:43 -0700 | [diff] [blame] | 1385 | } |
| 1386 | |
Andrew Morton | 018e9a4 | 2015-04-15 16:15:20 -0700 | [diff] [blame] | 1387 | /* Returns true if the page is within a block suitable for migration to */ |
Minchan Kim | a934b923 | 2023-01-12 09:52:49 -0800 | [diff] [blame] | 1388 | static bool suitable_migration_target(struct compact_control_ext *cc_ext, |
Vlastimil Babka | 9f7e338 | 2016-10-07 17:00:37 -0700 | [diff] [blame] | 1389 | struct page *page) |
Andrew Morton | 018e9a4 | 2015-04-15 16:15:20 -0700 | [diff] [blame] | 1390 | { |
Minchan Kim | a934b923 | 2023-01-12 09:52:49 -0800 | [diff] [blame] | 1391 | struct compact_control *cc = cc_ext->cc; |
Andrew Morton | 018e9a4 | 2015-04-15 16:15:20 -0700 | [diff] [blame] | 1392 | /* If the page is a large free page, then disallow migration */ |
| 1393 | if (PageBuddy(page)) { |
| 1394 | /* |
| 1395 | * We are checking page_order without zone->lock taken. But |
| 1396 | * the only small danger is that we skip a potentially suitable |
| 1397 | * pageblock, so it's not worth to check order for valid range. |
| 1398 | */ |
Matthew Wilcox (Oracle) | ab130f91 | 2020-10-15 20:10:15 -0700 | [diff] [blame] | 1399 | if (buddy_order_unsafe(page) >= pageblock_order) |
Andrew Morton | 018e9a4 | 2015-04-15 16:15:20 -0700 | [diff] [blame] | 1400 | return false; |
| 1401 | } |
| 1402 | |
Yisheng Xie | 1ef36db | 2017-05-03 14:53:54 -0700 | [diff] [blame] | 1403 | if (cc->ignore_block_suitable) |
| 1404 | return true; |
| 1405 | |
Minchan Kim | a934b923 | 2023-01-12 09:52:49 -0800 | [diff] [blame] | 1406 | /* Allow file pages to migrate only into MIGRATE_MOVABLE blocks */ |
| 1407 | if (cc_ext->nr_migrate_file_pages) |
| 1408 | return get_pageblock_migratetype(page) == MIGRATE_MOVABLE; |
| 1409 | |
Andrew Morton | 018e9a4 | 2015-04-15 16:15:20 -0700 | [diff] [blame] | 1410 | /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */ |
Vlastimil Babka | b682deb | 2017-05-08 15:54:43 -0700 | [diff] [blame] | 1411 | if (is_migrate_movable(get_pageblock_migratetype(page))) |
Andrew Morton | 018e9a4 | 2015-04-15 16:15:20 -0700 | [diff] [blame] | 1412 | return true; |
| 1413 | |
| 1414 | /* Otherwise skip the block */ |
| 1415 | return false; |
| 1416 | } |
| 1417 | |
Mel Gorman | 70b4459 | 2019-03-05 15:44:54 -0800 | [diff] [blame] | 1418 | static inline unsigned int |
| 1419 | freelist_scan_limit(struct compact_control *cc) |
| 1420 | { |
Qian Cai | dd7ef7b | 2019-05-13 17:17:38 -0700 | [diff] [blame] | 1421 | unsigned short shift = BITS_PER_LONG - 1; |
| 1422 | |
| 1423 | return (COMPACT_CLUSTER_MAX >> min(shift, cc->fast_search_fail)) + 1; |
Mel Gorman | 70b4459 | 2019-03-05 15:44:54 -0800 | [diff] [blame] | 1424 | } |
| 1425 | |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1426 | /* |
Vlastimil Babka | f2849aa | 2015-09-08 15:02:36 -0700 | [diff] [blame] | 1427 | * Test whether the free scanner has reached the same or lower pageblock than |
| 1428 | * the migration scanner, and compaction should thus terminate. |
| 1429 | */ |
| 1430 | static inline bool compact_scanners_met(struct compact_control *cc) |
| 1431 | { |
| 1432 | return (cc->free_pfn >> pageblock_order) |
| 1433 | <= (cc->migrate_pfn >> pageblock_order); |
| 1434 | } |
| 1435 | |
Mel Gorman | 5a81188 | 2019-03-05 15:45:01 -0800 | [diff] [blame] | 1436 | /* |
| 1437 | * Used when scanning for a suitable migration target which scans freelists |
| 1438 | * in reverse. Reorders the list such as the unscanned pages are scanned |
| 1439 | * first on the next iteration of the free scanner |
| 1440 | */ |
| 1441 | static void |
| 1442 | move_freelist_head(struct list_head *freelist, struct page *freepage) |
| 1443 | { |
| 1444 | LIST_HEAD(sublist); |
| 1445 | |
| 1446 | if (!list_is_last(freelist, &freepage->lru)) { |
| 1447 | list_cut_before(&sublist, freelist, &freepage->lru); |
Liu Xiang | d2155fe | 2021-06-30 18:50:51 -0700 | [diff] [blame] | 1448 | list_splice_tail(&sublist, freelist); |
Mel Gorman | 5a81188 | 2019-03-05 15:45:01 -0800 | [diff] [blame] | 1449 | } |
| 1450 | } |
| 1451 | |
| 1452 | /* |
| 1453 | * Similar to move_freelist_head except used by the migration scanner |
| 1454 | * when scanning forward. It's possible for these list operations to |
| 1455 | * move against each other if they search the free list exactly in |
| 1456 | * lockstep. |
| 1457 | */ |
Mel Gorman | 70b4459 | 2019-03-05 15:44:54 -0800 | [diff] [blame] | 1458 | static void |
| 1459 | move_freelist_tail(struct list_head *freelist, struct page *freepage) |
| 1460 | { |
| 1461 | LIST_HEAD(sublist); |
| 1462 | |
| 1463 | if (!list_is_first(freelist, &freepage->lru)) { |
| 1464 | list_cut_position(&sublist, freelist, &freepage->lru); |
Liu Xiang | d2155fe | 2021-06-30 18:50:51 -0700 | [diff] [blame] | 1465 | list_splice_tail(&sublist, freelist); |
Mel Gorman | 70b4459 | 2019-03-05 15:44:54 -0800 | [diff] [blame] | 1466 | } |
| 1467 | } |
| 1468 | |
Mel Gorman | 5a81188 | 2019-03-05 15:45:01 -0800 | [diff] [blame] | 1469 | static void |
NARIBAYASHI Akira | 35d8a89 | 2022-10-26 20:24:38 +0900 | [diff] [blame] | 1470 | fast_isolate_around(struct compact_control *cc, unsigned long pfn) |
Mel Gorman | 5a81188 | 2019-03-05 15:45:01 -0800 | [diff] [blame] | 1471 | { |
| 1472 | unsigned long start_pfn, end_pfn; |
Vlastimil Babka | 6e2b704 | 2021-02-24 12:09:39 -0800 | [diff] [blame] | 1473 | struct page *page; |
Mel Gorman | 5a81188 | 2019-03-05 15:45:01 -0800 | [diff] [blame] | 1474 | |
| 1475 | /* Do not search around if there are enough pages already */ |
| 1476 | if (cc->nr_freepages >= cc->nr_migratepages) |
| 1477 | return; |
| 1478 | |
| 1479 | /* Minimise scanning during async compaction */ |
| 1480 | if (cc->direct_compaction && cc->mode == MIGRATE_ASYNC) |
| 1481 | return; |
| 1482 | |
| 1483 | /* Pageblock boundaries */ |
Vlastimil Babka | 6e2b704 | 2021-02-24 12:09:39 -0800 | [diff] [blame] | 1484 | start_pfn = max(pageblock_start_pfn(pfn), cc->zone->zone_start_pfn); |
| 1485 | end_pfn = min(pageblock_end_pfn(pfn), zone_end_pfn(cc->zone)); |
| 1486 | |
| 1487 | page = pageblock_pfn_to_page(start_pfn, end_pfn, cc->zone); |
| 1488 | if (!page) |
| 1489 | return; |
Mel Gorman | 5a81188 | 2019-03-05 15:45:01 -0800 | [diff] [blame] | 1490 | |
NARIBAYASHI Akira | 35d8a89 | 2022-10-26 20:24:38 +0900 | [diff] [blame] | 1491 | isolate_freepages_block(cc, &start_pfn, end_pfn, &cc->freepages, 1, false); |
Mel Gorman | 5a81188 | 2019-03-05 15:45:01 -0800 | [diff] [blame] | 1492 | |
| 1493 | /* Skip this pageblock in the future as it's full or nearly full */ |
| 1494 | if (cc->nr_freepages < cc->nr_migratepages) |
| 1495 | set_pageblock_skip(page); |
NARIBAYASHI Akira | 35d8a89 | 2022-10-26 20:24:38 +0900 | [diff] [blame] | 1496 | |
| 1497 | return; |
Mel Gorman | 5a81188 | 2019-03-05 15:45:01 -0800 | [diff] [blame] | 1498 | } |
| 1499 | |
Mel Gorman | dbe2d4e | 2019-03-05 15:45:31 -0800 | [diff] [blame] | 1500 | /* Search orders in round-robin fashion */ |
| 1501 | static int next_search_order(struct compact_control *cc, int order) |
| 1502 | { |
| 1503 | order--; |
| 1504 | if (order < 0) |
| 1505 | order = cc->order - 1; |
| 1506 | |
| 1507 | /* Search wrapped around? */ |
| 1508 | if (order == cc->search_order) { |
| 1509 | cc->search_order--; |
| 1510 | if (cc->search_order < 0) |
| 1511 | cc->search_order = cc->order - 1; |
| 1512 | return -1; |
| 1513 | } |
| 1514 | |
| 1515 | return order; |
| 1516 | } |
| 1517 | |
Mel Gorman | 5a81188 | 2019-03-05 15:45:01 -0800 | [diff] [blame] | 1518 | static unsigned long |
| 1519 | fast_isolate_freepages(struct compact_control *cc) |
| 1520 | { |
Wonhyuk Yang | b55ca52 | 2021-06-30 18:50:53 -0700 | [diff] [blame] | 1521 | unsigned int limit = max(1U, freelist_scan_limit(cc) >> 1); |
Mel Gorman | 5a81188 | 2019-03-05 15:45:01 -0800 | [diff] [blame] | 1522 | unsigned int nr_scanned = 0; |
Rokudo Yan | 74e2148 | 2021-02-04 18:32:20 -0800 | [diff] [blame] | 1523 | unsigned long low_pfn, min_pfn, highest = 0; |
Mel Gorman | 5a81188 | 2019-03-05 15:45:01 -0800 | [diff] [blame] | 1524 | unsigned long nr_isolated = 0; |
| 1525 | unsigned long distance; |
| 1526 | struct page *page = NULL; |
| 1527 | bool scan_start = false; |
| 1528 | int order; |
| 1529 | |
| 1530 | /* Full compaction passes in a negative order */ |
| 1531 | if (cc->order <= 0) |
| 1532 | return cc->free_pfn; |
| 1533 | |
| 1534 | /* |
| 1535 | * If starting the scan, use a deeper search and use the highest |
| 1536 | * PFN found if a suitable one is not found. |
| 1537 | */ |
Mel Gorman | e332f74 | 2019-03-05 15:45:38 -0800 | [diff] [blame] | 1538 | if (cc->free_pfn >= cc->zone->compact_init_free_pfn) { |
Mel Gorman | 5a81188 | 2019-03-05 15:45:01 -0800 | [diff] [blame] | 1539 | limit = pageblock_nr_pages >> 1; |
| 1540 | scan_start = true; |
| 1541 | } |
| 1542 | |
| 1543 | /* |
| 1544 | * Preferred point is in the top quarter of the scan space but take |
| 1545 | * a pfn from the top half if the search is problematic. |
| 1546 | */ |
| 1547 | distance = (cc->free_pfn - cc->migrate_pfn); |
| 1548 | low_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 2)); |
| 1549 | min_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 1)); |
| 1550 | |
| 1551 | if (WARN_ON_ONCE(min_pfn > low_pfn)) |
| 1552 | low_pfn = min_pfn; |
| 1553 | |
Mel Gorman | dbe2d4e | 2019-03-05 15:45:31 -0800 | [diff] [blame] | 1554 | /* |
| 1555 | * Search starts from the last successful isolation order or the next |
| 1556 | * order to search after a previous failure |
| 1557 | */ |
| 1558 | cc->search_order = min_t(unsigned int, cc->order - 1, cc->search_order); |
| 1559 | |
| 1560 | for (order = cc->search_order; |
| 1561 | !page && order >= 0; |
| 1562 | order = next_search_order(cc, order)) { |
Mel Gorman | 5a81188 | 2019-03-05 15:45:01 -0800 | [diff] [blame] | 1563 | struct free_area *area = &cc->zone->free_area[order]; |
| 1564 | struct list_head *freelist; |
| 1565 | struct page *freepage; |
| 1566 | unsigned long flags; |
| 1567 | unsigned int order_scanned = 0; |
Rokudo Yan | 74e2148 | 2021-02-04 18:32:20 -0800 | [diff] [blame] | 1568 | unsigned long high_pfn = 0; |
Mel Gorman | 5a81188 | 2019-03-05 15:45:01 -0800 | [diff] [blame] | 1569 | |
| 1570 | if (!area->nr_free) |
| 1571 | continue; |
| 1572 | |
| 1573 | spin_lock_irqsave(&cc->zone->lock, flags); |
| 1574 | freelist = &area->free_list[MIGRATE_MOVABLE]; |
| 1575 | list_for_each_entry_reverse(freepage, freelist, lru) { |
| 1576 | unsigned long pfn; |
| 1577 | |
| 1578 | order_scanned++; |
| 1579 | nr_scanned++; |
| 1580 | pfn = page_to_pfn(freepage); |
| 1581 | |
| 1582 | if (pfn >= highest) |
Vlastimil Babka | 6e2b704 | 2021-02-24 12:09:39 -0800 | [diff] [blame] | 1583 | highest = max(pageblock_start_pfn(pfn), |
| 1584 | cc->zone->zone_start_pfn); |
Mel Gorman | 5a81188 | 2019-03-05 15:45:01 -0800 | [diff] [blame] | 1585 | |
| 1586 | if (pfn >= low_pfn) { |
| 1587 | cc->fast_search_fail = 0; |
Mel Gorman | dbe2d4e | 2019-03-05 15:45:31 -0800 | [diff] [blame] | 1588 | cc->search_order = order; |
Mel Gorman | 5a81188 | 2019-03-05 15:45:01 -0800 | [diff] [blame] | 1589 | page = freepage; |
| 1590 | break; |
| 1591 | } |
| 1592 | |
| 1593 | if (pfn >= min_pfn && pfn > high_pfn) { |
| 1594 | high_pfn = pfn; |
| 1595 | |
| 1596 | /* Shorten the scan if a candidate is found */ |
| 1597 | limit >>= 1; |
| 1598 | } |
| 1599 | |
| 1600 | if (order_scanned >= limit) |
| 1601 | break; |
| 1602 | } |
| 1603 | |
| 1604 | /* Use a minimum pfn if a preferred one was not found */ |
| 1605 | if (!page && high_pfn) { |
| 1606 | page = pfn_to_page(high_pfn); |
| 1607 | |
| 1608 | /* Update freepage for the list reorder below */ |
| 1609 | freepage = page; |
| 1610 | } |
| 1611 | |
| 1612 | /* Reorder to so a future search skips recent pages */ |
| 1613 | move_freelist_head(freelist, freepage); |
| 1614 | |
| 1615 | /* Isolate the page if available */ |
| 1616 | if (page) { |
| 1617 | if (__isolate_free_page(page, order)) { |
| 1618 | set_page_private(page, order); |
| 1619 | nr_isolated = 1 << order; |
| 1620 | cc->nr_freepages += nr_isolated; |
| 1621 | list_add_tail(&page->lru, &cc->freepages); |
| 1622 | count_compact_events(COMPACTISOLATED, nr_isolated); |
| 1623 | } else { |
| 1624 | /* If isolation fails, abort the search */ |
Qian Cai | 5b56d99 | 2019-04-04 11:54:41 +0100 | [diff] [blame] | 1625 | order = cc->search_order + 1; |
Mel Gorman | 5a81188 | 2019-03-05 15:45:01 -0800 | [diff] [blame] | 1626 | page = NULL; |
| 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | spin_unlock_irqrestore(&cc->zone->lock, flags); |
| 1631 | |
| 1632 | /* |
Wonhyuk Yang | b55ca52 | 2021-06-30 18:50:53 -0700 | [diff] [blame] | 1633 | * Smaller scan on next order so the total scan is related |
Mel Gorman | 5a81188 | 2019-03-05 15:45:01 -0800 | [diff] [blame] | 1634 | * to freelist_scan_limit. |
| 1635 | */ |
| 1636 | if (order_scanned >= limit) |
Wonhyuk Yang | b55ca52 | 2021-06-30 18:50:53 -0700 | [diff] [blame] | 1637 | limit = max(1U, limit >> 1); |
Mel Gorman | 5a81188 | 2019-03-05 15:45:01 -0800 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | if (!page) { |
| 1641 | cc->fast_search_fail++; |
| 1642 | if (scan_start) { |
| 1643 | /* |
| 1644 | * Use the highest PFN found above min. If one was |
Ethon Paul | f386775 | 2020-06-04 16:49:13 -0700 | [diff] [blame] | 1645 | * not found, be pessimistic for direct compaction |
Mel Gorman | 5a81188 | 2019-03-05 15:45:01 -0800 | [diff] [blame] | 1646 | * and use the min mark. |
| 1647 | */ |
| 1648 | if (highest) { |
| 1649 | page = pfn_to_page(highest); |
| 1650 | cc->free_pfn = highest; |
| 1651 | } else { |
Suzuki K Poulose | e577c8b | 2019-05-31 22:30:59 -0700 | [diff] [blame] | 1652 | if (cc->direct_compaction && pfn_valid(min_pfn)) { |
Baoquan He | 73a6e47 | 2020-06-03 15:57:55 -0700 | [diff] [blame] | 1653 | page = pageblock_pfn_to_page(min_pfn, |
Vlastimil Babka | 6e2b704 | 2021-02-24 12:09:39 -0800 | [diff] [blame] | 1654 | min(pageblock_end_pfn(min_pfn), |
| 1655 | zone_end_pfn(cc->zone)), |
Baoquan He | 73a6e47 | 2020-06-03 15:57:55 -0700 | [diff] [blame] | 1656 | cc->zone); |
Mel Gorman | 5a81188 | 2019-03-05 15:45:01 -0800 | [diff] [blame] | 1657 | cc->free_pfn = min_pfn; |
| 1658 | } |
| 1659 | } |
| 1660 | } |
| 1661 | } |
| 1662 | |
Mel Gorman | d097a6f | 2019-03-05 15:45:28 -0800 | [diff] [blame] | 1663 | if (highest && highest >= cc->zone->compact_cached_free_pfn) { |
| 1664 | highest -= pageblock_nr_pages; |
Mel Gorman | 5a81188 | 2019-03-05 15:45:01 -0800 | [diff] [blame] | 1665 | cc->zone->compact_cached_free_pfn = highest; |
Mel Gorman | d097a6f | 2019-03-05 15:45:28 -0800 | [diff] [blame] | 1666 | } |
Mel Gorman | 5a81188 | 2019-03-05 15:45:01 -0800 | [diff] [blame] | 1667 | |
| 1668 | cc->total_free_scanned += nr_scanned; |
| 1669 | if (!page) |
| 1670 | return cc->free_pfn; |
| 1671 | |
| 1672 | low_pfn = page_to_pfn(page); |
NARIBAYASHI Akira | 35d8a89 | 2022-10-26 20:24:38 +0900 | [diff] [blame] | 1673 | fast_isolate_around(cc, low_pfn); |
Mel Gorman | 5a81188 | 2019-03-05 15:45:01 -0800 | [diff] [blame] | 1674 | return low_pfn; |
| 1675 | } |
| 1676 | |
Vlastimil Babka | f2849aa | 2015-09-08 15:02:36 -0700 | [diff] [blame] | 1677 | /* |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1678 | * Based on information in the current compact_control, find blocks |
| 1679 | * suitable for isolating free pages from and then isolate them. |
| 1680 | */ |
Minchan Kim | a934b923 | 2023-01-12 09:52:49 -0800 | [diff] [blame] | 1681 | static void isolate_freepages(struct compact_control_ext *cc_ext) |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1682 | { |
Minchan Kim | a934b923 | 2023-01-12 09:52:49 -0800 | [diff] [blame] | 1683 | struct compact_control *cc = cc_ext->cc; |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1684 | struct zone *zone = cc->zone; |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1685 | struct page *page; |
Vlastimil Babka | c96b9e5 | 2014-06-04 16:07:26 -0700 | [diff] [blame] | 1686 | unsigned long block_start_pfn; /* start of current pageblock */ |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 1687 | unsigned long isolate_start_pfn; /* exact pfn we start at */ |
Vlastimil Babka | c96b9e5 | 2014-06-04 16:07:26 -0700 | [diff] [blame] | 1688 | unsigned long block_end_pfn; /* end of current pageblock */ |
| 1689 | unsigned long low_pfn; /* lowest pfn scanner is able to scan */ |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1690 | struct list_head *freelist = &cc->freepages; |
Mel Gorman | 4fca973 | 2019-03-05 15:45:34 -0800 | [diff] [blame] | 1691 | unsigned int stride; |
qinglin.li | 0681d57 | 2023-11-06 15:02:04 +0800 | [diff] [blame] | 1692 | bool bypass = false; |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 1693 | #ifdef CONFIG_AMLOGIC_CMA |
| 1694 | int migrate_type; |
| 1695 | #endif /* CONFIG_AMLOGIC_CMA */ |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1696 | |
Mel Gorman | 5a81188 | 2019-03-05 15:45:01 -0800 | [diff] [blame] | 1697 | /* Try a small search of the free lists for a candidate */ |
| 1698 | isolate_start_pfn = fast_isolate_freepages(cc); |
| 1699 | if (cc->nr_freepages) |
| 1700 | goto splitmap; |
| 1701 | |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1702 | /* |
| 1703 | * Initialise the free scanner. The starting point is where we last |
Vlastimil Babka | 49e068f | 2014-05-06 12:50:03 -0700 | [diff] [blame] | 1704 | * successfully isolated from, zone-cached value, or the end of the |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 1705 | * zone when isolating for the first time. For looping we also need |
| 1706 | * this pfn aligned down to the pageblock boundary, because we do |
Vlastimil Babka | c96b9e5 | 2014-06-04 16:07:26 -0700 | [diff] [blame] | 1707 | * block_start_pfn -= pageblock_nr_pages in the for loop. |
| 1708 | * For ending point, take care when isolating in last pageblock of a |
Randy Dunlap | a1c1dbe | 2020-08-11 18:32:49 -0700 | [diff] [blame] | 1709 | * zone which ends in the middle of a pageblock. |
Vlastimil Babka | 49e068f | 2014-05-06 12:50:03 -0700 | [diff] [blame] | 1710 | * The low boundary is the end of the pageblock the migration scanner |
| 1711 | * is using. |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1712 | */ |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 1713 | isolate_start_pfn = cc->free_pfn; |
Mel Gorman | 5a81188 | 2019-03-05 15:45:01 -0800 | [diff] [blame] | 1714 | block_start_pfn = pageblock_start_pfn(isolate_start_pfn); |
Vlastimil Babka | c96b9e5 | 2014-06-04 16:07:26 -0700 | [diff] [blame] | 1715 | block_end_pfn = min(block_start_pfn + pageblock_nr_pages, |
| 1716 | zone_end_pfn(zone)); |
Vlastimil Babka | 06b6640 | 2016-05-19 17:11:48 -0700 | [diff] [blame] | 1717 | low_pfn = pageblock_end_pfn(cc->migrate_pfn); |
Mel Gorman | 4fca973 | 2019-03-05 15:45:34 -0800 | [diff] [blame] | 1718 | stride = cc->mode == MIGRATE_ASYNC ? COMPACT_CLUSTER_MAX : 1; |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1719 | |
| 1720 | /* |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1721 | * Isolate free pages until enough are available to migrate the |
| 1722 | * pages on cc->migratepages. We stop searching if the migrate |
| 1723 | * and free page scanners meet or enough free pages are isolated. |
| 1724 | */ |
Vlastimil Babka | f5f61a3 | 2015-09-08 15:02:39 -0700 | [diff] [blame] | 1725 | for (; block_start_pfn >= low_pfn; |
Vlastimil Babka | c96b9e5 | 2014-06-04 16:07:26 -0700 | [diff] [blame] | 1726 | block_end_pfn = block_start_pfn, |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 1727 | block_start_pfn -= pageblock_nr_pages, |
| 1728 | isolate_start_pfn = block_start_pfn) { |
Mel Gorman | 4fca973 | 2019-03-05 15:45:34 -0800 | [diff] [blame] | 1729 | unsigned long nr_isolated; |
| 1730 | |
David Rientjes | f6ea3ad | 2013-09-30 13:45:03 -0700 | [diff] [blame] | 1731 | /* |
| 1732 | * This can iterate a massively long zone without finding any |
Mel Gorman | cb810ad | 2019-03-05 15:45:21 -0800 | [diff] [blame] | 1733 | * suitable migration targets, so periodically check resched. |
David Rientjes | f6ea3ad | 2013-09-30 13:45:03 -0700 | [diff] [blame] | 1734 | */ |
Mel Gorman | cb810ad | 2019-03-05 15:45:21 -0800 | [diff] [blame] | 1735 | if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))) |
Mel Gorman | cf66f07 | 2019-03-05 15:45:24 -0800 | [diff] [blame] | 1736 | cond_resched(); |
David Rientjes | f6ea3ad | 2013-09-30 13:45:03 -0700 | [diff] [blame] | 1737 | |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 1738 | page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, |
| 1739 | zone); |
| 1740 | if (!page) |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1741 | continue; |
| 1742 | |
| 1743 | /* Check the block is suitable for migration */ |
Minchan Kim | a934b923 | 2023-01-12 09:52:49 -0800 | [diff] [blame] | 1744 | if (!suitable_migration_target(cc_ext, page)) |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1745 | continue; |
Linus Torvalds | 68e3e92 | 2012-06-03 20:05:57 -0700 | [diff] [blame] | 1746 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 1747 | /* If isolation recently failed, do not retry */ |
| 1748 | if (!isolation_suitable(cc, page)) |
| 1749 | continue; |
| 1750 | |
qinglin.li | 0681d57 | 2023-11-06 15:02:04 +0800 | [diff] [blame] | 1751 | trace_android_vh_isolate_freepages(cc, page, &bypass); |
| 1752 | if (bypass) |
| 1753 | continue; |
| 1754 | |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 1755 | #ifdef CONFIG_AMLOGIC_CMA |
| 1756 | /* avoid compact to cma area */ |
| 1757 | migrate_type = get_pageblock_migratetype(page); |
| 1758 | if (is_migrate_isolate(migrate_type)) |
| 1759 | continue; |
| 1760 | if (is_migrate_cma(migrate_type) && |
| 1761 | test_bit(FORBID_TO_CMA_BIT, &cc->total_migrate_scanned)) |
| 1762 | continue; |
| 1763 | #endif /* CONFIG_AMLOGIC_CMA */ |
| 1764 | |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 1765 | /* Found a block suitable for isolating free pages from. */ |
Mel Gorman | 4fca973 | 2019-03-05 15:45:34 -0800 | [diff] [blame] | 1766 | nr_isolated = isolate_freepages_block(cc, &isolate_start_pfn, |
| 1767 | block_end_pfn, freelist, stride, false); |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1768 | |
Mel Gorman | d097a6f | 2019-03-05 15:45:28 -0800 | [diff] [blame] | 1769 | /* Update the skip hint if the full pageblock was scanned */ |
| 1770 | if (isolate_start_pfn == block_end_pfn) |
| 1771 | update_pageblock_skip(cc, page, block_start_pfn); |
| 1772 | |
Mel Gorman | cb2dcaf | 2019-03-05 15:45:11 -0800 | [diff] [blame] | 1773 | /* Are enough freepages isolated? */ |
| 1774 | if (cc->nr_freepages >= cc->nr_migratepages) { |
David Rientjes | a46cbf3 | 2016-07-14 12:06:50 -0700 | [diff] [blame] | 1775 | if (isolate_start_pfn >= block_end_pfn) { |
| 1776 | /* |
| 1777 | * Restart at previous pageblock if more |
| 1778 | * freepages can be isolated next time. |
| 1779 | */ |
Vlastimil Babka | f5f61a3 | 2015-09-08 15:02:39 -0700 | [diff] [blame] | 1780 | isolate_start_pfn = |
| 1781 | block_start_pfn - pageblock_nr_pages; |
David Rientjes | a46cbf3 | 2016-07-14 12:06:50 -0700 | [diff] [blame] | 1782 | } |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 1783 | break; |
David Rientjes | a46cbf3 | 2016-07-14 12:06:50 -0700 | [diff] [blame] | 1784 | } else if (isolate_start_pfn < block_end_pfn) { |
Vlastimil Babka | f5f61a3 | 2015-09-08 15:02:39 -0700 | [diff] [blame] | 1785 | /* |
David Rientjes | a46cbf3 | 2016-07-14 12:06:50 -0700 | [diff] [blame] | 1786 | * If isolation failed early, do not continue |
| 1787 | * needlessly. |
Vlastimil Babka | f5f61a3 | 2015-09-08 15:02:39 -0700 | [diff] [blame] | 1788 | */ |
David Rientjes | a46cbf3 | 2016-07-14 12:06:50 -0700 | [diff] [blame] | 1789 | break; |
Vlastimil Babka | f5f61a3 | 2015-09-08 15:02:39 -0700 | [diff] [blame] | 1790 | } |
Mel Gorman | 4fca973 | 2019-03-05 15:45:34 -0800 | [diff] [blame] | 1791 | |
| 1792 | /* Adjust stride depending on isolation */ |
| 1793 | if (nr_isolated) { |
| 1794 | stride = 1; |
| 1795 | continue; |
| 1796 | } |
| 1797 | stride = min_t(unsigned int, COMPACT_CLUSTER_MAX, stride << 1); |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 1798 | } |
| 1799 | |
Vlastimil Babka | 7ed695e | 2014-01-21 15:51:09 -0800 | [diff] [blame] | 1800 | /* |
Vlastimil Babka | f5f61a3 | 2015-09-08 15:02:39 -0700 | [diff] [blame] | 1801 | * Record where the free scanner will restart next time. Either we |
| 1802 | * broke from the loop and set isolate_start_pfn based on the last |
| 1803 | * call to isolate_freepages_block(), or we met the migration scanner |
| 1804 | * and the loop terminated due to isolate_start_pfn < low_pfn |
Vlastimil Babka | 7ed695e | 2014-01-21 15:51:09 -0800 | [diff] [blame] | 1805 | */ |
Vlastimil Babka | f5f61a3 | 2015-09-08 15:02:39 -0700 | [diff] [blame] | 1806 | cc->free_pfn = isolate_start_pfn; |
Mel Gorman | 5a81188 | 2019-03-05 15:45:01 -0800 | [diff] [blame] | 1807 | |
| 1808 | splitmap: |
| 1809 | /* __isolate_free_page() does not map the pages */ |
| 1810 | split_map_pages(freelist); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | /* |
| 1814 | * This is a migrate-callback that "allocates" freepages by taking pages |
| 1815 | * from the isolated freelists in the block we are migrating to. |
| 1816 | */ |
| 1817 | static struct page *compaction_alloc(struct page *migratepage, |
Michal Hocko | 666feb2 | 2018-04-10 16:30:03 -0700 | [diff] [blame] | 1818 | unsigned long data) |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1819 | { |
Minchan Kim | a934b923 | 2023-01-12 09:52:49 -0800 | [diff] [blame] | 1820 | struct compact_control_ext *cc_ext = (struct compact_control_ext *)data; |
| 1821 | struct compact_control *cc = cc_ext->cc; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1822 | struct page *freepage; |
| 1823 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1824 | if (list_empty(&cc->freepages)) { |
Minchan Kim | a934b923 | 2023-01-12 09:52:49 -0800 | [diff] [blame] | 1825 | isolate_freepages(cc_ext); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1826 | |
| 1827 | if (list_empty(&cc->freepages)) |
| 1828 | return NULL; |
| 1829 | } |
| 1830 | |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 1831 | #ifdef CONFIG_AMLOGIC_CMA |
| 1832 | freepage = get_compact_page(migratepage, cc); |
| 1833 | #else |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1834 | freepage = list_entry(cc->freepages.next, struct page, lru); |
| 1835 | list_del(&freepage->lru); |
| 1836 | cc->nr_freepages--; |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 1837 | #endif |
Tao Zeng | a1db7dc | 2022-01-20 15:09:43 +0800 | [diff] [blame] | 1838 | #ifdef CONFIG_AMLOGIC_PAGE_TRACE |
| 1839 | replace_page_trace(freepage, migratepage); |
| 1840 | #endif |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1841 | |
| 1842 | return freepage; |
| 1843 | } |
| 1844 | |
| 1845 | /* |
David Rientjes | d53aea3 | 2014-06-04 16:08:26 -0700 | [diff] [blame] | 1846 | * This is a migrate-callback that "frees" freepages back to the isolated |
| 1847 | * freelist. All pages on the freelist are from the same zone, so there is no |
| 1848 | * special handling needed for NUMA. |
| 1849 | */ |
| 1850 | static void compaction_free(struct page *page, unsigned long data) |
| 1851 | { |
Minchan Kim | a934b923 | 2023-01-12 09:52:49 -0800 | [diff] [blame] | 1852 | struct compact_control_ext *cc_ext = (struct compact_control_ext *)data; |
| 1853 | struct compact_control *cc = cc_ext->cc; |
David Rientjes | d53aea3 | 2014-06-04 16:08:26 -0700 | [diff] [blame] | 1854 | |
| 1855 | list_add(&page->lru, &cc->freepages); |
| 1856 | cc->nr_freepages++; |
| 1857 | } |
| 1858 | |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1859 | /* possible outcome of isolate_migratepages */ |
| 1860 | typedef enum { |
| 1861 | ISOLATE_ABORT, /* Abort compaction now */ |
| 1862 | ISOLATE_NONE, /* No pages isolated, continue scanning */ |
| 1863 | ISOLATE_SUCCESS, /* Pages isolated, migrate */ |
| 1864 | } isolate_migrate_t; |
| 1865 | |
| 1866 | /* |
Eric B Munson | 5bbe354 | 2015-04-15 16:13:20 -0700 | [diff] [blame] | 1867 | * Allow userspace to control policy on scanning the unevictable LRU for |
| 1868 | * compactable pages. |
| 1869 | */ |
Sebastian Andrzej Siewior | 6923aa0 | 2020-04-01 21:10:42 -0700 | [diff] [blame] | 1870 | #ifdef CONFIG_PREEMPT_RT |
| 1871 | int sysctl_compact_unevictable_allowed __read_mostly = 0; |
| 1872 | #else |
Eric B Munson | 5bbe354 | 2015-04-15 16:13:20 -0700 | [diff] [blame] | 1873 | int sysctl_compact_unevictable_allowed __read_mostly = 1; |
Sebastian Andrzej Siewior | 6923aa0 | 2020-04-01 21:10:42 -0700 | [diff] [blame] | 1874 | #endif |
Eric B Munson | 5bbe354 | 2015-04-15 16:13:20 -0700 | [diff] [blame] | 1875 | |
Mel Gorman | 70b4459 | 2019-03-05 15:44:54 -0800 | [diff] [blame] | 1876 | static inline void |
| 1877 | update_fast_start_pfn(struct compact_control *cc, unsigned long pfn) |
| 1878 | { |
| 1879 | if (cc->fast_start_pfn == ULONG_MAX) |
| 1880 | return; |
| 1881 | |
| 1882 | if (!cc->fast_start_pfn) |
| 1883 | cc->fast_start_pfn = pfn; |
| 1884 | |
| 1885 | cc->fast_start_pfn = min(cc->fast_start_pfn, pfn); |
| 1886 | } |
| 1887 | |
| 1888 | static inline unsigned long |
| 1889 | reinit_migrate_pfn(struct compact_control *cc) |
| 1890 | { |
| 1891 | if (!cc->fast_start_pfn || cc->fast_start_pfn == ULONG_MAX) |
| 1892 | return cc->migrate_pfn; |
| 1893 | |
| 1894 | cc->migrate_pfn = cc->fast_start_pfn; |
| 1895 | cc->fast_start_pfn = ULONG_MAX; |
| 1896 | |
| 1897 | return cc->migrate_pfn; |
| 1898 | } |
| 1899 | |
| 1900 | /* |
| 1901 | * Briefly search the free lists for a migration source that already has |
| 1902 | * some free pages to reduce the number of pages that need migration |
| 1903 | * before a pageblock is free. |
| 1904 | */ |
| 1905 | static unsigned long fast_find_migrateblock(struct compact_control *cc) |
| 1906 | { |
| 1907 | unsigned int limit = freelist_scan_limit(cc); |
| 1908 | unsigned int nr_scanned = 0; |
| 1909 | unsigned long distance; |
| 1910 | unsigned long pfn = cc->migrate_pfn; |
| 1911 | unsigned long high_pfn; |
| 1912 | int order; |
Wonhyuk Yang | 15d28d0 | 2021-02-24 12:09:36 -0800 | [diff] [blame] | 1913 | bool found_block = false; |
Mel Gorman | 70b4459 | 2019-03-05 15:44:54 -0800 | [diff] [blame] | 1914 | |
| 1915 | /* Skip hints are relied on to avoid repeats on the fast search */ |
| 1916 | if (cc->ignore_skip_hint) |
| 1917 | return pfn; |
| 1918 | |
| 1919 | /* |
| 1920 | * If the migrate_pfn is not at the start of a zone or the start |
| 1921 | * of a pageblock then assume this is a continuation of a previous |
| 1922 | * scan restarted due to COMPACT_CLUSTER_MAX. |
| 1923 | */ |
| 1924 | if (pfn != cc->zone->zone_start_pfn && pfn != pageblock_start_pfn(pfn)) |
| 1925 | return pfn; |
| 1926 | |
| 1927 | /* |
| 1928 | * For smaller orders, just linearly scan as the number of pages |
| 1929 | * to migrate should be relatively small and does not necessarily |
| 1930 | * justify freeing up a large block for a small allocation. |
| 1931 | */ |
| 1932 | if (cc->order <= PAGE_ALLOC_COSTLY_ORDER) |
| 1933 | return pfn; |
| 1934 | |
| 1935 | /* |
| 1936 | * Only allow kcompactd and direct requests for movable pages to |
| 1937 | * quickly clear out a MOVABLE pageblock for allocation. This |
| 1938 | * reduces the risk that a large movable pageblock is freed for |
| 1939 | * an unmovable/reclaimable small allocation. |
| 1940 | */ |
| 1941 | if (cc->direct_compaction && cc->migratetype != MIGRATE_MOVABLE) |
| 1942 | return pfn; |
| 1943 | |
| 1944 | /* |
| 1945 | * When starting the migration scanner, pick any pageblock within the |
| 1946 | * first half of the search space. Otherwise try and pick a pageblock |
| 1947 | * within the first eighth to reduce the chances that a migration |
| 1948 | * target later becomes a source. |
| 1949 | */ |
| 1950 | distance = (cc->free_pfn - cc->migrate_pfn) >> 1; |
| 1951 | if (cc->migrate_pfn != cc->zone->zone_start_pfn) |
| 1952 | distance >>= 2; |
| 1953 | high_pfn = pageblock_start_pfn(cc->migrate_pfn + distance); |
| 1954 | |
| 1955 | for (order = cc->order - 1; |
Wonhyuk Yang | 15d28d0 | 2021-02-24 12:09:36 -0800 | [diff] [blame] | 1956 | order >= PAGE_ALLOC_COSTLY_ORDER && !found_block && nr_scanned < limit; |
Mel Gorman | 70b4459 | 2019-03-05 15:44:54 -0800 | [diff] [blame] | 1957 | order--) { |
| 1958 | struct free_area *area = &cc->zone->free_area[order]; |
| 1959 | struct list_head *freelist; |
| 1960 | unsigned long flags; |
| 1961 | struct page *freepage; |
| 1962 | |
| 1963 | if (!area->nr_free) |
| 1964 | continue; |
| 1965 | |
| 1966 | spin_lock_irqsave(&cc->zone->lock, flags); |
| 1967 | freelist = &area->free_list[MIGRATE_MOVABLE]; |
| 1968 | list_for_each_entry(freepage, freelist, lru) { |
| 1969 | unsigned long free_pfn; |
| 1970 | |
Wonhyuk Yang | 15d28d0 | 2021-02-24 12:09:36 -0800 | [diff] [blame] | 1971 | if (nr_scanned++ >= limit) { |
| 1972 | move_freelist_tail(freelist, freepage); |
| 1973 | break; |
| 1974 | } |
| 1975 | |
Mel Gorman | 70b4459 | 2019-03-05 15:44:54 -0800 | [diff] [blame] | 1976 | free_pfn = page_to_pfn(freepage); |
| 1977 | if (free_pfn < high_pfn) { |
Mel Gorman | 70b4459 | 2019-03-05 15:44:54 -0800 | [diff] [blame] | 1978 | /* |
| 1979 | * Avoid if skipped recently. Ideally it would |
| 1980 | * move to the tail but even safe iteration of |
| 1981 | * the list assumes an entry is deleted, not |
| 1982 | * reordered. |
| 1983 | */ |
Wonhyuk Yang | 15d28d0 | 2021-02-24 12:09:36 -0800 | [diff] [blame] | 1984 | if (get_pageblock_skip(freepage)) |
Mel Gorman | 70b4459 | 2019-03-05 15:44:54 -0800 | [diff] [blame] | 1985 | continue; |
Mel Gorman | 70b4459 | 2019-03-05 15:44:54 -0800 | [diff] [blame] | 1986 | |
| 1987 | /* Reorder to so a future search skips recent pages */ |
| 1988 | move_freelist_tail(freelist, freepage); |
| 1989 | |
Mel Gorman | e380beb | 2019-03-05 15:44:58 -0800 | [diff] [blame] | 1990 | update_fast_start_pfn(cc, free_pfn); |
Mel Gorman | 70b4459 | 2019-03-05 15:44:54 -0800 | [diff] [blame] | 1991 | pfn = pageblock_start_pfn(free_pfn); |
Rei Yamamoto | 20e6ec7 | 2022-05-13 16:48:57 -0700 | [diff] [blame] | 1992 | if (pfn < cc->zone->zone_start_pfn) |
| 1993 | pfn = cc->zone->zone_start_pfn; |
Mel Gorman | 70b4459 | 2019-03-05 15:44:54 -0800 | [diff] [blame] | 1994 | cc->fast_search_fail = 0; |
Wonhyuk Yang | 15d28d0 | 2021-02-24 12:09:36 -0800 | [diff] [blame] | 1995 | found_block = true; |
Mel Gorman | 70b4459 | 2019-03-05 15:44:54 -0800 | [diff] [blame] | 1996 | set_pageblock_skip(freepage); |
| 1997 | break; |
| 1998 | } |
Mel Gorman | 70b4459 | 2019-03-05 15:44:54 -0800 | [diff] [blame] | 1999 | } |
| 2000 | spin_unlock_irqrestore(&cc->zone->lock, flags); |
| 2001 | } |
| 2002 | |
| 2003 | cc->total_migrate_scanned += nr_scanned; |
| 2004 | |
| 2005 | /* |
| 2006 | * If fast scanning failed then use a cached entry for a page block |
| 2007 | * that had free pages as the basis for starting a linear scan. |
| 2008 | */ |
Wonhyuk Yang | 15d28d0 | 2021-02-24 12:09:36 -0800 | [diff] [blame] | 2009 | if (!found_block) { |
| 2010 | cc->fast_search_fail++; |
Mel Gorman | 70b4459 | 2019-03-05 15:44:54 -0800 | [diff] [blame] | 2011 | pfn = reinit_migrate_pfn(cc); |
Wonhyuk Yang | 15d28d0 | 2021-02-24 12:09:36 -0800 | [diff] [blame] | 2012 | } |
Mel Gorman | 70b4459 | 2019-03-05 15:44:54 -0800 | [diff] [blame] | 2013 | return pfn; |
| 2014 | } |
| 2015 | |
Eric B Munson | 5bbe354 | 2015-04-15 16:13:20 -0700 | [diff] [blame] | 2016 | /* |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 2017 | * Isolate all pages that can be migrated from the first suitable block, |
| 2018 | * starting at the block pointed to by the migrate scanner pfn within |
| 2019 | * compact_control. |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 2020 | */ |
Minchan Kim | a934b923 | 2023-01-12 09:52:49 -0800 | [diff] [blame] | 2021 | static isolate_migrate_t isolate_migratepages(struct compact_control_ext *cc_ext) |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 2022 | { |
Minchan Kim | a934b923 | 2023-01-12 09:52:49 -0800 | [diff] [blame] | 2023 | struct compact_control *cc = cc_ext->cc; |
Joonsoo Kim | e1409c3 | 2016-03-15 14:57:48 -0700 | [diff] [blame] | 2024 | unsigned long block_start_pfn; |
| 2025 | unsigned long block_end_pfn; |
| 2026 | unsigned long low_pfn; |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 2027 | struct page *page; |
| 2028 | const isolate_mode_t isolate_mode = |
Eric B Munson | 5bbe354 | 2015-04-15 16:13:20 -0700 | [diff] [blame] | 2029 | (sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) | |
Hugh Dickins | 1d2047f | 2016-07-28 15:48:41 -0700 | [diff] [blame] | 2030 | (cc->mode != MIGRATE_SYNC ? ISOLATE_ASYNC_MIGRATE : 0); |
Mel Gorman | 70b4459 | 2019-03-05 15:44:54 -0800 | [diff] [blame] | 2031 | bool fast_find_block; |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 2032 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 2033 | /* |
| 2034 | * Start at where we last stopped, or beginning of the zone as |
Mel Gorman | 70b4459 | 2019-03-05 15:44:54 -0800 | [diff] [blame] | 2035 | * initialized by compact_zone(). The first failure will use |
| 2036 | * the lowest PFN as the starting point for linear scanning. |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 2037 | */ |
Mel Gorman | 70b4459 | 2019-03-05 15:44:54 -0800 | [diff] [blame] | 2038 | low_pfn = fast_find_migrateblock(cc); |
Vlastimil Babka | 06b6640 | 2016-05-19 17:11:48 -0700 | [diff] [blame] | 2039 | block_start_pfn = pageblock_start_pfn(low_pfn); |
Pengfei Li | 32aaf05 | 2019-09-23 15:36:58 -0700 | [diff] [blame] | 2040 | if (block_start_pfn < cc->zone->zone_start_pfn) |
| 2041 | block_start_pfn = cc->zone->zone_start_pfn; |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 2042 | |
Mel Gorman | 70b4459 | 2019-03-05 15:44:54 -0800 | [diff] [blame] | 2043 | /* |
| 2044 | * fast_find_migrateblock marks a pageblock skipped so to avoid |
| 2045 | * the isolation_suitable check below, check whether the fast |
| 2046 | * search was successful. |
| 2047 | */ |
| 2048 | fast_find_block = low_pfn != cc->migrate_pfn && !cc->fast_search_fail; |
| 2049 | |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 2050 | /* Only scan within a pageblock boundary */ |
Vlastimil Babka | 06b6640 | 2016-05-19 17:11:48 -0700 | [diff] [blame] | 2051 | block_end_pfn = pageblock_end_pfn(low_pfn); |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 2052 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 2053 | /* |
| 2054 | * Iterate over whole pageblocks until we find the first suitable. |
| 2055 | * Do not cross the free scanner. |
| 2056 | */ |
Joonsoo Kim | e1409c3 | 2016-03-15 14:57:48 -0700 | [diff] [blame] | 2057 | for (; block_end_pfn <= cc->free_pfn; |
Mel Gorman | 70b4459 | 2019-03-05 15:44:54 -0800 | [diff] [blame] | 2058 | fast_find_block = false, |
Oscar Salvador | c2ad7a1 | 2021-05-04 18:35:17 -0700 | [diff] [blame] | 2059 | cc->migrate_pfn = low_pfn = block_end_pfn, |
Joonsoo Kim | e1409c3 | 2016-03-15 14:57:48 -0700 | [diff] [blame] | 2060 | block_start_pfn = block_end_pfn, |
| 2061 | block_end_pfn += pageblock_nr_pages) { |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 2062 | |
| 2063 | /* |
| 2064 | * This can potentially iterate a massively long zone with |
| 2065 | * many pageblocks unsuitable, so periodically check if we |
Mel Gorman | cb810ad | 2019-03-05 15:45:21 -0800 | [diff] [blame] | 2066 | * need to schedule. |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 2067 | */ |
Mel Gorman | cb810ad | 2019-03-05 15:45:21 -0800 | [diff] [blame] | 2068 | if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))) |
Mel Gorman | cf66f07 | 2019-03-05 15:45:24 -0800 | [diff] [blame] | 2069 | cond_resched(); |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 2070 | |
Pengfei Li | 32aaf05 | 2019-09-23 15:36:58 -0700 | [diff] [blame] | 2071 | page = pageblock_pfn_to_page(block_start_pfn, |
| 2072 | block_end_pfn, cc->zone); |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 2073 | if (!page) |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 2074 | continue; |
| 2075 | |
Mel Gorman | e380beb | 2019-03-05 15:44:58 -0800 | [diff] [blame] | 2076 | /* |
| 2077 | * If isolation recently failed, do not retry. Only check the |
| 2078 | * pageblock once. COMPACT_CLUSTER_MAX causes a pageblock |
| 2079 | * to be visited multiple times. Assume skip was checked |
| 2080 | * before making it "skip" so other compaction instances do |
| 2081 | * not scan the same block. |
| 2082 | */ |
| 2083 | if (IS_ALIGNED(low_pfn, pageblock_nr_pages) && |
| 2084 | !fast_find_block && !isolation_suitable(cc, page)) |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 2085 | continue; |
| 2086 | |
| 2087 | /* |
Mel Gorman | 9bebefd | 2019-03-05 15:45:14 -0800 | [diff] [blame] | 2088 | * For async compaction, also only scan in MOVABLE blocks |
| 2089 | * without huge pages. Async compaction is optimistic to see |
| 2090 | * if the minimum amount of work satisfies the allocation. |
| 2091 | * The cached PFN is updated as it's possible that all |
| 2092 | * remaining blocks between source and target are unsuitable |
| 2093 | * and the compaction scanners fail to meet. |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 2094 | */ |
Mel Gorman | 9bebefd | 2019-03-05 15:45:14 -0800 | [diff] [blame] | 2095 | if (!suitable_migration_source(cc, page)) { |
| 2096 | update_cached_migrate(cc, block_end_pfn); |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 2097 | continue; |
Mel Gorman | 9bebefd | 2019-03-05 15:45:14 -0800 | [diff] [blame] | 2098 | } |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 2099 | |
| 2100 | /* Perform the isolation */ |
Minchan Kim | a934b923 | 2023-01-12 09:52:49 -0800 | [diff] [blame] | 2101 | if (isolate_migratepages_block(cc_ext, low_pfn, block_end_pfn, |
Oscar Salvador | c2ad7a1 | 2021-05-04 18:35:17 -0700 | [diff] [blame] | 2102 | isolate_mode)) |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 2103 | return ISOLATE_ABORT; |
| 2104 | |
| 2105 | /* |
| 2106 | * Either we isolated something and proceed with migration. Or |
| 2107 | * we failed and compact_zone should decide if we should |
| 2108 | * continue or not. |
| 2109 | */ |
| 2110 | break; |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 2111 | } |
| 2112 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 2113 | return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE; |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 2114 | } |
| 2115 | |
Yaowei Bai | 21c527a | 2015-11-05 18:47:20 -0800 | [diff] [blame] | 2116 | /* |
| 2117 | * order == -1 is expected when compacting via |
| 2118 | * /proc/sys/vm/compact_memory |
| 2119 | */ |
| 2120 | static inline bool is_via_compact_memory(int order) |
| 2121 | { |
| 2122 | return order == -1; |
| 2123 | } |
| 2124 | |
Nitin Gupta | facdaa9 | 2020-08-11 18:31:00 -0700 | [diff] [blame] | 2125 | static bool kswapd_is_running(pg_data_t *pgdat) |
| 2126 | { |
Peter Zijlstra | b03fbd4 | 2021-06-11 10:28:12 +0200 | [diff] [blame] | 2127 | return pgdat->kswapd && task_is_running(pgdat->kswapd); |
Nitin Gupta | facdaa9 | 2020-08-11 18:31:00 -0700 | [diff] [blame] | 2128 | } |
| 2129 | |
| 2130 | /* |
| 2131 | * A zone's fragmentation score is the external fragmentation wrt to the |
Charan Teja Reddy | 40d7e20 | 2021-02-24 12:09:32 -0800 | [diff] [blame] | 2132 | * COMPACTION_HPAGE_ORDER. It returns a value in the range [0, 100]. |
| 2133 | */ |
| 2134 | static unsigned int fragmentation_score_zone(struct zone *zone) |
| 2135 | { |
| 2136 | return extfrag_for_order(zone, COMPACTION_HPAGE_ORDER); |
| 2137 | } |
| 2138 | |
| 2139 | /* |
| 2140 | * A weighted zone's fragmentation score is the external fragmentation |
| 2141 | * wrt to the COMPACTION_HPAGE_ORDER scaled by the zone's size. It |
| 2142 | * returns a value in the range [0, 100]. |
Nitin Gupta | facdaa9 | 2020-08-11 18:31:00 -0700 | [diff] [blame] | 2143 | * |
| 2144 | * The scaling factor ensures that proactive compaction focuses on larger |
| 2145 | * zones like ZONE_NORMAL, rather than smaller, specialized zones like |
| 2146 | * ZONE_DMA32. For smaller zones, the score value remains close to zero, |
| 2147 | * and thus never exceeds the high threshold for proactive compaction. |
| 2148 | */ |
Charan Teja Reddy | 40d7e20 | 2021-02-24 12:09:32 -0800 | [diff] [blame] | 2149 | static unsigned int fragmentation_score_zone_weighted(struct zone *zone) |
Nitin Gupta | facdaa9 | 2020-08-11 18:31:00 -0700 | [diff] [blame] | 2150 | { |
| 2151 | unsigned long score; |
| 2152 | |
Charan Teja Reddy | 40d7e20 | 2021-02-24 12:09:32 -0800 | [diff] [blame] | 2153 | score = zone->present_pages * fragmentation_score_zone(zone); |
Nitin Gupta | facdaa9 | 2020-08-11 18:31:00 -0700 | [diff] [blame] | 2154 | return div64_ul(score, zone->zone_pgdat->node_present_pages + 1); |
| 2155 | } |
| 2156 | |
| 2157 | /* |
| 2158 | * The per-node proactive (background) compaction process is started by its |
| 2159 | * corresponding kcompactd thread when the node's fragmentation score |
| 2160 | * exceeds the high threshold. The compaction process remains active till |
| 2161 | * the node's score falls below the low threshold, or one of the back-off |
| 2162 | * conditions is met. |
| 2163 | */ |
Nitin Gupta | d34c0a7 | 2020-08-11 18:31:07 -0700 | [diff] [blame] | 2164 | static unsigned int fragmentation_score_node(pg_data_t *pgdat) |
Nitin Gupta | facdaa9 | 2020-08-11 18:31:00 -0700 | [diff] [blame] | 2165 | { |
Nitin Gupta | d34c0a7 | 2020-08-11 18:31:07 -0700 | [diff] [blame] | 2166 | unsigned int score = 0; |
Nitin Gupta | facdaa9 | 2020-08-11 18:31:00 -0700 | [diff] [blame] | 2167 | int zoneid; |
| 2168 | |
| 2169 | for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { |
| 2170 | struct zone *zone; |
| 2171 | |
| 2172 | zone = &pgdat->node_zones[zoneid]; |
Charan Teja Reddy | 40d7e20 | 2021-02-24 12:09:32 -0800 | [diff] [blame] | 2173 | score += fragmentation_score_zone_weighted(zone); |
Nitin Gupta | facdaa9 | 2020-08-11 18:31:00 -0700 | [diff] [blame] | 2174 | } |
| 2175 | |
| 2176 | return score; |
| 2177 | } |
| 2178 | |
Nitin Gupta | d34c0a7 | 2020-08-11 18:31:07 -0700 | [diff] [blame] | 2179 | static unsigned int fragmentation_score_wmark(pg_data_t *pgdat, bool low) |
Nitin Gupta | facdaa9 | 2020-08-11 18:31:00 -0700 | [diff] [blame] | 2180 | { |
Nitin Gupta | d34c0a7 | 2020-08-11 18:31:07 -0700 | [diff] [blame] | 2181 | unsigned int wmark_low; |
Nitin Gupta | facdaa9 | 2020-08-11 18:31:00 -0700 | [diff] [blame] | 2182 | |
| 2183 | /* |
Ingo Molnar | f0953a1 | 2021-05-06 18:06:47 -0700 | [diff] [blame] | 2184 | * Cap the low watermark to avoid excessive compaction |
| 2185 | * activity in case a user sets the proactiveness tunable |
Nitin Gupta | facdaa9 | 2020-08-11 18:31:00 -0700 | [diff] [blame] | 2186 | * close to 100 (maximum). |
| 2187 | */ |
Nitin Gupta | d34c0a7 | 2020-08-11 18:31:07 -0700 | [diff] [blame] | 2188 | wmark_low = max(100U - sysctl_compaction_proactiveness, 5U); |
| 2189 | return low ? wmark_low : min(wmark_low + 10, 100U); |
Nitin Gupta | facdaa9 | 2020-08-11 18:31:00 -0700 | [diff] [blame] | 2190 | } |
| 2191 | |
| 2192 | static bool should_proactive_compact_node(pg_data_t *pgdat) |
| 2193 | { |
| 2194 | int wmark_high; |
| 2195 | |
| 2196 | if (!sysctl_compaction_proactiveness || kswapd_is_running(pgdat)) |
| 2197 | return false; |
| 2198 | |
| 2199 | wmark_high = fragmentation_score_wmark(pgdat, false); |
| 2200 | return fragmentation_score_node(pgdat) > wmark_high; |
| 2201 | } |
| 2202 | |
Mel Gorman | 40cacbc | 2019-03-05 15:44:36 -0800 | [diff] [blame] | 2203 | static enum compact_result __compact_finished(struct compact_control *cc) |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 2204 | { |
Mel Gorman | 8fb74b9 | 2013-01-11 14:32:16 -0800 | [diff] [blame] | 2205 | unsigned int order; |
Vlastimil Babka | d39773a | 2017-05-08 15:54:46 -0700 | [diff] [blame] | 2206 | const int migratetype = cc->migratetype; |
Mel Gorman | cb2dcaf | 2019-03-05 15:45:11 -0800 | [diff] [blame] | 2207 | int ret; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 2208 | |
Mel Gorman | 753341a | 2012-10-08 16:32:40 -0700 | [diff] [blame] | 2209 | /* Compaction run completes if the migrate and free scanner meet */ |
Vlastimil Babka | f2849aa | 2015-09-08 15:02:36 -0700 | [diff] [blame] | 2210 | if (compact_scanners_met(cc)) { |
Vlastimil Babka | 55b7c4c | 2014-01-21 15:51:11 -0800 | [diff] [blame] | 2211 | /* Let the next compaction start anew. */ |
Mel Gorman | 40cacbc | 2019-03-05 15:44:36 -0800 | [diff] [blame] | 2212 | reset_cached_positions(cc->zone); |
Vlastimil Babka | 55b7c4c | 2014-01-21 15:51:11 -0800 | [diff] [blame] | 2213 | |
Mel Gorman | 6299702 | 2012-10-08 16:32:47 -0700 | [diff] [blame] | 2214 | /* |
| 2215 | * Mark that the PG_migrate_skip information should be cleared |
Vlastimil Babka | accf624 | 2016-03-17 14:18:15 -0700 | [diff] [blame] | 2216 | * by kswapd when it goes to sleep. kcompactd does not set the |
Mel Gorman | 6299702 | 2012-10-08 16:32:47 -0700 | [diff] [blame] | 2217 | * flag itself as the decision to be clear should be directly |
| 2218 | * based on an allocation request. |
| 2219 | */ |
Vlastimil Babka | accf624 | 2016-03-17 14:18:15 -0700 | [diff] [blame] | 2220 | if (cc->direct_compaction) |
Mel Gorman | 40cacbc | 2019-03-05 15:44:36 -0800 | [diff] [blame] | 2221 | cc->zone->compact_blockskip_flush = true; |
Mel Gorman | 6299702 | 2012-10-08 16:32:47 -0700 | [diff] [blame] | 2222 | |
Michal Hocko | c8f7de0 | 2016-05-20 16:56:47 -0700 | [diff] [blame] | 2223 | if (cc->whole_zone) |
| 2224 | return COMPACT_COMPLETE; |
| 2225 | else |
| 2226 | return COMPACT_PARTIAL_SKIPPED; |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 2227 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 2228 | |
Nitin Gupta | facdaa9 | 2020-08-11 18:31:00 -0700 | [diff] [blame] | 2229 | if (cc->proactive_compaction) { |
| 2230 | int score, wmark_low; |
| 2231 | pg_data_t *pgdat; |
| 2232 | |
| 2233 | pgdat = cc->zone->zone_pgdat; |
| 2234 | if (kswapd_is_running(pgdat)) |
| 2235 | return COMPACT_PARTIAL_SKIPPED; |
| 2236 | |
| 2237 | score = fragmentation_score_zone(cc->zone); |
| 2238 | wmark_low = fragmentation_score_wmark(pgdat, true); |
| 2239 | |
| 2240 | if (score > wmark_low) |
| 2241 | ret = COMPACT_CONTINUE; |
| 2242 | else |
| 2243 | ret = COMPACT_SUCCESS; |
| 2244 | |
| 2245 | goto out; |
| 2246 | } |
| 2247 | |
Yaowei Bai | 21c527a | 2015-11-05 18:47:20 -0800 | [diff] [blame] | 2248 | if (is_via_compact_memory(cc->order)) |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 2249 | return COMPACT_CONTINUE; |
| 2250 | |
Mel Gorman | efe771c | 2019-03-05 15:44:46 -0800 | [diff] [blame] | 2251 | /* |
| 2252 | * Always finish scanning a pageblock to reduce the possibility of |
| 2253 | * fallbacks in the future. This is particularly important when |
| 2254 | * migration source is unmovable/reclaimable but it's not worth |
| 2255 | * special casing. |
| 2256 | */ |
| 2257 | if (!IS_ALIGNED(cc->migrate_pfn, pageblock_nr_pages)) |
| 2258 | return COMPACT_CONTINUE; |
Vlastimil Babka | baf6a9a | 2017-05-08 15:54:52 -0700 | [diff] [blame] | 2259 | |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 2260 | /* Direct compactor: Is a suitable page free? */ |
Mel Gorman | cb2dcaf | 2019-03-05 15:45:11 -0800 | [diff] [blame] | 2261 | ret = COMPACT_NO_SUITABLE_PAGE; |
Mel Gorman | 8fb74b9 | 2013-01-11 14:32:16 -0800 | [diff] [blame] | 2262 | for (order = cc->order; order < MAX_ORDER; order++) { |
Mel Gorman | 40cacbc | 2019-03-05 15:44:36 -0800 | [diff] [blame] | 2263 | struct free_area *area = &cc->zone->free_area[order]; |
Joonsoo Kim | 2149cda | 2015-04-14 15:45:21 -0700 | [diff] [blame] | 2264 | bool can_steal; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 2265 | |
Mel Gorman | 8fb74b9 | 2013-01-11 14:32:16 -0800 | [diff] [blame] | 2266 | /* Job done if page is free of the right migratetype */ |
Dan Williams | b03641a | 2019-05-14 15:41:32 -0700 | [diff] [blame] | 2267 | if (!free_area_empty(area, migratetype)) |
Vlastimil Babka | cf37831 | 2016-10-07 16:57:41 -0700 | [diff] [blame] | 2268 | return COMPACT_SUCCESS; |
Mel Gorman | 8fb74b9 | 2013-01-11 14:32:16 -0800 | [diff] [blame] | 2269 | |
Joonsoo Kim | 2149cda | 2015-04-14 15:45:21 -0700 | [diff] [blame] | 2270 | #ifdef CONFIG_CMA |
| 2271 | /* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */ |
| 2272 | if (migratetype == MIGRATE_MOVABLE && |
Dan Williams | b03641a | 2019-05-14 15:41:32 -0700 | [diff] [blame] | 2273 | !free_area_empty(area, MIGRATE_CMA)) |
Vlastimil Babka | cf37831 | 2016-10-07 16:57:41 -0700 | [diff] [blame] | 2274 | return COMPACT_SUCCESS; |
Joonsoo Kim | 2149cda | 2015-04-14 15:45:21 -0700 | [diff] [blame] | 2275 | #endif |
| 2276 | /* |
| 2277 | * Job done if allocation would steal freepages from |
| 2278 | * other migratetype buddy lists. |
| 2279 | */ |
| 2280 | if (find_suitable_fallback(area, order, migratetype, |
Vlastimil Babka | baf6a9a | 2017-05-08 15:54:52 -0700 | [diff] [blame] | 2281 | true, &can_steal) != -1) { |
| 2282 | |
| 2283 | /* movable pages are OK in any pageblock */ |
| 2284 | if (migratetype == MIGRATE_MOVABLE) |
| 2285 | return COMPACT_SUCCESS; |
| 2286 | |
| 2287 | /* |
| 2288 | * We are stealing for a non-movable allocation. Make |
| 2289 | * sure we finish compacting the current pageblock |
| 2290 | * first so it is as free as possible and we won't |
| 2291 | * have to steal another one soon. This only applies |
| 2292 | * to sync compaction, as async compaction operates |
| 2293 | * on pageblocks of the same migratetype. |
| 2294 | */ |
| 2295 | if (cc->mode == MIGRATE_ASYNC || |
| 2296 | IS_ALIGNED(cc->migrate_pfn, |
| 2297 | pageblock_nr_pages)) { |
| 2298 | return COMPACT_SUCCESS; |
| 2299 | } |
| 2300 | |
Mel Gorman | cb2dcaf | 2019-03-05 15:45:11 -0800 | [diff] [blame] | 2301 | ret = COMPACT_CONTINUE; |
| 2302 | break; |
Vlastimil Babka | baf6a9a | 2017-05-08 15:54:52 -0700 | [diff] [blame] | 2303 | } |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 2304 | } |
| 2305 | |
Nitin Gupta | facdaa9 | 2020-08-11 18:31:00 -0700 | [diff] [blame] | 2306 | out: |
Mel Gorman | cb2dcaf | 2019-03-05 15:45:11 -0800 | [diff] [blame] | 2307 | if (cc->contended || fatal_signal_pending(current)) |
| 2308 | ret = COMPACT_CONTENDED; |
| 2309 | |
| 2310 | return ret; |
Joonsoo Kim | 837d026 | 2015-02-11 15:27:06 -0800 | [diff] [blame] | 2311 | } |
| 2312 | |
Mel Gorman | 40cacbc | 2019-03-05 15:44:36 -0800 | [diff] [blame] | 2313 | static enum compact_result compact_finished(struct compact_control *cc) |
Joonsoo Kim | 837d026 | 2015-02-11 15:27:06 -0800 | [diff] [blame] | 2314 | { |
| 2315 | int ret; |
| 2316 | |
Mel Gorman | 40cacbc | 2019-03-05 15:44:36 -0800 | [diff] [blame] | 2317 | ret = __compact_finished(cc); |
| 2318 | trace_mm_compaction_finished(cc->zone, cc->order, ret); |
Joonsoo Kim | 837d026 | 2015-02-11 15:27:06 -0800 | [diff] [blame] | 2319 | if (ret == COMPACT_NO_SUITABLE_PAGE) |
| 2320 | ret = COMPACT_CONTINUE; |
| 2321 | |
| 2322 | return ret; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 2323 | } |
| 2324 | |
Michal Hocko | ea7ab98 | 2016-05-20 16:56:38 -0700 | [diff] [blame] | 2325 | static enum compact_result __compaction_suitable(struct zone *zone, int order, |
Mel Gorman | c603844 | 2016-05-19 17:13:38 -0700 | [diff] [blame] | 2326 | unsigned int alloc_flags, |
Joonsoo Kim | 97a225e | 2020-06-03 15:59:01 -0700 | [diff] [blame] | 2327 | int highest_zoneidx, |
Michal Hocko | 86a294a | 2016-05-20 16:57:12 -0700 | [diff] [blame] | 2328 | unsigned long wmark_target) |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 2329 | { |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 2330 | unsigned long watermark; |
| 2331 | |
Yaowei Bai | 21c527a | 2015-11-05 18:47:20 -0800 | [diff] [blame] | 2332 | if (is_via_compact_memory(order)) |
Michal Hocko | 3957c77 | 2011-06-15 15:08:25 -0700 | [diff] [blame] | 2333 | return COMPACT_CONTINUE; |
| 2334 | |
Mel Gorman | a921444 | 2018-12-28 00:35:44 -0800 | [diff] [blame] | 2335 | watermark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK); |
Vlastimil Babka | ebff398 | 2014-12-10 15:43:22 -0800 | [diff] [blame] | 2336 | /* |
| 2337 | * If watermarks for high-order allocation are already met, there |
| 2338 | * should be no need for compaction at all. |
| 2339 | */ |
Joonsoo Kim | 97a225e | 2020-06-03 15:59:01 -0700 | [diff] [blame] | 2340 | if (zone_watermark_ok(zone, order, watermark, highest_zoneidx, |
Vlastimil Babka | ebff398 | 2014-12-10 15:43:22 -0800 | [diff] [blame] | 2341 | alloc_flags)) |
Vlastimil Babka | cf37831 | 2016-10-07 16:57:41 -0700 | [diff] [blame] | 2342 | return COMPACT_SUCCESS; |
Vlastimil Babka | ebff398 | 2014-12-10 15:43:22 -0800 | [diff] [blame] | 2343 | |
Michal Hocko | 3957c77 | 2011-06-15 15:08:25 -0700 | [diff] [blame] | 2344 | /* |
Vlastimil Babka | 9861a62 | 2016-10-07 16:57:53 -0700 | [diff] [blame] | 2345 | * Watermarks for order-0 must be met for compaction to be able to |
Vlastimil Babka | 984fdba | 2016-10-07 16:57:57 -0700 | [diff] [blame] | 2346 | * isolate free pages for migration targets. This means that the |
| 2347 | * watermark and alloc_flags have to match, or be more pessimistic than |
| 2348 | * the check in __isolate_free_page(). We don't use the direct |
| 2349 | * compactor's alloc_flags, as they are not relevant for freepage |
Joonsoo Kim | 97a225e | 2020-06-03 15:59:01 -0700 | [diff] [blame] | 2350 | * isolation. We however do use the direct compactor's highest_zoneidx |
| 2351 | * to skip over zones where lowmem reserves would prevent allocation |
| 2352 | * even if compaction succeeds. |
Vlastimil Babka | 8348faf | 2016-10-07 16:58:00 -0700 | [diff] [blame] | 2353 | * For costly orders, we require low watermark instead of min for |
| 2354 | * compaction to proceed to increase its chances. |
Joonsoo Kim | d883c6c | 2018-05-23 10:18:21 +0900 | [diff] [blame] | 2355 | * ALLOC_CMA is used, as pages in CMA pageblocks are considered |
| 2356 | * suitable migration targets |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 2357 | */ |
Vlastimil Babka | 8348faf | 2016-10-07 16:58:00 -0700 | [diff] [blame] | 2358 | watermark = (order > PAGE_ALLOC_COSTLY_ORDER) ? |
| 2359 | low_wmark_pages(zone) : min_wmark_pages(zone); |
| 2360 | watermark += compact_gap(order); |
Joonsoo Kim | 97a225e | 2020-06-03 15:59:01 -0700 | [diff] [blame] | 2361 | if (!__zone_watermark_ok(zone, 0, watermark, highest_zoneidx, |
Joonsoo Kim | d883c6c | 2018-05-23 10:18:21 +0900 | [diff] [blame] | 2362 | ALLOC_CMA, wmark_target)) |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 2363 | return COMPACT_SKIPPED; |
| 2364 | |
Vlastimil Babka | cc5c9f0 | 2016-10-07 17:00:43 -0700 | [diff] [blame] | 2365 | return COMPACT_CONTINUE; |
| 2366 | } |
| 2367 | |
Hui Su | 2b1a20c | 2020-12-14 19:12:42 -0800 | [diff] [blame] | 2368 | /* |
| 2369 | * compaction_suitable: Is this suitable to run compaction on this zone now? |
| 2370 | * Returns |
| 2371 | * COMPACT_SKIPPED - If there are too few free pages for compaction |
| 2372 | * COMPACT_SUCCESS - If the allocation would succeed without compaction |
| 2373 | * COMPACT_CONTINUE - If compaction should run now |
| 2374 | */ |
Vlastimil Babka | cc5c9f0 | 2016-10-07 17:00:43 -0700 | [diff] [blame] | 2375 | enum compact_result compaction_suitable(struct zone *zone, int order, |
| 2376 | unsigned int alloc_flags, |
Joonsoo Kim | 97a225e | 2020-06-03 15:59:01 -0700 | [diff] [blame] | 2377 | int highest_zoneidx) |
Vlastimil Babka | cc5c9f0 | 2016-10-07 17:00:43 -0700 | [diff] [blame] | 2378 | { |
| 2379 | enum compact_result ret; |
| 2380 | int fragindex; |
| 2381 | |
Joonsoo Kim | 97a225e | 2020-06-03 15:59:01 -0700 | [diff] [blame] | 2382 | ret = __compaction_suitable(zone, order, alloc_flags, highest_zoneidx, |
Vlastimil Babka | cc5c9f0 | 2016-10-07 17:00:43 -0700 | [diff] [blame] | 2383 | zone_page_state(zone, NR_FREE_PAGES)); |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 2384 | /* |
| 2385 | * fragmentation index determines if allocation failures are due to |
| 2386 | * low memory or external fragmentation |
| 2387 | * |
Vlastimil Babka | ebff398 | 2014-12-10 15:43:22 -0800 | [diff] [blame] | 2388 | * index of -1000 would imply allocations might succeed depending on |
| 2389 | * watermarks, but we already failed the high-order watermark check |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 2390 | * index towards 0 implies failure is due to lack of memory |
| 2391 | * index towards 1000 implies failure is due to fragmentation |
| 2392 | * |
Vlastimil Babka | 2031142 | 2016-10-07 17:00:46 -0700 | [diff] [blame] | 2393 | * Only compact if a failure would be due to fragmentation. Also |
| 2394 | * ignore fragindex for non-costly orders where the alternative to |
| 2395 | * a successful reclaim/compaction is OOM. Fragindex and the |
| 2396 | * vm.extfrag_threshold sysctl is meant as a heuristic to prevent |
| 2397 | * excessive compaction for costly orders, but it should not be at the |
| 2398 | * expense of system stability. |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 2399 | */ |
Vlastimil Babka | 2031142 | 2016-10-07 17:00:46 -0700 | [diff] [blame] | 2400 | if (ret == COMPACT_CONTINUE && (order > PAGE_ALLOC_COSTLY_ORDER)) { |
Vlastimil Babka | cc5c9f0 | 2016-10-07 17:00:43 -0700 | [diff] [blame] | 2401 | fragindex = fragmentation_index(zone, order); |
| 2402 | if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold) |
| 2403 | ret = COMPACT_NOT_SUITABLE_ZONE; |
| 2404 | } |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 2405 | |
Joonsoo Kim | 837d026 | 2015-02-11 15:27:06 -0800 | [diff] [blame] | 2406 | trace_mm_compaction_suitable(zone, order, ret); |
| 2407 | if (ret == COMPACT_NOT_SUITABLE_ZONE) |
| 2408 | ret = COMPACT_SKIPPED; |
| 2409 | |
| 2410 | return ret; |
| 2411 | } |
| 2412 | |
Michal Hocko | 86a294a | 2016-05-20 16:57:12 -0700 | [diff] [blame] | 2413 | bool compaction_zonelist_suitable(struct alloc_context *ac, int order, |
| 2414 | int alloc_flags) |
| 2415 | { |
| 2416 | struct zone *zone; |
| 2417 | struct zoneref *z; |
| 2418 | |
| 2419 | /* |
| 2420 | * Make sure at least one zone would pass __compaction_suitable if we continue |
| 2421 | * retrying the reclaim. |
| 2422 | */ |
Joonsoo Kim | 97a225e | 2020-06-03 15:59:01 -0700 | [diff] [blame] | 2423 | for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, |
| 2424 | ac->highest_zoneidx, ac->nodemask) { |
Michal Hocko | 86a294a | 2016-05-20 16:57:12 -0700 | [diff] [blame] | 2425 | unsigned long available; |
| 2426 | enum compact_result compact_result; |
| 2427 | |
| 2428 | /* |
| 2429 | * Do not consider all the reclaimable memory because we do not |
| 2430 | * want to trash just for a single high order allocation which |
| 2431 | * is even not guaranteed to appear even if __compaction_suitable |
| 2432 | * is happy about the watermark check. |
| 2433 | */ |
Mel Gorman | 5a1c84b | 2016-07-28 15:47:31 -0700 | [diff] [blame] | 2434 | available = zone_reclaimable_pages(zone) / order; |
Michal Hocko | 86a294a | 2016-05-20 16:57:12 -0700 | [diff] [blame] | 2435 | available += zone_page_state_snapshot(zone, NR_FREE_PAGES); |
| 2436 | compact_result = __compaction_suitable(zone, order, alloc_flags, |
Joonsoo Kim | 97a225e | 2020-06-03 15:59:01 -0700 | [diff] [blame] | 2437 | ac->highest_zoneidx, available); |
Vlastimil Babka | cc5c9f0 | 2016-10-07 17:00:43 -0700 | [diff] [blame] | 2438 | if (compact_result != COMPACT_SKIPPED) |
Michal Hocko | 86a294a | 2016-05-20 16:57:12 -0700 | [diff] [blame] | 2439 | return true; |
| 2440 | } |
| 2441 | |
| 2442 | return false; |
| 2443 | } |
| 2444 | |
Mel Gorman | 5e1f0f0 | 2019-03-05 15:45:41 -0800 | [diff] [blame] | 2445 | static enum compact_result |
| 2446 | compact_zone(struct compact_control *cc, struct capture_control *capc) |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 2447 | { |
Michal Hocko | ea7ab98 | 2016-05-20 16:56:38 -0700 | [diff] [blame] | 2448 | enum compact_result ret; |
Mel Gorman | 40cacbc | 2019-03-05 15:44:36 -0800 | [diff] [blame] | 2449 | unsigned long start_pfn = cc->zone->zone_start_pfn; |
| 2450 | unsigned long end_pfn = zone_end_pfn(cc->zone); |
Mel Gorman | 566e54e | 2019-03-05 15:44:32 -0800 | [diff] [blame] | 2451 | unsigned long last_migrated_pfn; |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 2452 | const bool sync = cc->mode != MIGRATE_ASYNC; |
Mel Gorman | 8854c55 | 2019-03-05 15:45:18 -0800 | [diff] [blame] | 2453 | bool update_cached; |
Robin Hsu | 1eeadb4 | 2022-05-13 10:19:50 +0800 | [diff] [blame] | 2454 | long vendor_ret; |
Minchan Kim | a934b923 | 2023-01-12 09:52:49 -0800 | [diff] [blame] | 2455 | struct compact_control_ext cc_ext = { |
| 2456 | .cc = cc, |
| 2457 | .nr_migrate_file_pages = 0, |
| 2458 | }; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 2459 | |
Yafang Shao | a94b525 | 2019-09-23 15:36:54 -0700 | [diff] [blame] | 2460 | /* |
| 2461 | * These counters track activities during zone compaction. Initialize |
| 2462 | * them before compacting a new zone. |
| 2463 | */ |
| 2464 | cc->total_migrate_scanned = 0; |
| 2465 | cc->total_free_scanned = 0; |
| 2466 | cc->nr_migratepages = 0; |
| 2467 | cc->nr_freepages = 0; |
| 2468 | INIT_LIST_HEAD(&cc->freepages); |
| 2469 | INIT_LIST_HEAD(&cc->migratepages); |
| 2470 | |
Wei Yang | 01c0bfe | 2020-06-03 15:59:08 -0700 | [diff] [blame] | 2471 | cc->migratetype = gfp_migratetype(cc->gfp_mask); |
Mel Gorman | 40cacbc | 2019-03-05 15:44:36 -0800 | [diff] [blame] | 2472 | ret = compaction_suitable(cc->zone, cc->order, cc->alloc_flags, |
Joonsoo Kim | 97a225e | 2020-06-03 15:59:01 -0700 | [diff] [blame] | 2473 | cc->highest_zoneidx); |
Michal Hocko | c46649d | 2016-05-20 16:56:41 -0700 | [diff] [blame] | 2474 | /* Compaction is likely to fail */ |
Vlastimil Babka | cf37831 | 2016-10-07 16:57:41 -0700 | [diff] [blame] | 2475 | if (ret == COMPACT_SUCCESS || ret == COMPACT_SKIPPED) |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 2476 | return ret; |
Michal Hocko | c46649d | 2016-05-20 16:56:41 -0700 | [diff] [blame] | 2477 | |
| 2478 | /* huh, compaction_suitable is returning something unexpected */ |
| 2479 | VM_BUG_ON(ret != COMPACT_CONTINUE); |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 2480 | |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 2481 | /* |
Vlastimil Babka | d3132e4 | 2014-01-21 15:51:08 -0800 | [diff] [blame] | 2482 | * Clear pageblock skip if there were failures recently and compaction |
Vlastimil Babka | accf624 | 2016-03-17 14:18:15 -0700 | [diff] [blame] | 2483 | * is about to be retried after being deferred. |
Vlastimil Babka | d3132e4 | 2014-01-21 15:51:08 -0800 | [diff] [blame] | 2484 | */ |
Mel Gorman | 40cacbc | 2019-03-05 15:44:36 -0800 | [diff] [blame] | 2485 | if (compaction_restarting(cc->zone, cc->order)) |
| 2486 | __reset_isolation_suitable(cc->zone); |
Vlastimil Babka | d3132e4 | 2014-01-21 15:51:08 -0800 | [diff] [blame] | 2487 | |
| 2488 | /* |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 2489 | * Setup to move all movable pages to the end of the zone. Used cached |
Vlastimil Babka | 06ed299 | 2016-10-07 16:57:35 -0700 | [diff] [blame] | 2490 | * information on where the scanners should start (unless we explicitly |
| 2491 | * want to compact the whole zone), but check that it is initialised |
| 2492 | * by ensuring the values are within zone boundaries. |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 2493 | */ |
Mel Gorman | 70b4459 | 2019-03-05 15:44:54 -0800 | [diff] [blame] | 2494 | cc->fast_start_pfn = 0; |
Vlastimil Babka | 06ed299 | 2016-10-07 16:57:35 -0700 | [diff] [blame] | 2495 | if (cc->whole_zone) { |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 2496 | cc->migrate_pfn = start_pfn; |
Vlastimil Babka | 06ed299 | 2016-10-07 16:57:35 -0700 | [diff] [blame] | 2497 | cc->free_pfn = pageblock_start_pfn(end_pfn - 1); |
| 2498 | } else { |
Mel Gorman | 40cacbc | 2019-03-05 15:44:36 -0800 | [diff] [blame] | 2499 | cc->migrate_pfn = cc->zone->compact_cached_migrate_pfn[sync]; |
| 2500 | cc->free_pfn = cc->zone->compact_cached_free_pfn; |
Vlastimil Babka | 06ed299 | 2016-10-07 16:57:35 -0700 | [diff] [blame] | 2501 | if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) { |
| 2502 | cc->free_pfn = pageblock_start_pfn(end_pfn - 1); |
Mel Gorman | 40cacbc | 2019-03-05 15:44:36 -0800 | [diff] [blame] | 2503 | cc->zone->compact_cached_free_pfn = cc->free_pfn; |
Vlastimil Babka | 06ed299 | 2016-10-07 16:57:35 -0700 | [diff] [blame] | 2504 | } |
| 2505 | if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) { |
| 2506 | cc->migrate_pfn = start_pfn; |
Mel Gorman | 40cacbc | 2019-03-05 15:44:36 -0800 | [diff] [blame] | 2507 | cc->zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn; |
| 2508 | cc->zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn; |
Vlastimil Babka | 06ed299 | 2016-10-07 16:57:35 -0700 | [diff] [blame] | 2509 | } |
Michal Hocko | c8f7de0 | 2016-05-20 16:56:47 -0700 | [diff] [blame] | 2510 | |
Mel Gorman | e332f74 | 2019-03-05 15:45:38 -0800 | [diff] [blame] | 2511 | if (cc->migrate_pfn <= cc->zone->compact_init_migrate_pfn) |
Vlastimil Babka | 06ed299 | 2016-10-07 16:57:35 -0700 | [diff] [blame] | 2512 | cc->whole_zone = true; |
| 2513 | } |
Michal Hocko | c8f7de0 | 2016-05-20 16:56:47 -0700 | [diff] [blame] | 2514 | |
Mel Gorman | 566e54e | 2019-03-05 15:44:32 -0800 | [diff] [blame] | 2515 | last_migrated_pfn = 0; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 2516 | |
Mel Gorman | 8854c55 | 2019-03-05 15:45:18 -0800 | [diff] [blame] | 2517 | /* |
| 2518 | * Migrate has separate cached PFNs for ASYNC and SYNC* migration on |
| 2519 | * the basis that some migrations will fail in ASYNC mode. However, |
| 2520 | * if the cached PFNs match and pageblocks are skipped due to having |
| 2521 | * no isolation candidates, then the sync state does not matter. |
| 2522 | * Until a pageblock with isolation candidates is found, keep the |
| 2523 | * cached PFNs in sync to avoid revisiting the same blocks. |
| 2524 | */ |
| 2525 | update_cached = !sync && |
| 2526 | cc->zone->compact_cached_migrate_pfn[0] == cc->zone->compact_cached_migrate_pfn[1]; |
| 2527 | |
Joonsoo Kim | 16c4a09 | 2015-02-11 15:27:01 -0800 | [diff] [blame] | 2528 | trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, |
| 2529 | cc->free_pfn, end_pfn, sync); |
Robin Hsu | 1eeadb4 | 2022-05-13 10:19:50 +0800 | [diff] [blame] | 2530 | trace_android_vh_mm_compaction_begin(cc, &vendor_ret); |
Mel Gorman | 0eb927c | 2014-01-21 15:51:05 -0800 | [diff] [blame] | 2531 | |
Minchan Kim | 361a2a2 | 2021-05-04 18:36:57 -0700 | [diff] [blame] | 2532 | /* lru_add_drain_all could be expensive with involving other CPUs */ |
| 2533 | lru_add_drain(); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 2534 | |
Mel Gorman | 40cacbc | 2019-03-05 15:44:36 -0800 | [diff] [blame] | 2535 | while ((ret = compact_finished(cc)) == COMPACT_CONTINUE) { |
Minchan Kim | 9d502c1 | 2011-03-22 16:30:39 -0700 | [diff] [blame] | 2536 | int err; |
Yanfei Xu | 19d3cf9 | 2020-12-14 19:12:39 -0800 | [diff] [blame] | 2537 | unsigned long iteration_start_pfn = cc->migrate_pfn; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 2538 | |
Mel Gorman | 804d312 | 2019-03-05 15:45:07 -0800 | [diff] [blame] | 2539 | /* |
| 2540 | * Avoid multiple rescans which can happen if a page cannot be |
| 2541 | * isolated (dirty/writeback in async mode) or if the migrated |
| 2542 | * pages are being allocated before the pageblock is cleared. |
| 2543 | * The first rescan will capture the entire pageblock for |
| 2544 | * migration. If it fails, it'll be marked skip and scanning |
| 2545 | * will proceed as normal. |
| 2546 | */ |
| 2547 | cc->rescan = false; |
| 2548 | if (pageblock_start_pfn(last_migrated_pfn) == |
Yanfei Xu | 19d3cf9 | 2020-12-14 19:12:39 -0800 | [diff] [blame] | 2549 | pageblock_start_pfn(iteration_start_pfn)) { |
Mel Gorman | 804d312 | 2019-03-05 15:45:07 -0800 | [diff] [blame] | 2550 | cc->rescan = true; |
| 2551 | } |
| 2552 | |
Minchan Kim | a934b923 | 2023-01-12 09:52:49 -0800 | [diff] [blame] | 2553 | switch (isolate_migratepages(&cc_ext)) { |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 2554 | case ISOLATE_ABORT: |
Vlastimil Babka | 2d1e104 | 2015-11-05 18:48:02 -0800 | [diff] [blame] | 2555 | ret = COMPACT_CONTENDED; |
Rafael Aquini | 5733c7d | 2012-12-11 16:02:47 -0800 | [diff] [blame] | 2556 | putback_movable_pages(&cc->migratepages); |
Shaohua Li | e64c523 | 2012-10-08 16:32:27 -0700 | [diff] [blame] | 2557 | cc->nr_migratepages = 0; |
Minchan Kim | a934b923 | 2023-01-12 09:52:49 -0800 | [diff] [blame] | 2558 | cc_ext.nr_migrate_file_pages = 0; |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 2559 | goto out; |
| 2560 | case ISOLATE_NONE: |
Mel Gorman | 8854c55 | 2019-03-05 15:45:18 -0800 | [diff] [blame] | 2561 | if (update_cached) { |
| 2562 | cc->zone->compact_cached_migrate_pfn[1] = |
| 2563 | cc->zone->compact_cached_migrate_pfn[0]; |
| 2564 | } |
| 2565 | |
Vlastimil Babka | fdaf7f5 | 2014-12-10 15:43:34 -0800 | [diff] [blame] | 2566 | /* |
| 2567 | * We haven't isolated and migrated anything, but |
| 2568 | * there might still be unflushed migrations from |
| 2569 | * previous cc->order aligned block. |
| 2570 | */ |
| 2571 | goto check_drain; |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 2572 | case ISOLATE_SUCCESS: |
Mel Gorman | 8854c55 | 2019-03-05 15:45:18 -0800 | [diff] [blame] | 2573 | update_cached = false; |
Yanfei Xu | 19d3cf9 | 2020-12-14 19:12:39 -0800 | [diff] [blame] | 2574 | last_migrated_pfn = iteration_start_pfn; |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 2575 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 2576 | |
David Rientjes | d53aea3 | 2014-06-04 16:08:26 -0700 | [diff] [blame] | 2577 | err = migrate_pages(&cc->migratepages, compaction_alloc, |
Minchan Kim | a934b923 | 2023-01-12 09:52:49 -0800 | [diff] [blame] | 2578 | compaction_free, (unsigned long)&cc_ext, cc->mode, |
Yang Shi | 5ac9588 | 2021-09-02 14:59:13 -0700 | [diff] [blame] | 2579 | MR_COMPACTION, NULL); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 2580 | |
Vlastimil Babka | f8c9301 | 2014-06-04 16:08:32 -0700 | [diff] [blame] | 2581 | trace_mm_compaction_migratepages(cc->nr_migratepages, err, |
| 2582 | &cc->migratepages); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 2583 | |
Vlastimil Babka | f8c9301 | 2014-06-04 16:08:32 -0700 | [diff] [blame] | 2584 | /* All pages were either migrated or will be released */ |
| 2585 | cc->nr_migratepages = 0; |
Minchan Kim | a934b923 | 2023-01-12 09:52:49 -0800 | [diff] [blame] | 2586 | cc_ext.nr_migrate_file_pages = 0; |
Minchan Kim | 9d502c1 | 2011-03-22 16:30:39 -0700 | [diff] [blame] | 2587 | if (err) { |
Rafael Aquini | 5733c7d | 2012-12-11 16:02:47 -0800 | [diff] [blame] | 2588 | putback_movable_pages(&cc->migratepages); |
Vlastimil Babka | 7ed695e | 2014-01-21 15:51:09 -0800 | [diff] [blame] | 2589 | /* |
| 2590 | * migrate_pages() may return -ENOMEM when scanners meet |
| 2591 | * and we want compact_finished() to detect it |
| 2592 | */ |
Vlastimil Babka | f2849aa | 2015-09-08 15:02:36 -0700 | [diff] [blame] | 2593 | if (err == -ENOMEM && !compact_scanners_met(cc)) { |
Vlastimil Babka | 2d1e104 | 2015-11-05 18:48:02 -0800 | [diff] [blame] | 2594 | ret = COMPACT_CONTENDED; |
David Rientjes | 4bf2bba | 2012-07-11 14:02:13 -0700 | [diff] [blame] | 2595 | goto out; |
| 2596 | } |
Vlastimil Babka | fdd048e | 2016-05-19 17:11:55 -0700 | [diff] [blame] | 2597 | /* |
| 2598 | * We failed to migrate at least one page in the current |
| 2599 | * order-aligned block, so skip the rest of it. |
| 2600 | */ |
| 2601 | if (cc->direct_compaction && |
| 2602 | (cc->mode == MIGRATE_ASYNC)) { |
| 2603 | cc->migrate_pfn = block_end_pfn( |
| 2604 | cc->migrate_pfn - 1, cc->order); |
| 2605 | /* Draining pcplists is useless in this case */ |
Mel Gorman | 566e54e | 2019-03-05 15:44:32 -0800 | [diff] [blame] | 2606 | last_migrated_pfn = 0; |
Vlastimil Babka | fdd048e | 2016-05-19 17:11:55 -0700 | [diff] [blame] | 2607 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 2608 | } |
Vlastimil Babka | fdaf7f5 | 2014-12-10 15:43:34 -0800 | [diff] [blame] | 2609 | |
Vlastimil Babka | fdaf7f5 | 2014-12-10 15:43:34 -0800 | [diff] [blame] | 2610 | check_drain: |
| 2611 | /* |
| 2612 | * Has the migration scanner moved away from the previous |
| 2613 | * cc->order aligned block where we migrated from? If yes, |
| 2614 | * flush the pages that were freed, so that they can merge and |
| 2615 | * compact_finished() can detect immediately if allocation |
| 2616 | * would succeed. |
| 2617 | */ |
Mel Gorman | 566e54e | 2019-03-05 15:44:32 -0800 | [diff] [blame] | 2618 | if (cc->order > 0 && last_migrated_pfn) { |
Vlastimil Babka | fdaf7f5 | 2014-12-10 15:43:34 -0800 | [diff] [blame] | 2619 | unsigned long current_block_start = |
Vlastimil Babka | 06b6640 | 2016-05-19 17:11:48 -0700 | [diff] [blame] | 2620 | block_start_pfn(cc->migrate_pfn, cc->order); |
Vlastimil Babka | fdaf7f5 | 2014-12-10 15:43:34 -0800 | [diff] [blame] | 2621 | |
Mel Gorman | 566e54e | 2019-03-05 15:44:32 -0800 | [diff] [blame] | 2622 | if (last_migrated_pfn < current_block_start) { |
Ingo Molnar | b01b214 | 2020-05-27 22:11:15 +0200 | [diff] [blame] | 2623 | lru_add_drain_cpu_zone(cc->zone); |
Vlastimil Babka | fdaf7f5 | 2014-12-10 15:43:34 -0800 | [diff] [blame] | 2624 | /* No more flushing until we migrate again */ |
Mel Gorman | 566e54e | 2019-03-05 15:44:32 -0800 | [diff] [blame] | 2625 | last_migrated_pfn = 0; |
Vlastimil Babka | fdaf7f5 | 2014-12-10 15:43:34 -0800 | [diff] [blame] | 2626 | } |
| 2627 | } |
| 2628 | |
Mel Gorman | 5e1f0f0 | 2019-03-05 15:45:41 -0800 | [diff] [blame] | 2629 | /* Stop if a page has been captured */ |
| 2630 | if (capc && capc->page) { |
| 2631 | ret = COMPACT_SUCCESS; |
| 2632 | break; |
| 2633 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 2634 | } |
| 2635 | |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 2636 | out: |
Vlastimil Babka | 6bace09 | 2014-12-10 15:43:31 -0800 | [diff] [blame] | 2637 | /* |
| 2638 | * Release free pages and update where the free scanner should restart, |
| 2639 | * so we don't leave any returned pages behind in the next attempt. |
| 2640 | */ |
| 2641 | if (cc->nr_freepages > 0) { |
| 2642 | unsigned long free_pfn = release_freepages(&cc->freepages); |
| 2643 | |
| 2644 | cc->nr_freepages = 0; |
| 2645 | VM_BUG_ON(free_pfn == 0); |
| 2646 | /* The cached pfn is always the first in a pageblock */ |
Vlastimil Babka | 06b6640 | 2016-05-19 17:11:48 -0700 | [diff] [blame] | 2647 | free_pfn = pageblock_start_pfn(free_pfn); |
Vlastimil Babka | 6bace09 | 2014-12-10 15:43:31 -0800 | [diff] [blame] | 2648 | /* |
| 2649 | * Only go back, not forward. The cached pfn might have been |
| 2650 | * already reset to zone end in compact_finished() |
| 2651 | */ |
Mel Gorman | 40cacbc | 2019-03-05 15:44:36 -0800 | [diff] [blame] | 2652 | if (free_pfn > cc->zone->compact_cached_free_pfn) |
| 2653 | cc->zone->compact_cached_free_pfn = free_pfn; |
Vlastimil Babka | 6bace09 | 2014-12-10 15:43:31 -0800 | [diff] [blame] | 2654 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 2655 | |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 2656 | #ifdef CONFIG_AMLOGIC_CMA |
| 2657 | __clear_bit(FORBID_TO_CMA_BIT, &cc->total_migrate_scanned); |
| 2658 | #endif |
David Rientjes | 7f354a5 | 2017-02-22 15:44:50 -0800 | [diff] [blame] | 2659 | count_compact_events(COMPACTMIGRATE_SCANNED, cc->total_migrate_scanned); |
| 2660 | count_compact_events(COMPACTFREE_SCANNED, cc->total_free_scanned); |
| 2661 | |
Robin Hsu | 1eeadb4 | 2022-05-13 10:19:50 +0800 | [diff] [blame] | 2662 | trace_android_vh_mm_compaction_end(cc, vendor_ret); |
Joonsoo Kim | 16c4a09 | 2015-02-11 15:27:01 -0800 | [diff] [blame] | 2663 | trace_mm_compaction_end(start_pfn, cc->migrate_pfn, |
| 2664 | cc->free_pfn, end_pfn, sync, ret); |
Mel Gorman | 0eb927c | 2014-01-21 15:51:05 -0800 | [diff] [blame] | 2665 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 2666 | return ret; |
| 2667 | } |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 2668 | |
Michal Hocko | ea7ab98 | 2016-05-20 16:56:38 -0700 | [diff] [blame] | 2669 | static enum compact_result compact_zone_order(struct zone *zone, int order, |
Vlastimil Babka | c3486f5 | 2016-07-28 15:49:30 -0700 | [diff] [blame] | 2670 | gfp_t gfp_mask, enum compact_priority prio, |
Joonsoo Kim | 97a225e | 2020-06-03 15:59:01 -0700 | [diff] [blame] | 2671 | unsigned int alloc_flags, int highest_zoneidx, |
Mel Gorman | 5e1f0f0 | 2019-03-05 15:45:41 -0800 | [diff] [blame] | 2672 | struct page **capture) |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 2673 | { |
Michal Hocko | ea7ab98 | 2016-05-20 16:56:38 -0700 | [diff] [blame] | 2674 | enum compact_result ret; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 2675 | struct compact_control cc = { |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 2676 | .order = order, |
Mel Gorman | dbe2d4e | 2019-03-05 15:45:31 -0800 | [diff] [blame] | 2677 | .search_order = order, |
David Rientjes | 6d7ce55 | 2014-10-09 15:27:27 -0700 | [diff] [blame] | 2678 | .gfp_mask = gfp_mask, |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 2679 | .zone = zone, |
Vlastimil Babka | a5508cd | 2016-07-28 15:49:28 -0700 | [diff] [blame] | 2680 | .mode = (prio == COMPACT_PRIO_ASYNC) ? |
| 2681 | MIGRATE_ASYNC : MIGRATE_SYNC_LIGHT, |
Vlastimil Babka | ebff398 | 2014-12-10 15:43:22 -0800 | [diff] [blame] | 2682 | .alloc_flags = alloc_flags, |
Joonsoo Kim | 97a225e | 2020-06-03 15:59:01 -0700 | [diff] [blame] | 2683 | .highest_zoneidx = highest_zoneidx, |
Vlastimil Babka | accf624 | 2016-03-17 14:18:15 -0700 | [diff] [blame] | 2684 | .direct_compaction = true, |
Vlastimil Babka | a8e025e | 2016-10-07 16:57:47 -0700 | [diff] [blame] | 2685 | .whole_zone = (prio == MIN_COMPACT_PRIORITY), |
Vlastimil Babka | 9f7e338 | 2016-10-07 17:00:37 -0700 | [diff] [blame] | 2686 | .ignore_skip_hint = (prio == MIN_COMPACT_PRIORITY), |
| 2687 | .ignore_block_suitable = (prio == MIN_COMPACT_PRIORITY) |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 2688 | }; |
Mel Gorman | 5e1f0f0 | 2019-03-05 15:45:41 -0800 | [diff] [blame] | 2689 | struct capture_control capc = { |
| 2690 | .cc = &cc, |
| 2691 | .page = NULL, |
| 2692 | }; |
| 2693 | |
Vlastimil Babka | b9e20f0 | 2020-06-25 20:29:24 -0700 | [diff] [blame] | 2694 | /* |
| 2695 | * Make sure the structs are really initialized before we expose the |
| 2696 | * capture control, in case we are interrupted and the interrupt handler |
| 2697 | * frees a page. |
| 2698 | */ |
| 2699 | barrier(); |
| 2700 | WRITE_ONCE(current->capture_control, &capc); |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 2701 | |
Mel Gorman | 5e1f0f0 | 2019-03-05 15:45:41 -0800 | [diff] [blame] | 2702 | ret = compact_zone(&cc, &capc); |
Shaohua Li | e64c523 | 2012-10-08 16:32:27 -0700 | [diff] [blame] | 2703 | |
| 2704 | VM_BUG_ON(!list_empty(&cc.freepages)); |
| 2705 | VM_BUG_ON(!list_empty(&cc.migratepages)); |
| 2706 | |
Vlastimil Babka | b9e20f0 | 2020-06-25 20:29:24 -0700 | [diff] [blame] | 2707 | /* |
| 2708 | * Make sure we hide capture control first before we read the captured |
| 2709 | * page pointer, otherwise an interrupt could free and capture a page |
| 2710 | * and we would leak it. |
| 2711 | */ |
| 2712 | WRITE_ONCE(current->capture_control, NULL); |
| 2713 | *capture = READ_ONCE(capc.page); |
Charan Teja Reddy | 06dac2f | 2021-05-04 18:36:51 -0700 | [diff] [blame] | 2714 | /* |
| 2715 | * Technically, it is also possible that compaction is skipped but |
| 2716 | * the page is still captured out of luck(IRQ came and freed the page). |
| 2717 | * Returning COMPACT_SUCCESS in such cases helps in properly accounting |
| 2718 | * the COMPACT[STALL|FAIL] when compaction is skipped. |
| 2719 | */ |
| 2720 | if (*capture) |
| 2721 | ret = COMPACT_SUCCESS; |
Mel Gorman | 5e1f0f0 | 2019-03-05 15:45:41 -0800 | [diff] [blame] | 2722 | |
Shaohua Li | e64c523 | 2012-10-08 16:32:27 -0700 | [diff] [blame] | 2723 | return ret; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 2724 | } |
| 2725 | |
Mel Gorman | 5e77190 | 2010-05-24 14:32:31 -0700 | [diff] [blame] | 2726 | int sysctl_extfrag_threshold = 500; |
| 2727 | |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 2728 | /** |
| 2729 | * try_to_compact_pages - Direct compact to satisfy a high-order allocation |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 2730 | * @gfp_mask: The GFP mask of the current allocation |
Vlastimil Babka | 1a6d53a | 2015-02-11 15:25:44 -0800 | [diff] [blame] | 2731 | * @order: The order of the current allocation |
| 2732 | * @alloc_flags: The allocation flags of the current allocation |
| 2733 | * @ac: The context of current allocation |
Yang Shi | 112d2d2 | 2018-01-31 16:20:23 -0800 | [diff] [blame] | 2734 | * @prio: Determines how hard direct compaction should try to succeed |
Vlastimil Babka | 6467552 | 2020-04-01 21:10:35 -0700 | [diff] [blame] | 2735 | * @capture: Pointer to free page created by compaction will be stored here |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 2736 | * |
| 2737 | * This is the main entry point for direct page compaction. |
| 2738 | */ |
Michal Hocko | ea7ab98 | 2016-05-20 16:56:38 -0700 | [diff] [blame] | 2739 | enum compact_result try_to_compact_pages(gfp_t gfp_mask, unsigned int order, |
Mel Gorman | c603844 | 2016-05-19 17:13:38 -0700 | [diff] [blame] | 2740 | unsigned int alloc_flags, const struct alloc_context *ac, |
Mel Gorman | 5e1f0f0 | 2019-03-05 15:45:41 -0800 | [diff] [blame] | 2741 | enum compact_priority prio, struct page **capture) |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 2742 | { |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 2743 | int may_perform_io = gfp_mask & __GFP_IO; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 2744 | struct zoneref *z; |
| 2745 | struct zone *zone; |
Michal Hocko | 1d4746d | 2016-05-20 16:56:44 -0700 | [diff] [blame] | 2746 | enum compact_result rc = COMPACT_SKIPPED; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 2747 | |
Michal Hocko | 73e64c5 | 2016-12-14 15:04:07 -0800 | [diff] [blame] | 2748 | /* |
| 2749 | * Check if the GFP flags allow compaction - GFP_NOIO is really |
| 2750 | * tricky context because the migration might require IO |
| 2751 | */ |
| 2752 | if (!may_perform_io) |
Vlastimil Babka | 53853e2 | 2014-10-09 15:27:02 -0700 | [diff] [blame] | 2753 | return COMPACT_SKIPPED; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 2754 | |
Vlastimil Babka | a5508cd | 2016-07-28 15:49:28 -0700 | [diff] [blame] | 2755 | trace_mm_compaction_try_to_compact_pages(order, gfp_mask, prio); |
Joonsoo Kim | 837d026 | 2015-02-11 15:27:06 -0800 | [diff] [blame] | 2756 | |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 2757 | /* Compact each zone in the list */ |
Joonsoo Kim | 97a225e | 2020-06-03 15:59:01 -0700 | [diff] [blame] | 2758 | for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, |
| 2759 | ac->highest_zoneidx, ac->nodemask) { |
Michal Hocko | ea7ab98 | 2016-05-20 16:56:38 -0700 | [diff] [blame] | 2760 | enum compact_result status; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 2761 | |
Vlastimil Babka | a8e025e | 2016-10-07 16:57:47 -0700 | [diff] [blame] | 2762 | if (prio > MIN_COMPACT_PRIORITY |
| 2763 | && compaction_deferred(zone, order)) { |
Michal Hocko | 1d4746d | 2016-05-20 16:56:44 -0700 | [diff] [blame] | 2764 | rc = max_t(enum compact_result, COMPACT_DEFERRED, rc); |
Vlastimil Babka | 53853e2 | 2014-10-09 15:27:02 -0700 | [diff] [blame] | 2765 | continue; |
Michal Hocko | 1d4746d | 2016-05-20 16:56:44 -0700 | [diff] [blame] | 2766 | } |
Vlastimil Babka | 53853e2 | 2014-10-09 15:27:02 -0700 | [diff] [blame] | 2767 | |
Vlastimil Babka | a5508cd | 2016-07-28 15:49:28 -0700 | [diff] [blame] | 2768 | status = compact_zone_order(zone, order, gfp_mask, prio, |
Joonsoo Kim | 97a225e | 2020-06-03 15:59:01 -0700 | [diff] [blame] | 2769 | alloc_flags, ac->highest_zoneidx, capture); |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 2770 | rc = max(status, rc); |
| 2771 | |
Vlastimil Babka | 7ceb009 | 2016-10-07 16:57:44 -0700 | [diff] [blame] | 2772 | /* The allocation should succeed, stop compacting */ |
| 2773 | if (status == COMPACT_SUCCESS) { |
Vlastimil Babka | 53853e2 | 2014-10-09 15:27:02 -0700 | [diff] [blame] | 2774 | /* |
| 2775 | * We think the allocation will succeed in this zone, |
| 2776 | * but it is not certain, hence the false. The caller |
| 2777 | * will repeat this with true if allocation indeed |
| 2778 | * succeeds in this zone. |
| 2779 | */ |
| 2780 | compaction_defer_reset(zone, order, false); |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame] | 2781 | |
Vlastimil Babka | c3486f5 | 2016-07-28 15:49:30 -0700 | [diff] [blame] | 2782 | break; |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame] | 2783 | } |
| 2784 | |
Vlastimil Babka | a5508cd | 2016-07-28 15:49:28 -0700 | [diff] [blame] | 2785 | if (prio != COMPACT_PRIO_ASYNC && (status == COMPACT_COMPLETE || |
Vlastimil Babka | c3486f5 | 2016-07-28 15:49:30 -0700 | [diff] [blame] | 2786 | status == COMPACT_PARTIAL_SKIPPED)) |
Vlastimil Babka | 53853e2 | 2014-10-09 15:27:02 -0700 | [diff] [blame] | 2787 | /* |
| 2788 | * We think that allocation won't succeed in this zone |
| 2789 | * so we defer compaction there. If it ends up |
| 2790 | * succeeding after all, it will be reset. |
| 2791 | */ |
| 2792 | defer_compaction(zone, order); |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame] | 2793 | |
| 2794 | /* |
| 2795 | * We might have stopped compacting due to need_resched() in |
| 2796 | * async compaction, or due to a fatal signal detected. In that |
Vlastimil Babka | c3486f5 | 2016-07-28 15:49:30 -0700 | [diff] [blame] | 2797 | * case do not try further zones |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame] | 2798 | */ |
Vlastimil Babka | c3486f5 | 2016-07-28 15:49:30 -0700 | [diff] [blame] | 2799 | if ((prio == COMPACT_PRIO_ASYNC && need_resched()) |
| 2800 | || fatal_signal_pending(current)) |
| 2801 | break; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 2802 | } |
| 2803 | |
| 2804 | return rc; |
| 2805 | } |
| 2806 | |
Nitin Gupta | facdaa9 | 2020-08-11 18:31:00 -0700 | [diff] [blame] | 2807 | /* |
| 2808 | * Compact all zones within a node till each zone's fragmentation score |
| 2809 | * reaches within proactive compaction thresholds (as determined by the |
| 2810 | * proactiveness tunable). |
| 2811 | * |
| 2812 | * It is possible that the function returns before reaching score targets |
| 2813 | * due to various back-off conditions, such as, contention on per-node or |
| 2814 | * per-zone locks. |
| 2815 | */ |
| 2816 | static void proactive_compact_node(pg_data_t *pgdat) |
| 2817 | { |
| 2818 | int zoneid; |
| 2819 | struct zone *zone; |
| 2820 | struct compact_control cc = { |
| 2821 | .order = -1, |
| 2822 | .mode = MIGRATE_SYNC_LIGHT, |
| 2823 | .ignore_skip_hint = true, |
| 2824 | .whole_zone = true, |
| 2825 | .gfp_mask = GFP_KERNEL, |
| 2826 | .proactive_compaction = true, |
| 2827 | }; |
| 2828 | |
| 2829 | for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { |
| 2830 | zone = &pgdat->node_zones[zoneid]; |
| 2831 | if (!populated_zone(zone)) |
| 2832 | continue; |
| 2833 | |
| 2834 | cc.zone = zone; |
| 2835 | |
| 2836 | compact_zone(&cc, NULL); |
| 2837 | |
| 2838 | VM_BUG_ON(!list_empty(&cc.freepages)); |
| 2839 | VM_BUG_ON(!list_empty(&cc.migratepages)); |
| 2840 | } |
| 2841 | } |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 2842 | |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 2843 | /* Compact all zones within a node */ |
Andrew Morton | 7103f16 | 2013-02-22 16:32:33 -0800 | [diff] [blame] | 2844 | static void compact_node(int nid) |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 2845 | { |
Vlastimil Babka | 791cae9 | 2016-10-07 16:57:38 -0700 | [diff] [blame] | 2846 | pg_data_t *pgdat = NODE_DATA(nid); |
| 2847 | int zoneid; |
| 2848 | struct zone *zone; |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 2849 | struct compact_control cc = { |
| 2850 | .order = -1, |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 2851 | .mode = MIGRATE_SYNC, |
David Rientjes | 91ca918 | 2014-04-03 14:47:23 -0700 | [diff] [blame] | 2852 | .ignore_skip_hint = true, |
Vlastimil Babka | 06ed299 | 2016-10-07 16:57:35 -0700 | [diff] [blame] | 2853 | .whole_zone = true, |
Michal Hocko | 73e64c5 | 2016-12-14 15:04:07 -0800 | [diff] [blame] | 2854 | .gfp_mask = GFP_KERNEL, |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 2855 | }; |
| 2856 | |
Vlastimil Babka | 791cae9 | 2016-10-07 16:57:38 -0700 | [diff] [blame] | 2857 | |
| 2858 | for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { |
| 2859 | |
| 2860 | zone = &pgdat->node_zones[zoneid]; |
| 2861 | if (!populated_zone(zone)) |
| 2862 | continue; |
| 2863 | |
Vlastimil Babka | 791cae9 | 2016-10-07 16:57:38 -0700 | [diff] [blame] | 2864 | cc.zone = zone; |
Vlastimil Babka | 791cae9 | 2016-10-07 16:57:38 -0700 | [diff] [blame] | 2865 | |
Mel Gorman | 5e1f0f0 | 2019-03-05 15:45:41 -0800 | [diff] [blame] | 2866 | compact_zone(&cc, NULL); |
Vlastimil Babka | 791cae9 | 2016-10-07 16:57:38 -0700 | [diff] [blame] | 2867 | |
| 2868 | VM_BUG_ON(!list_empty(&cc.freepages)); |
| 2869 | VM_BUG_ON(!list_empty(&cc.migratepages)); |
| 2870 | } |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 2871 | } |
| 2872 | |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 2873 | /* Compact all nodes in the system */ |
Jason Liu | 7964c06 | 2013-01-11 14:31:47 -0800 | [diff] [blame] | 2874 | static void compact_nodes(void) |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 2875 | { |
| 2876 | int nid; |
| 2877 | |
Hugh Dickins | 8575ec2 | 2012-03-21 16:33:53 -0700 | [diff] [blame] | 2878 | /* Flush pending updates to the LRU lists */ |
| 2879 | lru_add_drain_all(); |
| 2880 | |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 2881 | for_each_online_node(nid) |
| 2882 | compact_node(nid); |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 2883 | } |
| 2884 | |
Yaowei Bai | fec4eb2 | 2016-01-14 15:20:09 -0800 | [diff] [blame] | 2885 | /* |
Nitin Gupta | facdaa9 | 2020-08-11 18:31:00 -0700 | [diff] [blame] | 2886 | * Tunable for proactive compaction. It determines how |
| 2887 | * aggressively the kernel should compact memory in the |
| 2888 | * background. It takes values in the range [0, 100]. |
| 2889 | */ |
Nitin Gupta | d34c0a7 | 2020-08-11 18:31:07 -0700 | [diff] [blame] | 2890 | unsigned int __read_mostly sysctl_compaction_proactiveness = 20; |
Nitin Gupta | facdaa9 | 2020-08-11 18:31:00 -0700 | [diff] [blame] | 2891 | |
Charan Teja Reddy | 65d759c | 2021-09-02 14:59:59 -0700 | [diff] [blame] | 2892 | int compaction_proactiveness_sysctl_handler(struct ctl_table *table, int write, |
| 2893 | void *buffer, size_t *length, loff_t *ppos) |
| 2894 | { |
| 2895 | int rc, nid; |
| 2896 | |
| 2897 | rc = proc_dointvec_minmax(table, write, buffer, length, ppos); |
| 2898 | if (rc) |
| 2899 | return rc; |
| 2900 | |
| 2901 | if (write && sysctl_compaction_proactiveness) { |
| 2902 | for_each_online_node(nid) { |
| 2903 | pg_data_t *pgdat = NODE_DATA(nid); |
| 2904 | |
| 2905 | if (pgdat->proactive_compact_trigger) |
| 2906 | continue; |
| 2907 | |
| 2908 | pgdat->proactive_compact_trigger = true; |
| 2909 | wake_up_interruptible(&pgdat->kcompactd_wait); |
| 2910 | } |
| 2911 | } |
| 2912 | |
| 2913 | return 0; |
| 2914 | } |
| 2915 | |
Nitin Gupta | facdaa9 | 2020-08-11 18:31:00 -0700 | [diff] [blame] | 2916 | /* |
Yaowei Bai | fec4eb2 | 2016-01-14 15:20:09 -0800 | [diff] [blame] | 2917 | * This is the entry point for compacting all nodes via |
| 2918 | * /proc/sys/vm/compact_memory |
| 2919 | */ |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 2920 | int sysctl_compaction_handler(struct ctl_table *table, int write, |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 2921 | void *buffer, size_t *length, loff_t *ppos) |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 2922 | { |
| 2923 | if (write) |
Jason Liu | 7964c06 | 2013-01-11 14:31:47 -0800 | [diff] [blame] | 2924 | compact_nodes(); |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 2925 | |
| 2926 | return 0; |
| 2927 | } |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 2928 | |
| 2929 | #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) |
YueHaibing | 17adb23 | 2021-06-30 18:50:48 -0700 | [diff] [blame] | 2930 | static ssize_t compact_store(struct device *dev, |
| 2931 | struct device_attribute *attr, |
| 2932 | const char *buf, size_t count) |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 2933 | { |
Hugh Dickins | 8575ec2 | 2012-03-21 16:33:53 -0700 | [diff] [blame] | 2934 | int nid = dev->id; |
| 2935 | |
| 2936 | if (nid >= 0 && nid < nr_node_ids && node_online(nid)) { |
| 2937 | /* Flush pending updates to the LRU lists */ |
| 2938 | lru_add_drain_all(); |
| 2939 | |
| 2940 | compact_node(nid); |
| 2941 | } |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 2942 | |
| 2943 | return count; |
| 2944 | } |
YueHaibing | 17adb23 | 2021-06-30 18:50:48 -0700 | [diff] [blame] | 2945 | static DEVICE_ATTR_WO(compact); |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 2946 | |
| 2947 | int compaction_register_node(struct node *node) |
| 2948 | { |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 2949 | return device_create_file(&node->dev, &dev_attr_compact); |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 2950 | } |
| 2951 | |
| 2952 | void compaction_unregister_node(struct node *node) |
| 2953 | { |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 2954 | return device_remove_file(&node->dev, &dev_attr_compact); |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 2955 | } |
| 2956 | #endif /* CONFIG_SYSFS && CONFIG_NUMA */ |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 2957 | |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 2958 | static inline bool kcompactd_work_requested(pg_data_t *pgdat) |
| 2959 | { |
Charan Teja Reddy | 65d759c | 2021-09-02 14:59:59 -0700 | [diff] [blame] | 2960 | return pgdat->kcompactd_max_order > 0 || kthread_should_stop() || |
| 2961 | pgdat->proactive_compact_trigger; |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 2962 | } |
| 2963 | |
| 2964 | static bool kcompactd_node_suitable(pg_data_t *pgdat) |
| 2965 | { |
| 2966 | int zoneid; |
| 2967 | struct zone *zone; |
Joonsoo Kim | 97a225e | 2020-06-03 15:59:01 -0700 | [diff] [blame] | 2968 | enum zone_type highest_zoneidx = pgdat->kcompactd_highest_zoneidx; |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 2969 | |
Joonsoo Kim | 97a225e | 2020-06-03 15:59:01 -0700 | [diff] [blame] | 2970 | for (zoneid = 0; zoneid <= highest_zoneidx; zoneid++) { |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 2971 | zone = &pgdat->node_zones[zoneid]; |
| 2972 | |
| 2973 | if (!populated_zone(zone)) |
| 2974 | continue; |
| 2975 | |
| 2976 | if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0, |
Joonsoo Kim | 97a225e | 2020-06-03 15:59:01 -0700 | [diff] [blame] | 2977 | highest_zoneidx) == COMPACT_CONTINUE) |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 2978 | return true; |
| 2979 | } |
| 2980 | |
| 2981 | return false; |
| 2982 | } |
| 2983 | |
| 2984 | static void kcompactd_do_work(pg_data_t *pgdat) |
| 2985 | { |
| 2986 | /* |
| 2987 | * With no special task, compact all zones so that a page of requested |
| 2988 | * order is allocatable. |
| 2989 | */ |
| 2990 | int zoneid; |
| 2991 | struct zone *zone; |
| 2992 | struct compact_control cc = { |
| 2993 | .order = pgdat->kcompactd_max_order, |
Mel Gorman | dbe2d4e | 2019-03-05 15:45:31 -0800 | [diff] [blame] | 2994 | .search_order = pgdat->kcompactd_max_order, |
Joonsoo Kim | 97a225e | 2020-06-03 15:59:01 -0700 | [diff] [blame] | 2995 | .highest_zoneidx = pgdat->kcompactd_highest_zoneidx, |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 2996 | .mode = MIGRATE_SYNC_LIGHT, |
David Rientjes | a0647dc | 2017-11-17 15:26:27 -0800 | [diff] [blame] | 2997 | .ignore_skip_hint = false, |
Michal Hocko | 73e64c5 | 2016-12-14 15:04:07 -0800 | [diff] [blame] | 2998 | .gfp_mask = GFP_KERNEL, |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 2999 | }; |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 3000 | trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order, |
Joonsoo Kim | 97a225e | 2020-06-03 15:59:01 -0700 | [diff] [blame] | 3001 | cc.highest_zoneidx); |
David Rientjes | 7f354a5 | 2017-02-22 15:44:50 -0800 | [diff] [blame] | 3002 | count_compact_event(KCOMPACTD_WAKE); |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 3003 | |
Joonsoo Kim | 97a225e | 2020-06-03 15:59:01 -0700 | [diff] [blame] | 3004 | for (zoneid = 0; zoneid <= cc.highest_zoneidx; zoneid++) { |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 3005 | int status; |
| 3006 | |
| 3007 | zone = &pgdat->node_zones[zoneid]; |
| 3008 | if (!populated_zone(zone)) |
| 3009 | continue; |
| 3010 | |
| 3011 | if (compaction_deferred(zone, cc.order)) |
| 3012 | continue; |
| 3013 | |
| 3014 | if (compaction_suitable(zone, cc.order, 0, zoneid) != |
| 3015 | COMPACT_CONTINUE) |
| 3016 | continue; |
| 3017 | |
Vlastimil Babka | 172400c | 2016-05-05 16:22:32 -0700 | [diff] [blame] | 3018 | if (kthread_should_stop()) |
| 3019 | return; |
Yafang Shao | a94b525 | 2019-09-23 15:36:54 -0700 | [diff] [blame] | 3020 | |
| 3021 | cc.zone = zone; |
Mel Gorman | 5e1f0f0 | 2019-03-05 15:45:41 -0800 | [diff] [blame] | 3022 | status = compact_zone(&cc, NULL); |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 3023 | |
Vlastimil Babka | 7ceb009 | 2016-10-07 16:57:44 -0700 | [diff] [blame] | 3024 | if (status == COMPACT_SUCCESS) { |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 3025 | compaction_defer_reset(zone, cc.order, false); |
Michal Hocko | c8f7de0 | 2016-05-20 16:56:47 -0700 | [diff] [blame] | 3026 | } else if (status == COMPACT_PARTIAL_SKIPPED || status == COMPACT_COMPLETE) { |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 3027 | /* |
David Rientjes | bc3106b | 2018-04-05 16:24:02 -0700 | [diff] [blame] | 3028 | * Buddy pages may become stranded on pcps that could |
| 3029 | * otherwise coalesce on the zone's free area for |
| 3030 | * order >= cc.order. This is ratelimited by the |
| 3031 | * upcoming deferral. |
| 3032 | */ |
| 3033 | drain_all_pages(zone); |
| 3034 | |
| 3035 | /* |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 3036 | * We use sync migration mode here, so we defer like |
| 3037 | * sync direct compaction does. |
| 3038 | */ |
| 3039 | defer_compaction(zone, cc.order); |
| 3040 | } |
| 3041 | |
David Rientjes | 7f354a5 | 2017-02-22 15:44:50 -0800 | [diff] [blame] | 3042 | count_compact_events(KCOMPACTD_MIGRATE_SCANNED, |
| 3043 | cc.total_migrate_scanned); |
| 3044 | count_compact_events(KCOMPACTD_FREE_SCANNED, |
| 3045 | cc.total_free_scanned); |
| 3046 | |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 3047 | VM_BUG_ON(!list_empty(&cc.freepages)); |
| 3048 | VM_BUG_ON(!list_empty(&cc.migratepages)); |
| 3049 | } |
| 3050 | |
| 3051 | /* |
| 3052 | * Regardless of success, we are done until woken up next. But remember |
Joonsoo Kim | 97a225e | 2020-06-03 15:59:01 -0700 | [diff] [blame] | 3053 | * the requested order/highest_zoneidx in case it was higher/tighter |
| 3054 | * than our current ones |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 3055 | */ |
| 3056 | if (pgdat->kcompactd_max_order <= cc.order) |
| 3057 | pgdat->kcompactd_max_order = 0; |
Joonsoo Kim | 97a225e | 2020-06-03 15:59:01 -0700 | [diff] [blame] | 3058 | if (pgdat->kcompactd_highest_zoneidx >= cc.highest_zoneidx) |
| 3059 | pgdat->kcompactd_highest_zoneidx = pgdat->nr_zones - 1; |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 3060 | } |
| 3061 | |
Joonsoo Kim | 97a225e | 2020-06-03 15:59:01 -0700 | [diff] [blame] | 3062 | void wakeup_kcompactd(pg_data_t *pgdat, int order, int highest_zoneidx) |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 3063 | { |
| 3064 | if (!order) |
| 3065 | return; |
| 3066 | |
| 3067 | if (pgdat->kcompactd_max_order < order) |
| 3068 | pgdat->kcompactd_max_order = order; |
| 3069 | |
Joonsoo Kim | 97a225e | 2020-06-03 15:59:01 -0700 | [diff] [blame] | 3070 | if (pgdat->kcompactd_highest_zoneidx > highest_zoneidx) |
| 3071 | pgdat->kcompactd_highest_zoneidx = highest_zoneidx; |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 3072 | |
Davidlohr Bueso | 6818600 | 2017-10-03 16:15:03 -0700 | [diff] [blame] | 3073 | /* |
| 3074 | * Pairs with implicit barrier in wait_event_freezable() |
| 3075 | * such that wakeups are not missed. |
| 3076 | */ |
| 3077 | if (!wq_has_sleeper(&pgdat->kcompactd_wait)) |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 3078 | return; |
| 3079 | |
| 3080 | if (!kcompactd_node_suitable(pgdat)) |
| 3081 | return; |
| 3082 | |
| 3083 | trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order, |
Joonsoo Kim | 97a225e | 2020-06-03 15:59:01 -0700 | [diff] [blame] | 3084 | highest_zoneidx); |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 3085 | wake_up_interruptible(&pgdat->kcompactd_wait); |
| 3086 | } |
| 3087 | |
| 3088 | /* |
| 3089 | * The background compaction daemon, started as a kernel thread |
| 3090 | * from the init process. |
| 3091 | */ |
| 3092 | static int kcompactd(void *p) |
| 3093 | { |
Zhiyuan Dai | 68d68ff | 2021-05-04 18:40:12 -0700 | [diff] [blame] | 3094 | pg_data_t *pgdat = (pg_data_t *)p; |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 3095 | struct task_struct *tsk = current; |
Charan Teja Reddy | e1e92bf | 2021-09-02 14:59:56 -0700 | [diff] [blame] | 3096 | long default_timeout = msecs_to_jiffies(HPAGE_FRAG_CHECK_INTERVAL_MSEC); |
| 3097 | long timeout = default_timeout; |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 3098 | |
| 3099 | const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); |
| 3100 | |
| 3101 | if (!cpumask_empty(cpumask)) |
| 3102 | set_cpus_allowed_ptr(tsk, cpumask); |
| 3103 | |
| 3104 | set_freezable(); |
| 3105 | |
| 3106 | pgdat->kcompactd_max_order = 0; |
Joonsoo Kim | 97a225e | 2020-06-03 15:59:01 -0700 | [diff] [blame] | 3107 | pgdat->kcompactd_highest_zoneidx = pgdat->nr_zones - 1; |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 3108 | |
| 3109 | while (!kthread_should_stop()) { |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 3110 | unsigned long pflags; |
| 3111 | |
Charan Teja Reddy | 65d759c | 2021-09-02 14:59:59 -0700 | [diff] [blame] | 3112 | /* |
| 3113 | * Avoid the unnecessary wakeup for proactive compaction |
| 3114 | * when it is disabled. |
| 3115 | */ |
| 3116 | if (!sysctl_compaction_proactiveness) |
| 3117 | timeout = MAX_SCHEDULE_TIMEOUT; |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 3118 | trace_mm_compaction_kcompactd_sleep(pgdat->node_id); |
Nitin Gupta | facdaa9 | 2020-08-11 18:31:00 -0700 | [diff] [blame] | 3119 | if (wait_event_freezable_timeout(pgdat->kcompactd_wait, |
Charan Teja Reddy | 65d759c | 2021-09-02 14:59:59 -0700 | [diff] [blame] | 3120 | kcompactd_work_requested(pgdat), timeout) && |
| 3121 | !pgdat->proactive_compact_trigger) { |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 3122 | |
Nitin Gupta | facdaa9 | 2020-08-11 18:31:00 -0700 | [diff] [blame] | 3123 | psi_memstall_enter(&pflags); |
| 3124 | kcompactd_do_work(pgdat); |
| 3125 | psi_memstall_leave(&pflags); |
Charan Teja Reddy | e1e92bf | 2021-09-02 14:59:56 -0700 | [diff] [blame] | 3126 | /* |
| 3127 | * Reset the timeout value. The defer timeout from |
| 3128 | * proactive compaction is lost here but that is fine |
| 3129 | * as the condition of the zone changing substantionally |
| 3130 | * then carrying on with the previous defer interval is |
| 3131 | * not useful. |
| 3132 | */ |
| 3133 | timeout = default_timeout; |
Nitin Gupta | facdaa9 | 2020-08-11 18:31:00 -0700 | [diff] [blame] | 3134 | continue; |
| 3135 | } |
| 3136 | |
Charan Teja Reddy | e1e92bf | 2021-09-02 14:59:56 -0700 | [diff] [blame] | 3137 | /* |
| 3138 | * Start the proactive work with default timeout. Based |
| 3139 | * on the fragmentation score, this timeout is updated. |
| 3140 | */ |
| 3141 | timeout = default_timeout; |
Nitin Gupta | facdaa9 | 2020-08-11 18:31:00 -0700 | [diff] [blame] | 3142 | if (should_proactive_compact_node(pgdat)) { |
| 3143 | unsigned int prev_score, score; |
| 3144 | |
Nitin Gupta | facdaa9 | 2020-08-11 18:31:00 -0700 | [diff] [blame] | 3145 | prev_score = fragmentation_score_node(pgdat); |
| 3146 | proactive_compact_node(pgdat); |
| 3147 | score = fragmentation_score_node(pgdat); |
| 3148 | /* |
| 3149 | * Defer proactive compaction if the fragmentation |
| 3150 | * score did not go down i.e. no progress made. |
| 3151 | */ |
Charan Teja Reddy | e1e92bf | 2021-09-02 14:59:56 -0700 | [diff] [blame] | 3152 | if (unlikely(score >= prev_score)) |
| 3153 | timeout = |
| 3154 | default_timeout << COMPACT_MAX_DEFER_SHIFT; |
Nitin Gupta | facdaa9 | 2020-08-11 18:31:00 -0700 | [diff] [blame] | 3155 | } |
Charan Teja Reddy | 65d759c | 2021-09-02 14:59:59 -0700 | [diff] [blame] | 3156 | if (unlikely(pgdat->proactive_compact_trigger)) |
| 3157 | pgdat->proactive_compact_trigger = false; |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 3158 | } |
| 3159 | |
| 3160 | return 0; |
| 3161 | } |
| 3162 | |
| 3163 | /* |
| 3164 | * This kcompactd start function will be called by init and node-hot-add. |
| 3165 | * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added. |
| 3166 | */ |
| 3167 | int kcompactd_run(int nid) |
| 3168 | { |
| 3169 | pg_data_t *pgdat = NODE_DATA(nid); |
| 3170 | int ret = 0; |
| 3171 | |
| 3172 | if (pgdat->kcompactd) |
| 3173 | return 0; |
| 3174 | |
| 3175 | pgdat->kcompactd = kthread_run(kcompactd, pgdat, "kcompactd%d", nid); |
| 3176 | if (IS_ERR(pgdat->kcompactd)) { |
| 3177 | pr_err("Failed to start kcompactd on node %d\n", nid); |
| 3178 | ret = PTR_ERR(pgdat->kcompactd); |
| 3179 | pgdat->kcompactd = NULL; |
| 3180 | } |
| 3181 | return ret; |
| 3182 | } |
| 3183 | |
| 3184 | /* |
| 3185 | * Called by memory hotplug when all memory in a node is offlined. Caller must |
| 3186 | * hold mem_hotplug_begin/end(). |
| 3187 | */ |
| 3188 | void kcompactd_stop(int nid) |
| 3189 | { |
| 3190 | struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd; |
| 3191 | |
| 3192 | if (kcompactd) { |
| 3193 | kthread_stop(kcompactd); |
| 3194 | NODE_DATA(nid)->kcompactd = NULL; |
| 3195 | } |
| 3196 | } |
| 3197 | |
| 3198 | /* |
| 3199 | * It's optimal to keep kcompactd on the same CPUs as their memory, but |
| 3200 | * not required for correctness. So if the last cpu in a node goes |
| 3201 | * away, we get changed to run anywhere: as the first one comes back, |
| 3202 | * restore their cpu bindings. |
| 3203 | */ |
Anna-Maria Gleixner | e46b1db | 2016-11-27 00:13:42 +0100 | [diff] [blame] | 3204 | static int kcompactd_cpu_online(unsigned int cpu) |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 3205 | { |
| 3206 | int nid; |
| 3207 | |
Anna-Maria Gleixner | e46b1db | 2016-11-27 00:13:42 +0100 | [diff] [blame] | 3208 | for_each_node_state(nid, N_MEMORY) { |
| 3209 | pg_data_t *pgdat = NODE_DATA(nid); |
| 3210 | const struct cpumask *mask; |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 3211 | |
Anna-Maria Gleixner | e46b1db | 2016-11-27 00:13:42 +0100 | [diff] [blame] | 3212 | mask = cpumask_of_node(pgdat->node_id); |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 3213 | |
Anna-Maria Gleixner | e46b1db | 2016-11-27 00:13:42 +0100 | [diff] [blame] | 3214 | if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) |
| 3215 | /* One of our CPUs online: restore mask */ |
| 3216 | set_cpus_allowed_ptr(pgdat->kcompactd, mask); |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 3217 | } |
Anna-Maria Gleixner | e46b1db | 2016-11-27 00:13:42 +0100 | [diff] [blame] | 3218 | return 0; |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 3219 | } |
| 3220 | |
| 3221 | static int __init kcompactd_init(void) |
| 3222 | { |
| 3223 | int nid; |
Anna-Maria Gleixner | e46b1db | 2016-11-27 00:13:42 +0100 | [diff] [blame] | 3224 | int ret; |
| 3225 | |
| 3226 | ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, |
| 3227 | "mm/compaction:online", |
| 3228 | kcompactd_cpu_online, NULL); |
| 3229 | if (ret < 0) { |
| 3230 | pr_err("kcompactd: failed to register hotplug callbacks.\n"); |
| 3231 | return ret; |
| 3232 | } |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 3233 | |
| 3234 | for_each_node_state(nid, N_MEMORY) |
| 3235 | kcompactd_run(nid); |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 3236 | return 0; |
| 3237 | } |
| 3238 | subsys_initcall(kcompactd_init) |
| 3239 | |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 3240 | #endif /* CONFIG_COMPACTION */ |