]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - mm/compaction.c
mm, compaction: defer only on COMPACT_COMPLETE
[mirror_ubuntu-bionic-kernel.git] / mm / compaction.c
CommitLineData
748446bb
MG
1/*
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>
76ab0f53 15#include <linux/sysctl.h>
ed4a6d7f 16#include <linux/sysfs.h>
bf6bddf1 17#include <linux/balloon_compaction.h>
194159fb 18#include <linux/page-isolation.h>
748446bb
MG
19#include "internal.h"
20
010fc29a
MK
21#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
ff9543fd
MN
36#if defined CONFIG_COMPACTION || defined CONFIG_CMA
37
b7aba698
MG
38#define CREATE_TRACE_POINTS
39#include <trace/events/compaction.h>
40
748446bb
MG
41static 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
ff9543fd
MN
55static 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
47118af0
MN
65static inline bool migrate_async_suitable(int migratetype)
66{
67 return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
68}
69
7d49d886
VB
70/*
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
bb13ffeb
MG
113#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 */
62997027 129static void __reset_isolation_suitable(struct zone *zone)
bb13ffeb
MG
130{
131 unsigned long start_pfn = zone->zone_start_pfn;
108bcc96 132 unsigned long end_pfn = zone_end_pfn(zone);
bb13ffeb
MG
133 unsigned long pfn;
134
35979ef3
DR
135 zone->compact_cached_migrate_pfn[0] = start_pfn;
136 zone->compact_cached_migrate_pfn[1] = start_pfn;
c89511ab 137 zone->compact_cached_free_pfn = end_pfn;
62997027 138 zone->compact_blockskip_flush = false;
bb13ffeb
MG
139
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
62997027
MG
157void 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
bb13ffeb
MG
172/*
173 * If no pages were isolated then mark this pageblock to be skipped in the
62997027 174 * future. The information is later cleared by __reset_isolation_suitable().
bb13ffeb 175 */
c89511ab
MG
176static void update_pageblock_skip(struct compact_control *cc,
177 struct page *page, unsigned long nr_isolated,
edc2ca61 178 bool migrate_scanner)
bb13ffeb 179{
c89511ab 180 struct zone *zone = cc->zone;
35979ef3 181 unsigned long pfn;
6815bf3f
JK
182
183 if (cc->ignore_skip_hint)
184 return;
185
bb13ffeb
MG
186 if (!page)
187 return;
188
35979ef3
DR
189 if (nr_isolated)
190 return;
191
edc2ca61 192 set_pageblock_skip(page);
c89511ab 193
35979ef3
DR
194 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;
e0b9daeb
DR
202 if (cc->mode != MIGRATE_ASYNC &&
203 pfn > zone->compact_cached_migrate_pfn[1])
35979ef3
DR
204 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;
c89511ab 210 }
bb13ffeb
MG
211}
212#else
213static inline bool isolation_suitable(struct compact_control *cc,
214 struct page *page)
215{
216 return true;
217}
218
c89511ab
MG
219static void update_pageblock_skip(struct compact_control *cc,
220 struct page *page, unsigned long nr_isolated,
edc2ca61 221 bool migrate_scanner)
bb13ffeb
MG
222{
223}
224#endif /* CONFIG_COMPACTION */
225
8b44d279
VB
226/*
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)
2a1402aa 236{
8b44d279
VB
237 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 }
1f9efdef 245
8b44d279 246 return true;
2a1402aa
MG
247}
248
c67fe375
MG
249/*
250 * Compaction requires the taking of some coarse locks that are potentially
8b44d279
VB
251 * 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.
c67fe375 258 *
8b44d279
VB
259 * 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)
c67fe375 263 */
8b44d279
VB
264static bool compact_unlock_should_abort(spinlock_t *lock,
265 unsigned long flags, bool *locked, struct compact_control *cc)
c67fe375 266{
8b44d279
VB
267 if (*locked) {
268 spin_unlock_irqrestore(lock, flags);
269 *locked = false;
270 }
1f9efdef 271
8b44d279
VB
272 if (fatal_signal_pending(current)) {
273 cc->contended = COMPACT_CONTENDED_SCHED;
274 return true;
275 }
c67fe375 276
8b44d279 277 if (need_resched()) {
e0b9daeb 278 if (cc->mode == MIGRATE_ASYNC) {
8b44d279
VB
279 cc->contended = COMPACT_CONTENDED_SCHED;
280 return true;
c67fe375 281 }
c67fe375 282 cond_resched();
c67fe375
MG
283 }
284
8b44d279 285 return false;
c67fe375
MG
286}
287
be976572
VB
288/*
289 * Aside from avoiding lock contention, compaction also periodically checks
290 * need_resched() and either schedules in sync compaction or aborts async
8b44d279 291 * compaction. This is similar to what compact_unlock_should_abort() does, but
be976572
VB
292 * 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) {
1f9efdef 302 cc->contended = COMPACT_CONTENDED_SCHED;
be976572
VB
303 return true;
304 }
305
306 cond_resched();
307 }
308
309 return false;
310}
311
f40d1e42
MG
312/* Returns true if the page is within a block suitable for migration to */
313static bool suitable_migration_target(struct page *page)
314{
7d348b9e 315 /* If the page is a large free page, then disallow migration */
99c0fd5e
VB
316 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 }
f40d1e42
MG
325
326 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
7d348b9e 327 if (migrate_async_suitable(get_pageblock_migratetype(page)))
f40d1e42
MG
328 return true;
329
330 /* Otherwise skip the block */
331 return false;
332}
333
85aa125f 334/*
9e4be470
JM
335 * 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).
85aa125f 338 */
f40d1e42 339static unsigned long isolate_freepages_block(struct compact_control *cc,
e14c720e 340 unsigned long *start_pfn,
85aa125f
MN
341 unsigned long end_pfn,
342 struct list_head *freelist,
343 bool strict)
748446bb 344{
b7aba698 345 int nr_scanned = 0, total_isolated = 0;
bb13ffeb 346 struct page *cursor, *valid_page = NULL;
b8b2d825 347 unsigned long flags = 0;
f40d1e42 348 bool locked = false;
e14c720e 349 unsigned long blockpfn = *start_pfn;
748446bb 350
748446bb
MG
351 cursor = pfn_to_page(blockpfn);
352
f40d1e42 353 /* Isolate free pages. */
748446bb
MG
354 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
355 int isolated, i;
356 struct page *page = cursor;
357
8b44d279
VB
358 /*
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
b7aba698 368 nr_scanned++;
f40d1e42 369 if (!pfn_valid_within(blockpfn))
2af120bc
LA
370 goto isolate_fail;
371
bb13ffeb
MG
372 if (!valid_page)
373 valid_page = page;
f40d1e42 374 if (!PageBuddy(page))
2af120bc 375 goto isolate_fail;
f40d1e42
MG
376
377 /*
69b7189f
VB
378 * If we already hold the lock, we can skip some rechecking.
379 * Note that if we hold the lock now, checked_pageblock was
380 * already set in some previous iteration (or strict is true),
381 * so it is correct to skip the suitable migration target
382 * recheck as well.
f40d1e42 383 */
69b7189f
VB
384 if (!locked) {
385 /*
386 * The zone lock must be held to isolate freepages.
387 * Unfortunately this is a very coarse lock and can be
388 * heavily contended if there are parallel allocations
389 * or parallel compactions. For async compaction do not
390 * spin on the lock and we acquire the lock as late as
391 * possible.
392 */
8b44d279
VB
393 locked = compact_trylock_irqsave(&cc->zone->lock,
394 &flags, cc);
69b7189f
VB
395 if (!locked)
396 break;
f40d1e42 397
69b7189f
VB
398 /* Recheck this is a buddy page under lock */
399 if (!PageBuddy(page))
400 goto isolate_fail;
401 }
748446bb
MG
402
403 /* Found a free page, break it into order-0 pages */
404 isolated = split_free_page(page);
405 total_isolated += isolated;
406 for (i = 0; i < isolated; i++) {
407 list_add(&page->lru, freelist);
408 page++;
409 }
410
411 /* If a page was split, advance to the end of it */
412 if (isolated) {
413 blockpfn += isolated - 1;
414 cursor += isolated - 1;
2af120bc 415 continue;
748446bb 416 }
2af120bc
LA
417
418isolate_fail:
419 if (strict)
420 break;
421 else
422 continue;
423
748446bb
MG
424 }
425
e14c720e
VB
426 /* Record how far we have got within the block */
427 *start_pfn = blockpfn;
428
b7aba698 429 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
f40d1e42
MG
430
431 /*
432 * If strict isolation is requested by CMA then check that all the
433 * pages requested were isolated. If there were any failures, 0 is
434 * returned and CMA will fail.
435 */
2af120bc 436 if (strict && blockpfn < end_pfn)
f40d1e42
MG
437 total_isolated = 0;
438
439 if (locked)
440 spin_unlock_irqrestore(&cc->zone->lock, flags);
441
bb13ffeb
MG
442 /* Update the pageblock-skip if the whole pageblock was scanned */
443 if (blockpfn == end_pfn)
edc2ca61 444 update_pageblock_skip(cc, valid_page, total_isolated, false);
bb13ffeb 445
010fc29a 446 count_compact_events(COMPACTFREE_SCANNED, nr_scanned);
397487db 447 if (total_isolated)
010fc29a 448 count_compact_events(COMPACTISOLATED, total_isolated);
748446bb
MG
449 return total_isolated;
450}
451
85aa125f
MN
452/**
453 * isolate_freepages_range() - isolate free pages.
454 * @start_pfn: The first PFN to start isolating.
455 * @end_pfn: The one-past-last PFN.
456 *
457 * Non-free pages, invalid PFNs, or zone boundaries within the
458 * [start_pfn, end_pfn) range are considered errors, cause function to
459 * undo its actions and return zero.
460 *
461 * Otherwise, function returns one-past-the-last PFN of isolated page
462 * (which may be greater then end_pfn if end fell in a middle of
463 * a free page).
464 */
ff9543fd 465unsigned long
bb13ffeb
MG
466isolate_freepages_range(struct compact_control *cc,
467 unsigned long start_pfn, unsigned long end_pfn)
85aa125f 468{
f40d1e42 469 unsigned long isolated, pfn, block_end_pfn;
85aa125f
MN
470 LIST_HEAD(freelist);
471
7d49d886
VB
472 pfn = start_pfn;
473 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
474
475 for (; pfn < end_pfn; pfn += isolated,
476 block_end_pfn += pageblock_nr_pages) {
e14c720e
VB
477 /* Protect pfn from changing by isolate_freepages_block */
478 unsigned long isolate_start_pfn = pfn;
85aa125f 479
85aa125f
MN
480 block_end_pfn = min(block_end_pfn, end_pfn);
481
58420016
JK
482 /*
483 * pfn could pass the block_end_pfn if isolated freepage
484 * is more than pageblock order. In this case, we adjust
485 * scanning range to right one.
486 */
487 if (pfn >= block_end_pfn) {
488 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
489 block_end_pfn = min(block_end_pfn, end_pfn);
490 }
491
7d49d886
VB
492 if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
493 break;
494
e14c720e
VB
495 isolated = isolate_freepages_block(cc, &isolate_start_pfn,
496 block_end_pfn, &freelist, true);
85aa125f
MN
497
498 /*
499 * In strict mode, isolate_freepages_block() returns 0 if
500 * there are any holes in the block (ie. invalid PFNs or
501 * non-free pages).
502 */
503 if (!isolated)
504 break;
505
506 /*
507 * If we managed to isolate pages, it is always (1 << n) *
508 * pageblock_nr_pages for some non-negative n. (Max order
509 * page may span two pageblocks).
510 */
511 }
512
513 /* split_free_page does not map the pages */
514 map_pages(&freelist);
515
516 if (pfn < end_pfn) {
517 /* Loop terminated early, cleanup. */
518 release_freepages(&freelist);
519 return 0;
520 }
521
522 /* We don't use freelists for anything. */
523 return pfn;
524}
525
748446bb 526/* Update the number of anon and file isolated pages in the zone */
edc2ca61 527static void acct_isolated(struct zone *zone, struct compact_control *cc)
748446bb
MG
528{
529 struct page *page;
b9e84ac1 530 unsigned int count[2] = { 0, };
748446bb 531
edc2ca61
VB
532 if (list_empty(&cc->migratepages))
533 return;
534
b9e84ac1
MK
535 list_for_each_entry(page, &cc->migratepages, lru)
536 count[!!page_is_file_cache(page)]++;
748446bb 537
edc2ca61
VB
538 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
539 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
748446bb
MG
540}
541
542/* Similar to reclaim, but different enough that they don't share logic */
543static bool too_many_isolated(struct zone *zone)
544{
bc693045 545 unsigned long active, inactive, isolated;
748446bb
MG
546
547 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
548 zone_page_state(zone, NR_INACTIVE_ANON);
bc693045
MK
549 active = zone_page_state(zone, NR_ACTIVE_FILE) +
550 zone_page_state(zone, NR_ACTIVE_ANON);
748446bb
MG
551 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
552 zone_page_state(zone, NR_ISOLATED_ANON);
553
bc693045 554 return isolated > (inactive + active) / 2;
748446bb
MG
555}
556
2fe86e00 557/**
edc2ca61
VB
558 * isolate_migratepages_block() - isolate all migrate-able pages within
559 * a single pageblock
2fe86e00 560 * @cc: Compaction control structure.
edc2ca61
VB
561 * @low_pfn: The first PFN to isolate
562 * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock
563 * @isolate_mode: Isolation mode to be used.
2fe86e00
MN
564 *
565 * Isolate all pages that can be migrated from the range specified by
edc2ca61
VB
566 * [low_pfn, end_pfn). The range is expected to be within same pageblock.
567 * Returns zero if there is a fatal signal pending, otherwise PFN of the
568 * first page that was not scanned (which may be both less, equal to or more
569 * than end_pfn).
2fe86e00 570 *
edc2ca61
VB
571 * The pages are isolated on cc->migratepages list (not required to be empty),
572 * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field
573 * is neither read nor updated.
748446bb 574 */
edc2ca61
VB
575static unsigned long
576isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
577 unsigned long end_pfn, isolate_mode_t isolate_mode)
748446bb 578{
edc2ca61 579 struct zone *zone = cc->zone;
b7aba698 580 unsigned long nr_scanned = 0, nr_isolated = 0;
748446bb 581 struct list_head *migratelist = &cc->migratepages;
fa9add64 582 struct lruvec *lruvec;
b8b2d825 583 unsigned long flags = 0;
2a1402aa 584 bool locked = false;
bb13ffeb 585 struct page *page = NULL, *valid_page = NULL;
748446bb 586
748446bb
MG
587 /*
588 * Ensure that there are not too many pages isolated from the LRU
589 * list by either parallel reclaimers or compaction. If there are,
590 * delay for some time until fewer pages are isolated
591 */
592 while (unlikely(too_many_isolated(zone))) {
f9e35b3b 593 /* async migration should just abort */
e0b9daeb 594 if (cc->mode == MIGRATE_ASYNC)
2fe86e00 595 return 0;
f9e35b3b 596
748446bb
MG
597 congestion_wait(BLK_RW_ASYNC, HZ/10);
598
599 if (fatal_signal_pending(current))
2fe86e00 600 return 0;
748446bb
MG
601 }
602
be976572
VB
603 if (compact_should_abort(cc))
604 return 0;
aeef4b83 605
748446bb 606 /* Time to isolate some pages for migration */
748446bb 607 for (; low_pfn < end_pfn; low_pfn++) {
8b44d279
VB
608 /*
609 * Periodically drop the lock (if held) regardless of its
610 * contention, to give chance to IRQs. Abort async compaction
611 * if contended.
612 */
613 if (!(low_pfn % SWAP_CLUSTER_MAX)
614 && compact_unlock_should_abort(&zone->lru_lock, flags,
615 &locked, cc))
616 break;
c67fe375 617
748446bb
MG
618 if (!pfn_valid_within(low_pfn))
619 continue;
b7aba698 620 nr_scanned++;
748446bb 621
748446bb 622 page = pfn_to_page(low_pfn);
dc908600 623
bb13ffeb
MG
624 if (!valid_page)
625 valid_page = page;
626
6c14466c 627 /*
99c0fd5e
VB
628 * Skip if free. We read page order here without zone lock
629 * which is generally unsafe, but the race window is small and
630 * the worst thing that can happen is that we skip some
631 * potential isolation targets.
6c14466c 632 */
99c0fd5e
VB
633 if (PageBuddy(page)) {
634 unsigned long freepage_order = page_order_unsafe(page);
635
636 /*
637 * Without lock, we cannot be sure that what we got is
638 * a valid page order. Consider only values in the
639 * valid order range to prevent low_pfn overflow.
640 */
641 if (freepage_order > 0 && freepage_order < MAX_ORDER)
642 low_pfn += (1UL << freepage_order) - 1;
748446bb 643 continue;
99c0fd5e 644 }
748446bb 645
bf6bddf1
RA
646 /*
647 * Check may be lockless but that's ok as we recheck later.
648 * It's possible to migrate LRU pages and balloon pages
649 * Skip any other type of page
650 */
651 if (!PageLRU(page)) {
652 if (unlikely(balloon_page_movable(page))) {
d6d86c0a 653 if (balloon_page_isolate(page)) {
bf6bddf1 654 /* Successfully isolated */
b6c75016 655 goto isolate_success;
bf6bddf1
RA
656 }
657 }
bc835011 658 continue;
bf6bddf1 659 }
bc835011
AA
660
661 /*
2a1402aa
MG
662 * PageLRU is set. lru_lock normally excludes isolation
663 * splitting and collapsing (collapsing has already happened
664 * if PageLRU is set) but the lock is not necessarily taken
665 * here and it is wasteful to take it just to check transhuge.
666 * Check TransHuge without lock and skip the whole pageblock if
667 * it's either a transhuge or hugetlbfs page, as calling
668 * compound_order() without preventing THP from splitting the
669 * page underneath us may return surprising results.
bc835011 670 */
2a1402aa
MG
671 if (PageTransHuge(page)) {
672 if (!locked)
edc2ca61
VB
673 low_pfn = ALIGN(low_pfn + 1,
674 pageblock_nr_pages) - 1;
675 else
676 low_pfn += (1 << compound_order(page)) - 1;
677
2a1402aa
MG
678 continue;
679 }
680
119d6d59
DR
681 /*
682 * Migration will fail if an anonymous page is pinned in memory,
683 * so avoid taking lru_lock and isolating it unnecessarily in an
684 * admittedly racy check.
685 */
686 if (!page_mapping(page) &&
687 page_count(page) > page_mapcount(page))
688 continue;
689
69b7189f
VB
690 /* If we already hold the lock, we can skip some rechecking */
691 if (!locked) {
8b44d279
VB
692 locked = compact_trylock_irqsave(&zone->lru_lock,
693 &flags, cc);
69b7189f
VB
694 if (!locked)
695 break;
2a1402aa 696
69b7189f
VB
697 /* Recheck PageLRU and PageTransHuge under lock */
698 if (!PageLRU(page))
699 continue;
700 if (PageTransHuge(page)) {
701 low_pfn += (1 << compound_order(page)) - 1;
702 continue;
703 }
bc835011
AA
704 }
705
fa9add64
HD
706 lruvec = mem_cgroup_page_lruvec(page, zone);
707
748446bb 708 /* Try isolate the page */
edc2ca61 709 if (__isolate_lru_page(page, isolate_mode) != 0)
748446bb
MG
710 continue;
711
309381fe 712 VM_BUG_ON_PAGE(PageTransCompound(page), page);
bc835011 713
748446bb 714 /* Successfully isolated */
fa9add64 715 del_page_from_lru_list(page, lruvec, page_lru(page));
b6c75016
JK
716
717isolate_success:
718 cc->finished_update_migrate = true;
748446bb 719 list_add(&page->lru, migratelist);
748446bb 720 cc->nr_migratepages++;
b7aba698 721 nr_isolated++;
748446bb
MG
722
723 /* Avoid isolating too much */
31b8384a
HD
724 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
725 ++low_pfn;
748446bb 726 break;
31b8384a 727 }
748446bb
MG
728 }
729
99c0fd5e
VB
730 /*
731 * The PageBuddy() check could have potentially brought us outside
732 * the range to be scanned.
733 */
734 if (unlikely(low_pfn > end_pfn))
735 low_pfn = end_pfn;
736
c67fe375
MG
737 if (locked)
738 spin_unlock_irqrestore(&zone->lru_lock, flags);
748446bb 739
50b5b094
VB
740 /*
741 * Update the pageblock-skip information and cached scanner pfn,
742 * if the whole pageblock was scanned without isolating any page.
50b5b094 743 */
35979ef3 744 if (low_pfn == end_pfn)
edc2ca61 745 update_pageblock_skip(cc, valid_page, nr_isolated, true);
bb13ffeb 746
b7aba698
MG
747 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
748
010fc29a 749 count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned);
397487db 750 if (nr_isolated)
010fc29a 751 count_compact_events(COMPACTISOLATED, nr_isolated);
397487db 752
2fe86e00
MN
753 return low_pfn;
754}
755
edc2ca61
VB
756/**
757 * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
758 * @cc: Compaction control structure.
759 * @start_pfn: The first PFN to start isolating.
760 * @end_pfn: The one-past-last PFN.
761 *
762 * Returns zero if isolation fails fatally due to e.g. pending signal.
763 * Otherwise, function returns one-past-the-last PFN of isolated page
764 * (which may be greater than end_pfn if end fell in a middle of a THP page).
765 */
766unsigned long
767isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
768 unsigned long end_pfn)
769{
770 unsigned long pfn, block_end_pfn;
771
772 /* Scan block by block. First and last block may be incomplete */
773 pfn = start_pfn;
774 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
775
776 for (; pfn < end_pfn; pfn = block_end_pfn,
777 block_end_pfn += pageblock_nr_pages) {
778
779 block_end_pfn = min(block_end_pfn, end_pfn);
780
7d49d886 781 if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
edc2ca61
VB
782 continue;
783
784 pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
785 ISOLATE_UNEVICTABLE);
786
787 /*
788 * In case of fatal failure, release everything that might
789 * have been isolated in the previous iteration, and signal
790 * the failure back to caller.
791 */
792 if (!pfn) {
793 putback_movable_pages(&cc->migratepages);
794 cc->nr_migratepages = 0;
795 break;
796 }
6ea41c0c
JK
797
798 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
799 break;
edc2ca61
VB
800 }
801 acct_isolated(cc->zone, cc);
802
803 return pfn;
804}
805
ff9543fd
MN
806#endif /* CONFIG_COMPACTION || CONFIG_CMA */
807#ifdef CONFIG_COMPACTION
2fe86e00 808/*
ff9543fd
MN
809 * Based on information in the current compact_control, find blocks
810 * suitable for isolating free pages from and then isolate them.
2fe86e00 811 */
edc2ca61 812static void isolate_freepages(struct compact_control *cc)
2fe86e00 813{
edc2ca61 814 struct zone *zone = cc->zone;
ff9543fd 815 struct page *page;
c96b9e50 816 unsigned long block_start_pfn; /* start of current pageblock */
e14c720e 817 unsigned long isolate_start_pfn; /* exact pfn we start at */
c96b9e50
VB
818 unsigned long block_end_pfn; /* end of current pageblock */
819 unsigned long low_pfn; /* lowest pfn scanner is able to scan */
ff9543fd
MN
820 int nr_freepages = cc->nr_freepages;
821 struct list_head *freelist = &cc->freepages;
2fe86e00 822
ff9543fd
MN
823 /*
824 * Initialise the free scanner. The starting point is where we last
49e068f0 825 * successfully isolated from, zone-cached value, or the end of the
e14c720e
VB
826 * zone when isolating for the first time. For looping we also need
827 * this pfn aligned down to the pageblock boundary, because we do
c96b9e50
VB
828 * block_start_pfn -= pageblock_nr_pages in the for loop.
829 * For ending point, take care when isolating in last pageblock of a
830 * a zone which ends in the middle of a pageblock.
49e068f0
VB
831 * The low boundary is the end of the pageblock the migration scanner
832 * is using.
ff9543fd 833 */
e14c720e 834 isolate_start_pfn = cc->free_pfn;
c96b9e50
VB
835 block_start_pfn = cc->free_pfn & ~(pageblock_nr_pages-1);
836 block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
837 zone_end_pfn(zone));
7ed695e0 838 low_pfn = ALIGN(cc->migrate_pfn + 1, pageblock_nr_pages);
2fe86e00 839
ff9543fd
MN
840 /*
841 * Isolate free pages until enough are available to migrate the
842 * pages on cc->migratepages. We stop searching if the migrate
843 * and free page scanners meet or enough free pages are isolated.
844 */
c96b9e50
VB
845 for (; block_start_pfn >= low_pfn && cc->nr_migratepages > nr_freepages;
846 block_end_pfn = block_start_pfn,
e14c720e
VB
847 block_start_pfn -= pageblock_nr_pages,
848 isolate_start_pfn = block_start_pfn) {
ff9543fd 849 unsigned long isolated;
2fe86e00 850
f6ea3adb
DR
851 /*
852 * This can iterate a massively long zone without finding any
853 * suitable migration targets, so periodically check if we need
be976572 854 * to schedule, or even abort async compaction.
f6ea3adb 855 */
be976572
VB
856 if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
857 && compact_should_abort(cc))
858 break;
f6ea3adb 859
7d49d886
VB
860 page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
861 zone);
862 if (!page)
ff9543fd
MN
863 continue;
864
865 /* Check the block is suitable for migration */
68e3e926 866 if (!suitable_migration_target(page))
ff9543fd 867 continue;
68e3e926 868
bb13ffeb
MG
869 /* If isolation recently failed, do not retry */
870 if (!isolation_suitable(cc, page))
871 continue;
872
e14c720e
VB
873 /* Found a block suitable for isolating free pages from. */
874 isolated = isolate_freepages_block(cc, &isolate_start_pfn,
c96b9e50 875 block_end_pfn, freelist, false);
f40d1e42 876 nr_freepages += isolated;
ff9543fd 877
e14c720e
VB
878 /*
879 * Remember where the free scanner should restart next time,
880 * which is where isolate_freepages_block() left off.
881 * But if it scanned the whole pageblock, isolate_start_pfn
882 * now points at block_end_pfn, which is the start of the next
883 * pageblock.
884 * In that case we will however want to restart at the start
885 * of the previous pageblock.
886 */
887 cc->free_pfn = (isolate_start_pfn < block_end_pfn) ?
888 isolate_start_pfn :
889 block_start_pfn - pageblock_nr_pages;
890
ff9543fd 891 /*
e9ade569
VB
892 * Set a flag that we successfully isolated in this pageblock.
893 * In the next loop iteration, zone->compact_cached_free_pfn
894 * will not be updated and thus it will effectively contain the
895 * highest pageblock we isolated pages from.
ff9543fd 896 */
e9ade569 897 if (isolated)
c89511ab 898 cc->finished_update_free = true;
be976572
VB
899
900 /*
901 * isolate_freepages_block() might have aborted due to async
902 * compaction being contended
903 */
904 if (cc->contended)
905 break;
ff9543fd
MN
906 }
907
908 /* split_free_page does not map the pages */
909 map_pages(freelist);
910
7ed695e0
VB
911 /*
912 * If we crossed the migrate scanner, we want to keep it that way
913 * so that compact_finished() may detect this
914 */
c96b9e50 915 if (block_start_pfn < low_pfn)
e9ade569 916 cc->free_pfn = cc->migrate_pfn;
c96b9e50 917
ff9543fd 918 cc->nr_freepages = nr_freepages;
748446bb
MG
919}
920
921/*
922 * This is a migrate-callback that "allocates" freepages by taking pages
923 * from the isolated freelists in the block we are migrating to.
924 */
925static struct page *compaction_alloc(struct page *migratepage,
926 unsigned long data,
927 int **result)
928{
929 struct compact_control *cc = (struct compact_control *)data;
930 struct page *freepage;
931
be976572
VB
932 /*
933 * Isolate free pages if necessary, and if we are not aborting due to
934 * contention.
935 */
748446bb 936 if (list_empty(&cc->freepages)) {
be976572 937 if (!cc->contended)
edc2ca61 938 isolate_freepages(cc);
748446bb
MG
939
940 if (list_empty(&cc->freepages))
941 return NULL;
942 }
943
944 freepage = list_entry(cc->freepages.next, struct page, lru);
945 list_del(&freepage->lru);
946 cc->nr_freepages--;
947
948 return freepage;
949}
950
951/*
d53aea3d
DR
952 * This is a migrate-callback that "frees" freepages back to the isolated
953 * freelist. All pages on the freelist are from the same zone, so there is no
954 * special handling needed for NUMA.
955 */
956static void compaction_free(struct page *page, unsigned long data)
957{
958 struct compact_control *cc = (struct compact_control *)data;
959
960 list_add(&page->lru, &cc->freepages);
961 cc->nr_freepages++;
962}
963
ff9543fd
MN
964/* possible outcome of isolate_migratepages */
965typedef enum {
966 ISOLATE_ABORT, /* Abort compaction now */
967 ISOLATE_NONE, /* No pages isolated, continue scanning */
968 ISOLATE_SUCCESS, /* Pages isolated, migrate */
969} isolate_migrate_t;
970
971/*
edc2ca61
VB
972 * Isolate all pages that can be migrated from the first suitable block,
973 * starting at the block pointed to by the migrate scanner pfn within
974 * compact_control.
ff9543fd
MN
975 */
976static isolate_migrate_t isolate_migratepages(struct zone *zone,
977 struct compact_control *cc)
978{
979 unsigned long low_pfn, end_pfn;
edc2ca61
VB
980 struct page *page;
981 const isolate_mode_t isolate_mode =
982 (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0);
ff9543fd 983
edc2ca61
VB
984 /*
985 * Start at where we last stopped, or beginning of the zone as
986 * initialized by compact_zone()
987 */
988 low_pfn = cc->migrate_pfn;
ff9543fd
MN
989
990 /* Only scan within a pageblock boundary */
a9aacbcc 991 end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages);
ff9543fd 992
edc2ca61
VB
993 /*
994 * Iterate over whole pageblocks until we find the first suitable.
995 * Do not cross the free scanner.
996 */
997 for (; end_pfn <= cc->free_pfn;
998 low_pfn = end_pfn, end_pfn += pageblock_nr_pages) {
ff9543fd 999
edc2ca61
VB
1000 /*
1001 * This can potentially iterate a massively long zone with
1002 * many pageblocks unsuitable, so periodically check if we
1003 * need to schedule, or even abort async compaction.
1004 */
1005 if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
1006 && compact_should_abort(cc))
1007 break;
ff9543fd 1008
7d49d886
VB
1009 page = pageblock_pfn_to_page(low_pfn, end_pfn, zone);
1010 if (!page)
edc2ca61
VB
1011 continue;
1012
edc2ca61
VB
1013 /* If isolation recently failed, do not retry */
1014 if (!isolation_suitable(cc, page))
1015 continue;
1016
1017 /*
1018 * For async compaction, also only scan in MOVABLE blocks.
1019 * Async compaction is optimistic to see if the minimum amount
1020 * of work satisfies the allocation.
1021 */
1022 if (cc->mode == MIGRATE_ASYNC &&
1023 !migrate_async_suitable(get_pageblock_migratetype(page)))
1024 continue;
1025
1026 /* Perform the isolation */
1027 low_pfn = isolate_migratepages_block(cc, low_pfn, end_pfn,
1028 isolate_mode);
1029
1030 if (!low_pfn || cc->contended)
1031 return ISOLATE_ABORT;
1032
1033 /*
1034 * Either we isolated something and proceed with migration. Or
1035 * we failed and compact_zone should decide if we should
1036 * continue or not.
1037 */
1038 break;
1039 }
1040
1041 acct_isolated(zone, cc);
1d5bfe1f
VB
1042 /*
1043 * Record where migration scanner will be restarted. If we end up in
1044 * the same pageblock as the free scanner, make the scanners fully
1045 * meet so that compact_finished() terminates compaction.
1046 */
1047 cc->migrate_pfn = (end_pfn <= cc->free_pfn) ? low_pfn : cc->free_pfn;
ff9543fd 1048
edc2ca61 1049 return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
ff9543fd
MN
1050}
1051
6d7ce559
DR
1052static int compact_finished(struct zone *zone, struct compact_control *cc,
1053 const int migratetype)
748446bb 1054{
8fb74b9f 1055 unsigned int order;
5a03b051 1056 unsigned long watermark;
56de7263 1057
be976572 1058 if (cc->contended || fatal_signal_pending(current))
748446bb
MG
1059 return COMPACT_PARTIAL;
1060
753341a4 1061 /* Compaction run completes if the migrate and free scanner meet */
bb13ffeb 1062 if (cc->free_pfn <= cc->migrate_pfn) {
55b7c4c9 1063 /* Let the next compaction start anew. */
35979ef3
DR
1064 zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
1065 zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
55b7c4c9
VB
1066 zone->compact_cached_free_pfn = zone_end_pfn(zone);
1067
62997027
MG
1068 /*
1069 * Mark that the PG_migrate_skip information should be cleared
1070 * by kswapd when it goes to sleep. kswapd does not set the
1071 * flag itself as the decision to be clear should be directly
1072 * based on an allocation request.
1073 */
1074 if (!current_is_kswapd())
1075 zone->compact_blockskip_flush = true;
1076
748446bb 1077 return COMPACT_COMPLETE;
bb13ffeb 1078 }
748446bb 1079
82478fb7
JW
1080 /*
1081 * order == -1 is expected when compacting via
1082 * /proc/sys/vm/compact_memory
1083 */
56de7263
MG
1084 if (cc->order == -1)
1085 return COMPACT_CONTINUE;
1086
3957c776
MH
1087 /* Compaction run is not finished if the watermark is not met */
1088 watermark = low_wmark_pages(zone);
3957c776 1089
ebff3980
VB
1090 if (!zone_watermark_ok(zone, cc->order, watermark, cc->classzone_idx,
1091 cc->alloc_flags))
3957c776
MH
1092 return COMPACT_CONTINUE;
1093
56de7263 1094 /* Direct compactor: Is a suitable page free? */
8fb74b9f
MG
1095 for (order = cc->order; order < MAX_ORDER; order++) {
1096 struct free_area *area = &zone->free_area[order];
1097
1098 /* Job done if page is free of the right migratetype */
6d7ce559 1099 if (!list_empty(&area->free_list[migratetype]))
8fb74b9f
MG
1100 return COMPACT_PARTIAL;
1101
1102 /* Job done if allocation would set block type */
1103 if (cc->order >= pageblock_order && area->nr_free)
56de7263
MG
1104 return COMPACT_PARTIAL;
1105 }
1106
748446bb
MG
1107 return COMPACT_CONTINUE;
1108}
1109
3e7d3449
MG
1110/*
1111 * compaction_suitable: Is this suitable to run compaction on this zone now?
1112 * Returns
1113 * COMPACT_SKIPPED - If there are too few free pages for compaction
1114 * COMPACT_PARTIAL - If the allocation would succeed without compaction
1115 * COMPACT_CONTINUE - If compaction should run now
1116 */
ebff3980
VB
1117unsigned long compaction_suitable(struct zone *zone, int order,
1118 int alloc_flags, int classzone_idx)
3e7d3449
MG
1119{
1120 int fragindex;
1121 unsigned long watermark;
1122
3957c776
MH
1123 /*
1124 * order == -1 is expected when compacting via
1125 * /proc/sys/vm/compact_memory
1126 */
1127 if (order == -1)
1128 return COMPACT_CONTINUE;
1129
ebff3980
VB
1130 watermark = low_wmark_pages(zone);
1131 /*
1132 * If watermarks for high-order allocation are already met, there
1133 * should be no need for compaction at all.
1134 */
1135 if (zone_watermark_ok(zone, order, watermark, classzone_idx,
1136 alloc_flags))
1137 return COMPACT_PARTIAL;
1138
3e7d3449
MG
1139 /*
1140 * Watermarks for order-0 must be met for compaction. Note the 2UL.
1141 * This is because during migration, copies of pages need to be
1142 * allocated and for a short time, the footprint is higher
1143 */
ebff3980
VB
1144 watermark += (2UL << order);
1145 if (!zone_watermark_ok(zone, 0, watermark, classzone_idx, alloc_flags))
3e7d3449
MG
1146 return COMPACT_SKIPPED;
1147
1148 /*
1149 * fragmentation index determines if allocation failures are due to
1150 * low memory or external fragmentation
1151 *
ebff3980
VB
1152 * index of -1000 would imply allocations might succeed depending on
1153 * watermarks, but we already failed the high-order watermark check
3e7d3449
MG
1154 * index towards 0 implies failure is due to lack of memory
1155 * index towards 1000 implies failure is due to fragmentation
1156 *
1157 * Only compact if a failure would be due to fragmentation.
1158 */
1159 fragindex = fragmentation_index(zone, order);
1160 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
1161 return COMPACT_SKIPPED;
1162
3e7d3449
MG
1163 return COMPACT_CONTINUE;
1164}
1165
748446bb
MG
1166static int compact_zone(struct zone *zone, struct compact_control *cc)
1167{
1168 int ret;
c89511ab 1169 unsigned long start_pfn = zone->zone_start_pfn;
108bcc96 1170 unsigned long end_pfn = zone_end_pfn(zone);
6d7ce559 1171 const int migratetype = gfpflags_to_migratetype(cc->gfp_mask);
e0b9daeb 1172 const bool sync = cc->mode != MIGRATE_ASYNC;
748446bb 1173
ebff3980
VB
1174 ret = compaction_suitable(zone, cc->order, cc->alloc_flags,
1175 cc->classzone_idx);
3e7d3449
MG
1176 switch (ret) {
1177 case COMPACT_PARTIAL:
1178 case COMPACT_SKIPPED:
1179 /* Compaction is likely to fail */
1180 return ret;
1181 case COMPACT_CONTINUE:
1182 /* Fall through to compaction */
1183 ;
1184 }
1185
d3132e4b
VB
1186 /*
1187 * Clear pageblock skip if there were failures recently and compaction
1188 * is about to be retried after being deferred. kswapd does not do
1189 * this reset as it'll reset the cached information when going to sleep.
1190 */
1191 if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
1192 __reset_isolation_suitable(zone);
1193
c89511ab
MG
1194 /*
1195 * Setup to move all movable pages to the end of the zone. Used cached
1196 * information on where the scanners should start but check that it
1197 * is initialised by ensuring the values are within zone boundaries.
1198 */
e0b9daeb 1199 cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync];
c89511ab
MG
1200 cc->free_pfn = zone->compact_cached_free_pfn;
1201 if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
1202 cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
1203 zone->compact_cached_free_pfn = cc->free_pfn;
1204 }
1205 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
1206 cc->migrate_pfn = start_pfn;
35979ef3
DR
1207 zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
1208 zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
c89511ab 1209 }
748446bb 1210
0eb927c0
MG
1211 trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, cc->free_pfn, end_pfn);
1212
748446bb
MG
1213 migrate_prep_local();
1214
6d7ce559
DR
1215 while ((ret = compact_finished(zone, cc, migratetype)) ==
1216 COMPACT_CONTINUE) {
9d502c1c 1217 int err;
748446bb 1218
f9e35b3b
MG
1219 switch (isolate_migratepages(zone, cc)) {
1220 case ISOLATE_ABORT:
1221 ret = COMPACT_PARTIAL;
5733c7d1 1222 putback_movable_pages(&cc->migratepages);
e64c5237 1223 cc->nr_migratepages = 0;
f9e35b3b
MG
1224 goto out;
1225 case ISOLATE_NONE:
748446bb 1226 continue;
f9e35b3b
MG
1227 case ISOLATE_SUCCESS:
1228 ;
1229 }
748446bb 1230
d53aea3d 1231 err = migrate_pages(&cc->migratepages, compaction_alloc,
e0b9daeb 1232 compaction_free, (unsigned long)cc, cc->mode,
7b2a2d4a 1233 MR_COMPACTION);
748446bb 1234
f8c9301f
VB
1235 trace_mm_compaction_migratepages(cc->nr_migratepages, err,
1236 &cc->migratepages);
748446bb 1237
f8c9301f
VB
1238 /* All pages were either migrated or will be released */
1239 cc->nr_migratepages = 0;
9d502c1c 1240 if (err) {
5733c7d1 1241 putback_movable_pages(&cc->migratepages);
7ed695e0
VB
1242 /*
1243 * migrate_pages() may return -ENOMEM when scanners meet
1244 * and we want compact_finished() to detect it
1245 */
1246 if (err == -ENOMEM && cc->free_pfn > cc->migrate_pfn) {
4bf2bba3
DR
1247 ret = COMPACT_PARTIAL;
1248 goto out;
1249 }
748446bb 1250 }
748446bb
MG
1251 }
1252
f9e35b3b 1253out:
748446bb
MG
1254 /* Release free pages and check accounting */
1255 cc->nr_freepages -= release_freepages(&cc->freepages);
1256 VM_BUG_ON(cc->nr_freepages != 0);
1257
0eb927c0
MG
1258 trace_mm_compaction_end(ret);
1259
748446bb
MG
1260 return ret;
1261}
76ab0f53 1262
e0b9daeb 1263static unsigned long compact_zone_order(struct zone *zone, int order,
ebff3980
VB
1264 gfp_t gfp_mask, enum migrate_mode mode, int *contended,
1265 int alloc_flags, int classzone_idx)
56de7263 1266{
e64c5237 1267 unsigned long ret;
56de7263
MG
1268 struct compact_control cc = {
1269 .nr_freepages = 0,
1270 .nr_migratepages = 0,
1271 .order = order,
6d7ce559 1272 .gfp_mask = gfp_mask,
56de7263 1273 .zone = zone,
e0b9daeb 1274 .mode = mode,
ebff3980
VB
1275 .alloc_flags = alloc_flags,
1276 .classzone_idx = classzone_idx,
56de7263
MG
1277 };
1278 INIT_LIST_HEAD(&cc.freepages);
1279 INIT_LIST_HEAD(&cc.migratepages);
1280
e64c5237
SL
1281 ret = compact_zone(zone, &cc);
1282
1283 VM_BUG_ON(!list_empty(&cc.freepages));
1284 VM_BUG_ON(!list_empty(&cc.migratepages));
1285
1286 *contended = cc.contended;
1287 return ret;
56de7263
MG
1288}
1289
5e771905
MG
1290int sysctl_extfrag_threshold = 500;
1291
56de7263
MG
1292/**
1293 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
1294 * @zonelist: The zonelist used for the current allocation
1295 * @order: The order of the current allocation
1296 * @gfp_mask: The GFP mask of the current allocation
1297 * @nodemask: The allowed nodes to allocate from
e0b9daeb 1298 * @mode: The migration mode for async, sync light, or sync migration
1f9efdef
VB
1299 * @contended: Return value that determines if compaction was aborted due to
1300 * need_resched() or lock contention
56de7263
MG
1301 *
1302 * This is the main entry point for direct page compaction.
1303 */
1304unsigned long try_to_compact_pages(struct zonelist *zonelist,
77f1fe6b 1305 int order, gfp_t gfp_mask, nodemask_t *nodemask,
1f9efdef 1306 enum migrate_mode mode, int *contended,
97d47a65 1307 int alloc_flags, int classzone_idx)
56de7263
MG
1308{
1309 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
1310 int may_enter_fs = gfp_mask & __GFP_FS;
1311 int may_perform_io = gfp_mask & __GFP_IO;
56de7263
MG
1312 struct zoneref *z;
1313 struct zone *zone;
53853e2d 1314 int rc = COMPACT_DEFERRED;
1f9efdef
VB
1315 int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */
1316
1317 *contended = COMPACT_CONTENDED_NONE;
56de7263 1318
4ffb6335 1319 /* Check if the GFP flags allow compaction */
c5a73c3d 1320 if (!order || !may_enter_fs || !may_perform_io)
53853e2d 1321 return COMPACT_SKIPPED;
56de7263
MG
1322
1323 /* Compact each zone in the list */
1324 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
1325 nodemask) {
56de7263 1326 int status;
1f9efdef 1327 int zone_contended;
56de7263 1328
53853e2d
VB
1329 if (compaction_deferred(zone, order))
1330 continue;
1331
e0b9daeb 1332 status = compact_zone_order(zone, order, gfp_mask, mode,
ebff3980 1333 &zone_contended, alloc_flags, classzone_idx);
56de7263 1334 rc = max(status, rc);
1f9efdef
VB
1335 /*
1336 * It takes at least one zone that wasn't lock contended
1337 * to clear all_zones_contended.
1338 */
1339 all_zones_contended &= zone_contended;
56de7263 1340
3e7d3449 1341 /* If a normal allocation would succeed, stop compacting */
ebff3980
VB
1342 if (zone_watermark_ok(zone, order, low_wmark_pages(zone),
1343 classzone_idx, alloc_flags)) {
53853e2d
VB
1344 /*
1345 * We think the allocation will succeed in this zone,
1346 * but it is not certain, hence the false. The caller
1347 * will repeat this with true if allocation indeed
1348 * succeeds in this zone.
1349 */
1350 compaction_defer_reset(zone, order, false);
1f9efdef
VB
1351 /*
1352 * It is possible that async compaction aborted due to
1353 * need_resched() and the watermarks were ok thanks to
1354 * somebody else freeing memory. The allocation can
1355 * however still fail so we better signal the
1356 * need_resched() contention anyway (this will not
1357 * prevent the allocation attempt).
1358 */
1359 if (zone_contended == COMPACT_CONTENDED_SCHED)
1360 *contended = COMPACT_CONTENDED_SCHED;
1361
1362 goto break_loop;
1363 }
1364
f8669795 1365 if (mode != MIGRATE_ASYNC && status == COMPACT_COMPLETE) {
53853e2d
VB
1366 /*
1367 * We think that allocation won't succeed in this zone
1368 * so we defer compaction there. If it ends up
1369 * succeeding after all, it will be reset.
1370 */
1371 defer_compaction(zone, order);
1372 }
1f9efdef
VB
1373
1374 /*
1375 * We might have stopped compacting due to need_resched() in
1376 * async compaction, or due to a fatal signal detected. In that
1377 * case do not try further zones and signal need_resched()
1378 * contention.
1379 */
1380 if ((zone_contended == COMPACT_CONTENDED_SCHED)
1381 || fatal_signal_pending(current)) {
1382 *contended = COMPACT_CONTENDED_SCHED;
1383 goto break_loop;
1384 }
1385
1386 continue;
1387break_loop:
1388 /*
1389 * We might not have tried all the zones, so be conservative
1390 * and assume they are not all lock contended.
1391 */
1392 all_zones_contended = 0;
1393 break;
56de7263
MG
1394 }
1395
1f9efdef
VB
1396 /*
1397 * If at least one zone wasn't deferred or skipped, we report if all
1398 * zones that were tried were lock contended.
1399 */
1400 if (rc > COMPACT_SKIPPED && all_zones_contended)
1401 *contended = COMPACT_CONTENDED_LOCK;
1402
56de7263
MG
1403 return rc;
1404}
1405
1406
76ab0f53 1407/* Compact all zones within a node */
7103f16d 1408static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
76ab0f53
MG
1409{
1410 int zoneid;
76ab0f53
MG
1411 struct zone *zone;
1412
76ab0f53 1413 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
76ab0f53
MG
1414
1415 zone = &pgdat->node_zones[zoneid];
1416 if (!populated_zone(zone))
1417 continue;
1418
7be62de9
RR
1419 cc->nr_freepages = 0;
1420 cc->nr_migratepages = 0;
1421 cc->zone = zone;
1422 INIT_LIST_HEAD(&cc->freepages);
1423 INIT_LIST_HEAD(&cc->migratepages);
76ab0f53 1424
aad6ec37 1425 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
7be62de9 1426 compact_zone(zone, cc);
76ab0f53 1427
aff62249 1428 if (cc->order > 0) {
de6c60a6
VB
1429 if (zone_watermark_ok(zone, cc->order,
1430 low_wmark_pages(zone), 0, 0))
1431 compaction_defer_reset(zone, cc->order, false);
aff62249
RR
1432 }
1433
7be62de9
RR
1434 VM_BUG_ON(!list_empty(&cc->freepages));
1435 VM_BUG_ON(!list_empty(&cc->migratepages));
76ab0f53 1436 }
76ab0f53
MG
1437}
1438
7103f16d 1439void compact_pgdat(pg_data_t *pgdat, int order)
7be62de9
RR
1440{
1441 struct compact_control cc = {
1442 .order = order,
e0b9daeb 1443 .mode = MIGRATE_ASYNC,
7be62de9
RR
1444 };
1445
3a7200af
MG
1446 if (!order)
1447 return;
1448
7103f16d 1449 __compact_pgdat(pgdat, &cc);
7be62de9
RR
1450}
1451
7103f16d 1452static void compact_node(int nid)
7be62de9 1453{
7be62de9
RR
1454 struct compact_control cc = {
1455 .order = -1,
e0b9daeb 1456 .mode = MIGRATE_SYNC,
91ca9186 1457 .ignore_skip_hint = true,
7be62de9
RR
1458 };
1459
7103f16d 1460 __compact_pgdat(NODE_DATA(nid), &cc);
7be62de9
RR
1461}
1462
76ab0f53 1463/* Compact all nodes in the system */
7964c06d 1464static void compact_nodes(void)
76ab0f53
MG
1465{
1466 int nid;
1467
8575ec29
HD
1468 /* Flush pending updates to the LRU lists */
1469 lru_add_drain_all();
1470
76ab0f53
MG
1471 for_each_online_node(nid)
1472 compact_node(nid);
76ab0f53
MG
1473}
1474
1475/* The written value is actually unused, all memory is compacted */
1476int sysctl_compact_memory;
1477
1478/* This is the entry point for compacting all nodes via /proc/sys/vm */
1479int sysctl_compaction_handler(struct ctl_table *table, int write,
1480 void __user *buffer, size_t *length, loff_t *ppos)
1481{
1482 if (write)
7964c06d 1483 compact_nodes();
76ab0f53
MG
1484
1485 return 0;
1486}
ed4a6d7f 1487
5e771905
MG
1488int sysctl_extfrag_handler(struct ctl_table *table, int write,
1489 void __user *buffer, size_t *length, loff_t *ppos)
1490{
1491 proc_dointvec_minmax(table, write, buffer, length, ppos);
1492
1493 return 0;
1494}
1495
ed4a6d7f 1496#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
74e77fb9 1497static ssize_t sysfs_compact_node(struct device *dev,
10fbcf4c 1498 struct device_attribute *attr,
ed4a6d7f
MG
1499 const char *buf, size_t count)
1500{
8575ec29
HD
1501 int nid = dev->id;
1502
1503 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1504 /* Flush pending updates to the LRU lists */
1505 lru_add_drain_all();
1506
1507 compact_node(nid);
1508 }
ed4a6d7f
MG
1509
1510 return count;
1511}
10fbcf4c 1512static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
ed4a6d7f
MG
1513
1514int compaction_register_node(struct node *node)
1515{
10fbcf4c 1516 return device_create_file(&node->dev, &dev_attr_compact);
ed4a6d7f
MG
1517}
1518
1519void compaction_unregister_node(struct node *node)
1520{
10fbcf4c 1521 return device_remove_file(&node->dev, &dev_attr_compact);
ed4a6d7f
MG
1522}
1523#endif /* CONFIG_SYSFS && CONFIG_NUMA */
ff9543fd
MN
1524
1525#endif /* CONFIG_COMPACTION */