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