Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/mm/compaction.c |
| 3 | * |
| 4 | * Memory compaction for the reduction of external fragmentation. Note that |
| 5 | * this heavily depends upon page migration to do all the real heavy |
| 6 | * lifting |
| 7 | * |
| 8 | * Copyright IBM Corp. 2007-2010 Mel Gorman <mel@csn.ul.ie> |
| 9 | */ |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 10 | #include <linux/cpu.h> |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 11 | #include <linux/swap.h> |
| 12 | #include <linux/migrate.h> |
| 13 | #include <linux/compaction.h> |
| 14 | #include <linux/mm_inline.h> |
| 15 | #include <linux/backing-dev.h> |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 16 | #include <linux/sysctl.h> |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 17 | #include <linux/sysfs.h> |
Minchan Kim | 194159f | 2013-02-22 16:33:58 -0800 | [diff] [blame] | 18 | #include <linux/page-isolation.h> |
Andrey Ryabinin | b8c73fc | 2015-02-13 14:39:28 -0800 | [diff] [blame] | 19 | #include <linux/kasan.h> |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 20 | #include <linux/kthread.h> |
| 21 | #include <linux/freezer.h> |
Joonsoo Kim | 83358ec | 2016-07-26 15:23:43 -0700 | [diff] [blame] | 22 | #include <linux/page_owner.h> |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 23 | #include "internal.h" |
| 24 | |
Minchan Kim | 010fc29 | 2012-12-20 15:05:06 -0800 | [diff] [blame] | 25 | #ifdef CONFIG_COMPACTION |
| 26 | static inline void count_compact_event(enum vm_event_item item) |
| 27 | { |
| 28 | count_vm_event(item); |
| 29 | } |
| 30 | |
| 31 | static inline void count_compact_events(enum vm_event_item item, long delta) |
| 32 | { |
| 33 | count_vm_events(item, delta); |
| 34 | } |
| 35 | #else |
| 36 | #define count_compact_event(item) do { } while (0) |
| 37 | #define count_compact_events(item, delta) do { } while (0) |
| 38 | #endif |
| 39 | |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 40 | #if defined CONFIG_COMPACTION || defined CONFIG_CMA |
| 41 | |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 42 | #define CREATE_TRACE_POINTS |
| 43 | #include <trace/events/compaction.h> |
| 44 | |
Vlastimil Babka | 06b6640 | 2016-05-19 17:11:48 -0700 | [diff] [blame] | 45 | #define block_start_pfn(pfn, order) round_down(pfn, 1UL << (order)) |
| 46 | #define block_end_pfn(pfn, order) ALIGN((pfn) + 1, 1UL << (order)) |
| 47 | #define pageblock_start_pfn(pfn) block_start_pfn(pfn, pageblock_order) |
| 48 | #define pageblock_end_pfn(pfn) block_end_pfn(pfn, pageblock_order) |
| 49 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 50 | static unsigned long release_freepages(struct list_head *freelist) |
| 51 | { |
| 52 | struct page *page, *next; |
Vlastimil Babka | 6bace09 | 2014-12-10 15:43:31 -0800 | [diff] [blame] | 53 | unsigned long high_pfn = 0; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 54 | |
| 55 | list_for_each_entry_safe(page, next, freelist, lru) { |
Vlastimil Babka | 6bace09 | 2014-12-10 15:43:31 -0800 | [diff] [blame] | 56 | unsigned long pfn = page_to_pfn(page); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 57 | list_del(&page->lru); |
| 58 | __free_page(page); |
Vlastimil Babka | 6bace09 | 2014-12-10 15:43:31 -0800 | [diff] [blame] | 59 | if (pfn > high_pfn) |
| 60 | high_pfn = pfn; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Vlastimil Babka | 6bace09 | 2014-12-10 15:43:31 -0800 | [diff] [blame] | 63 | return high_pfn; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 66 | static void map_pages(struct list_head *list) |
| 67 | { |
Joonsoo Kim | 66c6422 | 2016-07-26 15:23:40 -0700 | [diff] [blame] | 68 | unsigned int i, order, nr_pages; |
| 69 | struct page *page, *next; |
| 70 | LIST_HEAD(tmp_list); |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 71 | |
Joonsoo Kim | 66c6422 | 2016-07-26 15:23:40 -0700 | [diff] [blame] | 72 | list_for_each_entry_safe(page, next, list, lru) { |
| 73 | list_del(&page->lru); |
| 74 | |
| 75 | order = page_private(page); |
| 76 | nr_pages = 1 << order; |
| 77 | set_page_private(page, 0); |
| 78 | set_page_refcounted(page); |
| 79 | |
| 80 | arch_alloc_page(page, order); |
| 81 | kernel_map_pages(page, nr_pages, 1); |
| 82 | kasan_alloc_pages(page, order); |
Joonsoo Kim | 83358ec | 2016-07-26 15:23:43 -0700 | [diff] [blame] | 83 | |
| 84 | set_page_owner(page, order, __GFP_MOVABLE); |
Joonsoo Kim | 66c6422 | 2016-07-26 15:23:40 -0700 | [diff] [blame] | 85 | if (order) |
| 86 | split_page(page, order); |
| 87 | |
| 88 | for (i = 0; i < nr_pages; i++) { |
| 89 | list_add(&page->lru, &tmp_list); |
| 90 | page++; |
| 91 | } |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 92 | } |
Joonsoo Kim | 66c6422 | 2016-07-26 15:23:40 -0700 | [diff] [blame] | 93 | |
| 94 | list_splice(&tmp_list, list); |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Michal Nazarewicz | 47118af | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 97 | static inline bool migrate_async_suitable(int migratetype) |
| 98 | { |
| 99 | return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE; |
| 100 | } |
| 101 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 102 | #ifdef CONFIG_COMPACTION |
Joonsoo Kim | 24e2716 | 2015-02-11 15:27:09 -0800 | [diff] [blame] | 103 | |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 104 | int PageMovable(struct page *page) |
| 105 | { |
| 106 | struct address_space *mapping; |
| 107 | |
| 108 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
| 109 | if (!__PageMovable(page)) |
| 110 | return 0; |
| 111 | |
| 112 | mapping = page_mapping(page); |
| 113 | if (mapping && mapping->a_ops && mapping->a_ops->isolate_page) |
| 114 | return 1; |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | EXPORT_SYMBOL(PageMovable); |
| 119 | |
| 120 | void __SetPageMovable(struct page *page, struct address_space *mapping) |
| 121 | { |
| 122 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
| 123 | VM_BUG_ON_PAGE((unsigned long)mapping & PAGE_MAPPING_MOVABLE, page); |
| 124 | page->mapping = (void *)((unsigned long)mapping | PAGE_MAPPING_MOVABLE); |
| 125 | } |
| 126 | EXPORT_SYMBOL(__SetPageMovable); |
| 127 | |
| 128 | void __ClearPageMovable(struct page *page) |
| 129 | { |
| 130 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
| 131 | VM_BUG_ON_PAGE(!PageMovable(page), page); |
| 132 | /* |
| 133 | * Clear registered address_space val with keeping PAGE_MAPPING_MOVABLE |
| 134 | * flag so that VM can catch up released page by driver after isolation. |
| 135 | * With it, VM migration doesn't try to put it back. |
| 136 | */ |
| 137 | page->mapping = (void *)((unsigned long)page->mapping & |
| 138 | PAGE_MAPPING_MOVABLE); |
| 139 | } |
| 140 | EXPORT_SYMBOL(__ClearPageMovable); |
| 141 | |
Joonsoo Kim | 24e2716 | 2015-02-11 15:27:09 -0800 | [diff] [blame] | 142 | /* Do not skip compaction more than 64 times */ |
| 143 | #define COMPACT_MAX_DEFER_SHIFT 6 |
| 144 | |
| 145 | /* |
| 146 | * Compaction is deferred when compaction fails to result in a page |
| 147 | * allocation success. 1 << compact_defer_limit compactions are skipped up |
| 148 | * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT |
| 149 | */ |
| 150 | void defer_compaction(struct zone *zone, int order) |
| 151 | { |
| 152 | zone->compact_considered = 0; |
| 153 | zone->compact_defer_shift++; |
| 154 | |
| 155 | if (order < zone->compact_order_failed) |
| 156 | zone->compact_order_failed = order; |
| 157 | |
| 158 | if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT) |
| 159 | zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT; |
| 160 | |
| 161 | trace_mm_compaction_defer_compaction(zone, order); |
| 162 | } |
| 163 | |
| 164 | /* Returns true if compaction should be skipped this time */ |
| 165 | bool compaction_deferred(struct zone *zone, int order) |
| 166 | { |
| 167 | unsigned long defer_limit = 1UL << zone->compact_defer_shift; |
| 168 | |
| 169 | if (order < zone->compact_order_failed) |
| 170 | return false; |
| 171 | |
| 172 | /* Avoid possible overflow */ |
| 173 | if (++zone->compact_considered > defer_limit) |
| 174 | zone->compact_considered = defer_limit; |
| 175 | |
| 176 | if (zone->compact_considered >= defer_limit) |
| 177 | return false; |
| 178 | |
| 179 | trace_mm_compaction_deferred(zone, order); |
| 180 | |
| 181 | return true; |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Update defer tracking counters after successful compaction of given order, |
| 186 | * which means an allocation either succeeded (alloc_success == true) or is |
| 187 | * expected to succeed. |
| 188 | */ |
| 189 | void compaction_defer_reset(struct zone *zone, int order, |
| 190 | bool alloc_success) |
| 191 | { |
| 192 | if (alloc_success) { |
| 193 | zone->compact_considered = 0; |
| 194 | zone->compact_defer_shift = 0; |
| 195 | } |
| 196 | if (order >= zone->compact_order_failed) |
| 197 | zone->compact_order_failed = order + 1; |
| 198 | |
| 199 | trace_mm_compaction_defer_reset(zone, order); |
| 200 | } |
| 201 | |
| 202 | /* Returns true if restarting compaction after many failures */ |
| 203 | bool compaction_restarting(struct zone *zone, int order) |
| 204 | { |
| 205 | if (order < zone->compact_order_failed) |
| 206 | return false; |
| 207 | |
| 208 | return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT && |
| 209 | zone->compact_considered >= 1UL << zone->compact_defer_shift; |
| 210 | } |
| 211 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 212 | /* Returns true if the pageblock should be scanned for pages to isolate. */ |
| 213 | static inline bool isolation_suitable(struct compact_control *cc, |
| 214 | struct page *page) |
| 215 | { |
| 216 | if (cc->ignore_skip_hint) |
| 217 | return true; |
| 218 | |
| 219 | return !get_pageblock_skip(page); |
| 220 | } |
| 221 | |
Vlastimil Babka | 0233364 | 2015-09-08 15:02:42 -0700 | [diff] [blame] | 222 | static void reset_cached_positions(struct zone *zone) |
| 223 | { |
| 224 | zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn; |
| 225 | zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn; |
Joonsoo Kim | 623446e | 2016-03-15 14:57:45 -0700 | [diff] [blame] | 226 | zone->compact_cached_free_pfn = |
Vlastimil Babka | 06b6640 | 2016-05-19 17:11:48 -0700 | [diff] [blame] | 227 | pageblock_start_pfn(zone_end_pfn(zone) - 1); |
Vlastimil Babka | 0233364 | 2015-09-08 15:02:42 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 230 | /* |
| 231 | * This function is called to clear all cached information on pageblocks that |
| 232 | * should be skipped for page isolation when the migrate and free page scanner |
| 233 | * meet. |
| 234 | */ |
Mel Gorman | 6299702 | 2012-10-08 16:32:47 -0700 | [diff] [blame] | 235 | static void __reset_isolation_suitable(struct zone *zone) |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 236 | { |
| 237 | unsigned long start_pfn = zone->zone_start_pfn; |
Cody P Schafer | 108bcc9 | 2013-02-22 16:35:23 -0800 | [diff] [blame] | 238 | unsigned long end_pfn = zone_end_pfn(zone); |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 239 | unsigned long pfn; |
| 240 | |
Mel Gorman | 6299702 | 2012-10-08 16:32:47 -0700 | [diff] [blame] | 241 | zone->compact_blockskip_flush = false; |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 242 | |
| 243 | /* Walk the zone and mark every pageblock as suitable for isolation */ |
| 244 | for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) { |
| 245 | struct page *page; |
| 246 | |
| 247 | cond_resched(); |
| 248 | |
| 249 | if (!pfn_valid(pfn)) |
| 250 | continue; |
| 251 | |
| 252 | page = pfn_to_page(pfn); |
| 253 | if (zone != page_zone(page)) |
| 254 | continue; |
| 255 | |
| 256 | clear_pageblock_skip(page); |
| 257 | } |
Vlastimil Babka | 0233364 | 2015-09-08 15:02:42 -0700 | [diff] [blame] | 258 | |
| 259 | reset_cached_positions(zone); |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Mel Gorman | 6299702 | 2012-10-08 16:32:47 -0700 | [diff] [blame] | 262 | void reset_isolation_suitable(pg_data_t *pgdat) |
| 263 | { |
| 264 | int zoneid; |
| 265 | |
| 266 | for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { |
| 267 | struct zone *zone = &pgdat->node_zones[zoneid]; |
| 268 | if (!populated_zone(zone)) |
| 269 | continue; |
| 270 | |
| 271 | /* Only flush if a full compaction finished recently */ |
| 272 | if (zone->compact_blockskip_flush) |
| 273 | __reset_isolation_suitable(zone); |
| 274 | } |
| 275 | } |
| 276 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 277 | /* |
| 278 | * 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] | 279 | * future. The information is later cleared by __reset_isolation_suitable(). |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 280 | */ |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 281 | static void update_pageblock_skip(struct compact_control *cc, |
| 282 | struct page *page, unsigned long nr_isolated, |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 283 | bool migrate_scanner) |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 284 | { |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 285 | struct zone *zone = cc->zone; |
David Rientjes | 35979ef | 2014-06-04 16:08:27 -0700 | [diff] [blame] | 286 | unsigned long pfn; |
Joonsoo Kim | 6815bf3 | 2013-12-18 17:08:52 -0800 | [diff] [blame] | 287 | |
| 288 | if (cc->ignore_skip_hint) |
| 289 | return; |
| 290 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 291 | if (!page) |
| 292 | return; |
| 293 | |
David Rientjes | 35979ef | 2014-06-04 16:08:27 -0700 | [diff] [blame] | 294 | if (nr_isolated) |
| 295 | return; |
| 296 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 297 | set_pageblock_skip(page); |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 298 | |
David Rientjes | 35979ef | 2014-06-04 16:08:27 -0700 | [diff] [blame] | 299 | pfn = page_to_pfn(page); |
| 300 | |
| 301 | /* Update where async and sync compaction should restart */ |
| 302 | if (migrate_scanner) { |
David Rientjes | 35979ef | 2014-06-04 16:08:27 -0700 | [diff] [blame] | 303 | if (pfn > zone->compact_cached_migrate_pfn[0]) |
| 304 | zone->compact_cached_migrate_pfn[0] = pfn; |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 305 | if (cc->mode != MIGRATE_ASYNC && |
| 306 | pfn > zone->compact_cached_migrate_pfn[1]) |
David Rientjes | 35979ef | 2014-06-04 16:08:27 -0700 | [diff] [blame] | 307 | zone->compact_cached_migrate_pfn[1] = pfn; |
| 308 | } else { |
David Rientjes | 35979ef | 2014-06-04 16:08:27 -0700 | [diff] [blame] | 309 | if (pfn < zone->compact_cached_free_pfn) |
| 310 | zone->compact_cached_free_pfn = pfn; |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 311 | } |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 312 | } |
| 313 | #else |
| 314 | static inline bool isolation_suitable(struct compact_control *cc, |
| 315 | struct page *page) |
| 316 | { |
| 317 | return true; |
| 318 | } |
| 319 | |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 320 | static void update_pageblock_skip(struct compact_control *cc, |
| 321 | struct page *page, unsigned long nr_isolated, |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 322 | bool migrate_scanner) |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 323 | { |
| 324 | } |
| 325 | #endif /* CONFIG_COMPACTION */ |
| 326 | |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 327 | /* |
| 328 | * Compaction requires the taking of some coarse locks that are potentially |
| 329 | * very heavily contended. For async compaction, back out if the lock cannot |
| 330 | * be taken immediately. For sync compaction, spin on the lock if needed. |
| 331 | * |
| 332 | * Returns true if the lock is held |
| 333 | * Returns false if the lock is not held and compaction should abort |
| 334 | */ |
| 335 | static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags, |
| 336 | struct compact_control *cc) |
Mel Gorman | 2a1402a | 2012-10-08 16:32:33 -0700 | [diff] [blame] | 337 | { |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 338 | if (cc->mode == MIGRATE_ASYNC) { |
| 339 | if (!spin_trylock_irqsave(lock, *flags)) { |
| 340 | cc->contended = COMPACT_CONTENDED_LOCK; |
| 341 | return false; |
| 342 | } |
| 343 | } else { |
| 344 | spin_lock_irqsave(lock, *flags); |
| 345 | } |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame] | 346 | |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 347 | return true; |
Mel Gorman | 2a1402a | 2012-10-08 16:32:33 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 350 | /* |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 351 | * Compaction requires the taking of some coarse locks that are potentially |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 352 | * very heavily contended. The lock should be periodically unlocked to avoid |
| 353 | * having disabled IRQs for a long time, even when there is nobody waiting on |
| 354 | * the lock. It might also be that allowing the IRQs will result in |
| 355 | * need_resched() becoming true. If scheduling is needed, async compaction |
| 356 | * aborts. Sync compaction schedules. |
| 357 | * Either compaction type will also abort if a fatal signal is pending. |
| 358 | * 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] | 359 | * |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 360 | * Returns true if compaction should abort due to fatal signal pending, or |
| 361 | * async compaction due to need_resched() |
| 362 | * Returns false when compaction can continue (sync compaction might have |
| 363 | * scheduled) |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 364 | */ |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 365 | static bool compact_unlock_should_abort(spinlock_t *lock, |
| 366 | unsigned long flags, bool *locked, struct compact_control *cc) |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 367 | { |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 368 | if (*locked) { |
| 369 | spin_unlock_irqrestore(lock, flags); |
| 370 | *locked = false; |
| 371 | } |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame] | 372 | |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 373 | if (fatal_signal_pending(current)) { |
| 374 | cc->contended = COMPACT_CONTENDED_SCHED; |
| 375 | return true; |
| 376 | } |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 377 | |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 378 | if (need_resched()) { |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 379 | if (cc->mode == MIGRATE_ASYNC) { |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 380 | cc->contended = COMPACT_CONTENDED_SCHED; |
| 381 | return true; |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 382 | } |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 383 | cond_resched(); |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 386 | return false; |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 389 | /* |
| 390 | * Aside from avoiding lock contention, compaction also periodically checks |
| 391 | * need_resched() and either schedules in sync compaction or aborts async |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 392 | * compaction. This is similar to what compact_unlock_should_abort() does, but |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 393 | * is used where no lock is concerned. |
| 394 | * |
| 395 | * Returns false when no scheduling was needed, or sync compaction scheduled. |
| 396 | * Returns true when async compaction should abort. |
| 397 | */ |
| 398 | static inline bool compact_should_abort(struct compact_control *cc) |
| 399 | { |
| 400 | /* async compaction aborts if contended */ |
| 401 | if (need_resched()) { |
| 402 | if (cc->mode == MIGRATE_ASYNC) { |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame] | 403 | cc->contended = COMPACT_CONTENDED_SCHED; |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 404 | return true; |
| 405 | } |
| 406 | |
| 407 | cond_resched(); |
| 408 | } |
| 409 | |
| 410 | return false; |
| 411 | } |
| 412 | |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 413 | /* |
Jerome Marchand | 9e4be47 | 2013-11-12 15:07:12 -0800 | [diff] [blame] | 414 | * Isolate free pages onto a private freelist. If @strict is true, will abort |
| 415 | * returning 0 on any invalid PFNs or non-free pages inside of the pageblock |
| 416 | * (even though it may still end up isolating some pages). |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 417 | */ |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 418 | static unsigned long isolate_freepages_block(struct compact_control *cc, |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 419 | unsigned long *start_pfn, |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 420 | unsigned long end_pfn, |
| 421 | struct list_head *freelist, |
| 422 | bool strict) |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 423 | { |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 424 | int nr_scanned = 0, total_isolated = 0; |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 425 | struct page *cursor, *valid_page = NULL; |
Xiubo Li | b8b2d82 | 2014-10-09 15:28:21 -0700 | [diff] [blame] | 426 | unsigned long flags = 0; |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 427 | bool locked = false; |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 428 | unsigned long blockpfn = *start_pfn; |
Joonsoo Kim | 66c6422 | 2016-07-26 15:23:40 -0700 | [diff] [blame] | 429 | unsigned int order; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 430 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 431 | cursor = pfn_to_page(blockpfn); |
| 432 | |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 433 | /* Isolate free pages. */ |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 434 | for (; blockpfn < end_pfn; blockpfn++, cursor++) { |
Joonsoo Kim | 66c6422 | 2016-07-26 15:23:40 -0700 | [diff] [blame] | 435 | int isolated; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 436 | struct page *page = cursor; |
| 437 | |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 438 | /* |
| 439 | * Periodically drop the lock (if held) regardless of its |
| 440 | * contention, to give chance to IRQs. Abort if fatal signal |
| 441 | * pending or async compaction detects need_resched() |
| 442 | */ |
| 443 | if (!(blockpfn % SWAP_CLUSTER_MAX) |
| 444 | && compact_unlock_should_abort(&cc->zone->lock, flags, |
| 445 | &locked, cc)) |
| 446 | break; |
| 447 | |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 448 | nr_scanned++; |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 449 | if (!pfn_valid_within(blockpfn)) |
Laura Abbott | 2af120b | 2014-03-10 15:49:44 -0700 | [diff] [blame] | 450 | goto isolate_fail; |
| 451 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 452 | if (!valid_page) |
| 453 | valid_page = page; |
Vlastimil Babka | 9fcd6d2 | 2015-09-08 15:02:49 -0700 | [diff] [blame] | 454 | |
| 455 | /* |
| 456 | * For compound pages such as THP and hugetlbfs, we can save |
| 457 | * potentially a lot of iterations if we skip them at once. |
| 458 | * The check is racy, but we can consider only valid values |
| 459 | * and the only danger is skipping too much. |
| 460 | */ |
| 461 | if (PageCompound(page)) { |
| 462 | unsigned int comp_order = compound_order(page); |
| 463 | |
| 464 | if (likely(comp_order < MAX_ORDER)) { |
| 465 | blockpfn += (1UL << comp_order) - 1; |
| 466 | cursor += (1UL << comp_order) - 1; |
| 467 | } |
| 468 | |
| 469 | goto isolate_fail; |
| 470 | } |
| 471 | |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 472 | if (!PageBuddy(page)) |
Laura Abbott | 2af120b | 2014-03-10 15:49:44 -0700 | [diff] [blame] | 473 | goto isolate_fail; |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 474 | |
| 475 | /* |
Vlastimil Babka | 69b7189 | 2014-10-09 15:27:18 -0700 | [diff] [blame] | 476 | * If we already hold the lock, we can skip some rechecking. |
| 477 | * Note that if we hold the lock now, checked_pageblock was |
| 478 | * already set in some previous iteration (or strict is true), |
| 479 | * so it is correct to skip the suitable migration target |
| 480 | * recheck as well. |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 481 | */ |
Vlastimil Babka | 69b7189 | 2014-10-09 15:27:18 -0700 | [diff] [blame] | 482 | if (!locked) { |
| 483 | /* |
| 484 | * The zone lock must be held to isolate freepages. |
| 485 | * Unfortunately this is a very coarse lock and can be |
| 486 | * heavily contended if there are parallel allocations |
| 487 | * or parallel compactions. For async compaction do not |
| 488 | * spin on the lock and we acquire the lock as late as |
| 489 | * possible. |
| 490 | */ |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 491 | locked = compact_trylock_irqsave(&cc->zone->lock, |
| 492 | &flags, cc); |
Vlastimil Babka | 69b7189 | 2014-10-09 15:27:18 -0700 | [diff] [blame] | 493 | if (!locked) |
| 494 | break; |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 495 | |
Vlastimil Babka | 69b7189 | 2014-10-09 15:27:18 -0700 | [diff] [blame] | 496 | /* Recheck this is a buddy page under lock */ |
| 497 | if (!PageBuddy(page)) |
| 498 | goto isolate_fail; |
| 499 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 500 | |
Joonsoo Kim | 66c6422 | 2016-07-26 15:23:40 -0700 | [diff] [blame] | 501 | /* Found a free page, will break it into order-0 pages */ |
| 502 | order = page_order(page); |
| 503 | isolated = __isolate_free_page(page, order); |
David Rientjes | a4f04f2 | 2016-06-24 14:50:10 -0700 | [diff] [blame] | 504 | if (!isolated) |
| 505 | break; |
Joonsoo Kim | 66c6422 | 2016-07-26 15:23:40 -0700 | [diff] [blame] | 506 | set_page_private(page, order); |
David Rientjes | a4f04f2 | 2016-06-24 14:50:10 -0700 | [diff] [blame] | 507 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 508 | total_isolated += isolated; |
David Rientjes | a4f04f2 | 2016-06-24 14:50:10 -0700 | [diff] [blame] | 509 | cc->nr_freepages += isolated; |
Joonsoo Kim | 66c6422 | 2016-07-26 15:23:40 -0700 | [diff] [blame] | 510 | list_add_tail(&page->lru, freelist); |
| 511 | |
David Rientjes | a4f04f2 | 2016-06-24 14:50:10 -0700 | [diff] [blame] | 512 | if (!strict && cc->nr_migratepages <= cc->nr_freepages) { |
| 513 | blockpfn += isolated; |
| 514 | break; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 515 | } |
David Rientjes | a4f04f2 | 2016-06-24 14:50:10 -0700 | [diff] [blame] | 516 | /* Advance to the end of split page */ |
| 517 | blockpfn += isolated - 1; |
| 518 | cursor += isolated - 1; |
| 519 | continue; |
Laura Abbott | 2af120b | 2014-03-10 15:49:44 -0700 | [diff] [blame] | 520 | |
| 521 | isolate_fail: |
| 522 | if (strict) |
| 523 | break; |
| 524 | else |
| 525 | continue; |
| 526 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 527 | } |
| 528 | |
David Rientjes | a4f04f2 | 2016-06-24 14:50:10 -0700 | [diff] [blame] | 529 | if (locked) |
| 530 | spin_unlock_irqrestore(&cc->zone->lock, flags); |
| 531 | |
Vlastimil Babka | 9fcd6d2 | 2015-09-08 15:02:49 -0700 | [diff] [blame] | 532 | /* |
| 533 | * There is a tiny chance that we have read bogus compound_order(), |
| 534 | * so be careful to not go outside of the pageblock. |
| 535 | */ |
| 536 | if (unlikely(blockpfn > end_pfn)) |
| 537 | blockpfn = end_pfn; |
| 538 | |
Joonsoo Kim | e34d85f | 2015-02-11 15:27:04 -0800 | [diff] [blame] | 539 | trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn, |
| 540 | nr_scanned, total_isolated); |
| 541 | |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 542 | /* Record how far we have got within the block */ |
| 543 | *start_pfn = blockpfn; |
| 544 | |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 545 | /* |
| 546 | * If strict isolation is requested by CMA then check that all the |
| 547 | * pages requested were isolated. If there were any failures, 0 is |
| 548 | * returned and CMA will fail. |
| 549 | */ |
Laura Abbott | 2af120b | 2014-03-10 15:49:44 -0700 | [diff] [blame] | 550 | if (strict && blockpfn < end_pfn) |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 551 | total_isolated = 0; |
| 552 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 553 | /* Update the pageblock-skip if the whole pageblock was scanned */ |
| 554 | if (blockpfn == end_pfn) |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 555 | update_pageblock_skip(cc, valid_page, total_isolated, false); |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 556 | |
Minchan Kim | 010fc29 | 2012-12-20 15:05:06 -0800 | [diff] [blame] | 557 | count_compact_events(COMPACTFREE_SCANNED, nr_scanned); |
Mel Gorman | 397487d | 2012-10-19 12:00:10 +0100 | [diff] [blame] | 558 | if (total_isolated) |
Minchan Kim | 010fc29 | 2012-12-20 15:05:06 -0800 | [diff] [blame] | 559 | count_compact_events(COMPACTISOLATED, total_isolated); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 560 | return total_isolated; |
| 561 | } |
| 562 | |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 563 | /** |
| 564 | * isolate_freepages_range() - isolate free pages. |
| 565 | * @start_pfn: The first PFN to start isolating. |
| 566 | * @end_pfn: The one-past-last PFN. |
| 567 | * |
| 568 | * Non-free pages, invalid PFNs, or zone boundaries within the |
| 569 | * [start_pfn, end_pfn) range are considered errors, cause function to |
| 570 | * undo its actions and return zero. |
| 571 | * |
| 572 | * Otherwise, function returns one-past-the-last PFN of isolated page |
| 573 | * (which may be greater then end_pfn if end fell in a middle of |
| 574 | * a free page). |
| 575 | */ |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 576 | unsigned long |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 577 | isolate_freepages_range(struct compact_control *cc, |
| 578 | unsigned long start_pfn, unsigned long end_pfn) |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 579 | { |
Joonsoo Kim | e1409c3 | 2016-03-15 14:57:48 -0700 | [diff] [blame] | 580 | unsigned long isolated, pfn, block_start_pfn, block_end_pfn; |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 581 | LIST_HEAD(freelist); |
| 582 | |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 583 | pfn = start_pfn; |
Vlastimil Babka | 06b6640 | 2016-05-19 17:11:48 -0700 | [diff] [blame] | 584 | block_start_pfn = pageblock_start_pfn(pfn); |
Joonsoo Kim | e1409c3 | 2016-03-15 14:57:48 -0700 | [diff] [blame] | 585 | if (block_start_pfn < cc->zone->zone_start_pfn) |
| 586 | block_start_pfn = cc->zone->zone_start_pfn; |
Vlastimil Babka | 06b6640 | 2016-05-19 17:11:48 -0700 | [diff] [blame] | 587 | block_end_pfn = pageblock_end_pfn(pfn); |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 588 | |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 589 | for (; pfn < end_pfn; pfn += isolated, |
Joonsoo Kim | e1409c3 | 2016-03-15 14:57:48 -0700 | [diff] [blame] | 590 | block_start_pfn = block_end_pfn, |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 591 | block_end_pfn += pageblock_nr_pages) { |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 592 | /* Protect pfn from changing by isolate_freepages_block */ |
| 593 | unsigned long isolate_start_pfn = pfn; |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 594 | |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 595 | block_end_pfn = min(block_end_pfn, end_pfn); |
| 596 | |
Joonsoo Kim | 5842001 | 2014-11-13 15:19:07 -0800 | [diff] [blame] | 597 | /* |
| 598 | * pfn could pass the block_end_pfn if isolated freepage |
| 599 | * is more than pageblock order. In this case, we adjust |
| 600 | * scanning range to right one. |
| 601 | */ |
| 602 | if (pfn >= block_end_pfn) { |
Vlastimil Babka | 06b6640 | 2016-05-19 17:11:48 -0700 | [diff] [blame] | 603 | block_start_pfn = pageblock_start_pfn(pfn); |
| 604 | block_end_pfn = pageblock_end_pfn(pfn); |
Joonsoo Kim | 5842001 | 2014-11-13 15:19:07 -0800 | [diff] [blame] | 605 | block_end_pfn = min(block_end_pfn, end_pfn); |
| 606 | } |
| 607 | |
Joonsoo Kim | e1409c3 | 2016-03-15 14:57:48 -0700 | [diff] [blame] | 608 | if (!pageblock_pfn_to_page(block_start_pfn, |
| 609 | block_end_pfn, cc->zone)) |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 610 | break; |
| 611 | |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 612 | isolated = isolate_freepages_block(cc, &isolate_start_pfn, |
| 613 | block_end_pfn, &freelist, true); |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 614 | |
| 615 | /* |
| 616 | * In strict mode, isolate_freepages_block() returns 0 if |
| 617 | * there are any holes in the block (ie. invalid PFNs or |
| 618 | * non-free pages). |
| 619 | */ |
| 620 | if (!isolated) |
| 621 | break; |
| 622 | |
| 623 | /* |
| 624 | * If we managed to isolate pages, it is always (1 << n) * |
| 625 | * pageblock_nr_pages for some non-negative n. (Max order |
| 626 | * page may span two pageblocks). |
| 627 | */ |
| 628 | } |
| 629 | |
Joonsoo Kim | 66c6422 | 2016-07-26 15:23:40 -0700 | [diff] [blame] | 630 | /* __isolate_free_page() does not map the pages */ |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 631 | map_pages(&freelist); |
| 632 | |
| 633 | if (pfn < end_pfn) { |
| 634 | /* Loop terminated early, cleanup. */ |
| 635 | release_freepages(&freelist); |
| 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | /* We don't use freelists for anything. */ |
| 640 | return pfn; |
| 641 | } |
| 642 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 643 | /* Update the number of anon and file isolated pages in the zone */ |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 644 | static void acct_isolated(struct zone *zone, struct compact_control *cc) |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 645 | { |
| 646 | struct page *page; |
Minchan Kim | b9e84ac | 2011-10-31 17:06:44 -0700 | [diff] [blame] | 647 | unsigned int count[2] = { 0, }; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 648 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 649 | if (list_empty(&cc->migratepages)) |
| 650 | return; |
| 651 | |
Minchan Kim | b9e84ac | 2011-10-31 17:06:44 -0700 | [diff] [blame] | 652 | list_for_each_entry(page, &cc->migratepages, lru) |
| 653 | count[!!page_is_file_cache(page)]++; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 654 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 655 | mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]); |
| 656 | mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | /* Similar to reclaim, but different enough that they don't share logic */ |
| 660 | static bool too_many_isolated(struct zone *zone) |
| 661 | { |
Minchan Kim | bc69304 | 2010-09-09 16:38:00 -0700 | [diff] [blame] | 662 | unsigned long active, inactive, isolated; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 663 | |
| 664 | inactive = zone_page_state(zone, NR_INACTIVE_FILE) + |
| 665 | zone_page_state(zone, NR_INACTIVE_ANON); |
Minchan Kim | bc69304 | 2010-09-09 16:38:00 -0700 | [diff] [blame] | 666 | active = zone_page_state(zone, NR_ACTIVE_FILE) + |
| 667 | zone_page_state(zone, NR_ACTIVE_ANON); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 668 | isolated = zone_page_state(zone, NR_ISOLATED_FILE) + |
| 669 | zone_page_state(zone, NR_ISOLATED_ANON); |
| 670 | |
Minchan Kim | bc69304 | 2010-09-09 16:38:00 -0700 | [diff] [blame] | 671 | return isolated > (inactive + active) / 2; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 674 | /** |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 675 | * isolate_migratepages_block() - isolate all migrate-able pages within |
| 676 | * a single pageblock |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 677 | * @cc: Compaction control structure. |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 678 | * @low_pfn: The first PFN to isolate |
| 679 | * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock |
| 680 | * @isolate_mode: Isolation mode to be used. |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 681 | * |
| 682 | * Isolate all pages that can be migrated from the range specified by |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 683 | * [low_pfn, end_pfn). The range is expected to be within same pageblock. |
| 684 | * Returns zero if there is a fatal signal pending, otherwise PFN of the |
| 685 | * first page that was not scanned (which may be both less, equal to or more |
| 686 | * than end_pfn). |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 687 | * |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 688 | * The pages are isolated on cc->migratepages list (not required to be empty), |
| 689 | * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field |
| 690 | * is neither read nor updated. |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 691 | */ |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 692 | static unsigned long |
| 693 | isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, |
| 694 | unsigned long end_pfn, isolate_mode_t isolate_mode) |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 695 | { |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 696 | struct zone *zone = cc->zone; |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 697 | unsigned long nr_scanned = 0, nr_isolated = 0; |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 698 | struct lruvec *lruvec; |
Xiubo Li | b8b2d82 | 2014-10-09 15:28:21 -0700 | [diff] [blame] | 699 | unsigned long flags = 0; |
Mel Gorman | 2a1402a | 2012-10-08 16:32:33 -0700 | [diff] [blame] | 700 | bool locked = false; |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 701 | struct page *page = NULL, *valid_page = NULL; |
Joonsoo Kim | e34d85f | 2015-02-11 15:27:04 -0800 | [diff] [blame] | 702 | unsigned long start_pfn = low_pfn; |
Vlastimil Babka | fdd048e | 2016-05-19 17:11:55 -0700 | [diff] [blame] | 703 | bool skip_on_failure = false; |
| 704 | unsigned long next_skip_pfn = 0; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 705 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 706 | /* |
| 707 | * Ensure that there are not too many pages isolated from the LRU |
| 708 | * list by either parallel reclaimers or compaction. If there are, |
| 709 | * delay for some time until fewer pages are isolated |
| 710 | */ |
| 711 | while (unlikely(too_many_isolated(zone))) { |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 712 | /* async migration should just abort */ |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 713 | if (cc->mode == MIGRATE_ASYNC) |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 714 | return 0; |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 715 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 716 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
| 717 | |
| 718 | if (fatal_signal_pending(current)) |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 719 | return 0; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 720 | } |
| 721 | |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 722 | if (compact_should_abort(cc)) |
| 723 | return 0; |
David Rientjes | aeef4b8 | 2014-06-04 16:08:31 -0700 | [diff] [blame] | 724 | |
Vlastimil Babka | fdd048e | 2016-05-19 17:11:55 -0700 | [diff] [blame] | 725 | if (cc->direct_compaction && (cc->mode == MIGRATE_ASYNC)) { |
| 726 | skip_on_failure = true; |
| 727 | next_skip_pfn = block_end_pfn(low_pfn, cc->order); |
| 728 | } |
| 729 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 730 | /* Time to isolate some pages for migration */ |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 731 | for (; low_pfn < end_pfn; low_pfn++) { |
Vlastimil Babka | 29c0dde | 2015-09-08 15:02:46 -0700 | [diff] [blame] | 732 | |
Vlastimil Babka | fdd048e | 2016-05-19 17:11:55 -0700 | [diff] [blame] | 733 | if (skip_on_failure && low_pfn >= next_skip_pfn) { |
| 734 | /* |
| 735 | * We have isolated all migration candidates in the |
| 736 | * previous order-aligned block, and did not skip it due |
| 737 | * to failure. We should migrate the pages now and |
| 738 | * hopefully succeed compaction. |
| 739 | */ |
| 740 | if (nr_isolated) |
| 741 | break; |
| 742 | |
| 743 | /* |
| 744 | * We failed to isolate in the previous order-aligned |
| 745 | * block. Set the new boundary to the end of the |
| 746 | * current block. Note we can't simply increase |
| 747 | * next_skip_pfn by 1 << order, as low_pfn might have |
| 748 | * been incremented by a higher number due to skipping |
| 749 | * a compound or a high-order buddy page in the |
| 750 | * previous loop iteration. |
| 751 | */ |
| 752 | next_skip_pfn = block_end_pfn(low_pfn, cc->order); |
| 753 | } |
| 754 | |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 755 | /* |
| 756 | * Periodically drop the lock (if held) regardless of its |
| 757 | * contention, to give chance to IRQs. Abort async compaction |
| 758 | * if contended. |
| 759 | */ |
| 760 | if (!(low_pfn % SWAP_CLUSTER_MAX) |
| 761 | && compact_unlock_should_abort(&zone->lru_lock, flags, |
| 762 | &locked, cc)) |
| 763 | break; |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 764 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 765 | if (!pfn_valid_within(low_pfn)) |
Vlastimil Babka | fdd048e | 2016-05-19 17:11:55 -0700 | [diff] [blame] | 766 | goto isolate_fail; |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 767 | nr_scanned++; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 768 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 769 | page = pfn_to_page(low_pfn); |
Mel Gorman | dc90860 | 2012-02-08 17:13:38 -0800 | [diff] [blame] | 770 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 771 | if (!valid_page) |
| 772 | valid_page = page; |
| 773 | |
Mel Gorman | 6c14466 | 2014-01-23 15:53:38 -0800 | [diff] [blame] | 774 | /* |
Vlastimil Babka | 99c0fd5 | 2014-10-09 15:27:23 -0700 | [diff] [blame] | 775 | * Skip if free. We read page order here without zone lock |
| 776 | * which is generally unsafe, but the race window is small and |
| 777 | * the worst thing that can happen is that we skip some |
| 778 | * potential isolation targets. |
Mel Gorman | 6c14466 | 2014-01-23 15:53:38 -0800 | [diff] [blame] | 779 | */ |
Vlastimil Babka | 99c0fd5 | 2014-10-09 15:27:23 -0700 | [diff] [blame] | 780 | if (PageBuddy(page)) { |
| 781 | unsigned long freepage_order = page_order_unsafe(page); |
| 782 | |
| 783 | /* |
| 784 | * Without lock, we cannot be sure that what we got is |
| 785 | * a valid page order. Consider only values in the |
| 786 | * valid order range to prevent low_pfn overflow. |
| 787 | */ |
| 788 | if (freepage_order > 0 && freepage_order < MAX_ORDER) |
| 789 | low_pfn += (1UL << freepage_order) - 1; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 790 | continue; |
Vlastimil Babka | 99c0fd5 | 2014-10-09 15:27:23 -0700 | [diff] [blame] | 791 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 792 | |
Mel Gorman | 9927af74 | 2011-01-13 15:45:59 -0800 | [diff] [blame] | 793 | /* |
Vlastimil Babka | 29c0dde | 2015-09-08 15:02:46 -0700 | [diff] [blame] | 794 | * Regardless of being on LRU, compound pages such as THP and |
| 795 | * hugetlbfs are not to be compacted. We can potentially save |
| 796 | * a lot of iterations if we skip them at once. The check is |
| 797 | * racy, but we can consider only valid values and the only |
| 798 | * danger is skipping too much. |
Andrea Arcangeli | bc83501 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 799 | */ |
Vlastimil Babka | 29c0dde | 2015-09-08 15:02:46 -0700 | [diff] [blame] | 800 | if (PageCompound(page)) { |
| 801 | unsigned int comp_order = compound_order(page); |
| 802 | |
| 803 | if (likely(comp_order < MAX_ORDER)) |
| 804 | low_pfn += (1UL << comp_order) - 1; |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 805 | |
Vlastimil Babka | fdd048e | 2016-05-19 17:11:55 -0700 | [diff] [blame] | 806 | goto isolate_fail; |
Mel Gorman | 2a1402a | 2012-10-08 16:32:33 -0700 | [diff] [blame] | 807 | } |
| 808 | |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 809 | /* |
| 810 | * Check may be lockless but that's ok as we recheck later. |
| 811 | * It's possible to migrate LRU and non-lru movable pages. |
| 812 | * Skip any other type of page |
| 813 | */ |
| 814 | if (!PageLRU(page)) { |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 815 | /* |
| 816 | * __PageMovable can return false positive so we need |
| 817 | * to verify it under page_lock. |
| 818 | */ |
| 819 | if (unlikely(__PageMovable(page)) && |
| 820 | !PageIsolated(page)) { |
| 821 | if (locked) { |
| 822 | spin_unlock_irqrestore(&zone->lru_lock, |
| 823 | flags); |
| 824 | locked = false; |
| 825 | } |
| 826 | |
| 827 | if (isolate_movable_page(page, isolate_mode)) |
| 828 | goto isolate_success; |
| 829 | } |
| 830 | |
Vlastimil Babka | fdd048e | 2016-05-19 17:11:55 -0700 | [diff] [blame] | 831 | goto isolate_fail; |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 832 | } |
Vlastimil Babka | 29c0dde | 2015-09-08 15:02:46 -0700 | [diff] [blame] | 833 | |
David Rientjes | 119d6d5 | 2014-04-03 14:48:00 -0700 | [diff] [blame] | 834 | /* |
| 835 | * Migration will fail if an anonymous page is pinned in memory, |
| 836 | * so avoid taking lru_lock and isolating it unnecessarily in an |
| 837 | * admittedly racy check. |
| 838 | */ |
| 839 | if (!page_mapping(page) && |
| 840 | page_count(page) > page_mapcount(page)) |
Vlastimil Babka | fdd048e | 2016-05-19 17:11:55 -0700 | [diff] [blame] | 841 | goto isolate_fail; |
David Rientjes | 119d6d5 | 2014-04-03 14:48:00 -0700 | [diff] [blame] | 842 | |
Vlastimil Babka | 69b7189 | 2014-10-09 15:27:18 -0700 | [diff] [blame] | 843 | /* If we already hold the lock, we can skip some rechecking */ |
| 844 | if (!locked) { |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 845 | locked = compact_trylock_irqsave(&zone->lru_lock, |
| 846 | &flags, cc); |
Vlastimil Babka | 69b7189 | 2014-10-09 15:27:18 -0700 | [diff] [blame] | 847 | if (!locked) |
| 848 | break; |
Mel Gorman | 2a1402a | 2012-10-08 16:32:33 -0700 | [diff] [blame] | 849 | |
Vlastimil Babka | 29c0dde | 2015-09-08 15:02:46 -0700 | [diff] [blame] | 850 | /* Recheck PageLRU and PageCompound under lock */ |
Vlastimil Babka | 69b7189 | 2014-10-09 15:27:18 -0700 | [diff] [blame] | 851 | if (!PageLRU(page)) |
Vlastimil Babka | fdd048e | 2016-05-19 17:11:55 -0700 | [diff] [blame] | 852 | goto isolate_fail; |
Vlastimil Babka | 29c0dde | 2015-09-08 15:02:46 -0700 | [diff] [blame] | 853 | |
| 854 | /* |
| 855 | * Page become compound since the non-locked check, |
| 856 | * and it's on LRU. It can only be a THP so the order |
| 857 | * is safe to read and it's 0 for tail pages. |
| 858 | */ |
| 859 | if (unlikely(PageCompound(page))) { |
| 860 | low_pfn += (1UL << compound_order(page)) - 1; |
Vlastimil Babka | fdd048e | 2016-05-19 17:11:55 -0700 | [diff] [blame] | 861 | goto isolate_fail; |
Vlastimil Babka | 69b7189 | 2014-10-09 15:27:18 -0700 | [diff] [blame] | 862 | } |
Andrea Arcangeli | bc83501 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 863 | } |
| 864 | |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 865 | lruvec = mem_cgroup_page_lruvec(page, zone); |
| 866 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 867 | /* Try isolate the page */ |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 868 | if (__isolate_lru_page(page, isolate_mode) != 0) |
Vlastimil Babka | fdd048e | 2016-05-19 17:11:55 -0700 | [diff] [blame] | 869 | goto isolate_fail; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 870 | |
Vlastimil Babka | 29c0dde | 2015-09-08 15:02:46 -0700 | [diff] [blame] | 871 | VM_BUG_ON_PAGE(PageCompound(page), page); |
Andrea Arcangeli | bc83501 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 872 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 873 | /* Successfully isolated */ |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 874 | del_page_from_lru_list(page, lruvec, page_lru(page)); |
Joonsoo Kim | b6c7501 | 2014-04-07 15:37:07 -0700 | [diff] [blame] | 875 | |
| 876 | isolate_success: |
Vlastimil Babka | fdd048e | 2016-05-19 17:11:55 -0700 | [diff] [blame] | 877 | list_add(&page->lru, &cc->migratepages); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 878 | cc->nr_migratepages++; |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 879 | nr_isolated++; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 880 | |
Vlastimil Babka | a34753d | 2016-05-19 17:11:51 -0700 | [diff] [blame] | 881 | /* |
| 882 | * Record where we could have freed pages by migration and not |
| 883 | * yet flushed them to buddy allocator. |
| 884 | * - this is the lowest page that was isolated and likely be |
| 885 | * then freed by migration. |
| 886 | */ |
| 887 | if (!cc->last_migrated_pfn) |
| 888 | cc->last_migrated_pfn = low_pfn; |
| 889 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 890 | /* Avoid isolating too much */ |
Hillf Danton | 31b8384 | 2012-01-10 15:07:59 -0800 | [diff] [blame] | 891 | if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) { |
| 892 | ++low_pfn; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 893 | break; |
Hillf Danton | 31b8384 | 2012-01-10 15:07:59 -0800 | [diff] [blame] | 894 | } |
Vlastimil Babka | fdd048e | 2016-05-19 17:11:55 -0700 | [diff] [blame] | 895 | |
| 896 | continue; |
| 897 | isolate_fail: |
| 898 | if (!skip_on_failure) |
| 899 | continue; |
| 900 | |
| 901 | /* |
| 902 | * We have isolated some pages, but then failed. Release them |
| 903 | * instead of migrating, as we cannot form the cc->order buddy |
| 904 | * page anyway. |
| 905 | */ |
| 906 | if (nr_isolated) { |
| 907 | if (locked) { |
| 908 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
| 909 | locked = false; |
| 910 | } |
| 911 | acct_isolated(zone, cc); |
| 912 | putback_movable_pages(&cc->migratepages); |
| 913 | cc->nr_migratepages = 0; |
| 914 | cc->last_migrated_pfn = 0; |
| 915 | nr_isolated = 0; |
| 916 | } |
| 917 | |
| 918 | if (low_pfn < next_skip_pfn) { |
| 919 | low_pfn = next_skip_pfn - 1; |
| 920 | /* |
| 921 | * The check near the loop beginning would have updated |
| 922 | * next_skip_pfn too, but this is a bit simpler. |
| 923 | */ |
| 924 | next_skip_pfn += 1UL << cc->order; |
| 925 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 926 | } |
| 927 | |
Vlastimil Babka | 99c0fd5 | 2014-10-09 15:27:23 -0700 | [diff] [blame] | 928 | /* |
| 929 | * The PageBuddy() check could have potentially brought us outside |
| 930 | * the range to be scanned. |
| 931 | */ |
| 932 | if (unlikely(low_pfn > end_pfn)) |
| 933 | low_pfn = end_pfn; |
| 934 | |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 935 | if (locked) |
| 936 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 937 | |
Vlastimil Babka | 50b5b09 | 2014-01-21 15:51:10 -0800 | [diff] [blame] | 938 | /* |
| 939 | * Update the pageblock-skip information and cached scanner pfn, |
| 940 | * if the whole pageblock was scanned without isolating any page. |
Vlastimil Babka | 50b5b09 | 2014-01-21 15:51:10 -0800 | [diff] [blame] | 941 | */ |
David Rientjes | 35979ef | 2014-06-04 16:08:27 -0700 | [diff] [blame] | 942 | if (low_pfn == end_pfn) |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 943 | update_pageblock_skip(cc, valid_page, nr_isolated, true); |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 944 | |
Joonsoo Kim | e34d85f | 2015-02-11 15:27:04 -0800 | [diff] [blame] | 945 | trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn, |
| 946 | nr_scanned, nr_isolated); |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 947 | |
Minchan Kim | 010fc29 | 2012-12-20 15:05:06 -0800 | [diff] [blame] | 948 | count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned); |
Mel Gorman | 397487d | 2012-10-19 12:00:10 +0100 | [diff] [blame] | 949 | if (nr_isolated) |
Minchan Kim | 010fc29 | 2012-12-20 15:05:06 -0800 | [diff] [blame] | 950 | count_compact_events(COMPACTISOLATED, nr_isolated); |
Mel Gorman | 397487d | 2012-10-19 12:00:10 +0100 | [diff] [blame] | 951 | |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 952 | return low_pfn; |
| 953 | } |
| 954 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 955 | /** |
| 956 | * isolate_migratepages_range() - isolate migrate-able pages in a PFN range |
| 957 | * @cc: Compaction control structure. |
| 958 | * @start_pfn: The first PFN to start isolating. |
| 959 | * @end_pfn: The one-past-last PFN. |
| 960 | * |
| 961 | * Returns zero if isolation fails fatally due to e.g. pending signal. |
| 962 | * Otherwise, function returns one-past-the-last PFN of isolated page |
| 963 | * (which may be greater than end_pfn if end fell in a middle of a THP page). |
| 964 | */ |
| 965 | unsigned long |
| 966 | isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn, |
| 967 | unsigned long end_pfn) |
| 968 | { |
Joonsoo Kim | e1409c3 | 2016-03-15 14:57:48 -0700 | [diff] [blame] | 969 | unsigned long pfn, block_start_pfn, block_end_pfn; |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 970 | |
| 971 | /* Scan block by block. First and last block may be incomplete */ |
| 972 | pfn = start_pfn; |
Vlastimil Babka | 06b6640 | 2016-05-19 17:11:48 -0700 | [diff] [blame] | 973 | block_start_pfn = pageblock_start_pfn(pfn); |
Joonsoo Kim | e1409c3 | 2016-03-15 14:57:48 -0700 | [diff] [blame] | 974 | if (block_start_pfn < cc->zone->zone_start_pfn) |
| 975 | block_start_pfn = cc->zone->zone_start_pfn; |
Vlastimil Babka | 06b6640 | 2016-05-19 17:11:48 -0700 | [diff] [blame] | 976 | block_end_pfn = pageblock_end_pfn(pfn); |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 977 | |
| 978 | for (; pfn < end_pfn; pfn = block_end_pfn, |
Joonsoo Kim | e1409c3 | 2016-03-15 14:57:48 -0700 | [diff] [blame] | 979 | block_start_pfn = block_end_pfn, |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 980 | block_end_pfn += pageblock_nr_pages) { |
| 981 | |
| 982 | block_end_pfn = min(block_end_pfn, end_pfn); |
| 983 | |
Joonsoo Kim | e1409c3 | 2016-03-15 14:57:48 -0700 | [diff] [blame] | 984 | if (!pageblock_pfn_to_page(block_start_pfn, |
| 985 | block_end_pfn, cc->zone)) |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 986 | continue; |
| 987 | |
| 988 | pfn = isolate_migratepages_block(cc, pfn, block_end_pfn, |
| 989 | ISOLATE_UNEVICTABLE); |
| 990 | |
Hugh Dickins | 14af4a5 | 2016-05-05 16:22:15 -0700 | [diff] [blame] | 991 | if (!pfn) |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 992 | break; |
Joonsoo Kim | 6ea41c0 | 2014-10-29 14:50:20 -0700 | [diff] [blame] | 993 | |
| 994 | if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) |
| 995 | break; |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 996 | } |
| 997 | acct_isolated(cc->zone, cc); |
| 998 | |
| 999 | return pfn; |
| 1000 | } |
| 1001 | |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1002 | #endif /* CONFIG_COMPACTION || CONFIG_CMA */ |
| 1003 | #ifdef CONFIG_COMPACTION |
Andrew Morton | 018e9a4 | 2015-04-15 16:15:20 -0700 | [diff] [blame] | 1004 | |
| 1005 | /* Returns true if the page is within a block suitable for migration to */ |
| 1006 | static bool suitable_migration_target(struct page *page) |
| 1007 | { |
| 1008 | /* If the page is a large free page, then disallow migration */ |
| 1009 | if (PageBuddy(page)) { |
| 1010 | /* |
| 1011 | * We are checking page_order without zone->lock taken. But |
| 1012 | * the only small danger is that we skip a potentially suitable |
| 1013 | * pageblock, so it's not worth to check order for valid range. |
| 1014 | */ |
| 1015 | if (page_order_unsafe(page) >= pageblock_order) |
| 1016 | return false; |
| 1017 | } |
| 1018 | |
| 1019 | /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */ |
| 1020 | if (migrate_async_suitable(get_pageblock_migratetype(page))) |
| 1021 | return true; |
| 1022 | |
| 1023 | /* Otherwise skip the block */ |
| 1024 | return false; |
| 1025 | } |
| 1026 | |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1027 | /* |
Vlastimil Babka | f2849aa | 2015-09-08 15:02:36 -0700 | [diff] [blame] | 1028 | * Test whether the free scanner has reached the same or lower pageblock than |
| 1029 | * the migration scanner, and compaction should thus terminate. |
| 1030 | */ |
| 1031 | static inline bool compact_scanners_met(struct compact_control *cc) |
| 1032 | { |
| 1033 | return (cc->free_pfn >> pageblock_order) |
| 1034 | <= (cc->migrate_pfn >> pageblock_order); |
| 1035 | } |
| 1036 | |
| 1037 | /* |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1038 | * Based on information in the current compact_control, find blocks |
| 1039 | * suitable for isolating free pages from and then isolate them. |
| 1040 | */ |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1041 | static void isolate_freepages(struct compact_control *cc) |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1042 | { |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1043 | struct zone *zone = cc->zone; |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1044 | struct page *page; |
Vlastimil Babka | c96b9e5 | 2014-06-04 16:07:26 -0700 | [diff] [blame] | 1045 | unsigned long block_start_pfn; /* start of current pageblock */ |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 1046 | unsigned long isolate_start_pfn; /* exact pfn we start at */ |
Vlastimil Babka | c96b9e5 | 2014-06-04 16:07:26 -0700 | [diff] [blame] | 1047 | unsigned long block_end_pfn; /* end of current pageblock */ |
| 1048 | unsigned long low_pfn; /* lowest pfn scanner is able to scan */ |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1049 | struct list_head *freelist = &cc->freepages; |
| 1050 | |
| 1051 | /* |
| 1052 | * Initialise the free scanner. The starting point is where we last |
Vlastimil Babka | 49e068f | 2014-05-06 12:50:03 -0700 | [diff] [blame] | 1053 | * successfully isolated from, zone-cached value, or the end of the |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 1054 | * zone when isolating for the first time. For looping we also need |
| 1055 | * this pfn aligned down to the pageblock boundary, because we do |
Vlastimil Babka | c96b9e5 | 2014-06-04 16:07:26 -0700 | [diff] [blame] | 1056 | * block_start_pfn -= pageblock_nr_pages in the for loop. |
| 1057 | * For ending point, take care when isolating in last pageblock of a |
| 1058 | * a zone which ends in the middle of a pageblock. |
Vlastimil Babka | 49e068f | 2014-05-06 12:50:03 -0700 | [diff] [blame] | 1059 | * The low boundary is the end of the pageblock the migration scanner |
| 1060 | * is using. |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1061 | */ |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 1062 | isolate_start_pfn = cc->free_pfn; |
Vlastimil Babka | 06b6640 | 2016-05-19 17:11:48 -0700 | [diff] [blame] | 1063 | block_start_pfn = pageblock_start_pfn(cc->free_pfn); |
Vlastimil Babka | c96b9e5 | 2014-06-04 16:07:26 -0700 | [diff] [blame] | 1064 | block_end_pfn = min(block_start_pfn + pageblock_nr_pages, |
| 1065 | zone_end_pfn(zone)); |
Vlastimil Babka | 06b6640 | 2016-05-19 17:11:48 -0700 | [diff] [blame] | 1066 | low_pfn = pageblock_end_pfn(cc->migrate_pfn); |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1067 | |
| 1068 | /* |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1069 | * Isolate free pages until enough are available to migrate the |
| 1070 | * pages on cc->migratepages. We stop searching if the migrate |
| 1071 | * and free page scanners meet or enough free pages are isolated. |
| 1072 | */ |
Vlastimil Babka | f5f61a3 | 2015-09-08 15:02:39 -0700 | [diff] [blame] | 1073 | for (; block_start_pfn >= low_pfn; |
Vlastimil Babka | c96b9e5 | 2014-06-04 16:07:26 -0700 | [diff] [blame] | 1074 | block_end_pfn = block_start_pfn, |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 1075 | block_start_pfn -= pageblock_nr_pages, |
| 1076 | isolate_start_pfn = block_start_pfn) { |
David Rientjes | f6ea3ad | 2013-09-30 13:45:03 -0700 | [diff] [blame] | 1077 | /* |
| 1078 | * This can iterate a massively long zone without finding any |
| 1079 | * suitable migration targets, so periodically check if we need |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 1080 | * to schedule, or even abort async compaction. |
David Rientjes | f6ea3ad | 2013-09-30 13:45:03 -0700 | [diff] [blame] | 1081 | */ |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 1082 | if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) |
| 1083 | && compact_should_abort(cc)) |
| 1084 | break; |
David Rientjes | f6ea3ad | 2013-09-30 13:45:03 -0700 | [diff] [blame] | 1085 | |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 1086 | page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, |
| 1087 | zone); |
| 1088 | if (!page) |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1089 | continue; |
| 1090 | |
| 1091 | /* Check the block is suitable for migration */ |
Linus Torvalds | 68e3e92 | 2012-06-03 20:05:57 -0700 | [diff] [blame] | 1092 | if (!suitable_migration_target(page)) |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1093 | continue; |
Linus Torvalds | 68e3e92 | 2012-06-03 20:05:57 -0700 | [diff] [blame] | 1094 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 1095 | /* If isolation recently failed, do not retry */ |
| 1096 | if (!isolation_suitable(cc, page)) |
| 1097 | continue; |
| 1098 | |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 1099 | /* Found a block suitable for isolating free pages from. */ |
David Rientjes | a46cbf3 | 2016-07-14 12:06:50 -0700 | [diff] [blame] | 1100 | isolate_freepages_block(cc, &isolate_start_pfn, block_end_pfn, |
| 1101 | freelist, false); |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1102 | |
| 1103 | /* |
David Rientjes | a46cbf3 | 2016-07-14 12:06:50 -0700 | [diff] [blame] | 1104 | * If we isolated enough freepages, or aborted due to lock |
| 1105 | * contention, terminate. |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 1106 | */ |
Vlastimil Babka | f5f61a3 | 2015-09-08 15:02:39 -0700 | [diff] [blame] | 1107 | if ((cc->nr_freepages >= cc->nr_migratepages) |
| 1108 | || cc->contended) { |
David Rientjes | a46cbf3 | 2016-07-14 12:06:50 -0700 | [diff] [blame] | 1109 | if (isolate_start_pfn >= block_end_pfn) { |
| 1110 | /* |
| 1111 | * Restart at previous pageblock if more |
| 1112 | * freepages can be isolated next time. |
| 1113 | */ |
Vlastimil Babka | f5f61a3 | 2015-09-08 15:02:39 -0700 | [diff] [blame] | 1114 | isolate_start_pfn = |
| 1115 | block_start_pfn - pageblock_nr_pages; |
David Rientjes | a46cbf3 | 2016-07-14 12:06:50 -0700 | [diff] [blame] | 1116 | } |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 1117 | break; |
David Rientjes | a46cbf3 | 2016-07-14 12:06:50 -0700 | [diff] [blame] | 1118 | } else if (isolate_start_pfn < block_end_pfn) { |
Vlastimil Babka | f5f61a3 | 2015-09-08 15:02:39 -0700 | [diff] [blame] | 1119 | /* |
David Rientjes | a46cbf3 | 2016-07-14 12:06:50 -0700 | [diff] [blame] | 1120 | * If isolation failed early, do not continue |
| 1121 | * needlessly. |
Vlastimil Babka | f5f61a3 | 2015-09-08 15:02:39 -0700 | [diff] [blame] | 1122 | */ |
David Rientjes | a46cbf3 | 2016-07-14 12:06:50 -0700 | [diff] [blame] | 1123 | break; |
Vlastimil Babka | f5f61a3 | 2015-09-08 15:02:39 -0700 | [diff] [blame] | 1124 | } |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 1125 | } |
| 1126 | |
Joonsoo Kim | 66c6422 | 2016-07-26 15:23:40 -0700 | [diff] [blame] | 1127 | /* __isolate_free_page() does not map the pages */ |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1128 | map_pages(freelist); |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 1129 | |
Vlastimil Babka | 7ed695e | 2014-01-21 15:51:09 -0800 | [diff] [blame] | 1130 | /* |
Vlastimil Babka | f5f61a3 | 2015-09-08 15:02:39 -0700 | [diff] [blame] | 1131 | * Record where the free scanner will restart next time. Either we |
| 1132 | * broke from the loop and set isolate_start_pfn based on the last |
| 1133 | * call to isolate_freepages_block(), or we met the migration scanner |
| 1134 | * and the loop terminated due to isolate_start_pfn < low_pfn |
Vlastimil Babka | 7ed695e | 2014-01-21 15:51:09 -0800 | [diff] [blame] | 1135 | */ |
Vlastimil Babka | f5f61a3 | 2015-09-08 15:02:39 -0700 | [diff] [blame] | 1136 | cc->free_pfn = isolate_start_pfn; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | /* |
| 1140 | * This is a migrate-callback that "allocates" freepages by taking pages |
| 1141 | * from the isolated freelists in the block we are migrating to. |
| 1142 | */ |
| 1143 | static struct page *compaction_alloc(struct page *migratepage, |
| 1144 | unsigned long data, |
| 1145 | int **result) |
| 1146 | { |
| 1147 | struct compact_control *cc = (struct compact_control *)data; |
| 1148 | struct page *freepage; |
| 1149 | |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 1150 | /* |
| 1151 | * Isolate free pages if necessary, and if we are not aborting due to |
| 1152 | * contention. |
| 1153 | */ |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1154 | if (list_empty(&cc->freepages)) { |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 1155 | if (!cc->contended) |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1156 | isolate_freepages(cc); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1157 | |
| 1158 | if (list_empty(&cc->freepages)) |
| 1159 | return NULL; |
| 1160 | } |
| 1161 | |
| 1162 | freepage = list_entry(cc->freepages.next, struct page, lru); |
| 1163 | list_del(&freepage->lru); |
| 1164 | cc->nr_freepages--; |
| 1165 | |
| 1166 | return freepage; |
| 1167 | } |
| 1168 | |
| 1169 | /* |
David Rientjes | d53aea3 | 2014-06-04 16:08:26 -0700 | [diff] [blame] | 1170 | * This is a migrate-callback that "frees" freepages back to the isolated |
| 1171 | * freelist. All pages on the freelist are from the same zone, so there is no |
| 1172 | * special handling needed for NUMA. |
| 1173 | */ |
| 1174 | static void compaction_free(struct page *page, unsigned long data) |
| 1175 | { |
| 1176 | struct compact_control *cc = (struct compact_control *)data; |
| 1177 | |
| 1178 | list_add(&page->lru, &cc->freepages); |
| 1179 | cc->nr_freepages++; |
| 1180 | } |
| 1181 | |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1182 | /* possible outcome of isolate_migratepages */ |
| 1183 | typedef enum { |
| 1184 | ISOLATE_ABORT, /* Abort compaction now */ |
| 1185 | ISOLATE_NONE, /* No pages isolated, continue scanning */ |
| 1186 | ISOLATE_SUCCESS, /* Pages isolated, migrate */ |
| 1187 | } isolate_migrate_t; |
| 1188 | |
| 1189 | /* |
Eric B Munson | 5bbe354 | 2015-04-15 16:13:20 -0700 | [diff] [blame] | 1190 | * Allow userspace to control policy on scanning the unevictable LRU for |
| 1191 | * compactable pages. |
| 1192 | */ |
| 1193 | int sysctl_compact_unevictable_allowed __read_mostly = 1; |
| 1194 | |
| 1195 | /* |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1196 | * Isolate all pages that can be migrated from the first suitable block, |
| 1197 | * starting at the block pointed to by the migrate scanner pfn within |
| 1198 | * compact_control. |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1199 | */ |
| 1200 | static isolate_migrate_t isolate_migratepages(struct zone *zone, |
| 1201 | struct compact_control *cc) |
| 1202 | { |
Joonsoo Kim | e1409c3 | 2016-03-15 14:57:48 -0700 | [diff] [blame] | 1203 | unsigned long block_start_pfn; |
| 1204 | unsigned long block_end_pfn; |
| 1205 | unsigned long low_pfn; |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1206 | struct page *page; |
| 1207 | const isolate_mode_t isolate_mode = |
Eric B Munson | 5bbe354 | 2015-04-15 16:13:20 -0700 | [diff] [blame] | 1208 | (sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1209 | (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0); |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1210 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1211 | /* |
| 1212 | * Start at where we last stopped, or beginning of the zone as |
| 1213 | * initialized by compact_zone() |
| 1214 | */ |
| 1215 | low_pfn = cc->migrate_pfn; |
Vlastimil Babka | 06b6640 | 2016-05-19 17:11:48 -0700 | [diff] [blame] | 1216 | block_start_pfn = pageblock_start_pfn(low_pfn); |
Joonsoo Kim | e1409c3 | 2016-03-15 14:57:48 -0700 | [diff] [blame] | 1217 | if (block_start_pfn < zone->zone_start_pfn) |
| 1218 | block_start_pfn = zone->zone_start_pfn; |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1219 | |
| 1220 | /* Only scan within a pageblock boundary */ |
Vlastimil Babka | 06b6640 | 2016-05-19 17:11:48 -0700 | [diff] [blame] | 1221 | block_end_pfn = pageblock_end_pfn(low_pfn); |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1222 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1223 | /* |
| 1224 | * Iterate over whole pageblocks until we find the first suitable. |
| 1225 | * Do not cross the free scanner. |
| 1226 | */ |
Joonsoo Kim | e1409c3 | 2016-03-15 14:57:48 -0700 | [diff] [blame] | 1227 | for (; block_end_pfn <= cc->free_pfn; |
| 1228 | low_pfn = block_end_pfn, |
| 1229 | block_start_pfn = block_end_pfn, |
| 1230 | block_end_pfn += pageblock_nr_pages) { |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1231 | |
| 1232 | /* |
| 1233 | * This can potentially iterate a massively long zone with |
| 1234 | * many pageblocks unsuitable, so periodically check if we |
| 1235 | * need to schedule, or even abort async compaction. |
| 1236 | */ |
| 1237 | if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) |
| 1238 | && compact_should_abort(cc)) |
| 1239 | break; |
| 1240 | |
Joonsoo Kim | e1409c3 | 2016-03-15 14:57:48 -0700 | [diff] [blame] | 1241 | page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, |
| 1242 | zone); |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 1243 | if (!page) |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1244 | continue; |
| 1245 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1246 | /* If isolation recently failed, do not retry */ |
| 1247 | if (!isolation_suitable(cc, page)) |
| 1248 | continue; |
| 1249 | |
| 1250 | /* |
| 1251 | * For async compaction, also only scan in MOVABLE blocks. |
| 1252 | * Async compaction is optimistic to see if the minimum amount |
| 1253 | * of work satisfies the allocation. |
| 1254 | */ |
| 1255 | if (cc->mode == MIGRATE_ASYNC && |
| 1256 | !migrate_async_suitable(get_pageblock_migratetype(page))) |
| 1257 | continue; |
| 1258 | |
| 1259 | /* Perform the isolation */ |
Joonsoo Kim | e1409c3 | 2016-03-15 14:57:48 -0700 | [diff] [blame] | 1260 | low_pfn = isolate_migratepages_block(cc, low_pfn, |
| 1261 | block_end_pfn, isolate_mode); |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1262 | |
Hugh Dickins | ff59909 | 2015-02-12 15:00:28 -0800 | [diff] [blame] | 1263 | if (!low_pfn || cc->contended) { |
| 1264 | acct_isolated(zone, cc); |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1265 | return ISOLATE_ABORT; |
Hugh Dickins | ff59909 | 2015-02-12 15:00:28 -0800 | [diff] [blame] | 1266 | } |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1267 | |
| 1268 | /* |
| 1269 | * Either we isolated something and proceed with migration. Or |
| 1270 | * we failed and compact_zone should decide if we should |
| 1271 | * continue or not. |
| 1272 | */ |
| 1273 | break; |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1274 | } |
| 1275 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1276 | acct_isolated(zone, cc); |
Vlastimil Babka | f2849aa | 2015-09-08 15:02:36 -0700 | [diff] [blame] | 1277 | /* Record where migration scanner will be restarted. */ |
| 1278 | cc->migrate_pfn = low_pfn; |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1279 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1280 | return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE; |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1281 | } |
| 1282 | |
Yaowei Bai | 21c527a | 2015-11-05 18:47:20 -0800 | [diff] [blame] | 1283 | /* |
| 1284 | * order == -1 is expected when compacting via |
| 1285 | * /proc/sys/vm/compact_memory |
| 1286 | */ |
| 1287 | static inline bool is_via_compact_memory(int order) |
| 1288 | { |
| 1289 | return order == -1; |
| 1290 | } |
| 1291 | |
Michal Hocko | ea7ab98 | 2016-05-20 16:56:38 -0700 | [diff] [blame] | 1292 | static enum compact_result __compact_finished(struct zone *zone, struct compact_control *cc, |
David Rientjes | 6d7ce55 | 2014-10-09 15:27:27 -0700 | [diff] [blame] | 1293 | const int migratetype) |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1294 | { |
Mel Gorman | 8fb74b9 | 2013-01-11 14:32:16 -0800 | [diff] [blame] | 1295 | unsigned int order; |
Andrea Arcangeli | 5a03b05 | 2011-01-13 15:47:11 -0800 | [diff] [blame] | 1296 | unsigned long watermark; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1297 | |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 1298 | if (cc->contended || fatal_signal_pending(current)) |
Vlastimil Babka | 2d1e104 | 2015-11-05 18:48:02 -0800 | [diff] [blame] | 1299 | return COMPACT_CONTENDED; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1300 | |
Mel Gorman | 753341a | 2012-10-08 16:32:40 -0700 | [diff] [blame] | 1301 | /* Compaction run completes if the migrate and free scanner meet */ |
Vlastimil Babka | f2849aa | 2015-09-08 15:02:36 -0700 | [diff] [blame] | 1302 | if (compact_scanners_met(cc)) { |
Vlastimil Babka | 55b7c4c | 2014-01-21 15:51:11 -0800 | [diff] [blame] | 1303 | /* Let the next compaction start anew. */ |
Vlastimil Babka | 0233364 | 2015-09-08 15:02:42 -0700 | [diff] [blame] | 1304 | reset_cached_positions(zone); |
Vlastimil Babka | 55b7c4c | 2014-01-21 15:51:11 -0800 | [diff] [blame] | 1305 | |
Mel Gorman | 6299702 | 2012-10-08 16:32:47 -0700 | [diff] [blame] | 1306 | /* |
| 1307 | * Mark that the PG_migrate_skip information should be cleared |
Vlastimil Babka | accf624 | 2016-03-17 14:18:15 -0700 | [diff] [blame] | 1308 | * by kswapd when it goes to sleep. kcompactd does not set the |
Mel Gorman | 6299702 | 2012-10-08 16:32:47 -0700 | [diff] [blame] | 1309 | * flag itself as the decision to be clear should be directly |
| 1310 | * based on an allocation request. |
| 1311 | */ |
Vlastimil Babka | accf624 | 2016-03-17 14:18:15 -0700 | [diff] [blame] | 1312 | if (cc->direct_compaction) |
Mel Gorman | 6299702 | 2012-10-08 16:32:47 -0700 | [diff] [blame] | 1313 | zone->compact_blockskip_flush = true; |
| 1314 | |
Michal Hocko | c8f7de0 | 2016-05-20 16:56:47 -0700 | [diff] [blame] | 1315 | if (cc->whole_zone) |
| 1316 | return COMPACT_COMPLETE; |
| 1317 | else |
| 1318 | return COMPACT_PARTIAL_SKIPPED; |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 1319 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1320 | |
Yaowei Bai | 21c527a | 2015-11-05 18:47:20 -0800 | [diff] [blame] | 1321 | if (is_via_compact_memory(cc->order)) |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1322 | return COMPACT_CONTINUE; |
| 1323 | |
Michal Hocko | 3957c77 | 2011-06-15 15:08:25 -0700 | [diff] [blame] | 1324 | /* Compaction run is not finished if the watermark is not met */ |
| 1325 | watermark = low_wmark_pages(zone); |
Michal Hocko | 3957c77 | 2011-06-15 15:08:25 -0700 | [diff] [blame] | 1326 | |
Vlastimil Babka | ebff398 | 2014-12-10 15:43:22 -0800 | [diff] [blame] | 1327 | if (!zone_watermark_ok(zone, cc->order, watermark, cc->classzone_idx, |
| 1328 | cc->alloc_flags)) |
Michal Hocko | 3957c77 | 2011-06-15 15:08:25 -0700 | [diff] [blame] | 1329 | return COMPACT_CONTINUE; |
| 1330 | |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1331 | /* Direct compactor: Is a suitable page free? */ |
Mel Gorman | 8fb74b9 | 2013-01-11 14:32:16 -0800 | [diff] [blame] | 1332 | for (order = cc->order; order < MAX_ORDER; order++) { |
| 1333 | struct free_area *area = &zone->free_area[order]; |
Joonsoo Kim | 2149cda | 2015-04-14 15:45:21 -0700 | [diff] [blame] | 1334 | bool can_steal; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1335 | |
Mel Gorman | 8fb74b9 | 2013-01-11 14:32:16 -0800 | [diff] [blame] | 1336 | /* Job done if page is free of the right migratetype */ |
David Rientjes | 6d7ce55 | 2014-10-09 15:27:27 -0700 | [diff] [blame] | 1337 | if (!list_empty(&area->free_list[migratetype])) |
Mel Gorman | 8fb74b9 | 2013-01-11 14:32:16 -0800 | [diff] [blame] | 1338 | return COMPACT_PARTIAL; |
| 1339 | |
Joonsoo Kim | 2149cda | 2015-04-14 15:45:21 -0700 | [diff] [blame] | 1340 | #ifdef CONFIG_CMA |
| 1341 | /* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */ |
| 1342 | if (migratetype == MIGRATE_MOVABLE && |
| 1343 | !list_empty(&area->free_list[MIGRATE_CMA])) |
| 1344 | return COMPACT_PARTIAL; |
| 1345 | #endif |
| 1346 | /* |
| 1347 | * Job done if allocation would steal freepages from |
| 1348 | * other migratetype buddy lists. |
| 1349 | */ |
| 1350 | if (find_suitable_fallback(area, order, migratetype, |
| 1351 | true, &can_steal) != -1) |
Mel Gorman | 8fb74b9 | 2013-01-11 14:32:16 -0800 | [diff] [blame] | 1352 | return COMPACT_PARTIAL; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1353 | } |
| 1354 | |
Joonsoo Kim | 837d026 | 2015-02-11 15:27:06 -0800 | [diff] [blame] | 1355 | return COMPACT_NO_SUITABLE_PAGE; |
| 1356 | } |
| 1357 | |
Michal Hocko | ea7ab98 | 2016-05-20 16:56:38 -0700 | [diff] [blame] | 1358 | static enum compact_result compact_finished(struct zone *zone, |
| 1359 | struct compact_control *cc, |
| 1360 | const int migratetype) |
Joonsoo Kim | 837d026 | 2015-02-11 15:27:06 -0800 | [diff] [blame] | 1361 | { |
| 1362 | int ret; |
| 1363 | |
| 1364 | ret = __compact_finished(zone, cc, migratetype); |
| 1365 | trace_mm_compaction_finished(zone, cc->order, ret); |
| 1366 | if (ret == COMPACT_NO_SUITABLE_PAGE) |
| 1367 | ret = COMPACT_CONTINUE; |
| 1368 | |
| 1369 | return ret; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1370 | } |
| 1371 | |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 1372 | /* |
| 1373 | * compaction_suitable: Is this suitable to run compaction on this zone now? |
| 1374 | * Returns |
| 1375 | * COMPACT_SKIPPED - If there are too few free pages for compaction |
| 1376 | * COMPACT_PARTIAL - If the allocation would succeed without compaction |
| 1377 | * COMPACT_CONTINUE - If compaction should run now |
| 1378 | */ |
Michal Hocko | ea7ab98 | 2016-05-20 16:56:38 -0700 | [diff] [blame] | 1379 | static enum compact_result __compaction_suitable(struct zone *zone, int order, |
Mel Gorman | c603844 | 2016-05-19 17:13:38 -0700 | [diff] [blame] | 1380 | unsigned int alloc_flags, |
Michal Hocko | 86a294a | 2016-05-20 16:57:12 -0700 | [diff] [blame] | 1381 | int classzone_idx, |
| 1382 | unsigned long wmark_target) |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 1383 | { |
| 1384 | int fragindex; |
| 1385 | unsigned long watermark; |
| 1386 | |
Yaowei Bai | 21c527a | 2015-11-05 18:47:20 -0800 | [diff] [blame] | 1387 | if (is_via_compact_memory(order)) |
Michal Hocko | 3957c77 | 2011-06-15 15:08:25 -0700 | [diff] [blame] | 1388 | return COMPACT_CONTINUE; |
| 1389 | |
Vlastimil Babka | ebff398 | 2014-12-10 15:43:22 -0800 | [diff] [blame] | 1390 | watermark = low_wmark_pages(zone); |
| 1391 | /* |
| 1392 | * If watermarks for high-order allocation are already met, there |
| 1393 | * should be no need for compaction at all. |
| 1394 | */ |
| 1395 | if (zone_watermark_ok(zone, order, watermark, classzone_idx, |
| 1396 | alloc_flags)) |
| 1397 | return COMPACT_PARTIAL; |
| 1398 | |
Michal Hocko | 3957c77 | 2011-06-15 15:08:25 -0700 | [diff] [blame] | 1399 | /* |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 1400 | * Watermarks for order-0 must be met for compaction. Note the 2UL. |
| 1401 | * This is because during migration, copies of pages need to be |
| 1402 | * allocated and for a short time, the footprint is higher |
| 1403 | */ |
Vlastimil Babka | ebff398 | 2014-12-10 15:43:22 -0800 | [diff] [blame] | 1404 | watermark += (2UL << order); |
Michal Hocko | 86a294a | 2016-05-20 16:57:12 -0700 | [diff] [blame] | 1405 | if (!__zone_watermark_ok(zone, 0, watermark, classzone_idx, |
| 1406 | alloc_flags, wmark_target)) |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 1407 | return COMPACT_SKIPPED; |
| 1408 | |
| 1409 | /* |
| 1410 | * fragmentation index determines if allocation failures are due to |
| 1411 | * low memory or external fragmentation |
| 1412 | * |
Vlastimil Babka | ebff398 | 2014-12-10 15:43:22 -0800 | [diff] [blame] | 1413 | * index of -1000 would imply allocations might succeed depending on |
| 1414 | * watermarks, but we already failed the high-order watermark check |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 1415 | * index towards 0 implies failure is due to lack of memory |
| 1416 | * index towards 1000 implies failure is due to fragmentation |
| 1417 | * |
| 1418 | * Only compact if a failure would be due to fragmentation. |
| 1419 | */ |
| 1420 | fragindex = fragmentation_index(zone, order); |
| 1421 | if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold) |
Joonsoo Kim | 837d026 | 2015-02-11 15:27:06 -0800 | [diff] [blame] | 1422 | return COMPACT_NOT_SUITABLE_ZONE; |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 1423 | |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 1424 | return COMPACT_CONTINUE; |
| 1425 | } |
| 1426 | |
Michal Hocko | ea7ab98 | 2016-05-20 16:56:38 -0700 | [diff] [blame] | 1427 | enum compact_result compaction_suitable(struct zone *zone, int order, |
Mel Gorman | c603844 | 2016-05-19 17:13:38 -0700 | [diff] [blame] | 1428 | unsigned int alloc_flags, |
| 1429 | int classzone_idx) |
Joonsoo Kim | 837d026 | 2015-02-11 15:27:06 -0800 | [diff] [blame] | 1430 | { |
Michal Hocko | ea7ab98 | 2016-05-20 16:56:38 -0700 | [diff] [blame] | 1431 | enum compact_result ret; |
Joonsoo Kim | 837d026 | 2015-02-11 15:27:06 -0800 | [diff] [blame] | 1432 | |
Michal Hocko | 86a294a | 2016-05-20 16:57:12 -0700 | [diff] [blame] | 1433 | ret = __compaction_suitable(zone, order, alloc_flags, classzone_idx, |
| 1434 | zone_page_state(zone, NR_FREE_PAGES)); |
Joonsoo Kim | 837d026 | 2015-02-11 15:27:06 -0800 | [diff] [blame] | 1435 | trace_mm_compaction_suitable(zone, order, ret); |
| 1436 | if (ret == COMPACT_NOT_SUITABLE_ZONE) |
| 1437 | ret = COMPACT_SKIPPED; |
| 1438 | |
| 1439 | return ret; |
| 1440 | } |
| 1441 | |
Michal Hocko | 86a294a | 2016-05-20 16:57:12 -0700 | [diff] [blame] | 1442 | bool compaction_zonelist_suitable(struct alloc_context *ac, int order, |
| 1443 | int alloc_flags) |
| 1444 | { |
| 1445 | struct zone *zone; |
| 1446 | struct zoneref *z; |
| 1447 | |
| 1448 | /* |
| 1449 | * Make sure at least one zone would pass __compaction_suitable if we continue |
| 1450 | * retrying the reclaim. |
| 1451 | */ |
| 1452 | for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx, |
| 1453 | ac->nodemask) { |
| 1454 | unsigned long available; |
| 1455 | enum compact_result compact_result; |
| 1456 | |
| 1457 | /* |
| 1458 | * Do not consider all the reclaimable memory because we do not |
| 1459 | * want to trash just for a single high order allocation which |
| 1460 | * is even not guaranteed to appear even if __compaction_suitable |
| 1461 | * is happy about the watermark check. |
| 1462 | */ |
| 1463 | available = zone_reclaimable_pages(zone) / order; |
| 1464 | available += zone_page_state_snapshot(zone, NR_FREE_PAGES); |
| 1465 | compact_result = __compaction_suitable(zone, order, alloc_flags, |
| 1466 | ac_classzone_idx(ac), available); |
| 1467 | if (compact_result != COMPACT_SKIPPED && |
| 1468 | compact_result != COMPACT_NOT_SUITABLE_ZONE) |
| 1469 | return true; |
| 1470 | } |
| 1471 | |
| 1472 | return false; |
| 1473 | } |
| 1474 | |
Michal Hocko | ea7ab98 | 2016-05-20 16:56:38 -0700 | [diff] [blame] | 1475 | static enum compact_result compact_zone(struct zone *zone, struct compact_control *cc) |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1476 | { |
Michal Hocko | ea7ab98 | 2016-05-20 16:56:38 -0700 | [diff] [blame] | 1477 | enum compact_result ret; |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 1478 | unsigned long start_pfn = zone->zone_start_pfn; |
Cody P Schafer | 108bcc9 | 2013-02-22 16:35:23 -0800 | [diff] [blame] | 1479 | unsigned long end_pfn = zone_end_pfn(zone); |
David Rientjes | 6d7ce55 | 2014-10-09 15:27:27 -0700 | [diff] [blame] | 1480 | const int migratetype = gfpflags_to_migratetype(cc->gfp_mask); |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 1481 | const bool sync = cc->mode != MIGRATE_ASYNC; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1482 | |
Vlastimil Babka | ebff398 | 2014-12-10 15:43:22 -0800 | [diff] [blame] | 1483 | ret = compaction_suitable(zone, cc->order, cc->alloc_flags, |
| 1484 | cc->classzone_idx); |
Michal Hocko | c46649d | 2016-05-20 16:56:41 -0700 | [diff] [blame] | 1485 | /* Compaction is likely to fail */ |
| 1486 | if (ret == COMPACT_PARTIAL || ret == COMPACT_SKIPPED) |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 1487 | return ret; |
Michal Hocko | c46649d | 2016-05-20 16:56:41 -0700 | [diff] [blame] | 1488 | |
| 1489 | /* huh, compaction_suitable is returning something unexpected */ |
| 1490 | VM_BUG_ON(ret != COMPACT_CONTINUE); |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 1491 | |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 1492 | /* |
Vlastimil Babka | d3132e4 | 2014-01-21 15:51:08 -0800 | [diff] [blame] | 1493 | * Clear pageblock skip if there were failures recently and compaction |
Vlastimil Babka | accf624 | 2016-03-17 14:18:15 -0700 | [diff] [blame] | 1494 | * is about to be retried after being deferred. |
Vlastimil Babka | d3132e4 | 2014-01-21 15:51:08 -0800 | [diff] [blame] | 1495 | */ |
Vlastimil Babka | accf624 | 2016-03-17 14:18:15 -0700 | [diff] [blame] | 1496 | if (compaction_restarting(zone, cc->order)) |
Vlastimil Babka | d3132e4 | 2014-01-21 15:51:08 -0800 | [diff] [blame] | 1497 | __reset_isolation_suitable(zone); |
| 1498 | |
| 1499 | /* |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 1500 | * Setup to move all movable pages to the end of the zone. Used cached |
| 1501 | * information on where the scanners should start but check that it |
| 1502 | * is initialised by ensuring the values are within zone boundaries. |
| 1503 | */ |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 1504 | cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync]; |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 1505 | cc->free_pfn = zone->compact_cached_free_pfn; |
Joonsoo Kim | 623446e | 2016-03-15 14:57:45 -0700 | [diff] [blame] | 1506 | if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) { |
Vlastimil Babka | 06b6640 | 2016-05-19 17:11:48 -0700 | [diff] [blame] | 1507 | cc->free_pfn = pageblock_start_pfn(end_pfn - 1); |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 1508 | zone->compact_cached_free_pfn = cc->free_pfn; |
| 1509 | } |
Joonsoo Kim | 623446e | 2016-03-15 14:57:45 -0700 | [diff] [blame] | 1510 | if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) { |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 1511 | cc->migrate_pfn = start_pfn; |
David Rientjes | 35979ef | 2014-06-04 16:08:27 -0700 | [diff] [blame] | 1512 | zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn; |
| 1513 | zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn; |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 1514 | } |
Michal Hocko | c8f7de0 | 2016-05-20 16:56:47 -0700 | [diff] [blame] | 1515 | |
| 1516 | if (cc->migrate_pfn == start_pfn) |
| 1517 | cc->whole_zone = true; |
| 1518 | |
Joonsoo Kim | 1a16718 | 2015-09-08 15:03:59 -0700 | [diff] [blame] | 1519 | cc->last_migrated_pfn = 0; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1520 | |
Joonsoo Kim | 16c4a09 | 2015-02-11 15:27:01 -0800 | [diff] [blame] | 1521 | trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, |
| 1522 | cc->free_pfn, end_pfn, sync); |
Mel Gorman | 0eb927c | 2014-01-21 15:51:05 -0800 | [diff] [blame] | 1523 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1524 | migrate_prep_local(); |
| 1525 | |
David Rientjes | 6d7ce55 | 2014-10-09 15:27:27 -0700 | [diff] [blame] | 1526 | while ((ret = compact_finished(zone, cc, migratetype)) == |
| 1527 | COMPACT_CONTINUE) { |
Minchan Kim | 9d502c1 | 2011-03-22 16:30:39 -0700 | [diff] [blame] | 1528 | int err; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1529 | |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 1530 | switch (isolate_migratepages(zone, cc)) { |
| 1531 | case ISOLATE_ABORT: |
Vlastimil Babka | 2d1e104 | 2015-11-05 18:48:02 -0800 | [diff] [blame] | 1532 | ret = COMPACT_CONTENDED; |
Rafael Aquini | 5733c7d | 2012-12-11 16:02:47 -0800 | [diff] [blame] | 1533 | putback_movable_pages(&cc->migratepages); |
Shaohua Li | e64c523 | 2012-10-08 16:32:27 -0700 | [diff] [blame] | 1534 | cc->nr_migratepages = 0; |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 1535 | goto out; |
| 1536 | case ISOLATE_NONE: |
Vlastimil Babka | fdaf7f5 | 2014-12-10 15:43:34 -0800 | [diff] [blame] | 1537 | /* |
| 1538 | * We haven't isolated and migrated anything, but |
| 1539 | * there might still be unflushed migrations from |
| 1540 | * previous cc->order aligned block. |
| 1541 | */ |
| 1542 | goto check_drain; |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 1543 | case ISOLATE_SUCCESS: |
| 1544 | ; |
| 1545 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1546 | |
David Rientjes | d53aea3 | 2014-06-04 16:08:26 -0700 | [diff] [blame] | 1547 | err = migrate_pages(&cc->migratepages, compaction_alloc, |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 1548 | compaction_free, (unsigned long)cc, cc->mode, |
Mel Gorman | 7b2a2d4 | 2012-10-19 14:07:31 +0100 | [diff] [blame] | 1549 | MR_COMPACTION); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1550 | |
Vlastimil Babka | f8c9301 | 2014-06-04 16:08:32 -0700 | [diff] [blame] | 1551 | trace_mm_compaction_migratepages(cc->nr_migratepages, err, |
| 1552 | &cc->migratepages); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1553 | |
Vlastimil Babka | f8c9301 | 2014-06-04 16:08:32 -0700 | [diff] [blame] | 1554 | /* All pages were either migrated or will be released */ |
| 1555 | cc->nr_migratepages = 0; |
Minchan Kim | 9d502c1 | 2011-03-22 16:30:39 -0700 | [diff] [blame] | 1556 | if (err) { |
Rafael Aquini | 5733c7d | 2012-12-11 16:02:47 -0800 | [diff] [blame] | 1557 | putback_movable_pages(&cc->migratepages); |
Vlastimil Babka | 7ed695e | 2014-01-21 15:51:09 -0800 | [diff] [blame] | 1558 | /* |
| 1559 | * migrate_pages() may return -ENOMEM when scanners meet |
| 1560 | * and we want compact_finished() to detect it |
| 1561 | */ |
Vlastimil Babka | f2849aa | 2015-09-08 15:02:36 -0700 | [diff] [blame] | 1562 | if (err == -ENOMEM && !compact_scanners_met(cc)) { |
Vlastimil Babka | 2d1e104 | 2015-11-05 18:48:02 -0800 | [diff] [blame] | 1563 | ret = COMPACT_CONTENDED; |
David Rientjes | 4bf2bba | 2012-07-11 14:02:13 -0700 | [diff] [blame] | 1564 | goto out; |
| 1565 | } |
Vlastimil Babka | fdd048e | 2016-05-19 17:11:55 -0700 | [diff] [blame] | 1566 | /* |
| 1567 | * We failed to migrate at least one page in the current |
| 1568 | * order-aligned block, so skip the rest of it. |
| 1569 | */ |
| 1570 | if (cc->direct_compaction && |
| 1571 | (cc->mode == MIGRATE_ASYNC)) { |
| 1572 | cc->migrate_pfn = block_end_pfn( |
| 1573 | cc->migrate_pfn - 1, cc->order); |
| 1574 | /* Draining pcplists is useless in this case */ |
| 1575 | cc->last_migrated_pfn = 0; |
| 1576 | |
| 1577 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1578 | } |
Vlastimil Babka | fdaf7f5 | 2014-12-10 15:43:34 -0800 | [diff] [blame] | 1579 | |
Vlastimil Babka | fdaf7f5 | 2014-12-10 15:43:34 -0800 | [diff] [blame] | 1580 | check_drain: |
| 1581 | /* |
| 1582 | * Has the migration scanner moved away from the previous |
| 1583 | * cc->order aligned block where we migrated from? If yes, |
| 1584 | * flush the pages that were freed, so that they can merge and |
| 1585 | * compact_finished() can detect immediately if allocation |
| 1586 | * would succeed. |
| 1587 | */ |
Joonsoo Kim | 1a16718 | 2015-09-08 15:03:59 -0700 | [diff] [blame] | 1588 | if (cc->order > 0 && cc->last_migrated_pfn) { |
Vlastimil Babka | fdaf7f5 | 2014-12-10 15:43:34 -0800 | [diff] [blame] | 1589 | int cpu; |
| 1590 | unsigned long current_block_start = |
Vlastimil Babka | 06b6640 | 2016-05-19 17:11:48 -0700 | [diff] [blame] | 1591 | block_start_pfn(cc->migrate_pfn, cc->order); |
Vlastimil Babka | fdaf7f5 | 2014-12-10 15:43:34 -0800 | [diff] [blame] | 1592 | |
Joonsoo Kim | 1a16718 | 2015-09-08 15:03:59 -0700 | [diff] [blame] | 1593 | if (cc->last_migrated_pfn < current_block_start) { |
Vlastimil Babka | fdaf7f5 | 2014-12-10 15:43:34 -0800 | [diff] [blame] | 1594 | cpu = get_cpu(); |
| 1595 | lru_add_drain_cpu(cpu); |
| 1596 | drain_local_pages(zone); |
| 1597 | put_cpu(); |
| 1598 | /* No more flushing until we migrate again */ |
Joonsoo Kim | 1a16718 | 2015-09-08 15:03:59 -0700 | [diff] [blame] | 1599 | cc->last_migrated_pfn = 0; |
Vlastimil Babka | fdaf7f5 | 2014-12-10 15:43:34 -0800 | [diff] [blame] | 1600 | } |
| 1601 | } |
| 1602 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1603 | } |
| 1604 | |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 1605 | out: |
Vlastimil Babka | 6bace09 | 2014-12-10 15:43:31 -0800 | [diff] [blame] | 1606 | /* |
| 1607 | * Release free pages and update where the free scanner should restart, |
| 1608 | * so we don't leave any returned pages behind in the next attempt. |
| 1609 | */ |
| 1610 | if (cc->nr_freepages > 0) { |
| 1611 | unsigned long free_pfn = release_freepages(&cc->freepages); |
| 1612 | |
| 1613 | cc->nr_freepages = 0; |
| 1614 | VM_BUG_ON(free_pfn == 0); |
| 1615 | /* The cached pfn is always the first in a pageblock */ |
Vlastimil Babka | 06b6640 | 2016-05-19 17:11:48 -0700 | [diff] [blame] | 1616 | free_pfn = pageblock_start_pfn(free_pfn); |
Vlastimil Babka | 6bace09 | 2014-12-10 15:43:31 -0800 | [diff] [blame] | 1617 | /* |
| 1618 | * Only go back, not forward. The cached pfn might have been |
| 1619 | * already reset to zone end in compact_finished() |
| 1620 | */ |
| 1621 | if (free_pfn > zone->compact_cached_free_pfn) |
| 1622 | zone->compact_cached_free_pfn = free_pfn; |
| 1623 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1624 | |
Joonsoo Kim | 16c4a09 | 2015-02-11 15:27:01 -0800 | [diff] [blame] | 1625 | trace_mm_compaction_end(start_pfn, cc->migrate_pfn, |
| 1626 | cc->free_pfn, end_pfn, sync, ret); |
Mel Gorman | 0eb927c | 2014-01-21 15:51:05 -0800 | [diff] [blame] | 1627 | |
Vlastimil Babka | 2d1e104 | 2015-11-05 18:48:02 -0800 | [diff] [blame] | 1628 | if (ret == COMPACT_CONTENDED) |
| 1629 | ret = COMPACT_PARTIAL; |
| 1630 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1631 | return ret; |
| 1632 | } |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1633 | |
Michal Hocko | ea7ab98 | 2016-05-20 16:56:38 -0700 | [diff] [blame] | 1634 | static enum compact_result compact_zone_order(struct zone *zone, int order, |
Vlastimil Babka | ebff398 | 2014-12-10 15:43:22 -0800 | [diff] [blame] | 1635 | gfp_t gfp_mask, enum migrate_mode mode, int *contended, |
Mel Gorman | c603844 | 2016-05-19 17:13:38 -0700 | [diff] [blame] | 1636 | unsigned int alloc_flags, int classzone_idx) |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1637 | { |
Michal Hocko | ea7ab98 | 2016-05-20 16:56:38 -0700 | [diff] [blame] | 1638 | enum compact_result ret; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1639 | struct compact_control cc = { |
| 1640 | .nr_freepages = 0, |
| 1641 | .nr_migratepages = 0, |
| 1642 | .order = order, |
David Rientjes | 6d7ce55 | 2014-10-09 15:27:27 -0700 | [diff] [blame] | 1643 | .gfp_mask = gfp_mask, |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1644 | .zone = zone, |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 1645 | .mode = mode, |
Vlastimil Babka | ebff398 | 2014-12-10 15:43:22 -0800 | [diff] [blame] | 1646 | .alloc_flags = alloc_flags, |
| 1647 | .classzone_idx = classzone_idx, |
Vlastimil Babka | accf624 | 2016-03-17 14:18:15 -0700 | [diff] [blame] | 1648 | .direct_compaction = true, |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1649 | }; |
| 1650 | INIT_LIST_HEAD(&cc.freepages); |
| 1651 | INIT_LIST_HEAD(&cc.migratepages); |
| 1652 | |
Shaohua Li | e64c523 | 2012-10-08 16:32:27 -0700 | [diff] [blame] | 1653 | ret = compact_zone(zone, &cc); |
| 1654 | |
| 1655 | VM_BUG_ON(!list_empty(&cc.freepages)); |
| 1656 | VM_BUG_ON(!list_empty(&cc.migratepages)); |
| 1657 | |
| 1658 | *contended = cc.contended; |
| 1659 | return ret; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1660 | } |
| 1661 | |
Mel Gorman | 5e77190 | 2010-05-24 14:32:31 -0700 | [diff] [blame] | 1662 | int sysctl_extfrag_threshold = 500; |
| 1663 | |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1664 | /** |
| 1665 | * try_to_compact_pages - Direct compact to satisfy a high-order allocation |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1666 | * @gfp_mask: The GFP mask of the current allocation |
Vlastimil Babka | 1a6d53a | 2015-02-11 15:25:44 -0800 | [diff] [blame] | 1667 | * @order: The order of the current allocation |
| 1668 | * @alloc_flags: The allocation flags of the current allocation |
| 1669 | * @ac: The context of current allocation |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 1670 | * @mode: The migration mode for async, sync light, or sync migration |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame] | 1671 | * @contended: Return value that determines if compaction was aborted due to |
| 1672 | * need_resched() or lock contention |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1673 | * |
| 1674 | * This is the main entry point for direct page compaction. |
| 1675 | */ |
Michal Hocko | ea7ab98 | 2016-05-20 16:56:38 -0700 | [diff] [blame] | 1676 | 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] | 1677 | unsigned int alloc_flags, const struct alloc_context *ac, |
| 1678 | enum migrate_mode mode, int *contended) |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1679 | { |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1680 | int may_enter_fs = gfp_mask & __GFP_FS; |
| 1681 | int may_perform_io = gfp_mask & __GFP_IO; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1682 | struct zoneref *z; |
| 1683 | struct zone *zone; |
Michal Hocko | 1d4746d | 2016-05-20 16:56:44 -0700 | [diff] [blame] | 1684 | enum compact_result rc = COMPACT_SKIPPED; |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame] | 1685 | int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */ |
| 1686 | |
| 1687 | *contended = COMPACT_CONTENDED_NONE; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1688 | |
Mel Gorman | 4ffb633 | 2012-10-08 16:29:09 -0700 | [diff] [blame] | 1689 | /* Check if the GFP flags allow compaction */ |
Andrea Arcangeli | c5a73c3 | 2011-01-13 15:47:11 -0800 | [diff] [blame] | 1690 | if (!order || !may_enter_fs || !may_perform_io) |
Vlastimil Babka | 53853e2 | 2014-10-09 15:27:02 -0700 | [diff] [blame] | 1691 | return COMPACT_SKIPPED; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1692 | |
Joonsoo Kim | 837d026 | 2015-02-11 15:27:06 -0800 | [diff] [blame] | 1693 | trace_mm_compaction_try_to_compact_pages(order, gfp_mask, mode); |
| 1694 | |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1695 | /* Compact each zone in the list */ |
Vlastimil Babka | 1a6d53a | 2015-02-11 15:25:44 -0800 | [diff] [blame] | 1696 | for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx, |
| 1697 | ac->nodemask) { |
Michal Hocko | ea7ab98 | 2016-05-20 16:56:38 -0700 | [diff] [blame] | 1698 | enum compact_result status; |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame] | 1699 | int zone_contended; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1700 | |
Michal Hocko | 1d4746d | 2016-05-20 16:56:44 -0700 | [diff] [blame] | 1701 | if (compaction_deferred(zone, order)) { |
| 1702 | rc = max_t(enum compact_result, COMPACT_DEFERRED, rc); |
Vlastimil Babka | 53853e2 | 2014-10-09 15:27:02 -0700 | [diff] [blame] | 1703 | continue; |
Michal Hocko | 1d4746d | 2016-05-20 16:56:44 -0700 | [diff] [blame] | 1704 | } |
Vlastimil Babka | 53853e2 | 2014-10-09 15:27:02 -0700 | [diff] [blame] | 1705 | |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 1706 | status = compact_zone_order(zone, order, gfp_mask, mode, |
Vlastimil Babka | 1a6d53a | 2015-02-11 15:25:44 -0800 | [diff] [blame] | 1707 | &zone_contended, alloc_flags, |
Mel Gorman | 93ea996 | 2016-05-19 17:14:13 -0700 | [diff] [blame] | 1708 | ac_classzone_idx(ac)); |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1709 | rc = max(status, rc); |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame] | 1710 | /* |
| 1711 | * It takes at least one zone that wasn't lock contended |
| 1712 | * to clear all_zones_contended. |
| 1713 | */ |
| 1714 | all_zones_contended &= zone_contended; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1715 | |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 1716 | /* If a normal allocation would succeed, stop compacting */ |
Vlastimil Babka | ebff398 | 2014-12-10 15:43:22 -0800 | [diff] [blame] | 1717 | if (zone_watermark_ok(zone, order, low_wmark_pages(zone), |
Mel Gorman | 93ea996 | 2016-05-19 17:14:13 -0700 | [diff] [blame] | 1718 | ac_classzone_idx(ac), alloc_flags)) { |
Vlastimil Babka | 53853e2 | 2014-10-09 15:27:02 -0700 | [diff] [blame] | 1719 | /* |
| 1720 | * We think the allocation will succeed in this zone, |
| 1721 | * but it is not certain, hence the false. The caller |
| 1722 | * will repeat this with true if allocation indeed |
| 1723 | * succeeds in this zone. |
| 1724 | */ |
| 1725 | compaction_defer_reset(zone, order, false); |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame] | 1726 | /* |
| 1727 | * It is possible that async compaction aborted due to |
| 1728 | * need_resched() and the watermarks were ok thanks to |
| 1729 | * somebody else freeing memory. The allocation can |
| 1730 | * however still fail so we better signal the |
| 1731 | * need_resched() contention anyway (this will not |
| 1732 | * prevent the allocation attempt). |
| 1733 | */ |
| 1734 | if (zone_contended == COMPACT_CONTENDED_SCHED) |
| 1735 | *contended = COMPACT_CONTENDED_SCHED; |
| 1736 | |
| 1737 | goto break_loop; |
| 1738 | } |
| 1739 | |
Michal Hocko | c8f7de0 | 2016-05-20 16:56:47 -0700 | [diff] [blame] | 1740 | if (mode != MIGRATE_ASYNC && (status == COMPACT_COMPLETE || |
| 1741 | status == COMPACT_PARTIAL_SKIPPED)) { |
Vlastimil Babka | 53853e2 | 2014-10-09 15:27:02 -0700 | [diff] [blame] | 1742 | /* |
| 1743 | * We think that allocation won't succeed in this zone |
| 1744 | * so we defer compaction there. If it ends up |
| 1745 | * succeeding after all, it will be reset. |
| 1746 | */ |
| 1747 | defer_compaction(zone, order); |
| 1748 | } |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame] | 1749 | |
| 1750 | /* |
| 1751 | * We might have stopped compacting due to need_resched() in |
| 1752 | * async compaction, or due to a fatal signal detected. In that |
| 1753 | * case do not try further zones and signal need_resched() |
| 1754 | * contention. |
| 1755 | */ |
| 1756 | if ((zone_contended == COMPACT_CONTENDED_SCHED) |
| 1757 | || fatal_signal_pending(current)) { |
| 1758 | *contended = COMPACT_CONTENDED_SCHED; |
| 1759 | goto break_loop; |
| 1760 | } |
| 1761 | |
| 1762 | continue; |
| 1763 | break_loop: |
| 1764 | /* |
| 1765 | * We might not have tried all the zones, so be conservative |
| 1766 | * and assume they are not all lock contended. |
| 1767 | */ |
| 1768 | all_zones_contended = 0; |
| 1769 | break; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1770 | } |
| 1771 | |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame] | 1772 | /* |
| 1773 | * If at least one zone wasn't deferred or skipped, we report if all |
| 1774 | * zones that were tried were lock contended. |
| 1775 | */ |
Michal Hocko | 1d4746d | 2016-05-20 16:56:44 -0700 | [diff] [blame] | 1776 | if (rc > COMPACT_INACTIVE && all_zones_contended) |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame] | 1777 | *contended = COMPACT_CONTENDED_LOCK; |
| 1778 | |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1779 | return rc; |
| 1780 | } |
| 1781 | |
| 1782 | |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1783 | /* Compact all zones within a node */ |
Andrew Morton | 7103f16 | 2013-02-22 16:32:33 -0800 | [diff] [blame] | 1784 | static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc) |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1785 | { |
| 1786 | int zoneid; |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1787 | struct zone *zone; |
| 1788 | |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1789 | for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1790 | |
| 1791 | zone = &pgdat->node_zones[zoneid]; |
| 1792 | if (!populated_zone(zone)) |
| 1793 | continue; |
| 1794 | |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1795 | cc->nr_freepages = 0; |
| 1796 | cc->nr_migratepages = 0; |
| 1797 | cc->zone = zone; |
| 1798 | INIT_LIST_HEAD(&cc->freepages); |
| 1799 | INIT_LIST_HEAD(&cc->migratepages); |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1800 | |
Gioh Kim | 195b0c6 | 2015-04-15 16:13:33 -0700 | [diff] [blame] | 1801 | /* |
| 1802 | * When called via /proc/sys/vm/compact_memory |
| 1803 | * this makes sure we compact the whole zone regardless of |
| 1804 | * cached scanner positions. |
| 1805 | */ |
Yaowei Bai | 21c527a | 2015-11-05 18:47:20 -0800 | [diff] [blame] | 1806 | if (is_via_compact_memory(cc->order)) |
Gioh Kim | 195b0c6 | 2015-04-15 16:13:33 -0700 | [diff] [blame] | 1807 | __reset_isolation_suitable(zone); |
| 1808 | |
Yaowei Bai | 21c527a | 2015-11-05 18:47:20 -0800 | [diff] [blame] | 1809 | if (is_via_compact_memory(cc->order) || |
| 1810 | !compaction_deferred(zone, cc->order)) |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1811 | compact_zone(zone, cc); |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1812 | |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1813 | VM_BUG_ON(!list_empty(&cc->freepages)); |
| 1814 | VM_BUG_ON(!list_empty(&cc->migratepages)); |
Joonsoo Kim | 7546934 | 2016-01-14 15:20:48 -0800 | [diff] [blame] | 1815 | |
| 1816 | if (is_via_compact_memory(cc->order)) |
| 1817 | continue; |
| 1818 | |
| 1819 | if (zone_watermark_ok(zone, cc->order, |
| 1820 | low_wmark_pages(zone), 0, 0)) |
| 1821 | compaction_defer_reset(zone, cc->order, false); |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1822 | } |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1823 | } |
| 1824 | |
Andrew Morton | 7103f16 | 2013-02-22 16:32:33 -0800 | [diff] [blame] | 1825 | void compact_pgdat(pg_data_t *pgdat, int order) |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1826 | { |
| 1827 | struct compact_control cc = { |
| 1828 | .order = order, |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 1829 | .mode = MIGRATE_ASYNC, |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1830 | }; |
| 1831 | |
Mel Gorman | 3a7200a | 2013-09-11 14:22:19 -0700 | [diff] [blame] | 1832 | if (!order) |
| 1833 | return; |
| 1834 | |
Andrew Morton | 7103f16 | 2013-02-22 16:32:33 -0800 | [diff] [blame] | 1835 | __compact_pgdat(pgdat, &cc); |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1836 | } |
| 1837 | |
Andrew Morton | 7103f16 | 2013-02-22 16:32:33 -0800 | [diff] [blame] | 1838 | static void compact_node(int nid) |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1839 | { |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1840 | struct compact_control cc = { |
| 1841 | .order = -1, |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 1842 | .mode = MIGRATE_SYNC, |
David Rientjes | 91ca918 | 2014-04-03 14:47:23 -0700 | [diff] [blame] | 1843 | .ignore_skip_hint = true, |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1844 | }; |
| 1845 | |
Andrew Morton | 7103f16 | 2013-02-22 16:32:33 -0800 | [diff] [blame] | 1846 | __compact_pgdat(NODE_DATA(nid), &cc); |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1847 | } |
| 1848 | |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1849 | /* Compact all nodes in the system */ |
Jason Liu | 7964c06 | 2013-01-11 14:31:47 -0800 | [diff] [blame] | 1850 | static void compact_nodes(void) |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1851 | { |
| 1852 | int nid; |
| 1853 | |
Hugh Dickins | 8575ec2 | 2012-03-21 16:33:53 -0700 | [diff] [blame] | 1854 | /* Flush pending updates to the LRU lists */ |
| 1855 | lru_add_drain_all(); |
| 1856 | |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1857 | for_each_online_node(nid) |
| 1858 | compact_node(nid); |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1859 | } |
| 1860 | |
| 1861 | /* The written value is actually unused, all memory is compacted */ |
| 1862 | int sysctl_compact_memory; |
| 1863 | |
Yaowei Bai | fec4eb2 | 2016-01-14 15:20:09 -0800 | [diff] [blame] | 1864 | /* |
| 1865 | * This is the entry point for compacting all nodes via |
| 1866 | * /proc/sys/vm/compact_memory |
| 1867 | */ |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1868 | int sysctl_compaction_handler(struct ctl_table *table, int write, |
| 1869 | void __user *buffer, size_t *length, loff_t *ppos) |
| 1870 | { |
| 1871 | if (write) |
Jason Liu | 7964c06 | 2013-01-11 14:31:47 -0800 | [diff] [blame] | 1872 | compact_nodes(); |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1873 | |
| 1874 | return 0; |
| 1875 | } |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 1876 | |
Mel Gorman | 5e77190 | 2010-05-24 14:32:31 -0700 | [diff] [blame] | 1877 | int sysctl_extfrag_handler(struct ctl_table *table, int write, |
| 1878 | void __user *buffer, size_t *length, loff_t *ppos) |
| 1879 | { |
| 1880 | proc_dointvec_minmax(table, write, buffer, length, ppos); |
| 1881 | |
| 1882 | return 0; |
| 1883 | } |
| 1884 | |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 1885 | #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) |
Rashika Kheria | 74e77fb | 2014-04-03 14:48:01 -0700 | [diff] [blame] | 1886 | static ssize_t sysfs_compact_node(struct device *dev, |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 1887 | struct device_attribute *attr, |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 1888 | const char *buf, size_t count) |
| 1889 | { |
Hugh Dickins | 8575ec2 | 2012-03-21 16:33:53 -0700 | [diff] [blame] | 1890 | int nid = dev->id; |
| 1891 | |
| 1892 | if (nid >= 0 && nid < nr_node_ids && node_online(nid)) { |
| 1893 | /* Flush pending updates to the LRU lists */ |
| 1894 | lru_add_drain_all(); |
| 1895 | |
| 1896 | compact_node(nid); |
| 1897 | } |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 1898 | |
| 1899 | return count; |
| 1900 | } |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 1901 | static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node); |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 1902 | |
| 1903 | int compaction_register_node(struct node *node) |
| 1904 | { |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 1905 | return device_create_file(&node->dev, &dev_attr_compact); |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 1906 | } |
| 1907 | |
| 1908 | void compaction_unregister_node(struct node *node) |
| 1909 | { |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 1910 | return device_remove_file(&node->dev, &dev_attr_compact); |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 1911 | } |
| 1912 | #endif /* CONFIG_SYSFS && CONFIG_NUMA */ |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1913 | |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 1914 | static inline bool kcompactd_work_requested(pg_data_t *pgdat) |
| 1915 | { |
Vlastimil Babka | 172400c | 2016-05-05 16:22:32 -0700 | [diff] [blame] | 1916 | return pgdat->kcompactd_max_order > 0 || kthread_should_stop(); |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 1917 | } |
| 1918 | |
| 1919 | static bool kcompactd_node_suitable(pg_data_t *pgdat) |
| 1920 | { |
| 1921 | int zoneid; |
| 1922 | struct zone *zone; |
| 1923 | enum zone_type classzone_idx = pgdat->kcompactd_classzone_idx; |
| 1924 | |
Chen Feng | 6cd9dc3 | 2016-05-20 16:59:02 -0700 | [diff] [blame] | 1925 | for (zoneid = 0; zoneid <= classzone_idx; zoneid++) { |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 1926 | zone = &pgdat->node_zones[zoneid]; |
| 1927 | |
| 1928 | if (!populated_zone(zone)) |
| 1929 | continue; |
| 1930 | |
| 1931 | if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0, |
| 1932 | classzone_idx) == COMPACT_CONTINUE) |
| 1933 | return true; |
| 1934 | } |
| 1935 | |
| 1936 | return false; |
| 1937 | } |
| 1938 | |
| 1939 | static void kcompactd_do_work(pg_data_t *pgdat) |
| 1940 | { |
| 1941 | /* |
| 1942 | * With no special task, compact all zones so that a page of requested |
| 1943 | * order is allocatable. |
| 1944 | */ |
| 1945 | int zoneid; |
| 1946 | struct zone *zone; |
| 1947 | struct compact_control cc = { |
| 1948 | .order = pgdat->kcompactd_max_order, |
| 1949 | .classzone_idx = pgdat->kcompactd_classzone_idx, |
| 1950 | .mode = MIGRATE_SYNC_LIGHT, |
| 1951 | .ignore_skip_hint = true, |
| 1952 | |
| 1953 | }; |
| 1954 | bool success = false; |
| 1955 | |
| 1956 | trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order, |
| 1957 | cc.classzone_idx); |
| 1958 | count_vm_event(KCOMPACTD_WAKE); |
| 1959 | |
Chen Feng | 6cd9dc3 | 2016-05-20 16:59:02 -0700 | [diff] [blame] | 1960 | for (zoneid = 0; zoneid <= cc.classzone_idx; zoneid++) { |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 1961 | int status; |
| 1962 | |
| 1963 | zone = &pgdat->node_zones[zoneid]; |
| 1964 | if (!populated_zone(zone)) |
| 1965 | continue; |
| 1966 | |
| 1967 | if (compaction_deferred(zone, cc.order)) |
| 1968 | continue; |
| 1969 | |
| 1970 | if (compaction_suitable(zone, cc.order, 0, zoneid) != |
| 1971 | COMPACT_CONTINUE) |
| 1972 | continue; |
| 1973 | |
| 1974 | cc.nr_freepages = 0; |
| 1975 | cc.nr_migratepages = 0; |
| 1976 | cc.zone = zone; |
| 1977 | INIT_LIST_HEAD(&cc.freepages); |
| 1978 | INIT_LIST_HEAD(&cc.migratepages); |
| 1979 | |
Vlastimil Babka | 172400c | 2016-05-05 16:22:32 -0700 | [diff] [blame] | 1980 | if (kthread_should_stop()) |
| 1981 | return; |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 1982 | status = compact_zone(zone, &cc); |
| 1983 | |
| 1984 | if (zone_watermark_ok(zone, cc.order, low_wmark_pages(zone), |
| 1985 | cc.classzone_idx, 0)) { |
| 1986 | success = true; |
| 1987 | compaction_defer_reset(zone, cc.order, false); |
Michal Hocko | c8f7de0 | 2016-05-20 16:56:47 -0700 | [diff] [blame] | 1988 | } else if (status == COMPACT_PARTIAL_SKIPPED || status == COMPACT_COMPLETE) { |
Vlastimil Babka | 698b1b3 | 2016-03-17 14:18:08 -0700 | [diff] [blame] | 1989 | /* |
| 1990 | * We use sync migration mode here, so we defer like |
| 1991 | * sync direct compaction does. |
| 1992 | */ |
| 1993 | defer_compaction(zone, cc.order); |
| 1994 | } |
| 1995 | |
| 1996 | VM_BUG_ON(!list_empty(&cc.freepages)); |
| 1997 | VM_BUG_ON(!list_empty(&cc.migratepages)); |
| 1998 | } |
| 1999 | |
| 2000 | /* |
| 2001 | * Regardless of success, we are done until woken up next. But remember |
| 2002 | * the requested order/classzone_idx in case it was higher/tighter than |
| 2003 | * our current ones |
| 2004 | */ |
| 2005 | if (pgdat->kcompactd_max_order <= cc.order) |
| 2006 | pgdat->kcompactd_max_order = 0; |
| 2007 | if (pgdat->kcompactd_classzone_idx >= cc.classzone_idx) |
| 2008 | pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1; |
| 2009 | } |
| 2010 | |
| 2011 | void wakeup_kcompactd(pg_data_t *pgdat, int order, int classzone_idx) |
| 2012 | { |
| 2013 | if (!order) |
| 2014 | return; |
| 2015 | |
| 2016 | if (pgdat->kcompactd_max_order < order) |
| 2017 | pgdat->kcompactd_max_order = order; |
| 2018 | |
| 2019 | if (pgdat->kcompactd_classzone_idx > classzone_idx) |
| 2020 | pgdat->kcompactd_classzone_idx = classzone_idx; |
| 2021 | |
| 2022 | if (!waitqueue_active(&pgdat->kcompactd_wait)) |
| 2023 | return; |
| 2024 | |
| 2025 | if (!kcompactd_node_suitable(pgdat)) |
| 2026 | return; |
| 2027 | |
| 2028 | trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order, |
| 2029 | classzone_idx); |
| 2030 | wake_up_interruptible(&pgdat->kcompactd_wait); |
| 2031 | } |
| 2032 | |
| 2033 | /* |
| 2034 | * The background compaction daemon, started as a kernel thread |
| 2035 | * from the init process. |
| 2036 | */ |
| 2037 | static int kcompactd(void *p) |
| 2038 | { |
| 2039 | pg_data_t *pgdat = (pg_data_t*)p; |
| 2040 | struct task_struct *tsk = current; |
| 2041 | |
| 2042 | const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); |
| 2043 | |
| 2044 | if (!cpumask_empty(cpumask)) |
| 2045 | set_cpus_allowed_ptr(tsk, cpumask); |
| 2046 | |
| 2047 | set_freezable(); |
| 2048 | |
| 2049 | pgdat->kcompactd_max_order = 0; |
| 2050 | pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1; |
| 2051 | |
| 2052 | while (!kthread_should_stop()) { |
| 2053 | trace_mm_compaction_kcompactd_sleep(pgdat->node_id); |
| 2054 | wait_event_freezable(pgdat->kcompactd_wait, |
| 2055 | kcompactd_work_requested(pgdat)); |
| 2056 | |
| 2057 | kcompactd_do_work(pgdat); |
| 2058 | } |
| 2059 | |
| 2060 | return 0; |
| 2061 | } |
| 2062 | |
| 2063 | /* |
| 2064 | * This kcompactd start function will be called by init and node-hot-add. |
| 2065 | * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added. |
| 2066 | */ |
| 2067 | int kcompactd_run(int nid) |
| 2068 | { |
| 2069 | pg_data_t *pgdat = NODE_DATA(nid); |
| 2070 | int ret = 0; |
| 2071 | |
| 2072 | if (pgdat->kcompactd) |
| 2073 | return 0; |
| 2074 | |
| 2075 | pgdat->kcompactd = kthread_run(kcompactd, pgdat, "kcompactd%d", nid); |
| 2076 | if (IS_ERR(pgdat->kcompactd)) { |
| 2077 | pr_err("Failed to start kcompactd on node %d\n", nid); |
| 2078 | ret = PTR_ERR(pgdat->kcompactd); |
| 2079 | pgdat->kcompactd = NULL; |
| 2080 | } |
| 2081 | return ret; |
| 2082 | } |
| 2083 | |
| 2084 | /* |
| 2085 | * Called by memory hotplug when all memory in a node is offlined. Caller must |
| 2086 | * hold mem_hotplug_begin/end(). |
| 2087 | */ |
| 2088 | void kcompactd_stop(int nid) |
| 2089 | { |
| 2090 | struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd; |
| 2091 | |
| 2092 | if (kcompactd) { |
| 2093 | kthread_stop(kcompactd); |
| 2094 | NODE_DATA(nid)->kcompactd = NULL; |
| 2095 | } |
| 2096 | } |
| 2097 | |
| 2098 | /* |
| 2099 | * It's optimal to keep kcompactd on the same CPUs as their memory, but |
| 2100 | * not required for correctness. So if the last cpu in a node goes |
| 2101 | * away, we get changed to run anywhere: as the first one comes back, |
| 2102 | * restore their cpu bindings. |
| 2103 | */ |
| 2104 | static int cpu_callback(struct notifier_block *nfb, unsigned long action, |
| 2105 | void *hcpu) |
| 2106 | { |
| 2107 | int nid; |
| 2108 | |
| 2109 | if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) { |
| 2110 | for_each_node_state(nid, N_MEMORY) { |
| 2111 | pg_data_t *pgdat = NODE_DATA(nid); |
| 2112 | const struct cpumask *mask; |
| 2113 | |
| 2114 | mask = cpumask_of_node(pgdat->node_id); |
| 2115 | |
| 2116 | if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) |
| 2117 | /* One of our CPUs online: restore mask */ |
| 2118 | set_cpus_allowed_ptr(pgdat->kcompactd, mask); |
| 2119 | } |
| 2120 | } |
| 2121 | return NOTIFY_OK; |
| 2122 | } |
| 2123 | |
| 2124 | static int __init kcompactd_init(void) |
| 2125 | { |
| 2126 | int nid; |
| 2127 | |
| 2128 | for_each_node_state(nid, N_MEMORY) |
| 2129 | kcompactd_run(nid); |
| 2130 | hotcpu_notifier(cpu_callback, 0); |
| 2131 | return 0; |
| 2132 | } |
| 2133 | subsys_initcall(kcompactd_init) |
| 2134 | |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 2135 | #endif /* CONFIG_COMPACTION */ |