blob: 81c89c39dcf730c81cb7d23a9738eaed8b2a564c [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>
Andrey Ryabinin93f051b2015-02-13 14:39:28 -080019#include <linux/kasan.h>
Mel Gorman748446b2010-05-24 14:32:27 -070020#include "internal.h"
21
Minchan Kim010fc292012-12-20 15:05:06 -080022#ifdef CONFIG_COMPACTION
23static inline void count_compact_event(enum vm_event_item item)
24{
25 count_vm_event(item);
26}
27
28static inline void count_compact_events(enum vm_event_item item, long delta)
29{
30 count_vm_events(item, delta);
31}
32#else
33#define count_compact_event(item) do { } while (0)
34#define count_compact_events(item, delta) do { } while (0)
35#endif
36
Michal Nazarewiczff9543f2011-12-29 13:09:50 +010037#if defined CONFIG_COMPACTION || defined CONFIG_CMA
38
Mel Gormanb7aba692011-01-13 15:45:54 -080039#define CREATE_TRACE_POINTS
40#include <trace/events/compaction.h>
41
Mel Gorman748446b2010-05-24 14:32:27 -070042static unsigned long release_freepages(struct list_head *freelist)
43{
44 struct page *page, *next;
45 unsigned long count = 0;
46
47 list_for_each_entry_safe(page, next, freelist, lru) {
48 list_del(&page->lru);
49 __free_page(page);
50 count++;
51 }
52
53 return count;
54}
55
Michal Nazarewiczff9543f2011-12-29 13:09:50 +010056static void map_pages(struct list_head *list)
57{
58 struct page *page;
59
60 list_for_each_entry(page, list, lru) {
61 arch_alloc_page(page, 0);
62 kernel_map_pages(page, 1, 1);
Andrey Ryabinin93f051b2015-02-13 14:39:28 -080063 kasan_alloc_pages(page, 0);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +010064 }
65}
66
Michal Nazarewicz47118af2011-12-29 13:09:50 +010067static inline bool migrate_async_suitable(int migratetype)
68{
69 return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
70}
71
Vlastimil Babka7d49d882014-10-09 15:27:11 -070072/*
73 * Check that the whole (or subset of) a pageblock given by the interval of
74 * [start_pfn, end_pfn) is valid and within the same zone, before scanning it
75 * with the migration of free compaction scanner. The scanners then need to
76 * use only pfn_valid_within() check for arches that allow holes within
77 * pageblocks.
78 *
79 * Return struct page pointer of start_pfn, or NULL if checks were not passed.
80 *
81 * It's possible on some configurations to have a setup like node0 node1 node0
82 * i.e. it's possible that all pages within a zones range of pages do not
83 * belong to a single zone. We assume that a border between node0 and node1
84 * can occur within a single pageblock, but not a node0 node1 node0
85 * interleaving within a single pageblock. It is therefore sufficient to check
86 * the first and last page of a pageblock and avoid checking each individual
87 * page in a pageblock.
88 */
89static struct page *pageblock_pfn_to_page(unsigned long start_pfn,
90 unsigned long end_pfn, struct zone *zone)
91{
92 struct page *start_page;
93 struct page *end_page;
94
95 /* end_pfn is one past the range we are checking */
96 end_pfn--;
97
98 if (!pfn_valid(start_pfn) || !pfn_valid(end_pfn))
99 return NULL;
100
101 start_page = pfn_to_page(start_pfn);
102
103 if (page_zone(start_page) != zone)
104 return NULL;
105
106 end_page = pfn_to_page(end_pfn);
107
108 /* This gives a shorter code than deriving page_zone(end_page) */
109 if (page_zone_id(start_page) != page_zone_id(end_page))
110 return NULL;
111
112 return start_page;
113}
114
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700115#ifdef CONFIG_COMPACTION
116/* Returns true if the pageblock should be scanned for pages to isolate. */
117static inline bool isolation_suitable(struct compact_control *cc,
118 struct page *page)
119{
120 if (cc->ignore_skip_hint)
121 return true;
122
123 return !get_pageblock_skip(page);
124}
125
126/*
127 * This function is called to clear all cached information on pageblocks that
128 * should be skipped for page isolation when the migrate and free page scanner
129 * meet.
130 */
Mel Gorman62997022012-10-08 16:32:47 -0700131static void __reset_isolation_suitable(struct zone *zone)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700132{
133 unsigned long start_pfn = zone->zone_start_pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -0800134 unsigned long end_pfn = zone_end_pfn(zone);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700135 unsigned long pfn;
136
David Rientjes35979ef2014-06-04 16:08:27 -0700137 zone->compact_cached_migrate_pfn[0] = start_pfn;
138 zone->compact_cached_migrate_pfn[1] = start_pfn;
Mel Gormanc89511a2012-10-08 16:32:45 -0700139 zone->compact_cached_free_pfn = end_pfn;
Mel Gorman62997022012-10-08 16:32:47 -0700140 zone->compact_blockskip_flush = false;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700141
142 /* Walk the zone and mark every pageblock as suitable for isolation */
143 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
144 struct page *page;
145
146 cond_resched();
147
148 if (!pfn_valid(pfn))
149 continue;
150
151 page = pfn_to_page(pfn);
152 if (zone != page_zone(page))
153 continue;
154
155 clear_pageblock_skip(page);
156 }
157}
158
Mel Gorman62997022012-10-08 16:32:47 -0700159void reset_isolation_suitable(pg_data_t *pgdat)
160{
161 int zoneid;
162
163 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
164 struct zone *zone = &pgdat->node_zones[zoneid];
165 if (!populated_zone(zone))
166 continue;
167
168 /* Only flush if a full compaction finished recently */
169 if (zone->compact_blockskip_flush)
170 __reset_isolation_suitable(zone);
171 }
172}
173
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700174/*
175 * If no pages were isolated then mark this pageblock to be skipped in the
Mel Gorman62997022012-10-08 16:32:47 -0700176 * future. The information is later cleared by __reset_isolation_suitable().
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700177 */
Mel Gormanc89511a2012-10-08 16:32:45 -0700178static void update_pageblock_skip(struct compact_control *cc,
179 struct page *page, unsigned long nr_isolated,
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700180 bool migrate_scanner)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700181{
Mel Gormanc89511a2012-10-08 16:32:45 -0700182 struct zone *zone = cc->zone;
David Rientjes35979ef2014-06-04 16:08:27 -0700183 unsigned long pfn;
Joonsoo Kim6815bf32013-12-18 17:08:52 -0800184
185 if (cc->ignore_skip_hint)
186 return;
187
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700188 if (!page)
189 return;
190
David Rientjes35979ef2014-06-04 16:08:27 -0700191 if (nr_isolated)
192 return;
193
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700194 set_pageblock_skip(page);
Mel Gormanc89511a2012-10-08 16:32:45 -0700195
David Rientjes35979ef2014-06-04 16:08:27 -0700196 pfn = page_to_pfn(page);
197
198 /* Update where async and sync compaction should restart */
199 if (migrate_scanner) {
200 if (cc->finished_update_migrate)
201 return;
202 if (pfn > zone->compact_cached_migrate_pfn[0])
203 zone->compact_cached_migrate_pfn[0] = pfn;
David Rientjese0b9dae2014-06-04 16:08:28 -0700204 if (cc->mode != MIGRATE_ASYNC &&
205 pfn > zone->compact_cached_migrate_pfn[1])
David Rientjes35979ef2014-06-04 16:08:27 -0700206 zone->compact_cached_migrate_pfn[1] = pfn;
207 } else {
208 if (cc->finished_update_free)
209 return;
210 if (pfn < zone->compact_cached_free_pfn)
211 zone->compact_cached_free_pfn = pfn;
Mel Gormanc89511a2012-10-08 16:32:45 -0700212 }
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700213}
214#else
215static inline bool isolation_suitable(struct compact_control *cc,
216 struct page *page)
217{
218 return true;
219}
220
Mel Gormanc89511a2012-10-08 16:32:45 -0700221static void update_pageblock_skip(struct compact_control *cc,
222 struct page *page, unsigned long nr_isolated,
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700223 bool migrate_scanner)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700224{
225}
226#endif /* CONFIG_COMPACTION */
227
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700228/*
229 * Compaction requires the taking of some coarse locks that are potentially
230 * very heavily contended. For async compaction, back out if the lock cannot
231 * be taken immediately. For sync compaction, spin on the lock if needed.
232 *
233 * Returns true if the lock is held
234 * Returns false if the lock is not held and compaction should abort
235 */
236static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags,
237 struct compact_control *cc)
Mel Gorman2a1402a2012-10-08 16:32:33 -0700238{
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700239 if (cc->mode == MIGRATE_ASYNC) {
240 if (!spin_trylock_irqsave(lock, *flags)) {
241 cc->contended = COMPACT_CONTENDED_LOCK;
242 return false;
243 }
244 } else {
245 spin_lock_irqsave(lock, *flags);
246 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700247
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700248 return true;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700249}
250
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100251/*
Mel Gormanc67fe372012-08-21 16:16:17 -0700252 * Compaction requires the taking of some coarse locks that are potentially
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700253 * very heavily contended. The lock should be periodically unlocked to avoid
254 * having disabled IRQs for a long time, even when there is nobody waiting on
255 * the lock. It might also be that allowing the IRQs will result in
256 * need_resched() becoming true. If scheduling is needed, async compaction
257 * aborts. Sync compaction schedules.
258 * Either compaction type will also abort if a fatal signal is pending.
259 * In either case if the lock was locked, it is dropped and not regained.
Mel Gormanc67fe372012-08-21 16:16:17 -0700260 *
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700261 * Returns true if compaction should abort due to fatal signal pending, or
262 * async compaction due to need_resched()
263 * Returns false when compaction can continue (sync compaction might have
264 * scheduled)
Mel Gormanc67fe372012-08-21 16:16:17 -0700265 */
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700266static bool compact_unlock_should_abort(spinlock_t *lock,
267 unsigned long flags, bool *locked, struct compact_control *cc)
Mel Gormanc67fe372012-08-21 16:16:17 -0700268{
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700269 if (*locked) {
270 spin_unlock_irqrestore(lock, flags);
271 *locked = false;
272 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700273
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700274 if (fatal_signal_pending(current)) {
275 cc->contended = COMPACT_CONTENDED_SCHED;
276 return true;
277 }
Mel Gormanc67fe372012-08-21 16:16:17 -0700278
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700279 if (need_resched()) {
David Rientjese0b9dae2014-06-04 16:08:28 -0700280 if (cc->mode == MIGRATE_ASYNC) {
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700281 cc->contended = COMPACT_CONTENDED_SCHED;
282 return true;
Mel Gormanc67fe372012-08-21 16:16:17 -0700283 }
Mel Gormanc67fe372012-08-21 16:16:17 -0700284 cond_resched();
Mel Gormanc67fe372012-08-21 16:16:17 -0700285 }
286
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700287 return false;
Mel Gormanc67fe372012-08-21 16:16:17 -0700288}
289
Vlastimil Babkabe976572014-06-04 16:10:41 -0700290/*
291 * Aside from avoiding lock contention, compaction also periodically checks
292 * need_resched() and either schedules in sync compaction or aborts async
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700293 * compaction. This is similar to what compact_unlock_should_abort() does, but
Vlastimil Babkabe976572014-06-04 16:10:41 -0700294 * is used where no lock is concerned.
295 *
296 * Returns false when no scheduling was needed, or sync compaction scheduled.
297 * Returns true when async compaction should abort.
298 */
299static inline bool compact_should_abort(struct compact_control *cc)
300{
301 /* async compaction aborts if contended */
302 if (need_resched()) {
303 if (cc->mode == MIGRATE_ASYNC) {
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700304 cc->contended = COMPACT_CONTENDED_SCHED;
Vlastimil Babkabe976572014-06-04 16:10:41 -0700305 return true;
306 }
307
308 cond_resched();
309 }
310
311 return false;
312}
313
Mel Gormanf40d1e42012-10-08 16:32:36 -0700314/* Returns true if the page is within a block suitable for migration to */
315static bool suitable_migration_target(struct page *page)
316{
Joonsoo Kim7d348b92014-04-07 15:37:03 -0700317 /* If the page is a large free page, then disallow migration */
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700318 if (PageBuddy(page)) {
319 /*
320 * We are checking page_order without zone->lock taken. But
321 * the only small danger is that we skip a potentially suitable
322 * pageblock, so it's not worth to check order for valid range.
323 */
324 if (page_order_unsafe(page) >= pageblock_order)
325 return false;
326 }
Mel Gormanf40d1e42012-10-08 16:32:36 -0700327
328 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
Joonsoo Kim7d348b92014-04-07 15:37:03 -0700329 if (migrate_async_suitable(get_pageblock_migratetype(page)))
Mel Gormanf40d1e42012-10-08 16:32:36 -0700330 return true;
331
332 /* Otherwise skip the block */
333 return false;
334}
335
Mel Gormanc67fe372012-08-21 16:16:17 -0700336/*
Jerome Marchand9e4be472013-11-12 15:07:12 -0800337 * Isolate free pages onto a private freelist. If @strict is true, will abort
338 * returning 0 on any invalid PFNs or non-free pages inside of the pageblock
339 * (even though it may still end up isolating some pages).
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100340 */
Mel Gormanf40d1e42012-10-08 16:32:36 -0700341static unsigned long isolate_freepages_block(struct compact_control *cc,
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700342 unsigned long *start_pfn,
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100343 unsigned long end_pfn,
344 struct list_head *freelist,
345 bool strict)
Mel Gorman748446b2010-05-24 14:32:27 -0700346{
Mel Gormanb7aba692011-01-13 15:45:54 -0800347 int nr_scanned = 0, total_isolated = 0;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700348 struct page *cursor, *valid_page = NULL;
Xiubo Lib8b2d822014-10-09 15:28:21 -0700349 unsigned long flags = 0;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700350 bool locked = false;
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700351 unsigned long blockpfn = *start_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700352
Mel Gorman748446b2010-05-24 14:32:27 -0700353 cursor = pfn_to_page(blockpfn);
354
Mel Gormanf40d1e42012-10-08 16:32:36 -0700355 /* Isolate free pages. */
Mel Gorman748446b2010-05-24 14:32:27 -0700356 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
357 int isolated, i;
358 struct page *page = cursor;
359
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700360 /*
361 * Periodically drop the lock (if held) regardless of its
362 * contention, to give chance to IRQs. Abort if fatal signal
363 * pending or async compaction detects need_resched()
364 */
365 if (!(blockpfn % SWAP_CLUSTER_MAX)
366 && compact_unlock_should_abort(&cc->zone->lock, flags,
367 &locked, cc))
368 break;
369
Mel Gormanb7aba692011-01-13 15:45:54 -0800370 nr_scanned++;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700371 if (!pfn_valid_within(blockpfn))
Laura Abbott2af120b2014-03-10 15:49:44 -0700372 goto isolate_fail;
373
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700374 if (!valid_page)
375 valid_page = page;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700376 if (!PageBuddy(page))
Laura Abbott2af120b2014-03-10 15:49:44 -0700377 goto isolate_fail;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700378
379 /*
Vlastimil Babka69b71892014-10-09 15:27:18 -0700380 * If we already hold the lock, we can skip some rechecking.
381 * Note that if we hold the lock now, checked_pageblock was
382 * already set in some previous iteration (or strict is true),
383 * so it is correct to skip the suitable migration target
384 * recheck as well.
Mel Gormanf40d1e42012-10-08 16:32:36 -0700385 */
Vlastimil Babka69b71892014-10-09 15:27:18 -0700386 if (!locked) {
387 /*
388 * The zone lock must be held to isolate freepages.
389 * Unfortunately this is a very coarse lock and can be
390 * heavily contended if there are parallel allocations
391 * or parallel compactions. For async compaction do not
392 * spin on the lock and we acquire the lock as late as
393 * possible.
394 */
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700395 locked = compact_trylock_irqsave(&cc->zone->lock,
396 &flags, cc);
Vlastimil Babka69b71892014-10-09 15:27:18 -0700397 if (!locked)
398 break;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700399
Vlastimil Babka69b71892014-10-09 15:27:18 -0700400 /* Recheck this is a buddy page under lock */
401 if (!PageBuddy(page))
402 goto isolate_fail;
403 }
Mel Gorman748446b2010-05-24 14:32:27 -0700404
405 /* Found a free page, break it into order-0 pages */
406 isolated = split_free_page(page);
407 total_isolated += isolated;
408 for (i = 0; i < isolated; i++) {
409 list_add(&page->lru, freelist);
410 page++;
411 }
412
413 /* If a page was split, advance to the end of it */
414 if (isolated) {
415 blockpfn += isolated - 1;
416 cursor += isolated - 1;
Laura Abbott2af120b2014-03-10 15:49:44 -0700417 continue;
Mel Gorman748446b2010-05-24 14:32:27 -0700418 }
Laura Abbott2af120b2014-03-10 15:49:44 -0700419
420isolate_fail:
421 if (strict)
422 break;
423 else
424 continue;
425
Mel Gorman748446b2010-05-24 14:32:27 -0700426 }
427
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700428 /* Record how far we have got within the block */
429 *start_pfn = blockpfn;
430
Mel Gormanb7aba692011-01-13 15:45:54 -0800431 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
Mel Gormanf40d1e42012-10-08 16:32:36 -0700432
433 /*
434 * If strict isolation is requested by CMA then check that all the
435 * pages requested were isolated. If there were any failures, 0 is
436 * returned and CMA will fail.
437 */
Laura Abbott2af120b2014-03-10 15:49:44 -0700438 if (strict && blockpfn < end_pfn)
Mel Gormanf40d1e42012-10-08 16:32:36 -0700439 total_isolated = 0;
440
441 if (locked)
442 spin_unlock_irqrestore(&cc->zone->lock, flags);
443
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700444 /* Update the pageblock-skip if the whole pageblock was scanned */
445 if (blockpfn == end_pfn)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700446 update_pageblock_skip(cc, valid_page, total_isolated, false);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700447
Minchan Kim010fc292012-12-20 15:05:06 -0800448 count_compact_events(COMPACTFREE_SCANNED, nr_scanned);
Mel Gorman397487d2012-10-19 12:00:10 +0100449 if (total_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -0800450 count_compact_events(COMPACTISOLATED, total_isolated);
Mel Gorman748446b2010-05-24 14:32:27 -0700451 return total_isolated;
452}
453
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100454/**
455 * isolate_freepages_range() - isolate free pages.
456 * @start_pfn: The first PFN to start isolating.
457 * @end_pfn: The one-past-last PFN.
458 *
459 * Non-free pages, invalid PFNs, or zone boundaries within the
460 * [start_pfn, end_pfn) range are considered errors, cause function to
461 * undo its actions and return zero.
462 *
463 * Otherwise, function returns one-past-the-last PFN of isolated page
464 * (which may be greater then end_pfn if end fell in a middle of
465 * a free page).
466 */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100467unsigned long
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700468isolate_freepages_range(struct compact_control *cc,
469 unsigned long start_pfn, unsigned long end_pfn)
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100470{
Mel Gormanf40d1e42012-10-08 16:32:36 -0700471 unsigned long isolated, pfn, block_end_pfn;
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100472 LIST_HEAD(freelist);
473
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700474 pfn = start_pfn;
475 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100476
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700477 for (; pfn < end_pfn; pfn += isolated,
478 block_end_pfn += pageblock_nr_pages) {
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700479 /* Protect pfn from changing by isolate_freepages_block */
480 unsigned long isolate_start_pfn = pfn;
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700481
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100482 block_end_pfn = min(block_end_pfn, end_pfn);
483
Joonsoo Kim58420012014-11-13 15:19:07 -0800484 /*
485 * pfn could pass the block_end_pfn if isolated freepage
486 * is more than pageblock order. In this case, we adjust
487 * scanning range to right one.
488 */
489 if (pfn >= block_end_pfn) {
490 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
491 block_end_pfn = min(block_end_pfn, end_pfn);
492 }
493
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700494 if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
495 break;
496
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700497 isolated = isolate_freepages_block(cc, &isolate_start_pfn,
498 block_end_pfn, &freelist, true);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100499
500 /*
501 * In strict mode, isolate_freepages_block() returns 0 if
502 * there are any holes in the block (ie. invalid PFNs or
503 * non-free pages).
504 */
505 if (!isolated)
506 break;
507
508 /*
509 * If we managed to isolate pages, it is always (1 << n) *
510 * pageblock_nr_pages for some non-negative n. (Max order
511 * page may span two pageblocks).
512 */
513 }
514
515 /* split_free_page does not map the pages */
516 map_pages(&freelist);
517
518 if (pfn < end_pfn) {
519 /* Loop terminated early, cleanup. */
520 release_freepages(&freelist);
521 return 0;
522 }
523
524 /* We don't use freelists for anything. */
525 return pfn;
526}
527
Mel Gorman748446b2010-05-24 14:32:27 -0700528/* Update the number of anon and file isolated pages in the zone */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700529static void acct_isolated(struct zone *zone, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700530{
531 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700532 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700533
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700534 if (list_empty(&cc->migratepages))
535 return;
536
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700537 list_for_each_entry(page, &cc->migratepages, lru)
538 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700539
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700540 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
541 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
Mel Gorman748446b2010-05-24 14:32:27 -0700542}
543
544/* Similar to reclaim, but different enough that they don't share logic */
545static bool too_many_isolated(struct zone *zone)
546{
Minchan Kimbc693042010-09-09 16:38:00 -0700547 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700548
549 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
550 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700551 active = zone_page_state(zone, NR_ACTIVE_FILE) +
552 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700553 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
554 zone_page_state(zone, NR_ISOLATED_ANON);
555
Minchan Kimbc693042010-09-09 16:38:00 -0700556 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700557}
558
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100559/**
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700560 * isolate_migratepages_block() - isolate all migrate-able pages within
561 * a single pageblock
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100562 * @cc: Compaction control structure.
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700563 * @low_pfn: The first PFN to isolate
564 * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock
565 * @isolate_mode: Isolation mode to be used.
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100566 *
567 * Isolate all pages that can be migrated from the range specified by
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700568 * [low_pfn, end_pfn). The range is expected to be within same pageblock.
569 * Returns zero if there is a fatal signal pending, otherwise PFN of the
570 * first page that was not scanned (which may be both less, equal to or more
571 * than end_pfn).
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100572 *
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700573 * The pages are isolated on cc->migratepages list (not required to be empty),
574 * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field
575 * is neither read nor updated.
Mel Gorman748446b2010-05-24 14:32:27 -0700576 */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700577static unsigned long
578isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
579 unsigned long end_pfn, isolate_mode_t isolate_mode)
Mel Gorman748446b2010-05-24 14:32:27 -0700580{
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700581 struct zone *zone = cc->zone;
Mel Gormanb7aba692011-01-13 15:45:54 -0800582 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700583 struct list_head *migratelist = &cc->migratepages;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700584 struct lruvec *lruvec;
Xiubo Lib8b2d822014-10-09 15:28:21 -0700585 unsigned long flags = 0;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700586 bool locked = false;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700587 struct page *page = NULL, *valid_page = NULL;
Mel Gorman748446b2010-05-24 14:32:27 -0700588
Mel Gorman748446b2010-05-24 14:32:27 -0700589 /*
590 * Ensure that there are not too many pages isolated from the LRU
591 * list by either parallel reclaimers or compaction. If there are,
592 * delay for some time until fewer pages are isolated
593 */
594 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700595 /* async migration should just abort */
David Rientjese0b9dae2014-06-04 16:08:28 -0700596 if (cc->mode == MIGRATE_ASYNC)
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100597 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700598
Mel Gorman748446b2010-05-24 14:32:27 -0700599 congestion_wait(BLK_RW_ASYNC, HZ/10);
600
601 if (fatal_signal_pending(current))
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100602 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700603 }
604
Vlastimil Babkabe976572014-06-04 16:10:41 -0700605 if (compact_should_abort(cc))
606 return 0;
David Rientjesaeef4b82014-06-04 16:08:31 -0700607
Mel Gorman748446b2010-05-24 14:32:27 -0700608 /* Time to isolate some pages for migration */
Mel Gorman748446b2010-05-24 14:32:27 -0700609 for (; low_pfn < end_pfn; low_pfn++) {
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700610 /*
611 * Periodically drop the lock (if held) regardless of its
612 * contention, to give chance to IRQs. Abort async compaction
613 * if contended.
614 */
615 if (!(low_pfn % SWAP_CLUSTER_MAX)
616 && compact_unlock_should_abort(&zone->lru_lock, flags,
617 &locked, cc))
618 break;
Mel Gormanc67fe372012-08-21 16:16:17 -0700619
Mel Gorman748446b2010-05-24 14:32:27 -0700620 if (!pfn_valid_within(low_pfn))
621 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800622 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700623
Mel Gorman748446b2010-05-24 14:32:27 -0700624 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800625
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700626 if (!valid_page)
627 valid_page = page;
628
Mel Gorman6c144662014-01-23 15:53:38 -0800629 /*
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700630 * Skip if free. We read page order here without zone lock
631 * which is generally unsafe, but the race window is small and
632 * the worst thing that can happen is that we skip some
633 * potential isolation targets.
Mel Gorman6c144662014-01-23 15:53:38 -0800634 */
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700635 if (PageBuddy(page)) {
636 unsigned long freepage_order = page_order_unsafe(page);
637
638 /*
639 * Without lock, we cannot be sure that what we got is
640 * a valid page order. Consider only values in the
641 * valid order range to prevent low_pfn overflow.
642 */
643 if (freepage_order > 0 && freepage_order < MAX_ORDER)
644 low_pfn += (1UL << freepage_order) - 1;
Mel Gorman748446b2010-05-24 14:32:27 -0700645 continue;
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700646 }
Mel Gorman748446b2010-05-24 14:32:27 -0700647
Mel Gorman9927af742011-01-13 15:45:59 -0800648 /*
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800649 * Check may be lockless but that's ok as we recheck later.
650 * It's possible to migrate LRU pages and balloon pages
651 * Skip any other type of page
652 */
653 if (!PageLRU(page)) {
654 if (unlikely(balloon_page_movable(page))) {
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700655 if (balloon_page_isolate(page)) {
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800656 /* Successfully isolated */
Joonsoo Kimb6c75012014-04-07 15:37:07 -0700657 goto isolate_success;
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800658 }
659 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800660 continue;
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800661 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800662
663 /*
Mel Gorman2a1402a2012-10-08 16:32:33 -0700664 * PageLRU is set. lru_lock normally excludes isolation
665 * splitting and collapsing (collapsing has already happened
666 * if PageLRU is set) but the lock is not necessarily taken
667 * here and it is wasteful to take it just to check transhuge.
668 * Check TransHuge without lock and skip the whole pageblock if
669 * it's either a transhuge or hugetlbfs page, as calling
670 * compound_order() without preventing THP from splitting the
671 * page underneath us may return surprising results.
Andrea Arcangelibc835012011-01-13 15:47:08 -0800672 */
673 if (PageTransHuge(page)) {
Mel Gorman2a1402a2012-10-08 16:32:33 -0700674 if (!locked)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700675 low_pfn = ALIGN(low_pfn + 1,
676 pageblock_nr_pages) - 1;
677 else
678 low_pfn += (1 << compound_order(page)) - 1;
679
Mel Gorman2a1402a2012-10-08 16:32:33 -0700680 continue;
681 }
682
David Rientjes119d6d52014-04-03 14:48:00 -0700683 /*
684 * Migration will fail if an anonymous page is pinned in memory,
685 * so avoid taking lru_lock and isolating it unnecessarily in an
686 * admittedly racy check.
687 */
688 if (!page_mapping(page) &&
689 page_count(page) > page_mapcount(page))
690 continue;
691
Vlastimil Babka69b71892014-10-09 15:27:18 -0700692 /* If we already hold the lock, we can skip some rechecking */
693 if (!locked) {
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700694 locked = compact_trylock_irqsave(&zone->lru_lock,
695 &flags, cc);
Vlastimil Babka69b71892014-10-09 15:27:18 -0700696 if (!locked)
697 break;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700698
Vlastimil Babka69b71892014-10-09 15:27:18 -0700699 /* Recheck PageLRU and PageTransHuge under lock */
700 if (!PageLRU(page))
701 continue;
702 if (PageTransHuge(page)) {
703 low_pfn += (1 << compound_order(page)) - 1;
704 continue;
705 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800706 }
707
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700708 lruvec = mem_cgroup_page_lruvec(page, zone);
709
Mel Gorman748446b2010-05-24 14:32:27 -0700710 /* Try isolate the page */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700711 if (__isolate_lru_page(page, isolate_mode) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700712 continue;
713
Sasha Levin309381fea2014-01-23 15:52:54 -0800714 VM_BUG_ON_PAGE(PageTransCompound(page), page);
Andrea Arcangelibc835012011-01-13 15:47:08 -0800715
Mel Gorman748446b2010-05-24 14:32:27 -0700716 /* Successfully isolated */
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700717 del_page_from_lru_list(page, lruvec, page_lru(page));
Joonsoo Kimb6c75012014-04-07 15:37:07 -0700718
719isolate_success:
720 cc->finished_update_migrate = true;
Mel Gorman748446b2010-05-24 14:32:27 -0700721 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700722 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800723 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700724
725 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800726 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
727 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700728 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800729 }
Mel Gorman748446b2010-05-24 14:32:27 -0700730 }
731
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700732 /*
733 * The PageBuddy() check could have potentially brought us outside
734 * the range to be scanned.
735 */
736 if (unlikely(low_pfn > end_pfn))
737 low_pfn = end_pfn;
738
Mel Gormanc67fe372012-08-21 16:16:17 -0700739 if (locked)
740 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700741
Vlastimil Babka50b5b092014-01-21 15:51:10 -0800742 /*
743 * Update the pageblock-skip information and cached scanner pfn,
744 * if the whole pageblock was scanned without isolating any page.
Vlastimil Babka50b5b092014-01-21 15:51:10 -0800745 */
David Rientjes35979ef2014-06-04 16:08:27 -0700746 if (low_pfn == end_pfn)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700747 update_pageblock_skip(cc, valid_page, nr_isolated, true);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700748
Mel Gormanb7aba692011-01-13 15:45:54 -0800749 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
750
Minchan Kim010fc292012-12-20 15:05:06 -0800751 count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned);
Mel Gorman397487d2012-10-19 12:00:10 +0100752 if (nr_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -0800753 count_compact_events(COMPACTISOLATED, nr_isolated);
Mel Gorman397487d2012-10-19 12:00:10 +0100754
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100755 return low_pfn;
756}
757
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700758/**
759 * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
760 * @cc: Compaction control structure.
761 * @start_pfn: The first PFN to start isolating.
762 * @end_pfn: The one-past-last PFN.
763 *
764 * Returns zero if isolation fails fatally due to e.g. pending signal.
765 * Otherwise, function returns one-past-the-last PFN of isolated page
766 * (which may be greater than end_pfn if end fell in a middle of a THP page).
767 */
768unsigned long
769isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
770 unsigned long end_pfn)
771{
772 unsigned long pfn, block_end_pfn;
773
774 /* Scan block by block. First and last block may be incomplete */
775 pfn = start_pfn;
776 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
777
778 for (; pfn < end_pfn; pfn = block_end_pfn,
779 block_end_pfn += pageblock_nr_pages) {
780
781 block_end_pfn = min(block_end_pfn, end_pfn);
782
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700783 if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700784 continue;
785
786 pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
787 ISOLATE_UNEVICTABLE);
788
789 /*
790 * In case of fatal failure, release everything that might
791 * have been isolated in the previous iteration, and signal
792 * the failure back to caller.
793 */
794 if (!pfn) {
795 putback_movable_pages(&cc->migratepages);
796 cc->nr_migratepages = 0;
797 break;
798 }
Joonsoo Kim6ea41c02014-10-29 14:50:20 -0700799
800 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
801 break;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700802 }
803 acct_isolated(cc->zone, cc);
804
805 return pfn;
806}
807
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100808#endif /* CONFIG_COMPACTION || CONFIG_CMA */
809#ifdef CONFIG_COMPACTION
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100810/*
811 * Based on information in the current compact_control, find blocks
812 * suitable for isolating free pages from and then isolate them.
813 */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700814static void isolate_freepages(struct compact_control *cc)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100815{
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700816 struct zone *zone = cc->zone;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100817 struct page *page;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700818 unsigned long block_start_pfn; /* start of current pageblock */
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700819 unsigned long isolate_start_pfn; /* exact pfn we start at */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700820 unsigned long block_end_pfn; /* end of current pageblock */
821 unsigned long low_pfn; /* lowest pfn scanner is able to scan */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100822 int nr_freepages = cc->nr_freepages;
823 struct list_head *freelist = &cc->freepages;
824
825 /*
826 * Initialise the free scanner. The starting point is where we last
Vlastimil Babka49e068f2014-05-06 12:50:03 -0700827 * successfully isolated from, zone-cached value, or the end of the
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700828 * zone when isolating for the first time. For looping we also need
829 * this pfn aligned down to the pageblock boundary, because we do
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700830 * block_start_pfn -= pageblock_nr_pages in the for loop.
831 * For ending point, take care when isolating in last pageblock of a
832 * a zone which ends in the middle of a pageblock.
Vlastimil Babka49e068f2014-05-06 12:50:03 -0700833 * The low boundary is the end of the pageblock the migration scanner
834 * is using.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100835 */
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700836 isolate_start_pfn = cc->free_pfn;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700837 block_start_pfn = cc->free_pfn & ~(pageblock_nr_pages-1);
838 block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
839 zone_end_pfn(zone));
Vlastimil Babka7ed695e02014-01-21 15:51:09 -0800840 low_pfn = ALIGN(cc->migrate_pfn + 1, pageblock_nr_pages);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100841
842 /*
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100843 * Isolate free pages until enough are available to migrate the
844 * pages on cc->migratepages. We stop searching if the migrate
845 * and free page scanners meet or enough free pages are isolated.
846 */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700847 for (; block_start_pfn >= low_pfn && cc->nr_migratepages > nr_freepages;
848 block_end_pfn = block_start_pfn,
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700849 block_start_pfn -= pageblock_nr_pages,
850 isolate_start_pfn = block_start_pfn) {
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100851 unsigned long isolated;
852
David Rientjesf6ea3ad2013-09-30 13:45:03 -0700853 /*
854 * This can iterate a massively long zone without finding any
855 * suitable migration targets, so periodically check if we need
Vlastimil Babkabe976572014-06-04 16:10:41 -0700856 * to schedule, or even abort async compaction.
David Rientjesf6ea3ad2013-09-30 13:45:03 -0700857 */
Vlastimil Babkabe976572014-06-04 16:10:41 -0700858 if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
859 && compact_should_abort(cc))
860 break;
David Rientjesf6ea3ad2013-09-30 13:45:03 -0700861
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700862 page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
863 zone);
864 if (!page)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100865 continue;
866
867 /* Check the block is suitable for migration */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700868 if (!suitable_migration_target(page))
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100869 continue;
Linus Torvalds68e3e922012-06-03 20:05:57 -0700870
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700871 /* If isolation recently failed, do not retry */
872 if (!isolation_suitable(cc, page))
873 continue;
874
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700875 /* Found a block suitable for isolating free pages from. */
876 isolated = isolate_freepages_block(cc, &isolate_start_pfn,
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700877 block_end_pfn, freelist, false);
Mel Gormanf40d1e42012-10-08 16:32:36 -0700878 nr_freepages += isolated;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100879
880 /*
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700881 * Remember where the free scanner should restart next time,
882 * which is where isolate_freepages_block() left off.
883 * But if it scanned the whole pageblock, isolate_start_pfn
884 * now points at block_end_pfn, which is the start of the next
885 * pageblock.
886 * In that case we will however want to restart at the start
887 * of the previous pageblock.
888 */
889 cc->free_pfn = (isolate_start_pfn < block_end_pfn) ?
890 isolate_start_pfn :
891 block_start_pfn - pageblock_nr_pages;
892
893 /*
Vlastimil Babkae9ade562014-06-04 16:08:34 -0700894 * Set a flag that we successfully isolated in this pageblock.
895 * In the next loop iteration, zone->compact_cached_free_pfn
896 * will not be updated and thus it will effectively contain the
897 * highest pageblock we isolated pages from.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100898 */
Vlastimil Babkae9ade562014-06-04 16:08:34 -0700899 if (isolated)
Mel Gormanc89511a2012-10-08 16:32:45 -0700900 cc->finished_update_free = true;
Vlastimil Babkabe976572014-06-04 16:10:41 -0700901
902 /*
903 * isolate_freepages_block() might have aborted due to async
904 * compaction being contended
905 */
906 if (cc->contended)
907 break;
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100908 }
909
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100910 /* split_free_page does not map the pages */
911 map_pages(freelist);
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100912
Vlastimil Babka7ed695e02014-01-21 15:51:09 -0800913 /*
914 * If we crossed the migrate scanner, we want to keep it that way
915 * so that compact_finished() may detect this
916 */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700917 if (block_start_pfn < low_pfn)
Vlastimil Babkae9ade562014-06-04 16:08:34 -0700918 cc->free_pfn = cc->migrate_pfn;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700919
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100920 cc->nr_freepages = nr_freepages;
Mel Gorman748446b2010-05-24 14:32:27 -0700921}
922
923/*
924 * This is a migrate-callback that "allocates" freepages by taking pages
925 * from the isolated freelists in the block we are migrating to.
926 */
927static struct page *compaction_alloc(struct page *migratepage,
928 unsigned long data,
929 int **result)
930{
931 struct compact_control *cc = (struct compact_control *)data;
932 struct page *freepage;
933
Vlastimil Babkabe976572014-06-04 16:10:41 -0700934 /*
935 * Isolate free pages if necessary, and if we are not aborting due to
936 * contention.
937 */
Mel Gorman748446b2010-05-24 14:32:27 -0700938 if (list_empty(&cc->freepages)) {
Vlastimil Babkabe976572014-06-04 16:10:41 -0700939 if (!cc->contended)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700940 isolate_freepages(cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700941
942 if (list_empty(&cc->freepages))
943 return NULL;
944 }
945
946 freepage = list_entry(cc->freepages.next, struct page, lru);
947 list_del(&freepage->lru);
948 cc->nr_freepages--;
949
950 return freepage;
951}
952
953/*
David Rientjesd53aea32014-06-04 16:08:26 -0700954 * This is a migrate-callback that "frees" freepages back to the isolated
955 * freelist. All pages on the freelist are from the same zone, so there is no
956 * special handling needed for NUMA.
957 */
958static void compaction_free(struct page *page, unsigned long data)
959{
960 struct compact_control *cc = (struct compact_control *)data;
961
962 list_add(&page->lru, &cc->freepages);
963 cc->nr_freepages++;
964}
965
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100966/* possible outcome of isolate_migratepages */
967typedef enum {
968 ISOLATE_ABORT, /* Abort compaction now */
969 ISOLATE_NONE, /* No pages isolated, continue scanning */
970 ISOLATE_SUCCESS, /* Pages isolated, migrate */
971} isolate_migrate_t;
972
973/*
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700974 * Isolate all pages that can be migrated from the first suitable block,
975 * starting at the block pointed to by the migrate scanner pfn within
976 * compact_control.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100977 */
978static isolate_migrate_t isolate_migratepages(struct zone *zone,
979 struct compact_control *cc)
980{
981 unsigned long low_pfn, end_pfn;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700982 struct page *page;
983 const isolate_mode_t isolate_mode =
984 (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100985
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700986 /*
987 * Start at where we last stopped, or beginning of the zone as
988 * initialized by compact_zone()
989 */
990 low_pfn = cc->migrate_pfn;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100991
992 /* Only scan within a pageblock boundary */
Mel Gormana9aacbc2013-02-22 16:32:25 -0800993 end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100994
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700995 /*
996 * Iterate over whole pageblocks until we find the first suitable.
997 * Do not cross the free scanner.
998 */
999 for (; end_pfn <= cc->free_pfn;
1000 low_pfn = end_pfn, end_pfn += pageblock_nr_pages) {
1001
1002 /*
1003 * This can potentially iterate a massively long zone with
1004 * many pageblocks unsuitable, so periodically check if we
1005 * need to schedule, or even abort async compaction.
1006 */
1007 if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
1008 && compact_should_abort(cc))
1009 break;
1010
Vlastimil Babka7d49d882014-10-09 15:27:11 -07001011 page = pageblock_pfn_to_page(low_pfn, end_pfn, zone);
1012 if (!page)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001013 continue;
1014
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001015 /* If isolation recently failed, do not retry */
1016 if (!isolation_suitable(cc, page))
1017 continue;
1018
1019 /*
1020 * For async compaction, also only scan in MOVABLE blocks.
1021 * Async compaction is optimistic to see if the minimum amount
1022 * of work satisfies the allocation.
1023 */
1024 if (cc->mode == MIGRATE_ASYNC &&
1025 !migrate_async_suitable(get_pageblock_migratetype(page)))
1026 continue;
1027
1028 /* Perform the isolation */
1029 low_pfn = isolate_migratepages_block(cc, low_pfn, end_pfn,
1030 isolate_mode);
1031
Hugh Dickinsb7c386c2015-02-12 15:00:28 -08001032 if (!low_pfn || cc->contended) {
1033 acct_isolated(zone, cc);
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001034 return ISOLATE_ABORT;
Hugh Dickinsb7c386c2015-02-12 15:00:28 -08001035 }
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001036
1037 /*
1038 * Either we isolated something and proceed with migration. Or
1039 * we failed and compact_zone should decide if we should
1040 * continue or not.
1041 */
1042 break;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001043 }
1044
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001045 acct_isolated(zone, cc);
Vlastimil Babka1d5bfe12014-11-13 15:19:30 -08001046 /*
1047 * Record where migration scanner will be restarted. If we end up in
1048 * the same pageblock as the free scanner, make the scanners fully
1049 * meet so that compact_finished() terminates compaction.
1050 */
1051 cc->migrate_pfn = (end_pfn <= cc->free_pfn) ? low_pfn : cc->free_pfn;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001052
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001053 return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001054}
1055
David Rientjes6d7ce552014-10-09 15:27:27 -07001056static int compact_finished(struct zone *zone, struct compact_control *cc,
1057 const int migratetype)
Mel Gorman748446b2010-05-24 14:32:27 -07001058{
Mel Gorman8fb74b92013-01-11 14:32:16 -08001059 unsigned int order;
Andrea Arcangeli5a03b052011-01-13 15:47:11 -08001060 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -07001061
Vlastimil Babkabe976572014-06-04 16:10:41 -07001062 if (cc->contended || fatal_signal_pending(current))
Mel Gorman748446b2010-05-24 14:32:27 -07001063 return COMPACT_PARTIAL;
1064
Mel Gorman753341a2012-10-08 16:32:40 -07001065 /* Compaction run completes if the migrate and free scanner meet */
Mel Gormanbb13ffe2012-10-08 16:32:41 -07001066 if (cc->free_pfn <= cc->migrate_pfn) {
Vlastimil Babka55b7c4c2014-01-21 15:51:11 -08001067 /* Let the next compaction start anew. */
David Rientjes35979ef2014-06-04 16:08:27 -07001068 zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
1069 zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
Vlastimil Babka55b7c4c2014-01-21 15:51:11 -08001070 zone->compact_cached_free_pfn = zone_end_pfn(zone);
1071
Mel Gorman62997022012-10-08 16:32:47 -07001072 /*
1073 * Mark that the PG_migrate_skip information should be cleared
1074 * by kswapd when it goes to sleep. kswapd does not set the
1075 * flag itself as the decision to be clear should be directly
1076 * based on an allocation request.
1077 */
1078 if (!current_is_kswapd())
1079 zone->compact_blockskip_flush = true;
1080
Mel Gorman748446b2010-05-24 14:32:27 -07001081 return COMPACT_COMPLETE;
Mel Gormanbb13ffe2012-10-08 16:32:41 -07001082 }
Mel Gorman748446b2010-05-24 14:32:27 -07001083
Johannes Weiner82478fb2011-01-20 14:44:21 -08001084 /*
1085 * order == -1 is expected when compacting via
1086 * /proc/sys/vm/compact_memory
1087 */
Mel Gorman56de7262010-05-24 14:32:30 -07001088 if (cc->order == -1)
1089 return COMPACT_CONTINUE;
1090
Michal Hocko3957c772011-06-15 15:08:25 -07001091 /* Compaction run is not finished if the watermark is not met */
1092 watermark = low_wmark_pages(zone);
1093 watermark += (1 << cc->order);
1094
1095 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
1096 return COMPACT_CONTINUE;
1097
Mel Gorman56de7262010-05-24 14:32:30 -07001098 /* Direct compactor: Is a suitable page free? */
Mel Gorman8fb74b92013-01-11 14:32:16 -08001099 for (order = cc->order; order < MAX_ORDER; order++) {
1100 struct free_area *area = &zone->free_area[order];
Mel Gorman56de7262010-05-24 14:32:30 -07001101
Mel Gorman8fb74b92013-01-11 14:32:16 -08001102 /* Job done if page is free of the right migratetype */
David Rientjes6d7ce552014-10-09 15:27:27 -07001103 if (!list_empty(&area->free_list[migratetype]))
Mel Gorman8fb74b92013-01-11 14:32:16 -08001104 return COMPACT_PARTIAL;
1105
1106 /* Job done if allocation would set block type */
Joonsoo Kim42af81d2015-02-12 14:59:50 -08001107 if (order >= pageblock_order && area->nr_free)
Mel Gorman8fb74b92013-01-11 14:32:16 -08001108 return COMPACT_PARTIAL;
Mel Gorman56de7262010-05-24 14:32:30 -07001109 }
1110
Mel Gorman748446b2010-05-24 14:32:27 -07001111 return COMPACT_CONTINUE;
1112}
1113
Mel Gorman3e7d3442011-01-13 15:45:56 -08001114/*
1115 * compaction_suitable: Is this suitable to run compaction on this zone now?
1116 * Returns
1117 * COMPACT_SKIPPED - If there are too few free pages for compaction
1118 * COMPACT_PARTIAL - If the allocation would succeed without compaction
1119 * COMPACT_CONTINUE - If compaction should run now
1120 */
1121unsigned long compaction_suitable(struct zone *zone, int order)
1122{
1123 int fragindex;
1124 unsigned long watermark;
1125
1126 /*
Michal Hocko3957c772011-06-15 15:08:25 -07001127 * order == -1 is expected when compacting via
1128 * /proc/sys/vm/compact_memory
1129 */
1130 if (order == -1)
1131 return COMPACT_CONTINUE;
1132
1133 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -08001134 * Watermarks for order-0 must be met for compaction. Note the 2UL.
1135 * This is because during migration, copies of pages need to be
1136 * allocated and for a short time, the footprint is higher
1137 */
1138 watermark = low_wmark_pages(zone) + (2UL << order);
1139 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
1140 return COMPACT_SKIPPED;
1141
1142 /*
1143 * fragmentation index determines if allocation failures are due to
1144 * low memory or external fragmentation
1145 *
Shaohua Lia582a732011-06-15 15:08:49 -07001146 * index of -1000 implies allocations might succeed depending on
1147 * watermarks
Mel Gorman3e7d3442011-01-13 15:45:56 -08001148 * index towards 0 implies failure is due to lack of memory
1149 * index towards 1000 implies failure is due to fragmentation
1150 *
1151 * Only compact if a failure would be due to fragmentation.
1152 */
1153 fragindex = fragmentation_index(zone, order);
1154 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
1155 return COMPACT_SKIPPED;
1156
Shaohua Lia582a732011-06-15 15:08:49 -07001157 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
1158 0, 0))
Mel Gorman3e7d3442011-01-13 15:45:56 -08001159 return COMPACT_PARTIAL;
1160
1161 return COMPACT_CONTINUE;
1162}
1163
Mel Gorman748446b2010-05-24 14:32:27 -07001164static int compact_zone(struct zone *zone, struct compact_control *cc)
1165{
1166 int ret;
Mel Gormanc89511a2012-10-08 16:32:45 -07001167 unsigned long start_pfn = zone->zone_start_pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -08001168 unsigned long end_pfn = zone_end_pfn(zone);
David Rientjes6d7ce552014-10-09 15:27:27 -07001169 const int migratetype = gfpflags_to_migratetype(cc->gfp_mask);
David Rientjese0b9dae2014-06-04 16:08:28 -07001170 const bool sync = cc->mode != MIGRATE_ASYNC;
Mel Gorman748446b2010-05-24 14:32:27 -07001171
Mel Gorman3e7d3442011-01-13 15:45:56 -08001172 ret = compaction_suitable(zone, cc->order);
1173 switch (ret) {
1174 case COMPACT_PARTIAL:
1175 case COMPACT_SKIPPED:
1176 /* Compaction is likely to fail */
1177 return ret;
1178 case COMPACT_CONTINUE:
1179 /* Fall through to compaction */
1180 ;
1181 }
1182
Mel Gormanc89511a2012-10-08 16:32:45 -07001183 /*
Vlastimil Babkad3132e42014-01-21 15:51:08 -08001184 * Clear pageblock skip if there were failures recently and compaction
1185 * is about to be retried after being deferred. kswapd does not do
1186 * this reset as it'll reset the cached information when going to sleep.
1187 */
1188 if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
1189 __reset_isolation_suitable(zone);
1190
1191 /*
Mel Gormanc89511a2012-10-08 16:32:45 -07001192 * Setup to move all movable pages to the end of the zone. Used cached
1193 * information on where the scanners should start but check that it
1194 * is initialised by ensuring the values are within zone boundaries.
1195 */
David Rientjese0b9dae2014-06-04 16:08:28 -07001196 cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync];
Mel Gormanc89511a2012-10-08 16:32:45 -07001197 cc->free_pfn = zone->compact_cached_free_pfn;
1198 if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
1199 cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
1200 zone->compact_cached_free_pfn = cc->free_pfn;
1201 }
1202 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
1203 cc->migrate_pfn = start_pfn;
David Rientjes35979ef2014-06-04 16:08:27 -07001204 zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
1205 zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
Mel Gormanc89511a2012-10-08 16:32:45 -07001206 }
Mel Gorman748446b2010-05-24 14:32:27 -07001207
Mel Gorman0eb927c2014-01-21 15:51:05 -08001208 trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, cc->free_pfn, end_pfn);
1209
Mel Gorman748446b2010-05-24 14:32:27 -07001210 migrate_prep_local();
1211
David Rientjes6d7ce552014-10-09 15:27:27 -07001212 while ((ret = compact_finished(zone, cc, migratetype)) ==
1213 COMPACT_CONTINUE) {
Minchan Kim9d502c12011-03-22 16:30:39 -07001214 int err;
Mel Gorman748446b2010-05-24 14:32:27 -07001215
Mel Gormanf9e35b32011-06-15 15:08:52 -07001216 switch (isolate_migratepages(zone, cc)) {
1217 case ISOLATE_ABORT:
1218 ret = COMPACT_PARTIAL;
Rafael Aquini5733c7d2012-12-11 16:02:47 -08001219 putback_movable_pages(&cc->migratepages);
Shaohua Lie64c5232012-10-08 16:32:27 -07001220 cc->nr_migratepages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -07001221 goto out;
1222 case ISOLATE_NONE:
Mel Gorman748446b2010-05-24 14:32:27 -07001223 continue;
Mel Gormanf9e35b32011-06-15 15:08:52 -07001224 case ISOLATE_SUCCESS:
1225 ;
1226 }
Mel Gorman748446b2010-05-24 14:32:27 -07001227
David Rientjesd53aea32014-06-04 16:08:26 -07001228 err = migrate_pages(&cc->migratepages, compaction_alloc,
David Rientjese0b9dae2014-06-04 16:08:28 -07001229 compaction_free, (unsigned long)cc, cc->mode,
Mel Gorman7b2a2d42012-10-19 14:07:31 +01001230 MR_COMPACTION);
Mel Gorman748446b2010-05-24 14:32:27 -07001231
Vlastimil Babkaf8c93012014-06-04 16:08:32 -07001232 trace_mm_compaction_migratepages(cc->nr_migratepages, err,
1233 &cc->migratepages);
Mel Gorman748446b2010-05-24 14:32:27 -07001234
Vlastimil Babkaf8c93012014-06-04 16:08:32 -07001235 /* All pages were either migrated or will be released */
1236 cc->nr_migratepages = 0;
Minchan Kim9d502c12011-03-22 16:30:39 -07001237 if (err) {
Rafael Aquini5733c7d2012-12-11 16:02:47 -08001238 putback_movable_pages(&cc->migratepages);
Vlastimil Babka7ed695e02014-01-21 15:51:09 -08001239 /*
1240 * migrate_pages() may return -ENOMEM when scanners meet
1241 * and we want compact_finished() to detect it
1242 */
1243 if (err == -ENOMEM && cc->free_pfn > cc->migrate_pfn) {
David Rientjes4bf2bba2012-07-11 14:02:13 -07001244 ret = COMPACT_PARTIAL;
1245 goto out;
1246 }
Mel Gorman748446b2010-05-24 14:32:27 -07001247 }
Mel Gorman748446b2010-05-24 14:32:27 -07001248 }
1249
Mel Gormanf9e35b32011-06-15 15:08:52 -07001250out:
Mel Gorman748446b2010-05-24 14:32:27 -07001251 /* Release free pages and check accounting */
1252 cc->nr_freepages -= release_freepages(&cc->freepages);
1253 VM_BUG_ON(cc->nr_freepages != 0);
1254
Mel Gorman0eb927c2014-01-21 15:51:05 -08001255 trace_mm_compaction_end(ret);
1256
Mel Gorman748446b2010-05-24 14:32:27 -07001257 return ret;
1258}
Mel Gorman76ab0f52010-05-24 14:32:28 -07001259
David Rientjese0b9dae2014-06-04 16:08:28 -07001260static unsigned long compact_zone_order(struct zone *zone, int order,
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001261 gfp_t gfp_mask, enum migrate_mode mode, int *contended)
Mel Gorman56de7262010-05-24 14:32:30 -07001262{
Shaohua Lie64c5232012-10-08 16:32:27 -07001263 unsigned long ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001264 struct compact_control cc = {
1265 .nr_freepages = 0,
1266 .nr_migratepages = 0,
1267 .order = order,
David Rientjes6d7ce552014-10-09 15:27:27 -07001268 .gfp_mask = gfp_mask,
Mel Gorman56de7262010-05-24 14:32:30 -07001269 .zone = zone,
David Rientjese0b9dae2014-06-04 16:08:28 -07001270 .mode = mode,
Mel Gorman56de7262010-05-24 14:32:30 -07001271 };
1272 INIT_LIST_HEAD(&cc.freepages);
1273 INIT_LIST_HEAD(&cc.migratepages);
1274
Shaohua Lie64c5232012-10-08 16:32:27 -07001275 ret = compact_zone(zone, &cc);
1276
1277 VM_BUG_ON(!list_empty(&cc.freepages));
1278 VM_BUG_ON(!list_empty(&cc.migratepages));
1279
1280 *contended = cc.contended;
1281 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001282}
1283
Mel Gorman5e771902010-05-24 14:32:31 -07001284int sysctl_extfrag_threshold = 500;
1285
Mel Gorman56de7262010-05-24 14:32:30 -07001286/**
1287 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
1288 * @zonelist: The zonelist used for the current allocation
1289 * @order: The order of the current allocation
1290 * @gfp_mask: The GFP mask of the current allocation
1291 * @nodemask: The allowed nodes to allocate from
David Rientjese0b9dae2014-06-04 16:08:28 -07001292 * @mode: The migration mode for async, sync light, or sync migration
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001293 * @contended: Return value that determines if compaction was aborted due to
1294 * need_resched() or lock contention
Vlastimil Babka53853e22014-10-09 15:27:02 -07001295 * @candidate_zone: Return the zone where we think allocation should succeed
Mel Gorman56de7262010-05-24 14:32:30 -07001296 *
1297 * This is the main entry point for direct page compaction.
1298 */
1299unsigned long try_to_compact_pages(struct zonelist *zonelist,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001300 int order, gfp_t gfp_mask, nodemask_t *nodemask,
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001301 enum migrate_mode mode, int *contended,
Vlastimil Babka53853e22014-10-09 15:27:02 -07001302 struct zone **candidate_zone)
Mel Gorman56de7262010-05-24 14:32:30 -07001303{
1304 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
1305 int may_enter_fs = gfp_mask & __GFP_FS;
1306 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -07001307 struct zoneref *z;
1308 struct zone *zone;
Vlastimil Babka53853e22014-10-09 15:27:02 -07001309 int rc = COMPACT_DEFERRED;
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001310 int alloc_flags = 0;
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001311 int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */
1312
1313 *contended = COMPACT_CONTENDED_NONE;
Mel Gorman56de7262010-05-24 14:32:30 -07001314
Mel Gorman4ffb6332012-10-08 16:29:09 -07001315 /* Check if the GFP flags allow compaction */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -08001316 if (!order || !may_enter_fs || !may_perform_io)
Vlastimil Babka53853e22014-10-09 15:27:02 -07001317 return COMPACT_SKIPPED;
Mel Gorman56de7262010-05-24 14:32:30 -07001318
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001319#ifdef CONFIG_CMA
David Rientjes43e7a342014-10-09 15:27:25 -07001320 if (gfpflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001321 alloc_flags |= ALLOC_CMA;
1322#endif
Mel Gorman56de7262010-05-24 14:32:30 -07001323 /* Compact each zone in the list */
1324 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
1325 nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -07001326 int status;
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001327 int zone_contended;
Mel Gorman56de7262010-05-24 14:32:30 -07001328
Vlastimil Babka53853e22014-10-09 15:27:02 -07001329 if (compaction_deferred(zone, order))
1330 continue;
1331
David Rientjese0b9dae2014-06-04 16:08:28 -07001332 status = compact_zone_order(zone, order, gfp_mask, mode,
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001333 &zone_contended);
Mel Gorman56de7262010-05-24 14:32:30 -07001334 rc = max(status, rc);
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001335 /*
1336 * It takes at least one zone that wasn't lock contended
1337 * to clear all_zones_contended.
1338 */
1339 all_zones_contended &= zone_contended;
Mel Gorman56de7262010-05-24 14:32:30 -07001340
Mel Gorman3e7d3442011-01-13 15:45:56 -08001341 /* If a normal allocation would succeed, stop compacting */
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001342 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
Vlastimil Babka53853e22014-10-09 15:27:02 -07001343 alloc_flags)) {
1344 *candidate_zone = zone;
1345 /*
1346 * We think the allocation will succeed in this zone,
1347 * but it is not certain, hence the false. The caller
1348 * will repeat this with true if allocation indeed
1349 * succeeds in this zone.
1350 */
1351 compaction_defer_reset(zone, order, false);
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001352 /*
1353 * It is possible that async compaction aborted due to
1354 * need_resched() and the watermarks were ok thanks to
1355 * somebody else freeing memory. The allocation can
1356 * however still fail so we better signal the
1357 * need_resched() contention anyway (this will not
1358 * prevent the allocation attempt).
1359 */
1360 if (zone_contended == COMPACT_CONTENDED_SCHED)
1361 *contended = COMPACT_CONTENDED_SCHED;
1362
1363 goto break_loop;
1364 }
1365
1366 if (mode != MIGRATE_ASYNC) {
Vlastimil Babka53853e22014-10-09 15:27:02 -07001367 /*
1368 * We think that allocation won't succeed in this zone
1369 * so we defer compaction there. If it ends up
1370 * succeeding after all, it will be reset.
1371 */
1372 defer_compaction(zone, order);
1373 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001374
1375 /*
1376 * We might have stopped compacting due to need_resched() in
1377 * async compaction, or due to a fatal signal detected. In that
1378 * case do not try further zones and signal need_resched()
1379 * contention.
1380 */
1381 if ((zone_contended == COMPACT_CONTENDED_SCHED)
1382 || fatal_signal_pending(current)) {
1383 *contended = COMPACT_CONTENDED_SCHED;
1384 goto break_loop;
1385 }
1386
1387 continue;
1388break_loop:
1389 /*
1390 * We might not have tried all the zones, so be conservative
1391 * and assume they are not all lock contended.
1392 */
1393 all_zones_contended = 0;
1394 break;
Mel Gorman56de7262010-05-24 14:32:30 -07001395 }
1396
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001397 /*
1398 * If at least one zone wasn't deferred or skipped, we report if all
1399 * zones that were tried were lock contended.
1400 */
1401 if (rc > COMPACT_SKIPPED && all_zones_contended)
1402 *contended = COMPACT_CONTENDED_LOCK;
1403
Mel Gorman56de7262010-05-24 14:32:30 -07001404 return rc;
1405}
1406
1407
Mel Gorman76ab0f52010-05-24 14:32:28 -07001408/* Compact all zones within a node */
Andrew Morton7103f162013-02-22 16:32:33 -08001409static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001410{
1411 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -07001412 struct zone *zone;
1413
Mel Gorman76ab0f52010-05-24 14:32:28 -07001414 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -07001415
1416 zone = &pgdat->node_zones[zoneid];
1417 if (!populated_zone(zone))
1418 continue;
1419
Rik van Riel7be62de2012-03-21 16:33:52 -07001420 cc->nr_freepages = 0;
1421 cc->nr_migratepages = 0;
1422 cc->zone = zone;
1423 INIT_LIST_HEAD(&cc->freepages);
1424 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001425
Dan Carpenteraad6ec32012-03-21 16:33:54 -07001426 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -07001427 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001428
Rik van Rielaff62242012-03-21 16:33:52 -07001429 if (cc->order > 0) {
Vlastimil Babkade6c60a2014-01-21 15:51:07 -08001430 if (zone_watermark_ok(zone, cc->order,
1431 low_wmark_pages(zone), 0, 0))
1432 compaction_defer_reset(zone, cc->order, false);
Rik van Rielaff62242012-03-21 16:33:52 -07001433 }
1434
Rik van Riel7be62de2012-03-21 16:33:52 -07001435 VM_BUG_ON(!list_empty(&cc->freepages));
1436 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -07001437 }
Mel Gorman76ab0f52010-05-24 14:32:28 -07001438}
1439
Andrew Morton7103f162013-02-22 16:32:33 -08001440void compact_pgdat(pg_data_t *pgdat, int order)
Rik van Riel7be62de2012-03-21 16:33:52 -07001441{
1442 struct compact_control cc = {
1443 .order = order,
David Rientjese0b9dae2014-06-04 16:08:28 -07001444 .mode = MIGRATE_ASYNC,
Rik van Riel7be62de2012-03-21 16:33:52 -07001445 };
1446
Mel Gorman3a7200a2013-09-11 14:22:19 -07001447 if (!order)
1448 return;
1449
Andrew Morton7103f162013-02-22 16:32:33 -08001450 __compact_pgdat(pgdat, &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001451}
1452
Andrew Morton7103f162013-02-22 16:32:33 -08001453static void compact_node(int nid)
Rik van Riel7be62de2012-03-21 16:33:52 -07001454{
Rik van Riel7be62de2012-03-21 16:33:52 -07001455 struct compact_control cc = {
1456 .order = -1,
David Rientjese0b9dae2014-06-04 16:08:28 -07001457 .mode = MIGRATE_SYNC,
David Rientjes91ca9182014-04-03 14:47:23 -07001458 .ignore_skip_hint = true,
Rik van Riel7be62de2012-03-21 16:33:52 -07001459 };
1460
Andrew Morton7103f162013-02-22 16:32:33 -08001461 __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001462}
1463
Mel Gorman76ab0f52010-05-24 14:32:28 -07001464/* Compact all nodes in the system */
Jason Liu7964c062013-01-11 14:31:47 -08001465static void compact_nodes(void)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001466{
1467 int nid;
1468
Hugh Dickins8575ec22012-03-21 16:33:53 -07001469 /* Flush pending updates to the LRU lists */
1470 lru_add_drain_all();
1471
Mel Gorman76ab0f52010-05-24 14:32:28 -07001472 for_each_online_node(nid)
1473 compact_node(nid);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001474}
1475
1476/* The written value is actually unused, all memory is compacted */
1477int sysctl_compact_memory;
1478
1479/* This is the entry point for compacting all nodes via /proc/sys/vm */
1480int sysctl_compaction_handler(struct ctl_table *table, int write,
1481 void __user *buffer, size_t *length, loff_t *ppos)
1482{
1483 if (write)
Jason Liu7964c062013-01-11 14:31:47 -08001484 compact_nodes();
Mel Gorman76ab0f52010-05-24 14:32:28 -07001485
1486 return 0;
1487}
Mel Gormaned4a6d72010-05-24 14:32:29 -07001488
Mel Gorman5e771902010-05-24 14:32:31 -07001489int sysctl_extfrag_handler(struct ctl_table *table, int write,
1490 void __user *buffer, size_t *length, loff_t *ppos)
1491{
1492 proc_dointvec_minmax(table, write, buffer, length, ppos);
1493
1494 return 0;
1495}
1496
Mel Gormaned4a6d72010-05-24 14:32:29 -07001497#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Rashika Kheria74e77fb2014-04-03 14:48:01 -07001498static ssize_t sysfs_compact_node(struct device *dev,
Kay Sievers10fbcf42011-12-21 14:48:43 -08001499 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -07001500 const char *buf, size_t count)
1501{
Hugh Dickins8575ec22012-03-21 16:33:53 -07001502 int nid = dev->id;
1503
1504 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1505 /* Flush pending updates to the LRU lists */
1506 lru_add_drain_all();
1507
1508 compact_node(nid);
1509 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07001510
1511 return count;
1512}
Kay Sievers10fbcf42011-12-21 14:48:43 -08001513static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001514
1515int compaction_register_node(struct node *node)
1516{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001517 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001518}
1519
1520void compaction_unregister_node(struct node *node)
1521{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001522 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001523}
1524#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001525
1526#endif /* CONFIG_COMPACTION */