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