blob: 694eaabaaebdc0827c93d81c97f200fc534a115e [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 Nazarewiczff9543f2011-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 Nazarewiczff9543f2011-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 Nazarewicz47118af2011-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 Gormanbb13ffe2012-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 Gorman62997022012-10-08 16:32:47 -070069static void __reset_isolation_suitable(struct zone *zone)
Mel Gormanbb13ffe2012-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 Gormanc89511a2012-10-08 16:32:45 -070075 zone->compact_cached_migrate_pfn = start_pfn;
76 zone->compact_cached_free_pfn = end_pfn;
Mel Gorman62997022012-10-08 16:32:47 -070077 zone->compact_blockskip_flush = false;
Mel Gormanbb13ffe2012-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 Gorman62997022012-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 Gormanbb13ffe2012-10-08 16:32:41 -0700111/*
112 * If no pages were isolated then mark this pageblock to be skipped in the
Mel Gorman62997022012-10-08 16:32:47 -0700113 * future. The information is later cleared by __reset_isolation_suitable().
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700114 */
Mel Gormanc89511a2012-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 Gormanbb13ffe2012-10-08 16:32:41 -0700118{
Mel Gormanc89511a2012-10-08 16:32:45 -0700119 struct zone *zone = cc->zone;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700120 if (!page)
121 return;
122
Mel Gormanc89511a2012-10-08 16:32:45 -0700123 if (!nr_isolated) {
124 unsigned long pfn = page_to_pfn(page);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700125 set_pageblock_skip(page);
Mel Gormanc89511a2012-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 Gormanbb13ffe2012-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 Gormanc89511a2012-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 Gormanbb13ffe2012-10-08 16:32:41 -0700149{
150}
151#endif /* CONFIG_COMPACTION */
152
Mel Gorman2a1402a2012-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 Nazarewicz85aa1252012-01-30 13:24:03 +0100158/*
Mel Gormanc67fe372012-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 Gorman2a1402a2012-10-08 16:32:33 -0700170 if (should_release_lock(lock)) {
Mel Gormanc67fe372012-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 Lie64c5232012-10-08 16:32:27 -0700178 cc->contended = true;
Mel Gormanc67fe372012-08-21 16:16:17 -0700179 return false;
180 }
181
182 cond_resched();
Mel Gormanc67fe372012-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 Gormanf40d1e42012-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 Gorman1fb3f8c2012-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 Gormanc67fe372012-08-21 16:16:17 -0700271/*
Michal Nazarewicz85aa1252012-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 Gormanf40d1e42012-10-08 16:32:36 -0700277static unsigned long isolate_freepages_block(struct compact_control *cc,
278 unsigned long blockpfn,
Michal Nazarewicz85aa1252012-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 Gormanbb13ffe2012-10-08 16:32:41 -0700284 struct page *cursor, *valid_page = NULL;
Mel Gormanf40d1e42012-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 Gormanf40d1e42012-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 Gormanf40d1e42012-10-08 16:32:36 -0700297 if (!pfn_valid_within(blockpfn))
Mel Gorman748446b2010-05-24 14:32:27 -0700298 continue;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700299 if (!valid_page)
300 valid_page = page;
Mel Gormanf40d1e42012-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 Nazarewicz85aa1252012-01-30 13:24:03 +0100327 if (!isolated && strict)
Mel Gormanf40d1e42012-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 Gormanf40d1e42012-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 Gorman0db63d72012-10-19 13:56:57 -0700349 if (strict && nr_strict_required > total_isolated)
Mel Gormanf40d1e42012-10-08 16:32:36 -0700350 total_isolated = 0;
351
352 if (locked)
353 spin_unlock_irqrestore(&cc->zone->lock, flags);
354
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700355 /* Update the pageblock-skip if the whole pageblock was scanned */
356 if (blockpfn == end_pfn)
Mel Gormanc89511a2012-10-08 16:32:45 -0700357 update_pageblock_skip(cc, valid_page, total_isolated, false);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700358
Mel Gorman748446b2010-05-24 14:32:27 -0700359 return total_isolated;
360}
361
Michal Nazarewicz85aa1252012-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 Nazarewiczff9543f2011-12-29 13:09:50 +0100375unsigned long
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700376isolate_freepages_range(struct compact_control *cc,
377 unsigned long start_pfn, unsigned long end_pfn)
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100378{
Mel Gormanf40d1e42012-10-08 16:32:36 -0700379 unsigned long isolated, pfn, block_end_pfn;
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100380 LIST_HEAD(freelist);
381
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100382 for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700383 if (!pfn_valid(pfn) || cc->zone != page_zone(pfn_to_page(pfn)))
Michal Nazarewicz85aa1252012-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 Gormanbb13ffe2012-10-08 16:32:41 -0700393 isolated = isolate_freepages_block(cc, pfn, block_end_pfn,
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100394 &freelist, true);
Michal Nazarewicz85aa1252012-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 Gormanc67fe372012-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 Gormanc67fe372012-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 Nazarewicz2fe86e02012-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 Kime46a2872012-10-08 16:33:48 -0700464 * @unevictable: true if it allows to isolate unevictable pages
Michal Nazarewicz2fe86e02012-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 Nazarewiczff9543f2011-12-29 13:09:50 +0100478unsigned long
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100479isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
Minchan Kime46a2872012-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 Khlebnikovf3fd4a62012-05-29 15:06:54 -0700485 isolate_mode_t mode = 0;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700486 struct lruvec *lruvec;
Mel Gormanc67fe372012-08-21 16:16:17 -0700487 unsigned long flags;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700488 bool locked = false;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700489 struct page *page = NULL, *valid_page = NULL;
Mel Gorman748446b2010-05-24 14:32:27 -0700490
Mel Gorman748446b2010-05-24 14:32:27 -0700491 /*
492 * Ensure that there are not too many pages isolated from the LRU
493 * list by either parallel reclaimers or compaction. If there are,
494 * delay for some time until fewer pages are isolated
495 */
496 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700497 /* async migration should just abort */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700498 if (!cc->sync)
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100499 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700500
Mel Gorman748446b2010-05-24 14:32:27 -0700501 congestion_wait(BLK_RW_ASYNC, HZ/10);
502
503 if (fatal_signal_pending(current))
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100504 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700505 }
506
507 /* Time to isolate some pages for migration */
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700508 cond_resched();
Mel Gorman748446b2010-05-24 14:32:27 -0700509 for (; low_pfn < end_pfn; low_pfn++) {
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700510 /* give a chance to irqs before checking need_resched() */
Mel Gorman2a1402a2012-10-08 16:32:33 -0700511 if (locked && !((low_pfn+1) % SWAP_CLUSTER_MAX)) {
512 if (should_release_lock(&zone->lru_lock)) {
513 spin_unlock_irqrestore(&zone->lru_lock, flags);
514 locked = false;
515 }
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700516 }
Mel Gormanc67fe372012-08-21 16:16:17 -0700517
Mel Gorman0bf380b2012-02-03 15:37:18 -0800518 /*
519 * migrate_pfn does not necessarily start aligned to a
520 * pageblock. Ensure that pfn_valid is called when moving
521 * into a new MAX_ORDER_NR_PAGES range in case of large
522 * memory holes within the zone
523 */
524 if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
525 if (!pfn_valid(low_pfn)) {
526 low_pfn += MAX_ORDER_NR_PAGES - 1;
527 continue;
528 }
529 }
530
Mel Gorman748446b2010-05-24 14:32:27 -0700531 if (!pfn_valid_within(low_pfn))
532 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800533 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700534
Mel Gormandc908602012-02-08 17:13:38 -0800535 /*
536 * Get the page and ensure the page is within the same zone.
537 * See the comment in isolate_freepages about overlapping
538 * nodes. It is deliberate that the new zone lock is not taken
539 * as memory compaction should not move pages between nodes.
540 */
Mel Gorman748446b2010-05-24 14:32:27 -0700541 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800542 if (page_zone(page) != zone)
543 continue;
544
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700545 if (!valid_page)
546 valid_page = page;
547
548 /* If isolation recently failed, do not retry */
549 pageblock_nr = low_pfn >> pageblock_order;
550 if (!isolation_suitable(cc, page))
551 goto next_pageblock;
552
Mel Gormandc908602012-02-08 17:13:38 -0800553 /* Skip if free */
Mel Gorman748446b2010-05-24 14:32:27 -0700554 if (PageBuddy(page))
555 continue;
556
Mel Gorman9927af742011-01-13 15:45:59 -0800557 /*
558 * For async migration, also only scan in MOVABLE blocks. Async
559 * migration is optimistic to see if the minimum amount of work
560 * satisfies the allocation
561 */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700562 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
Michal Nazarewicz47118af2011-12-29 13:09:50 +0100563 !migrate_async_suitable(get_pageblock_migratetype(page))) {
Mel Gormanc89511a2012-10-08 16:32:45 -0700564 cc->finished_update_migrate = true;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700565 goto next_pageblock;
Mel Gorman9927af742011-01-13 15:45:59 -0800566 }
567
Mel Gorman2a1402a2012-10-08 16:32:33 -0700568 /* Check may be lockless but that's ok as we recheck later */
Andrea Arcangelibc835012011-01-13 15:47:08 -0800569 if (!PageLRU(page))
570 continue;
571
572 /*
Mel Gorman2a1402a2012-10-08 16:32:33 -0700573 * PageLRU is set. lru_lock normally excludes isolation
574 * splitting and collapsing (collapsing has already happened
575 * if PageLRU is set) but the lock is not necessarily taken
576 * here and it is wasteful to take it just to check transhuge.
577 * Check TransHuge without lock and skip the whole pageblock if
578 * it's either a transhuge or hugetlbfs page, as calling
579 * compound_order() without preventing THP from splitting the
580 * page underneath us may return surprising results.
Andrea Arcangelibc835012011-01-13 15:47:08 -0800581 */
582 if (PageTransHuge(page)) {
Mel Gorman2a1402a2012-10-08 16:32:33 -0700583 if (!locked)
584 goto next_pageblock;
585 low_pfn += (1 << compound_order(page)) - 1;
586 continue;
587 }
588
589 /* Check if it is ok to still hold the lock */
590 locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
591 locked, cc);
592 if (!locked || fatal_signal_pending(current))
593 break;
594
595 /* Recheck PageLRU and PageTransHuge under lock */
596 if (!PageLRU(page))
597 continue;
598 if (PageTransHuge(page)) {
Andrea Arcangelibc835012011-01-13 15:47:08 -0800599 low_pfn += (1 << compound_order(page)) - 1;
600 continue;
601 }
602
Linus Torvalds68e3e922012-06-03 20:05:57 -0700603 if (!cc->sync)
Mel Gormanc8244932012-01-12 17:19:38 -0800604 mode |= ISOLATE_ASYNC_MIGRATE;
605
Minchan Kime46a2872012-10-08 16:33:48 -0700606 if (unevictable)
607 mode |= ISOLATE_UNEVICTABLE;
608
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700609 lruvec = mem_cgroup_page_lruvec(page, zone);
610
Mel Gorman748446b2010-05-24 14:32:27 -0700611 /* Try isolate the page */
Konstantin Khlebnikovf3fd4a62012-05-29 15:06:54 -0700612 if (__isolate_lru_page(page, mode) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700613 continue;
614
Andrea Arcangelibc835012011-01-13 15:47:08 -0800615 VM_BUG_ON(PageTransCompound(page));
616
Mel Gorman748446b2010-05-24 14:32:27 -0700617 /* Successfully isolated */
Mel Gormanc89511a2012-10-08 16:32:45 -0700618 cc->finished_update_migrate = true;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700619 del_page_from_lru_list(page, lruvec, page_lru(page));
Mel Gorman748446b2010-05-24 14:32:27 -0700620 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700621 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800622 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700623
624 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800625 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
626 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700627 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800628 }
Mel Gorman2a1402a2012-10-08 16:32:33 -0700629
630 continue;
631
632next_pageblock:
633 low_pfn += pageblock_nr_pages;
634 low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
635 last_pageblock_nr = pageblock_nr;
Mel Gorman748446b2010-05-24 14:32:27 -0700636 }
637
Mel Gormanc67fe372012-08-21 16:16:17 -0700638 acct_isolated(zone, locked, cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700639
Mel Gormanc67fe372012-08-21 16:16:17 -0700640 if (locked)
641 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700642
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700643 /* Update the pageblock-skip if the whole pageblock was scanned */
644 if (low_pfn == end_pfn)
Mel Gormanc89511a2012-10-08 16:32:45 -0700645 update_pageblock_skip(cc, valid_page, nr_isolated, true);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700646
Mel Gormanb7aba692011-01-13 15:45:54 -0800647 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
648
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100649 return low_pfn;
650}
651
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100652#endif /* CONFIG_COMPACTION || CONFIG_CMA */
653#ifdef CONFIG_COMPACTION
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100654/*
655 * Based on information in the current compact_control, find blocks
656 * suitable for isolating free pages from and then isolate them.
657 */
658static void isolate_freepages(struct zone *zone,
659 struct compact_control *cc)
660{
661 struct page *page;
662 unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100663 int nr_freepages = cc->nr_freepages;
664 struct list_head *freelist = &cc->freepages;
665
666 /*
667 * Initialise the free scanner. The starting point is where we last
668 * scanned from (or the end of the zone if starting). The low point
669 * is the end of the pageblock the migration scanner is using.
670 */
671 pfn = cc->free_pfn;
672 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
673
674 /*
675 * Take care that if the migration scanner is at the end of the zone
676 * that the free scanner does not accidentally move to the next zone
677 * in the next isolation cycle.
678 */
679 high_pfn = min(low_pfn, pfn);
680
681 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
682
683 /*
684 * Isolate free pages until enough are available to migrate the
685 * pages on cc->migratepages. We stop searching if the migrate
686 * and free page scanners meet or enough free pages are isolated.
687 */
688 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
689 pfn -= pageblock_nr_pages) {
690 unsigned long isolated;
691
692 if (!pfn_valid(pfn))
693 continue;
694
695 /*
696 * Check for overlapping nodes/zones. It's possible on some
697 * configurations to have a setup like
698 * node0 node1 node0
699 * i.e. it's possible that all pages within a zones range of
700 * pages do not belong to a single zone.
701 */
702 page = pfn_to_page(pfn);
703 if (page_zone(page) != zone)
704 continue;
705
706 /* Check the block is suitable for migration */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700707 if (!suitable_migration_target(page))
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100708 continue;
Linus Torvalds68e3e922012-06-03 20:05:57 -0700709
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700710 /* If isolation recently failed, do not retry */
711 if (!isolation_suitable(cc, page))
712 continue;
713
Mel Gormanf40d1e42012-10-08 16:32:36 -0700714 /* Found a block suitable for isolating free pages from */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100715 isolated = 0;
Mel Gorman60177d32012-12-06 19:01:14 +0000716
717 /*
718 * As pfn may not start aligned, pfn+pageblock_nr_page
719 * may cross a MAX_ORDER_NR_PAGES boundary and miss
720 * a pfn_valid check. Ensure isolate_freepages_block()
721 * only scans within a pageblock
722 */
723 end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
724 end_pfn = min(end_pfn, zone_end_pfn);
Mel Gormanf40d1e42012-10-08 16:32:36 -0700725 isolated = isolate_freepages_block(cc, pfn, end_pfn,
726 freelist, false);
727 nr_freepages += isolated;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100728
729 /*
730 * Record the highest PFN we isolated pages from. When next
731 * looking for free pages, the search will restart here as
732 * page migration may have returned some pages to the allocator
733 */
Mel Gormanc89511a2012-10-08 16:32:45 -0700734 if (isolated) {
735 cc->finished_update_free = true;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100736 high_pfn = max(high_pfn, pfn);
Mel Gormanc89511a2012-10-08 16:32:45 -0700737 }
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100738 }
739
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100740 /* split_free_page does not map the pages */
741 map_pages(freelist);
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100742
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100743 cc->free_pfn = high_pfn;
744 cc->nr_freepages = nr_freepages;
Mel Gorman748446b2010-05-24 14:32:27 -0700745}
746
747/*
748 * This is a migrate-callback that "allocates" freepages by taking pages
749 * from the isolated freelists in the block we are migrating to.
750 */
751static struct page *compaction_alloc(struct page *migratepage,
752 unsigned long data,
753 int **result)
754{
755 struct compact_control *cc = (struct compact_control *)data;
756 struct page *freepage;
757
758 /* Isolate free pages if necessary */
759 if (list_empty(&cc->freepages)) {
760 isolate_freepages(cc->zone, cc);
761
762 if (list_empty(&cc->freepages))
763 return NULL;
764 }
765
766 freepage = list_entry(cc->freepages.next, struct page, lru);
767 list_del(&freepage->lru);
768 cc->nr_freepages--;
769
770 return freepage;
771}
772
773/*
774 * We cannot control nr_migratepages and nr_freepages fully when migration is
775 * running as migrate_pages() has no knowledge of compact_control. When
776 * migration is complete, we count the number of pages on the lists by hand.
777 */
778static void update_nr_listpages(struct compact_control *cc)
779{
780 int nr_migratepages = 0;
781 int nr_freepages = 0;
782 struct page *page;
783
784 list_for_each_entry(page, &cc->migratepages, lru)
785 nr_migratepages++;
786 list_for_each_entry(page, &cc->freepages, lru)
787 nr_freepages++;
788
789 cc->nr_migratepages = nr_migratepages;
790 cc->nr_freepages = nr_freepages;
791}
792
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100793/* possible outcome of isolate_migratepages */
794typedef enum {
795 ISOLATE_ABORT, /* Abort compaction now */
796 ISOLATE_NONE, /* No pages isolated, continue scanning */
797 ISOLATE_SUCCESS, /* Pages isolated, migrate */
798} isolate_migrate_t;
799
800/*
801 * Isolate all pages that can be migrated from the block pointed to by
802 * the migrate scanner within compact_control.
803 */
804static isolate_migrate_t isolate_migratepages(struct zone *zone,
805 struct compact_control *cc)
806{
807 unsigned long low_pfn, end_pfn;
808
809 /* Do not scan outside zone boundaries */
810 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
811
812 /* Only scan within a pageblock boundary */
813 end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
814
815 /* Do not cross the free scanner or scan within a memory hole */
816 if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
817 cc->migrate_pfn = end_pfn;
818 return ISOLATE_NONE;
819 }
820
821 /* Perform the isolation */
Minchan Kime46a2872012-10-08 16:33:48 -0700822 low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn, false);
Shaohua Lie64c5232012-10-08 16:32:27 -0700823 if (!low_pfn || cc->contended)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100824 return ISOLATE_ABORT;
825
826 cc->migrate_pfn = low_pfn;
827
828 return ISOLATE_SUCCESS;
829}
830
Mel Gorman748446b2010-05-24 14:32:27 -0700831static int compact_finished(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800832 struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700833{
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800834 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -0700835
Mel Gorman748446b2010-05-24 14:32:27 -0700836 if (fatal_signal_pending(current))
837 return COMPACT_PARTIAL;
838
Mel Gorman753341a2012-10-08 16:32:40 -0700839 /* Compaction run completes if the migrate and free scanner meet */
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700840 if (cc->free_pfn <= cc->migrate_pfn) {
Mel Gorman62997022012-10-08 16:32:47 -0700841 /*
842 * Mark that the PG_migrate_skip information should be cleared
843 * by kswapd when it goes to sleep. kswapd does not set the
844 * flag itself as the decision to be clear should be directly
845 * based on an allocation request.
846 */
847 if (!current_is_kswapd())
848 zone->compact_blockskip_flush = true;
849
Mel Gorman748446b2010-05-24 14:32:27 -0700850 return COMPACT_COMPLETE;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700851 }
Mel Gorman748446b2010-05-24 14:32:27 -0700852
Johannes Weiner82478fb2011-01-20 14:44:21 -0800853 /*
854 * order == -1 is expected when compacting via
855 * /proc/sys/vm/compact_memory
856 */
Mel Gorman56de7262010-05-24 14:32:30 -0700857 if (cc->order == -1)
858 return COMPACT_CONTINUE;
859
Michal Hocko3957c772011-06-15 15:08:25 -0700860 /* Compaction run is not finished if the watermark is not met */
861 watermark = low_wmark_pages(zone);
862 watermark += (1 << cc->order);
863
864 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
865 return COMPACT_CONTINUE;
866
Mel Gorman56de7262010-05-24 14:32:30 -0700867 /* Direct compactor: Is a suitable page free? */
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700868 if (cc->page) {
869 /* Was a suitable page captured? */
870 if (*cc->page)
Mel Gorman56de7262010-05-24 14:32:30 -0700871 return COMPACT_PARTIAL;
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700872 } else {
873 unsigned int order;
874 for (order = cc->order; order < MAX_ORDER; order++) {
875 struct free_area *area = &zone->free_area[cc->order];
876 /* Job done if page is free of the right migratetype */
877 if (!list_empty(&area->free_list[cc->migratetype]))
878 return COMPACT_PARTIAL;
Mel Gorman56de7262010-05-24 14:32:30 -0700879
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700880 /* Job done if allocation would set block type */
881 if (cc->order >= pageblock_order && area->nr_free)
882 return COMPACT_PARTIAL;
883 }
Mel Gorman56de7262010-05-24 14:32:30 -0700884 }
885
Mel Gorman748446b2010-05-24 14:32:27 -0700886 return COMPACT_CONTINUE;
887}
888
Mel Gorman3e7d3442011-01-13 15:45:56 -0800889/*
890 * compaction_suitable: Is this suitable to run compaction on this zone now?
891 * Returns
892 * COMPACT_SKIPPED - If there are too few free pages for compaction
893 * COMPACT_PARTIAL - If the allocation would succeed without compaction
894 * COMPACT_CONTINUE - If compaction should run now
895 */
896unsigned long compaction_suitable(struct zone *zone, int order)
897{
898 int fragindex;
899 unsigned long watermark;
900
901 /*
Michal Hocko3957c772011-06-15 15:08:25 -0700902 * order == -1 is expected when compacting via
903 * /proc/sys/vm/compact_memory
904 */
905 if (order == -1)
906 return COMPACT_CONTINUE;
907
908 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -0800909 * Watermarks for order-0 must be met for compaction. Note the 2UL.
910 * This is because during migration, copies of pages need to be
911 * allocated and for a short time, the footprint is higher
912 */
913 watermark = low_wmark_pages(zone) + (2UL << order);
914 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
915 return COMPACT_SKIPPED;
916
917 /*
918 * fragmentation index determines if allocation failures are due to
919 * low memory or external fragmentation
920 *
Shaohua Lia582a732011-06-15 15:08:49 -0700921 * index of -1000 implies allocations might succeed depending on
922 * watermarks
Mel Gorman3e7d3442011-01-13 15:45:56 -0800923 * index towards 0 implies failure is due to lack of memory
924 * index towards 1000 implies failure is due to fragmentation
925 *
926 * Only compact if a failure would be due to fragmentation.
927 */
928 fragindex = fragmentation_index(zone, order);
929 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
930 return COMPACT_SKIPPED;
931
Shaohua Lia582a732011-06-15 15:08:49 -0700932 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
933 0, 0))
Mel Gorman3e7d3442011-01-13 15:45:56 -0800934 return COMPACT_PARTIAL;
935
936 return COMPACT_CONTINUE;
937}
938
Mel Gorman748446b2010-05-24 14:32:27 -0700939static int compact_zone(struct zone *zone, struct compact_control *cc)
940{
941 int ret;
Mel Gormanc89511a2012-10-08 16:32:45 -0700942 unsigned long start_pfn = zone->zone_start_pfn;
943 unsigned long end_pfn = zone->zone_start_pfn + zone->spanned_pages;
Mel Gorman748446b2010-05-24 14:32:27 -0700944
Mel Gorman3e7d3442011-01-13 15:45:56 -0800945 ret = compaction_suitable(zone, cc->order);
946 switch (ret) {
947 case COMPACT_PARTIAL:
948 case COMPACT_SKIPPED:
949 /* Compaction is likely to fail */
950 return ret;
951 case COMPACT_CONTINUE:
952 /* Fall through to compaction */
953 ;
954 }
955
Mel Gormanc89511a2012-10-08 16:32:45 -0700956 /*
957 * Setup to move all movable pages to the end of the zone. Used cached
958 * information on where the scanners should start but check that it
959 * is initialised by ensuring the values are within zone boundaries.
960 */
961 cc->migrate_pfn = zone->compact_cached_migrate_pfn;
962 cc->free_pfn = zone->compact_cached_free_pfn;
963 if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
964 cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
965 zone->compact_cached_free_pfn = cc->free_pfn;
966 }
967 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
968 cc->migrate_pfn = start_pfn;
969 zone->compact_cached_migrate_pfn = cc->migrate_pfn;
970 }
Mel Gorman748446b2010-05-24 14:32:27 -0700971
Mel Gorman62997022012-10-08 16:32:47 -0700972 /*
973 * Clear pageblock skip if there were failures recently and compaction
974 * is about to be retried after being deferred. kswapd does not do
975 * this reset as it'll reset the cached information when going to sleep.
976 */
977 if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
978 __reset_isolation_suitable(zone);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700979
Mel Gorman748446b2010-05-24 14:32:27 -0700980 migrate_prep_local();
981
982 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
983 unsigned long nr_migrate, nr_remaining;
Minchan Kim9d502c12011-03-22 16:30:39 -0700984 int err;
Mel Gorman748446b2010-05-24 14:32:27 -0700985
Mel Gormanf9e35b32011-06-15 15:08:52 -0700986 switch (isolate_migratepages(zone, cc)) {
987 case ISOLATE_ABORT:
988 ret = COMPACT_PARTIAL;
Shaohua Lie64c5232012-10-08 16:32:27 -0700989 putback_lru_pages(&cc->migratepages);
990 cc->nr_migratepages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700991 goto out;
992 case ISOLATE_NONE:
Mel Gorman748446b2010-05-24 14:32:27 -0700993 continue;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700994 case ISOLATE_SUCCESS:
995 ;
996 }
Mel Gorman748446b2010-05-24 14:32:27 -0700997
998 nr_migrate = cc->nr_migratepages;
Minchan Kim9d502c12011-03-22 16:30:39 -0700999 err = migrate_pages(&cc->migratepages, compaction_alloc,
Linus Torvalds68e3e922012-06-03 20:05:57 -07001000 (unsigned long)cc, false,
1001 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
Mel Gorman748446b2010-05-24 14:32:27 -07001002 update_nr_listpages(cc);
1003 nr_remaining = cc->nr_migratepages;
1004
1005 count_vm_event(COMPACTBLOCKS);
1006 count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
1007 if (nr_remaining)
1008 count_vm_events(COMPACTPAGEFAILED, nr_remaining);
Mel Gormanb7aba692011-01-13 15:45:54 -08001009 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
1010 nr_remaining);
Mel Gorman748446b2010-05-24 14:32:27 -07001011
1012 /* Release LRU pages not migrated */
Minchan Kim9d502c12011-03-22 16:30:39 -07001013 if (err) {
Mel Gorman748446b2010-05-24 14:32:27 -07001014 putback_lru_pages(&cc->migratepages);
1015 cc->nr_migratepages = 0;
David Rientjes4bf2bba2012-07-11 14:02:13 -07001016 if (err == -ENOMEM) {
1017 ret = COMPACT_PARTIAL;
1018 goto out;
1019 }
Mel Gorman748446b2010-05-24 14:32:27 -07001020 }
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001021
1022 /* Capture a page now if it is a suitable size */
1023 compact_capture_page(cc);
Mel Gorman748446b2010-05-24 14:32:27 -07001024 }
1025
Mel Gormanf9e35b32011-06-15 15:08:52 -07001026out:
Mel Gorman748446b2010-05-24 14:32:27 -07001027 /* Release free pages and check accounting */
1028 cc->nr_freepages -= release_freepages(&cc->freepages);
1029 VM_BUG_ON(cc->nr_freepages != 0);
1030
1031 return ret;
1032}
Mel Gorman76ab0f52010-05-24 14:32:28 -07001033
Kyungmin Parkd43a87e2011-10-31 17:09:08 -07001034static unsigned long compact_zone_order(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -08001035 int order, gfp_t gfp_mask,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001036 bool sync, bool *contended,
1037 struct page **page)
Mel Gorman56de7262010-05-24 14:32:30 -07001038{
Shaohua Lie64c5232012-10-08 16:32:27 -07001039 unsigned long ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001040 struct compact_control cc = {
1041 .nr_freepages = 0,
1042 .nr_migratepages = 0,
1043 .order = order,
1044 .migratetype = allocflags_to_migratetype(gfp_mask),
1045 .zone = zone,
Linus Torvalds68e3e922012-06-03 20:05:57 -07001046 .sync = sync,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001047 .page = page,
Mel Gorman56de7262010-05-24 14:32:30 -07001048 };
1049 INIT_LIST_HEAD(&cc.freepages);
1050 INIT_LIST_HEAD(&cc.migratepages);
1051
Shaohua Lie64c5232012-10-08 16:32:27 -07001052 ret = compact_zone(zone, &cc);
1053
1054 VM_BUG_ON(!list_empty(&cc.freepages));
1055 VM_BUG_ON(!list_empty(&cc.migratepages));
1056
1057 *contended = cc.contended;
1058 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001059}
1060
Mel Gorman5e771902010-05-24 14:32:31 -07001061int sysctl_extfrag_threshold = 500;
1062
Mel Gorman56de7262010-05-24 14:32:30 -07001063/**
1064 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
1065 * @zonelist: The zonelist used for the current allocation
1066 * @order: The order of the current allocation
1067 * @gfp_mask: The GFP mask of the current allocation
1068 * @nodemask: The allowed nodes to allocate from
Mel Gorman77f1fe62011-01-13 15:45:57 -08001069 * @sync: Whether migration is synchronous or not
Mel Gorman661c4cb2012-10-08 16:32:31 -07001070 * @contended: Return value that is true if compaction was aborted due to lock contention
1071 * @page: Optionally capture a free page of the requested order during compaction
Mel Gorman56de7262010-05-24 14:32:30 -07001072 *
1073 * This is the main entry point for direct page compaction.
1074 */
1075unsigned long try_to_compact_pages(struct zonelist *zonelist,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001076 int order, gfp_t gfp_mask, nodemask_t *nodemask,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001077 bool sync, bool *contended, struct page **page)
Mel Gorman56de7262010-05-24 14:32:30 -07001078{
1079 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
1080 int may_enter_fs = gfp_mask & __GFP_FS;
1081 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -07001082 struct zoneref *z;
1083 struct zone *zone;
1084 int rc = COMPACT_SKIPPED;
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001085 int alloc_flags = 0;
Mel Gorman56de7262010-05-24 14:32:30 -07001086
Mel Gorman4ffb6332012-10-08 16:29:09 -07001087 /* Check if the GFP flags allow compaction */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -08001088 if (!order || !may_enter_fs || !may_perform_io)
Mel Gorman56de7262010-05-24 14:32:30 -07001089 return rc;
1090
1091 count_vm_event(COMPACTSTALL);
1092
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001093#ifdef CONFIG_CMA
1094 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
1095 alloc_flags |= ALLOC_CMA;
1096#endif
Mel Gorman56de7262010-05-24 14:32:30 -07001097 /* Compact each zone in the list */
1098 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
1099 nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -07001100 int status;
1101
Mel Gormanc67fe372012-08-21 16:16:17 -07001102 status = compact_zone_order(zone, order, gfp_mask, sync,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001103 contended, page);
Mel Gorman56de7262010-05-24 14:32:30 -07001104 rc = max(status, rc);
1105
Mel Gorman3e7d3442011-01-13 15:45:56 -08001106 /* If a normal allocation would succeed, stop compacting */
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001107 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
1108 alloc_flags))
Mel Gorman56de7262010-05-24 14:32:30 -07001109 break;
1110 }
1111
1112 return rc;
1113}
1114
1115
Mel Gorman76ab0f52010-05-24 14:32:28 -07001116/* Compact all zones within a node */
Rik van Riel7be62de2012-03-21 16:33:52 -07001117static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001118{
1119 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -07001120 struct zone *zone;
1121
Mel Gorman76ab0f52010-05-24 14:32:28 -07001122 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -07001123
1124 zone = &pgdat->node_zones[zoneid];
1125 if (!populated_zone(zone))
1126 continue;
1127
Rik van Riel7be62de2012-03-21 16:33:52 -07001128 cc->nr_freepages = 0;
1129 cc->nr_migratepages = 0;
1130 cc->zone = zone;
1131 INIT_LIST_HEAD(&cc->freepages);
1132 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001133
Dan Carpenteraad6ec32012-03-21 16:33:54 -07001134 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -07001135 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001136
Rik van Rielaff62242012-03-21 16:33:52 -07001137 if (cc->order > 0) {
1138 int ok = zone_watermark_ok(zone, cc->order,
1139 low_wmark_pages(zone), 0, 0);
Minchan Kimc81758f2012-08-21 16:16:03 -07001140 if (ok && cc->order >= zone->compact_order_failed)
Rik van Rielaff62242012-03-21 16:33:52 -07001141 zone->compact_order_failed = cc->order + 1;
1142 /* Currently async compaction is never deferred. */
Linus Torvalds68e3e922012-06-03 20:05:57 -07001143 else if (!ok && cc->sync)
Rik van Rielaff62242012-03-21 16:33:52 -07001144 defer_compaction(zone, cc->order);
1145 }
1146
Rik van Riel7be62de2012-03-21 16:33:52 -07001147 VM_BUG_ON(!list_empty(&cc->freepages));
1148 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -07001149 }
1150
1151 return 0;
1152}
1153
Rik van Riel7be62de2012-03-21 16:33:52 -07001154int compact_pgdat(pg_data_t *pgdat, int order)
1155{
1156 struct compact_control cc = {
1157 .order = order,
Linus Torvalds68e3e922012-06-03 20:05:57 -07001158 .sync = false,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001159 .page = NULL,
Rik van Riel7be62de2012-03-21 16:33:52 -07001160 };
1161
1162 return __compact_pgdat(pgdat, &cc);
1163}
1164
1165static int compact_node(int nid)
1166{
Rik van Riel7be62de2012-03-21 16:33:52 -07001167 struct compact_control cc = {
1168 .order = -1,
Linus Torvalds68e3e922012-06-03 20:05:57 -07001169 .sync = true,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001170 .page = NULL,
Rik van Riel7be62de2012-03-21 16:33:52 -07001171 };
1172
Hugh Dickins8575ec22012-03-21 16:33:53 -07001173 return __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001174}
1175
Mel Gorman76ab0f52010-05-24 14:32:28 -07001176/* Compact all nodes in the system */
1177static int compact_nodes(void)
1178{
1179 int nid;
1180
Hugh Dickins8575ec22012-03-21 16:33:53 -07001181 /* Flush pending updates to the LRU lists */
1182 lru_add_drain_all();
1183
Mel Gorman76ab0f52010-05-24 14:32:28 -07001184 for_each_online_node(nid)
1185 compact_node(nid);
1186
1187 return COMPACT_COMPLETE;
1188}
1189
1190/* The written value is actually unused, all memory is compacted */
1191int sysctl_compact_memory;
1192
1193/* This is the entry point for compacting all nodes via /proc/sys/vm */
1194int sysctl_compaction_handler(struct ctl_table *table, int write,
1195 void __user *buffer, size_t *length, loff_t *ppos)
1196{
1197 if (write)
1198 return compact_nodes();
1199
1200 return 0;
1201}
Mel Gormaned4a6d72010-05-24 14:32:29 -07001202
Mel Gorman5e771902010-05-24 14:32:31 -07001203int sysctl_extfrag_handler(struct ctl_table *table, int write,
1204 void __user *buffer, size_t *length, loff_t *ppos)
1205{
1206 proc_dointvec_minmax(table, write, buffer, length, ppos);
1207
1208 return 0;
1209}
1210
Mel Gormaned4a6d72010-05-24 14:32:29 -07001211#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Kay Sievers10fbcf42011-12-21 14:48:43 -08001212ssize_t sysfs_compact_node(struct device *dev,
1213 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -07001214 const char *buf, size_t count)
1215{
Hugh Dickins8575ec22012-03-21 16:33:53 -07001216 int nid = dev->id;
1217
1218 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1219 /* Flush pending updates to the LRU lists */
1220 lru_add_drain_all();
1221
1222 compact_node(nid);
1223 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07001224
1225 return count;
1226}
Kay Sievers10fbcf42011-12-21 14:48:43 -08001227static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001228
1229int compaction_register_node(struct node *node)
1230{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001231 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001232}
1233
1234void compaction_unregister_node(struct node *node)
1235{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001236 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001237}
1238#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001239
1240#endif /* CONFIG_COMPACTION */