mm: compaction: move fatal signal check out of compact_checklock_irqsave
[GitHub/mt8127/android_kernel_alcatel_ttab.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>
748446bb
MG
17#include "internal.h"
18
ff9543fd
MN
19#if defined CONFIG_COMPACTION || defined CONFIG_CMA
20
b7aba698
MG
21#define CREATE_TRACE_POINTS
22#include <trace/events/compaction.h>
23
748446bb
MG
24static unsigned long release_freepages(struct list_head *freelist)
25{
26 struct page *page, *next;
27 unsigned long count = 0;
28
29 list_for_each_entry_safe(page, next, freelist, lru) {
30 list_del(&page->lru);
31 __free_page(page);
32 count++;
33 }
34
35 return count;
36}
37
ff9543fd
MN
38static void map_pages(struct list_head *list)
39{
40 struct page *page;
41
42 list_for_each_entry(page, list, lru) {
43 arch_alloc_page(page, 0);
44 kernel_map_pages(page, 1, 1);
45 }
46}
47
47118af0
MN
48static inline bool migrate_async_suitable(int migratetype)
49{
50 return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
51}
52
c67fe375
MG
53/*
54 * Compaction requires the taking of some coarse locks that are potentially
55 * very heavily contended. Check if the process needs to be scheduled or
56 * if the lock is contended. For async compaction, back out in the event
57 * if contention is severe. For sync compaction, schedule.
58 *
59 * Returns true if the lock is held.
60 * Returns false if the lock is released and compaction should abort
61 */
62static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags,
63 bool locked, struct compact_control *cc)
64{
65 if (need_resched() || spin_is_contended(lock)) {
66 if (locked) {
67 spin_unlock_irqrestore(lock, *flags);
68 locked = false;
69 }
70
71 /* async aborts if taking too long or contended */
72 if (!cc->sync) {
e64c5237 73 cc->contended = true;
c67fe375
MG
74 return false;
75 }
76
77 cond_resched();
c67fe375
MG
78 }
79
80 if (!locked)
81 spin_lock_irqsave(lock, *flags);
82 return true;
83}
84
85static inline bool compact_trylock_irqsave(spinlock_t *lock,
86 unsigned long *flags, struct compact_control *cc)
87{
88 return compact_checklock_irqsave(lock, flags, false, cc);
89}
90
1fb3f8ca
MG
91static void compact_capture_page(struct compact_control *cc)
92{
93 unsigned long flags;
94 int mtype, mtype_low, mtype_high;
95
96 if (!cc->page || *cc->page)
97 return;
98
99 /*
100 * For MIGRATE_MOVABLE allocations we capture a suitable page ASAP
101 * regardless of the migratetype of the freelist is is captured from.
102 * This is fine because the order for a high-order MIGRATE_MOVABLE
103 * allocation is typically at least a pageblock size and overall
104 * fragmentation is not impaired. Other allocation types must
105 * capture pages from their own migratelist because otherwise they
106 * could pollute other pageblocks like MIGRATE_MOVABLE with
107 * difficult to move pages and making fragmentation worse overall.
108 */
109 if (cc->migratetype == MIGRATE_MOVABLE) {
110 mtype_low = 0;
111 mtype_high = MIGRATE_PCPTYPES;
112 } else {
113 mtype_low = cc->migratetype;
114 mtype_high = cc->migratetype + 1;
115 }
116
117 /* Speculatively examine the free lists without zone lock */
118 for (mtype = mtype_low; mtype < mtype_high; mtype++) {
119 int order;
120 for (order = cc->order; order < MAX_ORDER; order++) {
121 struct page *page;
122 struct free_area *area;
123 area = &(cc->zone->free_area[order]);
124 if (list_empty(&area->free_list[mtype]))
125 continue;
126
127 /* Take the lock and attempt capture of the page */
128 if (!compact_trylock_irqsave(&cc->zone->lock, &flags, cc))
129 return;
130 if (!list_empty(&area->free_list[mtype])) {
131 page = list_entry(area->free_list[mtype].next,
132 struct page, lru);
133 if (capture_free_page(page, cc->order, mtype)) {
134 spin_unlock_irqrestore(&cc->zone->lock,
135 flags);
136 *cc->page = page;
137 return;
138 }
139 }
140 spin_unlock_irqrestore(&cc->zone->lock, flags);
141 }
142 }
143}
144
85aa125f
MN
145/*
146 * Isolate free pages onto a private freelist. Caller must hold zone->lock.
147 * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
148 * pages inside of the pageblock (even though it may still end up isolating
149 * some pages).
150 */
151static unsigned long isolate_freepages_block(unsigned long blockpfn,
152 unsigned long end_pfn,
153 struct list_head *freelist,
154 bool strict)
748446bb 155{
b7aba698 156 int nr_scanned = 0, total_isolated = 0;
748446bb
MG
157 struct page *cursor;
158
748446bb
MG
159 cursor = pfn_to_page(blockpfn);
160
161 /* Isolate free pages. This assumes the block is valid */
162 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
163 int isolated, i;
164 struct page *page = cursor;
165
85aa125f
MN
166 if (!pfn_valid_within(blockpfn)) {
167 if (strict)
168 return 0;
748446bb 169 continue;
85aa125f 170 }
b7aba698 171 nr_scanned++;
748446bb 172
85aa125f
MN
173 if (!PageBuddy(page)) {
174 if (strict)
175 return 0;
748446bb 176 continue;
85aa125f 177 }
748446bb
MG
178
179 /* Found a free page, break it into order-0 pages */
180 isolated = split_free_page(page);
85aa125f
MN
181 if (!isolated && strict)
182 return 0;
748446bb
MG
183 total_isolated += isolated;
184 for (i = 0; i < isolated; i++) {
185 list_add(&page->lru, freelist);
186 page++;
187 }
188
189 /* If a page was split, advance to the end of it */
190 if (isolated) {
191 blockpfn += isolated - 1;
192 cursor += isolated - 1;
193 }
194 }
195
b7aba698 196 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
748446bb
MG
197 return total_isolated;
198}
199
85aa125f
MN
200/**
201 * isolate_freepages_range() - isolate free pages.
202 * @start_pfn: The first PFN to start isolating.
203 * @end_pfn: The one-past-last PFN.
204 *
205 * Non-free pages, invalid PFNs, or zone boundaries within the
206 * [start_pfn, end_pfn) range are considered errors, cause function to
207 * undo its actions and return zero.
208 *
209 * Otherwise, function returns one-past-the-last PFN of isolated page
210 * (which may be greater then end_pfn if end fell in a middle of
211 * a free page).
212 */
ff9543fd 213unsigned long
85aa125f
MN
214isolate_freepages_range(unsigned long start_pfn, unsigned long end_pfn)
215{
216 unsigned long isolated, pfn, block_end_pfn, flags;
217 struct zone *zone = NULL;
218 LIST_HEAD(freelist);
219
220 if (pfn_valid(start_pfn))
221 zone = page_zone(pfn_to_page(start_pfn));
222
223 for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
224 if (!pfn_valid(pfn) || zone != page_zone(pfn_to_page(pfn)))
225 break;
226
227 /*
228 * On subsequent iterations ALIGN() is actually not needed,
229 * but we keep it that we not to complicate the code.
230 */
231 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
232 block_end_pfn = min(block_end_pfn, end_pfn);
233
234 spin_lock_irqsave(&zone->lock, flags);
235 isolated = isolate_freepages_block(pfn, block_end_pfn,
236 &freelist, true);
237 spin_unlock_irqrestore(&zone->lock, flags);
238
239 /*
240 * In strict mode, isolate_freepages_block() returns 0 if
241 * there are any holes in the block (ie. invalid PFNs or
242 * non-free pages).
243 */
244 if (!isolated)
245 break;
246
247 /*
248 * If we managed to isolate pages, it is always (1 << n) *
249 * pageblock_nr_pages for some non-negative n. (Max order
250 * page may span two pageblocks).
251 */
252 }
253
254 /* split_free_page does not map the pages */
255 map_pages(&freelist);
256
257 if (pfn < end_pfn) {
258 /* Loop terminated early, cleanup. */
259 release_freepages(&freelist);
260 return 0;
261 }
262
263 /* We don't use freelists for anything. */
264 return pfn;
265}
266
748446bb 267/* Update the number of anon and file isolated pages in the zone */
c67fe375 268static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc)
748446bb
MG
269{
270 struct page *page;
b9e84ac1 271 unsigned int count[2] = { 0, };
748446bb 272
b9e84ac1
MK
273 list_for_each_entry(page, &cc->migratepages, lru)
274 count[!!page_is_file_cache(page)]++;
748446bb 275
c67fe375
MG
276 /* If locked we can use the interrupt unsafe versions */
277 if (locked) {
278 __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
279 __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
280 } else {
281 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
282 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
283 }
748446bb
MG
284}
285
286/* Similar to reclaim, but different enough that they don't share logic */
287static bool too_many_isolated(struct zone *zone)
288{
bc693045 289 unsigned long active, inactive, isolated;
748446bb
MG
290
291 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
292 zone_page_state(zone, NR_INACTIVE_ANON);
bc693045
MK
293 active = zone_page_state(zone, NR_ACTIVE_FILE) +
294 zone_page_state(zone, NR_ACTIVE_ANON);
748446bb
MG
295 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
296 zone_page_state(zone, NR_ISOLATED_ANON);
297
bc693045 298 return isolated > (inactive + active) / 2;
748446bb
MG
299}
300
2fe86e00
MN
301/**
302 * isolate_migratepages_range() - isolate all migrate-able pages in range.
303 * @zone: Zone pages are in.
304 * @cc: Compaction control structure.
305 * @low_pfn: The first PFN of the range.
306 * @end_pfn: The one-past-the-last PFN of the range.
307 *
308 * Isolate all pages that can be migrated from the range specified by
309 * [low_pfn, end_pfn). Returns zero if there is a fatal signal
310 * pending), otherwise PFN of the first page that was not scanned
311 * (which may be both less, equal to or more then end_pfn).
312 *
313 * Assumes that cc->migratepages is empty and cc->nr_migratepages is
314 * zero.
315 *
316 * Apart from cc->migratepages and cc->nr_migratetypes this function
317 * does not modify any cc's fields, in particular it does not modify
318 * (or read for that matter) cc->migrate_pfn.
748446bb 319 */
ff9543fd 320unsigned long
2fe86e00
MN
321isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
322 unsigned long low_pfn, unsigned long end_pfn)
748446bb 323{
9927af74 324 unsigned long last_pageblock_nr = 0, pageblock_nr;
b7aba698 325 unsigned long nr_scanned = 0, nr_isolated = 0;
748446bb 326 struct list_head *migratelist = &cc->migratepages;
f3fd4a61 327 isolate_mode_t mode = 0;
fa9add64 328 struct lruvec *lruvec;
c67fe375
MG
329 unsigned long flags;
330 bool locked;
748446bb 331
748446bb
MG
332 /*
333 * Ensure that there are not too many pages isolated from the LRU
334 * list by either parallel reclaimers or compaction. If there are,
335 * delay for some time until fewer pages are isolated
336 */
337 while (unlikely(too_many_isolated(zone))) {
f9e35b3b 338 /* async migration should just abort */
68e3e926 339 if (!cc->sync)
2fe86e00 340 return 0;
f9e35b3b 341
748446bb
MG
342 congestion_wait(BLK_RW_ASYNC, HZ/10);
343
344 if (fatal_signal_pending(current))
2fe86e00 345 return 0;
748446bb
MG
346 }
347
348 /* Time to isolate some pages for migration */
b2eef8c0 349 cond_resched();
c67fe375
MG
350 spin_lock_irqsave(&zone->lru_lock, flags);
351 locked = true;
748446bb
MG
352 for (; low_pfn < end_pfn; low_pfn++) {
353 struct page *page;
b2eef8c0
AA
354
355 /* give a chance to irqs before checking need_resched() */
356 if (!((low_pfn+1) % SWAP_CLUSTER_MAX)) {
c67fe375 357 spin_unlock_irqrestore(&zone->lru_lock, flags);
b2eef8c0
AA
358 locked = false;
359 }
c67fe375
MG
360
361 /* Check if it is ok to still hold the lock */
362 locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
363 locked, cc);
3cc668f4 364 if (!locked || fatal_signal_pending(current))
c67fe375 365 break;
b2eef8c0 366
0bf380bc
MG
367 /*
368 * migrate_pfn does not necessarily start aligned to a
369 * pageblock. Ensure that pfn_valid is called when moving
370 * into a new MAX_ORDER_NR_PAGES range in case of large
371 * memory holes within the zone
372 */
373 if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
374 if (!pfn_valid(low_pfn)) {
375 low_pfn += MAX_ORDER_NR_PAGES - 1;
376 continue;
377 }
378 }
379
748446bb
MG
380 if (!pfn_valid_within(low_pfn))
381 continue;
b7aba698 382 nr_scanned++;
748446bb 383
dc908600
MG
384 /*
385 * Get the page and ensure the page is within the same zone.
386 * See the comment in isolate_freepages about overlapping
387 * nodes. It is deliberate that the new zone lock is not taken
388 * as memory compaction should not move pages between nodes.
389 */
748446bb 390 page = pfn_to_page(low_pfn);
dc908600
MG
391 if (page_zone(page) != zone)
392 continue;
393
394 /* Skip if free */
748446bb
MG
395 if (PageBuddy(page))
396 continue;
397
9927af74
MG
398 /*
399 * For async migration, also only scan in MOVABLE blocks. Async
400 * migration is optimistic to see if the minimum amount of work
401 * satisfies the allocation
402 */
403 pageblock_nr = low_pfn >> pageblock_order;
68e3e926 404 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
47118af0 405 !migrate_async_suitable(get_pageblock_migratetype(page))) {
9927af74
MG
406 low_pfn += pageblock_nr_pages;
407 low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
408 last_pageblock_nr = pageblock_nr;
409 continue;
410 }
411
bc835011
AA
412 if (!PageLRU(page))
413 continue;
414
415 /*
416 * PageLRU is set, and lru_lock excludes isolation,
417 * splitting and collapsing (collapsing has already
418 * happened if PageLRU is set).
419 */
420 if (PageTransHuge(page)) {
421 low_pfn += (1 << compound_order(page)) - 1;
422 continue;
423 }
424
68e3e926 425 if (!cc->sync)
c8244935
MG
426 mode |= ISOLATE_ASYNC_MIGRATE;
427
fa9add64
HD
428 lruvec = mem_cgroup_page_lruvec(page, zone);
429
748446bb 430 /* Try isolate the page */
f3fd4a61 431 if (__isolate_lru_page(page, mode) != 0)
748446bb
MG
432 continue;
433
bc835011
AA
434 VM_BUG_ON(PageTransCompound(page));
435
748446bb 436 /* Successfully isolated */
fa9add64 437 del_page_from_lru_list(page, lruvec, page_lru(page));
748446bb 438 list_add(&page->lru, migratelist);
748446bb 439 cc->nr_migratepages++;
b7aba698 440 nr_isolated++;
748446bb
MG
441
442 /* Avoid isolating too much */
31b8384a
HD
443 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
444 ++low_pfn;
748446bb 445 break;
31b8384a 446 }
748446bb
MG
447 }
448
c67fe375 449 acct_isolated(zone, locked, cc);
748446bb 450
c67fe375
MG
451 if (locked)
452 spin_unlock_irqrestore(&zone->lru_lock, flags);
748446bb 453
b7aba698
MG
454 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
455
2fe86e00
MN
456 return low_pfn;
457}
458
ff9543fd
MN
459#endif /* CONFIG_COMPACTION || CONFIG_CMA */
460#ifdef CONFIG_COMPACTION
461
68e3e926
LT
462/* Returns true if the page is within a block suitable for migration to */
463static bool suitable_migration_target(struct page *page)
ff9543fd
MN
464{
465
466 int migratetype = get_pageblock_migratetype(page);
467
468 /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
469 if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
68e3e926 470 return false;
ff9543fd
MN
471
472 /* If the page is a large free page, then allow migration */
473 if (PageBuddy(page) && page_order(page) >= pageblock_order)
68e3e926 474 return true;
ff9543fd 475
47118af0 476 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
68e3e926
LT
477 if (migrate_async_suitable(migratetype))
478 return true;
ff9543fd
MN
479
480 /* Otherwise skip the block */
68e3e926 481 return false;
ff9543fd
MN
482}
483
de74f1cc
MG
484/*
485 * Returns the start pfn of the last page block in a zone. This is the starting
486 * point for full compaction of a zone. Compaction searches for free pages from
487 * the end of each zone, while isolate_freepages_block scans forward inside each
488 * page block.
489 */
490static unsigned long start_free_pfn(struct zone *zone)
491{
492 unsigned long free_pfn;
493 free_pfn = zone->zone_start_pfn + zone->spanned_pages;
494 free_pfn &= ~(pageblock_nr_pages-1);
495 return free_pfn;
496}
497
2fe86e00 498/*
ff9543fd
MN
499 * Based on information in the current compact_control, find blocks
500 * suitable for isolating free pages from and then isolate them.
2fe86e00 501 */
ff9543fd
MN
502static void isolate_freepages(struct zone *zone,
503 struct compact_control *cc)
2fe86e00 504{
ff9543fd
MN
505 struct page *page;
506 unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
507 unsigned long flags;
508 int nr_freepages = cc->nr_freepages;
509 struct list_head *freelist = &cc->freepages;
2fe86e00 510
ff9543fd
MN
511 /*
512 * Initialise the free scanner. The starting point is where we last
513 * scanned from (or the end of the zone if starting). The low point
514 * is the end of the pageblock the migration scanner is using.
515 */
516 pfn = cc->free_pfn;
517 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
2fe86e00 518
ff9543fd
MN
519 /*
520 * Take care that if the migration scanner is at the end of the zone
521 * that the free scanner does not accidentally move to the next zone
522 * in the next isolation cycle.
523 */
524 high_pfn = min(low_pfn, pfn);
2fe86e00 525
ff9543fd 526 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
2fe86e00 527
ff9543fd
MN
528 /*
529 * Isolate free pages until enough are available to migrate the
530 * pages on cc->migratepages. We stop searching if the migrate
531 * and free page scanners meet or enough free pages are isolated.
532 */
533 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
534 pfn -= pageblock_nr_pages) {
535 unsigned long isolated;
2fe86e00 536
ff9543fd
MN
537 if (!pfn_valid(pfn))
538 continue;
2fe86e00 539
ff9543fd
MN
540 /*
541 * Check for overlapping nodes/zones. It's possible on some
542 * configurations to have a setup like
543 * node0 node1 node0
544 * i.e. it's possible that all pages within a zones range of
545 * pages do not belong to a single zone.
546 */
547 page = pfn_to_page(pfn);
548 if (page_zone(page) != zone)
549 continue;
550
551 /* Check the block is suitable for migration */
68e3e926 552 if (!suitable_migration_target(page))
ff9543fd 553 continue;
68e3e926 554
ff9543fd
MN
555 /*
556 * Found a block suitable for isolating free pages from. Now
557 * we disabled interrupts, double check things are ok and
558 * isolate the pages. This is to minimise the time IRQs
559 * are disabled
560 */
561 isolated = 0;
c67fe375
MG
562
563 /*
564 * The zone lock must be held to isolate freepages. This
565 * unfortunately this is a very coarse lock and can be
566 * heavily contended if there are parallel allocations
567 * or parallel compactions. For async compaction do not
568 * spin on the lock
569 */
570 if (!compact_trylock_irqsave(&zone->lock, &flags, cc))
571 break;
68e3e926 572 if (suitable_migration_target(page)) {
ff9543fd
MN
573 end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn);
574 isolated = isolate_freepages_block(pfn, end_pfn,
575 freelist, false);
576 nr_freepages += isolated;
68e3e926 577 }
ff9543fd
MN
578 spin_unlock_irqrestore(&zone->lock, flags);
579
580 /*
581 * Record the highest PFN we isolated pages from. When next
582 * looking for free pages, the search will restart here as
583 * page migration may have returned some pages to the allocator
584 */
7db8889a 585 if (isolated) {
ff9543fd 586 high_pfn = max(high_pfn, pfn);
de74f1cc
MG
587
588 /*
589 * If the free scanner has wrapped, update
590 * compact_cached_free_pfn to point to the highest
591 * pageblock with free pages. This reduces excessive
592 * scanning of full pageblocks near the end of the
593 * zone
594 */
595 if (cc->order > 0 && cc->wrapped)
7db8889a
RR
596 zone->compact_cached_free_pfn = high_pfn;
597 }
ff9543fd
MN
598 }
599
600 /* split_free_page does not map the pages */
601 map_pages(freelist);
602
603 cc->free_pfn = high_pfn;
604 cc->nr_freepages = nr_freepages;
de74f1cc
MG
605
606 /* If compact_cached_free_pfn is reset then set it now */
607 if (cc->order > 0 && !cc->wrapped &&
608 zone->compact_cached_free_pfn == start_free_pfn(zone))
609 zone->compact_cached_free_pfn = high_pfn;
748446bb
MG
610}
611
612/*
613 * This is a migrate-callback that "allocates" freepages by taking pages
614 * from the isolated freelists in the block we are migrating to.
615 */
616static struct page *compaction_alloc(struct page *migratepage,
617 unsigned long data,
618 int **result)
619{
620 struct compact_control *cc = (struct compact_control *)data;
621 struct page *freepage;
622
623 /* Isolate free pages if necessary */
624 if (list_empty(&cc->freepages)) {
625 isolate_freepages(cc->zone, cc);
626
627 if (list_empty(&cc->freepages))
628 return NULL;
629 }
630
631 freepage = list_entry(cc->freepages.next, struct page, lru);
632 list_del(&freepage->lru);
633 cc->nr_freepages--;
634
635 return freepage;
636}
637
638/*
639 * We cannot control nr_migratepages and nr_freepages fully when migration is
640 * running as migrate_pages() has no knowledge of compact_control. When
641 * migration is complete, we count the number of pages on the lists by hand.
642 */
643static void update_nr_listpages(struct compact_control *cc)
644{
645 int nr_migratepages = 0;
646 int nr_freepages = 0;
647 struct page *page;
648
649 list_for_each_entry(page, &cc->migratepages, lru)
650 nr_migratepages++;
651 list_for_each_entry(page, &cc->freepages, lru)
652 nr_freepages++;
653
654 cc->nr_migratepages = nr_migratepages;
655 cc->nr_freepages = nr_freepages;
656}
657
ff9543fd
MN
658/* possible outcome of isolate_migratepages */
659typedef enum {
660 ISOLATE_ABORT, /* Abort compaction now */
661 ISOLATE_NONE, /* No pages isolated, continue scanning */
662 ISOLATE_SUCCESS, /* Pages isolated, migrate */
663} isolate_migrate_t;
664
665/*
666 * Isolate all pages that can be migrated from the block pointed to by
667 * the migrate scanner within compact_control.
668 */
669static isolate_migrate_t isolate_migratepages(struct zone *zone,
670 struct compact_control *cc)
671{
672 unsigned long low_pfn, end_pfn;
673
674 /* Do not scan outside zone boundaries */
675 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
676
677 /* Only scan within a pageblock boundary */
678 end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
679
680 /* Do not cross the free scanner or scan within a memory hole */
681 if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
682 cc->migrate_pfn = end_pfn;
683 return ISOLATE_NONE;
684 }
685
686 /* Perform the isolation */
687 low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn);
e64c5237 688 if (!low_pfn || cc->contended)
ff9543fd
MN
689 return ISOLATE_ABORT;
690
691 cc->migrate_pfn = low_pfn;
692
693 return ISOLATE_SUCCESS;
694}
695
748446bb 696static int compact_finished(struct zone *zone,
5a03b051 697 struct compact_control *cc)
748446bb 698{
5a03b051 699 unsigned long watermark;
56de7263 700
748446bb
MG
701 if (fatal_signal_pending(current))
702 return COMPACT_PARTIAL;
703
7db8889a
RR
704 /*
705 * A full (order == -1) compaction run starts at the beginning and
706 * end of a zone; it completes when the migrate and free scanner meet.
707 * A partial (order > 0) compaction can start with the free scanner
708 * at a random point in the zone, and may have to restart.
709 */
710 if (cc->free_pfn <= cc->migrate_pfn) {
711 if (cc->order > 0 && !cc->wrapped) {
712 /* We started partway through; restart at the end. */
713 unsigned long free_pfn = start_free_pfn(zone);
714 zone->compact_cached_free_pfn = free_pfn;
715 cc->free_pfn = free_pfn;
716 cc->wrapped = 1;
717 return COMPACT_CONTINUE;
718 }
719 return COMPACT_COMPLETE;
720 }
721
722 /* We wrapped around and ended up where we started. */
723 if (cc->wrapped && cc->free_pfn <= cc->start_free_pfn)
748446bb
MG
724 return COMPACT_COMPLETE;
725
82478fb7
JW
726 /*
727 * order == -1 is expected when compacting via
728 * /proc/sys/vm/compact_memory
729 */
56de7263
MG
730 if (cc->order == -1)
731 return COMPACT_CONTINUE;
732
3957c776
MH
733 /* Compaction run is not finished if the watermark is not met */
734 watermark = low_wmark_pages(zone);
735 watermark += (1 << cc->order);
736
737 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
738 return COMPACT_CONTINUE;
739
56de7263 740 /* Direct compactor: Is a suitable page free? */
1fb3f8ca
MG
741 if (cc->page) {
742 /* Was a suitable page captured? */
743 if (*cc->page)
56de7263 744 return COMPACT_PARTIAL;
1fb3f8ca
MG
745 } else {
746 unsigned int order;
747 for (order = cc->order; order < MAX_ORDER; order++) {
748 struct free_area *area = &zone->free_area[cc->order];
749 /* Job done if page is free of the right migratetype */
750 if (!list_empty(&area->free_list[cc->migratetype]))
751 return COMPACT_PARTIAL;
752
753 /* Job done if allocation would set block type */
754 if (cc->order >= pageblock_order && area->nr_free)
755 return COMPACT_PARTIAL;
756 }
56de7263
MG
757 }
758
748446bb
MG
759 return COMPACT_CONTINUE;
760}
761
3e7d3449
MG
762/*
763 * compaction_suitable: Is this suitable to run compaction on this zone now?
764 * Returns
765 * COMPACT_SKIPPED - If there are too few free pages for compaction
766 * COMPACT_PARTIAL - If the allocation would succeed without compaction
767 * COMPACT_CONTINUE - If compaction should run now
768 */
769unsigned long compaction_suitable(struct zone *zone, int order)
770{
771 int fragindex;
772 unsigned long watermark;
773
3957c776
MH
774 /*
775 * order == -1 is expected when compacting via
776 * /proc/sys/vm/compact_memory
777 */
778 if (order == -1)
779 return COMPACT_CONTINUE;
780
3e7d3449
MG
781 /*
782 * Watermarks for order-0 must be met for compaction. Note the 2UL.
783 * This is because during migration, copies of pages need to be
784 * allocated and for a short time, the footprint is higher
785 */
786 watermark = low_wmark_pages(zone) + (2UL << order);
787 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
788 return COMPACT_SKIPPED;
789
790 /*
791 * fragmentation index determines if allocation failures are due to
792 * low memory or external fragmentation
793 *
a582a738
SL
794 * index of -1000 implies allocations might succeed depending on
795 * watermarks
3e7d3449
MG
796 * index towards 0 implies failure is due to lack of memory
797 * index towards 1000 implies failure is due to fragmentation
798 *
799 * Only compact if a failure would be due to fragmentation.
800 */
801 fragindex = fragmentation_index(zone, order);
802 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
803 return COMPACT_SKIPPED;
804
a582a738
SL
805 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
806 0, 0))
3e7d3449
MG
807 return COMPACT_PARTIAL;
808
809 return COMPACT_CONTINUE;
810}
811
748446bb
MG
812static int compact_zone(struct zone *zone, struct compact_control *cc)
813{
814 int ret;
815
3e7d3449
MG
816 ret = compaction_suitable(zone, cc->order);
817 switch (ret) {
818 case COMPACT_PARTIAL:
819 case COMPACT_SKIPPED:
820 /* Compaction is likely to fail */
821 return ret;
822 case COMPACT_CONTINUE:
823 /* Fall through to compaction */
824 ;
825 }
826
748446bb
MG
827 /* Setup to move all movable pages to the end of the zone */
828 cc->migrate_pfn = zone->zone_start_pfn;
7db8889a
RR
829
830 if (cc->order > 0) {
831 /* Incremental compaction. Start where the last one stopped. */
832 cc->free_pfn = zone->compact_cached_free_pfn;
833 cc->start_free_pfn = cc->free_pfn;
834 } else {
835 /* Order == -1 starts at the end of the zone. */
836 cc->free_pfn = start_free_pfn(zone);
837 }
748446bb
MG
838
839 migrate_prep_local();
840
841 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
842 unsigned long nr_migrate, nr_remaining;
9d502c1c 843 int err;
748446bb 844
f9e35b3b
MG
845 switch (isolate_migratepages(zone, cc)) {
846 case ISOLATE_ABORT:
847 ret = COMPACT_PARTIAL;
e64c5237
SL
848 putback_lru_pages(&cc->migratepages);
849 cc->nr_migratepages = 0;
f9e35b3b
MG
850 goto out;
851 case ISOLATE_NONE:
748446bb 852 continue;
f9e35b3b
MG
853 case ISOLATE_SUCCESS:
854 ;
855 }
748446bb
MG
856
857 nr_migrate = cc->nr_migratepages;
9d502c1c 858 err = migrate_pages(&cc->migratepages, compaction_alloc,
68e3e926
LT
859 (unsigned long)cc, false,
860 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
748446bb
MG
861 update_nr_listpages(cc);
862 nr_remaining = cc->nr_migratepages;
863
864 count_vm_event(COMPACTBLOCKS);
865 count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
866 if (nr_remaining)
867 count_vm_events(COMPACTPAGEFAILED, nr_remaining);
b7aba698
MG
868 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
869 nr_remaining);
748446bb
MG
870
871 /* Release LRU pages not migrated */
9d502c1c 872 if (err) {
748446bb
MG
873 putback_lru_pages(&cc->migratepages);
874 cc->nr_migratepages = 0;
4bf2bba3
DR
875 if (err == -ENOMEM) {
876 ret = COMPACT_PARTIAL;
877 goto out;
878 }
748446bb 879 }
1fb3f8ca
MG
880
881 /* Capture a page now if it is a suitable size */
882 compact_capture_page(cc);
748446bb
MG
883 }
884
f9e35b3b 885out:
748446bb
MG
886 /* Release free pages and check accounting */
887 cc->nr_freepages -= release_freepages(&cc->freepages);
888 VM_BUG_ON(cc->nr_freepages != 0);
889
890 return ret;
891}
76ab0f53 892
d43a87e6 893static unsigned long compact_zone_order(struct zone *zone,
5a03b051 894 int order, gfp_t gfp_mask,
1fb3f8ca
MG
895 bool sync, bool *contended,
896 struct page **page)
56de7263 897{
e64c5237 898 unsigned long ret;
56de7263
MG
899 struct compact_control cc = {
900 .nr_freepages = 0,
901 .nr_migratepages = 0,
902 .order = order,
903 .migratetype = allocflags_to_migratetype(gfp_mask),
904 .zone = zone,
68e3e926 905 .sync = sync,
1fb3f8ca 906 .page = page,
56de7263
MG
907 };
908 INIT_LIST_HEAD(&cc.freepages);
909 INIT_LIST_HEAD(&cc.migratepages);
910
e64c5237
SL
911 ret = compact_zone(zone, &cc);
912
913 VM_BUG_ON(!list_empty(&cc.freepages));
914 VM_BUG_ON(!list_empty(&cc.migratepages));
915
916 *contended = cc.contended;
917 return ret;
56de7263
MG
918}
919
5e771905
MG
920int sysctl_extfrag_threshold = 500;
921
56de7263
MG
922/**
923 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
924 * @zonelist: The zonelist used for the current allocation
925 * @order: The order of the current allocation
926 * @gfp_mask: The GFP mask of the current allocation
927 * @nodemask: The allowed nodes to allocate from
77f1fe6b 928 * @sync: Whether migration is synchronous or not
56de7263
MG
929 *
930 * This is the main entry point for direct page compaction.
931 */
932unsigned long try_to_compact_pages(struct zonelist *zonelist,
77f1fe6b 933 int order, gfp_t gfp_mask, nodemask_t *nodemask,
1fb3f8ca 934 bool sync, bool *contended, struct page **page)
56de7263
MG
935{
936 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
937 int may_enter_fs = gfp_mask & __GFP_FS;
938 int may_perform_io = gfp_mask & __GFP_IO;
56de7263
MG
939 struct zoneref *z;
940 struct zone *zone;
941 int rc = COMPACT_SKIPPED;
d95ea5d1 942 int alloc_flags = 0;
56de7263 943
4ffb6335 944 /* Check if the GFP flags allow compaction */
c5a73c3d 945 if (!order || !may_enter_fs || !may_perform_io)
56de7263
MG
946 return rc;
947
948 count_vm_event(COMPACTSTALL);
949
d95ea5d1
BZ
950#ifdef CONFIG_CMA
951 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
952 alloc_flags |= ALLOC_CMA;
953#endif
56de7263
MG
954 /* Compact each zone in the list */
955 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
956 nodemask) {
56de7263
MG
957 int status;
958
c67fe375 959 status = compact_zone_order(zone, order, gfp_mask, sync,
1fb3f8ca 960 contended, page);
56de7263
MG
961 rc = max(status, rc);
962
3e7d3449 963 /* If a normal allocation would succeed, stop compacting */
d95ea5d1
BZ
964 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
965 alloc_flags))
56de7263
MG
966 break;
967 }
968
969 return rc;
970}
971
972
76ab0f53 973/* Compact all zones within a node */
7be62de9 974static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
76ab0f53
MG
975{
976 int zoneid;
76ab0f53
MG
977 struct zone *zone;
978
76ab0f53 979 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
76ab0f53
MG
980
981 zone = &pgdat->node_zones[zoneid];
982 if (!populated_zone(zone))
983 continue;
984
7be62de9
RR
985 cc->nr_freepages = 0;
986 cc->nr_migratepages = 0;
987 cc->zone = zone;
988 INIT_LIST_HEAD(&cc->freepages);
989 INIT_LIST_HEAD(&cc->migratepages);
76ab0f53 990
aad6ec37 991 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
7be62de9 992 compact_zone(zone, cc);
76ab0f53 993
aff62249
RR
994 if (cc->order > 0) {
995 int ok = zone_watermark_ok(zone, cc->order,
996 low_wmark_pages(zone), 0, 0);
c81758fb 997 if (ok && cc->order >= zone->compact_order_failed)
aff62249
RR
998 zone->compact_order_failed = cc->order + 1;
999 /* Currently async compaction is never deferred. */
68e3e926 1000 else if (!ok && cc->sync)
aff62249
RR
1001 defer_compaction(zone, cc->order);
1002 }
1003
7be62de9
RR
1004 VM_BUG_ON(!list_empty(&cc->freepages));
1005 VM_BUG_ON(!list_empty(&cc->migratepages));
76ab0f53
MG
1006 }
1007
1008 return 0;
1009}
1010
7be62de9
RR
1011int compact_pgdat(pg_data_t *pgdat, int order)
1012{
1013 struct compact_control cc = {
1014 .order = order,
68e3e926 1015 .sync = false,
1fb3f8ca 1016 .page = NULL,
7be62de9
RR
1017 };
1018
1019 return __compact_pgdat(pgdat, &cc);
1020}
1021
1022static int compact_node(int nid)
1023{
7be62de9
RR
1024 struct compact_control cc = {
1025 .order = -1,
68e3e926 1026 .sync = true,
1fb3f8ca 1027 .page = NULL,
7be62de9
RR
1028 };
1029
8575ec29 1030 return __compact_pgdat(NODE_DATA(nid), &cc);
7be62de9
RR
1031}
1032
76ab0f53
MG
1033/* Compact all nodes in the system */
1034static int compact_nodes(void)
1035{
1036 int nid;
1037
8575ec29
HD
1038 /* Flush pending updates to the LRU lists */
1039 lru_add_drain_all();
1040
76ab0f53
MG
1041 for_each_online_node(nid)
1042 compact_node(nid);
1043
1044 return COMPACT_COMPLETE;
1045}
1046
1047/* The written value is actually unused, all memory is compacted */
1048int sysctl_compact_memory;
1049
1050/* This is the entry point for compacting all nodes via /proc/sys/vm */
1051int sysctl_compaction_handler(struct ctl_table *table, int write,
1052 void __user *buffer, size_t *length, loff_t *ppos)
1053{
1054 if (write)
1055 return compact_nodes();
1056
1057 return 0;
1058}
ed4a6d7f 1059
5e771905
MG
1060int sysctl_extfrag_handler(struct ctl_table *table, int write,
1061 void __user *buffer, size_t *length, loff_t *ppos)
1062{
1063 proc_dointvec_minmax(table, write, buffer, length, ppos);
1064
1065 return 0;
1066}
1067
ed4a6d7f 1068#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
10fbcf4c
KS
1069ssize_t sysfs_compact_node(struct device *dev,
1070 struct device_attribute *attr,
ed4a6d7f
MG
1071 const char *buf, size_t count)
1072{
8575ec29
HD
1073 int nid = dev->id;
1074
1075 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1076 /* Flush pending updates to the LRU lists */
1077 lru_add_drain_all();
1078
1079 compact_node(nid);
1080 }
ed4a6d7f
MG
1081
1082 return count;
1083}
10fbcf4c 1084static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
ed4a6d7f
MG
1085
1086int compaction_register_node(struct node *node)
1087{
10fbcf4c 1088 return device_create_file(&node->dev, &dev_attr_compact);
ed4a6d7f
MG
1089}
1090
1091void compaction_unregister_node(struct node *node)
1092{
10fbcf4c 1093 return device_remove_file(&node->dev, &dev_attr_compact);
ed4a6d7f
MG
1094}
1095#endif /* CONFIG_SYSFS && CONFIG_NUMA */
ff9543fd
MN
1096
1097#endif /* CONFIG_COMPACTION */