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