blob: 35bb243740f290b8571e35c2c6c7096b4d8ad7cb [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 flags;
247 bool locked = false;
Mel Gorman748446b2010-05-24 14:32:27 -0700248
Mel Gorman748446b2010-05-24 14:32:27 -0700249 cursor = pfn_to_page(blockpfn);
250
Mel Gormanb35e2d72012-10-08 16:32:36 -0700251 /* Isolate free pages. */
Mel Gorman748446b2010-05-24 14:32:27 -0700252 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
253 int isolated, i;
254 struct page *page = cursor;
255
Mel Gormanb7aba692011-01-13 15:45:54 -0800256 nr_scanned++;
Mel Gormanb35e2d72012-10-08 16:32:36 -0700257 if (!pfn_valid_within(blockpfn))
Laura Abbott7a93e742014-03-05 17:37:22 -0800258 goto isolate_fail;
259
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))
Laura Abbott7a93e742014-03-05 17:37:22 -0800263 goto isolate_fail;
Mel Gormanb35e2d72012-10-08 16:32:36 -0700264
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))
Laura Abbott7a93e742014-03-05 17:37:22 -0800284 goto isolate_fail;
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);
288 total_isolated += isolated;
289 for (i = 0; i < isolated; i++) {
290 list_add(&page->lru, freelist);
291 page++;
292 }
293
294 /* If a page was split, advance to the end of it */
295 if (isolated) {
296 blockpfn += isolated - 1;
297 cursor += isolated - 1;
Laura Abbott7a93e742014-03-05 17:37:22 -0800298 continue;
Mel Gorman748446b2010-05-24 14:32:27 -0700299 }
Laura Abbott7a93e742014-03-05 17:37:22 -0800300
301isolate_fail:
302 if (strict)
303 break;
304
Mel Gorman748446b2010-05-24 14:32:27 -0700305 }
306
Mel Gormanb7aba692011-01-13 15:45:54 -0800307 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
Mel Gormanb35e2d72012-10-08 16:32:36 -0700308
309 /*
310 * If strict isolation is requested by CMA then check that all the
311 * pages requested were isolated. If there were any failures, 0 is
312 * returned and CMA will fail.
313 */
Laura Abbott7a93e742014-03-05 17:37:22 -0800314 if (strict && blockpfn < end_pfn)
Mel Gormanb35e2d72012-10-08 16:32:36 -0700315 total_isolated = 0;
316
317 if (locked)
318 spin_unlock_irqrestore(&cc->zone->lock, flags);
319
Mel Gorman6b506642012-10-08 16:32:41 -0700320 /* Update the pageblock-skip if the whole pageblock was scanned */
321 if (blockpfn == end_pfn)
Mel Gormand58f1f22012-10-08 16:32:45 -0700322 update_pageblock_skip(cc, valid_page, total_isolated, false);
Mel Gorman6b506642012-10-08 16:32:41 -0700323
Minchan Kimab211022012-12-20 15:05:06 -0800324 count_compact_events(COMPACTFREE_SCANNED, nr_scanned);
Mel Gorman3f2afc22012-10-19 12:00:10 +0100325 if (total_isolated)
Minchan Kimab211022012-12-20 15:05:06 -0800326 count_compact_events(COMPACTISOLATED, total_isolated);
Mel Gorman748446b2010-05-24 14:32:27 -0700327 return total_isolated;
328}
329
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100330/**
331 * isolate_freepages_range() - isolate free pages.
332 * @start_pfn: The first PFN to start isolating.
333 * @end_pfn: The one-past-last PFN.
334 *
335 * Non-free pages, invalid PFNs, or zone boundaries within the
336 * [start_pfn, end_pfn) range are considered errors, cause function to
337 * undo its actions and return zero.
338 *
339 * Otherwise, function returns one-past-the-last PFN of isolated page
340 * (which may be greater then end_pfn if end fell in a middle of
341 * a free page).
342 */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100343unsigned long
Mel Gorman6b506642012-10-08 16:32:41 -0700344isolate_freepages_range(struct compact_control *cc,
345 unsigned long start_pfn, unsigned long end_pfn)
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100346{
Mel Gormanb35e2d72012-10-08 16:32:36 -0700347 unsigned long isolated, pfn, block_end_pfn;
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100348 LIST_HEAD(freelist);
349
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100350 for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
Mel Gorman6b506642012-10-08 16:32:41 -0700351 if (!pfn_valid(pfn) || cc->zone != page_zone(pfn_to_page(pfn)))
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100352 break;
353
354 /*
355 * On subsequent iterations ALIGN() is actually not needed,
356 * but we keep it that we not to complicate the code.
357 */
358 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
359 block_end_pfn = min(block_end_pfn, end_pfn);
360
Mel Gorman6b506642012-10-08 16:32:41 -0700361 isolated = isolate_freepages_block(cc, pfn, block_end_pfn,
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100362 &freelist, true);
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100363
364 /*
365 * In strict mode, isolate_freepages_block() returns 0 if
366 * there are any holes in the block (ie. invalid PFNs or
367 * non-free pages).
368 */
369 if (!isolated)
370 break;
371
372 /*
373 * If we managed to isolate pages, it is always (1 << n) *
374 * pageblock_nr_pages for some non-negative n. (Max order
375 * page may span two pageblocks).
376 */
377 }
378
379 /* split_free_page does not map the pages */
380 map_pages(&freelist);
381
382 if (pfn < end_pfn) {
383 /* Loop terminated early, cleanup. */
384 release_freepages(&freelist);
385 return 0;
386 }
387
388 /* We don't use freelists for anything. */
389 return pfn;
390}
391
Mel Gorman748446b2010-05-24 14:32:27 -0700392/* Update the number of anon and file isolated pages in the zone */
Mel Gorman27d1af82012-08-21 16:16:17 -0700393static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700394{
395 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700396 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700397
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700398 list_for_each_entry(page, &cc->migratepages, lru)
399 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700400
Mel Gorman27d1af82012-08-21 16:16:17 -0700401 /* If locked we can use the interrupt unsafe versions */
402 if (locked) {
403 __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
404 __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
405 } else {
406 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
407 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
408 }
Mel Gorman748446b2010-05-24 14:32:27 -0700409}
410
411/* Similar to reclaim, but different enough that they don't share logic */
412static bool too_many_isolated(struct zone *zone)
413{
Minchan Kimbc693042010-09-09 16:38:00 -0700414 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700415
416 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
417 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700418 active = zone_page_state(zone, NR_ACTIVE_FILE) +
419 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700420 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
421 zone_page_state(zone, NR_ISOLATED_ANON);
422
Minchan Kimbc693042010-09-09 16:38:00 -0700423 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700424}
425
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100426/**
427 * isolate_migratepages_range() - isolate all migrate-able pages in range.
428 * @zone: Zone pages are in.
429 * @cc: Compaction control structure.
430 * @low_pfn: The first PFN of the range.
431 * @end_pfn: The one-past-the-last PFN of the range.
Minchan Kimd05b4522012-10-08 16:33:48 -0700432 * @unevictable: true if it allows to isolate unevictable pages
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100433 *
434 * Isolate all pages that can be migrated from the range specified by
435 * [low_pfn, end_pfn). Returns zero if there is a fatal signal
436 * pending), otherwise PFN of the first page that was not scanned
437 * (which may be both less, equal to or more then end_pfn).
438 *
439 * Assumes that cc->migratepages is empty and cc->nr_migratepages is
440 * zero.
441 *
442 * Apart from cc->migratepages and cc->nr_migratetypes this function
443 * does not modify any cc's fields, in particular it does not modify
444 * (or read for that matter) cc->migrate_pfn.
Mel Gorman748446b2010-05-24 14:32:27 -0700445 */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100446unsigned long
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100447isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
Minchan Kimd05b4522012-10-08 16:33:48 -0700448 unsigned long low_pfn, unsigned long end_pfn, bool unevictable)
Mel Gorman748446b2010-05-24 14:32:27 -0700449{
Mel Gorman9927af742011-01-13 15:45:59 -0800450 unsigned long last_pageblock_nr = 0, pageblock_nr;
Mel Gormanb7aba692011-01-13 15:45:54 -0800451 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700452 struct list_head *migratelist = &cc->migratepages;
Konstantin Khlebnikov6d8a50e2012-05-29 15:06:54 -0700453 isolate_mode_t mode = 0;
Mel Gorman27d1af82012-08-21 16:16:17 -0700454 unsigned long flags;
Mel Gorman79f6ed52012-10-08 16:32:33 -0700455 bool locked = false;
Mel Gorman6b506642012-10-08 16:32:41 -0700456 struct page *page = NULL, *valid_page = NULL;
Mel Gorman748446b2010-05-24 14:32:27 -0700457
Mel Gorman748446b2010-05-24 14:32:27 -0700458 /*
459 * Ensure that there are not too many pages isolated from the LRU
460 * list by either parallel reclaimers or compaction. If there are,
461 * delay for some time until fewer pages are isolated
462 */
463 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700464 /* async migration should just abort */
465 if (!cc->sync)
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100466 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700467
Mel Gorman748446b2010-05-24 14:32:27 -0700468 congestion_wait(BLK_RW_ASYNC, HZ/10);
469
470 if (fatal_signal_pending(current))
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100471 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700472 }
473
474 /* Time to isolate some pages for migration */
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700475 cond_resched();
Mel Gorman748446b2010-05-24 14:32:27 -0700476 for (; low_pfn < end_pfn; low_pfn++) {
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700477 /* give a chance to irqs before checking need_resched() */
Mel Gorman79f6ed52012-10-08 16:32:33 -0700478 if (locked && !((low_pfn+1) % SWAP_CLUSTER_MAX)) {
479 if (should_release_lock(&zone->lru_lock)) {
480 spin_unlock_irqrestore(&zone->lru_lock, flags);
481 locked = false;
482 }
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700483 }
Mel Gorman27d1af82012-08-21 16:16:17 -0700484
Mel Gorman0bf380b2012-02-03 15:37:18 -0800485 /*
486 * migrate_pfn does not necessarily start aligned to a
487 * pageblock. Ensure that pfn_valid is called when moving
488 * into a new MAX_ORDER_NR_PAGES range in case of large
489 * memory holes within the zone
490 */
491 if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
492 if (!pfn_valid(low_pfn)) {
493 low_pfn += MAX_ORDER_NR_PAGES - 1;
494 continue;
495 }
496 }
497
Mel Gorman748446b2010-05-24 14:32:27 -0700498 if (!pfn_valid_within(low_pfn))
499 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800500 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700501
Mel Gormandc908602012-02-08 17:13:38 -0800502 /*
503 * Get the page and ensure the page is within the same zone.
504 * See the comment in isolate_freepages about overlapping
505 * nodes. It is deliberate that the new zone lock is not taken
506 * as memory compaction should not move pages between nodes.
507 */
Mel Gorman748446b2010-05-24 14:32:27 -0700508 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800509 if (page_zone(page) != zone)
510 continue;
511
Mel Gorman6b506642012-10-08 16:32:41 -0700512 if (!valid_page)
513 valid_page = page;
514
515 /* If isolation recently failed, do not retry */
516 pageblock_nr = low_pfn >> pageblock_order;
517 if (!isolation_suitable(cc, page))
518 goto next_pageblock;
519
Mel Gormandc908602012-02-08 17:13:38 -0800520 /* Skip if free */
Mel Gorman748446b2010-05-24 14:32:27 -0700521 if (PageBuddy(page))
522 continue;
523
Mel Gorman9927af742011-01-13 15:45:59 -0800524 /*
525 * For async migration, also only scan in MOVABLE blocks. Async
526 * migration is optimistic to see if the minimum amount of work
527 * satisfies the allocation
528 */
Mel Gorman9927af742011-01-13 15:45:59 -0800529 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
Michal Nazarewiczd4158d22011-12-29 13:09:50 +0100530 !migrate_async_suitable(get_pageblock_migratetype(page))) {
Mel Gormand58f1f22012-10-08 16:32:45 -0700531 cc->finished_update_migrate = true;
Mel Gorman79f6ed52012-10-08 16:32:33 -0700532 goto next_pageblock;
Mel Gorman9927af742011-01-13 15:45:59 -0800533 }
534
Mel Gorman79f6ed52012-10-08 16:32:33 -0700535 /* Check may be lockless but that's ok as we recheck later */
Andrea Arcangelibc835012011-01-13 15:47:08 -0800536 if (!PageLRU(page))
537 continue;
538
539 /*
Mel Gorman79f6ed52012-10-08 16:32:33 -0700540 * PageLRU is set. lru_lock normally excludes isolation
541 * splitting and collapsing (collapsing has already happened
542 * if PageLRU is set) but the lock is not necessarily taken
543 * here and it is wasteful to take it just to check transhuge.
544 * Check TransHuge without lock and skip the whole pageblock if
545 * it's either a transhuge or hugetlbfs page, as calling
546 * compound_order() without preventing THP from splitting the
547 * page underneath us may return surprising results.
Andrea Arcangelibc835012011-01-13 15:47:08 -0800548 */
549 if (PageTransHuge(page)) {
Mel Gorman79f6ed52012-10-08 16:32:33 -0700550 if (!locked)
551 goto next_pageblock;
552 low_pfn += (1 << compound_order(page)) - 1;
553 continue;
554 }
555
556 /* Check if it is ok to still hold the lock */
557 locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
558 locked, cc);
559 if (!locked || fatal_signal_pending(current))
560 break;
561
562 /* Recheck PageLRU and PageTransHuge under lock */
563 if (!PageLRU(page))
564 continue;
565 if (PageTransHuge(page)) {
Andrea Arcangelibc835012011-01-13 15:47:08 -0800566 low_pfn += (1 << compound_order(page)) - 1;
567 continue;
568 }
569
Mel Gormanc8244932012-01-12 17:19:38 -0800570 if (!cc->sync)
571 mode |= ISOLATE_ASYNC_MIGRATE;
572
Minchan Kimd05b4522012-10-08 16:33:48 -0700573 if (unevictable)
574 mode |= ISOLATE_UNEVICTABLE;
575
Mel Gorman748446b2010-05-24 14:32:27 -0700576 /* Try isolate the page */
Konstantin Khlebnikov6d8a50e2012-05-29 15:06:54 -0700577 if (__isolate_lru_page(page, mode) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700578 continue;
579
Andrea Arcangelibc835012011-01-13 15:47:08 -0800580 VM_BUG_ON(PageTransCompound(page));
581
Mel Gorman748446b2010-05-24 14:32:27 -0700582 /* Successfully isolated */
Mel Gormand58f1f22012-10-08 16:32:45 -0700583 cc->finished_update_migrate = true;
Mel Gorman748446b2010-05-24 14:32:27 -0700584 del_page_from_lru_list(zone, page, page_lru(page));
585 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700586 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800587 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700588
589 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800590 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
591 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700592 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800593 }
Mel Gorman79f6ed52012-10-08 16:32:33 -0700594
595 continue;
596
597next_pageblock:
598 low_pfn += pageblock_nr_pages;
599 low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
600 last_pageblock_nr = pageblock_nr;
Mel Gorman748446b2010-05-24 14:32:27 -0700601 }
602
Mel Gorman27d1af82012-08-21 16:16:17 -0700603 acct_isolated(zone, locked, cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700604
Mel Gorman27d1af82012-08-21 16:16:17 -0700605 if (locked)
606 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700607
Mel Gorman6b506642012-10-08 16:32:41 -0700608 /* Update the pageblock-skip if the whole pageblock was scanned */
609 if (low_pfn == end_pfn)
Mel Gormand58f1f22012-10-08 16:32:45 -0700610 update_pageblock_skip(cc, valid_page, nr_isolated, true);
Mel Gorman6b506642012-10-08 16:32:41 -0700611
Mel Gormanb7aba692011-01-13 15:45:54 -0800612 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
613
Minchan Kimab211022012-12-20 15:05:06 -0800614 count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned);
Mel Gorman3f2afc22012-10-19 12:00:10 +0100615 if (nr_isolated)
Minchan Kimab211022012-12-20 15:05:06 -0800616 count_compact_events(COMPACTISOLATED, nr_isolated);
Mel Gorman3f2afc22012-10-19 12:00:10 +0100617
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100618 return low_pfn;
619}
620
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100621#endif /* CONFIG_COMPACTION || CONFIG_CMA */
622#ifdef CONFIG_COMPACTION
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100623/*
624 * Based on information in the current compact_control, find blocks
625 * suitable for isolating free pages from and then isolate them.
626 */
627static void isolate_freepages(struct zone *zone,
628 struct compact_control *cc)
629{
630 struct page *page;
631 unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100632 int nr_freepages = cc->nr_freepages;
633 struct list_head *freelist = &cc->freepages;
634
635 /*
636 * Initialise the free scanner. The starting point is where we last
637 * scanned from (or the end of the zone if starting). The low point
638 * is the end of the pageblock the migration scanner is using.
639 */
640 pfn = cc->free_pfn;
641 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
642
643 /*
644 * Take care that if the migration scanner is at the end of the zone
645 * that the free scanner does not accidentally move to the next zone
646 * in the next isolation cycle.
647 */
648 high_pfn = min(low_pfn, pfn);
649
650 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
651
652 /*
653 * Isolate free pages until enough are available to migrate the
654 * pages on cc->migratepages. We stop searching if the migrate
655 * and free page scanners meet or enough free pages are isolated.
656 */
657 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
658 pfn -= pageblock_nr_pages) {
659 unsigned long isolated;
660
661 if (!pfn_valid(pfn))
662 continue;
663
664 /*
665 * Check for overlapping nodes/zones. It's possible on some
666 * configurations to have a setup like
667 * node0 node1 node0
668 * i.e. it's possible that all pages within a zones range of
669 * pages do not belong to a single zone.
670 */
671 page = pfn_to_page(pfn);
672 if (page_zone(page) != zone)
673 continue;
674
675 /* Check the block is suitable for migration */
676 if (!suitable_migration_target(page))
677 continue;
678
Mel Gorman6b506642012-10-08 16:32:41 -0700679 /* If isolation recently failed, do not retry */
680 if (!isolation_suitable(cc, page))
681 continue;
682
Mel Gormanb35e2d72012-10-08 16:32:36 -0700683 /* Found a block suitable for isolating free pages from */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100684 isolated = 0;
Mel Gorman812f7a32012-12-06 19:01:14 +0000685
686 /*
687 * As pfn may not start aligned, pfn+pageblock_nr_page
688 * may cross a MAX_ORDER_NR_PAGES boundary and miss
689 * a pfn_valid check. Ensure isolate_freepages_block()
690 * only scans within a pageblock
691 */
692 end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
693 end_pfn = min(end_pfn, zone_end_pfn);
Mel Gormanb35e2d72012-10-08 16:32:36 -0700694 isolated = isolate_freepages_block(cc, pfn, end_pfn,
695 freelist, false);
696 nr_freepages += isolated;
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100697
698 /*
699 * Record the highest PFN we isolated pages from. When next
700 * looking for free pages, the search will restart here as
701 * page migration may have returned some pages to the allocator
702 */
Mel Gormand58f1f22012-10-08 16:32:45 -0700703 if (isolated) {
704 cc->finished_update_free = true;
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100705 high_pfn = max(high_pfn, pfn);
Mel Gormand58f1f22012-10-08 16:32:45 -0700706 }
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100707 }
708
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100709 /* split_free_page does not map the pages */
710 map_pages(freelist);
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100711
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100712 cc->free_pfn = high_pfn;
713 cc->nr_freepages = nr_freepages;
Mel Gorman748446b2010-05-24 14:32:27 -0700714}
715
716/*
717 * This is a migrate-callback that "allocates" freepages by taking pages
718 * from the isolated freelists in the block we are migrating to.
719 */
720static struct page *compaction_alloc(struct page *migratepage,
721 unsigned long data,
722 int **result)
723{
724 struct compact_control *cc = (struct compact_control *)data;
725 struct page *freepage;
726
727 /* Isolate free pages if necessary */
728 if (list_empty(&cc->freepages)) {
729 isolate_freepages(cc->zone, cc);
730
731 if (list_empty(&cc->freepages))
732 return NULL;
733 }
734
735 freepage = list_entry(cc->freepages.next, struct page, lru);
736 list_del(&freepage->lru);
737 cc->nr_freepages--;
738
739 return freepage;
740}
741
742/*
743 * We cannot control nr_migratepages and nr_freepages fully when migration is
744 * running as migrate_pages() has no knowledge of compact_control. When
745 * migration is complete, we count the number of pages on the lists by hand.
746 */
747static void update_nr_listpages(struct compact_control *cc)
748{
749 int nr_migratepages = 0;
750 int nr_freepages = 0;
751 struct page *page;
752
753 list_for_each_entry(page, &cc->migratepages, lru)
754 nr_migratepages++;
755 list_for_each_entry(page, &cc->freepages, lru)
756 nr_freepages++;
757
758 cc->nr_migratepages = nr_migratepages;
759 cc->nr_freepages = nr_freepages;
760}
761
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100762/* possible outcome of isolate_migratepages */
763typedef enum {
764 ISOLATE_ABORT, /* Abort compaction now */
765 ISOLATE_NONE, /* No pages isolated, continue scanning */
766 ISOLATE_SUCCESS, /* Pages isolated, migrate */
767} isolate_migrate_t;
768
769/*
770 * Isolate all pages that can be migrated from the block pointed to by
771 * the migrate scanner within compact_control.
772 */
773static isolate_migrate_t isolate_migratepages(struct zone *zone,
774 struct compact_control *cc)
775{
776 unsigned long low_pfn, end_pfn;
777
778 /* Do not scan outside zone boundaries */
779 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
780
781 /* Only scan within a pageblock boundary */
782 end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
783
784 /* Do not cross the free scanner or scan within a memory hole */
785 if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
786 cc->migrate_pfn = end_pfn;
787 return ISOLATE_NONE;
788 }
789
790 /* Perform the isolation */
Minchan Kimd05b4522012-10-08 16:33:48 -0700791 low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn, false);
Shaohua Li3cbf3512012-10-08 16:32:27 -0700792 if (!low_pfn || cc->contended)
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100793 return ISOLATE_ABORT;
794
795 cc->migrate_pfn = low_pfn;
796
797 return ISOLATE_SUCCESS;
798}
799
Mel Gorman748446b2010-05-24 14:32:27 -0700800static int compact_finished(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800801 struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700802{
Mel Gorman98149402013-01-11 14:32:16 -0800803 unsigned int order;
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800804 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -0700805
Mel Gorman748446b2010-05-24 14:32:27 -0700806 if (fatal_signal_pending(current))
807 return COMPACT_PARTIAL;
808
809 /* Compaction run completes if the migrate and free scanner meet */
Mel Gorman6b506642012-10-08 16:32:41 -0700810 if (cc->free_pfn <= cc->migrate_pfn) {
Mel Gorman9317a2b2012-10-08 16:32:47 -0700811 /*
812 * Mark that the PG_migrate_skip information should be cleared
813 * by kswapd when it goes to sleep. kswapd does not set the
814 * flag itself as the decision to be clear should be directly
815 * based on an allocation request.
816 */
817 if (!current_is_kswapd())
818 zone->compact_blockskip_flush = true;
819
Mel Gorman748446b2010-05-24 14:32:27 -0700820 return COMPACT_COMPLETE;
Mel Gorman6b506642012-10-08 16:32:41 -0700821 }
Mel Gorman748446b2010-05-24 14:32:27 -0700822
Johannes Weiner82478fb2011-01-20 14:44:21 -0800823 /*
824 * order == -1 is expected when compacting via
825 * /proc/sys/vm/compact_memory
826 */
Mel Gorman56de7262010-05-24 14:32:30 -0700827 if (cc->order == -1)
828 return COMPACT_CONTINUE;
829
Michal Hocko3957c772011-06-15 15:08:25 -0700830 /* Compaction run is not finished if the watermark is not met */
831 watermark = low_wmark_pages(zone);
832 watermark += (1 << cc->order);
833
834 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
835 return COMPACT_CONTINUE;
836
Mel Gorman56de7262010-05-24 14:32:30 -0700837 /* Direct compactor: Is a suitable page free? */
Mel Gorman98149402013-01-11 14:32:16 -0800838 for (order = cc->order; order < MAX_ORDER; order++) {
839 struct free_area *area = &zone->free_area[order];
Mel Gorman56de7262010-05-24 14:32:30 -0700840
Mel Gorman98149402013-01-11 14:32:16 -0800841 /* Job done if page is free of the right migratetype */
842 if (!list_empty(&area->free_list[cc->migratetype]))
843 return COMPACT_PARTIAL;
844
845 /* Job done if allocation would set block type */
846 if (cc->order >= pageblock_order && area->nr_free)
847 return COMPACT_PARTIAL;
Mel Gorman56de7262010-05-24 14:32:30 -0700848 }
849
Mel Gorman748446b2010-05-24 14:32:27 -0700850 return COMPACT_CONTINUE;
851}
852
Mel Gorman3e7d3442011-01-13 15:45:56 -0800853/*
854 * compaction_suitable: Is this suitable to run compaction on this zone now?
855 * Returns
856 * COMPACT_SKIPPED - If there are too few free pages for compaction
857 * COMPACT_PARTIAL - If the allocation would succeed without compaction
858 * COMPACT_CONTINUE - If compaction should run now
859 */
860unsigned long compaction_suitable(struct zone *zone, int order)
861{
862 int fragindex;
863 unsigned long watermark;
864
865 /*
Michal Hocko3957c772011-06-15 15:08:25 -0700866 * order == -1 is expected when compacting via
867 * /proc/sys/vm/compact_memory
868 */
869 if (order == -1)
870 return COMPACT_CONTINUE;
871
872 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -0800873 * Watermarks for order-0 must be met for compaction. Note the 2UL.
874 * This is because during migration, copies of pages need to be
875 * allocated and for a short time, the footprint is higher
876 */
877 watermark = low_wmark_pages(zone) + (2UL << order);
878 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
879 return COMPACT_SKIPPED;
880
881 /*
882 * fragmentation index determines if allocation failures are due to
883 * low memory or external fragmentation
884 *
Shaohua Lia582a732011-06-15 15:08:49 -0700885 * index of -1000 implies allocations might succeed depending on
886 * watermarks
Mel Gorman3e7d3442011-01-13 15:45:56 -0800887 * index towards 0 implies failure is due to lack of memory
888 * index towards 1000 implies failure is due to fragmentation
889 *
890 * Only compact if a failure would be due to fragmentation.
891 */
892 fragindex = fragmentation_index(zone, order);
893 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
894 return COMPACT_SKIPPED;
895
Shaohua Lia582a732011-06-15 15:08:49 -0700896 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
897 0, 0))
Mel Gorman3e7d3442011-01-13 15:45:56 -0800898 return COMPACT_PARTIAL;
899
900 return COMPACT_CONTINUE;
901}
902
Mel Gorman748446b2010-05-24 14:32:27 -0700903static int compact_zone(struct zone *zone, struct compact_control *cc)
904{
905 int ret;
Mel Gormand58f1f22012-10-08 16:32:45 -0700906 unsigned long start_pfn = zone->zone_start_pfn;
907 unsigned long end_pfn = zone->zone_start_pfn + zone->spanned_pages;
Mel Gorman748446b2010-05-24 14:32:27 -0700908
Mel Gorman3e7d3442011-01-13 15:45:56 -0800909 ret = compaction_suitable(zone, cc->order);
910 switch (ret) {
911 case COMPACT_PARTIAL:
912 case COMPACT_SKIPPED:
913 /* Compaction is likely to fail */
914 return ret;
915 case COMPACT_CONTINUE:
916 /* Fall through to compaction */
917 ;
918 }
919
Mel Gormand58f1f22012-10-08 16:32:45 -0700920 /*
921 * Setup to move all movable pages to the end of the zone. Used cached
922 * information on where the scanners should start but check that it
923 * is initialised by ensuring the values are within zone boundaries.
924 */
925 cc->migrate_pfn = zone->compact_cached_migrate_pfn;
926 cc->free_pfn = zone->compact_cached_free_pfn;
927 if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
928 cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
929 zone->compact_cached_free_pfn = cc->free_pfn;
930 }
931 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
932 cc->migrate_pfn = start_pfn;
933 zone->compact_cached_migrate_pfn = cc->migrate_pfn;
934 }
Mel Gorman748446b2010-05-24 14:32:27 -0700935
Mel Gorman9317a2b2012-10-08 16:32:47 -0700936 /*
937 * Clear pageblock skip if there were failures recently and compaction
938 * is about to be retried after being deferred. kswapd does not do
939 * this reset as it'll reset the cached information when going to sleep.
940 */
941 if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
942 __reset_isolation_suitable(zone);
Mel Gorman6b506642012-10-08 16:32:41 -0700943
Mel Gorman748446b2010-05-24 14:32:27 -0700944 migrate_prep_local();
945
946 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
947 unsigned long nr_migrate, nr_remaining;
Minchan Kim9d502c12011-03-22 16:30:39 -0700948 int err;
Mel Gorman748446b2010-05-24 14:32:27 -0700949
Mel Gormanf9e35b32011-06-15 15:08:52 -0700950 switch (isolate_migratepages(zone, cc)) {
951 case ISOLATE_ABORT:
952 ret = COMPACT_PARTIAL;
Shaohua Li3cbf3512012-10-08 16:32:27 -0700953 putback_lru_pages(&cc->migratepages);
954 cc->nr_migratepages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700955 goto out;
956 case ISOLATE_NONE:
Mel Gorman748446b2010-05-24 14:32:27 -0700957 continue;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700958 case ISOLATE_SUCCESS:
959 ;
960 }
Mel Gorman748446b2010-05-24 14:32:27 -0700961
962 nr_migrate = cc->nr_migratepages;
Minchan Kim9d502c12011-03-22 16:30:39 -0700963 err = migrate_pages(&cc->migratepages, compaction_alloc,
Mel Gorman7f0f2492011-01-13 15:45:58 -0800964 (unsigned long)cc, false,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800965 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
Mel Gorman748446b2010-05-24 14:32:27 -0700966 update_nr_listpages(cc);
967 nr_remaining = cc->nr_migratepages;
968
Mel Gormanb7aba692011-01-13 15:45:54 -0800969 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
970 nr_remaining);
Mel Gorman748446b2010-05-24 14:32:27 -0700971
972 /* Release LRU pages not migrated */
Minchan Kim9d502c12011-03-22 16:30:39 -0700973 if (err) {
Mel Gorman748446b2010-05-24 14:32:27 -0700974 putback_lru_pages(&cc->migratepages);
975 cc->nr_migratepages = 0;
David Rientjesa56063e2012-07-11 14:02:13 -0700976 if (err == -ENOMEM) {
977 ret = COMPACT_PARTIAL;
978 goto out;
979 }
Mel Gorman748446b2010-05-24 14:32:27 -0700980 }
Mel Gorman748446b2010-05-24 14:32:27 -0700981 }
982
Mel Gormanf9e35b32011-06-15 15:08:52 -0700983out:
Mel Gorman748446b2010-05-24 14:32:27 -0700984 /* Release free pages and check accounting */
985 cc->nr_freepages -= release_freepages(&cc->freepages);
986 VM_BUG_ON(cc->nr_freepages != 0);
987
988 return ret;
989}
Mel Gorman76ab0f52010-05-24 14:32:28 -0700990
Kyungmin Parkd43a87e2011-10-31 17:09:08 -0700991static unsigned long compact_zone_order(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800992 int order, gfp_t gfp_mask,
Mel Gorman98149402013-01-11 14:32:16 -0800993 bool sync, bool *contended)
Mel Gorman56de7262010-05-24 14:32:30 -0700994{
Shaohua Li3cbf3512012-10-08 16:32:27 -0700995 unsigned long ret;
Mel Gorman56de7262010-05-24 14:32:30 -0700996 struct compact_control cc = {
997 .nr_freepages = 0,
998 .nr_migratepages = 0,
999 .order = order,
1000 .migratetype = allocflags_to_migratetype(gfp_mask),
1001 .zone = zone,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001002 .sync = sync,
Mel Gorman56de7262010-05-24 14:32:30 -07001003 };
1004 INIT_LIST_HEAD(&cc.freepages);
1005 INIT_LIST_HEAD(&cc.migratepages);
1006
Shaohua Li3cbf3512012-10-08 16:32:27 -07001007 ret = compact_zone(zone, &cc);
1008
1009 VM_BUG_ON(!list_empty(&cc.freepages));
1010 VM_BUG_ON(!list_empty(&cc.migratepages));
1011
1012 *contended = cc.contended;
1013 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001014}
1015
Mel Gorman5e771902010-05-24 14:32:31 -07001016int sysctl_extfrag_threshold = 500;
1017
Mel Gorman56de7262010-05-24 14:32:30 -07001018/**
1019 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
1020 * @zonelist: The zonelist used for the current allocation
1021 * @order: The order of the current allocation
1022 * @gfp_mask: The GFP mask of the current allocation
1023 * @nodemask: The allowed nodes to allocate from
Mel Gorman77f1fe62011-01-13 15:45:57 -08001024 * @sync: Whether migration is synchronous or not
Mel Gorman5e663dc2012-10-08 16:32:31 -07001025 * @contended: Return value that is true if compaction was aborted due to lock contention
1026 * @page: Optionally capture a free page of the requested order during compaction
Mel Gorman56de7262010-05-24 14:32:30 -07001027 *
1028 * This is the main entry point for direct page compaction.
1029 */
1030unsigned long try_to_compact_pages(struct zonelist *zonelist,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001031 int order, gfp_t gfp_mask, nodemask_t *nodemask,
Mel Gorman98149402013-01-11 14:32:16 -08001032 bool sync, bool *contended)
Mel Gorman56de7262010-05-24 14:32:30 -07001033{
1034 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
1035 int may_enter_fs = gfp_mask & __GFP_FS;
1036 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -07001037 struct zoneref *z;
1038 struct zone *zone;
1039 int rc = COMPACT_SKIPPED;
Bartlomiej Zolnierkiewicz19644312012-10-08 16:32:05 -07001040 int alloc_flags = 0;
Mel Gorman56de7262010-05-24 14:32:30 -07001041
Mel Gorman05fd3fd2012-10-08 16:29:09 -07001042 /* Check if the GFP flags allow compaction */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -08001043 if (!order || !may_enter_fs || !may_perform_io)
Mel Gorman56de7262010-05-24 14:32:30 -07001044 return rc;
1045
Minchan Kimab211022012-12-20 15:05:06 -08001046 count_compact_event(COMPACTSTALL);
Mel Gorman56de7262010-05-24 14:32:30 -07001047
Bartlomiej Zolnierkiewicz19644312012-10-08 16:32:05 -07001048#ifdef CONFIG_CMA
1049 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
1050 alloc_flags |= ALLOC_CMA;
1051#endif
Mel Gorman56de7262010-05-24 14:32:30 -07001052 /* Compact each zone in the list */
1053 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
1054 nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -07001055 int status;
1056
Mel Gorman27d1af82012-08-21 16:16:17 -07001057 status = compact_zone_order(zone, order, gfp_mask, sync,
Mel Gorman98149402013-01-11 14:32:16 -08001058 contended);
Mel Gorman56de7262010-05-24 14:32:30 -07001059 rc = max(status, rc);
1060
Mel Gorman3e7d3442011-01-13 15:45:56 -08001061 /* If a normal allocation would succeed, stop compacting */
Bartlomiej Zolnierkiewicz19644312012-10-08 16:32:05 -07001062 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
1063 alloc_flags))
Mel Gorman56de7262010-05-24 14:32:30 -07001064 break;
1065 }
1066
1067 return rc;
1068}
1069
1070
Mel Gorman76ab0f52010-05-24 14:32:28 -07001071/* Compact all zones within a node */
Rik van Riel7be62de2012-03-21 16:33:52 -07001072static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001073{
1074 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -07001075 struct zone *zone;
1076
Mel Gorman76ab0f52010-05-24 14:32:28 -07001077 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -07001078
1079 zone = &pgdat->node_zones[zoneid];
1080 if (!populated_zone(zone))
1081 continue;
1082
Rik van Riel7be62de2012-03-21 16:33:52 -07001083 cc->nr_freepages = 0;
1084 cc->nr_migratepages = 0;
1085 cc->zone = zone;
1086 INIT_LIST_HEAD(&cc->freepages);
1087 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001088
Dan Carpenteraad6ec32012-03-21 16:33:54 -07001089 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -07001090 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001091
Rik van Rielaff62242012-03-21 16:33:52 -07001092 if (cc->order > 0) {
1093 int ok = zone_watermark_ok(zone, cc->order,
1094 low_wmark_pages(zone), 0, 0);
Minchan Kimfc83d282012-08-21 16:16:03 -07001095 if (ok && cc->order >= zone->compact_order_failed)
Rik van Rielaff62242012-03-21 16:33:52 -07001096 zone->compact_order_failed = cc->order + 1;
1097 /* Currently async compaction is never deferred. */
1098 else if (!ok && cc->sync)
1099 defer_compaction(zone, cc->order);
1100 }
1101
Rik van Riel7be62de2012-03-21 16:33:52 -07001102 VM_BUG_ON(!list_empty(&cc->freepages));
1103 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -07001104 }
1105
1106 return 0;
1107}
1108
Rik van Riel7be62de2012-03-21 16:33:52 -07001109int compact_pgdat(pg_data_t *pgdat, int order)
1110{
1111 struct compact_control cc = {
1112 .order = order,
1113 .sync = false,
1114 };
1115
1116 return __compact_pgdat(pgdat, &cc);
1117}
1118
1119static int compact_node(int nid)
1120{
Rik van Riel7be62de2012-03-21 16:33:52 -07001121 struct compact_control cc = {
1122 .order = -1,
1123 .sync = true,
1124 };
1125
Hugh Dickins8575ec22012-03-21 16:33:53 -07001126 return __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001127}
1128
Mel Gorman76ab0f52010-05-24 14:32:28 -07001129/* Compact all nodes in the system */
Jason Liu7cca82b2013-01-11 14:31:47 -08001130static void compact_nodes(void)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001131{
1132 int nid;
1133
Hugh Dickins8575ec22012-03-21 16:33:53 -07001134 /* Flush pending updates to the LRU lists */
1135 lru_add_drain_all();
1136
Mel Gorman76ab0f52010-05-24 14:32:28 -07001137 for_each_online_node(nid)
1138 compact_node(nid);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001139}
1140
1141/* The written value is actually unused, all memory is compacted */
1142int sysctl_compact_memory;
1143
1144/* This is the entry point for compacting all nodes via /proc/sys/vm */
1145int sysctl_compaction_handler(struct ctl_table *table, int write,
1146 void __user *buffer, size_t *length, loff_t *ppos)
1147{
1148 if (write)
Jason Liu7cca82b2013-01-11 14:31:47 -08001149 compact_nodes();
Mel Gorman76ab0f52010-05-24 14:32:28 -07001150
1151 return 0;
1152}
Mel Gormaned4a6d72010-05-24 14:32:29 -07001153
Mel Gorman5e771902010-05-24 14:32:31 -07001154int sysctl_extfrag_handler(struct ctl_table *table, int write,
1155 void __user *buffer, size_t *length, loff_t *ppos)
1156{
1157 proc_dointvec_minmax(table, write, buffer, length, ppos);
1158
1159 return 0;
1160}
1161
Mel Gormaned4a6d72010-05-24 14:32:29 -07001162#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Kay Sievers10fbcf42011-12-21 14:48:43 -08001163ssize_t sysfs_compact_node(struct device *dev,
1164 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -07001165 const char *buf, size_t count)
1166{
Hugh Dickins8575ec22012-03-21 16:33:53 -07001167 int nid = dev->id;
1168
1169 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1170 /* Flush pending updates to the LRU lists */
1171 lru_add_drain_all();
1172
1173 compact_node(nid);
1174 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07001175
1176 return count;
1177}
Kay Sievers10fbcf42011-12-21 14:48:43 -08001178static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001179
1180int compaction_register_node(struct node *node)
1181{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001182 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001183}
1184
1185void compaction_unregister_node(struct node *node)
1186{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001187 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001188}
1189#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +01001190
1191#endif /* CONFIG_COMPACTION */