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