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