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