blob: 0649cc1b3479daf56bc1a3b995b7c5c5ef59dd9d [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
Michal Nazarewicz85aa1252012-01-30 13:24:03 +010053/*
Mel Gormanc67fe372012-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) {
Shaohua Lie64c5232012-10-08 16:32:27 -070073 cc->contended = true;
Mel Gormanc67fe372012-08-21 16:16:17 -070074 return false;
75 }
76
77 cond_resched();
78 if (fatal_signal_pending(current))
79 return false;
80 }
81
82 if (!locked)
83 spin_lock_irqsave(lock, *flags);
84 return true;
85}
86
87static inline bool compact_trylock_irqsave(spinlock_t *lock,
88 unsigned long *flags, struct compact_control *cc)
89{
90 return compact_checklock_irqsave(lock, flags, false, cc);
91}
92
Mel Gorman1fb3f8c2012-10-08 16:29:12 -070093static void compact_capture_page(struct compact_control *cc)
94{
95 unsigned long flags;
96 int mtype, mtype_low, mtype_high;
97
98 if (!cc->page || *cc->page)
99 return;
100
101 /*
102 * For MIGRATE_MOVABLE allocations we capture a suitable page ASAP
103 * regardless of the migratetype of the freelist is is captured from.
104 * This is fine because the order for a high-order MIGRATE_MOVABLE
105 * allocation is typically at least a pageblock size and overall
106 * fragmentation is not impaired. Other allocation types must
107 * capture pages from their own migratelist because otherwise they
108 * could pollute other pageblocks like MIGRATE_MOVABLE with
109 * difficult to move pages and making fragmentation worse overall.
110 */
111 if (cc->migratetype == MIGRATE_MOVABLE) {
112 mtype_low = 0;
113 mtype_high = MIGRATE_PCPTYPES;
114 } else {
115 mtype_low = cc->migratetype;
116 mtype_high = cc->migratetype + 1;
117 }
118
119 /* Speculatively examine the free lists without zone lock */
120 for (mtype = mtype_low; mtype < mtype_high; mtype++) {
121 int order;
122 for (order = cc->order; order < MAX_ORDER; order++) {
123 struct page *page;
124 struct free_area *area;
125 area = &(cc->zone->free_area[order]);
126 if (list_empty(&area->free_list[mtype]))
127 continue;
128
129 /* Take the lock and attempt capture of the page */
130 if (!compact_trylock_irqsave(&cc->zone->lock, &flags, cc))
131 return;
132 if (!list_empty(&area->free_list[mtype])) {
133 page = list_entry(area->free_list[mtype].next,
134 struct page, lru);
135 if (capture_free_page(page, cc->order, mtype)) {
136 spin_unlock_irqrestore(&cc->zone->lock,
137 flags);
138 *cc->page = page;
139 return;
140 }
141 }
142 spin_unlock_irqrestore(&cc->zone->lock, flags);
143 }
144 }
145}
146
Mel Gormanc67fe372012-08-21 16:16:17 -0700147/*
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100148 * Isolate free pages onto a private freelist. Caller must hold zone->lock.
149 * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
150 * pages inside of the pageblock (even though it may still end up isolating
151 * some pages).
152 */
153static unsigned long isolate_freepages_block(unsigned long blockpfn,
154 unsigned long end_pfn,
155 struct list_head *freelist,
156 bool strict)
Mel Gorman748446b2010-05-24 14:32:27 -0700157{
Mel Gormanb7aba692011-01-13 15:45:54 -0800158 int nr_scanned = 0, total_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700159 struct page *cursor;
160
Mel Gorman748446b2010-05-24 14:32:27 -0700161 cursor = pfn_to_page(blockpfn);
162
163 /* Isolate free pages. This assumes the block is valid */
164 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
165 int isolated, i;
166 struct page *page = cursor;
167
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100168 if (!pfn_valid_within(blockpfn)) {
169 if (strict)
170 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700171 continue;
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100172 }
Mel Gormanb7aba692011-01-13 15:45:54 -0800173 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700174
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100175 if (!PageBuddy(page)) {
176 if (strict)
177 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700178 continue;
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100179 }
Mel Gorman748446b2010-05-24 14:32:27 -0700180
181 /* Found a free page, break it into order-0 pages */
182 isolated = split_free_page(page);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100183 if (!isolated && strict)
184 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700185 total_isolated += isolated;
186 for (i = 0; i < isolated; i++) {
187 list_add(&page->lru, freelist);
188 page++;
189 }
190
191 /* If a page was split, advance to the end of it */
192 if (isolated) {
193 blockpfn += isolated - 1;
194 cursor += isolated - 1;
195 }
196 }
197
Mel Gormanb7aba692011-01-13 15:45:54 -0800198 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
Mel Gorman748446b2010-05-24 14:32:27 -0700199 return total_isolated;
200}
201
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100202/**
203 * isolate_freepages_range() - isolate free pages.
204 * @start_pfn: The first PFN to start isolating.
205 * @end_pfn: The one-past-last PFN.
206 *
207 * Non-free pages, invalid PFNs, or zone boundaries within the
208 * [start_pfn, end_pfn) range are considered errors, cause function to
209 * undo its actions and return zero.
210 *
211 * Otherwise, function returns one-past-the-last PFN of isolated page
212 * (which may be greater then end_pfn if end fell in a middle of
213 * a free page).
214 */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100215unsigned long
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100216isolate_freepages_range(unsigned long start_pfn, unsigned long end_pfn)
217{
218 unsigned long isolated, pfn, block_end_pfn, flags;
219 struct zone *zone = NULL;
220 LIST_HEAD(freelist);
221
222 if (pfn_valid(start_pfn))
223 zone = page_zone(pfn_to_page(start_pfn));
224
225 for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
226 if (!pfn_valid(pfn) || zone != page_zone(pfn_to_page(pfn)))
227 break;
228
229 /*
230 * On subsequent iterations ALIGN() is actually not needed,
231 * but we keep it that we not to complicate the code.
232 */
233 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
234 block_end_pfn = min(block_end_pfn, end_pfn);
235
236 spin_lock_irqsave(&zone->lock, flags);
237 isolated = isolate_freepages_block(pfn, block_end_pfn,
238 &freelist, true);
239 spin_unlock_irqrestore(&zone->lock, flags);
240
241 /*
242 * In strict mode, isolate_freepages_block() returns 0 if
243 * there are any holes in the block (ie. invalid PFNs or
244 * non-free pages).
245 */
246 if (!isolated)
247 break;
248
249 /*
250 * If we managed to isolate pages, it is always (1 << n) *
251 * pageblock_nr_pages for some non-negative n. (Max order
252 * page may span two pageblocks).
253 */
254 }
255
256 /* split_free_page does not map the pages */
257 map_pages(&freelist);
258
259 if (pfn < end_pfn) {
260 /* Loop terminated early, cleanup. */
261 release_freepages(&freelist);
262 return 0;
263 }
264
265 /* We don't use freelists for anything. */
266 return pfn;
267}
268
Mel Gorman748446b2010-05-24 14:32:27 -0700269/* Update the number of anon and file isolated pages in the zone */
Mel Gormanc67fe372012-08-21 16:16:17 -0700270static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700271{
272 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700273 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700274
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700275 list_for_each_entry(page, &cc->migratepages, lru)
276 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700277
Mel Gormanc67fe372012-08-21 16:16:17 -0700278 /* If locked we can use the interrupt unsafe versions */
279 if (locked) {
280 __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
281 __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
282 } else {
283 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
284 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
285 }
Mel Gorman748446b2010-05-24 14:32:27 -0700286}
287
288/* Similar to reclaim, but different enough that they don't share logic */
289static bool too_many_isolated(struct zone *zone)
290{
Minchan Kimbc693042010-09-09 16:38:00 -0700291 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700292
293 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
294 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700295 active = zone_page_state(zone, NR_ACTIVE_FILE) +
296 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700297 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
298 zone_page_state(zone, NR_ISOLATED_ANON);
299
Minchan Kimbc693042010-09-09 16:38:00 -0700300 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700301}
302
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100303/**
304 * isolate_migratepages_range() - isolate all migrate-able pages in range.
305 * @zone: Zone pages are in.
306 * @cc: Compaction control structure.
307 * @low_pfn: The first PFN of the range.
308 * @end_pfn: The one-past-the-last PFN of the range.
309 *
310 * Isolate all pages that can be migrated from the range specified by
311 * [low_pfn, end_pfn). Returns zero if there is a fatal signal
312 * pending), otherwise PFN of the first page that was not scanned
313 * (which may be both less, equal to or more then end_pfn).
314 *
315 * Assumes that cc->migratepages is empty and cc->nr_migratepages is
316 * zero.
317 *
318 * Apart from cc->migratepages and cc->nr_migratetypes this function
319 * does not modify any cc's fields, in particular it does not modify
320 * (or read for that matter) cc->migrate_pfn.
Mel Gorman748446b2010-05-24 14:32:27 -0700321 */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100322unsigned long
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100323isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
324 unsigned long low_pfn, unsigned long end_pfn)
Mel Gorman748446b2010-05-24 14:32:27 -0700325{
Mel Gorman9927af742011-01-13 15:45:59 -0800326 unsigned long last_pageblock_nr = 0, pageblock_nr;
Mel Gormanb7aba692011-01-13 15:45:54 -0800327 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700328 struct list_head *migratelist = &cc->migratepages;
Konstantin Khlebnikovf3fd4a62012-05-29 15:06:54 -0700329 isolate_mode_t mode = 0;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700330 struct lruvec *lruvec;
Mel Gormanc67fe372012-08-21 16:16:17 -0700331 unsigned long flags;
332 bool locked;
Mel Gorman748446b2010-05-24 14:32:27 -0700333
Mel Gorman748446b2010-05-24 14:32:27 -0700334 /*
335 * Ensure that there are not too many pages isolated from the LRU
336 * list by either parallel reclaimers or compaction. If there are,
337 * delay for some time until fewer pages are isolated
338 */
339 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700340 /* async migration should just abort */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700341 if (!cc->sync)
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100342 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700343
Mel Gorman748446b2010-05-24 14:32:27 -0700344 congestion_wait(BLK_RW_ASYNC, HZ/10);
345
346 if (fatal_signal_pending(current))
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100347 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700348 }
349
350 /* Time to isolate some pages for migration */
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700351 cond_resched();
Mel Gormanc67fe372012-08-21 16:16:17 -0700352 spin_lock_irqsave(&zone->lru_lock, flags);
353 locked = true;
Mel Gorman748446b2010-05-24 14:32:27 -0700354 for (; low_pfn < end_pfn; low_pfn++) {
355 struct page *page;
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700356
357 /* give a chance to irqs before checking need_resched() */
358 if (!((low_pfn+1) % SWAP_CLUSTER_MAX)) {
Mel Gormanc67fe372012-08-21 16:16:17 -0700359 spin_unlock_irqrestore(&zone->lru_lock, flags);
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700360 locked = false;
361 }
Mel Gormanc67fe372012-08-21 16:16:17 -0700362
363 /* Check if it is ok to still hold the lock */
364 locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
365 locked, cc);
366 if (!locked)
367 break;
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700368
Mel Gorman0bf380b2012-02-03 15:37:18 -0800369 /*
370 * migrate_pfn does not necessarily start aligned to a
371 * pageblock. Ensure that pfn_valid is called when moving
372 * into a new MAX_ORDER_NR_PAGES range in case of large
373 * memory holes within the zone
374 */
375 if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
376 if (!pfn_valid(low_pfn)) {
377 low_pfn += MAX_ORDER_NR_PAGES - 1;
378 continue;
379 }
380 }
381
Mel Gorman748446b2010-05-24 14:32:27 -0700382 if (!pfn_valid_within(low_pfn))
383 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800384 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700385
Mel Gormandc908602012-02-08 17:13:38 -0800386 /*
387 * Get the page and ensure the page is within the same zone.
388 * See the comment in isolate_freepages about overlapping
389 * nodes. It is deliberate that the new zone lock is not taken
390 * as memory compaction should not move pages between nodes.
391 */
Mel Gorman748446b2010-05-24 14:32:27 -0700392 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800393 if (page_zone(page) != zone)
394 continue;
395
396 /* Skip if free */
Mel Gorman748446b2010-05-24 14:32:27 -0700397 if (PageBuddy(page))
398 continue;
399
Mel Gorman9927af742011-01-13 15:45:59 -0800400 /*
401 * For async migration, also only scan in MOVABLE blocks. Async
402 * migration is optimistic to see if the minimum amount of work
403 * satisfies the allocation
404 */
405 pageblock_nr = low_pfn >> pageblock_order;
Linus Torvalds68e3e922012-06-03 20:05:57 -0700406 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
Michal Nazarewicz47118af2011-12-29 13:09:50 +0100407 !migrate_async_suitable(get_pageblock_migratetype(page))) {
Mel Gorman9927af742011-01-13 15:45:59 -0800408 low_pfn += pageblock_nr_pages;
409 low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
410 last_pageblock_nr = pageblock_nr;
411 continue;
412 }
413
Andrea Arcangelibc835012011-01-13 15:47:08 -0800414 if (!PageLRU(page))
415 continue;
416
417 /*
418 * PageLRU is set, and lru_lock excludes isolation,
419 * splitting and collapsing (collapsing has already
420 * happened if PageLRU is set).
421 */
422 if (PageTransHuge(page)) {
423 low_pfn += (1 << compound_order(page)) - 1;
424 continue;
425 }
426
Linus Torvalds68e3e922012-06-03 20:05:57 -0700427 if (!cc->sync)
Mel Gormanc8244932012-01-12 17:19:38 -0800428 mode |= ISOLATE_ASYNC_MIGRATE;
429
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700430 lruvec = mem_cgroup_page_lruvec(page, zone);
431
Mel Gorman748446b2010-05-24 14:32:27 -0700432 /* Try isolate the page */
Konstantin Khlebnikovf3fd4a62012-05-29 15:06:54 -0700433 if (__isolate_lru_page(page, mode) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700434 continue;
435
Andrea Arcangelibc835012011-01-13 15:47:08 -0800436 VM_BUG_ON(PageTransCompound(page));
437
Mel Gorman748446b2010-05-24 14:32:27 -0700438 /* Successfully isolated */
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700439 del_page_from_lru_list(page, lruvec, page_lru(page));
Mel Gorman748446b2010-05-24 14:32:27 -0700440 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700441 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800442 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700443
444 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800445 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
446 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700447 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800448 }
Mel Gorman748446b2010-05-24 14:32:27 -0700449 }
450
Mel Gormanc67fe372012-08-21 16:16:17 -0700451 acct_isolated(zone, locked, cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700452
Mel Gormanc67fe372012-08-21 16:16:17 -0700453 if (locked)
454 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700455
Mel Gormanb7aba692011-01-13 15:45:54 -0800456 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
457
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100458 return low_pfn;
459}
460
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100461#endif /* CONFIG_COMPACTION || CONFIG_CMA */
462#ifdef CONFIG_COMPACTION
463
Linus Torvalds68e3e922012-06-03 20:05:57 -0700464/* Returns true if the page is within a block suitable for migration to */
465static bool suitable_migration_target(struct page *page)
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100466{
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100467
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100468 int migratetype = get_pageblock_migratetype(page);
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100469
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100470 /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
471 if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
Linus Torvalds68e3e922012-06-03 20:05:57 -0700472 return false;
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100473
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100474 /* If the page is a large free page, then allow migration */
475 if (PageBuddy(page) && page_order(page) >= pageblock_order)
Linus Torvalds68e3e922012-06-03 20:05:57 -0700476 return true;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100477
Michal Nazarewicz47118af2011-12-29 13:09:50 +0100478 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700479 if (migrate_async_suitable(migratetype))
480 return true;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100481
482 /* Otherwise skip the block */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700483 return false;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100484}
485
486/*
Mel Gormande74f1c2012-08-21 16:16:15 -0700487 * Returns the start pfn of the last page block in a zone. This is the starting
488 * point for full compaction of a zone. Compaction searches for free pages from
489 * the end of each zone, while isolate_freepages_block scans forward inside each
490 * page block.
491 */
492static unsigned long start_free_pfn(struct zone *zone)
493{
494 unsigned long free_pfn;
495 free_pfn = zone->zone_start_pfn + zone->spanned_pages;
496 free_pfn &= ~(pageblock_nr_pages-1);
497 return free_pfn;
498}
499
500/*
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100501 * Based on information in the current compact_control, find blocks
502 * suitable for isolating free pages from and then isolate them.
503 */
504static void isolate_freepages(struct zone *zone,
505 struct compact_control *cc)
506{
507 struct page *page;
508 unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
509 unsigned long flags;
510 int nr_freepages = cc->nr_freepages;
511 struct list_head *freelist = &cc->freepages;
512
513 /*
514 * Initialise the free scanner. The starting point is where we last
515 * scanned from (or the end of the zone if starting). The low point
516 * is the end of the pageblock the migration scanner is using.
517 */
518 pfn = cc->free_pfn;
519 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
520
521 /*
522 * Take care that if the migration scanner is at the end of the zone
523 * that the free scanner does not accidentally move to the next zone
524 * in the next isolation cycle.
525 */
526 high_pfn = min(low_pfn, pfn);
527
528 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
529
530 /*
531 * Isolate free pages until enough are available to migrate the
532 * pages on cc->migratepages. We stop searching if the migrate
533 * and free page scanners meet or enough free pages are isolated.
534 */
535 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
536 pfn -= pageblock_nr_pages) {
537 unsigned long isolated;
538
539 if (!pfn_valid(pfn))
540 continue;
541
542 /*
543 * Check for overlapping nodes/zones. It's possible on some
544 * configurations to have a setup like
545 * node0 node1 node0
546 * i.e. it's possible that all pages within a zones range of
547 * pages do not belong to a single zone.
548 */
549 page = pfn_to_page(pfn);
550 if (page_zone(page) != zone)
551 continue;
552
553 /* Check the block is suitable for migration */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700554 if (!suitable_migration_target(page))
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100555 continue;
Linus Torvalds68e3e922012-06-03 20:05:57 -0700556
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100557 /*
558 * Found a block suitable for isolating free pages from. Now
559 * we disabled interrupts, double check things are ok and
560 * isolate the pages. This is to minimise the time IRQs
561 * are disabled
562 */
563 isolated = 0;
Mel Gormanc67fe372012-08-21 16:16:17 -0700564
565 /*
566 * The zone lock must be held to isolate freepages. This
567 * unfortunately this is a very coarse lock and can be
568 * heavily contended if there are parallel allocations
569 * or parallel compactions. For async compaction do not
570 * spin on the lock
571 */
572 if (!compact_trylock_irqsave(&zone->lock, &flags, cc))
573 break;
Linus Torvalds68e3e922012-06-03 20:05:57 -0700574 if (suitable_migration_target(page)) {
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100575 end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn);
576 isolated = isolate_freepages_block(pfn, end_pfn,
577 freelist, false);
578 nr_freepages += isolated;
Linus Torvalds68e3e922012-06-03 20:05:57 -0700579 }
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100580 spin_unlock_irqrestore(&zone->lock, flags);
581
582 /*
583 * Record the highest PFN we isolated pages from. When next
584 * looking for free pages, the search will restart here as
585 * page migration may have returned some pages to the allocator
586 */
Rik van Riel7db88892012-07-31 16:43:12 -0700587 if (isolated) {
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100588 high_pfn = max(high_pfn, pfn);
Mel Gormande74f1c2012-08-21 16:16:15 -0700589
590 /*
591 * If the free scanner has wrapped, update
592 * compact_cached_free_pfn to point to the highest
593 * pageblock with free pages. This reduces excessive
594 * scanning of full pageblocks near the end of the
595 * zone
596 */
597 if (cc->order > 0 && cc->wrapped)
Rik van Riel7db88892012-07-31 16:43:12 -0700598 zone->compact_cached_free_pfn = high_pfn;
599 }
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100600 }
601
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100602 /* split_free_page does not map the pages */
603 map_pages(freelist);
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100604
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100605 cc->free_pfn = high_pfn;
606 cc->nr_freepages = nr_freepages;
Mel Gormande74f1c2012-08-21 16:16:15 -0700607
608 /* If compact_cached_free_pfn is reset then set it now */
609 if (cc->order > 0 && !cc->wrapped &&
610 zone->compact_cached_free_pfn == start_free_pfn(zone))
611 zone->compact_cached_free_pfn = high_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700612}
613
614/*
615 * This is a migrate-callback that "allocates" freepages by taking pages
616 * from the isolated freelists in the block we are migrating to.
617 */
618static struct page *compaction_alloc(struct page *migratepage,
619 unsigned long data,
620 int **result)
621{
622 struct compact_control *cc = (struct compact_control *)data;
623 struct page *freepage;
624
625 /* Isolate free pages if necessary */
626 if (list_empty(&cc->freepages)) {
627 isolate_freepages(cc->zone, cc);
628
629 if (list_empty(&cc->freepages))
630 return NULL;
631 }
632
633 freepage = list_entry(cc->freepages.next, struct page, lru);
634 list_del(&freepage->lru);
635 cc->nr_freepages--;
636
637 return freepage;
638}
639
640/*
641 * We cannot control nr_migratepages and nr_freepages fully when migration is
642 * running as migrate_pages() has no knowledge of compact_control. When
643 * migration is complete, we count the number of pages on the lists by hand.
644 */
645static void update_nr_listpages(struct compact_control *cc)
646{
647 int nr_migratepages = 0;
648 int nr_freepages = 0;
649 struct page *page;
650
651 list_for_each_entry(page, &cc->migratepages, lru)
652 nr_migratepages++;
653 list_for_each_entry(page, &cc->freepages, lru)
654 nr_freepages++;
655
656 cc->nr_migratepages = nr_migratepages;
657 cc->nr_freepages = nr_freepages;
658}
659
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100660/* possible outcome of isolate_migratepages */
661typedef enum {
662 ISOLATE_ABORT, /* Abort compaction now */
663 ISOLATE_NONE, /* No pages isolated, continue scanning */
664 ISOLATE_SUCCESS, /* Pages isolated, migrate */
665} isolate_migrate_t;
666
667/*
668 * Isolate all pages that can be migrated from the block pointed to by
669 * the migrate scanner within compact_control.
670 */
671static isolate_migrate_t isolate_migratepages(struct zone *zone,
672 struct compact_control *cc)
673{
674 unsigned long low_pfn, end_pfn;
675
676 /* Do not scan outside zone boundaries */
677 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
678
679 /* Only scan within a pageblock boundary */
680 end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
681
682 /* Do not cross the free scanner or scan within a memory hole */
683 if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
684 cc->migrate_pfn = end_pfn;
685 return ISOLATE_NONE;
686 }
687
688 /* Perform the isolation */
689 low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn);
Shaohua Lie64c5232012-10-08 16:32:27 -0700690 if (!low_pfn || cc->contended)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100691 return ISOLATE_ABORT;
692
693 cc->migrate_pfn = low_pfn;
694
695 return ISOLATE_SUCCESS;
696}
697
Mel Gorman748446b2010-05-24 14:32:27 -0700698static int compact_finished(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800699 struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700700{
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800701 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -0700702
Mel Gorman748446b2010-05-24 14:32:27 -0700703 if (fatal_signal_pending(current))
704 return COMPACT_PARTIAL;
705
Rik van Riel7db88892012-07-31 16:43:12 -0700706 /*
707 * A full (order == -1) compaction run starts at the beginning and
708 * end of a zone; it completes when the migrate and free scanner meet.
709 * A partial (order > 0) compaction can start with the free scanner
710 * at a random point in the zone, and may have to restart.
711 */
712 if (cc->free_pfn <= cc->migrate_pfn) {
713 if (cc->order > 0 && !cc->wrapped) {
714 /* We started partway through; restart at the end. */
715 unsigned long free_pfn = start_free_pfn(zone);
716 zone->compact_cached_free_pfn = free_pfn;
717 cc->free_pfn = free_pfn;
718 cc->wrapped = 1;
719 return COMPACT_CONTINUE;
720 }
721 return COMPACT_COMPLETE;
722 }
723
724 /* We wrapped around and ended up where we started. */
725 if (cc->wrapped && cc->free_pfn <= cc->start_free_pfn)
Mel Gorman748446b2010-05-24 14:32:27 -0700726 return COMPACT_COMPLETE;
727
Johannes Weiner82478fb2011-01-20 14:44:21 -0800728 /*
729 * order == -1 is expected when compacting via
730 * /proc/sys/vm/compact_memory
731 */
Mel Gorman56de7262010-05-24 14:32:30 -0700732 if (cc->order == -1)
733 return COMPACT_CONTINUE;
734
Michal Hocko3957c772011-06-15 15:08:25 -0700735 /* Compaction run is not finished if the watermark is not met */
736 watermark = low_wmark_pages(zone);
737 watermark += (1 << cc->order);
738
739 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
740 return COMPACT_CONTINUE;
741
Mel Gorman56de7262010-05-24 14:32:30 -0700742 /* Direct compactor: Is a suitable page free? */
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700743 if (cc->page) {
744 /* Was a suitable page captured? */
745 if (*cc->page)
Mel Gorman56de7262010-05-24 14:32:30 -0700746 return COMPACT_PARTIAL;
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700747 } else {
748 unsigned int order;
749 for (order = cc->order; order < MAX_ORDER; order++) {
750 struct free_area *area = &zone->free_area[cc->order];
751 /* Job done if page is free of the right migratetype */
752 if (!list_empty(&area->free_list[cc->migratetype]))
753 return COMPACT_PARTIAL;
Mel Gorman56de7262010-05-24 14:32:30 -0700754
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700755 /* Job done if allocation would set block type */
756 if (cc->order >= pageblock_order && area->nr_free)
757 return COMPACT_PARTIAL;
758 }
Mel Gorman56de7262010-05-24 14:32:30 -0700759 }
760
Mel Gorman748446b2010-05-24 14:32:27 -0700761 return COMPACT_CONTINUE;
762}
763
Mel Gorman3e7d3442011-01-13 15:45:56 -0800764/*
765 * compaction_suitable: Is this suitable to run compaction on this zone now?
766 * Returns
767 * COMPACT_SKIPPED - If there are too few free pages for compaction
768 * COMPACT_PARTIAL - If the allocation would succeed without compaction
769 * COMPACT_CONTINUE - If compaction should run now
770 */
771unsigned long compaction_suitable(struct zone *zone, int order)
772{
773 int fragindex;
774 unsigned long watermark;
775
776 /*
Michal Hocko3957c772011-06-15 15:08:25 -0700777 * order == -1 is expected when compacting via
778 * /proc/sys/vm/compact_memory
779 */
780 if (order == -1)
781 return COMPACT_CONTINUE;
782
783 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -0800784 * Watermarks for order-0 must be met for compaction. Note the 2UL.
785 * This is because during migration, copies of pages need to be
786 * allocated and for a short time, the footprint is higher
787 */
788 watermark = low_wmark_pages(zone) + (2UL << order);
789 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
790 return COMPACT_SKIPPED;
791
792 /*
793 * fragmentation index determines if allocation failures are due to
794 * low memory or external fragmentation
795 *
Shaohua Lia582a732011-06-15 15:08:49 -0700796 * index of -1000 implies allocations might succeed depending on
797 * watermarks
Mel Gorman3e7d3442011-01-13 15:45:56 -0800798 * index towards 0 implies failure is due to lack of memory
799 * index towards 1000 implies failure is due to fragmentation
800 *
801 * Only compact if a failure would be due to fragmentation.
802 */
803 fragindex = fragmentation_index(zone, order);
804 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
805 return COMPACT_SKIPPED;
806
Shaohua Lia582a732011-06-15 15:08:49 -0700807 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
808 0, 0))
Mel Gorman3e7d3442011-01-13 15:45:56 -0800809 return COMPACT_PARTIAL;
810
811 return COMPACT_CONTINUE;
812}
813
Mel Gorman748446b2010-05-24 14:32:27 -0700814static int compact_zone(struct zone *zone, struct compact_control *cc)
815{
816 int ret;
817
Mel Gorman3e7d3442011-01-13 15:45:56 -0800818 ret = compaction_suitable(zone, cc->order);
819 switch (ret) {
820 case COMPACT_PARTIAL:
821 case COMPACT_SKIPPED:
822 /* Compaction is likely to fail */
823 return ret;
824 case COMPACT_CONTINUE:
825 /* Fall through to compaction */
826 ;
827 }
828
Mel Gorman748446b2010-05-24 14:32:27 -0700829 /* Setup to move all movable pages to the end of the zone */
830 cc->migrate_pfn = zone->zone_start_pfn;
Rik van Riel7db88892012-07-31 16:43:12 -0700831
832 if (cc->order > 0) {
833 /* Incremental compaction. Start where the last one stopped. */
834 cc->free_pfn = zone->compact_cached_free_pfn;
835 cc->start_free_pfn = cc->free_pfn;
836 } else {
837 /* Order == -1 starts at the end of the zone. */
838 cc->free_pfn = start_free_pfn(zone);
839 }
Mel Gorman748446b2010-05-24 14:32:27 -0700840
841 migrate_prep_local();
842
843 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
844 unsigned long nr_migrate, nr_remaining;
Minchan Kim9d502c12011-03-22 16:30:39 -0700845 int err;
Mel Gorman748446b2010-05-24 14:32:27 -0700846
Mel Gormanf9e35b32011-06-15 15:08:52 -0700847 switch (isolate_migratepages(zone, cc)) {
848 case ISOLATE_ABORT:
849 ret = COMPACT_PARTIAL;
Shaohua Lie64c5232012-10-08 16:32:27 -0700850 putback_lru_pages(&cc->migratepages);
851 cc->nr_migratepages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700852 goto out;
853 case ISOLATE_NONE:
Mel Gorman748446b2010-05-24 14:32:27 -0700854 continue;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700855 case ISOLATE_SUCCESS:
856 ;
857 }
Mel Gorman748446b2010-05-24 14:32:27 -0700858
859 nr_migrate = cc->nr_migratepages;
Minchan Kim9d502c12011-03-22 16:30:39 -0700860 err = migrate_pages(&cc->migratepages, compaction_alloc,
Linus Torvalds68e3e922012-06-03 20:05:57 -0700861 (unsigned long)cc, false,
862 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
Mel Gorman748446b2010-05-24 14:32:27 -0700863 update_nr_listpages(cc);
864 nr_remaining = cc->nr_migratepages;
865
866 count_vm_event(COMPACTBLOCKS);
867 count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
868 if (nr_remaining)
869 count_vm_events(COMPACTPAGEFAILED, nr_remaining);
Mel Gormanb7aba692011-01-13 15:45:54 -0800870 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
871 nr_remaining);
Mel Gorman748446b2010-05-24 14:32:27 -0700872
873 /* Release LRU pages not migrated */
Minchan Kim9d502c12011-03-22 16:30:39 -0700874 if (err) {
Mel Gorman748446b2010-05-24 14:32:27 -0700875 putback_lru_pages(&cc->migratepages);
876 cc->nr_migratepages = 0;
David Rientjes4bf2bba2012-07-11 14:02:13 -0700877 if (err == -ENOMEM) {
878 ret = COMPACT_PARTIAL;
879 goto out;
880 }
Mel Gorman748446b2010-05-24 14:32:27 -0700881 }
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700882
883 /* Capture a page now if it is a suitable size */
884 compact_capture_page(cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700885 }
886
Mel Gormanf9e35b32011-06-15 15:08:52 -0700887out:
Mel Gorman748446b2010-05-24 14:32:27 -0700888 /* Release free pages and check accounting */
889 cc->nr_freepages -= release_freepages(&cc->freepages);
890 VM_BUG_ON(cc->nr_freepages != 0);
891
892 return ret;
893}
Mel Gorman76ab0f52010-05-24 14:32:28 -0700894
Kyungmin Parkd43a87e2011-10-31 17:09:08 -0700895static unsigned long compact_zone_order(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800896 int order, gfp_t gfp_mask,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700897 bool sync, bool *contended,
898 struct page **page)
Mel Gorman56de7262010-05-24 14:32:30 -0700899{
Shaohua Lie64c5232012-10-08 16:32:27 -0700900 unsigned long ret;
Mel Gorman56de7262010-05-24 14:32:30 -0700901 struct compact_control cc = {
902 .nr_freepages = 0,
903 .nr_migratepages = 0,
904 .order = order,
905 .migratetype = allocflags_to_migratetype(gfp_mask),
906 .zone = zone,
Linus Torvalds68e3e922012-06-03 20:05:57 -0700907 .sync = sync,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700908 .page = page,
Mel Gorman56de7262010-05-24 14:32:30 -0700909 };
910 INIT_LIST_HEAD(&cc.freepages);
911 INIT_LIST_HEAD(&cc.migratepages);
912
Shaohua Lie64c5232012-10-08 16:32:27 -0700913 ret = compact_zone(zone, &cc);
914
915 VM_BUG_ON(!list_empty(&cc.freepages));
916 VM_BUG_ON(!list_empty(&cc.migratepages));
917
918 *contended = cc.contended;
919 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -0700920}
921
Mel Gorman5e771902010-05-24 14:32:31 -0700922int sysctl_extfrag_threshold = 500;
923
Mel Gorman56de7262010-05-24 14:32:30 -0700924/**
925 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
926 * @zonelist: The zonelist used for the current allocation
927 * @order: The order of the current allocation
928 * @gfp_mask: The GFP mask of the current allocation
929 * @nodemask: The allowed nodes to allocate from
Mel Gorman77f1fe62011-01-13 15:45:57 -0800930 * @sync: Whether migration is synchronous or not
Mel Gorman56de7262010-05-24 14:32:30 -0700931 *
932 * This is the main entry point for direct page compaction.
933 */
934unsigned long try_to_compact_pages(struct zonelist *zonelist,
Mel Gorman77f1fe62011-01-13 15:45:57 -0800935 int order, gfp_t gfp_mask, nodemask_t *nodemask,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700936 bool sync, bool *contended, struct page **page)
Mel Gorman56de7262010-05-24 14:32:30 -0700937{
938 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
939 int may_enter_fs = gfp_mask & __GFP_FS;
940 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -0700941 struct zoneref *z;
942 struct zone *zone;
943 int rc = COMPACT_SKIPPED;
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -0700944 int alloc_flags = 0;
Mel Gorman56de7262010-05-24 14:32:30 -0700945
Mel Gorman4ffb6332012-10-08 16:29:09 -0700946 /* Check if the GFP flags allow compaction */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -0800947 if (!order || !may_enter_fs || !may_perform_io)
Mel Gorman56de7262010-05-24 14:32:30 -0700948 return rc;
949
950 count_vm_event(COMPACTSTALL);
951
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -0700952#ifdef CONFIG_CMA
953 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
954 alloc_flags |= ALLOC_CMA;
955#endif
Mel Gorman56de7262010-05-24 14:32:30 -0700956 /* Compact each zone in the list */
957 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
958 nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -0700959 int status;
960
Mel Gormanc67fe372012-08-21 16:16:17 -0700961 status = compact_zone_order(zone, order, gfp_mask, sync,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700962 contended, page);
Mel Gorman56de7262010-05-24 14:32:30 -0700963 rc = max(status, rc);
964
Mel Gorman3e7d3442011-01-13 15:45:56 -0800965 /* If a normal allocation would succeed, stop compacting */
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -0700966 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
967 alloc_flags))
Mel Gorman56de7262010-05-24 14:32:30 -0700968 break;
969 }
970
971 return rc;
972}
973
974
Mel Gorman76ab0f52010-05-24 14:32:28 -0700975/* Compact all zones within a node */
Rik van Riel7be62de2012-03-21 16:33:52 -0700976static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -0700977{
978 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -0700979 struct zone *zone;
980
Mel Gorman76ab0f52010-05-24 14:32:28 -0700981 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -0700982
983 zone = &pgdat->node_zones[zoneid];
984 if (!populated_zone(zone))
985 continue;
986
Rik van Riel7be62de2012-03-21 16:33:52 -0700987 cc->nr_freepages = 0;
988 cc->nr_migratepages = 0;
989 cc->zone = zone;
990 INIT_LIST_HEAD(&cc->freepages);
991 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -0700992
Dan Carpenteraad6ec32012-03-21 16:33:54 -0700993 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -0700994 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -0700995
Rik van Rielaff62242012-03-21 16:33:52 -0700996 if (cc->order > 0) {
997 int ok = zone_watermark_ok(zone, cc->order,
998 low_wmark_pages(zone), 0, 0);
Minchan Kimc81758f2012-08-21 16:16:03 -0700999 if (ok && cc->order >= zone->compact_order_failed)
Rik van Rielaff62242012-03-21 16:33:52 -07001000 zone->compact_order_failed = cc->order + 1;
1001 /* Currently async compaction is never deferred. */
Linus Torvalds68e3e922012-06-03 20:05:57 -07001002 else if (!ok && cc->sync)
Rik van Rielaff62242012-03-21 16:33:52 -07001003 defer_compaction(zone, cc->order);
1004 }
1005
Rik van Riel7be62de2012-03-21 16:33:52 -07001006 VM_BUG_ON(!list_empty(&cc->freepages));
1007 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -07001008 }
1009
1010 return 0;
1011}
1012
Rik van Riel7be62de2012-03-21 16:33:52 -07001013int compact_pgdat(pg_data_t *pgdat, int order)
1014{
1015 struct compact_control cc = {
1016 .order = order,
Linus Torvalds68e3e922012-06-03 20:05:57 -07001017 .sync = false,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001018 .page = NULL,
Rik van Riel7be62de2012-03-21 16:33:52 -07001019 };
1020
1021 return __compact_pgdat(pgdat, &cc);
1022}
1023
1024static int compact_node(int nid)
1025{
Rik van Riel7be62de2012-03-21 16:33:52 -07001026 struct compact_control cc = {
1027 .order = -1,
Linus Torvalds68e3e922012-06-03 20:05:57 -07001028 .sync = true,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001029 .page = NULL,
Rik van Riel7be62de2012-03-21 16:33:52 -07001030 };
1031
Hugh Dickins8575ec22012-03-21 16:33:53 -07001032 return __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001033}
1034
Mel Gorman76ab0f52010-05-24 14:32:28 -07001035/* Compact all nodes in the system */
1036static int compact_nodes(void)
1037{
1038 int nid;
1039
Hugh Dickins8575ec22012-03-21 16:33:53 -07001040 /* Flush pending updates to the LRU lists */
1041 lru_add_drain_all();
1042
Mel Gorman76ab0f52010-05-24 14:32:28 -07001043 for_each_online_node(nid)
1044 compact_node(nid);
1045
1046 return COMPACT_COMPLETE;
1047}
1048
1049/* The written value is actually unused, all memory is compacted */
1050int sysctl_compact_memory;
1051
1052/* This is the entry point for compacting all nodes via /proc/sys/vm */
1053int sysctl_compaction_handler(struct ctl_table *table, int write,
1054 void __user *buffer, size_t *length, loff_t *ppos)
1055{
1056 if (write)
1057 return compact_nodes();
1058
1059 return 0;
1060}
Mel Gormaned4a6d72010-05-24 14:32:29 -07001061
Mel Gorman5e771902010-05-24 14:32:31 -07001062int sysctl_extfrag_handler(struct ctl_table *table, int write,
1063 void __user *buffer, size_t *length, loff_t *ppos)
1064{
1065 proc_dointvec_minmax(table, write, buffer, length, ppos);
1066
1067 return 0;
1068}
1069
Mel Gormaned4a6d72010-05-24 14:32:29 -07001070#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Kay Sievers10fbcf42011-12-21 14:48:43 -08001071ssize_t sysfs_compact_node(struct device *dev,
1072 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -07001073 const char *buf, size_t count)
1074{
Hugh Dickins8575ec22012-03-21 16:33:53 -07001075 int nid = dev->id;
1076
1077 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1078 /* Flush pending updates to the LRU lists */
1079 lru_add_drain_all();
1080
1081 compact_node(nid);
1082 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07001083
1084 return count;
1085}
Kay Sievers10fbcf42011-12-21 14:48:43 -08001086static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001087
1088int compaction_register_node(struct node *node)
1089{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001090 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001091}
1092
1093void compaction_unregister_node(struct node *node)
1094{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001095 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001096}
1097#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001098
1099#endif /* CONFIG_COMPACTION */