blob: 74770e40cfe59a32f1d4de0c512a24dfed2b36d5 [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 /*
370 * The zone lock must be held to isolate freepages.
371 * Unfortunately this is a very coarse lock and can be
372 * heavily contended if there are parallel allocations
373 * or parallel compactions. For async compaction do not
374 * spin on the lock and we acquire the lock as late as
375 * possible.
376 */
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700377 if (!locked)
378 locked = compact_trylock_irqsave(&cc->zone->lock,
379 &flags, cc);
Mel Gormanf40d1e42012-10-08 16:32:36 -0700380 if (!locked)
381 break;
382
Mel Gormanf40d1e42012-10-08 16:32:36 -0700383 /* Recheck this is a buddy page under lock */
384 if (!PageBuddy(page))
Laura Abbott2af120b2014-03-10 15:49:44 -0700385 goto isolate_fail;
Mel Gorman748446b2010-05-24 14:32:27 -0700386
387 /* Found a free page, break it into order-0 pages */
388 isolated = split_free_page(page);
389 total_isolated += isolated;
390 for (i = 0; i < isolated; i++) {
391 list_add(&page->lru, freelist);
392 page++;
393 }
394
395 /* If a page was split, advance to the end of it */
396 if (isolated) {
397 blockpfn += isolated - 1;
398 cursor += isolated - 1;
Laura Abbott2af120b2014-03-10 15:49:44 -0700399 continue;
Mel Gorman748446b2010-05-24 14:32:27 -0700400 }
Laura Abbott2af120b2014-03-10 15:49:44 -0700401
402isolate_fail:
403 if (strict)
404 break;
405 else
406 continue;
407
Mel Gorman748446b2010-05-24 14:32:27 -0700408 }
409
Mel Gormanb7aba692011-01-13 15:45:54 -0800410 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
Mel Gormanf40d1e42012-10-08 16:32:36 -0700411
412 /*
413 * If strict isolation is requested by CMA then check that all the
414 * pages requested were isolated. If there were any failures, 0 is
415 * returned and CMA will fail.
416 */
Laura Abbott2af120b2014-03-10 15:49:44 -0700417 if (strict && blockpfn < end_pfn)
Mel Gormanf40d1e42012-10-08 16:32:36 -0700418 total_isolated = 0;
419
420 if (locked)
421 spin_unlock_irqrestore(&cc->zone->lock, flags);
422
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700423 /* Update the pageblock-skip if the whole pageblock was scanned */
424 if (blockpfn == end_pfn)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700425 update_pageblock_skip(cc, valid_page, total_isolated, false);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700426
Minchan Kim010fc292012-12-20 15:05:06 -0800427 count_compact_events(COMPACTFREE_SCANNED, nr_scanned);
Mel Gorman397487d2012-10-19 12:00:10 +0100428 if (total_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -0800429 count_compact_events(COMPACTISOLATED, total_isolated);
Mel Gorman748446b2010-05-24 14:32:27 -0700430 return total_isolated;
431}
432
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100433/**
434 * isolate_freepages_range() - isolate free pages.
435 * @start_pfn: The first PFN to start isolating.
436 * @end_pfn: The one-past-last PFN.
437 *
438 * Non-free pages, invalid PFNs, or zone boundaries within the
439 * [start_pfn, end_pfn) range are considered errors, cause function to
440 * undo its actions and return zero.
441 *
442 * Otherwise, function returns one-past-the-last PFN of isolated page
443 * (which may be greater then end_pfn if end fell in a middle of
444 * a free page).
445 */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100446unsigned long
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700447isolate_freepages_range(struct compact_control *cc,
448 unsigned long start_pfn, unsigned long end_pfn)
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100449{
Mel Gormanf40d1e42012-10-08 16:32:36 -0700450 unsigned long isolated, pfn, block_end_pfn;
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100451 LIST_HEAD(freelist);
452
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700453 pfn = start_pfn;
454 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100455
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700456 for (; pfn < end_pfn; pfn += isolated,
457 block_end_pfn += pageblock_nr_pages) {
458
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100459 block_end_pfn = min(block_end_pfn, end_pfn);
460
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700461 if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
462 break;
463
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700464 isolated = isolate_freepages_block(cc, pfn, block_end_pfn,
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100465 &freelist, true);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100466
467 /*
468 * In strict mode, isolate_freepages_block() returns 0 if
469 * there are any holes in the block (ie. invalid PFNs or
470 * non-free pages).
471 */
472 if (!isolated)
473 break;
474
475 /*
476 * If we managed to isolate pages, it is always (1 << n) *
477 * pageblock_nr_pages for some non-negative n. (Max order
478 * page may span two pageblocks).
479 */
480 }
481
482 /* split_free_page does not map the pages */
483 map_pages(&freelist);
484
485 if (pfn < end_pfn) {
486 /* Loop terminated early, cleanup. */
487 release_freepages(&freelist);
488 return 0;
489 }
490
491 /* We don't use freelists for anything. */
492 return pfn;
493}
494
Mel Gorman748446b2010-05-24 14:32:27 -0700495/* Update the number of anon and file isolated pages in the zone */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700496static void acct_isolated(struct zone *zone, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700497{
498 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700499 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700500
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700501 if (list_empty(&cc->migratepages))
502 return;
503
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700504 list_for_each_entry(page, &cc->migratepages, lru)
505 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700506
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700507 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
508 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
Mel Gorman748446b2010-05-24 14:32:27 -0700509}
510
511/* Similar to reclaim, but different enough that they don't share logic */
512static bool too_many_isolated(struct zone *zone)
513{
Minchan Kimbc693042010-09-09 16:38:00 -0700514 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700515
516 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
517 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700518 active = zone_page_state(zone, NR_ACTIVE_FILE) +
519 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700520 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
521 zone_page_state(zone, NR_ISOLATED_ANON);
522
Minchan Kimbc693042010-09-09 16:38:00 -0700523 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700524}
525
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100526/**
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700527 * isolate_migratepages_block() - isolate all migrate-able pages within
528 * a single pageblock
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100529 * @cc: Compaction control structure.
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700530 * @low_pfn: The first PFN to isolate
531 * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock
532 * @isolate_mode: Isolation mode to be used.
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100533 *
534 * Isolate all pages that can be migrated from the range specified by
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700535 * [low_pfn, end_pfn). The range is expected to be within same pageblock.
536 * Returns zero if there is a fatal signal pending, otherwise PFN of the
537 * first page that was not scanned (which may be both less, equal to or more
538 * than end_pfn).
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100539 *
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700540 * The pages are isolated on cc->migratepages list (not required to be empty),
541 * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field
542 * is neither read nor updated.
Mel Gorman748446b2010-05-24 14:32:27 -0700543 */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700544static unsigned long
545isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
546 unsigned long end_pfn, isolate_mode_t isolate_mode)
Mel Gorman748446b2010-05-24 14:32:27 -0700547{
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700548 struct zone *zone = cc->zone;
Mel Gormanb7aba692011-01-13 15:45:54 -0800549 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700550 struct list_head *migratelist = &cc->migratepages;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700551 struct lruvec *lruvec;
Mel Gormanc67fe372012-08-21 16:16:17 -0700552 unsigned long flags;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700553 bool locked = false;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700554 struct page *page = NULL, *valid_page = NULL;
Mel Gorman748446b2010-05-24 14:32:27 -0700555
Mel Gorman748446b2010-05-24 14:32:27 -0700556 /*
557 * Ensure that there are not too many pages isolated from the LRU
558 * list by either parallel reclaimers or compaction. If there are,
559 * delay for some time until fewer pages are isolated
560 */
561 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700562 /* async migration should just abort */
David Rientjese0b9dae2014-06-04 16:08:28 -0700563 if (cc->mode == MIGRATE_ASYNC)
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100564 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700565
Mel Gorman748446b2010-05-24 14:32:27 -0700566 congestion_wait(BLK_RW_ASYNC, HZ/10);
567
568 if (fatal_signal_pending(current))
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100569 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700570 }
571
Vlastimil Babkabe976572014-06-04 16:10:41 -0700572 if (compact_should_abort(cc))
573 return 0;
David Rientjesaeef4b82014-06-04 16:08:31 -0700574
Mel Gorman748446b2010-05-24 14:32:27 -0700575 /* Time to isolate some pages for migration */
Mel Gorman748446b2010-05-24 14:32:27 -0700576 for (; low_pfn < end_pfn; low_pfn++) {
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700577 /*
578 * Periodically drop the lock (if held) regardless of its
579 * contention, to give chance to IRQs. Abort async compaction
580 * if contended.
581 */
582 if (!(low_pfn % SWAP_CLUSTER_MAX)
583 && compact_unlock_should_abort(&zone->lru_lock, flags,
584 &locked, cc))
585 break;
Mel Gormanc67fe372012-08-21 16:16:17 -0700586
Mel Gorman748446b2010-05-24 14:32:27 -0700587 if (!pfn_valid_within(low_pfn))
588 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800589 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700590
Mel Gorman748446b2010-05-24 14:32:27 -0700591 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800592
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700593 if (!valid_page)
594 valid_page = page;
595
Mel Gorman6c144662014-01-23 15:53:38 -0800596 /*
597 * Skip if free. page_order cannot be used without zone->lock
598 * as nothing prevents parallel allocations or buddy merging.
599 */
Mel Gorman748446b2010-05-24 14:32:27 -0700600 if (PageBuddy(page))
601 continue;
602
Mel Gorman9927af742011-01-13 15:45:59 -0800603 /*
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800604 * Check may be lockless but that's ok as we recheck later.
605 * It's possible to migrate LRU pages and balloon pages
606 * Skip any other type of page
607 */
608 if (!PageLRU(page)) {
609 if (unlikely(balloon_page_movable(page))) {
610 if (locked && balloon_page_isolate(page)) {
611 /* Successfully isolated */
Joonsoo Kimb6c75012014-04-07 15:37:07 -0700612 goto isolate_success;
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800613 }
614 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800615 continue;
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800616 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800617
618 /*
Mel Gorman2a1402a2012-10-08 16:32:33 -0700619 * PageLRU is set. lru_lock normally excludes isolation
620 * splitting and collapsing (collapsing has already happened
621 * if PageLRU is set) but the lock is not necessarily taken
622 * here and it is wasteful to take it just to check transhuge.
623 * Check TransHuge without lock and skip the whole pageblock if
624 * it's either a transhuge or hugetlbfs page, as calling
625 * compound_order() without preventing THP from splitting the
626 * page underneath us may return surprising results.
Andrea Arcangelibc835012011-01-13 15:47:08 -0800627 */
628 if (PageTransHuge(page)) {
Mel Gorman2a1402a2012-10-08 16:32:33 -0700629 if (!locked)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700630 low_pfn = ALIGN(low_pfn + 1,
631 pageblock_nr_pages) - 1;
632 else
633 low_pfn += (1 << compound_order(page)) - 1;
634
Mel Gorman2a1402a2012-10-08 16:32:33 -0700635 continue;
636 }
637
David Rientjes119d6d52014-04-03 14:48:00 -0700638 /*
639 * Migration will fail if an anonymous page is pinned in memory,
640 * so avoid taking lru_lock and isolating it unnecessarily in an
641 * admittedly racy check.
642 */
643 if (!page_mapping(page) &&
644 page_count(page) > page_mapcount(page))
645 continue;
646
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700647 /* If the lock is not held, try to take it */
648 if (!locked)
649 locked = compact_trylock_irqsave(&zone->lru_lock,
650 &flags, cc);
651 if (!locked)
Mel Gorman2a1402a2012-10-08 16:32:33 -0700652 break;
653
654 /* Recheck PageLRU and PageTransHuge under lock */
655 if (!PageLRU(page))
656 continue;
657 if (PageTransHuge(page)) {
Andrea Arcangelibc835012011-01-13 15:47:08 -0800658 low_pfn += (1 << compound_order(page)) - 1;
659 continue;
660 }
661
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700662 lruvec = mem_cgroup_page_lruvec(page, zone);
663
Mel Gorman748446b2010-05-24 14:32:27 -0700664 /* Try isolate the page */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700665 if (__isolate_lru_page(page, isolate_mode) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700666 continue;
667
Sasha Levin309381fea2014-01-23 15:52:54 -0800668 VM_BUG_ON_PAGE(PageTransCompound(page), page);
Andrea Arcangelibc835012011-01-13 15:47:08 -0800669
Mel Gorman748446b2010-05-24 14:32:27 -0700670 /* Successfully isolated */
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700671 del_page_from_lru_list(page, lruvec, page_lru(page));
Joonsoo Kimb6c75012014-04-07 15:37:07 -0700672
673isolate_success:
674 cc->finished_update_migrate = true;
Mel Gorman748446b2010-05-24 14:32:27 -0700675 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700676 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800677 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700678
679 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800680 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
681 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700682 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800683 }
Mel Gorman748446b2010-05-24 14:32:27 -0700684 }
685
Mel Gormanc67fe372012-08-21 16:16:17 -0700686 if (locked)
687 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700688
Vlastimil Babka50b5b092014-01-21 15:51:10 -0800689 /*
690 * Update the pageblock-skip information and cached scanner pfn,
691 * if the whole pageblock was scanned without isolating any page.
Vlastimil Babka50b5b092014-01-21 15:51:10 -0800692 */
David Rientjes35979ef2014-06-04 16:08:27 -0700693 if (low_pfn == end_pfn)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700694 update_pageblock_skip(cc, valid_page, nr_isolated, true);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700695
Mel Gormanb7aba692011-01-13 15:45:54 -0800696 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
697
Minchan Kim010fc292012-12-20 15:05:06 -0800698 count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned);
Mel Gorman397487d2012-10-19 12:00:10 +0100699 if (nr_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -0800700 count_compact_events(COMPACTISOLATED, nr_isolated);
Mel Gorman397487d2012-10-19 12:00:10 +0100701
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100702 return low_pfn;
703}
704
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700705/**
706 * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
707 * @cc: Compaction control structure.
708 * @start_pfn: The first PFN to start isolating.
709 * @end_pfn: The one-past-last PFN.
710 *
711 * Returns zero if isolation fails fatally due to e.g. pending signal.
712 * Otherwise, function returns one-past-the-last PFN of isolated page
713 * (which may be greater than end_pfn if end fell in a middle of a THP page).
714 */
715unsigned long
716isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
717 unsigned long end_pfn)
718{
719 unsigned long pfn, block_end_pfn;
720
721 /* Scan block by block. First and last block may be incomplete */
722 pfn = start_pfn;
723 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
724
725 for (; pfn < end_pfn; pfn = block_end_pfn,
726 block_end_pfn += pageblock_nr_pages) {
727
728 block_end_pfn = min(block_end_pfn, end_pfn);
729
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700730 if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700731 continue;
732
733 pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
734 ISOLATE_UNEVICTABLE);
735
736 /*
737 * In case of fatal failure, release everything that might
738 * have been isolated in the previous iteration, and signal
739 * the failure back to caller.
740 */
741 if (!pfn) {
742 putback_movable_pages(&cc->migratepages);
743 cc->nr_migratepages = 0;
744 break;
745 }
746 }
747 acct_isolated(cc->zone, cc);
748
749 return pfn;
750}
751
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100752#endif /* CONFIG_COMPACTION || CONFIG_CMA */
753#ifdef CONFIG_COMPACTION
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100754/*
755 * Based on information in the current compact_control, find blocks
756 * suitable for isolating free pages from and then isolate them.
757 */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700758static void isolate_freepages(struct compact_control *cc)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100759{
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700760 struct zone *zone = cc->zone;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100761 struct page *page;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700762 unsigned long block_start_pfn; /* start of current pageblock */
763 unsigned long block_end_pfn; /* end of current pageblock */
764 unsigned long low_pfn; /* lowest pfn scanner is able to scan */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100765 int nr_freepages = cc->nr_freepages;
766 struct list_head *freelist = &cc->freepages;
767
768 /*
769 * Initialise the free scanner. The starting point is where we last
Vlastimil Babka49e068f2014-05-06 12:50:03 -0700770 * successfully isolated from, zone-cached value, or the end of the
771 * zone when isolating for the first time. We need this aligned to
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700772 * the pageblock boundary, because we do
773 * block_start_pfn -= pageblock_nr_pages in the for loop.
774 * For ending point, take care when isolating in last pageblock of a
775 * a zone which ends in the middle of a pageblock.
Vlastimil Babka49e068f2014-05-06 12:50:03 -0700776 * The low boundary is the end of the pageblock the migration scanner
777 * is using.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100778 */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700779 block_start_pfn = cc->free_pfn & ~(pageblock_nr_pages-1);
780 block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
781 zone_end_pfn(zone));
Vlastimil Babka7ed695e2014-01-21 15:51:09 -0800782 low_pfn = ALIGN(cc->migrate_pfn + 1, pageblock_nr_pages);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100783
784 /*
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100785 * Isolate free pages until enough are available to migrate the
786 * pages on cc->migratepages. We stop searching if the migrate
787 * and free page scanners meet or enough free pages are isolated.
788 */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700789 for (; block_start_pfn >= low_pfn && cc->nr_migratepages > nr_freepages;
790 block_end_pfn = block_start_pfn,
791 block_start_pfn -= pageblock_nr_pages) {
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100792 unsigned long isolated;
793
David Rientjesf6ea3ad2013-09-30 13:45:03 -0700794 /*
795 * This can iterate a massively long zone without finding any
796 * suitable migration targets, so periodically check if we need
Vlastimil Babkabe976572014-06-04 16:10:41 -0700797 * to schedule, or even abort async compaction.
David Rientjesf6ea3ad2013-09-30 13:45:03 -0700798 */
Vlastimil Babkabe976572014-06-04 16:10:41 -0700799 if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
800 && compact_should_abort(cc))
801 break;
David Rientjesf6ea3ad2013-09-30 13:45:03 -0700802
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700803 page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
804 zone);
805 if (!page)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100806 continue;
807
808 /* Check the block is suitable for migration */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700809 if (!suitable_migration_target(page))
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100810 continue;
Linus Torvalds68e3e922012-06-03 20:05:57 -0700811
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700812 /* If isolation recently failed, do not retry */
813 if (!isolation_suitable(cc, page))
814 continue;
815
Mel Gormanf40d1e42012-10-08 16:32:36 -0700816 /* Found a block suitable for isolating free pages from */
Vlastimil Babkae9ade562014-06-04 16:08:34 -0700817 cc->free_pfn = block_start_pfn;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700818 isolated = isolate_freepages_block(cc, block_start_pfn,
819 block_end_pfn, freelist, false);
Mel Gormanf40d1e42012-10-08 16:32:36 -0700820 nr_freepages += isolated;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100821
822 /*
Vlastimil Babkae9ade562014-06-04 16:08:34 -0700823 * Set a flag that we successfully isolated in this pageblock.
824 * In the next loop iteration, zone->compact_cached_free_pfn
825 * will not be updated and thus it will effectively contain the
826 * highest pageblock we isolated pages from.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100827 */
Vlastimil Babkae9ade562014-06-04 16:08:34 -0700828 if (isolated)
Mel Gormanc89511a2012-10-08 16:32:45 -0700829 cc->finished_update_free = true;
Vlastimil Babkabe976572014-06-04 16:10:41 -0700830
831 /*
832 * isolate_freepages_block() might have aborted due to async
833 * compaction being contended
834 */
835 if (cc->contended)
836 break;
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100837 }
838
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100839 /* split_free_page does not map the pages */
840 map_pages(freelist);
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100841
Vlastimil Babka7ed695e2014-01-21 15:51:09 -0800842 /*
843 * If we crossed the migrate scanner, we want to keep it that way
844 * so that compact_finished() may detect this
845 */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700846 if (block_start_pfn < low_pfn)
Vlastimil Babkae9ade562014-06-04 16:08:34 -0700847 cc->free_pfn = cc->migrate_pfn;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700848
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100849 cc->nr_freepages = nr_freepages;
Mel Gorman748446b2010-05-24 14:32:27 -0700850}
851
852/*
853 * This is a migrate-callback that "allocates" freepages by taking pages
854 * from the isolated freelists in the block we are migrating to.
855 */
856static struct page *compaction_alloc(struct page *migratepage,
857 unsigned long data,
858 int **result)
859{
860 struct compact_control *cc = (struct compact_control *)data;
861 struct page *freepage;
862
Vlastimil Babkabe976572014-06-04 16:10:41 -0700863 /*
864 * Isolate free pages if necessary, and if we are not aborting due to
865 * contention.
866 */
Mel Gorman748446b2010-05-24 14:32:27 -0700867 if (list_empty(&cc->freepages)) {
Vlastimil Babkabe976572014-06-04 16:10:41 -0700868 if (!cc->contended)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700869 isolate_freepages(cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700870
871 if (list_empty(&cc->freepages))
872 return NULL;
873 }
874
875 freepage = list_entry(cc->freepages.next, struct page, lru);
876 list_del(&freepage->lru);
877 cc->nr_freepages--;
878
879 return freepage;
880}
881
882/*
David Rientjesd53aea32014-06-04 16:08:26 -0700883 * This is a migrate-callback that "frees" freepages back to the isolated
884 * freelist. All pages on the freelist are from the same zone, so there is no
885 * special handling needed for NUMA.
886 */
887static void compaction_free(struct page *page, unsigned long data)
888{
889 struct compact_control *cc = (struct compact_control *)data;
890
891 list_add(&page->lru, &cc->freepages);
892 cc->nr_freepages++;
893}
894
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100895/* possible outcome of isolate_migratepages */
896typedef enum {
897 ISOLATE_ABORT, /* Abort compaction now */
898 ISOLATE_NONE, /* No pages isolated, continue scanning */
899 ISOLATE_SUCCESS, /* Pages isolated, migrate */
900} isolate_migrate_t;
901
902/*
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700903 * Isolate all pages that can be migrated from the first suitable block,
904 * starting at the block pointed to by the migrate scanner pfn within
905 * compact_control.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100906 */
907static isolate_migrate_t isolate_migratepages(struct zone *zone,
908 struct compact_control *cc)
909{
910 unsigned long low_pfn, end_pfn;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700911 struct page *page;
912 const isolate_mode_t isolate_mode =
913 (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100914
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700915 /*
916 * Start at where we last stopped, or beginning of the zone as
917 * initialized by compact_zone()
918 */
919 low_pfn = cc->migrate_pfn;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100920
921 /* Only scan within a pageblock boundary */
Mel Gormana9aacbc2013-02-22 16:32:25 -0800922 end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100923
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700924 /*
925 * Iterate over whole pageblocks until we find the first suitable.
926 * Do not cross the free scanner.
927 */
928 for (; end_pfn <= cc->free_pfn;
929 low_pfn = end_pfn, end_pfn += pageblock_nr_pages) {
930
931 /*
932 * This can potentially iterate a massively long zone with
933 * many pageblocks unsuitable, so periodically check if we
934 * need to schedule, or even abort async compaction.
935 */
936 if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
937 && compact_should_abort(cc))
938 break;
939
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700940 page = pageblock_pfn_to_page(low_pfn, end_pfn, zone);
941 if (!page)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700942 continue;
943
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700944 /* If isolation recently failed, do not retry */
945 if (!isolation_suitable(cc, page))
946 continue;
947
948 /*
949 * For async compaction, also only scan in MOVABLE blocks.
950 * Async compaction is optimistic to see if the minimum amount
951 * of work satisfies the allocation.
952 */
953 if (cc->mode == MIGRATE_ASYNC &&
954 !migrate_async_suitable(get_pageblock_migratetype(page)))
955 continue;
956
957 /* Perform the isolation */
958 low_pfn = isolate_migratepages_block(cc, low_pfn, end_pfn,
959 isolate_mode);
960
961 if (!low_pfn || cc->contended)
962 return ISOLATE_ABORT;
963
964 /*
965 * Either we isolated something and proceed with migration. Or
966 * we failed and compact_zone should decide if we should
967 * continue or not.
968 */
969 break;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100970 }
971
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700972 acct_isolated(zone, cc);
973 /* Record where migration scanner will be restarted */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100974 cc->migrate_pfn = low_pfn;
975
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700976 return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100977}
978
Mel Gorman748446b2010-05-24 14:32:27 -0700979static int compact_finished(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800980 struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700981{
Mel Gorman8fb74b92013-01-11 14:32:16 -0800982 unsigned int order;
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800983 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -0700984
Vlastimil Babkabe976572014-06-04 16:10:41 -0700985 if (cc->contended || fatal_signal_pending(current))
Mel Gorman748446b2010-05-24 14:32:27 -0700986 return COMPACT_PARTIAL;
987
Mel Gorman753341a2012-10-08 16:32:40 -0700988 /* Compaction run completes if the migrate and free scanner meet */
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700989 if (cc->free_pfn <= cc->migrate_pfn) {
Vlastimil Babka55b7c4c2014-01-21 15:51:11 -0800990 /* Let the next compaction start anew. */
David Rientjes35979ef2014-06-04 16:08:27 -0700991 zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
992 zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
Vlastimil Babka55b7c4c2014-01-21 15:51:11 -0800993 zone->compact_cached_free_pfn = zone_end_pfn(zone);
994
Mel Gorman62997022012-10-08 16:32:47 -0700995 /*
996 * Mark that the PG_migrate_skip information should be cleared
997 * by kswapd when it goes to sleep. kswapd does not set the
998 * flag itself as the decision to be clear should be directly
999 * based on an allocation request.
1000 */
1001 if (!current_is_kswapd())
1002 zone->compact_blockskip_flush = true;
1003
Mel Gorman748446b2010-05-24 14:32:27 -07001004 return COMPACT_COMPLETE;
Mel Gormanbb13ffe2012-10-08 16:32:41 -07001005 }
Mel Gorman748446b2010-05-24 14:32:27 -07001006
Johannes Weiner82478fb2011-01-20 14:44:21 -08001007 /*
1008 * order == -1 is expected when compacting via
1009 * /proc/sys/vm/compact_memory
1010 */
Mel Gorman56de7262010-05-24 14:32:30 -07001011 if (cc->order == -1)
1012 return COMPACT_CONTINUE;
1013
Michal Hocko3957c772011-06-15 15:08:25 -07001014 /* Compaction run is not finished if the watermark is not met */
1015 watermark = low_wmark_pages(zone);
1016 watermark += (1 << cc->order);
1017
1018 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
1019 return COMPACT_CONTINUE;
1020
Mel Gorman56de7262010-05-24 14:32:30 -07001021 /* Direct compactor: Is a suitable page free? */
Mel Gorman8fb74b92013-01-11 14:32:16 -08001022 for (order = cc->order; order < MAX_ORDER; order++) {
1023 struct free_area *area = &zone->free_area[order];
Mel Gorman56de7262010-05-24 14:32:30 -07001024
Mel Gorman8fb74b92013-01-11 14:32:16 -08001025 /* Job done if page is free of the right migratetype */
1026 if (!list_empty(&area->free_list[cc->migratetype]))
1027 return COMPACT_PARTIAL;
1028
1029 /* Job done if allocation would set block type */
1030 if (cc->order >= pageblock_order && area->nr_free)
1031 return COMPACT_PARTIAL;
Mel Gorman56de7262010-05-24 14:32:30 -07001032 }
1033
Mel Gorman748446b2010-05-24 14:32:27 -07001034 return COMPACT_CONTINUE;
1035}
1036
Mel Gorman3e7d3442011-01-13 15:45:56 -08001037/*
1038 * compaction_suitable: Is this suitable to run compaction on this zone now?
1039 * Returns
1040 * COMPACT_SKIPPED - If there are too few free pages for compaction
1041 * COMPACT_PARTIAL - If the allocation would succeed without compaction
1042 * COMPACT_CONTINUE - If compaction should run now
1043 */
1044unsigned long compaction_suitable(struct zone *zone, int order)
1045{
1046 int fragindex;
1047 unsigned long watermark;
1048
1049 /*
Michal Hocko3957c772011-06-15 15:08:25 -07001050 * order == -1 is expected when compacting via
1051 * /proc/sys/vm/compact_memory
1052 */
1053 if (order == -1)
1054 return COMPACT_CONTINUE;
1055
1056 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -08001057 * Watermarks for order-0 must be met for compaction. Note the 2UL.
1058 * This is because during migration, copies of pages need to be
1059 * allocated and for a short time, the footprint is higher
1060 */
1061 watermark = low_wmark_pages(zone) + (2UL << order);
1062 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
1063 return COMPACT_SKIPPED;
1064
1065 /*
1066 * fragmentation index determines if allocation failures are due to
1067 * low memory or external fragmentation
1068 *
Shaohua Lia582a732011-06-15 15:08:49 -07001069 * index of -1000 implies allocations might succeed depending on
1070 * watermarks
Mel Gorman3e7d3442011-01-13 15:45:56 -08001071 * index towards 0 implies failure is due to lack of memory
1072 * index towards 1000 implies failure is due to fragmentation
1073 *
1074 * Only compact if a failure would be due to fragmentation.
1075 */
1076 fragindex = fragmentation_index(zone, order);
1077 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
1078 return COMPACT_SKIPPED;
1079
Shaohua Lia582a732011-06-15 15:08:49 -07001080 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
1081 0, 0))
Mel Gorman3e7d3442011-01-13 15:45:56 -08001082 return COMPACT_PARTIAL;
1083
1084 return COMPACT_CONTINUE;
1085}
1086
Mel Gorman748446b2010-05-24 14:32:27 -07001087static int compact_zone(struct zone *zone, struct compact_control *cc)
1088{
1089 int ret;
Mel Gormanc89511a2012-10-08 16:32:45 -07001090 unsigned long start_pfn = zone->zone_start_pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -08001091 unsigned long end_pfn = zone_end_pfn(zone);
David Rientjese0b9dae2014-06-04 16:08:28 -07001092 const bool sync = cc->mode != MIGRATE_ASYNC;
Mel Gorman748446b2010-05-24 14:32:27 -07001093
Mel Gorman3e7d3442011-01-13 15:45:56 -08001094 ret = compaction_suitable(zone, cc->order);
1095 switch (ret) {
1096 case COMPACT_PARTIAL:
1097 case COMPACT_SKIPPED:
1098 /* Compaction is likely to fail */
1099 return ret;
1100 case COMPACT_CONTINUE:
1101 /* Fall through to compaction */
1102 ;
1103 }
1104
Mel Gormanc89511a2012-10-08 16:32:45 -07001105 /*
Vlastimil Babkad3132e42014-01-21 15:51:08 -08001106 * Clear pageblock skip if there were failures recently and compaction
1107 * is about to be retried after being deferred. kswapd does not do
1108 * this reset as it'll reset the cached information when going to sleep.
1109 */
1110 if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
1111 __reset_isolation_suitable(zone);
1112
1113 /*
Mel Gormanc89511a2012-10-08 16:32:45 -07001114 * Setup to move all movable pages to the end of the zone. Used cached
1115 * information on where the scanners should start but check that it
1116 * is initialised by ensuring the values are within zone boundaries.
1117 */
David Rientjese0b9dae2014-06-04 16:08:28 -07001118 cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync];
Mel Gormanc89511a2012-10-08 16:32:45 -07001119 cc->free_pfn = zone->compact_cached_free_pfn;
1120 if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
1121 cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
1122 zone->compact_cached_free_pfn = cc->free_pfn;
1123 }
1124 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
1125 cc->migrate_pfn = start_pfn;
David Rientjes35979ef2014-06-04 16:08:27 -07001126 zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
1127 zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
Mel Gormanc89511a2012-10-08 16:32:45 -07001128 }
Mel Gorman748446b2010-05-24 14:32:27 -07001129
Mel Gorman0eb927c2014-01-21 15:51:05 -08001130 trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, cc->free_pfn, end_pfn);
1131
Mel Gorman748446b2010-05-24 14:32:27 -07001132 migrate_prep_local();
1133
1134 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
Minchan Kim9d502c12011-03-22 16:30:39 -07001135 int err;
Mel Gorman748446b2010-05-24 14:32:27 -07001136
Mel Gormanf9e35b32011-06-15 15:08:52 -07001137 switch (isolate_migratepages(zone, cc)) {
1138 case ISOLATE_ABORT:
1139 ret = COMPACT_PARTIAL;
Rafael Aquini5733c7d2012-12-11 16:02:47 -08001140 putback_movable_pages(&cc->migratepages);
Shaohua Lie64c5232012-10-08 16:32:27 -07001141 cc->nr_migratepages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -07001142 goto out;
1143 case ISOLATE_NONE:
Mel Gorman748446b2010-05-24 14:32:27 -07001144 continue;
Mel Gormanf9e35b32011-06-15 15:08:52 -07001145 case ISOLATE_SUCCESS:
1146 ;
1147 }
Mel Gorman748446b2010-05-24 14:32:27 -07001148
David Rientjesd53aea32014-06-04 16:08:26 -07001149 err = migrate_pages(&cc->migratepages, compaction_alloc,
David Rientjese0b9dae2014-06-04 16:08:28 -07001150 compaction_free, (unsigned long)cc, cc->mode,
Mel Gorman7b2a2d42012-10-19 14:07:31 +01001151 MR_COMPACTION);
Mel Gorman748446b2010-05-24 14:32:27 -07001152
Vlastimil Babkaf8c93012014-06-04 16:08:32 -07001153 trace_mm_compaction_migratepages(cc->nr_migratepages, err,
1154 &cc->migratepages);
Mel Gorman748446b2010-05-24 14:32:27 -07001155
Vlastimil Babkaf8c93012014-06-04 16:08:32 -07001156 /* All pages were either migrated or will be released */
1157 cc->nr_migratepages = 0;
Minchan Kim9d502c12011-03-22 16:30:39 -07001158 if (err) {
Rafael Aquini5733c7d2012-12-11 16:02:47 -08001159 putback_movable_pages(&cc->migratepages);
Vlastimil Babka7ed695e2014-01-21 15:51:09 -08001160 /*
1161 * migrate_pages() may return -ENOMEM when scanners meet
1162 * and we want compact_finished() to detect it
1163 */
1164 if (err == -ENOMEM && cc->free_pfn > cc->migrate_pfn) {
David Rientjes4bf2bba2012-07-11 14:02:13 -07001165 ret = COMPACT_PARTIAL;
1166 goto out;
1167 }
Mel Gorman748446b2010-05-24 14:32:27 -07001168 }
Mel Gorman748446b2010-05-24 14:32:27 -07001169 }
1170
Mel Gormanf9e35b32011-06-15 15:08:52 -07001171out:
Mel Gorman748446b2010-05-24 14:32:27 -07001172 /* Release free pages and check accounting */
1173 cc->nr_freepages -= release_freepages(&cc->freepages);
1174 VM_BUG_ON(cc->nr_freepages != 0);
1175
Mel Gorman0eb927c2014-01-21 15:51:05 -08001176 trace_mm_compaction_end(ret);
1177
Mel Gorman748446b2010-05-24 14:32:27 -07001178 return ret;
1179}
Mel Gorman76ab0f52010-05-24 14:32:28 -07001180
David Rientjese0b9dae2014-06-04 16:08:28 -07001181static unsigned long compact_zone_order(struct zone *zone, int order,
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001182 gfp_t gfp_mask, enum migrate_mode mode, int *contended)
Mel Gorman56de7262010-05-24 14:32:30 -07001183{
Shaohua Lie64c5232012-10-08 16:32:27 -07001184 unsigned long ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001185 struct compact_control cc = {
1186 .nr_freepages = 0,
1187 .nr_migratepages = 0,
1188 .order = order,
1189 .migratetype = allocflags_to_migratetype(gfp_mask),
1190 .zone = zone,
David Rientjese0b9dae2014-06-04 16:08:28 -07001191 .mode = mode,
Mel Gorman56de7262010-05-24 14:32:30 -07001192 };
1193 INIT_LIST_HEAD(&cc.freepages);
1194 INIT_LIST_HEAD(&cc.migratepages);
1195
Shaohua Lie64c5232012-10-08 16:32:27 -07001196 ret = compact_zone(zone, &cc);
1197
1198 VM_BUG_ON(!list_empty(&cc.freepages));
1199 VM_BUG_ON(!list_empty(&cc.migratepages));
1200
1201 *contended = cc.contended;
1202 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001203}
1204
Mel Gorman5e771902010-05-24 14:32:31 -07001205int sysctl_extfrag_threshold = 500;
1206
Mel Gorman56de7262010-05-24 14:32:30 -07001207/**
1208 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
1209 * @zonelist: The zonelist used for the current allocation
1210 * @order: The order of the current allocation
1211 * @gfp_mask: The GFP mask of the current allocation
1212 * @nodemask: The allowed nodes to allocate from
David Rientjese0b9dae2014-06-04 16:08:28 -07001213 * @mode: The migration mode for async, sync light, or sync migration
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001214 * @contended: Return value that determines if compaction was aborted due to
1215 * need_resched() or lock contention
Vlastimil Babka53853e22014-10-09 15:27:02 -07001216 * @candidate_zone: Return the zone where we think allocation should succeed
Mel Gorman56de7262010-05-24 14:32:30 -07001217 *
1218 * This is the main entry point for direct page compaction.
1219 */
1220unsigned long try_to_compact_pages(struct zonelist *zonelist,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001221 int order, gfp_t gfp_mask, nodemask_t *nodemask,
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001222 enum migrate_mode mode, int *contended,
Vlastimil Babka53853e22014-10-09 15:27:02 -07001223 struct zone **candidate_zone)
Mel Gorman56de7262010-05-24 14:32:30 -07001224{
1225 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
1226 int may_enter_fs = gfp_mask & __GFP_FS;
1227 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -07001228 struct zoneref *z;
1229 struct zone *zone;
Vlastimil Babka53853e22014-10-09 15:27:02 -07001230 int rc = COMPACT_DEFERRED;
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001231 int alloc_flags = 0;
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001232 int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */
1233
1234 *contended = COMPACT_CONTENDED_NONE;
Mel Gorman56de7262010-05-24 14:32:30 -07001235
Mel Gorman4ffb6332012-10-08 16:29:09 -07001236 /* Check if the GFP flags allow compaction */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -08001237 if (!order || !may_enter_fs || !may_perform_io)
Vlastimil Babka53853e22014-10-09 15:27:02 -07001238 return COMPACT_SKIPPED;
Mel Gorman56de7262010-05-24 14:32:30 -07001239
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001240#ifdef CONFIG_CMA
1241 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
1242 alloc_flags |= ALLOC_CMA;
1243#endif
Mel Gorman56de7262010-05-24 14:32:30 -07001244 /* Compact each zone in the list */
1245 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
1246 nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -07001247 int status;
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001248 int zone_contended;
Mel Gorman56de7262010-05-24 14:32:30 -07001249
Vlastimil Babka53853e22014-10-09 15:27:02 -07001250 if (compaction_deferred(zone, order))
1251 continue;
1252
David Rientjese0b9dae2014-06-04 16:08:28 -07001253 status = compact_zone_order(zone, order, gfp_mask, mode,
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001254 &zone_contended);
Mel Gorman56de7262010-05-24 14:32:30 -07001255 rc = max(status, rc);
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001256 /*
1257 * It takes at least one zone that wasn't lock contended
1258 * to clear all_zones_contended.
1259 */
1260 all_zones_contended &= zone_contended;
Mel Gorman56de7262010-05-24 14:32:30 -07001261
Mel Gorman3e7d3442011-01-13 15:45:56 -08001262 /* If a normal allocation would succeed, stop compacting */
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001263 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
Vlastimil Babka53853e22014-10-09 15:27:02 -07001264 alloc_flags)) {
1265 *candidate_zone = zone;
1266 /*
1267 * We think the allocation will succeed in this zone,
1268 * but it is not certain, hence the false. The caller
1269 * will repeat this with true if allocation indeed
1270 * succeeds in this zone.
1271 */
1272 compaction_defer_reset(zone, order, false);
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001273 /*
1274 * It is possible that async compaction aborted due to
1275 * need_resched() and the watermarks were ok thanks to
1276 * somebody else freeing memory. The allocation can
1277 * however still fail so we better signal the
1278 * need_resched() contention anyway (this will not
1279 * prevent the allocation attempt).
1280 */
1281 if (zone_contended == COMPACT_CONTENDED_SCHED)
1282 *contended = COMPACT_CONTENDED_SCHED;
1283
1284 goto break_loop;
1285 }
1286
1287 if (mode != MIGRATE_ASYNC) {
Vlastimil Babka53853e22014-10-09 15:27:02 -07001288 /*
1289 * We think that allocation won't succeed in this zone
1290 * so we defer compaction there. If it ends up
1291 * succeeding after all, it will be reset.
1292 */
1293 defer_compaction(zone, order);
1294 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001295
1296 /*
1297 * We might have stopped compacting due to need_resched() in
1298 * async compaction, or due to a fatal signal detected. In that
1299 * case do not try further zones and signal need_resched()
1300 * contention.
1301 */
1302 if ((zone_contended == COMPACT_CONTENDED_SCHED)
1303 || fatal_signal_pending(current)) {
1304 *contended = COMPACT_CONTENDED_SCHED;
1305 goto break_loop;
1306 }
1307
1308 continue;
1309break_loop:
1310 /*
1311 * We might not have tried all the zones, so be conservative
1312 * and assume they are not all lock contended.
1313 */
1314 all_zones_contended = 0;
1315 break;
Mel Gorman56de7262010-05-24 14:32:30 -07001316 }
1317
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001318 /*
1319 * If at least one zone wasn't deferred or skipped, we report if all
1320 * zones that were tried were lock contended.
1321 */
1322 if (rc > COMPACT_SKIPPED && all_zones_contended)
1323 *contended = COMPACT_CONTENDED_LOCK;
1324
Mel Gorman56de7262010-05-24 14:32:30 -07001325 return rc;
1326}
1327
1328
Mel Gorman76ab0f52010-05-24 14:32:28 -07001329/* Compact all zones within a node */
Andrew Morton7103f162013-02-22 16:32:33 -08001330static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001331{
1332 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -07001333 struct zone *zone;
1334
Mel Gorman76ab0f52010-05-24 14:32:28 -07001335 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -07001336
1337 zone = &pgdat->node_zones[zoneid];
1338 if (!populated_zone(zone))
1339 continue;
1340
Rik van Riel7be62de2012-03-21 16:33:52 -07001341 cc->nr_freepages = 0;
1342 cc->nr_migratepages = 0;
1343 cc->zone = zone;
1344 INIT_LIST_HEAD(&cc->freepages);
1345 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001346
Dan Carpenteraad6ec32012-03-21 16:33:54 -07001347 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -07001348 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001349
Rik van Rielaff62242012-03-21 16:33:52 -07001350 if (cc->order > 0) {
Vlastimil Babkade6c60a2014-01-21 15:51:07 -08001351 if (zone_watermark_ok(zone, cc->order,
1352 low_wmark_pages(zone), 0, 0))
1353 compaction_defer_reset(zone, cc->order, false);
Rik van Rielaff62242012-03-21 16:33:52 -07001354 }
1355
Rik van Riel7be62de2012-03-21 16:33:52 -07001356 VM_BUG_ON(!list_empty(&cc->freepages));
1357 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -07001358 }
Mel Gorman76ab0f52010-05-24 14:32:28 -07001359}
1360
Andrew Morton7103f162013-02-22 16:32:33 -08001361void compact_pgdat(pg_data_t *pgdat, int order)
Rik van Riel7be62de2012-03-21 16:33:52 -07001362{
1363 struct compact_control cc = {
1364 .order = order,
David Rientjese0b9dae2014-06-04 16:08:28 -07001365 .mode = MIGRATE_ASYNC,
Rik van Riel7be62de2012-03-21 16:33:52 -07001366 };
1367
Mel Gorman3a7200a2013-09-11 14:22:19 -07001368 if (!order)
1369 return;
1370
Andrew Morton7103f162013-02-22 16:32:33 -08001371 __compact_pgdat(pgdat, &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001372}
1373
Andrew Morton7103f162013-02-22 16:32:33 -08001374static void compact_node(int nid)
Rik van Riel7be62de2012-03-21 16:33:52 -07001375{
Rik van Riel7be62de2012-03-21 16:33:52 -07001376 struct compact_control cc = {
1377 .order = -1,
David Rientjese0b9dae2014-06-04 16:08:28 -07001378 .mode = MIGRATE_SYNC,
David Rientjes91ca9182014-04-03 14:47:23 -07001379 .ignore_skip_hint = true,
Rik van Riel7be62de2012-03-21 16:33:52 -07001380 };
1381
Andrew Morton7103f162013-02-22 16:32:33 -08001382 __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001383}
1384
Mel Gorman76ab0f52010-05-24 14:32:28 -07001385/* Compact all nodes in the system */
Jason Liu7964c062013-01-11 14:31:47 -08001386static void compact_nodes(void)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001387{
1388 int nid;
1389
Hugh Dickins8575ec22012-03-21 16:33:53 -07001390 /* Flush pending updates to the LRU lists */
1391 lru_add_drain_all();
1392
Mel Gorman76ab0f52010-05-24 14:32:28 -07001393 for_each_online_node(nid)
1394 compact_node(nid);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001395}
1396
1397/* The written value is actually unused, all memory is compacted */
1398int sysctl_compact_memory;
1399
1400/* This is the entry point for compacting all nodes via /proc/sys/vm */
1401int sysctl_compaction_handler(struct ctl_table *table, int write,
1402 void __user *buffer, size_t *length, loff_t *ppos)
1403{
1404 if (write)
Jason Liu7964c062013-01-11 14:31:47 -08001405 compact_nodes();
Mel Gorman76ab0f52010-05-24 14:32:28 -07001406
1407 return 0;
1408}
Mel Gormaned4a6d72010-05-24 14:32:29 -07001409
Mel Gorman5e771902010-05-24 14:32:31 -07001410int sysctl_extfrag_handler(struct ctl_table *table, int write,
1411 void __user *buffer, size_t *length, loff_t *ppos)
1412{
1413 proc_dointvec_minmax(table, write, buffer, length, ppos);
1414
1415 return 0;
1416}
1417
Mel Gormaned4a6d72010-05-24 14:32:29 -07001418#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Rashika Kheria74e77fb2014-04-03 14:48:01 -07001419static ssize_t sysfs_compact_node(struct device *dev,
Kay Sievers10fbcf42011-12-21 14:48:43 -08001420 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -07001421 const char *buf, size_t count)
1422{
Hugh Dickins8575ec22012-03-21 16:33:53 -07001423 int nid = dev->id;
1424
1425 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1426 /* Flush pending updates to the LRU lists */
1427 lru_add_drain_all();
1428
1429 compact_node(nid);
1430 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07001431
1432 return count;
1433}
Kay Sievers10fbcf42011-12-21 14:48:43 -08001434static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001435
1436int compaction_register_node(struct node *node)
1437{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001438 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001439}
1440
1441void compaction_unregister_node(struct node *node)
1442{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001443 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001444}
1445#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001446
1447#endif /* CONFIG_COMPACTION */