blob: b16dd3822995ab4b0111b84dad13e9c2f60c70ec [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();
Mel Gormanc67fe372012-08-21 16:16:17 -070078 }
79
80 if (!locked)
81 spin_lock_irqsave(lock, *flags);
82 return true;
83}
84
85static inline bool compact_trylock_irqsave(spinlock_t *lock,
86 unsigned long *flags, struct compact_control *cc)
87{
88 return compact_checklock_irqsave(lock, flags, false, cc);
89}
90
Mel Gorman1fb3f8c2012-10-08 16:29:12 -070091static void compact_capture_page(struct compact_control *cc)
92{
93 unsigned long flags;
94 int mtype, mtype_low, mtype_high;
95
96 if (!cc->page || *cc->page)
97 return;
98
99 /*
100 * For MIGRATE_MOVABLE allocations we capture a suitable page ASAP
101 * regardless of the migratetype of the freelist is is captured from.
102 * This is fine because the order for a high-order MIGRATE_MOVABLE
103 * allocation is typically at least a pageblock size and overall
104 * fragmentation is not impaired. Other allocation types must
105 * capture pages from their own migratelist because otherwise they
106 * could pollute other pageblocks like MIGRATE_MOVABLE with
107 * difficult to move pages and making fragmentation worse overall.
108 */
109 if (cc->migratetype == MIGRATE_MOVABLE) {
110 mtype_low = 0;
111 mtype_high = MIGRATE_PCPTYPES;
112 } else {
113 mtype_low = cc->migratetype;
114 mtype_high = cc->migratetype + 1;
115 }
116
117 /* Speculatively examine the free lists without zone lock */
118 for (mtype = mtype_low; mtype < mtype_high; mtype++) {
119 int order;
120 for (order = cc->order; order < MAX_ORDER; order++) {
121 struct page *page;
122 struct free_area *area;
123 area = &(cc->zone->free_area[order]);
124 if (list_empty(&area->free_list[mtype]))
125 continue;
126
127 /* Take the lock and attempt capture of the page */
128 if (!compact_trylock_irqsave(&cc->zone->lock, &flags, cc))
129 return;
130 if (!list_empty(&area->free_list[mtype])) {
131 page = list_entry(area->free_list[mtype].next,
132 struct page, lru);
133 if (capture_free_page(page, cc->order, mtype)) {
134 spin_unlock_irqrestore(&cc->zone->lock,
135 flags);
136 *cc->page = page;
137 return;
138 }
139 }
140 spin_unlock_irqrestore(&cc->zone->lock, flags);
141 }
142 }
143}
144
Mel Gormanc67fe372012-08-21 16:16:17 -0700145/*
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100146 * Isolate free pages onto a private freelist. Caller must hold zone->lock.
147 * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
148 * pages inside of the pageblock (even though it may still end up isolating
149 * some pages).
150 */
151static unsigned long isolate_freepages_block(unsigned long blockpfn,
152 unsigned long end_pfn,
153 struct list_head *freelist,
154 bool strict)
Mel Gorman748446b2010-05-24 14:32:27 -0700155{
Mel Gormanb7aba692011-01-13 15:45:54 -0800156 int nr_scanned = 0, total_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700157 struct page *cursor;
158
Mel Gorman748446b2010-05-24 14:32:27 -0700159 cursor = pfn_to_page(blockpfn);
160
161 /* Isolate free pages. This assumes the block is valid */
162 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
163 int isolated, i;
164 struct page *page = cursor;
165
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100166 if (!pfn_valid_within(blockpfn)) {
167 if (strict)
168 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700169 continue;
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100170 }
Mel Gormanb7aba692011-01-13 15:45:54 -0800171 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700172
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100173 if (!PageBuddy(page)) {
174 if (strict)
175 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700176 continue;
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100177 }
Mel Gorman748446b2010-05-24 14:32:27 -0700178
179 /* Found a free page, break it into order-0 pages */
180 isolated = split_free_page(page);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100181 if (!isolated && strict)
182 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700183 total_isolated += isolated;
184 for (i = 0; i < isolated; i++) {
185 list_add(&page->lru, freelist);
186 page++;
187 }
188
189 /* If a page was split, advance to the end of it */
190 if (isolated) {
191 blockpfn += isolated - 1;
192 cursor += isolated - 1;
193 }
194 }
195
Mel Gormanb7aba692011-01-13 15:45:54 -0800196 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
Mel Gorman748446b2010-05-24 14:32:27 -0700197 return total_isolated;
198}
199
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100200/**
201 * isolate_freepages_range() - isolate free pages.
202 * @start_pfn: The first PFN to start isolating.
203 * @end_pfn: The one-past-last PFN.
204 *
205 * Non-free pages, invalid PFNs, or zone boundaries within the
206 * [start_pfn, end_pfn) range are considered errors, cause function to
207 * undo its actions and return zero.
208 *
209 * Otherwise, function returns one-past-the-last PFN of isolated page
210 * (which may be greater then end_pfn if end fell in a middle of
211 * a free page).
212 */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100213unsigned long
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100214isolate_freepages_range(unsigned long start_pfn, unsigned long end_pfn)
215{
216 unsigned long isolated, pfn, block_end_pfn, flags;
217 struct zone *zone = NULL;
218 LIST_HEAD(freelist);
219
220 if (pfn_valid(start_pfn))
221 zone = page_zone(pfn_to_page(start_pfn));
222
223 for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
224 if (!pfn_valid(pfn) || zone != page_zone(pfn_to_page(pfn)))
225 break;
226
227 /*
228 * On subsequent iterations ALIGN() is actually not needed,
229 * but we keep it that we not to complicate the code.
230 */
231 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
232 block_end_pfn = min(block_end_pfn, end_pfn);
233
234 spin_lock_irqsave(&zone->lock, flags);
235 isolated = isolate_freepages_block(pfn, block_end_pfn,
236 &freelist, true);
237 spin_unlock_irqrestore(&zone->lock, flags);
238
239 /*
240 * In strict mode, isolate_freepages_block() returns 0 if
241 * there are any holes in the block (ie. invalid PFNs or
242 * non-free pages).
243 */
244 if (!isolated)
245 break;
246
247 /*
248 * If we managed to isolate pages, it is always (1 << n) *
249 * pageblock_nr_pages for some non-negative n. (Max order
250 * page may span two pageblocks).
251 */
252 }
253
254 /* split_free_page does not map the pages */
255 map_pages(&freelist);
256
257 if (pfn < end_pfn) {
258 /* Loop terminated early, cleanup. */
259 release_freepages(&freelist);
260 return 0;
261 }
262
263 /* We don't use freelists for anything. */
264 return pfn;
265}
266
Mel Gorman748446b2010-05-24 14:32:27 -0700267/* Update the number of anon and file isolated pages in the zone */
Mel Gormanc67fe372012-08-21 16:16:17 -0700268static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700269{
270 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700271 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700272
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700273 list_for_each_entry(page, &cc->migratepages, lru)
274 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700275
Mel Gormanc67fe372012-08-21 16:16:17 -0700276 /* If locked we can use the interrupt unsafe versions */
277 if (locked) {
278 __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
279 __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
280 } else {
281 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
282 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
283 }
Mel Gorman748446b2010-05-24 14:32:27 -0700284}
285
286/* Similar to reclaim, but different enough that they don't share logic */
287static bool too_many_isolated(struct zone *zone)
288{
Minchan Kimbc693042010-09-09 16:38:00 -0700289 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700290
291 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
292 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700293 active = zone_page_state(zone, NR_ACTIVE_FILE) +
294 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700295 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
296 zone_page_state(zone, NR_ISOLATED_ANON);
297
Minchan Kimbc693042010-09-09 16:38:00 -0700298 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700299}
300
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100301/**
302 * isolate_migratepages_range() - isolate all migrate-able pages in range.
303 * @zone: Zone pages are in.
304 * @cc: Compaction control structure.
305 * @low_pfn: The first PFN of the range.
306 * @end_pfn: The one-past-the-last PFN of the range.
307 *
308 * Isolate all pages that can be migrated from the range specified by
309 * [low_pfn, end_pfn). Returns zero if there is a fatal signal
310 * pending), otherwise PFN of the first page that was not scanned
311 * (which may be both less, equal to or more then end_pfn).
312 *
313 * Assumes that cc->migratepages is empty and cc->nr_migratepages is
314 * zero.
315 *
316 * Apart from cc->migratepages and cc->nr_migratetypes this function
317 * does not modify any cc's fields, in particular it does not modify
318 * (or read for that matter) cc->migrate_pfn.
Mel Gorman748446b2010-05-24 14:32:27 -0700319 */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100320unsigned long
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100321isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
322 unsigned long low_pfn, unsigned long end_pfn)
Mel Gorman748446b2010-05-24 14:32:27 -0700323{
Mel Gorman9927af742011-01-13 15:45:59 -0800324 unsigned long last_pageblock_nr = 0, pageblock_nr;
Mel Gormanb7aba692011-01-13 15:45:54 -0800325 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700326 struct list_head *migratelist = &cc->migratepages;
Konstantin Khlebnikovf3fd4a62012-05-29 15:06:54 -0700327 isolate_mode_t mode = 0;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700328 struct lruvec *lruvec;
Mel Gormanc67fe372012-08-21 16:16:17 -0700329 unsigned long flags;
330 bool locked;
Mel Gorman748446b2010-05-24 14:32:27 -0700331
Mel Gorman748446b2010-05-24 14:32:27 -0700332 /*
333 * Ensure that there are not too many pages isolated from the LRU
334 * list by either parallel reclaimers or compaction. If there are,
335 * delay for some time until fewer pages are isolated
336 */
337 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700338 /* async migration should just abort */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700339 if (!cc->sync)
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100340 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700341
Mel Gorman748446b2010-05-24 14:32:27 -0700342 congestion_wait(BLK_RW_ASYNC, HZ/10);
343
344 if (fatal_signal_pending(current))
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100345 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700346 }
347
348 /* Time to isolate some pages for migration */
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700349 cond_resched();
Mel Gormanc67fe372012-08-21 16:16:17 -0700350 spin_lock_irqsave(&zone->lru_lock, flags);
351 locked = true;
Mel Gorman748446b2010-05-24 14:32:27 -0700352 for (; low_pfn < end_pfn; low_pfn++) {
353 struct page *page;
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700354
355 /* give a chance to irqs before checking need_resched() */
356 if (!((low_pfn+1) % SWAP_CLUSTER_MAX)) {
Mel Gormanc67fe372012-08-21 16:16:17 -0700357 spin_unlock_irqrestore(&zone->lru_lock, flags);
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700358 locked = false;
359 }
Mel Gormanc67fe372012-08-21 16:16:17 -0700360
361 /* Check if it is ok to still hold the lock */
362 locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
363 locked, cc);
Mel Gorman3cc668f42012-10-08 16:32:30 -0700364 if (!locked || fatal_signal_pending(current))
Mel Gormanc67fe372012-08-21 16:16:17 -0700365 break;
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700366
Mel Gorman0bf380b2012-02-03 15:37:18 -0800367 /*
368 * migrate_pfn does not necessarily start aligned to a
369 * pageblock. Ensure that pfn_valid is called when moving
370 * into a new MAX_ORDER_NR_PAGES range in case of large
371 * memory holes within the zone
372 */
373 if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
374 if (!pfn_valid(low_pfn)) {
375 low_pfn += MAX_ORDER_NR_PAGES - 1;
376 continue;
377 }
378 }
379
Mel Gorman748446b2010-05-24 14:32:27 -0700380 if (!pfn_valid_within(low_pfn))
381 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800382 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700383
Mel Gormandc908602012-02-08 17:13:38 -0800384 /*
385 * Get the page and ensure the page is within the same zone.
386 * See the comment in isolate_freepages about overlapping
387 * nodes. It is deliberate that the new zone lock is not taken
388 * as memory compaction should not move pages between nodes.
389 */
Mel Gorman748446b2010-05-24 14:32:27 -0700390 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800391 if (page_zone(page) != zone)
392 continue;
393
394 /* Skip if free */
Mel Gorman748446b2010-05-24 14:32:27 -0700395 if (PageBuddy(page))
396 continue;
397
Mel Gorman9927af742011-01-13 15:45:59 -0800398 /*
399 * For async migration, also only scan in MOVABLE blocks. Async
400 * migration is optimistic to see if the minimum amount of work
401 * satisfies the allocation
402 */
403 pageblock_nr = low_pfn >> pageblock_order;
Linus Torvalds68e3e922012-06-03 20:05:57 -0700404 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
Michal Nazarewicz47118af2011-12-29 13:09:50 +0100405 !migrate_async_suitable(get_pageblock_migratetype(page))) {
Mel Gorman9927af742011-01-13 15:45:59 -0800406 low_pfn += pageblock_nr_pages;
407 low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
408 last_pageblock_nr = pageblock_nr;
409 continue;
410 }
411
Andrea Arcangelibc835012011-01-13 15:47:08 -0800412 if (!PageLRU(page))
413 continue;
414
415 /*
416 * PageLRU is set, and lru_lock excludes isolation,
417 * splitting and collapsing (collapsing has already
418 * happened if PageLRU is set).
419 */
420 if (PageTransHuge(page)) {
421 low_pfn += (1 << compound_order(page)) - 1;
422 continue;
423 }
424
Linus Torvalds68e3e922012-06-03 20:05:57 -0700425 if (!cc->sync)
Mel Gormanc8244932012-01-12 17:19:38 -0800426 mode |= ISOLATE_ASYNC_MIGRATE;
427
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700428 lruvec = mem_cgroup_page_lruvec(page, zone);
429
Mel Gorman748446b2010-05-24 14:32:27 -0700430 /* Try isolate the page */
Konstantin Khlebnikovf3fd4a62012-05-29 15:06:54 -0700431 if (__isolate_lru_page(page, mode) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700432 continue;
433
Andrea Arcangelibc835012011-01-13 15:47:08 -0800434 VM_BUG_ON(PageTransCompound(page));
435
Mel Gorman748446b2010-05-24 14:32:27 -0700436 /* Successfully isolated */
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700437 del_page_from_lru_list(page, lruvec, page_lru(page));
Mel Gorman748446b2010-05-24 14:32:27 -0700438 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700439 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800440 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700441
442 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800443 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
444 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700445 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800446 }
Mel Gorman748446b2010-05-24 14:32:27 -0700447 }
448
Mel Gormanc67fe372012-08-21 16:16:17 -0700449 acct_isolated(zone, locked, cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700450
Mel Gormanc67fe372012-08-21 16:16:17 -0700451 if (locked)
452 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700453
Mel Gormanb7aba692011-01-13 15:45:54 -0800454 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
455
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100456 return low_pfn;
457}
458
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100459#endif /* CONFIG_COMPACTION || CONFIG_CMA */
460#ifdef CONFIG_COMPACTION
461
Linus Torvalds68e3e922012-06-03 20:05:57 -0700462/* Returns true if the page is within a block suitable for migration to */
463static bool suitable_migration_target(struct page *page)
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100464{
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100465
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100466 int migratetype = get_pageblock_migratetype(page);
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100467
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100468 /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
469 if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
Linus Torvalds68e3e922012-06-03 20:05:57 -0700470 return false;
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100471
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100472 /* If the page is a large free page, then allow migration */
473 if (PageBuddy(page) && page_order(page) >= pageblock_order)
Linus Torvalds68e3e922012-06-03 20:05:57 -0700474 return true;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100475
Michal Nazarewicz47118af2011-12-29 13:09:50 +0100476 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700477 if (migrate_async_suitable(migratetype))
478 return true;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100479
480 /* Otherwise skip the block */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700481 return false;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100482}
483
484/*
Mel Gormande74f1c2012-08-21 16:16:15 -0700485 * Returns the start pfn of the last page block in a zone. This is the starting
486 * point for full compaction of a zone. Compaction searches for free pages from
487 * the end of each zone, while isolate_freepages_block scans forward inside each
488 * page block.
489 */
490static unsigned long start_free_pfn(struct zone *zone)
491{
492 unsigned long free_pfn;
493 free_pfn = zone->zone_start_pfn + zone->spanned_pages;
494 free_pfn &= ~(pageblock_nr_pages-1);
495 return free_pfn;
496}
497
498/*
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100499 * Based on information in the current compact_control, find blocks
500 * suitable for isolating free pages from and then isolate them.
501 */
502static void isolate_freepages(struct zone *zone,
503 struct compact_control *cc)
504{
505 struct page *page;
506 unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
507 unsigned long flags;
508 int nr_freepages = cc->nr_freepages;
509 struct list_head *freelist = &cc->freepages;
510
511 /*
512 * Initialise the free scanner. The starting point is where we last
513 * scanned from (or the end of the zone if starting). The low point
514 * is the end of the pageblock the migration scanner is using.
515 */
516 pfn = cc->free_pfn;
517 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
518
519 /*
520 * Take care that if the migration scanner is at the end of the zone
521 * that the free scanner does not accidentally move to the next zone
522 * in the next isolation cycle.
523 */
524 high_pfn = min(low_pfn, pfn);
525
526 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
527
528 /*
529 * Isolate free pages until enough are available to migrate the
530 * pages on cc->migratepages. We stop searching if the migrate
531 * and free page scanners meet or enough free pages are isolated.
532 */
533 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
534 pfn -= pageblock_nr_pages) {
535 unsigned long isolated;
536
537 if (!pfn_valid(pfn))
538 continue;
539
540 /*
541 * Check for overlapping nodes/zones. It's possible on some
542 * configurations to have a setup like
543 * node0 node1 node0
544 * i.e. it's possible that all pages within a zones range of
545 * pages do not belong to a single zone.
546 */
547 page = pfn_to_page(pfn);
548 if (page_zone(page) != zone)
549 continue;
550
551 /* Check the block is suitable for migration */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700552 if (!suitable_migration_target(page))
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100553 continue;
Linus Torvalds68e3e922012-06-03 20:05:57 -0700554
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100555 /*
556 * Found a block suitable for isolating free pages from. Now
557 * we disabled interrupts, double check things are ok and
558 * isolate the pages. This is to minimise the time IRQs
559 * are disabled
560 */
561 isolated = 0;
Mel Gormanc67fe372012-08-21 16:16:17 -0700562
563 /*
564 * The zone lock must be held to isolate freepages. This
565 * unfortunately this is a very coarse lock and can be
566 * heavily contended if there are parallel allocations
567 * or parallel compactions. For async compaction do not
568 * spin on the lock
569 */
570 if (!compact_trylock_irqsave(&zone->lock, &flags, cc))
571 break;
Linus Torvalds68e3e922012-06-03 20:05:57 -0700572 if (suitable_migration_target(page)) {
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100573 end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn);
574 isolated = isolate_freepages_block(pfn, end_pfn,
575 freelist, false);
576 nr_freepages += isolated;
Linus Torvalds68e3e922012-06-03 20:05:57 -0700577 }
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100578 spin_unlock_irqrestore(&zone->lock, flags);
579
580 /*
581 * Record the highest PFN we isolated pages from. When next
582 * looking for free pages, the search will restart here as
583 * page migration may have returned some pages to the allocator
584 */
Rik van Riel7db88892012-07-31 16:43:12 -0700585 if (isolated) {
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100586 high_pfn = max(high_pfn, pfn);
Mel Gormande74f1c2012-08-21 16:16:15 -0700587
588 /*
589 * If the free scanner has wrapped, update
590 * compact_cached_free_pfn to point to the highest
591 * pageblock with free pages. This reduces excessive
592 * scanning of full pageblocks near the end of the
593 * zone
594 */
595 if (cc->order > 0 && cc->wrapped)
Rik van Riel7db88892012-07-31 16:43:12 -0700596 zone->compact_cached_free_pfn = high_pfn;
597 }
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100598 }
599
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100600 /* split_free_page does not map the pages */
601 map_pages(freelist);
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100602
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100603 cc->free_pfn = high_pfn;
604 cc->nr_freepages = nr_freepages;
Mel Gormande74f1c2012-08-21 16:16:15 -0700605
606 /* If compact_cached_free_pfn is reset then set it now */
607 if (cc->order > 0 && !cc->wrapped &&
608 zone->compact_cached_free_pfn == start_free_pfn(zone))
609 zone->compact_cached_free_pfn = high_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700610}
611
612/*
613 * This is a migrate-callback that "allocates" freepages by taking pages
614 * from the isolated freelists in the block we are migrating to.
615 */
616static struct page *compaction_alloc(struct page *migratepage,
617 unsigned long data,
618 int **result)
619{
620 struct compact_control *cc = (struct compact_control *)data;
621 struct page *freepage;
622
623 /* Isolate free pages if necessary */
624 if (list_empty(&cc->freepages)) {
625 isolate_freepages(cc->zone, cc);
626
627 if (list_empty(&cc->freepages))
628 return NULL;
629 }
630
631 freepage = list_entry(cc->freepages.next, struct page, lru);
632 list_del(&freepage->lru);
633 cc->nr_freepages--;
634
635 return freepage;
636}
637
638/*
639 * We cannot control nr_migratepages and nr_freepages fully when migration is
640 * running as migrate_pages() has no knowledge of compact_control. When
641 * migration is complete, we count the number of pages on the lists by hand.
642 */
643static void update_nr_listpages(struct compact_control *cc)
644{
645 int nr_migratepages = 0;
646 int nr_freepages = 0;
647 struct page *page;
648
649 list_for_each_entry(page, &cc->migratepages, lru)
650 nr_migratepages++;
651 list_for_each_entry(page, &cc->freepages, lru)
652 nr_freepages++;
653
654 cc->nr_migratepages = nr_migratepages;
655 cc->nr_freepages = nr_freepages;
656}
657
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100658/* possible outcome of isolate_migratepages */
659typedef enum {
660 ISOLATE_ABORT, /* Abort compaction now */
661 ISOLATE_NONE, /* No pages isolated, continue scanning */
662 ISOLATE_SUCCESS, /* Pages isolated, migrate */
663} isolate_migrate_t;
664
665/*
666 * Isolate all pages that can be migrated from the block pointed to by
667 * the migrate scanner within compact_control.
668 */
669static isolate_migrate_t isolate_migratepages(struct zone *zone,
670 struct compact_control *cc)
671{
672 unsigned long low_pfn, end_pfn;
673
674 /* Do not scan outside zone boundaries */
675 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
676
677 /* Only scan within a pageblock boundary */
678 end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
679
680 /* Do not cross the free scanner or scan within a memory hole */
681 if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
682 cc->migrate_pfn = end_pfn;
683 return ISOLATE_NONE;
684 }
685
686 /* Perform the isolation */
687 low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn);
Shaohua Lie64c5232012-10-08 16:32:27 -0700688 if (!low_pfn || cc->contended)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100689 return ISOLATE_ABORT;
690
691 cc->migrate_pfn = low_pfn;
692
693 return ISOLATE_SUCCESS;
694}
695
Mel Gorman748446b2010-05-24 14:32:27 -0700696static int compact_finished(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800697 struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700698{
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800699 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -0700700
Mel Gorman748446b2010-05-24 14:32:27 -0700701 if (fatal_signal_pending(current))
702 return COMPACT_PARTIAL;
703
Rik van Riel7db88892012-07-31 16:43:12 -0700704 /*
705 * A full (order == -1) compaction run starts at the beginning and
706 * end of a zone; it completes when the migrate and free scanner meet.
707 * A partial (order > 0) compaction can start with the free scanner
708 * at a random point in the zone, and may have to restart.
709 */
710 if (cc->free_pfn <= cc->migrate_pfn) {
711 if (cc->order > 0 && !cc->wrapped) {
712 /* We started partway through; restart at the end. */
713 unsigned long free_pfn = start_free_pfn(zone);
714 zone->compact_cached_free_pfn = free_pfn;
715 cc->free_pfn = free_pfn;
716 cc->wrapped = 1;
717 return COMPACT_CONTINUE;
718 }
719 return COMPACT_COMPLETE;
720 }
721
722 /* We wrapped around and ended up where we started. */
723 if (cc->wrapped && cc->free_pfn <= cc->start_free_pfn)
Mel Gorman748446b2010-05-24 14:32:27 -0700724 return COMPACT_COMPLETE;
725
Johannes Weiner82478fb2011-01-20 14:44:21 -0800726 /*
727 * order == -1 is expected when compacting via
728 * /proc/sys/vm/compact_memory
729 */
Mel Gorman56de7262010-05-24 14:32:30 -0700730 if (cc->order == -1)
731 return COMPACT_CONTINUE;
732
Michal Hocko3957c772011-06-15 15:08:25 -0700733 /* Compaction run is not finished if the watermark is not met */
734 watermark = low_wmark_pages(zone);
735 watermark += (1 << cc->order);
736
737 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
738 return COMPACT_CONTINUE;
739
Mel Gorman56de7262010-05-24 14:32:30 -0700740 /* Direct compactor: Is a suitable page free? */
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700741 if (cc->page) {
742 /* Was a suitable page captured? */
743 if (*cc->page)
Mel Gorman56de7262010-05-24 14:32:30 -0700744 return COMPACT_PARTIAL;
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700745 } else {
746 unsigned int order;
747 for (order = cc->order; order < MAX_ORDER; order++) {
748 struct free_area *area = &zone->free_area[cc->order];
749 /* Job done if page is free of the right migratetype */
750 if (!list_empty(&area->free_list[cc->migratetype]))
751 return COMPACT_PARTIAL;
Mel Gorman56de7262010-05-24 14:32:30 -0700752
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700753 /* Job done if allocation would set block type */
754 if (cc->order >= pageblock_order && area->nr_free)
755 return COMPACT_PARTIAL;
756 }
Mel Gorman56de7262010-05-24 14:32:30 -0700757 }
758
Mel Gorman748446b2010-05-24 14:32:27 -0700759 return COMPACT_CONTINUE;
760}
761
Mel Gorman3e7d3442011-01-13 15:45:56 -0800762/*
763 * compaction_suitable: Is this suitable to run compaction on this zone now?
764 * Returns
765 * COMPACT_SKIPPED - If there are too few free pages for compaction
766 * COMPACT_PARTIAL - If the allocation would succeed without compaction
767 * COMPACT_CONTINUE - If compaction should run now
768 */
769unsigned long compaction_suitable(struct zone *zone, int order)
770{
771 int fragindex;
772 unsigned long watermark;
773
774 /*
Michal Hocko3957c772011-06-15 15:08:25 -0700775 * order == -1 is expected when compacting via
776 * /proc/sys/vm/compact_memory
777 */
778 if (order == -1)
779 return COMPACT_CONTINUE;
780
781 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -0800782 * Watermarks for order-0 must be met for compaction. Note the 2UL.
783 * This is because during migration, copies of pages need to be
784 * allocated and for a short time, the footprint is higher
785 */
786 watermark = low_wmark_pages(zone) + (2UL << order);
787 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
788 return COMPACT_SKIPPED;
789
790 /*
791 * fragmentation index determines if allocation failures are due to
792 * low memory or external fragmentation
793 *
Shaohua Lia582a732011-06-15 15:08:49 -0700794 * index of -1000 implies allocations might succeed depending on
795 * watermarks
Mel Gorman3e7d3442011-01-13 15:45:56 -0800796 * index towards 0 implies failure is due to lack of memory
797 * index towards 1000 implies failure is due to fragmentation
798 *
799 * Only compact if a failure would be due to fragmentation.
800 */
801 fragindex = fragmentation_index(zone, order);
802 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
803 return COMPACT_SKIPPED;
804
Shaohua Lia582a732011-06-15 15:08:49 -0700805 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
806 0, 0))
Mel Gorman3e7d3442011-01-13 15:45:56 -0800807 return COMPACT_PARTIAL;
808
809 return COMPACT_CONTINUE;
810}
811
Mel Gorman748446b2010-05-24 14:32:27 -0700812static int compact_zone(struct zone *zone, struct compact_control *cc)
813{
814 int ret;
815
Mel Gorman3e7d3442011-01-13 15:45:56 -0800816 ret = compaction_suitable(zone, cc->order);
817 switch (ret) {
818 case COMPACT_PARTIAL:
819 case COMPACT_SKIPPED:
820 /* Compaction is likely to fail */
821 return ret;
822 case COMPACT_CONTINUE:
823 /* Fall through to compaction */
824 ;
825 }
826
Mel Gorman748446b2010-05-24 14:32:27 -0700827 /* Setup to move all movable pages to the end of the zone */
828 cc->migrate_pfn = zone->zone_start_pfn;
Rik van Riel7db88892012-07-31 16:43:12 -0700829
830 if (cc->order > 0) {
831 /* Incremental compaction. Start where the last one stopped. */
832 cc->free_pfn = zone->compact_cached_free_pfn;
833 cc->start_free_pfn = cc->free_pfn;
834 } else {
835 /* Order == -1 starts at the end of the zone. */
836 cc->free_pfn = start_free_pfn(zone);
837 }
Mel Gorman748446b2010-05-24 14:32:27 -0700838
839 migrate_prep_local();
840
841 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
842 unsigned long nr_migrate, nr_remaining;
Minchan Kim9d502c12011-03-22 16:30:39 -0700843 int err;
Mel Gorman748446b2010-05-24 14:32:27 -0700844
Mel Gormanf9e35b32011-06-15 15:08:52 -0700845 switch (isolate_migratepages(zone, cc)) {
846 case ISOLATE_ABORT:
847 ret = COMPACT_PARTIAL;
Shaohua Lie64c5232012-10-08 16:32:27 -0700848 putback_lru_pages(&cc->migratepages);
849 cc->nr_migratepages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700850 goto out;
851 case ISOLATE_NONE:
Mel Gorman748446b2010-05-24 14:32:27 -0700852 continue;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700853 case ISOLATE_SUCCESS:
854 ;
855 }
Mel Gorman748446b2010-05-24 14:32:27 -0700856
857 nr_migrate = cc->nr_migratepages;
Minchan Kim9d502c12011-03-22 16:30:39 -0700858 err = migrate_pages(&cc->migratepages, compaction_alloc,
Linus Torvalds68e3e922012-06-03 20:05:57 -0700859 (unsigned long)cc, false,
860 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
Mel Gorman748446b2010-05-24 14:32:27 -0700861 update_nr_listpages(cc);
862 nr_remaining = cc->nr_migratepages;
863
864 count_vm_event(COMPACTBLOCKS);
865 count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
866 if (nr_remaining)
867 count_vm_events(COMPACTPAGEFAILED, nr_remaining);
Mel Gormanb7aba692011-01-13 15:45:54 -0800868 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
869 nr_remaining);
Mel Gorman748446b2010-05-24 14:32:27 -0700870
871 /* Release LRU pages not migrated */
Minchan Kim9d502c12011-03-22 16:30:39 -0700872 if (err) {
Mel Gorman748446b2010-05-24 14:32:27 -0700873 putback_lru_pages(&cc->migratepages);
874 cc->nr_migratepages = 0;
David Rientjes4bf2bba2012-07-11 14:02:13 -0700875 if (err == -ENOMEM) {
876 ret = COMPACT_PARTIAL;
877 goto out;
878 }
Mel Gorman748446b2010-05-24 14:32:27 -0700879 }
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700880
881 /* Capture a page now if it is a suitable size */
882 compact_capture_page(cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700883 }
884
Mel Gormanf9e35b32011-06-15 15:08:52 -0700885out:
Mel Gorman748446b2010-05-24 14:32:27 -0700886 /* Release free pages and check accounting */
887 cc->nr_freepages -= release_freepages(&cc->freepages);
888 VM_BUG_ON(cc->nr_freepages != 0);
889
890 return ret;
891}
Mel Gorman76ab0f52010-05-24 14:32:28 -0700892
Kyungmin Parkd43a87e2011-10-31 17:09:08 -0700893static unsigned long compact_zone_order(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800894 int order, gfp_t gfp_mask,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700895 bool sync, bool *contended,
896 struct page **page)
Mel Gorman56de7262010-05-24 14:32:30 -0700897{
Shaohua Lie64c5232012-10-08 16:32:27 -0700898 unsigned long ret;
Mel Gorman56de7262010-05-24 14:32:30 -0700899 struct compact_control cc = {
900 .nr_freepages = 0,
901 .nr_migratepages = 0,
902 .order = order,
903 .migratetype = allocflags_to_migratetype(gfp_mask),
904 .zone = zone,
Linus Torvalds68e3e922012-06-03 20:05:57 -0700905 .sync = sync,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700906 .page = page,
Mel Gorman56de7262010-05-24 14:32:30 -0700907 };
908 INIT_LIST_HEAD(&cc.freepages);
909 INIT_LIST_HEAD(&cc.migratepages);
910
Shaohua Lie64c5232012-10-08 16:32:27 -0700911 ret = compact_zone(zone, &cc);
912
913 VM_BUG_ON(!list_empty(&cc.freepages));
914 VM_BUG_ON(!list_empty(&cc.migratepages));
915
916 *contended = cc.contended;
917 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -0700918}
919
Mel Gorman5e771902010-05-24 14:32:31 -0700920int sysctl_extfrag_threshold = 500;
921
Mel Gorman56de7262010-05-24 14:32:30 -0700922/**
923 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
924 * @zonelist: The zonelist used for the current allocation
925 * @order: The order of the current allocation
926 * @gfp_mask: The GFP mask of the current allocation
927 * @nodemask: The allowed nodes to allocate from
Mel Gorman77f1fe62011-01-13 15:45:57 -0800928 * @sync: Whether migration is synchronous or not
Mel Gorman661c4cb2012-10-08 16:32:31 -0700929 * @contended: Return value that is true if compaction was aborted due to lock contention
930 * @page: Optionally capture a free page of the requested order during compaction
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 */