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