blob: 9a3e351da29b797817d7311b1e70ad6618b7edbb [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>
Rafael Aquinibf6bddf2012-12-11 16:02:42 -080017#include <linux/balloon_compaction.h>
Minchan Kim194159f2013-02-22 16:33:58 -080018#include <linux/page-isolation.h>
Mel Gorman748446b2010-05-24 14:32:27 -070019#include "internal.h"
20
Minchan Kim010fc292012-12-20 15:05:06 -080021#ifdef CONFIG_COMPACTION
22static inline void count_compact_event(enum vm_event_item item)
23{
24 count_vm_event(item);
25}
26
27static inline void count_compact_events(enum vm_event_item item, long delta)
28{
29 count_vm_events(item, delta);
30}
31#else
32#define count_compact_event(item) do { } while (0)
33#define count_compact_events(item, delta) do { } while (0)
34#endif
35
Michal Nazarewiczff9543f2011-12-29 13:09:50 +010036#if defined CONFIG_COMPACTION || defined CONFIG_CMA
37
Mel Gormanb7aba692011-01-13 15:45:54 -080038#define CREATE_TRACE_POINTS
39#include <trace/events/compaction.h>
40
Mel Gorman748446b2010-05-24 14:32:27 -070041static unsigned long release_freepages(struct list_head *freelist)
42{
43 struct page *page, *next;
44 unsigned long count = 0;
45
46 list_for_each_entry_safe(page, next, freelist, lru) {
47 list_del(&page->lru);
48 __free_page(page);
49 count++;
50 }
51
52 return count;
53}
54
Michal Nazarewiczff9543f2011-12-29 13:09:50 +010055static void map_pages(struct list_head *list)
56{
57 struct page *page;
58
59 list_for_each_entry(page, list, lru) {
60 arch_alloc_page(page, 0);
61 kernel_map_pages(page, 1, 1);
62 }
63}
64
Michal Nazarewicz47118af2011-12-29 13:09:50 +010065static inline bool migrate_async_suitable(int migratetype)
66{
67 return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
68}
69
Mel Gormanbb13ffe2012-10-08 16:32:41 -070070#ifdef CONFIG_COMPACTION
71/* Returns true if the pageblock should be scanned for pages to isolate. */
72static inline bool isolation_suitable(struct compact_control *cc,
73 struct page *page)
74{
75 if (cc->ignore_skip_hint)
76 return true;
77
78 return !get_pageblock_skip(page);
79}
80
81/*
82 * This function is called to clear all cached information on pageblocks that
83 * should be skipped for page isolation when the migrate and free page scanner
84 * meet.
85 */
Mel Gorman62997022012-10-08 16:32:47 -070086static void __reset_isolation_suitable(struct zone *zone)
Mel Gormanbb13ffe2012-10-08 16:32:41 -070087{
88 unsigned long start_pfn = zone->zone_start_pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -080089 unsigned long end_pfn = zone_end_pfn(zone);
Mel Gormanbb13ffe2012-10-08 16:32:41 -070090 unsigned long pfn;
91
Mel Gormanc89511a2012-10-08 16:32:45 -070092 zone->compact_cached_migrate_pfn = start_pfn;
93 zone->compact_cached_free_pfn = end_pfn;
Mel Gorman62997022012-10-08 16:32:47 -070094 zone->compact_blockskip_flush = false;
Mel Gormanbb13ffe2012-10-08 16:32:41 -070095
96 /* Walk the zone and mark every pageblock as suitable for isolation */
97 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
98 struct page *page;
99
100 cond_resched();
101
102 if (!pfn_valid(pfn))
103 continue;
104
105 page = pfn_to_page(pfn);
106 if (zone != page_zone(page))
107 continue;
108
109 clear_pageblock_skip(page);
110 }
111}
112
Mel Gorman62997022012-10-08 16:32:47 -0700113void reset_isolation_suitable(pg_data_t *pgdat)
114{
115 int zoneid;
116
117 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
118 struct zone *zone = &pgdat->node_zones[zoneid];
119 if (!populated_zone(zone))
120 continue;
121
122 /* Only flush if a full compaction finished recently */
123 if (zone->compact_blockskip_flush)
124 __reset_isolation_suitable(zone);
125 }
126}
127
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700128/*
129 * If no pages were isolated then mark this pageblock to be skipped in the
Mel Gorman62997022012-10-08 16:32:47 -0700130 * future. The information is later cleared by __reset_isolation_suitable().
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700131 */
Mel Gormanc89511a2012-10-08 16:32:45 -0700132static void update_pageblock_skip(struct compact_control *cc,
133 struct page *page, unsigned long nr_isolated,
134 bool migrate_scanner)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700135{
Mel Gormanc89511a2012-10-08 16:32:45 -0700136 struct zone *zone = cc->zone;
Joonsoo Kim31eb5f22013-12-18 17:08:52 -0800137
138 if (cc->ignore_skip_hint)
139 return;
140
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700141 if (!page)
142 return;
143
Mel Gormanc89511a2012-10-08 16:32:45 -0700144 if (!nr_isolated) {
145 unsigned long pfn = page_to_pfn(page);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700146 set_pageblock_skip(page);
Mel Gormanc89511a2012-10-08 16:32:45 -0700147
148 /* Update where compaction should restart */
149 if (migrate_scanner) {
150 if (!cc->finished_update_migrate &&
151 pfn > zone->compact_cached_migrate_pfn)
152 zone->compact_cached_migrate_pfn = pfn;
153 } else {
154 if (!cc->finished_update_free &&
155 pfn < zone->compact_cached_free_pfn)
156 zone->compact_cached_free_pfn = pfn;
157 }
158 }
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700159}
160#else
161static inline bool isolation_suitable(struct compact_control *cc,
162 struct page *page)
163{
164 return true;
165}
166
Mel Gormanc89511a2012-10-08 16:32:45 -0700167static void update_pageblock_skip(struct compact_control *cc,
168 struct page *page, unsigned long nr_isolated,
169 bool migrate_scanner)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700170{
171}
172#endif /* CONFIG_COMPACTION */
173
Mel Gorman2a1402a2012-10-08 16:32:33 -0700174static inline bool should_release_lock(spinlock_t *lock)
175{
176 return need_resched() || spin_is_contended(lock);
177}
178
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100179/*
Mel Gormanc67fe372012-08-21 16:16:17 -0700180 * Compaction requires the taking of some coarse locks that are potentially
181 * very heavily contended. Check if the process needs to be scheduled or
182 * if the lock is contended. For async compaction, back out in the event
183 * if contention is severe. For sync compaction, schedule.
184 *
185 * Returns true if the lock is held.
186 * Returns false if the lock is released and compaction should abort
187 */
188static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags,
189 bool locked, struct compact_control *cc)
190{
Mel Gorman2a1402a2012-10-08 16:32:33 -0700191 if (should_release_lock(lock)) {
Mel Gormanc67fe372012-08-21 16:16:17 -0700192 if (locked) {
193 spin_unlock_irqrestore(lock, *flags);
194 locked = false;
195 }
196
197 /* async aborts if taking too long or contended */
198 if (!cc->sync) {
Shaohua Lie64c5232012-10-08 16:32:27 -0700199 cc->contended = true;
Mel Gormanc67fe372012-08-21 16:16:17 -0700200 return false;
201 }
202
203 cond_resched();
Mel Gormanc67fe372012-08-21 16:16:17 -0700204 }
205
206 if (!locked)
207 spin_lock_irqsave(lock, *flags);
208 return true;
209}
210
211static inline bool compact_trylock_irqsave(spinlock_t *lock,
212 unsigned long *flags, struct compact_control *cc)
213{
214 return compact_checklock_irqsave(lock, flags, false, cc);
215}
216
Mel Gormanf40d1e42012-10-08 16:32:36 -0700217/* Returns true if the page is within a block suitable for migration to */
218static bool suitable_migration_target(struct page *page)
219{
220 int migratetype = get_pageblock_migratetype(page);
221
222 /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
Minchan Kim194159f2013-02-22 16:33:58 -0800223 if (migratetype == MIGRATE_RESERVE)
224 return false;
225
226 if (is_migrate_isolate(migratetype))
Mel Gormanf40d1e42012-10-08 16:32:36 -0700227 return false;
228
229 /* If the page is a large free page, then allow migration */
230 if (PageBuddy(page) && page_order(page) >= pageblock_order)
231 return true;
232
233 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
234 if (migrate_async_suitable(migratetype))
235 return true;
236
237 /* Otherwise skip the block */
238 return false;
239}
240
Mel Gormanc67fe372012-08-21 16:16:17 -0700241/*
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100242 * Isolate free pages onto a private freelist. Caller must hold zone->lock.
243 * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
244 * pages inside of the pageblock (even though it may still end up isolating
245 * some pages).
246 */
Mel Gormanf40d1e42012-10-08 16:32:36 -0700247static unsigned long isolate_freepages_block(struct compact_control *cc,
248 unsigned long blockpfn,
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100249 unsigned long end_pfn,
250 struct list_head *freelist,
251 bool strict)
Mel Gorman748446b2010-05-24 14:32:27 -0700252{
Mel Gormanb7aba692011-01-13 15:45:54 -0800253 int nr_scanned = 0, total_isolated = 0;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700254 struct page *cursor, *valid_page = NULL;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700255 unsigned long nr_strict_required = end_pfn - blockpfn;
256 unsigned long flags;
257 bool locked = false;
Mel Gorman748446b2010-05-24 14:32:27 -0700258
Mel Gorman748446b2010-05-24 14:32:27 -0700259 cursor = pfn_to_page(blockpfn);
260
Mel Gormanf40d1e42012-10-08 16:32:36 -0700261 /* Isolate free pages. */
Mel Gorman748446b2010-05-24 14:32:27 -0700262 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
263 int isolated, i;
264 struct page *page = cursor;
265
Mel Gormanb7aba692011-01-13 15:45:54 -0800266 nr_scanned++;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700267 if (!pfn_valid_within(blockpfn))
Mel Gorman748446b2010-05-24 14:32:27 -0700268 continue;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700269 if (!valid_page)
270 valid_page = page;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700271 if (!PageBuddy(page))
272 continue;
273
274 /*
275 * The zone lock must be held to isolate freepages.
276 * Unfortunately this is a very coarse lock and can be
277 * heavily contended if there are parallel allocations
278 * or parallel compactions. For async compaction do not
279 * spin on the lock and we acquire the lock as late as
280 * possible.
281 */
282 locked = compact_checklock_irqsave(&cc->zone->lock, &flags,
283 locked, cc);
284 if (!locked)
285 break;
286
287 /* Recheck this is a suitable migration target under lock */
288 if (!strict && !suitable_migration_target(page))
289 break;
290
291 /* Recheck this is a buddy page under lock */
292 if (!PageBuddy(page))
293 continue;
Mel Gorman748446b2010-05-24 14:32:27 -0700294
295 /* Found a free page, break it into order-0 pages */
296 isolated = split_free_page(page);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100297 if (!isolated && strict)
Mel Gormanf40d1e42012-10-08 16:32:36 -0700298 break;
Mel Gorman748446b2010-05-24 14:32:27 -0700299 total_isolated += isolated;
300 for (i = 0; i < isolated; i++) {
301 list_add(&page->lru, freelist);
302 page++;
303 }
304
305 /* If a page was split, advance to the end of it */
306 if (isolated) {
307 blockpfn += isolated - 1;
308 cursor += isolated - 1;
309 }
310 }
311
Mel Gormanb7aba692011-01-13 15:45:54 -0800312 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
Mel Gormanf40d1e42012-10-08 16:32:36 -0700313
314 /*
315 * If strict isolation is requested by CMA then check that all the
316 * pages requested were isolated. If there were any failures, 0 is
317 * returned and CMA will fail.
318 */
Mel Gorman0db63d72012-10-19 13:56:57 -0700319 if (strict && nr_strict_required > total_isolated)
Mel Gormanf40d1e42012-10-08 16:32:36 -0700320 total_isolated = 0;
321
322 if (locked)
323 spin_unlock_irqrestore(&cc->zone->lock, flags);
324
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700325 /* Update the pageblock-skip if the whole pageblock was scanned */
326 if (blockpfn == end_pfn)
Mel Gormanc89511a2012-10-08 16:32:45 -0700327 update_pageblock_skip(cc, valid_page, total_isolated, false);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700328
Minchan Kim010fc292012-12-20 15:05:06 -0800329 count_compact_events(COMPACTFREE_SCANNED, nr_scanned);
Mel Gorman397487d2012-10-19 12:00:10 +0100330 if (total_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -0800331 count_compact_events(COMPACTISOLATED, total_isolated);
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.
Minchan Kime46a2872012-10-08 16:33:48 -0700437 * @unevictable: true if it allows to isolate unevictable pages
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100438 *
439 * Isolate all pages that can be migrated from the range specified by
440 * [low_pfn, end_pfn). Returns zero if there is a fatal signal
441 * pending), otherwise PFN of the first page that was not scanned
442 * (which may be both less, equal to or more then end_pfn).
443 *
444 * Assumes that cc->migratepages is empty and cc->nr_migratepages is
445 * zero.
446 *
447 * Apart from cc->migratepages and cc->nr_migratetypes this function
448 * does not modify any cc's fields, in particular it does not modify
449 * (or read for that matter) cc->migrate_pfn.
Mel Gorman748446b2010-05-24 14:32:27 -0700450 */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100451unsigned long
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100452isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
Minchan Kime46a2872012-10-08 16:33:48 -0700453 unsigned long low_pfn, unsigned long end_pfn, bool unevictable)
Mel Gorman748446b2010-05-24 14:32:27 -0700454{
Mel Gorman9927af742011-01-13 15:45:59 -0800455 unsigned long last_pageblock_nr = 0, pageblock_nr;
Mel Gormanb7aba692011-01-13 15:45:54 -0800456 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700457 struct list_head *migratelist = &cc->migratepages;
Konstantin Khlebnikovf3fd4a62012-05-29 15:06:54 -0700458 isolate_mode_t mode = 0;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700459 struct lruvec *lruvec;
Mel Gormanc67fe372012-08-21 16:16:17 -0700460 unsigned long flags;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700461 bool locked = false;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700462 struct page *page = NULL, *valid_page = NULL;
Mel Gorman748446b2010-05-24 14:32:27 -0700463
Mel Gorman748446b2010-05-24 14:32:27 -0700464 /*
465 * Ensure that there are not too many pages isolated from the LRU
466 * list by either parallel reclaimers or compaction. If there are,
467 * delay for some time until fewer pages are isolated
468 */
469 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700470 /* async migration should just abort */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700471 if (!cc->sync)
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100472 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700473
Mel Gorman748446b2010-05-24 14:32:27 -0700474 congestion_wait(BLK_RW_ASYNC, HZ/10);
475
476 if (fatal_signal_pending(current))
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100477 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700478 }
479
480 /* Time to isolate some pages for migration */
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700481 cond_resched();
Mel Gorman748446b2010-05-24 14:32:27 -0700482 for (; low_pfn < end_pfn; low_pfn++) {
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700483 /* give a chance to irqs before checking need_resched() */
Mel Gorman2a1402a2012-10-08 16:32:33 -0700484 if (locked && !((low_pfn+1) % SWAP_CLUSTER_MAX)) {
485 if (should_release_lock(&zone->lru_lock)) {
486 spin_unlock_irqrestore(&zone->lru_lock, flags);
487 locked = false;
488 }
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700489 }
Mel Gormanc67fe372012-08-21 16:16:17 -0700490
Mel Gorman0bf380b2012-02-03 15:37:18 -0800491 /*
492 * migrate_pfn does not necessarily start aligned to a
493 * pageblock. Ensure that pfn_valid is called when moving
494 * into a new MAX_ORDER_NR_PAGES range in case of large
495 * memory holes within the zone
496 */
497 if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
498 if (!pfn_valid(low_pfn)) {
499 low_pfn += MAX_ORDER_NR_PAGES - 1;
500 continue;
501 }
502 }
503
Mel Gorman748446b2010-05-24 14:32:27 -0700504 if (!pfn_valid_within(low_pfn))
505 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800506 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700507
Mel Gormandc908602012-02-08 17:13:38 -0800508 /*
509 * Get the page and ensure the page is within the same zone.
510 * See the comment in isolate_freepages about overlapping
511 * nodes. It is deliberate that the new zone lock is not taken
512 * as memory compaction should not move pages between nodes.
513 */
Mel Gorman748446b2010-05-24 14:32:27 -0700514 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800515 if (page_zone(page) != zone)
516 continue;
517
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700518 if (!valid_page)
519 valid_page = page;
520
521 /* If isolation recently failed, do not retry */
522 pageblock_nr = low_pfn >> pageblock_order;
523 if (!isolation_suitable(cc, page))
524 goto next_pageblock;
525
Mel Gormandc908602012-02-08 17:13:38 -0800526 /* Skip if free */
Mel Gorman748446b2010-05-24 14:32:27 -0700527 if (PageBuddy(page))
528 continue;
529
Mel Gorman9927af742011-01-13 15:45:59 -0800530 /*
531 * For async migration, also only scan in MOVABLE blocks. Async
532 * migration is optimistic to see if the minimum amount of work
533 * satisfies the allocation
534 */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700535 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
Michal Nazarewicz47118af2011-12-29 13:09:50 +0100536 !migrate_async_suitable(get_pageblock_migratetype(page))) {
Mel Gormanc89511a2012-10-08 16:32:45 -0700537 cc->finished_update_migrate = true;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700538 goto next_pageblock;
Mel Gorman9927af742011-01-13 15:45:59 -0800539 }
540
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800541 /*
542 * Check may be lockless but that's ok as we recheck later.
543 * It's possible to migrate LRU pages and balloon pages
544 * Skip any other type of page
545 */
546 if (!PageLRU(page)) {
547 if (unlikely(balloon_page_movable(page))) {
548 if (locked && balloon_page_isolate(page)) {
549 /* Successfully isolated */
550 cc->finished_update_migrate = true;
551 list_add(&page->lru, migratelist);
552 cc->nr_migratepages++;
553 nr_isolated++;
554 goto check_compact_cluster;
555 }
556 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800557 continue;
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800558 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800559
560 /*
Mel Gorman2a1402a2012-10-08 16:32:33 -0700561 * PageLRU is set. lru_lock normally excludes isolation
562 * splitting and collapsing (collapsing has already happened
563 * if PageLRU is set) but the lock is not necessarily taken
564 * here and it is wasteful to take it just to check transhuge.
565 * Check TransHuge without lock and skip the whole pageblock if
566 * it's either a transhuge or hugetlbfs page, as calling
567 * compound_order() without preventing THP from splitting the
568 * page underneath us may return surprising results.
Andrea Arcangelibc835012011-01-13 15:47:08 -0800569 */
570 if (PageTransHuge(page)) {
Mel Gorman2a1402a2012-10-08 16:32:33 -0700571 if (!locked)
572 goto next_pageblock;
573 low_pfn += (1 << compound_order(page)) - 1;
574 continue;
575 }
576
577 /* Check if it is ok to still hold the lock */
578 locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
579 locked, cc);
580 if (!locked || fatal_signal_pending(current))
581 break;
582
583 /* Recheck PageLRU and PageTransHuge under lock */
584 if (!PageLRU(page))
585 continue;
586 if (PageTransHuge(page)) {
Andrea Arcangelibc835012011-01-13 15:47:08 -0800587 low_pfn += (1 << compound_order(page)) - 1;
588 continue;
589 }
590
Linus Torvalds68e3e922012-06-03 20:05:57 -0700591 if (!cc->sync)
Mel Gormanc8244932012-01-12 17:19:38 -0800592 mode |= ISOLATE_ASYNC_MIGRATE;
593
Minchan Kime46a2872012-10-08 16:33:48 -0700594 if (unevictable)
595 mode |= ISOLATE_UNEVICTABLE;
596
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700597 lruvec = mem_cgroup_page_lruvec(page, zone);
598
Mel Gorman748446b2010-05-24 14:32:27 -0700599 /* Try isolate the page */
Konstantin Khlebnikovf3fd4a62012-05-29 15:06:54 -0700600 if (__isolate_lru_page(page, mode) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700601 continue;
602
Andrea Arcangelibc835012011-01-13 15:47:08 -0800603 VM_BUG_ON(PageTransCompound(page));
604
Mel Gorman748446b2010-05-24 14:32:27 -0700605 /* Successfully isolated */
Mel Gormanc89511a2012-10-08 16:32:45 -0700606 cc->finished_update_migrate = true;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700607 del_page_from_lru_list(page, lruvec, page_lru(page));
Mel Gorman748446b2010-05-24 14:32:27 -0700608 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700609 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800610 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700611
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800612check_compact_cluster:
Mel Gorman748446b2010-05-24 14:32:27 -0700613 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800614 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
615 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700616 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800617 }
Mel Gorman2a1402a2012-10-08 16:32:33 -0700618
619 continue;
620
621next_pageblock:
Mel Gormana9aacbc2013-02-22 16:32:25 -0800622 low_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages) - 1;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700623 last_pageblock_nr = pageblock_nr;
Mel Gorman748446b2010-05-24 14:32:27 -0700624 }
625
Mel Gormanc67fe372012-08-21 16:16:17 -0700626 acct_isolated(zone, locked, cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700627
Mel Gormanc67fe372012-08-21 16:16:17 -0700628 if (locked)
629 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700630
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700631 /* Update the pageblock-skip if the whole pageblock was scanned */
632 if (low_pfn == end_pfn)
Mel Gormanc89511a2012-10-08 16:32:45 -0700633 update_pageblock_skip(cc, valid_page, nr_isolated, true);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700634
Mel Gormanb7aba692011-01-13 15:45:54 -0800635 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
636
Minchan Kim010fc292012-12-20 15:05:06 -0800637 count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned);
Mel Gorman397487d2012-10-19 12:00:10 +0100638 if (nr_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -0800639 count_compact_events(COMPACTISOLATED, nr_isolated);
Mel Gorman397487d2012-10-19 12:00:10 +0100640
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100641 return low_pfn;
642}
643
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100644#endif /* CONFIG_COMPACTION || CONFIG_CMA */
645#ifdef CONFIG_COMPACTION
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100646/*
647 * Based on information in the current compact_control, find blocks
648 * suitable for isolating free pages from and then isolate them.
649 */
650static void isolate_freepages(struct zone *zone,
651 struct compact_control *cc)
652{
653 struct page *page;
Cody P Schafer108bcc92013-02-22 16:35:23 -0800654 unsigned long high_pfn, low_pfn, pfn, z_end_pfn, end_pfn;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100655 int nr_freepages = cc->nr_freepages;
656 struct list_head *freelist = &cc->freepages;
657
658 /*
659 * Initialise the free scanner. The starting point is where we last
660 * scanned from (or the end of the zone if starting). The low point
661 * is the end of the pageblock the migration scanner is using.
662 */
663 pfn = cc->free_pfn;
664 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
665
666 /*
667 * Take care that if the migration scanner is at the end of the zone
668 * that the free scanner does not accidentally move to the next zone
669 * in the next isolation cycle.
670 */
671 high_pfn = min(low_pfn, pfn);
672
Cody P Schafer108bcc92013-02-22 16:35:23 -0800673 z_end_pfn = zone_end_pfn(zone);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100674
675 /*
676 * Isolate free pages until enough are available to migrate the
677 * pages on cc->migratepages. We stop searching if the migrate
678 * and free page scanners meet or enough free pages are isolated.
679 */
680 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
681 pfn -= pageblock_nr_pages) {
682 unsigned long isolated;
683
684 if (!pfn_valid(pfn))
685 continue;
686
687 /*
688 * Check for overlapping nodes/zones. It's possible on some
689 * configurations to have a setup like
690 * node0 node1 node0
691 * i.e. it's possible that all pages within a zones range of
692 * pages do not belong to a single zone.
693 */
694 page = pfn_to_page(pfn);
695 if (page_zone(page) != zone)
696 continue;
697
698 /* Check the block is suitable for migration */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700699 if (!suitable_migration_target(page))
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100700 continue;
Linus Torvalds68e3e922012-06-03 20:05:57 -0700701
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700702 /* If isolation recently failed, do not retry */
703 if (!isolation_suitable(cc, page))
704 continue;
705
Mel Gormanf40d1e42012-10-08 16:32:36 -0700706 /* Found a block suitable for isolating free pages from */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100707 isolated = 0;
Mel Gorman60177d32012-12-06 19:01:14 +0000708
709 /*
710 * As pfn may not start aligned, pfn+pageblock_nr_page
711 * may cross a MAX_ORDER_NR_PAGES boundary and miss
712 * a pfn_valid check. Ensure isolate_freepages_block()
713 * only scans within a pageblock
714 */
715 end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
Cody P Schafer108bcc92013-02-22 16:35:23 -0800716 end_pfn = min(end_pfn, z_end_pfn);
Mel Gormanf40d1e42012-10-08 16:32:36 -0700717 isolated = isolate_freepages_block(cc, pfn, end_pfn,
718 freelist, false);
719 nr_freepages += isolated;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100720
721 /*
722 * Record the highest PFN we isolated pages from. When next
723 * looking for free pages, the search will restart here as
724 * page migration may have returned some pages to the allocator
725 */
Mel Gormanc89511a2012-10-08 16:32:45 -0700726 if (isolated) {
727 cc->finished_update_free = true;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100728 high_pfn = max(high_pfn, pfn);
Mel Gormanc89511a2012-10-08 16:32:45 -0700729 }
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100730 }
731
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100732 /* split_free_page does not map the pages */
733 map_pages(freelist);
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100734
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100735 cc->free_pfn = high_pfn;
736 cc->nr_freepages = nr_freepages;
Mel Gorman748446b2010-05-24 14:32:27 -0700737}
738
739/*
740 * This is a migrate-callback that "allocates" freepages by taking pages
741 * from the isolated freelists in the block we are migrating to.
742 */
743static struct page *compaction_alloc(struct page *migratepage,
744 unsigned long data,
745 int **result)
746{
747 struct compact_control *cc = (struct compact_control *)data;
748 struct page *freepage;
749
750 /* Isolate free pages if necessary */
751 if (list_empty(&cc->freepages)) {
752 isolate_freepages(cc->zone, cc);
753
754 if (list_empty(&cc->freepages))
755 return NULL;
756 }
757
758 freepage = list_entry(cc->freepages.next, struct page, lru);
759 list_del(&freepage->lru);
760 cc->nr_freepages--;
761
762 return freepage;
763}
764
765/*
766 * We cannot control nr_migratepages and nr_freepages fully when migration is
767 * running as migrate_pages() has no knowledge of compact_control. When
768 * migration is complete, we count the number of pages on the lists by hand.
769 */
770static void update_nr_listpages(struct compact_control *cc)
771{
772 int nr_migratepages = 0;
773 int nr_freepages = 0;
774 struct page *page;
775
776 list_for_each_entry(page, &cc->migratepages, lru)
777 nr_migratepages++;
778 list_for_each_entry(page, &cc->freepages, lru)
779 nr_freepages++;
780
781 cc->nr_migratepages = nr_migratepages;
782 cc->nr_freepages = nr_freepages;
783}
784
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100785/* possible outcome of isolate_migratepages */
786typedef enum {
787 ISOLATE_ABORT, /* Abort compaction now */
788 ISOLATE_NONE, /* No pages isolated, continue scanning */
789 ISOLATE_SUCCESS, /* Pages isolated, migrate */
790} isolate_migrate_t;
791
792/*
793 * Isolate all pages that can be migrated from the block pointed to by
794 * the migrate scanner within compact_control.
795 */
796static isolate_migrate_t isolate_migratepages(struct zone *zone,
797 struct compact_control *cc)
798{
799 unsigned long low_pfn, end_pfn;
800
801 /* Do not scan outside zone boundaries */
802 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
803
804 /* Only scan within a pageblock boundary */
Mel Gormana9aacbc2013-02-22 16:32:25 -0800805 end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100806
807 /* Do not cross the free scanner or scan within a memory hole */
808 if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
809 cc->migrate_pfn = end_pfn;
810 return ISOLATE_NONE;
811 }
812
813 /* Perform the isolation */
Minchan Kime46a2872012-10-08 16:33:48 -0700814 low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn, false);
Shaohua Lie64c5232012-10-08 16:32:27 -0700815 if (!low_pfn || cc->contended)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100816 return ISOLATE_ABORT;
817
818 cc->migrate_pfn = low_pfn;
819
820 return ISOLATE_SUCCESS;
821}
822
Mel Gorman748446b2010-05-24 14:32:27 -0700823static int compact_finished(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800824 struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700825{
Mel Gorman8fb74b92013-01-11 14:32:16 -0800826 unsigned int order;
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800827 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -0700828
Mel Gorman748446b2010-05-24 14:32:27 -0700829 if (fatal_signal_pending(current))
830 return COMPACT_PARTIAL;
831
Mel Gorman753341a2012-10-08 16:32:40 -0700832 /* Compaction run completes if the migrate and free scanner meet */
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700833 if (cc->free_pfn <= cc->migrate_pfn) {
Mel Gorman62997022012-10-08 16:32:47 -0700834 /*
835 * Mark that the PG_migrate_skip information should be cleared
836 * by kswapd when it goes to sleep. kswapd does not set the
837 * flag itself as the decision to be clear should be directly
838 * based on an allocation request.
839 */
840 if (!current_is_kswapd())
841 zone->compact_blockskip_flush = true;
842
Mel Gorman748446b2010-05-24 14:32:27 -0700843 return COMPACT_COMPLETE;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700844 }
Mel Gorman748446b2010-05-24 14:32:27 -0700845
Johannes Weiner82478fb2011-01-20 14:44:21 -0800846 /*
847 * order == -1 is expected when compacting via
848 * /proc/sys/vm/compact_memory
849 */
Mel Gorman56de7262010-05-24 14:32:30 -0700850 if (cc->order == -1)
851 return COMPACT_CONTINUE;
852
Michal Hocko3957c772011-06-15 15:08:25 -0700853 /* Compaction run is not finished if the watermark is not met */
854 watermark = low_wmark_pages(zone);
855 watermark += (1 << cc->order);
856
857 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
858 return COMPACT_CONTINUE;
859
Mel Gorman56de7262010-05-24 14:32:30 -0700860 /* Direct compactor: Is a suitable page free? */
Mel Gorman8fb74b92013-01-11 14:32:16 -0800861 for (order = cc->order; order < MAX_ORDER; order++) {
862 struct free_area *area = &zone->free_area[order];
Mel Gorman56de7262010-05-24 14:32:30 -0700863
Mel Gorman8fb74b92013-01-11 14:32:16 -0800864 /* Job done if page is free of the right migratetype */
865 if (!list_empty(&area->free_list[cc->migratetype]))
866 return COMPACT_PARTIAL;
867
868 /* Job done if allocation would set block type */
869 if (cc->order >= pageblock_order && area->nr_free)
870 return COMPACT_PARTIAL;
Mel Gorman56de7262010-05-24 14:32:30 -0700871 }
872
Mel Gorman748446b2010-05-24 14:32:27 -0700873 return COMPACT_CONTINUE;
874}
875
Mel Gorman3e7d3442011-01-13 15:45:56 -0800876/*
877 * compaction_suitable: Is this suitable to run compaction on this zone now?
878 * Returns
879 * COMPACT_SKIPPED - If there are too few free pages for compaction
880 * COMPACT_PARTIAL - If the allocation would succeed without compaction
881 * COMPACT_CONTINUE - If compaction should run now
882 */
883unsigned long compaction_suitable(struct zone *zone, int order)
884{
885 int fragindex;
886 unsigned long watermark;
887
888 /*
Michal Hocko3957c772011-06-15 15:08:25 -0700889 * order == -1 is expected when compacting via
890 * /proc/sys/vm/compact_memory
891 */
892 if (order == -1)
893 return COMPACT_CONTINUE;
894
895 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -0800896 * Watermarks for order-0 must be met for compaction. Note the 2UL.
897 * This is because during migration, copies of pages need to be
898 * allocated and for a short time, the footprint is higher
899 */
900 watermark = low_wmark_pages(zone) + (2UL << order);
901 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
902 return COMPACT_SKIPPED;
903
904 /*
905 * fragmentation index determines if allocation failures are due to
906 * low memory or external fragmentation
907 *
Shaohua Lia582a732011-06-15 15:08:49 -0700908 * index of -1000 implies allocations might succeed depending on
909 * watermarks
Mel Gorman3e7d3442011-01-13 15:45:56 -0800910 * index towards 0 implies failure is due to lack of memory
911 * index towards 1000 implies failure is due to fragmentation
912 *
913 * Only compact if a failure would be due to fragmentation.
914 */
915 fragindex = fragmentation_index(zone, order);
916 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
917 return COMPACT_SKIPPED;
918
Shaohua Lia582a732011-06-15 15:08:49 -0700919 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
920 0, 0))
Mel Gorman3e7d3442011-01-13 15:45:56 -0800921 return COMPACT_PARTIAL;
922
923 return COMPACT_CONTINUE;
924}
925
Mel Gorman748446b2010-05-24 14:32:27 -0700926static int compact_zone(struct zone *zone, struct compact_control *cc)
927{
928 int ret;
Mel Gormanc89511a2012-10-08 16:32:45 -0700929 unsigned long start_pfn = zone->zone_start_pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -0800930 unsigned long end_pfn = zone_end_pfn(zone);
Mel Gorman748446b2010-05-24 14:32:27 -0700931
Mel Gorman3e7d3442011-01-13 15:45:56 -0800932 ret = compaction_suitable(zone, cc->order);
933 switch (ret) {
934 case COMPACT_PARTIAL:
935 case COMPACT_SKIPPED:
936 /* Compaction is likely to fail */
937 return ret;
938 case COMPACT_CONTINUE:
939 /* Fall through to compaction */
940 ;
941 }
942
Mel Gormanc89511a2012-10-08 16:32:45 -0700943 /*
944 * Setup to move all movable pages to the end of the zone. Used cached
945 * information on where the scanners should start but check that it
946 * is initialised by ensuring the values are within zone boundaries.
947 */
948 cc->migrate_pfn = zone->compact_cached_migrate_pfn;
949 cc->free_pfn = zone->compact_cached_free_pfn;
950 if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
951 cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
952 zone->compact_cached_free_pfn = cc->free_pfn;
953 }
954 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
955 cc->migrate_pfn = start_pfn;
956 zone->compact_cached_migrate_pfn = cc->migrate_pfn;
957 }
Mel Gorman748446b2010-05-24 14:32:27 -0700958
Mel Gorman62997022012-10-08 16:32:47 -0700959 /*
960 * Clear pageblock skip if there were failures recently and compaction
961 * is about to be retried after being deferred. kswapd does not do
962 * this reset as it'll reset the cached information when going to sleep.
963 */
964 if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
965 __reset_isolation_suitable(zone);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700966
Mel Gorman748446b2010-05-24 14:32:27 -0700967 migrate_prep_local();
968
969 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
970 unsigned long nr_migrate, nr_remaining;
Minchan Kim9d502c12011-03-22 16:30:39 -0700971 int err;
Mel Gorman748446b2010-05-24 14:32:27 -0700972
Mel Gormanf9e35b32011-06-15 15:08:52 -0700973 switch (isolate_migratepages(zone, cc)) {
974 case ISOLATE_ABORT:
975 ret = COMPACT_PARTIAL;
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800976 putback_movable_pages(&cc->migratepages);
Shaohua Lie64c5232012-10-08 16:32:27 -0700977 cc->nr_migratepages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700978 goto out;
979 case ISOLATE_NONE:
Mel Gorman748446b2010-05-24 14:32:27 -0700980 continue;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700981 case ISOLATE_SUCCESS:
982 ;
983 }
Mel Gorman748446b2010-05-24 14:32:27 -0700984
985 nr_migrate = cc->nr_migratepages;
Minchan Kim9d502c12011-03-22 16:30:39 -0700986 err = migrate_pages(&cc->migratepages, compaction_alloc,
Hugh Dickins9c620e22013-02-22 16:35:14 -0800987 (unsigned long)cc,
Mel Gorman7b2a2d42012-10-19 14:07:31 +0100988 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC,
989 MR_COMPACTION);
Mel Gorman748446b2010-05-24 14:32:27 -0700990 update_nr_listpages(cc);
991 nr_remaining = cc->nr_migratepages;
992
Mel Gormanb7aba692011-01-13 15:45:54 -0800993 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
994 nr_remaining);
Mel Gorman748446b2010-05-24 14:32:27 -0700995
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800996 /* Release isolated pages not migrated */
Minchan Kim9d502c12011-03-22 16:30:39 -0700997 if (err) {
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800998 putback_movable_pages(&cc->migratepages);
Mel Gorman748446b2010-05-24 14:32:27 -0700999 cc->nr_migratepages = 0;
David Rientjes4bf2bba2012-07-11 14:02:13 -07001000 if (err == -ENOMEM) {
1001 ret = COMPACT_PARTIAL;
1002 goto out;
1003 }
Mel Gorman748446b2010-05-24 14:32:27 -07001004 }
Mel Gorman748446b2010-05-24 14:32:27 -07001005 }
1006
Mel Gormanf9e35b32011-06-15 15:08:52 -07001007out:
Mel Gorman748446b2010-05-24 14:32:27 -07001008 /* Release free pages and check accounting */
1009 cc->nr_freepages -= release_freepages(&cc->freepages);
1010 VM_BUG_ON(cc->nr_freepages != 0);
1011
1012 return ret;
1013}
Mel Gorman76ab0f52010-05-24 14:32:28 -07001014
Kyungmin Parkd43a87e2011-10-31 17:09:08 -07001015static unsigned long compact_zone_order(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -08001016 int order, gfp_t gfp_mask,
Mel Gorman8fb74b92013-01-11 14:32:16 -08001017 bool sync, bool *contended)
Mel Gorman56de7262010-05-24 14:32:30 -07001018{
Shaohua Lie64c5232012-10-08 16:32:27 -07001019 unsigned long ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001020 struct compact_control cc = {
1021 .nr_freepages = 0,
1022 .nr_migratepages = 0,
1023 .order = order,
1024 .migratetype = allocflags_to_migratetype(gfp_mask),
1025 .zone = zone,
Linus Torvalds68e3e922012-06-03 20:05:57 -07001026 .sync = sync,
Mel Gorman56de7262010-05-24 14:32:30 -07001027 };
1028 INIT_LIST_HEAD(&cc.freepages);
1029 INIT_LIST_HEAD(&cc.migratepages);
1030
Shaohua Lie64c5232012-10-08 16:32:27 -07001031 ret = compact_zone(zone, &cc);
1032
1033 VM_BUG_ON(!list_empty(&cc.freepages));
1034 VM_BUG_ON(!list_empty(&cc.migratepages));
1035
1036 *contended = cc.contended;
1037 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001038}
1039
Mel Gorman5e771902010-05-24 14:32:31 -07001040int sysctl_extfrag_threshold = 500;
1041
Mel Gorman56de7262010-05-24 14:32:30 -07001042/**
1043 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
1044 * @zonelist: The zonelist used for the current allocation
1045 * @order: The order of the current allocation
1046 * @gfp_mask: The GFP mask of the current allocation
1047 * @nodemask: The allowed nodes to allocate from
Mel Gorman77f1fe62011-01-13 15:45:57 -08001048 * @sync: Whether migration is synchronous or not
Mel Gorman661c4cb2012-10-08 16:32:31 -07001049 * @contended: Return value that is true if compaction was aborted due to lock contention
1050 * @page: Optionally capture a free page of the requested order during compaction
Mel Gorman56de7262010-05-24 14:32:30 -07001051 *
1052 * This is the main entry point for direct page compaction.
1053 */
1054unsigned long try_to_compact_pages(struct zonelist *zonelist,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001055 int order, gfp_t gfp_mask, nodemask_t *nodemask,
Mel Gorman8fb74b92013-01-11 14:32:16 -08001056 bool sync, bool *contended)
Mel Gorman56de7262010-05-24 14:32:30 -07001057{
1058 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
1059 int may_enter_fs = gfp_mask & __GFP_FS;
1060 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -07001061 struct zoneref *z;
1062 struct zone *zone;
1063 int rc = COMPACT_SKIPPED;
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001064 int alloc_flags = 0;
Mel Gorman56de7262010-05-24 14:32:30 -07001065
Mel Gorman4ffb6332012-10-08 16:29:09 -07001066 /* Check if the GFP flags allow compaction */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -08001067 if (!order || !may_enter_fs || !may_perform_io)
Mel Gorman56de7262010-05-24 14:32:30 -07001068 return rc;
1069
Minchan Kim010fc292012-12-20 15:05:06 -08001070 count_compact_event(COMPACTSTALL);
Mel Gorman56de7262010-05-24 14:32:30 -07001071
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001072#ifdef CONFIG_CMA
1073 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
1074 alloc_flags |= ALLOC_CMA;
1075#endif
Mel Gorman56de7262010-05-24 14:32:30 -07001076 /* Compact each zone in the list */
1077 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
1078 nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -07001079 int status;
1080
Mel Gormanc67fe372012-08-21 16:16:17 -07001081 status = compact_zone_order(zone, order, gfp_mask, sync,
Mel Gorman8fb74b92013-01-11 14:32:16 -08001082 contended);
Mel Gorman56de7262010-05-24 14:32:30 -07001083 rc = max(status, rc);
1084
Mel Gorman3e7d3442011-01-13 15:45:56 -08001085 /* If a normal allocation would succeed, stop compacting */
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001086 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
1087 alloc_flags))
Mel Gorman56de7262010-05-24 14:32:30 -07001088 break;
1089 }
1090
1091 return rc;
1092}
1093
1094
Mel Gorman76ab0f52010-05-24 14:32:28 -07001095/* Compact all zones within a node */
Andrew Morton7103f162013-02-22 16:32:33 -08001096static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001097{
1098 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -07001099 struct zone *zone;
1100
Mel Gorman76ab0f52010-05-24 14:32:28 -07001101 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -07001102
1103 zone = &pgdat->node_zones[zoneid];
1104 if (!populated_zone(zone))
1105 continue;
1106
Rik van Riel7be62de2012-03-21 16:33:52 -07001107 cc->nr_freepages = 0;
1108 cc->nr_migratepages = 0;
1109 cc->zone = zone;
1110 INIT_LIST_HEAD(&cc->freepages);
1111 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001112
Dan Carpenteraad6ec32012-03-21 16:33:54 -07001113 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -07001114 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001115
Rik van Rielaff62242012-03-21 16:33:52 -07001116 if (cc->order > 0) {
1117 int ok = zone_watermark_ok(zone, cc->order,
1118 low_wmark_pages(zone), 0, 0);
Minchan Kimc81758f2012-08-21 16:16:03 -07001119 if (ok && cc->order >= zone->compact_order_failed)
Rik van Rielaff62242012-03-21 16:33:52 -07001120 zone->compact_order_failed = cc->order + 1;
1121 /* Currently async compaction is never deferred. */
Linus Torvalds68e3e922012-06-03 20:05:57 -07001122 else if (!ok && cc->sync)
Rik van Rielaff62242012-03-21 16:33:52 -07001123 defer_compaction(zone, cc->order);
1124 }
1125
Rik van Riel7be62de2012-03-21 16:33:52 -07001126 VM_BUG_ON(!list_empty(&cc->freepages));
1127 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -07001128 }
Mel Gorman76ab0f52010-05-24 14:32:28 -07001129}
1130
Andrew Morton7103f162013-02-22 16:32:33 -08001131void compact_pgdat(pg_data_t *pgdat, int order)
Rik van Riel7be62de2012-03-21 16:33:52 -07001132{
1133 struct compact_control cc = {
1134 .order = order,
Linus Torvalds68e3e922012-06-03 20:05:57 -07001135 .sync = false,
Rik van Riel7be62de2012-03-21 16:33:52 -07001136 };
1137
Andrew Morton7103f162013-02-22 16:32:33 -08001138 __compact_pgdat(pgdat, &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001139}
1140
Andrew Morton7103f162013-02-22 16:32:33 -08001141static void compact_node(int nid)
Rik van Riel7be62de2012-03-21 16:33:52 -07001142{
Rik van Riel7be62de2012-03-21 16:33:52 -07001143 struct compact_control cc = {
1144 .order = -1,
Linus Torvalds68e3e922012-06-03 20:05:57 -07001145 .sync = true,
Rik van Riel7be62de2012-03-21 16:33:52 -07001146 };
1147
Andrew Morton7103f162013-02-22 16:32:33 -08001148 __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001149}
1150
Mel Gorman76ab0f52010-05-24 14:32:28 -07001151/* Compact all nodes in the system */
Jason Liu7964c062013-01-11 14:31:47 -08001152static void compact_nodes(void)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001153{
1154 int nid;
1155
Hugh Dickins8575ec22012-03-21 16:33:53 -07001156 /* Flush pending updates to the LRU lists */
1157 lru_add_drain_all();
1158
Mel Gorman76ab0f52010-05-24 14:32:28 -07001159 for_each_online_node(nid)
1160 compact_node(nid);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001161}
1162
1163/* The written value is actually unused, all memory is compacted */
1164int sysctl_compact_memory;
1165
1166/* This is the entry point for compacting all nodes via /proc/sys/vm */
1167int sysctl_compaction_handler(struct ctl_table *table, int write,
1168 void __user *buffer, size_t *length, loff_t *ppos)
1169{
1170 if (write)
Jason Liu7964c062013-01-11 14:31:47 -08001171 compact_nodes();
Mel Gorman76ab0f52010-05-24 14:32:28 -07001172
1173 return 0;
1174}
Mel Gormaned4a6d72010-05-24 14:32:29 -07001175
Mel Gorman5e771902010-05-24 14:32:31 -07001176int sysctl_extfrag_handler(struct ctl_table *table, int write,
1177 void __user *buffer, size_t *length, loff_t *ppos)
1178{
1179 proc_dointvec_minmax(table, write, buffer, length, ppos);
1180
1181 return 0;
1182}
1183
Mel Gormaned4a6d72010-05-24 14:32:29 -07001184#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Kay Sievers10fbcf42011-12-21 14:48:43 -08001185ssize_t sysfs_compact_node(struct device *dev,
1186 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -07001187 const char *buf, size_t count)
1188{
Hugh Dickins8575ec22012-03-21 16:33:53 -07001189 int nid = dev->id;
1190
1191 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1192 /* Flush pending updates to the LRU lists */
1193 lru_add_drain_all();
1194
1195 compact_node(nid);
1196 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07001197
1198 return count;
1199}
Kay Sievers10fbcf42011-12-21 14:48:43 -08001200static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001201
1202int compaction_register_node(struct node *node)
1203{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001204 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001205}
1206
1207void compaction_unregister_node(struct node *node)
1208{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001209 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001210}
1211#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001212
1213#endif /* CONFIG_COMPACTION */