blob: 673142db7882914c0865a557a62183356b9518e1 [file] [log] [blame]
Mel Gorman748446b2010-05-24 14:32:27 -07001/*
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 Gorman76ab0f52010-05-24 14:32:28 -070015#include <linux/sysctl.h>
Mel Gormaned4a6d72010-05-24 14:32:29 -070016#include <linux/sysfs.h>
Mel Gorman748446b2010-05-24 14:32:27 -070017#include "internal.h"
18
Minchan Kimab211022012-12-20 15:05:06 -080019#ifdef CONFIG_COMPACTION
20static inline void count_compact_event(enum vm_event_item item)
21{
22 count_vm_event(item);
23}
24
25static inline void count_compact_events(enum vm_event_item item, long delta)
26{
27 count_vm_events(item, delta);
28}
29#else
30#define count_compact_event(item) do { } while (0)
31#define count_compact_events(item, delta) do { } while (0)
32#endif
33
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +010034#if defined CONFIG_COMPACTION || defined CONFIG_CMA
35
Mel Gormanb7aba692011-01-13 15:45:54 -080036#define CREATE_TRACE_POINTS
37#include <trace/events/compaction.h>
38
Mel Gorman748446b2010-05-24 14:32:27 -070039static unsigned long release_freepages(struct list_head *freelist)
40{
41 struct page *page, *next;
42 unsigned long count = 0;
43
44 list_for_each_entry_safe(page, next, freelist, lru) {
45 list_del(&page->lru);
46 __free_page(page);
47 count++;
48 }
49
50 return count;
51}
52
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +010053static void map_pages(struct list_head *list)
54{
55 struct page *page;
56
57 list_for_each_entry(page, list, lru) {
58 arch_alloc_page(page, 0);
59 kernel_map_pages(page, 1, 1);
60 }
61}
62
Michal Nazarewiczd4158d22011-12-29 13:09:50 +010063static inline bool migrate_async_suitable(int migratetype)
64{
65 return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
66}
67
Mel Gorman6b506642012-10-08 16:32:41 -070068#ifdef CONFIG_COMPACTION
69/* Returns true if the pageblock should be scanned for pages to isolate. */
70static inline bool isolation_suitable(struct compact_control *cc,
71 struct page *page)
72{
73 if (cc->ignore_skip_hint)
74 return true;
75
76 return !get_pageblock_skip(page);
77}
78
79/*
80 * This function is called to clear all cached information on pageblocks that
81 * should be skipped for page isolation when the migrate and free page scanner
82 * meet.
83 */
Mel Gorman9317a2b2012-10-08 16:32:47 -070084static void __reset_isolation_suitable(struct zone *zone)
Mel Gorman6b506642012-10-08 16:32:41 -070085{
86 unsigned long start_pfn = zone->zone_start_pfn;
87 unsigned long end_pfn = zone->zone_start_pfn + zone->spanned_pages;
88 unsigned long pfn;
89
Mel Gormand58f1f22012-10-08 16:32:45 -070090 zone->compact_cached_migrate_pfn = start_pfn;
91 zone->compact_cached_free_pfn = end_pfn;
Mel Gorman9317a2b2012-10-08 16:32:47 -070092 zone->compact_blockskip_flush = false;
Mel Gorman6b506642012-10-08 16:32:41 -070093
94 /* Walk the zone and mark every pageblock as suitable for isolation */
95 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
96 struct page *page;
97
98 cond_resched();
99
100 if (!pfn_valid(pfn))
101 continue;
102
103 page = pfn_to_page(pfn);
104 if (zone != page_zone(page))
105 continue;
106
107 clear_pageblock_skip(page);
108 }
109}
110
Mel Gorman9317a2b2012-10-08 16:32:47 -0700111void reset_isolation_suitable(pg_data_t *pgdat)
112{
113 int zoneid;
114
115 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
116 struct zone *zone = &pgdat->node_zones[zoneid];
117 if (!populated_zone(zone))
118 continue;
119
120 /* Only flush if a full compaction finished recently */
121 if (zone->compact_blockskip_flush)
122 __reset_isolation_suitable(zone);
123 }
124}
125
Mel Gorman6b506642012-10-08 16:32:41 -0700126/*
127 * If no pages were isolated then mark this pageblock to be skipped in the
Mel Gorman9317a2b2012-10-08 16:32:47 -0700128 * future. The information is later cleared by __reset_isolation_suitable().
Mel Gorman6b506642012-10-08 16:32:41 -0700129 */
Mel Gormand58f1f22012-10-08 16:32:45 -0700130static void update_pageblock_skip(struct compact_control *cc,
131 struct page *page, unsigned long nr_isolated,
132 bool migrate_scanner)
Mel Gorman6b506642012-10-08 16:32:41 -0700133{
Mel Gormand58f1f22012-10-08 16:32:45 -0700134 struct zone *zone = cc->zone;
Mel Gorman6b506642012-10-08 16:32:41 -0700135 if (!page)
136 return;
137
Mel Gormand58f1f22012-10-08 16:32:45 -0700138 if (!nr_isolated) {
139 unsigned long pfn = page_to_pfn(page);
Mel Gorman6b506642012-10-08 16:32:41 -0700140 set_pageblock_skip(page);
Mel Gormand58f1f22012-10-08 16:32:45 -0700141
142 /* Update where compaction should restart */
143 if (migrate_scanner) {
144 if (!cc->finished_update_migrate &&
145 pfn > zone->compact_cached_migrate_pfn)
146 zone->compact_cached_migrate_pfn = pfn;
147 } else {
148 if (!cc->finished_update_free &&
149 pfn < zone->compact_cached_free_pfn)
150 zone->compact_cached_free_pfn = pfn;
151 }
152 }
Mel Gorman6b506642012-10-08 16:32:41 -0700153}
154#else
155static inline bool isolation_suitable(struct compact_control *cc,
156 struct page *page)
157{
158 return true;
159}
160
Mel Gormand58f1f22012-10-08 16:32:45 -0700161static void update_pageblock_skip(struct compact_control *cc,
162 struct page *page, unsigned long nr_isolated,
163 bool migrate_scanner)
Mel Gorman6b506642012-10-08 16:32:41 -0700164{
165}
166#endif /* CONFIG_COMPACTION */
167
Mel Gorman79f6ed52012-10-08 16:32:33 -0700168static inline bool should_release_lock(spinlock_t *lock)
169{
170 return need_resched() || spin_is_contended(lock);
171}
172
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100173/*
Mel Gorman27d1af82012-08-21 16:16:17 -0700174 * Compaction requires the taking of some coarse locks that are potentially
175 * very heavily contended. Check if the process needs to be scheduled or
176 * if the lock is contended. For async compaction, back out in the event
177 * if contention is severe. For sync compaction, schedule.
178 *
179 * Returns true if the lock is held.
180 * Returns false if the lock is released and compaction should abort
181 */
182static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags,
183 bool locked, struct compact_control *cc)
184{
Mel Gorman79f6ed52012-10-08 16:32:33 -0700185 if (should_release_lock(lock)) {
Mel Gorman27d1af82012-08-21 16:16:17 -0700186 if (locked) {
187 spin_unlock_irqrestore(lock, *flags);
188 locked = false;
189 }
190
191 /* async aborts if taking too long or contended */
192 if (!cc->sync) {
Shaohua Li3cbf3512012-10-08 16:32:27 -0700193 cc->contended = true;
Mel Gorman27d1af82012-08-21 16:16:17 -0700194 return false;
195 }
196
197 cond_resched();
Mel Gorman27d1af82012-08-21 16:16:17 -0700198 }
199
200 if (!locked)
201 spin_lock_irqsave(lock, *flags);
202 return true;
203}
204
205static inline bool compact_trylock_irqsave(spinlock_t *lock,
206 unsigned long *flags, struct compact_control *cc)
207{
208 return compact_checklock_irqsave(lock, flags, false, cc);
209}
210
Mel Gormanb35e2d72012-10-08 16:32:36 -0700211/* Returns true if the page is within a block suitable for migration to */
212static bool suitable_migration_target(struct page *page)
213{
214 int migratetype = get_pageblock_migratetype(page);
215
216 /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
217 if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
218 return false;
219
220 /* If the page is a large free page, then allow migration */
221 if (PageBuddy(page) && page_order(page) >= pageblock_order)
222 return true;
223
224 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
225 if (migrate_async_suitable(migratetype))
226 return true;
227
228 /* Otherwise skip the block */
229 return false;
230}
231
Mel Gorman27d1af82012-08-21 16:16:17 -0700232/*
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100233 * Isolate free pages onto a private freelist. Caller must hold zone->lock.
234 * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
235 * pages inside of the pageblock (even though it may still end up isolating
236 * some pages).
237 */
Mel Gormanb35e2d72012-10-08 16:32:36 -0700238static unsigned long isolate_freepages_block(struct compact_control *cc,
239 unsigned long blockpfn,
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100240 unsigned long end_pfn,
241 struct list_head *freelist,
242 bool strict)
Mel Gorman748446b2010-05-24 14:32:27 -0700243{
Mel Gormanb7aba692011-01-13 15:45:54 -0800244 int nr_scanned = 0, total_isolated = 0;
Mel Gorman6b506642012-10-08 16:32:41 -0700245 struct page *cursor, *valid_page = NULL;
Mel Gormanb35e2d72012-10-08 16:32:36 -0700246 unsigned long nr_strict_required = end_pfn - blockpfn;
247 unsigned long flags;
248 bool locked = false;
Mel Gorman748446b2010-05-24 14:32:27 -0700249
Mel Gorman748446b2010-05-24 14:32:27 -0700250 cursor = pfn_to_page(blockpfn);
251
Mel Gormanb35e2d72012-10-08 16:32:36 -0700252 /* Isolate free pages. */
Mel Gorman748446b2010-05-24 14:32:27 -0700253 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
254 int isolated, i;
255 struct page *page = cursor;
256
Mel Gormanb7aba692011-01-13 15:45:54 -0800257 nr_scanned++;
Mel Gormanb35e2d72012-10-08 16:32:36 -0700258 if (!pfn_valid_within(blockpfn))
Mel Gorman748446b2010-05-24 14:32:27 -0700259 continue;
Mel Gorman6b506642012-10-08 16:32:41 -0700260 if (!valid_page)
261 valid_page = page;
Mel Gormanb35e2d72012-10-08 16:32:36 -0700262 if (!PageBuddy(page))
263 continue;
264
265 /*
266 * The zone lock must be held to isolate freepages.
267 * Unfortunately this is a very coarse lock and can be
268 * heavily contended if there are parallel allocations
269 * or parallel compactions. For async compaction do not
270 * spin on the lock and we acquire the lock as late as
271 * possible.
272 */
273 locked = compact_checklock_irqsave(&cc->zone->lock, &flags,
274 locked, cc);
275 if (!locked)
276 break;
277
278 /* Recheck this is a suitable migration target under lock */
279 if (!strict && !suitable_migration_target(page))
280 break;
281
282 /* Recheck this is a buddy page under lock */
283 if (!PageBuddy(page))
284 continue;
Mel Gorman748446b2010-05-24 14:32:27 -0700285
286 /* Found a free page, break it into order-0 pages */
287 isolated = split_free_page(page);
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100288 if (!isolated && strict)
Mel Gormanb35e2d72012-10-08 16:32:36 -0700289 break;
Mel Gorman748446b2010-05-24 14:32:27 -0700290 total_isolated += isolated;
291 for (i = 0; i < isolated; i++) {
292 list_add(&page->lru, freelist);
293 page++;
294 }
295
296 /* If a page was split, advance to the end of it */
297 if (isolated) {
298 blockpfn += isolated - 1;
299 cursor += isolated - 1;
300 }
301 }
302
Mel Gormanb7aba692011-01-13 15:45:54 -0800303 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
Mel Gormanb35e2d72012-10-08 16:32:36 -0700304
305 /*
306 * If strict isolation is requested by CMA then check that all the
307 * pages requested were isolated. If there were any failures, 0 is
308 * returned and CMA will fail.
309 */
Mel Gorman2a3232d2012-10-19 13:56:57 -0700310 if (strict && nr_strict_required > total_isolated)
Mel Gormanb35e2d72012-10-08 16:32:36 -0700311 total_isolated = 0;
312
313 if (locked)
314 spin_unlock_irqrestore(&cc->zone->lock, flags);
315
Mel Gorman6b506642012-10-08 16:32:41 -0700316 /* Update the pageblock-skip if the whole pageblock was scanned */
317 if (blockpfn == end_pfn)
Mel Gormand58f1f22012-10-08 16:32:45 -0700318 update_pageblock_skip(cc, valid_page, total_isolated, false);
Mel Gorman6b506642012-10-08 16:32:41 -0700319
Minchan Kimab211022012-12-20 15:05:06 -0800320 count_compact_events(COMPACTFREE_SCANNED, nr_scanned);
Mel Gorman3f2afc22012-10-19 12:00:10 +0100321 if (total_isolated)
Minchan Kimab211022012-12-20 15:05:06 -0800322 count_compact_events(COMPACTISOLATED, total_isolated);
Mel Gorman748446b2010-05-24 14:32:27 -0700323 return total_isolated;
324}
325
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100326/**
327 * isolate_freepages_range() - isolate free pages.
328 * @start_pfn: The first PFN to start isolating.
329 * @end_pfn: The one-past-last PFN.
330 *
331 * Non-free pages, invalid PFNs, or zone boundaries within the
332 * [start_pfn, end_pfn) range are considered errors, cause function to
333 * undo its actions and return zero.
334 *
335 * Otherwise, function returns one-past-the-last PFN of isolated page
336 * (which may be greater then end_pfn if end fell in a middle of
337 * a free page).
338 */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100339unsigned long
Mel Gorman6b506642012-10-08 16:32:41 -0700340isolate_freepages_range(struct compact_control *cc,
341 unsigned long start_pfn, unsigned long end_pfn)
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100342{
Mel Gormanb35e2d72012-10-08 16:32:36 -0700343 unsigned long isolated, pfn, block_end_pfn;
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100344 LIST_HEAD(freelist);
345
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100346 for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
Mel Gorman6b506642012-10-08 16:32:41 -0700347 if (!pfn_valid(pfn) || cc->zone != page_zone(pfn_to_page(pfn)))
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100348 break;
349
350 /*
351 * On subsequent iterations ALIGN() is actually not needed,
352 * but we keep it that we not to complicate the code.
353 */
354 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
355 block_end_pfn = min(block_end_pfn, end_pfn);
356
Mel Gorman6b506642012-10-08 16:32:41 -0700357 isolated = isolate_freepages_block(cc, pfn, block_end_pfn,
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100358 &freelist, true);
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100359
360 /*
361 * In strict mode, isolate_freepages_block() returns 0 if
362 * there are any holes in the block (ie. invalid PFNs or
363 * non-free pages).
364 */
365 if (!isolated)
366 break;
367
368 /*
369 * If we managed to isolate pages, it is always (1 << n) *
370 * pageblock_nr_pages for some non-negative n. (Max order
371 * page may span two pageblocks).
372 */
373 }
374
375 /* split_free_page does not map the pages */
376 map_pages(&freelist);
377
378 if (pfn < end_pfn) {
379 /* Loop terminated early, cleanup. */
380 release_freepages(&freelist);
381 return 0;
382 }
383
384 /* We don't use freelists for anything. */
385 return pfn;
386}
387
Mel Gorman748446b2010-05-24 14:32:27 -0700388/* Update the number of anon and file isolated pages in the zone */
Mel Gorman27d1af82012-08-21 16:16:17 -0700389static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700390{
391 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700392 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700393
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700394 list_for_each_entry(page, &cc->migratepages, lru)
395 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700396
Mel Gorman27d1af82012-08-21 16:16:17 -0700397 /* If locked we can use the interrupt unsafe versions */
398 if (locked) {
399 __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
400 __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
401 } else {
402 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
403 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
404 }
Mel Gorman748446b2010-05-24 14:32:27 -0700405}
406
407/* Similar to reclaim, but different enough that they don't share logic */
408static bool too_many_isolated(struct zone *zone)
409{
Minchan Kimbc693042010-09-09 16:38:00 -0700410 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700411
412 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
413 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700414 active = zone_page_state(zone, NR_ACTIVE_FILE) +
415 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700416 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
417 zone_page_state(zone, NR_ISOLATED_ANON);
418
Minchan Kimbc693042010-09-09 16:38:00 -0700419 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700420}
421
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100422/**
423 * isolate_migratepages_range() - isolate all migrate-able pages in range.
424 * @zone: Zone pages are in.
425 * @cc: Compaction control structure.
426 * @low_pfn: The first PFN of the range.
427 * @end_pfn: The one-past-the-last PFN of the range.
Minchan Kimd05b4522012-10-08 16:33:48 -0700428 * @unevictable: true if it allows to isolate unevictable pages
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100429 *
430 * Isolate all pages that can be migrated from the range specified by
431 * [low_pfn, end_pfn). Returns zero if there is a fatal signal
432 * pending), otherwise PFN of the first page that was not scanned
433 * (which may be both less, equal to or more then end_pfn).
434 *
435 * Assumes that cc->migratepages is empty and cc->nr_migratepages is
436 * zero.
437 *
438 * Apart from cc->migratepages and cc->nr_migratetypes this function
439 * does not modify any cc's fields, in particular it does not modify
440 * (or read for that matter) cc->migrate_pfn.
Mel Gorman748446b2010-05-24 14:32:27 -0700441 */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100442unsigned long
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100443isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
Minchan Kimd05b4522012-10-08 16:33:48 -0700444 unsigned long low_pfn, unsigned long end_pfn, bool unevictable)
Mel Gorman748446b2010-05-24 14:32:27 -0700445{
Mel Gorman9927af742011-01-13 15:45:59 -0800446 unsigned long last_pageblock_nr = 0, pageblock_nr;
Mel Gormanb7aba692011-01-13 15:45:54 -0800447 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700448 struct list_head *migratelist = &cc->migratepages;
Konstantin Khlebnikov6d8a50e2012-05-29 15:06:54 -0700449 isolate_mode_t mode = 0;
Mel Gorman27d1af82012-08-21 16:16:17 -0700450 unsigned long flags;
Mel Gorman79f6ed52012-10-08 16:32:33 -0700451 bool locked = false;
Mel Gorman6b506642012-10-08 16:32:41 -0700452 struct page *page = NULL, *valid_page = NULL;
Mel Gorman748446b2010-05-24 14:32:27 -0700453
Mel Gorman748446b2010-05-24 14:32:27 -0700454 /*
455 * Ensure that there are not too many pages isolated from the LRU
456 * list by either parallel reclaimers or compaction. If there are,
457 * delay for some time until fewer pages are isolated
458 */
459 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700460 /* async migration should just abort */
461 if (!cc->sync)
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100462 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700463
Mel Gorman748446b2010-05-24 14:32:27 -0700464 congestion_wait(BLK_RW_ASYNC, HZ/10);
465
466 if (fatal_signal_pending(current))
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100467 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700468 }
469
470 /* Time to isolate some pages for migration */
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700471 cond_resched();
Mel Gorman748446b2010-05-24 14:32:27 -0700472 for (; low_pfn < end_pfn; low_pfn++) {
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700473 /* give a chance to irqs before checking need_resched() */
Mel Gorman79f6ed52012-10-08 16:32:33 -0700474 if (locked && !((low_pfn+1) % SWAP_CLUSTER_MAX)) {
475 if (should_release_lock(&zone->lru_lock)) {
476 spin_unlock_irqrestore(&zone->lru_lock, flags);
477 locked = false;
478 }
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700479 }
Mel Gorman27d1af82012-08-21 16:16:17 -0700480
Mel Gorman0bf380b2012-02-03 15:37:18 -0800481 /*
482 * migrate_pfn does not necessarily start aligned to a
483 * pageblock. Ensure that pfn_valid is called when moving
484 * into a new MAX_ORDER_NR_PAGES range in case of large
485 * memory holes within the zone
486 */
487 if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
488 if (!pfn_valid(low_pfn)) {
489 low_pfn += MAX_ORDER_NR_PAGES - 1;
490 continue;
491 }
492 }
493
Mel Gorman748446b2010-05-24 14:32:27 -0700494 if (!pfn_valid_within(low_pfn))
495 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800496 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700497
Mel Gormandc908602012-02-08 17:13:38 -0800498 /*
499 * Get the page and ensure the page is within the same zone.
500 * See the comment in isolate_freepages about overlapping
501 * nodes. It is deliberate that the new zone lock is not taken
502 * as memory compaction should not move pages between nodes.
503 */
Mel Gorman748446b2010-05-24 14:32:27 -0700504 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800505 if (page_zone(page) != zone)
506 continue;
507
Mel Gorman6b506642012-10-08 16:32:41 -0700508 if (!valid_page)
509 valid_page = page;
510
511 /* If isolation recently failed, do not retry */
512 pageblock_nr = low_pfn >> pageblock_order;
513 if (!isolation_suitable(cc, page))
514 goto next_pageblock;
515
Mel Gormandc908602012-02-08 17:13:38 -0800516 /* Skip if free */
Mel Gorman748446b2010-05-24 14:32:27 -0700517 if (PageBuddy(page))
518 continue;
519
Mel Gorman9927af742011-01-13 15:45:59 -0800520 /*
521 * For async migration, also only scan in MOVABLE blocks. Async
522 * migration is optimistic to see if the minimum amount of work
523 * satisfies the allocation
524 */
Mel Gorman9927af742011-01-13 15:45:59 -0800525 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
Michal Nazarewiczd4158d22011-12-29 13:09:50 +0100526 !migrate_async_suitable(get_pageblock_migratetype(page))) {
Mel Gormand58f1f22012-10-08 16:32:45 -0700527 cc->finished_update_migrate = true;
Mel Gorman79f6ed52012-10-08 16:32:33 -0700528 goto next_pageblock;
Mel Gorman9927af742011-01-13 15:45:59 -0800529 }
530
Mel Gorman79f6ed52012-10-08 16:32:33 -0700531 /* Check may be lockless but that's ok as we recheck later */
Andrea Arcangelibc835012011-01-13 15:47:08 -0800532 if (!PageLRU(page))
533 continue;
534
535 /*
Mel Gorman79f6ed52012-10-08 16:32:33 -0700536 * PageLRU is set. lru_lock normally excludes isolation
537 * splitting and collapsing (collapsing has already happened
538 * if PageLRU is set) but the lock is not necessarily taken
539 * here and it is wasteful to take it just to check transhuge.
540 * Check TransHuge without lock and skip the whole pageblock if
541 * it's either a transhuge or hugetlbfs page, as calling
542 * compound_order() without preventing THP from splitting the
543 * page underneath us may return surprising results.
Andrea Arcangelibc835012011-01-13 15:47:08 -0800544 */
545 if (PageTransHuge(page)) {
Mel Gorman79f6ed52012-10-08 16:32:33 -0700546 if (!locked)
547 goto next_pageblock;
548 low_pfn += (1 << compound_order(page)) - 1;
549 continue;
550 }
551
552 /* Check if it is ok to still hold the lock */
553 locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
554 locked, cc);
555 if (!locked || fatal_signal_pending(current))
556 break;
557
558 /* Recheck PageLRU and PageTransHuge under lock */
559 if (!PageLRU(page))
560 continue;
561 if (PageTransHuge(page)) {
Andrea Arcangelibc835012011-01-13 15:47:08 -0800562 low_pfn += (1 << compound_order(page)) - 1;
563 continue;
564 }
565
Mel Gormanc8244932012-01-12 17:19:38 -0800566 if (!cc->sync)
567 mode |= ISOLATE_ASYNC_MIGRATE;
568
Minchan Kimd05b4522012-10-08 16:33:48 -0700569 if (unevictable)
570 mode |= ISOLATE_UNEVICTABLE;
571
Mel Gorman748446b2010-05-24 14:32:27 -0700572 /* Try isolate the page */
Konstantin Khlebnikov6d8a50e2012-05-29 15:06:54 -0700573 if (__isolate_lru_page(page, mode) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700574 continue;
575
Andrea Arcangelibc835012011-01-13 15:47:08 -0800576 VM_BUG_ON(PageTransCompound(page));
577
Mel Gorman748446b2010-05-24 14:32:27 -0700578 /* Successfully isolated */
Mel Gormand58f1f22012-10-08 16:32:45 -0700579 cc->finished_update_migrate = true;
Mel Gorman748446b2010-05-24 14:32:27 -0700580 del_page_from_lru_list(zone, page, page_lru(page));
581 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700582 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800583 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700584
585 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800586 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
587 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700588 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800589 }
Mel Gorman79f6ed52012-10-08 16:32:33 -0700590
591 continue;
592
593next_pageblock:
594 low_pfn += pageblock_nr_pages;
595 low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
596 last_pageblock_nr = pageblock_nr;
Mel Gorman748446b2010-05-24 14:32:27 -0700597 }
598
Mel Gorman27d1af82012-08-21 16:16:17 -0700599 acct_isolated(zone, locked, cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700600
Mel Gorman27d1af82012-08-21 16:16:17 -0700601 if (locked)
602 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700603
Mel Gorman6b506642012-10-08 16:32:41 -0700604 /* Update the pageblock-skip if the whole pageblock was scanned */
605 if (low_pfn == end_pfn)
Mel Gormand58f1f22012-10-08 16:32:45 -0700606 update_pageblock_skip(cc, valid_page, nr_isolated, true);
Mel Gorman6b506642012-10-08 16:32:41 -0700607
Mel Gormanb7aba692011-01-13 15:45:54 -0800608 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
609
Minchan Kimab211022012-12-20 15:05:06 -0800610 count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned);
Mel Gorman3f2afc22012-10-19 12:00:10 +0100611 if (nr_isolated)
Minchan Kimab211022012-12-20 15:05:06 -0800612 count_compact_events(COMPACTISOLATED, nr_isolated);
Mel Gorman3f2afc22012-10-19 12:00:10 +0100613
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100614 return low_pfn;
615}
616
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100617#endif /* CONFIG_COMPACTION || CONFIG_CMA */
618#ifdef CONFIG_COMPACTION
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100619/*
620 * Based on information in the current compact_control, find blocks
621 * suitable for isolating free pages from and then isolate them.
622 */
623static void isolate_freepages(struct zone *zone,
624 struct compact_control *cc)
625{
626 struct page *page;
627 unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100628 int nr_freepages = cc->nr_freepages;
629 struct list_head *freelist = &cc->freepages;
630
631 /*
632 * Initialise the free scanner. The starting point is where we last
633 * scanned from (or the end of the zone if starting). The low point
634 * is the end of the pageblock the migration scanner is using.
635 */
636 pfn = cc->free_pfn;
637 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
638
639 /*
640 * Take care that if the migration scanner is at the end of the zone
641 * that the free scanner does not accidentally move to the next zone
642 * in the next isolation cycle.
643 */
644 high_pfn = min(low_pfn, pfn);
645
646 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
647
648 /*
649 * Isolate free pages until enough are available to migrate the
650 * pages on cc->migratepages. We stop searching if the migrate
651 * and free page scanners meet or enough free pages are isolated.
652 */
653 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
654 pfn -= pageblock_nr_pages) {
655 unsigned long isolated;
656
657 if (!pfn_valid(pfn))
658 continue;
659
660 /*
661 * Check for overlapping nodes/zones. It's possible on some
662 * configurations to have a setup like
663 * node0 node1 node0
664 * i.e. it's possible that all pages within a zones range of
665 * pages do not belong to a single zone.
666 */
667 page = pfn_to_page(pfn);
668 if (page_zone(page) != zone)
669 continue;
670
671 /* Check the block is suitable for migration */
672 if (!suitable_migration_target(page))
673 continue;
674
Mel Gorman6b506642012-10-08 16:32:41 -0700675 /* If isolation recently failed, do not retry */
676 if (!isolation_suitable(cc, page))
677 continue;
678
Mel Gormanb35e2d72012-10-08 16:32:36 -0700679 /* Found a block suitable for isolating free pages from */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100680 isolated = 0;
Mel Gorman812f7a32012-12-06 19:01:14 +0000681
682 /*
683 * As pfn may not start aligned, pfn+pageblock_nr_page
684 * may cross a MAX_ORDER_NR_PAGES boundary and miss
685 * a pfn_valid check. Ensure isolate_freepages_block()
686 * only scans within a pageblock
687 */
688 end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
689 end_pfn = min(end_pfn, zone_end_pfn);
Mel Gormanb35e2d72012-10-08 16:32:36 -0700690 isolated = isolate_freepages_block(cc, pfn, end_pfn,
691 freelist, false);
692 nr_freepages += isolated;
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100693
694 /*
695 * Record the highest PFN we isolated pages from. When next
696 * looking for free pages, the search will restart here as
697 * page migration may have returned some pages to the allocator
698 */
Mel Gormand58f1f22012-10-08 16:32:45 -0700699 if (isolated) {
700 cc->finished_update_free = true;
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100701 high_pfn = max(high_pfn, pfn);
Mel Gormand58f1f22012-10-08 16:32:45 -0700702 }
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100703 }
704
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100705 /* split_free_page does not map the pages */
706 map_pages(freelist);
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100707
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100708 cc->free_pfn = high_pfn;
709 cc->nr_freepages = nr_freepages;
Mel Gorman748446b2010-05-24 14:32:27 -0700710}
711
712/*
713 * This is a migrate-callback that "allocates" freepages by taking pages
714 * from the isolated freelists in the block we are migrating to.
715 */
716static struct page *compaction_alloc(struct page *migratepage,
717 unsigned long data,
718 int **result)
719{
720 struct compact_control *cc = (struct compact_control *)data;
721 struct page *freepage;
722
723 /* Isolate free pages if necessary */
724 if (list_empty(&cc->freepages)) {
725 isolate_freepages(cc->zone, cc);
726
727 if (list_empty(&cc->freepages))
728 return NULL;
729 }
730
731 freepage = list_entry(cc->freepages.next, struct page, lru);
732 list_del(&freepage->lru);
733 cc->nr_freepages--;
734
735 return freepage;
736}
737
738/*
739 * We cannot control nr_migratepages and nr_freepages fully when migration is
740 * running as migrate_pages() has no knowledge of compact_control. When
741 * migration is complete, we count the number of pages on the lists by hand.
742 */
743static void update_nr_listpages(struct compact_control *cc)
744{
745 int nr_migratepages = 0;
746 int nr_freepages = 0;
747 struct page *page;
748
749 list_for_each_entry(page, &cc->migratepages, lru)
750 nr_migratepages++;
751 list_for_each_entry(page, &cc->freepages, lru)
752 nr_freepages++;
753
754 cc->nr_migratepages = nr_migratepages;
755 cc->nr_freepages = nr_freepages;
756}
757
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100758/* possible outcome of isolate_migratepages */
759typedef enum {
760 ISOLATE_ABORT, /* Abort compaction now */
761 ISOLATE_NONE, /* No pages isolated, continue scanning */
762 ISOLATE_SUCCESS, /* Pages isolated, migrate */
763} isolate_migrate_t;
764
765/*
766 * Isolate all pages that can be migrated from the block pointed to by
767 * the migrate scanner within compact_control.
768 */
769static isolate_migrate_t isolate_migratepages(struct zone *zone,
770 struct compact_control *cc)
771{
772 unsigned long low_pfn, end_pfn;
773
774 /* Do not scan outside zone boundaries */
775 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
776
777 /* Only scan within a pageblock boundary */
778 end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
779
780 /* Do not cross the free scanner or scan within a memory hole */
781 if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
782 cc->migrate_pfn = end_pfn;
783 return ISOLATE_NONE;
784 }
785
786 /* Perform the isolation */
Minchan Kimd05b4522012-10-08 16:33:48 -0700787 low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn, false);
Shaohua Li3cbf3512012-10-08 16:32:27 -0700788 if (!low_pfn || cc->contended)
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100789 return ISOLATE_ABORT;
790
791 cc->migrate_pfn = low_pfn;
792
793 return ISOLATE_SUCCESS;
794}
795
Mel Gorman748446b2010-05-24 14:32:27 -0700796static int compact_finished(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800797 struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700798{
Mel Gorman98149402013-01-11 14:32:16 -0800799 unsigned int order;
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800800 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -0700801
Mel Gorman748446b2010-05-24 14:32:27 -0700802 if (fatal_signal_pending(current))
803 return COMPACT_PARTIAL;
804
805 /* Compaction run completes if the migrate and free scanner meet */
Mel Gorman6b506642012-10-08 16:32:41 -0700806 if (cc->free_pfn <= cc->migrate_pfn) {
Mel Gorman9317a2b2012-10-08 16:32:47 -0700807 /*
808 * Mark that the PG_migrate_skip information should be cleared
809 * by kswapd when it goes to sleep. kswapd does not set the
810 * flag itself as the decision to be clear should be directly
811 * based on an allocation request.
812 */
813 if (!current_is_kswapd())
814 zone->compact_blockskip_flush = true;
815
Mel Gorman748446b2010-05-24 14:32:27 -0700816 return COMPACT_COMPLETE;
Mel Gorman6b506642012-10-08 16:32:41 -0700817 }
Mel Gorman748446b2010-05-24 14:32:27 -0700818
Johannes Weiner82478fb2011-01-20 14:44:21 -0800819 /*
820 * order == -1 is expected when compacting via
821 * /proc/sys/vm/compact_memory
822 */
Mel Gorman56de7262010-05-24 14:32:30 -0700823 if (cc->order == -1)
824 return COMPACT_CONTINUE;
825
Michal Hocko3957c772011-06-15 15:08:25 -0700826 /* Compaction run is not finished if the watermark is not met */
827 watermark = low_wmark_pages(zone);
828 watermark += (1 << cc->order);
829
830 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
831 return COMPACT_CONTINUE;
832
Mel Gorman56de7262010-05-24 14:32:30 -0700833 /* Direct compactor: Is a suitable page free? */
Mel Gorman98149402013-01-11 14:32:16 -0800834 for (order = cc->order; order < MAX_ORDER; order++) {
835 struct free_area *area = &zone->free_area[order];
Mel Gorman56de7262010-05-24 14:32:30 -0700836
Mel Gorman98149402013-01-11 14:32:16 -0800837 /* Job done if page is free of the right migratetype */
838 if (!list_empty(&area->free_list[cc->migratetype]))
839 return COMPACT_PARTIAL;
840
841 /* Job done if allocation would set block type */
842 if (cc->order >= pageblock_order && area->nr_free)
843 return COMPACT_PARTIAL;
Mel Gorman56de7262010-05-24 14:32:30 -0700844 }
845
Mel Gorman748446b2010-05-24 14:32:27 -0700846 return COMPACT_CONTINUE;
847}
848
Mel Gorman3e7d3442011-01-13 15:45:56 -0800849/*
850 * compaction_suitable: Is this suitable to run compaction on this zone now?
851 * Returns
852 * COMPACT_SKIPPED - If there are too few free pages for compaction
853 * COMPACT_PARTIAL - If the allocation would succeed without compaction
854 * COMPACT_CONTINUE - If compaction should run now
855 */
856unsigned long compaction_suitable(struct zone *zone, int order)
857{
858 int fragindex;
859 unsigned long watermark;
860
861 /*
Michal Hocko3957c772011-06-15 15:08:25 -0700862 * order == -1 is expected when compacting via
863 * /proc/sys/vm/compact_memory
864 */
865 if (order == -1)
866 return COMPACT_CONTINUE;
867
868 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -0800869 * Watermarks for order-0 must be met for compaction. Note the 2UL.
870 * This is because during migration, copies of pages need to be
871 * allocated and for a short time, the footprint is higher
872 */
873 watermark = low_wmark_pages(zone) + (2UL << order);
874 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
875 return COMPACT_SKIPPED;
876
877 /*
878 * fragmentation index determines if allocation failures are due to
879 * low memory or external fragmentation
880 *
Shaohua Lia582a732011-06-15 15:08:49 -0700881 * index of -1000 implies allocations might succeed depending on
882 * watermarks
Mel Gorman3e7d3442011-01-13 15:45:56 -0800883 * index towards 0 implies failure is due to lack of memory
884 * index towards 1000 implies failure is due to fragmentation
885 *
886 * Only compact if a failure would be due to fragmentation.
887 */
888 fragindex = fragmentation_index(zone, order);
889 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
890 return COMPACT_SKIPPED;
891
Shaohua Lia582a732011-06-15 15:08:49 -0700892 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
893 0, 0))
Mel Gorman3e7d3442011-01-13 15:45:56 -0800894 return COMPACT_PARTIAL;
895
896 return COMPACT_CONTINUE;
897}
898
Mel Gorman748446b2010-05-24 14:32:27 -0700899static int compact_zone(struct zone *zone, struct compact_control *cc)
900{
901 int ret;
Mel Gormand58f1f22012-10-08 16:32:45 -0700902 unsigned long start_pfn = zone->zone_start_pfn;
903 unsigned long end_pfn = zone->zone_start_pfn + zone->spanned_pages;
Mel Gorman748446b2010-05-24 14:32:27 -0700904
Mel Gorman3e7d3442011-01-13 15:45:56 -0800905 ret = compaction_suitable(zone, cc->order);
906 switch (ret) {
907 case COMPACT_PARTIAL:
908 case COMPACT_SKIPPED:
909 /* Compaction is likely to fail */
910 return ret;
911 case COMPACT_CONTINUE:
912 /* Fall through to compaction */
913 ;
914 }
915
Mel Gormand58f1f22012-10-08 16:32:45 -0700916 /*
917 * Setup to move all movable pages to the end of the zone. Used cached
918 * information on where the scanners should start but check that it
919 * is initialised by ensuring the values are within zone boundaries.
920 */
921 cc->migrate_pfn = zone->compact_cached_migrate_pfn;
922 cc->free_pfn = zone->compact_cached_free_pfn;
923 if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
924 cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
925 zone->compact_cached_free_pfn = cc->free_pfn;
926 }
927 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
928 cc->migrate_pfn = start_pfn;
929 zone->compact_cached_migrate_pfn = cc->migrate_pfn;
930 }
Mel Gorman748446b2010-05-24 14:32:27 -0700931
Mel Gorman9317a2b2012-10-08 16:32:47 -0700932 /*
933 * Clear pageblock skip if there were failures recently and compaction
934 * is about to be retried after being deferred. kswapd does not do
935 * this reset as it'll reset the cached information when going to sleep.
936 */
937 if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
938 __reset_isolation_suitable(zone);
Mel Gorman6b506642012-10-08 16:32:41 -0700939
Mel Gorman748446b2010-05-24 14:32:27 -0700940 migrate_prep_local();
941
942 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
943 unsigned long nr_migrate, nr_remaining;
Minchan Kim9d502c12011-03-22 16:30:39 -0700944 int err;
Mel Gorman748446b2010-05-24 14:32:27 -0700945
Mel Gormanf9e35b32011-06-15 15:08:52 -0700946 switch (isolate_migratepages(zone, cc)) {
947 case ISOLATE_ABORT:
948 ret = COMPACT_PARTIAL;
Shaohua Li3cbf3512012-10-08 16:32:27 -0700949 putback_lru_pages(&cc->migratepages);
950 cc->nr_migratepages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700951 goto out;
952 case ISOLATE_NONE:
Mel Gorman748446b2010-05-24 14:32:27 -0700953 continue;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700954 case ISOLATE_SUCCESS:
955 ;
956 }
Mel Gorman748446b2010-05-24 14:32:27 -0700957
958 nr_migrate = cc->nr_migratepages;
Minchan Kim9d502c12011-03-22 16:30:39 -0700959 err = migrate_pages(&cc->migratepages, compaction_alloc,
Mel Gorman7f0f2492011-01-13 15:45:58 -0800960 (unsigned long)cc, false,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800961 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
Mel Gorman748446b2010-05-24 14:32:27 -0700962 update_nr_listpages(cc);
963 nr_remaining = cc->nr_migratepages;
964
Mel Gormanb7aba692011-01-13 15:45:54 -0800965 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
966 nr_remaining);
Mel Gorman748446b2010-05-24 14:32:27 -0700967
968 /* Release LRU pages not migrated */
Minchan Kim9d502c12011-03-22 16:30:39 -0700969 if (err) {
Mel Gorman748446b2010-05-24 14:32:27 -0700970 putback_lru_pages(&cc->migratepages);
971 cc->nr_migratepages = 0;
David Rientjesa56063e2012-07-11 14:02:13 -0700972 if (err == -ENOMEM) {
973 ret = COMPACT_PARTIAL;
974 goto out;
975 }
Mel Gorman748446b2010-05-24 14:32:27 -0700976 }
Mel Gorman748446b2010-05-24 14:32:27 -0700977 }
978
Mel Gormanf9e35b32011-06-15 15:08:52 -0700979out:
Mel Gorman748446b2010-05-24 14:32:27 -0700980 /* Release free pages and check accounting */
981 cc->nr_freepages -= release_freepages(&cc->freepages);
982 VM_BUG_ON(cc->nr_freepages != 0);
983
984 return ret;
985}
Mel Gorman76ab0f52010-05-24 14:32:28 -0700986
Kyungmin Parkd43a87e2011-10-31 17:09:08 -0700987static unsigned long compact_zone_order(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800988 int order, gfp_t gfp_mask,
Mel Gorman98149402013-01-11 14:32:16 -0800989 bool sync, bool *contended)
Mel Gorman56de7262010-05-24 14:32:30 -0700990{
Shaohua Li3cbf3512012-10-08 16:32:27 -0700991 unsigned long ret;
Mel Gorman56de7262010-05-24 14:32:30 -0700992 struct compact_control cc = {
993 .nr_freepages = 0,
994 .nr_migratepages = 0,
995 .order = order,
996 .migratetype = allocflags_to_migratetype(gfp_mask),
997 .zone = zone,
Mel Gorman77f1fe62011-01-13 15:45:57 -0800998 .sync = sync,
Mel Gorman56de7262010-05-24 14:32:30 -0700999 };
1000 INIT_LIST_HEAD(&cc.freepages);
1001 INIT_LIST_HEAD(&cc.migratepages);
1002
Shaohua Li3cbf3512012-10-08 16:32:27 -07001003 ret = compact_zone(zone, &cc);
1004
1005 VM_BUG_ON(!list_empty(&cc.freepages));
1006 VM_BUG_ON(!list_empty(&cc.migratepages));
1007
1008 *contended = cc.contended;
1009 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001010}
1011
Mel Gorman5e771902010-05-24 14:32:31 -07001012int sysctl_extfrag_threshold = 500;
1013
Mel Gorman56de7262010-05-24 14:32:30 -07001014/**
1015 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
1016 * @zonelist: The zonelist used for the current allocation
1017 * @order: The order of the current allocation
1018 * @gfp_mask: The GFP mask of the current allocation
1019 * @nodemask: The allowed nodes to allocate from
Mel Gorman77f1fe62011-01-13 15:45:57 -08001020 * @sync: Whether migration is synchronous or not
Mel Gorman5e663dc2012-10-08 16:32:31 -07001021 * @contended: Return value that is true if compaction was aborted due to lock contention
1022 * @page: Optionally capture a free page of the requested order during compaction
Mel Gorman56de7262010-05-24 14:32:30 -07001023 *
1024 * This is the main entry point for direct page compaction.
1025 */
1026unsigned long try_to_compact_pages(struct zonelist *zonelist,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001027 int order, gfp_t gfp_mask, nodemask_t *nodemask,
Mel Gorman98149402013-01-11 14:32:16 -08001028 bool sync, bool *contended)
Mel Gorman56de7262010-05-24 14:32:30 -07001029{
1030 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
1031 int may_enter_fs = gfp_mask & __GFP_FS;
1032 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -07001033 struct zoneref *z;
1034 struct zone *zone;
1035 int rc = COMPACT_SKIPPED;
Bartlomiej Zolnierkiewicz19644312012-10-08 16:32:05 -07001036 int alloc_flags = 0;
Mel Gorman56de7262010-05-24 14:32:30 -07001037
Mel Gorman05fd3fd2012-10-08 16:29:09 -07001038 /* Check if the GFP flags allow compaction */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -08001039 if (!order || !may_enter_fs || !may_perform_io)
Mel Gorman56de7262010-05-24 14:32:30 -07001040 return rc;
1041
Minchan Kimab211022012-12-20 15:05:06 -08001042 count_compact_event(COMPACTSTALL);
Mel Gorman56de7262010-05-24 14:32:30 -07001043
Bartlomiej Zolnierkiewicz19644312012-10-08 16:32:05 -07001044#ifdef CONFIG_CMA
1045 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
1046 alloc_flags |= ALLOC_CMA;
1047#endif
Mel Gorman56de7262010-05-24 14:32:30 -07001048 /* Compact each zone in the list */
1049 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
1050 nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -07001051 int status;
1052
Mel Gorman27d1af82012-08-21 16:16:17 -07001053 status = compact_zone_order(zone, order, gfp_mask, sync,
Mel Gorman98149402013-01-11 14:32:16 -08001054 contended);
Mel Gorman56de7262010-05-24 14:32:30 -07001055 rc = max(status, rc);
1056
Mel Gorman3e7d3442011-01-13 15:45:56 -08001057 /* If a normal allocation would succeed, stop compacting */
Bartlomiej Zolnierkiewicz19644312012-10-08 16:32:05 -07001058 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
1059 alloc_flags))
Mel Gorman56de7262010-05-24 14:32:30 -07001060 break;
1061 }
1062
1063 return rc;
1064}
1065
1066
Mel Gorman76ab0f52010-05-24 14:32:28 -07001067/* Compact all zones within a node */
Rik van Riel7be62de2012-03-21 16:33:52 -07001068static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001069{
1070 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -07001071 struct zone *zone;
1072
Mel Gorman76ab0f52010-05-24 14:32:28 -07001073 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -07001074
1075 zone = &pgdat->node_zones[zoneid];
1076 if (!populated_zone(zone))
1077 continue;
1078
Rik van Riel7be62de2012-03-21 16:33:52 -07001079 cc->nr_freepages = 0;
1080 cc->nr_migratepages = 0;
1081 cc->zone = zone;
1082 INIT_LIST_HEAD(&cc->freepages);
1083 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001084
Dan Carpenteraad6ec32012-03-21 16:33:54 -07001085 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -07001086 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001087
Rik van Rielaff62242012-03-21 16:33:52 -07001088 if (cc->order > 0) {
1089 int ok = zone_watermark_ok(zone, cc->order,
1090 low_wmark_pages(zone), 0, 0);
Minchan Kimfc83d282012-08-21 16:16:03 -07001091 if (ok && cc->order >= zone->compact_order_failed)
Rik van Rielaff62242012-03-21 16:33:52 -07001092 zone->compact_order_failed = cc->order + 1;
1093 /* Currently async compaction is never deferred. */
1094 else if (!ok && cc->sync)
1095 defer_compaction(zone, cc->order);
1096 }
1097
Rik van Riel7be62de2012-03-21 16:33:52 -07001098 VM_BUG_ON(!list_empty(&cc->freepages));
1099 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -07001100 }
1101
1102 return 0;
1103}
1104
Rik van Riel7be62de2012-03-21 16:33:52 -07001105int compact_pgdat(pg_data_t *pgdat, int order)
1106{
1107 struct compact_control cc = {
1108 .order = order,
1109 .sync = false,
1110 };
1111
1112 return __compact_pgdat(pgdat, &cc);
1113}
1114
1115static int compact_node(int nid)
1116{
Rik van Riel7be62de2012-03-21 16:33:52 -07001117 struct compact_control cc = {
1118 .order = -1,
1119 .sync = true,
1120 };
1121
Hugh Dickins8575ec22012-03-21 16:33:53 -07001122 return __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001123}
1124
Mel Gorman76ab0f52010-05-24 14:32:28 -07001125/* Compact all nodes in the system */
Jason Liu7cca82b2013-01-11 14:31:47 -08001126static void compact_nodes(void)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001127{
1128 int nid;
1129
Hugh Dickins8575ec22012-03-21 16:33:53 -07001130 /* Flush pending updates to the LRU lists */
1131 lru_add_drain_all();
1132
Mel Gorman76ab0f52010-05-24 14:32:28 -07001133 for_each_online_node(nid)
1134 compact_node(nid);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001135}
1136
1137/* The written value is actually unused, all memory is compacted */
1138int sysctl_compact_memory;
1139
1140/* This is the entry point for compacting all nodes via /proc/sys/vm */
1141int sysctl_compaction_handler(struct ctl_table *table, int write,
1142 void __user *buffer, size_t *length, loff_t *ppos)
1143{
1144 if (write)
Jason Liu7cca82b2013-01-11 14:31:47 -08001145 compact_nodes();
Mel Gorman76ab0f52010-05-24 14:32:28 -07001146
1147 return 0;
1148}
Mel Gormaned4a6d72010-05-24 14:32:29 -07001149
Mel Gorman5e771902010-05-24 14:32:31 -07001150int sysctl_extfrag_handler(struct ctl_table *table, int write,
1151 void __user *buffer, size_t *length, loff_t *ppos)
1152{
1153 proc_dointvec_minmax(table, write, buffer, length, ppos);
1154
1155 return 0;
1156}
1157
Mel Gormaned4a6d72010-05-24 14:32:29 -07001158#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Kay Sievers10fbcf42011-12-21 14:48:43 -08001159ssize_t sysfs_compact_node(struct device *dev,
1160 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -07001161 const char *buf, size_t count)
1162{
Hugh Dickins8575ec22012-03-21 16:33:53 -07001163 int nid = dev->id;
1164
1165 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1166 /* Flush pending updates to the LRU lists */
1167 lru_add_drain_all();
1168
1169 compact_node(nid);
1170 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07001171
1172 return count;
1173}
Kay Sievers10fbcf42011-12-21 14:48:43 -08001174static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001175
1176int compaction_register_node(struct node *node)
1177{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001178 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001179}
1180
1181void compaction_unregister_node(struct node *node)
1182{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001183 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001184}
1185#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +01001186
1187#endif /* CONFIG_COMPACTION */