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