blob: 195e9be458b46448c44428de74b0bd9f8c55d825 [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 Gorman6b506642012-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 Gorman9317a2b2012-10-08 16:32:47 -070069static void __reset_isolation_suitable(struct zone *zone)
Mel Gorman6b506642012-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 Gormand58f1f22012-10-08 16:32:45 -070075 zone->compact_cached_migrate_pfn = start_pfn;
76 zone->compact_cached_free_pfn = end_pfn;
Mel Gorman9317a2b2012-10-08 16:32:47 -070077 zone->compact_blockskip_flush = false;
Mel Gorman6b506642012-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 Gorman9317a2b2012-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 Gorman6b506642012-10-08 16:32:41 -0700111/*
112 * If no pages were isolated then mark this pageblock to be skipped in the
Mel Gorman9317a2b2012-10-08 16:32:47 -0700113 * future. The information is later cleared by __reset_isolation_suitable().
Mel Gorman6b506642012-10-08 16:32:41 -0700114 */
Mel Gormand58f1f22012-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 Gorman6b506642012-10-08 16:32:41 -0700118{
Mel Gormand58f1f22012-10-08 16:32:45 -0700119 struct zone *zone = cc->zone;
Mel Gorman6b506642012-10-08 16:32:41 -0700120 if (!page)
121 return;
122
Mel Gormand58f1f22012-10-08 16:32:45 -0700123 if (!nr_isolated) {
124 unsigned long pfn = page_to_pfn(page);
Mel Gorman6b506642012-10-08 16:32:41 -0700125 set_pageblock_skip(page);
Mel Gormand58f1f22012-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 Gorman6b506642012-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 Gormand58f1f22012-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 Gorman6b506642012-10-08 16:32:41 -0700149{
150}
151#endif /* CONFIG_COMPACTION */
152
Mel Gorman79f6ed52012-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 Gorman27d1af82012-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 Gorman79f6ed52012-10-08 16:32:33 -0700170 if (should_release_lock(lock)) {
Mel Gorman27d1af82012-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 Li3cbf3512012-10-08 16:32:27 -0700178 cc->contended = true;
Mel Gorman27d1af82012-08-21 16:16:17 -0700179 return false;
180 }
181
182 cond_resched();
Mel Gorman27d1af82012-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 Gormanb35e2d72012-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 Gorman0a1104a2012-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 Gorman27d1af82012-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 Gormanb35e2d72012-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 Gorman6b506642012-10-08 16:32:41 -0700284 struct page *cursor, *valid_page = NULL;
Mel Gormanb35e2d72012-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 Gormanb35e2d72012-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 Gormanb35e2d72012-10-08 16:32:36 -0700297 if (!pfn_valid_within(blockpfn))
Mel Gorman748446b2010-05-24 14:32:27 -0700298 continue;
Mel Gorman6b506642012-10-08 16:32:41 -0700299 if (!valid_page)
300 valid_page = page;
Mel Gormanb35e2d72012-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 Gormanb35e2d72012-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 Gormanb35e2d72012-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 Gorman2a3232d2012-10-19 13:56:57 -0700349 if (strict && nr_strict_required > total_isolated)
Mel Gormanb35e2d72012-10-08 16:32:36 -0700350 total_isolated = 0;
351
352 if (locked)
353 spin_unlock_irqrestore(&cc->zone->lock, flags);
354
Mel Gorman6b506642012-10-08 16:32:41 -0700355 /* Update the pageblock-skip if the whole pageblock was scanned */
356 if (blockpfn == end_pfn)
Mel Gormand58f1f22012-10-08 16:32:45 -0700357 update_pageblock_skip(cc, valid_page, total_isolated, false);
Mel Gorman6b506642012-10-08 16:32:41 -0700358
Mel Gorman748446b2010-05-24 14:32:27 -0700359 return total_isolated;
360}
361
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100362/**
363 * isolate_freepages_range() - isolate free pages.
364 * @start_pfn: The first PFN to start isolating.
365 * @end_pfn: The one-past-last PFN.
366 *
367 * Non-free pages, invalid PFNs, or zone boundaries within the
368 * [start_pfn, end_pfn) range are considered errors, cause function to
369 * undo its actions and return zero.
370 *
371 * Otherwise, function returns one-past-the-last PFN of isolated page
372 * (which may be greater then end_pfn if end fell in a middle of
373 * a free page).
374 */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100375unsigned long
Mel Gorman6b506642012-10-08 16:32:41 -0700376isolate_freepages_range(struct compact_control *cc,
377 unsigned long start_pfn, unsigned long end_pfn)
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100378{
Mel Gormanb35e2d72012-10-08 16:32:36 -0700379 unsigned long isolated, pfn, block_end_pfn;
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100380 LIST_HEAD(freelist);
381
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100382 for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
Mel Gorman6b506642012-10-08 16:32:41 -0700383 if (!pfn_valid(pfn) || cc->zone != page_zone(pfn_to_page(pfn)))
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100384 break;
385
386 /*
387 * On subsequent iterations ALIGN() is actually not needed,
388 * but we keep it that we not to complicate the code.
389 */
390 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
391 block_end_pfn = min(block_end_pfn, end_pfn);
392
Mel Gorman6b506642012-10-08 16:32:41 -0700393 isolated = isolate_freepages_block(cc, pfn, block_end_pfn,
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100394 &freelist, true);
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100395
396 /*
397 * In strict mode, isolate_freepages_block() returns 0 if
398 * there are any holes in the block (ie. invalid PFNs or
399 * non-free pages).
400 */
401 if (!isolated)
402 break;
403
404 /*
405 * If we managed to isolate pages, it is always (1 << n) *
406 * pageblock_nr_pages for some non-negative n. (Max order
407 * page may span two pageblocks).
408 */
409 }
410
411 /* split_free_page does not map the pages */
412 map_pages(&freelist);
413
414 if (pfn < end_pfn) {
415 /* Loop terminated early, cleanup. */
416 release_freepages(&freelist);
417 return 0;
418 }
419
420 /* We don't use freelists for anything. */
421 return pfn;
422}
423
Mel Gorman748446b2010-05-24 14:32:27 -0700424/* Update the number of anon and file isolated pages in the zone */
Mel Gorman27d1af82012-08-21 16:16:17 -0700425static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700426{
427 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700428 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700429
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700430 list_for_each_entry(page, &cc->migratepages, lru)
431 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700432
Mel Gorman27d1af82012-08-21 16:16:17 -0700433 /* If locked we can use the interrupt unsafe versions */
434 if (locked) {
435 __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
436 __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
437 } else {
438 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
439 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
440 }
Mel Gorman748446b2010-05-24 14:32:27 -0700441}
442
443/* Similar to reclaim, but different enough that they don't share logic */
444static bool too_many_isolated(struct zone *zone)
445{
Minchan Kimbc693042010-09-09 16:38:00 -0700446 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700447
448 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
449 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700450 active = zone_page_state(zone, NR_ACTIVE_FILE) +
451 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700452 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
453 zone_page_state(zone, NR_ISOLATED_ANON);
454
Minchan Kimbc693042010-09-09 16:38:00 -0700455 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700456}
457
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100458/**
459 * isolate_migratepages_range() - isolate all migrate-able pages in range.
460 * @zone: Zone pages are in.
461 * @cc: Compaction control structure.
462 * @low_pfn: The first PFN of the range.
463 * @end_pfn: The one-past-the-last PFN of the range.
Minchan Kimd05b4522012-10-08 16:33:48 -0700464 * @unevictable: true if it allows to isolate unevictable pages
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100465 *
466 * Isolate all pages that can be migrated from the range specified by
467 * [low_pfn, end_pfn). Returns zero if there is a fatal signal
468 * pending), otherwise PFN of the first page that was not scanned
469 * (which may be both less, equal to or more then end_pfn).
470 *
471 * Assumes that cc->migratepages is empty and cc->nr_migratepages is
472 * zero.
473 *
474 * Apart from cc->migratepages and cc->nr_migratetypes this function
475 * does not modify any cc's fields, in particular it does not modify
476 * (or read for that matter) cc->migrate_pfn.
Mel Gorman748446b2010-05-24 14:32:27 -0700477 */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100478unsigned long
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100479isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
Minchan Kimd05b4522012-10-08 16:33:48 -0700480 unsigned long low_pfn, unsigned long end_pfn, bool unevictable)
Mel Gorman748446b2010-05-24 14:32:27 -0700481{
Mel Gorman9927af742011-01-13 15:45:59 -0800482 unsigned long last_pageblock_nr = 0, pageblock_nr;
Mel Gormanb7aba692011-01-13 15:45:54 -0800483 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700484 struct list_head *migratelist = &cc->migratepages;
Konstantin Khlebnikov6d8a50e2012-05-29 15:06:54 -0700485 isolate_mode_t mode = 0;
Mel Gorman27d1af82012-08-21 16:16:17 -0700486 unsigned long flags;
Mel Gorman79f6ed52012-10-08 16:32:33 -0700487 bool locked = false;
Mel Gorman6b506642012-10-08 16:32:41 -0700488 struct page *page = NULL, *valid_page = NULL;
Mel Gorman748446b2010-05-24 14:32:27 -0700489
Mel Gorman748446b2010-05-24 14:32:27 -0700490 /*
491 * Ensure that there are not too many pages isolated from the LRU
492 * list by either parallel reclaimers or compaction. If there are,
493 * delay for some time until fewer pages are isolated
494 */
495 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700496 /* async migration should just abort */
497 if (!cc->sync)
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100498 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700499
Mel Gorman748446b2010-05-24 14:32:27 -0700500 congestion_wait(BLK_RW_ASYNC, HZ/10);
501
502 if (fatal_signal_pending(current))
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100503 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700504 }
505
506 /* Time to isolate some pages for migration */
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700507 cond_resched();
Mel Gorman748446b2010-05-24 14:32:27 -0700508 for (; low_pfn < end_pfn; low_pfn++) {
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700509 /* give a chance to irqs before checking need_resched() */
Mel Gorman79f6ed52012-10-08 16:32:33 -0700510 if (locked && !((low_pfn+1) % SWAP_CLUSTER_MAX)) {
511 if (should_release_lock(&zone->lru_lock)) {
512 spin_unlock_irqrestore(&zone->lru_lock, flags);
513 locked = false;
514 }
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700515 }
Mel Gorman27d1af82012-08-21 16:16:17 -0700516
Mel Gorman0bf380b2012-02-03 15:37:18 -0800517 /*
518 * migrate_pfn does not necessarily start aligned to a
519 * pageblock. Ensure that pfn_valid is called when moving
520 * into a new MAX_ORDER_NR_PAGES range in case of large
521 * memory holes within the zone
522 */
523 if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
524 if (!pfn_valid(low_pfn)) {
525 low_pfn += MAX_ORDER_NR_PAGES - 1;
526 continue;
527 }
528 }
529
Mel Gorman748446b2010-05-24 14:32:27 -0700530 if (!pfn_valid_within(low_pfn))
531 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800532 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700533
Mel Gormandc908602012-02-08 17:13:38 -0800534 /*
535 * Get the page and ensure the page is within the same zone.
536 * See the comment in isolate_freepages about overlapping
537 * nodes. It is deliberate that the new zone lock is not taken
538 * as memory compaction should not move pages between nodes.
539 */
Mel Gorman748446b2010-05-24 14:32:27 -0700540 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800541 if (page_zone(page) != zone)
542 continue;
543
Mel Gorman6b506642012-10-08 16:32:41 -0700544 if (!valid_page)
545 valid_page = page;
546
547 /* If isolation recently failed, do not retry */
548 pageblock_nr = low_pfn >> pageblock_order;
549 if (!isolation_suitable(cc, page))
550 goto next_pageblock;
551
Mel Gormandc908602012-02-08 17:13:38 -0800552 /* Skip if free */
Mel Gorman748446b2010-05-24 14:32:27 -0700553 if (PageBuddy(page))
554 continue;
555
Mel Gorman9927af742011-01-13 15:45:59 -0800556 /*
557 * For async migration, also only scan in MOVABLE blocks. Async
558 * migration is optimistic to see if the minimum amount of work
559 * satisfies the allocation
560 */
Mel Gorman9927af742011-01-13 15:45:59 -0800561 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
Michal Nazarewiczd4158d22011-12-29 13:09:50 +0100562 !migrate_async_suitable(get_pageblock_migratetype(page))) {
Mel Gormand58f1f22012-10-08 16:32:45 -0700563 cc->finished_update_migrate = true;
Mel Gorman79f6ed52012-10-08 16:32:33 -0700564 goto next_pageblock;
Mel Gorman9927af742011-01-13 15:45:59 -0800565 }
566
Mel Gorman79f6ed52012-10-08 16:32:33 -0700567 /* Check may be lockless but that's ok as we recheck later */
Andrea Arcangelibc835012011-01-13 15:47:08 -0800568 if (!PageLRU(page))
569 continue;
570
571 /*
Mel Gorman79f6ed52012-10-08 16:32:33 -0700572 * PageLRU is set. lru_lock normally excludes isolation
573 * splitting and collapsing (collapsing has already happened
574 * if PageLRU is set) but the lock is not necessarily taken
575 * here and it is wasteful to take it just to check transhuge.
576 * Check TransHuge without lock and skip the whole pageblock if
577 * it's either a transhuge or hugetlbfs page, as calling
578 * compound_order() without preventing THP from splitting the
579 * page underneath us may return surprising results.
Andrea Arcangelibc835012011-01-13 15:47:08 -0800580 */
581 if (PageTransHuge(page)) {
Mel Gorman79f6ed52012-10-08 16:32:33 -0700582 if (!locked)
583 goto next_pageblock;
584 low_pfn += (1 << compound_order(page)) - 1;
585 continue;
586 }
587
588 /* Check if it is ok to still hold the lock */
589 locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
590 locked, cc);
591 if (!locked || fatal_signal_pending(current))
592 break;
593
594 /* Recheck PageLRU and PageTransHuge under lock */
595 if (!PageLRU(page))
596 continue;
597 if (PageTransHuge(page)) {
Andrea Arcangelibc835012011-01-13 15:47:08 -0800598 low_pfn += (1 << compound_order(page)) - 1;
599 continue;
600 }
601
Mel Gormanc8244932012-01-12 17:19:38 -0800602 if (!cc->sync)
603 mode |= ISOLATE_ASYNC_MIGRATE;
604
Minchan Kimd05b4522012-10-08 16:33:48 -0700605 if (unevictable)
606 mode |= ISOLATE_UNEVICTABLE;
607
Mel Gorman748446b2010-05-24 14:32:27 -0700608 /* Try isolate the page */
Konstantin Khlebnikov6d8a50e2012-05-29 15:06:54 -0700609 if (__isolate_lru_page(page, mode) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700610 continue;
611
Andrea Arcangelibc835012011-01-13 15:47:08 -0800612 VM_BUG_ON(PageTransCompound(page));
613
Mel Gorman748446b2010-05-24 14:32:27 -0700614 /* Successfully isolated */
Mel Gormand58f1f22012-10-08 16:32:45 -0700615 cc->finished_update_migrate = true;
Mel Gorman748446b2010-05-24 14:32:27 -0700616 del_page_from_lru_list(zone, page, page_lru(page));
617 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700618 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800619 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700620
621 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800622 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
623 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700624 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800625 }
Mel Gorman79f6ed52012-10-08 16:32:33 -0700626
627 continue;
628
629next_pageblock:
630 low_pfn += pageblock_nr_pages;
631 low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
632 last_pageblock_nr = pageblock_nr;
Mel Gorman748446b2010-05-24 14:32:27 -0700633 }
634
Mel Gorman27d1af82012-08-21 16:16:17 -0700635 acct_isolated(zone, locked, cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700636
Mel Gorman27d1af82012-08-21 16:16:17 -0700637 if (locked)
638 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700639
Mel Gorman6b506642012-10-08 16:32:41 -0700640 /* Update the pageblock-skip if the whole pageblock was scanned */
641 if (low_pfn == end_pfn)
Mel Gormand58f1f22012-10-08 16:32:45 -0700642 update_pageblock_skip(cc, valid_page, nr_isolated, true);
Mel Gorman6b506642012-10-08 16:32:41 -0700643
Mel Gormanb7aba692011-01-13 15:45:54 -0800644 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
645
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100646 return low_pfn;
647}
648
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100649#endif /* CONFIG_COMPACTION || CONFIG_CMA */
650#ifdef CONFIG_COMPACTION
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100651/*
652 * Based on information in the current compact_control, find blocks
653 * suitable for isolating free pages from and then isolate them.
654 */
655static void isolate_freepages(struct zone *zone,
656 struct compact_control *cc)
657{
658 struct page *page;
659 unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100660 int nr_freepages = cc->nr_freepages;
661 struct list_head *freelist = &cc->freepages;
662
663 /*
664 * Initialise the free scanner. The starting point is where we last
665 * scanned from (or the end of the zone if starting). The low point
666 * is the end of the pageblock the migration scanner is using.
667 */
668 pfn = cc->free_pfn;
669 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
670
671 /*
672 * Take care that if the migration scanner is at the end of the zone
673 * that the free scanner does not accidentally move to the next zone
674 * in the next isolation cycle.
675 */
676 high_pfn = min(low_pfn, pfn);
677
678 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
679
680 /*
681 * Isolate free pages until enough are available to migrate the
682 * pages on cc->migratepages. We stop searching if the migrate
683 * and free page scanners meet or enough free pages are isolated.
684 */
685 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
686 pfn -= pageblock_nr_pages) {
687 unsigned long isolated;
688
689 if (!pfn_valid(pfn))
690 continue;
691
692 /*
693 * Check for overlapping nodes/zones. It's possible on some
694 * configurations to have a setup like
695 * node0 node1 node0
696 * i.e. it's possible that all pages within a zones range of
697 * pages do not belong to a single zone.
698 */
699 page = pfn_to_page(pfn);
700 if (page_zone(page) != zone)
701 continue;
702
703 /* Check the block is suitable for migration */
704 if (!suitable_migration_target(page))
705 continue;
706
Mel Gorman6b506642012-10-08 16:32:41 -0700707 /* If isolation recently failed, do not retry */
708 if (!isolation_suitable(cc, page))
709 continue;
710
Mel Gormanb35e2d72012-10-08 16:32:36 -0700711 /* Found a block suitable for isolating free pages from */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100712 isolated = 0;
Mel Gormanb35e2d72012-10-08 16:32:36 -0700713 end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn);
714 isolated = isolate_freepages_block(cc, pfn, end_pfn,
715 freelist, false);
716 nr_freepages += isolated;
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100717
718 /*
719 * Record the highest PFN we isolated pages from. When next
720 * looking for free pages, the search will restart here as
721 * page migration may have returned some pages to the allocator
722 */
Mel Gormand58f1f22012-10-08 16:32:45 -0700723 if (isolated) {
724 cc->finished_update_free = true;
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100725 high_pfn = max(high_pfn, pfn);
Mel Gormand58f1f22012-10-08 16:32:45 -0700726 }
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100727 }
728
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100729 /* split_free_page does not map the pages */
730 map_pages(freelist);
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100731
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100732 cc->free_pfn = high_pfn;
733 cc->nr_freepages = nr_freepages;
Mel Gorman748446b2010-05-24 14:32:27 -0700734}
735
736/*
737 * This is a migrate-callback that "allocates" freepages by taking pages
738 * from the isolated freelists in the block we are migrating to.
739 */
740static struct page *compaction_alloc(struct page *migratepage,
741 unsigned long data,
742 int **result)
743{
744 struct compact_control *cc = (struct compact_control *)data;
745 struct page *freepage;
746
747 /* Isolate free pages if necessary */
748 if (list_empty(&cc->freepages)) {
749 isolate_freepages(cc->zone, cc);
750
751 if (list_empty(&cc->freepages))
752 return NULL;
753 }
754
755 freepage = list_entry(cc->freepages.next, struct page, lru);
756 list_del(&freepage->lru);
757 cc->nr_freepages--;
758
759 return freepage;
760}
761
762/*
763 * We cannot control nr_migratepages and nr_freepages fully when migration is
764 * running as migrate_pages() has no knowledge of compact_control. When
765 * migration is complete, we count the number of pages on the lists by hand.
766 */
767static void update_nr_listpages(struct compact_control *cc)
768{
769 int nr_migratepages = 0;
770 int nr_freepages = 0;
771 struct page *page;
772
773 list_for_each_entry(page, &cc->migratepages, lru)
774 nr_migratepages++;
775 list_for_each_entry(page, &cc->freepages, lru)
776 nr_freepages++;
777
778 cc->nr_migratepages = nr_migratepages;
779 cc->nr_freepages = nr_freepages;
780}
781
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100782/* possible outcome of isolate_migratepages */
783typedef enum {
784 ISOLATE_ABORT, /* Abort compaction now */
785 ISOLATE_NONE, /* No pages isolated, continue scanning */
786 ISOLATE_SUCCESS, /* Pages isolated, migrate */
787} isolate_migrate_t;
788
789/*
790 * Isolate all pages that can be migrated from the block pointed to by
791 * the migrate scanner within compact_control.
792 */
793static isolate_migrate_t isolate_migratepages(struct zone *zone,
794 struct compact_control *cc)
795{
796 unsigned long low_pfn, end_pfn;
797
798 /* Do not scan outside zone boundaries */
799 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
800
801 /* Only scan within a pageblock boundary */
802 end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
803
804 /* Do not cross the free scanner or scan within a memory hole */
805 if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
806 cc->migrate_pfn = end_pfn;
807 return ISOLATE_NONE;
808 }
809
810 /* Perform the isolation */
Minchan Kimd05b4522012-10-08 16:33:48 -0700811 low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn, false);
Shaohua Li3cbf3512012-10-08 16:32:27 -0700812 if (!low_pfn || cc->contended)
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100813 return ISOLATE_ABORT;
814
815 cc->migrate_pfn = low_pfn;
816
817 return ISOLATE_SUCCESS;
818}
819
Mel Gorman748446b2010-05-24 14:32:27 -0700820static int compact_finished(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800821 struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700822{
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800823 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -0700824
Mel Gorman748446b2010-05-24 14:32:27 -0700825 if (fatal_signal_pending(current))
826 return COMPACT_PARTIAL;
827
828 /* Compaction run completes if the migrate and free scanner meet */
Mel Gorman6b506642012-10-08 16:32:41 -0700829 if (cc->free_pfn <= cc->migrate_pfn) {
Mel Gorman9317a2b2012-10-08 16:32:47 -0700830 /*
831 * Mark that the PG_migrate_skip information should be cleared
832 * by kswapd when it goes to sleep. kswapd does not set the
833 * flag itself as the decision to be clear should be directly
834 * based on an allocation request.
835 */
836 if (!current_is_kswapd())
837 zone->compact_blockskip_flush = true;
838
Mel Gorman748446b2010-05-24 14:32:27 -0700839 return COMPACT_COMPLETE;
Mel Gorman6b506642012-10-08 16:32:41 -0700840 }
Mel Gorman748446b2010-05-24 14:32:27 -0700841
Johannes Weiner82478fb2011-01-20 14:44:21 -0800842 /*
843 * order == -1 is expected when compacting via
844 * /proc/sys/vm/compact_memory
845 */
Mel Gorman56de7262010-05-24 14:32:30 -0700846 if (cc->order == -1)
847 return COMPACT_CONTINUE;
848
Michal Hocko3957c772011-06-15 15:08:25 -0700849 /* Compaction run is not finished if the watermark is not met */
850 watermark = low_wmark_pages(zone);
851 watermark += (1 << cc->order);
852
853 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
854 return COMPACT_CONTINUE;
855
Mel Gorman56de7262010-05-24 14:32:30 -0700856 /* Direct compactor: Is a suitable page free? */
Mel Gorman0a1104a2012-10-08 16:29:12 -0700857 if (cc->page) {
858 /* Was a suitable page captured? */
859 if (*cc->page)
Mel Gorman56de7262010-05-24 14:32:30 -0700860 return COMPACT_PARTIAL;
Mel Gorman0a1104a2012-10-08 16:29:12 -0700861 } else {
862 unsigned int order;
863 for (order = cc->order; order < MAX_ORDER; order++) {
864 struct free_area *area = &zone->free_area[cc->order];
865 /* Job done if page is free of the right migratetype */
866 if (!list_empty(&area->free_list[cc->migratetype]))
867 return COMPACT_PARTIAL;
Mel Gorman56de7262010-05-24 14:32:30 -0700868
Mel Gorman0a1104a2012-10-08 16:29:12 -0700869 /* Job done if allocation would set block type */
870 if (cc->order >= pageblock_order && area->nr_free)
871 return COMPACT_PARTIAL;
872 }
Mel Gorman56de7262010-05-24 14:32:30 -0700873 }
874
Mel Gorman748446b2010-05-24 14:32:27 -0700875 return COMPACT_CONTINUE;
876}
877
Mel Gorman3e7d3442011-01-13 15:45:56 -0800878/*
879 * compaction_suitable: Is this suitable to run compaction on this zone now?
880 * Returns
881 * COMPACT_SKIPPED - If there are too few free pages for compaction
882 * COMPACT_PARTIAL - If the allocation would succeed without compaction
883 * COMPACT_CONTINUE - If compaction should run now
884 */
885unsigned long compaction_suitable(struct zone *zone, int order)
886{
887 int fragindex;
888 unsigned long watermark;
889
890 /*
Michal Hocko3957c772011-06-15 15:08:25 -0700891 * order == -1 is expected when compacting via
892 * /proc/sys/vm/compact_memory
893 */
894 if (order == -1)
895 return COMPACT_CONTINUE;
896
897 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -0800898 * Watermarks for order-0 must be met for compaction. Note the 2UL.
899 * This is because during migration, copies of pages need to be
900 * allocated and for a short time, the footprint is higher
901 */
902 watermark = low_wmark_pages(zone) + (2UL << order);
903 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
904 return COMPACT_SKIPPED;
905
906 /*
907 * fragmentation index determines if allocation failures are due to
908 * low memory or external fragmentation
909 *
Shaohua Lia582a732011-06-15 15:08:49 -0700910 * index of -1000 implies allocations might succeed depending on
911 * watermarks
Mel Gorman3e7d3442011-01-13 15:45:56 -0800912 * index towards 0 implies failure is due to lack of memory
913 * index towards 1000 implies failure is due to fragmentation
914 *
915 * Only compact if a failure would be due to fragmentation.
916 */
917 fragindex = fragmentation_index(zone, order);
918 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
919 return COMPACT_SKIPPED;
920
Shaohua Lia582a732011-06-15 15:08:49 -0700921 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
922 0, 0))
Mel Gorman3e7d3442011-01-13 15:45:56 -0800923 return COMPACT_PARTIAL;
924
925 return COMPACT_CONTINUE;
926}
927
Mel Gorman748446b2010-05-24 14:32:27 -0700928static int compact_zone(struct zone *zone, struct compact_control *cc)
929{
930 int ret;
Mel Gormand58f1f22012-10-08 16:32:45 -0700931 unsigned long start_pfn = zone->zone_start_pfn;
932 unsigned long end_pfn = zone->zone_start_pfn + zone->spanned_pages;
Mel Gorman748446b2010-05-24 14:32:27 -0700933
Mel Gorman3e7d3442011-01-13 15:45:56 -0800934 ret = compaction_suitable(zone, cc->order);
935 switch (ret) {
936 case COMPACT_PARTIAL:
937 case COMPACT_SKIPPED:
938 /* Compaction is likely to fail */
939 return ret;
940 case COMPACT_CONTINUE:
941 /* Fall through to compaction */
942 ;
943 }
944
Mel Gormand58f1f22012-10-08 16:32:45 -0700945 /*
946 * Setup to move all movable pages to the end of the zone. Used cached
947 * information on where the scanners should start but check that it
948 * is initialised by ensuring the values are within zone boundaries.
949 */
950 cc->migrate_pfn = zone->compact_cached_migrate_pfn;
951 cc->free_pfn = zone->compact_cached_free_pfn;
952 if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
953 cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
954 zone->compact_cached_free_pfn = cc->free_pfn;
955 }
956 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
957 cc->migrate_pfn = start_pfn;
958 zone->compact_cached_migrate_pfn = cc->migrate_pfn;
959 }
Mel Gorman748446b2010-05-24 14:32:27 -0700960
Mel Gorman9317a2b2012-10-08 16:32:47 -0700961 /*
962 * Clear pageblock skip if there were failures recently and compaction
963 * is about to be retried after being deferred. kswapd does not do
964 * this reset as it'll reset the cached information when going to sleep.
965 */
966 if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
967 __reset_isolation_suitable(zone);
Mel Gorman6b506642012-10-08 16:32:41 -0700968
Mel Gorman748446b2010-05-24 14:32:27 -0700969 migrate_prep_local();
970
971 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
972 unsigned long nr_migrate, nr_remaining;
Minchan Kim9d502c12011-03-22 16:30:39 -0700973 int err;
Mel Gorman748446b2010-05-24 14:32:27 -0700974
Mel Gormanf9e35b32011-06-15 15:08:52 -0700975 switch (isolate_migratepages(zone, cc)) {
976 case ISOLATE_ABORT:
977 ret = COMPACT_PARTIAL;
Shaohua Li3cbf3512012-10-08 16:32:27 -0700978 putback_lru_pages(&cc->migratepages);
979 cc->nr_migratepages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700980 goto out;
981 case ISOLATE_NONE:
Mel Gorman748446b2010-05-24 14:32:27 -0700982 continue;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700983 case ISOLATE_SUCCESS:
984 ;
985 }
Mel Gorman748446b2010-05-24 14:32:27 -0700986
987 nr_migrate = cc->nr_migratepages;
Minchan Kim9d502c12011-03-22 16:30:39 -0700988 err = migrate_pages(&cc->migratepages, compaction_alloc,
Mel Gorman7f0f2492011-01-13 15:45:58 -0800989 (unsigned long)cc, false,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800990 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
Mel Gorman748446b2010-05-24 14:32:27 -0700991 update_nr_listpages(cc);
992 nr_remaining = cc->nr_migratepages;
993
994 count_vm_event(COMPACTBLOCKS);
995 count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
996 if (nr_remaining)
997 count_vm_events(COMPACTPAGEFAILED, nr_remaining);
Mel Gormanb7aba692011-01-13 15:45:54 -0800998 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
999 nr_remaining);
Mel Gorman748446b2010-05-24 14:32:27 -07001000
1001 /* Release LRU pages not migrated */
Minchan Kim9d502c12011-03-22 16:30:39 -07001002 if (err) {
Mel Gorman748446b2010-05-24 14:32:27 -07001003 putback_lru_pages(&cc->migratepages);
1004 cc->nr_migratepages = 0;
David Rientjesa56063e2012-07-11 14:02:13 -07001005 if (err == -ENOMEM) {
1006 ret = COMPACT_PARTIAL;
1007 goto out;
1008 }
Mel Gorman748446b2010-05-24 14:32:27 -07001009 }
Mel Gorman0a1104a2012-10-08 16:29:12 -07001010
1011 /* Capture a page now if it is a suitable size */
1012 compact_capture_page(cc);
Mel Gorman748446b2010-05-24 14:32:27 -07001013 }
1014
Mel Gormanf9e35b32011-06-15 15:08:52 -07001015out:
Mel Gorman748446b2010-05-24 14:32:27 -07001016 /* Release free pages and check accounting */
1017 cc->nr_freepages -= release_freepages(&cc->freepages);
1018 VM_BUG_ON(cc->nr_freepages != 0);
1019
1020 return ret;
1021}
Mel Gorman76ab0f52010-05-24 14:32:28 -07001022
Kyungmin Parkd43a87e2011-10-31 17:09:08 -07001023static unsigned long compact_zone_order(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -08001024 int order, gfp_t gfp_mask,
Mel Gorman0a1104a2012-10-08 16:29:12 -07001025 bool sync, bool *contended,
1026 struct page **page)
Mel Gorman56de7262010-05-24 14:32:30 -07001027{
Shaohua Li3cbf3512012-10-08 16:32:27 -07001028 unsigned long ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001029 struct compact_control cc = {
1030 .nr_freepages = 0,
1031 .nr_migratepages = 0,
1032 .order = order,
1033 .migratetype = allocflags_to_migratetype(gfp_mask),
1034 .zone = zone,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001035 .sync = sync,
Mel Gorman0a1104a2012-10-08 16:29:12 -07001036 .page = page,
Mel Gorman56de7262010-05-24 14:32:30 -07001037 };
1038 INIT_LIST_HEAD(&cc.freepages);
1039 INIT_LIST_HEAD(&cc.migratepages);
1040
Shaohua Li3cbf3512012-10-08 16:32:27 -07001041 ret = compact_zone(zone, &cc);
1042
1043 VM_BUG_ON(!list_empty(&cc.freepages));
1044 VM_BUG_ON(!list_empty(&cc.migratepages));
1045
1046 *contended = cc.contended;
1047 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001048}
1049
Mel Gorman5e771902010-05-24 14:32:31 -07001050int sysctl_extfrag_threshold = 500;
1051
Mel Gorman56de7262010-05-24 14:32:30 -07001052/**
1053 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
1054 * @zonelist: The zonelist used for the current allocation
1055 * @order: The order of the current allocation
1056 * @gfp_mask: The GFP mask of the current allocation
1057 * @nodemask: The allowed nodes to allocate from
Mel Gorman77f1fe62011-01-13 15:45:57 -08001058 * @sync: Whether migration is synchronous or not
Mel Gorman5e663dc2012-10-08 16:32:31 -07001059 * @contended: Return value that is true if compaction was aborted due to lock contention
1060 * @page: Optionally capture a free page of the requested order during compaction
Mel Gorman56de7262010-05-24 14:32:30 -07001061 *
1062 * This is the main entry point for direct page compaction.
1063 */
1064unsigned long try_to_compact_pages(struct zonelist *zonelist,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001065 int order, gfp_t gfp_mask, nodemask_t *nodemask,
Mel Gorman0a1104a2012-10-08 16:29:12 -07001066 bool sync, bool *contended, struct page **page)
Mel Gorman56de7262010-05-24 14:32:30 -07001067{
1068 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
1069 int may_enter_fs = gfp_mask & __GFP_FS;
1070 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -07001071 struct zoneref *z;
1072 struct zone *zone;
1073 int rc = COMPACT_SKIPPED;
Bartlomiej Zolnierkiewicz19644312012-10-08 16:32:05 -07001074 int alloc_flags = 0;
Mel Gorman56de7262010-05-24 14:32:30 -07001075
Mel Gorman05fd3fd2012-10-08 16:29:09 -07001076 /* Check if the GFP flags allow compaction */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -08001077 if (!order || !may_enter_fs || !may_perform_io)
Mel Gorman56de7262010-05-24 14:32:30 -07001078 return rc;
1079
1080 count_vm_event(COMPACTSTALL);
1081
Bartlomiej Zolnierkiewicz19644312012-10-08 16:32:05 -07001082#ifdef CONFIG_CMA
1083 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
1084 alloc_flags |= ALLOC_CMA;
1085#endif
Mel Gorman56de7262010-05-24 14:32:30 -07001086 /* Compact each zone in the list */
1087 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
1088 nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -07001089 int status;
1090
Mel Gorman27d1af82012-08-21 16:16:17 -07001091 status = compact_zone_order(zone, order, gfp_mask, sync,
Mel Gorman0a1104a2012-10-08 16:29:12 -07001092 contended, page);
Mel Gorman56de7262010-05-24 14:32:30 -07001093 rc = max(status, rc);
1094
Mel Gorman3e7d3442011-01-13 15:45:56 -08001095 /* If a normal allocation would succeed, stop compacting */
Bartlomiej Zolnierkiewicz19644312012-10-08 16:32:05 -07001096 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
1097 alloc_flags))
Mel Gorman56de7262010-05-24 14:32:30 -07001098 break;
1099 }
1100
1101 return rc;
1102}
1103
1104
Mel Gorman76ab0f52010-05-24 14:32:28 -07001105/* Compact all zones within a node */
Rik van Riel7be62de2012-03-21 16:33:52 -07001106static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001107{
1108 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -07001109 struct zone *zone;
1110
Mel Gorman76ab0f52010-05-24 14:32:28 -07001111 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -07001112
1113 zone = &pgdat->node_zones[zoneid];
1114 if (!populated_zone(zone))
1115 continue;
1116
Rik van Riel7be62de2012-03-21 16:33:52 -07001117 cc->nr_freepages = 0;
1118 cc->nr_migratepages = 0;
1119 cc->zone = zone;
1120 INIT_LIST_HEAD(&cc->freepages);
1121 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001122
Dan Carpenteraad6ec32012-03-21 16:33:54 -07001123 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -07001124 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001125
Rik van Rielaff62242012-03-21 16:33:52 -07001126 if (cc->order > 0) {
1127 int ok = zone_watermark_ok(zone, cc->order,
1128 low_wmark_pages(zone), 0, 0);
Minchan Kimfc83d282012-08-21 16:16:03 -07001129 if (ok && cc->order >= zone->compact_order_failed)
Rik van Rielaff62242012-03-21 16:33:52 -07001130 zone->compact_order_failed = cc->order + 1;
1131 /* Currently async compaction is never deferred. */
1132 else if (!ok && cc->sync)
1133 defer_compaction(zone, cc->order);
1134 }
1135
Rik van Riel7be62de2012-03-21 16:33:52 -07001136 VM_BUG_ON(!list_empty(&cc->freepages));
1137 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -07001138 }
1139
1140 return 0;
1141}
1142
Rik van Riel7be62de2012-03-21 16:33:52 -07001143int compact_pgdat(pg_data_t *pgdat, int order)
1144{
1145 struct compact_control cc = {
1146 .order = order,
1147 .sync = false,
Mel Gorman0a1104a2012-10-08 16:29:12 -07001148 .page = NULL,
Rik van Riel7be62de2012-03-21 16:33:52 -07001149 };
1150
1151 return __compact_pgdat(pgdat, &cc);
1152}
1153
1154static int compact_node(int nid)
1155{
Rik van Riel7be62de2012-03-21 16:33:52 -07001156 struct compact_control cc = {
1157 .order = -1,
1158 .sync = true,
Mel Gorman0a1104a2012-10-08 16:29:12 -07001159 .page = NULL,
Rik van Riel7be62de2012-03-21 16:33:52 -07001160 };
1161
Hugh Dickins8575ec22012-03-21 16:33:53 -07001162 return __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001163}
1164
Mel Gorman76ab0f52010-05-24 14:32:28 -07001165/* Compact all nodes in the system */
1166static int compact_nodes(void)
1167{
1168 int nid;
1169
Hugh Dickins8575ec22012-03-21 16:33:53 -07001170 /* Flush pending updates to the LRU lists */
1171 lru_add_drain_all();
1172
Mel Gorman76ab0f52010-05-24 14:32:28 -07001173 for_each_online_node(nid)
1174 compact_node(nid);
1175
1176 return COMPACT_COMPLETE;
1177}
1178
1179/* The written value is actually unused, all memory is compacted */
1180int sysctl_compact_memory;
1181
1182/* This is the entry point for compacting all nodes via /proc/sys/vm */
1183int sysctl_compaction_handler(struct ctl_table *table, int write,
1184 void __user *buffer, size_t *length, loff_t *ppos)
1185{
1186 if (write)
1187 return compact_nodes();
1188
1189 return 0;
1190}
Mel Gormaned4a6d72010-05-24 14:32:29 -07001191
Mel Gorman5e771902010-05-24 14:32:31 -07001192int sysctl_extfrag_handler(struct ctl_table *table, int write,
1193 void __user *buffer, size_t *length, loff_t *ppos)
1194{
1195 proc_dointvec_minmax(table, write, buffer, length, ppos);
1196
1197 return 0;
1198}
1199
Mel Gormaned4a6d72010-05-24 14:32:29 -07001200#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Kay Sievers10fbcf42011-12-21 14:48:43 -08001201ssize_t sysfs_compact_node(struct device *dev,
1202 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -07001203 const char *buf, size_t count)
1204{
Hugh Dickins8575ec22012-03-21 16:33:53 -07001205 int nid = dev->id;
1206
1207 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1208 /* Flush pending updates to the LRU lists */
1209 lru_add_drain_all();
1210
1211 compact_node(nid);
1212 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07001213
1214 return count;
1215}
Kay Sievers10fbcf42011-12-21 14:48:43 -08001216static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001217
1218int compaction_register_node(struct node *node)
1219{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001220 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001221}
1222
1223void compaction_unregister_node(struct node *node)
1224{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001225 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001226}
1227#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +01001228
1229#endif /* CONFIG_COMPACTION */