blob: edba18aed1738c752793f63ae409c22ae373342a [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
Vlastimil Babka7d49d882014-10-09 15:27:11 -070070/*
71 * Check that the whole (or subset of) a pageblock given by the interval of
72 * [start_pfn, end_pfn) is valid and within the same zone, before scanning it
73 * with the migration of free compaction scanner. The scanners then need to
74 * use only pfn_valid_within() check for arches that allow holes within
75 * pageblocks.
76 *
77 * Return struct page pointer of start_pfn, or NULL if checks were not passed.
78 *
79 * It's possible on some configurations to have a setup like node0 node1 node0
80 * i.e. it's possible that all pages within a zones range of pages do not
81 * belong to a single zone. We assume that a border between node0 and node1
82 * can occur within a single pageblock, but not a node0 node1 node0
83 * interleaving within a single pageblock. It is therefore sufficient to check
84 * the first and last page of a pageblock and avoid checking each individual
85 * page in a pageblock.
86 */
87static struct page *pageblock_pfn_to_page(unsigned long start_pfn,
88 unsigned long end_pfn, struct zone *zone)
89{
90 struct page *start_page;
91 struct page *end_page;
92
93 /* end_pfn is one past the range we are checking */
94 end_pfn--;
95
96 if (!pfn_valid(start_pfn) || !pfn_valid(end_pfn))
97 return NULL;
98
99 start_page = pfn_to_page(start_pfn);
100
101 if (page_zone(start_page) != zone)
102 return NULL;
103
104 end_page = pfn_to_page(end_pfn);
105
106 /* This gives a shorter code than deriving page_zone(end_page) */
107 if (page_zone_id(start_page) != page_zone_id(end_page))
108 return NULL;
109
110 return start_page;
111}
112
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700113#ifdef CONFIG_COMPACTION
114/* Returns true if the pageblock should be scanned for pages to isolate. */
115static inline bool isolation_suitable(struct compact_control *cc,
116 struct page *page)
117{
118 if (cc->ignore_skip_hint)
119 return true;
120
121 return !get_pageblock_skip(page);
122}
123
124/*
125 * This function is called to clear all cached information on pageblocks that
126 * should be skipped for page isolation when the migrate and free page scanner
127 * meet.
128 */
Mel Gorman62997022012-10-08 16:32:47 -0700129static void __reset_isolation_suitable(struct zone *zone)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700130{
131 unsigned long start_pfn = zone->zone_start_pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -0800132 unsigned long end_pfn = zone_end_pfn(zone);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700133 unsigned long pfn;
134
David Rientjes35979ef2014-06-04 16:08:27 -0700135 zone->compact_cached_migrate_pfn[0] = start_pfn;
136 zone->compact_cached_migrate_pfn[1] = start_pfn;
Mel Gormanc89511a2012-10-08 16:32:45 -0700137 zone->compact_cached_free_pfn = end_pfn;
Mel Gorman62997022012-10-08 16:32:47 -0700138 zone->compact_blockskip_flush = false;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700139
140 /* Walk the zone and mark every pageblock as suitable for isolation */
141 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
142 struct page *page;
143
144 cond_resched();
145
146 if (!pfn_valid(pfn))
147 continue;
148
149 page = pfn_to_page(pfn);
150 if (zone != page_zone(page))
151 continue;
152
153 clear_pageblock_skip(page);
154 }
155}
156
Mel Gorman62997022012-10-08 16:32:47 -0700157void reset_isolation_suitable(pg_data_t *pgdat)
158{
159 int zoneid;
160
161 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
162 struct zone *zone = &pgdat->node_zones[zoneid];
163 if (!populated_zone(zone))
164 continue;
165
166 /* Only flush if a full compaction finished recently */
167 if (zone->compact_blockskip_flush)
168 __reset_isolation_suitable(zone);
169 }
170}
171
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700172/*
173 * If no pages were isolated then mark this pageblock to be skipped in the
Mel Gorman62997022012-10-08 16:32:47 -0700174 * future. The information is later cleared by __reset_isolation_suitable().
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700175 */
Mel Gormanc89511a2012-10-08 16:32:45 -0700176static void update_pageblock_skip(struct compact_control *cc,
177 struct page *page, unsigned long nr_isolated,
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700178 bool migrate_scanner)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700179{
Mel Gormanc89511a2012-10-08 16:32:45 -0700180 struct zone *zone = cc->zone;
David Rientjes35979ef2014-06-04 16:08:27 -0700181 unsigned long pfn;
Joonsoo Kim6815bf32013-12-18 17:08:52 -0800182
183 if (cc->ignore_skip_hint)
184 return;
185
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700186 if (!page)
187 return;
188
David Rientjes35979ef2014-06-04 16:08:27 -0700189 if (nr_isolated)
190 return;
191
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700192 set_pageblock_skip(page);
Mel Gormanc89511a2012-10-08 16:32:45 -0700193
David Rientjes35979ef2014-06-04 16:08:27 -0700194 pfn = page_to_pfn(page);
195
196 /* Update where async and sync compaction should restart */
197 if (migrate_scanner) {
198 if (cc->finished_update_migrate)
199 return;
200 if (pfn > zone->compact_cached_migrate_pfn[0])
201 zone->compact_cached_migrate_pfn[0] = pfn;
David Rientjese0b9dae2014-06-04 16:08:28 -0700202 if (cc->mode != MIGRATE_ASYNC &&
203 pfn > zone->compact_cached_migrate_pfn[1])
David Rientjes35979ef2014-06-04 16:08:27 -0700204 zone->compact_cached_migrate_pfn[1] = pfn;
205 } else {
206 if (cc->finished_update_free)
207 return;
208 if (pfn < zone->compact_cached_free_pfn)
209 zone->compact_cached_free_pfn = pfn;
Mel Gormanc89511a2012-10-08 16:32:45 -0700210 }
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700211}
212#else
213static inline bool isolation_suitable(struct compact_control *cc,
214 struct page *page)
215{
216 return true;
217}
218
Mel Gormanc89511a2012-10-08 16:32:45 -0700219static void update_pageblock_skip(struct compact_control *cc,
220 struct page *page, unsigned long nr_isolated,
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700221 bool migrate_scanner)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700222{
223}
224#endif /* CONFIG_COMPACTION */
225
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700226/*
227 * Compaction requires the taking of some coarse locks that are potentially
228 * very heavily contended. For async compaction, back out if the lock cannot
229 * be taken immediately. For sync compaction, spin on the lock if needed.
230 *
231 * Returns true if the lock is held
232 * Returns false if the lock is not held and compaction should abort
233 */
234static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags,
235 struct compact_control *cc)
Mel Gorman2a1402a2012-10-08 16:32:33 -0700236{
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700237 if (cc->mode == MIGRATE_ASYNC) {
238 if (!spin_trylock_irqsave(lock, *flags)) {
239 cc->contended = COMPACT_CONTENDED_LOCK;
240 return false;
241 }
242 } else {
243 spin_lock_irqsave(lock, *flags);
244 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700245
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700246 return true;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700247}
248
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100249/*
Mel Gormanc67fe372012-08-21 16:16:17 -0700250 * Compaction requires the taking of some coarse locks that are potentially
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700251 * very heavily contended. The lock should be periodically unlocked to avoid
252 * having disabled IRQs for a long time, even when there is nobody waiting on
253 * the lock. It might also be that allowing the IRQs will result in
254 * need_resched() becoming true. If scheduling is needed, async compaction
255 * aborts. Sync compaction schedules.
256 * Either compaction type will also abort if a fatal signal is pending.
257 * In either case if the lock was locked, it is dropped and not regained.
Mel Gormanc67fe372012-08-21 16:16:17 -0700258 *
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700259 * Returns true if compaction should abort due to fatal signal pending, or
260 * async compaction due to need_resched()
261 * Returns false when compaction can continue (sync compaction might have
262 * scheduled)
Mel Gormanc67fe372012-08-21 16:16:17 -0700263 */
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700264static bool compact_unlock_should_abort(spinlock_t *lock,
265 unsigned long flags, bool *locked, struct compact_control *cc)
Mel Gormanc67fe372012-08-21 16:16:17 -0700266{
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700267 if (*locked) {
268 spin_unlock_irqrestore(lock, flags);
269 *locked = false;
270 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700271
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700272 if (fatal_signal_pending(current)) {
273 cc->contended = COMPACT_CONTENDED_SCHED;
274 return true;
275 }
Mel Gormanc67fe372012-08-21 16:16:17 -0700276
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700277 if (need_resched()) {
David Rientjese0b9dae2014-06-04 16:08:28 -0700278 if (cc->mode == MIGRATE_ASYNC) {
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700279 cc->contended = COMPACT_CONTENDED_SCHED;
280 return true;
Mel Gormanc67fe372012-08-21 16:16:17 -0700281 }
Mel Gormanc67fe372012-08-21 16:16:17 -0700282 cond_resched();
Mel Gormanc67fe372012-08-21 16:16:17 -0700283 }
284
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700285 return false;
Mel Gormanc67fe372012-08-21 16:16:17 -0700286}
287
Vlastimil Babkabe976572014-06-04 16:10:41 -0700288/*
289 * Aside from avoiding lock contention, compaction also periodically checks
290 * need_resched() and either schedules in sync compaction or aborts async
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700291 * compaction. This is similar to what compact_unlock_should_abort() does, but
Vlastimil Babkabe976572014-06-04 16:10:41 -0700292 * is used where no lock is concerned.
293 *
294 * Returns false when no scheduling was needed, or sync compaction scheduled.
295 * Returns true when async compaction should abort.
296 */
297static inline bool compact_should_abort(struct compact_control *cc)
298{
299 /* async compaction aborts if contended */
300 if (need_resched()) {
301 if (cc->mode == MIGRATE_ASYNC) {
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700302 cc->contended = COMPACT_CONTENDED_SCHED;
Vlastimil Babkabe976572014-06-04 16:10:41 -0700303 return true;
304 }
305
306 cond_resched();
307 }
308
309 return false;
310}
311
Mel Gormanf40d1e42012-10-08 16:32:36 -0700312/* Returns true if the page is within a block suitable for migration to */
313static bool suitable_migration_target(struct page *page)
314{
Joonsoo Kim7d348b92014-04-07 15:37:03 -0700315 /* If the page is a large free page, then disallow migration */
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700316 if (PageBuddy(page)) {
317 /*
318 * We are checking page_order without zone->lock taken. But
319 * the only small danger is that we skip a potentially suitable
320 * pageblock, so it's not worth to check order for valid range.
321 */
322 if (page_order_unsafe(page) >= pageblock_order)
323 return false;
324 }
Mel Gormanf40d1e42012-10-08 16:32:36 -0700325
326 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
Joonsoo Kim7d348b92014-04-07 15:37:03 -0700327 if (migrate_async_suitable(get_pageblock_migratetype(page)))
Mel Gormanf40d1e42012-10-08 16:32:36 -0700328 return true;
329
330 /* Otherwise skip the block */
331 return false;
332}
333
Mel Gormanc67fe372012-08-21 16:16:17 -0700334/*
Jerome Marchand9e4be472013-11-12 15:07:12 -0800335 * Isolate free pages onto a private freelist. If @strict is true, will abort
336 * returning 0 on any invalid PFNs or non-free pages inside of the pageblock
337 * (even though it may still end up isolating some pages).
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100338 */
Mel Gormanf40d1e42012-10-08 16:32:36 -0700339static unsigned long isolate_freepages_block(struct compact_control *cc,
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700340 unsigned long *start_pfn,
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100341 unsigned long end_pfn,
342 struct list_head *freelist,
343 bool strict)
Mel Gorman748446b2010-05-24 14:32:27 -0700344{
Mel Gormanb7aba692011-01-13 15:45:54 -0800345 int nr_scanned = 0, total_isolated = 0;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700346 struct page *cursor, *valid_page = NULL;
Xiubo Lib8b2d822014-10-09 15:28:21 -0700347 unsigned long flags = 0;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700348 bool locked = false;
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700349 unsigned long blockpfn = *start_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700350
Mel Gorman748446b2010-05-24 14:32:27 -0700351 cursor = pfn_to_page(blockpfn);
352
Mel Gormanf40d1e42012-10-08 16:32:36 -0700353 /* Isolate free pages. */
Mel Gorman748446b2010-05-24 14:32:27 -0700354 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
355 int isolated, i;
356 struct page *page = cursor;
357
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700358 /*
359 * Periodically drop the lock (if held) regardless of its
360 * contention, to give chance to IRQs. Abort if fatal signal
361 * pending or async compaction detects need_resched()
362 */
363 if (!(blockpfn % SWAP_CLUSTER_MAX)
364 && compact_unlock_should_abort(&cc->zone->lock, flags,
365 &locked, cc))
366 break;
367
Mel Gormanb7aba692011-01-13 15:45:54 -0800368 nr_scanned++;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700369 if (!pfn_valid_within(blockpfn))
Laura Abbott2af120b2014-03-10 15:49:44 -0700370 goto isolate_fail;
371
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700372 if (!valid_page)
373 valid_page = page;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700374 if (!PageBuddy(page))
Laura Abbott2af120b2014-03-10 15:49:44 -0700375 goto isolate_fail;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700376
377 /*
Vlastimil Babka69b71892014-10-09 15:27:18 -0700378 * If we already hold the lock, we can skip some rechecking.
379 * Note that if we hold the lock now, checked_pageblock was
380 * already set in some previous iteration (or strict is true),
381 * so it is correct to skip the suitable migration target
382 * recheck as well.
Mel Gormanf40d1e42012-10-08 16:32:36 -0700383 */
Vlastimil Babka69b71892014-10-09 15:27:18 -0700384 if (!locked) {
385 /*
386 * The zone lock must be held to isolate freepages.
387 * Unfortunately this is a very coarse lock and can be
388 * heavily contended if there are parallel allocations
389 * or parallel compactions. For async compaction do not
390 * spin on the lock and we acquire the lock as late as
391 * possible.
392 */
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700393 locked = compact_trylock_irqsave(&cc->zone->lock,
394 &flags, cc);
Vlastimil Babka69b71892014-10-09 15:27:18 -0700395 if (!locked)
396 break;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700397
Vlastimil Babka69b71892014-10-09 15:27:18 -0700398 /* Recheck this is a buddy page under lock */
399 if (!PageBuddy(page))
400 goto isolate_fail;
401 }
Mel Gorman748446b2010-05-24 14:32:27 -0700402
403 /* Found a free page, break it into order-0 pages */
404 isolated = split_free_page(page);
405 total_isolated += isolated;
406 for (i = 0; i < isolated; i++) {
407 list_add(&page->lru, freelist);
408 page++;
409 }
410
411 /* If a page was split, advance to the end of it */
412 if (isolated) {
413 blockpfn += isolated - 1;
414 cursor += isolated - 1;
Laura Abbott2af120b2014-03-10 15:49:44 -0700415 continue;
Mel Gorman748446b2010-05-24 14:32:27 -0700416 }
Laura Abbott2af120b2014-03-10 15:49:44 -0700417
418isolate_fail:
419 if (strict)
420 break;
421 else
422 continue;
423
Mel Gorman748446b2010-05-24 14:32:27 -0700424 }
425
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700426 /* Record how far we have got within the block */
427 *start_pfn = blockpfn;
428
Mel Gormanb7aba692011-01-13 15:45:54 -0800429 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
Mel Gormanf40d1e42012-10-08 16:32:36 -0700430
431 /*
432 * If strict isolation is requested by CMA then check that all the
433 * pages requested were isolated. If there were any failures, 0 is
434 * returned and CMA will fail.
435 */
Laura Abbott2af120b2014-03-10 15:49:44 -0700436 if (strict && blockpfn < end_pfn)
Mel Gormanf40d1e42012-10-08 16:32:36 -0700437 total_isolated = 0;
438
439 if (locked)
440 spin_unlock_irqrestore(&cc->zone->lock, flags);
441
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700442 /* Update the pageblock-skip if the whole pageblock was scanned */
443 if (blockpfn == end_pfn)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700444 update_pageblock_skip(cc, valid_page, total_isolated, false);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700445
Minchan Kim010fc292012-12-20 15:05:06 -0800446 count_compact_events(COMPACTFREE_SCANNED, nr_scanned);
Mel Gorman397487d2012-10-19 12:00:10 +0100447 if (total_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -0800448 count_compact_events(COMPACTISOLATED, total_isolated);
Mel Gorman748446b2010-05-24 14:32:27 -0700449 return total_isolated;
450}
451
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100452/**
453 * isolate_freepages_range() - isolate free pages.
454 * @start_pfn: The first PFN to start isolating.
455 * @end_pfn: The one-past-last PFN.
456 *
457 * Non-free pages, invalid PFNs, or zone boundaries within the
458 * [start_pfn, end_pfn) range are considered errors, cause function to
459 * undo its actions and return zero.
460 *
461 * Otherwise, function returns one-past-the-last PFN of isolated page
462 * (which may be greater then end_pfn if end fell in a middle of
463 * a free page).
464 */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100465unsigned long
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700466isolate_freepages_range(struct compact_control *cc,
467 unsigned long start_pfn, unsigned long end_pfn)
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100468{
Mel Gormanf40d1e42012-10-08 16:32:36 -0700469 unsigned long isolated, pfn, block_end_pfn;
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100470 LIST_HEAD(freelist);
471
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700472 pfn = start_pfn;
473 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100474
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700475 for (; pfn < end_pfn; pfn += isolated,
476 block_end_pfn += pageblock_nr_pages) {
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700477 /* Protect pfn from changing by isolate_freepages_block */
478 unsigned long isolate_start_pfn = pfn;
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700479
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100480 block_end_pfn = min(block_end_pfn, end_pfn);
481
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700482 if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
483 break;
484
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700485 isolated = isolate_freepages_block(cc, &isolate_start_pfn,
486 block_end_pfn, &freelist, true);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100487
488 /*
489 * In strict mode, isolate_freepages_block() returns 0 if
490 * there are any holes in the block (ie. invalid PFNs or
491 * non-free pages).
492 */
493 if (!isolated)
494 break;
495
496 /*
497 * If we managed to isolate pages, it is always (1 << n) *
498 * pageblock_nr_pages for some non-negative n. (Max order
499 * page may span two pageblocks).
500 */
501 }
502
503 /* split_free_page does not map the pages */
504 map_pages(&freelist);
505
506 if (pfn < end_pfn) {
507 /* Loop terminated early, cleanup. */
508 release_freepages(&freelist);
509 return 0;
510 }
511
512 /* We don't use freelists for anything. */
513 return pfn;
514}
515
Mel Gorman748446b2010-05-24 14:32:27 -0700516/* Update the number of anon and file isolated pages in the zone */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700517static void acct_isolated(struct zone *zone, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700518{
519 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700520 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700521
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700522 if (list_empty(&cc->migratepages))
523 return;
524
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700525 list_for_each_entry(page, &cc->migratepages, lru)
526 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700527
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700528 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
529 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
Mel Gorman748446b2010-05-24 14:32:27 -0700530}
531
532/* Similar to reclaim, but different enough that they don't share logic */
533static bool too_many_isolated(struct zone *zone)
534{
Minchan Kimbc693042010-09-09 16:38:00 -0700535 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700536
537 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
538 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700539 active = zone_page_state(zone, NR_ACTIVE_FILE) +
540 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700541 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
542 zone_page_state(zone, NR_ISOLATED_ANON);
543
Minchan Kimbc693042010-09-09 16:38:00 -0700544 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700545}
546
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100547/**
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700548 * isolate_migratepages_block() - isolate all migrate-able pages within
549 * a single pageblock
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100550 * @cc: Compaction control structure.
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700551 * @low_pfn: The first PFN to isolate
552 * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock
553 * @isolate_mode: Isolation mode to be used.
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100554 *
555 * Isolate all pages that can be migrated from the range specified by
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700556 * [low_pfn, end_pfn). The range is expected to be within same pageblock.
557 * Returns zero if there is a fatal signal pending, otherwise PFN of the
558 * first page that was not scanned (which may be both less, equal to or more
559 * than end_pfn).
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100560 *
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700561 * The pages are isolated on cc->migratepages list (not required to be empty),
562 * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field
563 * is neither read nor updated.
Mel Gorman748446b2010-05-24 14:32:27 -0700564 */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700565static unsigned long
566isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
567 unsigned long end_pfn, isolate_mode_t isolate_mode)
Mel Gorman748446b2010-05-24 14:32:27 -0700568{
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700569 struct zone *zone = cc->zone;
Mel Gormanb7aba692011-01-13 15:45:54 -0800570 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700571 struct list_head *migratelist = &cc->migratepages;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700572 struct lruvec *lruvec;
Xiubo Lib8b2d822014-10-09 15:28:21 -0700573 unsigned long flags = 0;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700574 bool locked = false;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700575 struct page *page = NULL, *valid_page = NULL;
Mel Gorman748446b2010-05-24 14:32:27 -0700576
Mel Gorman748446b2010-05-24 14:32:27 -0700577 /*
578 * Ensure that there are not too many pages isolated from the LRU
579 * list by either parallel reclaimers or compaction. If there are,
580 * delay for some time until fewer pages are isolated
581 */
582 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700583 /* async migration should just abort */
David Rientjese0b9dae2014-06-04 16:08:28 -0700584 if (cc->mode == MIGRATE_ASYNC)
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100585 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700586
Mel Gorman748446b2010-05-24 14:32:27 -0700587 congestion_wait(BLK_RW_ASYNC, HZ/10);
588
589 if (fatal_signal_pending(current))
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100590 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700591 }
592
Vlastimil Babkabe976572014-06-04 16:10:41 -0700593 if (compact_should_abort(cc))
594 return 0;
David Rientjesaeef4b82014-06-04 16:08:31 -0700595
Mel Gorman748446b2010-05-24 14:32:27 -0700596 /* Time to isolate some pages for migration */
Mel Gorman748446b2010-05-24 14:32:27 -0700597 for (; low_pfn < end_pfn; low_pfn++) {
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700598 /*
599 * Periodically drop the lock (if held) regardless of its
600 * contention, to give chance to IRQs. Abort async compaction
601 * if contended.
602 */
603 if (!(low_pfn % SWAP_CLUSTER_MAX)
604 && compact_unlock_should_abort(&zone->lru_lock, flags,
605 &locked, cc))
606 break;
Mel Gormanc67fe372012-08-21 16:16:17 -0700607
Mel Gorman748446b2010-05-24 14:32:27 -0700608 if (!pfn_valid_within(low_pfn))
609 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800610 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700611
Mel Gorman748446b2010-05-24 14:32:27 -0700612 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800613
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700614 if (!valid_page)
615 valid_page = page;
616
Mel Gorman6c144662014-01-23 15:53:38 -0800617 /*
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700618 * Skip if free. We read page order here without zone lock
619 * which is generally unsafe, but the race window is small and
620 * the worst thing that can happen is that we skip some
621 * potential isolation targets.
Mel Gorman6c144662014-01-23 15:53:38 -0800622 */
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700623 if (PageBuddy(page)) {
624 unsigned long freepage_order = page_order_unsafe(page);
625
626 /*
627 * Without lock, we cannot be sure that what we got is
628 * a valid page order. Consider only values in the
629 * valid order range to prevent low_pfn overflow.
630 */
631 if (freepage_order > 0 && freepage_order < MAX_ORDER)
632 low_pfn += (1UL << freepage_order) - 1;
Mel Gorman748446b2010-05-24 14:32:27 -0700633 continue;
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700634 }
Mel Gorman748446b2010-05-24 14:32:27 -0700635
Mel Gorman9927af742011-01-13 15:45:59 -0800636 /*
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800637 * Check may be lockless but that's ok as we recheck later.
638 * It's possible to migrate LRU pages and balloon pages
639 * Skip any other type of page
640 */
641 if (!PageLRU(page)) {
642 if (unlikely(balloon_page_movable(page))) {
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700643 if (balloon_page_isolate(page)) {
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800644 /* Successfully isolated */
Joonsoo Kimb6c75012014-04-07 15:37:07 -0700645 goto isolate_success;
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800646 }
647 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800648 continue;
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800649 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800650
651 /*
Mel Gorman2a1402a2012-10-08 16:32:33 -0700652 * PageLRU is set. lru_lock normally excludes isolation
653 * splitting and collapsing (collapsing has already happened
654 * if PageLRU is set) but the lock is not necessarily taken
655 * here and it is wasteful to take it just to check transhuge.
656 * Check TransHuge without lock and skip the whole pageblock if
657 * it's either a transhuge or hugetlbfs page, as calling
658 * compound_order() without preventing THP from splitting the
659 * page underneath us may return surprising results.
Andrea Arcangelibc835012011-01-13 15:47:08 -0800660 */
661 if (PageTransHuge(page)) {
Mel Gorman2a1402a2012-10-08 16:32:33 -0700662 if (!locked)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700663 low_pfn = ALIGN(low_pfn + 1,
664 pageblock_nr_pages) - 1;
665 else
666 low_pfn += (1 << compound_order(page)) - 1;
667
Mel Gorman2a1402a2012-10-08 16:32:33 -0700668 continue;
669 }
670
David Rientjes119d6d52014-04-03 14:48:00 -0700671 /*
672 * Migration will fail if an anonymous page is pinned in memory,
673 * so avoid taking lru_lock and isolating it unnecessarily in an
674 * admittedly racy check.
675 */
676 if (!page_mapping(page) &&
677 page_count(page) > page_mapcount(page))
678 continue;
679
Vlastimil Babka69b71892014-10-09 15:27:18 -0700680 /* If we already hold the lock, we can skip some rechecking */
681 if (!locked) {
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700682 locked = compact_trylock_irqsave(&zone->lru_lock,
683 &flags, cc);
Vlastimil Babka69b71892014-10-09 15:27:18 -0700684 if (!locked)
685 break;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700686
Vlastimil Babka69b71892014-10-09 15:27:18 -0700687 /* Recheck PageLRU and PageTransHuge under lock */
688 if (!PageLRU(page))
689 continue;
690 if (PageTransHuge(page)) {
691 low_pfn += (1 << compound_order(page)) - 1;
692 continue;
693 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800694 }
695
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700696 lruvec = mem_cgroup_page_lruvec(page, zone);
697
Mel Gorman748446b2010-05-24 14:32:27 -0700698 /* Try isolate the page */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700699 if (__isolate_lru_page(page, isolate_mode) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700700 continue;
701
Sasha Levin309381fea2014-01-23 15:52:54 -0800702 VM_BUG_ON_PAGE(PageTransCompound(page), page);
Andrea Arcangelibc835012011-01-13 15:47:08 -0800703
Mel Gorman748446b2010-05-24 14:32:27 -0700704 /* Successfully isolated */
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700705 del_page_from_lru_list(page, lruvec, page_lru(page));
Joonsoo Kimb6c75012014-04-07 15:37:07 -0700706
707isolate_success:
708 cc->finished_update_migrate = true;
Mel Gorman748446b2010-05-24 14:32:27 -0700709 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700710 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800711 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700712
713 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800714 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
715 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700716 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800717 }
Mel Gorman748446b2010-05-24 14:32:27 -0700718 }
719
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700720 /*
721 * The PageBuddy() check could have potentially brought us outside
722 * the range to be scanned.
723 */
724 if (unlikely(low_pfn > end_pfn))
725 low_pfn = end_pfn;
726
Mel Gormanc67fe372012-08-21 16:16:17 -0700727 if (locked)
728 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700729
Vlastimil Babka50b5b092014-01-21 15:51:10 -0800730 /*
731 * Update the pageblock-skip information and cached scanner pfn,
732 * if the whole pageblock was scanned without isolating any page.
Vlastimil Babka50b5b092014-01-21 15:51:10 -0800733 */
David Rientjes35979ef2014-06-04 16:08:27 -0700734 if (low_pfn == end_pfn)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700735 update_pageblock_skip(cc, valid_page, nr_isolated, true);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700736
Mel Gormanb7aba692011-01-13 15:45:54 -0800737 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
738
Minchan Kim010fc292012-12-20 15:05:06 -0800739 count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned);
Mel Gorman397487d2012-10-19 12:00:10 +0100740 if (nr_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -0800741 count_compact_events(COMPACTISOLATED, nr_isolated);
Mel Gorman397487d2012-10-19 12:00:10 +0100742
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100743 return low_pfn;
744}
745
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700746/**
747 * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
748 * @cc: Compaction control structure.
749 * @start_pfn: The first PFN to start isolating.
750 * @end_pfn: The one-past-last PFN.
751 *
752 * Returns zero if isolation fails fatally due to e.g. pending signal.
753 * Otherwise, function returns one-past-the-last PFN of isolated page
754 * (which may be greater than end_pfn if end fell in a middle of a THP page).
755 */
756unsigned long
757isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
758 unsigned long end_pfn)
759{
760 unsigned long pfn, block_end_pfn;
761
762 /* Scan block by block. First and last block may be incomplete */
763 pfn = start_pfn;
764 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
765
766 for (; pfn < end_pfn; pfn = block_end_pfn,
767 block_end_pfn += pageblock_nr_pages) {
768
769 block_end_pfn = min(block_end_pfn, end_pfn);
770
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700771 if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700772 continue;
773
774 pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
775 ISOLATE_UNEVICTABLE);
776
777 /*
778 * In case of fatal failure, release everything that might
779 * have been isolated in the previous iteration, and signal
780 * the failure back to caller.
781 */
782 if (!pfn) {
783 putback_movable_pages(&cc->migratepages);
784 cc->nr_migratepages = 0;
785 break;
786 }
787 }
788 acct_isolated(cc->zone, cc);
789
790 return pfn;
791}
792
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100793#endif /* CONFIG_COMPACTION || CONFIG_CMA */
794#ifdef CONFIG_COMPACTION
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100795/*
796 * Based on information in the current compact_control, find blocks
797 * suitable for isolating free pages from and then isolate them.
798 */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700799static void isolate_freepages(struct compact_control *cc)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100800{
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700801 struct zone *zone = cc->zone;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100802 struct page *page;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700803 unsigned long block_start_pfn; /* start of current pageblock */
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700804 unsigned long isolate_start_pfn; /* exact pfn we start at */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700805 unsigned long block_end_pfn; /* end of current pageblock */
806 unsigned long low_pfn; /* lowest pfn scanner is able to scan */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100807 int nr_freepages = cc->nr_freepages;
808 struct list_head *freelist = &cc->freepages;
809
810 /*
811 * Initialise the free scanner. The starting point is where we last
Vlastimil Babka49e068f2014-05-06 12:50:03 -0700812 * successfully isolated from, zone-cached value, or the end of the
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700813 * zone when isolating for the first time. For looping we also need
814 * this pfn aligned down to the pageblock boundary, because we do
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700815 * block_start_pfn -= pageblock_nr_pages in the for loop.
816 * For ending point, take care when isolating in last pageblock of a
817 * a zone which ends in the middle of a pageblock.
Vlastimil Babka49e068f2014-05-06 12:50:03 -0700818 * The low boundary is the end of the pageblock the migration scanner
819 * is using.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100820 */
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700821 isolate_start_pfn = cc->free_pfn;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700822 block_start_pfn = cc->free_pfn & ~(pageblock_nr_pages-1);
823 block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
824 zone_end_pfn(zone));
Vlastimil Babka7ed695e2014-01-21 15:51:09 -0800825 low_pfn = ALIGN(cc->migrate_pfn + 1, pageblock_nr_pages);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100826
827 /*
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100828 * Isolate free pages until enough are available to migrate the
829 * pages on cc->migratepages. We stop searching if the migrate
830 * and free page scanners meet or enough free pages are isolated.
831 */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700832 for (; block_start_pfn >= low_pfn && cc->nr_migratepages > nr_freepages;
833 block_end_pfn = block_start_pfn,
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700834 block_start_pfn -= pageblock_nr_pages,
835 isolate_start_pfn = block_start_pfn) {
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100836 unsigned long isolated;
837
David Rientjesf6ea3ad2013-09-30 13:45:03 -0700838 /*
839 * This can iterate a massively long zone without finding any
840 * suitable migration targets, so periodically check if we need
Vlastimil Babkabe976572014-06-04 16:10:41 -0700841 * to schedule, or even abort async compaction.
David Rientjesf6ea3ad2013-09-30 13:45:03 -0700842 */
Vlastimil Babkabe976572014-06-04 16:10:41 -0700843 if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
844 && compact_should_abort(cc))
845 break;
David Rientjesf6ea3ad2013-09-30 13:45:03 -0700846
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700847 page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
848 zone);
849 if (!page)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100850 continue;
851
852 /* Check the block is suitable for migration */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700853 if (!suitable_migration_target(page))
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100854 continue;
Linus Torvalds68e3e922012-06-03 20:05:57 -0700855
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700856 /* If isolation recently failed, do not retry */
857 if (!isolation_suitable(cc, page))
858 continue;
859
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700860 /* Found a block suitable for isolating free pages from. */
861 isolated = isolate_freepages_block(cc, &isolate_start_pfn,
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700862 block_end_pfn, freelist, false);
Mel Gormanf40d1e42012-10-08 16:32:36 -0700863 nr_freepages += isolated;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100864
865 /*
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700866 * Remember where the free scanner should restart next time,
867 * which is where isolate_freepages_block() left off.
868 * But if it scanned the whole pageblock, isolate_start_pfn
869 * now points at block_end_pfn, which is the start of the next
870 * pageblock.
871 * In that case we will however want to restart at the start
872 * of the previous pageblock.
873 */
874 cc->free_pfn = (isolate_start_pfn < block_end_pfn) ?
875 isolate_start_pfn :
876 block_start_pfn - pageblock_nr_pages;
877
878 /*
Vlastimil Babkae9ade562014-06-04 16:08:34 -0700879 * Set a flag that we successfully isolated in this pageblock.
880 * In the next loop iteration, zone->compact_cached_free_pfn
881 * will not be updated and thus it will effectively contain the
882 * highest pageblock we isolated pages from.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100883 */
Vlastimil Babkae9ade562014-06-04 16:08:34 -0700884 if (isolated)
Mel Gormanc89511a2012-10-08 16:32:45 -0700885 cc->finished_update_free = true;
Vlastimil Babkabe976572014-06-04 16:10:41 -0700886
887 /*
888 * isolate_freepages_block() might have aborted due to async
889 * compaction being contended
890 */
891 if (cc->contended)
892 break;
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100893 }
894
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100895 /* split_free_page does not map the pages */
896 map_pages(freelist);
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100897
Vlastimil Babka7ed695e2014-01-21 15:51:09 -0800898 /*
899 * If we crossed the migrate scanner, we want to keep it that way
900 * so that compact_finished() may detect this
901 */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700902 if (block_start_pfn < low_pfn)
Vlastimil Babkae9ade562014-06-04 16:08:34 -0700903 cc->free_pfn = cc->migrate_pfn;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700904
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100905 cc->nr_freepages = nr_freepages;
Mel Gorman748446b2010-05-24 14:32:27 -0700906}
907
908/*
909 * This is a migrate-callback that "allocates" freepages by taking pages
910 * from the isolated freelists in the block we are migrating to.
911 */
912static struct page *compaction_alloc(struct page *migratepage,
913 unsigned long data,
914 int **result)
915{
916 struct compact_control *cc = (struct compact_control *)data;
917 struct page *freepage;
918
Vlastimil Babkabe976572014-06-04 16:10:41 -0700919 /*
920 * Isolate free pages if necessary, and if we are not aborting due to
921 * contention.
922 */
Mel Gorman748446b2010-05-24 14:32:27 -0700923 if (list_empty(&cc->freepages)) {
Vlastimil Babkabe976572014-06-04 16:10:41 -0700924 if (!cc->contended)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700925 isolate_freepages(cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700926
927 if (list_empty(&cc->freepages))
928 return NULL;
929 }
930
931 freepage = list_entry(cc->freepages.next, struct page, lru);
932 list_del(&freepage->lru);
933 cc->nr_freepages--;
934
935 return freepage;
936}
937
938/*
David Rientjesd53aea32014-06-04 16:08:26 -0700939 * This is a migrate-callback that "frees" freepages back to the isolated
940 * freelist. All pages on the freelist are from the same zone, so there is no
941 * special handling needed for NUMA.
942 */
943static void compaction_free(struct page *page, unsigned long data)
944{
945 struct compact_control *cc = (struct compact_control *)data;
946
947 list_add(&page->lru, &cc->freepages);
948 cc->nr_freepages++;
949}
950
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100951/* possible outcome of isolate_migratepages */
952typedef enum {
953 ISOLATE_ABORT, /* Abort compaction now */
954 ISOLATE_NONE, /* No pages isolated, continue scanning */
955 ISOLATE_SUCCESS, /* Pages isolated, migrate */
956} isolate_migrate_t;
957
958/*
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700959 * Isolate all pages that can be migrated from the first suitable block,
960 * starting at the block pointed to by the migrate scanner pfn within
961 * compact_control.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100962 */
963static isolate_migrate_t isolate_migratepages(struct zone *zone,
964 struct compact_control *cc)
965{
966 unsigned long low_pfn, end_pfn;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700967 struct page *page;
968 const isolate_mode_t isolate_mode =
969 (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100970
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700971 /*
972 * Start at where we last stopped, or beginning of the zone as
973 * initialized by compact_zone()
974 */
975 low_pfn = cc->migrate_pfn;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100976
977 /* Only scan within a pageblock boundary */
Mel Gormana9aacbc2013-02-22 16:32:25 -0800978 end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100979
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700980 /*
981 * Iterate over whole pageblocks until we find the first suitable.
982 * Do not cross the free scanner.
983 */
984 for (; end_pfn <= cc->free_pfn;
985 low_pfn = end_pfn, end_pfn += pageblock_nr_pages) {
986
987 /*
988 * This can potentially iterate a massively long zone with
989 * many pageblocks unsuitable, so periodically check if we
990 * need to schedule, or even abort async compaction.
991 */
992 if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
993 && compact_should_abort(cc))
994 break;
995
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700996 page = pageblock_pfn_to_page(low_pfn, end_pfn, zone);
997 if (!page)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700998 continue;
999
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001000 /* If isolation recently failed, do not retry */
1001 if (!isolation_suitable(cc, page))
1002 continue;
1003
1004 /*
1005 * For async compaction, also only scan in MOVABLE blocks.
1006 * Async compaction is optimistic to see if the minimum amount
1007 * of work satisfies the allocation.
1008 */
1009 if (cc->mode == MIGRATE_ASYNC &&
1010 !migrate_async_suitable(get_pageblock_migratetype(page)))
1011 continue;
1012
1013 /* Perform the isolation */
1014 low_pfn = isolate_migratepages_block(cc, low_pfn, end_pfn,
1015 isolate_mode);
1016
1017 if (!low_pfn || cc->contended)
1018 return ISOLATE_ABORT;
1019
1020 /*
1021 * Either we isolated something and proceed with migration. Or
1022 * we failed and compact_zone should decide if we should
1023 * continue or not.
1024 */
1025 break;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001026 }
1027
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001028 acct_isolated(zone, cc);
1029 /* Record where migration scanner will be restarted */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001030 cc->migrate_pfn = low_pfn;
1031
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001032 return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001033}
1034
David Rientjes6d7ce552014-10-09 15:27:27 -07001035static int compact_finished(struct zone *zone, struct compact_control *cc,
1036 const int migratetype)
Mel Gorman748446b2010-05-24 14:32:27 -07001037{
Mel Gorman8fb74b92013-01-11 14:32:16 -08001038 unsigned int order;
Andrea Arcangeli5a03b052011-01-13 15:47:11 -08001039 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -07001040
Vlastimil Babkabe976572014-06-04 16:10:41 -07001041 if (cc->contended || fatal_signal_pending(current))
Mel Gorman748446b2010-05-24 14:32:27 -07001042 return COMPACT_PARTIAL;
1043
Mel Gorman753341a2012-10-08 16:32:40 -07001044 /* Compaction run completes if the migrate and free scanner meet */
Mel Gormanbb13ffe2012-10-08 16:32:41 -07001045 if (cc->free_pfn <= cc->migrate_pfn) {
Vlastimil Babka55b7c4c2014-01-21 15:51:11 -08001046 /* Let the next compaction start anew. */
David Rientjes35979ef2014-06-04 16:08:27 -07001047 zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
1048 zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
Vlastimil Babka55b7c4c2014-01-21 15:51:11 -08001049 zone->compact_cached_free_pfn = zone_end_pfn(zone);
1050
Mel Gorman62997022012-10-08 16:32:47 -07001051 /*
1052 * Mark that the PG_migrate_skip information should be cleared
1053 * by kswapd when it goes to sleep. kswapd does not set the
1054 * flag itself as the decision to be clear should be directly
1055 * based on an allocation request.
1056 */
1057 if (!current_is_kswapd())
1058 zone->compact_blockskip_flush = true;
1059
Mel Gorman748446b2010-05-24 14:32:27 -07001060 return COMPACT_COMPLETE;
Mel Gormanbb13ffe2012-10-08 16:32:41 -07001061 }
Mel Gorman748446b2010-05-24 14:32:27 -07001062
Johannes Weiner82478fb2011-01-20 14:44:21 -08001063 /*
1064 * order == -1 is expected when compacting via
1065 * /proc/sys/vm/compact_memory
1066 */
Mel Gorman56de7262010-05-24 14:32:30 -07001067 if (cc->order == -1)
1068 return COMPACT_CONTINUE;
1069
Michal Hocko3957c772011-06-15 15:08:25 -07001070 /* Compaction run is not finished if the watermark is not met */
1071 watermark = low_wmark_pages(zone);
1072 watermark += (1 << cc->order);
1073
1074 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
1075 return COMPACT_CONTINUE;
1076
Mel Gorman56de7262010-05-24 14:32:30 -07001077 /* Direct compactor: Is a suitable page free? */
Mel Gorman8fb74b92013-01-11 14:32:16 -08001078 for (order = cc->order; order < MAX_ORDER; order++) {
1079 struct free_area *area = &zone->free_area[order];
Mel Gorman56de7262010-05-24 14:32:30 -07001080
Mel Gorman8fb74b92013-01-11 14:32:16 -08001081 /* Job done if page is free of the right migratetype */
David Rientjes6d7ce552014-10-09 15:27:27 -07001082 if (!list_empty(&area->free_list[migratetype]))
Mel Gorman8fb74b92013-01-11 14:32:16 -08001083 return COMPACT_PARTIAL;
1084
1085 /* Job done if allocation would set block type */
1086 if (cc->order >= pageblock_order && area->nr_free)
1087 return COMPACT_PARTIAL;
Mel Gorman56de7262010-05-24 14:32:30 -07001088 }
1089
Mel Gorman748446b2010-05-24 14:32:27 -07001090 return COMPACT_CONTINUE;
1091}
1092
Mel Gorman3e7d3442011-01-13 15:45:56 -08001093/*
1094 * compaction_suitable: Is this suitable to run compaction on this zone now?
1095 * Returns
1096 * COMPACT_SKIPPED - If there are too few free pages for compaction
1097 * COMPACT_PARTIAL - If the allocation would succeed without compaction
1098 * COMPACT_CONTINUE - If compaction should run now
1099 */
1100unsigned long compaction_suitable(struct zone *zone, int order)
1101{
1102 int fragindex;
1103 unsigned long watermark;
1104
1105 /*
Michal Hocko3957c772011-06-15 15:08:25 -07001106 * order == -1 is expected when compacting via
1107 * /proc/sys/vm/compact_memory
1108 */
1109 if (order == -1)
1110 return COMPACT_CONTINUE;
1111
1112 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -08001113 * Watermarks for order-0 must be met for compaction. Note the 2UL.
1114 * This is because during migration, copies of pages need to be
1115 * allocated and for a short time, the footprint is higher
1116 */
1117 watermark = low_wmark_pages(zone) + (2UL << order);
1118 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
1119 return COMPACT_SKIPPED;
1120
1121 /*
1122 * fragmentation index determines if allocation failures are due to
1123 * low memory or external fragmentation
1124 *
Shaohua Lia582a732011-06-15 15:08:49 -07001125 * index of -1000 implies allocations might succeed depending on
1126 * watermarks
Mel Gorman3e7d3442011-01-13 15:45:56 -08001127 * index towards 0 implies failure is due to lack of memory
1128 * index towards 1000 implies failure is due to fragmentation
1129 *
1130 * Only compact if a failure would be due to fragmentation.
1131 */
1132 fragindex = fragmentation_index(zone, order);
1133 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
1134 return COMPACT_SKIPPED;
1135
Shaohua Lia582a732011-06-15 15:08:49 -07001136 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
1137 0, 0))
Mel Gorman3e7d3442011-01-13 15:45:56 -08001138 return COMPACT_PARTIAL;
1139
1140 return COMPACT_CONTINUE;
1141}
1142
Mel Gorman748446b2010-05-24 14:32:27 -07001143static int compact_zone(struct zone *zone, struct compact_control *cc)
1144{
1145 int ret;
Mel Gormanc89511a2012-10-08 16:32:45 -07001146 unsigned long start_pfn = zone->zone_start_pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -08001147 unsigned long end_pfn = zone_end_pfn(zone);
David Rientjes6d7ce552014-10-09 15:27:27 -07001148 const int migratetype = gfpflags_to_migratetype(cc->gfp_mask);
David Rientjese0b9dae2014-06-04 16:08:28 -07001149 const bool sync = cc->mode != MIGRATE_ASYNC;
Mel Gorman748446b2010-05-24 14:32:27 -07001150
Mel Gorman3e7d3442011-01-13 15:45:56 -08001151 ret = compaction_suitable(zone, cc->order);
1152 switch (ret) {
1153 case COMPACT_PARTIAL:
1154 case COMPACT_SKIPPED:
1155 /* Compaction is likely to fail */
1156 return ret;
1157 case COMPACT_CONTINUE:
1158 /* Fall through to compaction */
1159 ;
1160 }
1161
Mel Gormanc89511a2012-10-08 16:32:45 -07001162 /*
Vlastimil Babkad3132e42014-01-21 15:51:08 -08001163 * Clear pageblock skip if there were failures recently and compaction
1164 * is about to be retried after being deferred. kswapd does not do
1165 * this reset as it'll reset the cached information when going to sleep.
1166 */
1167 if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
1168 __reset_isolation_suitable(zone);
1169
1170 /*
Mel Gormanc89511a2012-10-08 16:32:45 -07001171 * Setup to move all movable pages to the end of the zone. Used cached
1172 * information on where the scanners should start but check that it
1173 * is initialised by ensuring the values are within zone boundaries.
1174 */
David Rientjese0b9dae2014-06-04 16:08:28 -07001175 cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync];
Mel Gormanc89511a2012-10-08 16:32:45 -07001176 cc->free_pfn = zone->compact_cached_free_pfn;
1177 if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
1178 cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
1179 zone->compact_cached_free_pfn = cc->free_pfn;
1180 }
1181 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
1182 cc->migrate_pfn = start_pfn;
David Rientjes35979ef2014-06-04 16:08:27 -07001183 zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
1184 zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
Mel Gormanc89511a2012-10-08 16:32:45 -07001185 }
Mel Gorman748446b2010-05-24 14:32:27 -07001186
Mel Gorman0eb927c2014-01-21 15:51:05 -08001187 trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, cc->free_pfn, end_pfn);
1188
Mel Gorman748446b2010-05-24 14:32:27 -07001189 migrate_prep_local();
1190
David Rientjes6d7ce552014-10-09 15:27:27 -07001191 while ((ret = compact_finished(zone, cc, migratetype)) ==
1192 COMPACT_CONTINUE) {
Minchan Kim9d502c12011-03-22 16:30:39 -07001193 int err;
Mel Gorman748446b2010-05-24 14:32:27 -07001194
Mel Gormanf9e35b32011-06-15 15:08:52 -07001195 switch (isolate_migratepages(zone, cc)) {
1196 case ISOLATE_ABORT:
1197 ret = COMPACT_PARTIAL;
Rafael Aquini5733c7d2012-12-11 16:02:47 -08001198 putback_movable_pages(&cc->migratepages);
Shaohua Lie64c5232012-10-08 16:32:27 -07001199 cc->nr_migratepages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -07001200 goto out;
1201 case ISOLATE_NONE:
Mel Gorman748446b2010-05-24 14:32:27 -07001202 continue;
Mel Gormanf9e35b32011-06-15 15:08:52 -07001203 case ISOLATE_SUCCESS:
1204 ;
1205 }
Mel Gorman748446b2010-05-24 14:32:27 -07001206
David Rientjesd53aea32014-06-04 16:08:26 -07001207 err = migrate_pages(&cc->migratepages, compaction_alloc,
David Rientjese0b9dae2014-06-04 16:08:28 -07001208 compaction_free, (unsigned long)cc, cc->mode,
Mel Gorman7b2a2d42012-10-19 14:07:31 +01001209 MR_COMPACTION);
Mel Gorman748446b2010-05-24 14:32:27 -07001210
Vlastimil Babkaf8c93012014-06-04 16:08:32 -07001211 trace_mm_compaction_migratepages(cc->nr_migratepages, err,
1212 &cc->migratepages);
Mel Gorman748446b2010-05-24 14:32:27 -07001213
Vlastimil Babkaf8c93012014-06-04 16:08:32 -07001214 /* All pages were either migrated or will be released */
1215 cc->nr_migratepages = 0;
Minchan Kim9d502c12011-03-22 16:30:39 -07001216 if (err) {
Rafael Aquini5733c7d2012-12-11 16:02:47 -08001217 putback_movable_pages(&cc->migratepages);
Vlastimil Babka7ed695e2014-01-21 15:51:09 -08001218 /*
1219 * migrate_pages() may return -ENOMEM when scanners meet
1220 * and we want compact_finished() to detect it
1221 */
1222 if (err == -ENOMEM && cc->free_pfn > cc->migrate_pfn) {
David Rientjes4bf2bba2012-07-11 14:02:13 -07001223 ret = COMPACT_PARTIAL;
1224 goto out;
1225 }
Mel Gorman748446b2010-05-24 14:32:27 -07001226 }
Mel Gorman748446b2010-05-24 14:32:27 -07001227 }
1228
Mel Gormanf9e35b32011-06-15 15:08:52 -07001229out:
Mel Gorman748446b2010-05-24 14:32:27 -07001230 /* Release free pages and check accounting */
1231 cc->nr_freepages -= release_freepages(&cc->freepages);
1232 VM_BUG_ON(cc->nr_freepages != 0);
1233
Mel Gorman0eb927c2014-01-21 15:51:05 -08001234 trace_mm_compaction_end(ret);
1235
Mel Gorman748446b2010-05-24 14:32:27 -07001236 return ret;
1237}
Mel Gorman76ab0f52010-05-24 14:32:28 -07001238
David Rientjese0b9dae2014-06-04 16:08:28 -07001239static unsigned long compact_zone_order(struct zone *zone, int order,
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001240 gfp_t gfp_mask, enum migrate_mode mode, int *contended)
Mel Gorman56de7262010-05-24 14:32:30 -07001241{
Shaohua Lie64c5232012-10-08 16:32:27 -07001242 unsigned long ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001243 struct compact_control cc = {
1244 .nr_freepages = 0,
1245 .nr_migratepages = 0,
1246 .order = order,
David Rientjes6d7ce552014-10-09 15:27:27 -07001247 .gfp_mask = gfp_mask,
Mel Gorman56de7262010-05-24 14:32:30 -07001248 .zone = zone,
David Rientjese0b9dae2014-06-04 16:08:28 -07001249 .mode = mode,
Mel Gorman56de7262010-05-24 14:32:30 -07001250 };
1251 INIT_LIST_HEAD(&cc.freepages);
1252 INIT_LIST_HEAD(&cc.migratepages);
1253
Shaohua Lie64c5232012-10-08 16:32:27 -07001254 ret = compact_zone(zone, &cc);
1255
1256 VM_BUG_ON(!list_empty(&cc.freepages));
1257 VM_BUG_ON(!list_empty(&cc.migratepages));
1258
1259 *contended = cc.contended;
1260 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001261}
1262
Mel Gorman5e771902010-05-24 14:32:31 -07001263int sysctl_extfrag_threshold = 500;
1264
Mel Gorman56de7262010-05-24 14:32:30 -07001265/**
1266 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
1267 * @zonelist: The zonelist used for the current allocation
1268 * @order: The order of the current allocation
1269 * @gfp_mask: The GFP mask of the current allocation
1270 * @nodemask: The allowed nodes to allocate from
David Rientjese0b9dae2014-06-04 16:08:28 -07001271 * @mode: The migration mode for async, sync light, or sync migration
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001272 * @contended: Return value that determines if compaction was aborted due to
1273 * need_resched() or lock contention
Vlastimil Babka53853e22014-10-09 15:27:02 -07001274 * @candidate_zone: Return the zone where we think allocation should succeed
Mel Gorman56de7262010-05-24 14:32:30 -07001275 *
1276 * This is the main entry point for direct page compaction.
1277 */
1278unsigned long try_to_compact_pages(struct zonelist *zonelist,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001279 int order, gfp_t gfp_mask, nodemask_t *nodemask,
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001280 enum migrate_mode mode, int *contended,
Vlastimil Babka53853e22014-10-09 15:27:02 -07001281 struct zone **candidate_zone)
Mel Gorman56de7262010-05-24 14:32:30 -07001282{
1283 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
1284 int may_enter_fs = gfp_mask & __GFP_FS;
1285 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -07001286 struct zoneref *z;
1287 struct zone *zone;
Vlastimil Babka53853e22014-10-09 15:27:02 -07001288 int rc = COMPACT_DEFERRED;
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001289 int alloc_flags = 0;
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001290 int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */
1291
1292 *contended = COMPACT_CONTENDED_NONE;
Mel Gorman56de7262010-05-24 14:32:30 -07001293
Mel Gorman4ffb6332012-10-08 16:29:09 -07001294 /* Check if the GFP flags allow compaction */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -08001295 if (!order || !may_enter_fs || !may_perform_io)
Vlastimil Babka53853e22014-10-09 15:27:02 -07001296 return COMPACT_SKIPPED;
Mel Gorman56de7262010-05-24 14:32:30 -07001297
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001298#ifdef CONFIG_CMA
David Rientjes43e7a342014-10-09 15:27:25 -07001299 if (gfpflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001300 alloc_flags |= ALLOC_CMA;
1301#endif
Mel Gorman56de7262010-05-24 14:32:30 -07001302 /* Compact each zone in the list */
1303 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
1304 nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -07001305 int status;
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001306 int zone_contended;
Mel Gorman56de7262010-05-24 14:32:30 -07001307
Vlastimil Babka53853e22014-10-09 15:27:02 -07001308 if (compaction_deferred(zone, order))
1309 continue;
1310
David Rientjese0b9dae2014-06-04 16:08:28 -07001311 status = compact_zone_order(zone, order, gfp_mask, mode,
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001312 &zone_contended);
Mel Gorman56de7262010-05-24 14:32:30 -07001313 rc = max(status, rc);
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001314 /*
1315 * It takes at least one zone that wasn't lock contended
1316 * to clear all_zones_contended.
1317 */
1318 all_zones_contended &= zone_contended;
Mel Gorman56de7262010-05-24 14:32:30 -07001319
Mel Gorman3e7d3442011-01-13 15:45:56 -08001320 /* If a normal allocation would succeed, stop compacting */
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001321 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
Vlastimil Babka53853e22014-10-09 15:27:02 -07001322 alloc_flags)) {
1323 *candidate_zone = zone;
1324 /*
1325 * We think the allocation will succeed in this zone,
1326 * but it is not certain, hence the false. The caller
1327 * will repeat this with true if allocation indeed
1328 * succeeds in this zone.
1329 */
1330 compaction_defer_reset(zone, order, false);
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001331 /*
1332 * It is possible that async compaction aborted due to
1333 * need_resched() and the watermarks were ok thanks to
1334 * somebody else freeing memory. The allocation can
1335 * however still fail so we better signal the
1336 * need_resched() contention anyway (this will not
1337 * prevent the allocation attempt).
1338 */
1339 if (zone_contended == COMPACT_CONTENDED_SCHED)
1340 *contended = COMPACT_CONTENDED_SCHED;
1341
1342 goto break_loop;
1343 }
1344
1345 if (mode != MIGRATE_ASYNC) {
Vlastimil Babka53853e22014-10-09 15:27:02 -07001346 /*
1347 * We think that allocation won't succeed in this zone
1348 * so we defer compaction there. If it ends up
1349 * succeeding after all, it will be reset.
1350 */
1351 defer_compaction(zone, order);
1352 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001353
1354 /*
1355 * We might have stopped compacting due to need_resched() in
1356 * async compaction, or due to a fatal signal detected. In that
1357 * case do not try further zones and signal need_resched()
1358 * contention.
1359 */
1360 if ((zone_contended == COMPACT_CONTENDED_SCHED)
1361 || fatal_signal_pending(current)) {
1362 *contended = COMPACT_CONTENDED_SCHED;
1363 goto break_loop;
1364 }
1365
1366 continue;
1367break_loop:
1368 /*
1369 * We might not have tried all the zones, so be conservative
1370 * and assume they are not all lock contended.
1371 */
1372 all_zones_contended = 0;
1373 break;
Mel Gorman56de7262010-05-24 14:32:30 -07001374 }
1375
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001376 /*
1377 * If at least one zone wasn't deferred or skipped, we report if all
1378 * zones that were tried were lock contended.
1379 */
1380 if (rc > COMPACT_SKIPPED && all_zones_contended)
1381 *contended = COMPACT_CONTENDED_LOCK;
1382
Mel Gorman56de7262010-05-24 14:32:30 -07001383 return rc;
1384}
1385
1386
Mel Gorman76ab0f52010-05-24 14:32:28 -07001387/* Compact all zones within a node */
Andrew Morton7103f162013-02-22 16:32:33 -08001388static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001389{
1390 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -07001391 struct zone *zone;
1392
Mel Gorman76ab0f52010-05-24 14:32:28 -07001393 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -07001394
1395 zone = &pgdat->node_zones[zoneid];
1396 if (!populated_zone(zone))
1397 continue;
1398
Rik van Riel7be62de2012-03-21 16:33:52 -07001399 cc->nr_freepages = 0;
1400 cc->nr_migratepages = 0;
1401 cc->zone = zone;
1402 INIT_LIST_HEAD(&cc->freepages);
1403 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001404
Dan Carpenteraad6ec32012-03-21 16:33:54 -07001405 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -07001406 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001407
Rik van Rielaff62242012-03-21 16:33:52 -07001408 if (cc->order > 0) {
Vlastimil Babkade6c60a2014-01-21 15:51:07 -08001409 if (zone_watermark_ok(zone, cc->order,
1410 low_wmark_pages(zone), 0, 0))
1411 compaction_defer_reset(zone, cc->order, false);
Rik van Rielaff62242012-03-21 16:33:52 -07001412 }
1413
Rik van Riel7be62de2012-03-21 16:33:52 -07001414 VM_BUG_ON(!list_empty(&cc->freepages));
1415 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -07001416 }
Mel Gorman76ab0f52010-05-24 14:32:28 -07001417}
1418
Andrew Morton7103f162013-02-22 16:32:33 -08001419void compact_pgdat(pg_data_t *pgdat, int order)
Rik van Riel7be62de2012-03-21 16:33:52 -07001420{
1421 struct compact_control cc = {
1422 .order = order,
David Rientjese0b9dae2014-06-04 16:08:28 -07001423 .mode = MIGRATE_ASYNC,
Rik van Riel7be62de2012-03-21 16:33:52 -07001424 };
1425
Mel Gorman3a7200a2013-09-11 14:22:19 -07001426 if (!order)
1427 return;
1428
Andrew Morton7103f162013-02-22 16:32:33 -08001429 __compact_pgdat(pgdat, &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001430}
1431
Andrew Morton7103f162013-02-22 16:32:33 -08001432static void compact_node(int nid)
Rik van Riel7be62de2012-03-21 16:33:52 -07001433{
Rik van Riel7be62de2012-03-21 16:33:52 -07001434 struct compact_control cc = {
1435 .order = -1,
David Rientjese0b9dae2014-06-04 16:08:28 -07001436 .mode = MIGRATE_SYNC,
David Rientjes91ca9182014-04-03 14:47:23 -07001437 .ignore_skip_hint = true,
Rik van Riel7be62de2012-03-21 16:33:52 -07001438 };
1439
Andrew Morton7103f162013-02-22 16:32:33 -08001440 __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001441}
1442
Mel Gorman76ab0f52010-05-24 14:32:28 -07001443/* Compact all nodes in the system */
Jason Liu7964c062013-01-11 14:31:47 -08001444static void compact_nodes(void)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001445{
1446 int nid;
1447
Hugh Dickins8575ec22012-03-21 16:33:53 -07001448 /* Flush pending updates to the LRU lists */
1449 lru_add_drain_all();
1450
Mel Gorman76ab0f52010-05-24 14:32:28 -07001451 for_each_online_node(nid)
1452 compact_node(nid);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001453}
1454
1455/* The written value is actually unused, all memory is compacted */
1456int sysctl_compact_memory;
1457
1458/* This is the entry point for compacting all nodes via /proc/sys/vm */
1459int sysctl_compaction_handler(struct ctl_table *table, int write,
1460 void __user *buffer, size_t *length, loff_t *ppos)
1461{
1462 if (write)
Jason Liu7964c062013-01-11 14:31:47 -08001463 compact_nodes();
Mel Gorman76ab0f52010-05-24 14:32:28 -07001464
1465 return 0;
1466}
Mel Gormaned4a6d72010-05-24 14:32:29 -07001467
Mel Gorman5e771902010-05-24 14:32:31 -07001468int sysctl_extfrag_handler(struct ctl_table *table, int write,
1469 void __user *buffer, size_t *length, loff_t *ppos)
1470{
1471 proc_dointvec_minmax(table, write, buffer, length, ppos);
1472
1473 return 0;
1474}
1475
Mel Gormaned4a6d72010-05-24 14:32:29 -07001476#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Rashika Kheria74e77fb2014-04-03 14:48:01 -07001477static ssize_t sysfs_compact_node(struct device *dev,
Kay Sievers10fbcf42011-12-21 14:48:43 -08001478 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -07001479 const char *buf, size_t count)
1480{
Hugh Dickins8575ec22012-03-21 16:33:53 -07001481 int nid = dev->id;
1482
1483 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1484 /* Flush pending updates to the LRU lists */
1485 lru_add_drain_all();
1486
1487 compact_node(nid);
1488 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07001489
1490 return count;
1491}
Kay Sievers10fbcf42011-12-21 14:48:43 -08001492static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001493
1494int compaction_register_node(struct node *node)
1495{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001496 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001497}
1498
1499void compaction_unregister_node(struct node *node)
1500{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001501 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001502}
1503#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001504
1505#endif /* CONFIG_COMPACTION */