mm: compaction: Update try_to_compact_pages()kerneldoc comment
[deliverable/linux.git] / mm / compaction.c
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>
15 #include <linux/sysctl.h>
16 #include <linux/sysfs.h>
17 #include "internal.h"
18
19 #if defined CONFIG_COMPACTION || defined CONFIG_CMA
20
21 #define CREATE_TRACE_POINTS
22 #include <trace/events/compaction.h>
23
24 static 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
38 static 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
48 static inline bool migrate_async_suitable(int migratetype)
49 {
50 return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
51 }
52
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 */
62 static 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) {
73 cc->contended = true;
74 return false;
75 }
76
77 cond_resched();
78 }
79
80 if (!locked)
81 spin_lock_irqsave(lock, *flags);
82 return true;
83 }
84
85 static 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
91 static 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
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 */
151 static unsigned long isolate_freepages_block(unsigned long blockpfn,
152 unsigned long end_pfn,
153 struct list_head *freelist,
154 bool strict)
155 {
156 int nr_scanned = 0, total_isolated = 0;
157 struct page *cursor;
158
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
166 if (!pfn_valid_within(blockpfn)) {
167 if (strict)
168 return 0;
169 continue;
170 }
171 nr_scanned++;
172
173 if (!PageBuddy(page)) {
174 if (strict)
175 return 0;
176 continue;
177 }
178
179 /* Found a free page, break it into order-0 pages */
180 isolated = split_free_page(page);
181 if (!isolated && strict)
182 return 0;
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
196 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
197 return total_isolated;
198 }
199
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 */
213 unsigned long
214 isolate_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
267 /* Update the number of anon and file isolated pages in the zone */
268 static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc)
269 {
270 struct page *page;
271 unsigned int count[2] = { 0, };
272
273 list_for_each_entry(page, &cc->migratepages, lru)
274 count[!!page_is_file_cache(page)]++;
275
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 }
284 }
285
286 /* Similar to reclaim, but different enough that they don't share logic */
287 static bool too_many_isolated(struct zone *zone)
288 {
289 unsigned long active, inactive, isolated;
290
291 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
292 zone_page_state(zone, NR_INACTIVE_ANON);
293 active = zone_page_state(zone, NR_ACTIVE_FILE) +
294 zone_page_state(zone, NR_ACTIVE_ANON);
295 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
296 zone_page_state(zone, NR_ISOLATED_ANON);
297
298 return isolated > (inactive + active) / 2;
299 }
300
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.
319 */
320 unsigned long
321 isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
322 unsigned long low_pfn, unsigned long end_pfn)
323 {
324 unsigned long last_pageblock_nr = 0, pageblock_nr;
325 unsigned long nr_scanned = 0, nr_isolated = 0;
326 struct list_head *migratelist = &cc->migratepages;
327 isolate_mode_t mode = 0;
328 struct lruvec *lruvec;
329 unsigned long flags;
330 bool locked;
331
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))) {
338 /* async migration should just abort */
339 if (!cc->sync)
340 return 0;
341
342 congestion_wait(BLK_RW_ASYNC, HZ/10);
343
344 if (fatal_signal_pending(current))
345 return 0;
346 }
347
348 /* Time to isolate some pages for migration */
349 cond_resched();
350 spin_lock_irqsave(&zone->lru_lock, flags);
351 locked = true;
352 for (; low_pfn < end_pfn; low_pfn++) {
353 struct page *page;
354
355 /* give a chance to irqs before checking need_resched() */
356 if (!((low_pfn+1) % SWAP_CLUSTER_MAX)) {
357 spin_unlock_irqrestore(&zone->lru_lock, flags);
358 locked = false;
359 }
360
361 /* Check if it is ok to still hold the lock */
362 locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
363 locked, cc);
364 if (!locked || fatal_signal_pending(current))
365 break;
366
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
380 if (!pfn_valid_within(low_pfn))
381 continue;
382 nr_scanned++;
383
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 */
390 page = pfn_to_page(low_pfn);
391 if (page_zone(page) != zone)
392 continue;
393
394 /* Skip if free */
395 if (PageBuddy(page))
396 continue;
397
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;
404 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
405 !migrate_async_suitable(get_pageblock_migratetype(page))) {
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
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
425 if (!cc->sync)
426 mode |= ISOLATE_ASYNC_MIGRATE;
427
428 lruvec = mem_cgroup_page_lruvec(page, zone);
429
430 /* Try isolate the page */
431 if (__isolate_lru_page(page, mode) != 0)
432 continue;
433
434 VM_BUG_ON(PageTransCompound(page));
435
436 /* Successfully isolated */
437 del_page_from_lru_list(page, lruvec, page_lru(page));
438 list_add(&page->lru, migratelist);
439 cc->nr_migratepages++;
440 nr_isolated++;
441
442 /* Avoid isolating too much */
443 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
444 ++low_pfn;
445 break;
446 }
447 }
448
449 acct_isolated(zone, locked, cc);
450
451 if (locked)
452 spin_unlock_irqrestore(&zone->lru_lock, flags);
453
454 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
455
456 return low_pfn;
457 }
458
459 #endif /* CONFIG_COMPACTION || CONFIG_CMA */
460 #ifdef CONFIG_COMPACTION
461
462 /* Returns true if the page is within a block suitable for migration to */
463 static bool suitable_migration_target(struct page *page)
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)
470 return false;
471
472 /* If the page is a large free page, then allow migration */
473 if (PageBuddy(page) && page_order(page) >= pageblock_order)
474 return true;
475
476 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
477 if (migrate_async_suitable(migratetype))
478 return true;
479
480 /* Otherwise skip the block */
481 return false;
482 }
483
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 */
490 static 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
498 /*
499 * Based on information in the current compact_control, find blocks
500 * suitable for isolating free pages from and then isolate them.
501 */
502 static void isolate_freepages(struct zone *zone,
503 struct compact_control *cc)
504 {
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;
510
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;
518
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);
525
526 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
527
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;
536
537 if (!pfn_valid(pfn))
538 continue;
539
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 */
552 if (!suitable_migration_target(page))
553 continue;
554
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;
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;
572 if (suitable_migration_target(page)) {
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;
577 }
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 */
585 if (isolated) {
586 high_pfn = max(high_pfn, pfn);
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)
596 zone->compact_cached_free_pfn = high_pfn;
597 }
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;
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;
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 */
616 static 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 */
643 static 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
658 /* possible outcome of isolate_migratepages */
659 typedef 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 */
669 static 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);
688 if (!low_pfn || cc->contended)
689 return ISOLATE_ABORT;
690
691 cc->migrate_pfn = low_pfn;
692
693 return ISOLATE_SUCCESS;
694 }
695
696 static int compact_finished(struct zone *zone,
697 struct compact_control *cc)
698 {
699 unsigned long watermark;
700
701 if (fatal_signal_pending(current))
702 return COMPACT_PARTIAL;
703
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)
724 return COMPACT_COMPLETE;
725
726 /*
727 * order == -1 is expected when compacting via
728 * /proc/sys/vm/compact_memory
729 */
730 if (cc->order == -1)
731 return COMPACT_CONTINUE;
732
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
740 /* Direct compactor: Is a suitable page free? */
741 if (cc->page) {
742 /* Was a suitable page captured? */
743 if (*cc->page)
744 return COMPACT_PARTIAL;
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 }
757 }
758
759 return COMPACT_CONTINUE;
760 }
761
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 */
769 unsigned long compaction_suitable(struct zone *zone, int order)
770 {
771 int fragindex;
772 unsigned long watermark;
773
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
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 *
794 * index of -1000 implies allocations might succeed depending on
795 * watermarks
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
805 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
806 0, 0))
807 return COMPACT_PARTIAL;
808
809 return COMPACT_CONTINUE;
810 }
811
812 static int compact_zone(struct zone *zone, struct compact_control *cc)
813 {
814 int ret;
815
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
827 /* Setup to move all movable pages to the end of the zone */
828 cc->migrate_pfn = zone->zone_start_pfn;
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 }
838
839 migrate_prep_local();
840
841 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
842 unsigned long nr_migrate, nr_remaining;
843 int err;
844
845 switch (isolate_migratepages(zone, cc)) {
846 case ISOLATE_ABORT:
847 ret = COMPACT_PARTIAL;
848 putback_lru_pages(&cc->migratepages);
849 cc->nr_migratepages = 0;
850 goto out;
851 case ISOLATE_NONE:
852 continue;
853 case ISOLATE_SUCCESS:
854 ;
855 }
856
857 nr_migrate = cc->nr_migratepages;
858 err = migrate_pages(&cc->migratepages, compaction_alloc,
859 (unsigned long)cc, false,
860 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
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);
868 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
869 nr_remaining);
870
871 /* Release LRU pages not migrated */
872 if (err) {
873 putback_lru_pages(&cc->migratepages);
874 cc->nr_migratepages = 0;
875 if (err == -ENOMEM) {
876 ret = COMPACT_PARTIAL;
877 goto out;
878 }
879 }
880
881 /* Capture a page now if it is a suitable size */
882 compact_capture_page(cc);
883 }
884
885 out:
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 }
892
893 static unsigned long compact_zone_order(struct zone *zone,
894 int order, gfp_t gfp_mask,
895 bool sync, bool *contended,
896 struct page **page)
897 {
898 unsigned long ret;
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,
905 .sync = sync,
906 .page = page,
907 };
908 INIT_LIST_HEAD(&cc.freepages);
909 INIT_LIST_HEAD(&cc.migratepages);
910
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;
918 }
919
920 int sysctl_extfrag_threshold = 500;
921
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
928 * @sync: Whether migration is synchronous or not
929 * @contended: Return value that is true if compaction was aborted due to lock contention
930 * @page: Optionally capture a free page of the requested order during compaction
931 *
932 * This is the main entry point for direct page compaction.
933 */
934 unsigned long try_to_compact_pages(struct zonelist *zonelist,
935 int order, gfp_t gfp_mask, nodemask_t *nodemask,
936 bool sync, bool *contended, struct page **page)
937 {
938 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
939 int may_enter_fs = gfp_mask & __GFP_FS;
940 int may_perform_io = gfp_mask & __GFP_IO;
941 struct zoneref *z;
942 struct zone *zone;
943 int rc = COMPACT_SKIPPED;
944 int alloc_flags = 0;
945
946 /* Check if the GFP flags allow compaction */
947 if (!order || !may_enter_fs || !may_perform_io)
948 return rc;
949
950 count_vm_event(COMPACTSTALL);
951
952 #ifdef CONFIG_CMA
953 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
954 alloc_flags |= ALLOC_CMA;
955 #endif
956 /* Compact each zone in the list */
957 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
958 nodemask) {
959 int status;
960
961 status = compact_zone_order(zone, order, gfp_mask, sync,
962 contended, page);
963 rc = max(status, rc);
964
965 /* If a normal allocation would succeed, stop compacting */
966 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
967 alloc_flags))
968 break;
969 }
970
971 return rc;
972 }
973
974
975 /* Compact all zones within a node */
976 static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
977 {
978 int zoneid;
979 struct zone *zone;
980
981 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
982
983 zone = &pgdat->node_zones[zoneid];
984 if (!populated_zone(zone))
985 continue;
986
987 cc->nr_freepages = 0;
988 cc->nr_migratepages = 0;
989 cc->zone = zone;
990 INIT_LIST_HEAD(&cc->freepages);
991 INIT_LIST_HEAD(&cc->migratepages);
992
993 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
994 compact_zone(zone, cc);
995
996 if (cc->order > 0) {
997 int ok = zone_watermark_ok(zone, cc->order,
998 low_wmark_pages(zone), 0, 0);
999 if (ok && cc->order >= zone->compact_order_failed)
1000 zone->compact_order_failed = cc->order + 1;
1001 /* Currently async compaction is never deferred. */
1002 else if (!ok && cc->sync)
1003 defer_compaction(zone, cc->order);
1004 }
1005
1006 VM_BUG_ON(!list_empty(&cc->freepages));
1007 VM_BUG_ON(!list_empty(&cc->migratepages));
1008 }
1009
1010 return 0;
1011 }
1012
1013 int compact_pgdat(pg_data_t *pgdat, int order)
1014 {
1015 struct compact_control cc = {
1016 .order = order,
1017 .sync = false,
1018 .page = NULL,
1019 };
1020
1021 return __compact_pgdat(pgdat, &cc);
1022 }
1023
1024 static int compact_node(int nid)
1025 {
1026 struct compact_control cc = {
1027 .order = -1,
1028 .sync = true,
1029 .page = NULL,
1030 };
1031
1032 return __compact_pgdat(NODE_DATA(nid), &cc);
1033 }
1034
1035 /* Compact all nodes in the system */
1036 static int compact_nodes(void)
1037 {
1038 int nid;
1039
1040 /* Flush pending updates to the LRU lists */
1041 lru_add_drain_all();
1042
1043 for_each_online_node(nid)
1044 compact_node(nid);
1045
1046 return COMPACT_COMPLETE;
1047 }
1048
1049 /* The written value is actually unused, all memory is compacted */
1050 int sysctl_compact_memory;
1051
1052 /* This is the entry point for compacting all nodes via /proc/sys/vm */
1053 int sysctl_compaction_handler(struct ctl_table *table, int write,
1054 void __user *buffer, size_t *length, loff_t *ppos)
1055 {
1056 if (write)
1057 return compact_nodes();
1058
1059 return 0;
1060 }
1061
1062 int sysctl_extfrag_handler(struct ctl_table *table, int write,
1063 void __user *buffer, size_t *length, loff_t *ppos)
1064 {
1065 proc_dointvec_minmax(table, write, buffer, length, ppos);
1066
1067 return 0;
1068 }
1069
1070 #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
1071 ssize_t sysfs_compact_node(struct device *dev,
1072 struct device_attribute *attr,
1073 const char *buf, size_t count)
1074 {
1075 int nid = dev->id;
1076
1077 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1078 /* Flush pending updates to the LRU lists */
1079 lru_add_drain_all();
1080
1081 compact_node(nid);
1082 }
1083
1084 return count;
1085 }
1086 static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
1087
1088 int compaction_register_node(struct node *node)
1089 {
1090 return device_create_file(&node->dev, &dev_attr_compact);
1091 }
1092
1093 void compaction_unregister_node(struct node *node)
1094 {
1095 return device_remove_file(&node->dev, &dev_attr_compact);
1096 }
1097 #endif /* CONFIG_SYSFS && CONFIG_NUMA */
1098
1099 #endif /* CONFIG_COMPACTION */
This page took 0.052028 seconds and 6 git commands to generate.