blob: 5039c964f5c849530c8792970df405787331c5df [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 */
Mel Gormanf40d1e42012-10-08 16:32:36 -0700316 if (PageBuddy(page) && page_order(page) >= pageblock_order)
Joonsoo Kim7d348b92014-04-07 15:37:03 -0700317 return false;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700318
319 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
Joonsoo Kim7d348b92014-04-07 15:37:03 -0700320 if (migrate_async_suitable(get_pageblock_migratetype(page)))
Mel Gormanf40d1e42012-10-08 16:32:36 -0700321 return true;
322
323 /* Otherwise skip the block */
324 return false;
325}
326
Mel Gormanc67fe372012-08-21 16:16:17 -0700327/*
Jerome Marchand9e4be472013-11-12 15:07:12 -0800328 * Isolate free pages onto a private freelist. If @strict is true, will abort
329 * returning 0 on any invalid PFNs or non-free pages inside of the pageblock
330 * (even though it may still end up isolating some pages).
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100331 */
Mel Gormanf40d1e42012-10-08 16:32:36 -0700332static unsigned long isolate_freepages_block(struct compact_control *cc,
333 unsigned long blockpfn,
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100334 unsigned long end_pfn,
335 struct list_head *freelist,
336 bool strict)
Mel Gorman748446b2010-05-24 14:32:27 -0700337{
Mel Gormanb7aba692011-01-13 15:45:54 -0800338 int nr_scanned = 0, total_isolated = 0;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700339 struct page *cursor, *valid_page = NULL;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700340 unsigned long flags;
341 bool locked = false;
Mel Gorman748446b2010-05-24 14:32:27 -0700342
Mel Gorman748446b2010-05-24 14:32:27 -0700343 cursor = pfn_to_page(blockpfn);
344
Mel Gormanf40d1e42012-10-08 16:32:36 -0700345 /* Isolate free pages. */
Mel Gorman748446b2010-05-24 14:32:27 -0700346 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
347 int isolated, i;
348 struct page *page = cursor;
349
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700350 /*
351 * Periodically drop the lock (if held) regardless of its
352 * contention, to give chance to IRQs. Abort if fatal signal
353 * pending or async compaction detects need_resched()
354 */
355 if (!(blockpfn % SWAP_CLUSTER_MAX)
356 && compact_unlock_should_abort(&cc->zone->lock, flags,
357 &locked, cc))
358 break;
359
Mel Gormanb7aba692011-01-13 15:45:54 -0800360 nr_scanned++;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700361 if (!pfn_valid_within(blockpfn))
Laura Abbott2af120b2014-03-10 15:49:44 -0700362 goto isolate_fail;
363
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700364 if (!valid_page)
365 valid_page = page;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700366 if (!PageBuddy(page))
Laura Abbott2af120b2014-03-10 15:49:44 -0700367 goto isolate_fail;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700368
369 /*
Vlastimil Babka69b71892014-10-09 15:27:18 -0700370 * If we already hold the lock, we can skip some rechecking.
371 * Note that if we hold the lock now, checked_pageblock was
372 * already set in some previous iteration (or strict is true),
373 * so it is correct to skip the suitable migration target
374 * recheck as well.
Mel Gormanf40d1e42012-10-08 16:32:36 -0700375 */
Vlastimil Babka69b71892014-10-09 15:27:18 -0700376 if (!locked) {
377 /*
378 * The zone lock must be held to isolate freepages.
379 * Unfortunately this is a very coarse lock and can be
380 * heavily contended if there are parallel allocations
381 * or parallel compactions. For async compaction do not
382 * spin on the lock and we acquire the lock as late as
383 * possible.
384 */
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700385 locked = compact_trylock_irqsave(&cc->zone->lock,
386 &flags, cc);
Vlastimil Babka69b71892014-10-09 15:27:18 -0700387 if (!locked)
388 break;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700389
Vlastimil Babka69b71892014-10-09 15:27:18 -0700390 /* Recheck this is a buddy page under lock */
391 if (!PageBuddy(page))
392 goto isolate_fail;
393 }
Mel Gorman748446b2010-05-24 14:32:27 -0700394
395 /* Found a free page, break it into order-0 pages */
396 isolated = split_free_page(page);
397 total_isolated += isolated;
398 for (i = 0; i < isolated; i++) {
399 list_add(&page->lru, freelist);
400 page++;
401 }
402
403 /* If a page was split, advance to the end of it */
404 if (isolated) {
405 blockpfn += isolated - 1;
406 cursor += isolated - 1;
Laura Abbott2af120b2014-03-10 15:49:44 -0700407 continue;
Mel Gorman748446b2010-05-24 14:32:27 -0700408 }
Laura Abbott2af120b2014-03-10 15:49:44 -0700409
410isolate_fail:
411 if (strict)
412 break;
413 else
414 continue;
415
Mel Gorman748446b2010-05-24 14:32:27 -0700416 }
417
Mel Gormanb7aba692011-01-13 15:45:54 -0800418 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
Mel Gormanf40d1e42012-10-08 16:32:36 -0700419
420 /*
421 * If strict isolation is requested by CMA then check that all the
422 * pages requested were isolated. If there were any failures, 0 is
423 * returned and CMA will fail.
424 */
Laura Abbott2af120b2014-03-10 15:49:44 -0700425 if (strict && blockpfn < end_pfn)
Mel Gormanf40d1e42012-10-08 16:32:36 -0700426 total_isolated = 0;
427
428 if (locked)
429 spin_unlock_irqrestore(&cc->zone->lock, flags);
430
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700431 /* Update the pageblock-skip if the whole pageblock was scanned */
432 if (blockpfn == end_pfn)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700433 update_pageblock_skip(cc, valid_page, total_isolated, false);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700434
Minchan Kim010fc292012-12-20 15:05:06 -0800435 count_compact_events(COMPACTFREE_SCANNED, nr_scanned);
Mel Gorman397487d2012-10-19 12:00:10 +0100436 if (total_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -0800437 count_compact_events(COMPACTISOLATED, total_isolated);
Mel Gorman748446b2010-05-24 14:32:27 -0700438 return total_isolated;
439}
440
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100441/**
442 * isolate_freepages_range() - isolate free pages.
443 * @start_pfn: The first PFN to start isolating.
444 * @end_pfn: The one-past-last PFN.
445 *
446 * Non-free pages, invalid PFNs, or zone boundaries within the
447 * [start_pfn, end_pfn) range are considered errors, cause function to
448 * undo its actions and return zero.
449 *
450 * Otherwise, function returns one-past-the-last PFN of isolated page
451 * (which may be greater then end_pfn if end fell in a middle of
452 * a free page).
453 */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100454unsigned long
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700455isolate_freepages_range(struct compact_control *cc,
456 unsigned long start_pfn, unsigned long end_pfn)
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100457{
Mel Gormanf40d1e42012-10-08 16:32:36 -0700458 unsigned long isolated, pfn, block_end_pfn;
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100459 LIST_HEAD(freelist);
460
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700461 pfn = start_pfn;
462 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100463
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700464 for (; pfn < end_pfn; pfn += isolated,
465 block_end_pfn += pageblock_nr_pages) {
466
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100467 block_end_pfn = min(block_end_pfn, end_pfn);
468
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700469 if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
470 break;
471
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700472 isolated = isolate_freepages_block(cc, pfn, block_end_pfn,
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100473 &freelist, true);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100474
475 /*
476 * In strict mode, isolate_freepages_block() returns 0 if
477 * there are any holes in the block (ie. invalid PFNs or
478 * non-free pages).
479 */
480 if (!isolated)
481 break;
482
483 /*
484 * If we managed to isolate pages, it is always (1 << n) *
485 * pageblock_nr_pages for some non-negative n. (Max order
486 * page may span two pageblocks).
487 */
488 }
489
490 /* split_free_page does not map the pages */
491 map_pages(&freelist);
492
493 if (pfn < end_pfn) {
494 /* Loop terminated early, cleanup. */
495 release_freepages(&freelist);
496 return 0;
497 }
498
499 /* We don't use freelists for anything. */
500 return pfn;
501}
502
Mel Gorman748446b2010-05-24 14:32:27 -0700503/* Update the number of anon and file isolated pages in the zone */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700504static void acct_isolated(struct zone *zone, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700505{
506 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700507 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700508
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700509 if (list_empty(&cc->migratepages))
510 return;
511
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700512 list_for_each_entry(page, &cc->migratepages, lru)
513 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700514
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700515 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
516 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
Mel Gorman748446b2010-05-24 14:32:27 -0700517}
518
519/* Similar to reclaim, but different enough that they don't share logic */
520static bool too_many_isolated(struct zone *zone)
521{
Minchan Kimbc693042010-09-09 16:38:00 -0700522 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700523
524 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
525 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700526 active = zone_page_state(zone, NR_ACTIVE_FILE) +
527 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700528 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
529 zone_page_state(zone, NR_ISOLATED_ANON);
530
Minchan Kimbc693042010-09-09 16:38:00 -0700531 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700532}
533
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100534/**
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700535 * isolate_migratepages_block() - isolate all migrate-able pages within
536 * a single pageblock
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100537 * @cc: Compaction control structure.
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700538 * @low_pfn: The first PFN to isolate
539 * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock
540 * @isolate_mode: Isolation mode to be used.
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100541 *
542 * Isolate all pages that can be migrated from the range specified by
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700543 * [low_pfn, end_pfn). The range is expected to be within same pageblock.
544 * Returns zero if there is a fatal signal pending, otherwise PFN of the
545 * first page that was not scanned (which may be both less, equal to or more
546 * than end_pfn).
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100547 *
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700548 * The pages are isolated on cc->migratepages list (not required to be empty),
549 * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field
550 * is neither read nor updated.
Mel Gorman748446b2010-05-24 14:32:27 -0700551 */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700552static unsigned long
553isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
554 unsigned long end_pfn, isolate_mode_t isolate_mode)
Mel Gorman748446b2010-05-24 14:32:27 -0700555{
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700556 struct zone *zone = cc->zone;
Mel Gormanb7aba692011-01-13 15:45:54 -0800557 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700558 struct list_head *migratelist = &cc->migratepages;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700559 struct lruvec *lruvec;
Mel Gormanc67fe372012-08-21 16:16:17 -0700560 unsigned long flags;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700561 bool locked = false;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700562 struct page *page = NULL, *valid_page = NULL;
Mel Gorman748446b2010-05-24 14:32:27 -0700563
Mel Gorman748446b2010-05-24 14:32:27 -0700564 /*
565 * Ensure that there are not too many pages isolated from the LRU
566 * list by either parallel reclaimers or compaction. If there are,
567 * delay for some time until fewer pages are isolated
568 */
569 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700570 /* async migration should just abort */
David Rientjese0b9dae2014-06-04 16:08:28 -0700571 if (cc->mode == MIGRATE_ASYNC)
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100572 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700573
Mel Gorman748446b2010-05-24 14:32:27 -0700574 congestion_wait(BLK_RW_ASYNC, HZ/10);
575
576 if (fatal_signal_pending(current))
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100577 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700578 }
579
Vlastimil Babkabe976572014-06-04 16:10:41 -0700580 if (compact_should_abort(cc))
581 return 0;
David Rientjesaeef4b82014-06-04 16:08:31 -0700582
Mel Gorman748446b2010-05-24 14:32:27 -0700583 /* Time to isolate some pages for migration */
Mel Gorman748446b2010-05-24 14:32:27 -0700584 for (; low_pfn < end_pfn; low_pfn++) {
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700585 /*
586 * Periodically drop the lock (if held) regardless of its
587 * contention, to give chance to IRQs. Abort async compaction
588 * if contended.
589 */
590 if (!(low_pfn % SWAP_CLUSTER_MAX)
591 && compact_unlock_should_abort(&zone->lru_lock, flags,
592 &locked, cc))
593 break;
Mel Gormanc67fe372012-08-21 16:16:17 -0700594
Mel Gorman748446b2010-05-24 14:32:27 -0700595 if (!pfn_valid_within(low_pfn))
596 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800597 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700598
Mel Gorman748446b2010-05-24 14:32:27 -0700599 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800600
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700601 if (!valid_page)
602 valid_page = page;
603
Mel Gorman6c144662014-01-23 15:53:38 -0800604 /*
605 * Skip if free. page_order cannot be used without zone->lock
606 * as nothing prevents parallel allocations or buddy merging.
607 */
Mel Gorman748446b2010-05-24 14:32:27 -0700608 if (PageBuddy(page))
609 continue;
610
Mel Gorman9927af742011-01-13 15:45:59 -0800611 /*
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800612 * Check may be lockless but that's ok as we recheck later.
613 * It's possible to migrate LRU pages and balloon pages
614 * Skip any other type of page
615 */
616 if (!PageLRU(page)) {
617 if (unlikely(balloon_page_movable(page))) {
618 if (locked && balloon_page_isolate(page)) {
619 /* Successfully isolated */
Joonsoo Kimb6c75012014-04-07 15:37:07 -0700620 goto isolate_success;
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800621 }
622 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800623 continue;
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800624 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800625
626 /*
Mel Gorman2a1402a2012-10-08 16:32:33 -0700627 * PageLRU is set. lru_lock normally excludes isolation
628 * splitting and collapsing (collapsing has already happened
629 * if PageLRU is set) but the lock is not necessarily taken
630 * here and it is wasteful to take it just to check transhuge.
631 * Check TransHuge without lock and skip the whole pageblock if
632 * it's either a transhuge or hugetlbfs page, as calling
633 * compound_order() without preventing THP from splitting the
634 * page underneath us may return surprising results.
Andrea Arcangelibc835012011-01-13 15:47:08 -0800635 */
636 if (PageTransHuge(page)) {
Mel Gorman2a1402a2012-10-08 16:32:33 -0700637 if (!locked)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700638 low_pfn = ALIGN(low_pfn + 1,
639 pageblock_nr_pages) - 1;
640 else
641 low_pfn += (1 << compound_order(page)) - 1;
642
Mel Gorman2a1402a2012-10-08 16:32:33 -0700643 continue;
644 }
645
David Rientjes119d6d52014-04-03 14:48:00 -0700646 /*
647 * Migration will fail if an anonymous page is pinned in memory,
648 * so avoid taking lru_lock and isolating it unnecessarily in an
649 * admittedly racy check.
650 */
651 if (!page_mapping(page) &&
652 page_count(page) > page_mapcount(page))
653 continue;
654
Vlastimil Babka69b71892014-10-09 15:27:18 -0700655 /* If we already hold the lock, we can skip some rechecking */
656 if (!locked) {
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700657 locked = compact_trylock_irqsave(&zone->lru_lock,
658 &flags, cc);
Vlastimil Babka69b71892014-10-09 15:27:18 -0700659 if (!locked)
660 break;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700661
Vlastimil Babka69b71892014-10-09 15:27:18 -0700662 /* Recheck PageLRU and PageTransHuge under lock */
663 if (!PageLRU(page))
664 continue;
665 if (PageTransHuge(page)) {
666 low_pfn += (1 << compound_order(page)) - 1;
667 continue;
668 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800669 }
670
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700671 lruvec = mem_cgroup_page_lruvec(page, zone);
672
Mel Gorman748446b2010-05-24 14:32:27 -0700673 /* Try isolate the page */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700674 if (__isolate_lru_page(page, isolate_mode) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700675 continue;
676
Sasha Levin309381fea2014-01-23 15:52:54 -0800677 VM_BUG_ON_PAGE(PageTransCompound(page), page);
Andrea Arcangelibc835012011-01-13 15:47:08 -0800678
Mel Gorman748446b2010-05-24 14:32:27 -0700679 /* Successfully isolated */
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700680 del_page_from_lru_list(page, lruvec, page_lru(page));
Joonsoo Kimb6c75012014-04-07 15:37:07 -0700681
682isolate_success:
683 cc->finished_update_migrate = true;
Mel Gorman748446b2010-05-24 14:32:27 -0700684 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700685 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800686 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700687
688 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800689 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
690 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700691 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800692 }
Mel Gorman748446b2010-05-24 14:32:27 -0700693 }
694
Mel Gormanc67fe372012-08-21 16:16:17 -0700695 if (locked)
696 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700697
Vlastimil Babka50b5b092014-01-21 15:51:10 -0800698 /*
699 * Update the pageblock-skip information and cached scanner pfn,
700 * if the whole pageblock was scanned without isolating any page.
Vlastimil Babka50b5b092014-01-21 15:51:10 -0800701 */
David Rientjes35979ef2014-06-04 16:08:27 -0700702 if (low_pfn == end_pfn)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700703 update_pageblock_skip(cc, valid_page, nr_isolated, true);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700704
Mel Gormanb7aba692011-01-13 15:45:54 -0800705 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
706
Minchan Kim010fc292012-12-20 15:05:06 -0800707 count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned);
Mel Gorman397487d2012-10-19 12:00:10 +0100708 if (nr_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -0800709 count_compact_events(COMPACTISOLATED, nr_isolated);
Mel Gorman397487d2012-10-19 12:00:10 +0100710
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100711 return low_pfn;
712}
713
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700714/**
715 * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
716 * @cc: Compaction control structure.
717 * @start_pfn: The first PFN to start isolating.
718 * @end_pfn: The one-past-last PFN.
719 *
720 * Returns zero if isolation fails fatally due to e.g. pending signal.
721 * Otherwise, function returns one-past-the-last PFN of isolated page
722 * (which may be greater than end_pfn if end fell in a middle of a THP page).
723 */
724unsigned long
725isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
726 unsigned long end_pfn)
727{
728 unsigned long pfn, block_end_pfn;
729
730 /* Scan block by block. First and last block may be incomplete */
731 pfn = start_pfn;
732 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
733
734 for (; pfn < end_pfn; pfn = block_end_pfn,
735 block_end_pfn += pageblock_nr_pages) {
736
737 block_end_pfn = min(block_end_pfn, end_pfn);
738
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700739 if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700740 continue;
741
742 pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
743 ISOLATE_UNEVICTABLE);
744
745 /*
746 * In case of fatal failure, release everything that might
747 * have been isolated in the previous iteration, and signal
748 * the failure back to caller.
749 */
750 if (!pfn) {
751 putback_movable_pages(&cc->migratepages);
752 cc->nr_migratepages = 0;
753 break;
754 }
755 }
756 acct_isolated(cc->zone, cc);
757
758 return pfn;
759}
760
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100761#endif /* CONFIG_COMPACTION || CONFIG_CMA */
762#ifdef CONFIG_COMPACTION
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100763/*
764 * Based on information in the current compact_control, find blocks
765 * suitable for isolating free pages from and then isolate them.
766 */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700767static void isolate_freepages(struct compact_control *cc)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100768{
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700769 struct zone *zone = cc->zone;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100770 struct page *page;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700771 unsigned long block_start_pfn; /* start of current pageblock */
772 unsigned long block_end_pfn; /* end of current pageblock */
773 unsigned long low_pfn; /* lowest pfn scanner is able to scan */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100774 int nr_freepages = cc->nr_freepages;
775 struct list_head *freelist = &cc->freepages;
776
777 /*
778 * Initialise the free scanner. The starting point is where we last
Vlastimil Babka49e068f2014-05-06 12:50:03 -0700779 * successfully isolated from, zone-cached value, or the end of the
780 * zone when isolating for the first time. We need this aligned to
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700781 * the pageblock boundary, because we do
782 * block_start_pfn -= pageblock_nr_pages in the for loop.
783 * For ending point, take care when isolating in last pageblock of a
784 * a zone which ends in the middle of a pageblock.
Vlastimil Babka49e068f2014-05-06 12:50:03 -0700785 * The low boundary is the end of the pageblock the migration scanner
786 * is using.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100787 */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700788 block_start_pfn = cc->free_pfn & ~(pageblock_nr_pages-1);
789 block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
790 zone_end_pfn(zone));
Vlastimil Babka7ed695e2014-01-21 15:51:09 -0800791 low_pfn = ALIGN(cc->migrate_pfn + 1, pageblock_nr_pages);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100792
793 /*
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100794 * Isolate free pages until enough are available to migrate the
795 * pages on cc->migratepages. We stop searching if the migrate
796 * and free page scanners meet or enough free pages are isolated.
797 */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700798 for (; block_start_pfn >= low_pfn && cc->nr_migratepages > nr_freepages;
799 block_end_pfn = block_start_pfn,
800 block_start_pfn -= pageblock_nr_pages) {
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100801 unsigned long isolated;
802
David Rientjesf6ea3ad2013-09-30 13:45:03 -0700803 /*
804 * This can iterate a massively long zone without finding any
805 * suitable migration targets, so periodically check if we need
Vlastimil Babkabe976572014-06-04 16:10:41 -0700806 * to schedule, or even abort async compaction.
David Rientjesf6ea3ad2013-09-30 13:45:03 -0700807 */
Vlastimil Babkabe976572014-06-04 16:10:41 -0700808 if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
809 && compact_should_abort(cc))
810 break;
David Rientjesf6ea3ad2013-09-30 13:45:03 -0700811
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700812 page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
813 zone);
814 if (!page)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100815 continue;
816
817 /* Check the block is suitable for migration */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700818 if (!suitable_migration_target(page))
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100819 continue;
Linus Torvalds68e3e922012-06-03 20:05:57 -0700820
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700821 /* If isolation recently failed, do not retry */
822 if (!isolation_suitable(cc, page))
823 continue;
824
Mel Gormanf40d1e42012-10-08 16:32:36 -0700825 /* Found a block suitable for isolating free pages from */
Vlastimil Babkae9ade562014-06-04 16:08:34 -0700826 cc->free_pfn = block_start_pfn;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700827 isolated = isolate_freepages_block(cc, block_start_pfn,
828 block_end_pfn, freelist, false);
Mel Gormanf40d1e42012-10-08 16:32:36 -0700829 nr_freepages += isolated;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100830
831 /*
Vlastimil Babkae9ade562014-06-04 16:08:34 -0700832 * Set a flag that we successfully isolated in this pageblock.
833 * In the next loop iteration, zone->compact_cached_free_pfn
834 * will not be updated and thus it will effectively contain the
835 * highest pageblock we isolated pages from.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100836 */
Vlastimil Babkae9ade562014-06-04 16:08:34 -0700837 if (isolated)
Mel Gormanc89511a2012-10-08 16:32:45 -0700838 cc->finished_update_free = true;
Vlastimil Babkabe976572014-06-04 16:10:41 -0700839
840 /*
841 * isolate_freepages_block() might have aborted due to async
842 * compaction being contended
843 */
844 if (cc->contended)
845 break;
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100846 }
847
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100848 /* split_free_page does not map the pages */
849 map_pages(freelist);
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100850
Vlastimil Babka7ed695e2014-01-21 15:51:09 -0800851 /*
852 * If we crossed the migrate scanner, we want to keep it that way
853 * so that compact_finished() may detect this
854 */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700855 if (block_start_pfn < low_pfn)
Vlastimil Babkae9ade562014-06-04 16:08:34 -0700856 cc->free_pfn = cc->migrate_pfn;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700857
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100858 cc->nr_freepages = nr_freepages;
Mel Gorman748446b2010-05-24 14:32:27 -0700859}
860
861/*
862 * This is a migrate-callback that "allocates" freepages by taking pages
863 * from the isolated freelists in the block we are migrating to.
864 */
865static struct page *compaction_alloc(struct page *migratepage,
866 unsigned long data,
867 int **result)
868{
869 struct compact_control *cc = (struct compact_control *)data;
870 struct page *freepage;
871
Vlastimil Babkabe976572014-06-04 16:10:41 -0700872 /*
873 * Isolate free pages if necessary, and if we are not aborting due to
874 * contention.
875 */
Mel Gorman748446b2010-05-24 14:32:27 -0700876 if (list_empty(&cc->freepages)) {
Vlastimil Babkabe976572014-06-04 16:10:41 -0700877 if (!cc->contended)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700878 isolate_freepages(cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700879
880 if (list_empty(&cc->freepages))
881 return NULL;
882 }
883
884 freepage = list_entry(cc->freepages.next, struct page, lru);
885 list_del(&freepage->lru);
886 cc->nr_freepages--;
887
888 return freepage;
889}
890
891/*
David Rientjesd53aea32014-06-04 16:08:26 -0700892 * This is a migrate-callback that "frees" freepages back to the isolated
893 * freelist. All pages on the freelist are from the same zone, so there is no
894 * special handling needed for NUMA.
895 */
896static void compaction_free(struct page *page, unsigned long data)
897{
898 struct compact_control *cc = (struct compact_control *)data;
899
900 list_add(&page->lru, &cc->freepages);
901 cc->nr_freepages++;
902}
903
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100904/* possible outcome of isolate_migratepages */
905typedef enum {
906 ISOLATE_ABORT, /* Abort compaction now */
907 ISOLATE_NONE, /* No pages isolated, continue scanning */
908 ISOLATE_SUCCESS, /* Pages isolated, migrate */
909} isolate_migrate_t;
910
911/*
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700912 * Isolate all pages that can be migrated from the first suitable block,
913 * starting at the block pointed to by the migrate scanner pfn within
914 * compact_control.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100915 */
916static isolate_migrate_t isolate_migratepages(struct zone *zone,
917 struct compact_control *cc)
918{
919 unsigned long low_pfn, end_pfn;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700920 struct page *page;
921 const isolate_mode_t isolate_mode =
922 (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100923
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700924 /*
925 * Start at where we last stopped, or beginning of the zone as
926 * initialized by compact_zone()
927 */
928 low_pfn = cc->migrate_pfn;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100929
930 /* Only scan within a pageblock boundary */
Mel Gormana9aacbc2013-02-22 16:32:25 -0800931 end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100932
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700933 /*
934 * Iterate over whole pageblocks until we find the first suitable.
935 * Do not cross the free scanner.
936 */
937 for (; end_pfn <= cc->free_pfn;
938 low_pfn = end_pfn, end_pfn += pageblock_nr_pages) {
939
940 /*
941 * This can potentially iterate a massively long zone with
942 * many pageblocks unsuitable, so periodically check if we
943 * need to schedule, or even abort async compaction.
944 */
945 if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
946 && compact_should_abort(cc))
947 break;
948
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700949 page = pageblock_pfn_to_page(low_pfn, end_pfn, zone);
950 if (!page)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700951 continue;
952
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700953 /* If isolation recently failed, do not retry */
954 if (!isolation_suitable(cc, page))
955 continue;
956
957 /*
958 * For async compaction, also only scan in MOVABLE blocks.
959 * Async compaction is optimistic to see if the minimum amount
960 * of work satisfies the allocation.
961 */
962 if (cc->mode == MIGRATE_ASYNC &&
963 !migrate_async_suitable(get_pageblock_migratetype(page)))
964 continue;
965
966 /* Perform the isolation */
967 low_pfn = isolate_migratepages_block(cc, low_pfn, end_pfn,
968 isolate_mode);
969
970 if (!low_pfn || cc->contended)
971 return ISOLATE_ABORT;
972
973 /*
974 * Either we isolated something and proceed with migration. Or
975 * we failed and compact_zone should decide if we should
976 * continue or not.
977 */
978 break;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100979 }
980
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700981 acct_isolated(zone, cc);
982 /* Record where migration scanner will be restarted */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100983 cc->migrate_pfn = low_pfn;
984
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700985 return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100986}
987
Mel Gorman748446b2010-05-24 14:32:27 -0700988static int compact_finished(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800989 struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700990{
Mel Gorman8fb74b92013-01-11 14:32:16 -0800991 unsigned int order;
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800992 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -0700993
Vlastimil Babkabe976572014-06-04 16:10:41 -0700994 if (cc->contended || fatal_signal_pending(current))
Mel Gorman748446b2010-05-24 14:32:27 -0700995 return COMPACT_PARTIAL;
996
Mel Gorman753341a2012-10-08 16:32:40 -0700997 /* Compaction run completes if the migrate and free scanner meet */
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700998 if (cc->free_pfn <= cc->migrate_pfn) {
Vlastimil Babka55b7c4c2014-01-21 15:51:11 -0800999 /* Let the next compaction start anew. */
David Rientjes35979ef2014-06-04 16:08:27 -07001000 zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
1001 zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
Vlastimil Babka55b7c4c2014-01-21 15:51:11 -08001002 zone->compact_cached_free_pfn = zone_end_pfn(zone);
1003
Mel Gorman62997022012-10-08 16:32:47 -07001004 /*
1005 * Mark that the PG_migrate_skip information should be cleared
1006 * by kswapd when it goes to sleep. kswapd does not set the
1007 * flag itself as the decision to be clear should be directly
1008 * based on an allocation request.
1009 */
1010 if (!current_is_kswapd())
1011 zone->compact_blockskip_flush = true;
1012
Mel Gorman748446b2010-05-24 14:32:27 -07001013 return COMPACT_COMPLETE;
Mel Gormanbb13ffe2012-10-08 16:32:41 -07001014 }
Mel Gorman748446b2010-05-24 14:32:27 -07001015
Johannes Weiner82478fb2011-01-20 14:44:21 -08001016 /*
1017 * order == -1 is expected when compacting via
1018 * /proc/sys/vm/compact_memory
1019 */
Mel Gorman56de7262010-05-24 14:32:30 -07001020 if (cc->order == -1)
1021 return COMPACT_CONTINUE;
1022
Michal Hocko3957c772011-06-15 15:08:25 -07001023 /* Compaction run is not finished if the watermark is not met */
1024 watermark = low_wmark_pages(zone);
1025 watermark += (1 << cc->order);
1026
1027 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
1028 return COMPACT_CONTINUE;
1029
Mel Gorman56de7262010-05-24 14:32:30 -07001030 /* Direct compactor: Is a suitable page free? */
Mel Gorman8fb74b92013-01-11 14:32:16 -08001031 for (order = cc->order; order < MAX_ORDER; order++) {
1032 struct free_area *area = &zone->free_area[order];
Mel Gorman56de7262010-05-24 14:32:30 -07001033
Mel Gorman8fb74b92013-01-11 14:32:16 -08001034 /* Job done if page is free of the right migratetype */
1035 if (!list_empty(&area->free_list[cc->migratetype]))
1036 return COMPACT_PARTIAL;
1037
1038 /* Job done if allocation would set block type */
1039 if (cc->order >= pageblock_order && area->nr_free)
1040 return COMPACT_PARTIAL;
Mel Gorman56de7262010-05-24 14:32:30 -07001041 }
1042
Mel Gorman748446b2010-05-24 14:32:27 -07001043 return COMPACT_CONTINUE;
1044}
1045
Mel Gorman3e7d3442011-01-13 15:45:56 -08001046/*
1047 * compaction_suitable: Is this suitable to run compaction on this zone now?
1048 * Returns
1049 * COMPACT_SKIPPED - If there are too few free pages for compaction
1050 * COMPACT_PARTIAL - If the allocation would succeed without compaction
1051 * COMPACT_CONTINUE - If compaction should run now
1052 */
1053unsigned long compaction_suitable(struct zone *zone, int order)
1054{
1055 int fragindex;
1056 unsigned long watermark;
1057
1058 /*
Michal Hocko3957c772011-06-15 15:08:25 -07001059 * order == -1 is expected when compacting via
1060 * /proc/sys/vm/compact_memory
1061 */
1062 if (order == -1)
1063 return COMPACT_CONTINUE;
1064
1065 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -08001066 * Watermarks for order-0 must be met for compaction. Note the 2UL.
1067 * This is because during migration, copies of pages need to be
1068 * allocated and for a short time, the footprint is higher
1069 */
1070 watermark = low_wmark_pages(zone) + (2UL << order);
1071 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
1072 return COMPACT_SKIPPED;
1073
1074 /*
1075 * fragmentation index determines if allocation failures are due to
1076 * low memory or external fragmentation
1077 *
Shaohua Lia582a732011-06-15 15:08:49 -07001078 * index of -1000 implies allocations might succeed depending on
1079 * watermarks
Mel Gorman3e7d3442011-01-13 15:45:56 -08001080 * index towards 0 implies failure is due to lack of memory
1081 * index towards 1000 implies failure is due to fragmentation
1082 *
1083 * Only compact if a failure would be due to fragmentation.
1084 */
1085 fragindex = fragmentation_index(zone, order);
1086 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
1087 return COMPACT_SKIPPED;
1088
Shaohua Lia582a732011-06-15 15:08:49 -07001089 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
1090 0, 0))
Mel Gorman3e7d3442011-01-13 15:45:56 -08001091 return COMPACT_PARTIAL;
1092
1093 return COMPACT_CONTINUE;
1094}
1095
Mel Gorman748446b2010-05-24 14:32:27 -07001096static int compact_zone(struct zone *zone, struct compact_control *cc)
1097{
1098 int ret;
Mel Gormanc89511a2012-10-08 16:32:45 -07001099 unsigned long start_pfn = zone->zone_start_pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -08001100 unsigned long end_pfn = zone_end_pfn(zone);
David Rientjese0b9dae2014-06-04 16:08:28 -07001101 const bool sync = cc->mode != MIGRATE_ASYNC;
Mel Gorman748446b2010-05-24 14:32:27 -07001102
Mel Gorman3e7d3442011-01-13 15:45:56 -08001103 ret = compaction_suitable(zone, cc->order);
1104 switch (ret) {
1105 case COMPACT_PARTIAL:
1106 case COMPACT_SKIPPED:
1107 /* Compaction is likely to fail */
1108 return ret;
1109 case COMPACT_CONTINUE:
1110 /* Fall through to compaction */
1111 ;
1112 }
1113
Mel Gormanc89511a2012-10-08 16:32:45 -07001114 /*
Vlastimil Babkad3132e42014-01-21 15:51:08 -08001115 * Clear pageblock skip if there were failures recently and compaction
1116 * is about to be retried after being deferred. kswapd does not do
1117 * this reset as it'll reset the cached information when going to sleep.
1118 */
1119 if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
1120 __reset_isolation_suitable(zone);
1121
1122 /*
Mel Gormanc89511a2012-10-08 16:32:45 -07001123 * Setup to move all movable pages to the end of the zone. Used cached
1124 * information on where the scanners should start but check that it
1125 * is initialised by ensuring the values are within zone boundaries.
1126 */
David Rientjese0b9dae2014-06-04 16:08:28 -07001127 cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync];
Mel Gormanc89511a2012-10-08 16:32:45 -07001128 cc->free_pfn = zone->compact_cached_free_pfn;
1129 if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
1130 cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
1131 zone->compact_cached_free_pfn = cc->free_pfn;
1132 }
1133 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
1134 cc->migrate_pfn = start_pfn;
David Rientjes35979ef2014-06-04 16:08:27 -07001135 zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
1136 zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
Mel Gormanc89511a2012-10-08 16:32:45 -07001137 }
Mel Gorman748446b2010-05-24 14:32:27 -07001138
Mel Gorman0eb927c2014-01-21 15:51:05 -08001139 trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, cc->free_pfn, end_pfn);
1140
Mel Gorman748446b2010-05-24 14:32:27 -07001141 migrate_prep_local();
1142
1143 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
Minchan Kim9d502c12011-03-22 16:30:39 -07001144 int err;
Mel Gorman748446b2010-05-24 14:32:27 -07001145
Mel Gormanf9e35b32011-06-15 15:08:52 -07001146 switch (isolate_migratepages(zone, cc)) {
1147 case ISOLATE_ABORT:
1148 ret = COMPACT_PARTIAL;
Rafael Aquini5733c7d2012-12-11 16:02:47 -08001149 putback_movable_pages(&cc->migratepages);
Shaohua Lie64c5232012-10-08 16:32:27 -07001150 cc->nr_migratepages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -07001151 goto out;
1152 case ISOLATE_NONE:
Mel Gorman748446b2010-05-24 14:32:27 -07001153 continue;
Mel Gormanf9e35b32011-06-15 15:08:52 -07001154 case ISOLATE_SUCCESS:
1155 ;
1156 }
Mel Gorman748446b2010-05-24 14:32:27 -07001157
David Rientjesd53aea32014-06-04 16:08:26 -07001158 err = migrate_pages(&cc->migratepages, compaction_alloc,
David Rientjese0b9dae2014-06-04 16:08:28 -07001159 compaction_free, (unsigned long)cc, cc->mode,
Mel Gorman7b2a2d42012-10-19 14:07:31 +01001160 MR_COMPACTION);
Mel Gorman748446b2010-05-24 14:32:27 -07001161
Vlastimil Babkaf8c93012014-06-04 16:08:32 -07001162 trace_mm_compaction_migratepages(cc->nr_migratepages, err,
1163 &cc->migratepages);
Mel Gorman748446b2010-05-24 14:32:27 -07001164
Vlastimil Babkaf8c93012014-06-04 16:08:32 -07001165 /* All pages were either migrated or will be released */
1166 cc->nr_migratepages = 0;
Minchan Kim9d502c12011-03-22 16:30:39 -07001167 if (err) {
Rafael Aquini5733c7d2012-12-11 16:02:47 -08001168 putback_movable_pages(&cc->migratepages);
Vlastimil Babka7ed695e2014-01-21 15:51:09 -08001169 /*
1170 * migrate_pages() may return -ENOMEM when scanners meet
1171 * and we want compact_finished() to detect it
1172 */
1173 if (err == -ENOMEM && cc->free_pfn > cc->migrate_pfn) {
David Rientjes4bf2bba2012-07-11 14:02:13 -07001174 ret = COMPACT_PARTIAL;
1175 goto out;
1176 }
Mel Gorman748446b2010-05-24 14:32:27 -07001177 }
Mel Gorman748446b2010-05-24 14:32:27 -07001178 }
1179
Mel Gormanf9e35b32011-06-15 15:08:52 -07001180out:
Mel Gorman748446b2010-05-24 14:32:27 -07001181 /* Release free pages and check accounting */
1182 cc->nr_freepages -= release_freepages(&cc->freepages);
1183 VM_BUG_ON(cc->nr_freepages != 0);
1184
Mel Gorman0eb927c2014-01-21 15:51:05 -08001185 trace_mm_compaction_end(ret);
1186
Mel Gorman748446b2010-05-24 14:32:27 -07001187 return ret;
1188}
Mel Gorman76ab0f52010-05-24 14:32:28 -07001189
David Rientjese0b9dae2014-06-04 16:08:28 -07001190static unsigned long compact_zone_order(struct zone *zone, int order,
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001191 gfp_t gfp_mask, enum migrate_mode mode, int *contended)
Mel Gorman56de7262010-05-24 14:32:30 -07001192{
Shaohua Lie64c5232012-10-08 16:32:27 -07001193 unsigned long ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001194 struct compact_control cc = {
1195 .nr_freepages = 0,
1196 .nr_migratepages = 0,
1197 .order = order,
1198 .migratetype = allocflags_to_migratetype(gfp_mask),
1199 .zone = zone,
David Rientjese0b9dae2014-06-04 16:08:28 -07001200 .mode = mode,
Mel Gorman56de7262010-05-24 14:32:30 -07001201 };
1202 INIT_LIST_HEAD(&cc.freepages);
1203 INIT_LIST_HEAD(&cc.migratepages);
1204
Shaohua Lie64c5232012-10-08 16:32:27 -07001205 ret = compact_zone(zone, &cc);
1206
1207 VM_BUG_ON(!list_empty(&cc.freepages));
1208 VM_BUG_ON(!list_empty(&cc.migratepages));
1209
1210 *contended = cc.contended;
1211 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001212}
1213
Mel Gorman5e771902010-05-24 14:32:31 -07001214int sysctl_extfrag_threshold = 500;
1215
Mel Gorman56de7262010-05-24 14:32:30 -07001216/**
1217 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
1218 * @zonelist: The zonelist used for the current allocation
1219 * @order: The order of the current allocation
1220 * @gfp_mask: The GFP mask of the current allocation
1221 * @nodemask: The allowed nodes to allocate from
David Rientjese0b9dae2014-06-04 16:08:28 -07001222 * @mode: The migration mode for async, sync light, or sync migration
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001223 * @contended: Return value that determines if compaction was aborted due to
1224 * need_resched() or lock contention
Vlastimil Babka53853e22014-10-09 15:27:02 -07001225 * @candidate_zone: Return the zone where we think allocation should succeed
Mel Gorman56de7262010-05-24 14:32:30 -07001226 *
1227 * This is the main entry point for direct page compaction.
1228 */
1229unsigned long try_to_compact_pages(struct zonelist *zonelist,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001230 int order, gfp_t gfp_mask, nodemask_t *nodemask,
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001231 enum migrate_mode mode, int *contended,
Vlastimil Babka53853e22014-10-09 15:27:02 -07001232 struct zone **candidate_zone)
Mel Gorman56de7262010-05-24 14:32:30 -07001233{
1234 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
1235 int may_enter_fs = gfp_mask & __GFP_FS;
1236 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -07001237 struct zoneref *z;
1238 struct zone *zone;
Vlastimil Babka53853e22014-10-09 15:27:02 -07001239 int rc = COMPACT_DEFERRED;
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001240 int alloc_flags = 0;
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001241 int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */
1242
1243 *contended = COMPACT_CONTENDED_NONE;
Mel Gorman56de7262010-05-24 14:32:30 -07001244
Mel Gorman4ffb6332012-10-08 16:29:09 -07001245 /* Check if the GFP flags allow compaction */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -08001246 if (!order || !may_enter_fs || !may_perform_io)
Vlastimil Babka53853e22014-10-09 15:27:02 -07001247 return COMPACT_SKIPPED;
Mel Gorman56de7262010-05-24 14:32:30 -07001248
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001249#ifdef CONFIG_CMA
1250 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
1251 alloc_flags |= ALLOC_CMA;
1252#endif
Mel Gorman56de7262010-05-24 14:32:30 -07001253 /* Compact each zone in the list */
1254 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
1255 nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -07001256 int status;
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001257 int zone_contended;
Mel Gorman56de7262010-05-24 14:32:30 -07001258
Vlastimil Babka53853e22014-10-09 15:27:02 -07001259 if (compaction_deferred(zone, order))
1260 continue;
1261
David Rientjese0b9dae2014-06-04 16:08:28 -07001262 status = compact_zone_order(zone, order, gfp_mask, mode,
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001263 &zone_contended);
Mel Gorman56de7262010-05-24 14:32:30 -07001264 rc = max(status, rc);
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001265 /*
1266 * It takes at least one zone that wasn't lock contended
1267 * to clear all_zones_contended.
1268 */
1269 all_zones_contended &= zone_contended;
Mel Gorman56de7262010-05-24 14:32:30 -07001270
Mel Gorman3e7d3442011-01-13 15:45:56 -08001271 /* If a normal allocation would succeed, stop compacting */
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001272 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
Vlastimil Babka53853e22014-10-09 15:27:02 -07001273 alloc_flags)) {
1274 *candidate_zone = zone;
1275 /*
1276 * We think the allocation will succeed in this zone,
1277 * but it is not certain, hence the false. The caller
1278 * will repeat this with true if allocation indeed
1279 * succeeds in this zone.
1280 */
1281 compaction_defer_reset(zone, order, false);
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001282 /*
1283 * It is possible that async compaction aborted due to
1284 * need_resched() and the watermarks were ok thanks to
1285 * somebody else freeing memory. The allocation can
1286 * however still fail so we better signal the
1287 * need_resched() contention anyway (this will not
1288 * prevent the allocation attempt).
1289 */
1290 if (zone_contended == COMPACT_CONTENDED_SCHED)
1291 *contended = COMPACT_CONTENDED_SCHED;
1292
1293 goto break_loop;
1294 }
1295
1296 if (mode != MIGRATE_ASYNC) {
Vlastimil Babka53853e22014-10-09 15:27:02 -07001297 /*
1298 * We think that allocation won't succeed in this zone
1299 * so we defer compaction there. If it ends up
1300 * succeeding after all, it will be reset.
1301 */
1302 defer_compaction(zone, order);
1303 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001304
1305 /*
1306 * We might have stopped compacting due to need_resched() in
1307 * async compaction, or due to a fatal signal detected. In that
1308 * case do not try further zones and signal need_resched()
1309 * contention.
1310 */
1311 if ((zone_contended == COMPACT_CONTENDED_SCHED)
1312 || fatal_signal_pending(current)) {
1313 *contended = COMPACT_CONTENDED_SCHED;
1314 goto break_loop;
1315 }
1316
1317 continue;
1318break_loop:
1319 /*
1320 * We might not have tried all the zones, so be conservative
1321 * and assume they are not all lock contended.
1322 */
1323 all_zones_contended = 0;
1324 break;
Mel Gorman56de7262010-05-24 14:32:30 -07001325 }
1326
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001327 /*
1328 * If at least one zone wasn't deferred or skipped, we report if all
1329 * zones that were tried were lock contended.
1330 */
1331 if (rc > COMPACT_SKIPPED && all_zones_contended)
1332 *contended = COMPACT_CONTENDED_LOCK;
1333
Mel Gorman56de7262010-05-24 14:32:30 -07001334 return rc;
1335}
1336
1337
Mel Gorman76ab0f52010-05-24 14:32:28 -07001338/* Compact all zones within a node */
Andrew Morton7103f162013-02-22 16:32:33 -08001339static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001340{
1341 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -07001342 struct zone *zone;
1343
Mel Gorman76ab0f52010-05-24 14:32:28 -07001344 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -07001345
1346 zone = &pgdat->node_zones[zoneid];
1347 if (!populated_zone(zone))
1348 continue;
1349
Rik van Riel7be62de2012-03-21 16:33:52 -07001350 cc->nr_freepages = 0;
1351 cc->nr_migratepages = 0;
1352 cc->zone = zone;
1353 INIT_LIST_HEAD(&cc->freepages);
1354 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001355
Dan Carpenteraad6ec32012-03-21 16:33:54 -07001356 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -07001357 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001358
Rik van Rielaff62242012-03-21 16:33:52 -07001359 if (cc->order > 0) {
Vlastimil Babkade6c60a2014-01-21 15:51:07 -08001360 if (zone_watermark_ok(zone, cc->order,
1361 low_wmark_pages(zone), 0, 0))
1362 compaction_defer_reset(zone, cc->order, false);
Rik van Rielaff62242012-03-21 16:33:52 -07001363 }
1364
Rik van Riel7be62de2012-03-21 16:33:52 -07001365 VM_BUG_ON(!list_empty(&cc->freepages));
1366 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -07001367 }
Mel Gorman76ab0f52010-05-24 14:32:28 -07001368}
1369
Andrew Morton7103f162013-02-22 16:32:33 -08001370void compact_pgdat(pg_data_t *pgdat, int order)
Rik van Riel7be62de2012-03-21 16:33:52 -07001371{
1372 struct compact_control cc = {
1373 .order = order,
David Rientjese0b9dae2014-06-04 16:08:28 -07001374 .mode = MIGRATE_ASYNC,
Rik van Riel7be62de2012-03-21 16:33:52 -07001375 };
1376
Mel Gorman3a7200a2013-09-11 14:22:19 -07001377 if (!order)
1378 return;
1379
Andrew Morton7103f162013-02-22 16:32:33 -08001380 __compact_pgdat(pgdat, &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001381}
1382
Andrew Morton7103f162013-02-22 16:32:33 -08001383static void compact_node(int nid)
Rik van Riel7be62de2012-03-21 16:33:52 -07001384{
Rik van Riel7be62de2012-03-21 16:33:52 -07001385 struct compact_control cc = {
1386 .order = -1,
David Rientjese0b9dae2014-06-04 16:08:28 -07001387 .mode = MIGRATE_SYNC,
David Rientjes91ca9182014-04-03 14:47:23 -07001388 .ignore_skip_hint = true,
Rik van Riel7be62de2012-03-21 16:33:52 -07001389 };
1390
Andrew Morton7103f162013-02-22 16:32:33 -08001391 __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001392}
1393
Mel Gorman76ab0f52010-05-24 14:32:28 -07001394/* Compact all nodes in the system */
Jason Liu7964c062013-01-11 14:31:47 -08001395static void compact_nodes(void)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001396{
1397 int nid;
1398
Hugh Dickins8575ec22012-03-21 16:33:53 -07001399 /* Flush pending updates to the LRU lists */
1400 lru_add_drain_all();
1401
Mel Gorman76ab0f52010-05-24 14:32:28 -07001402 for_each_online_node(nid)
1403 compact_node(nid);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001404}
1405
1406/* The written value is actually unused, all memory is compacted */
1407int sysctl_compact_memory;
1408
1409/* This is the entry point for compacting all nodes via /proc/sys/vm */
1410int sysctl_compaction_handler(struct ctl_table *table, int write,
1411 void __user *buffer, size_t *length, loff_t *ppos)
1412{
1413 if (write)
Jason Liu7964c062013-01-11 14:31:47 -08001414 compact_nodes();
Mel Gorman76ab0f52010-05-24 14:32:28 -07001415
1416 return 0;
1417}
Mel Gormaned4a6d72010-05-24 14:32:29 -07001418
Mel Gorman5e771902010-05-24 14:32:31 -07001419int sysctl_extfrag_handler(struct ctl_table *table, int write,
1420 void __user *buffer, size_t *length, loff_t *ppos)
1421{
1422 proc_dointvec_minmax(table, write, buffer, length, ppos);
1423
1424 return 0;
1425}
1426
Mel Gormaned4a6d72010-05-24 14:32:29 -07001427#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Rashika Kheria74e77fb2014-04-03 14:48:01 -07001428static ssize_t sysfs_compact_node(struct device *dev,
Kay Sievers10fbcf42011-12-21 14:48:43 -08001429 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -07001430 const char *buf, size_t count)
1431{
Hugh Dickins8575ec22012-03-21 16:33:53 -07001432 int nid = dev->id;
1433
1434 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1435 /* Flush pending updates to the LRU lists */
1436 lru_add_drain_all();
1437
1438 compact_node(nid);
1439 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07001440
1441 return count;
1442}
Kay Sievers10fbcf42011-12-21 14:48:43 -08001443static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001444
1445int compaction_register_node(struct node *node)
1446{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001447 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001448}
1449
1450void compaction_unregister_node(struct node *node)
1451{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001452 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001453}
1454#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001455
1456#endif /* CONFIG_COMPACTION */