Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/mm/compaction.c |
| 3 | * |
| 4 | * Memory compaction for the reduction of external fragmentation. Note that |
| 5 | * this heavily depends upon page migration to do all the real heavy |
| 6 | * lifting |
| 7 | * |
| 8 | * Copyright IBM Corp. 2007-2010 Mel Gorman <mel@csn.ul.ie> |
| 9 | */ |
| 10 | #include <linux/swap.h> |
| 11 | #include <linux/migrate.h> |
| 12 | #include <linux/compaction.h> |
| 13 | #include <linux/mm_inline.h> |
| 14 | #include <linux/backing-dev.h> |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 15 | #include <linux/sysctl.h> |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 16 | #include <linux/sysfs.h> |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 17 | #include "internal.h" |
| 18 | |
Michal Nazarewicz | 02ff1de | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 19 | #if defined CONFIG_COMPACTION || defined CONFIG_CMA |
| 20 | |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 21 | #define CREATE_TRACE_POINTS |
| 22 | #include <trace/events/compaction.h> |
| 23 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 24 | static unsigned long release_freepages(struct list_head *freelist) |
| 25 | { |
| 26 | struct page *page, *next; |
| 27 | unsigned long count = 0; |
| 28 | |
| 29 | list_for_each_entry_safe(page, next, freelist, lru) { |
| 30 | list_del(&page->lru); |
| 31 | __free_page(page); |
| 32 | count++; |
| 33 | } |
| 34 | |
| 35 | return count; |
| 36 | } |
| 37 | |
Michal Nazarewicz | 02ff1de | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 38 | static void map_pages(struct list_head *list) |
| 39 | { |
| 40 | struct page *page; |
| 41 | |
| 42 | list_for_each_entry(page, list, lru) { |
| 43 | arch_alloc_page(page, 0); |
| 44 | kernel_map_pages(page, 1, 1); |
| 45 | } |
| 46 | } |
| 47 | |
Michal Nazarewicz | d4158d2 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 48 | static inline bool migrate_async_suitable(int migratetype) |
| 49 | { |
| 50 | return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE; |
| 51 | } |
| 52 | |
Michal Nazarewicz | 61ee2fc | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 53 | /* |
Mel Gorman | 27d1af8 | 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 | 61ee2fc | 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 | 61ee2fc | 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 | 61ee2fc | 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 | 61ee2fc | 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 | 61ee2fc | 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 | 61ee2fc | 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 | 61ee2fc | 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 | 02ff1de | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 162 | unsigned long |
Michal Nazarewicz | 61ee2fc | 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 | 27d1af8 | 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 | 27d1af8 | 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 | a196a6c | 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 | 02ff1de | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 269 | unsigned long |
Michal Nazarewicz | a196a6c | 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 | 6d8a50e | 2012-05-29 15:06:54 -0700 | [diff] [blame] | 276 | isolate_mode_t mode = 0; |
Mel Gorman | 27d1af8 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 277 | unsigned long flags; |
| 278 | bool locked; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 279 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 280 | /* |
| 281 | * Ensure that there are not too many pages isolated from the LRU |
| 282 | * list by either parallel reclaimers or compaction. If there are, |
| 283 | * delay for some time until fewer pages are isolated |
| 284 | */ |
| 285 | while (unlikely(too_many_isolated(zone))) { |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 286 | /* async migration should just abort */ |
| 287 | if (!cc->sync) |
Michal Nazarewicz | a196a6c | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 288 | return 0; |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 289 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 290 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
| 291 | |
| 292 | if (fatal_signal_pending(current)) |
Michal Nazarewicz | a196a6c | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 293 | return 0; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /* Time to isolate some pages for migration */ |
Andrea Arcangeli | b2eef8c | 2011-03-22 16:33:10 -0700 | [diff] [blame] | 297 | cond_resched(); |
Mel Gorman | 27d1af8 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 298 | spin_lock_irqsave(&zone->lru_lock, flags); |
| 299 | locked = true; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 300 | for (; low_pfn < end_pfn; low_pfn++) { |
| 301 | struct page *page; |
Andrea Arcangeli | b2eef8c | 2011-03-22 16:33:10 -0700 | [diff] [blame] | 302 | |
| 303 | /* give a chance to irqs before checking need_resched() */ |
| 304 | if (!((low_pfn+1) % SWAP_CLUSTER_MAX)) { |
Mel Gorman | 27d1af8 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 305 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
Andrea Arcangeli | b2eef8c | 2011-03-22 16:33:10 -0700 | [diff] [blame] | 306 | locked = false; |
| 307 | } |
Mel Gorman | 27d1af8 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 308 | |
| 309 | /* Check if it is ok to still hold the lock */ |
| 310 | locked = compact_checklock_irqsave(&zone->lru_lock, &flags, |
| 311 | locked, cc); |
| 312 | if (!locked) |
| 313 | break; |
Andrea Arcangeli | b2eef8c | 2011-03-22 16:33:10 -0700 | [diff] [blame] | 314 | |
Mel Gorman | 0bf380b | 2012-02-03 15:37:18 -0800 | [diff] [blame] | 315 | /* |
| 316 | * migrate_pfn does not necessarily start aligned to a |
| 317 | * pageblock. Ensure that pfn_valid is called when moving |
| 318 | * into a new MAX_ORDER_NR_PAGES range in case of large |
| 319 | * memory holes within the zone |
| 320 | */ |
| 321 | if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) { |
| 322 | if (!pfn_valid(low_pfn)) { |
| 323 | low_pfn += MAX_ORDER_NR_PAGES - 1; |
| 324 | continue; |
| 325 | } |
| 326 | } |
| 327 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 328 | if (!pfn_valid_within(low_pfn)) |
| 329 | continue; |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 330 | nr_scanned++; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 331 | |
Mel Gorman | dc90860 | 2012-02-08 17:13:38 -0800 | [diff] [blame] | 332 | /* |
| 333 | * Get the page and ensure the page is within the same zone. |
| 334 | * See the comment in isolate_freepages about overlapping |
| 335 | * nodes. It is deliberate that the new zone lock is not taken |
| 336 | * as memory compaction should not move pages between nodes. |
| 337 | */ |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 338 | page = pfn_to_page(low_pfn); |
Mel Gorman | dc90860 | 2012-02-08 17:13:38 -0800 | [diff] [blame] | 339 | if (page_zone(page) != zone) |
| 340 | continue; |
| 341 | |
| 342 | /* Skip if free */ |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 343 | if (PageBuddy(page)) |
| 344 | continue; |
| 345 | |
Mel Gorman | 9927af74 | 2011-01-13 15:45:59 -0800 | [diff] [blame] | 346 | /* |
| 347 | * For async migration, also only scan in MOVABLE blocks. Async |
| 348 | * migration is optimistic to see if the minimum amount of work |
| 349 | * satisfies the allocation |
| 350 | */ |
| 351 | pageblock_nr = low_pfn >> pageblock_order; |
| 352 | if (!cc->sync && last_pageblock_nr != pageblock_nr && |
Michal Nazarewicz | d4158d2 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 353 | !migrate_async_suitable(get_pageblock_migratetype(page))) { |
Mel Gorman | 9927af74 | 2011-01-13 15:45:59 -0800 | [diff] [blame] | 354 | low_pfn += pageblock_nr_pages; |
| 355 | low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1; |
| 356 | last_pageblock_nr = pageblock_nr; |
| 357 | continue; |
| 358 | } |
| 359 | |
Andrea Arcangeli | bc83501 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 360 | if (!PageLRU(page)) |
| 361 | continue; |
| 362 | |
| 363 | /* |
| 364 | * PageLRU is set, and lru_lock excludes isolation, |
| 365 | * splitting and collapsing (collapsing has already |
| 366 | * happened if PageLRU is set). |
| 367 | */ |
| 368 | if (PageTransHuge(page)) { |
| 369 | low_pfn += (1 << compound_order(page)) - 1; |
| 370 | continue; |
| 371 | } |
| 372 | |
Mel Gorman | c824493 | 2012-01-12 17:19:38 -0800 | [diff] [blame] | 373 | if (!cc->sync) |
| 374 | mode |= ISOLATE_ASYNC_MIGRATE; |
| 375 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 376 | /* Try isolate the page */ |
Konstantin Khlebnikov | 6d8a50e | 2012-05-29 15:06:54 -0700 | [diff] [blame] | 377 | if (__isolate_lru_page(page, mode) != 0) |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 378 | continue; |
| 379 | |
Andrea Arcangeli | bc83501 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 380 | VM_BUG_ON(PageTransCompound(page)); |
| 381 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 382 | /* Successfully isolated */ |
| 383 | del_page_from_lru_list(zone, page, page_lru(page)); |
| 384 | list_add(&page->lru, migratelist); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 385 | cc->nr_migratepages++; |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 386 | nr_isolated++; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 387 | |
| 388 | /* Avoid isolating too much */ |
Hillf Danton | 31b8384 | 2012-01-10 15:07:59 -0800 | [diff] [blame] | 389 | if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) { |
| 390 | ++low_pfn; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 391 | break; |
Hillf Danton | 31b8384 | 2012-01-10 15:07:59 -0800 | [diff] [blame] | 392 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Mel Gorman | 27d1af8 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 395 | acct_isolated(zone, locked, cc); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 396 | |
Mel Gorman | 27d1af8 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 397 | if (locked) |
| 398 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 399 | |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 400 | trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated); |
| 401 | |
Michal Nazarewicz | a196a6c | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 402 | return low_pfn; |
| 403 | } |
| 404 | |
Michal Nazarewicz | 02ff1de | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 405 | #endif /* CONFIG_COMPACTION || CONFIG_CMA */ |
| 406 | #ifdef CONFIG_COMPACTION |
| 407 | |
| 408 | /* Returns true if the page is within a block suitable for migration to */ |
| 409 | static bool suitable_migration_target(struct page *page) |
Michal Nazarewicz | a196a6c | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 410 | { |
Michal Nazarewicz | a196a6c | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 411 | |
Michal Nazarewicz | 02ff1de | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 412 | int migratetype = get_pageblock_migratetype(page); |
Michal Nazarewicz | a196a6c | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 413 | |
Michal Nazarewicz | 02ff1de | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 414 | /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */ |
| 415 | if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE) |
| 416 | return false; |
Michal Nazarewicz | a196a6c | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 417 | |
Michal Nazarewicz | 02ff1de | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 418 | /* If the page is a large free page, then allow migration */ |
| 419 | if (PageBuddy(page) && page_order(page) >= pageblock_order) |
| 420 | return true; |
| 421 | |
Michal Nazarewicz | d4158d2 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 422 | /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */ |
| 423 | if (migrate_async_suitable(migratetype)) |
Michal Nazarewicz | 02ff1de | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 424 | return true; |
| 425 | |
| 426 | /* Otherwise skip the block */ |
| 427 | return false; |
| 428 | } |
| 429 | |
| 430 | /* |
| 431 | * Based on information in the current compact_control, find blocks |
| 432 | * suitable for isolating free pages from and then isolate them. |
| 433 | */ |
| 434 | static void isolate_freepages(struct zone *zone, |
| 435 | struct compact_control *cc) |
| 436 | { |
| 437 | struct page *page; |
| 438 | unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn; |
| 439 | unsigned long flags; |
| 440 | int nr_freepages = cc->nr_freepages; |
| 441 | struct list_head *freelist = &cc->freepages; |
| 442 | |
| 443 | /* |
| 444 | * Initialise the free scanner. The starting point is where we last |
| 445 | * scanned from (or the end of the zone if starting). The low point |
| 446 | * is the end of the pageblock the migration scanner is using. |
| 447 | */ |
| 448 | pfn = cc->free_pfn; |
| 449 | low_pfn = cc->migrate_pfn + pageblock_nr_pages; |
| 450 | |
| 451 | /* |
| 452 | * Take care that if the migration scanner is at the end of the zone |
| 453 | * that the free scanner does not accidentally move to the next zone |
| 454 | * in the next isolation cycle. |
| 455 | */ |
| 456 | high_pfn = min(low_pfn, pfn); |
| 457 | |
| 458 | zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages; |
| 459 | |
| 460 | /* |
| 461 | * Isolate free pages until enough are available to migrate the |
| 462 | * pages on cc->migratepages. We stop searching if the migrate |
| 463 | * and free page scanners meet or enough free pages are isolated. |
| 464 | */ |
| 465 | for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages; |
| 466 | pfn -= pageblock_nr_pages) { |
| 467 | unsigned long isolated; |
| 468 | |
| 469 | if (!pfn_valid(pfn)) |
| 470 | continue; |
| 471 | |
| 472 | /* |
| 473 | * Check for overlapping nodes/zones. It's possible on some |
| 474 | * configurations to have a setup like |
| 475 | * node0 node1 node0 |
| 476 | * i.e. it's possible that all pages within a zones range of |
| 477 | * pages do not belong to a single zone. |
| 478 | */ |
| 479 | page = pfn_to_page(pfn); |
| 480 | if (page_zone(page) != zone) |
| 481 | continue; |
| 482 | |
| 483 | /* Check the block is suitable for migration */ |
| 484 | if (!suitable_migration_target(page)) |
| 485 | continue; |
| 486 | |
| 487 | /* |
| 488 | * Found a block suitable for isolating free pages from. Now |
| 489 | * we disabled interrupts, double check things are ok and |
| 490 | * isolate the pages. This is to minimise the time IRQs |
| 491 | * are disabled |
| 492 | */ |
| 493 | isolated = 0; |
Mel Gorman | 27d1af8 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 494 | |
| 495 | /* |
| 496 | * The zone lock must be held to isolate freepages. This |
| 497 | * unfortunately this is a very coarse lock and can be |
| 498 | * heavily contended if there are parallel allocations |
| 499 | * or parallel compactions. For async compaction do not |
| 500 | * spin on the lock |
| 501 | */ |
| 502 | if (!compact_trylock_irqsave(&zone->lock, &flags, cc)) |
| 503 | break; |
Michal Nazarewicz | 02ff1de | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 504 | if (suitable_migration_target(page)) { |
| 505 | end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn); |
| 506 | isolated = isolate_freepages_block(pfn, end_pfn, |
| 507 | freelist, false); |
| 508 | nr_freepages += isolated; |
| 509 | } |
| 510 | spin_unlock_irqrestore(&zone->lock, flags); |
| 511 | |
| 512 | /* |
| 513 | * Record the highest PFN we isolated pages from. When next |
| 514 | * looking for free pages, the search will restart here as |
| 515 | * page migration may have returned some pages to the allocator |
| 516 | */ |
| 517 | if (isolated) |
| 518 | high_pfn = max(high_pfn, pfn); |
Michal Nazarewicz | a196a6c | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 519 | } |
| 520 | |
Michal Nazarewicz | 02ff1de | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 521 | /* split_free_page does not map the pages */ |
| 522 | map_pages(freelist); |
Michal Nazarewicz | a196a6c | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 523 | |
Michal Nazarewicz | 02ff1de | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 524 | cc->free_pfn = high_pfn; |
| 525 | cc->nr_freepages = nr_freepages; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | /* |
| 529 | * This is a migrate-callback that "allocates" freepages by taking pages |
| 530 | * from the isolated freelists in the block we are migrating to. |
| 531 | */ |
| 532 | static struct page *compaction_alloc(struct page *migratepage, |
| 533 | unsigned long data, |
| 534 | int **result) |
| 535 | { |
| 536 | struct compact_control *cc = (struct compact_control *)data; |
| 537 | struct page *freepage; |
| 538 | |
| 539 | /* Isolate free pages if necessary */ |
| 540 | if (list_empty(&cc->freepages)) { |
| 541 | isolate_freepages(cc->zone, cc); |
| 542 | |
| 543 | if (list_empty(&cc->freepages)) |
| 544 | return NULL; |
| 545 | } |
| 546 | |
| 547 | freepage = list_entry(cc->freepages.next, struct page, lru); |
| 548 | list_del(&freepage->lru); |
| 549 | cc->nr_freepages--; |
| 550 | |
| 551 | return freepage; |
| 552 | } |
| 553 | |
| 554 | /* |
| 555 | * We cannot control nr_migratepages and nr_freepages fully when migration is |
| 556 | * running as migrate_pages() has no knowledge of compact_control. When |
| 557 | * migration is complete, we count the number of pages on the lists by hand. |
| 558 | */ |
| 559 | static void update_nr_listpages(struct compact_control *cc) |
| 560 | { |
| 561 | int nr_migratepages = 0; |
| 562 | int nr_freepages = 0; |
| 563 | struct page *page; |
| 564 | |
| 565 | list_for_each_entry(page, &cc->migratepages, lru) |
| 566 | nr_migratepages++; |
| 567 | list_for_each_entry(page, &cc->freepages, lru) |
| 568 | nr_freepages++; |
| 569 | |
| 570 | cc->nr_migratepages = nr_migratepages; |
| 571 | cc->nr_freepages = nr_freepages; |
| 572 | } |
| 573 | |
Michal Nazarewicz | 02ff1de | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 574 | /* possible outcome of isolate_migratepages */ |
| 575 | typedef enum { |
| 576 | ISOLATE_ABORT, /* Abort compaction now */ |
| 577 | ISOLATE_NONE, /* No pages isolated, continue scanning */ |
| 578 | ISOLATE_SUCCESS, /* Pages isolated, migrate */ |
| 579 | } isolate_migrate_t; |
| 580 | |
| 581 | /* |
| 582 | * Isolate all pages that can be migrated from the block pointed to by |
| 583 | * the migrate scanner within compact_control. |
| 584 | */ |
| 585 | static isolate_migrate_t isolate_migratepages(struct zone *zone, |
| 586 | struct compact_control *cc) |
| 587 | { |
| 588 | unsigned long low_pfn, end_pfn; |
| 589 | |
| 590 | /* Do not scan outside zone boundaries */ |
| 591 | low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn); |
| 592 | |
| 593 | /* Only scan within a pageblock boundary */ |
| 594 | end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages); |
| 595 | |
| 596 | /* Do not cross the free scanner or scan within a memory hole */ |
| 597 | if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) { |
| 598 | cc->migrate_pfn = end_pfn; |
| 599 | return ISOLATE_NONE; |
| 600 | } |
| 601 | |
| 602 | /* Perform the isolation */ |
| 603 | low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn); |
| 604 | if (!low_pfn) |
| 605 | return ISOLATE_ABORT; |
| 606 | |
| 607 | cc->migrate_pfn = low_pfn; |
| 608 | |
| 609 | return ISOLATE_SUCCESS; |
| 610 | } |
| 611 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 612 | static int compact_finished(struct zone *zone, |
Andrea Arcangeli | 5a03b05 | 2011-01-13 15:47:11 -0800 | [diff] [blame] | 613 | struct compact_control *cc) |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 614 | { |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 615 | unsigned int order; |
Andrea Arcangeli | 5a03b05 | 2011-01-13 15:47:11 -0800 | [diff] [blame] | 616 | unsigned long watermark; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 617 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 618 | if (fatal_signal_pending(current)) |
| 619 | return COMPACT_PARTIAL; |
| 620 | |
| 621 | /* Compaction run completes if the migrate and free scanner meet */ |
| 622 | if (cc->free_pfn <= cc->migrate_pfn) |
| 623 | return COMPACT_COMPLETE; |
| 624 | |
Johannes Weiner | 82478fb | 2011-01-20 14:44:21 -0800 | [diff] [blame] | 625 | /* |
| 626 | * order == -1 is expected when compacting via |
| 627 | * /proc/sys/vm/compact_memory |
| 628 | */ |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 629 | if (cc->order == -1) |
| 630 | return COMPACT_CONTINUE; |
| 631 | |
Michal Hocko | 3957c77 | 2011-06-15 15:08:25 -0700 | [diff] [blame] | 632 | /* Compaction run is not finished if the watermark is not met */ |
| 633 | watermark = low_wmark_pages(zone); |
| 634 | watermark += (1 << cc->order); |
| 635 | |
| 636 | if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0)) |
| 637 | return COMPACT_CONTINUE; |
| 638 | |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 639 | /* Direct compactor: Is a suitable page free? */ |
| 640 | for (order = cc->order; order < MAX_ORDER; order++) { |
| 641 | /* Job done if page is free of the right migratetype */ |
| 642 | if (!list_empty(&zone->free_area[order].free_list[cc->migratetype])) |
| 643 | return COMPACT_PARTIAL; |
| 644 | |
| 645 | /* Job done if allocation would set block type */ |
| 646 | if (order >= pageblock_order && zone->free_area[order].nr_free) |
| 647 | return COMPACT_PARTIAL; |
| 648 | } |
| 649 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 650 | return COMPACT_CONTINUE; |
| 651 | } |
| 652 | |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 653 | /* |
| 654 | * compaction_suitable: Is this suitable to run compaction on this zone now? |
| 655 | * Returns |
| 656 | * COMPACT_SKIPPED - If there are too few free pages for compaction |
| 657 | * COMPACT_PARTIAL - If the allocation would succeed without compaction |
| 658 | * COMPACT_CONTINUE - If compaction should run now |
| 659 | */ |
| 660 | unsigned long compaction_suitable(struct zone *zone, int order) |
| 661 | { |
| 662 | int fragindex; |
| 663 | unsigned long watermark; |
| 664 | |
| 665 | /* |
Michal Hocko | 3957c77 | 2011-06-15 15:08:25 -0700 | [diff] [blame] | 666 | * order == -1 is expected when compacting via |
| 667 | * /proc/sys/vm/compact_memory |
| 668 | */ |
| 669 | if (order == -1) |
| 670 | return COMPACT_CONTINUE; |
| 671 | |
| 672 | /* |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 673 | * Watermarks for order-0 must be met for compaction. Note the 2UL. |
| 674 | * This is because during migration, copies of pages need to be |
| 675 | * allocated and for a short time, the footprint is higher |
| 676 | */ |
| 677 | watermark = low_wmark_pages(zone) + (2UL << order); |
| 678 | if (!zone_watermark_ok(zone, 0, watermark, 0, 0)) |
| 679 | return COMPACT_SKIPPED; |
| 680 | |
| 681 | /* |
| 682 | * fragmentation index determines if allocation failures are due to |
| 683 | * low memory or external fragmentation |
| 684 | * |
Shaohua Li | a582a73 | 2011-06-15 15:08:49 -0700 | [diff] [blame] | 685 | * index of -1000 implies allocations might succeed depending on |
| 686 | * watermarks |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 687 | * index towards 0 implies failure is due to lack of memory |
| 688 | * index towards 1000 implies failure is due to fragmentation |
| 689 | * |
| 690 | * Only compact if a failure would be due to fragmentation. |
| 691 | */ |
| 692 | fragindex = fragmentation_index(zone, order); |
| 693 | if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold) |
| 694 | return COMPACT_SKIPPED; |
| 695 | |
Shaohua Li | a582a73 | 2011-06-15 15:08:49 -0700 | [diff] [blame] | 696 | if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark, |
| 697 | 0, 0)) |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 698 | return COMPACT_PARTIAL; |
| 699 | |
| 700 | return COMPACT_CONTINUE; |
| 701 | } |
| 702 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 703 | static int compact_zone(struct zone *zone, struct compact_control *cc) |
| 704 | { |
| 705 | int ret; |
| 706 | |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 707 | ret = compaction_suitable(zone, cc->order); |
| 708 | switch (ret) { |
| 709 | case COMPACT_PARTIAL: |
| 710 | case COMPACT_SKIPPED: |
| 711 | /* Compaction is likely to fail */ |
| 712 | return ret; |
| 713 | case COMPACT_CONTINUE: |
| 714 | /* Fall through to compaction */ |
| 715 | ; |
| 716 | } |
| 717 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 718 | /* Setup to move all movable pages to the end of the zone */ |
| 719 | cc->migrate_pfn = zone->zone_start_pfn; |
| 720 | cc->free_pfn = cc->migrate_pfn + zone->spanned_pages; |
| 721 | cc->free_pfn &= ~(pageblock_nr_pages-1); |
| 722 | |
| 723 | migrate_prep_local(); |
| 724 | |
| 725 | while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) { |
| 726 | unsigned long nr_migrate, nr_remaining; |
Minchan Kim | 9d502c1 | 2011-03-22 16:30:39 -0700 | [diff] [blame] | 727 | int err; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 728 | |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 729 | switch (isolate_migratepages(zone, cc)) { |
| 730 | case ISOLATE_ABORT: |
| 731 | ret = COMPACT_PARTIAL; |
| 732 | goto out; |
| 733 | case ISOLATE_NONE: |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 734 | continue; |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 735 | case ISOLATE_SUCCESS: |
| 736 | ; |
| 737 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 738 | |
| 739 | nr_migrate = cc->nr_migratepages; |
Minchan Kim | 9d502c1 | 2011-03-22 16:30:39 -0700 | [diff] [blame] | 740 | err = migrate_pages(&cc->migratepages, compaction_alloc, |
Mel Gorman | 7f0f249 | 2011-01-13 15:45:58 -0800 | [diff] [blame] | 741 | (unsigned long)cc, false, |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 742 | cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 743 | update_nr_listpages(cc); |
| 744 | nr_remaining = cc->nr_migratepages; |
| 745 | |
| 746 | count_vm_event(COMPACTBLOCKS); |
| 747 | count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining); |
| 748 | if (nr_remaining) |
| 749 | count_vm_events(COMPACTPAGEFAILED, nr_remaining); |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 750 | trace_mm_compaction_migratepages(nr_migrate - nr_remaining, |
| 751 | nr_remaining); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 752 | |
| 753 | /* Release LRU pages not migrated */ |
Minchan Kim | 9d502c1 | 2011-03-22 16:30:39 -0700 | [diff] [blame] | 754 | if (err) { |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 755 | putback_lru_pages(&cc->migratepages); |
| 756 | cc->nr_migratepages = 0; |
David Rientjes | a56063e | 2012-07-11 14:02:13 -0700 | [diff] [blame] | 757 | if (err == -ENOMEM) { |
| 758 | ret = COMPACT_PARTIAL; |
| 759 | goto out; |
| 760 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 761 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 762 | } |
| 763 | |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 764 | out: |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 765 | /* Release free pages and check accounting */ |
| 766 | cc->nr_freepages -= release_freepages(&cc->freepages); |
| 767 | VM_BUG_ON(cc->nr_freepages != 0); |
| 768 | |
| 769 | return ret; |
| 770 | } |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 771 | |
Kyungmin Park | d43a87e | 2011-10-31 17:09:08 -0700 | [diff] [blame] | 772 | static unsigned long compact_zone_order(struct zone *zone, |
Andrea Arcangeli | 5a03b05 | 2011-01-13 15:47:11 -0800 | [diff] [blame] | 773 | int order, gfp_t gfp_mask, |
Mel Gorman | 27d1af8 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 774 | bool sync, bool *contended) |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 775 | { |
| 776 | struct compact_control cc = { |
| 777 | .nr_freepages = 0, |
| 778 | .nr_migratepages = 0, |
| 779 | .order = order, |
| 780 | .migratetype = allocflags_to_migratetype(gfp_mask), |
| 781 | .zone = zone, |
Mel Gorman | 77f1fe6 | 2011-01-13 15:45:57 -0800 | [diff] [blame] | 782 | .sync = sync, |
Mel Gorman | 27d1af8 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 783 | .contended = contended, |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 784 | }; |
| 785 | INIT_LIST_HEAD(&cc.freepages); |
| 786 | INIT_LIST_HEAD(&cc.migratepages); |
| 787 | |
| 788 | return compact_zone(zone, &cc); |
| 789 | } |
| 790 | |
Mel Gorman | 5e77190 | 2010-05-24 14:32:31 -0700 | [diff] [blame] | 791 | int sysctl_extfrag_threshold = 500; |
| 792 | |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 793 | /** |
| 794 | * try_to_compact_pages - Direct compact to satisfy a high-order allocation |
| 795 | * @zonelist: The zonelist used for the current allocation |
| 796 | * @order: The order of the current allocation |
| 797 | * @gfp_mask: The GFP mask of the current allocation |
| 798 | * @nodemask: The allowed nodes to allocate from |
Mel Gorman | 77f1fe6 | 2011-01-13 15:45:57 -0800 | [diff] [blame] | 799 | * @sync: Whether migration is synchronous or not |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 800 | * |
| 801 | * This is the main entry point for direct page compaction. |
| 802 | */ |
| 803 | unsigned long try_to_compact_pages(struct zonelist *zonelist, |
Mel Gorman | 77f1fe6 | 2011-01-13 15:45:57 -0800 | [diff] [blame] | 804 | int order, gfp_t gfp_mask, nodemask_t *nodemask, |
Mel Gorman | 27d1af8 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 805 | bool sync, bool *contended) |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 806 | { |
| 807 | enum zone_type high_zoneidx = gfp_zone(gfp_mask); |
| 808 | int may_enter_fs = gfp_mask & __GFP_FS; |
| 809 | int may_perform_io = gfp_mask & __GFP_IO; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 810 | struct zoneref *z; |
| 811 | struct zone *zone; |
| 812 | int rc = COMPACT_SKIPPED; |
Bartlomiej Zolnierkiewicz | 1964431 | 2012-10-08 16:32:05 -0700 | [diff] [blame] | 813 | int alloc_flags = 0; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 814 | |
Mel Gorman | 05fd3fd | 2012-10-08 16:29:09 -0700 | [diff] [blame^] | 815 | /* Check if the GFP flags allow compaction */ |
Andrea Arcangeli | c5a73c3 | 2011-01-13 15:47:11 -0800 | [diff] [blame] | 816 | if (!order || !may_enter_fs || !may_perform_io) |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 817 | return rc; |
| 818 | |
| 819 | count_vm_event(COMPACTSTALL); |
| 820 | |
Bartlomiej Zolnierkiewicz | 1964431 | 2012-10-08 16:32:05 -0700 | [diff] [blame] | 821 | #ifdef CONFIG_CMA |
| 822 | if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE) |
| 823 | alloc_flags |= ALLOC_CMA; |
| 824 | #endif |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 825 | /* Compact each zone in the list */ |
| 826 | for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx, |
| 827 | nodemask) { |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 828 | int status; |
| 829 | |
Mel Gorman | 27d1af8 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 830 | status = compact_zone_order(zone, order, gfp_mask, sync, |
| 831 | contended); |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 832 | rc = max(status, rc); |
| 833 | |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 834 | /* If a normal allocation would succeed, stop compacting */ |
Bartlomiej Zolnierkiewicz | 1964431 | 2012-10-08 16:32:05 -0700 | [diff] [blame] | 835 | if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0, |
| 836 | alloc_flags)) |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 837 | break; |
| 838 | } |
| 839 | |
| 840 | return rc; |
| 841 | } |
| 842 | |
| 843 | |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 844 | /* Compact all zones within a node */ |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 845 | static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc) |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 846 | { |
| 847 | int zoneid; |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 848 | struct zone *zone; |
| 849 | |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 850 | for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 851 | |
| 852 | zone = &pgdat->node_zones[zoneid]; |
| 853 | if (!populated_zone(zone)) |
| 854 | continue; |
| 855 | |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 856 | cc->nr_freepages = 0; |
| 857 | cc->nr_migratepages = 0; |
| 858 | cc->zone = zone; |
| 859 | INIT_LIST_HEAD(&cc->freepages); |
| 860 | INIT_LIST_HEAD(&cc->migratepages); |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 861 | |
Dan Carpenter | aad6ec3 | 2012-03-21 16:33:54 -0700 | [diff] [blame] | 862 | if (cc->order == -1 || !compaction_deferred(zone, cc->order)) |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 863 | compact_zone(zone, cc); |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 864 | |
Rik van Riel | aff6224 | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 865 | if (cc->order > 0) { |
| 866 | int ok = zone_watermark_ok(zone, cc->order, |
| 867 | low_wmark_pages(zone), 0, 0); |
Minchan Kim | fc83d28 | 2012-08-21 16:16:03 -0700 | [diff] [blame] | 868 | if (ok && cc->order >= zone->compact_order_failed) |
Rik van Riel | aff6224 | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 869 | zone->compact_order_failed = cc->order + 1; |
| 870 | /* Currently async compaction is never deferred. */ |
| 871 | else if (!ok && cc->sync) |
| 872 | defer_compaction(zone, cc->order); |
| 873 | } |
| 874 | |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 875 | VM_BUG_ON(!list_empty(&cc->freepages)); |
| 876 | VM_BUG_ON(!list_empty(&cc->migratepages)); |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | return 0; |
| 880 | } |
| 881 | |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 882 | int compact_pgdat(pg_data_t *pgdat, int order) |
| 883 | { |
| 884 | struct compact_control cc = { |
| 885 | .order = order, |
| 886 | .sync = false, |
| 887 | }; |
| 888 | |
| 889 | return __compact_pgdat(pgdat, &cc); |
| 890 | } |
| 891 | |
| 892 | static int compact_node(int nid) |
| 893 | { |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 894 | struct compact_control cc = { |
| 895 | .order = -1, |
| 896 | .sync = true, |
| 897 | }; |
| 898 | |
Hugh Dickins | 8575ec2 | 2012-03-21 16:33:53 -0700 | [diff] [blame] | 899 | return __compact_pgdat(NODE_DATA(nid), &cc); |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 900 | } |
| 901 | |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 902 | /* Compact all nodes in the system */ |
| 903 | static int compact_nodes(void) |
| 904 | { |
| 905 | int nid; |
| 906 | |
Hugh Dickins | 8575ec2 | 2012-03-21 16:33:53 -0700 | [diff] [blame] | 907 | /* Flush pending updates to the LRU lists */ |
| 908 | lru_add_drain_all(); |
| 909 | |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 910 | for_each_online_node(nid) |
| 911 | compact_node(nid); |
| 912 | |
| 913 | return COMPACT_COMPLETE; |
| 914 | } |
| 915 | |
| 916 | /* The written value is actually unused, all memory is compacted */ |
| 917 | int sysctl_compact_memory; |
| 918 | |
| 919 | /* This is the entry point for compacting all nodes via /proc/sys/vm */ |
| 920 | int sysctl_compaction_handler(struct ctl_table *table, int write, |
| 921 | void __user *buffer, size_t *length, loff_t *ppos) |
| 922 | { |
| 923 | if (write) |
| 924 | return compact_nodes(); |
| 925 | |
| 926 | return 0; |
| 927 | } |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 928 | |
Mel Gorman | 5e77190 | 2010-05-24 14:32:31 -0700 | [diff] [blame] | 929 | int sysctl_extfrag_handler(struct ctl_table *table, int write, |
| 930 | void __user *buffer, size_t *length, loff_t *ppos) |
| 931 | { |
| 932 | proc_dointvec_minmax(table, write, buffer, length, ppos); |
| 933 | |
| 934 | return 0; |
| 935 | } |
| 936 | |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 937 | #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 938 | ssize_t sysfs_compact_node(struct device *dev, |
| 939 | struct device_attribute *attr, |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 940 | const char *buf, size_t count) |
| 941 | { |
Hugh Dickins | 8575ec2 | 2012-03-21 16:33:53 -0700 | [diff] [blame] | 942 | int nid = dev->id; |
| 943 | |
| 944 | if (nid >= 0 && nid < nr_node_ids && node_online(nid)) { |
| 945 | /* Flush pending updates to the LRU lists */ |
| 946 | lru_add_drain_all(); |
| 947 | |
| 948 | compact_node(nid); |
| 949 | } |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 950 | |
| 951 | return count; |
| 952 | } |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 953 | static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node); |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 954 | |
| 955 | int compaction_register_node(struct node *node) |
| 956 | { |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 957 | return device_create_file(&node->dev, &dev_attr_compact); |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | void compaction_unregister_node(struct node *node) |
| 961 | { |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 962 | return device_remove_file(&node->dev, &dev_attr_compact); |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 963 | } |
| 964 | #endif /* CONFIG_SYSFS && CONFIG_NUMA */ |
Michal Nazarewicz | 02ff1de | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 965 | |
| 966 | #endif /* CONFIG_COMPACTION */ |