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