blob: b12df9fe10b4e59c488ba2a2484a7e9c8d3c2fee [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
Joonsoo Kim16c4a092015-02-11 15:27:01 -080037#ifdef CONFIG_TRACEPOINTS
38static const char *const compaction_status_string[] = {
39 "deferred",
40 "skipped",
41 "continue",
42 "partial",
43 "complete",
44};
45#endif
Michal Nazarewiczff9543f2011-12-29 13:09:50 +010046
Mel Gormanb7aba692011-01-13 15:45:54 -080047#define CREATE_TRACE_POINTS
48#include <trace/events/compaction.h>
49
Mel Gorman748446b2010-05-24 14:32:27 -070050static unsigned long release_freepages(struct list_head *freelist)
51{
52 struct page *page, *next;
Vlastimil Babka6bace092014-12-10 15:43:31 -080053 unsigned long high_pfn = 0;
Mel Gorman748446b2010-05-24 14:32:27 -070054
55 list_for_each_entry_safe(page, next, freelist, lru) {
Vlastimil Babka6bace092014-12-10 15:43:31 -080056 unsigned long pfn = page_to_pfn(page);
Mel Gorman748446b2010-05-24 14:32:27 -070057 list_del(&page->lru);
58 __free_page(page);
Vlastimil Babka6bace092014-12-10 15:43:31 -080059 if (pfn > high_pfn)
60 high_pfn = pfn;
Mel Gorman748446b2010-05-24 14:32:27 -070061 }
62
Vlastimil Babka6bace092014-12-10 15:43:31 -080063 return high_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -070064}
65
Michal Nazarewiczff9543f2011-12-29 13:09:50 +010066static void map_pages(struct list_head *list)
67{
68 struct page *page;
69
70 list_for_each_entry(page, list, lru) {
71 arch_alloc_page(page, 0);
72 kernel_map_pages(page, 1, 1);
73 }
74}
75
Michal Nazarewicz47118af2011-12-29 13:09:50 +010076static inline bool migrate_async_suitable(int migratetype)
77{
78 return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
79}
80
Vlastimil Babka7d49d882014-10-09 15:27:11 -070081/*
82 * Check that the whole (or subset of) a pageblock given by the interval of
83 * [start_pfn, end_pfn) is valid and within the same zone, before scanning it
84 * with the migration of free compaction scanner. The scanners then need to
85 * use only pfn_valid_within() check for arches that allow holes within
86 * pageblocks.
87 *
88 * Return struct page pointer of start_pfn, or NULL if checks were not passed.
89 *
90 * It's possible on some configurations to have a setup like node0 node1 node0
91 * i.e. it's possible that all pages within a zones range of pages do not
92 * belong to a single zone. We assume that a border between node0 and node1
93 * can occur within a single pageblock, but not a node0 node1 node0
94 * interleaving within a single pageblock. It is therefore sufficient to check
95 * the first and last page of a pageblock and avoid checking each individual
96 * page in a pageblock.
97 */
98static struct page *pageblock_pfn_to_page(unsigned long start_pfn,
99 unsigned long end_pfn, struct zone *zone)
100{
101 struct page *start_page;
102 struct page *end_page;
103
104 /* end_pfn is one past the range we are checking */
105 end_pfn--;
106
107 if (!pfn_valid(start_pfn) || !pfn_valid(end_pfn))
108 return NULL;
109
110 start_page = pfn_to_page(start_pfn);
111
112 if (page_zone(start_page) != zone)
113 return NULL;
114
115 end_page = pfn_to_page(end_pfn);
116
117 /* This gives a shorter code than deriving page_zone(end_page) */
118 if (page_zone_id(start_page) != page_zone_id(end_page))
119 return NULL;
120
121 return start_page;
122}
123
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700124#ifdef CONFIG_COMPACTION
125/* Returns true if the pageblock should be scanned for pages to isolate. */
126static inline bool isolation_suitable(struct compact_control *cc,
127 struct page *page)
128{
129 if (cc->ignore_skip_hint)
130 return true;
131
132 return !get_pageblock_skip(page);
133}
134
135/*
136 * This function is called to clear all cached information on pageblocks that
137 * should be skipped for page isolation when the migrate and free page scanner
138 * meet.
139 */
Mel Gorman62997022012-10-08 16:32:47 -0700140static void __reset_isolation_suitable(struct zone *zone)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700141{
142 unsigned long start_pfn = zone->zone_start_pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -0800143 unsigned long end_pfn = zone_end_pfn(zone);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700144 unsigned long pfn;
145
David Rientjes35979ef2014-06-04 16:08:27 -0700146 zone->compact_cached_migrate_pfn[0] = start_pfn;
147 zone->compact_cached_migrate_pfn[1] = start_pfn;
Mel Gormanc89511a2012-10-08 16:32:45 -0700148 zone->compact_cached_free_pfn = end_pfn;
Mel Gorman62997022012-10-08 16:32:47 -0700149 zone->compact_blockskip_flush = false;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700150
151 /* Walk the zone and mark every pageblock as suitable for isolation */
152 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
153 struct page *page;
154
155 cond_resched();
156
157 if (!pfn_valid(pfn))
158 continue;
159
160 page = pfn_to_page(pfn);
161 if (zone != page_zone(page))
162 continue;
163
164 clear_pageblock_skip(page);
165 }
166}
167
Mel Gorman62997022012-10-08 16:32:47 -0700168void reset_isolation_suitable(pg_data_t *pgdat)
169{
170 int zoneid;
171
172 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
173 struct zone *zone = &pgdat->node_zones[zoneid];
174 if (!populated_zone(zone))
175 continue;
176
177 /* Only flush if a full compaction finished recently */
178 if (zone->compact_blockskip_flush)
179 __reset_isolation_suitable(zone);
180 }
181}
182
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700183/*
184 * If no pages were isolated then mark this pageblock to be skipped in the
Mel Gorman62997022012-10-08 16:32:47 -0700185 * future. The information is later cleared by __reset_isolation_suitable().
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700186 */
Mel Gormanc89511a2012-10-08 16:32:45 -0700187static void update_pageblock_skip(struct compact_control *cc,
188 struct page *page, unsigned long nr_isolated,
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700189 bool migrate_scanner)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700190{
Mel Gormanc89511a2012-10-08 16:32:45 -0700191 struct zone *zone = cc->zone;
David Rientjes35979ef2014-06-04 16:08:27 -0700192 unsigned long pfn;
Joonsoo Kim6815bf32013-12-18 17:08:52 -0800193
194 if (cc->ignore_skip_hint)
195 return;
196
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700197 if (!page)
198 return;
199
David Rientjes35979ef2014-06-04 16:08:27 -0700200 if (nr_isolated)
201 return;
202
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700203 set_pageblock_skip(page);
Mel Gormanc89511a2012-10-08 16:32:45 -0700204
David Rientjes35979ef2014-06-04 16:08:27 -0700205 pfn = page_to_pfn(page);
206
207 /* Update where async and sync compaction should restart */
208 if (migrate_scanner) {
David Rientjes35979ef2014-06-04 16:08:27 -0700209 if (pfn > zone->compact_cached_migrate_pfn[0])
210 zone->compact_cached_migrate_pfn[0] = pfn;
David Rientjese0b9dae2014-06-04 16:08:28 -0700211 if (cc->mode != MIGRATE_ASYNC &&
212 pfn > zone->compact_cached_migrate_pfn[1])
David Rientjes35979ef2014-06-04 16:08:27 -0700213 zone->compact_cached_migrate_pfn[1] = pfn;
214 } else {
David Rientjes35979ef2014-06-04 16:08:27 -0700215 if (pfn < zone->compact_cached_free_pfn)
216 zone->compact_cached_free_pfn = pfn;
Mel Gormanc89511a2012-10-08 16:32:45 -0700217 }
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700218}
219#else
220static inline bool isolation_suitable(struct compact_control *cc,
221 struct page *page)
222{
223 return true;
224}
225
Mel Gormanc89511a2012-10-08 16:32:45 -0700226static void update_pageblock_skip(struct compact_control *cc,
227 struct page *page, unsigned long nr_isolated,
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700228 bool migrate_scanner)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700229{
230}
231#endif /* CONFIG_COMPACTION */
232
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700233/*
234 * Compaction requires the taking of some coarse locks that are potentially
235 * very heavily contended. For async compaction, back out if the lock cannot
236 * be taken immediately. For sync compaction, spin on the lock if needed.
237 *
238 * Returns true if the lock is held
239 * Returns false if the lock is not held and compaction should abort
240 */
241static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags,
242 struct compact_control *cc)
Mel Gorman2a1402a2012-10-08 16:32:33 -0700243{
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700244 if (cc->mode == MIGRATE_ASYNC) {
245 if (!spin_trylock_irqsave(lock, *flags)) {
246 cc->contended = COMPACT_CONTENDED_LOCK;
247 return false;
248 }
249 } else {
250 spin_lock_irqsave(lock, *flags);
251 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700252
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700253 return true;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700254}
255
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100256/*
Mel Gormanc67fe372012-08-21 16:16:17 -0700257 * Compaction requires the taking of some coarse locks that are potentially
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700258 * very heavily contended. The lock should be periodically unlocked to avoid
259 * having disabled IRQs for a long time, even when there is nobody waiting on
260 * the lock. It might also be that allowing the IRQs will result in
261 * need_resched() becoming true. If scheduling is needed, async compaction
262 * aborts. Sync compaction schedules.
263 * Either compaction type will also abort if a fatal signal is pending.
264 * In either case if the lock was locked, it is dropped and not regained.
Mel Gormanc67fe372012-08-21 16:16:17 -0700265 *
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700266 * Returns true if compaction should abort due to fatal signal pending, or
267 * async compaction due to need_resched()
268 * Returns false when compaction can continue (sync compaction might have
269 * scheduled)
Mel Gormanc67fe372012-08-21 16:16:17 -0700270 */
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700271static bool compact_unlock_should_abort(spinlock_t *lock,
272 unsigned long flags, bool *locked, struct compact_control *cc)
Mel Gormanc67fe372012-08-21 16:16:17 -0700273{
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700274 if (*locked) {
275 spin_unlock_irqrestore(lock, flags);
276 *locked = false;
277 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700278
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700279 if (fatal_signal_pending(current)) {
280 cc->contended = COMPACT_CONTENDED_SCHED;
281 return true;
282 }
Mel Gormanc67fe372012-08-21 16:16:17 -0700283
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700284 if (need_resched()) {
David Rientjese0b9dae2014-06-04 16:08:28 -0700285 if (cc->mode == MIGRATE_ASYNC) {
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700286 cc->contended = COMPACT_CONTENDED_SCHED;
287 return true;
Mel Gormanc67fe372012-08-21 16:16:17 -0700288 }
Mel Gormanc67fe372012-08-21 16:16:17 -0700289 cond_resched();
Mel Gormanc67fe372012-08-21 16:16:17 -0700290 }
291
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700292 return false;
Mel Gormanc67fe372012-08-21 16:16:17 -0700293}
294
Vlastimil Babkabe976572014-06-04 16:10:41 -0700295/*
296 * Aside from avoiding lock contention, compaction also periodically checks
297 * need_resched() and either schedules in sync compaction or aborts async
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700298 * compaction. This is similar to what compact_unlock_should_abort() does, but
Vlastimil Babkabe976572014-06-04 16:10:41 -0700299 * is used where no lock is concerned.
300 *
301 * Returns false when no scheduling was needed, or sync compaction scheduled.
302 * Returns true when async compaction should abort.
303 */
304static inline bool compact_should_abort(struct compact_control *cc)
305{
306 /* async compaction aborts if contended */
307 if (need_resched()) {
308 if (cc->mode == MIGRATE_ASYNC) {
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700309 cc->contended = COMPACT_CONTENDED_SCHED;
Vlastimil Babkabe976572014-06-04 16:10:41 -0700310 return true;
311 }
312
313 cond_resched();
314 }
315
316 return false;
317}
318
Mel Gormanf40d1e42012-10-08 16:32:36 -0700319/* Returns true if the page is within a block suitable for migration to */
320static bool suitable_migration_target(struct page *page)
321{
Joonsoo Kim7d348b92014-04-07 15:37:03 -0700322 /* If the page is a large free page, then disallow migration */
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700323 if (PageBuddy(page)) {
324 /*
325 * We are checking page_order without zone->lock taken. But
326 * the only small danger is that we skip a potentially suitable
327 * pageblock, so it's not worth to check order for valid range.
328 */
329 if (page_order_unsafe(page) >= pageblock_order)
330 return false;
331 }
Mel Gormanf40d1e42012-10-08 16:32:36 -0700332
333 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
Joonsoo Kim7d348b92014-04-07 15:37:03 -0700334 if (migrate_async_suitable(get_pageblock_migratetype(page)))
Mel Gormanf40d1e42012-10-08 16:32:36 -0700335 return true;
336
337 /* Otherwise skip the block */
338 return false;
339}
340
Mel Gormanc67fe372012-08-21 16:16:17 -0700341/*
Jerome Marchand9e4be472013-11-12 15:07:12 -0800342 * Isolate free pages onto a private freelist. If @strict is true, will abort
343 * returning 0 on any invalid PFNs or non-free pages inside of the pageblock
344 * (even though it may still end up isolating some pages).
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100345 */
Mel Gormanf40d1e42012-10-08 16:32:36 -0700346static unsigned long isolate_freepages_block(struct compact_control *cc,
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700347 unsigned long *start_pfn,
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100348 unsigned long end_pfn,
349 struct list_head *freelist,
350 bool strict)
Mel Gorman748446b2010-05-24 14:32:27 -0700351{
Mel Gormanb7aba692011-01-13 15:45:54 -0800352 int nr_scanned = 0, total_isolated = 0;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700353 struct page *cursor, *valid_page = NULL;
Xiubo Lib8b2d822014-10-09 15:28:21 -0700354 unsigned long flags = 0;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700355 bool locked = false;
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700356 unsigned long blockpfn = *start_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700357
Mel Gorman748446b2010-05-24 14:32:27 -0700358 cursor = pfn_to_page(blockpfn);
359
Mel Gormanf40d1e42012-10-08 16:32:36 -0700360 /* Isolate free pages. */
Mel Gorman748446b2010-05-24 14:32:27 -0700361 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
362 int isolated, i;
363 struct page *page = cursor;
364
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700365 /*
366 * Periodically drop the lock (if held) regardless of its
367 * contention, to give chance to IRQs. Abort if fatal signal
368 * pending or async compaction detects need_resched()
369 */
370 if (!(blockpfn % SWAP_CLUSTER_MAX)
371 && compact_unlock_should_abort(&cc->zone->lock, flags,
372 &locked, cc))
373 break;
374
Mel Gormanb7aba692011-01-13 15:45:54 -0800375 nr_scanned++;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700376 if (!pfn_valid_within(blockpfn))
Laura Abbott2af120b2014-03-10 15:49:44 -0700377 goto isolate_fail;
378
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700379 if (!valid_page)
380 valid_page = page;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700381 if (!PageBuddy(page))
Laura Abbott2af120b2014-03-10 15:49:44 -0700382 goto isolate_fail;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700383
384 /*
Vlastimil Babka69b71892014-10-09 15:27:18 -0700385 * If we already hold the lock, we can skip some rechecking.
386 * Note that if we hold the lock now, checked_pageblock was
387 * already set in some previous iteration (or strict is true),
388 * so it is correct to skip the suitable migration target
389 * recheck as well.
Mel Gormanf40d1e42012-10-08 16:32:36 -0700390 */
Vlastimil Babka69b71892014-10-09 15:27:18 -0700391 if (!locked) {
392 /*
393 * The zone lock must be held to isolate freepages.
394 * Unfortunately this is a very coarse lock and can be
395 * heavily contended if there are parallel allocations
396 * or parallel compactions. For async compaction do not
397 * spin on the lock and we acquire the lock as late as
398 * possible.
399 */
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700400 locked = compact_trylock_irqsave(&cc->zone->lock,
401 &flags, cc);
Vlastimil Babka69b71892014-10-09 15:27:18 -0700402 if (!locked)
403 break;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700404
Vlastimil Babka69b71892014-10-09 15:27:18 -0700405 /* Recheck this is a buddy page under lock */
406 if (!PageBuddy(page))
407 goto isolate_fail;
408 }
Mel Gorman748446b2010-05-24 14:32:27 -0700409
410 /* Found a free page, break it into order-0 pages */
411 isolated = split_free_page(page);
412 total_isolated += isolated;
413 for (i = 0; i < isolated; i++) {
414 list_add(&page->lru, freelist);
415 page++;
416 }
417
418 /* If a page was split, advance to the end of it */
419 if (isolated) {
420 blockpfn += isolated - 1;
421 cursor += isolated - 1;
Laura Abbott2af120b2014-03-10 15:49:44 -0700422 continue;
Mel Gorman748446b2010-05-24 14:32:27 -0700423 }
Laura Abbott2af120b2014-03-10 15:49:44 -0700424
425isolate_fail:
426 if (strict)
427 break;
428 else
429 continue;
430
Mel Gorman748446b2010-05-24 14:32:27 -0700431 }
432
Joonsoo Kime34d85f2015-02-11 15:27:04 -0800433 trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn,
434 nr_scanned, total_isolated);
435
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700436 /* Record how far we have got within the block */
437 *start_pfn = blockpfn;
438
Mel Gormanf40d1e42012-10-08 16:32:36 -0700439 /*
440 * If strict isolation is requested by CMA then check that all the
441 * pages requested were isolated. If there were any failures, 0 is
442 * returned and CMA will fail.
443 */
Laura Abbott2af120b2014-03-10 15:49:44 -0700444 if (strict && blockpfn < end_pfn)
Mel Gormanf40d1e42012-10-08 16:32:36 -0700445 total_isolated = 0;
446
447 if (locked)
448 spin_unlock_irqrestore(&cc->zone->lock, flags);
449
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700450 /* Update the pageblock-skip if the whole pageblock was scanned */
451 if (blockpfn == end_pfn)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700452 update_pageblock_skip(cc, valid_page, total_isolated, false);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700453
Minchan Kim010fc292012-12-20 15:05:06 -0800454 count_compact_events(COMPACTFREE_SCANNED, nr_scanned);
Mel Gorman397487d2012-10-19 12:00:10 +0100455 if (total_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -0800456 count_compact_events(COMPACTISOLATED, total_isolated);
Mel Gorman748446b2010-05-24 14:32:27 -0700457 return total_isolated;
458}
459
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100460/**
461 * isolate_freepages_range() - isolate free pages.
462 * @start_pfn: The first PFN to start isolating.
463 * @end_pfn: The one-past-last PFN.
464 *
465 * Non-free pages, invalid PFNs, or zone boundaries within the
466 * [start_pfn, end_pfn) range are considered errors, cause function to
467 * undo its actions and return zero.
468 *
469 * Otherwise, function returns one-past-the-last PFN of isolated page
470 * (which may be greater then end_pfn if end fell in a middle of
471 * a free page).
472 */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100473unsigned long
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700474isolate_freepages_range(struct compact_control *cc,
475 unsigned long start_pfn, unsigned long end_pfn)
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100476{
Mel Gormanf40d1e42012-10-08 16:32:36 -0700477 unsigned long isolated, pfn, block_end_pfn;
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100478 LIST_HEAD(freelist);
479
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700480 pfn = start_pfn;
481 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100482
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700483 for (; pfn < end_pfn; pfn += isolated,
484 block_end_pfn += pageblock_nr_pages) {
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700485 /* Protect pfn from changing by isolate_freepages_block */
486 unsigned long isolate_start_pfn = pfn;
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700487
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100488 block_end_pfn = min(block_end_pfn, end_pfn);
489
Joonsoo Kim58420012014-11-13 15:19:07 -0800490 /*
491 * pfn could pass the block_end_pfn if isolated freepage
492 * is more than pageblock order. In this case, we adjust
493 * scanning range to right one.
494 */
495 if (pfn >= block_end_pfn) {
496 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
497 block_end_pfn = min(block_end_pfn, end_pfn);
498 }
499
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700500 if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
501 break;
502
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700503 isolated = isolate_freepages_block(cc, &isolate_start_pfn,
504 block_end_pfn, &freelist, true);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100505
506 /*
507 * In strict mode, isolate_freepages_block() returns 0 if
508 * there are any holes in the block (ie. invalid PFNs or
509 * non-free pages).
510 */
511 if (!isolated)
512 break;
513
514 /*
515 * If we managed to isolate pages, it is always (1 << n) *
516 * pageblock_nr_pages for some non-negative n. (Max order
517 * page may span two pageblocks).
518 */
519 }
520
521 /* split_free_page does not map the pages */
522 map_pages(&freelist);
523
524 if (pfn < end_pfn) {
525 /* Loop terminated early, cleanup. */
526 release_freepages(&freelist);
527 return 0;
528 }
529
530 /* We don't use freelists for anything. */
531 return pfn;
532}
533
Mel Gorman748446b2010-05-24 14:32:27 -0700534/* Update the number of anon and file isolated pages in the zone */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700535static void acct_isolated(struct zone *zone, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700536{
537 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700538 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700539
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700540 if (list_empty(&cc->migratepages))
541 return;
542
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700543 list_for_each_entry(page, &cc->migratepages, lru)
544 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700545
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700546 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
547 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
Mel Gorman748446b2010-05-24 14:32:27 -0700548}
549
550/* Similar to reclaim, but different enough that they don't share logic */
551static bool too_many_isolated(struct zone *zone)
552{
Minchan Kimbc693042010-09-09 16:38:00 -0700553 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700554
555 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
556 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700557 active = zone_page_state(zone, NR_ACTIVE_FILE) +
558 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700559 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
560 zone_page_state(zone, NR_ISOLATED_ANON);
561
Minchan Kimbc693042010-09-09 16:38:00 -0700562 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700563}
564
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100565/**
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700566 * isolate_migratepages_block() - isolate all migrate-able pages within
567 * a single pageblock
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100568 * @cc: Compaction control structure.
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700569 * @low_pfn: The first PFN to isolate
570 * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock
571 * @isolate_mode: Isolation mode to be used.
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100572 *
573 * Isolate all pages that can be migrated from the range specified by
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700574 * [low_pfn, end_pfn). The range is expected to be within same pageblock.
575 * Returns zero if there is a fatal signal pending, otherwise PFN of the
576 * first page that was not scanned (which may be both less, equal to or more
577 * than end_pfn).
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100578 *
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700579 * The pages are isolated on cc->migratepages list (not required to be empty),
580 * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field
581 * is neither read nor updated.
Mel Gorman748446b2010-05-24 14:32:27 -0700582 */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700583static unsigned long
584isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
585 unsigned long end_pfn, isolate_mode_t isolate_mode)
Mel Gorman748446b2010-05-24 14:32:27 -0700586{
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700587 struct zone *zone = cc->zone;
Mel Gormanb7aba692011-01-13 15:45:54 -0800588 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700589 struct list_head *migratelist = &cc->migratepages;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700590 struct lruvec *lruvec;
Xiubo Lib8b2d822014-10-09 15:28:21 -0700591 unsigned long flags = 0;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700592 bool locked = false;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700593 struct page *page = NULL, *valid_page = NULL;
Joonsoo Kime34d85f2015-02-11 15:27:04 -0800594 unsigned long start_pfn = low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700595
Mel Gorman748446b2010-05-24 14:32:27 -0700596 /*
597 * Ensure that there are not too many pages isolated from the LRU
598 * list by either parallel reclaimers or compaction. If there are,
599 * delay for some time until fewer pages are isolated
600 */
601 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700602 /* async migration should just abort */
David Rientjese0b9dae2014-06-04 16:08:28 -0700603 if (cc->mode == MIGRATE_ASYNC)
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100604 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700605
Mel Gorman748446b2010-05-24 14:32:27 -0700606 congestion_wait(BLK_RW_ASYNC, HZ/10);
607
608 if (fatal_signal_pending(current))
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100609 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700610 }
611
Vlastimil Babkabe976572014-06-04 16:10:41 -0700612 if (compact_should_abort(cc))
613 return 0;
David Rientjesaeef4b82014-06-04 16:08:31 -0700614
Mel Gorman748446b2010-05-24 14:32:27 -0700615 /* Time to isolate some pages for migration */
Mel Gorman748446b2010-05-24 14:32:27 -0700616 for (; low_pfn < end_pfn; low_pfn++) {
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700617 /*
618 * Periodically drop the lock (if held) regardless of its
619 * contention, to give chance to IRQs. Abort async compaction
620 * if contended.
621 */
622 if (!(low_pfn % SWAP_CLUSTER_MAX)
623 && compact_unlock_should_abort(&zone->lru_lock, flags,
624 &locked, cc))
625 break;
Mel Gormanc67fe372012-08-21 16:16:17 -0700626
Mel Gorman748446b2010-05-24 14:32:27 -0700627 if (!pfn_valid_within(low_pfn))
628 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800629 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700630
Mel Gorman748446b2010-05-24 14:32:27 -0700631 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800632
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700633 if (!valid_page)
634 valid_page = page;
635
Mel Gorman6c144662014-01-23 15:53:38 -0800636 /*
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700637 * Skip if free. We read page order here without zone lock
638 * which is generally unsafe, but the race window is small and
639 * the worst thing that can happen is that we skip some
640 * potential isolation targets.
Mel Gorman6c144662014-01-23 15:53:38 -0800641 */
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700642 if (PageBuddy(page)) {
643 unsigned long freepage_order = page_order_unsafe(page);
644
645 /*
646 * Without lock, we cannot be sure that what we got is
647 * a valid page order. Consider only values in the
648 * valid order range to prevent low_pfn overflow.
649 */
650 if (freepage_order > 0 && freepage_order < MAX_ORDER)
651 low_pfn += (1UL << freepage_order) - 1;
Mel Gorman748446b2010-05-24 14:32:27 -0700652 continue;
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700653 }
Mel Gorman748446b2010-05-24 14:32:27 -0700654
Mel Gorman9927af742011-01-13 15:45:59 -0800655 /*
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800656 * Check may be lockless but that's ok as we recheck later.
657 * It's possible to migrate LRU pages and balloon pages
658 * Skip any other type of page
659 */
660 if (!PageLRU(page)) {
661 if (unlikely(balloon_page_movable(page))) {
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700662 if (balloon_page_isolate(page)) {
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800663 /* Successfully isolated */
Joonsoo Kimb6c75012014-04-07 15:37:07 -0700664 goto isolate_success;
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800665 }
666 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800667 continue;
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800668 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800669
670 /*
Mel Gorman2a1402a2012-10-08 16:32:33 -0700671 * PageLRU is set. lru_lock normally excludes isolation
672 * splitting and collapsing (collapsing has already happened
673 * if PageLRU is set) but the lock is not necessarily taken
674 * here and it is wasteful to take it just to check transhuge.
675 * Check TransHuge without lock and skip the whole pageblock if
676 * it's either a transhuge or hugetlbfs page, as calling
677 * compound_order() without preventing THP from splitting the
678 * page underneath us may return surprising results.
Andrea Arcangelibc835012011-01-13 15:47:08 -0800679 */
680 if (PageTransHuge(page)) {
Mel Gorman2a1402a2012-10-08 16:32:33 -0700681 if (!locked)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700682 low_pfn = ALIGN(low_pfn + 1,
683 pageblock_nr_pages) - 1;
684 else
685 low_pfn += (1 << compound_order(page)) - 1;
686
Mel Gorman2a1402a2012-10-08 16:32:33 -0700687 continue;
688 }
689
David Rientjes119d6d52014-04-03 14:48:00 -0700690 /*
691 * Migration will fail if an anonymous page is pinned in memory,
692 * so avoid taking lru_lock and isolating it unnecessarily in an
693 * admittedly racy check.
694 */
695 if (!page_mapping(page) &&
696 page_count(page) > page_mapcount(page))
697 continue;
698
Vlastimil Babka69b71892014-10-09 15:27:18 -0700699 /* If we already hold the lock, we can skip some rechecking */
700 if (!locked) {
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700701 locked = compact_trylock_irqsave(&zone->lru_lock,
702 &flags, cc);
Vlastimil Babka69b71892014-10-09 15:27:18 -0700703 if (!locked)
704 break;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700705
Vlastimil Babka69b71892014-10-09 15:27:18 -0700706 /* Recheck PageLRU and PageTransHuge under lock */
707 if (!PageLRU(page))
708 continue;
709 if (PageTransHuge(page)) {
710 low_pfn += (1 << compound_order(page)) - 1;
711 continue;
712 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800713 }
714
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700715 lruvec = mem_cgroup_page_lruvec(page, zone);
716
Mel Gorman748446b2010-05-24 14:32:27 -0700717 /* Try isolate the page */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700718 if (__isolate_lru_page(page, isolate_mode) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700719 continue;
720
Sasha Levin309381fea2014-01-23 15:52:54 -0800721 VM_BUG_ON_PAGE(PageTransCompound(page), page);
Andrea Arcangelibc835012011-01-13 15:47:08 -0800722
Mel Gorman748446b2010-05-24 14:32:27 -0700723 /* Successfully isolated */
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700724 del_page_from_lru_list(page, lruvec, page_lru(page));
Joonsoo Kimb6c75012014-04-07 15:37:07 -0700725
726isolate_success:
Mel Gorman748446b2010-05-24 14:32:27 -0700727 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700728 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800729 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700730
731 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800732 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
733 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700734 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800735 }
Mel Gorman748446b2010-05-24 14:32:27 -0700736 }
737
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700738 /*
739 * The PageBuddy() check could have potentially brought us outside
740 * the range to be scanned.
741 */
742 if (unlikely(low_pfn > end_pfn))
743 low_pfn = end_pfn;
744
Mel Gormanc67fe372012-08-21 16:16:17 -0700745 if (locked)
746 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700747
Vlastimil Babka50b5b092014-01-21 15:51:10 -0800748 /*
749 * Update the pageblock-skip information and cached scanner pfn,
750 * if the whole pageblock was scanned without isolating any page.
Vlastimil Babka50b5b092014-01-21 15:51:10 -0800751 */
David Rientjes35979ef2014-06-04 16:08:27 -0700752 if (low_pfn == end_pfn)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700753 update_pageblock_skip(cc, valid_page, nr_isolated, true);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700754
Joonsoo Kime34d85f2015-02-11 15:27:04 -0800755 trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn,
756 nr_scanned, nr_isolated);
Mel Gormanb7aba692011-01-13 15:45:54 -0800757
Minchan Kim010fc292012-12-20 15:05:06 -0800758 count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned);
Mel Gorman397487d2012-10-19 12:00:10 +0100759 if (nr_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -0800760 count_compact_events(COMPACTISOLATED, nr_isolated);
Mel Gorman397487d2012-10-19 12:00:10 +0100761
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100762 return low_pfn;
763}
764
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700765/**
766 * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
767 * @cc: Compaction control structure.
768 * @start_pfn: The first PFN to start isolating.
769 * @end_pfn: The one-past-last PFN.
770 *
771 * Returns zero if isolation fails fatally due to e.g. pending signal.
772 * Otherwise, function returns one-past-the-last PFN of isolated page
773 * (which may be greater than end_pfn if end fell in a middle of a THP page).
774 */
775unsigned long
776isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
777 unsigned long end_pfn)
778{
779 unsigned long pfn, block_end_pfn;
780
781 /* Scan block by block. First and last block may be incomplete */
782 pfn = start_pfn;
783 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
784
785 for (; pfn < end_pfn; pfn = block_end_pfn,
786 block_end_pfn += pageblock_nr_pages) {
787
788 block_end_pfn = min(block_end_pfn, end_pfn);
789
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700790 if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700791 continue;
792
793 pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
794 ISOLATE_UNEVICTABLE);
795
796 /*
797 * In case of fatal failure, release everything that might
798 * have been isolated in the previous iteration, and signal
799 * the failure back to caller.
800 */
801 if (!pfn) {
802 putback_movable_pages(&cc->migratepages);
803 cc->nr_migratepages = 0;
804 break;
805 }
Joonsoo Kim6ea41c02014-10-29 14:50:20 -0700806
807 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
808 break;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700809 }
810 acct_isolated(cc->zone, cc);
811
812 return pfn;
813}
814
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100815#endif /* CONFIG_COMPACTION || CONFIG_CMA */
816#ifdef CONFIG_COMPACTION
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100817/*
818 * Based on information in the current compact_control, find blocks
819 * suitable for isolating free pages from and then isolate them.
820 */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700821static void isolate_freepages(struct compact_control *cc)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100822{
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700823 struct zone *zone = cc->zone;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100824 struct page *page;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700825 unsigned long block_start_pfn; /* start of current pageblock */
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700826 unsigned long isolate_start_pfn; /* exact pfn we start at */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700827 unsigned long block_end_pfn; /* end of current pageblock */
828 unsigned long low_pfn; /* lowest pfn scanner is able to scan */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100829 int nr_freepages = cc->nr_freepages;
830 struct list_head *freelist = &cc->freepages;
831
832 /*
833 * Initialise the free scanner. The starting point is where we last
Vlastimil Babka49e068f2014-05-06 12:50:03 -0700834 * successfully isolated from, zone-cached value, or the end of the
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700835 * zone when isolating for the first time. For looping we also need
836 * this pfn aligned down to the pageblock boundary, because we do
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700837 * block_start_pfn -= pageblock_nr_pages in the for loop.
838 * For ending point, take care when isolating in last pageblock of a
839 * a zone which ends in the middle of a pageblock.
Vlastimil Babka49e068f2014-05-06 12:50:03 -0700840 * The low boundary is the end of the pageblock the migration scanner
841 * is using.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100842 */
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700843 isolate_start_pfn = cc->free_pfn;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700844 block_start_pfn = cc->free_pfn & ~(pageblock_nr_pages-1);
845 block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
846 zone_end_pfn(zone));
Vlastimil Babka7ed695e2014-01-21 15:51:09 -0800847 low_pfn = ALIGN(cc->migrate_pfn + 1, pageblock_nr_pages);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100848
849 /*
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100850 * Isolate free pages until enough are available to migrate the
851 * pages on cc->migratepages. We stop searching if the migrate
852 * and free page scanners meet or enough free pages are isolated.
853 */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700854 for (; block_start_pfn >= low_pfn && cc->nr_migratepages > nr_freepages;
855 block_end_pfn = block_start_pfn,
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700856 block_start_pfn -= pageblock_nr_pages,
857 isolate_start_pfn = block_start_pfn) {
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100858 unsigned long isolated;
859
David Rientjesf6ea3ad2013-09-30 13:45:03 -0700860 /*
861 * This can iterate a massively long zone without finding any
862 * suitable migration targets, so periodically check if we need
Vlastimil Babkabe976572014-06-04 16:10:41 -0700863 * to schedule, or even abort async compaction.
David Rientjesf6ea3ad2013-09-30 13:45:03 -0700864 */
Vlastimil Babkabe976572014-06-04 16:10:41 -0700865 if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
866 && compact_should_abort(cc))
867 break;
David Rientjesf6ea3ad2013-09-30 13:45:03 -0700868
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700869 page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
870 zone);
871 if (!page)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100872 continue;
873
874 /* Check the block is suitable for migration */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700875 if (!suitable_migration_target(page))
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100876 continue;
Linus Torvalds68e3e922012-06-03 20:05:57 -0700877
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700878 /* If isolation recently failed, do not retry */
879 if (!isolation_suitable(cc, page))
880 continue;
881
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700882 /* Found a block suitable for isolating free pages from. */
883 isolated = isolate_freepages_block(cc, &isolate_start_pfn,
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700884 block_end_pfn, freelist, false);
Mel Gormanf40d1e42012-10-08 16:32:36 -0700885 nr_freepages += isolated;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100886
887 /*
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700888 * Remember where the free scanner should restart next time,
889 * which is where isolate_freepages_block() left off.
890 * But if it scanned the whole pageblock, isolate_start_pfn
891 * now points at block_end_pfn, which is the start of the next
892 * pageblock.
893 * In that case we will however want to restart at the start
894 * of the previous pageblock.
895 */
896 cc->free_pfn = (isolate_start_pfn < block_end_pfn) ?
897 isolate_start_pfn :
898 block_start_pfn - pageblock_nr_pages;
899
900 /*
Vlastimil Babkabe976572014-06-04 16:10:41 -0700901 * isolate_freepages_block() might have aborted due to async
902 * compaction being contended
903 */
904 if (cc->contended)
905 break;
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100906 }
907
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100908 /* split_free_page does not map the pages */
909 map_pages(freelist);
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100910
Vlastimil Babka7ed695e2014-01-21 15:51:09 -0800911 /*
912 * If we crossed the migrate scanner, we want to keep it that way
913 * so that compact_finished() may detect this
914 */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700915 if (block_start_pfn < low_pfn)
Vlastimil Babkae9ade562014-06-04 16:08:34 -0700916 cc->free_pfn = cc->migrate_pfn;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700917
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100918 cc->nr_freepages = nr_freepages;
Mel Gorman748446b2010-05-24 14:32:27 -0700919}
920
921/*
922 * This is a migrate-callback that "allocates" freepages by taking pages
923 * from the isolated freelists in the block we are migrating to.
924 */
925static struct page *compaction_alloc(struct page *migratepage,
926 unsigned long data,
927 int **result)
928{
929 struct compact_control *cc = (struct compact_control *)data;
930 struct page *freepage;
931
Vlastimil Babkabe976572014-06-04 16:10:41 -0700932 /*
933 * Isolate free pages if necessary, and if we are not aborting due to
934 * contention.
935 */
Mel Gorman748446b2010-05-24 14:32:27 -0700936 if (list_empty(&cc->freepages)) {
Vlastimil Babkabe976572014-06-04 16:10:41 -0700937 if (!cc->contended)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700938 isolate_freepages(cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700939
940 if (list_empty(&cc->freepages))
941 return NULL;
942 }
943
944 freepage = list_entry(cc->freepages.next, struct page, lru);
945 list_del(&freepage->lru);
946 cc->nr_freepages--;
947
948 return freepage;
949}
950
951/*
David Rientjesd53aea32014-06-04 16:08:26 -0700952 * This is a migrate-callback that "frees" freepages back to the isolated
953 * freelist. All pages on the freelist are from the same zone, so there is no
954 * special handling needed for NUMA.
955 */
956static void compaction_free(struct page *page, unsigned long data)
957{
958 struct compact_control *cc = (struct compact_control *)data;
959
960 list_add(&page->lru, &cc->freepages);
961 cc->nr_freepages++;
962}
963
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100964/* possible outcome of isolate_migratepages */
965typedef enum {
966 ISOLATE_ABORT, /* Abort compaction now */
967 ISOLATE_NONE, /* No pages isolated, continue scanning */
968 ISOLATE_SUCCESS, /* Pages isolated, migrate */
969} isolate_migrate_t;
970
971/*
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700972 * Isolate all pages that can be migrated from the first suitable block,
973 * starting at the block pointed to by the migrate scanner pfn within
974 * compact_control.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100975 */
976static isolate_migrate_t isolate_migratepages(struct zone *zone,
977 struct compact_control *cc)
978{
979 unsigned long low_pfn, end_pfn;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700980 struct page *page;
981 const isolate_mode_t isolate_mode =
982 (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100983
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700984 /*
985 * Start at where we last stopped, or beginning of the zone as
986 * initialized by compact_zone()
987 */
988 low_pfn = cc->migrate_pfn;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100989
990 /* Only scan within a pageblock boundary */
Mel Gormana9aacbc2013-02-22 16:32:25 -0800991 end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100992
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700993 /*
994 * Iterate over whole pageblocks until we find the first suitable.
995 * Do not cross the free scanner.
996 */
997 for (; end_pfn <= cc->free_pfn;
998 low_pfn = end_pfn, end_pfn += pageblock_nr_pages) {
999
1000 /*
1001 * This can potentially iterate a massively long zone with
1002 * many pageblocks unsuitable, so periodically check if we
1003 * need to schedule, or even abort async compaction.
1004 */
1005 if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
1006 && compact_should_abort(cc))
1007 break;
1008
Vlastimil Babka7d49d882014-10-09 15:27:11 -07001009 page = pageblock_pfn_to_page(low_pfn, end_pfn, zone);
1010 if (!page)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001011 continue;
1012
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001013 /* If isolation recently failed, do not retry */
1014 if (!isolation_suitable(cc, page))
1015 continue;
1016
1017 /*
1018 * For async compaction, also only scan in MOVABLE blocks.
1019 * Async compaction is optimistic to see if the minimum amount
1020 * of work satisfies the allocation.
1021 */
1022 if (cc->mode == MIGRATE_ASYNC &&
1023 !migrate_async_suitable(get_pageblock_migratetype(page)))
1024 continue;
1025
1026 /* Perform the isolation */
1027 low_pfn = isolate_migratepages_block(cc, low_pfn, end_pfn,
1028 isolate_mode);
1029
1030 if (!low_pfn || cc->contended)
1031 return ISOLATE_ABORT;
1032
1033 /*
1034 * Either we isolated something and proceed with migration. Or
1035 * we failed and compact_zone should decide if we should
1036 * continue or not.
1037 */
1038 break;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001039 }
1040
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001041 acct_isolated(zone, cc);
Vlastimil Babka1d5bfe12014-11-13 15:19:30 -08001042 /*
1043 * Record where migration scanner will be restarted. If we end up in
1044 * the same pageblock as the free scanner, make the scanners fully
1045 * meet so that compact_finished() terminates compaction.
1046 */
1047 cc->migrate_pfn = (end_pfn <= cc->free_pfn) ? low_pfn : cc->free_pfn;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001048
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001049 return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001050}
1051
David Rientjes6d7ce552014-10-09 15:27:27 -07001052static int compact_finished(struct zone *zone, struct compact_control *cc,
1053 const int migratetype)
Mel Gorman748446b2010-05-24 14:32:27 -07001054{
Mel Gorman8fb74b92013-01-11 14:32:16 -08001055 unsigned int order;
Andrea Arcangeli5a03b052011-01-13 15:47:11 -08001056 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -07001057
Vlastimil Babkabe976572014-06-04 16:10:41 -07001058 if (cc->contended || fatal_signal_pending(current))
Mel Gorman748446b2010-05-24 14:32:27 -07001059 return COMPACT_PARTIAL;
1060
Mel Gorman753341a2012-10-08 16:32:40 -07001061 /* Compaction run completes if the migrate and free scanner meet */
Mel Gormanbb13ffe2012-10-08 16:32:41 -07001062 if (cc->free_pfn <= cc->migrate_pfn) {
Vlastimil Babka55b7c4c2014-01-21 15:51:11 -08001063 /* Let the next compaction start anew. */
David Rientjes35979ef2014-06-04 16:08:27 -07001064 zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
1065 zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
Vlastimil Babka55b7c4c2014-01-21 15:51:11 -08001066 zone->compact_cached_free_pfn = zone_end_pfn(zone);
1067
Mel Gorman62997022012-10-08 16:32:47 -07001068 /*
1069 * Mark that the PG_migrate_skip information should be cleared
1070 * by kswapd when it goes to sleep. kswapd does not set the
1071 * flag itself as the decision to be clear should be directly
1072 * based on an allocation request.
1073 */
1074 if (!current_is_kswapd())
1075 zone->compact_blockskip_flush = true;
1076
Mel Gorman748446b2010-05-24 14:32:27 -07001077 return COMPACT_COMPLETE;
Mel Gormanbb13ffe2012-10-08 16:32:41 -07001078 }
Mel Gorman748446b2010-05-24 14:32:27 -07001079
Johannes Weiner82478fb2011-01-20 14:44:21 -08001080 /*
1081 * order == -1 is expected when compacting via
1082 * /proc/sys/vm/compact_memory
1083 */
Mel Gorman56de7262010-05-24 14:32:30 -07001084 if (cc->order == -1)
1085 return COMPACT_CONTINUE;
1086
Michal Hocko3957c772011-06-15 15:08:25 -07001087 /* Compaction run is not finished if the watermark is not met */
1088 watermark = low_wmark_pages(zone);
Michal Hocko3957c772011-06-15 15:08:25 -07001089
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001090 if (!zone_watermark_ok(zone, cc->order, watermark, cc->classzone_idx,
1091 cc->alloc_flags))
Michal Hocko3957c772011-06-15 15:08:25 -07001092 return COMPACT_CONTINUE;
1093
Mel Gorman56de7262010-05-24 14:32:30 -07001094 /* Direct compactor: Is a suitable page free? */
Mel Gorman8fb74b92013-01-11 14:32:16 -08001095 for (order = cc->order; order < MAX_ORDER; order++) {
1096 struct free_area *area = &zone->free_area[order];
Mel Gorman56de7262010-05-24 14:32:30 -07001097
Mel Gorman8fb74b92013-01-11 14:32:16 -08001098 /* Job done if page is free of the right migratetype */
David Rientjes6d7ce552014-10-09 15:27:27 -07001099 if (!list_empty(&area->free_list[migratetype]))
Mel Gorman8fb74b92013-01-11 14:32:16 -08001100 return COMPACT_PARTIAL;
1101
1102 /* Job done if allocation would set block type */
1103 if (cc->order >= pageblock_order && area->nr_free)
1104 return COMPACT_PARTIAL;
Mel Gorman56de7262010-05-24 14:32:30 -07001105 }
1106
Mel Gorman748446b2010-05-24 14:32:27 -07001107 return COMPACT_CONTINUE;
1108}
1109
Mel Gorman3e7d3442011-01-13 15:45:56 -08001110/*
1111 * compaction_suitable: Is this suitable to run compaction on this zone now?
1112 * Returns
1113 * COMPACT_SKIPPED - If there are too few free pages for compaction
1114 * COMPACT_PARTIAL - If the allocation would succeed without compaction
1115 * COMPACT_CONTINUE - If compaction should run now
1116 */
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001117unsigned long compaction_suitable(struct zone *zone, int order,
1118 int alloc_flags, int classzone_idx)
Mel Gorman3e7d3442011-01-13 15:45:56 -08001119{
1120 int fragindex;
1121 unsigned long watermark;
1122
1123 /*
Michal Hocko3957c772011-06-15 15:08:25 -07001124 * order == -1 is expected when compacting via
1125 * /proc/sys/vm/compact_memory
1126 */
1127 if (order == -1)
1128 return COMPACT_CONTINUE;
1129
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001130 watermark = low_wmark_pages(zone);
1131 /*
1132 * If watermarks for high-order allocation are already met, there
1133 * should be no need for compaction at all.
1134 */
1135 if (zone_watermark_ok(zone, order, watermark, classzone_idx,
1136 alloc_flags))
1137 return COMPACT_PARTIAL;
1138
Michal Hocko3957c772011-06-15 15:08:25 -07001139 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -08001140 * Watermarks for order-0 must be met for compaction. Note the 2UL.
1141 * This is because during migration, copies of pages need to be
1142 * allocated and for a short time, the footprint is higher
1143 */
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001144 watermark += (2UL << order);
1145 if (!zone_watermark_ok(zone, 0, watermark, classzone_idx, alloc_flags))
Mel Gorman3e7d3442011-01-13 15:45:56 -08001146 return COMPACT_SKIPPED;
1147
1148 /*
1149 * fragmentation index determines if allocation failures are due to
1150 * low memory or external fragmentation
1151 *
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001152 * index of -1000 would imply allocations might succeed depending on
1153 * watermarks, but we already failed the high-order watermark check
Mel Gorman3e7d3442011-01-13 15:45:56 -08001154 * index towards 0 implies failure is due to lack of memory
1155 * index towards 1000 implies failure is due to fragmentation
1156 *
1157 * Only compact if a failure would be due to fragmentation.
1158 */
1159 fragindex = fragmentation_index(zone, order);
1160 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
1161 return COMPACT_SKIPPED;
1162
Mel Gorman3e7d3442011-01-13 15:45:56 -08001163 return COMPACT_CONTINUE;
1164}
1165
Mel Gorman748446b2010-05-24 14:32:27 -07001166static int compact_zone(struct zone *zone, struct compact_control *cc)
1167{
1168 int ret;
Mel Gormanc89511a2012-10-08 16:32:45 -07001169 unsigned long start_pfn = zone->zone_start_pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -08001170 unsigned long end_pfn = zone_end_pfn(zone);
David Rientjes6d7ce552014-10-09 15:27:27 -07001171 const int migratetype = gfpflags_to_migratetype(cc->gfp_mask);
David Rientjese0b9dae2014-06-04 16:08:28 -07001172 const bool sync = cc->mode != MIGRATE_ASYNC;
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08001173 unsigned long last_migrated_pfn = 0;
Mel Gorman748446b2010-05-24 14:32:27 -07001174
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001175 ret = compaction_suitable(zone, cc->order, cc->alloc_flags,
1176 cc->classzone_idx);
Mel Gorman3e7d3442011-01-13 15:45:56 -08001177 switch (ret) {
1178 case COMPACT_PARTIAL:
1179 case COMPACT_SKIPPED:
1180 /* Compaction is likely to fail */
1181 return ret;
1182 case COMPACT_CONTINUE:
1183 /* Fall through to compaction */
1184 ;
1185 }
1186
Mel Gormanc89511a2012-10-08 16:32:45 -07001187 /*
Vlastimil Babkad3132e42014-01-21 15:51:08 -08001188 * Clear pageblock skip if there were failures recently and compaction
1189 * is about to be retried after being deferred. kswapd does not do
1190 * this reset as it'll reset the cached information when going to sleep.
1191 */
1192 if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
1193 __reset_isolation_suitable(zone);
1194
1195 /*
Mel Gormanc89511a2012-10-08 16:32:45 -07001196 * Setup to move all movable pages to the end of the zone. Used cached
1197 * information on where the scanners should start but check that it
1198 * is initialised by ensuring the values are within zone boundaries.
1199 */
David Rientjese0b9dae2014-06-04 16:08:28 -07001200 cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync];
Mel Gormanc89511a2012-10-08 16:32:45 -07001201 cc->free_pfn = zone->compact_cached_free_pfn;
1202 if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
1203 cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
1204 zone->compact_cached_free_pfn = cc->free_pfn;
1205 }
1206 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
1207 cc->migrate_pfn = start_pfn;
David Rientjes35979ef2014-06-04 16:08:27 -07001208 zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
1209 zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
Mel Gormanc89511a2012-10-08 16:32:45 -07001210 }
Mel Gorman748446b2010-05-24 14:32:27 -07001211
Joonsoo Kim16c4a092015-02-11 15:27:01 -08001212 trace_mm_compaction_begin(start_pfn, cc->migrate_pfn,
1213 cc->free_pfn, end_pfn, sync);
Mel Gorman0eb927c2014-01-21 15:51:05 -08001214
Mel Gorman748446b2010-05-24 14:32:27 -07001215 migrate_prep_local();
1216
David Rientjes6d7ce552014-10-09 15:27:27 -07001217 while ((ret = compact_finished(zone, cc, migratetype)) ==
1218 COMPACT_CONTINUE) {
Minchan Kim9d502c12011-03-22 16:30:39 -07001219 int err;
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08001220 unsigned long isolate_start_pfn = cc->migrate_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -07001221
Mel Gormanf9e35b32011-06-15 15:08:52 -07001222 switch (isolate_migratepages(zone, cc)) {
1223 case ISOLATE_ABORT:
1224 ret = COMPACT_PARTIAL;
Rafael Aquini5733c7d2012-12-11 16:02:47 -08001225 putback_movable_pages(&cc->migratepages);
Shaohua Lie64c5232012-10-08 16:32:27 -07001226 cc->nr_migratepages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -07001227 goto out;
1228 case ISOLATE_NONE:
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08001229 /*
1230 * We haven't isolated and migrated anything, but
1231 * there might still be unflushed migrations from
1232 * previous cc->order aligned block.
1233 */
1234 goto check_drain;
Mel Gormanf9e35b32011-06-15 15:08:52 -07001235 case ISOLATE_SUCCESS:
1236 ;
1237 }
Mel Gorman748446b2010-05-24 14:32:27 -07001238
David Rientjesd53aea32014-06-04 16:08:26 -07001239 err = migrate_pages(&cc->migratepages, compaction_alloc,
David Rientjese0b9dae2014-06-04 16:08:28 -07001240 compaction_free, (unsigned long)cc, cc->mode,
Mel Gorman7b2a2d42012-10-19 14:07:31 +01001241 MR_COMPACTION);
Mel Gorman748446b2010-05-24 14:32:27 -07001242
Vlastimil Babkaf8c93012014-06-04 16:08:32 -07001243 trace_mm_compaction_migratepages(cc->nr_migratepages, err,
1244 &cc->migratepages);
Mel Gorman748446b2010-05-24 14:32:27 -07001245
Vlastimil Babkaf8c93012014-06-04 16:08:32 -07001246 /* All pages were either migrated or will be released */
1247 cc->nr_migratepages = 0;
Minchan Kim9d502c12011-03-22 16:30:39 -07001248 if (err) {
Rafael Aquini5733c7d2012-12-11 16:02:47 -08001249 putback_movable_pages(&cc->migratepages);
Vlastimil Babka7ed695e2014-01-21 15:51:09 -08001250 /*
1251 * migrate_pages() may return -ENOMEM when scanners meet
1252 * and we want compact_finished() to detect it
1253 */
1254 if (err == -ENOMEM && cc->free_pfn > cc->migrate_pfn) {
David Rientjes4bf2bba2012-07-11 14:02:13 -07001255 ret = COMPACT_PARTIAL;
1256 goto out;
1257 }
Mel Gorman748446b2010-05-24 14:32:27 -07001258 }
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08001259
1260 /*
1261 * Record where we could have freed pages by migration and not
1262 * yet flushed them to buddy allocator. We use the pfn that
1263 * isolate_migratepages() started from in this loop iteration
1264 * - this is the lowest page that could have been isolated and
1265 * then freed by migration.
1266 */
1267 if (!last_migrated_pfn)
1268 last_migrated_pfn = isolate_start_pfn;
1269
1270check_drain:
1271 /*
1272 * Has the migration scanner moved away from the previous
1273 * cc->order aligned block where we migrated from? If yes,
1274 * flush the pages that were freed, so that they can merge and
1275 * compact_finished() can detect immediately if allocation
1276 * would succeed.
1277 */
1278 if (cc->order > 0 && last_migrated_pfn) {
1279 int cpu;
1280 unsigned long current_block_start =
1281 cc->migrate_pfn & ~((1UL << cc->order) - 1);
1282
1283 if (last_migrated_pfn < current_block_start) {
1284 cpu = get_cpu();
1285 lru_add_drain_cpu(cpu);
1286 drain_local_pages(zone);
1287 put_cpu();
1288 /* No more flushing until we migrate again */
1289 last_migrated_pfn = 0;
1290 }
1291 }
1292
Mel Gorman748446b2010-05-24 14:32:27 -07001293 }
1294
Mel Gormanf9e35b32011-06-15 15:08:52 -07001295out:
Vlastimil Babka6bace092014-12-10 15:43:31 -08001296 /*
1297 * Release free pages and update where the free scanner should restart,
1298 * so we don't leave any returned pages behind in the next attempt.
1299 */
1300 if (cc->nr_freepages > 0) {
1301 unsigned long free_pfn = release_freepages(&cc->freepages);
1302
1303 cc->nr_freepages = 0;
1304 VM_BUG_ON(free_pfn == 0);
1305 /* The cached pfn is always the first in a pageblock */
1306 free_pfn &= ~(pageblock_nr_pages-1);
1307 /*
1308 * Only go back, not forward. The cached pfn might have been
1309 * already reset to zone end in compact_finished()
1310 */
1311 if (free_pfn > zone->compact_cached_free_pfn)
1312 zone->compact_cached_free_pfn = free_pfn;
1313 }
Mel Gorman748446b2010-05-24 14:32:27 -07001314
Joonsoo Kim16c4a092015-02-11 15:27:01 -08001315 trace_mm_compaction_end(start_pfn, cc->migrate_pfn,
1316 cc->free_pfn, end_pfn, sync, ret);
Mel Gorman0eb927c2014-01-21 15:51:05 -08001317
Mel Gorman748446b2010-05-24 14:32:27 -07001318 return ret;
1319}
Mel Gorman76ab0f52010-05-24 14:32:28 -07001320
David Rientjese0b9dae2014-06-04 16:08:28 -07001321static unsigned long compact_zone_order(struct zone *zone, int order,
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001322 gfp_t gfp_mask, enum migrate_mode mode, int *contended,
1323 int alloc_flags, int classzone_idx)
Mel Gorman56de7262010-05-24 14:32:30 -07001324{
Shaohua Lie64c5232012-10-08 16:32:27 -07001325 unsigned long ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001326 struct compact_control cc = {
1327 .nr_freepages = 0,
1328 .nr_migratepages = 0,
1329 .order = order,
David Rientjes6d7ce552014-10-09 15:27:27 -07001330 .gfp_mask = gfp_mask,
Mel Gorman56de7262010-05-24 14:32:30 -07001331 .zone = zone,
David Rientjese0b9dae2014-06-04 16:08:28 -07001332 .mode = mode,
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001333 .alloc_flags = alloc_flags,
1334 .classzone_idx = classzone_idx,
Mel Gorman56de7262010-05-24 14:32:30 -07001335 };
1336 INIT_LIST_HEAD(&cc.freepages);
1337 INIT_LIST_HEAD(&cc.migratepages);
1338
Shaohua Lie64c5232012-10-08 16:32:27 -07001339 ret = compact_zone(zone, &cc);
1340
1341 VM_BUG_ON(!list_empty(&cc.freepages));
1342 VM_BUG_ON(!list_empty(&cc.migratepages));
1343
1344 *contended = cc.contended;
1345 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001346}
1347
Mel Gorman5e771902010-05-24 14:32:31 -07001348int sysctl_extfrag_threshold = 500;
1349
Mel Gorman56de7262010-05-24 14:32:30 -07001350/**
1351 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
Mel Gorman56de7262010-05-24 14:32:30 -07001352 * @gfp_mask: The GFP mask of the current allocation
Vlastimil Babka1a6d53a2015-02-11 15:25:44 -08001353 * @order: The order of the current allocation
1354 * @alloc_flags: The allocation flags of the current allocation
1355 * @ac: The context of current allocation
David Rientjese0b9dae2014-06-04 16:08:28 -07001356 * @mode: The migration mode for async, sync light, or sync migration
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001357 * @contended: Return value that determines if compaction was aborted due to
1358 * need_resched() or lock contention
Mel Gorman56de7262010-05-24 14:32:30 -07001359 *
1360 * This is the main entry point for direct page compaction.
1361 */
Vlastimil Babka1a6d53a2015-02-11 15:25:44 -08001362unsigned long try_to_compact_pages(gfp_t gfp_mask, unsigned int order,
1363 int alloc_flags, const struct alloc_context *ac,
1364 enum migrate_mode mode, int *contended)
Mel Gorman56de7262010-05-24 14:32:30 -07001365{
Mel Gorman56de7262010-05-24 14:32:30 -07001366 int may_enter_fs = gfp_mask & __GFP_FS;
1367 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -07001368 struct zoneref *z;
1369 struct zone *zone;
Vlastimil Babka53853e22014-10-09 15:27:02 -07001370 int rc = COMPACT_DEFERRED;
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001371 int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */
1372
1373 *contended = COMPACT_CONTENDED_NONE;
Mel Gorman56de7262010-05-24 14:32:30 -07001374
Mel Gorman4ffb6332012-10-08 16:29:09 -07001375 /* Check if the GFP flags allow compaction */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -08001376 if (!order || !may_enter_fs || !may_perform_io)
Vlastimil Babka53853e22014-10-09 15:27:02 -07001377 return COMPACT_SKIPPED;
Mel Gorman56de7262010-05-24 14:32:30 -07001378
1379 /* Compact each zone in the list */
Vlastimil Babka1a6d53a2015-02-11 15:25:44 -08001380 for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
1381 ac->nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -07001382 int status;
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001383 int zone_contended;
Mel Gorman56de7262010-05-24 14:32:30 -07001384
Vlastimil Babka53853e22014-10-09 15:27:02 -07001385 if (compaction_deferred(zone, order))
1386 continue;
1387
David Rientjese0b9dae2014-06-04 16:08:28 -07001388 status = compact_zone_order(zone, order, gfp_mask, mode,
Vlastimil Babka1a6d53a2015-02-11 15:25:44 -08001389 &zone_contended, alloc_flags,
1390 ac->classzone_idx);
Mel Gorman56de7262010-05-24 14:32:30 -07001391 rc = max(status, rc);
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001392 /*
1393 * It takes at least one zone that wasn't lock contended
1394 * to clear all_zones_contended.
1395 */
1396 all_zones_contended &= zone_contended;
Mel Gorman56de7262010-05-24 14:32:30 -07001397
Mel Gorman3e7d3442011-01-13 15:45:56 -08001398 /* If a normal allocation would succeed, stop compacting */
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001399 if (zone_watermark_ok(zone, order, low_wmark_pages(zone),
Vlastimil Babka1a6d53a2015-02-11 15:25:44 -08001400 ac->classzone_idx, alloc_flags)) {
Vlastimil Babka53853e22014-10-09 15:27:02 -07001401 /*
1402 * We think the allocation will succeed in this zone,
1403 * but it is not certain, hence the false. The caller
1404 * will repeat this with true if allocation indeed
1405 * succeeds in this zone.
1406 */
1407 compaction_defer_reset(zone, order, false);
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001408 /*
1409 * It is possible that async compaction aborted due to
1410 * need_resched() and the watermarks were ok thanks to
1411 * somebody else freeing memory. The allocation can
1412 * however still fail so we better signal the
1413 * need_resched() contention anyway (this will not
1414 * prevent the allocation attempt).
1415 */
1416 if (zone_contended == COMPACT_CONTENDED_SCHED)
1417 *contended = COMPACT_CONTENDED_SCHED;
1418
1419 goto break_loop;
1420 }
1421
Vlastimil Babkaf8669792014-12-10 15:43:28 -08001422 if (mode != MIGRATE_ASYNC && status == COMPACT_COMPLETE) {
Vlastimil Babka53853e22014-10-09 15:27:02 -07001423 /*
1424 * We think that allocation won't succeed in this zone
1425 * so we defer compaction there. If it ends up
1426 * succeeding after all, it will be reset.
1427 */
1428 defer_compaction(zone, order);
1429 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001430
1431 /*
1432 * We might have stopped compacting due to need_resched() in
1433 * async compaction, or due to a fatal signal detected. In that
1434 * case do not try further zones and signal need_resched()
1435 * contention.
1436 */
1437 if ((zone_contended == COMPACT_CONTENDED_SCHED)
1438 || fatal_signal_pending(current)) {
1439 *contended = COMPACT_CONTENDED_SCHED;
1440 goto break_loop;
1441 }
1442
1443 continue;
1444break_loop:
1445 /*
1446 * We might not have tried all the zones, so be conservative
1447 * and assume they are not all lock contended.
1448 */
1449 all_zones_contended = 0;
1450 break;
Mel Gorman56de7262010-05-24 14:32:30 -07001451 }
1452
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001453 /*
1454 * If at least one zone wasn't deferred or skipped, we report if all
1455 * zones that were tried were lock contended.
1456 */
1457 if (rc > COMPACT_SKIPPED && all_zones_contended)
1458 *contended = COMPACT_CONTENDED_LOCK;
1459
Mel Gorman56de7262010-05-24 14:32:30 -07001460 return rc;
1461}
1462
1463
Mel Gorman76ab0f52010-05-24 14:32:28 -07001464/* Compact all zones within a node */
Andrew Morton7103f162013-02-22 16:32:33 -08001465static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001466{
1467 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -07001468 struct zone *zone;
1469
Mel Gorman76ab0f52010-05-24 14:32:28 -07001470 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -07001471
1472 zone = &pgdat->node_zones[zoneid];
1473 if (!populated_zone(zone))
1474 continue;
1475
Rik van Riel7be62de2012-03-21 16:33:52 -07001476 cc->nr_freepages = 0;
1477 cc->nr_migratepages = 0;
1478 cc->zone = zone;
1479 INIT_LIST_HEAD(&cc->freepages);
1480 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001481
Dan Carpenteraad6ec32012-03-21 16:33:54 -07001482 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -07001483 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001484
Rik van Rielaff62242012-03-21 16:33:52 -07001485 if (cc->order > 0) {
Vlastimil Babkade6c60a2014-01-21 15:51:07 -08001486 if (zone_watermark_ok(zone, cc->order,
1487 low_wmark_pages(zone), 0, 0))
1488 compaction_defer_reset(zone, cc->order, false);
Rik van Rielaff62242012-03-21 16:33:52 -07001489 }
1490
Rik van Riel7be62de2012-03-21 16:33:52 -07001491 VM_BUG_ON(!list_empty(&cc->freepages));
1492 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -07001493 }
Mel Gorman76ab0f52010-05-24 14:32:28 -07001494}
1495
Andrew Morton7103f162013-02-22 16:32:33 -08001496void compact_pgdat(pg_data_t *pgdat, int order)
Rik van Riel7be62de2012-03-21 16:33:52 -07001497{
1498 struct compact_control cc = {
1499 .order = order,
David Rientjese0b9dae2014-06-04 16:08:28 -07001500 .mode = MIGRATE_ASYNC,
Rik van Riel7be62de2012-03-21 16:33:52 -07001501 };
1502
Mel Gorman3a7200a2013-09-11 14:22:19 -07001503 if (!order)
1504 return;
1505
Andrew Morton7103f162013-02-22 16:32:33 -08001506 __compact_pgdat(pgdat, &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001507}
1508
Andrew Morton7103f162013-02-22 16:32:33 -08001509static void compact_node(int nid)
Rik van Riel7be62de2012-03-21 16:33:52 -07001510{
Rik van Riel7be62de2012-03-21 16:33:52 -07001511 struct compact_control cc = {
1512 .order = -1,
David Rientjese0b9dae2014-06-04 16:08:28 -07001513 .mode = MIGRATE_SYNC,
David Rientjes91ca9182014-04-03 14:47:23 -07001514 .ignore_skip_hint = true,
Rik van Riel7be62de2012-03-21 16:33:52 -07001515 };
1516
Andrew Morton7103f162013-02-22 16:32:33 -08001517 __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001518}
1519
Mel Gorman76ab0f52010-05-24 14:32:28 -07001520/* Compact all nodes in the system */
Jason Liu7964c062013-01-11 14:31:47 -08001521static void compact_nodes(void)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001522{
1523 int nid;
1524
Hugh Dickins8575ec22012-03-21 16:33:53 -07001525 /* Flush pending updates to the LRU lists */
1526 lru_add_drain_all();
1527
Mel Gorman76ab0f52010-05-24 14:32:28 -07001528 for_each_online_node(nid)
1529 compact_node(nid);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001530}
1531
1532/* The written value is actually unused, all memory is compacted */
1533int sysctl_compact_memory;
1534
1535/* This is the entry point for compacting all nodes via /proc/sys/vm */
1536int sysctl_compaction_handler(struct ctl_table *table, int write,
1537 void __user *buffer, size_t *length, loff_t *ppos)
1538{
1539 if (write)
Jason Liu7964c062013-01-11 14:31:47 -08001540 compact_nodes();
Mel Gorman76ab0f52010-05-24 14:32:28 -07001541
1542 return 0;
1543}
Mel Gormaned4a6d72010-05-24 14:32:29 -07001544
Mel Gorman5e771902010-05-24 14:32:31 -07001545int sysctl_extfrag_handler(struct ctl_table *table, int write,
1546 void __user *buffer, size_t *length, loff_t *ppos)
1547{
1548 proc_dointvec_minmax(table, write, buffer, length, ppos);
1549
1550 return 0;
1551}
1552
Mel Gormaned4a6d72010-05-24 14:32:29 -07001553#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Rashika Kheria74e77fb2014-04-03 14:48:01 -07001554static ssize_t sysfs_compact_node(struct device *dev,
Kay Sievers10fbcf42011-12-21 14:48:43 -08001555 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -07001556 const char *buf, size_t count)
1557{
Hugh Dickins8575ec22012-03-21 16:33:53 -07001558 int nid = dev->id;
1559
1560 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1561 /* Flush pending updates to the LRU lists */
1562 lru_add_drain_all();
1563
1564 compact_node(nid);
1565 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07001566
1567 return count;
1568}
Kay Sievers10fbcf42011-12-21 14:48:43 -08001569static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001570
1571int compaction_register_node(struct node *node)
1572{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001573 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001574}
1575
1576void compaction_unregister_node(struct node *node)
1577{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001578 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001579}
1580#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001581
1582#endif /* CONFIG_COMPACTION */