blob: 832c4183dccce2e48c398f066f6e6cb2c2750f46 [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 Gorman2a1402a2012-10-08 16:32:33 -070053static inline bool should_release_lock(spinlock_t *lock)
54{
55 return need_resched() || spin_is_contended(lock);
56}
57
Michal Nazarewicz85aa1252012-01-30 13:24:03 +010058/*
Mel Gormanc67fe372012-08-21 16:16:17 -070059 * Compaction requires the taking of some coarse locks that are potentially
60 * very heavily contended. Check if the process needs to be scheduled or
61 * if the lock is contended. For async compaction, back out in the event
62 * if contention is severe. For sync compaction, schedule.
63 *
64 * Returns true if the lock is held.
65 * Returns false if the lock is released and compaction should abort
66 */
67static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags,
68 bool locked, struct compact_control *cc)
69{
Mel Gorman2a1402a2012-10-08 16:32:33 -070070 if (should_release_lock(lock)) {
Mel Gormanc67fe372012-08-21 16:16:17 -070071 if (locked) {
72 spin_unlock_irqrestore(lock, *flags);
73 locked = false;
74 }
75
76 /* async aborts if taking too long or contended */
77 if (!cc->sync) {
Shaohua Lie64c5232012-10-08 16:32:27 -070078 cc->contended = true;
Mel Gormanc67fe372012-08-21 16:16:17 -070079 return false;
80 }
81
82 cond_resched();
Mel Gormanc67fe372012-08-21 16:16:17 -070083 }
84
85 if (!locked)
86 spin_lock_irqsave(lock, *flags);
87 return true;
88}
89
90static inline bool compact_trylock_irqsave(spinlock_t *lock,
91 unsigned long *flags, struct compact_control *cc)
92{
93 return compact_checklock_irqsave(lock, flags, false, cc);
94}
95
Mel Gorman1fb3f8c2012-10-08 16:29:12 -070096static void compact_capture_page(struct compact_control *cc)
97{
98 unsigned long flags;
99 int mtype, mtype_low, mtype_high;
100
101 if (!cc->page || *cc->page)
102 return;
103
104 /*
105 * For MIGRATE_MOVABLE allocations we capture a suitable page ASAP
106 * regardless of the migratetype of the freelist is is captured from.
107 * This is fine because the order for a high-order MIGRATE_MOVABLE
108 * allocation is typically at least a pageblock size and overall
109 * fragmentation is not impaired. Other allocation types must
110 * capture pages from their own migratelist because otherwise they
111 * could pollute other pageblocks like MIGRATE_MOVABLE with
112 * difficult to move pages and making fragmentation worse overall.
113 */
114 if (cc->migratetype == MIGRATE_MOVABLE) {
115 mtype_low = 0;
116 mtype_high = MIGRATE_PCPTYPES;
117 } else {
118 mtype_low = cc->migratetype;
119 mtype_high = cc->migratetype + 1;
120 }
121
122 /* Speculatively examine the free lists without zone lock */
123 for (mtype = mtype_low; mtype < mtype_high; mtype++) {
124 int order;
125 for (order = cc->order; order < MAX_ORDER; order++) {
126 struct page *page;
127 struct free_area *area;
128 area = &(cc->zone->free_area[order]);
129 if (list_empty(&area->free_list[mtype]))
130 continue;
131
132 /* Take the lock and attempt capture of the page */
133 if (!compact_trylock_irqsave(&cc->zone->lock, &flags, cc))
134 return;
135 if (!list_empty(&area->free_list[mtype])) {
136 page = list_entry(area->free_list[mtype].next,
137 struct page, lru);
138 if (capture_free_page(page, cc->order, mtype)) {
139 spin_unlock_irqrestore(&cc->zone->lock,
140 flags);
141 *cc->page = page;
142 return;
143 }
144 }
145 spin_unlock_irqrestore(&cc->zone->lock, flags);
146 }
147 }
148}
149
Mel Gormanc67fe372012-08-21 16:16:17 -0700150/*
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100151 * Isolate free pages onto a private freelist. Caller must hold zone->lock.
152 * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
153 * pages inside of the pageblock (even though it may still end up isolating
154 * some pages).
155 */
156static unsigned long isolate_freepages_block(unsigned long blockpfn,
157 unsigned long end_pfn,
158 struct list_head *freelist,
159 bool strict)
Mel Gorman748446b2010-05-24 14:32:27 -0700160{
Mel Gormanb7aba692011-01-13 15:45:54 -0800161 int nr_scanned = 0, total_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700162 struct page *cursor;
163
Mel Gorman748446b2010-05-24 14:32:27 -0700164 cursor = pfn_to_page(blockpfn);
165
166 /* Isolate free pages. This assumes the block is valid */
167 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
168 int isolated, i;
169 struct page *page = cursor;
170
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100171 if (!pfn_valid_within(blockpfn)) {
172 if (strict)
173 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700174 continue;
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100175 }
Mel Gormanb7aba692011-01-13 15:45:54 -0800176 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700177
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100178 if (!PageBuddy(page)) {
179 if (strict)
180 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700181 continue;
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100182 }
Mel Gorman748446b2010-05-24 14:32:27 -0700183
184 /* Found a free page, break it into order-0 pages */
185 isolated = split_free_page(page);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100186 if (!isolated && strict)
187 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700188 total_isolated += isolated;
189 for (i = 0; i < isolated; i++) {
190 list_add(&page->lru, freelist);
191 page++;
192 }
193
194 /* If a page was split, advance to the end of it */
195 if (isolated) {
196 blockpfn += isolated - 1;
197 cursor += isolated - 1;
198 }
199 }
200
Mel Gormanb7aba692011-01-13 15:45:54 -0800201 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
Mel Gorman748446b2010-05-24 14:32:27 -0700202 return total_isolated;
203}
204
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100205/**
206 * isolate_freepages_range() - isolate free pages.
207 * @start_pfn: The first PFN to start isolating.
208 * @end_pfn: The one-past-last PFN.
209 *
210 * Non-free pages, invalid PFNs, or zone boundaries within the
211 * [start_pfn, end_pfn) range are considered errors, cause function to
212 * undo its actions and return zero.
213 *
214 * Otherwise, function returns one-past-the-last PFN of isolated page
215 * (which may be greater then end_pfn if end fell in a middle of
216 * a free page).
217 */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100218unsigned long
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100219isolate_freepages_range(unsigned long start_pfn, unsigned long end_pfn)
220{
221 unsigned long isolated, pfn, block_end_pfn, flags;
222 struct zone *zone = NULL;
223 LIST_HEAD(freelist);
224
225 if (pfn_valid(start_pfn))
226 zone = page_zone(pfn_to_page(start_pfn));
227
228 for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
229 if (!pfn_valid(pfn) || zone != page_zone(pfn_to_page(pfn)))
230 break;
231
232 /*
233 * On subsequent iterations ALIGN() is actually not needed,
234 * but we keep it that we not to complicate the code.
235 */
236 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
237 block_end_pfn = min(block_end_pfn, end_pfn);
238
239 spin_lock_irqsave(&zone->lock, flags);
240 isolated = isolate_freepages_block(pfn, block_end_pfn,
241 &freelist, true);
242 spin_unlock_irqrestore(&zone->lock, flags);
243
244 /*
245 * In strict mode, isolate_freepages_block() returns 0 if
246 * there are any holes in the block (ie. invalid PFNs or
247 * non-free pages).
248 */
249 if (!isolated)
250 break;
251
252 /*
253 * If we managed to isolate pages, it is always (1 << n) *
254 * pageblock_nr_pages for some non-negative n. (Max order
255 * page may span two pageblocks).
256 */
257 }
258
259 /* split_free_page does not map the pages */
260 map_pages(&freelist);
261
262 if (pfn < end_pfn) {
263 /* Loop terminated early, cleanup. */
264 release_freepages(&freelist);
265 return 0;
266 }
267
268 /* We don't use freelists for anything. */
269 return pfn;
270}
271
Mel Gorman748446b2010-05-24 14:32:27 -0700272/* Update the number of anon and file isolated pages in the zone */
Mel Gormanc67fe372012-08-21 16:16:17 -0700273static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700274{
275 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700276 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700277
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700278 list_for_each_entry(page, &cc->migratepages, lru)
279 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700280
Mel Gormanc67fe372012-08-21 16:16:17 -0700281 /* If locked we can use the interrupt unsafe versions */
282 if (locked) {
283 __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
284 __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
285 } else {
286 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
287 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
288 }
Mel Gorman748446b2010-05-24 14:32:27 -0700289}
290
291/* Similar to reclaim, but different enough that they don't share logic */
292static bool too_many_isolated(struct zone *zone)
293{
Minchan Kimbc693042010-09-09 16:38:00 -0700294 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700295
296 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
297 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700298 active = zone_page_state(zone, NR_ACTIVE_FILE) +
299 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700300 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
301 zone_page_state(zone, NR_ISOLATED_ANON);
302
Minchan Kimbc693042010-09-09 16:38:00 -0700303 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700304}
305
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100306/**
307 * isolate_migratepages_range() - isolate all migrate-able pages in range.
308 * @zone: Zone pages are in.
309 * @cc: Compaction control structure.
310 * @low_pfn: The first PFN of the range.
311 * @end_pfn: The one-past-the-last PFN of the range.
312 *
313 * Isolate all pages that can be migrated from the range specified by
314 * [low_pfn, end_pfn). Returns zero if there is a fatal signal
315 * pending), otherwise PFN of the first page that was not scanned
316 * (which may be both less, equal to or more then end_pfn).
317 *
318 * Assumes that cc->migratepages is empty and cc->nr_migratepages is
319 * zero.
320 *
321 * Apart from cc->migratepages and cc->nr_migratetypes this function
322 * does not modify any cc's fields, in particular it does not modify
323 * (or read for that matter) cc->migrate_pfn.
Mel Gorman748446b2010-05-24 14:32:27 -0700324 */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100325unsigned long
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100326isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
327 unsigned long low_pfn, unsigned long end_pfn)
Mel Gorman748446b2010-05-24 14:32:27 -0700328{
Mel Gorman9927af742011-01-13 15:45:59 -0800329 unsigned long last_pageblock_nr = 0, pageblock_nr;
Mel Gormanb7aba692011-01-13 15:45:54 -0800330 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700331 struct list_head *migratelist = &cc->migratepages;
Konstantin Khlebnikovf3fd4a62012-05-29 15:06:54 -0700332 isolate_mode_t mode = 0;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700333 struct lruvec *lruvec;
Mel Gormanc67fe372012-08-21 16:16:17 -0700334 unsigned long flags;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700335 bool locked = false;
Mel Gorman748446b2010-05-24 14:32:27 -0700336
Mel Gorman748446b2010-05-24 14:32:27 -0700337 /*
338 * Ensure that there are not too many pages isolated from the LRU
339 * list by either parallel reclaimers or compaction. If there are,
340 * delay for some time until fewer pages are isolated
341 */
342 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700343 /* async migration should just abort */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700344 if (!cc->sync)
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100345 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700346
Mel Gorman748446b2010-05-24 14:32:27 -0700347 congestion_wait(BLK_RW_ASYNC, HZ/10);
348
349 if (fatal_signal_pending(current))
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100350 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700351 }
352
353 /* Time to isolate some pages for migration */
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700354 cond_resched();
Mel Gorman748446b2010-05-24 14:32:27 -0700355 for (; low_pfn < end_pfn; low_pfn++) {
356 struct page *page;
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700357
358 /* give a chance to irqs before checking need_resched() */
Mel Gorman2a1402a2012-10-08 16:32:33 -0700359 if (locked && !((low_pfn+1) % SWAP_CLUSTER_MAX)) {
360 if (should_release_lock(&zone->lru_lock)) {
361 spin_unlock_irqrestore(&zone->lru_lock, flags);
362 locked = false;
363 }
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700364 }
Mel Gormanc67fe372012-08-21 16:16:17 -0700365
Mel Gorman0bf380b2012-02-03 15:37:18 -0800366 /*
367 * migrate_pfn does not necessarily start aligned to a
368 * pageblock. Ensure that pfn_valid is called when moving
369 * into a new MAX_ORDER_NR_PAGES range in case of large
370 * memory holes within the zone
371 */
372 if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
373 if (!pfn_valid(low_pfn)) {
374 low_pfn += MAX_ORDER_NR_PAGES - 1;
375 continue;
376 }
377 }
378
Mel Gorman748446b2010-05-24 14:32:27 -0700379 if (!pfn_valid_within(low_pfn))
380 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800381 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700382
Mel Gormandc908602012-02-08 17:13:38 -0800383 /*
384 * Get the page and ensure the page is within the same zone.
385 * See the comment in isolate_freepages about overlapping
386 * nodes. It is deliberate that the new zone lock is not taken
387 * as memory compaction should not move pages between nodes.
388 */
Mel Gorman748446b2010-05-24 14:32:27 -0700389 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800390 if (page_zone(page) != zone)
391 continue;
392
393 /* Skip if free */
Mel Gorman748446b2010-05-24 14:32:27 -0700394 if (PageBuddy(page))
395 continue;
396
Mel Gorman9927af742011-01-13 15:45:59 -0800397 /*
398 * For async migration, also only scan in MOVABLE blocks. Async
399 * migration is optimistic to see if the minimum amount of work
400 * satisfies the allocation
401 */
402 pageblock_nr = low_pfn >> pageblock_order;
Linus Torvalds68e3e922012-06-03 20:05:57 -0700403 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
Michal Nazarewicz47118af2011-12-29 13:09:50 +0100404 !migrate_async_suitable(get_pageblock_migratetype(page))) {
Mel Gorman2a1402a2012-10-08 16:32:33 -0700405 goto next_pageblock;
Mel Gorman9927af742011-01-13 15:45:59 -0800406 }
407
Mel Gorman2a1402a2012-10-08 16:32:33 -0700408 /* Check may be lockless but that's ok as we recheck later */
Andrea Arcangelibc835012011-01-13 15:47:08 -0800409 if (!PageLRU(page))
410 continue;
411
412 /*
Mel Gorman2a1402a2012-10-08 16:32:33 -0700413 * PageLRU is set. lru_lock normally excludes isolation
414 * splitting and collapsing (collapsing has already happened
415 * if PageLRU is set) but the lock is not necessarily taken
416 * here and it is wasteful to take it just to check transhuge.
417 * Check TransHuge without lock and skip the whole pageblock if
418 * it's either a transhuge or hugetlbfs page, as calling
419 * compound_order() without preventing THP from splitting the
420 * page underneath us may return surprising results.
Andrea Arcangelibc835012011-01-13 15:47:08 -0800421 */
422 if (PageTransHuge(page)) {
Mel Gorman2a1402a2012-10-08 16:32:33 -0700423 if (!locked)
424 goto next_pageblock;
425 low_pfn += (1 << compound_order(page)) - 1;
426 continue;
427 }
428
429 /* Check if it is ok to still hold the lock */
430 locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
431 locked, cc);
432 if (!locked || fatal_signal_pending(current))
433 break;
434
435 /* Recheck PageLRU and PageTransHuge under lock */
436 if (!PageLRU(page))
437 continue;
438 if (PageTransHuge(page)) {
Andrea Arcangelibc835012011-01-13 15:47:08 -0800439 low_pfn += (1 << compound_order(page)) - 1;
440 continue;
441 }
442
Linus Torvalds68e3e922012-06-03 20:05:57 -0700443 if (!cc->sync)
Mel Gormanc8244932012-01-12 17:19:38 -0800444 mode |= ISOLATE_ASYNC_MIGRATE;
445
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700446 lruvec = mem_cgroup_page_lruvec(page, zone);
447
Mel Gorman748446b2010-05-24 14:32:27 -0700448 /* Try isolate the page */
Konstantin Khlebnikovf3fd4a62012-05-29 15:06:54 -0700449 if (__isolate_lru_page(page, mode) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700450 continue;
451
Andrea Arcangelibc835012011-01-13 15:47:08 -0800452 VM_BUG_ON(PageTransCompound(page));
453
Mel Gorman748446b2010-05-24 14:32:27 -0700454 /* Successfully isolated */
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700455 del_page_from_lru_list(page, lruvec, page_lru(page));
Mel Gorman748446b2010-05-24 14:32:27 -0700456 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700457 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800458 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700459
460 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800461 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
462 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700463 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800464 }
Mel Gorman2a1402a2012-10-08 16:32:33 -0700465
466 continue;
467
468next_pageblock:
469 low_pfn += pageblock_nr_pages;
470 low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
471 last_pageblock_nr = pageblock_nr;
Mel Gorman748446b2010-05-24 14:32:27 -0700472 }
473
Mel Gormanc67fe372012-08-21 16:16:17 -0700474 acct_isolated(zone, locked, cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700475
Mel Gormanc67fe372012-08-21 16:16:17 -0700476 if (locked)
477 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700478
Mel Gormanb7aba692011-01-13 15:45:54 -0800479 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
480
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100481 return low_pfn;
482}
483
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100484#endif /* CONFIG_COMPACTION || CONFIG_CMA */
485#ifdef CONFIG_COMPACTION
486
Linus Torvalds68e3e922012-06-03 20:05:57 -0700487/* Returns true if the page is within a block suitable for migration to */
488static bool suitable_migration_target(struct page *page)
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100489{
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100490
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100491 int migratetype = get_pageblock_migratetype(page);
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100492
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100493 /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
494 if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
Linus Torvalds68e3e922012-06-03 20:05:57 -0700495 return false;
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100496
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100497 /* If the page is a large free page, then allow migration */
498 if (PageBuddy(page) && page_order(page) >= pageblock_order)
Linus Torvalds68e3e922012-06-03 20:05:57 -0700499 return true;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100500
Michal Nazarewicz47118af2011-12-29 13:09:50 +0100501 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700502 if (migrate_async_suitable(migratetype))
503 return true;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100504
505 /* Otherwise skip the block */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700506 return false;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100507}
508
509/*
Mel Gormande74f1c2012-08-21 16:16:15 -0700510 * Returns the start pfn of the last page block in a zone. This is the starting
511 * point for full compaction of a zone. Compaction searches for free pages from
512 * the end of each zone, while isolate_freepages_block scans forward inside each
513 * page block.
514 */
515static unsigned long start_free_pfn(struct zone *zone)
516{
517 unsigned long free_pfn;
518 free_pfn = zone->zone_start_pfn + zone->spanned_pages;
519 free_pfn &= ~(pageblock_nr_pages-1);
520 return free_pfn;
521}
522
523/*
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100524 * Based on information in the current compact_control, find blocks
525 * suitable for isolating free pages from and then isolate them.
526 */
527static void isolate_freepages(struct zone *zone,
528 struct compact_control *cc)
529{
530 struct page *page;
531 unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
532 unsigned long flags;
533 int nr_freepages = cc->nr_freepages;
534 struct list_head *freelist = &cc->freepages;
535
536 /*
537 * Initialise the free scanner. The starting point is where we last
538 * scanned from (or the end of the zone if starting). The low point
539 * is the end of the pageblock the migration scanner is using.
540 */
541 pfn = cc->free_pfn;
542 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
543
544 /*
545 * Take care that if the migration scanner is at the end of the zone
546 * that the free scanner does not accidentally move to the next zone
547 * in the next isolation cycle.
548 */
549 high_pfn = min(low_pfn, pfn);
550
551 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
552
553 /*
554 * Isolate free pages until enough are available to migrate the
555 * pages on cc->migratepages. We stop searching if the migrate
556 * and free page scanners meet or enough free pages are isolated.
557 */
558 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
559 pfn -= pageblock_nr_pages) {
560 unsigned long isolated;
561
562 if (!pfn_valid(pfn))
563 continue;
564
565 /*
566 * Check for overlapping nodes/zones. It's possible on some
567 * configurations to have a setup like
568 * node0 node1 node0
569 * i.e. it's possible that all pages within a zones range of
570 * pages do not belong to a single zone.
571 */
572 page = pfn_to_page(pfn);
573 if (page_zone(page) != zone)
574 continue;
575
576 /* Check the block is suitable for migration */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700577 if (!suitable_migration_target(page))
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100578 continue;
Linus Torvalds68e3e922012-06-03 20:05:57 -0700579
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100580 /*
581 * Found a block suitable for isolating free pages from. Now
582 * we disabled interrupts, double check things are ok and
583 * isolate the pages. This is to minimise the time IRQs
584 * are disabled
585 */
586 isolated = 0;
Mel Gormanc67fe372012-08-21 16:16:17 -0700587
588 /*
589 * The zone lock must be held to isolate freepages. This
590 * unfortunately this is a very coarse lock and can be
591 * heavily contended if there are parallel allocations
592 * or parallel compactions. For async compaction do not
593 * spin on the lock
594 */
595 if (!compact_trylock_irqsave(&zone->lock, &flags, cc))
596 break;
Linus Torvalds68e3e922012-06-03 20:05:57 -0700597 if (suitable_migration_target(page)) {
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100598 end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn);
599 isolated = isolate_freepages_block(pfn, end_pfn,
600 freelist, false);
601 nr_freepages += isolated;
Linus Torvalds68e3e922012-06-03 20:05:57 -0700602 }
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100603 spin_unlock_irqrestore(&zone->lock, flags);
604
605 /*
606 * Record the highest PFN we isolated pages from. When next
607 * looking for free pages, the search will restart here as
608 * page migration may have returned some pages to the allocator
609 */
Rik van Riel7db88892012-07-31 16:43:12 -0700610 if (isolated) {
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100611 high_pfn = max(high_pfn, pfn);
Mel Gormande74f1c2012-08-21 16:16:15 -0700612
613 /*
614 * If the free scanner has wrapped, update
615 * compact_cached_free_pfn to point to the highest
616 * pageblock with free pages. This reduces excessive
617 * scanning of full pageblocks near the end of the
618 * zone
619 */
620 if (cc->order > 0 && cc->wrapped)
Rik van Riel7db88892012-07-31 16:43:12 -0700621 zone->compact_cached_free_pfn = high_pfn;
622 }
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100623 }
624
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100625 /* split_free_page does not map the pages */
626 map_pages(freelist);
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100627
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100628 cc->free_pfn = high_pfn;
629 cc->nr_freepages = nr_freepages;
Mel Gormande74f1c2012-08-21 16:16:15 -0700630
631 /* If compact_cached_free_pfn is reset then set it now */
632 if (cc->order > 0 && !cc->wrapped &&
633 zone->compact_cached_free_pfn == start_free_pfn(zone))
634 zone->compact_cached_free_pfn = high_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700635}
636
637/*
638 * This is a migrate-callback that "allocates" freepages by taking pages
639 * from the isolated freelists in the block we are migrating to.
640 */
641static struct page *compaction_alloc(struct page *migratepage,
642 unsigned long data,
643 int **result)
644{
645 struct compact_control *cc = (struct compact_control *)data;
646 struct page *freepage;
647
648 /* Isolate free pages if necessary */
649 if (list_empty(&cc->freepages)) {
650 isolate_freepages(cc->zone, cc);
651
652 if (list_empty(&cc->freepages))
653 return NULL;
654 }
655
656 freepage = list_entry(cc->freepages.next, struct page, lru);
657 list_del(&freepage->lru);
658 cc->nr_freepages--;
659
660 return freepage;
661}
662
663/*
664 * We cannot control nr_migratepages and nr_freepages fully when migration is
665 * running as migrate_pages() has no knowledge of compact_control. When
666 * migration is complete, we count the number of pages on the lists by hand.
667 */
668static void update_nr_listpages(struct compact_control *cc)
669{
670 int nr_migratepages = 0;
671 int nr_freepages = 0;
672 struct page *page;
673
674 list_for_each_entry(page, &cc->migratepages, lru)
675 nr_migratepages++;
676 list_for_each_entry(page, &cc->freepages, lru)
677 nr_freepages++;
678
679 cc->nr_migratepages = nr_migratepages;
680 cc->nr_freepages = nr_freepages;
681}
682
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100683/* possible outcome of isolate_migratepages */
684typedef enum {
685 ISOLATE_ABORT, /* Abort compaction now */
686 ISOLATE_NONE, /* No pages isolated, continue scanning */
687 ISOLATE_SUCCESS, /* Pages isolated, migrate */
688} isolate_migrate_t;
689
690/*
691 * Isolate all pages that can be migrated from the block pointed to by
692 * the migrate scanner within compact_control.
693 */
694static isolate_migrate_t isolate_migratepages(struct zone *zone,
695 struct compact_control *cc)
696{
697 unsigned long low_pfn, end_pfn;
698
699 /* Do not scan outside zone boundaries */
700 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
701
702 /* Only scan within a pageblock boundary */
703 end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
704
705 /* Do not cross the free scanner or scan within a memory hole */
706 if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
707 cc->migrate_pfn = end_pfn;
708 return ISOLATE_NONE;
709 }
710
711 /* Perform the isolation */
712 low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn);
Shaohua Lie64c5232012-10-08 16:32:27 -0700713 if (!low_pfn || cc->contended)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100714 return ISOLATE_ABORT;
715
716 cc->migrate_pfn = low_pfn;
717
718 return ISOLATE_SUCCESS;
719}
720
Mel Gorman748446b2010-05-24 14:32:27 -0700721static int compact_finished(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800722 struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700723{
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800724 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -0700725
Mel Gorman748446b2010-05-24 14:32:27 -0700726 if (fatal_signal_pending(current))
727 return COMPACT_PARTIAL;
728
Rik van Riel7db88892012-07-31 16:43:12 -0700729 /*
730 * A full (order == -1) compaction run starts at the beginning and
731 * end of a zone; it completes when the migrate and free scanner meet.
732 * A partial (order > 0) compaction can start with the free scanner
733 * at a random point in the zone, and may have to restart.
734 */
735 if (cc->free_pfn <= cc->migrate_pfn) {
736 if (cc->order > 0 && !cc->wrapped) {
737 /* We started partway through; restart at the end. */
738 unsigned long free_pfn = start_free_pfn(zone);
739 zone->compact_cached_free_pfn = free_pfn;
740 cc->free_pfn = free_pfn;
741 cc->wrapped = 1;
742 return COMPACT_CONTINUE;
743 }
744 return COMPACT_COMPLETE;
745 }
746
747 /* We wrapped around and ended up where we started. */
748 if (cc->wrapped && cc->free_pfn <= cc->start_free_pfn)
Mel Gorman748446b2010-05-24 14:32:27 -0700749 return COMPACT_COMPLETE;
750
Johannes Weiner82478fb2011-01-20 14:44:21 -0800751 /*
752 * order == -1 is expected when compacting via
753 * /proc/sys/vm/compact_memory
754 */
Mel Gorman56de7262010-05-24 14:32:30 -0700755 if (cc->order == -1)
756 return COMPACT_CONTINUE;
757
Michal Hocko3957c772011-06-15 15:08:25 -0700758 /* Compaction run is not finished if the watermark is not met */
759 watermark = low_wmark_pages(zone);
760 watermark += (1 << cc->order);
761
762 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
763 return COMPACT_CONTINUE;
764
Mel Gorman56de7262010-05-24 14:32:30 -0700765 /* Direct compactor: Is a suitable page free? */
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700766 if (cc->page) {
767 /* Was a suitable page captured? */
768 if (*cc->page)
Mel Gorman56de7262010-05-24 14:32:30 -0700769 return COMPACT_PARTIAL;
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700770 } else {
771 unsigned int order;
772 for (order = cc->order; order < MAX_ORDER; order++) {
773 struct free_area *area = &zone->free_area[cc->order];
774 /* Job done if page is free of the right migratetype */
775 if (!list_empty(&area->free_list[cc->migratetype]))
776 return COMPACT_PARTIAL;
Mel Gorman56de7262010-05-24 14:32:30 -0700777
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700778 /* Job done if allocation would set block type */
779 if (cc->order >= pageblock_order && area->nr_free)
780 return COMPACT_PARTIAL;
781 }
Mel Gorman56de7262010-05-24 14:32:30 -0700782 }
783
Mel Gorman748446b2010-05-24 14:32:27 -0700784 return COMPACT_CONTINUE;
785}
786
Mel Gorman3e7d3442011-01-13 15:45:56 -0800787/*
788 * compaction_suitable: Is this suitable to run compaction on this zone now?
789 * Returns
790 * COMPACT_SKIPPED - If there are too few free pages for compaction
791 * COMPACT_PARTIAL - If the allocation would succeed without compaction
792 * COMPACT_CONTINUE - If compaction should run now
793 */
794unsigned long compaction_suitable(struct zone *zone, int order)
795{
796 int fragindex;
797 unsigned long watermark;
798
799 /*
Michal Hocko3957c772011-06-15 15:08:25 -0700800 * order == -1 is expected when compacting via
801 * /proc/sys/vm/compact_memory
802 */
803 if (order == -1)
804 return COMPACT_CONTINUE;
805
806 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -0800807 * Watermarks for order-0 must be met for compaction. Note the 2UL.
808 * This is because during migration, copies of pages need to be
809 * allocated and for a short time, the footprint is higher
810 */
811 watermark = low_wmark_pages(zone) + (2UL << order);
812 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
813 return COMPACT_SKIPPED;
814
815 /*
816 * fragmentation index determines if allocation failures are due to
817 * low memory or external fragmentation
818 *
Shaohua Lia582a732011-06-15 15:08:49 -0700819 * index of -1000 implies allocations might succeed depending on
820 * watermarks
Mel Gorman3e7d3442011-01-13 15:45:56 -0800821 * index towards 0 implies failure is due to lack of memory
822 * index towards 1000 implies failure is due to fragmentation
823 *
824 * Only compact if a failure would be due to fragmentation.
825 */
826 fragindex = fragmentation_index(zone, order);
827 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
828 return COMPACT_SKIPPED;
829
Shaohua Lia582a732011-06-15 15:08:49 -0700830 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
831 0, 0))
Mel Gorman3e7d3442011-01-13 15:45:56 -0800832 return COMPACT_PARTIAL;
833
834 return COMPACT_CONTINUE;
835}
836
Mel Gorman748446b2010-05-24 14:32:27 -0700837static int compact_zone(struct zone *zone, struct compact_control *cc)
838{
839 int ret;
840
Mel Gorman3e7d3442011-01-13 15:45:56 -0800841 ret = compaction_suitable(zone, cc->order);
842 switch (ret) {
843 case COMPACT_PARTIAL:
844 case COMPACT_SKIPPED:
845 /* Compaction is likely to fail */
846 return ret;
847 case COMPACT_CONTINUE:
848 /* Fall through to compaction */
849 ;
850 }
851
Mel Gorman748446b2010-05-24 14:32:27 -0700852 /* Setup to move all movable pages to the end of the zone */
853 cc->migrate_pfn = zone->zone_start_pfn;
Rik van Riel7db88892012-07-31 16:43:12 -0700854
855 if (cc->order > 0) {
856 /* Incremental compaction. Start where the last one stopped. */
857 cc->free_pfn = zone->compact_cached_free_pfn;
858 cc->start_free_pfn = cc->free_pfn;
859 } else {
860 /* Order == -1 starts at the end of the zone. */
861 cc->free_pfn = start_free_pfn(zone);
862 }
Mel Gorman748446b2010-05-24 14:32:27 -0700863
864 migrate_prep_local();
865
866 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
867 unsigned long nr_migrate, nr_remaining;
Minchan Kim9d502c12011-03-22 16:30:39 -0700868 int err;
Mel Gorman748446b2010-05-24 14:32:27 -0700869
Mel Gormanf9e35b32011-06-15 15:08:52 -0700870 switch (isolate_migratepages(zone, cc)) {
871 case ISOLATE_ABORT:
872 ret = COMPACT_PARTIAL;
Shaohua Lie64c5232012-10-08 16:32:27 -0700873 putback_lru_pages(&cc->migratepages);
874 cc->nr_migratepages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700875 goto out;
876 case ISOLATE_NONE:
Mel Gorman748446b2010-05-24 14:32:27 -0700877 continue;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700878 case ISOLATE_SUCCESS:
879 ;
880 }
Mel Gorman748446b2010-05-24 14:32:27 -0700881
882 nr_migrate = cc->nr_migratepages;
Minchan Kim9d502c12011-03-22 16:30:39 -0700883 err = migrate_pages(&cc->migratepages, compaction_alloc,
Linus Torvalds68e3e922012-06-03 20:05:57 -0700884 (unsigned long)cc, false,
885 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
Mel Gorman748446b2010-05-24 14:32:27 -0700886 update_nr_listpages(cc);
887 nr_remaining = cc->nr_migratepages;
888
889 count_vm_event(COMPACTBLOCKS);
890 count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
891 if (nr_remaining)
892 count_vm_events(COMPACTPAGEFAILED, nr_remaining);
Mel Gormanb7aba692011-01-13 15:45:54 -0800893 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
894 nr_remaining);
Mel Gorman748446b2010-05-24 14:32:27 -0700895
896 /* Release LRU pages not migrated */
Minchan Kim9d502c12011-03-22 16:30:39 -0700897 if (err) {
Mel Gorman748446b2010-05-24 14:32:27 -0700898 putback_lru_pages(&cc->migratepages);
899 cc->nr_migratepages = 0;
David Rientjes4bf2bba2012-07-11 14:02:13 -0700900 if (err == -ENOMEM) {
901 ret = COMPACT_PARTIAL;
902 goto out;
903 }
Mel Gorman748446b2010-05-24 14:32:27 -0700904 }
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700905
906 /* Capture a page now if it is a suitable size */
907 compact_capture_page(cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700908 }
909
Mel Gormanf9e35b32011-06-15 15:08:52 -0700910out:
Mel Gorman748446b2010-05-24 14:32:27 -0700911 /* Release free pages and check accounting */
912 cc->nr_freepages -= release_freepages(&cc->freepages);
913 VM_BUG_ON(cc->nr_freepages != 0);
914
915 return ret;
916}
Mel Gorman76ab0f52010-05-24 14:32:28 -0700917
Kyungmin Parkd43a87e2011-10-31 17:09:08 -0700918static unsigned long compact_zone_order(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800919 int order, gfp_t gfp_mask,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700920 bool sync, bool *contended,
921 struct page **page)
Mel Gorman56de7262010-05-24 14:32:30 -0700922{
Shaohua Lie64c5232012-10-08 16:32:27 -0700923 unsigned long ret;
Mel Gorman56de7262010-05-24 14:32:30 -0700924 struct compact_control cc = {
925 .nr_freepages = 0,
926 .nr_migratepages = 0,
927 .order = order,
928 .migratetype = allocflags_to_migratetype(gfp_mask),
929 .zone = zone,
Linus Torvalds68e3e922012-06-03 20:05:57 -0700930 .sync = sync,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700931 .page = page,
Mel Gorman56de7262010-05-24 14:32:30 -0700932 };
933 INIT_LIST_HEAD(&cc.freepages);
934 INIT_LIST_HEAD(&cc.migratepages);
935
Shaohua Lie64c5232012-10-08 16:32:27 -0700936 ret = compact_zone(zone, &cc);
937
938 VM_BUG_ON(!list_empty(&cc.freepages));
939 VM_BUG_ON(!list_empty(&cc.migratepages));
940
941 *contended = cc.contended;
942 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -0700943}
944
Mel Gorman5e771902010-05-24 14:32:31 -0700945int sysctl_extfrag_threshold = 500;
946
Mel Gorman56de7262010-05-24 14:32:30 -0700947/**
948 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
949 * @zonelist: The zonelist used for the current allocation
950 * @order: The order of the current allocation
951 * @gfp_mask: The GFP mask of the current allocation
952 * @nodemask: The allowed nodes to allocate from
Mel Gorman77f1fe62011-01-13 15:45:57 -0800953 * @sync: Whether migration is synchronous or not
Mel Gorman661c4cb2012-10-08 16:32:31 -0700954 * @contended: Return value that is true if compaction was aborted due to lock contention
955 * @page: Optionally capture a free page of the requested order during compaction
Mel Gorman56de7262010-05-24 14:32:30 -0700956 *
957 * This is the main entry point for direct page compaction.
958 */
959unsigned long try_to_compact_pages(struct zonelist *zonelist,
Mel Gorman77f1fe62011-01-13 15:45:57 -0800960 int order, gfp_t gfp_mask, nodemask_t *nodemask,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700961 bool sync, bool *contended, struct page **page)
Mel Gorman56de7262010-05-24 14:32:30 -0700962{
963 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
964 int may_enter_fs = gfp_mask & __GFP_FS;
965 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -0700966 struct zoneref *z;
967 struct zone *zone;
968 int rc = COMPACT_SKIPPED;
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -0700969 int alloc_flags = 0;
Mel Gorman56de7262010-05-24 14:32:30 -0700970
Mel Gorman4ffb6332012-10-08 16:29:09 -0700971 /* Check if the GFP flags allow compaction */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -0800972 if (!order || !may_enter_fs || !may_perform_io)
Mel Gorman56de7262010-05-24 14:32:30 -0700973 return rc;
974
975 count_vm_event(COMPACTSTALL);
976
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -0700977#ifdef CONFIG_CMA
978 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
979 alloc_flags |= ALLOC_CMA;
980#endif
Mel Gorman56de7262010-05-24 14:32:30 -0700981 /* Compact each zone in the list */
982 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
983 nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -0700984 int status;
985
Mel Gormanc67fe372012-08-21 16:16:17 -0700986 status = compact_zone_order(zone, order, gfp_mask, sync,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700987 contended, page);
Mel Gorman56de7262010-05-24 14:32:30 -0700988 rc = max(status, rc);
989
Mel Gorman3e7d3442011-01-13 15:45:56 -0800990 /* If a normal allocation would succeed, stop compacting */
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -0700991 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
992 alloc_flags))
Mel Gorman56de7262010-05-24 14:32:30 -0700993 break;
994 }
995
996 return rc;
997}
998
999
Mel Gorman76ab0f52010-05-24 14:32:28 -07001000/* Compact all zones within a node */
Rik van Riel7be62de2012-03-21 16:33:52 -07001001static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001002{
1003 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -07001004 struct zone *zone;
1005
Mel Gorman76ab0f52010-05-24 14:32:28 -07001006 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -07001007
1008 zone = &pgdat->node_zones[zoneid];
1009 if (!populated_zone(zone))
1010 continue;
1011
Rik van Riel7be62de2012-03-21 16:33:52 -07001012 cc->nr_freepages = 0;
1013 cc->nr_migratepages = 0;
1014 cc->zone = zone;
1015 INIT_LIST_HEAD(&cc->freepages);
1016 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001017
Dan Carpenteraad6ec32012-03-21 16:33:54 -07001018 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -07001019 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001020
Rik van Rielaff62242012-03-21 16:33:52 -07001021 if (cc->order > 0) {
1022 int ok = zone_watermark_ok(zone, cc->order,
1023 low_wmark_pages(zone), 0, 0);
Minchan Kimc81758f2012-08-21 16:16:03 -07001024 if (ok && cc->order >= zone->compact_order_failed)
Rik van Rielaff62242012-03-21 16:33:52 -07001025 zone->compact_order_failed = cc->order + 1;
1026 /* Currently async compaction is never deferred. */
Linus Torvalds68e3e922012-06-03 20:05:57 -07001027 else if (!ok && cc->sync)
Rik van Rielaff62242012-03-21 16:33:52 -07001028 defer_compaction(zone, cc->order);
1029 }
1030
Rik van Riel7be62de2012-03-21 16:33:52 -07001031 VM_BUG_ON(!list_empty(&cc->freepages));
1032 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -07001033 }
1034
1035 return 0;
1036}
1037
Rik van Riel7be62de2012-03-21 16:33:52 -07001038int compact_pgdat(pg_data_t *pgdat, int order)
1039{
1040 struct compact_control cc = {
1041 .order = order,
Linus Torvalds68e3e922012-06-03 20:05:57 -07001042 .sync = false,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001043 .page = NULL,
Rik van Riel7be62de2012-03-21 16:33:52 -07001044 };
1045
1046 return __compact_pgdat(pgdat, &cc);
1047}
1048
1049static int compact_node(int nid)
1050{
Rik van Riel7be62de2012-03-21 16:33:52 -07001051 struct compact_control cc = {
1052 .order = -1,
Linus Torvalds68e3e922012-06-03 20:05:57 -07001053 .sync = true,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001054 .page = NULL,
Rik van Riel7be62de2012-03-21 16:33:52 -07001055 };
1056
Hugh Dickins8575ec22012-03-21 16:33:53 -07001057 return __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001058}
1059
Mel Gorman76ab0f52010-05-24 14:32:28 -07001060/* Compact all nodes in the system */
1061static int compact_nodes(void)
1062{
1063 int nid;
1064
Hugh Dickins8575ec22012-03-21 16:33:53 -07001065 /* Flush pending updates to the LRU lists */
1066 lru_add_drain_all();
1067
Mel Gorman76ab0f52010-05-24 14:32:28 -07001068 for_each_online_node(nid)
1069 compact_node(nid);
1070
1071 return COMPACT_COMPLETE;
1072}
1073
1074/* The written value is actually unused, all memory is compacted */
1075int sysctl_compact_memory;
1076
1077/* This is the entry point for compacting all nodes via /proc/sys/vm */
1078int sysctl_compaction_handler(struct ctl_table *table, int write,
1079 void __user *buffer, size_t *length, loff_t *ppos)
1080{
1081 if (write)
1082 return compact_nodes();
1083
1084 return 0;
1085}
Mel Gormaned4a6d72010-05-24 14:32:29 -07001086
Mel Gorman5e771902010-05-24 14:32:31 -07001087int sysctl_extfrag_handler(struct ctl_table *table, int write,
1088 void __user *buffer, size_t *length, loff_t *ppos)
1089{
1090 proc_dointvec_minmax(table, write, buffer, length, ppos);
1091
1092 return 0;
1093}
1094
Mel Gormaned4a6d72010-05-24 14:32:29 -07001095#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Kay Sievers10fbcf42011-12-21 14:48:43 -08001096ssize_t sysfs_compact_node(struct device *dev,
1097 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -07001098 const char *buf, size_t count)
1099{
Hugh Dickins8575ec22012-03-21 16:33:53 -07001100 int nid = dev->id;
1101
1102 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1103 /* Flush pending updates to the LRU lists */
1104 lru_add_drain_all();
1105
1106 compact_node(nid);
1107 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07001108
1109 return count;
1110}
Kay Sievers10fbcf42011-12-21 14:48:43 -08001111static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001112
1113int compaction_register_node(struct node *node)
1114{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001115 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001116}
1117
1118void compaction_unregister_node(struct node *node)
1119{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001120 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001121}
1122#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001123
1124#endif /* CONFIG_COMPACTION */