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