blob: d24dd2d7bad407140c4b0e6fe514a0cb187bbcc8 [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>
Rafael Aquinibf6bddf2012-12-11 16:02:42 -080017#include <linux/balloon_compaction.h>
Mel Gorman748446b2010-05-24 14:32:27 -070018#include "internal.h"
19
Michal Nazarewiczff9543f2011-12-29 13:09:50 +010020#if defined CONFIG_COMPACTION || defined CONFIG_CMA
21
Mel Gormanb7aba692011-01-13 15:45:54 -080022#define CREATE_TRACE_POINTS
23#include <trace/events/compaction.h>
24
Mel Gorman748446b2010-05-24 14:32:27 -070025static unsigned long release_freepages(struct list_head *freelist)
26{
27 struct page *page, *next;
28 unsigned long count = 0;
29
30 list_for_each_entry_safe(page, next, freelist, lru) {
31 list_del(&page->lru);
32 __free_page(page);
33 count++;
34 }
35
36 return count;
37}
38
Michal Nazarewiczff9543f2011-12-29 13:09:50 +010039static void map_pages(struct list_head *list)
40{
41 struct page *page;
42
43 list_for_each_entry(page, list, lru) {
44 arch_alloc_page(page, 0);
45 kernel_map_pages(page, 1, 1);
46 }
47}
48
Michal Nazarewicz47118af2011-12-29 13:09:50 +010049static inline bool migrate_async_suitable(int migratetype)
50{
51 return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
52}
53
Mel Gormanbb13ffe2012-10-08 16:32:41 -070054#ifdef CONFIG_COMPACTION
55/* Returns true if the pageblock should be scanned for pages to isolate. */
56static inline bool isolation_suitable(struct compact_control *cc,
57 struct page *page)
58{
59 if (cc->ignore_skip_hint)
60 return true;
61
62 return !get_pageblock_skip(page);
63}
64
65/*
66 * This function is called to clear all cached information on pageblocks that
67 * should be skipped for page isolation when the migrate and free page scanner
68 * meet.
69 */
Mel Gorman62997022012-10-08 16:32:47 -070070static void __reset_isolation_suitable(struct zone *zone)
Mel Gormanbb13ffe2012-10-08 16:32:41 -070071{
72 unsigned long start_pfn = zone->zone_start_pfn;
73 unsigned long end_pfn = zone->zone_start_pfn + zone->spanned_pages;
74 unsigned long pfn;
75
Mel Gormanc89511a2012-10-08 16:32:45 -070076 zone->compact_cached_migrate_pfn = start_pfn;
77 zone->compact_cached_free_pfn = end_pfn;
Mel Gorman62997022012-10-08 16:32:47 -070078 zone->compact_blockskip_flush = false;
Mel Gormanbb13ffe2012-10-08 16:32:41 -070079
80 /* Walk the zone and mark every pageblock as suitable for isolation */
81 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
82 struct page *page;
83
84 cond_resched();
85
86 if (!pfn_valid(pfn))
87 continue;
88
89 page = pfn_to_page(pfn);
90 if (zone != page_zone(page))
91 continue;
92
93 clear_pageblock_skip(page);
94 }
95}
96
Mel Gorman62997022012-10-08 16:32:47 -070097void reset_isolation_suitable(pg_data_t *pgdat)
98{
99 int zoneid;
100
101 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
102 struct zone *zone = &pgdat->node_zones[zoneid];
103 if (!populated_zone(zone))
104 continue;
105
106 /* Only flush if a full compaction finished recently */
107 if (zone->compact_blockskip_flush)
108 __reset_isolation_suitable(zone);
109 }
110}
111
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700112/*
113 * If no pages were isolated then mark this pageblock to be skipped in the
Mel Gorman62997022012-10-08 16:32:47 -0700114 * future. The information is later cleared by __reset_isolation_suitable().
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700115 */
Mel Gormanc89511a2012-10-08 16:32:45 -0700116static void update_pageblock_skip(struct compact_control *cc,
117 struct page *page, unsigned long nr_isolated,
118 bool migrate_scanner)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700119{
Mel Gormanc89511a2012-10-08 16:32:45 -0700120 struct zone *zone = cc->zone;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700121 if (!page)
122 return;
123
Mel Gormanc89511a2012-10-08 16:32:45 -0700124 if (!nr_isolated) {
125 unsigned long pfn = page_to_pfn(page);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700126 set_pageblock_skip(page);
Mel Gormanc89511a2012-10-08 16:32:45 -0700127
128 /* Update where compaction should restart */
129 if (migrate_scanner) {
130 if (!cc->finished_update_migrate &&
131 pfn > zone->compact_cached_migrate_pfn)
132 zone->compact_cached_migrate_pfn = pfn;
133 } else {
134 if (!cc->finished_update_free &&
135 pfn < zone->compact_cached_free_pfn)
136 zone->compact_cached_free_pfn = pfn;
137 }
138 }
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700139}
140#else
141static inline bool isolation_suitable(struct compact_control *cc,
142 struct page *page)
143{
144 return true;
145}
146
Mel Gormanc89511a2012-10-08 16:32:45 -0700147static void update_pageblock_skip(struct compact_control *cc,
148 struct page *page, unsigned long nr_isolated,
149 bool migrate_scanner)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700150{
151}
152#endif /* CONFIG_COMPACTION */
153
Mel Gorman2a1402a2012-10-08 16:32:33 -0700154static inline bool should_release_lock(spinlock_t *lock)
155{
156 return need_resched() || spin_is_contended(lock);
157}
158
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100159/*
Mel Gormanc67fe372012-08-21 16:16:17 -0700160 * Compaction requires the taking of some coarse locks that are potentially
161 * very heavily contended. Check if the process needs to be scheduled or
162 * if the lock is contended. For async compaction, back out in the event
163 * if contention is severe. For sync compaction, schedule.
164 *
165 * Returns true if the lock is held.
166 * Returns false if the lock is released and compaction should abort
167 */
168static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags,
169 bool locked, struct compact_control *cc)
170{
Mel Gorman2a1402a2012-10-08 16:32:33 -0700171 if (should_release_lock(lock)) {
Mel Gormanc67fe372012-08-21 16:16:17 -0700172 if (locked) {
173 spin_unlock_irqrestore(lock, *flags);
174 locked = false;
175 }
176
177 /* async aborts if taking too long or contended */
178 if (!cc->sync) {
Shaohua Lie64c5232012-10-08 16:32:27 -0700179 cc->contended = true;
Mel Gormanc67fe372012-08-21 16:16:17 -0700180 return false;
181 }
182
183 cond_resched();
Mel Gormanc67fe372012-08-21 16:16:17 -0700184 }
185
186 if (!locked)
187 spin_lock_irqsave(lock, *flags);
188 return true;
189}
190
191static inline bool compact_trylock_irqsave(spinlock_t *lock,
192 unsigned long *flags, struct compact_control *cc)
193{
194 return compact_checklock_irqsave(lock, flags, false, cc);
195}
196
Mel Gormanf40d1e42012-10-08 16:32:36 -0700197/* Returns true if the page is within a block suitable for migration to */
198static bool suitable_migration_target(struct page *page)
199{
200 int migratetype = get_pageblock_migratetype(page);
201
202 /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
203 if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
204 return false;
205
206 /* If the page is a large free page, then allow migration */
207 if (PageBuddy(page) && page_order(page) >= pageblock_order)
208 return true;
209
210 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
211 if (migrate_async_suitable(migratetype))
212 return true;
213
214 /* Otherwise skip the block */
215 return false;
216}
217
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700218static void compact_capture_page(struct compact_control *cc)
219{
220 unsigned long flags;
221 int mtype, mtype_low, mtype_high;
222
223 if (!cc->page || *cc->page)
224 return;
225
226 /*
227 * For MIGRATE_MOVABLE allocations we capture a suitable page ASAP
228 * regardless of the migratetype of the freelist is is captured from.
229 * This is fine because the order for a high-order MIGRATE_MOVABLE
230 * allocation is typically at least a pageblock size and overall
231 * fragmentation is not impaired. Other allocation types must
232 * capture pages from their own migratelist because otherwise they
233 * could pollute other pageblocks like MIGRATE_MOVABLE with
234 * difficult to move pages and making fragmentation worse overall.
235 */
236 if (cc->migratetype == MIGRATE_MOVABLE) {
237 mtype_low = 0;
238 mtype_high = MIGRATE_PCPTYPES;
239 } else {
240 mtype_low = cc->migratetype;
241 mtype_high = cc->migratetype + 1;
242 }
243
244 /* Speculatively examine the free lists without zone lock */
245 for (mtype = mtype_low; mtype < mtype_high; mtype++) {
246 int order;
247 for (order = cc->order; order < MAX_ORDER; order++) {
248 struct page *page;
249 struct free_area *area;
250 area = &(cc->zone->free_area[order]);
251 if (list_empty(&area->free_list[mtype]))
252 continue;
253
254 /* Take the lock and attempt capture of the page */
255 if (!compact_trylock_irqsave(&cc->zone->lock, &flags, cc))
256 return;
257 if (!list_empty(&area->free_list[mtype])) {
258 page = list_entry(area->free_list[mtype].next,
259 struct page, lru);
260 if (capture_free_page(page, cc->order, mtype)) {
261 spin_unlock_irqrestore(&cc->zone->lock,
262 flags);
263 *cc->page = page;
264 return;
265 }
266 }
267 spin_unlock_irqrestore(&cc->zone->lock, flags);
268 }
269 }
270}
271
Mel Gormanc67fe372012-08-21 16:16:17 -0700272/*
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100273 * Isolate free pages onto a private freelist. Caller must hold zone->lock.
274 * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
275 * pages inside of the pageblock (even though it may still end up isolating
276 * some pages).
277 */
Mel Gormanf40d1e42012-10-08 16:32:36 -0700278static unsigned long isolate_freepages_block(struct compact_control *cc,
279 unsigned long blockpfn,
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100280 unsigned long end_pfn,
281 struct list_head *freelist,
282 bool strict)
Mel Gorman748446b2010-05-24 14:32:27 -0700283{
Mel Gormanb7aba692011-01-13 15:45:54 -0800284 int nr_scanned = 0, total_isolated = 0;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700285 struct page *cursor, *valid_page = NULL;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700286 unsigned long nr_strict_required = end_pfn - blockpfn;
287 unsigned long flags;
288 bool locked = false;
Mel Gorman748446b2010-05-24 14:32:27 -0700289
Mel Gorman748446b2010-05-24 14:32:27 -0700290 cursor = pfn_to_page(blockpfn);
291
Mel Gormanf40d1e42012-10-08 16:32:36 -0700292 /* Isolate free pages. */
Mel Gorman748446b2010-05-24 14:32:27 -0700293 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
294 int isolated, i;
295 struct page *page = cursor;
296
Mel Gormanb7aba692011-01-13 15:45:54 -0800297 nr_scanned++;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700298 if (!pfn_valid_within(blockpfn))
Mel Gorman748446b2010-05-24 14:32:27 -0700299 continue;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700300 if (!valid_page)
301 valid_page = page;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700302 if (!PageBuddy(page))
303 continue;
304
305 /*
306 * The zone lock must be held to isolate freepages.
307 * Unfortunately this is a very coarse lock and can be
308 * heavily contended if there are parallel allocations
309 * or parallel compactions. For async compaction do not
310 * spin on the lock and we acquire the lock as late as
311 * possible.
312 */
313 locked = compact_checklock_irqsave(&cc->zone->lock, &flags,
314 locked, cc);
315 if (!locked)
316 break;
317
318 /* Recheck this is a suitable migration target under lock */
319 if (!strict && !suitable_migration_target(page))
320 break;
321
322 /* Recheck this is a buddy page under lock */
323 if (!PageBuddy(page))
324 continue;
Mel Gorman748446b2010-05-24 14:32:27 -0700325
326 /* Found a free page, break it into order-0 pages */
327 isolated = split_free_page(page);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100328 if (!isolated && strict)
Mel Gormanf40d1e42012-10-08 16:32:36 -0700329 break;
Mel Gorman748446b2010-05-24 14:32:27 -0700330 total_isolated += isolated;
331 for (i = 0; i < isolated; i++) {
332 list_add(&page->lru, freelist);
333 page++;
334 }
335
336 /* If a page was split, advance to the end of it */
337 if (isolated) {
338 blockpfn += isolated - 1;
339 cursor += isolated - 1;
340 }
341 }
342
Mel Gormanb7aba692011-01-13 15:45:54 -0800343 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
Mel Gormanf40d1e42012-10-08 16:32:36 -0700344
345 /*
346 * If strict isolation is requested by CMA then check that all the
347 * pages requested were isolated. If there were any failures, 0 is
348 * returned and CMA will fail.
349 */
Mel Gorman0db63d72012-10-19 13:56:57 -0700350 if (strict && nr_strict_required > total_isolated)
Mel Gormanf40d1e42012-10-08 16:32:36 -0700351 total_isolated = 0;
352
353 if (locked)
354 spin_unlock_irqrestore(&cc->zone->lock, flags);
355
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700356 /* Update the pageblock-skip if the whole pageblock was scanned */
357 if (blockpfn == end_pfn)
Mel Gormanc89511a2012-10-08 16:32:45 -0700358 update_pageblock_skip(cc, valid_page, total_isolated, false);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700359
Mel Gorman748446b2010-05-24 14:32:27 -0700360 return total_isolated;
361}
362
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100363/**
364 * isolate_freepages_range() - isolate free pages.
365 * @start_pfn: The first PFN to start isolating.
366 * @end_pfn: The one-past-last PFN.
367 *
368 * Non-free pages, invalid PFNs, or zone boundaries within the
369 * [start_pfn, end_pfn) range are considered errors, cause function to
370 * undo its actions and return zero.
371 *
372 * Otherwise, function returns one-past-the-last PFN of isolated page
373 * (which may be greater then end_pfn if end fell in a middle of
374 * a free page).
375 */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100376unsigned long
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700377isolate_freepages_range(struct compact_control *cc,
378 unsigned long start_pfn, unsigned long end_pfn)
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100379{
Mel Gormanf40d1e42012-10-08 16:32:36 -0700380 unsigned long isolated, pfn, block_end_pfn;
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100381 LIST_HEAD(freelist);
382
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100383 for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700384 if (!pfn_valid(pfn) || cc->zone != page_zone(pfn_to_page(pfn)))
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100385 break;
386
387 /*
388 * On subsequent iterations ALIGN() is actually not needed,
389 * but we keep it that we not to complicate the code.
390 */
391 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
392 block_end_pfn = min(block_end_pfn, end_pfn);
393
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700394 isolated = isolate_freepages_block(cc, pfn, block_end_pfn,
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100395 &freelist, true);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100396
397 /*
398 * In strict mode, isolate_freepages_block() returns 0 if
399 * there are any holes in the block (ie. invalid PFNs or
400 * non-free pages).
401 */
402 if (!isolated)
403 break;
404
405 /*
406 * If we managed to isolate pages, it is always (1 << n) *
407 * pageblock_nr_pages for some non-negative n. (Max order
408 * page may span two pageblocks).
409 */
410 }
411
412 /* split_free_page does not map the pages */
413 map_pages(&freelist);
414
415 if (pfn < end_pfn) {
416 /* Loop terminated early, cleanup. */
417 release_freepages(&freelist);
418 return 0;
419 }
420
421 /* We don't use freelists for anything. */
422 return pfn;
423}
424
Mel Gorman748446b2010-05-24 14:32:27 -0700425/* Update the number of anon and file isolated pages in the zone */
Mel Gormanc67fe372012-08-21 16:16:17 -0700426static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700427{
428 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700429 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700430
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700431 list_for_each_entry(page, &cc->migratepages, lru)
432 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700433
Mel Gormanc67fe372012-08-21 16:16:17 -0700434 /* If locked we can use the interrupt unsafe versions */
435 if (locked) {
436 __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
437 __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
438 } else {
439 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
440 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
441 }
Mel Gorman748446b2010-05-24 14:32:27 -0700442}
443
444/* Similar to reclaim, but different enough that they don't share logic */
445static bool too_many_isolated(struct zone *zone)
446{
Minchan Kimbc693042010-09-09 16:38:00 -0700447 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700448
449 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
450 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700451 active = zone_page_state(zone, NR_ACTIVE_FILE) +
452 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700453 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
454 zone_page_state(zone, NR_ISOLATED_ANON);
455
Minchan Kimbc693042010-09-09 16:38:00 -0700456 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700457}
458
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100459/**
460 * isolate_migratepages_range() - isolate all migrate-able pages in range.
461 * @zone: Zone pages are in.
462 * @cc: Compaction control structure.
463 * @low_pfn: The first PFN of the range.
464 * @end_pfn: The one-past-the-last PFN of the range.
Minchan Kime46a2872012-10-08 16:33:48 -0700465 * @unevictable: true if it allows to isolate unevictable pages
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100466 *
467 * Isolate all pages that can be migrated from the range specified by
468 * [low_pfn, end_pfn). Returns zero if there is a fatal signal
469 * pending), otherwise PFN of the first page that was not scanned
470 * (which may be both less, equal to or more then end_pfn).
471 *
472 * Assumes that cc->migratepages is empty and cc->nr_migratepages is
473 * zero.
474 *
475 * Apart from cc->migratepages and cc->nr_migratetypes this function
476 * does not modify any cc's fields, in particular it does not modify
477 * (or read for that matter) cc->migrate_pfn.
Mel Gorman748446b2010-05-24 14:32:27 -0700478 */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100479unsigned long
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100480isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
Minchan Kime46a2872012-10-08 16:33:48 -0700481 unsigned long low_pfn, unsigned long end_pfn, bool unevictable)
Mel Gorman748446b2010-05-24 14:32:27 -0700482{
Mel Gorman9927af742011-01-13 15:45:59 -0800483 unsigned long last_pageblock_nr = 0, pageblock_nr;
Mel Gormanb7aba692011-01-13 15:45:54 -0800484 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700485 struct list_head *migratelist = &cc->migratepages;
Konstantin Khlebnikovf3fd4a62012-05-29 15:06:54 -0700486 isolate_mode_t mode = 0;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700487 struct lruvec *lruvec;
Mel Gormanc67fe372012-08-21 16:16:17 -0700488 unsigned long flags;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700489 bool locked = false;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700490 struct page *page = NULL, *valid_page = NULL;
Mel Gorman748446b2010-05-24 14:32:27 -0700491
Mel Gorman748446b2010-05-24 14:32:27 -0700492 /*
493 * Ensure that there are not too many pages isolated from the LRU
494 * list by either parallel reclaimers or compaction. If there are,
495 * delay for some time until fewer pages are isolated
496 */
497 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700498 /* async migration should just abort */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700499 if (!cc->sync)
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100500 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700501
Mel Gorman748446b2010-05-24 14:32:27 -0700502 congestion_wait(BLK_RW_ASYNC, HZ/10);
503
504 if (fatal_signal_pending(current))
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100505 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700506 }
507
508 /* Time to isolate some pages for migration */
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700509 cond_resched();
Mel Gorman748446b2010-05-24 14:32:27 -0700510 for (; low_pfn < end_pfn; low_pfn++) {
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700511 /* give a chance to irqs before checking need_resched() */
Mel Gorman2a1402a2012-10-08 16:32:33 -0700512 if (locked && !((low_pfn+1) % SWAP_CLUSTER_MAX)) {
513 if (should_release_lock(&zone->lru_lock)) {
514 spin_unlock_irqrestore(&zone->lru_lock, flags);
515 locked = false;
516 }
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700517 }
Mel Gormanc67fe372012-08-21 16:16:17 -0700518
Mel Gorman0bf380b2012-02-03 15:37:18 -0800519 /*
520 * migrate_pfn does not necessarily start aligned to a
521 * pageblock. Ensure that pfn_valid is called when moving
522 * into a new MAX_ORDER_NR_PAGES range in case of large
523 * memory holes within the zone
524 */
525 if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
526 if (!pfn_valid(low_pfn)) {
527 low_pfn += MAX_ORDER_NR_PAGES - 1;
528 continue;
529 }
530 }
531
Mel Gorman748446b2010-05-24 14:32:27 -0700532 if (!pfn_valid_within(low_pfn))
533 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800534 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700535
Mel Gormandc908602012-02-08 17:13:38 -0800536 /*
537 * Get the page and ensure the page is within the same zone.
538 * See the comment in isolate_freepages about overlapping
539 * nodes. It is deliberate that the new zone lock is not taken
540 * as memory compaction should not move pages between nodes.
541 */
Mel Gorman748446b2010-05-24 14:32:27 -0700542 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800543 if (page_zone(page) != zone)
544 continue;
545
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700546 if (!valid_page)
547 valid_page = page;
548
549 /* If isolation recently failed, do not retry */
550 pageblock_nr = low_pfn >> pageblock_order;
551 if (!isolation_suitable(cc, page))
552 goto next_pageblock;
553
Mel Gormandc908602012-02-08 17:13:38 -0800554 /* Skip if free */
Mel Gorman748446b2010-05-24 14:32:27 -0700555 if (PageBuddy(page))
556 continue;
557
Mel Gorman9927af742011-01-13 15:45:59 -0800558 /*
559 * For async migration, also only scan in MOVABLE blocks. Async
560 * migration is optimistic to see if the minimum amount of work
561 * satisfies the allocation
562 */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700563 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
Michal Nazarewicz47118af2011-12-29 13:09:50 +0100564 !migrate_async_suitable(get_pageblock_migratetype(page))) {
Mel Gormanc89511a2012-10-08 16:32:45 -0700565 cc->finished_update_migrate = true;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700566 goto next_pageblock;
Mel Gorman9927af742011-01-13 15:45:59 -0800567 }
568
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800569 /*
570 * Check may be lockless but that's ok as we recheck later.
571 * It's possible to migrate LRU pages and balloon pages
572 * Skip any other type of page
573 */
574 if (!PageLRU(page)) {
575 if (unlikely(balloon_page_movable(page))) {
576 if (locked && balloon_page_isolate(page)) {
577 /* Successfully isolated */
578 cc->finished_update_migrate = true;
579 list_add(&page->lru, migratelist);
580 cc->nr_migratepages++;
581 nr_isolated++;
582 goto check_compact_cluster;
583 }
584 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800585 continue;
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800586 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800587
588 /*
Mel Gorman2a1402a2012-10-08 16:32:33 -0700589 * PageLRU is set. lru_lock normally excludes isolation
590 * splitting and collapsing (collapsing has already happened
591 * if PageLRU is set) but the lock is not necessarily taken
592 * here and it is wasteful to take it just to check transhuge.
593 * Check TransHuge without lock and skip the whole pageblock if
594 * it's either a transhuge or hugetlbfs page, as calling
595 * compound_order() without preventing THP from splitting the
596 * page underneath us may return surprising results.
Andrea Arcangelibc835012011-01-13 15:47:08 -0800597 */
598 if (PageTransHuge(page)) {
Mel Gorman2a1402a2012-10-08 16:32:33 -0700599 if (!locked)
600 goto next_pageblock;
601 low_pfn += (1 << compound_order(page)) - 1;
602 continue;
603 }
604
605 /* Check if it is ok to still hold the lock */
606 locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
607 locked, cc);
608 if (!locked || fatal_signal_pending(current))
609 break;
610
611 /* Recheck PageLRU and PageTransHuge under lock */
612 if (!PageLRU(page))
613 continue;
614 if (PageTransHuge(page)) {
Andrea Arcangelibc835012011-01-13 15:47:08 -0800615 low_pfn += (1 << compound_order(page)) - 1;
616 continue;
617 }
618
Linus Torvalds68e3e922012-06-03 20:05:57 -0700619 if (!cc->sync)
Mel Gormanc8244932012-01-12 17:19:38 -0800620 mode |= ISOLATE_ASYNC_MIGRATE;
621
Minchan Kime46a2872012-10-08 16:33:48 -0700622 if (unevictable)
623 mode |= ISOLATE_UNEVICTABLE;
624
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700625 lruvec = mem_cgroup_page_lruvec(page, zone);
626
Mel Gorman748446b2010-05-24 14:32:27 -0700627 /* Try isolate the page */
Konstantin Khlebnikovf3fd4a62012-05-29 15:06:54 -0700628 if (__isolate_lru_page(page, mode) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700629 continue;
630
Andrea Arcangelibc835012011-01-13 15:47:08 -0800631 VM_BUG_ON(PageTransCompound(page));
632
Mel Gorman748446b2010-05-24 14:32:27 -0700633 /* Successfully isolated */
Mel Gormanc89511a2012-10-08 16:32:45 -0700634 cc->finished_update_migrate = true;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700635 del_page_from_lru_list(page, lruvec, page_lru(page));
Mel Gorman748446b2010-05-24 14:32:27 -0700636 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700637 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800638 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700639
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800640check_compact_cluster:
Mel Gorman748446b2010-05-24 14:32:27 -0700641 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800642 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
643 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700644 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800645 }
Mel Gorman2a1402a2012-10-08 16:32:33 -0700646
647 continue;
648
649next_pageblock:
650 low_pfn += pageblock_nr_pages;
651 low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
652 last_pageblock_nr = pageblock_nr;
Mel Gorman748446b2010-05-24 14:32:27 -0700653 }
654
Mel Gormanc67fe372012-08-21 16:16:17 -0700655 acct_isolated(zone, locked, cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700656
Mel Gormanc67fe372012-08-21 16:16:17 -0700657 if (locked)
658 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700659
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700660 /* Update the pageblock-skip if the whole pageblock was scanned */
661 if (low_pfn == end_pfn)
Mel Gormanc89511a2012-10-08 16:32:45 -0700662 update_pageblock_skip(cc, valid_page, nr_isolated, true);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700663
Mel Gormanb7aba692011-01-13 15:45:54 -0800664 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
665
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100666 return low_pfn;
667}
668
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100669#endif /* CONFIG_COMPACTION || CONFIG_CMA */
670#ifdef CONFIG_COMPACTION
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100671/*
672 * Based on information in the current compact_control, find blocks
673 * suitable for isolating free pages from and then isolate them.
674 */
675static void isolate_freepages(struct zone *zone,
676 struct compact_control *cc)
677{
678 struct page *page;
679 unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100680 int nr_freepages = cc->nr_freepages;
681 struct list_head *freelist = &cc->freepages;
682
683 /*
684 * Initialise the free scanner. The starting point is where we last
685 * scanned from (or the end of the zone if starting). The low point
686 * is the end of the pageblock the migration scanner is using.
687 */
688 pfn = cc->free_pfn;
689 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
690
691 /*
692 * Take care that if the migration scanner is at the end of the zone
693 * that the free scanner does not accidentally move to the next zone
694 * in the next isolation cycle.
695 */
696 high_pfn = min(low_pfn, pfn);
697
698 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
699
700 /*
701 * Isolate free pages until enough are available to migrate the
702 * pages on cc->migratepages. We stop searching if the migrate
703 * and free page scanners meet or enough free pages are isolated.
704 */
705 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
706 pfn -= pageblock_nr_pages) {
707 unsigned long isolated;
708
709 if (!pfn_valid(pfn))
710 continue;
711
712 /*
713 * Check for overlapping nodes/zones. It's possible on some
714 * configurations to have a setup like
715 * node0 node1 node0
716 * i.e. it's possible that all pages within a zones range of
717 * pages do not belong to a single zone.
718 */
719 page = pfn_to_page(pfn);
720 if (page_zone(page) != zone)
721 continue;
722
723 /* Check the block is suitable for migration */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700724 if (!suitable_migration_target(page))
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100725 continue;
Linus Torvalds68e3e922012-06-03 20:05:57 -0700726
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700727 /* If isolation recently failed, do not retry */
728 if (!isolation_suitable(cc, page))
729 continue;
730
Mel Gormanf40d1e42012-10-08 16:32:36 -0700731 /* Found a block suitable for isolating free pages from */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100732 isolated = 0;
Mel Gorman60177d32012-12-06 19:01:14 +0000733
734 /*
735 * As pfn may not start aligned, pfn+pageblock_nr_page
736 * may cross a MAX_ORDER_NR_PAGES boundary and miss
737 * a pfn_valid check. Ensure isolate_freepages_block()
738 * only scans within a pageblock
739 */
740 end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
741 end_pfn = min(end_pfn, zone_end_pfn);
Mel Gormanf40d1e42012-10-08 16:32:36 -0700742 isolated = isolate_freepages_block(cc, pfn, end_pfn,
743 freelist, false);
744 nr_freepages += isolated;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100745
746 /*
747 * Record the highest PFN we isolated pages from. When next
748 * looking for free pages, the search will restart here as
749 * page migration may have returned some pages to the allocator
750 */
Mel Gormanc89511a2012-10-08 16:32:45 -0700751 if (isolated) {
752 cc->finished_update_free = true;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100753 high_pfn = max(high_pfn, pfn);
Mel Gormanc89511a2012-10-08 16:32:45 -0700754 }
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100755 }
756
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100757 /* split_free_page does not map the pages */
758 map_pages(freelist);
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100759
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100760 cc->free_pfn = high_pfn;
761 cc->nr_freepages = nr_freepages;
Mel Gorman748446b2010-05-24 14:32:27 -0700762}
763
764/*
765 * This is a migrate-callback that "allocates" freepages by taking pages
766 * from the isolated freelists in the block we are migrating to.
767 */
768static struct page *compaction_alloc(struct page *migratepage,
769 unsigned long data,
770 int **result)
771{
772 struct compact_control *cc = (struct compact_control *)data;
773 struct page *freepage;
774
775 /* Isolate free pages if necessary */
776 if (list_empty(&cc->freepages)) {
777 isolate_freepages(cc->zone, cc);
778
779 if (list_empty(&cc->freepages))
780 return NULL;
781 }
782
783 freepage = list_entry(cc->freepages.next, struct page, lru);
784 list_del(&freepage->lru);
785 cc->nr_freepages--;
786
787 return freepage;
788}
789
790/*
791 * We cannot control nr_migratepages and nr_freepages fully when migration is
792 * running as migrate_pages() has no knowledge of compact_control. When
793 * migration is complete, we count the number of pages on the lists by hand.
794 */
795static void update_nr_listpages(struct compact_control *cc)
796{
797 int nr_migratepages = 0;
798 int nr_freepages = 0;
799 struct page *page;
800
801 list_for_each_entry(page, &cc->migratepages, lru)
802 nr_migratepages++;
803 list_for_each_entry(page, &cc->freepages, lru)
804 nr_freepages++;
805
806 cc->nr_migratepages = nr_migratepages;
807 cc->nr_freepages = nr_freepages;
808}
809
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100810/* possible outcome of isolate_migratepages */
811typedef enum {
812 ISOLATE_ABORT, /* Abort compaction now */
813 ISOLATE_NONE, /* No pages isolated, continue scanning */
814 ISOLATE_SUCCESS, /* Pages isolated, migrate */
815} isolate_migrate_t;
816
817/*
818 * Isolate all pages that can be migrated from the block pointed to by
819 * the migrate scanner within compact_control.
820 */
821static isolate_migrate_t isolate_migratepages(struct zone *zone,
822 struct compact_control *cc)
823{
824 unsigned long low_pfn, end_pfn;
825
826 /* Do not scan outside zone boundaries */
827 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
828
829 /* Only scan within a pageblock boundary */
830 end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
831
832 /* Do not cross the free scanner or scan within a memory hole */
833 if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
834 cc->migrate_pfn = end_pfn;
835 return ISOLATE_NONE;
836 }
837
838 /* Perform the isolation */
Minchan Kime46a2872012-10-08 16:33:48 -0700839 low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn, false);
Shaohua Lie64c5232012-10-08 16:32:27 -0700840 if (!low_pfn || cc->contended)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100841 return ISOLATE_ABORT;
842
843 cc->migrate_pfn = low_pfn;
844
845 return ISOLATE_SUCCESS;
846}
847
Mel Gorman748446b2010-05-24 14:32:27 -0700848static int compact_finished(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800849 struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700850{
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800851 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -0700852
Mel Gorman748446b2010-05-24 14:32:27 -0700853 if (fatal_signal_pending(current))
854 return COMPACT_PARTIAL;
855
Mel Gorman753341a2012-10-08 16:32:40 -0700856 /* Compaction run completes if the migrate and free scanner meet */
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700857 if (cc->free_pfn <= cc->migrate_pfn) {
Mel Gorman62997022012-10-08 16:32:47 -0700858 /*
859 * Mark that the PG_migrate_skip information should be cleared
860 * by kswapd when it goes to sleep. kswapd does not set the
861 * flag itself as the decision to be clear should be directly
862 * based on an allocation request.
863 */
864 if (!current_is_kswapd())
865 zone->compact_blockskip_flush = true;
866
Mel Gorman748446b2010-05-24 14:32:27 -0700867 return COMPACT_COMPLETE;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700868 }
Mel Gorman748446b2010-05-24 14:32:27 -0700869
Johannes Weiner82478fb2011-01-20 14:44:21 -0800870 /*
871 * order == -1 is expected when compacting via
872 * /proc/sys/vm/compact_memory
873 */
Mel Gorman56de7262010-05-24 14:32:30 -0700874 if (cc->order == -1)
875 return COMPACT_CONTINUE;
876
Michal Hocko3957c772011-06-15 15:08:25 -0700877 /* Compaction run is not finished if the watermark is not met */
878 watermark = low_wmark_pages(zone);
879 watermark += (1 << cc->order);
880
881 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
882 return COMPACT_CONTINUE;
883
Mel Gorman56de7262010-05-24 14:32:30 -0700884 /* Direct compactor: Is a suitable page free? */
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700885 if (cc->page) {
886 /* Was a suitable page captured? */
887 if (*cc->page)
Mel Gorman56de7262010-05-24 14:32:30 -0700888 return COMPACT_PARTIAL;
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700889 } else {
890 unsigned int order;
891 for (order = cc->order; order < MAX_ORDER; order++) {
892 struct free_area *area = &zone->free_area[cc->order];
893 /* Job done if page is free of the right migratetype */
894 if (!list_empty(&area->free_list[cc->migratetype]))
895 return COMPACT_PARTIAL;
Mel Gorman56de7262010-05-24 14:32:30 -0700896
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700897 /* Job done if allocation would set block type */
898 if (cc->order >= pageblock_order && area->nr_free)
899 return COMPACT_PARTIAL;
900 }
Mel Gorman56de7262010-05-24 14:32:30 -0700901 }
902
Mel Gorman748446b2010-05-24 14:32:27 -0700903 return COMPACT_CONTINUE;
904}
905
Mel Gorman3e7d3442011-01-13 15:45:56 -0800906/*
907 * compaction_suitable: Is this suitable to run compaction on this zone now?
908 * Returns
909 * COMPACT_SKIPPED - If there are too few free pages for compaction
910 * COMPACT_PARTIAL - If the allocation would succeed without compaction
911 * COMPACT_CONTINUE - If compaction should run now
912 */
913unsigned long compaction_suitable(struct zone *zone, int order)
914{
915 int fragindex;
916 unsigned long watermark;
917
918 /*
Michal Hocko3957c772011-06-15 15:08:25 -0700919 * order == -1 is expected when compacting via
920 * /proc/sys/vm/compact_memory
921 */
922 if (order == -1)
923 return COMPACT_CONTINUE;
924
925 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -0800926 * Watermarks for order-0 must be met for compaction. Note the 2UL.
927 * This is because during migration, copies of pages need to be
928 * allocated and for a short time, the footprint is higher
929 */
930 watermark = low_wmark_pages(zone) + (2UL << order);
931 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
932 return COMPACT_SKIPPED;
933
934 /*
935 * fragmentation index determines if allocation failures are due to
936 * low memory or external fragmentation
937 *
Shaohua Lia582a732011-06-15 15:08:49 -0700938 * index of -1000 implies allocations might succeed depending on
939 * watermarks
Mel Gorman3e7d3442011-01-13 15:45:56 -0800940 * index towards 0 implies failure is due to lack of memory
941 * index towards 1000 implies failure is due to fragmentation
942 *
943 * Only compact if a failure would be due to fragmentation.
944 */
945 fragindex = fragmentation_index(zone, order);
946 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
947 return COMPACT_SKIPPED;
948
Shaohua Lia582a732011-06-15 15:08:49 -0700949 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
950 0, 0))
Mel Gorman3e7d3442011-01-13 15:45:56 -0800951 return COMPACT_PARTIAL;
952
953 return COMPACT_CONTINUE;
954}
955
Mel Gorman748446b2010-05-24 14:32:27 -0700956static int compact_zone(struct zone *zone, struct compact_control *cc)
957{
958 int ret;
Mel Gormanc89511a2012-10-08 16:32:45 -0700959 unsigned long start_pfn = zone->zone_start_pfn;
960 unsigned long end_pfn = zone->zone_start_pfn + zone->spanned_pages;
Mel Gorman748446b2010-05-24 14:32:27 -0700961
Mel Gorman3e7d3442011-01-13 15:45:56 -0800962 ret = compaction_suitable(zone, cc->order);
963 switch (ret) {
964 case COMPACT_PARTIAL:
965 case COMPACT_SKIPPED:
966 /* Compaction is likely to fail */
967 return ret;
968 case COMPACT_CONTINUE:
969 /* Fall through to compaction */
970 ;
971 }
972
Mel Gormanc89511a2012-10-08 16:32:45 -0700973 /*
974 * Setup to move all movable pages to the end of the zone. Used cached
975 * information on where the scanners should start but check that it
976 * is initialised by ensuring the values are within zone boundaries.
977 */
978 cc->migrate_pfn = zone->compact_cached_migrate_pfn;
979 cc->free_pfn = zone->compact_cached_free_pfn;
980 if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
981 cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
982 zone->compact_cached_free_pfn = cc->free_pfn;
983 }
984 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
985 cc->migrate_pfn = start_pfn;
986 zone->compact_cached_migrate_pfn = cc->migrate_pfn;
987 }
Mel Gorman748446b2010-05-24 14:32:27 -0700988
Mel Gorman62997022012-10-08 16:32:47 -0700989 /*
990 * Clear pageblock skip if there were failures recently and compaction
991 * is about to be retried after being deferred. kswapd does not do
992 * this reset as it'll reset the cached information when going to sleep.
993 */
994 if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
995 __reset_isolation_suitable(zone);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700996
Mel Gorman748446b2010-05-24 14:32:27 -0700997 migrate_prep_local();
998
999 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
1000 unsigned long nr_migrate, nr_remaining;
Minchan Kim9d502c12011-03-22 16:30:39 -07001001 int err;
Mel Gorman748446b2010-05-24 14:32:27 -07001002
Mel Gormanf9e35b32011-06-15 15:08:52 -07001003 switch (isolate_migratepages(zone, cc)) {
1004 case ISOLATE_ABORT:
1005 ret = COMPACT_PARTIAL;
Rafael Aquini5733c7d2012-12-11 16:02:47 -08001006 putback_movable_pages(&cc->migratepages);
Shaohua Lie64c5232012-10-08 16:32:27 -07001007 cc->nr_migratepages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -07001008 goto out;
1009 case ISOLATE_NONE:
Mel Gorman748446b2010-05-24 14:32:27 -07001010 continue;
Mel Gormanf9e35b32011-06-15 15:08:52 -07001011 case ISOLATE_SUCCESS:
1012 ;
1013 }
Mel Gorman748446b2010-05-24 14:32:27 -07001014
1015 nr_migrate = cc->nr_migratepages;
Minchan Kim9d502c12011-03-22 16:30:39 -07001016 err = migrate_pages(&cc->migratepages, compaction_alloc,
Linus Torvalds68e3e922012-06-03 20:05:57 -07001017 (unsigned long)cc, false,
1018 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
Mel Gorman748446b2010-05-24 14:32:27 -07001019 update_nr_listpages(cc);
1020 nr_remaining = cc->nr_migratepages;
1021
1022 count_vm_event(COMPACTBLOCKS);
1023 count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
1024 if (nr_remaining)
1025 count_vm_events(COMPACTPAGEFAILED, nr_remaining);
Mel Gormanb7aba692011-01-13 15:45:54 -08001026 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
1027 nr_remaining);
Mel Gorman748446b2010-05-24 14:32:27 -07001028
Rafael Aquini5733c7d2012-12-11 16:02:47 -08001029 /* Release isolated pages not migrated */
Minchan Kim9d502c12011-03-22 16:30:39 -07001030 if (err) {
Rafael Aquini5733c7d2012-12-11 16:02:47 -08001031 putback_movable_pages(&cc->migratepages);
Mel Gorman748446b2010-05-24 14:32:27 -07001032 cc->nr_migratepages = 0;
David Rientjes4bf2bba2012-07-11 14:02:13 -07001033 if (err == -ENOMEM) {
1034 ret = COMPACT_PARTIAL;
1035 goto out;
1036 }
Mel Gorman748446b2010-05-24 14:32:27 -07001037 }
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001038
1039 /* Capture a page now if it is a suitable size */
1040 compact_capture_page(cc);
Mel Gorman748446b2010-05-24 14:32:27 -07001041 }
1042
Mel Gormanf9e35b32011-06-15 15:08:52 -07001043out:
Mel Gorman748446b2010-05-24 14:32:27 -07001044 /* Release free pages and check accounting */
1045 cc->nr_freepages -= release_freepages(&cc->freepages);
1046 VM_BUG_ON(cc->nr_freepages != 0);
1047
1048 return ret;
1049}
Mel Gorman76ab0f52010-05-24 14:32:28 -07001050
Kyungmin Parkd43a87e2011-10-31 17:09:08 -07001051static unsigned long compact_zone_order(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -08001052 int order, gfp_t gfp_mask,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001053 bool sync, bool *contended,
1054 struct page **page)
Mel Gorman56de7262010-05-24 14:32:30 -07001055{
Shaohua Lie64c5232012-10-08 16:32:27 -07001056 unsigned long ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001057 struct compact_control cc = {
1058 .nr_freepages = 0,
1059 .nr_migratepages = 0,
1060 .order = order,
1061 .migratetype = allocflags_to_migratetype(gfp_mask),
1062 .zone = zone,
Linus Torvalds68e3e922012-06-03 20:05:57 -07001063 .sync = sync,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001064 .page = page,
Mel Gorman56de7262010-05-24 14:32:30 -07001065 };
1066 INIT_LIST_HEAD(&cc.freepages);
1067 INIT_LIST_HEAD(&cc.migratepages);
1068
Shaohua Lie64c5232012-10-08 16:32:27 -07001069 ret = compact_zone(zone, &cc);
1070
1071 VM_BUG_ON(!list_empty(&cc.freepages));
1072 VM_BUG_ON(!list_empty(&cc.migratepages));
1073
1074 *contended = cc.contended;
1075 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001076}
1077
Mel Gorman5e771902010-05-24 14:32:31 -07001078int sysctl_extfrag_threshold = 500;
1079
Mel Gorman56de7262010-05-24 14:32:30 -07001080/**
1081 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
1082 * @zonelist: The zonelist used for the current allocation
1083 * @order: The order of the current allocation
1084 * @gfp_mask: The GFP mask of the current allocation
1085 * @nodemask: The allowed nodes to allocate from
Mel Gorman77f1fe62011-01-13 15:45:57 -08001086 * @sync: Whether migration is synchronous or not
Mel Gorman661c4cb2012-10-08 16:32:31 -07001087 * @contended: Return value that is true if compaction was aborted due to lock contention
1088 * @page: Optionally capture a free page of the requested order during compaction
Mel Gorman56de7262010-05-24 14:32:30 -07001089 *
1090 * This is the main entry point for direct page compaction.
1091 */
1092unsigned long try_to_compact_pages(struct zonelist *zonelist,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001093 int order, gfp_t gfp_mask, nodemask_t *nodemask,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001094 bool sync, bool *contended, struct page **page)
Mel Gorman56de7262010-05-24 14:32:30 -07001095{
1096 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
1097 int may_enter_fs = gfp_mask & __GFP_FS;
1098 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -07001099 struct zoneref *z;
1100 struct zone *zone;
1101 int rc = COMPACT_SKIPPED;
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001102 int alloc_flags = 0;
Mel Gorman56de7262010-05-24 14:32:30 -07001103
Mel Gorman4ffb6332012-10-08 16:29:09 -07001104 /* Check if the GFP flags allow compaction */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -08001105 if (!order || !may_enter_fs || !may_perform_io)
Mel Gorman56de7262010-05-24 14:32:30 -07001106 return rc;
1107
1108 count_vm_event(COMPACTSTALL);
1109
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001110#ifdef CONFIG_CMA
1111 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
1112 alloc_flags |= ALLOC_CMA;
1113#endif
Mel Gorman56de7262010-05-24 14:32:30 -07001114 /* Compact each zone in the list */
1115 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
1116 nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -07001117 int status;
1118
Mel Gormanc67fe372012-08-21 16:16:17 -07001119 status = compact_zone_order(zone, order, gfp_mask, sync,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001120 contended, page);
Mel Gorman56de7262010-05-24 14:32:30 -07001121 rc = max(status, rc);
1122
Mel Gorman3e7d3442011-01-13 15:45:56 -08001123 /* If a normal allocation would succeed, stop compacting */
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001124 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
1125 alloc_flags))
Mel Gorman56de7262010-05-24 14:32:30 -07001126 break;
1127 }
1128
1129 return rc;
1130}
1131
1132
Mel Gorman76ab0f52010-05-24 14:32:28 -07001133/* Compact all zones within a node */
Rik van Riel7be62de2012-03-21 16:33:52 -07001134static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001135{
1136 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -07001137 struct zone *zone;
1138
Mel Gorman76ab0f52010-05-24 14:32:28 -07001139 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -07001140
1141 zone = &pgdat->node_zones[zoneid];
1142 if (!populated_zone(zone))
1143 continue;
1144
Rik van Riel7be62de2012-03-21 16:33:52 -07001145 cc->nr_freepages = 0;
1146 cc->nr_migratepages = 0;
1147 cc->zone = zone;
1148 INIT_LIST_HEAD(&cc->freepages);
1149 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001150
Dan Carpenteraad6ec32012-03-21 16:33:54 -07001151 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -07001152 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001153
Rik van Rielaff62242012-03-21 16:33:52 -07001154 if (cc->order > 0) {
1155 int ok = zone_watermark_ok(zone, cc->order,
1156 low_wmark_pages(zone), 0, 0);
Minchan Kimc81758f2012-08-21 16:16:03 -07001157 if (ok && cc->order >= zone->compact_order_failed)
Rik van Rielaff62242012-03-21 16:33:52 -07001158 zone->compact_order_failed = cc->order + 1;
1159 /* Currently async compaction is never deferred. */
Linus Torvalds68e3e922012-06-03 20:05:57 -07001160 else if (!ok && cc->sync)
Rik van Rielaff62242012-03-21 16:33:52 -07001161 defer_compaction(zone, cc->order);
1162 }
1163
Rik van Riel7be62de2012-03-21 16:33:52 -07001164 VM_BUG_ON(!list_empty(&cc->freepages));
1165 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -07001166 }
1167
1168 return 0;
1169}
1170
Rik van Riel7be62de2012-03-21 16:33:52 -07001171int compact_pgdat(pg_data_t *pgdat, int order)
1172{
1173 struct compact_control cc = {
1174 .order = order,
Linus Torvalds68e3e922012-06-03 20:05:57 -07001175 .sync = false,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001176 .page = NULL,
Rik van Riel7be62de2012-03-21 16:33:52 -07001177 };
1178
1179 return __compact_pgdat(pgdat, &cc);
1180}
1181
1182static int compact_node(int nid)
1183{
Rik van Riel7be62de2012-03-21 16:33:52 -07001184 struct compact_control cc = {
1185 .order = -1,
Linus Torvalds68e3e922012-06-03 20:05:57 -07001186 .sync = true,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001187 .page = NULL,
Rik van Riel7be62de2012-03-21 16:33:52 -07001188 };
1189
Hugh Dickins8575ec22012-03-21 16:33:53 -07001190 return __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001191}
1192
Mel Gorman76ab0f52010-05-24 14:32:28 -07001193/* Compact all nodes in the system */
1194static int compact_nodes(void)
1195{
1196 int nid;
1197
Hugh Dickins8575ec22012-03-21 16:33:53 -07001198 /* Flush pending updates to the LRU lists */
1199 lru_add_drain_all();
1200
Mel Gorman76ab0f52010-05-24 14:32:28 -07001201 for_each_online_node(nid)
1202 compact_node(nid);
1203
1204 return COMPACT_COMPLETE;
1205}
1206
1207/* The written value is actually unused, all memory is compacted */
1208int sysctl_compact_memory;
1209
1210/* This is the entry point for compacting all nodes via /proc/sys/vm */
1211int sysctl_compaction_handler(struct ctl_table *table, int write,
1212 void __user *buffer, size_t *length, loff_t *ppos)
1213{
1214 if (write)
1215 return compact_nodes();
1216
1217 return 0;
1218}
Mel Gormaned4a6d72010-05-24 14:32:29 -07001219
Mel Gorman5e771902010-05-24 14:32:31 -07001220int sysctl_extfrag_handler(struct ctl_table *table, int write,
1221 void __user *buffer, size_t *length, loff_t *ppos)
1222{
1223 proc_dointvec_minmax(table, write, buffer, length, ppos);
1224
1225 return 0;
1226}
1227
Mel Gormaned4a6d72010-05-24 14:32:29 -07001228#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Kay Sievers10fbcf42011-12-21 14:48:43 -08001229ssize_t sysfs_compact_node(struct device *dev,
1230 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -07001231 const char *buf, size_t count)
1232{
Hugh Dickins8575ec22012-03-21 16:33:53 -07001233 int nid = dev->id;
1234
1235 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1236 /* Flush pending updates to the LRU lists */
1237 lru_add_drain_all();
1238
1239 compact_node(nid);
1240 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07001241
1242 return count;
1243}
Kay Sievers10fbcf42011-12-21 14:48:43 -08001244static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001245
1246int compaction_register_node(struct node *node)
1247{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001248 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001249}
1250
1251void compaction_unregister_node(struct node *node)
1252{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001253 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001254}
1255#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001256
1257#endif /* CONFIG_COMPACTION */