blob: d9dbb97e607b8b9af1032ffcaafd56a38967605c [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 Gormanbb13ffe2012-10-08 16:32:41 -070053#ifdef CONFIG_COMPACTION
54/* Returns true if the pageblock should be scanned for pages to isolate. */
55static inline bool isolation_suitable(struct compact_control *cc,
56 struct page *page)
57{
58 if (cc->ignore_skip_hint)
59 return true;
60
61 return !get_pageblock_skip(page);
62}
63
64/*
65 * This function is called to clear all cached information on pageblocks that
66 * should be skipped for page isolation when the migrate and free page scanner
67 * meet.
68 */
69static void reset_isolation_suitable(struct zone *zone)
70{
71 unsigned long start_pfn = zone->zone_start_pfn;
72 unsigned long end_pfn = zone->zone_start_pfn + zone->spanned_pages;
73 unsigned long pfn;
74
75 /*
76 * Do not reset more than once every five seconds. If allocations are
77 * failing sufficiently quickly to allow this to happen then continually
78 * scanning for compaction is not going to help. The choice of five
79 * seconds is arbitrary but will mitigate excessive scanning.
80 */
81 if (time_before(jiffies, zone->compact_blockskip_expire))
82 return;
83 zone->compact_blockskip_expire = jiffies + (HZ * 5);
84
85 /* Walk the zone and mark every pageblock as suitable for isolation */
86 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
87 struct page *page;
88
89 cond_resched();
90
91 if (!pfn_valid(pfn))
92 continue;
93
94 page = pfn_to_page(pfn);
95 if (zone != page_zone(page))
96 continue;
97
98 clear_pageblock_skip(page);
99 }
100}
101
102/*
103 * If no pages were isolated then mark this pageblock to be skipped in the
104 * future. The information is later cleared by reset_isolation_suitable().
105 */
106static void update_pageblock_skip(struct page *page, unsigned long nr_isolated)
107{
108 if (!page)
109 return;
110
111 if (!nr_isolated)
112 set_pageblock_skip(page);
113}
114#else
115static inline bool isolation_suitable(struct compact_control *cc,
116 struct page *page)
117{
118 return true;
119}
120
121static void update_pageblock_skip(struct page *page, unsigned long nr_isolated)
122{
123}
124#endif /* CONFIG_COMPACTION */
125
Mel Gorman2a1402a2012-10-08 16:32:33 -0700126static inline bool should_release_lock(spinlock_t *lock)
127{
128 return need_resched() || spin_is_contended(lock);
129}
130
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100131/*
Mel Gormanc67fe372012-08-21 16:16:17 -0700132 * Compaction requires the taking of some coarse locks that are potentially
133 * very heavily contended. Check if the process needs to be scheduled or
134 * if the lock is contended. For async compaction, back out in the event
135 * if contention is severe. For sync compaction, schedule.
136 *
137 * Returns true if the lock is held.
138 * Returns false if the lock is released and compaction should abort
139 */
140static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags,
141 bool locked, struct compact_control *cc)
142{
Mel Gorman2a1402a2012-10-08 16:32:33 -0700143 if (should_release_lock(lock)) {
Mel Gormanc67fe372012-08-21 16:16:17 -0700144 if (locked) {
145 spin_unlock_irqrestore(lock, *flags);
146 locked = false;
147 }
148
149 /* async aborts if taking too long or contended */
150 if (!cc->sync) {
Shaohua Lie64c5232012-10-08 16:32:27 -0700151 cc->contended = true;
Mel Gormanc67fe372012-08-21 16:16:17 -0700152 return false;
153 }
154
155 cond_resched();
Mel Gormanc67fe372012-08-21 16:16:17 -0700156 }
157
158 if (!locked)
159 spin_lock_irqsave(lock, *flags);
160 return true;
161}
162
163static inline bool compact_trylock_irqsave(spinlock_t *lock,
164 unsigned long *flags, struct compact_control *cc)
165{
166 return compact_checklock_irqsave(lock, flags, false, cc);
167}
168
Mel Gormanf40d1e42012-10-08 16:32:36 -0700169/* Returns true if the page is within a block suitable for migration to */
170static bool suitable_migration_target(struct page *page)
171{
172 int migratetype = get_pageblock_migratetype(page);
173
174 /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
175 if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
176 return false;
177
178 /* If the page is a large free page, then allow migration */
179 if (PageBuddy(page) && page_order(page) >= pageblock_order)
180 return true;
181
182 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
183 if (migrate_async_suitable(migratetype))
184 return true;
185
186 /* Otherwise skip the block */
187 return false;
188}
189
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700190static void compact_capture_page(struct compact_control *cc)
191{
192 unsigned long flags;
193 int mtype, mtype_low, mtype_high;
194
195 if (!cc->page || *cc->page)
196 return;
197
198 /*
199 * For MIGRATE_MOVABLE allocations we capture a suitable page ASAP
200 * regardless of the migratetype of the freelist is is captured from.
201 * This is fine because the order for a high-order MIGRATE_MOVABLE
202 * allocation is typically at least a pageblock size and overall
203 * fragmentation is not impaired. Other allocation types must
204 * capture pages from their own migratelist because otherwise they
205 * could pollute other pageblocks like MIGRATE_MOVABLE with
206 * difficult to move pages and making fragmentation worse overall.
207 */
208 if (cc->migratetype == MIGRATE_MOVABLE) {
209 mtype_low = 0;
210 mtype_high = MIGRATE_PCPTYPES;
211 } else {
212 mtype_low = cc->migratetype;
213 mtype_high = cc->migratetype + 1;
214 }
215
216 /* Speculatively examine the free lists without zone lock */
217 for (mtype = mtype_low; mtype < mtype_high; mtype++) {
218 int order;
219 for (order = cc->order; order < MAX_ORDER; order++) {
220 struct page *page;
221 struct free_area *area;
222 area = &(cc->zone->free_area[order]);
223 if (list_empty(&area->free_list[mtype]))
224 continue;
225
226 /* Take the lock and attempt capture of the page */
227 if (!compact_trylock_irqsave(&cc->zone->lock, &flags, cc))
228 return;
229 if (!list_empty(&area->free_list[mtype])) {
230 page = list_entry(area->free_list[mtype].next,
231 struct page, lru);
232 if (capture_free_page(page, cc->order, mtype)) {
233 spin_unlock_irqrestore(&cc->zone->lock,
234 flags);
235 *cc->page = page;
236 return;
237 }
238 }
239 spin_unlock_irqrestore(&cc->zone->lock, flags);
240 }
241 }
242}
243
Mel Gormanc67fe372012-08-21 16:16:17 -0700244/*
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100245 * Isolate free pages onto a private freelist. Caller must hold zone->lock.
246 * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
247 * pages inside of the pageblock (even though it may still end up isolating
248 * some pages).
249 */
Mel Gormanf40d1e42012-10-08 16:32:36 -0700250static unsigned long isolate_freepages_block(struct compact_control *cc,
251 unsigned long blockpfn,
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100252 unsigned long end_pfn,
253 struct list_head *freelist,
254 bool strict)
Mel Gorman748446b2010-05-24 14:32:27 -0700255{
Mel Gormanb7aba692011-01-13 15:45:54 -0800256 int nr_scanned = 0, total_isolated = 0;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700257 struct page *cursor, *valid_page = NULL;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700258 unsigned long nr_strict_required = end_pfn - blockpfn;
259 unsigned long flags;
260 bool locked = false;
Mel Gorman748446b2010-05-24 14:32:27 -0700261
Mel Gorman748446b2010-05-24 14:32:27 -0700262 cursor = pfn_to_page(blockpfn);
263
Mel Gormanf40d1e42012-10-08 16:32:36 -0700264 /* Isolate free pages. */
Mel Gorman748446b2010-05-24 14:32:27 -0700265 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
266 int isolated, i;
267 struct page *page = cursor;
268
Mel Gormanb7aba692011-01-13 15:45:54 -0800269 nr_scanned++;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700270 if (!pfn_valid_within(blockpfn))
Mel Gorman748446b2010-05-24 14:32:27 -0700271 continue;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700272 if (!valid_page)
273 valid_page = page;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700274 if (!PageBuddy(page))
275 continue;
276
277 /*
278 * The zone lock must be held to isolate freepages.
279 * Unfortunately this is a very coarse lock and can be
280 * heavily contended if there are parallel allocations
281 * or parallel compactions. For async compaction do not
282 * spin on the lock and we acquire the lock as late as
283 * possible.
284 */
285 locked = compact_checklock_irqsave(&cc->zone->lock, &flags,
286 locked, cc);
287 if (!locked)
288 break;
289
290 /* Recheck this is a suitable migration target under lock */
291 if (!strict && !suitable_migration_target(page))
292 break;
293
294 /* Recheck this is a buddy page under lock */
295 if (!PageBuddy(page))
296 continue;
Mel Gorman748446b2010-05-24 14:32:27 -0700297
298 /* Found a free page, break it into order-0 pages */
299 isolated = split_free_page(page);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100300 if (!isolated && strict)
Mel Gormanf40d1e42012-10-08 16:32:36 -0700301 break;
Mel Gorman748446b2010-05-24 14:32:27 -0700302 total_isolated += isolated;
303 for (i = 0; i < isolated; i++) {
304 list_add(&page->lru, freelist);
305 page++;
306 }
307
308 /* If a page was split, advance to the end of it */
309 if (isolated) {
310 blockpfn += isolated - 1;
311 cursor += isolated - 1;
312 }
313 }
314
Mel Gormanb7aba692011-01-13 15:45:54 -0800315 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
Mel Gormanf40d1e42012-10-08 16:32:36 -0700316
317 /*
318 * If strict isolation is requested by CMA then check that all the
319 * pages requested were isolated. If there were any failures, 0 is
320 * returned and CMA will fail.
321 */
322 if (strict && nr_strict_required != total_isolated)
323 total_isolated = 0;
324
325 if (locked)
326 spin_unlock_irqrestore(&cc->zone->lock, flags);
327
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700328 /* Update the pageblock-skip if the whole pageblock was scanned */
329 if (blockpfn == end_pfn)
330 update_pageblock_skip(valid_page, total_isolated);
331
Mel Gorman748446b2010-05-24 14:32:27 -0700332 return total_isolated;
333}
334
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100335/**
336 * isolate_freepages_range() - isolate free pages.
337 * @start_pfn: The first PFN to start isolating.
338 * @end_pfn: The one-past-last PFN.
339 *
340 * Non-free pages, invalid PFNs, or zone boundaries within the
341 * [start_pfn, end_pfn) range are considered errors, cause function to
342 * undo its actions and return zero.
343 *
344 * Otherwise, function returns one-past-the-last PFN of isolated page
345 * (which may be greater then end_pfn if end fell in a middle of
346 * a free page).
347 */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100348unsigned long
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700349isolate_freepages_range(struct compact_control *cc,
350 unsigned long start_pfn, unsigned long end_pfn)
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100351{
Mel Gormanf40d1e42012-10-08 16:32:36 -0700352 unsigned long isolated, pfn, block_end_pfn;
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100353 LIST_HEAD(freelist);
354
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100355 for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700356 if (!pfn_valid(pfn) || cc->zone != page_zone(pfn_to_page(pfn)))
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100357 break;
358
359 /*
360 * On subsequent iterations ALIGN() is actually not needed,
361 * but we keep it that we not to complicate the code.
362 */
363 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
364 block_end_pfn = min(block_end_pfn, end_pfn);
365
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700366 isolated = isolate_freepages_block(cc, pfn, block_end_pfn,
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100367 &freelist, true);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100368
369 /*
370 * In strict mode, isolate_freepages_block() returns 0 if
371 * there are any holes in the block (ie. invalid PFNs or
372 * non-free pages).
373 */
374 if (!isolated)
375 break;
376
377 /*
378 * If we managed to isolate pages, it is always (1 << n) *
379 * pageblock_nr_pages for some non-negative n. (Max order
380 * page may span two pageblocks).
381 */
382 }
383
384 /* split_free_page does not map the pages */
385 map_pages(&freelist);
386
387 if (pfn < end_pfn) {
388 /* Loop terminated early, cleanup. */
389 release_freepages(&freelist);
390 return 0;
391 }
392
393 /* We don't use freelists for anything. */
394 return pfn;
395}
396
Mel Gorman748446b2010-05-24 14:32:27 -0700397/* Update the number of anon and file isolated pages in the zone */
Mel Gormanc67fe372012-08-21 16:16:17 -0700398static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700399{
400 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700401 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700402
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700403 list_for_each_entry(page, &cc->migratepages, lru)
404 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700405
Mel Gormanc67fe372012-08-21 16:16:17 -0700406 /* If locked we can use the interrupt unsafe versions */
407 if (locked) {
408 __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
409 __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
410 } else {
411 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
412 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
413 }
Mel Gorman748446b2010-05-24 14:32:27 -0700414}
415
416/* Similar to reclaim, but different enough that they don't share logic */
417static bool too_many_isolated(struct zone *zone)
418{
Minchan Kimbc693042010-09-09 16:38:00 -0700419 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700420
421 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
422 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700423 active = zone_page_state(zone, NR_ACTIVE_FILE) +
424 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700425 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
426 zone_page_state(zone, NR_ISOLATED_ANON);
427
Minchan Kimbc693042010-09-09 16:38:00 -0700428 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700429}
430
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100431/**
432 * isolate_migratepages_range() - isolate all migrate-able pages in range.
433 * @zone: Zone pages are in.
434 * @cc: Compaction control structure.
435 * @low_pfn: The first PFN of the range.
436 * @end_pfn: The one-past-the-last PFN of the range.
437 *
438 * Isolate all pages that can be migrated from the range specified by
439 * [low_pfn, end_pfn). Returns zero if there is a fatal signal
440 * pending), otherwise PFN of the first page that was not scanned
441 * (which may be both less, equal to or more then end_pfn).
442 *
443 * Assumes that cc->migratepages is empty and cc->nr_migratepages is
444 * zero.
445 *
446 * Apart from cc->migratepages and cc->nr_migratetypes this function
447 * does not modify any cc's fields, in particular it does not modify
448 * (or read for that matter) cc->migrate_pfn.
Mel Gorman748446b2010-05-24 14:32:27 -0700449 */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100450unsigned long
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100451isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
452 unsigned long low_pfn, unsigned long end_pfn)
Mel Gorman748446b2010-05-24 14:32:27 -0700453{
Mel Gorman9927af742011-01-13 15:45:59 -0800454 unsigned long last_pageblock_nr = 0, pageblock_nr;
Mel Gormanb7aba692011-01-13 15:45:54 -0800455 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700456 struct list_head *migratelist = &cc->migratepages;
Konstantin Khlebnikovf3fd4a62012-05-29 15:06:54 -0700457 isolate_mode_t mode = 0;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700458 struct lruvec *lruvec;
Mel Gormanc67fe372012-08-21 16:16:17 -0700459 unsigned long flags;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700460 bool locked = false;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700461 struct page *page = NULL, *valid_page = NULL;
Mel Gorman748446b2010-05-24 14:32:27 -0700462
Mel Gorman748446b2010-05-24 14:32:27 -0700463 /*
464 * Ensure that there are not too many pages isolated from the LRU
465 * list by either parallel reclaimers or compaction. If there are,
466 * delay for some time until fewer pages are isolated
467 */
468 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700469 /* async migration should just abort */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700470 if (!cc->sync)
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100471 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700472
Mel Gorman748446b2010-05-24 14:32:27 -0700473 congestion_wait(BLK_RW_ASYNC, HZ/10);
474
475 if (fatal_signal_pending(current))
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100476 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700477 }
478
479 /* Time to isolate some pages for migration */
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700480 cond_resched();
Mel Gorman748446b2010-05-24 14:32:27 -0700481 for (; low_pfn < end_pfn; low_pfn++) {
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700482 /* give a chance to irqs before checking need_resched() */
Mel Gorman2a1402a2012-10-08 16:32:33 -0700483 if (locked && !((low_pfn+1) % SWAP_CLUSTER_MAX)) {
484 if (should_release_lock(&zone->lru_lock)) {
485 spin_unlock_irqrestore(&zone->lru_lock, flags);
486 locked = false;
487 }
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700488 }
Mel Gormanc67fe372012-08-21 16:16:17 -0700489
Mel Gorman0bf380b2012-02-03 15:37:18 -0800490 /*
491 * migrate_pfn does not necessarily start aligned to a
492 * pageblock. Ensure that pfn_valid is called when moving
493 * into a new MAX_ORDER_NR_PAGES range in case of large
494 * memory holes within the zone
495 */
496 if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
497 if (!pfn_valid(low_pfn)) {
498 low_pfn += MAX_ORDER_NR_PAGES - 1;
499 continue;
500 }
501 }
502
Mel Gorman748446b2010-05-24 14:32:27 -0700503 if (!pfn_valid_within(low_pfn))
504 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800505 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700506
Mel Gormandc908602012-02-08 17:13:38 -0800507 /*
508 * Get the page and ensure the page is within the same zone.
509 * See the comment in isolate_freepages about overlapping
510 * nodes. It is deliberate that the new zone lock is not taken
511 * as memory compaction should not move pages between nodes.
512 */
Mel Gorman748446b2010-05-24 14:32:27 -0700513 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800514 if (page_zone(page) != zone)
515 continue;
516
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700517 if (!valid_page)
518 valid_page = page;
519
520 /* If isolation recently failed, do not retry */
521 pageblock_nr = low_pfn >> pageblock_order;
522 if (!isolation_suitable(cc, page))
523 goto next_pageblock;
524
Mel Gormandc908602012-02-08 17:13:38 -0800525 /* Skip if free */
Mel Gorman748446b2010-05-24 14:32:27 -0700526 if (PageBuddy(page))
527 continue;
528
Mel Gorman9927af742011-01-13 15:45:59 -0800529 /*
530 * For async migration, also only scan in MOVABLE blocks. Async
531 * migration is optimistic to see if the minimum amount of work
532 * satisfies the allocation
533 */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700534 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
Michal Nazarewicz47118af2011-12-29 13:09:50 +0100535 !migrate_async_suitable(get_pageblock_migratetype(page))) {
Mel Gorman2a1402a2012-10-08 16:32:33 -0700536 goto next_pageblock;
Mel Gorman9927af742011-01-13 15:45:59 -0800537 }
538
Mel Gorman2a1402a2012-10-08 16:32:33 -0700539 /* Check may be lockless but that's ok as we recheck later */
Andrea Arcangelibc835012011-01-13 15:47:08 -0800540 if (!PageLRU(page))
541 continue;
542
543 /*
Mel Gorman2a1402a2012-10-08 16:32:33 -0700544 * PageLRU is set. lru_lock normally excludes isolation
545 * splitting and collapsing (collapsing has already happened
546 * if PageLRU is set) but the lock is not necessarily taken
547 * here and it is wasteful to take it just to check transhuge.
548 * Check TransHuge without lock and skip the whole pageblock if
549 * it's either a transhuge or hugetlbfs page, as calling
550 * compound_order() without preventing THP from splitting the
551 * page underneath us may return surprising results.
Andrea Arcangelibc835012011-01-13 15:47:08 -0800552 */
553 if (PageTransHuge(page)) {
Mel Gorman2a1402a2012-10-08 16:32:33 -0700554 if (!locked)
555 goto next_pageblock;
556 low_pfn += (1 << compound_order(page)) - 1;
557 continue;
558 }
559
560 /* Check if it is ok to still hold the lock */
561 locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
562 locked, cc);
563 if (!locked || fatal_signal_pending(current))
564 break;
565
566 /* Recheck PageLRU and PageTransHuge under lock */
567 if (!PageLRU(page))
568 continue;
569 if (PageTransHuge(page)) {
Andrea Arcangelibc835012011-01-13 15:47:08 -0800570 low_pfn += (1 << compound_order(page)) - 1;
571 continue;
572 }
573
Linus Torvalds68e3e922012-06-03 20:05:57 -0700574 if (!cc->sync)
Mel Gormanc8244932012-01-12 17:19:38 -0800575 mode |= ISOLATE_ASYNC_MIGRATE;
576
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700577 lruvec = mem_cgroup_page_lruvec(page, zone);
578
Mel Gorman748446b2010-05-24 14:32:27 -0700579 /* Try isolate the page */
Konstantin Khlebnikovf3fd4a62012-05-29 15:06:54 -0700580 if (__isolate_lru_page(page, mode) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700581 continue;
582
Andrea Arcangelibc835012011-01-13 15:47:08 -0800583 VM_BUG_ON(PageTransCompound(page));
584
Mel Gorman748446b2010-05-24 14:32:27 -0700585 /* Successfully isolated */
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700586 del_page_from_lru_list(page, lruvec, page_lru(page));
Mel Gorman748446b2010-05-24 14:32:27 -0700587 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700588 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800589 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700590
591 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800592 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
593 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700594 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800595 }
Mel Gorman2a1402a2012-10-08 16:32:33 -0700596
597 continue;
598
599next_pageblock:
600 low_pfn += pageblock_nr_pages;
601 low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
602 last_pageblock_nr = pageblock_nr;
Mel Gorman748446b2010-05-24 14:32:27 -0700603 }
604
Mel Gormanc67fe372012-08-21 16:16:17 -0700605 acct_isolated(zone, locked, cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700606
Mel Gormanc67fe372012-08-21 16:16:17 -0700607 if (locked)
608 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700609
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700610 /* Update the pageblock-skip if the whole pageblock was scanned */
611 if (low_pfn == end_pfn)
612 update_pageblock_skip(valid_page, nr_isolated);
613
Mel Gormanb7aba692011-01-13 15:45:54 -0800614 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
615
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100616 return low_pfn;
617}
618
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100619#endif /* CONFIG_COMPACTION || CONFIG_CMA */
620#ifdef CONFIG_COMPACTION
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100621/*
622 * Based on information in the current compact_control, find blocks
623 * suitable for isolating free pages from and then isolate them.
624 */
625static void isolate_freepages(struct zone *zone,
626 struct compact_control *cc)
627{
628 struct page *page;
629 unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100630 int nr_freepages = cc->nr_freepages;
631 struct list_head *freelist = &cc->freepages;
632
633 /*
634 * Initialise the free scanner. The starting point is where we last
635 * scanned from (or the end of the zone if starting). The low point
636 * is the end of the pageblock the migration scanner is using.
637 */
638 pfn = cc->free_pfn;
639 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
640
641 /*
642 * Take care that if the migration scanner is at the end of the zone
643 * that the free scanner does not accidentally move to the next zone
644 * in the next isolation cycle.
645 */
646 high_pfn = min(low_pfn, pfn);
647
648 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
649
650 /*
651 * Isolate free pages until enough are available to migrate the
652 * pages on cc->migratepages. We stop searching if the migrate
653 * and free page scanners meet or enough free pages are isolated.
654 */
655 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
656 pfn -= pageblock_nr_pages) {
657 unsigned long isolated;
658
659 if (!pfn_valid(pfn))
660 continue;
661
662 /*
663 * Check for overlapping nodes/zones. It's possible on some
664 * configurations to have a setup like
665 * node0 node1 node0
666 * i.e. it's possible that all pages within a zones range of
667 * pages do not belong to a single zone.
668 */
669 page = pfn_to_page(pfn);
670 if (page_zone(page) != zone)
671 continue;
672
673 /* Check the block is suitable for migration */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700674 if (!suitable_migration_target(page))
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100675 continue;
Linus Torvalds68e3e922012-06-03 20:05:57 -0700676
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700677 /* If isolation recently failed, do not retry */
678 if (!isolation_suitable(cc, page))
679 continue;
680
Mel Gormanf40d1e42012-10-08 16:32:36 -0700681 /* Found a block suitable for isolating free pages from */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100682 isolated = 0;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700683 end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn);
684 isolated = isolate_freepages_block(cc, pfn, end_pfn,
685 freelist, false);
686 nr_freepages += isolated;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100687
688 /*
689 * Record the highest PFN we isolated pages from. When next
690 * looking for free pages, the search will restart here as
691 * page migration may have returned some pages to the allocator
692 */
Mel Gorman753341a2012-10-08 16:32:40 -0700693 if (isolated)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100694 high_pfn = max(high_pfn, pfn);
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100695 }
696
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100697 /* split_free_page does not map the pages */
698 map_pages(freelist);
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100699
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100700 cc->free_pfn = high_pfn;
701 cc->nr_freepages = nr_freepages;
Mel Gorman748446b2010-05-24 14:32:27 -0700702}
703
704/*
705 * This is a migrate-callback that "allocates" freepages by taking pages
706 * from the isolated freelists in the block we are migrating to.
707 */
708static struct page *compaction_alloc(struct page *migratepage,
709 unsigned long data,
710 int **result)
711{
712 struct compact_control *cc = (struct compact_control *)data;
713 struct page *freepage;
714
715 /* Isolate free pages if necessary */
716 if (list_empty(&cc->freepages)) {
717 isolate_freepages(cc->zone, cc);
718
719 if (list_empty(&cc->freepages))
720 return NULL;
721 }
722
723 freepage = list_entry(cc->freepages.next, struct page, lru);
724 list_del(&freepage->lru);
725 cc->nr_freepages--;
726
727 return freepage;
728}
729
730/*
731 * We cannot control nr_migratepages and nr_freepages fully when migration is
732 * running as migrate_pages() has no knowledge of compact_control. When
733 * migration is complete, we count the number of pages on the lists by hand.
734 */
735static void update_nr_listpages(struct compact_control *cc)
736{
737 int nr_migratepages = 0;
738 int nr_freepages = 0;
739 struct page *page;
740
741 list_for_each_entry(page, &cc->migratepages, lru)
742 nr_migratepages++;
743 list_for_each_entry(page, &cc->freepages, lru)
744 nr_freepages++;
745
746 cc->nr_migratepages = nr_migratepages;
747 cc->nr_freepages = nr_freepages;
748}
749
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100750/* possible outcome of isolate_migratepages */
751typedef enum {
752 ISOLATE_ABORT, /* Abort compaction now */
753 ISOLATE_NONE, /* No pages isolated, continue scanning */
754 ISOLATE_SUCCESS, /* Pages isolated, migrate */
755} isolate_migrate_t;
756
757/*
758 * Isolate all pages that can be migrated from the block pointed to by
759 * the migrate scanner within compact_control.
760 */
761static isolate_migrate_t isolate_migratepages(struct zone *zone,
762 struct compact_control *cc)
763{
764 unsigned long low_pfn, end_pfn;
765
766 /* Do not scan outside zone boundaries */
767 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
768
769 /* Only scan within a pageblock boundary */
770 end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
771
772 /* Do not cross the free scanner or scan within a memory hole */
773 if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
774 cc->migrate_pfn = end_pfn;
775 return ISOLATE_NONE;
776 }
777
778 /* Perform the isolation */
779 low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn);
Shaohua Lie64c5232012-10-08 16:32:27 -0700780 if (!low_pfn || cc->contended)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100781 return ISOLATE_ABORT;
782
783 cc->migrate_pfn = low_pfn;
784
785 return ISOLATE_SUCCESS;
786}
787
Mel Gorman748446b2010-05-24 14:32:27 -0700788static int compact_finished(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800789 struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700790{
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800791 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -0700792
Mel Gorman748446b2010-05-24 14:32:27 -0700793 if (fatal_signal_pending(current))
794 return COMPACT_PARTIAL;
795
Mel Gorman753341a2012-10-08 16:32:40 -0700796 /* Compaction run completes if the migrate and free scanner meet */
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700797 if (cc->free_pfn <= cc->migrate_pfn) {
798 reset_isolation_suitable(cc->zone);
Mel Gorman748446b2010-05-24 14:32:27 -0700799 return COMPACT_COMPLETE;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700800 }
Mel Gorman748446b2010-05-24 14:32:27 -0700801
Johannes Weiner82478fb2011-01-20 14:44:21 -0800802 /*
803 * order == -1 is expected when compacting via
804 * /proc/sys/vm/compact_memory
805 */
Mel Gorman56de7262010-05-24 14:32:30 -0700806 if (cc->order == -1)
807 return COMPACT_CONTINUE;
808
Michal Hocko3957c772011-06-15 15:08:25 -0700809 /* Compaction run is not finished if the watermark is not met */
810 watermark = low_wmark_pages(zone);
811 watermark += (1 << cc->order);
812
813 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
814 return COMPACT_CONTINUE;
815
Mel Gorman56de7262010-05-24 14:32:30 -0700816 /* Direct compactor: Is a suitable page free? */
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700817 if (cc->page) {
818 /* Was a suitable page captured? */
819 if (*cc->page)
Mel Gorman56de7262010-05-24 14:32:30 -0700820 return COMPACT_PARTIAL;
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700821 } else {
822 unsigned int order;
823 for (order = cc->order; order < MAX_ORDER; order++) {
824 struct free_area *area = &zone->free_area[cc->order];
825 /* Job done if page is free of the right migratetype */
826 if (!list_empty(&area->free_list[cc->migratetype]))
827 return COMPACT_PARTIAL;
Mel Gorman56de7262010-05-24 14:32:30 -0700828
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700829 /* Job done if allocation would set block type */
830 if (cc->order >= pageblock_order && area->nr_free)
831 return COMPACT_PARTIAL;
832 }
Mel Gorman56de7262010-05-24 14:32:30 -0700833 }
834
Mel Gorman748446b2010-05-24 14:32:27 -0700835 return COMPACT_CONTINUE;
836}
837
Mel Gorman3e7d3442011-01-13 15:45:56 -0800838/*
839 * compaction_suitable: Is this suitable to run compaction on this zone now?
840 * Returns
841 * COMPACT_SKIPPED - If there are too few free pages for compaction
842 * COMPACT_PARTIAL - If the allocation would succeed without compaction
843 * COMPACT_CONTINUE - If compaction should run now
844 */
845unsigned long compaction_suitable(struct zone *zone, int order)
846{
847 int fragindex;
848 unsigned long watermark;
849
850 /*
Michal Hocko3957c772011-06-15 15:08:25 -0700851 * order == -1 is expected when compacting via
852 * /proc/sys/vm/compact_memory
853 */
854 if (order == -1)
855 return COMPACT_CONTINUE;
856
857 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -0800858 * Watermarks for order-0 must be met for compaction. Note the 2UL.
859 * This is because during migration, copies of pages need to be
860 * allocated and for a short time, the footprint is higher
861 */
862 watermark = low_wmark_pages(zone) + (2UL << order);
863 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
864 return COMPACT_SKIPPED;
865
866 /*
867 * fragmentation index determines if allocation failures are due to
868 * low memory or external fragmentation
869 *
Shaohua Lia582a732011-06-15 15:08:49 -0700870 * index of -1000 implies allocations might succeed depending on
871 * watermarks
Mel Gorman3e7d3442011-01-13 15:45:56 -0800872 * index towards 0 implies failure is due to lack of memory
873 * index towards 1000 implies failure is due to fragmentation
874 *
875 * Only compact if a failure would be due to fragmentation.
876 */
877 fragindex = fragmentation_index(zone, order);
878 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
879 return COMPACT_SKIPPED;
880
Shaohua Lia582a732011-06-15 15:08:49 -0700881 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
882 0, 0))
Mel Gorman3e7d3442011-01-13 15:45:56 -0800883 return COMPACT_PARTIAL;
884
885 return COMPACT_CONTINUE;
886}
887
Mel Gorman748446b2010-05-24 14:32:27 -0700888static int compact_zone(struct zone *zone, struct compact_control *cc)
889{
890 int ret;
891
Mel Gorman3e7d3442011-01-13 15:45:56 -0800892 ret = compaction_suitable(zone, cc->order);
893 switch (ret) {
894 case COMPACT_PARTIAL:
895 case COMPACT_SKIPPED:
896 /* Compaction is likely to fail */
897 return ret;
898 case COMPACT_CONTINUE:
899 /* Fall through to compaction */
900 ;
901 }
902
Mel Gorman748446b2010-05-24 14:32:27 -0700903 /* Setup to move all movable pages to the end of the zone */
904 cc->migrate_pfn = zone->zone_start_pfn;
Mel Gorman753341a2012-10-08 16:32:40 -0700905 cc->free_pfn = cc->migrate_pfn + zone->spanned_pages;
906 cc->free_pfn &= ~(pageblock_nr_pages-1);
Mel Gorman748446b2010-05-24 14:32:27 -0700907
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700908 /* Clear pageblock skip if there are numerous alloc failures */
909 if (zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT)
910 reset_isolation_suitable(zone);
911
Mel Gorman748446b2010-05-24 14:32:27 -0700912 migrate_prep_local();
913
914 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
915 unsigned long nr_migrate, nr_remaining;
Minchan Kim9d502c12011-03-22 16:30:39 -0700916 int err;
Mel Gorman748446b2010-05-24 14:32:27 -0700917
Mel Gormanf9e35b32011-06-15 15:08:52 -0700918 switch (isolate_migratepages(zone, cc)) {
919 case ISOLATE_ABORT:
920 ret = COMPACT_PARTIAL;
Shaohua Lie64c5232012-10-08 16:32:27 -0700921 putback_lru_pages(&cc->migratepages);
922 cc->nr_migratepages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700923 goto out;
924 case ISOLATE_NONE:
Mel Gorman748446b2010-05-24 14:32:27 -0700925 continue;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700926 case ISOLATE_SUCCESS:
927 ;
928 }
Mel Gorman748446b2010-05-24 14:32:27 -0700929
930 nr_migrate = cc->nr_migratepages;
Minchan Kim9d502c12011-03-22 16:30:39 -0700931 err = migrate_pages(&cc->migratepages, compaction_alloc,
Linus Torvalds68e3e922012-06-03 20:05:57 -0700932 (unsigned long)cc, false,
933 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
Mel Gorman748446b2010-05-24 14:32:27 -0700934 update_nr_listpages(cc);
935 nr_remaining = cc->nr_migratepages;
936
937 count_vm_event(COMPACTBLOCKS);
938 count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
939 if (nr_remaining)
940 count_vm_events(COMPACTPAGEFAILED, nr_remaining);
Mel Gormanb7aba692011-01-13 15:45:54 -0800941 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
942 nr_remaining);
Mel Gorman748446b2010-05-24 14:32:27 -0700943
944 /* Release LRU pages not migrated */
Minchan Kim9d502c12011-03-22 16:30:39 -0700945 if (err) {
Mel Gorman748446b2010-05-24 14:32:27 -0700946 putback_lru_pages(&cc->migratepages);
947 cc->nr_migratepages = 0;
David Rientjes4bf2bba2012-07-11 14:02:13 -0700948 if (err == -ENOMEM) {
949 ret = COMPACT_PARTIAL;
950 goto out;
951 }
Mel Gorman748446b2010-05-24 14:32:27 -0700952 }
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700953
954 /* Capture a page now if it is a suitable size */
955 compact_capture_page(cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700956 }
957
Mel Gormanf9e35b32011-06-15 15:08:52 -0700958out:
Mel Gorman748446b2010-05-24 14:32:27 -0700959 /* Release free pages and check accounting */
960 cc->nr_freepages -= release_freepages(&cc->freepages);
961 VM_BUG_ON(cc->nr_freepages != 0);
962
963 return ret;
964}
Mel Gorman76ab0f52010-05-24 14:32:28 -0700965
Kyungmin Parkd43a87e2011-10-31 17:09:08 -0700966static unsigned long compact_zone_order(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800967 int order, gfp_t gfp_mask,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700968 bool sync, bool *contended,
969 struct page **page)
Mel Gorman56de7262010-05-24 14:32:30 -0700970{
Shaohua Lie64c5232012-10-08 16:32:27 -0700971 unsigned long ret;
Mel Gorman56de7262010-05-24 14:32:30 -0700972 struct compact_control cc = {
973 .nr_freepages = 0,
974 .nr_migratepages = 0,
975 .order = order,
976 .migratetype = allocflags_to_migratetype(gfp_mask),
977 .zone = zone,
Linus Torvalds68e3e922012-06-03 20:05:57 -0700978 .sync = sync,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -0700979 .page = page,
Mel Gorman56de7262010-05-24 14:32:30 -0700980 };
981 INIT_LIST_HEAD(&cc.freepages);
982 INIT_LIST_HEAD(&cc.migratepages);
983
Shaohua Lie64c5232012-10-08 16:32:27 -0700984 ret = compact_zone(zone, &cc);
985
986 VM_BUG_ON(!list_empty(&cc.freepages));
987 VM_BUG_ON(!list_empty(&cc.migratepages));
988
989 *contended = cc.contended;
990 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -0700991}
992
Mel Gorman5e771902010-05-24 14:32:31 -0700993int sysctl_extfrag_threshold = 500;
994
Mel Gorman56de7262010-05-24 14:32:30 -0700995/**
996 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
997 * @zonelist: The zonelist used for the current allocation
998 * @order: The order of the current allocation
999 * @gfp_mask: The GFP mask of the current allocation
1000 * @nodemask: The allowed nodes to allocate from
Mel Gorman77f1fe62011-01-13 15:45:57 -08001001 * @sync: Whether migration is synchronous or not
Mel Gorman661c4cb2012-10-08 16:32:31 -07001002 * @contended: Return value that is true if compaction was aborted due to lock contention
1003 * @page: Optionally capture a free page of the requested order during compaction
Mel Gorman56de7262010-05-24 14:32:30 -07001004 *
1005 * This is the main entry point for direct page compaction.
1006 */
1007unsigned long try_to_compact_pages(struct zonelist *zonelist,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001008 int order, gfp_t gfp_mask, nodemask_t *nodemask,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001009 bool sync, bool *contended, struct page **page)
Mel Gorman56de7262010-05-24 14:32:30 -07001010{
1011 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
1012 int may_enter_fs = gfp_mask & __GFP_FS;
1013 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -07001014 struct zoneref *z;
1015 struct zone *zone;
1016 int rc = COMPACT_SKIPPED;
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001017 int alloc_flags = 0;
Mel Gorman56de7262010-05-24 14:32:30 -07001018
Mel Gorman4ffb6332012-10-08 16:29:09 -07001019 /* Check if the GFP flags allow compaction */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -08001020 if (!order || !may_enter_fs || !may_perform_io)
Mel Gorman56de7262010-05-24 14:32:30 -07001021 return rc;
1022
1023 count_vm_event(COMPACTSTALL);
1024
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001025#ifdef CONFIG_CMA
1026 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
1027 alloc_flags |= ALLOC_CMA;
1028#endif
Mel Gorman56de7262010-05-24 14:32:30 -07001029 /* Compact each zone in the list */
1030 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
1031 nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -07001032 int status;
1033
Mel Gormanc67fe372012-08-21 16:16:17 -07001034 status = compact_zone_order(zone, order, gfp_mask, sync,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001035 contended, page);
Mel Gorman56de7262010-05-24 14:32:30 -07001036 rc = max(status, rc);
1037
Mel Gorman3e7d3442011-01-13 15:45:56 -08001038 /* If a normal allocation would succeed, stop compacting */
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001039 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
1040 alloc_flags))
Mel Gorman56de7262010-05-24 14:32:30 -07001041 break;
1042 }
1043
1044 return rc;
1045}
1046
1047
Mel Gorman76ab0f52010-05-24 14:32:28 -07001048/* Compact all zones within a node */
Rik van Riel7be62de2012-03-21 16:33:52 -07001049static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001050{
1051 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -07001052 struct zone *zone;
1053
Mel Gorman76ab0f52010-05-24 14:32:28 -07001054 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -07001055
1056 zone = &pgdat->node_zones[zoneid];
1057 if (!populated_zone(zone))
1058 continue;
1059
Rik van Riel7be62de2012-03-21 16:33:52 -07001060 cc->nr_freepages = 0;
1061 cc->nr_migratepages = 0;
1062 cc->zone = zone;
1063 INIT_LIST_HEAD(&cc->freepages);
1064 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001065
Dan Carpenteraad6ec32012-03-21 16:33:54 -07001066 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -07001067 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001068
Rik van Rielaff62242012-03-21 16:33:52 -07001069 if (cc->order > 0) {
1070 int ok = zone_watermark_ok(zone, cc->order,
1071 low_wmark_pages(zone), 0, 0);
Minchan Kimc81758f2012-08-21 16:16:03 -07001072 if (ok && cc->order >= zone->compact_order_failed)
Rik van Rielaff62242012-03-21 16:33:52 -07001073 zone->compact_order_failed = cc->order + 1;
1074 /* Currently async compaction is never deferred. */
Linus Torvalds68e3e922012-06-03 20:05:57 -07001075 else if (!ok && cc->sync)
Rik van Rielaff62242012-03-21 16:33:52 -07001076 defer_compaction(zone, cc->order);
1077 }
1078
Rik van Riel7be62de2012-03-21 16:33:52 -07001079 VM_BUG_ON(!list_empty(&cc->freepages));
1080 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -07001081 }
1082
1083 return 0;
1084}
1085
Rik van Riel7be62de2012-03-21 16:33:52 -07001086int compact_pgdat(pg_data_t *pgdat, int order)
1087{
1088 struct compact_control cc = {
1089 .order = order,
Linus Torvalds68e3e922012-06-03 20:05:57 -07001090 .sync = false,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001091 .page = NULL,
Rik van Riel7be62de2012-03-21 16:33:52 -07001092 };
1093
1094 return __compact_pgdat(pgdat, &cc);
1095}
1096
1097static int compact_node(int nid)
1098{
Rik van Riel7be62de2012-03-21 16:33:52 -07001099 struct compact_control cc = {
1100 .order = -1,
Linus Torvalds68e3e922012-06-03 20:05:57 -07001101 .sync = true,
Mel Gorman1fb3f8c2012-10-08 16:29:12 -07001102 .page = NULL,
Rik van Riel7be62de2012-03-21 16:33:52 -07001103 };
1104
Hugh Dickins8575ec22012-03-21 16:33:53 -07001105 return __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001106}
1107
Mel Gorman76ab0f52010-05-24 14:32:28 -07001108/* Compact all nodes in the system */
1109static int compact_nodes(void)
1110{
1111 int nid;
1112
Hugh Dickins8575ec22012-03-21 16:33:53 -07001113 /* Flush pending updates to the LRU lists */
1114 lru_add_drain_all();
1115
Mel Gorman76ab0f52010-05-24 14:32:28 -07001116 for_each_online_node(nid)
1117 compact_node(nid);
1118
1119 return COMPACT_COMPLETE;
1120}
1121
1122/* The written value is actually unused, all memory is compacted */
1123int sysctl_compact_memory;
1124
1125/* This is the entry point for compacting all nodes via /proc/sys/vm */
1126int sysctl_compaction_handler(struct ctl_table *table, int write,
1127 void __user *buffer, size_t *length, loff_t *ppos)
1128{
1129 if (write)
1130 return compact_nodes();
1131
1132 return 0;
1133}
Mel Gormaned4a6d72010-05-24 14:32:29 -07001134
Mel Gorman5e771902010-05-24 14:32:31 -07001135int sysctl_extfrag_handler(struct ctl_table *table, int write,
1136 void __user *buffer, size_t *length, loff_t *ppos)
1137{
1138 proc_dointvec_minmax(table, write, buffer, length, ppos);
1139
1140 return 0;
1141}
1142
Mel Gormaned4a6d72010-05-24 14:32:29 -07001143#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Kay Sievers10fbcf42011-12-21 14:48:43 -08001144ssize_t sysfs_compact_node(struct device *dev,
1145 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -07001146 const char *buf, size_t count)
1147{
Hugh Dickins8575ec22012-03-21 16:33:53 -07001148 int nid = dev->id;
1149
1150 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1151 /* Flush pending updates to the LRU lists */
1152 lru_add_drain_all();
1153
1154 compact_node(nid);
1155 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07001156
1157 return count;
1158}
Kay Sievers10fbcf42011-12-21 14:48:43 -08001159static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001160
1161int compaction_register_node(struct node *node)
1162{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001163 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001164}
1165
1166void compaction_unregister_node(struct node *node)
1167{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001168 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001169}
1170#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001171
1172#endif /* CONFIG_COMPACTION */