2 * linux/mm/compaction.c
4 * Memory compaction for the reduction of external fragmentation. Note that
5 * this heavily depends upon page migration to do all the real heavy
8 * Copyright IBM Corp. 2007-2010 Mel Gorman <mel@csn.ul.ie>
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>
19 #if defined CONFIG_COMPACTION || defined CONFIG_CMA
21 #define CREATE_TRACE_POINTS
22 #include <trace/events/compaction.h>
24 static unsigned long release_freepages(struct list_head *freelist)
26 struct page *page, *next;
27 unsigned long count = 0;
29 list_for_each_entry_safe(page, next, freelist, lru) {
38 static void map_pages(struct list_head *list)
42 list_for_each_entry(page, list, lru) {
43 arch_alloc_page(page, 0);
44 kernel_map_pages(page, 1, 1);
48 static inline bool migrate_async_suitable(int migratetype)
50 return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
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.
59 * Returns true if the lock is held.
60 * Returns false if the lock is released and compaction should abort
62 static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags,
63 bool locked, struct compact_control *cc)
65 if (need_resched() || spin_is_contended(lock)) {
67 spin_unlock_irqrestore(lock, *flags);
71 /* async aborts if taking too long or contended */
81 spin_lock_irqsave(lock, *flags);
85 static inline bool compact_trylock_irqsave(spinlock_t *lock,
86 unsigned long *flags, struct compact_control *cc)
88 return compact_checklock_irqsave(lock, flags, false, cc);
91 static void compact_capture_page(struct compact_control *cc)
94 int mtype, mtype_low, mtype_high;
96 if (!cc->page || *cc->page)
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.
109 if (cc->migratetype == MIGRATE_MOVABLE) {
111 mtype_high = MIGRATE_PCPTYPES;
113 mtype_low = cc->migratetype;
114 mtype_high = cc->migratetype + 1;
117 /* Speculatively examine the free lists without zone lock */
118 for (mtype = mtype_low; mtype < mtype_high; mtype++) {
120 for (order = cc->order; order < MAX_ORDER; order++) {
122 struct free_area *area;
123 area = &(cc->zone->free_area[order]);
124 if (list_empty(&area->free_list[mtype]))
127 /* Take the lock and attempt capture of the page */
128 if (!compact_trylock_irqsave(&cc->zone->lock, &flags, cc))
130 if (!list_empty(&area->free_list[mtype])) {
131 page = list_entry(area->free_list[mtype].next,
133 if (capture_free_page(page, cc->order, mtype)) {
134 spin_unlock_irqrestore(&cc->zone->lock,
140 spin_unlock_irqrestore(&cc->zone->lock, flags);
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
151 static unsigned long isolate_freepages_block(unsigned long blockpfn,
152 unsigned long end_pfn,
153 struct list_head *freelist,
156 int nr_scanned = 0, total_isolated = 0;
159 cursor = pfn_to_page(blockpfn);
161 /* Isolate free pages. This assumes the block is valid */
162 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
164 struct page *page = cursor;
166 if (!pfn_valid_within(blockpfn)) {
173 if (!PageBuddy(page)) {
179 /* Found a free page, break it into order-0 pages */
180 isolated = split_free_page(page);
181 if (!isolated && strict)
183 total_isolated += isolated;
184 for (i = 0; i < isolated; i++) {
185 list_add(&page->lru, freelist);
189 /* If a page was split, advance to the end of it */
191 blockpfn += isolated - 1;
192 cursor += isolated - 1;
196 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
197 return total_isolated;
201 * isolate_freepages_range() - isolate free pages.
202 * @start_pfn: The first PFN to start isolating.
203 * @end_pfn: The one-past-last PFN.
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.
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
214 isolate_freepages_range(unsigned long start_pfn, unsigned long end_pfn)
216 unsigned long isolated, pfn, block_end_pfn, flags;
217 struct zone *zone = NULL;
220 if (pfn_valid(start_pfn))
221 zone = page_zone(pfn_to_page(start_pfn));
223 for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
224 if (!pfn_valid(pfn) || zone != page_zone(pfn_to_page(pfn)))
228 * On subsequent iterations ALIGN() is actually not needed,
229 * but we keep it that we not to complicate the code.
231 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
232 block_end_pfn = min(block_end_pfn, end_pfn);
234 spin_lock_irqsave(&zone->lock, flags);
235 isolated = isolate_freepages_block(pfn, block_end_pfn,
237 spin_unlock_irqrestore(&zone->lock, flags);
240 * In strict mode, isolate_freepages_block() returns 0 if
241 * there are any holes in the block (ie. invalid PFNs or
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).
254 /* split_free_page does not map the pages */
255 map_pages(&freelist);
258 /* Loop terminated early, cleanup. */
259 release_freepages(&freelist);
263 /* We don't use freelists for anything. */
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)
271 unsigned int count[2] = { 0, };
273 list_for_each_entry(page, &cc->migratepages, lru)
274 count[!!page_is_file_cache(page)]++;
276 /* If locked we can use the interrupt unsafe versions */
278 __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
279 __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
281 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
282 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
286 /* Similar to reclaim, but different enough that they don't share logic */
287 static bool too_many_isolated(struct zone *zone)
289 unsigned long active, inactive, isolated;
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);
298 return isolated > (inactive + active) / 2;
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.
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).
313 * Assumes that cc->migratepages is empty and cc->nr_migratepages is
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.
321 isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
322 unsigned long low_pfn, unsigned long end_pfn)
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;
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
337 while (unlikely(too_many_isolated(zone))) {
338 /* async migration should just abort */
342 congestion_wait(BLK_RW_ASYNC, HZ/10);
344 if (fatal_signal_pending(current))
348 /* Time to isolate some pages for migration */
350 spin_lock_irqsave(&zone->lru_lock, flags);
352 for (; low_pfn < end_pfn; low_pfn++) {
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);
361 /* Check if it is ok to still hold the lock */
362 locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
364 if (!locked || fatal_signal_pending(current))
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
373 if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
374 if (!pfn_valid(low_pfn)) {
375 low_pfn += MAX_ORDER_NR_PAGES - 1;
380 if (!pfn_valid_within(low_pfn))
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.
390 page = pfn_to_page(low_pfn);
391 if (page_zone(page) != zone)
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
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;
416 * PageLRU is set, and lru_lock excludes isolation,
417 * splitting and collapsing (collapsing has already
418 * happened if PageLRU is set).
420 if (PageTransHuge(page)) {
421 low_pfn += (1 << compound_order(page)) - 1;
426 mode |= ISOLATE_ASYNC_MIGRATE;
428 lruvec = mem_cgroup_page_lruvec(page, zone);
430 /* Try isolate the page */
431 if (__isolate_lru_page(page, mode) != 0)
434 VM_BUG_ON(PageTransCompound(page));
436 /* Successfully isolated */
437 del_page_from_lru_list(page, lruvec, page_lru(page));
438 list_add(&page->lru, migratelist);
439 cc->nr_migratepages++;
442 /* Avoid isolating too much */
443 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
449 acct_isolated(zone, locked, cc);
452 spin_unlock_irqrestore(&zone->lru_lock, flags);
454 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
459 #endif /* CONFIG_COMPACTION || CONFIG_CMA */
460 #ifdef CONFIG_COMPACTION
462 /* Returns true if the page is within a block suitable for migration to */
463 static bool suitable_migration_target(struct page *page)
466 int migratetype = get_pageblock_migratetype(page);
468 /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
469 if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
472 /* If the page is a large free page, then allow migration */
473 if (PageBuddy(page) && page_order(page) >= pageblock_order)
476 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
477 if (migrate_async_suitable(migratetype))
480 /* Otherwise skip the block */
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
490 static unsigned long start_free_pfn(struct zone *zone)
492 unsigned long free_pfn;
493 free_pfn = zone->zone_start_pfn + zone->spanned_pages;
494 free_pfn &= ~(pageblock_nr_pages-1);
499 * Based on information in the current compact_control, find blocks
500 * suitable for isolating free pages from and then isolate them.
502 static void isolate_freepages(struct zone *zone,
503 struct compact_control *cc)
506 unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
508 int nr_freepages = cc->nr_freepages;
509 struct list_head *freelist = &cc->freepages;
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.
517 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
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.
524 high_pfn = min(low_pfn, pfn);
526 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
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.
533 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
534 pfn -= pageblock_nr_pages) {
535 unsigned long isolated;
541 * Check for overlapping nodes/zones. It's possible on some
542 * configurations to have a setup like
544 * i.e. it's possible that all pages within a zones range of
545 * pages do not belong to a single zone.
547 page = pfn_to_page(pfn);
548 if (page_zone(page) != zone)
551 /* Check the block is suitable for migration */
552 if (!suitable_migration_target(page))
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
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
570 if (!compact_trylock_irqsave(&zone->lock, &flags, cc))
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,
576 nr_freepages += isolated;
578 spin_unlock_irqrestore(&zone->lock, flags);
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
586 high_pfn = max(high_pfn, pfn);
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
595 if (cc->order > 0 && cc->wrapped)
596 zone->compact_cached_free_pfn = high_pfn;
600 /* split_free_page does not map the pages */
603 cc->free_pfn = high_pfn;
604 cc->nr_freepages = nr_freepages;
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;
613 * This is a migrate-callback that "allocates" freepages by taking pages
614 * from the isolated freelists in the block we are migrating to.
616 static struct page *compaction_alloc(struct page *migratepage,
620 struct compact_control *cc = (struct compact_control *)data;
621 struct page *freepage;
623 /* Isolate free pages if necessary */
624 if (list_empty(&cc->freepages)) {
625 isolate_freepages(cc->zone, cc);
627 if (list_empty(&cc->freepages))
631 freepage = list_entry(cc->freepages.next, struct page, lru);
632 list_del(&freepage->lru);
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.
643 static void update_nr_listpages(struct compact_control *cc)
645 int nr_migratepages = 0;
646 int nr_freepages = 0;
649 list_for_each_entry(page, &cc->migratepages, lru)
651 list_for_each_entry(page, &cc->freepages, lru)
654 cc->nr_migratepages = nr_migratepages;
655 cc->nr_freepages = nr_freepages;
658 /* possible outcome of isolate_migratepages */
660 ISOLATE_ABORT, /* Abort compaction now */
661 ISOLATE_NONE, /* No pages isolated, continue scanning */
662 ISOLATE_SUCCESS, /* Pages isolated, migrate */
666 * Isolate all pages that can be migrated from the block pointed to by
667 * the migrate scanner within compact_control.
669 static isolate_migrate_t isolate_migratepages(struct zone *zone,
670 struct compact_control *cc)
672 unsigned long low_pfn, end_pfn;
674 /* Do not scan outside zone boundaries */
675 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
677 /* Only scan within a pageblock boundary */
678 end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
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;
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;
691 cc->migrate_pfn = low_pfn;
693 return ISOLATE_SUCCESS;
696 static int compact_finished(struct zone *zone,
697 struct compact_control *cc)
699 unsigned long watermark;
701 if (fatal_signal_pending(current))
702 return COMPACT_PARTIAL;
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.
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;
717 return COMPACT_CONTINUE;
719 return COMPACT_COMPLETE;
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;
727 * order == -1 is expected when compacting via
728 * /proc/sys/vm/compact_memory
731 return COMPACT_CONTINUE;
733 /* Compaction run is not finished if the watermark is not met */
734 watermark = low_wmark_pages(zone);
735 watermark += (1 << cc->order);
737 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
738 return COMPACT_CONTINUE;
740 /* Direct compactor: Is a suitable page free? */
742 /* Was a suitable page captured? */
744 return COMPACT_PARTIAL;
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;
753 /* Job done if allocation would set block type */
754 if (cc->order >= pageblock_order && area->nr_free)
755 return COMPACT_PARTIAL;
759 return COMPACT_CONTINUE;
763 * compaction_suitable: Is this suitable to run compaction on this zone now?
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
769 unsigned long compaction_suitable(struct zone *zone, int order)
772 unsigned long watermark;
775 * order == -1 is expected when compacting via
776 * /proc/sys/vm/compact_memory
779 return COMPACT_CONTINUE;
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
786 watermark = low_wmark_pages(zone) + (2UL << order);
787 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
788 return COMPACT_SKIPPED;
791 * fragmentation index determines if allocation failures are due to
792 * low memory or external fragmentation
794 * index of -1000 implies allocations might succeed depending on
796 * index towards 0 implies failure is due to lack of memory
797 * index towards 1000 implies failure is due to fragmentation
799 * Only compact if a failure would be due to fragmentation.
801 fragindex = fragmentation_index(zone, order);
802 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
803 return COMPACT_SKIPPED;
805 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
807 return COMPACT_PARTIAL;
809 return COMPACT_CONTINUE;
812 static int compact_zone(struct zone *zone, struct compact_control *cc)
816 ret = compaction_suitable(zone, cc->order);
818 case COMPACT_PARTIAL:
819 case COMPACT_SKIPPED:
820 /* Compaction is likely to fail */
822 case COMPACT_CONTINUE:
823 /* Fall through to compaction */
827 /* Setup to move all movable pages to the end of the zone */
828 cc->migrate_pfn = zone->zone_start_pfn;
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;
835 /* Order == -1 starts at the end of the zone. */
836 cc->free_pfn = start_free_pfn(zone);
839 migrate_prep_local();
841 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
842 unsigned long nr_migrate, nr_remaining;
845 switch (isolate_migratepages(zone, cc)) {
847 ret = COMPACT_PARTIAL;
848 putback_lru_pages(&cc->migratepages);
849 cc->nr_migratepages = 0;
853 case ISOLATE_SUCCESS:
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;
864 count_vm_event(COMPACTBLOCKS);
865 count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
867 count_vm_events(COMPACTPAGEFAILED, nr_remaining);
868 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
871 /* Release LRU pages not migrated */
873 putback_lru_pages(&cc->migratepages);
874 cc->nr_migratepages = 0;
875 if (err == -ENOMEM) {
876 ret = COMPACT_PARTIAL;
881 /* Capture a page now if it is a suitable size */
882 compact_capture_page(cc);
886 /* Release free pages and check accounting */
887 cc->nr_freepages -= release_freepages(&cc->freepages);
888 VM_BUG_ON(cc->nr_freepages != 0);
893 static unsigned long compact_zone_order(struct zone *zone,
894 int order, gfp_t gfp_mask,
895 bool sync, bool *contended,
899 struct compact_control cc = {
901 .nr_migratepages = 0,
903 .migratetype = allocflags_to_migratetype(gfp_mask),
908 INIT_LIST_HEAD(&cc.freepages);
909 INIT_LIST_HEAD(&cc.migratepages);
911 ret = compact_zone(zone, &cc);
913 VM_BUG_ON(!list_empty(&cc.freepages));
914 VM_BUG_ON(!list_empty(&cc.migratepages));
916 *contended = cc.contended;
920 int sysctl_extfrag_threshold = 500;
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
932 * This is the main entry point for direct page compaction.
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)
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;
943 int rc = COMPACT_SKIPPED;
946 /* Check if the GFP flags allow compaction */
947 if (!order || !may_enter_fs || !may_perform_io)
950 count_vm_event(COMPACTSTALL);
953 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
954 alloc_flags |= ALLOC_CMA;
956 /* Compact each zone in the list */
957 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
961 status = compact_zone_order(zone, order, gfp_mask, sync,
963 rc = max(status, rc);
965 /* If a normal allocation would succeed, stop compacting */
966 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
975 /* Compact all zones within a node */
976 static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
981 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
983 zone = &pgdat->node_zones[zoneid];
984 if (!populated_zone(zone))
987 cc->nr_freepages = 0;
988 cc->nr_migratepages = 0;
990 INIT_LIST_HEAD(&cc->freepages);
991 INIT_LIST_HEAD(&cc->migratepages);
993 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
994 compact_zone(zone, cc);
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);
1006 VM_BUG_ON(!list_empty(&cc->freepages));
1007 VM_BUG_ON(!list_empty(&cc->migratepages));
1013 int compact_pgdat(pg_data_t *pgdat, int order)
1015 struct compact_control cc = {
1021 return __compact_pgdat(pgdat, &cc);
1024 static int compact_node(int nid)
1026 struct compact_control cc = {
1032 return __compact_pgdat(NODE_DATA(nid), &cc);
1035 /* Compact all nodes in the system */
1036 static int compact_nodes(void)
1040 /* Flush pending updates to the LRU lists */
1041 lru_add_drain_all();
1043 for_each_online_node(nid)
1046 return COMPACT_COMPLETE;
1049 /* The written value is actually unused, all memory is compacted */
1050 int sysctl_compact_memory;
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)
1057 return compact_nodes();
1062 int sysctl_extfrag_handler(struct ctl_table *table, int write,
1063 void __user *buffer, size_t *length, loff_t *ppos)
1065 proc_dointvec_minmax(table, write, buffer, length, ppos);
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)
1077 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1078 /* Flush pending updates to the LRU lists */
1079 lru_add_drain_all();
1086 static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
1088 int compaction_register_node(struct node *node)
1090 return device_create_file(&node->dev, &dev_attr_compact);
1093 void compaction_unregister_node(struct node *node)
1095 return device_remove_file(&node->dev, &dev_attr_compact);
1097 #endif /* CONFIG_SYSFS && CONFIG_NUMA */
1099 #endif /* CONFIG_COMPACTION */