blob: 66f7c365e888936a1c0ebae846c20d927d8a0d2d [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
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700433 /* Record how far we have got within the block */
434 *start_pfn = blockpfn;
435
Mel Gormanb7aba692011-01-13 15:45:54 -0800436 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
Mel Gormanf40d1e42012-10-08 16:32:36 -0700437
438 /*
439 * If strict isolation is requested by CMA then check that all the
440 * pages requested were isolated. If there were any failures, 0 is
441 * returned and CMA will fail.
442 */
Laura Abbott2af120b2014-03-10 15:49:44 -0700443 if (strict && blockpfn < end_pfn)
Mel Gormanf40d1e42012-10-08 16:32:36 -0700444 total_isolated = 0;
445
446 if (locked)
447 spin_unlock_irqrestore(&cc->zone->lock, flags);
448
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700449 /* Update the pageblock-skip if the whole pageblock was scanned */
450 if (blockpfn == end_pfn)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700451 update_pageblock_skip(cc, valid_page, total_isolated, false);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700452
Minchan Kim010fc292012-12-20 15:05:06 -0800453 count_compact_events(COMPACTFREE_SCANNED, nr_scanned);
Mel Gorman397487d2012-10-19 12:00:10 +0100454 if (total_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -0800455 count_compact_events(COMPACTISOLATED, total_isolated);
Mel Gorman748446b2010-05-24 14:32:27 -0700456 return total_isolated;
457}
458
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100459/**
460 * isolate_freepages_range() - isolate free pages.
461 * @start_pfn: The first PFN to start isolating.
462 * @end_pfn: The one-past-last PFN.
463 *
464 * Non-free pages, invalid PFNs, or zone boundaries within the
465 * [start_pfn, end_pfn) range are considered errors, cause function to
466 * undo its actions and return zero.
467 *
468 * Otherwise, function returns one-past-the-last PFN of isolated page
469 * (which may be greater then end_pfn if end fell in a middle of
470 * a free page).
471 */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100472unsigned long
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700473isolate_freepages_range(struct compact_control *cc,
474 unsigned long start_pfn, unsigned long end_pfn)
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100475{
Mel Gormanf40d1e42012-10-08 16:32:36 -0700476 unsigned long isolated, pfn, block_end_pfn;
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100477 LIST_HEAD(freelist);
478
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700479 pfn = start_pfn;
480 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100481
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700482 for (; pfn < end_pfn; pfn += isolated,
483 block_end_pfn += pageblock_nr_pages) {
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700484 /* Protect pfn from changing by isolate_freepages_block */
485 unsigned long isolate_start_pfn = pfn;
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700486
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100487 block_end_pfn = min(block_end_pfn, end_pfn);
488
Joonsoo Kim58420012014-11-13 15:19:07 -0800489 /*
490 * pfn could pass the block_end_pfn if isolated freepage
491 * is more than pageblock order. In this case, we adjust
492 * scanning range to right one.
493 */
494 if (pfn >= block_end_pfn) {
495 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
496 block_end_pfn = min(block_end_pfn, end_pfn);
497 }
498
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700499 if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
500 break;
501
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700502 isolated = isolate_freepages_block(cc, &isolate_start_pfn,
503 block_end_pfn, &freelist, true);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100504
505 /*
506 * In strict mode, isolate_freepages_block() returns 0 if
507 * there are any holes in the block (ie. invalid PFNs or
508 * non-free pages).
509 */
510 if (!isolated)
511 break;
512
513 /*
514 * If we managed to isolate pages, it is always (1 << n) *
515 * pageblock_nr_pages for some non-negative n. (Max order
516 * page may span two pageblocks).
517 */
518 }
519
520 /* split_free_page does not map the pages */
521 map_pages(&freelist);
522
523 if (pfn < end_pfn) {
524 /* Loop terminated early, cleanup. */
525 release_freepages(&freelist);
526 return 0;
527 }
528
529 /* We don't use freelists for anything. */
530 return pfn;
531}
532
Mel Gorman748446b2010-05-24 14:32:27 -0700533/* Update the number of anon and file isolated pages in the zone */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700534static void acct_isolated(struct zone *zone, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700535{
536 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700537 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700538
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700539 if (list_empty(&cc->migratepages))
540 return;
541
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700542 list_for_each_entry(page, &cc->migratepages, lru)
543 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700544
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700545 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
546 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
Mel Gorman748446b2010-05-24 14:32:27 -0700547}
548
549/* Similar to reclaim, but different enough that they don't share logic */
550static bool too_many_isolated(struct zone *zone)
551{
Minchan Kimbc693042010-09-09 16:38:00 -0700552 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700553
554 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
555 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700556 active = zone_page_state(zone, NR_ACTIVE_FILE) +
557 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700558 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
559 zone_page_state(zone, NR_ISOLATED_ANON);
560
Minchan Kimbc693042010-09-09 16:38:00 -0700561 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700562}
563
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100564/**
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700565 * isolate_migratepages_block() - isolate all migrate-able pages within
566 * a single pageblock
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100567 * @cc: Compaction control structure.
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700568 * @low_pfn: The first PFN to isolate
569 * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock
570 * @isolate_mode: Isolation mode to be used.
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100571 *
572 * Isolate all pages that can be migrated from the range specified by
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700573 * [low_pfn, end_pfn). The range is expected to be within same pageblock.
574 * Returns zero if there is a fatal signal pending, otherwise PFN of the
575 * first page that was not scanned (which may be both less, equal to or more
576 * than end_pfn).
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100577 *
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700578 * The pages are isolated on cc->migratepages list (not required to be empty),
579 * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field
580 * is neither read nor updated.
Mel Gorman748446b2010-05-24 14:32:27 -0700581 */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700582static unsigned long
583isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
584 unsigned long end_pfn, isolate_mode_t isolate_mode)
Mel Gorman748446b2010-05-24 14:32:27 -0700585{
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700586 struct zone *zone = cc->zone;
Mel Gormanb7aba692011-01-13 15:45:54 -0800587 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700588 struct list_head *migratelist = &cc->migratepages;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700589 struct lruvec *lruvec;
Xiubo Lib8b2d822014-10-09 15:28:21 -0700590 unsigned long flags = 0;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700591 bool locked = false;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700592 struct page *page = NULL, *valid_page = NULL;
Mel Gorman748446b2010-05-24 14:32:27 -0700593
Mel Gorman748446b2010-05-24 14:32:27 -0700594 /*
595 * Ensure that there are not too many pages isolated from the LRU
596 * list by either parallel reclaimers or compaction. If there are,
597 * delay for some time until fewer pages are isolated
598 */
599 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700600 /* async migration should just abort */
David Rientjese0b9dae2014-06-04 16:08:28 -0700601 if (cc->mode == MIGRATE_ASYNC)
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100602 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700603
Mel Gorman748446b2010-05-24 14:32:27 -0700604 congestion_wait(BLK_RW_ASYNC, HZ/10);
605
606 if (fatal_signal_pending(current))
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100607 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700608 }
609
Vlastimil Babkabe976572014-06-04 16:10:41 -0700610 if (compact_should_abort(cc))
611 return 0;
David Rientjesaeef4b82014-06-04 16:08:31 -0700612
Mel Gorman748446b2010-05-24 14:32:27 -0700613 /* Time to isolate some pages for migration */
Mel Gorman748446b2010-05-24 14:32:27 -0700614 for (; low_pfn < end_pfn; low_pfn++) {
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700615 /*
616 * Periodically drop the lock (if held) regardless of its
617 * contention, to give chance to IRQs. Abort async compaction
618 * if contended.
619 */
620 if (!(low_pfn % SWAP_CLUSTER_MAX)
621 && compact_unlock_should_abort(&zone->lru_lock, flags,
622 &locked, cc))
623 break;
Mel Gormanc67fe372012-08-21 16:16:17 -0700624
Mel Gorman748446b2010-05-24 14:32:27 -0700625 if (!pfn_valid_within(low_pfn))
626 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800627 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700628
Mel Gorman748446b2010-05-24 14:32:27 -0700629 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800630
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700631 if (!valid_page)
632 valid_page = page;
633
Mel Gorman6c144662014-01-23 15:53:38 -0800634 /*
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700635 * Skip if free. We read page order here without zone lock
636 * which is generally unsafe, but the race window is small and
637 * the worst thing that can happen is that we skip some
638 * potential isolation targets.
Mel Gorman6c144662014-01-23 15:53:38 -0800639 */
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700640 if (PageBuddy(page)) {
641 unsigned long freepage_order = page_order_unsafe(page);
642
643 /*
644 * Without lock, we cannot be sure that what we got is
645 * a valid page order. Consider only values in the
646 * valid order range to prevent low_pfn overflow.
647 */
648 if (freepage_order > 0 && freepage_order < MAX_ORDER)
649 low_pfn += (1UL << freepage_order) - 1;
Mel Gorman748446b2010-05-24 14:32:27 -0700650 continue;
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700651 }
Mel Gorman748446b2010-05-24 14:32:27 -0700652
Mel Gorman9927af742011-01-13 15:45:59 -0800653 /*
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800654 * Check may be lockless but that's ok as we recheck later.
655 * It's possible to migrate LRU pages and balloon pages
656 * Skip any other type of page
657 */
658 if (!PageLRU(page)) {
659 if (unlikely(balloon_page_movable(page))) {
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700660 if (balloon_page_isolate(page)) {
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800661 /* Successfully isolated */
Joonsoo Kimb6c75012014-04-07 15:37:07 -0700662 goto isolate_success;
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800663 }
664 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800665 continue;
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800666 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800667
668 /*
Mel Gorman2a1402a2012-10-08 16:32:33 -0700669 * PageLRU is set. lru_lock normally excludes isolation
670 * splitting and collapsing (collapsing has already happened
671 * if PageLRU is set) but the lock is not necessarily taken
672 * here and it is wasteful to take it just to check transhuge.
673 * Check TransHuge without lock and skip the whole pageblock if
674 * it's either a transhuge or hugetlbfs page, as calling
675 * compound_order() without preventing THP from splitting the
676 * page underneath us may return surprising results.
Andrea Arcangelibc835012011-01-13 15:47:08 -0800677 */
678 if (PageTransHuge(page)) {
Mel Gorman2a1402a2012-10-08 16:32:33 -0700679 if (!locked)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700680 low_pfn = ALIGN(low_pfn + 1,
681 pageblock_nr_pages) - 1;
682 else
683 low_pfn += (1 << compound_order(page)) - 1;
684
Mel Gorman2a1402a2012-10-08 16:32:33 -0700685 continue;
686 }
687
David Rientjes119d6d52014-04-03 14:48:00 -0700688 /*
689 * Migration will fail if an anonymous page is pinned in memory,
690 * so avoid taking lru_lock and isolating it unnecessarily in an
691 * admittedly racy check.
692 */
693 if (!page_mapping(page) &&
694 page_count(page) > page_mapcount(page))
695 continue;
696
Vlastimil Babka69b71892014-10-09 15:27:18 -0700697 /* If we already hold the lock, we can skip some rechecking */
698 if (!locked) {
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700699 locked = compact_trylock_irqsave(&zone->lru_lock,
700 &flags, cc);
Vlastimil Babka69b71892014-10-09 15:27:18 -0700701 if (!locked)
702 break;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700703
Vlastimil Babka69b71892014-10-09 15:27:18 -0700704 /* Recheck PageLRU and PageTransHuge under lock */
705 if (!PageLRU(page))
706 continue;
707 if (PageTransHuge(page)) {
708 low_pfn += (1 << compound_order(page)) - 1;
709 continue;
710 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800711 }
712
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700713 lruvec = mem_cgroup_page_lruvec(page, zone);
714
Mel Gorman748446b2010-05-24 14:32:27 -0700715 /* Try isolate the page */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700716 if (__isolate_lru_page(page, isolate_mode) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700717 continue;
718
Sasha Levin309381fea2014-01-23 15:52:54 -0800719 VM_BUG_ON_PAGE(PageTransCompound(page), page);
Andrea Arcangelibc835012011-01-13 15:47:08 -0800720
Mel Gorman748446b2010-05-24 14:32:27 -0700721 /* Successfully isolated */
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700722 del_page_from_lru_list(page, lruvec, page_lru(page));
Joonsoo Kimb6c75012014-04-07 15:37:07 -0700723
724isolate_success:
Mel Gorman748446b2010-05-24 14:32:27 -0700725 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700726 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800727 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700728
729 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800730 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
731 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700732 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800733 }
Mel Gorman748446b2010-05-24 14:32:27 -0700734 }
735
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700736 /*
737 * The PageBuddy() check could have potentially brought us outside
738 * the range to be scanned.
739 */
740 if (unlikely(low_pfn > end_pfn))
741 low_pfn = end_pfn;
742
Mel Gormanc67fe372012-08-21 16:16:17 -0700743 if (locked)
744 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700745
Vlastimil Babka50b5b092014-01-21 15:51:10 -0800746 /*
747 * Update the pageblock-skip information and cached scanner pfn,
748 * if the whole pageblock was scanned without isolating any page.
Vlastimil Babka50b5b092014-01-21 15:51:10 -0800749 */
David Rientjes35979ef2014-06-04 16:08:27 -0700750 if (low_pfn == end_pfn)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700751 update_pageblock_skip(cc, valid_page, nr_isolated, true);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700752
Mel Gormanb7aba692011-01-13 15:45:54 -0800753 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
754
Minchan Kim010fc292012-12-20 15:05:06 -0800755 count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned);
Mel Gorman397487d2012-10-19 12:00:10 +0100756 if (nr_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -0800757 count_compact_events(COMPACTISOLATED, nr_isolated);
Mel Gorman397487d2012-10-19 12:00:10 +0100758
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100759 return low_pfn;
760}
761
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700762/**
763 * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
764 * @cc: Compaction control structure.
765 * @start_pfn: The first PFN to start isolating.
766 * @end_pfn: The one-past-last PFN.
767 *
768 * Returns zero if isolation fails fatally due to e.g. pending signal.
769 * Otherwise, function returns one-past-the-last PFN of isolated page
770 * (which may be greater than end_pfn if end fell in a middle of a THP page).
771 */
772unsigned long
773isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
774 unsigned long end_pfn)
775{
776 unsigned long pfn, block_end_pfn;
777
778 /* Scan block by block. First and last block may be incomplete */
779 pfn = start_pfn;
780 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
781
782 for (; pfn < end_pfn; pfn = block_end_pfn,
783 block_end_pfn += pageblock_nr_pages) {
784
785 block_end_pfn = min(block_end_pfn, end_pfn);
786
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700787 if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700788 continue;
789
790 pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
791 ISOLATE_UNEVICTABLE);
792
793 /*
794 * In case of fatal failure, release everything that might
795 * have been isolated in the previous iteration, and signal
796 * the failure back to caller.
797 */
798 if (!pfn) {
799 putback_movable_pages(&cc->migratepages);
800 cc->nr_migratepages = 0;
801 break;
802 }
Joonsoo Kim6ea41c02014-10-29 14:50:20 -0700803
804 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
805 break;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700806 }
807 acct_isolated(cc->zone, cc);
808
809 return pfn;
810}
811
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100812#endif /* CONFIG_COMPACTION || CONFIG_CMA */
813#ifdef CONFIG_COMPACTION
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100814/*
815 * Based on information in the current compact_control, find blocks
816 * suitable for isolating free pages from and then isolate them.
817 */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700818static void isolate_freepages(struct compact_control *cc)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100819{
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700820 struct zone *zone = cc->zone;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100821 struct page *page;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700822 unsigned long block_start_pfn; /* start of current pageblock */
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700823 unsigned long isolate_start_pfn; /* exact pfn we start at */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700824 unsigned long block_end_pfn; /* end of current pageblock */
825 unsigned long low_pfn; /* lowest pfn scanner is able to scan */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100826 int nr_freepages = cc->nr_freepages;
827 struct list_head *freelist = &cc->freepages;
828
829 /*
830 * Initialise the free scanner. The starting point is where we last
Vlastimil Babka49e068f2014-05-06 12:50:03 -0700831 * successfully isolated from, zone-cached value, or the end of the
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700832 * zone when isolating for the first time. For looping we also need
833 * this pfn aligned down to the pageblock boundary, because we do
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700834 * block_start_pfn -= pageblock_nr_pages in the for loop.
835 * For ending point, take care when isolating in last pageblock of a
836 * a zone which ends in the middle of a pageblock.
Vlastimil Babka49e068f2014-05-06 12:50:03 -0700837 * The low boundary is the end of the pageblock the migration scanner
838 * is using.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100839 */
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700840 isolate_start_pfn = cc->free_pfn;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700841 block_start_pfn = cc->free_pfn & ~(pageblock_nr_pages-1);
842 block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
843 zone_end_pfn(zone));
Vlastimil Babka7ed695e2014-01-21 15:51:09 -0800844 low_pfn = ALIGN(cc->migrate_pfn + 1, pageblock_nr_pages);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100845
846 /*
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100847 * Isolate free pages until enough are available to migrate the
848 * pages on cc->migratepages. We stop searching if the migrate
849 * and free page scanners meet or enough free pages are isolated.
850 */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700851 for (; block_start_pfn >= low_pfn && cc->nr_migratepages > nr_freepages;
852 block_end_pfn = block_start_pfn,
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700853 block_start_pfn -= pageblock_nr_pages,
854 isolate_start_pfn = block_start_pfn) {
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100855 unsigned long isolated;
856
David Rientjesf6ea3ad2013-09-30 13:45:03 -0700857 /*
858 * This can iterate a massively long zone without finding any
859 * suitable migration targets, so periodically check if we need
Vlastimil Babkabe976572014-06-04 16:10:41 -0700860 * to schedule, or even abort async compaction.
David Rientjesf6ea3ad2013-09-30 13:45:03 -0700861 */
Vlastimil Babkabe976572014-06-04 16:10:41 -0700862 if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
863 && compact_should_abort(cc))
864 break;
David Rientjesf6ea3ad2013-09-30 13:45:03 -0700865
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700866 page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
867 zone);
868 if (!page)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100869 continue;
870
871 /* Check the block is suitable for migration */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700872 if (!suitable_migration_target(page))
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100873 continue;
Linus Torvalds68e3e922012-06-03 20:05:57 -0700874
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700875 /* If isolation recently failed, do not retry */
876 if (!isolation_suitable(cc, page))
877 continue;
878
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700879 /* Found a block suitable for isolating free pages from. */
880 isolated = isolate_freepages_block(cc, &isolate_start_pfn,
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700881 block_end_pfn, freelist, false);
Mel Gormanf40d1e42012-10-08 16:32:36 -0700882 nr_freepages += isolated;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100883
884 /*
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700885 * Remember where the free scanner should restart next time,
886 * which is where isolate_freepages_block() left off.
887 * But if it scanned the whole pageblock, isolate_start_pfn
888 * now points at block_end_pfn, which is the start of the next
889 * pageblock.
890 * In that case we will however want to restart at the start
891 * of the previous pageblock.
892 */
893 cc->free_pfn = (isolate_start_pfn < block_end_pfn) ?
894 isolate_start_pfn :
895 block_start_pfn - pageblock_nr_pages;
896
897 /*
Vlastimil Babkabe976572014-06-04 16:10:41 -0700898 * isolate_freepages_block() might have aborted due to async
899 * compaction being contended
900 */
901 if (cc->contended)
902 break;
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100903 }
904
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100905 /* split_free_page does not map the pages */
906 map_pages(freelist);
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100907
Vlastimil Babka7ed695e2014-01-21 15:51:09 -0800908 /*
909 * If we crossed the migrate scanner, we want to keep it that way
910 * so that compact_finished() may detect this
911 */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700912 if (block_start_pfn < low_pfn)
Vlastimil Babkae9ade562014-06-04 16:08:34 -0700913 cc->free_pfn = cc->migrate_pfn;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700914
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100915 cc->nr_freepages = nr_freepages;
Mel Gorman748446b2010-05-24 14:32:27 -0700916}
917
918/*
919 * This is a migrate-callback that "allocates" freepages by taking pages
920 * from the isolated freelists in the block we are migrating to.
921 */
922static struct page *compaction_alloc(struct page *migratepage,
923 unsigned long data,
924 int **result)
925{
926 struct compact_control *cc = (struct compact_control *)data;
927 struct page *freepage;
928
Vlastimil Babkabe976572014-06-04 16:10:41 -0700929 /*
930 * Isolate free pages if necessary, and if we are not aborting due to
931 * contention.
932 */
Mel Gorman748446b2010-05-24 14:32:27 -0700933 if (list_empty(&cc->freepages)) {
Vlastimil Babkabe976572014-06-04 16:10:41 -0700934 if (!cc->contended)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700935 isolate_freepages(cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700936
937 if (list_empty(&cc->freepages))
938 return NULL;
939 }
940
941 freepage = list_entry(cc->freepages.next, struct page, lru);
942 list_del(&freepage->lru);
943 cc->nr_freepages--;
944
945 return freepage;
946}
947
948/*
David Rientjesd53aea32014-06-04 16:08:26 -0700949 * This is a migrate-callback that "frees" freepages back to the isolated
950 * freelist. All pages on the freelist are from the same zone, so there is no
951 * special handling needed for NUMA.
952 */
953static void compaction_free(struct page *page, unsigned long data)
954{
955 struct compact_control *cc = (struct compact_control *)data;
956
957 list_add(&page->lru, &cc->freepages);
958 cc->nr_freepages++;
959}
960
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100961/* possible outcome of isolate_migratepages */
962typedef enum {
963 ISOLATE_ABORT, /* Abort compaction now */
964 ISOLATE_NONE, /* No pages isolated, continue scanning */
965 ISOLATE_SUCCESS, /* Pages isolated, migrate */
966} isolate_migrate_t;
967
968/*
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700969 * Isolate all pages that can be migrated from the first suitable block,
970 * starting at the block pointed to by the migrate scanner pfn within
971 * compact_control.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100972 */
973static isolate_migrate_t isolate_migratepages(struct zone *zone,
974 struct compact_control *cc)
975{
976 unsigned long low_pfn, end_pfn;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700977 struct page *page;
978 const isolate_mode_t isolate_mode =
979 (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100980
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700981 /*
982 * Start at where we last stopped, or beginning of the zone as
983 * initialized by compact_zone()
984 */
985 low_pfn = cc->migrate_pfn;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100986
987 /* Only scan within a pageblock boundary */
Mel Gormana9aacbc2013-02-22 16:32:25 -0800988 end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100989
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700990 /*
991 * Iterate over whole pageblocks until we find the first suitable.
992 * Do not cross the free scanner.
993 */
994 for (; end_pfn <= cc->free_pfn;
995 low_pfn = end_pfn, end_pfn += pageblock_nr_pages) {
996
997 /*
998 * This can potentially iterate a massively long zone with
999 * many pageblocks unsuitable, so periodically check if we
1000 * need to schedule, or even abort async compaction.
1001 */
1002 if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
1003 && compact_should_abort(cc))
1004 break;
1005
Vlastimil Babka7d49d882014-10-09 15:27:11 -07001006 page = pageblock_pfn_to_page(low_pfn, end_pfn, zone);
1007 if (!page)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001008 continue;
1009
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001010 /* If isolation recently failed, do not retry */
1011 if (!isolation_suitable(cc, page))
1012 continue;
1013
1014 /*
1015 * For async compaction, also only scan in MOVABLE blocks.
1016 * Async compaction is optimistic to see if the minimum amount
1017 * of work satisfies the allocation.
1018 */
1019 if (cc->mode == MIGRATE_ASYNC &&
1020 !migrate_async_suitable(get_pageblock_migratetype(page)))
1021 continue;
1022
1023 /* Perform the isolation */
1024 low_pfn = isolate_migratepages_block(cc, low_pfn, end_pfn,
1025 isolate_mode);
1026
1027 if (!low_pfn || cc->contended)
1028 return ISOLATE_ABORT;
1029
1030 /*
1031 * Either we isolated something and proceed with migration. Or
1032 * we failed and compact_zone should decide if we should
1033 * continue or not.
1034 */
1035 break;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001036 }
1037
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001038 acct_isolated(zone, cc);
Vlastimil Babka1d5bfe12014-11-13 15:19:30 -08001039 /*
1040 * Record where migration scanner will be restarted. If we end up in
1041 * the same pageblock as the free scanner, make the scanners fully
1042 * meet so that compact_finished() terminates compaction.
1043 */
1044 cc->migrate_pfn = (end_pfn <= cc->free_pfn) ? low_pfn : cc->free_pfn;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001045
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001046 return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001047}
1048
David Rientjes6d7ce552014-10-09 15:27:27 -07001049static int compact_finished(struct zone *zone, struct compact_control *cc,
1050 const int migratetype)
Mel Gorman748446b2010-05-24 14:32:27 -07001051{
Mel Gorman8fb74b92013-01-11 14:32:16 -08001052 unsigned int order;
Andrea Arcangeli5a03b052011-01-13 15:47:11 -08001053 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -07001054
Vlastimil Babkabe976572014-06-04 16:10:41 -07001055 if (cc->contended || fatal_signal_pending(current))
Mel Gorman748446b2010-05-24 14:32:27 -07001056 return COMPACT_PARTIAL;
1057
Mel Gorman753341a2012-10-08 16:32:40 -07001058 /* Compaction run completes if the migrate and free scanner meet */
Mel Gormanbb13ffe2012-10-08 16:32:41 -07001059 if (cc->free_pfn <= cc->migrate_pfn) {
Vlastimil Babka55b7c4c2014-01-21 15:51:11 -08001060 /* Let the next compaction start anew. */
David Rientjes35979ef2014-06-04 16:08:27 -07001061 zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
1062 zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
Vlastimil Babka55b7c4c2014-01-21 15:51:11 -08001063 zone->compact_cached_free_pfn = zone_end_pfn(zone);
1064
Mel Gorman62997022012-10-08 16:32:47 -07001065 /*
1066 * Mark that the PG_migrate_skip information should be cleared
1067 * by kswapd when it goes to sleep. kswapd does not set the
1068 * flag itself as the decision to be clear should be directly
1069 * based on an allocation request.
1070 */
1071 if (!current_is_kswapd())
1072 zone->compact_blockskip_flush = true;
1073
Mel Gorman748446b2010-05-24 14:32:27 -07001074 return COMPACT_COMPLETE;
Mel Gormanbb13ffe2012-10-08 16:32:41 -07001075 }
Mel Gorman748446b2010-05-24 14:32:27 -07001076
Johannes Weiner82478fb2011-01-20 14:44:21 -08001077 /*
1078 * order == -1 is expected when compacting via
1079 * /proc/sys/vm/compact_memory
1080 */
Mel Gorman56de7262010-05-24 14:32:30 -07001081 if (cc->order == -1)
1082 return COMPACT_CONTINUE;
1083
Michal Hocko3957c772011-06-15 15:08:25 -07001084 /* Compaction run is not finished if the watermark is not met */
1085 watermark = low_wmark_pages(zone);
Michal Hocko3957c772011-06-15 15:08:25 -07001086
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001087 if (!zone_watermark_ok(zone, cc->order, watermark, cc->classzone_idx,
1088 cc->alloc_flags))
Michal Hocko3957c772011-06-15 15:08:25 -07001089 return COMPACT_CONTINUE;
1090
Mel Gorman56de7262010-05-24 14:32:30 -07001091 /* Direct compactor: Is a suitable page free? */
Mel Gorman8fb74b92013-01-11 14:32:16 -08001092 for (order = cc->order; order < MAX_ORDER; order++) {
1093 struct free_area *area = &zone->free_area[order];
Mel Gorman56de7262010-05-24 14:32:30 -07001094
Mel Gorman8fb74b92013-01-11 14:32:16 -08001095 /* Job done if page is free of the right migratetype */
David Rientjes6d7ce552014-10-09 15:27:27 -07001096 if (!list_empty(&area->free_list[migratetype]))
Mel Gorman8fb74b92013-01-11 14:32:16 -08001097 return COMPACT_PARTIAL;
1098
1099 /* Job done if allocation would set block type */
1100 if (cc->order >= pageblock_order && area->nr_free)
1101 return COMPACT_PARTIAL;
Mel Gorman56de7262010-05-24 14:32:30 -07001102 }
1103
Mel Gorman748446b2010-05-24 14:32:27 -07001104 return COMPACT_CONTINUE;
1105}
1106
Mel Gorman3e7d3442011-01-13 15:45:56 -08001107/*
1108 * compaction_suitable: Is this suitable to run compaction on this zone now?
1109 * Returns
1110 * COMPACT_SKIPPED - If there are too few free pages for compaction
1111 * COMPACT_PARTIAL - If the allocation would succeed without compaction
1112 * COMPACT_CONTINUE - If compaction should run now
1113 */
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001114unsigned long compaction_suitable(struct zone *zone, int order,
1115 int alloc_flags, int classzone_idx)
Mel Gorman3e7d3442011-01-13 15:45:56 -08001116{
1117 int fragindex;
1118 unsigned long watermark;
1119
1120 /*
Michal Hocko3957c772011-06-15 15:08:25 -07001121 * order == -1 is expected when compacting via
1122 * /proc/sys/vm/compact_memory
1123 */
1124 if (order == -1)
1125 return COMPACT_CONTINUE;
1126
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001127 watermark = low_wmark_pages(zone);
1128 /*
1129 * If watermarks for high-order allocation are already met, there
1130 * should be no need for compaction at all.
1131 */
1132 if (zone_watermark_ok(zone, order, watermark, classzone_idx,
1133 alloc_flags))
1134 return COMPACT_PARTIAL;
1135
Michal Hocko3957c772011-06-15 15:08:25 -07001136 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -08001137 * Watermarks for order-0 must be met for compaction. Note the 2UL.
1138 * This is because during migration, copies of pages need to be
1139 * allocated and for a short time, the footprint is higher
1140 */
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001141 watermark += (2UL << order);
1142 if (!zone_watermark_ok(zone, 0, watermark, classzone_idx, alloc_flags))
Mel Gorman3e7d3442011-01-13 15:45:56 -08001143 return COMPACT_SKIPPED;
1144
1145 /*
1146 * fragmentation index determines if allocation failures are due to
1147 * low memory or external fragmentation
1148 *
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001149 * index of -1000 would imply allocations might succeed depending on
1150 * watermarks, but we already failed the high-order watermark check
Mel Gorman3e7d3442011-01-13 15:45:56 -08001151 * index towards 0 implies failure is due to lack of memory
1152 * index towards 1000 implies failure is due to fragmentation
1153 *
1154 * Only compact if a failure would be due to fragmentation.
1155 */
1156 fragindex = fragmentation_index(zone, order);
1157 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
1158 return COMPACT_SKIPPED;
1159
Mel Gorman3e7d3442011-01-13 15:45:56 -08001160 return COMPACT_CONTINUE;
1161}
1162
Mel Gorman748446b2010-05-24 14:32:27 -07001163static int compact_zone(struct zone *zone, struct compact_control *cc)
1164{
1165 int ret;
Mel Gormanc89511a2012-10-08 16:32:45 -07001166 unsigned long start_pfn = zone->zone_start_pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -08001167 unsigned long end_pfn = zone_end_pfn(zone);
David Rientjes6d7ce552014-10-09 15:27:27 -07001168 const int migratetype = gfpflags_to_migratetype(cc->gfp_mask);
David Rientjese0b9dae2014-06-04 16:08:28 -07001169 const bool sync = cc->mode != MIGRATE_ASYNC;
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08001170 unsigned long last_migrated_pfn = 0;
Mel Gorman748446b2010-05-24 14:32:27 -07001171
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001172 ret = compaction_suitable(zone, cc->order, cc->alloc_flags,
1173 cc->classzone_idx);
Mel Gorman3e7d3442011-01-13 15:45:56 -08001174 switch (ret) {
1175 case COMPACT_PARTIAL:
1176 case COMPACT_SKIPPED:
1177 /* Compaction is likely to fail */
1178 return ret;
1179 case COMPACT_CONTINUE:
1180 /* Fall through to compaction */
1181 ;
1182 }
1183
Mel Gormanc89511a2012-10-08 16:32:45 -07001184 /*
Vlastimil Babkad3132e42014-01-21 15:51:08 -08001185 * Clear pageblock skip if there were failures recently and compaction
1186 * is about to be retried after being deferred. kswapd does not do
1187 * this reset as it'll reset the cached information when going to sleep.
1188 */
1189 if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
1190 __reset_isolation_suitable(zone);
1191
1192 /*
Mel Gormanc89511a2012-10-08 16:32:45 -07001193 * Setup to move all movable pages to the end of the zone. Used cached
1194 * information on where the scanners should start but check that it
1195 * is initialised by ensuring the values are within zone boundaries.
1196 */
David Rientjese0b9dae2014-06-04 16:08:28 -07001197 cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync];
Mel Gormanc89511a2012-10-08 16:32:45 -07001198 cc->free_pfn = zone->compact_cached_free_pfn;
1199 if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
1200 cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
1201 zone->compact_cached_free_pfn = cc->free_pfn;
1202 }
1203 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
1204 cc->migrate_pfn = start_pfn;
David Rientjes35979ef2014-06-04 16:08:27 -07001205 zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
1206 zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
Mel Gormanc89511a2012-10-08 16:32:45 -07001207 }
Mel Gorman748446b2010-05-24 14:32:27 -07001208
Joonsoo Kim16c4a092015-02-11 15:27:01 -08001209 trace_mm_compaction_begin(start_pfn, cc->migrate_pfn,
1210 cc->free_pfn, end_pfn, sync);
Mel Gorman0eb927c2014-01-21 15:51:05 -08001211
Mel Gorman748446b2010-05-24 14:32:27 -07001212 migrate_prep_local();
1213
David Rientjes6d7ce552014-10-09 15:27:27 -07001214 while ((ret = compact_finished(zone, cc, migratetype)) ==
1215 COMPACT_CONTINUE) {
Minchan Kim9d502c12011-03-22 16:30:39 -07001216 int err;
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08001217 unsigned long isolate_start_pfn = cc->migrate_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -07001218
Mel Gormanf9e35b32011-06-15 15:08:52 -07001219 switch (isolate_migratepages(zone, cc)) {
1220 case ISOLATE_ABORT:
1221 ret = COMPACT_PARTIAL;
Rafael Aquini5733c7d2012-12-11 16:02:47 -08001222 putback_movable_pages(&cc->migratepages);
Shaohua Lie64c5232012-10-08 16:32:27 -07001223 cc->nr_migratepages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -07001224 goto out;
1225 case ISOLATE_NONE:
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08001226 /*
1227 * We haven't isolated and migrated anything, but
1228 * there might still be unflushed migrations from
1229 * previous cc->order aligned block.
1230 */
1231 goto check_drain;
Mel Gormanf9e35b32011-06-15 15:08:52 -07001232 case ISOLATE_SUCCESS:
1233 ;
1234 }
Mel Gorman748446b2010-05-24 14:32:27 -07001235
David Rientjesd53aea32014-06-04 16:08:26 -07001236 err = migrate_pages(&cc->migratepages, compaction_alloc,
David Rientjese0b9dae2014-06-04 16:08:28 -07001237 compaction_free, (unsigned long)cc, cc->mode,
Mel Gorman7b2a2d42012-10-19 14:07:31 +01001238 MR_COMPACTION);
Mel Gorman748446b2010-05-24 14:32:27 -07001239
Vlastimil Babkaf8c93012014-06-04 16:08:32 -07001240 trace_mm_compaction_migratepages(cc->nr_migratepages, err,
1241 &cc->migratepages);
Mel Gorman748446b2010-05-24 14:32:27 -07001242
Vlastimil Babkaf8c93012014-06-04 16:08:32 -07001243 /* All pages were either migrated or will be released */
1244 cc->nr_migratepages = 0;
Minchan Kim9d502c12011-03-22 16:30:39 -07001245 if (err) {
Rafael Aquini5733c7d2012-12-11 16:02:47 -08001246 putback_movable_pages(&cc->migratepages);
Vlastimil Babka7ed695e2014-01-21 15:51:09 -08001247 /*
1248 * migrate_pages() may return -ENOMEM when scanners meet
1249 * and we want compact_finished() to detect it
1250 */
1251 if (err == -ENOMEM && cc->free_pfn > cc->migrate_pfn) {
David Rientjes4bf2bba2012-07-11 14:02:13 -07001252 ret = COMPACT_PARTIAL;
1253 goto out;
1254 }
Mel Gorman748446b2010-05-24 14:32:27 -07001255 }
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08001256
1257 /*
1258 * Record where we could have freed pages by migration and not
1259 * yet flushed them to buddy allocator. We use the pfn that
1260 * isolate_migratepages() started from in this loop iteration
1261 * - this is the lowest page that could have been isolated and
1262 * then freed by migration.
1263 */
1264 if (!last_migrated_pfn)
1265 last_migrated_pfn = isolate_start_pfn;
1266
1267check_drain:
1268 /*
1269 * Has the migration scanner moved away from the previous
1270 * cc->order aligned block where we migrated from? If yes,
1271 * flush the pages that were freed, so that they can merge and
1272 * compact_finished() can detect immediately if allocation
1273 * would succeed.
1274 */
1275 if (cc->order > 0 && last_migrated_pfn) {
1276 int cpu;
1277 unsigned long current_block_start =
1278 cc->migrate_pfn & ~((1UL << cc->order) - 1);
1279
1280 if (last_migrated_pfn < current_block_start) {
1281 cpu = get_cpu();
1282 lru_add_drain_cpu(cpu);
1283 drain_local_pages(zone);
1284 put_cpu();
1285 /* No more flushing until we migrate again */
1286 last_migrated_pfn = 0;
1287 }
1288 }
1289
Mel Gorman748446b2010-05-24 14:32:27 -07001290 }
1291
Mel Gormanf9e35b32011-06-15 15:08:52 -07001292out:
Vlastimil Babka6bace092014-12-10 15:43:31 -08001293 /*
1294 * Release free pages and update where the free scanner should restart,
1295 * so we don't leave any returned pages behind in the next attempt.
1296 */
1297 if (cc->nr_freepages > 0) {
1298 unsigned long free_pfn = release_freepages(&cc->freepages);
1299
1300 cc->nr_freepages = 0;
1301 VM_BUG_ON(free_pfn == 0);
1302 /* The cached pfn is always the first in a pageblock */
1303 free_pfn &= ~(pageblock_nr_pages-1);
1304 /*
1305 * Only go back, not forward. The cached pfn might have been
1306 * already reset to zone end in compact_finished()
1307 */
1308 if (free_pfn > zone->compact_cached_free_pfn)
1309 zone->compact_cached_free_pfn = free_pfn;
1310 }
Mel Gorman748446b2010-05-24 14:32:27 -07001311
Joonsoo Kim16c4a092015-02-11 15:27:01 -08001312 trace_mm_compaction_end(start_pfn, cc->migrate_pfn,
1313 cc->free_pfn, end_pfn, sync, ret);
Mel Gorman0eb927c2014-01-21 15:51:05 -08001314
Mel Gorman748446b2010-05-24 14:32:27 -07001315 return ret;
1316}
Mel Gorman76ab0f52010-05-24 14:32:28 -07001317
David Rientjese0b9dae2014-06-04 16:08:28 -07001318static unsigned long compact_zone_order(struct zone *zone, int order,
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001319 gfp_t gfp_mask, enum migrate_mode mode, int *contended,
1320 int alloc_flags, int classzone_idx)
Mel Gorman56de7262010-05-24 14:32:30 -07001321{
Shaohua Lie64c5232012-10-08 16:32:27 -07001322 unsigned long ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001323 struct compact_control cc = {
1324 .nr_freepages = 0,
1325 .nr_migratepages = 0,
1326 .order = order,
David Rientjes6d7ce552014-10-09 15:27:27 -07001327 .gfp_mask = gfp_mask,
Mel Gorman56de7262010-05-24 14:32:30 -07001328 .zone = zone,
David Rientjese0b9dae2014-06-04 16:08:28 -07001329 .mode = mode,
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001330 .alloc_flags = alloc_flags,
1331 .classzone_idx = classzone_idx,
Mel Gorman56de7262010-05-24 14:32:30 -07001332 };
1333 INIT_LIST_HEAD(&cc.freepages);
1334 INIT_LIST_HEAD(&cc.migratepages);
1335
Shaohua Lie64c5232012-10-08 16:32:27 -07001336 ret = compact_zone(zone, &cc);
1337
1338 VM_BUG_ON(!list_empty(&cc.freepages));
1339 VM_BUG_ON(!list_empty(&cc.migratepages));
1340
1341 *contended = cc.contended;
1342 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001343}
1344
Mel Gorman5e771902010-05-24 14:32:31 -07001345int sysctl_extfrag_threshold = 500;
1346
Mel Gorman56de7262010-05-24 14:32:30 -07001347/**
1348 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
Mel Gorman56de7262010-05-24 14:32:30 -07001349 * @gfp_mask: The GFP mask of the current allocation
Vlastimil Babka1a6d53a2015-02-11 15:25:44 -08001350 * @order: The order of the current allocation
1351 * @alloc_flags: The allocation flags of the current allocation
1352 * @ac: The context of current allocation
David Rientjese0b9dae2014-06-04 16:08:28 -07001353 * @mode: The migration mode for async, sync light, or sync migration
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001354 * @contended: Return value that determines if compaction was aborted due to
1355 * need_resched() or lock contention
Mel Gorman56de7262010-05-24 14:32:30 -07001356 *
1357 * This is the main entry point for direct page compaction.
1358 */
Vlastimil Babka1a6d53a2015-02-11 15:25:44 -08001359unsigned long try_to_compact_pages(gfp_t gfp_mask, unsigned int order,
1360 int alloc_flags, const struct alloc_context *ac,
1361 enum migrate_mode mode, int *contended)
Mel Gorman56de7262010-05-24 14:32:30 -07001362{
Mel Gorman56de7262010-05-24 14:32:30 -07001363 int may_enter_fs = gfp_mask & __GFP_FS;
1364 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -07001365 struct zoneref *z;
1366 struct zone *zone;
Vlastimil Babka53853e22014-10-09 15:27:02 -07001367 int rc = COMPACT_DEFERRED;
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001368 int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */
1369
1370 *contended = COMPACT_CONTENDED_NONE;
Mel Gorman56de7262010-05-24 14:32:30 -07001371
Mel Gorman4ffb6332012-10-08 16:29:09 -07001372 /* Check if the GFP flags allow compaction */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -08001373 if (!order || !may_enter_fs || !may_perform_io)
Vlastimil Babka53853e22014-10-09 15:27:02 -07001374 return COMPACT_SKIPPED;
Mel Gorman56de7262010-05-24 14:32:30 -07001375
1376 /* Compact each zone in the list */
Vlastimil Babka1a6d53a2015-02-11 15:25:44 -08001377 for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
1378 ac->nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -07001379 int status;
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001380 int zone_contended;
Mel Gorman56de7262010-05-24 14:32:30 -07001381
Vlastimil Babka53853e22014-10-09 15:27:02 -07001382 if (compaction_deferred(zone, order))
1383 continue;
1384
David Rientjese0b9dae2014-06-04 16:08:28 -07001385 status = compact_zone_order(zone, order, gfp_mask, mode,
Vlastimil Babka1a6d53a2015-02-11 15:25:44 -08001386 &zone_contended, alloc_flags,
1387 ac->classzone_idx);
Mel Gorman56de7262010-05-24 14:32:30 -07001388 rc = max(status, rc);
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001389 /*
1390 * It takes at least one zone that wasn't lock contended
1391 * to clear all_zones_contended.
1392 */
1393 all_zones_contended &= zone_contended;
Mel Gorman56de7262010-05-24 14:32:30 -07001394
Mel Gorman3e7d3442011-01-13 15:45:56 -08001395 /* If a normal allocation would succeed, stop compacting */
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001396 if (zone_watermark_ok(zone, order, low_wmark_pages(zone),
Vlastimil Babka1a6d53a2015-02-11 15:25:44 -08001397 ac->classzone_idx, alloc_flags)) {
Vlastimil Babka53853e22014-10-09 15:27:02 -07001398 /*
1399 * We think the allocation will succeed in this zone,
1400 * but it is not certain, hence the false. The caller
1401 * will repeat this with true if allocation indeed
1402 * succeeds in this zone.
1403 */
1404 compaction_defer_reset(zone, order, false);
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001405 /*
1406 * It is possible that async compaction aborted due to
1407 * need_resched() and the watermarks were ok thanks to
1408 * somebody else freeing memory. The allocation can
1409 * however still fail so we better signal the
1410 * need_resched() contention anyway (this will not
1411 * prevent the allocation attempt).
1412 */
1413 if (zone_contended == COMPACT_CONTENDED_SCHED)
1414 *contended = COMPACT_CONTENDED_SCHED;
1415
1416 goto break_loop;
1417 }
1418
Vlastimil Babkaf8669792014-12-10 15:43:28 -08001419 if (mode != MIGRATE_ASYNC && status == COMPACT_COMPLETE) {
Vlastimil Babka53853e22014-10-09 15:27:02 -07001420 /*
1421 * We think that allocation won't succeed in this zone
1422 * so we defer compaction there. If it ends up
1423 * succeeding after all, it will be reset.
1424 */
1425 defer_compaction(zone, order);
1426 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001427
1428 /*
1429 * We might have stopped compacting due to need_resched() in
1430 * async compaction, or due to a fatal signal detected. In that
1431 * case do not try further zones and signal need_resched()
1432 * contention.
1433 */
1434 if ((zone_contended == COMPACT_CONTENDED_SCHED)
1435 || fatal_signal_pending(current)) {
1436 *contended = COMPACT_CONTENDED_SCHED;
1437 goto break_loop;
1438 }
1439
1440 continue;
1441break_loop:
1442 /*
1443 * We might not have tried all the zones, so be conservative
1444 * and assume they are not all lock contended.
1445 */
1446 all_zones_contended = 0;
1447 break;
Mel Gorman56de7262010-05-24 14:32:30 -07001448 }
1449
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001450 /*
1451 * If at least one zone wasn't deferred or skipped, we report if all
1452 * zones that were tried were lock contended.
1453 */
1454 if (rc > COMPACT_SKIPPED && all_zones_contended)
1455 *contended = COMPACT_CONTENDED_LOCK;
1456
Mel Gorman56de7262010-05-24 14:32:30 -07001457 return rc;
1458}
1459
1460
Mel Gorman76ab0f52010-05-24 14:32:28 -07001461/* Compact all zones within a node */
Andrew Morton7103f162013-02-22 16:32:33 -08001462static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001463{
1464 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -07001465 struct zone *zone;
1466
Mel Gorman76ab0f52010-05-24 14:32:28 -07001467 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -07001468
1469 zone = &pgdat->node_zones[zoneid];
1470 if (!populated_zone(zone))
1471 continue;
1472
Rik van Riel7be62de2012-03-21 16:33:52 -07001473 cc->nr_freepages = 0;
1474 cc->nr_migratepages = 0;
1475 cc->zone = zone;
1476 INIT_LIST_HEAD(&cc->freepages);
1477 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001478
Dan Carpenteraad6ec32012-03-21 16:33:54 -07001479 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -07001480 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001481
Rik van Rielaff62242012-03-21 16:33:52 -07001482 if (cc->order > 0) {
Vlastimil Babkade6c60a2014-01-21 15:51:07 -08001483 if (zone_watermark_ok(zone, cc->order,
1484 low_wmark_pages(zone), 0, 0))
1485 compaction_defer_reset(zone, cc->order, false);
Rik van Rielaff62242012-03-21 16:33:52 -07001486 }
1487
Rik van Riel7be62de2012-03-21 16:33:52 -07001488 VM_BUG_ON(!list_empty(&cc->freepages));
1489 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -07001490 }
Mel Gorman76ab0f52010-05-24 14:32:28 -07001491}
1492
Andrew Morton7103f162013-02-22 16:32:33 -08001493void compact_pgdat(pg_data_t *pgdat, int order)
Rik van Riel7be62de2012-03-21 16:33:52 -07001494{
1495 struct compact_control cc = {
1496 .order = order,
David Rientjese0b9dae2014-06-04 16:08:28 -07001497 .mode = MIGRATE_ASYNC,
Rik van Riel7be62de2012-03-21 16:33:52 -07001498 };
1499
Mel Gorman3a7200a2013-09-11 14:22:19 -07001500 if (!order)
1501 return;
1502
Andrew Morton7103f162013-02-22 16:32:33 -08001503 __compact_pgdat(pgdat, &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001504}
1505
Andrew Morton7103f162013-02-22 16:32:33 -08001506static void compact_node(int nid)
Rik van Riel7be62de2012-03-21 16:33:52 -07001507{
Rik van Riel7be62de2012-03-21 16:33:52 -07001508 struct compact_control cc = {
1509 .order = -1,
David Rientjese0b9dae2014-06-04 16:08:28 -07001510 .mode = MIGRATE_SYNC,
David Rientjes91ca9182014-04-03 14:47:23 -07001511 .ignore_skip_hint = true,
Rik van Riel7be62de2012-03-21 16:33:52 -07001512 };
1513
Andrew Morton7103f162013-02-22 16:32:33 -08001514 __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001515}
1516
Mel Gorman76ab0f52010-05-24 14:32:28 -07001517/* Compact all nodes in the system */
Jason Liu7964c062013-01-11 14:31:47 -08001518static void compact_nodes(void)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001519{
1520 int nid;
1521
Hugh Dickins8575ec22012-03-21 16:33:53 -07001522 /* Flush pending updates to the LRU lists */
1523 lru_add_drain_all();
1524
Mel Gorman76ab0f52010-05-24 14:32:28 -07001525 for_each_online_node(nid)
1526 compact_node(nid);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001527}
1528
1529/* The written value is actually unused, all memory is compacted */
1530int sysctl_compact_memory;
1531
1532/* This is the entry point for compacting all nodes via /proc/sys/vm */
1533int sysctl_compaction_handler(struct ctl_table *table, int write,
1534 void __user *buffer, size_t *length, loff_t *ppos)
1535{
1536 if (write)
Jason Liu7964c062013-01-11 14:31:47 -08001537 compact_nodes();
Mel Gorman76ab0f52010-05-24 14:32:28 -07001538
1539 return 0;
1540}
Mel Gormaned4a6d72010-05-24 14:32:29 -07001541
Mel Gorman5e771902010-05-24 14:32:31 -07001542int sysctl_extfrag_handler(struct ctl_table *table, int write,
1543 void __user *buffer, size_t *length, loff_t *ppos)
1544{
1545 proc_dointvec_minmax(table, write, buffer, length, ppos);
1546
1547 return 0;
1548}
1549
Mel Gormaned4a6d72010-05-24 14:32:29 -07001550#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Rashika Kheria74e77fb2014-04-03 14:48:01 -07001551static ssize_t sysfs_compact_node(struct device *dev,
Kay Sievers10fbcf42011-12-21 14:48:43 -08001552 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -07001553 const char *buf, size_t count)
1554{
Hugh Dickins8575ec22012-03-21 16:33:53 -07001555 int nid = dev->id;
1556
1557 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1558 /* Flush pending updates to the LRU lists */
1559 lru_add_drain_all();
1560
1561 compact_node(nid);
1562 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07001563
1564 return count;
1565}
Kay Sievers10fbcf42011-12-21 14:48:43 -08001566static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001567
1568int compaction_register_node(struct node *node)
1569{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001570 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001571}
1572
1573void compaction_unregister_node(struct node *node)
1574{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001575 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001576}
1577#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001578
1579#endif /* CONFIG_COMPACTION */