blob: 3e10a7c68af69d3776624e6ad6d84feab5edd900 [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
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +010053/*
Mel Gorman27d1af82012-08-21 16:16:17 -070054 * Compaction requires the taking of some coarse locks that are potentially
55 * very heavily contended. Check if the process needs to be scheduled or
56 * if the lock is contended. For async compaction, back out in the event
57 * if contention is severe. For sync compaction, schedule.
58 *
59 * Returns true if the lock is held.
60 * Returns false if the lock is released and compaction should abort
61 */
62static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags,
63 bool locked, struct compact_control *cc)
64{
65 if (need_resched() || spin_is_contended(lock)) {
66 if (locked) {
67 spin_unlock_irqrestore(lock, *flags);
68 locked = false;
69 }
70
71 /* async aborts if taking too long or contended */
72 if (!cc->sync) {
73 if (cc->contended)
74 *cc->contended = true;
75 return false;
76 }
77
78 cond_resched();
79 if (fatal_signal_pending(current))
80 return false;
81 }
82
83 if (!locked)
84 spin_lock_irqsave(lock, *flags);
85 return true;
86}
87
88static inline bool compact_trylock_irqsave(spinlock_t *lock,
89 unsigned long *flags, struct compact_control *cc)
90{
91 return compact_checklock_irqsave(lock, flags, false, cc);
92}
93
94/*
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +010095 * Isolate free pages onto a private freelist. Caller must hold zone->lock.
96 * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
97 * pages inside of the pageblock (even though it may still end up isolating
98 * some pages).
99 */
100static unsigned long isolate_freepages_block(unsigned long blockpfn,
101 unsigned long end_pfn,
102 struct list_head *freelist,
103 bool strict)
Mel Gorman748446b2010-05-24 14:32:27 -0700104{
Mel Gormanb7aba692011-01-13 15:45:54 -0800105 int nr_scanned = 0, total_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700106 struct page *cursor;
107
Mel Gorman748446b2010-05-24 14:32:27 -0700108 cursor = pfn_to_page(blockpfn);
109
110 /* Isolate free pages. This assumes the block is valid */
111 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
112 int isolated, i;
113 struct page *page = cursor;
114
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100115 if (!pfn_valid_within(blockpfn)) {
116 if (strict)
117 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700118 continue;
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100119 }
Mel Gormanb7aba692011-01-13 15:45:54 -0800120 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700121
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100122 if (!PageBuddy(page)) {
123 if (strict)
124 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700125 continue;
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100126 }
Mel Gorman748446b2010-05-24 14:32:27 -0700127
128 /* Found a free page, break it into order-0 pages */
129 isolated = split_free_page(page);
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100130 if (!isolated && strict)
131 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700132 total_isolated += isolated;
133 for (i = 0; i < isolated; i++) {
134 list_add(&page->lru, freelist);
135 page++;
136 }
137
138 /* If a page was split, advance to the end of it */
139 if (isolated) {
140 blockpfn += isolated - 1;
141 cursor += isolated - 1;
142 }
143 }
144
Mel Gormanb7aba692011-01-13 15:45:54 -0800145 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
Mel Gorman748446b2010-05-24 14:32:27 -0700146 return total_isolated;
147}
148
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100149/**
150 * isolate_freepages_range() - isolate free pages.
151 * @start_pfn: The first PFN to start isolating.
152 * @end_pfn: The one-past-last PFN.
153 *
154 * Non-free pages, invalid PFNs, or zone boundaries within the
155 * [start_pfn, end_pfn) range are considered errors, cause function to
156 * undo its actions and return zero.
157 *
158 * Otherwise, function returns one-past-the-last PFN of isolated page
159 * (which may be greater then end_pfn if end fell in a middle of
160 * a free page).
161 */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100162unsigned long
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100163isolate_freepages_range(unsigned long start_pfn, unsigned long end_pfn)
164{
165 unsigned long isolated, pfn, block_end_pfn, flags;
166 struct zone *zone = NULL;
167 LIST_HEAD(freelist);
168
169 if (pfn_valid(start_pfn))
170 zone = page_zone(pfn_to_page(start_pfn));
171
172 for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
173 if (!pfn_valid(pfn) || zone != page_zone(pfn_to_page(pfn)))
174 break;
175
176 /*
177 * On subsequent iterations ALIGN() is actually not needed,
178 * but we keep it that we not to complicate the code.
179 */
180 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
181 block_end_pfn = min(block_end_pfn, end_pfn);
182
183 spin_lock_irqsave(&zone->lock, flags);
184 isolated = isolate_freepages_block(pfn, block_end_pfn,
185 &freelist, true);
186 spin_unlock_irqrestore(&zone->lock, flags);
187
188 /*
189 * In strict mode, isolate_freepages_block() returns 0 if
190 * there are any holes in the block (ie. invalid PFNs or
191 * non-free pages).
192 */
193 if (!isolated)
194 break;
195
196 /*
197 * If we managed to isolate pages, it is always (1 << n) *
198 * pageblock_nr_pages for some non-negative n. (Max order
199 * page may span two pageblocks).
200 */
201 }
202
203 /* split_free_page does not map the pages */
204 map_pages(&freelist);
205
206 if (pfn < end_pfn) {
207 /* Loop terminated early, cleanup. */
208 release_freepages(&freelist);
209 return 0;
210 }
211
212 /* We don't use freelists for anything. */
213 return pfn;
214}
215
Mel Gorman748446b2010-05-24 14:32:27 -0700216/* Update the number of anon and file isolated pages in the zone */
Mel Gorman27d1af82012-08-21 16:16:17 -0700217static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700218{
219 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700220 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700221
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700222 list_for_each_entry(page, &cc->migratepages, lru)
223 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700224
Mel Gorman27d1af82012-08-21 16:16:17 -0700225 /* If locked we can use the interrupt unsafe versions */
226 if (locked) {
227 __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
228 __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
229 } else {
230 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
231 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
232 }
Mel Gorman748446b2010-05-24 14:32:27 -0700233}
234
235/* Similar to reclaim, but different enough that they don't share logic */
236static bool too_many_isolated(struct zone *zone)
237{
Minchan Kimbc693042010-09-09 16:38:00 -0700238 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700239
240 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
241 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700242 active = zone_page_state(zone, NR_ACTIVE_FILE) +
243 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700244 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
245 zone_page_state(zone, NR_ISOLATED_ANON);
246
Minchan Kimbc693042010-09-09 16:38:00 -0700247 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700248}
249
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100250/**
251 * isolate_migratepages_range() - isolate all migrate-able pages in range.
252 * @zone: Zone pages are in.
253 * @cc: Compaction control structure.
254 * @low_pfn: The first PFN of the range.
255 * @end_pfn: The one-past-the-last PFN of the range.
256 *
257 * Isolate all pages that can be migrated from the range specified by
258 * [low_pfn, end_pfn). Returns zero if there is a fatal signal
259 * pending), otherwise PFN of the first page that was not scanned
260 * (which may be both less, equal to or more then end_pfn).
261 *
262 * Assumes that cc->migratepages is empty and cc->nr_migratepages is
263 * zero.
264 *
265 * Apart from cc->migratepages and cc->nr_migratetypes this function
266 * does not modify any cc's fields, in particular it does not modify
267 * (or read for that matter) cc->migrate_pfn.
Mel Gorman748446b2010-05-24 14:32:27 -0700268 */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100269unsigned long
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100270isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
271 unsigned long low_pfn, unsigned long end_pfn)
Mel Gorman748446b2010-05-24 14:32:27 -0700272{
Mel Gorman9927af742011-01-13 15:45:59 -0800273 unsigned long last_pageblock_nr = 0, pageblock_nr;
Mel Gormanb7aba692011-01-13 15:45:54 -0800274 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700275 struct list_head *migratelist = &cc->migratepages;
Konstantin Khlebnikov6d8a50e2012-05-29 15:06:54 -0700276 isolate_mode_t mode = 0;
Mel Gorman27d1af82012-08-21 16:16:17 -0700277 unsigned long flags;
278 bool locked;
Mel Gorman748446b2010-05-24 14:32:27 -0700279
Mel Gorman748446b2010-05-24 14:32:27 -0700280 /*
281 * Ensure that there are not too many pages isolated from the LRU
282 * list by either parallel reclaimers or compaction. If there are,
283 * delay for some time until fewer pages are isolated
284 */
285 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700286 /* async migration should just abort */
287 if (!cc->sync)
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100288 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700289
Mel Gorman748446b2010-05-24 14:32:27 -0700290 congestion_wait(BLK_RW_ASYNC, HZ/10);
291
292 if (fatal_signal_pending(current))
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100293 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700294 }
295
296 /* Time to isolate some pages for migration */
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700297 cond_resched();
Mel Gorman27d1af82012-08-21 16:16:17 -0700298 spin_lock_irqsave(&zone->lru_lock, flags);
299 locked = true;
Mel Gorman748446b2010-05-24 14:32:27 -0700300 for (; low_pfn < end_pfn; low_pfn++) {
301 struct page *page;
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700302
303 /* give a chance to irqs before checking need_resched() */
304 if (!((low_pfn+1) % SWAP_CLUSTER_MAX)) {
Mel Gorman27d1af82012-08-21 16:16:17 -0700305 spin_unlock_irqrestore(&zone->lru_lock, flags);
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700306 locked = false;
307 }
Mel Gorman27d1af82012-08-21 16:16:17 -0700308
309 /* Check if it is ok to still hold the lock */
310 locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
311 locked, cc);
312 if (!locked)
313 break;
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700314
Mel Gorman0bf380b2012-02-03 15:37:18 -0800315 /*
316 * migrate_pfn does not necessarily start aligned to a
317 * pageblock. Ensure that pfn_valid is called when moving
318 * into a new MAX_ORDER_NR_PAGES range in case of large
319 * memory holes within the zone
320 */
321 if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
322 if (!pfn_valid(low_pfn)) {
323 low_pfn += MAX_ORDER_NR_PAGES - 1;
324 continue;
325 }
326 }
327
Mel Gorman748446b2010-05-24 14:32:27 -0700328 if (!pfn_valid_within(low_pfn))
329 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800330 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700331
Mel Gormandc908602012-02-08 17:13:38 -0800332 /*
333 * Get the page and ensure the page is within the same zone.
334 * See the comment in isolate_freepages about overlapping
335 * nodes. It is deliberate that the new zone lock is not taken
336 * as memory compaction should not move pages between nodes.
337 */
Mel Gorman748446b2010-05-24 14:32:27 -0700338 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800339 if (page_zone(page) != zone)
340 continue;
341
342 /* Skip if free */
Mel Gorman748446b2010-05-24 14:32:27 -0700343 if (PageBuddy(page))
344 continue;
345
Mel Gorman9927af742011-01-13 15:45:59 -0800346 /*
347 * For async migration, also only scan in MOVABLE blocks. Async
348 * migration is optimistic to see if the minimum amount of work
349 * satisfies the allocation
350 */
351 pageblock_nr = low_pfn >> pageblock_order;
352 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
Michal Nazarewiczd4158d22011-12-29 13:09:50 +0100353 !migrate_async_suitable(get_pageblock_migratetype(page))) {
Mel Gorman9927af742011-01-13 15:45:59 -0800354 low_pfn += pageblock_nr_pages;
355 low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
356 last_pageblock_nr = pageblock_nr;
357 continue;
358 }
359
Andrea Arcangelibc835012011-01-13 15:47:08 -0800360 if (!PageLRU(page))
361 continue;
362
363 /*
364 * PageLRU is set, and lru_lock excludes isolation,
365 * splitting and collapsing (collapsing has already
366 * happened if PageLRU is set).
367 */
368 if (PageTransHuge(page)) {
369 low_pfn += (1 << compound_order(page)) - 1;
370 continue;
371 }
372
Mel Gormanc8244932012-01-12 17:19:38 -0800373 if (!cc->sync)
374 mode |= ISOLATE_ASYNC_MIGRATE;
375
Mel Gorman748446b2010-05-24 14:32:27 -0700376 /* Try isolate the page */
Konstantin Khlebnikov6d8a50e2012-05-29 15:06:54 -0700377 if (__isolate_lru_page(page, mode) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700378 continue;
379
Andrea Arcangelibc835012011-01-13 15:47:08 -0800380 VM_BUG_ON(PageTransCompound(page));
381
Mel Gorman748446b2010-05-24 14:32:27 -0700382 /* Successfully isolated */
383 del_page_from_lru_list(zone, page, page_lru(page));
384 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700385 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800386 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700387
388 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800389 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
390 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700391 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800392 }
Mel Gorman748446b2010-05-24 14:32:27 -0700393 }
394
Mel Gorman27d1af82012-08-21 16:16:17 -0700395 acct_isolated(zone, locked, cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700396
Mel Gorman27d1af82012-08-21 16:16:17 -0700397 if (locked)
398 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700399
Mel Gormanb7aba692011-01-13 15:45:54 -0800400 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
401
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100402 return low_pfn;
403}
404
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100405#endif /* CONFIG_COMPACTION || CONFIG_CMA */
406#ifdef CONFIG_COMPACTION
407
408/* Returns true if the page is within a block suitable for migration to */
409static bool suitable_migration_target(struct page *page)
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100410{
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100411
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100412 int migratetype = get_pageblock_migratetype(page);
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100413
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100414 /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
415 if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
416 return false;
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100417
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100418 /* If the page is a large free page, then allow migration */
419 if (PageBuddy(page) && page_order(page) >= pageblock_order)
420 return true;
421
Michal Nazarewiczd4158d22011-12-29 13:09:50 +0100422 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
423 if (migrate_async_suitable(migratetype))
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100424 return true;
425
426 /* Otherwise skip the block */
427 return false;
428}
429
430/*
431 * Based on information in the current compact_control, find blocks
432 * suitable for isolating free pages from and then isolate them.
433 */
434static void isolate_freepages(struct zone *zone,
435 struct compact_control *cc)
436{
437 struct page *page;
438 unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
439 unsigned long flags;
440 int nr_freepages = cc->nr_freepages;
441 struct list_head *freelist = &cc->freepages;
442
443 /*
444 * Initialise the free scanner. The starting point is where we last
445 * scanned from (or the end of the zone if starting). The low point
446 * is the end of the pageblock the migration scanner is using.
447 */
448 pfn = cc->free_pfn;
449 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
450
451 /*
452 * Take care that if the migration scanner is at the end of the zone
453 * that the free scanner does not accidentally move to the next zone
454 * in the next isolation cycle.
455 */
456 high_pfn = min(low_pfn, pfn);
457
458 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
459
460 /*
461 * Isolate free pages until enough are available to migrate the
462 * pages on cc->migratepages. We stop searching if the migrate
463 * and free page scanners meet or enough free pages are isolated.
464 */
465 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
466 pfn -= pageblock_nr_pages) {
467 unsigned long isolated;
468
469 if (!pfn_valid(pfn))
470 continue;
471
472 /*
473 * Check for overlapping nodes/zones. It's possible on some
474 * configurations to have a setup like
475 * node0 node1 node0
476 * i.e. it's possible that all pages within a zones range of
477 * pages do not belong to a single zone.
478 */
479 page = pfn_to_page(pfn);
480 if (page_zone(page) != zone)
481 continue;
482
483 /* Check the block is suitable for migration */
484 if (!suitable_migration_target(page))
485 continue;
486
487 /*
488 * Found a block suitable for isolating free pages from. Now
489 * we disabled interrupts, double check things are ok and
490 * isolate the pages. This is to minimise the time IRQs
491 * are disabled
492 */
493 isolated = 0;
Mel Gorman27d1af82012-08-21 16:16:17 -0700494
495 /*
496 * The zone lock must be held to isolate freepages. This
497 * unfortunately this is a very coarse lock and can be
498 * heavily contended if there are parallel allocations
499 * or parallel compactions. For async compaction do not
500 * spin on the lock
501 */
502 if (!compact_trylock_irqsave(&zone->lock, &flags, cc))
503 break;
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100504 if (suitable_migration_target(page)) {
505 end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn);
506 isolated = isolate_freepages_block(pfn, end_pfn,
507 freelist, false);
508 nr_freepages += isolated;
509 }
510 spin_unlock_irqrestore(&zone->lock, flags);
511
512 /*
513 * Record the highest PFN we isolated pages from. When next
514 * looking for free pages, the search will restart here as
515 * page migration may have returned some pages to the allocator
516 */
517 if (isolated)
518 high_pfn = max(high_pfn, pfn);
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100519 }
520
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100521 /* split_free_page does not map the pages */
522 map_pages(freelist);
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100523
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100524 cc->free_pfn = high_pfn;
525 cc->nr_freepages = nr_freepages;
Mel Gorman748446b2010-05-24 14:32:27 -0700526}
527
528/*
529 * This is a migrate-callback that "allocates" freepages by taking pages
530 * from the isolated freelists in the block we are migrating to.
531 */
532static struct page *compaction_alloc(struct page *migratepage,
533 unsigned long data,
534 int **result)
535{
536 struct compact_control *cc = (struct compact_control *)data;
537 struct page *freepage;
538
539 /* Isolate free pages if necessary */
540 if (list_empty(&cc->freepages)) {
541 isolate_freepages(cc->zone, cc);
542
543 if (list_empty(&cc->freepages))
544 return NULL;
545 }
546
547 freepage = list_entry(cc->freepages.next, struct page, lru);
548 list_del(&freepage->lru);
549 cc->nr_freepages--;
550
551 return freepage;
552}
553
554/*
555 * We cannot control nr_migratepages and nr_freepages fully when migration is
556 * running as migrate_pages() has no knowledge of compact_control. When
557 * migration is complete, we count the number of pages on the lists by hand.
558 */
559static void update_nr_listpages(struct compact_control *cc)
560{
561 int nr_migratepages = 0;
562 int nr_freepages = 0;
563 struct page *page;
564
565 list_for_each_entry(page, &cc->migratepages, lru)
566 nr_migratepages++;
567 list_for_each_entry(page, &cc->freepages, lru)
568 nr_freepages++;
569
570 cc->nr_migratepages = nr_migratepages;
571 cc->nr_freepages = nr_freepages;
572}
573
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100574/* possible outcome of isolate_migratepages */
575typedef enum {
576 ISOLATE_ABORT, /* Abort compaction now */
577 ISOLATE_NONE, /* No pages isolated, continue scanning */
578 ISOLATE_SUCCESS, /* Pages isolated, migrate */
579} isolate_migrate_t;
580
581/*
582 * Isolate all pages that can be migrated from the block pointed to by
583 * the migrate scanner within compact_control.
584 */
585static isolate_migrate_t isolate_migratepages(struct zone *zone,
586 struct compact_control *cc)
587{
588 unsigned long low_pfn, end_pfn;
589
590 /* Do not scan outside zone boundaries */
591 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
592
593 /* Only scan within a pageblock boundary */
594 end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
595
596 /* Do not cross the free scanner or scan within a memory hole */
597 if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
598 cc->migrate_pfn = end_pfn;
599 return ISOLATE_NONE;
600 }
601
602 /* Perform the isolation */
603 low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn);
604 if (!low_pfn)
605 return ISOLATE_ABORT;
606
607 cc->migrate_pfn = low_pfn;
608
609 return ISOLATE_SUCCESS;
610}
611
Mel Gorman748446b2010-05-24 14:32:27 -0700612static int compact_finished(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800613 struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700614{
Mel Gorman56de7262010-05-24 14:32:30 -0700615 unsigned int order;
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800616 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -0700617
Mel Gorman748446b2010-05-24 14:32:27 -0700618 if (fatal_signal_pending(current))
619 return COMPACT_PARTIAL;
620
621 /* Compaction run completes if the migrate and free scanner meet */
622 if (cc->free_pfn <= cc->migrate_pfn)
623 return COMPACT_COMPLETE;
624
Johannes Weiner82478fb2011-01-20 14:44:21 -0800625 /*
626 * order == -1 is expected when compacting via
627 * /proc/sys/vm/compact_memory
628 */
Mel Gorman56de7262010-05-24 14:32:30 -0700629 if (cc->order == -1)
630 return COMPACT_CONTINUE;
631
Michal Hocko3957c772011-06-15 15:08:25 -0700632 /* Compaction run is not finished if the watermark is not met */
633 watermark = low_wmark_pages(zone);
634 watermark += (1 << cc->order);
635
636 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
637 return COMPACT_CONTINUE;
638
Mel Gorman56de7262010-05-24 14:32:30 -0700639 /* Direct compactor: Is a suitable page free? */
640 for (order = cc->order; order < MAX_ORDER; order++) {
641 /* Job done if page is free of the right migratetype */
642 if (!list_empty(&zone->free_area[order].free_list[cc->migratetype]))
643 return COMPACT_PARTIAL;
644
645 /* Job done if allocation would set block type */
646 if (order >= pageblock_order && zone->free_area[order].nr_free)
647 return COMPACT_PARTIAL;
648 }
649
Mel Gorman748446b2010-05-24 14:32:27 -0700650 return COMPACT_CONTINUE;
651}
652
Mel Gorman3e7d3442011-01-13 15:45:56 -0800653/*
654 * compaction_suitable: Is this suitable to run compaction on this zone now?
655 * Returns
656 * COMPACT_SKIPPED - If there are too few free pages for compaction
657 * COMPACT_PARTIAL - If the allocation would succeed without compaction
658 * COMPACT_CONTINUE - If compaction should run now
659 */
660unsigned long compaction_suitable(struct zone *zone, int order)
661{
662 int fragindex;
663 unsigned long watermark;
664
665 /*
Michal Hocko3957c772011-06-15 15:08:25 -0700666 * order == -1 is expected when compacting via
667 * /proc/sys/vm/compact_memory
668 */
669 if (order == -1)
670 return COMPACT_CONTINUE;
671
672 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -0800673 * Watermarks for order-0 must be met for compaction. Note the 2UL.
674 * This is because during migration, copies of pages need to be
675 * allocated and for a short time, the footprint is higher
676 */
677 watermark = low_wmark_pages(zone) + (2UL << order);
678 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
679 return COMPACT_SKIPPED;
680
681 /*
682 * fragmentation index determines if allocation failures are due to
683 * low memory or external fragmentation
684 *
Shaohua Lia582a732011-06-15 15:08:49 -0700685 * index of -1000 implies allocations might succeed depending on
686 * watermarks
Mel Gorman3e7d3442011-01-13 15:45:56 -0800687 * index towards 0 implies failure is due to lack of memory
688 * index towards 1000 implies failure is due to fragmentation
689 *
690 * Only compact if a failure would be due to fragmentation.
691 */
692 fragindex = fragmentation_index(zone, order);
693 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
694 return COMPACT_SKIPPED;
695
Shaohua Lia582a732011-06-15 15:08:49 -0700696 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
697 0, 0))
Mel Gorman3e7d3442011-01-13 15:45:56 -0800698 return COMPACT_PARTIAL;
699
700 return COMPACT_CONTINUE;
701}
702
Mel Gorman748446b2010-05-24 14:32:27 -0700703static int compact_zone(struct zone *zone, struct compact_control *cc)
704{
705 int ret;
706
Mel Gorman3e7d3442011-01-13 15:45:56 -0800707 ret = compaction_suitable(zone, cc->order);
708 switch (ret) {
709 case COMPACT_PARTIAL:
710 case COMPACT_SKIPPED:
711 /* Compaction is likely to fail */
712 return ret;
713 case COMPACT_CONTINUE:
714 /* Fall through to compaction */
715 ;
716 }
717
Mel Gorman748446b2010-05-24 14:32:27 -0700718 /* Setup to move all movable pages to the end of the zone */
719 cc->migrate_pfn = zone->zone_start_pfn;
720 cc->free_pfn = cc->migrate_pfn + zone->spanned_pages;
721 cc->free_pfn &= ~(pageblock_nr_pages-1);
722
723 migrate_prep_local();
724
725 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
726 unsigned long nr_migrate, nr_remaining;
Minchan Kim9d502c12011-03-22 16:30:39 -0700727 int err;
Mel Gorman748446b2010-05-24 14:32:27 -0700728
Mel Gormanf9e35b32011-06-15 15:08:52 -0700729 switch (isolate_migratepages(zone, cc)) {
730 case ISOLATE_ABORT:
731 ret = COMPACT_PARTIAL;
732 goto out;
733 case ISOLATE_NONE:
Mel Gorman748446b2010-05-24 14:32:27 -0700734 continue;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700735 case ISOLATE_SUCCESS:
736 ;
737 }
Mel Gorman748446b2010-05-24 14:32:27 -0700738
739 nr_migrate = cc->nr_migratepages;
Minchan Kim9d502c12011-03-22 16:30:39 -0700740 err = migrate_pages(&cc->migratepages, compaction_alloc,
Mel Gorman7f0f2492011-01-13 15:45:58 -0800741 (unsigned long)cc, false,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800742 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
Mel Gorman748446b2010-05-24 14:32:27 -0700743 update_nr_listpages(cc);
744 nr_remaining = cc->nr_migratepages;
745
746 count_vm_event(COMPACTBLOCKS);
747 count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
748 if (nr_remaining)
749 count_vm_events(COMPACTPAGEFAILED, nr_remaining);
Mel Gormanb7aba692011-01-13 15:45:54 -0800750 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
751 nr_remaining);
Mel Gorman748446b2010-05-24 14:32:27 -0700752
753 /* Release LRU pages not migrated */
Minchan Kim9d502c12011-03-22 16:30:39 -0700754 if (err) {
Mel Gorman748446b2010-05-24 14:32:27 -0700755 putback_lru_pages(&cc->migratepages);
756 cc->nr_migratepages = 0;
David Rientjesa56063e2012-07-11 14:02:13 -0700757 if (err == -ENOMEM) {
758 ret = COMPACT_PARTIAL;
759 goto out;
760 }
Mel Gorman748446b2010-05-24 14:32:27 -0700761 }
Mel Gorman748446b2010-05-24 14:32:27 -0700762 }
763
Mel Gormanf9e35b32011-06-15 15:08:52 -0700764out:
Mel Gorman748446b2010-05-24 14:32:27 -0700765 /* Release free pages and check accounting */
766 cc->nr_freepages -= release_freepages(&cc->freepages);
767 VM_BUG_ON(cc->nr_freepages != 0);
768
769 return ret;
770}
Mel Gorman76ab0f52010-05-24 14:32:28 -0700771
Kyungmin Parkd43a87e2011-10-31 17:09:08 -0700772static unsigned long compact_zone_order(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800773 int order, gfp_t gfp_mask,
Mel Gorman27d1af82012-08-21 16:16:17 -0700774 bool sync, bool *contended)
Mel Gorman56de7262010-05-24 14:32:30 -0700775{
776 struct compact_control cc = {
777 .nr_freepages = 0,
778 .nr_migratepages = 0,
779 .order = order,
780 .migratetype = allocflags_to_migratetype(gfp_mask),
781 .zone = zone,
Mel Gorman77f1fe62011-01-13 15:45:57 -0800782 .sync = sync,
Mel Gorman27d1af82012-08-21 16:16:17 -0700783 .contended = contended,
Mel Gorman56de7262010-05-24 14:32:30 -0700784 };
785 INIT_LIST_HEAD(&cc.freepages);
786 INIT_LIST_HEAD(&cc.migratepages);
787
788 return compact_zone(zone, &cc);
789}
790
Mel Gorman5e771902010-05-24 14:32:31 -0700791int sysctl_extfrag_threshold = 500;
792
Mel Gorman56de7262010-05-24 14:32:30 -0700793/**
794 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
795 * @zonelist: The zonelist used for the current allocation
796 * @order: The order of the current allocation
797 * @gfp_mask: The GFP mask of the current allocation
798 * @nodemask: The allowed nodes to allocate from
Mel Gorman77f1fe62011-01-13 15:45:57 -0800799 * @sync: Whether migration is synchronous or not
Mel Gorman56de7262010-05-24 14:32:30 -0700800 *
801 * This is the main entry point for direct page compaction.
802 */
803unsigned long try_to_compact_pages(struct zonelist *zonelist,
Mel Gorman77f1fe62011-01-13 15:45:57 -0800804 int order, gfp_t gfp_mask, nodemask_t *nodemask,
Mel Gorman27d1af82012-08-21 16:16:17 -0700805 bool sync, bool *contended)
Mel Gorman56de7262010-05-24 14:32:30 -0700806{
807 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
808 int may_enter_fs = gfp_mask & __GFP_FS;
809 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -0700810 struct zoneref *z;
811 struct zone *zone;
812 int rc = COMPACT_SKIPPED;
Bartlomiej Zolnierkiewicz19644312012-10-08 16:32:05 -0700813 int alloc_flags = 0;
Mel Gorman56de7262010-05-24 14:32:30 -0700814
Mel Gorman05fd3fd2012-10-08 16:29:09 -0700815 /* Check if the GFP flags allow compaction */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -0800816 if (!order || !may_enter_fs || !may_perform_io)
Mel Gorman56de7262010-05-24 14:32:30 -0700817 return rc;
818
819 count_vm_event(COMPACTSTALL);
820
Bartlomiej Zolnierkiewicz19644312012-10-08 16:32:05 -0700821#ifdef CONFIG_CMA
822 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
823 alloc_flags |= ALLOC_CMA;
824#endif
Mel Gorman56de7262010-05-24 14:32:30 -0700825 /* Compact each zone in the list */
826 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
827 nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -0700828 int status;
829
Mel Gorman27d1af82012-08-21 16:16:17 -0700830 status = compact_zone_order(zone, order, gfp_mask, sync,
831 contended);
Mel Gorman56de7262010-05-24 14:32:30 -0700832 rc = max(status, rc);
833
Mel Gorman3e7d3442011-01-13 15:45:56 -0800834 /* If a normal allocation would succeed, stop compacting */
Bartlomiej Zolnierkiewicz19644312012-10-08 16:32:05 -0700835 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
836 alloc_flags))
Mel Gorman56de7262010-05-24 14:32:30 -0700837 break;
838 }
839
840 return rc;
841}
842
843
Mel Gorman76ab0f52010-05-24 14:32:28 -0700844/* Compact all zones within a node */
Rik van Riel7be62de2012-03-21 16:33:52 -0700845static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -0700846{
847 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -0700848 struct zone *zone;
849
Mel Gorman76ab0f52010-05-24 14:32:28 -0700850 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -0700851
852 zone = &pgdat->node_zones[zoneid];
853 if (!populated_zone(zone))
854 continue;
855
Rik van Riel7be62de2012-03-21 16:33:52 -0700856 cc->nr_freepages = 0;
857 cc->nr_migratepages = 0;
858 cc->zone = zone;
859 INIT_LIST_HEAD(&cc->freepages);
860 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -0700861
Dan Carpenteraad6ec32012-03-21 16:33:54 -0700862 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -0700863 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -0700864
Rik van Rielaff62242012-03-21 16:33:52 -0700865 if (cc->order > 0) {
866 int ok = zone_watermark_ok(zone, cc->order,
867 low_wmark_pages(zone), 0, 0);
Minchan Kimfc83d282012-08-21 16:16:03 -0700868 if (ok && cc->order >= zone->compact_order_failed)
Rik van Rielaff62242012-03-21 16:33:52 -0700869 zone->compact_order_failed = cc->order + 1;
870 /* Currently async compaction is never deferred. */
871 else if (!ok && cc->sync)
872 defer_compaction(zone, cc->order);
873 }
874
Rik van Riel7be62de2012-03-21 16:33:52 -0700875 VM_BUG_ON(!list_empty(&cc->freepages));
876 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -0700877 }
878
879 return 0;
880}
881
Rik van Riel7be62de2012-03-21 16:33:52 -0700882int compact_pgdat(pg_data_t *pgdat, int order)
883{
884 struct compact_control cc = {
885 .order = order,
886 .sync = false,
887 };
888
889 return __compact_pgdat(pgdat, &cc);
890}
891
892static int compact_node(int nid)
893{
Rik van Riel7be62de2012-03-21 16:33:52 -0700894 struct compact_control cc = {
895 .order = -1,
896 .sync = true,
897 };
898
Hugh Dickins8575ec22012-03-21 16:33:53 -0700899 return __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -0700900}
901
Mel Gorman76ab0f52010-05-24 14:32:28 -0700902/* Compact all nodes in the system */
903static int compact_nodes(void)
904{
905 int nid;
906
Hugh Dickins8575ec22012-03-21 16:33:53 -0700907 /* Flush pending updates to the LRU lists */
908 lru_add_drain_all();
909
Mel Gorman76ab0f52010-05-24 14:32:28 -0700910 for_each_online_node(nid)
911 compact_node(nid);
912
913 return COMPACT_COMPLETE;
914}
915
916/* The written value is actually unused, all memory is compacted */
917int sysctl_compact_memory;
918
919/* This is the entry point for compacting all nodes via /proc/sys/vm */
920int sysctl_compaction_handler(struct ctl_table *table, int write,
921 void __user *buffer, size_t *length, loff_t *ppos)
922{
923 if (write)
924 return compact_nodes();
925
926 return 0;
927}
Mel Gormaned4a6d72010-05-24 14:32:29 -0700928
Mel Gorman5e771902010-05-24 14:32:31 -0700929int sysctl_extfrag_handler(struct ctl_table *table, int write,
930 void __user *buffer, size_t *length, loff_t *ppos)
931{
932 proc_dointvec_minmax(table, write, buffer, length, ppos);
933
934 return 0;
935}
936
Mel Gormaned4a6d72010-05-24 14:32:29 -0700937#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Kay Sievers10fbcf42011-12-21 14:48:43 -0800938ssize_t sysfs_compact_node(struct device *dev,
939 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -0700940 const char *buf, size_t count)
941{
Hugh Dickins8575ec22012-03-21 16:33:53 -0700942 int nid = dev->id;
943
944 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
945 /* Flush pending updates to the LRU lists */
946 lru_add_drain_all();
947
948 compact_node(nid);
949 }
Mel Gormaned4a6d72010-05-24 14:32:29 -0700950
951 return count;
952}
Kay Sievers10fbcf42011-12-21 14:48:43 -0800953static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -0700954
955int compaction_register_node(struct node *node)
956{
Kay Sievers10fbcf42011-12-21 14:48:43 -0800957 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -0700958}
959
960void compaction_unregister_node(struct node *node)
961{
Kay Sievers10fbcf42011-12-21 14:48:43 -0800962 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -0700963}
964#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100965
966#endif /* CONFIG_COMPACTION */