blob: 347e998dbd162cac4bfcaf927c432188e5ff780d [file] [log] [blame]
Mel Gorman748446b2010-05-24 14:32:27 -07001/*
2 * linux/mm/compaction.c
3 *
4 * Memory compaction for the reduction of external fragmentation. Note that
5 * this heavily depends upon page migration to do all the real heavy
6 * lifting
7 *
8 * Copyright IBM Corp. 2007-2010 Mel Gorman <mel@csn.ul.ie>
9 */
10#include <linux/swap.h>
11#include <linux/migrate.h>
12#include <linux/compaction.h>
13#include <linux/mm_inline.h>
14#include <linux/backing-dev.h>
Mel Gorman76ab0f52010-05-24 14:32:28 -070015#include <linux/sysctl.h>
Mel Gormaned4a6d72010-05-24 14:32:29 -070016#include <linux/sysfs.h>
Rafael Aquinibf6bddf2012-12-11 16:02:42 -080017#include <linux/balloon_compaction.h>
Minchan Kim194159f2013-02-22 16:33:58 -080018#include <linux/page-isolation.h>
Mel Gorman748446b2010-05-24 14:32:27 -070019#include "internal.h"
20
Minchan Kim010fc292012-12-20 15:05:06 -080021#ifdef CONFIG_COMPACTION
22static inline void count_compact_event(enum vm_event_item item)
23{
24 count_vm_event(item);
25}
26
27static inline void count_compact_events(enum vm_event_item item, long delta)
28{
29 count_vm_events(item, delta);
30}
31#else
32#define count_compact_event(item) do { } while (0)
33#define count_compact_events(item, delta) do { } while (0)
34#endif
35
Michal Nazarewiczff9543f2011-12-29 13:09:50 +010036#if defined CONFIG_COMPACTION || defined CONFIG_CMA
37
Mel Gormanb7aba692011-01-13 15:45:54 -080038#define CREATE_TRACE_POINTS
39#include <trace/events/compaction.h>
40
Mel Gorman748446b2010-05-24 14:32:27 -070041static unsigned long release_freepages(struct list_head *freelist)
42{
43 struct page *page, *next;
44 unsigned long count = 0;
45
46 list_for_each_entry_safe(page, next, freelist, lru) {
47 list_del(&page->lru);
48 __free_page(page);
49 count++;
50 }
51
52 return count;
53}
54
Michal Nazarewiczff9543f2011-12-29 13:09:50 +010055static void map_pages(struct list_head *list)
56{
57 struct page *page;
58
59 list_for_each_entry(page, list, lru) {
60 arch_alloc_page(page, 0);
61 kernel_map_pages(page, 1, 1);
62 }
63}
64
Michal Nazarewicz47118af2011-12-29 13:09:50 +010065static inline bool migrate_async_suitable(int migratetype)
66{
67 return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
68}
69
Vlastimil Babka7d49d882014-10-09 15:27:11 -070070/*
71 * Check that the whole (or subset of) a pageblock given by the interval of
72 * [start_pfn, end_pfn) is valid and within the same zone, before scanning it
73 * with the migration of free compaction scanner. The scanners then need to
74 * use only pfn_valid_within() check for arches that allow holes within
75 * pageblocks.
76 *
77 * Return struct page pointer of start_pfn, or NULL if checks were not passed.
78 *
79 * It's possible on some configurations to have a setup like node0 node1 node0
80 * i.e. it's possible that all pages within a zones range of pages do not
81 * belong to a single zone. We assume that a border between node0 and node1
82 * can occur within a single pageblock, but not a node0 node1 node0
83 * interleaving within a single pageblock. It is therefore sufficient to check
84 * the first and last page of a pageblock and avoid checking each individual
85 * page in a pageblock.
86 */
87static struct page *pageblock_pfn_to_page(unsigned long start_pfn,
88 unsigned long end_pfn, struct zone *zone)
89{
90 struct page *start_page;
91 struct page *end_page;
92
93 /* end_pfn is one past the range we are checking */
94 end_pfn--;
95
96 if (!pfn_valid(start_pfn) || !pfn_valid(end_pfn))
97 return NULL;
98
99 start_page = pfn_to_page(start_pfn);
100
101 if (page_zone(start_page) != zone)
102 return NULL;
103
104 end_page = pfn_to_page(end_pfn);
105
106 /* This gives a shorter code than deriving page_zone(end_page) */
107 if (page_zone_id(start_page) != page_zone_id(end_page))
108 return NULL;
109
110 return start_page;
111}
112
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700113#ifdef CONFIG_COMPACTION
114/* Returns true if the pageblock should be scanned for pages to isolate. */
115static inline bool isolation_suitable(struct compact_control *cc,
116 struct page *page)
117{
118 if (cc->ignore_skip_hint)
119 return true;
120
121 return !get_pageblock_skip(page);
122}
123
124/*
125 * This function is called to clear all cached information on pageblocks that
126 * should be skipped for page isolation when the migrate and free page scanner
127 * meet.
128 */
Mel Gorman62997022012-10-08 16:32:47 -0700129static void __reset_isolation_suitable(struct zone *zone)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700130{
131 unsigned long start_pfn = zone->zone_start_pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -0800132 unsigned long end_pfn = zone_end_pfn(zone);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700133 unsigned long pfn;
134
David Rientjes35979ef2014-06-04 16:08:27 -0700135 zone->compact_cached_migrate_pfn[0] = start_pfn;
136 zone->compact_cached_migrate_pfn[1] = start_pfn;
Mel Gormanc89511a2012-10-08 16:32:45 -0700137 zone->compact_cached_free_pfn = end_pfn;
Mel Gorman62997022012-10-08 16:32:47 -0700138 zone->compact_blockskip_flush = false;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700139
140 /* Walk the zone and mark every pageblock as suitable for isolation */
141 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
142 struct page *page;
143
144 cond_resched();
145
146 if (!pfn_valid(pfn))
147 continue;
148
149 page = pfn_to_page(pfn);
150 if (zone != page_zone(page))
151 continue;
152
153 clear_pageblock_skip(page);
154 }
155}
156
Mel Gorman62997022012-10-08 16:32:47 -0700157void reset_isolation_suitable(pg_data_t *pgdat)
158{
159 int zoneid;
160
161 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
162 struct zone *zone = &pgdat->node_zones[zoneid];
163 if (!populated_zone(zone))
164 continue;
165
166 /* Only flush if a full compaction finished recently */
167 if (zone->compact_blockskip_flush)
168 __reset_isolation_suitable(zone);
169 }
170}
171
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700172/*
173 * If no pages were isolated then mark this pageblock to be skipped in the
Mel Gorman62997022012-10-08 16:32:47 -0700174 * future. The information is later cleared by __reset_isolation_suitable().
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700175 */
Mel Gormanc89511a2012-10-08 16:32:45 -0700176static void update_pageblock_skip(struct compact_control *cc,
177 struct page *page, unsigned long nr_isolated,
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700178 bool migrate_scanner)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700179{
Mel Gormanc89511a2012-10-08 16:32:45 -0700180 struct zone *zone = cc->zone;
David Rientjes35979ef2014-06-04 16:08:27 -0700181 unsigned long pfn;
Joonsoo Kim6815bf32013-12-18 17:08:52 -0800182
183 if (cc->ignore_skip_hint)
184 return;
185
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700186 if (!page)
187 return;
188
David Rientjes35979ef2014-06-04 16:08:27 -0700189 if (nr_isolated)
190 return;
191
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700192 set_pageblock_skip(page);
Mel Gormanc89511a2012-10-08 16:32:45 -0700193
David Rientjes35979ef2014-06-04 16:08:27 -0700194 pfn = page_to_pfn(page);
195
196 /* Update where async and sync compaction should restart */
197 if (migrate_scanner) {
198 if (cc->finished_update_migrate)
199 return;
200 if (pfn > zone->compact_cached_migrate_pfn[0])
201 zone->compact_cached_migrate_pfn[0] = pfn;
David Rientjese0b9dae2014-06-04 16:08:28 -0700202 if (cc->mode != MIGRATE_ASYNC &&
203 pfn > zone->compact_cached_migrate_pfn[1])
David Rientjes35979ef2014-06-04 16:08:27 -0700204 zone->compact_cached_migrate_pfn[1] = pfn;
205 } else {
206 if (cc->finished_update_free)
207 return;
208 if (pfn < zone->compact_cached_free_pfn)
209 zone->compact_cached_free_pfn = pfn;
Mel Gormanc89511a2012-10-08 16:32:45 -0700210 }
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700211}
212#else
213static inline bool isolation_suitable(struct compact_control *cc,
214 struct page *page)
215{
216 return true;
217}
218
Mel Gormanc89511a2012-10-08 16:32:45 -0700219static void update_pageblock_skip(struct compact_control *cc,
220 struct page *page, unsigned long nr_isolated,
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700221 bool migrate_scanner)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700222{
223}
224#endif /* CONFIG_COMPACTION */
225
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700226/*
227 * Compaction requires the taking of some coarse locks that are potentially
228 * very heavily contended. For async compaction, back out if the lock cannot
229 * be taken immediately. For sync compaction, spin on the lock if needed.
230 *
231 * Returns true if the lock is held
232 * Returns false if the lock is not held and compaction should abort
233 */
234static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags,
235 struct compact_control *cc)
Mel Gorman2a1402a2012-10-08 16:32:33 -0700236{
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700237 if (cc->mode == MIGRATE_ASYNC) {
238 if (!spin_trylock_irqsave(lock, *flags)) {
239 cc->contended = COMPACT_CONTENDED_LOCK;
240 return false;
241 }
242 } else {
243 spin_lock_irqsave(lock, *flags);
244 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700245
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700246 return true;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700247}
248
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100249/*
Mel Gormanc67fe372012-08-21 16:16:17 -0700250 * Compaction requires the taking of some coarse locks that are potentially
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700251 * very heavily contended. The lock should be periodically unlocked to avoid
252 * having disabled IRQs for a long time, even when there is nobody waiting on
253 * the lock. It might also be that allowing the IRQs will result in
254 * need_resched() becoming true. If scheduling is needed, async compaction
255 * aborts. Sync compaction schedules.
256 * Either compaction type will also abort if a fatal signal is pending.
257 * In either case if the lock was locked, it is dropped and not regained.
Mel Gormanc67fe372012-08-21 16:16:17 -0700258 *
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700259 * Returns true if compaction should abort due to fatal signal pending, or
260 * async compaction due to need_resched()
261 * Returns false when compaction can continue (sync compaction might have
262 * scheduled)
Mel Gormanc67fe372012-08-21 16:16:17 -0700263 */
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700264static bool compact_unlock_should_abort(spinlock_t *lock,
265 unsigned long flags, bool *locked, struct compact_control *cc)
Mel Gormanc67fe372012-08-21 16:16:17 -0700266{
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700267 if (*locked) {
268 spin_unlock_irqrestore(lock, flags);
269 *locked = false;
270 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700271
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700272 if (fatal_signal_pending(current)) {
273 cc->contended = COMPACT_CONTENDED_SCHED;
274 return true;
275 }
Mel Gormanc67fe372012-08-21 16:16:17 -0700276
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700277 if (need_resched()) {
David Rientjese0b9dae2014-06-04 16:08:28 -0700278 if (cc->mode == MIGRATE_ASYNC) {
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700279 cc->contended = COMPACT_CONTENDED_SCHED;
280 return true;
Mel Gormanc67fe372012-08-21 16:16:17 -0700281 }
Mel Gormanc67fe372012-08-21 16:16:17 -0700282 cond_resched();
Mel Gormanc67fe372012-08-21 16:16:17 -0700283 }
284
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700285 return false;
Mel Gormanc67fe372012-08-21 16:16:17 -0700286}
287
Vlastimil Babkabe976572014-06-04 16:10:41 -0700288/*
289 * Aside from avoiding lock contention, compaction also periodically checks
290 * need_resched() and either schedules in sync compaction or aborts async
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700291 * compaction. This is similar to what compact_unlock_should_abort() does, but
Vlastimil Babkabe976572014-06-04 16:10:41 -0700292 * is used where no lock is concerned.
293 *
294 * Returns false when no scheduling was needed, or sync compaction scheduled.
295 * Returns true when async compaction should abort.
296 */
297static inline bool compact_should_abort(struct compact_control *cc)
298{
299 /* async compaction aborts if contended */
300 if (need_resched()) {
301 if (cc->mode == MIGRATE_ASYNC) {
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700302 cc->contended = COMPACT_CONTENDED_SCHED;
Vlastimil Babkabe976572014-06-04 16:10:41 -0700303 return true;
304 }
305
306 cond_resched();
307 }
308
309 return false;
310}
311
Mel Gormanf40d1e42012-10-08 16:32:36 -0700312/* Returns true if the page is within a block suitable for migration to */
313static bool suitable_migration_target(struct page *page)
314{
Joonsoo Kim7d348b92014-04-07 15:37:03 -0700315 /* If the page is a large free page, then disallow migration */
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700316 if (PageBuddy(page)) {
317 /*
318 * We are checking page_order without zone->lock taken. But
319 * the only small danger is that we skip a potentially suitable
320 * pageblock, so it's not worth to check order for valid range.
321 */
322 if (page_order_unsafe(page) >= pageblock_order)
323 return false;
324 }
Mel Gormanf40d1e42012-10-08 16:32:36 -0700325
326 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
Joonsoo Kim7d348b92014-04-07 15:37:03 -0700327 if (migrate_async_suitable(get_pageblock_migratetype(page)))
Mel Gormanf40d1e42012-10-08 16:32:36 -0700328 return true;
329
330 /* Otherwise skip the block */
331 return false;
332}
333
Mel Gormanc67fe372012-08-21 16:16:17 -0700334/*
Jerome Marchand9e4be472013-11-12 15:07:12 -0800335 * Isolate free pages onto a private freelist. If @strict is true, will abort
336 * returning 0 on any invalid PFNs or non-free pages inside of the pageblock
337 * (even though it may still end up isolating some pages).
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100338 */
Mel Gormanf40d1e42012-10-08 16:32:36 -0700339static unsigned long isolate_freepages_block(struct compact_control *cc,
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700340 unsigned long *start_pfn,
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100341 unsigned long end_pfn,
342 struct list_head *freelist,
343 bool strict)
Mel Gorman748446b2010-05-24 14:32:27 -0700344{
Mel Gormanb7aba692011-01-13 15:45:54 -0800345 int nr_scanned = 0, total_isolated = 0;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700346 struct page *cursor, *valid_page = NULL;
Xiubo Lib8b2d822014-10-09 15:28:21 -0700347 unsigned long flags = 0;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700348 bool locked = false;
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700349 unsigned long blockpfn = *start_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700350
Mel Gorman748446b2010-05-24 14:32:27 -0700351 cursor = pfn_to_page(blockpfn);
352
Mel Gormanf40d1e42012-10-08 16:32:36 -0700353 /* Isolate free pages. */
Mel Gorman748446b2010-05-24 14:32:27 -0700354 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
355 int isolated, i;
356 struct page *page = cursor;
357
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700358 /*
359 * Periodically drop the lock (if held) regardless of its
360 * contention, to give chance to IRQs. Abort if fatal signal
361 * pending or async compaction detects need_resched()
362 */
363 if (!(blockpfn % SWAP_CLUSTER_MAX)
364 && compact_unlock_should_abort(&cc->zone->lock, flags,
365 &locked, cc))
366 break;
367
Mel Gormanb7aba692011-01-13 15:45:54 -0800368 nr_scanned++;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700369 if (!pfn_valid_within(blockpfn))
Laura Abbott2af120b2014-03-10 15:49:44 -0700370 goto isolate_fail;
371
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700372 if (!valid_page)
373 valid_page = page;
Vlastimil Babkaf1f702e2015-09-08 15:02:49 -0700374
375 /*
376 * For compound pages such as THP and hugetlbfs, we can save
377 * potentially a lot of iterations if we skip them at once.
378 * The check is racy, but we can consider only valid values
379 * and the only danger is skipping too much.
380 */
381 if (PageCompound(page)) {
382 unsigned int comp_order = compound_order(page);
383
384 if (likely(comp_order < MAX_ORDER)) {
385 blockpfn += (1UL << comp_order) - 1;
386 cursor += (1UL << comp_order) - 1;
387 }
388
389 goto isolate_fail;
390 }
391
Mel Gormanf40d1e42012-10-08 16:32:36 -0700392 if (!PageBuddy(page))
Laura Abbott2af120b2014-03-10 15:49:44 -0700393 goto isolate_fail;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700394
395 /*
Vlastimil Babka69b71892014-10-09 15:27:18 -0700396 * If we already hold the lock, we can skip some rechecking.
397 * Note that if we hold the lock now, checked_pageblock was
398 * already set in some previous iteration (or strict is true),
399 * so it is correct to skip the suitable migration target
400 * recheck as well.
Mel Gormanf40d1e42012-10-08 16:32:36 -0700401 */
Vlastimil Babka69b71892014-10-09 15:27:18 -0700402 if (!locked) {
403 /*
404 * The zone lock must be held to isolate freepages.
405 * Unfortunately this is a very coarse lock and can be
406 * heavily contended if there are parallel allocations
407 * or parallel compactions. For async compaction do not
408 * spin on the lock and we acquire the lock as late as
409 * possible.
410 */
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700411 locked = compact_trylock_irqsave(&cc->zone->lock,
412 &flags, cc);
Vlastimil Babka69b71892014-10-09 15:27:18 -0700413 if (!locked)
414 break;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700415
Vlastimil Babka69b71892014-10-09 15:27:18 -0700416 /* Recheck this is a buddy page under lock */
417 if (!PageBuddy(page))
418 goto isolate_fail;
419 }
Mel Gorman748446b2010-05-24 14:32:27 -0700420
421 /* Found a free page, break it into order-0 pages */
422 isolated = split_free_page(page);
423 total_isolated += isolated;
424 for (i = 0; i < isolated; i++) {
425 list_add(&page->lru, freelist);
426 page++;
427 }
428
429 /* If a page was split, advance to the end of it */
430 if (isolated) {
431 blockpfn += isolated - 1;
432 cursor += isolated - 1;
Laura Abbott2af120b2014-03-10 15:49:44 -0700433 continue;
Mel Gorman748446b2010-05-24 14:32:27 -0700434 }
Laura Abbott2af120b2014-03-10 15:49:44 -0700435
436isolate_fail:
437 if (strict)
438 break;
439 else
440 continue;
441
Mel Gorman748446b2010-05-24 14:32:27 -0700442 }
443
Vlastimil Babkaf1f702e2015-09-08 15:02:49 -0700444 /*
445 * There is a tiny chance that we have read bogus compound_order(),
446 * so be careful to not go outside of the pageblock.
447 */
448 if (unlikely(blockpfn > end_pfn))
449 blockpfn = end_pfn;
450
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700451 /* Record how far we have got within the block */
452 *start_pfn = blockpfn;
453
Mel Gormanb7aba692011-01-13 15:45:54 -0800454 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
Mel Gormanf40d1e42012-10-08 16:32:36 -0700455
456 /*
457 * If strict isolation is requested by CMA then check that all the
458 * pages requested were isolated. If there were any failures, 0 is
459 * returned and CMA will fail.
460 */
Laura Abbott2af120b2014-03-10 15:49:44 -0700461 if (strict && blockpfn < end_pfn)
Mel Gormanf40d1e42012-10-08 16:32:36 -0700462 total_isolated = 0;
463
464 if (locked)
465 spin_unlock_irqrestore(&cc->zone->lock, flags);
466
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700467 /* Update the pageblock-skip if the whole pageblock was scanned */
468 if (blockpfn == end_pfn)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700469 update_pageblock_skip(cc, valid_page, total_isolated, false);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700470
Minchan Kim010fc292012-12-20 15:05:06 -0800471 count_compact_events(COMPACTFREE_SCANNED, nr_scanned);
Mel Gorman397487d2012-10-19 12:00:10 +0100472 if (total_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -0800473 count_compact_events(COMPACTISOLATED, total_isolated);
Mel Gorman748446b2010-05-24 14:32:27 -0700474 return total_isolated;
475}
476
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100477/**
478 * isolate_freepages_range() - isolate free pages.
479 * @start_pfn: The first PFN to start isolating.
480 * @end_pfn: The one-past-last PFN.
481 *
482 * Non-free pages, invalid PFNs, or zone boundaries within the
483 * [start_pfn, end_pfn) range are considered errors, cause function to
484 * undo its actions and return zero.
485 *
486 * Otherwise, function returns one-past-the-last PFN of isolated page
487 * (which may be greater then end_pfn if end fell in a middle of
488 * a free page).
489 */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100490unsigned long
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700491isolate_freepages_range(struct compact_control *cc,
492 unsigned long start_pfn, unsigned long end_pfn)
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100493{
Mel Gormanf40d1e42012-10-08 16:32:36 -0700494 unsigned long isolated, pfn, block_end_pfn;
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100495 LIST_HEAD(freelist);
496
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700497 pfn = start_pfn;
498 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100499
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700500 for (; pfn < end_pfn; pfn += isolated,
501 block_end_pfn += pageblock_nr_pages) {
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700502 /* Protect pfn from changing by isolate_freepages_block */
503 unsigned long isolate_start_pfn = pfn;
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700504
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100505 block_end_pfn = min(block_end_pfn, end_pfn);
506
Joonsoo Kim58420012014-11-13 15:19:07 -0800507 /*
508 * pfn could pass the block_end_pfn if isolated freepage
509 * is more than pageblock order. In this case, we adjust
510 * scanning range to right one.
511 */
512 if (pfn >= block_end_pfn) {
513 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
514 block_end_pfn = min(block_end_pfn, end_pfn);
515 }
516
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700517 if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
518 break;
519
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700520 isolated = isolate_freepages_block(cc, &isolate_start_pfn,
521 block_end_pfn, &freelist, true);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100522
523 /*
524 * In strict mode, isolate_freepages_block() returns 0 if
525 * there are any holes in the block (ie. invalid PFNs or
526 * non-free pages).
527 */
528 if (!isolated)
529 break;
530
531 /*
532 * If we managed to isolate pages, it is always (1 << n) *
533 * pageblock_nr_pages for some non-negative n. (Max order
534 * page may span two pageblocks).
535 */
536 }
537
538 /* split_free_page does not map the pages */
539 map_pages(&freelist);
540
541 if (pfn < end_pfn) {
542 /* Loop terminated early, cleanup. */
543 release_freepages(&freelist);
544 return 0;
545 }
546
547 /* We don't use freelists for anything. */
548 return pfn;
549}
550
Mel Gorman748446b2010-05-24 14:32:27 -0700551/* Update the number of anon and file isolated pages in the zone */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700552static void acct_isolated(struct zone *zone, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700553{
554 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700555 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700556
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700557 if (list_empty(&cc->migratepages))
558 return;
559
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700560 list_for_each_entry(page, &cc->migratepages, lru)
561 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700562
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700563 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
564 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
Mel Gorman748446b2010-05-24 14:32:27 -0700565}
566
567/* Similar to reclaim, but different enough that they don't share logic */
568static bool too_many_isolated(struct zone *zone)
569{
Minchan Kimbc693042010-09-09 16:38:00 -0700570 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700571
572 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
573 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700574 active = zone_page_state(zone, NR_ACTIVE_FILE) +
575 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700576 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
577 zone_page_state(zone, NR_ISOLATED_ANON);
578
Minchan Kimbc693042010-09-09 16:38:00 -0700579 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700580}
581
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100582/**
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700583 * isolate_migratepages_block() - isolate all migrate-able pages within
584 * a single pageblock
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100585 * @cc: Compaction control structure.
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700586 * @low_pfn: The first PFN to isolate
587 * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock
588 * @isolate_mode: Isolation mode to be used.
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100589 *
590 * Isolate all pages that can be migrated from the range specified by
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700591 * [low_pfn, end_pfn). The range is expected to be within same pageblock.
592 * Returns zero if there is a fatal signal pending, otherwise PFN of the
593 * first page that was not scanned (which may be both less, equal to or more
594 * than end_pfn).
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100595 *
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700596 * The pages are isolated on cc->migratepages list (not required to be empty),
597 * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field
598 * is neither read nor updated.
Mel Gorman748446b2010-05-24 14:32:27 -0700599 */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700600static unsigned long
601isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
602 unsigned long end_pfn, isolate_mode_t isolate_mode)
Mel Gorman748446b2010-05-24 14:32:27 -0700603{
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700604 struct zone *zone = cc->zone;
Mel Gormanb7aba692011-01-13 15:45:54 -0800605 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700606 struct list_head *migratelist = &cc->migratepages;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700607 struct lruvec *lruvec;
Xiubo Lib8b2d822014-10-09 15:28:21 -0700608 unsigned long flags = 0;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700609 bool locked = false;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700610 struct page *page = NULL, *valid_page = NULL;
Mel Gorman748446b2010-05-24 14:32:27 -0700611
Mel Gorman748446b2010-05-24 14:32:27 -0700612 /*
613 * Ensure that there are not too many pages isolated from the LRU
614 * list by either parallel reclaimers or compaction. If there are,
615 * delay for some time until fewer pages are isolated
616 */
617 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700618 /* async migration should just abort */
David Rientjese0b9dae2014-06-04 16:08:28 -0700619 if (cc->mode == MIGRATE_ASYNC)
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100620 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700621
Mel Gorman748446b2010-05-24 14:32:27 -0700622 congestion_wait(BLK_RW_ASYNC, HZ/10);
623
624 if (fatal_signal_pending(current))
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100625 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700626 }
627
Vlastimil Babkabe976572014-06-04 16:10:41 -0700628 if (compact_should_abort(cc))
629 return 0;
David Rientjesaeef4b82014-06-04 16:08:31 -0700630
Mel Gorman748446b2010-05-24 14:32:27 -0700631 /* Time to isolate some pages for migration */
Mel Gorman748446b2010-05-24 14:32:27 -0700632 for (; low_pfn < end_pfn; low_pfn++) {
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700633 /*
634 * Periodically drop the lock (if held) regardless of its
635 * contention, to give chance to IRQs. Abort async compaction
636 * if contended.
637 */
638 if (!(low_pfn % SWAP_CLUSTER_MAX)
639 && compact_unlock_should_abort(&zone->lru_lock, flags,
640 &locked, cc))
641 break;
Mel Gormanc67fe372012-08-21 16:16:17 -0700642
Mel Gorman748446b2010-05-24 14:32:27 -0700643 if (!pfn_valid_within(low_pfn))
644 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800645 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700646
Mel Gorman748446b2010-05-24 14:32:27 -0700647 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800648
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700649 if (!valid_page)
650 valid_page = page;
651
Mel Gorman6c144662014-01-23 15:53:38 -0800652 /*
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700653 * Skip if free. We read page order here without zone lock
654 * which is generally unsafe, but the race window is small and
655 * the worst thing that can happen is that we skip some
656 * potential isolation targets.
Mel Gorman6c144662014-01-23 15:53:38 -0800657 */
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700658 if (PageBuddy(page)) {
659 unsigned long freepage_order = page_order_unsafe(page);
660
661 /*
662 * Without lock, we cannot be sure that what we got is
663 * a valid page order. Consider only values in the
664 * valid order range to prevent low_pfn overflow.
665 */
666 if (freepage_order > 0 && freepage_order < MAX_ORDER)
667 low_pfn += (1UL << freepage_order) - 1;
Mel Gorman748446b2010-05-24 14:32:27 -0700668 continue;
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700669 }
Mel Gorman748446b2010-05-24 14:32:27 -0700670
Mel Gorman9927af742011-01-13 15:45:59 -0800671 /*
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800672 * Check may be lockless but that's ok as we recheck later.
673 * It's possible to migrate LRU pages and balloon pages
674 * Skip any other type of page
675 */
676 if (!PageLRU(page)) {
677 if (unlikely(balloon_page_movable(page))) {
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700678 if (balloon_page_isolate(page)) {
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800679 /* Successfully isolated */
Joonsoo Kimb6c75012014-04-07 15:37:07 -0700680 goto isolate_success;
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800681 }
682 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800683 continue;
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800684 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800685
686 /*
Mel Gorman2a1402a2012-10-08 16:32:33 -0700687 * PageLRU is set. lru_lock normally excludes isolation
688 * splitting and collapsing (collapsing has already happened
689 * if PageLRU is set) but the lock is not necessarily taken
690 * here and it is wasteful to take it just to check transhuge.
691 * Check TransHuge without lock and skip the whole pageblock if
692 * it's either a transhuge or hugetlbfs page, as calling
693 * compound_order() without preventing THP from splitting the
694 * page underneath us may return surprising results.
Andrea Arcangelibc835012011-01-13 15:47:08 -0800695 */
696 if (PageTransHuge(page)) {
Mel Gorman2a1402a2012-10-08 16:32:33 -0700697 if (!locked)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700698 low_pfn = ALIGN(low_pfn + 1,
699 pageblock_nr_pages) - 1;
700 else
701 low_pfn += (1 << compound_order(page)) - 1;
702
Mel Gorman2a1402a2012-10-08 16:32:33 -0700703 continue;
704 }
705
David Rientjes119d6d52014-04-03 14:48:00 -0700706 /*
707 * Migration will fail if an anonymous page is pinned in memory,
708 * so avoid taking lru_lock and isolating it unnecessarily in an
709 * admittedly racy check.
710 */
711 if (!page_mapping(page) &&
712 page_count(page) > page_mapcount(page))
713 continue;
714
Vlastimil Babka69b71892014-10-09 15:27:18 -0700715 /* If we already hold the lock, we can skip some rechecking */
716 if (!locked) {
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700717 locked = compact_trylock_irqsave(&zone->lru_lock,
718 &flags, cc);
Vlastimil Babka69b71892014-10-09 15:27:18 -0700719 if (!locked)
720 break;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700721
Vlastimil Babka69b71892014-10-09 15:27:18 -0700722 /* Recheck PageLRU and PageTransHuge under lock */
723 if (!PageLRU(page))
724 continue;
725 if (PageTransHuge(page)) {
726 low_pfn += (1 << compound_order(page)) - 1;
727 continue;
728 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800729 }
730
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700731 lruvec = mem_cgroup_page_lruvec(page, zone);
732
Mel Gorman748446b2010-05-24 14:32:27 -0700733 /* Try isolate the page */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700734 if (__isolate_lru_page(page, isolate_mode) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700735 continue;
736
Sasha Levin309381fea2014-01-23 15:52:54 -0800737 VM_BUG_ON_PAGE(PageTransCompound(page), page);
Andrea Arcangelibc835012011-01-13 15:47:08 -0800738
Mel Gorman748446b2010-05-24 14:32:27 -0700739 /* Successfully isolated */
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700740 del_page_from_lru_list(page, lruvec, page_lru(page));
Joonsoo Kimb6c75012014-04-07 15:37:07 -0700741
742isolate_success:
743 cc->finished_update_migrate = true;
Mel Gorman748446b2010-05-24 14:32:27 -0700744 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700745 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800746 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700747
748 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800749 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
750 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700751 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800752 }
Mel Gorman748446b2010-05-24 14:32:27 -0700753 }
754
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700755 /*
756 * The PageBuddy() check could have potentially brought us outside
757 * the range to be scanned.
758 */
759 if (unlikely(low_pfn > end_pfn))
760 low_pfn = end_pfn;
761
Mel Gormanc67fe372012-08-21 16:16:17 -0700762 if (locked)
763 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700764
Vlastimil Babka50b5b092014-01-21 15:51:10 -0800765 /*
766 * Update the pageblock-skip information and cached scanner pfn,
767 * if the whole pageblock was scanned without isolating any page.
Vlastimil Babka50b5b092014-01-21 15:51:10 -0800768 */
David Rientjes35979ef2014-06-04 16:08:27 -0700769 if (low_pfn == end_pfn)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700770 update_pageblock_skip(cc, valid_page, nr_isolated, true);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700771
Mel Gormanb7aba692011-01-13 15:45:54 -0800772 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
773
Minchan Kim010fc292012-12-20 15:05:06 -0800774 count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned);
Mel Gorman397487d2012-10-19 12:00:10 +0100775 if (nr_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -0800776 count_compact_events(COMPACTISOLATED, nr_isolated);
Mel Gorman397487d2012-10-19 12:00:10 +0100777
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100778 return low_pfn;
779}
780
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700781/**
782 * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
783 * @cc: Compaction control structure.
784 * @start_pfn: The first PFN to start isolating.
785 * @end_pfn: The one-past-last PFN.
786 *
787 * Returns zero if isolation fails fatally due to e.g. pending signal.
788 * Otherwise, function returns one-past-the-last PFN of isolated page
789 * (which may be greater than end_pfn if end fell in a middle of a THP page).
790 */
791unsigned long
792isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
793 unsigned long end_pfn)
794{
795 unsigned long pfn, block_end_pfn;
796
797 /* Scan block by block. First and last block may be incomplete */
798 pfn = start_pfn;
799 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
800
801 for (; pfn < end_pfn; pfn = block_end_pfn,
802 block_end_pfn += pageblock_nr_pages) {
803
804 block_end_pfn = min(block_end_pfn, end_pfn);
805
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700806 if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700807 continue;
808
809 pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
810 ISOLATE_UNEVICTABLE);
811
Hugh Dickinsd6926182016-05-05 16:22:15 -0700812 if (!pfn)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700813 break;
Joonsoo Kim6ea41c02014-10-29 14:50:20 -0700814
815 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
816 break;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700817 }
818 acct_isolated(cc->zone, cc);
819
820 return pfn;
821}
822
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100823#endif /* CONFIG_COMPACTION || CONFIG_CMA */
824#ifdef CONFIG_COMPACTION
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100825/*
826 * Based on information in the current compact_control, find blocks
827 * suitable for isolating free pages from and then isolate them.
828 */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700829static void isolate_freepages(struct compact_control *cc)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100830{
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700831 struct zone *zone = cc->zone;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100832 struct page *page;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700833 unsigned long block_start_pfn; /* start of current pageblock */
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700834 unsigned long isolate_start_pfn; /* exact pfn we start at */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700835 unsigned long block_end_pfn; /* end of current pageblock */
836 unsigned long low_pfn; /* lowest pfn scanner is able to scan */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100837 int nr_freepages = cc->nr_freepages;
838 struct list_head *freelist = &cc->freepages;
839
840 /*
841 * Initialise the free scanner. The starting point is where we last
Vlastimil Babka49e068f2014-05-06 12:50:03 -0700842 * successfully isolated from, zone-cached value, or the end of the
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700843 * zone when isolating for the first time. For looping we also need
844 * this pfn aligned down to the pageblock boundary, because we do
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700845 * block_start_pfn -= pageblock_nr_pages in the for loop.
846 * For ending point, take care when isolating in last pageblock of a
847 * a zone which ends in the middle of a pageblock.
Vlastimil Babka49e068f2014-05-06 12:50:03 -0700848 * The low boundary is the end of the pageblock the migration scanner
849 * is using.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100850 */
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700851 isolate_start_pfn = cc->free_pfn;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700852 block_start_pfn = cc->free_pfn & ~(pageblock_nr_pages-1);
853 block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
854 zone_end_pfn(zone));
Vlastimil Babka7ed695e02014-01-21 15:51:09 -0800855 low_pfn = ALIGN(cc->migrate_pfn + 1, pageblock_nr_pages);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100856
857 /*
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100858 * Isolate free pages until enough are available to migrate the
859 * pages on cc->migratepages. We stop searching if the migrate
860 * and free page scanners meet or enough free pages are isolated.
861 */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700862 for (; block_start_pfn >= low_pfn && cc->nr_migratepages > nr_freepages;
863 block_end_pfn = block_start_pfn,
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700864 block_start_pfn -= pageblock_nr_pages,
865 isolate_start_pfn = block_start_pfn) {
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100866 unsigned long isolated;
867
David Rientjesf6ea3ad2013-09-30 13:45:03 -0700868 /*
869 * This can iterate a massively long zone without finding any
870 * suitable migration targets, so periodically check if we need
Vlastimil Babkabe976572014-06-04 16:10:41 -0700871 * to schedule, or even abort async compaction.
David Rientjesf6ea3ad2013-09-30 13:45:03 -0700872 */
Vlastimil Babkabe976572014-06-04 16:10:41 -0700873 if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
874 && compact_should_abort(cc))
875 break;
David Rientjesf6ea3ad2013-09-30 13:45:03 -0700876
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700877 page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
878 zone);
879 if (!page)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100880 continue;
881
882 /* Check the block is suitable for migration */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700883 if (!suitable_migration_target(page))
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100884 continue;
Linus Torvalds68e3e922012-06-03 20:05:57 -0700885
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700886 /* If isolation recently failed, do not retry */
887 if (!isolation_suitable(cc, page))
888 continue;
889
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700890 /* Found a block suitable for isolating free pages from. */
891 isolated = isolate_freepages_block(cc, &isolate_start_pfn,
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700892 block_end_pfn, freelist, false);
Mel Gormanf40d1e42012-10-08 16:32:36 -0700893 nr_freepages += isolated;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100894
895 /*
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700896 * Remember where the free scanner should restart next time,
897 * which is where isolate_freepages_block() left off.
898 * But if it scanned the whole pageblock, isolate_start_pfn
899 * now points at block_end_pfn, which is the start of the next
900 * pageblock.
901 * In that case we will however want to restart at the start
902 * of the previous pageblock.
903 */
904 cc->free_pfn = (isolate_start_pfn < block_end_pfn) ?
905 isolate_start_pfn :
906 block_start_pfn - pageblock_nr_pages;
907
908 /*
Vlastimil Babkae9ade562014-06-04 16:08:34 -0700909 * Set a flag that we successfully isolated in this pageblock.
910 * In the next loop iteration, zone->compact_cached_free_pfn
911 * will not be updated and thus it will effectively contain the
912 * highest pageblock we isolated pages from.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100913 */
Vlastimil Babkae9ade562014-06-04 16:08:34 -0700914 if (isolated)
Mel Gormanc89511a2012-10-08 16:32:45 -0700915 cc->finished_update_free = true;
Vlastimil Babkabe976572014-06-04 16:10:41 -0700916
917 /*
918 * isolate_freepages_block() might have aborted due to async
919 * compaction being contended
920 */
921 if (cc->contended)
922 break;
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100923 }
924
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100925 /* split_free_page does not map the pages */
926 map_pages(freelist);
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100927
Vlastimil Babka7ed695e02014-01-21 15:51:09 -0800928 /*
929 * If we crossed the migrate scanner, we want to keep it that way
930 * so that compact_finished() may detect this
931 */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700932 if (block_start_pfn < low_pfn)
Vlastimil Babkae9ade562014-06-04 16:08:34 -0700933 cc->free_pfn = cc->migrate_pfn;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700934
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100935 cc->nr_freepages = nr_freepages;
Mel Gorman748446b2010-05-24 14:32:27 -0700936}
937
938/*
939 * This is a migrate-callback that "allocates" freepages by taking pages
940 * from the isolated freelists in the block we are migrating to.
941 */
942static struct page *compaction_alloc(struct page *migratepage,
943 unsigned long data,
944 int **result)
945{
946 struct compact_control *cc = (struct compact_control *)data;
947 struct page *freepage;
948
Vlastimil Babkabe976572014-06-04 16:10:41 -0700949 /*
950 * Isolate free pages if necessary, and if we are not aborting due to
951 * contention.
952 */
Mel Gorman748446b2010-05-24 14:32:27 -0700953 if (list_empty(&cc->freepages)) {
Vlastimil Babkabe976572014-06-04 16:10:41 -0700954 if (!cc->contended)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700955 isolate_freepages(cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700956
957 if (list_empty(&cc->freepages))
958 return NULL;
959 }
960
961 freepage = list_entry(cc->freepages.next, struct page, lru);
962 list_del(&freepage->lru);
963 cc->nr_freepages--;
964
965 return freepage;
966}
967
968/*
David Rientjesd53aea32014-06-04 16:08:26 -0700969 * This is a migrate-callback that "frees" freepages back to the isolated
970 * freelist. All pages on the freelist are from the same zone, so there is no
971 * special handling needed for NUMA.
972 */
973static void compaction_free(struct page *page, unsigned long data)
974{
975 struct compact_control *cc = (struct compact_control *)data;
976
977 list_add(&page->lru, &cc->freepages);
978 cc->nr_freepages++;
979}
980
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100981/* possible outcome of isolate_migratepages */
982typedef enum {
983 ISOLATE_ABORT, /* Abort compaction now */
984 ISOLATE_NONE, /* No pages isolated, continue scanning */
985 ISOLATE_SUCCESS, /* Pages isolated, migrate */
986} isolate_migrate_t;
987
988/*
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700989 * Isolate all pages that can be migrated from the first suitable block,
990 * starting at the block pointed to by the migrate scanner pfn within
991 * compact_control.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100992 */
993static isolate_migrate_t isolate_migratepages(struct zone *zone,
994 struct compact_control *cc)
995{
996 unsigned long low_pfn, end_pfn;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700997 struct page *page;
998 const isolate_mode_t isolate_mode =
999 (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001000
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001001 /*
1002 * Start at where we last stopped, or beginning of the zone as
1003 * initialized by compact_zone()
1004 */
1005 low_pfn = cc->migrate_pfn;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001006
1007 /* Only scan within a pageblock boundary */
Mel Gormana9aacbc2013-02-22 16:32:25 -08001008 end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001009
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001010 /*
1011 * Iterate over whole pageblocks until we find the first suitable.
1012 * Do not cross the free scanner.
1013 */
1014 for (; end_pfn <= cc->free_pfn;
1015 low_pfn = end_pfn, end_pfn += pageblock_nr_pages) {
1016
1017 /*
1018 * This can potentially iterate a massively long zone with
1019 * many pageblocks unsuitable, so periodically check if we
1020 * need to schedule, or even abort async compaction.
1021 */
1022 if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
1023 && compact_should_abort(cc))
1024 break;
1025
Vlastimil Babka7d49d882014-10-09 15:27:11 -07001026 page = pageblock_pfn_to_page(low_pfn, end_pfn, zone);
1027 if (!page)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001028 continue;
1029
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001030 /* If isolation recently failed, do not retry */
1031 if (!isolation_suitable(cc, page))
1032 continue;
1033
1034 /*
1035 * For async compaction, also only scan in MOVABLE blocks.
1036 * Async compaction is optimistic to see if the minimum amount
1037 * of work satisfies the allocation.
1038 */
1039 if (cc->mode == MIGRATE_ASYNC &&
1040 !migrate_async_suitable(get_pageblock_migratetype(page)))
1041 continue;
1042
1043 /* Perform the isolation */
1044 low_pfn = isolate_migratepages_block(cc, low_pfn, end_pfn,
1045 isolate_mode);
1046
Hugh Dickinsb7c386c2015-02-12 15:00:28 -08001047 if (!low_pfn || cc->contended) {
1048 acct_isolated(zone, cc);
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001049 return ISOLATE_ABORT;
Hugh Dickinsb7c386c2015-02-12 15:00:28 -08001050 }
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001051
1052 /*
1053 * Either we isolated something and proceed with migration. Or
1054 * we failed and compact_zone should decide if we should
1055 * continue or not.
1056 */
1057 break;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001058 }
1059
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001060 acct_isolated(zone, cc);
Vlastimil Babka1d5bfe12014-11-13 15:19:30 -08001061 /*
1062 * Record where migration scanner will be restarted. If we end up in
1063 * the same pageblock as the free scanner, make the scanners fully
1064 * meet so that compact_finished() terminates compaction.
1065 */
1066 cc->migrate_pfn = (end_pfn <= cc->free_pfn) ? low_pfn : cc->free_pfn;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001067
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001068 return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001069}
1070
David Rientjes6d7ce552014-10-09 15:27:27 -07001071static int compact_finished(struct zone *zone, struct compact_control *cc,
1072 const int migratetype)
Mel Gorman748446b2010-05-24 14:32:27 -07001073{
Mel Gorman8fb74b92013-01-11 14:32:16 -08001074 unsigned int order;
Andrea Arcangeli5a03b052011-01-13 15:47:11 -08001075 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -07001076
Vlastimil Babkabe976572014-06-04 16:10:41 -07001077 if (cc->contended || fatal_signal_pending(current))
Mel Gorman748446b2010-05-24 14:32:27 -07001078 return COMPACT_PARTIAL;
1079
Mel Gorman753341a2012-10-08 16:32:40 -07001080 /* Compaction run completes if the migrate and free scanner meet */
Mel Gormanbb13ffe2012-10-08 16:32:41 -07001081 if (cc->free_pfn <= cc->migrate_pfn) {
Vlastimil Babka55b7c4c2014-01-21 15:51:11 -08001082 /* Let the next compaction start anew. */
David Rientjes35979ef2014-06-04 16:08:27 -07001083 zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
1084 zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
Vlastimil Babka55b7c4c2014-01-21 15:51:11 -08001085 zone->compact_cached_free_pfn = zone_end_pfn(zone);
1086
Mel Gorman62997022012-10-08 16:32:47 -07001087 /*
1088 * Mark that the PG_migrate_skip information should be cleared
1089 * by kswapd when it goes to sleep. kswapd does not set the
1090 * flag itself as the decision to be clear should be directly
1091 * based on an allocation request.
1092 */
1093 if (!current_is_kswapd())
1094 zone->compact_blockskip_flush = true;
1095
Mel Gorman748446b2010-05-24 14:32:27 -07001096 return COMPACT_COMPLETE;
Mel Gormanbb13ffe2012-10-08 16:32:41 -07001097 }
Mel Gorman748446b2010-05-24 14:32:27 -07001098
Johannes Weiner82478fb2011-01-20 14:44:21 -08001099 /*
1100 * order == -1 is expected when compacting via
1101 * /proc/sys/vm/compact_memory
1102 */
Mel Gorman56de7262010-05-24 14:32:30 -07001103 if (cc->order == -1)
1104 return COMPACT_CONTINUE;
1105
Michal Hocko3957c772011-06-15 15:08:25 -07001106 /* Compaction run is not finished if the watermark is not met */
1107 watermark = low_wmark_pages(zone);
1108 watermark += (1 << cc->order);
1109
1110 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
1111 return COMPACT_CONTINUE;
1112
Mel Gorman56de7262010-05-24 14:32:30 -07001113 /* Direct compactor: Is a suitable page free? */
Mel Gorman8fb74b92013-01-11 14:32:16 -08001114 for (order = cc->order; order < MAX_ORDER; order++) {
1115 struct free_area *area = &zone->free_area[order];
Mel Gorman56de7262010-05-24 14:32:30 -07001116
Mel Gorman8fb74b92013-01-11 14:32:16 -08001117 /* Job done if page is free of the right migratetype */
David Rientjes6d7ce552014-10-09 15:27:27 -07001118 if (!list_empty(&area->free_list[migratetype]))
Mel Gorman8fb74b92013-01-11 14:32:16 -08001119 return COMPACT_PARTIAL;
1120
1121 /* Job done if allocation would set block type */
Joonsoo Kim42af81d2015-02-12 14:59:50 -08001122 if (order >= pageblock_order && area->nr_free)
Mel Gorman8fb74b92013-01-11 14:32:16 -08001123 return COMPACT_PARTIAL;
Mel Gorman56de7262010-05-24 14:32:30 -07001124 }
1125
Mel Gorman748446b2010-05-24 14:32:27 -07001126 return COMPACT_CONTINUE;
1127}
1128
Mel Gorman3e7d3442011-01-13 15:45:56 -08001129/*
1130 * compaction_suitable: Is this suitable to run compaction on this zone now?
1131 * Returns
1132 * COMPACT_SKIPPED - If there are too few free pages for compaction
1133 * COMPACT_PARTIAL - If the allocation would succeed without compaction
1134 * COMPACT_CONTINUE - If compaction should run now
1135 */
1136unsigned long compaction_suitable(struct zone *zone, int order)
1137{
1138 int fragindex;
1139 unsigned long watermark;
1140
1141 /*
Michal Hocko3957c772011-06-15 15:08:25 -07001142 * order == -1 is expected when compacting via
1143 * /proc/sys/vm/compact_memory
1144 */
1145 if (order == -1)
1146 return COMPACT_CONTINUE;
1147
1148 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -08001149 * Watermarks for order-0 must be met for compaction. Note the 2UL.
1150 * This is because during migration, copies of pages need to be
1151 * allocated and for a short time, the footprint is higher
1152 */
1153 watermark = low_wmark_pages(zone) + (2UL << order);
1154 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
1155 return COMPACT_SKIPPED;
1156
1157 /*
1158 * fragmentation index determines if allocation failures are due to
1159 * low memory or external fragmentation
1160 *
Shaohua Lia582a732011-06-15 15:08:49 -07001161 * index of -1000 implies allocations might succeed depending on
1162 * watermarks
Mel Gorman3e7d3442011-01-13 15:45:56 -08001163 * index towards 0 implies failure is due to lack of memory
1164 * index towards 1000 implies failure is due to fragmentation
1165 *
1166 * Only compact if a failure would be due to fragmentation.
1167 */
1168 fragindex = fragmentation_index(zone, order);
1169 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
1170 return COMPACT_SKIPPED;
1171
Shaohua Lia582a732011-06-15 15:08:49 -07001172 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
1173 0, 0))
Mel Gorman3e7d3442011-01-13 15:45:56 -08001174 return COMPACT_PARTIAL;
1175
1176 return COMPACT_CONTINUE;
1177}
1178
Mel Gorman748446b2010-05-24 14:32:27 -07001179static int compact_zone(struct zone *zone, struct compact_control *cc)
1180{
1181 int ret;
Mel Gormanc89511a2012-10-08 16:32:45 -07001182 unsigned long start_pfn = zone->zone_start_pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -08001183 unsigned long end_pfn = zone_end_pfn(zone);
David Rientjes6d7ce552014-10-09 15:27:27 -07001184 const int migratetype = gfpflags_to_migratetype(cc->gfp_mask);
David Rientjese0b9dae2014-06-04 16:08:28 -07001185 const bool sync = cc->mode != MIGRATE_ASYNC;
Mel Gorman748446b2010-05-24 14:32:27 -07001186
Mel Gorman3e7d3442011-01-13 15:45:56 -08001187 ret = compaction_suitable(zone, cc->order);
1188 switch (ret) {
1189 case COMPACT_PARTIAL:
1190 case COMPACT_SKIPPED:
1191 /* Compaction is likely to fail */
1192 return ret;
1193 case COMPACT_CONTINUE:
1194 /* Fall through to compaction */
1195 ;
1196 }
1197
Mel Gormanc89511a2012-10-08 16:32:45 -07001198 /*
Vlastimil Babkad3132e42014-01-21 15:51:08 -08001199 * Clear pageblock skip if there were failures recently and compaction
1200 * is about to be retried after being deferred. kswapd does not do
1201 * this reset as it'll reset the cached information when going to sleep.
1202 */
1203 if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
1204 __reset_isolation_suitable(zone);
1205
1206 /*
Mel Gormanc89511a2012-10-08 16:32:45 -07001207 * Setup to move all movable pages to the end of the zone. Used cached
1208 * information on where the scanners should start but check that it
1209 * is initialised by ensuring the values are within zone boundaries.
1210 */
David Rientjese0b9dae2014-06-04 16:08:28 -07001211 cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync];
Mel Gormanc89511a2012-10-08 16:32:45 -07001212 cc->free_pfn = zone->compact_cached_free_pfn;
1213 if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
1214 cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
1215 zone->compact_cached_free_pfn = cc->free_pfn;
1216 }
1217 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
1218 cc->migrate_pfn = start_pfn;
David Rientjes35979ef2014-06-04 16:08:27 -07001219 zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
1220 zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
Mel Gormanc89511a2012-10-08 16:32:45 -07001221 }
Mel Gorman748446b2010-05-24 14:32:27 -07001222
Mel Gorman0eb927c2014-01-21 15:51:05 -08001223 trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, cc->free_pfn, end_pfn);
1224
Mel Gorman748446b2010-05-24 14:32:27 -07001225 migrate_prep_local();
1226
David Rientjes6d7ce552014-10-09 15:27:27 -07001227 while ((ret = compact_finished(zone, cc, migratetype)) ==
1228 COMPACT_CONTINUE) {
Minchan Kim9d502c12011-03-22 16:30:39 -07001229 int err;
Mel Gorman748446b2010-05-24 14:32:27 -07001230
Mel Gormanf9e35b32011-06-15 15:08:52 -07001231 switch (isolate_migratepages(zone, cc)) {
1232 case ISOLATE_ABORT:
1233 ret = COMPACT_PARTIAL;
Rafael Aquini5733c7d2012-12-11 16:02:47 -08001234 putback_movable_pages(&cc->migratepages);
Shaohua Lie64c5232012-10-08 16:32:27 -07001235 cc->nr_migratepages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -07001236 goto out;
1237 case ISOLATE_NONE:
Mel Gorman748446b2010-05-24 14:32:27 -07001238 continue;
Mel Gormanf9e35b32011-06-15 15:08:52 -07001239 case ISOLATE_SUCCESS:
1240 ;
1241 }
Mel Gorman748446b2010-05-24 14:32:27 -07001242
David Rientjesd53aea32014-06-04 16:08:26 -07001243 err = migrate_pages(&cc->migratepages, compaction_alloc,
David Rientjese0b9dae2014-06-04 16:08:28 -07001244 compaction_free, (unsigned long)cc, cc->mode,
Mel Gorman7b2a2d42012-10-19 14:07:31 +01001245 MR_COMPACTION);
Mel Gorman748446b2010-05-24 14:32:27 -07001246
Vlastimil Babkaf8c93012014-06-04 16:08:32 -07001247 trace_mm_compaction_migratepages(cc->nr_migratepages, err,
1248 &cc->migratepages);
Mel Gorman748446b2010-05-24 14:32:27 -07001249
Vlastimil Babkaf8c93012014-06-04 16:08:32 -07001250 /* All pages were either migrated or will be released */
1251 cc->nr_migratepages = 0;
Minchan Kim9d502c12011-03-22 16:30:39 -07001252 if (err) {
Rafael Aquini5733c7d2012-12-11 16:02:47 -08001253 putback_movable_pages(&cc->migratepages);
Vlastimil Babka7ed695e02014-01-21 15:51:09 -08001254 /*
1255 * migrate_pages() may return -ENOMEM when scanners meet
1256 * and we want compact_finished() to detect it
1257 */
1258 if (err == -ENOMEM && cc->free_pfn > cc->migrate_pfn) {
David Rientjes4bf2bba2012-07-11 14:02:13 -07001259 ret = COMPACT_PARTIAL;
1260 goto out;
1261 }
Mel Gorman748446b2010-05-24 14:32:27 -07001262 }
Mel Gorman748446b2010-05-24 14:32:27 -07001263 }
1264
Mel Gormanf9e35b32011-06-15 15:08:52 -07001265out:
Mel Gorman748446b2010-05-24 14:32:27 -07001266 /* Release free pages and check accounting */
1267 cc->nr_freepages -= release_freepages(&cc->freepages);
1268 VM_BUG_ON(cc->nr_freepages != 0);
1269
Mel Gorman0eb927c2014-01-21 15:51:05 -08001270 trace_mm_compaction_end(ret);
1271
Mel Gorman748446b2010-05-24 14:32:27 -07001272 return ret;
1273}
Mel Gorman76ab0f52010-05-24 14:32:28 -07001274
David Rientjese0b9dae2014-06-04 16:08:28 -07001275static unsigned long compact_zone_order(struct zone *zone, int order,
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001276 gfp_t gfp_mask, enum migrate_mode mode, int *contended)
Mel Gorman56de7262010-05-24 14:32:30 -07001277{
Shaohua Lie64c5232012-10-08 16:32:27 -07001278 unsigned long ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001279 struct compact_control cc = {
1280 .nr_freepages = 0,
1281 .nr_migratepages = 0,
1282 .order = order,
David Rientjes6d7ce552014-10-09 15:27:27 -07001283 .gfp_mask = gfp_mask,
Mel Gorman56de7262010-05-24 14:32:30 -07001284 .zone = zone,
David Rientjese0b9dae2014-06-04 16:08:28 -07001285 .mode = mode,
Mel Gorman56de7262010-05-24 14:32:30 -07001286 };
1287 INIT_LIST_HEAD(&cc.freepages);
1288 INIT_LIST_HEAD(&cc.migratepages);
1289
Shaohua Lie64c5232012-10-08 16:32:27 -07001290 ret = compact_zone(zone, &cc);
1291
1292 VM_BUG_ON(!list_empty(&cc.freepages));
1293 VM_BUG_ON(!list_empty(&cc.migratepages));
1294
1295 *contended = cc.contended;
1296 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001297}
1298
Mel Gorman5e771902010-05-24 14:32:31 -07001299int sysctl_extfrag_threshold = 500;
1300
Mel Gorman56de7262010-05-24 14:32:30 -07001301/**
1302 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
1303 * @zonelist: The zonelist used for the current allocation
1304 * @order: The order of the current allocation
1305 * @gfp_mask: The GFP mask of the current allocation
1306 * @nodemask: The allowed nodes to allocate from
David Rientjese0b9dae2014-06-04 16:08:28 -07001307 * @mode: The migration mode for async, sync light, or sync migration
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001308 * @contended: Return value that determines if compaction was aborted due to
1309 * need_resched() or lock contention
Vlastimil Babka53853e22014-10-09 15:27:02 -07001310 * @candidate_zone: Return the zone where we think allocation should succeed
Mel Gorman56de7262010-05-24 14:32:30 -07001311 *
1312 * This is the main entry point for direct page compaction.
1313 */
1314unsigned long try_to_compact_pages(struct zonelist *zonelist,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001315 int order, gfp_t gfp_mask, nodemask_t *nodemask,
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001316 enum migrate_mode mode, int *contended,
Vlastimil Babka53853e22014-10-09 15:27:02 -07001317 struct zone **candidate_zone)
Mel Gorman56de7262010-05-24 14:32:30 -07001318{
1319 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
1320 int may_enter_fs = gfp_mask & __GFP_FS;
1321 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -07001322 struct zoneref *z;
1323 struct zone *zone;
Vlastimil Babka53853e22014-10-09 15:27:02 -07001324 int rc = COMPACT_DEFERRED;
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001325 int alloc_flags = 0;
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001326 int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */
1327
1328 *contended = COMPACT_CONTENDED_NONE;
Mel Gorman56de7262010-05-24 14:32:30 -07001329
Mel Gorman4ffb6332012-10-08 16:29:09 -07001330 /* Check if the GFP flags allow compaction */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -08001331 if (!order || !may_enter_fs || !may_perform_io)
Vlastimil Babka53853e22014-10-09 15:27:02 -07001332 return COMPACT_SKIPPED;
Mel Gorman56de7262010-05-24 14:32:30 -07001333
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001334#ifdef CONFIG_CMA
David Rientjes43e7a342014-10-09 15:27:25 -07001335 if (gfpflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001336 alloc_flags |= ALLOC_CMA;
1337#endif
Mel Gorman56de7262010-05-24 14:32:30 -07001338 /* Compact each zone in the list */
1339 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
1340 nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -07001341 int status;
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001342 int zone_contended;
Mel Gorman56de7262010-05-24 14:32:30 -07001343
Vlastimil Babka53853e22014-10-09 15:27:02 -07001344 if (compaction_deferred(zone, order))
1345 continue;
1346
David Rientjese0b9dae2014-06-04 16:08:28 -07001347 status = compact_zone_order(zone, order, gfp_mask, mode,
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001348 &zone_contended);
Mel Gorman56de7262010-05-24 14:32:30 -07001349 rc = max(status, rc);
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001350 /*
1351 * It takes at least one zone that wasn't lock contended
1352 * to clear all_zones_contended.
1353 */
1354 all_zones_contended &= zone_contended;
Mel Gorman56de7262010-05-24 14:32:30 -07001355
Mel Gorman3e7d3442011-01-13 15:45:56 -08001356 /* If a normal allocation would succeed, stop compacting */
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001357 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
Vlastimil Babka53853e22014-10-09 15:27:02 -07001358 alloc_flags)) {
1359 *candidate_zone = zone;
1360 /*
1361 * We think the allocation will succeed in this zone,
1362 * but it is not certain, hence the false. The caller
1363 * will repeat this with true if allocation indeed
1364 * succeeds in this zone.
1365 */
1366 compaction_defer_reset(zone, order, false);
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001367 /*
1368 * It is possible that async compaction aborted due to
1369 * need_resched() and the watermarks were ok thanks to
1370 * somebody else freeing memory. The allocation can
1371 * however still fail so we better signal the
1372 * need_resched() contention anyway (this will not
1373 * prevent the allocation attempt).
1374 */
1375 if (zone_contended == COMPACT_CONTENDED_SCHED)
1376 *contended = COMPACT_CONTENDED_SCHED;
1377
1378 goto break_loop;
1379 }
1380
1381 if (mode != MIGRATE_ASYNC) {
Vlastimil Babka53853e22014-10-09 15:27:02 -07001382 /*
1383 * We think that allocation won't succeed in this zone
1384 * so we defer compaction there. If it ends up
1385 * succeeding after all, it will be reset.
1386 */
1387 defer_compaction(zone, order);
1388 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001389
1390 /*
1391 * We might have stopped compacting due to need_resched() in
1392 * async compaction, or due to a fatal signal detected. In that
1393 * case do not try further zones and signal need_resched()
1394 * contention.
1395 */
1396 if ((zone_contended == COMPACT_CONTENDED_SCHED)
1397 || fatal_signal_pending(current)) {
1398 *contended = COMPACT_CONTENDED_SCHED;
1399 goto break_loop;
1400 }
1401
1402 continue;
1403break_loop:
1404 /*
1405 * We might not have tried all the zones, so be conservative
1406 * and assume they are not all lock contended.
1407 */
1408 all_zones_contended = 0;
1409 break;
Mel Gorman56de7262010-05-24 14:32:30 -07001410 }
1411
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001412 /*
1413 * If at least one zone wasn't deferred or skipped, we report if all
1414 * zones that were tried were lock contended.
1415 */
1416 if (rc > COMPACT_SKIPPED && all_zones_contended)
1417 *contended = COMPACT_CONTENDED_LOCK;
1418
Mel Gorman56de7262010-05-24 14:32:30 -07001419 return rc;
1420}
1421
1422
Mel Gorman76ab0f52010-05-24 14:32:28 -07001423/* Compact all zones within a node */
Andrew Morton7103f162013-02-22 16:32:33 -08001424static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001425{
1426 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -07001427 struct zone *zone;
1428
Mel Gorman76ab0f52010-05-24 14:32:28 -07001429 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -07001430
1431 zone = &pgdat->node_zones[zoneid];
1432 if (!populated_zone(zone))
1433 continue;
1434
Rik van Riel7be62de2012-03-21 16:33:52 -07001435 cc->nr_freepages = 0;
1436 cc->nr_migratepages = 0;
1437 cc->zone = zone;
1438 INIT_LIST_HEAD(&cc->freepages);
1439 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001440
Dan Carpenteraad6ec32012-03-21 16:33:54 -07001441 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -07001442 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001443
Rik van Rielaff62242012-03-21 16:33:52 -07001444 if (cc->order > 0) {
Vlastimil Babkade6c60a2014-01-21 15:51:07 -08001445 if (zone_watermark_ok(zone, cc->order,
1446 low_wmark_pages(zone), 0, 0))
1447 compaction_defer_reset(zone, cc->order, false);
Rik van Rielaff62242012-03-21 16:33:52 -07001448 }
1449
Rik van Riel7be62de2012-03-21 16:33:52 -07001450 VM_BUG_ON(!list_empty(&cc->freepages));
1451 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -07001452 }
Mel Gorman76ab0f52010-05-24 14:32:28 -07001453}
1454
Andrew Morton7103f162013-02-22 16:32:33 -08001455void compact_pgdat(pg_data_t *pgdat, int order)
Rik van Riel7be62de2012-03-21 16:33:52 -07001456{
1457 struct compact_control cc = {
1458 .order = order,
David Rientjese0b9dae2014-06-04 16:08:28 -07001459 .mode = MIGRATE_ASYNC,
Rik van Riel7be62de2012-03-21 16:33:52 -07001460 };
1461
Mel Gorman3a7200a2013-09-11 14:22:19 -07001462 if (!order)
1463 return;
1464
Andrew Morton7103f162013-02-22 16:32:33 -08001465 __compact_pgdat(pgdat, &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001466}
1467
Andrew Morton7103f162013-02-22 16:32:33 -08001468static void compact_node(int nid)
Rik van Riel7be62de2012-03-21 16:33:52 -07001469{
Rik van Riel7be62de2012-03-21 16:33:52 -07001470 struct compact_control cc = {
1471 .order = -1,
David Rientjese0b9dae2014-06-04 16:08:28 -07001472 .mode = MIGRATE_SYNC,
David Rientjes91ca9182014-04-03 14:47:23 -07001473 .ignore_skip_hint = true,
Rik van Riel7be62de2012-03-21 16:33:52 -07001474 };
1475
Andrew Morton7103f162013-02-22 16:32:33 -08001476 __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001477}
1478
Mel Gorman76ab0f52010-05-24 14:32:28 -07001479/* Compact all nodes in the system */
Jason Liu7964c062013-01-11 14:31:47 -08001480static void compact_nodes(void)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001481{
1482 int nid;
1483
Hugh Dickins8575ec22012-03-21 16:33:53 -07001484 /* Flush pending updates to the LRU lists */
1485 lru_add_drain_all();
1486
Mel Gorman76ab0f52010-05-24 14:32:28 -07001487 for_each_online_node(nid)
1488 compact_node(nid);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001489}
1490
1491/* The written value is actually unused, all memory is compacted */
1492int sysctl_compact_memory;
1493
1494/* This is the entry point for compacting all nodes via /proc/sys/vm */
1495int sysctl_compaction_handler(struct ctl_table *table, int write,
1496 void __user *buffer, size_t *length, loff_t *ppos)
1497{
1498 if (write)
Jason Liu7964c062013-01-11 14:31:47 -08001499 compact_nodes();
Mel Gorman76ab0f52010-05-24 14:32:28 -07001500
1501 return 0;
1502}
Mel Gormaned4a6d72010-05-24 14:32:29 -07001503
Mel Gorman5e771902010-05-24 14:32:31 -07001504int sysctl_extfrag_handler(struct ctl_table *table, int write,
1505 void __user *buffer, size_t *length, loff_t *ppos)
1506{
1507 proc_dointvec_minmax(table, write, buffer, length, ppos);
1508
1509 return 0;
1510}
1511
Mel Gormaned4a6d72010-05-24 14:32:29 -07001512#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Rashika Kheria74e77fb2014-04-03 14:48:01 -07001513static ssize_t sysfs_compact_node(struct device *dev,
Kay Sievers10fbcf42011-12-21 14:48:43 -08001514 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -07001515 const char *buf, size_t count)
1516{
Hugh Dickins8575ec22012-03-21 16:33:53 -07001517 int nid = dev->id;
1518
1519 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1520 /* Flush pending updates to the LRU lists */
1521 lru_add_drain_all();
1522
1523 compact_node(nid);
1524 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07001525
1526 return count;
1527}
Kay Sievers10fbcf42011-12-21 14:48:43 -08001528static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001529
1530int compaction_register_node(struct node *node)
1531{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001532 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001533}
1534
1535void compaction_unregister_node(struct node *node)
1536{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001537 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001538}
1539#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001540
1541#endif /* CONFIG_COMPACTION */