blob: 2b27847f80e5e313cade594befcaee4000b3e785 [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
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +010019#if defined CONFIG_COMPACTION || defined CONFIG_CMA
20
Mel Gormanb7aba692011-01-13 15:45:54 -080021#define CREATE_TRACE_POINTS
22#include <trace/events/compaction.h>
23
Mel Gorman748446b2010-05-24 14:32:27 -070024static unsigned long release_freepages(struct list_head *freelist)
25{
26 struct page *page, *next;
27 unsigned long count = 0;
28
29 list_for_each_entry_safe(page, next, freelist, lru) {
30 list_del(&page->lru);
31 __free_page(page);
32 count++;
33 }
34
35 return count;
36}
37
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +010038static void map_pages(struct list_head *list)
39{
40 struct page *page;
41
42 list_for_each_entry(page, list, lru) {
43 arch_alloc_page(page, 0);
44 kernel_map_pages(page, 1, 1);
45 }
46}
47
Michal Nazarewiczd4158d22011-12-29 13:09:50 +010048static inline bool migrate_async_suitable(int migratetype)
49{
50 return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
51}
52
Mel Gormancc5a88a2012-10-08 16:32:41 -070053#ifdef CONFIG_COMPACTION
54/* Returns true if the pageblock should be scanned for pages to isolate. */
55static inline bool isolation_suitable(struct compact_control *cc,
56 struct page *page)
57{
58 if (cc->ignore_skip_hint)
59 return true;
60
61 return !get_pageblock_skip(page);
62}
63
64/*
65 * This function is called to clear all cached information on pageblocks that
66 * should be skipped for page isolation when the migrate and free page scanner
67 * meet.
68 */
Mel Gormanb71127a2012-10-08 16:32:47 -070069static void __reset_isolation_suitable(struct zone *zone)
Mel Gormancc5a88a2012-10-08 16:32:41 -070070{
71 unsigned long start_pfn = zone->zone_start_pfn;
72 unsigned long end_pfn = zone->zone_start_pfn + zone->spanned_pages;
73 unsigned long pfn;
74
Mel Gormanf57bc462012-10-08 16:32:45 -070075 zone->compact_cached_migrate_pfn = start_pfn;
76 zone->compact_cached_free_pfn = end_pfn;
Mel Gormanb71127a2012-10-08 16:32:47 -070077 zone->compact_blockskip_flush = false;
Mel Gormancc5a88a2012-10-08 16:32:41 -070078
79 /* Walk the zone and mark every pageblock as suitable for isolation */
80 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
81 struct page *page;
82
83 cond_resched();
84
85 if (!pfn_valid(pfn))
86 continue;
87
88 page = pfn_to_page(pfn);
89 if (zone != page_zone(page))
90 continue;
91
92 clear_pageblock_skip(page);
93 }
94}
95
Mel Gormanb71127a2012-10-08 16:32:47 -070096void reset_isolation_suitable(pg_data_t *pgdat)
97{
98 int zoneid;
99
100 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
101 struct zone *zone = &pgdat->node_zones[zoneid];
102 if (!populated_zone(zone))
103 continue;
104
105 /* Only flush if a full compaction finished recently */
106 if (zone->compact_blockskip_flush)
107 __reset_isolation_suitable(zone);
108 }
109}
110
Mel Gormancc5a88a2012-10-08 16:32:41 -0700111/*
112 * If no pages were isolated then mark this pageblock to be skipped in the
Mel Gormanb71127a2012-10-08 16:32:47 -0700113 * future. The information is later cleared by __reset_isolation_suitable().
Mel Gormancc5a88a2012-10-08 16:32:41 -0700114 */
Mel Gormanf57bc462012-10-08 16:32:45 -0700115static void update_pageblock_skip(struct compact_control *cc,
116 struct page *page, unsigned long nr_isolated,
117 bool migrate_scanner)
Mel Gormancc5a88a2012-10-08 16:32:41 -0700118{
Mel Gormanf57bc462012-10-08 16:32:45 -0700119 struct zone *zone = cc->zone;
Mel Gormancc5a88a2012-10-08 16:32:41 -0700120 if (!page)
121 return;
122
Mel Gormanf57bc462012-10-08 16:32:45 -0700123 if (!nr_isolated) {
124 unsigned long pfn = page_to_pfn(page);
Mel Gormancc5a88a2012-10-08 16:32:41 -0700125 set_pageblock_skip(page);
Mel Gormanf57bc462012-10-08 16:32:45 -0700126
127 /* Update where compaction should restart */
128 if (migrate_scanner) {
129 if (!cc->finished_update_migrate &&
130 pfn > zone->compact_cached_migrate_pfn)
131 zone->compact_cached_migrate_pfn = pfn;
132 } else {
133 if (!cc->finished_update_free &&
134 pfn < zone->compact_cached_free_pfn)
135 zone->compact_cached_free_pfn = pfn;
136 }
137 }
Mel Gormancc5a88a2012-10-08 16:32:41 -0700138}
139#else
140static inline bool isolation_suitable(struct compact_control *cc,
141 struct page *page)
142{
143 return true;
144}
145
Mel Gormanf57bc462012-10-08 16:32:45 -0700146static void update_pageblock_skip(struct compact_control *cc,
147 struct page *page, unsigned long nr_isolated,
148 bool migrate_scanner)
Mel Gormancc5a88a2012-10-08 16:32:41 -0700149{
150}
151#endif /* CONFIG_COMPACTION */
152
Mel Gorman6a38cba2012-10-08 16:32:33 -0700153static inline bool should_release_lock(spinlock_t *lock)
154{
155 return need_resched() || spin_is_contended(lock);
156}
157
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100158/*
Mel Gorman0ba33882012-08-21 16:16:17 -0700159 * Compaction requires the taking of some coarse locks that are potentially
160 * very heavily contended. Check if the process needs to be scheduled or
161 * if the lock is contended. For async compaction, back out in the event
162 * if contention is severe. For sync compaction, schedule.
163 *
164 * Returns true if the lock is held.
165 * Returns false if the lock is released and compaction should abort
166 */
167static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags,
168 bool locked, struct compact_control *cc)
169{
Mel Gorman6a38cba2012-10-08 16:32:33 -0700170 if (should_release_lock(lock)) {
Mel Gorman0ba33882012-08-21 16:16:17 -0700171 if (locked) {
172 spin_unlock_irqrestore(lock, *flags);
173 locked = false;
174 }
175
176 /* async aborts if taking too long or contended */
177 if (!cc->sync) {
Shaohua Li03dd8fe2012-10-08 16:32:27 -0700178 cc->contended = true;
Mel Gorman0ba33882012-08-21 16:16:17 -0700179 return false;
180 }
181
182 cond_resched();
Mel Gorman0ba33882012-08-21 16:16:17 -0700183 }
184
185 if (!locked)
186 spin_lock_irqsave(lock, *flags);
187 return true;
188}
189
190static inline bool compact_trylock_irqsave(spinlock_t *lock,
191 unsigned long *flags, struct compact_control *cc)
192{
193 return compact_checklock_irqsave(lock, flags, false, cc);
194}
195
Mel Gormand6ed9722012-10-08 16:32:36 -0700196/* Returns true if the page is within a block suitable for migration to */
197static bool suitable_migration_target(struct page *page)
198{
199 int migratetype = get_pageblock_migratetype(page);
200
201 /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
202 if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
203 return false;
204
205 /* If the page is a large free page, then allow migration */
206 if (PageBuddy(page) && page_order(page) >= pageblock_order)
207 return true;
208
209 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
210 if (migrate_async_suitable(migratetype))
211 return true;
212
213 /* Otherwise skip the block */
214 return false;
215}
216
Mel Gormanc74068b2012-10-08 16:29:12 -0700217static void compact_capture_page(struct compact_control *cc)
218{
219 unsigned long flags;
220 int mtype, mtype_low, mtype_high;
221
222 if (!cc->page || *cc->page)
223 return;
224
225 /*
226 * For MIGRATE_MOVABLE allocations we capture a suitable page ASAP
227 * regardless of the migratetype of the freelist is is captured from.
228 * This is fine because the order for a high-order MIGRATE_MOVABLE
229 * allocation is typically at least a pageblock size and overall
230 * fragmentation is not impaired. Other allocation types must
231 * capture pages from their own migratelist because otherwise they
232 * could pollute other pageblocks like MIGRATE_MOVABLE with
233 * difficult to move pages and making fragmentation worse overall.
234 */
235 if (cc->migratetype == MIGRATE_MOVABLE) {
236 mtype_low = 0;
237 mtype_high = MIGRATE_PCPTYPES;
238 } else {
239 mtype_low = cc->migratetype;
240 mtype_high = cc->migratetype + 1;
241 }
242
243 /* Speculatively examine the free lists without zone lock */
244 for (mtype = mtype_low; mtype < mtype_high; mtype++) {
245 int order;
246 for (order = cc->order; order < MAX_ORDER; order++) {
247 struct page *page;
248 struct free_area *area;
249 area = &(cc->zone->free_area[order]);
250 if (list_empty(&area->free_list[mtype]))
251 continue;
252
253 /* Take the lock and attempt capture of the page */
254 if (!compact_trylock_irqsave(&cc->zone->lock, &flags, cc))
255 return;
256 if (!list_empty(&area->free_list[mtype])) {
257 page = list_entry(area->free_list[mtype].next,
258 struct page, lru);
259 if (capture_free_page(page, cc->order, mtype)) {
260 spin_unlock_irqrestore(&cc->zone->lock,
261 flags);
262 *cc->page = page;
263 return;
264 }
265 }
266 spin_unlock_irqrestore(&cc->zone->lock, flags);
267 }
268 }
269}
270
Mel Gorman0ba33882012-08-21 16:16:17 -0700271/*
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100272 * Isolate free pages onto a private freelist. Caller must hold zone->lock.
273 * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
274 * pages inside of the pageblock (even though it may still end up isolating
275 * some pages).
276 */
Mel Gormand6ed9722012-10-08 16:32:36 -0700277static unsigned long isolate_freepages_block(struct compact_control *cc,
278 unsigned long blockpfn,
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100279 unsigned long end_pfn,
280 struct list_head *freelist,
281 bool strict)
Mel Gorman748446b2010-05-24 14:32:27 -0700282{
Mel Gormanb7aba692011-01-13 15:45:54 -0800283 int nr_scanned = 0, total_isolated = 0;
Mel Gormancc5a88a2012-10-08 16:32:41 -0700284 struct page *cursor, *valid_page = NULL;
Mel Gormand6ed9722012-10-08 16:32:36 -0700285 unsigned long nr_strict_required = end_pfn - blockpfn;
286 unsigned long flags;
287 bool locked = false;
Mel Gorman748446b2010-05-24 14:32:27 -0700288
Mel Gorman748446b2010-05-24 14:32:27 -0700289 cursor = pfn_to_page(blockpfn);
290
Mel Gormand6ed9722012-10-08 16:32:36 -0700291 /* Isolate free pages. */
Mel Gorman748446b2010-05-24 14:32:27 -0700292 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
293 int isolated, i;
294 struct page *page = cursor;
295
Mel Gormanb7aba692011-01-13 15:45:54 -0800296 nr_scanned++;
Mel Gormand6ed9722012-10-08 16:32:36 -0700297 if (!pfn_valid_within(blockpfn))
Mel Gorman748446b2010-05-24 14:32:27 -0700298 continue;
Mel Gormancc5a88a2012-10-08 16:32:41 -0700299 if (!valid_page)
300 valid_page = page;
Mel Gormand6ed9722012-10-08 16:32:36 -0700301 if (!PageBuddy(page))
302 continue;
303
304 /*
305 * The zone lock must be held to isolate freepages.
306 * Unfortunately this is a very coarse lock and can be
307 * heavily contended if there are parallel allocations
308 * or parallel compactions. For async compaction do not
309 * spin on the lock and we acquire the lock as late as
310 * possible.
311 */
312 locked = compact_checklock_irqsave(&cc->zone->lock, &flags,
313 locked, cc);
314 if (!locked)
315 break;
316
317 /* Recheck this is a suitable migration target under lock */
318 if (!strict && !suitable_migration_target(page))
319 break;
320
321 /* Recheck this is a buddy page under lock */
322 if (!PageBuddy(page))
323 continue;
Mel Gorman748446b2010-05-24 14:32:27 -0700324
325 /* Found a free page, break it into order-0 pages */
326 isolated = split_free_page(page);
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100327 if (!isolated && strict)
Mel Gormand6ed9722012-10-08 16:32:36 -0700328 break;
Mel Gorman748446b2010-05-24 14:32:27 -0700329 total_isolated += isolated;
330 for (i = 0; i < isolated; i++) {
331 list_add(&page->lru, freelist);
332 page++;
333 }
334
335 /* If a page was split, advance to the end of it */
336 if (isolated) {
337 blockpfn += isolated - 1;
338 cursor += isolated - 1;
339 }
340 }
341
Mel Gormanb7aba692011-01-13 15:45:54 -0800342 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
Mel Gormand6ed9722012-10-08 16:32:36 -0700343
344 /*
345 * If strict isolation is requested by CMA then check that all the
346 * pages requested were isolated. If there were any failures, 0 is
347 * returned and CMA will fail.
348 */
Mel Gorman2a1e4f42012-10-19 13:56:57 -0700349 if (strict && nr_strict_required > total_isolated)
Mel Gormand6ed9722012-10-08 16:32:36 -0700350 total_isolated = 0;
351
352 if (locked)
353 spin_unlock_irqrestore(&cc->zone->lock, flags);
354
Mel Gormancc5a88a2012-10-08 16:32:41 -0700355 /* Update the pageblock-skip if the whole pageblock was scanned */
356 if (blockpfn == end_pfn)
Mel Gormanf57bc462012-10-08 16:32:45 -0700357 update_pageblock_skip(cc, valid_page, total_isolated, false);
Mel Gormancc5a88a2012-10-08 16:32:41 -0700358
Mel Gorman70e51922012-10-19 12:00:10 +0100359 count_vm_events(COMPACTFREE_SCANNED, nr_scanned);
360 if (total_isolated)
361 count_vm_events(COMPACTISOLATED, total_isolated);
362
Mel Gorman748446b2010-05-24 14:32:27 -0700363 return total_isolated;
364}
365
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100366/**
367 * isolate_freepages_range() - isolate free pages.
368 * @start_pfn: The first PFN to start isolating.
369 * @end_pfn: The one-past-last PFN.
370 *
371 * Non-free pages, invalid PFNs, or zone boundaries within the
372 * [start_pfn, end_pfn) range are considered errors, cause function to
373 * undo its actions and return zero.
374 *
375 * Otherwise, function returns one-past-the-last PFN of isolated page
376 * (which may be greater then end_pfn if end fell in a middle of
377 * a free page).
378 */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100379unsigned long
Mel Gormancc5a88a2012-10-08 16:32:41 -0700380isolate_freepages_range(struct compact_control *cc,
381 unsigned long start_pfn, unsigned long end_pfn)
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100382{
Mel Gormand6ed9722012-10-08 16:32:36 -0700383 unsigned long isolated, pfn, block_end_pfn;
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100384 LIST_HEAD(freelist);
385
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100386 for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
Mel Gormancc5a88a2012-10-08 16:32:41 -0700387 if (!pfn_valid(pfn) || cc->zone != page_zone(pfn_to_page(pfn)))
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100388 break;
389
390 /*
391 * On subsequent iterations ALIGN() is actually not needed,
392 * but we keep it that we not to complicate the code.
393 */
394 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
395 block_end_pfn = min(block_end_pfn, end_pfn);
396
Mel Gormancc5a88a2012-10-08 16:32:41 -0700397 isolated = isolate_freepages_block(cc, pfn, block_end_pfn,
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100398 &freelist, true);
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100399
400 /*
401 * In strict mode, isolate_freepages_block() returns 0 if
402 * there are any holes in the block (ie. invalid PFNs or
403 * non-free pages).
404 */
405 if (!isolated)
406 break;
407
408 /*
409 * If we managed to isolate pages, it is always (1 << n) *
410 * pageblock_nr_pages for some non-negative n. (Max order
411 * page may span two pageblocks).
412 */
413 }
414
415 /* split_free_page does not map the pages */
416 map_pages(&freelist);
417
418 if (pfn < end_pfn) {
419 /* Loop terminated early, cleanup. */
420 release_freepages(&freelist);
421 return 0;
422 }
423
424 /* We don't use freelists for anything. */
425 return pfn;
426}
427
Mel Gorman748446b2010-05-24 14:32:27 -0700428/* Update the number of anon and file isolated pages in the zone */
Mel Gorman0ba33882012-08-21 16:16:17 -0700429static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700430{
431 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700432 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700433
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700434 list_for_each_entry(page, &cc->migratepages, lru)
435 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700436
Mel Gorman0ba33882012-08-21 16:16:17 -0700437 /* If locked we can use the interrupt unsafe versions */
438 if (locked) {
439 __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
440 __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
441 } else {
442 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
443 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
444 }
Mel Gorman748446b2010-05-24 14:32:27 -0700445}
446
447/* Similar to reclaim, but different enough that they don't share logic */
448static bool too_many_isolated(struct zone *zone)
449{
Minchan Kimbc693042010-09-09 16:38:00 -0700450 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700451
452 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
453 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700454 active = zone_page_state(zone, NR_ACTIVE_FILE) +
455 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700456 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
457 zone_page_state(zone, NR_ISOLATED_ANON);
458
Minchan Kimbc693042010-09-09 16:38:00 -0700459 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700460}
461
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100462/**
463 * isolate_migratepages_range() - isolate all migrate-able pages in range.
464 * @zone: Zone pages are in.
465 * @cc: Compaction control structure.
466 * @low_pfn: The first PFN of the range.
467 * @end_pfn: The one-past-the-last PFN of the range.
Minchan Kimf8194dc2012-10-08 16:33:48 -0700468 * @unevictable: true if it allows to isolate unevictable pages
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100469 *
470 * Isolate all pages that can be migrated from the range specified by
471 * [low_pfn, end_pfn). Returns zero if there is a fatal signal
472 * pending), otherwise PFN of the first page that was not scanned
473 * (which may be both less, equal to or more then end_pfn).
474 *
475 * Assumes that cc->migratepages is empty and cc->nr_migratepages is
476 * zero.
477 *
478 * Apart from cc->migratepages and cc->nr_migratetypes this function
479 * does not modify any cc's fields, in particular it does not modify
480 * (or read for that matter) cc->migrate_pfn.
Mel Gorman748446b2010-05-24 14:32:27 -0700481 */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100482unsigned long
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100483isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
Minchan Kimf8194dc2012-10-08 16:33:48 -0700484 unsigned long low_pfn, unsigned long end_pfn, bool unevictable)
Mel Gorman748446b2010-05-24 14:32:27 -0700485{
Mel Gorman9927af742011-01-13 15:45:59 -0800486 unsigned long last_pageblock_nr = 0, pageblock_nr;
Mel Gormanb7aba692011-01-13 15:45:54 -0800487 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700488 struct list_head *migratelist = &cc->migratepages;
Konstantin Khlebnikovfa168092012-05-29 15:06:54 -0700489 isolate_mode_t mode = 0;
Mel Gorman0ba33882012-08-21 16:16:17 -0700490 unsigned long flags;
Mel Gorman6a38cba2012-10-08 16:32:33 -0700491 bool locked = false;
Mel Gormancc5a88a2012-10-08 16:32:41 -0700492 struct page *page = NULL, *valid_page = NULL;
Mel Gorman748446b2010-05-24 14:32:27 -0700493
Mel Gorman748446b2010-05-24 14:32:27 -0700494 /*
495 * Ensure that there are not too many pages isolated from the LRU
496 * list by either parallel reclaimers or compaction. If there are,
497 * delay for some time until fewer pages are isolated
498 */
499 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700500 /* async migration should just abort */
501 if (!cc->sync)
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100502 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700503
Mel Gorman748446b2010-05-24 14:32:27 -0700504 congestion_wait(BLK_RW_ASYNC, HZ/10);
505
506 if (fatal_signal_pending(current))
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100507 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700508 }
509
510 /* Time to isolate some pages for migration */
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700511 cond_resched();
Mel Gorman748446b2010-05-24 14:32:27 -0700512 for (; low_pfn < end_pfn; low_pfn++) {
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700513 /* give a chance to irqs before checking need_resched() */
Mel Gorman6a38cba2012-10-08 16:32:33 -0700514 if (locked && !((low_pfn+1) % SWAP_CLUSTER_MAX)) {
515 if (should_release_lock(&zone->lru_lock)) {
516 spin_unlock_irqrestore(&zone->lru_lock, flags);
517 locked = false;
518 }
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700519 }
Mel Gorman0ba33882012-08-21 16:16:17 -0700520
Mel Gorman0bf380b2012-02-03 15:37:18 -0800521 /*
522 * migrate_pfn does not necessarily start aligned to a
523 * pageblock. Ensure that pfn_valid is called when moving
524 * into a new MAX_ORDER_NR_PAGES range in case of large
525 * memory holes within the zone
526 */
527 if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
528 if (!pfn_valid(low_pfn)) {
529 low_pfn += MAX_ORDER_NR_PAGES - 1;
530 continue;
531 }
532 }
533
Mel Gorman748446b2010-05-24 14:32:27 -0700534 if (!pfn_valid_within(low_pfn))
535 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800536 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700537
Mel Gormandc908602012-02-08 17:13:38 -0800538 /*
539 * Get the page and ensure the page is within the same zone.
540 * See the comment in isolate_freepages about overlapping
541 * nodes. It is deliberate that the new zone lock is not taken
542 * as memory compaction should not move pages between nodes.
543 */
Mel Gorman748446b2010-05-24 14:32:27 -0700544 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800545 if (page_zone(page) != zone)
546 continue;
547
Mel Gormancc5a88a2012-10-08 16:32:41 -0700548 if (!valid_page)
549 valid_page = page;
550
551 /* If isolation recently failed, do not retry */
552 pageblock_nr = low_pfn >> pageblock_order;
553 if (!isolation_suitable(cc, page))
554 goto next_pageblock;
555
Mel Gormandc908602012-02-08 17:13:38 -0800556 /* Skip if free */
Mel Gorman748446b2010-05-24 14:32:27 -0700557 if (PageBuddy(page))
558 continue;
559
Mel Gorman9927af742011-01-13 15:45:59 -0800560 /*
561 * For async migration, also only scan in MOVABLE blocks. Async
562 * migration is optimistic to see if the minimum amount of work
563 * satisfies the allocation
564 */
Mel Gorman9927af742011-01-13 15:45:59 -0800565 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
Michal Nazarewiczd4158d22011-12-29 13:09:50 +0100566 !migrate_async_suitable(get_pageblock_migratetype(page))) {
Mel Gormanf57bc462012-10-08 16:32:45 -0700567 cc->finished_update_migrate = true;
Mel Gorman6a38cba2012-10-08 16:32:33 -0700568 goto next_pageblock;
Mel Gorman9927af742011-01-13 15:45:59 -0800569 }
570
Mel Gorman6a38cba2012-10-08 16:32:33 -0700571 /* Check may be lockless but that's ok as we recheck later */
Andrea Arcangelibc835012011-01-13 15:47:08 -0800572 if (!PageLRU(page))
573 continue;
574
575 /*
Mel Gorman6a38cba2012-10-08 16:32:33 -0700576 * PageLRU is set. lru_lock normally excludes isolation
577 * splitting and collapsing (collapsing has already happened
578 * if PageLRU is set) but the lock is not necessarily taken
579 * here and it is wasteful to take it just to check transhuge.
580 * Check TransHuge without lock and skip the whole pageblock if
581 * it's either a transhuge or hugetlbfs page, as calling
582 * compound_order() without preventing THP from splitting the
583 * page underneath us may return surprising results.
Andrea Arcangelibc835012011-01-13 15:47:08 -0800584 */
585 if (PageTransHuge(page)) {
Mel Gorman6a38cba2012-10-08 16:32:33 -0700586 if (!locked)
587 goto next_pageblock;
588 low_pfn += (1 << compound_order(page)) - 1;
589 continue;
590 }
591
592 /* Check if it is ok to still hold the lock */
593 locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
594 locked, cc);
595 if (!locked || fatal_signal_pending(current))
596 break;
597
598 /* Recheck PageLRU and PageTransHuge under lock */
599 if (!PageLRU(page))
600 continue;
601 if (PageTransHuge(page)) {
Andrea Arcangelibc835012011-01-13 15:47:08 -0800602 low_pfn += (1 << compound_order(page)) - 1;
603 continue;
604 }
605
Mel Gormanc8244932012-01-12 17:19:38 -0800606 if (!cc->sync)
607 mode |= ISOLATE_ASYNC_MIGRATE;
608
Minchan Kimf8194dc2012-10-08 16:33:48 -0700609 if (unevictable)
610 mode |= ISOLATE_UNEVICTABLE;
611
Mel Gorman748446b2010-05-24 14:32:27 -0700612 /* Try isolate the page */
Konstantin Khlebnikovfa168092012-05-29 15:06:54 -0700613 if (__isolate_lru_page(page, mode) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700614 continue;
615
Andrea Arcangelibc835012011-01-13 15:47:08 -0800616 VM_BUG_ON(PageTransCompound(page));
617
Mel Gorman748446b2010-05-24 14:32:27 -0700618 /* Successfully isolated */
Mel Gormanf57bc462012-10-08 16:32:45 -0700619 cc->finished_update_migrate = true;
Mel Gorman748446b2010-05-24 14:32:27 -0700620 del_page_from_lru_list(zone, page, page_lru(page));
621 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700622 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800623 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700624
625 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800626 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
627 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700628 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800629 }
Mel Gorman6a38cba2012-10-08 16:32:33 -0700630
631 continue;
632
633next_pageblock:
634 low_pfn += pageblock_nr_pages;
635 low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
636 last_pageblock_nr = pageblock_nr;
Mel Gorman748446b2010-05-24 14:32:27 -0700637 }
638
Mel Gorman0ba33882012-08-21 16:16:17 -0700639 acct_isolated(zone, locked, cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700640
Mel Gorman0ba33882012-08-21 16:16:17 -0700641 if (locked)
642 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700643
Mel Gormancc5a88a2012-10-08 16:32:41 -0700644 /* Update the pageblock-skip if the whole pageblock was scanned */
645 if (low_pfn == end_pfn)
Mel Gormanf57bc462012-10-08 16:32:45 -0700646 update_pageblock_skip(cc, valid_page, nr_isolated, true);
Mel Gormancc5a88a2012-10-08 16:32:41 -0700647
Mel Gormanb7aba692011-01-13 15:45:54 -0800648 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
649
Mel Gorman70e51922012-10-19 12:00:10 +0100650 count_vm_events(COMPACTMIGRATE_SCANNED, nr_scanned);
651 if (nr_isolated)
652 count_vm_events(COMPACTISOLATED, nr_isolated);
653
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100654 return low_pfn;
655}
656
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100657#endif /* CONFIG_COMPACTION || CONFIG_CMA */
658#ifdef CONFIG_COMPACTION
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100659/*
660 * Based on information in the current compact_control, find blocks
661 * suitable for isolating free pages from and then isolate them.
662 */
663static void isolate_freepages(struct zone *zone,
664 struct compact_control *cc)
665{
666 struct page *page;
667 unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100668 int nr_freepages = cc->nr_freepages;
669 struct list_head *freelist = &cc->freepages;
670
671 /*
672 * Initialise the free scanner. The starting point is where we last
673 * scanned from (or the end of the zone if starting). The low point
674 * is the end of the pageblock the migration scanner is using.
675 */
676 pfn = cc->free_pfn;
677 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
678
679 /*
680 * Take care that if the migration scanner is at the end of the zone
681 * that the free scanner does not accidentally move to the next zone
682 * in the next isolation cycle.
683 */
684 high_pfn = min(low_pfn, pfn);
685
686 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
687
688 /*
689 * Isolate free pages until enough are available to migrate the
690 * pages on cc->migratepages. We stop searching if the migrate
691 * and free page scanners meet or enough free pages are isolated.
692 */
693 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
694 pfn -= pageblock_nr_pages) {
695 unsigned long isolated;
696
697 if (!pfn_valid(pfn))
698 continue;
699
700 /*
701 * Check for overlapping nodes/zones. It's possible on some
702 * configurations to have a setup like
703 * node0 node1 node0
704 * i.e. it's possible that all pages within a zones range of
705 * pages do not belong to a single zone.
706 */
707 page = pfn_to_page(pfn);
708 if (page_zone(page) != zone)
709 continue;
710
711 /* Check the block is suitable for migration */
712 if (!suitable_migration_target(page))
713 continue;
714
Mel Gormancc5a88a2012-10-08 16:32:41 -0700715 /* If isolation recently failed, do not retry */
716 if (!isolation_suitable(cc, page))
717 continue;
718
Mel Gormand6ed9722012-10-08 16:32:36 -0700719 /* Found a block suitable for isolating free pages from */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100720 isolated = 0;
Mel Gorman76e36c32012-12-06 19:01:14 +0000721
722 /*
723 * As pfn may not start aligned, pfn+pageblock_nr_page
724 * may cross a MAX_ORDER_NR_PAGES boundary and miss
725 * a pfn_valid check. Ensure isolate_freepages_block()
726 * only scans within a pageblock
727 */
728 end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
729 end_pfn = min(end_pfn, zone_end_pfn);
Mel Gormand6ed9722012-10-08 16:32:36 -0700730 isolated = isolate_freepages_block(cc, pfn, end_pfn,
731 freelist, false);
732 nr_freepages += isolated;
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100733
734 /*
735 * Record the highest PFN we isolated pages from. When next
736 * looking for free pages, the search will restart here as
737 * page migration may have returned some pages to the allocator
738 */
Mel Gormanf57bc462012-10-08 16:32:45 -0700739 if (isolated) {
740 cc->finished_update_free = true;
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100741 high_pfn = max(high_pfn, pfn);
Mel Gormanf57bc462012-10-08 16:32:45 -0700742 }
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100743 }
744
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100745 /* split_free_page does not map the pages */
746 map_pages(freelist);
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100747
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100748 cc->free_pfn = high_pfn;
749 cc->nr_freepages = nr_freepages;
Mel Gorman748446b2010-05-24 14:32:27 -0700750}
751
752/*
753 * This is a migrate-callback that "allocates" freepages by taking pages
754 * from the isolated freelists in the block we are migrating to.
755 */
756static struct page *compaction_alloc(struct page *migratepage,
757 unsigned long data,
758 int **result)
759{
760 struct compact_control *cc = (struct compact_control *)data;
761 struct page *freepage;
762
763 /* Isolate free pages if necessary */
764 if (list_empty(&cc->freepages)) {
765 isolate_freepages(cc->zone, cc);
766
767 if (list_empty(&cc->freepages))
768 return NULL;
769 }
770
771 freepage = list_entry(cc->freepages.next, struct page, lru);
772 list_del(&freepage->lru);
773 cc->nr_freepages--;
774
775 return freepage;
776}
777
778/*
779 * We cannot control nr_migratepages and nr_freepages fully when migration is
780 * running as migrate_pages() has no knowledge of compact_control. When
781 * migration is complete, we count the number of pages on the lists by hand.
782 */
783static void update_nr_listpages(struct compact_control *cc)
784{
785 int nr_migratepages = 0;
786 int nr_freepages = 0;
787 struct page *page;
788
789 list_for_each_entry(page, &cc->migratepages, lru)
790 nr_migratepages++;
791 list_for_each_entry(page, &cc->freepages, lru)
792 nr_freepages++;
793
794 cc->nr_migratepages = nr_migratepages;
795 cc->nr_freepages = nr_freepages;
796}
797
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100798/* possible outcome of isolate_migratepages */
799typedef enum {
800 ISOLATE_ABORT, /* Abort compaction now */
801 ISOLATE_NONE, /* No pages isolated, continue scanning */
802 ISOLATE_SUCCESS, /* Pages isolated, migrate */
803} isolate_migrate_t;
804
805/*
806 * Isolate all pages that can be migrated from the block pointed to by
807 * the migrate scanner within compact_control.
808 */
809static isolate_migrate_t isolate_migratepages(struct zone *zone,
810 struct compact_control *cc)
811{
812 unsigned long low_pfn, end_pfn;
813
814 /* Do not scan outside zone boundaries */
815 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
816
817 /* Only scan within a pageblock boundary */
818 end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
819
820 /* Do not cross the free scanner or scan within a memory hole */
821 if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
822 cc->migrate_pfn = end_pfn;
823 return ISOLATE_NONE;
824 }
825
826 /* Perform the isolation */
Minchan Kimf8194dc2012-10-08 16:33:48 -0700827 low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn, false);
Shaohua Li03dd8fe2012-10-08 16:32:27 -0700828 if (!low_pfn || cc->contended)
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100829 return ISOLATE_ABORT;
830
831 cc->migrate_pfn = low_pfn;
832
833 return ISOLATE_SUCCESS;
834}
835
Mel Gorman748446b2010-05-24 14:32:27 -0700836static int compact_finished(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800837 struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700838{
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800839 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -0700840
Mel Gorman748446b2010-05-24 14:32:27 -0700841 if (fatal_signal_pending(current))
842 return COMPACT_PARTIAL;
843
844 /* Compaction run completes if the migrate and free scanner meet */
Mel Gormancc5a88a2012-10-08 16:32:41 -0700845 if (cc->free_pfn <= cc->migrate_pfn) {
Mel Gormanb71127a2012-10-08 16:32:47 -0700846 /*
847 * Mark that the PG_migrate_skip information should be cleared
848 * by kswapd when it goes to sleep. kswapd does not set the
849 * flag itself as the decision to be clear should be directly
850 * based on an allocation request.
851 */
852 if (!current_is_kswapd())
853 zone->compact_blockskip_flush = true;
854
Mel Gorman748446b2010-05-24 14:32:27 -0700855 return COMPACT_COMPLETE;
Mel Gormancc5a88a2012-10-08 16:32:41 -0700856 }
Mel Gorman748446b2010-05-24 14:32:27 -0700857
Johannes Weiner82478fb2011-01-20 14:44:21 -0800858 /*
859 * order == -1 is expected when compacting via
860 * /proc/sys/vm/compact_memory
861 */
Mel Gorman56de7262010-05-24 14:32:30 -0700862 if (cc->order == -1)
863 return COMPACT_CONTINUE;
864
Michal Hocko3957c772011-06-15 15:08:25 -0700865 /* Compaction run is not finished if the watermark is not met */
866 watermark = low_wmark_pages(zone);
867 watermark += (1 << cc->order);
868
869 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
870 return COMPACT_CONTINUE;
871
Mel Gorman56de7262010-05-24 14:32:30 -0700872 /* Direct compactor: Is a suitable page free? */
Mel Gormanc74068b2012-10-08 16:29:12 -0700873 if (cc->page) {
874 /* Was a suitable page captured? */
875 if (*cc->page)
Mel Gorman56de7262010-05-24 14:32:30 -0700876 return COMPACT_PARTIAL;
Mel Gormanc74068b2012-10-08 16:29:12 -0700877 } else {
878 unsigned int order;
879 for (order = cc->order; order < MAX_ORDER; order++) {
880 struct free_area *area = &zone->free_area[cc->order];
881 /* Job done if page is free of the right migratetype */
882 if (!list_empty(&area->free_list[cc->migratetype]))
883 return COMPACT_PARTIAL;
Mel Gorman56de7262010-05-24 14:32:30 -0700884
Mel Gormanc74068b2012-10-08 16:29:12 -0700885 /* Job done if allocation would set block type */
886 if (cc->order >= pageblock_order && area->nr_free)
887 return COMPACT_PARTIAL;
888 }
Mel Gorman56de7262010-05-24 14:32:30 -0700889 }
890
Mel Gorman748446b2010-05-24 14:32:27 -0700891 return COMPACT_CONTINUE;
892}
893
Mel Gorman3e7d3442011-01-13 15:45:56 -0800894/*
895 * compaction_suitable: Is this suitable to run compaction on this zone now?
896 * Returns
897 * COMPACT_SKIPPED - If there are too few free pages for compaction
898 * COMPACT_PARTIAL - If the allocation would succeed without compaction
899 * COMPACT_CONTINUE - If compaction should run now
900 */
901unsigned long compaction_suitable(struct zone *zone, int order)
902{
903 int fragindex;
904 unsigned long watermark;
905
906 /*
Michal Hocko3957c772011-06-15 15:08:25 -0700907 * order == -1 is expected when compacting via
908 * /proc/sys/vm/compact_memory
909 */
910 if (order == -1)
911 return COMPACT_CONTINUE;
912
913 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -0800914 * Watermarks for order-0 must be met for compaction. Note the 2UL.
915 * This is because during migration, copies of pages need to be
916 * allocated and for a short time, the footprint is higher
917 */
918 watermark = low_wmark_pages(zone) + (2UL << order);
919 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
920 return COMPACT_SKIPPED;
921
922 /*
923 * fragmentation index determines if allocation failures are due to
924 * low memory or external fragmentation
925 *
Shaohua Lia582a732011-06-15 15:08:49 -0700926 * index of -1000 implies allocations might succeed depending on
927 * watermarks
Mel Gorman3e7d3442011-01-13 15:45:56 -0800928 * index towards 0 implies failure is due to lack of memory
929 * index towards 1000 implies failure is due to fragmentation
930 *
931 * Only compact if a failure would be due to fragmentation.
932 */
933 fragindex = fragmentation_index(zone, order);
934 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
935 return COMPACT_SKIPPED;
936
Shaohua Lia582a732011-06-15 15:08:49 -0700937 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
938 0, 0))
Mel Gorman3e7d3442011-01-13 15:45:56 -0800939 return COMPACT_PARTIAL;
940
941 return COMPACT_CONTINUE;
942}
943
Mel Gorman748446b2010-05-24 14:32:27 -0700944static int compact_zone(struct zone *zone, struct compact_control *cc)
945{
946 int ret;
Mel Gormanf57bc462012-10-08 16:32:45 -0700947 unsigned long start_pfn = zone->zone_start_pfn;
948 unsigned long end_pfn = zone->zone_start_pfn + zone->spanned_pages;
Mel Gorman748446b2010-05-24 14:32:27 -0700949
Mel Gorman3e7d3442011-01-13 15:45:56 -0800950 ret = compaction_suitable(zone, cc->order);
951 switch (ret) {
952 case COMPACT_PARTIAL:
953 case COMPACT_SKIPPED:
954 /* Compaction is likely to fail */
955 return ret;
956 case COMPACT_CONTINUE:
957 /* Fall through to compaction */
958 ;
959 }
960
Mel Gormanf57bc462012-10-08 16:32:45 -0700961 /*
962 * Setup to move all movable pages to the end of the zone. Used cached
963 * information on where the scanners should start but check that it
964 * is initialised by ensuring the values are within zone boundaries.
965 */
966 cc->migrate_pfn = zone->compact_cached_migrate_pfn;
967 cc->free_pfn = zone->compact_cached_free_pfn;
968 if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
969 cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
970 zone->compact_cached_free_pfn = cc->free_pfn;
971 }
972 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
973 cc->migrate_pfn = start_pfn;
974 zone->compact_cached_migrate_pfn = cc->migrate_pfn;
975 }
Mel Gorman748446b2010-05-24 14:32:27 -0700976
Mel Gormanb71127a2012-10-08 16:32:47 -0700977 /*
978 * Clear pageblock skip if there were failures recently and compaction
979 * is about to be retried after being deferred. kswapd does not do
980 * this reset as it'll reset the cached information when going to sleep.
981 */
982 if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
983 __reset_isolation_suitable(zone);
Mel Gormancc5a88a2012-10-08 16:32:41 -0700984
Mel Gorman748446b2010-05-24 14:32:27 -0700985 migrate_prep_local();
986
987 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
988 unsigned long nr_migrate, nr_remaining;
Minchan Kim9d502c12011-03-22 16:30:39 -0700989 int err;
Mel Gorman748446b2010-05-24 14:32:27 -0700990
Mel Gormanf9e35b32011-06-15 15:08:52 -0700991 switch (isolate_migratepages(zone, cc)) {
992 case ISOLATE_ABORT:
993 ret = COMPACT_PARTIAL;
Shaohua Li03dd8fe2012-10-08 16:32:27 -0700994 putback_lru_pages(&cc->migratepages);
995 cc->nr_migratepages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700996 goto out;
997 case ISOLATE_NONE:
Mel Gorman748446b2010-05-24 14:32:27 -0700998 continue;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700999 case ISOLATE_SUCCESS:
1000 ;
1001 }
Mel Gorman748446b2010-05-24 14:32:27 -07001002
1003 nr_migrate = cc->nr_migratepages;
Minchan Kim9d502c12011-03-22 16:30:39 -07001004 err = migrate_pages(&cc->migratepages, compaction_alloc,
Mel Gorman7f0f2492011-01-13 15:45:58 -08001005 (unsigned long)cc, false,
Mel Gormana6bc32b2012-01-12 17:19:43 -08001006 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
Mel Gorman748446b2010-05-24 14:32:27 -07001007 update_nr_listpages(cc);
1008 nr_remaining = cc->nr_migratepages;
1009
Mel Gormanb7aba692011-01-13 15:45:54 -08001010 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
1011 nr_remaining);
Mel Gorman748446b2010-05-24 14:32:27 -07001012
1013 /* Release LRU pages not migrated */
Minchan Kim9d502c12011-03-22 16:30:39 -07001014 if (err) {
Mel Gorman748446b2010-05-24 14:32:27 -07001015 putback_lru_pages(&cc->migratepages);
1016 cc->nr_migratepages = 0;
David Rientjes7a08b442012-07-11 14:02:13 -07001017 if (err == -ENOMEM) {
1018 ret = COMPACT_PARTIAL;
1019 goto out;
1020 }
Mel Gorman748446b2010-05-24 14:32:27 -07001021 }
Mel Gormanc74068b2012-10-08 16:29:12 -07001022
1023 /* Capture a page now if it is a suitable size */
1024 compact_capture_page(cc);
Mel Gorman748446b2010-05-24 14:32:27 -07001025 }
1026
Mel Gormanf9e35b32011-06-15 15:08:52 -07001027out:
Mel Gorman748446b2010-05-24 14:32:27 -07001028 /* Release free pages and check accounting */
1029 cc->nr_freepages -= release_freepages(&cc->freepages);
1030 VM_BUG_ON(cc->nr_freepages != 0);
1031
1032 return ret;
1033}
Mel Gorman76ab0f52010-05-24 14:32:28 -07001034
Kyungmin Parkd43a87e2011-10-31 17:09:08 -07001035static unsigned long compact_zone_order(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -08001036 int order, gfp_t gfp_mask,
Mel Gormanc74068b2012-10-08 16:29:12 -07001037 bool sync, bool *contended,
1038 struct page **page)
Mel Gorman56de7262010-05-24 14:32:30 -07001039{
Shaohua Li03dd8fe2012-10-08 16:32:27 -07001040 unsigned long ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001041 struct compact_control cc = {
1042 .nr_freepages = 0,
1043 .nr_migratepages = 0,
1044 .order = order,
1045 .migratetype = allocflags_to_migratetype(gfp_mask),
1046 .zone = zone,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001047 .sync = sync,
Mel Gormanc74068b2012-10-08 16:29:12 -07001048 .page = page,
Mel Gorman56de7262010-05-24 14:32:30 -07001049 };
1050 INIT_LIST_HEAD(&cc.freepages);
1051 INIT_LIST_HEAD(&cc.migratepages);
1052
Shaohua Li03dd8fe2012-10-08 16:32:27 -07001053 ret = compact_zone(zone, &cc);
1054
1055 VM_BUG_ON(!list_empty(&cc.freepages));
1056 VM_BUG_ON(!list_empty(&cc.migratepages));
1057
1058 *contended = cc.contended;
1059 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001060}
1061
Mel Gorman5e771902010-05-24 14:32:31 -07001062int sysctl_extfrag_threshold = 500;
1063
Mel Gorman56de7262010-05-24 14:32:30 -07001064/**
1065 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
1066 * @zonelist: The zonelist used for the current allocation
1067 * @order: The order of the current allocation
1068 * @gfp_mask: The GFP mask of the current allocation
1069 * @nodemask: The allowed nodes to allocate from
Mel Gorman77f1fe62011-01-13 15:45:57 -08001070 * @sync: Whether migration is synchronous or not
Mel Gorman8a063192012-10-08 16:32:31 -07001071 * @contended: Return value that is true if compaction was aborted due to lock contention
1072 * @page: Optionally capture a free page of the requested order during compaction
Mel Gorman56de7262010-05-24 14:32:30 -07001073 *
1074 * This is the main entry point for direct page compaction.
1075 */
1076unsigned long try_to_compact_pages(struct zonelist *zonelist,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001077 int order, gfp_t gfp_mask, nodemask_t *nodemask,
Mel Gormanc74068b2012-10-08 16:29:12 -07001078 bool sync, bool *contended, struct page **page)
Mel Gorman56de7262010-05-24 14:32:30 -07001079{
1080 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
1081 int may_enter_fs = gfp_mask & __GFP_FS;
1082 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -07001083 struct zoneref *z;
1084 struct zone *zone;
1085 int rc = COMPACT_SKIPPED;
Bartlomiej Zolnierkiewicz850bf192012-10-08 16:32:05 -07001086 int alloc_flags = 0;
Mel Gorman56de7262010-05-24 14:32:30 -07001087
Mel Gormanbc337a92012-10-08 16:29:09 -07001088 /* Check if the GFP flags allow compaction */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -08001089 if (!order || !may_enter_fs || !may_perform_io)
Mel Gorman56de7262010-05-24 14:32:30 -07001090 return rc;
1091
1092 count_vm_event(COMPACTSTALL);
1093
Bartlomiej Zolnierkiewicz850bf192012-10-08 16:32:05 -07001094#ifdef CONFIG_CMA
1095 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
1096 alloc_flags |= ALLOC_CMA;
1097#endif
Mel Gorman56de7262010-05-24 14:32:30 -07001098 /* Compact each zone in the list */
1099 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
1100 nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -07001101 int status;
1102
Mel Gorman0ba33882012-08-21 16:16:17 -07001103 status = compact_zone_order(zone, order, gfp_mask, sync,
Mel Gormanc74068b2012-10-08 16:29:12 -07001104 contended, page);
Mel Gorman56de7262010-05-24 14:32:30 -07001105 rc = max(status, rc);
1106
Mel Gorman3e7d3442011-01-13 15:45:56 -08001107 /* If a normal allocation would succeed, stop compacting */
Bartlomiej Zolnierkiewicz850bf192012-10-08 16:32:05 -07001108 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
1109 alloc_flags))
Mel Gorman56de7262010-05-24 14:32:30 -07001110 break;
1111 }
1112
1113 return rc;
1114}
1115
1116
Mel Gorman76ab0f52010-05-24 14:32:28 -07001117/* Compact all zones within a node */
Rik van Riel7be62de2012-03-21 16:33:52 -07001118static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001119{
1120 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -07001121 struct zone *zone;
1122
Mel Gorman76ab0f52010-05-24 14:32:28 -07001123 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -07001124
1125 zone = &pgdat->node_zones[zoneid];
1126 if (!populated_zone(zone))
1127 continue;
1128
Rik van Riel7be62de2012-03-21 16:33:52 -07001129 cc->nr_freepages = 0;
1130 cc->nr_migratepages = 0;
1131 cc->zone = zone;
1132 INIT_LIST_HEAD(&cc->freepages);
1133 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001134
Dan Carpenteraad6ec32012-03-21 16:33:54 -07001135 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -07001136 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001137
Rik van Rielaff62242012-03-21 16:33:52 -07001138 if (cc->order > 0) {
1139 int ok = zone_watermark_ok(zone, cc->order,
1140 low_wmark_pages(zone), 0, 0);
Minchan Kim57bb21c2012-08-21 16:16:03 -07001141 if (ok && cc->order >= zone->compact_order_failed)
Rik van Rielaff62242012-03-21 16:33:52 -07001142 zone->compact_order_failed = cc->order + 1;
1143 /* Currently async compaction is never deferred. */
1144 else if (!ok && cc->sync)
1145 defer_compaction(zone, cc->order);
1146 }
1147
Rik van Riel7be62de2012-03-21 16:33:52 -07001148 VM_BUG_ON(!list_empty(&cc->freepages));
1149 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -07001150 }
1151
1152 return 0;
1153}
1154
Rik van Riel7be62de2012-03-21 16:33:52 -07001155int compact_pgdat(pg_data_t *pgdat, int order)
1156{
1157 struct compact_control cc = {
1158 .order = order,
1159 .sync = false,
Mel Gormanc74068b2012-10-08 16:29:12 -07001160 .page = NULL,
Rik van Riel7be62de2012-03-21 16:33:52 -07001161 };
1162
1163 return __compact_pgdat(pgdat, &cc);
1164}
1165
1166static int compact_node(int nid)
1167{
Rik van Riel7be62de2012-03-21 16:33:52 -07001168 struct compact_control cc = {
1169 .order = -1,
1170 .sync = true,
Mel Gormanc74068b2012-10-08 16:29:12 -07001171 .page = NULL,
Rik van Riel7be62de2012-03-21 16:33:52 -07001172 };
1173
Hugh Dickins8575ec22012-03-21 16:33:53 -07001174 return __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001175}
1176
Mel Gorman76ab0f52010-05-24 14:32:28 -07001177/* Compact all nodes in the system */
Jason Liuc0b96522013-01-11 14:31:47 -08001178static void compact_nodes(void)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001179{
1180 int nid;
1181
Hugh Dickins8575ec22012-03-21 16:33:53 -07001182 /* Flush pending updates to the LRU lists */
1183 lru_add_drain_all();
1184
Mel Gorman76ab0f52010-05-24 14:32:28 -07001185 for_each_online_node(nid)
1186 compact_node(nid);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001187}
1188
1189/* The written value is actually unused, all memory is compacted */
1190int sysctl_compact_memory;
1191
1192/* This is the entry point for compacting all nodes via /proc/sys/vm */
1193int sysctl_compaction_handler(struct ctl_table *table, int write,
1194 void __user *buffer, size_t *length, loff_t *ppos)
1195{
1196 if (write)
Jason Liuc0b96522013-01-11 14:31:47 -08001197 compact_nodes();
Mel Gorman76ab0f52010-05-24 14:32:28 -07001198
1199 return 0;
1200}
Mel Gormaned4a6d72010-05-24 14:32:29 -07001201
Mel Gorman5e771902010-05-24 14:32:31 -07001202int sysctl_extfrag_handler(struct ctl_table *table, int write,
1203 void __user *buffer, size_t *length, loff_t *ppos)
1204{
1205 proc_dointvec_minmax(table, write, buffer, length, ppos);
1206
1207 return 0;
1208}
1209
Mel Gormaned4a6d72010-05-24 14:32:29 -07001210#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Kay Sievers10fbcf42011-12-21 14:48:43 -08001211ssize_t sysfs_compact_node(struct device *dev,
1212 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -07001213 const char *buf, size_t count)
1214{
Hugh Dickins8575ec22012-03-21 16:33:53 -07001215 int nid = dev->id;
1216
1217 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1218 /* Flush pending updates to the LRU lists */
1219 lru_add_drain_all();
1220
1221 compact_node(nid);
1222 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07001223
1224 return count;
1225}
Kay Sievers10fbcf42011-12-21 14:48:43 -08001226static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001227
1228int compaction_register_node(struct node *node)
1229{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001230 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001231}
1232
1233void compaction_unregister_node(struct node *node)
1234{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001235 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001236}
1237#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +01001238
1239#endif /* CONFIG_COMPACTION */