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 | */ |
| 10 | #include <linux/swap.h> |
| 11 | #include <linux/migrate.h> |
| 12 | #include <linux/compaction.h> |
| 13 | #include <linux/mm_inline.h> |
| 14 | #include <linux/backing-dev.h> |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 15 | #include <linux/sysctl.h> |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 16 | #include <linux/sysfs.h> |
Rafael Aquini | bf6bddf | 2012-12-11 16:02:42 -0800 | [diff] [blame] | 17 | #include <linux/balloon_compaction.h> |
Minchan Kim | 194159f | 2013-02-22 16:33:58 -0800 | [diff] [blame] | 18 | #include <linux/page-isolation.h> |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 19 | #include "internal.h" |
| 20 | |
Minchan Kim | 010fc29 | 2012-12-20 15:05:06 -0800 | [diff] [blame] | 21 | #ifdef CONFIG_COMPACTION |
| 22 | static inline void count_compact_event(enum vm_event_item item) |
| 23 | { |
| 24 | count_vm_event(item); |
| 25 | } |
| 26 | |
| 27 | static inline void count_compact_events(enum vm_event_item item, long delta) |
| 28 | { |
| 29 | count_vm_events(item, delta); |
| 30 | } |
| 31 | #else |
| 32 | #define count_compact_event(item) do { } while (0) |
| 33 | #define count_compact_events(item, delta) do { } while (0) |
| 34 | #endif |
| 35 | |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 36 | #if defined CONFIG_COMPACTION || defined CONFIG_CMA |
| 37 | |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 38 | #define CREATE_TRACE_POINTS |
| 39 | #include <trace/events/compaction.h> |
| 40 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 41 | static unsigned long release_freepages(struct list_head *freelist) |
| 42 | { |
| 43 | struct page *page, *next; |
| 44 | unsigned long count = 0; |
| 45 | |
| 46 | list_for_each_entry_safe(page, next, freelist, lru) { |
| 47 | list_del(&page->lru); |
| 48 | __free_page(page); |
| 49 | count++; |
| 50 | } |
| 51 | |
| 52 | return count; |
| 53 | } |
| 54 | |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 55 | static void map_pages(struct list_head *list) |
| 56 | { |
| 57 | struct page *page; |
| 58 | |
| 59 | list_for_each_entry(page, list, lru) { |
| 60 | arch_alloc_page(page, 0); |
| 61 | kernel_map_pages(page, 1, 1); |
| 62 | } |
| 63 | } |
| 64 | |
Michal Nazarewicz | 47118af | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 65 | static inline bool migrate_async_suitable(int migratetype) |
| 66 | { |
| 67 | return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE; |
| 68 | } |
| 69 | |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 70 | /* |
| 71 | * Check that the whole (or subset of) a pageblock given by the interval of |
| 72 | * [start_pfn, end_pfn) is valid and within the same zone, before scanning it |
| 73 | * with the migration of free compaction scanner. The scanners then need to |
| 74 | * use only pfn_valid_within() check for arches that allow holes within |
| 75 | * pageblocks. |
| 76 | * |
| 77 | * Return struct page pointer of start_pfn, or NULL if checks were not passed. |
| 78 | * |
| 79 | * It's possible on some configurations to have a setup like node0 node1 node0 |
| 80 | * i.e. it's possible that all pages within a zones range of pages do not |
| 81 | * belong to a single zone. We assume that a border between node0 and node1 |
| 82 | * can occur within a single pageblock, but not a node0 node1 node0 |
| 83 | * interleaving within a single pageblock. It is therefore sufficient to check |
| 84 | * the first and last page of a pageblock and avoid checking each individual |
| 85 | * page in a pageblock. |
| 86 | */ |
| 87 | static struct page *pageblock_pfn_to_page(unsigned long start_pfn, |
| 88 | unsigned long end_pfn, struct zone *zone) |
| 89 | { |
| 90 | struct page *start_page; |
| 91 | struct page *end_page; |
| 92 | |
| 93 | /* end_pfn is one past the range we are checking */ |
| 94 | end_pfn--; |
| 95 | |
| 96 | if (!pfn_valid(start_pfn) || !pfn_valid(end_pfn)) |
| 97 | return NULL; |
| 98 | |
| 99 | start_page = pfn_to_page(start_pfn); |
| 100 | |
| 101 | if (page_zone(start_page) != zone) |
| 102 | return NULL; |
| 103 | |
| 104 | end_page = pfn_to_page(end_pfn); |
| 105 | |
| 106 | /* This gives a shorter code than deriving page_zone(end_page) */ |
| 107 | if (page_zone_id(start_page) != page_zone_id(end_page)) |
| 108 | return NULL; |
| 109 | |
| 110 | return start_page; |
| 111 | } |
| 112 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 113 | #ifdef CONFIG_COMPACTION |
| 114 | /* Returns true if the pageblock should be scanned for pages to isolate. */ |
| 115 | static inline bool isolation_suitable(struct compact_control *cc, |
| 116 | struct page *page) |
| 117 | { |
| 118 | if (cc->ignore_skip_hint) |
| 119 | return true; |
| 120 | |
| 121 | return !get_pageblock_skip(page); |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * This function is called to clear all cached information on pageblocks that |
| 126 | * should be skipped for page isolation when the migrate and free page scanner |
| 127 | * meet. |
| 128 | */ |
Mel Gorman | 6299702 | 2012-10-08 16:32:47 -0700 | [diff] [blame] | 129 | static void __reset_isolation_suitable(struct zone *zone) |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 130 | { |
| 131 | unsigned long start_pfn = zone->zone_start_pfn; |
Cody P Schafer | 108bcc9 | 2013-02-22 16:35:23 -0800 | [diff] [blame] | 132 | unsigned long end_pfn = zone_end_pfn(zone); |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 133 | unsigned long pfn; |
| 134 | |
David Rientjes | 35979ef | 2014-06-04 16:08:27 -0700 | [diff] [blame] | 135 | zone->compact_cached_migrate_pfn[0] = start_pfn; |
| 136 | zone->compact_cached_migrate_pfn[1] = start_pfn; |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 137 | zone->compact_cached_free_pfn = end_pfn; |
Mel Gorman | 6299702 | 2012-10-08 16:32:47 -0700 | [diff] [blame] | 138 | zone->compact_blockskip_flush = false; |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 139 | |
| 140 | /* Walk the zone and mark every pageblock as suitable for isolation */ |
| 141 | for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) { |
| 142 | struct page *page; |
| 143 | |
| 144 | cond_resched(); |
| 145 | |
| 146 | if (!pfn_valid(pfn)) |
| 147 | continue; |
| 148 | |
| 149 | page = pfn_to_page(pfn); |
| 150 | if (zone != page_zone(page)) |
| 151 | continue; |
| 152 | |
| 153 | clear_pageblock_skip(page); |
| 154 | } |
| 155 | } |
| 156 | |
Mel Gorman | 6299702 | 2012-10-08 16:32:47 -0700 | [diff] [blame] | 157 | void reset_isolation_suitable(pg_data_t *pgdat) |
| 158 | { |
| 159 | int zoneid; |
| 160 | |
| 161 | for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { |
| 162 | struct zone *zone = &pgdat->node_zones[zoneid]; |
| 163 | if (!populated_zone(zone)) |
| 164 | continue; |
| 165 | |
| 166 | /* Only flush if a full compaction finished recently */ |
| 167 | if (zone->compact_blockskip_flush) |
| 168 | __reset_isolation_suitable(zone); |
| 169 | } |
| 170 | } |
| 171 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 172 | /* |
| 173 | * 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] | 174 | * future. The information is later cleared by __reset_isolation_suitable(). |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 175 | */ |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 176 | static void update_pageblock_skip(struct compact_control *cc, |
| 177 | struct page *page, unsigned long nr_isolated, |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 178 | bool migrate_scanner) |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 179 | { |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 180 | struct zone *zone = cc->zone; |
David Rientjes | 35979ef | 2014-06-04 16:08:27 -0700 | [diff] [blame] | 181 | unsigned long pfn; |
Joonsoo Kim | 6815bf3 | 2013-12-18 17:08:52 -0800 | [diff] [blame] | 182 | |
| 183 | if (cc->ignore_skip_hint) |
| 184 | return; |
| 185 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 186 | if (!page) |
| 187 | return; |
| 188 | |
David Rientjes | 35979ef | 2014-06-04 16:08:27 -0700 | [diff] [blame] | 189 | if (nr_isolated) |
| 190 | return; |
| 191 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 192 | set_pageblock_skip(page); |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 193 | |
David Rientjes | 35979ef | 2014-06-04 16:08:27 -0700 | [diff] [blame] | 194 | pfn = page_to_pfn(page); |
| 195 | |
| 196 | /* Update where async and sync compaction should restart */ |
| 197 | if (migrate_scanner) { |
| 198 | if (cc->finished_update_migrate) |
| 199 | return; |
| 200 | if (pfn > zone->compact_cached_migrate_pfn[0]) |
| 201 | zone->compact_cached_migrate_pfn[0] = pfn; |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 202 | if (cc->mode != MIGRATE_ASYNC && |
| 203 | pfn > zone->compact_cached_migrate_pfn[1]) |
David Rientjes | 35979ef | 2014-06-04 16:08:27 -0700 | [diff] [blame] | 204 | zone->compact_cached_migrate_pfn[1] = pfn; |
| 205 | } else { |
| 206 | if (cc->finished_update_free) |
| 207 | return; |
| 208 | if (pfn < zone->compact_cached_free_pfn) |
| 209 | zone->compact_cached_free_pfn = pfn; |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 210 | } |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 211 | } |
| 212 | #else |
| 213 | static inline bool isolation_suitable(struct compact_control *cc, |
| 214 | struct page *page) |
| 215 | { |
| 216 | return true; |
| 217 | } |
| 218 | |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 219 | static void update_pageblock_skip(struct compact_control *cc, |
| 220 | struct page *page, unsigned long nr_isolated, |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 221 | bool migrate_scanner) |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 222 | { |
| 223 | } |
| 224 | #endif /* CONFIG_COMPACTION */ |
| 225 | |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame^] | 226 | static int should_release_lock(spinlock_t *lock) |
Mel Gorman | 2a1402a | 2012-10-08 16:32:33 -0700 | [diff] [blame] | 227 | { |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame^] | 228 | /* |
| 229 | * Sched contention has higher priority here as we may potentially |
| 230 | * have to abort whole compaction ASAP. Returning with lock contention |
| 231 | * means we will try another zone, and further decisions are |
| 232 | * influenced only when all zones are lock contended. That means |
| 233 | * potentially missing a lock contention is less critical. |
| 234 | */ |
| 235 | if (need_resched()) |
| 236 | return COMPACT_CONTENDED_SCHED; |
| 237 | else if (spin_is_contended(lock)) |
| 238 | return COMPACT_CONTENDED_LOCK; |
| 239 | |
| 240 | return COMPACT_CONTENDED_NONE; |
Mel Gorman | 2a1402a | 2012-10-08 16:32:33 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 243 | /* |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 244 | * Compaction requires the taking of some coarse locks that are potentially |
| 245 | * very heavily contended. Check if the process needs to be scheduled or |
| 246 | * if the lock is contended. For async compaction, back out in the event |
| 247 | * if contention is severe. For sync compaction, schedule. |
| 248 | * |
| 249 | * Returns true if the lock is held. |
| 250 | * Returns false if the lock is released and compaction should abort |
| 251 | */ |
| 252 | static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags, |
| 253 | bool locked, struct compact_control *cc) |
| 254 | { |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame^] | 255 | int contended = should_release_lock(lock); |
| 256 | |
| 257 | if (contended) { |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 258 | if (locked) { |
| 259 | spin_unlock_irqrestore(lock, *flags); |
| 260 | locked = false; |
| 261 | } |
| 262 | |
| 263 | /* async aborts if taking too long or contended */ |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 264 | if (cc->mode == MIGRATE_ASYNC) { |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame^] | 265 | cc->contended = contended; |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 266 | return false; |
| 267 | } |
| 268 | |
| 269 | cond_resched(); |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | if (!locked) |
| 273 | spin_lock_irqsave(lock, *flags); |
| 274 | return true; |
| 275 | } |
| 276 | |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 277 | /* |
| 278 | * Aside from avoiding lock contention, compaction also periodically checks |
| 279 | * need_resched() and either schedules in sync compaction or aborts async |
| 280 | * compaction. This is similar to what compact_checklock_irqsave() does, but |
| 281 | * is used where no lock is concerned. |
| 282 | * |
| 283 | * Returns false when no scheduling was needed, or sync compaction scheduled. |
| 284 | * Returns true when async compaction should abort. |
| 285 | */ |
| 286 | static inline bool compact_should_abort(struct compact_control *cc) |
| 287 | { |
| 288 | /* async compaction aborts if contended */ |
| 289 | if (need_resched()) { |
| 290 | if (cc->mode == MIGRATE_ASYNC) { |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame^] | 291 | cc->contended = COMPACT_CONTENDED_SCHED; |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 292 | return true; |
| 293 | } |
| 294 | |
| 295 | cond_resched(); |
| 296 | } |
| 297 | |
| 298 | return false; |
| 299 | } |
| 300 | |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 301 | /* Returns true if the page is within a block suitable for migration to */ |
| 302 | static bool suitable_migration_target(struct page *page) |
| 303 | { |
Joonsoo Kim | 7d348b9 | 2014-04-07 15:37:03 -0700 | [diff] [blame] | 304 | /* If the page is a large free page, then disallow migration */ |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 305 | if (PageBuddy(page) && page_order(page) >= pageblock_order) |
Joonsoo Kim | 7d348b9 | 2014-04-07 15:37:03 -0700 | [diff] [blame] | 306 | return false; |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 307 | |
| 308 | /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */ |
Joonsoo Kim | 7d348b9 | 2014-04-07 15:37:03 -0700 | [diff] [blame] | 309 | if (migrate_async_suitable(get_pageblock_migratetype(page))) |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 310 | return true; |
| 311 | |
| 312 | /* Otherwise skip the block */ |
| 313 | return false; |
| 314 | } |
| 315 | |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 316 | /* |
Jerome Marchand | 9e4be47 | 2013-11-12 15:07:12 -0800 | [diff] [blame] | 317 | * Isolate free pages onto a private freelist. If @strict is true, will abort |
| 318 | * returning 0 on any invalid PFNs or non-free pages inside of the pageblock |
| 319 | * (even though it may still end up isolating some pages). |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 320 | */ |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 321 | static unsigned long isolate_freepages_block(struct compact_control *cc, |
| 322 | unsigned long blockpfn, |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 323 | unsigned long end_pfn, |
| 324 | struct list_head *freelist, |
| 325 | bool strict) |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 326 | { |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 327 | int nr_scanned = 0, total_isolated = 0; |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 328 | struct page *cursor, *valid_page = NULL; |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 329 | unsigned long flags; |
| 330 | bool locked = false; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 331 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 332 | cursor = pfn_to_page(blockpfn); |
| 333 | |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 334 | /* Isolate free pages. */ |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 335 | for (; blockpfn < end_pfn; blockpfn++, cursor++) { |
| 336 | int isolated, i; |
| 337 | struct page *page = cursor; |
| 338 | |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 339 | nr_scanned++; |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 340 | if (!pfn_valid_within(blockpfn)) |
Laura Abbott | 2af120b | 2014-03-10 15:49:44 -0700 | [diff] [blame] | 341 | goto isolate_fail; |
| 342 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 343 | if (!valid_page) |
| 344 | valid_page = page; |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 345 | if (!PageBuddy(page)) |
Laura Abbott | 2af120b | 2014-03-10 15:49:44 -0700 | [diff] [blame] | 346 | goto isolate_fail; |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 347 | |
| 348 | /* |
| 349 | * The zone lock must be held to isolate freepages. |
| 350 | * Unfortunately this is a very coarse lock and can be |
| 351 | * heavily contended if there are parallel allocations |
| 352 | * or parallel compactions. For async compaction do not |
| 353 | * spin on the lock and we acquire the lock as late as |
| 354 | * possible. |
| 355 | */ |
| 356 | locked = compact_checklock_irqsave(&cc->zone->lock, &flags, |
| 357 | locked, cc); |
| 358 | if (!locked) |
| 359 | break; |
| 360 | |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 361 | /* Recheck this is a buddy page under lock */ |
| 362 | if (!PageBuddy(page)) |
Laura Abbott | 2af120b | 2014-03-10 15:49:44 -0700 | [diff] [blame] | 363 | goto isolate_fail; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 364 | |
| 365 | /* Found a free page, break it into order-0 pages */ |
| 366 | isolated = split_free_page(page); |
| 367 | total_isolated += isolated; |
| 368 | for (i = 0; i < isolated; i++) { |
| 369 | list_add(&page->lru, freelist); |
| 370 | page++; |
| 371 | } |
| 372 | |
| 373 | /* If a page was split, advance to the end of it */ |
| 374 | if (isolated) { |
| 375 | blockpfn += isolated - 1; |
| 376 | cursor += isolated - 1; |
Laura Abbott | 2af120b | 2014-03-10 15:49:44 -0700 | [diff] [blame] | 377 | continue; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 378 | } |
Laura Abbott | 2af120b | 2014-03-10 15:49:44 -0700 | [diff] [blame] | 379 | |
| 380 | isolate_fail: |
| 381 | if (strict) |
| 382 | break; |
| 383 | else |
| 384 | continue; |
| 385 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 388 | trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated); |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 389 | |
| 390 | /* |
| 391 | * If strict isolation is requested by CMA then check that all the |
| 392 | * pages requested were isolated. If there were any failures, 0 is |
| 393 | * returned and CMA will fail. |
| 394 | */ |
Laura Abbott | 2af120b | 2014-03-10 15:49:44 -0700 | [diff] [blame] | 395 | if (strict && blockpfn < end_pfn) |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 396 | total_isolated = 0; |
| 397 | |
| 398 | if (locked) |
| 399 | spin_unlock_irqrestore(&cc->zone->lock, flags); |
| 400 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 401 | /* Update the pageblock-skip if the whole pageblock was scanned */ |
| 402 | if (blockpfn == end_pfn) |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 403 | update_pageblock_skip(cc, valid_page, total_isolated, false); |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 404 | |
Minchan Kim | 010fc29 | 2012-12-20 15:05:06 -0800 | [diff] [blame] | 405 | count_compact_events(COMPACTFREE_SCANNED, nr_scanned); |
Mel Gorman | 397487d | 2012-10-19 12:00:10 +0100 | [diff] [blame] | 406 | if (total_isolated) |
Minchan Kim | 010fc29 | 2012-12-20 15:05:06 -0800 | [diff] [blame] | 407 | count_compact_events(COMPACTISOLATED, total_isolated); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 408 | return total_isolated; |
| 409 | } |
| 410 | |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 411 | /** |
| 412 | * isolate_freepages_range() - isolate free pages. |
| 413 | * @start_pfn: The first PFN to start isolating. |
| 414 | * @end_pfn: The one-past-last PFN. |
| 415 | * |
| 416 | * Non-free pages, invalid PFNs, or zone boundaries within the |
| 417 | * [start_pfn, end_pfn) range are considered errors, cause function to |
| 418 | * undo its actions and return zero. |
| 419 | * |
| 420 | * Otherwise, function returns one-past-the-last PFN of isolated page |
| 421 | * (which may be greater then end_pfn if end fell in a middle of |
| 422 | * a free page). |
| 423 | */ |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 424 | unsigned long |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 425 | isolate_freepages_range(struct compact_control *cc, |
| 426 | unsigned long start_pfn, unsigned long end_pfn) |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 427 | { |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 428 | unsigned long isolated, pfn, block_end_pfn; |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 429 | LIST_HEAD(freelist); |
| 430 | |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 431 | pfn = start_pfn; |
| 432 | block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 433 | |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 434 | for (; pfn < end_pfn; pfn += isolated, |
| 435 | block_end_pfn += pageblock_nr_pages) { |
| 436 | |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 437 | block_end_pfn = min(block_end_pfn, end_pfn); |
| 438 | |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 439 | if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone)) |
| 440 | break; |
| 441 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 442 | isolated = isolate_freepages_block(cc, pfn, block_end_pfn, |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 443 | &freelist, true); |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 444 | |
| 445 | /* |
| 446 | * In strict mode, isolate_freepages_block() returns 0 if |
| 447 | * there are any holes in the block (ie. invalid PFNs or |
| 448 | * non-free pages). |
| 449 | */ |
| 450 | if (!isolated) |
| 451 | break; |
| 452 | |
| 453 | /* |
| 454 | * If we managed to isolate pages, it is always (1 << n) * |
| 455 | * pageblock_nr_pages for some non-negative n. (Max order |
| 456 | * page may span two pageblocks). |
| 457 | */ |
| 458 | } |
| 459 | |
| 460 | /* split_free_page does not map the pages */ |
| 461 | map_pages(&freelist); |
| 462 | |
| 463 | if (pfn < end_pfn) { |
| 464 | /* Loop terminated early, cleanup. */ |
| 465 | release_freepages(&freelist); |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | /* We don't use freelists for anything. */ |
| 470 | return pfn; |
| 471 | } |
| 472 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 473 | /* Update the number of anon and file isolated pages in the zone */ |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 474 | static void acct_isolated(struct zone *zone, struct compact_control *cc) |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 475 | { |
| 476 | struct page *page; |
Minchan Kim | b9e84ac | 2011-10-31 17:06:44 -0700 | [diff] [blame] | 477 | unsigned int count[2] = { 0, }; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 478 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 479 | if (list_empty(&cc->migratepages)) |
| 480 | return; |
| 481 | |
Minchan Kim | b9e84ac | 2011-10-31 17:06:44 -0700 | [diff] [blame] | 482 | list_for_each_entry(page, &cc->migratepages, lru) |
| 483 | count[!!page_is_file_cache(page)]++; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 484 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 485 | mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]); |
| 486 | mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | /* Similar to reclaim, but different enough that they don't share logic */ |
| 490 | static bool too_many_isolated(struct zone *zone) |
| 491 | { |
Minchan Kim | bc69304 | 2010-09-09 16:38:00 -0700 | [diff] [blame] | 492 | unsigned long active, inactive, isolated; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 493 | |
| 494 | inactive = zone_page_state(zone, NR_INACTIVE_FILE) + |
| 495 | zone_page_state(zone, NR_INACTIVE_ANON); |
Minchan Kim | bc69304 | 2010-09-09 16:38:00 -0700 | [diff] [blame] | 496 | active = zone_page_state(zone, NR_ACTIVE_FILE) + |
| 497 | zone_page_state(zone, NR_ACTIVE_ANON); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 498 | isolated = zone_page_state(zone, NR_ISOLATED_FILE) + |
| 499 | zone_page_state(zone, NR_ISOLATED_ANON); |
| 500 | |
Minchan Kim | bc69304 | 2010-09-09 16:38:00 -0700 | [diff] [blame] | 501 | return isolated > (inactive + active) / 2; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 504 | /** |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 505 | * isolate_migratepages_block() - isolate all migrate-able pages within |
| 506 | * a single pageblock |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 507 | * @cc: Compaction control structure. |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 508 | * @low_pfn: The first PFN to isolate |
| 509 | * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock |
| 510 | * @isolate_mode: Isolation mode to be used. |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 511 | * |
| 512 | * Isolate all pages that can be migrated from the range specified by |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 513 | * [low_pfn, end_pfn). The range is expected to be within same pageblock. |
| 514 | * Returns zero if there is a fatal signal pending, otherwise PFN of the |
| 515 | * first page that was not scanned (which may be both less, equal to or more |
| 516 | * than end_pfn). |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 517 | * |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 518 | * The pages are isolated on cc->migratepages list (not required to be empty), |
| 519 | * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field |
| 520 | * is neither read nor updated. |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 521 | */ |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 522 | static unsigned long |
| 523 | isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, |
| 524 | unsigned long end_pfn, isolate_mode_t isolate_mode) |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 525 | { |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 526 | struct zone *zone = cc->zone; |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 527 | unsigned long nr_scanned = 0, nr_isolated = 0; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 528 | struct list_head *migratelist = &cc->migratepages; |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 529 | struct lruvec *lruvec; |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 530 | unsigned long flags; |
Mel Gorman | 2a1402a | 2012-10-08 16:32:33 -0700 | [diff] [blame] | 531 | bool locked = false; |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 532 | struct page *page = NULL, *valid_page = NULL; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 533 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 534 | /* |
| 535 | * Ensure that there are not too many pages isolated from the LRU |
| 536 | * list by either parallel reclaimers or compaction. If there are, |
| 537 | * delay for some time until fewer pages are isolated |
| 538 | */ |
| 539 | while (unlikely(too_many_isolated(zone))) { |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 540 | /* async migration should just abort */ |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 541 | if (cc->mode == MIGRATE_ASYNC) |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 542 | return 0; |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 543 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 544 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
| 545 | |
| 546 | if (fatal_signal_pending(current)) |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 547 | return 0; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 550 | if (compact_should_abort(cc)) |
| 551 | return 0; |
David Rientjes | aeef4b8 | 2014-06-04 16:08:31 -0700 | [diff] [blame] | 552 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 553 | /* Time to isolate some pages for migration */ |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 554 | for (; low_pfn < end_pfn; low_pfn++) { |
Andrea Arcangeli | b2eef8c | 2011-03-22 16:33:10 -0700 | [diff] [blame] | 555 | /* give a chance to irqs before checking need_resched() */ |
Joonsoo Kim | be1aa03 | 2014-04-07 15:37:05 -0700 | [diff] [blame] | 556 | if (locked && !(low_pfn % SWAP_CLUSTER_MAX)) { |
Mel Gorman | 2a1402a | 2012-10-08 16:32:33 -0700 | [diff] [blame] | 557 | if (should_release_lock(&zone->lru_lock)) { |
| 558 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
| 559 | locked = false; |
| 560 | } |
Andrea Arcangeli | b2eef8c | 2011-03-22 16:33:10 -0700 | [diff] [blame] | 561 | } |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 562 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 563 | if (!pfn_valid_within(low_pfn)) |
| 564 | continue; |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 565 | nr_scanned++; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 566 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 567 | page = pfn_to_page(low_pfn); |
Mel Gorman | dc90860 | 2012-02-08 17:13:38 -0800 | [diff] [blame] | 568 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 569 | if (!valid_page) |
| 570 | valid_page = page; |
| 571 | |
Mel Gorman | 6c14466 | 2014-01-23 15:53:38 -0800 | [diff] [blame] | 572 | /* |
| 573 | * Skip if free. page_order cannot be used without zone->lock |
| 574 | * as nothing prevents parallel allocations or buddy merging. |
| 575 | */ |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 576 | if (PageBuddy(page)) |
| 577 | continue; |
| 578 | |
Mel Gorman | 9927af74 | 2011-01-13 15:45:59 -0800 | [diff] [blame] | 579 | /* |
Rafael Aquini | bf6bddf | 2012-12-11 16:02:42 -0800 | [diff] [blame] | 580 | * Check may be lockless but that's ok as we recheck later. |
| 581 | * It's possible to migrate LRU pages and balloon pages |
| 582 | * Skip any other type of page |
| 583 | */ |
| 584 | if (!PageLRU(page)) { |
| 585 | if (unlikely(balloon_page_movable(page))) { |
| 586 | if (locked && balloon_page_isolate(page)) { |
| 587 | /* Successfully isolated */ |
Joonsoo Kim | b6c7501 | 2014-04-07 15:37:07 -0700 | [diff] [blame] | 588 | goto isolate_success; |
Rafael Aquini | bf6bddf | 2012-12-11 16:02:42 -0800 | [diff] [blame] | 589 | } |
| 590 | } |
Andrea Arcangeli | bc83501 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 591 | continue; |
Rafael Aquini | bf6bddf | 2012-12-11 16:02:42 -0800 | [diff] [blame] | 592 | } |
Andrea Arcangeli | bc83501 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 593 | |
| 594 | /* |
Mel Gorman | 2a1402a | 2012-10-08 16:32:33 -0700 | [diff] [blame] | 595 | * PageLRU is set. lru_lock normally excludes isolation |
| 596 | * splitting and collapsing (collapsing has already happened |
| 597 | * if PageLRU is set) but the lock is not necessarily taken |
| 598 | * here and it is wasteful to take it just to check transhuge. |
| 599 | * Check TransHuge without lock and skip the whole pageblock if |
| 600 | * it's either a transhuge or hugetlbfs page, as calling |
| 601 | * compound_order() without preventing THP from splitting the |
| 602 | * page underneath us may return surprising results. |
Andrea Arcangeli | bc83501 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 603 | */ |
| 604 | if (PageTransHuge(page)) { |
Mel Gorman | 2a1402a | 2012-10-08 16:32:33 -0700 | [diff] [blame] | 605 | if (!locked) |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 606 | low_pfn = ALIGN(low_pfn + 1, |
| 607 | pageblock_nr_pages) - 1; |
| 608 | else |
| 609 | low_pfn += (1 << compound_order(page)) - 1; |
| 610 | |
Mel Gorman | 2a1402a | 2012-10-08 16:32:33 -0700 | [diff] [blame] | 611 | continue; |
| 612 | } |
| 613 | |
David Rientjes | 119d6d5 | 2014-04-03 14:48:00 -0700 | [diff] [blame] | 614 | /* |
| 615 | * Migration will fail if an anonymous page is pinned in memory, |
| 616 | * so avoid taking lru_lock and isolating it unnecessarily in an |
| 617 | * admittedly racy check. |
| 618 | */ |
| 619 | if (!page_mapping(page) && |
| 620 | page_count(page) > page_mapcount(page)) |
| 621 | continue; |
| 622 | |
Mel Gorman | 2a1402a | 2012-10-08 16:32:33 -0700 | [diff] [blame] | 623 | /* Check if it is ok to still hold the lock */ |
| 624 | locked = compact_checklock_irqsave(&zone->lru_lock, &flags, |
| 625 | locked, cc); |
| 626 | if (!locked || fatal_signal_pending(current)) |
| 627 | break; |
| 628 | |
| 629 | /* Recheck PageLRU and PageTransHuge under lock */ |
| 630 | if (!PageLRU(page)) |
| 631 | continue; |
| 632 | if (PageTransHuge(page)) { |
Andrea Arcangeli | bc83501 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 633 | low_pfn += (1 << compound_order(page)) - 1; |
| 634 | continue; |
| 635 | } |
| 636 | |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 637 | lruvec = mem_cgroup_page_lruvec(page, zone); |
| 638 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 639 | /* Try isolate the page */ |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 640 | if (__isolate_lru_page(page, isolate_mode) != 0) |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 641 | continue; |
| 642 | |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 643 | VM_BUG_ON_PAGE(PageTransCompound(page), page); |
Andrea Arcangeli | bc83501 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 644 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 645 | /* Successfully isolated */ |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 646 | del_page_from_lru_list(page, lruvec, page_lru(page)); |
Joonsoo Kim | b6c7501 | 2014-04-07 15:37:07 -0700 | [diff] [blame] | 647 | |
| 648 | isolate_success: |
| 649 | cc->finished_update_migrate = true; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 650 | list_add(&page->lru, migratelist); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 651 | cc->nr_migratepages++; |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 652 | nr_isolated++; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 653 | |
| 654 | /* Avoid isolating too much */ |
Hillf Danton | 31b8384 | 2012-01-10 15:07:59 -0800 | [diff] [blame] | 655 | if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) { |
| 656 | ++low_pfn; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 657 | break; |
Hillf Danton | 31b8384 | 2012-01-10 15:07:59 -0800 | [diff] [blame] | 658 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 659 | } |
| 660 | |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 661 | if (locked) |
| 662 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 663 | |
Vlastimil Babka | 50b5b09 | 2014-01-21 15:51:10 -0800 | [diff] [blame] | 664 | /* |
| 665 | * Update the pageblock-skip information and cached scanner pfn, |
| 666 | * if the whole pageblock was scanned without isolating any page. |
Vlastimil Babka | 50b5b09 | 2014-01-21 15:51:10 -0800 | [diff] [blame] | 667 | */ |
David Rientjes | 35979ef | 2014-06-04 16:08:27 -0700 | [diff] [blame] | 668 | if (low_pfn == end_pfn) |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 669 | update_pageblock_skip(cc, valid_page, nr_isolated, true); |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 670 | |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 671 | trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated); |
| 672 | |
Minchan Kim | 010fc29 | 2012-12-20 15:05:06 -0800 | [diff] [blame] | 673 | count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned); |
Mel Gorman | 397487d | 2012-10-19 12:00:10 +0100 | [diff] [blame] | 674 | if (nr_isolated) |
Minchan Kim | 010fc29 | 2012-12-20 15:05:06 -0800 | [diff] [blame] | 675 | count_compact_events(COMPACTISOLATED, nr_isolated); |
Mel Gorman | 397487d | 2012-10-19 12:00:10 +0100 | [diff] [blame] | 676 | |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 677 | return low_pfn; |
| 678 | } |
| 679 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 680 | /** |
| 681 | * isolate_migratepages_range() - isolate migrate-able pages in a PFN range |
| 682 | * @cc: Compaction control structure. |
| 683 | * @start_pfn: The first PFN to start isolating. |
| 684 | * @end_pfn: The one-past-last PFN. |
| 685 | * |
| 686 | * Returns zero if isolation fails fatally due to e.g. pending signal. |
| 687 | * Otherwise, function returns one-past-the-last PFN of isolated page |
| 688 | * (which may be greater than end_pfn if end fell in a middle of a THP page). |
| 689 | */ |
| 690 | unsigned long |
| 691 | isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn, |
| 692 | unsigned long end_pfn) |
| 693 | { |
| 694 | unsigned long pfn, block_end_pfn; |
| 695 | |
| 696 | /* Scan block by block. First and last block may be incomplete */ |
| 697 | pfn = start_pfn; |
| 698 | block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); |
| 699 | |
| 700 | for (; pfn < end_pfn; pfn = block_end_pfn, |
| 701 | block_end_pfn += pageblock_nr_pages) { |
| 702 | |
| 703 | block_end_pfn = min(block_end_pfn, end_pfn); |
| 704 | |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 705 | if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone)) |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 706 | continue; |
| 707 | |
| 708 | pfn = isolate_migratepages_block(cc, pfn, block_end_pfn, |
| 709 | ISOLATE_UNEVICTABLE); |
| 710 | |
| 711 | /* |
| 712 | * In case of fatal failure, release everything that might |
| 713 | * have been isolated in the previous iteration, and signal |
| 714 | * the failure back to caller. |
| 715 | */ |
| 716 | if (!pfn) { |
| 717 | putback_movable_pages(&cc->migratepages); |
| 718 | cc->nr_migratepages = 0; |
| 719 | break; |
| 720 | } |
| 721 | } |
| 722 | acct_isolated(cc->zone, cc); |
| 723 | |
| 724 | return pfn; |
| 725 | } |
| 726 | |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 727 | #endif /* CONFIG_COMPACTION || CONFIG_CMA */ |
| 728 | #ifdef CONFIG_COMPACTION |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 729 | /* |
| 730 | * Based on information in the current compact_control, find blocks |
| 731 | * suitable for isolating free pages from and then isolate them. |
| 732 | */ |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 733 | static void isolate_freepages(struct compact_control *cc) |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 734 | { |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 735 | struct zone *zone = cc->zone; |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 736 | struct page *page; |
Vlastimil Babka | c96b9e5 | 2014-06-04 16:07:26 -0700 | [diff] [blame] | 737 | unsigned long block_start_pfn; /* start of current pageblock */ |
| 738 | unsigned long block_end_pfn; /* end of current pageblock */ |
| 739 | unsigned long low_pfn; /* lowest pfn scanner is able to scan */ |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 740 | int nr_freepages = cc->nr_freepages; |
| 741 | struct list_head *freelist = &cc->freepages; |
| 742 | |
| 743 | /* |
| 744 | * Initialise the free scanner. The starting point is where we last |
Vlastimil Babka | 49e068f | 2014-05-06 12:50:03 -0700 | [diff] [blame] | 745 | * successfully isolated from, zone-cached value, or the end of the |
| 746 | * zone when isolating for the first time. We need this aligned to |
Vlastimil Babka | c96b9e5 | 2014-06-04 16:07:26 -0700 | [diff] [blame] | 747 | * the pageblock boundary, because we do |
| 748 | * block_start_pfn -= pageblock_nr_pages in the for loop. |
| 749 | * For ending point, take care when isolating in last pageblock of a |
| 750 | * a zone which ends in the middle of a pageblock. |
Vlastimil Babka | 49e068f | 2014-05-06 12:50:03 -0700 | [diff] [blame] | 751 | * The low boundary is the end of the pageblock the migration scanner |
| 752 | * is using. |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 753 | */ |
Vlastimil Babka | c96b9e5 | 2014-06-04 16:07:26 -0700 | [diff] [blame] | 754 | block_start_pfn = cc->free_pfn & ~(pageblock_nr_pages-1); |
| 755 | block_end_pfn = min(block_start_pfn + pageblock_nr_pages, |
| 756 | zone_end_pfn(zone)); |
Vlastimil Babka | 7ed695e | 2014-01-21 15:51:09 -0800 | [diff] [blame] | 757 | low_pfn = ALIGN(cc->migrate_pfn + 1, pageblock_nr_pages); |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 758 | |
| 759 | /* |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 760 | * Isolate free pages until enough are available to migrate the |
| 761 | * pages on cc->migratepages. We stop searching if the migrate |
| 762 | * and free page scanners meet or enough free pages are isolated. |
| 763 | */ |
Vlastimil Babka | c96b9e5 | 2014-06-04 16:07:26 -0700 | [diff] [blame] | 764 | for (; block_start_pfn >= low_pfn && cc->nr_migratepages > nr_freepages; |
| 765 | block_end_pfn = block_start_pfn, |
| 766 | block_start_pfn -= pageblock_nr_pages) { |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 767 | unsigned long isolated; |
| 768 | |
David Rientjes | f6ea3ad | 2013-09-30 13:45:03 -0700 | [diff] [blame] | 769 | /* |
| 770 | * This can iterate a massively long zone without finding any |
| 771 | * suitable migration targets, so periodically check if we need |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 772 | * to schedule, or even abort async compaction. |
David Rientjes | f6ea3ad | 2013-09-30 13:45:03 -0700 | [diff] [blame] | 773 | */ |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 774 | if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) |
| 775 | && compact_should_abort(cc)) |
| 776 | break; |
David Rientjes | f6ea3ad | 2013-09-30 13:45:03 -0700 | [diff] [blame] | 777 | |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 778 | page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, |
| 779 | zone); |
| 780 | if (!page) |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 781 | continue; |
| 782 | |
| 783 | /* Check the block is suitable for migration */ |
Linus Torvalds | 68e3e92 | 2012-06-03 20:05:57 -0700 | [diff] [blame] | 784 | if (!suitable_migration_target(page)) |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 785 | continue; |
Linus Torvalds | 68e3e92 | 2012-06-03 20:05:57 -0700 | [diff] [blame] | 786 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 787 | /* If isolation recently failed, do not retry */ |
| 788 | if (!isolation_suitable(cc, page)) |
| 789 | continue; |
| 790 | |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 791 | /* Found a block suitable for isolating free pages from */ |
Vlastimil Babka | e9ade56 | 2014-06-04 16:08:34 -0700 | [diff] [blame] | 792 | cc->free_pfn = block_start_pfn; |
Vlastimil Babka | c96b9e5 | 2014-06-04 16:07:26 -0700 | [diff] [blame] | 793 | isolated = isolate_freepages_block(cc, block_start_pfn, |
| 794 | block_end_pfn, freelist, false); |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 795 | nr_freepages += isolated; |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 796 | |
| 797 | /* |
Vlastimil Babka | e9ade56 | 2014-06-04 16:08:34 -0700 | [diff] [blame] | 798 | * Set a flag that we successfully isolated in this pageblock. |
| 799 | * In the next loop iteration, zone->compact_cached_free_pfn |
| 800 | * will not be updated and thus it will effectively contain the |
| 801 | * highest pageblock we isolated pages from. |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 802 | */ |
Vlastimil Babka | e9ade56 | 2014-06-04 16:08:34 -0700 | [diff] [blame] | 803 | if (isolated) |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 804 | cc->finished_update_free = true; |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 805 | |
| 806 | /* |
| 807 | * isolate_freepages_block() might have aborted due to async |
| 808 | * compaction being contended |
| 809 | */ |
| 810 | if (cc->contended) |
| 811 | break; |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 812 | } |
| 813 | |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 814 | /* split_free_page does not map the pages */ |
| 815 | map_pages(freelist); |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 816 | |
Vlastimil Babka | 7ed695e | 2014-01-21 15:51:09 -0800 | [diff] [blame] | 817 | /* |
| 818 | * If we crossed the migrate scanner, we want to keep it that way |
| 819 | * so that compact_finished() may detect this |
| 820 | */ |
Vlastimil Babka | c96b9e5 | 2014-06-04 16:07:26 -0700 | [diff] [blame] | 821 | if (block_start_pfn < low_pfn) |
Vlastimil Babka | e9ade56 | 2014-06-04 16:08:34 -0700 | [diff] [blame] | 822 | cc->free_pfn = cc->migrate_pfn; |
Vlastimil Babka | c96b9e5 | 2014-06-04 16:07:26 -0700 | [diff] [blame] | 823 | |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 824 | cc->nr_freepages = nr_freepages; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | /* |
| 828 | * This is a migrate-callback that "allocates" freepages by taking pages |
| 829 | * from the isolated freelists in the block we are migrating to. |
| 830 | */ |
| 831 | static struct page *compaction_alloc(struct page *migratepage, |
| 832 | unsigned long data, |
| 833 | int **result) |
| 834 | { |
| 835 | struct compact_control *cc = (struct compact_control *)data; |
| 836 | struct page *freepage; |
| 837 | |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 838 | /* |
| 839 | * Isolate free pages if necessary, and if we are not aborting due to |
| 840 | * contention. |
| 841 | */ |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 842 | if (list_empty(&cc->freepages)) { |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 843 | if (!cc->contended) |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 844 | isolate_freepages(cc); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 845 | |
| 846 | if (list_empty(&cc->freepages)) |
| 847 | return NULL; |
| 848 | } |
| 849 | |
| 850 | freepage = list_entry(cc->freepages.next, struct page, lru); |
| 851 | list_del(&freepage->lru); |
| 852 | cc->nr_freepages--; |
| 853 | |
| 854 | return freepage; |
| 855 | } |
| 856 | |
| 857 | /* |
David Rientjes | d53aea3 | 2014-06-04 16:08:26 -0700 | [diff] [blame] | 858 | * This is a migrate-callback that "frees" freepages back to the isolated |
| 859 | * freelist. All pages on the freelist are from the same zone, so there is no |
| 860 | * special handling needed for NUMA. |
| 861 | */ |
| 862 | static void compaction_free(struct page *page, unsigned long data) |
| 863 | { |
| 864 | struct compact_control *cc = (struct compact_control *)data; |
| 865 | |
| 866 | list_add(&page->lru, &cc->freepages); |
| 867 | cc->nr_freepages++; |
| 868 | } |
| 869 | |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 870 | /* possible outcome of isolate_migratepages */ |
| 871 | typedef enum { |
| 872 | ISOLATE_ABORT, /* Abort compaction now */ |
| 873 | ISOLATE_NONE, /* No pages isolated, continue scanning */ |
| 874 | ISOLATE_SUCCESS, /* Pages isolated, migrate */ |
| 875 | } isolate_migrate_t; |
| 876 | |
| 877 | /* |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 878 | * Isolate all pages that can be migrated from the first suitable block, |
| 879 | * starting at the block pointed to by the migrate scanner pfn within |
| 880 | * compact_control. |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 881 | */ |
| 882 | static isolate_migrate_t isolate_migratepages(struct zone *zone, |
| 883 | struct compact_control *cc) |
| 884 | { |
| 885 | unsigned long low_pfn, end_pfn; |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 886 | struct page *page; |
| 887 | const isolate_mode_t isolate_mode = |
| 888 | (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0); |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 889 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 890 | /* |
| 891 | * Start at where we last stopped, or beginning of the zone as |
| 892 | * initialized by compact_zone() |
| 893 | */ |
| 894 | low_pfn = cc->migrate_pfn; |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 895 | |
| 896 | /* Only scan within a pageblock boundary */ |
Mel Gorman | a9aacbc | 2013-02-22 16:32:25 -0800 | [diff] [blame] | 897 | end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages); |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 898 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 899 | /* |
| 900 | * Iterate over whole pageblocks until we find the first suitable. |
| 901 | * Do not cross the free scanner. |
| 902 | */ |
| 903 | for (; end_pfn <= cc->free_pfn; |
| 904 | low_pfn = end_pfn, end_pfn += pageblock_nr_pages) { |
| 905 | |
| 906 | /* |
| 907 | * This can potentially iterate a massively long zone with |
| 908 | * many pageblocks unsuitable, so periodically check if we |
| 909 | * need to schedule, or even abort async compaction. |
| 910 | */ |
| 911 | if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) |
| 912 | && compact_should_abort(cc)) |
| 913 | break; |
| 914 | |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 915 | page = pageblock_pfn_to_page(low_pfn, end_pfn, zone); |
| 916 | if (!page) |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 917 | continue; |
| 918 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 919 | /* If isolation recently failed, do not retry */ |
| 920 | if (!isolation_suitable(cc, page)) |
| 921 | continue; |
| 922 | |
| 923 | /* |
| 924 | * For async compaction, also only scan in MOVABLE blocks. |
| 925 | * Async compaction is optimistic to see if the minimum amount |
| 926 | * of work satisfies the allocation. |
| 927 | */ |
| 928 | if (cc->mode == MIGRATE_ASYNC && |
| 929 | !migrate_async_suitable(get_pageblock_migratetype(page))) |
| 930 | continue; |
| 931 | |
| 932 | /* Perform the isolation */ |
| 933 | low_pfn = isolate_migratepages_block(cc, low_pfn, end_pfn, |
| 934 | isolate_mode); |
| 935 | |
| 936 | if (!low_pfn || cc->contended) |
| 937 | return ISOLATE_ABORT; |
| 938 | |
| 939 | /* |
| 940 | * Either we isolated something and proceed with migration. Or |
| 941 | * we failed and compact_zone should decide if we should |
| 942 | * continue or not. |
| 943 | */ |
| 944 | break; |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 945 | } |
| 946 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 947 | acct_isolated(zone, cc); |
| 948 | /* Record where migration scanner will be restarted */ |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 949 | cc->migrate_pfn = low_pfn; |
| 950 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 951 | return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE; |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 952 | } |
| 953 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 954 | static int compact_finished(struct zone *zone, |
Andrea Arcangeli | 5a03b05 | 2011-01-13 15:47:11 -0800 | [diff] [blame] | 955 | struct compact_control *cc) |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 956 | { |
Mel Gorman | 8fb74b9 | 2013-01-11 14:32:16 -0800 | [diff] [blame] | 957 | unsigned int order; |
Andrea Arcangeli | 5a03b05 | 2011-01-13 15:47:11 -0800 | [diff] [blame] | 958 | unsigned long watermark; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 959 | |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 960 | if (cc->contended || fatal_signal_pending(current)) |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 961 | return COMPACT_PARTIAL; |
| 962 | |
Mel Gorman | 753341a | 2012-10-08 16:32:40 -0700 | [diff] [blame] | 963 | /* Compaction run completes if the migrate and free scanner meet */ |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 964 | if (cc->free_pfn <= cc->migrate_pfn) { |
Vlastimil Babka | 55b7c4c | 2014-01-21 15:51:11 -0800 | [diff] [blame] | 965 | /* Let the next compaction start anew. */ |
David Rientjes | 35979ef | 2014-06-04 16:08:27 -0700 | [diff] [blame] | 966 | zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn; |
| 967 | zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn; |
Vlastimil Babka | 55b7c4c | 2014-01-21 15:51:11 -0800 | [diff] [blame] | 968 | zone->compact_cached_free_pfn = zone_end_pfn(zone); |
| 969 | |
Mel Gorman | 6299702 | 2012-10-08 16:32:47 -0700 | [diff] [blame] | 970 | /* |
| 971 | * Mark that the PG_migrate_skip information should be cleared |
| 972 | * by kswapd when it goes to sleep. kswapd does not set the |
| 973 | * flag itself as the decision to be clear should be directly |
| 974 | * based on an allocation request. |
| 975 | */ |
| 976 | if (!current_is_kswapd()) |
| 977 | zone->compact_blockskip_flush = true; |
| 978 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 979 | return COMPACT_COMPLETE; |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 980 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 981 | |
Johannes Weiner | 82478fb | 2011-01-20 14:44:21 -0800 | [diff] [blame] | 982 | /* |
| 983 | * order == -1 is expected when compacting via |
| 984 | * /proc/sys/vm/compact_memory |
| 985 | */ |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 986 | if (cc->order == -1) |
| 987 | return COMPACT_CONTINUE; |
| 988 | |
Michal Hocko | 3957c77 | 2011-06-15 15:08:25 -0700 | [diff] [blame] | 989 | /* Compaction run is not finished if the watermark is not met */ |
| 990 | watermark = low_wmark_pages(zone); |
| 991 | watermark += (1 << cc->order); |
| 992 | |
| 993 | if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0)) |
| 994 | return COMPACT_CONTINUE; |
| 995 | |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 996 | /* Direct compactor: Is a suitable page free? */ |
Mel Gorman | 8fb74b9 | 2013-01-11 14:32:16 -0800 | [diff] [blame] | 997 | for (order = cc->order; order < MAX_ORDER; order++) { |
| 998 | struct free_area *area = &zone->free_area[order]; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 999 | |
Mel Gorman | 8fb74b9 | 2013-01-11 14:32:16 -0800 | [diff] [blame] | 1000 | /* Job done if page is free of the right migratetype */ |
| 1001 | if (!list_empty(&area->free_list[cc->migratetype])) |
| 1002 | return COMPACT_PARTIAL; |
| 1003 | |
| 1004 | /* Job done if allocation would set block type */ |
| 1005 | if (cc->order >= pageblock_order && area->nr_free) |
| 1006 | return COMPACT_PARTIAL; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1009 | return COMPACT_CONTINUE; |
| 1010 | } |
| 1011 | |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 1012 | /* |
| 1013 | * compaction_suitable: Is this suitable to run compaction on this zone now? |
| 1014 | * Returns |
| 1015 | * COMPACT_SKIPPED - If there are too few free pages for compaction |
| 1016 | * COMPACT_PARTIAL - If the allocation would succeed without compaction |
| 1017 | * COMPACT_CONTINUE - If compaction should run now |
| 1018 | */ |
| 1019 | unsigned long compaction_suitable(struct zone *zone, int order) |
| 1020 | { |
| 1021 | int fragindex; |
| 1022 | unsigned long watermark; |
| 1023 | |
| 1024 | /* |
Michal Hocko | 3957c77 | 2011-06-15 15:08:25 -0700 | [diff] [blame] | 1025 | * order == -1 is expected when compacting via |
| 1026 | * /proc/sys/vm/compact_memory |
| 1027 | */ |
| 1028 | if (order == -1) |
| 1029 | return COMPACT_CONTINUE; |
| 1030 | |
| 1031 | /* |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 1032 | * Watermarks for order-0 must be met for compaction. Note the 2UL. |
| 1033 | * This is because during migration, copies of pages need to be |
| 1034 | * allocated and for a short time, the footprint is higher |
| 1035 | */ |
| 1036 | watermark = low_wmark_pages(zone) + (2UL << order); |
| 1037 | if (!zone_watermark_ok(zone, 0, watermark, 0, 0)) |
| 1038 | return COMPACT_SKIPPED; |
| 1039 | |
| 1040 | /* |
| 1041 | * fragmentation index determines if allocation failures are due to |
| 1042 | * low memory or external fragmentation |
| 1043 | * |
Shaohua Li | a582a73 | 2011-06-15 15:08:49 -0700 | [diff] [blame] | 1044 | * index of -1000 implies allocations might succeed depending on |
| 1045 | * watermarks |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 1046 | * index towards 0 implies failure is due to lack of memory |
| 1047 | * index towards 1000 implies failure is due to fragmentation |
| 1048 | * |
| 1049 | * Only compact if a failure would be due to fragmentation. |
| 1050 | */ |
| 1051 | fragindex = fragmentation_index(zone, order); |
| 1052 | if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold) |
| 1053 | return COMPACT_SKIPPED; |
| 1054 | |
Shaohua Li | a582a73 | 2011-06-15 15:08:49 -0700 | [diff] [blame] | 1055 | if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark, |
| 1056 | 0, 0)) |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 1057 | return COMPACT_PARTIAL; |
| 1058 | |
| 1059 | return COMPACT_CONTINUE; |
| 1060 | } |
| 1061 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1062 | static int compact_zone(struct zone *zone, struct compact_control *cc) |
| 1063 | { |
| 1064 | int ret; |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 1065 | unsigned long start_pfn = zone->zone_start_pfn; |
Cody P Schafer | 108bcc9 | 2013-02-22 16:35:23 -0800 | [diff] [blame] | 1066 | unsigned long end_pfn = zone_end_pfn(zone); |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 1067 | const bool sync = cc->mode != MIGRATE_ASYNC; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1068 | |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 1069 | ret = compaction_suitable(zone, cc->order); |
| 1070 | switch (ret) { |
| 1071 | case COMPACT_PARTIAL: |
| 1072 | case COMPACT_SKIPPED: |
| 1073 | /* Compaction is likely to fail */ |
| 1074 | return ret; |
| 1075 | case COMPACT_CONTINUE: |
| 1076 | /* Fall through to compaction */ |
| 1077 | ; |
| 1078 | } |
| 1079 | |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 1080 | /* |
Vlastimil Babka | d3132e4 | 2014-01-21 15:51:08 -0800 | [diff] [blame] | 1081 | * Clear pageblock skip if there were failures recently and compaction |
| 1082 | * is about to be retried after being deferred. kswapd does not do |
| 1083 | * this reset as it'll reset the cached information when going to sleep. |
| 1084 | */ |
| 1085 | if (compaction_restarting(zone, cc->order) && !current_is_kswapd()) |
| 1086 | __reset_isolation_suitable(zone); |
| 1087 | |
| 1088 | /* |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 1089 | * Setup to move all movable pages to the end of the zone. Used cached |
| 1090 | * information on where the scanners should start but check that it |
| 1091 | * is initialised by ensuring the values are within zone boundaries. |
| 1092 | */ |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 1093 | cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync]; |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 1094 | cc->free_pfn = zone->compact_cached_free_pfn; |
| 1095 | if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) { |
| 1096 | cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1); |
| 1097 | zone->compact_cached_free_pfn = cc->free_pfn; |
| 1098 | } |
| 1099 | if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) { |
| 1100 | cc->migrate_pfn = start_pfn; |
David Rientjes | 35979ef | 2014-06-04 16:08:27 -0700 | [diff] [blame] | 1101 | zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn; |
| 1102 | zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn; |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 1103 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1104 | |
Mel Gorman | 0eb927c | 2014-01-21 15:51:05 -0800 | [diff] [blame] | 1105 | trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, cc->free_pfn, end_pfn); |
| 1106 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1107 | migrate_prep_local(); |
| 1108 | |
| 1109 | while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) { |
Minchan Kim | 9d502c1 | 2011-03-22 16:30:39 -0700 | [diff] [blame] | 1110 | int err; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1111 | |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 1112 | switch (isolate_migratepages(zone, cc)) { |
| 1113 | case ISOLATE_ABORT: |
| 1114 | ret = COMPACT_PARTIAL; |
Rafael Aquini | 5733c7d | 2012-12-11 16:02:47 -0800 | [diff] [blame] | 1115 | putback_movable_pages(&cc->migratepages); |
Shaohua Li | e64c523 | 2012-10-08 16:32:27 -0700 | [diff] [blame] | 1116 | cc->nr_migratepages = 0; |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 1117 | goto out; |
| 1118 | case ISOLATE_NONE: |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1119 | continue; |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 1120 | case ISOLATE_SUCCESS: |
| 1121 | ; |
| 1122 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1123 | |
David Rientjes | d53aea3 | 2014-06-04 16:08:26 -0700 | [diff] [blame] | 1124 | err = migrate_pages(&cc->migratepages, compaction_alloc, |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 1125 | compaction_free, (unsigned long)cc, cc->mode, |
Mel Gorman | 7b2a2d4 | 2012-10-19 14:07:31 +0100 | [diff] [blame] | 1126 | MR_COMPACTION); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1127 | |
Vlastimil Babka | f8c9301 | 2014-06-04 16:08:32 -0700 | [diff] [blame] | 1128 | trace_mm_compaction_migratepages(cc->nr_migratepages, err, |
| 1129 | &cc->migratepages); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1130 | |
Vlastimil Babka | f8c9301 | 2014-06-04 16:08:32 -0700 | [diff] [blame] | 1131 | /* All pages were either migrated or will be released */ |
| 1132 | cc->nr_migratepages = 0; |
Minchan Kim | 9d502c1 | 2011-03-22 16:30:39 -0700 | [diff] [blame] | 1133 | if (err) { |
Rafael Aquini | 5733c7d | 2012-12-11 16:02:47 -0800 | [diff] [blame] | 1134 | putback_movable_pages(&cc->migratepages); |
Vlastimil Babka | 7ed695e | 2014-01-21 15:51:09 -0800 | [diff] [blame] | 1135 | /* |
| 1136 | * migrate_pages() may return -ENOMEM when scanners meet |
| 1137 | * and we want compact_finished() to detect it |
| 1138 | */ |
| 1139 | if (err == -ENOMEM && cc->free_pfn > cc->migrate_pfn) { |
David Rientjes | 4bf2bba | 2012-07-11 14:02:13 -0700 | [diff] [blame] | 1140 | ret = COMPACT_PARTIAL; |
| 1141 | goto out; |
| 1142 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1143 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 1146 | out: |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1147 | /* Release free pages and check accounting */ |
| 1148 | cc->nr_freepages -= release_freepages(&cc->freepages); |
| 1149 | VM_BUG_ON(cc->nr_freepages != 0); |
| 1150 | |
Mel Gorman | 0eb927c | 2014-01-21 15:51:05 -0800 | [diff] [blame] | 1151 | trace_mm_compaction_end(ret); |
| 1152 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1153 | return ret; |
| 1154 | } |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1155 | |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 1156 | static unsigned long compact_zone_order(struct zone *zone, int order, |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame^] | 1157 | gfp_t gfp_mask, enum migrate_mode mode, int *contended) |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1158 | { |
Shaohua Li | e64c523 | 2012-10-08 16:32:27 -0700 | [diff] [blame] | 1159 | unsigned long ret; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1160 | struct compact_control cc = { |
| 1161 | .nr_freepages = 0, |
| 1162 | .nr_migratepages = 0, |
| 1163 | .order = order, |
| 1164 | .migratetype = allocflags_to_migratetype(gfp_mask), |
| 1165 | .zone = zone, |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 1166 | .mode = mode, |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1167 | }; |
| 1168 | INIT_LIST_HEAD(&cc.freepages); |
| 1169 | INIT_LIST_HEAD(&cc.migratepages); |
| 1170 | |
Shaohua Li | e64c523 | 2012-10-08 16:32:27 -0700 | [diff] [blame] | 1171 | ret = compact_zone(zone, &cc); |
| 1172 | |
| 1173 | VM_BUG_ON(!list_empty(&cc.freepages)); |
| 1174 | VM_BUG_ON(!list_empty(&cc.migratepages)); |
| 1175 | |
| 1176 | *contended = cc.contended; |
| 1177 | return ret; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1178 | } |
| 1179 | |
Mel Gorman | 5e77190 | 2010-05-24 14:32:31 -0700 | [diff] [blame] | 1180 | int sysctl_extfrag_threshold = 500; |
| 1181 | |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1182 | /** |
| 1183 | * try_to_compact_pages - Direct compact to satisfy a high-order allocation |
| 1184 | * @zonelist: The zonelist used for the current allocation |
| 1185 | * @order: The order of the current allocation |
| 1186 | * @gfp_mask: The GFP mask of the current allocation |
| 1187 | * @nodemask: The allowed nodes to allocate from |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 1188 | * @mode: The migration mode for async, sync light, or sync migration |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame^] | 1189 | * @contended: Return value that determines if compaction was aborted due to |
| 1190 | * need_resched() or lock contention |
Vlastimil Babka | 53853e2 | 2014-10-09 15:27:02 -0700 | [diff] [blame] | 1191 | * @candidate_zone: Return the zone where we think allocation should succeed |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1192 | * |
| 1193 | * This is the main entry point for direct page compaction. |
| 1194 | */ |
| 1195 | unsigned long try_to_compact_pages(struct zonelist *zonelist, |
Mel Gorman | 77f1fe6 | 2011-01-13 15:45:57 -0800 | [diff] [blame] | 1196 | int order, gfp_t gfp_mask, nodemask_t *nodemask, |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame^] | 1197 | enum migrate_mode mode, int *contended, |
Vlastimil Babka | 53853e2 | 2014-10-09 15:27:02 -0700 | [diff] [blame] | 1198 | struct zone **candidate_zone) |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1199 | { |
| 1200 | enum zone_type high_zoneidx = gfp_zone(gfp_mask); |
| 1201 | int may_enter_fs = gfp_mask & __GFP_FS; |
| 1202 | int may_perform_io = gfp_mask & __GFP_IO; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1203 | struct zoneref *z; |
| 1204 | struct zone *zone; |
Vlastimil Babka | 53853e2 | 2014-10-09 15:27:02 -0700 | [diff] [blame] | 1205 | int rc = COMPACT_DEFERRED; |
Bartlomiej Zolnierkiewicz | d95ea5d | 2012-10-08 16:32:05 -0700 | [diff] [blame] | 1206 | int alloc_flags = 0; |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame^] | 1207 | int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */ |
| 1208 | |
| 1209 | *contended = COMPACT_CONTENDED_NONE; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1210 | |
Mel Gorman | 4ffb633 | 2012-10-08 16:29:09 -0700 | [diff] [blame] | 1211 | /* Check if the GFP flags allow compaction */ |
Andrea Arcangeli | c5a73c3 | 2011-01-13 15:47:11 -0800 | [diff] [blame] | 1212 | if (!order || !may_enter_fs || !may_perform_io) |
Vlastimil Babka | 53853e2 | 2014-10-09 15:27:02 -0700 | [diff] [blame] | 1213 | return COMPACT_SKIPPED; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1214 | |
Bartlomiej Zolnierkiewicz | d95ea5d | 2012-10-08 16:32:05 -0700 | [diff] [blame] | 1215 | #ifdef CONFIG_CMA |
| 1216 | if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE) |
| 1217 | alloc_flags |= ALLOC_CMA; |
| 1218 | #endif |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1219 | /* Compact each zone in the list */ |
| 1220 | for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx, |
| 1221 | nodemask) { |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1222 | int status; |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame^] | 1223 | int zone_contended; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1224 | |
Vlastimil Babka | 53853e2 | 2014-10-09 15:27:02 -0700 | [diff] [blame] | 1225 | if (compaction_deferred(zone, order)) |
| 1226 | continue; |
| 1227 | |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 1228 | status = compact_zone_order(zone, order, gfp_mask, mode, |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame^] | 1229 | &zone_contended); |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1230 | rc = max(status, rc); |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame^] | 1231 | /* |
| 1232 | * It takes at least one zone that wasn't lock contended |
| 1233 | * to clear all_zones_contended. |
| 1234 | */ |
| 1235 | all_zones_contended &= zone_contended; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1236 | |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 1237 | /* If a normal allocation would succeed, stop compacting */ |
Bartlomiej Zolnierkiewicz | d95ea5d | 2012-10-08 16:32:05 -0700 | [diff] [blame] | 1238 | if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0, |
Vlastimil Babka | 53853e2 | 2014-10-09 15:27:02 -0700 | [diff] [blame] | 1239 | alloc_flags)) { |
| 1240 | *candidate_zone = zone; |
| 1241 | /* |
| 1242 | * We think the allocation will succeed in this zone, |
| 1243 | * but it is not certain, hence the false. The caller |
| 1244 | * will repeat this with true if allocation indeed |
| 1245 | * succeeds in this zone. |
| 1246 | */ |
| 1247 | compaction_defer_reset(zone, order, false); |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame^] | 1248 | /* |
| 1249 | * It is possible that async compaction aborted due to |
| 1250 | * need_resched() and the watermarks were ok thanks to |
| 1251 | * somebody else freeing memory. The allocation can |
| 1252 | * however still fail so we better signal the |
| 1253 | * need_resched() contention anyway (this will not |
| 1254 | * prevent the allocation attempt). |
| 1255 | */ |
| 1256 | if (zone_contended == COMPACT_CONTENDED_SCHED) |
| 1257 | *contended = COMPACT_CONTENDED_SCHED; |
| 1258 | |
| 1259 | goto break_loop; |
| 1260 | } |
| 1261 | |
| 1262 | if (mode != MIGRATE_ASYNC) { |
Vlastimil Babka | 53853e2 | 2014-10-09 15:27:02 -0700 | [diff] [blame] | 1263 | /* |
| 1264 | * We think that allocation won't succeed in this zone |
| 1265 | * so we defer compaction there. If it ends up |
| 1266 | * succeeding after all, it will be reset. |
| 1267 | */ |
| 1268 | defer_compaction(zone, order); |
| 1269 | } |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame^] | 1270 | |
| 1271 | /* |
| 1272 | * We might have stopped compacting due to need_resched() in |
| 1273 | * async compaction, or due to a fatal signal detected. In that |
| 1274 | * case do not try further zones and signal need_resched() |
| 1275 | * contention. |
| 1276 | */ |
| 1277 | if ((zone_contended == COMPACT_CONTENDED_SCHED) |
| 1278 | || fatal_signal_pending(current)) { |
| 1279 | *contended = COMPACT_CONTENDED_SCHED; |
| 1280 | goto break_loop; |
| 1281 | } |
| 1282 | |
| 1283 | continue; |
| 1284 | break_loop: |
| 1285 | /* |
| 1286 | * We might not have tried all the zones, so be conservative |
| 1287 | * and assume they are not all lock contended. |
| 1288 | */ |
| 1289 | all_zones_contended = 0; |
| 1290 | break; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1291 | } |
| 1292 | |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame^] | 1293 | /* |
| 1294 | * If at least one zone wasn't deferred or skipped, we report if all |
| 1295 | * zones that were tried were lock contended. |
| 1296 | */ |
| 1297 | if (rc > COMPACT_SKIPPED && all_zones_contended) |
| 1298 | *contended = COMPACT_CONTENDED_LOCK; |
| 1299 | |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1300 | return rc; |
| 1301 | } |
| 1302 | |
| 1303 | |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1304 | /* Compact all zones within a node */ |
Andrew Morton | 7103f16 | 2013-02-22 16:32:33 -0800 | [diff] [blame] | 1305 | static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc) |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1306 | { |
| 1307 | int zoneid; |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1308 | struct zone *zone; |
| 1309 | |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1310 | for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1311 | |
| 1312 | zone = &pgdat->node_zones[zoneid]; |
| 1313 | if (!populated_zone(zone)) |
| 1314 | continue; |
| 1315 | |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1316 | cc->nr_freepages = 0; |
| 1317 | cc->nr_migratepages = 0; |
| 1318 | cc->zone = zone; |
| 1319 | INIT_LIST_HEAD(&cc->freepages); |
| 1320 | INIT_LIST_HEAD(&cc->migratepages); |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1321 | |
Dan Carpenter | aad6ec3 | 2012-03-21 16:33:54 -0700 | [diff] [blame] | 1322 | if (cc->order == -1 || !compaction_deferred(zone, cc->order)) |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1323 | compact_zone(zone, cc); |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1324 | |
Rik van Riel | aff6224 | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1325 | if (cc->order > 0) { |
Vlastimil Babka | de6c60a | 2014-01-21 15:51:07 -0800 | [diff] [blame] | 1326 | if (zone_watermark_ok(zone, cc->order, |
| 1327 | low_wmark_pages(zone), 0, 0)) |
| 1328 | compaction_defer_reset(zone, cc->order, false); |
Rik van Riel | aff6224 | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1331 | VM_BUG_ON(!list_empty(&cc->freepages)); |
| 1332 | VM_BUG_ON(!list_empty(&cc->migratepages)); |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1333 | } |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1334 | } |
| 1335 | |
Andrew Morton | 7103f16 | 2013-02-22 16:32:33 -0800 | [diff] [blame] | 1336 | void compact_pgdat(pg_data_t *pgdat, int order) |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1337 | { |
| 1338 | struct compact_control cc = { |
| 1339 | .order = order, |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 1340 | .mode = MIGRATE_ASYNC, |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1341 | }; |
| 1342 | |
Mel Gorman | 3a7200a | 2013-09-11 14:22:19 -0700 | [diff] [blame] | 1343 | if (!order) |
| 1344 | return; |
| 1345 | |
Andrew Morton | 7103f16 | 2013-02-22 16:32:33 -0800 | [diff] [blame] | 1346 | __compact_pgdat(pgdat, &cc); |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1347 | } |
| 1348 | |
Andrew Morton | 7103f16 | 2013-02-22 16:32:33 -0800 | [diff] [blame] | 1349 | static void compact_node(int nid) |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1350 | { |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1351 | struct compact_control cc = { |
| 1352 | .order = -1, |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 1353 | .mode = MIGRATE_SYNC, |
David Rientjes | 91ca918 | 2014-04-03 14:47:23 -0700 | [diff] [blame] | 1354 | .ignore_skip_hint = true, |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1355 | }; |
| 1356 | |
Andrew Morton | 7103f16 | 2013-02-22 16:32:33 -0800 | [diff] [blame] | 1357 | __compact_pgdat(NODE_DATA(nid), &cc); |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1358 | } |
| 1359 | |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1360 | /* Compact all nodes in the system */ |
Jason Liu | 7964c06 | 2013-01-11 14:31:47 -0800 | [diff] [blame] | 1361 | static void compact_nodes(void) |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1362 | { |
| 1363 | int nid; |
| 1364 | |
Hugh Dickins | 8575ec2 | 2012-03-21 16:33:53 -0700 | [diff] [blame] | 1365 | /* Flush pending updates to the LRU lists */ |
| 1366 | lru_add_drain_all(); |
| 1367 | |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1368 | for_each_online_node(nid) |
| 1369 | compact_node(nid); |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | /* The written value is actually unused, all memory is compacted */ |
| 1373 | int sysctl_compact_memory; |
| 1374 | |
| 1375 | /* This is the entry point for compacting all nodes via /proc/sys/vm */ |
| 1376 | int sysctl_compaction_handler(struct ctl_table *table, int write, |
| 1377 | void __user *buffer, size_t *length, loff_t *ppos) |
| 1378 | { |
| 1379 | if (write) |
Jason Liu | 7964c06 | 2013-01-11 14:31:47 -0800 | [diff] [blame] | 1380 | compact_nodes(); |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1381 | |
| 1382 | return 0; |
| 1383 | } |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 1384 | |
Mel Gorman | 5e77190 | 2010-05-24 14:32:31 -0700 | [diff] [blame] | 1385 | int sysctl_extfrag_handler(struct ctl_table *table, int write, |
| 1386 | void __user *buffer, size_t *length, loff_t *ppos) |
| 1387 | { |
| 1388 | proc_dointvec_minmax(table, write, buffer, length, ppos); |
| 1389 | |
| 1390 | return 0; |
| 1391 | } |
| 1392 | |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 1393 | #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) |
Rashika Kheria | 74e77fb | 2014-04-03 14:48:01 -0700 | [diff] [blame] | 1394 | static ssize_t sysfs_compact_node(struct device *dev, |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 1395 | struct device_attribute *attr, |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 1396 | const char *buf, size_t count) |
| 1397 | { |
Hugh Dickins | 8575ec2 | 2012-03-21 16:33:53 -0700 | [diff] [blame] | 1398 | int nid = dev->id; |
| 1399 | |
| 1400 | if (nid >= 0 && nid < nr_node_ids && node_online(nid)) { |
| 1401 | /* Flush pending updates to the LRU lists */ |
| 1402 | lru_add_drain_all(); |
| 1403 | |
| 1404 | compact_node(nid); |
| 1405 | } |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 1406 | |
| 1407 | return count; |
| 1408 | } |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 1409 | static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node); |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 1410 | |
| 1411 | int compaction_register_node(struct node *node) |
| 1412 | { |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 1413 | return device_create_file(&node->dev, &dev_attr_compact); |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | void compaction_unregister_node(struct node *node) |
| 1417 | { |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 1418 | return device_remove_file(&node->dev, &dev_attr_compact); |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 1419 | } |
| 1420 | #endif /* CONFIG_SYSFS && CONFIG_NUMA */ |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1421 | |
| 1422 | #endif /* CONFIG_COMPACTION */ |