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