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