blob: 0323585291ba5e87d637d6453203d6dadbdae575 [file] [log] [blame]
Mel Gorman748446b2010-05-24 14:32:27 -07001/*
2 * linux/mm/compaction.c
3 *
4 * Memory compaction for the reduction of external fragmentation. Note that
5 * this heavily depends upon page migration to do all the real heavy
6 * lifting
7 *
8 * Copyright IBM Corp. 2007-2010 Mel Gorman <mel@csn.ul.ie>
9 */
10#include <linux/swap.h>
11#include <linux/migrate.h>
12#include <linux/compaction.h>
13#include <linux/mm_inline.h>
14#include <linux/backing-dev.h>
Mel Gorman76ab0f52010-05-24 14:32:28 -070015#include <linux/sysctl.h>
Mel Gormaned4a6d72010-05-24 14:32:29 -070016#include <linux/sysfs.h>
Mel Gorman748446b2010-05-24 14:32:27 -070017#include "internal.h"
18
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +010019#if defined CONFIG_COMPACTION || defined CONFIG_CMA
20
Mel Gormanb7aba692011-01-13 15:45:54 -080021#define CREATE_TRACE_POINTS
22#include <trace/events/compaction.h>
23
Mel Gorman748446b2010-05-24 14:32:27 -070024static unsigned long release_freepages(struct list_head *freelist)
25{
26 struct page *page, *next;
27 unsigned long count = 0;
28
29 list_for_each_entry_safe(page, next, freelist, lru) {
30 list_del(&page->lru);
31 __free_page(page);
32 count++;
33 }
34
35 return count;
36}
37
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +010038static void map_pages(struct list_head *list)
39{
40 struct page *page;
41
42 list_for_each_entry(page, list, lru) {
43 arch_alloc_page(page, 0);
44 kernel_map_pages(page, 1, 1);
45 }
46}
47
Michal Nazarewiczd4158d22011-12-29 13:09:50 +010048static inline bool migrate_async_suitable(int migratetype)
49{
50 return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
51}
52
Mel Gormancc5a88a2012-10-08 16:32:41 -070053#ifdef CONFIG_COMPACTION
54/* Returns true if the pageblock should be scanned for pages to isolate. */
55static inline bool isolation_suitable(struct compact_control *cc,
56 struct page *page)
57{
58 if (cc->ignore_skip_hint)
59 return true;
60
61 return !get_pageblock_skip(page);
62}
63
64/*
65 * This function is called to clear all cached information on pageblocks that
66 * should be skipped for page isolation when the migrate and free page scanner
67 * meet.
68 */
69static void reset_isolation_suitable(struct zone *zone)
70{
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
75 /*
76 * Do not reset more than once every five seconds. If allocations are
77 * failing sufficiently quickly to allow this to happen then continually
78 * scanning for compaction is not going to help. The choice of five
79 * seconds is arbitrary but will mitigate excessive scanning.
80 */
81 if (time_before(jiffies, zone->compact_blockskip_expire))
82 return;
Mel Gormanf57bc462012-10-08 16:32:45 -070083
84 zone->compact_cached_migrate_pfn = start_pfn;
85 zone->compact_cached_free_pfn = end_pfn;
Mel Gormancc5a88a2012-10-08 16:32:41 -070086 zone->compact_blockskip_expire = jiffies + (HZ * 5);
87
88 /* Walk the zone and mark every pageblock as suitable for isolation */
89 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
90 struct page *page;
91
92 cond_resched();
93
94 if (!pfn_valid(pfn))
95 continue;
96
97 page = pfn_to_page(pfn);
98 if (zone != page_zone(page))
99 continue;
100
101 clear_pageblock_skip(page);
102 }
103}
104
105/*
106 * If no pages were isolated then mark this pageblock to be skipped in the
107 * future. The information is later cleared by reset_isolation_suitable().
108 */
Mel Gormanf57bc462012-10-08 16:32:45 -0700109static void update_pageblock_skip(struct compact_control *cc,
110 struct page *page, unsigned long nr_isolated,
111 bool migrate_scanner)
Mel Gormancc5a88a2012-10-08 16:32:41 -0700112{
Mel Gormanf57bc462012-10-08 16:32:45 -0700113 struct zone *zone = cc->zone;
Mel Gormancc5a88a2012-10-08 16:32:41 -0700114 if (!page)
115 return;
116
Mel Gormanf57bc462012-10-08 16:32:45 -0700117 if (!nr_isolated) {
118 unsigned long pfn = page_to_pfn(page);
Mel Gormancc5a88a2012-10-08 16:32:41 -0700119 set_pageblock_skip(page);
Mel Gormanf57bc462012-10-08 16:32:45 -0700120
121 /* Update where compaction should restart */
122 if (migrate_scanner) {
123 if (!cc->finished_update_migrate &&
124 pfn > zone->compact_cached_migrate_pfn)
125 zone->compact_cached_migrate_pfn = pfn;
126 } else {
127 if (!cc->finished_update_free &&
128 pfn < zone->compact_cached_free_pfn)
129 zone->compact_cached_free_pfn = pfn;
130 }
131 }
Mel Gormancc5a88a2012-10-08 16:32:41 -0700132}
133#else
134static inline bool isolation_suitable(struct compact_control *cc,
135 struct page *page)
136{
137 return true;
138}
139
Mel Gormanf57bc462012-10-08 16:32:45 -0700140static void update_pageblock_skip(struct compact_control *cc,
141 struct page *page, unsigned long nr_isolated,
142 bool migrate_scanner)
Mel Gormancc5a88a2012-10-08 16:32:41 -0700143{
144}
145#endif /* CONFIG_COMPACTION */
146
Mel Gorman6a38cba2012-10-08 16:32:33 -0700147static inline bool should_release_lock(spinlock_t *lock)
148{
149 return need_resched() || spin_is_contended(lock);
150}
151
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100152/*
Mel Gorman0ba33882012-08-21 16:16:17 -0700153 * Compaction requires the taking of some coarse locks that are potentially
154 * very heavily contended. Check if the process needs to be scheduled or
155 * if the lock is contended. For async compaction, back out in the event
156 * if contention is severe. For sync compaction, schedule.
157 *
158 * Returns true if the lock is held.
159 * Returns false if the lock is released and compaction should abort
160 */
161static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags,
162 bool locked, struct compact_control *cc)
163{
Mel Gorman6a38cba2012-10-08 16:32:33 -0700164 if (should_release_lock(lock)) {
Mel Gorman0ba33882012-08-21 16:16:17 -0700165 if (locked) {
166 spin_unlock_irqrestore(lock, *flags);
167 locked = false;
168 }
169
170 /* async aborts if taking too long or contended */
171 if (!cc->sync) {
Shaohua Li03dd8fe2012-10-08 16:32:27 -0700172 cc->contended = true;
Mel Gorman0ba33882012-08-21 16:16:17 -0700173 return false;
174 }
175
176 cond_resched();
Mel Gorman0ba33882012-08-21 16:16:17 -0700177 }
178
179 if (!locked)
180 spin_lock_irqsave(lock, *flags);
181 return true;
182}
183
184static inline bool compact_trylock_irqsave(spinlock_t *lock,
185 unsigned long *flags, struct compact_control *cc)
186{
187 return compact_checklock_irqsave(lock, flags, false, cc);
188}
189
Mel Gormand6ed9722012-10-08 16:32:36 -0700190/* Returns true if the page is within a block suitable for migration to */
191static bool suitable_migration_target(struct page *page)
192{
193 int migratetype = get_pageblock_migratetype(page);
194
195 /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
196 if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
197 return false;
198
199 /* If the page is a large free page, then allow migration */
200 if (PageBuddy(page) && page_order(page) >= pageblock_order)
201 return true;
202
203 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
204 if (migrate_async_suitable(migratetype))
205 return true;
206
207 /* Otherwise skip the block */
208 return false;
209}
210
Mel Gormanc74068b2012-10-08 16:29:12 -0700211static void compact_capture_page(struct compact_control *cc)
212{
213 unsigned long flags;
214 int mtype, mtype_low, mtype_high;
215
216 if (!cc->page || *cc->page)
217 return;
218
219 /*
220 * For MIGRATE_MOVABLE allocations we capture a suitable page ASAP
221 * regardless of the migratetype of the freelist is is captured from.
222 * This is fine because the order for a high-order MIGRATE_MOVABLE
223 * allocation is typically at least a pageblock size and overall
224 * fragmentation is not impaired. Other allocation types must
225 * capture pages from their own migratelist because otherwise they
226 * could pollute other pageblocks like MIGRATE_MOVABLE with
227 * difficult to move pages and making fragmentation worse overall.
228 */
229 if (cc->migratetype == MIGRATE_MOVABLE) {
230 mtype_low = 0;
231 mtype_high = MIGRATE_PCPTYPES;
232 } else {
233 mtype_low = cc->migratetype;
234 mtype_high = cc->migratetype + 1;
235 }
236
237 /* Speculatively examine the free lists without zone lock */
238 for (mtype = mtype_low; mtype < mtype_high; mtype++) {
239 int order;
240 for (order = cc->order; order < MAX_ORDER; order++) {
241 struct page *page;
242 struct free_area *area;
243 area = &(cc->zone->free_area[order]);
244 if (list_empty(&area->free_list[mtype]))
245 continue;
246
247 /* Take the lock and attempt capture of the page */
248 if (!compact_trylock_irqsave(&cc->zone->lock, &flags, cc))
249 return;
250 if (!list_empty(&area->free_list[mtype])) {
251 page = list_entry(area->free_list[mtype].next,
252 struct page, lru);
253 if (capture_free_page(page, cc->order, mtype)) {
254 spin_unlock_irqrestore(&cc->zone->lock,
255 flags);
256 *cc->page = page;
257 return;
258 }
259 }
260 spin_unlock_irqrestore(&cc->zone->lock, flags);
261 }
262 }
263}
264
Mel Gorman0ba33882012-08-21 16:16:17 -0700265/*
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100266 * Isolate free pages onto a private freelist. Caller must hold zone->lock.
267 * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
268 * pages inside of the pageblock (even though it may still end up isolating
269 * some pages).
270 */
Mel Gormand6ed9722012-10-08 16:32:36 -0700271static unsigned long isolate_freepages_block(struct compact_control *cc,
272 unsigned long blockpfn,
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100273 unsigned long end_pfn,
274 struct list_head *freelist,
275 bool strict)
Mel Gorman748446b2010-05-24 14:32:27 -0700276{
Mel Gormanb7aba692011-01-13 15:45:54 -0800277 int nr_scanned = 0, total_isolated = 0;
Mel Gormancc5a88a2012-10-08 16:32:41 -0700278 struct page *cursor, *valid_page = NULL;
Mel Gormand6ed9722012-10-08 16:32:36 -0700279 unsigned long nr_strict_required = end_pfn - blockpfn;
280 unsigned long flags;
281 bool locked = false;
Mel Gorman748446b2010-05-24 14:32:27 -0700282
Mel Gorman748446b2010-05-24 14:32:27 -0700283 cursor = pfn_to_page(blockpfn);
284
Mel Gormand6ed9722012-10-08 16:32:36 -0700285 /* Isolate free pages. */
Mel Gorman748446b2010-05-24 14:32:27 -0700286 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
287 int isolated, i;
288 struct page *page = cursor;
289
Mel Gormanb7aba692011-01-13 15:45:54 -0800290 nr_scanned++;
Mel Gormand6ed9722012-10-08 16:32:36 -0700291 if (!pfn_valid_within(blockpfn))
Mel Gorman748446b2010-05-24 14:32:27 -0700292 continue;
Mel Gormancc5a88a2012-10-08 16:32:41 -0700293 if (!valid_page)
294 valid_page = page;
Mel Gormand6ed9722012-10-08 16:32:36 -0700295 if (!PageBuddy(page))
296 continue;
297
298 /*
299 * The zone lock must be held to isolate freepages.
300 * Unfortunately this is a very coarse lock and can be
301 * heavily contended if there are parallel allocations
302 * or parallel compactions. For async compaction do not
303 * spin on the lock and we acquire the lock as late as
304 * possible.
305 */
306 locked = compact_checklock_irqsave(&cc->zone->lock, &flags,
307 locked, cc);
308 if (!locked)
309 break;
310
311 /* Recheck this is a suitable migration target under lock */
312 if (!strict && !suitable_migration_target(page))
313 break;
314
315 /* Recheck this is a buddy page under lock */
316 if (!PageBuddy(page))
317 continue;
Mel Gorman748446b2010-05-24 14:32:27 -0700318
319 /* Found a free page, break it into order-0 pages */
320 isolated = split_free_page(page);
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100321 if (!isolated && strict)
Mel Gormand6ed9722012-10-08 16:32:36 -0700322 break;
Mel Gorman748446b2010-05-24 14:32:27 -0700323 total_isolated += isolated;
324 for (i = 0; i < isolated; i++) {
325 list_add(&page->lru, freelist);
326 page++;
327 }
328
329 /* If a page was split, advance to the end of it */
330 if (isolated) {
331 blockpfn += isolated - 1;
332 cursor += isolated - 1;
333 }
334 }
335
Mel Gormanb7aba692011-01-13 15:45:54 -0800336 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
Mel Gormand6ed9722012-10-08 16:32:36 -0700337
338 /*
339 * If strict isolation is requested by CMA then check that all the
340 * pages requested were isolated. If there were any failures, 0 is
341 * returned and CMA will fail.
342 */
343 if (strict && nr_strict_required != total_isolated)
344 total_isolated = 0;
345
346 if (locked)
347 spin_unlock_irqrestore(&cc->zone->lock, flags);
348
Mel Gormancc5a88a2012-10-08 16:32:41 -0700349 /* Update the pageblock-skip if the whole pageblock was scanned */
350 if (blockpfn == end_pfn)
Mel Gormanf57bc462012-10-08 16:32:45 -0700351 update_pageblock_skip(cc, valid_page, total_isolated, false);
Mel Gormancc5a88a2012-10-08 16:32:41 -0700352
Mel Gorman748446b2010-05-24 14:32:27 -0700353 return total_isolated;
354}
355
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100356/**
357 * isolate_freepages_range() - isolate free pages.
358 * @start_pfn: The first PFN to start isolating.
359 * @end_pfn: The one-past-last PFN.
360 *
361 * Non-free pages, invalid PFNs, or zone boundaries within the
362 * [start_pfn, end_pfn) range are considered errors, cause function to
363 * undo its actions and return zero.
364 *
365 * Otherwise, function returns one-past-the-last PFN of isolated page
366 * (which may be greater then end_pfn if end fell in a middle of
367 * a free page).
368 */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100369unsigned long
Mel Gormancc5a88a2012-10-08 16:32:41 -0700370isolate_freepages_range(struct compact_control *cc,
371 unsigned long start_pfn, unsigned long end_pfn)
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100372{
Mel Gormand6ed9722012-10-08 16:32:36 -0700373 unsigned long isolated, pfn, block_end_pfn;
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100374 LIST_HEAD(freelist);
375
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100376 for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
Mel Gormancc5a88a2012-10-08 16:32:41 -0700377 if (!pfn_valid(pfn) || cc->zone != page_zone(pfn_to_page(pfn)))
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100378 break;
379
380 /*
381 * On subsequent iterations ALIGN() is actually not needed,
382 * but we keep it that we not to complicate the code.
383 */
384 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
385 block_end_pfn = min(block_end_pfn, end_pfn);
386
Mel Gormancc5a88a2012-10-08 16:32:41 -0700387 isolated = isolate_freepages_block(cc, pfn, block_end_pfn,
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100388 &freelist, true);
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100389
390 /*
391 * In strict mode, isolate_freepages_block() returns 0 if
392 * there are any holes in the block (ie. invalid PFNs or
393 * non-free pages).
394 */
395 if (!isolated)
396 break;
397
398 /*
399 * If we managed to isolate pages, it is always (1 << n) *
400 * pageblock_nr_pages for some non-negative n. (Max order
401 * page may span two pageblocks).
402 */
403 }
404
405 /* split_free_page does not map the pages */
406 map_pages(&freelist);
407
408 if (pfn < end_pfn) {
409 /* Loop terminated early, cleanup. */
410 release_freepages(&freelist);
411 return 0;
412 }
413
414 /* We don't use freelists for anything. */
415 return pfn;
416}
417
Mel Gorman748446b2010-05-24 14:32:27 -0700418/* Update the number of anon and file isolated pages in the zone */
Mel Gorman0ba33882012-08-21 16:16:17 -0700419static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700420{
421 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700422 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700423
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700424 list_for_each_entry(page, &cc->migratepages, lru)
425 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700426
Mel Gorman0ba33882012-08-21 16:16:17 -0700427 /* If locked we can use the interrupt unsafe versions */
428 if (locked) {
429 __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
430 __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
431 } else {
432 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
433 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
434 }
Mel Gorman748446b2010-05-24 14:32:27 -0700435}
436
437/* Similar to reclaim, but different enough that they don't share logic */
438static bool too_many_isolated(struct zone *zone)
439{
Minchan Kimbc693042010-09-09 16:38:00 -0700440 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700441
442 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
443 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700444 active = zone_page_state(zone, NR_ACTIVE_FILE) +
445 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700446 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
447 zone_page_state(zone, NR_ISOLATED_ANON);
448
Minchan Kimbc693042010-09-09 16:38:00 -0700449 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700450}
451
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100452/**
453 * isolate_migratepages_range() - isolate all migrate-able pages in range.
454 * @zone: Zone pages are in.
455 * @cc: Compaction control structure.
456 * @low_pfn: The first PFN of the range.
457 * @end_pfn: The one-past-the-last PFN of the range.
458 *
459 * Isolate all pages that can be migrated from the range specified by
460 * [low_pfn, end_pfn). Returns zero if there is a fatal signal
461 * pending), otherwise PFN of the first page that was not scanned
462 * (which may be both less, equal to or more then end_pfn).
463 *
464 * Assumes that cc->migratepages is empty and cc->nr_migratepages is
465 * zero.
466 *
467 * Apart from cc->migratepages and cc->nr_migratetypes this function
468 * does not modify any cc's fields, in particular it does not modify
469 * (or read for that matter) cc->migrate_pfn.
Mel Gorman748446b2010-05-24 14:32:27 -0700470 */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100471unsigned long
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100472isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
473 unsigned long low_pfn, unsigned long end_pfn)
Mel Gorman748446b2010-05-24 14:32:27 -0700474{
Mel Gorman9927af742011-01-13 15:45:59 -0800475 unsigned long last_pageblock_nr = 0, pageblock_nr;
Mel Gormanb7aba692011-01-13 15:45:54 -0800476 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700477 struct list_head *migratelist = &cc->migratepages;
Konstantin Khlebnikovfa168092012-05-29 15:06:54 -0700478 isolate_mode_t mode = 0;
Mel Gorman0ba33882012-08-21 16:16:17 -0700479 unsigned long flags;
Mel Gorman6a38cba2012-10-08 16:32:33 -0700480 bool locked = false;
Mel Gormancc5a88a2012-10-08 16:32:41 -0700481 struct page *page = NULL, *valid_page = NULL;
Mel Gorman748446b2010-05-24 14:32:27 -0700482
Mel Gorman748446b2010-05-24 14:32:27 -0700483 /*
484 * Ensure that there are not too many pages isolated from the LRU
485 * list by either parallel reclaimers or compaction. If there are,
486 * delay for some time until fewer pages are isolated
487 */
488 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700489 /* async migration should just abort */
490 if (!cc->sync)
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100491 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700492
Mel Gorman748446b2010-05-24 14:32:27 -0700493 congestion_wait(BLK_RW_ASYNC, HZ/10);
494
495 if (fatal_signal_pending(current))
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100496 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700497 }
498
499 /* Time to isolate some pages for migration */
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700500 cond_resched();
Mel Gorman748446b2010-05-24 14:32:27 -0700501 for (; low_pfn < end_pfn; low_pfn++) {
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700502 /* give a chance to irqs before checking need_resched() */
Mel Gorman6a38cba2012-10-08 16:32:33 -0700503 if (locked && !((low_pfn+1) % SWAP_CLUSTER_MAX)) {
504 if (should_release_lock(&zone->lru_lock)) {
505 spin_unlock_irqrestore(&zone->lru_lock, flags);
506 locked = false;
507 }
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700508 }
Mel Gorman0ba33882012-08-21 16:16:17 -0700509
Mel Gorman0bf380b2012-02-03 15:37:18 -0800510 /*
511 * migrate_pfn does not necessarily start aligned to a
512 * pageblock. Ensure that pfn_valid is called when moving
513 * into a new MAX_ORDER_NR_PAGES range in case of large
514 * memory holes within the zone
515 */
516 if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
517 if (!pfn_valid(low_pfn)) {
518 low_pfn += MAX_ORDER_NR_PAGES - 1;
519 continue;
520 }
521 }
522
Mel Gorman748446b2010-05-24 14:32:27 -0700523 if (!pfn_valid_within(low_pfn))
524 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800525 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700526
Mel Gormandc908602012-02-08 17:13:38 -0800527 /*
528 * Get the page and ensure the page is within the same zone.
529 * See the comment in isolate_freepages about overlapping
530 * nodes. It is deliberate that the new zone lock is not taken
531 * as memory compaction should not move pages between nodes.
532 */
Mel Gorman748446b2010-05-24 14:32:27 -0700533 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800534 if (page_zone(page) != zone)
535 continue;
536
Mel Gormancc5a88a2012-10-08 16:32:41 -0700537 if (!valid_page)
538 valid_page = page;
539
540 /* If isolation recently failed, do not retry */
541 pageblock_nr = low_pfn >> pageblock_order;
542 if (!isolation_suitable(cc, page))
543 goto next_pageblock;
544
Mel Gormandc908602012-02-08 17:13:38 -0800545 /* Skip if free */
Mel Gorman748446b2010-05-24 14:32:27 -0700546 if (PageBuddy(page))
547 continue;
548
Mel Gorman9927af742011-01-13 15:45:59 -0800549 /*
550 * For async migration, also only scan in MOVABLE blocks. Async
551 * migration is optimistic to see if the minimum amount of work
552 * satisfies the allocation
553 */
Mel Gorman9927af742011-01-13 15:45:59 -0800554 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
Michal Nazarewiczd4158d22011-12-29 13:09:50 +0100555 !migrate_async_suitable(get_pageblock_migratetype(page))) {
Mel Gormanf57bc462012-10-08 16:32:45 -0700556 cc->finished_update_migrate = true;
Mel Gorman6a38cba2012-10-08 16:32:33 -0700557 goto next_pageblock;
Mel Gorman9927af742011-01-13 15:45:59 -0800558 }
559
Mel Gorman6a38cba2012-10-08 16:32:33 -0700560 /* Check may be lockless but that's ok as we recheck later */
Andrea Arcangelibc835012011-01-13 15:47:08 -0800561 if (!PageLRU(page))
562 continue;
563
564 /*
Mel Gorman6a38cba2012-10-08 16:32:33 -0700565 * PageLRU is set. lru_lock normally excludes isolation
566 * splitting and collapsing (collapsing has already happened
567 * if PageLRU is set) but the lock is not necessarily taken
568 * here and it is wasteful to take it just to check transhuge.
569 * Check TransHuge without lock and skip the whole pageblock if
570 * it's either a transhuge or hugetlbfs page, as calling
571 * compound_order() without preventing THP from splitting the
572 * page underneath us may return surprising results.
Andrea Arcangelibc835012011-01-13 15:47:08 -0800573 */
574 if (PageTransHuge(page)) {
Mel Gorman6a38cba2012-10-08 16:32:33 -0700575 if (!locked)
576 goto next_pageblock;
577 low_pfn += (1 << compound_order(page)) - 1;
578 continue;
579 }
580
581 /* Check if it is ok to still hold the lock */
582 locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
583 locked, cc);
584 if (!locked || fatal_signal_pending(current))
585 break;
586
587 /* Recheck PageLRU and PageTransHuge under lock */
588 if (!PageLRU(page))
589 continue;
590 if (PageTransHuge(page)) {
Andrea Arcangelibc835012011-01-13 15:47:08 -0800591 low_pfn += (1 << compound_order(page)) - 1;
592 continue;
593 }
594
Mel Gormanc8244932012-01-12 17:19:38 -0800595 if (!cc->sync)
596 mode |= ISOLATE_ASYNC_MIGRATE;
597
Mel Gorman748446b2010-05-24 14:32:27 -0700598 /* Try isolate the page */
Konstantin Khlebnikovfa168092012-05-29 15:06:54 -0700599 if (__isolate_lru_page(page, mode) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700600 continue;
601
Andrea Arcangelibc835012011-01-13 15:47:08 -0800602 VM_BUG_ON(PageTransCompound(page));
603
Mel Gorman748446b2010-05-24 14:32:27 -0700604 /* Successfully isolated */
Mel Gormanf57bc462012-10-08 16:32:45 -0700605 cc->finished_update_migrate = true;
Mel Gorman748446b2010-05-24 14:32:27 -0700606 del_page_from_lru_list(zone, page, page_lru(page));
607 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700608 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800609 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700610
611 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800612 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
613 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700614 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800615 }
Mel Gorman6a38cba2012-10-08 16:32:33 -0700616
617 continue;
618
619next_pageblock:
620 low_pfn += pageblock_nr_pages;
621 low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
622 last_pageblock_nr = pageblock_nr;
Mel Gorman748446b2010-05-24 14:32:27 -0700623 }
624
Mel Gorman0ba33882012-08-21 16:16:17 -0700625 acct_isolated(zone, locked, cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700626
Mel Gorman0ba33882012-08-21 16:16:17 -0700627 if (locked)
628 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700629
Mel Gormancc5a88a2012-10-08 16:32:41 -0700630 /* Update the pageblock-skip if the whole pageblock was scanned */
631 if (low_pfn == end_pfn)
Mel Gormanf57bc462012-10-08 16:32:45 -0700632 update_pageblock_skip(cc, valid_page, nr_isolated, true);
Mel Gormancc5a88a2012-10-08 16:32:41 -0700633
Mel Gormanb7aba692011-01-13 15:45:54 -0800634 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
635
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100636 return low_pfn;
637}
638
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100639#endif /* CONFIG_COMPACTION || CONFIG_CMA */
640#ifdef CONFIG_COMPACTION
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100641/*
642 * Based on information in the current compact_control, find blocks
643 * suitable for isolating free pages from and then isolate them.
644 */
645static void isolate_freepages(struct zone *zone,
646 struct compact_control *cc)
647{
648 struct page *page;
649 unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100650 int nr_freepages = cc->nr_freepages;
651 struct list_head *freelist = &cc->freepages;
652
653 /*
654 * Initialise the free scanner. The starting point is where we last
655 * scanned from (or the end of the zone if starting). The low point
656 * is the end of the pageblock the migration scanner is using.
657 */
658 pfn = cc->free_pfn;
659 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
660
661 /*
662 * Take care that if the migration scanner is at the end of the zone
663 * that the free scanner does not accidentally move to the next zone
664 * in the next isolation cycle.
665 */
666 high_pfn = min(low_pfn, pfn);
667
668 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
669
670 /*
671 * Isolate free pages until enough are available to migrate the
672 * pages on cc->migratepages. We stop searching if the migrate
673 * and free page scanners meet or enough free pages are isolated.
674 */
675 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
676 pfn -= pageblock_nr_pages) {
677 unsigned long isolated;
678
679 if (!pfn_valid(pfn))
680 continue;
681
682 /*
683 * Check for overlapping nodes/zones. It's possible on some
684 * configurations to have a setup like
685 * node0 node1 node0
686 * i.e. it's possible that all pages within a zones range of
687 * pages do not belong to a single zone.
688 */
689 page = pfn_to_page(pfn);
690 if (page_zone(page) != zone)
691 continue;
692
693 /* Check the block is suitable for migration */
694 if (!suitable_migration_target(page))
695 continue;
696
Mel Gormancc5a88a2012-10-08 16:32:41 -0700697 /* If isolation recently failed, do not retry */
698 if (!isolation_suitable(cc, page))
699 continue;
700
Mel Gormand6ed9722012-10-08 16:32:36 -0700701 /* Found a block suitable for isolating free pages from */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100702 isolated = 0;
Mel Gormand6ed9722012-10-08 16:32:36 -0700703 end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn);
704 isolated = isolate_freepages_block(cc, pfn, end_pfn,
705 freelist, false);
706 nr_freepages += isolated;
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100707
708 /*
709 * Record the highest PFN we isolated pages from. When next
710 * looking for free pages, the search will restart here as
711 * page migration may have returned some pages to the allocator
712 */
Mel Gormanf57bc462012-10-08 16:32:45 -0700713 if (isolated) {
714 cc->finished_update_free = true;
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100715 high_pfn = max(high_pfn, pfn);
Mel Gormanf57bc462012-10-08 16:32:45 -0700716 }
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100717 }
718
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100719 /* split_free_page does not map the pages */
720 map_pages(freelist);
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100721
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100722 cc->free_pfn = high_pfn;
723 cc->nr_freepages = nr_freepages;
Mel Gorman748446b2010-05-24 14:32:27 -0700724}
725
726/*
727 * This is a migrate-callback that "allocates" freepages by taking pages
728 * from the isolated freelists in the block we are migrating to.
729 */
730static struct page *compaction_alloc(struct page *migratepage,
731 unsigned long data,
732 int **result)
733{
734 struct compact_control *cc = (struct compact_control *)data;
735 struct page *freepage;
736
737 /* Isolate free pages if necessary */
738 if (list_empty(&cc->freepages)) {
739 isolate_freepages(cc->zone, cc);
740
741 if (list_empty(&cc->freepages))
742 return NULL;
743 }
744
745 freepage = list_entry(cc->freepages.next, struct page, lru);
746 list_del(&freepage->lru);
747 cc->nr_freepages--;
748
749 return freepage;
750}
751
752/*
753 * We cannot control nr_migratepages and nr_freepages fully when migration is
754 * running as migrate_pages() has no knowledge of compact_control. When
755 * migration is complete, we count the number of pages on the lists by hand.
756 */
757static void update_nr_listpages(struct compact_control *cc)
758{
759 int nr_migratepages = 0;
760 int nr_freepages = 0;
761 struct page *page;
762
763 list_for_each_entry(page, &cc->migratepages, lru)
764 nr_migratepages++;
765 list_for_each_entry(page, &cc->freepages, lru)
766 nr_freepages++;
767
768 cc->nr_migratepages = nr_migratepages;
769 cc->nr_freepages = nr_freepages;
770}
771
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100772/* possible outcome of isolate_migratepages */
773typedef enum {
774 ISOLATE_ABORT, /* Abort compaction now */
775 ISOLATE_NONE, /* No pages isolated, continue scanning */
776 ISOLATE_SUCCESS, /* Pages isolated, migrate */
777} isolate_migrate_t;
778
779/*
780 * Isolate all pages that can be migrated from the block pointed to by
781 * the migrate scanner within compact_control.
782 */
783static isolate_migrate_t isolate_migratepages(struct zone *zone,
784 struct compact_control *cc)
785{
786 unsigned long low_pfn, end_pfn;
787
788 /* Do not scan outside zone boundaries */
789 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
790
791 /* Only scan within a pageblock boundary */
792 end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
793
794 /* Do not cross the free scanner or scan within a memory hole */
795 if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
796 cc->migrate_pfn = end_pfn;
797 return ISOLATE_NONE;
798 }
799
800 /* Perform the isolation */
801 low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn);
Shaohua Li03dd8fe2012-10-08 16:32:27 -0700802 if (!low_pfn || cc->contended)
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100803 return ISOLATE_ABORT;
804
805 cc->migrate_pfn = low_pfn;
806
807 return ISOLATE_SUCCESS;
808}
809
Mel Gorman748446b2010-05-24 14:32:27 -0700810static int compact_finished(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800811 struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700812{
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800813 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -0700814
Mel Gorman748446b2010-05-24 14:32:27 -0700815 if (fatal_signal_pending(current))
816 return COMPACT_PARTIAL;
817
818 /* Compaction run completes if the migrate and free scanner meet */
Mel Gormancc5a88a2012-10-08 16:32:41 -0700819 if (cc->free_pfn <= cc->migrate_pfn) {
820 reset_isolation_suitable(cc->zone);
Mel Gorman748446b2010-05-24 14:32:27 -0700821 return COMPACT_COMPLETE;
Mel Gormancc5a88a2012-10-08 16:32:41 -0700822 }
Mel Gorman748446b2010-05-24 14:32:27 -0700823
Johannes Weiner82478fb2011-01-20 14:44:21 -0800824 /*
825 * order == -1 is expected when compacting via
826 * /proc/sys/vm/compact_memory
827 */
Mel Gorman56de7262010-05-24 14:32:30 -0700828 if (cc->order == -1)
829 return COMPACT_CONTINUE;
830
Michal Hocko3957c772011-06-15 15:08:25 -0700831 /* Compaction run is not finished if the watermark is not met */
832 watermark = low_wmark_pages(zone);
833 watermark += (1 << cc->order);
834
835 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
836 return COMPACT_CONTINUE;
837
Mel Gorman56de7262010-05-24 14:32:30 -0700838 /* Direct compactor: Is a suitable page free? */
Mel Gormanc74068b2012-10-08 16:29:12 -0700839 if (cc->page) {
840 /* Was a suitable page captured? */
841 if (*cc->page)
Mel Gorman56de7262010-05-24 14:32:30 -0700842 return COMPACT_PARTIAL;
Mel Gormanc74068b2012-10-08 16:29:12 -0700843 } else {
844 unsigned int order;
845 for (order = cc->order; order < MAX_ORDER; order++) {
846 struct free_area *area = &zone->free_area[cc->order];
847 /* Job done if page is free of the right migratetype */
848 if (!list_empty(&area->free_list[cc->migratetype]))
849 return COMPACT_PARTIAL;
Mel Gorman56de7262010-05-24 14:32:30 -0700850
Mel Gormanc74068b2012-10-08 16:29:12 -0700851 /* Job done if allocation would set block type */
852 if (cc->order >= pageblock_order && area->nr_free)
853 return COMPACT_PARTIAL;
854 }
Mel Gorman56de7262010-05-24 14:32:30 -0700855 }
856
Mel Gorman748446b2010-05-24 14:32:27 -0700857 return COMPACT_CONTINUE;
858}
859
Mel Gorman3e7d3442011-01-13 15:45:56 -0800860/*
861 * compaction_suitable: Is this suitable to run compaction on this zone now?
862 * Returns
863 * COMPACT_SKIPPED - If there are too few free pages for compaction
864 * COMPACT_PARTIAL - If the allocation would succeed without compaction
865 * COMPACT_CONTINUE - If compaction should run now
866 */
867unsigned long compaction_suitable(struct zone *zone, int order)
868{
869 int fragindex;
870 unsigned long watermark;
871
872 /*
Michal Hocko3957c772011-06-15 15:08:25 -0700873 * order == -1 is expected when compacting via
874 * /proc/sys/vm/compact_memory
875 */
876 if (order == -1)
877 return COMPACT_CONTINUE;
878
879 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -0800880 * Watermarks for order-0 must be met for compaction. Note the 2UL.
881 * This is because during migration, copies of pages need to be
882 * allocated and for a short time, the footprint is higher
883 */
884 watermark = low_wmark_pages(zone) + (2UL << order);
885 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
886 return COMPACT_SKIPPED;
887
888 /*
889 * fragmentation index determines if allocation failures are due to
890 * low memory or external fragmentation
891 *
Shaohua Lia582a732011-06-15 15:08:49 -0700892 * index of -1000 implies allocations might succeed depending on
893 * watermarks
Mel Gorman3e7d3442011-01-13 15:45:56 -0800894 * index towards 0 implies failure is due to lack of memory
895 * index towards 1000 implies failure is due to fragmentation
896 *
897 * Only compact if a failure would be due to fragmentation.
898 */
899 fragindex = fragmentation_index(zone, order);
900 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
901 return COMPACT_SKIPPED;
902
Shaohua Lia582a732011-06-15 15:08:49 -0700903 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
904 0, 0))
Mel Gorman3e7d3442011-01-13 15:45:56 -0800905 return COMPACT_PARTIAL;
906
907 return COMPACT_CONTINUE;
908}
909
Mel Gorman748446b2010-05-24 14:32:27 -0700910static int compact_zone(struct zone *zone, struct compact_control *cc)
911{
912 int ret;
Mel Gormanf57bc462012-10-08 16:32:45 -0700913 unsigned long start_pfn = zone->zone_start_pfn;
914 unsigned long end_pfn = zone->zone_start_pfn + zone->spanned_pages;
Mel Gorman748446b2010-05-24 14:32:27 -0700915
Mel Gorman3e7d3442011-01-13 15:45:56 -0800916 ret = compaction_suitable(zone, cc->order);
917 switch (ret) {
918 case COMPACT_PARTIAL:
919 case COMPACT_SKIPPED:
920 /* Compaction is likely to fail */
921 return ret;
922 case COMPACT_CONTINUE:
923 /* Fall through to compaction */
924 ;
925 }
926
Mel Gormanf57bc462012-10-08 16:32:45 -0700927 /*
928 * Setup to move all movable pages to the end of the zone. Used cached
929 * information on where the scanners should start but check that it
930 * is initialised by ensuring the values are within zone boundaries.
931 */
932 cc->migrate_pfn = zone->compact_cached_migrate_pfn;
933 cc->free_pfn = zone->compact_cached_free_pfn;
934 if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
935 cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
936 zone->compact_cached_free_pfn = cc->free_pfn;
937 }
938 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
939 cc->migrate_pfn = start_pfn;
940 zone->compact_cached_migrate_pfn = cc->migrate_pfn;
941 }
Mel Gorman748446b2010-05-24 14:32:27 -0700942
Mel Gormancc5a88a2012-10-08 16:32:41 -0700943 /* Clear pageblock skip if there are numerous alloc failures */
944 if (zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT)
945 reset_isolation_suitable(zone);
946
Mel Gorman748446b2010-05-24 14:32:27 -0700947 migrate_prep_local();
948
949 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
950 unsigned long nr_migrate, nr_remaining;
Minchan Kim9d502c12011-03-22 16:30:39 -0700951 int err;
Mel Gorman748446b2010-05-24 14:32:27 -0700952
Mel Gormanf9e35b32011-06-15 15:08:52 -0700953 switch (isolate_migratepages(zone, cc)) {
954 case ISOLATE_ABORT:
955 ret = COMPACT_PARTIAL;
Shaohua Li03dd8fe2012-10-08 16:32:27 -0700956 putback_lru_pages(&cc->migratepages);
957 cc->nr_migratepages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700958 goto out;
959 case ISOLATE_NONE:
Mel Gorman748446b2010-05-24 14:32:27 -0700960 continue;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700961 case ISOLATE_SUCCESS:
962 ;
963 }
Mel Gorman748446b2010-05-24 14:32:27 -0700964
965 nr_migrate = cc->nr_migratepages;
Minchan Kim9d502c12011-03-22 16:30:39 -0700966 err = migrate_pages(&cc->migratepages, compaction_alloc,
Mel Gorman7f0f2492011-01-13 15:45:58 -0800967 (unsigned long)cc, false,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800968 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
Mel Gorman748446b2010-05-24 14:32:27 -0700969 update_nr_listpages(cc);
970 nr_remaining = cc->nr_migratepages;
971
972 count_vm_event(COMPACTBLOCKS);
973 count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
974 if (nr_remaining)
975 count_vm_events(COMPACTPAGEFAILED, nr_remaining);
Mel Gormanb7aba692011-01-13 15:45:54 -0800976 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
977 nr_remaining);
Mel Gorman748446b2010-05-24 14:32:27 -0700978
979 /* Release LRU pages not migrated */
Minchan Kim9d502c12011-03-22 16:30:39 -0700980 if (err) {
Mel Gorman748446b2010-05-24 14:32:27 -0700981 putback_lru_pages(&cc->migratepages);
982 cc->nr_migratepages = 0;
David Rientjes7a08b442012-07-11 14:02:13 -0700983 if (err == -ENOMEM) {
984 ret = COMPACT_PARTIAL;
985 goto out;
986 }
Mel Gorman748446b2010-05-24 14:32:27 -0700987 }
Mel Gormanc74068b2012-10-08 16:29:12 -0700988
989 /* Capture a page now if it is a suitable size */
990 compact_capture_page(cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700991 }
992
Mel Gormanf9e35b32011-06-15 15:08:52 -0700993out:
Mel Gorman748446b2010-05-24 14:32:27 -0700994 /* Release free pages and check accounting */
995 cc->nr_freepages -= release_freepages(&cc->freepages);
996 VM_BUG_ON(cc->nr_freepages != 0);
997
998 return ret;
999}
Mel Gorman76ab0f52010-05-24 14:32:28 -07001000
Kyungmin Parkd43a87e2011-10-31 17:09:08 -07001001static unsigned long compact_zone_order(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -08001002 int order, gfp_t gfp_mask,
Mel Gormanc74068b2012-10-08 16:29:12 -07001003 bool sync, bool *contended,
1004 struct page **page)
Mel Gorman56de7262010-05-24 14:32:30 -07001005{
Shaohua Li03dd8fe2012-10-08 16:32:27 -07001006 unsigned long ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001007 struct compact_control cc = {
1008 .nr_freepages = 0,
1009 .nr_migratepages = 0,
1010 .order = order,
1011 .migratetype = allocflags_to_migratetype(gfp_mask),
1012 .zone = zone,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001013 .sync = sync,
Mel Gormanc74068b2012-10-08 16:29:12 -07001014 .page = page,
Mel Gorman56de7262010-05-24 14:32:30 -07001015 };
1016 INIT_LIST_HEAD(&cc.freepages);
1017 INIT_LIST_HEAD(&cc.migratepages);
1018
Shaohua Li03dd8fe2012-10-08 16:32:27 -07001019 ret = compact_zone(zone, &cc);
1020
1021 VM_BUG_ON(!list_empty(&cc.freepages));
1022 VM_BUG_ON(!list_empty(&cc.migratepages));
1023
1024 *contended = cc.contended;
1025 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001026}
1027
Mel Gorman5e771902010-05-24 14:32:31 -07001028int sysctl_extfrag_threshold = 500;
1029
Mel Gorman56de7262010-05-24 14:32:30 -07001030/**
1031 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
1032 * @zonelist: The zonelist used for the current allocation
1033 * @order: The order of the current allocation
1034 * @gfp_mask: The GFP mask of the current allocation
1035 * @nodemask: The allowed nodes to allocate from
Mel Gorman77f1fe62011-01-13 15:45:57 -08001036 * @sync: Whether migration is synchronous or not
Mel Gorman8a063192012-10-08 16:32:31 -07001037 * @contended: Return value that is true if compaction was aborted due to lock contention
1038 * @page: Optionally capture a free page of the requested order during compaction
Mel Gorman56de7262010-05-24 14:32:30 -07001039 *
1040 * This is the main entry point for direct page compaction.
1041 */
1042unsigned long try_to_compact_pages(struct zonelist *zonelist,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001043 int order, gfp_t gfp_mask, nodemask_t *nodemask,
Mel Gormanc74068b2012-10-08 16:29:12 -07001044 bool sync, bool *contended, struct page **page)
Mel Gorman56de7262010-05-24 14:32:30 -07001045{
1046 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
1047 int may_enter_fs = gfp_mask & __GFP_FS;
1048 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -07001049 struct zoneref *z;
1050 struct zone *zone;
1051 int rc = COMPACT_SKIPPED;
Bartlomiej Zolnierkiewicz850bf192012-10-08 16:32:05 -07001052 int alloc_flags = 0;
Mel Gorman56de7262010-05-24 14:32:30 -07001053
Mel Gormanbc337a92012-10-08 16:29:09 -07001054 /* Check if the GFP flags allow compaction */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -08001055 if (!order || !may_enter_fs || !may_perform_io)
Mel Gorman56de7262010-05-24 14:32:30 -07001056 return rc;
1057
1058 count_vm_event(COMPACTSTALL);
1059
Bartlomiej Zolnierkiewicz850bf192012-10-08 16:32:05 -07001060#ifdef CONFIG_CMA
1061 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
1062 alloc_flags |= ALLOC_CMA;
1063#endif
Mel Gorman56de7262010-05-24 14:32:30 -07001064 /* Compact each zone in the list */
1065 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
1066 nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -07001067 int status;
1068
Mel Gorman0ba33882012-08-21 16:16:17 -07001069 status = compact_zone_order(zone, order, gfp_mask, sync,
Mel Gormanc74068b2012-10-08 16:29:12 -07001070 contended, page);
Mel Gorman56de7262010-05-24 14:32:30 -07001071 rc = max(status, rc);
1072
Mel Gorman3e7d3442011-01-13 15:45:56 -08001073 /* If a normal allocation would succeed, stop compacting */
Bartlomiej Zolnierkiewicz850bf192012-10-08 16:32:05 -07001074 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
1075 alloc_flags))
Mel Gorman56de7262010-05-24 14:32:30 -07001076 break;
1077 }
1078
1079 return rc;
1080}
1081
1082
Mel Gorman76ab0f52010-05-24 14:32:28 -07001083/* Compact all zones within a node */
Rik van Riel7be62de2012-03-21 16:33:52 -07001084static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001085{
1086 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -07001087 struct zone *zone;
1088
Mel Gorman76ab0f52010-05-24 14:32:28 -07001089 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -07001090
1091 zone = &pgdat->node_zones[zoneid];
1092 if (!populated_zone(zone))
1093 continue;
1094
Rik van Riel7be62de2012-03-21 16:33:52 -07001095 cc->nr_freepages = 0;
1096 cc->nr_migratepages = 0;
1097 cc->zone = zone;
1098 INIT_LIST_HEAD(&cc->freepages);
1099 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001100
Dan Carpenteraad6ec32012-03-21 16:33:54 -07001101 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -07001102 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001103
Rik van Rielaff62242012-03-21 16:33:52 -07001104 if (cc->order > 0) {
1105 int ok = zone_watermark_ok(zone, cc->order,
1106 low_wmark_pages(zone), 0, 0);
Minchan Kim57bb21c2012-08-21 16:16:03 -07001107 if (ok && cc->order >= zone->compact_order_failed)
Rik van Rielaff62242012-03-21 16:33:52 -07001108 zone->compact_order_failed = cc->order + 1;
1109 /* Currently async compaction is never deferred. */
1110 else if (!ok && cc->sync)
1111 defer_compaction(zone, cc->order);
1112 }
1113
Rik van Riel7be62de2012-03-21 16:33:52 -07001114 VM_BUG_ON(!list_empty(&cc->freepages));
1115 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -07001116 }
1117
1118 return 0;
1119}
1120
Rik van Riel7be62de2012-03-21 16:33:52 -07001121int compact_pgdat(pg_data_t *pgdat, int order)
1122{
1123 struct compact_control cc = {
1124 .order = order,
1125 .sync = false,
Mel Gormanc74068b2012-10-08 16:29:12 -07001126 .page = NULL,
Rik van Riel7be62de2012-03-21 16:33:52 -07001127 };
1128
1129 return __compact_pgdat(pgdat, &cc);
1130}
1131
1132static int compact_node(int nid)
1133{
Rik van Riel7be62de2012-03-21 16:33:52 -07001134 struct compact_control cc = {
1135 .order = -1,
1136 .sync = true,
Mel Gormanc74068b2012-10-08 16:29:12 -07001137 .page = NULL,
Rik van Riel7be62de2012-03-21 16:33:52 -07001138 };
1139
Hugh Dickins8575ec22012-03-21 16:33:53 -07001140 return __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001141}
1142
Mel Gorman76ab0f52010-05-24 14:32:28 -07001143/* Compact all nodes in the system */
Jason Liuc0b96522013-01-11 14:31:47 -08001144static void compact_nodes(void)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001145{
1146 int nid;
1147
Hugh Dickins8575ec22012-03-21 16:33:53 -07001148 /* Flush pending updates to the LRU lists */
1149 lru_add_drain_all();
1150
Mel Gorman76ab0f52010-05-24 14:32:28 -07001151 for_each_online_node(nid)
1152 compact_node(nid);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001153}
1154
1155/* The written value is actually unused, all memory is compacted */
1156int sysctl_compact_memory;
1157
1158/* This is the entry point for compacting all nodes via /proc/sys/vm */
1159int sysctl_compaction_handler(struct ctl_table *table, int write,
1160 void __user *buffer, size_t *length, loff_t *ppos)
1161{
1162 if (write)
Jason Liuc0b96522013-01-11 14:31:47 -08001163 compact_nodes();
Mel Gorman76ab0f52010-05-24 14:32:28 -07001164
1165 return 0;
1166}
Mel Gormaned4a6d72010-05-24 14:32:29 -07001167
Mel Gorman5e771902010-05-24 14:32:31 -07001168int sysctl_extfrag_handler(struct ctl_table *table, int write,
1169 void __user *buffer, size_t *length, loff_t *ppos)
1170{
1171 proc_dointvec_minmax(table, write, buffer, length, ppos);
1172
1173 return 0;
1174}
1175
Mel Gormaned4a6d72010-05-24 14:32:29 -07001176#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Kay Sievers10fbcf42011-12-21 14:48:43 -08001177ssize_t sysfs_compact_node(struct device *dev,
1178 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -07001179 const char *buf, size_t count)
1180{
Hugh Dickins8575ec22012-03-21 16:33:53 -07001181 int nid = dev->id;
1182
1183 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1184 /* Flush pending updates to the LRU lists */
1185 lru_add_drain_all();
1186
1187 compact_node(nid);
1188 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07001189
1190 return count;
1191}
Kay Sievers10fbcf42011-12-21 14:48:43 -08001192static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001193
1194int compaction_register_node(struct node *node)
1195{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001196 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001197}
1198
1199void compaction_unregister_node(struct node *node)
1200{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001201 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001202}
1203#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +01001204
1205#endif /* CONFIG_COMPACTION */