Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mm: compaction: update the cc->nr_migratepages when allocating or freeing the freepages

Currently we will use 'cc->nr_freepages >= cc->nr_migratepages' comparison
to ensure that enough freepages are isolated in isolate_freepages(),
however it just decreases the cc->nr_freepages without updating
cc->nr_migratepages in compaction_alloc(), which will waste more CPU
cycles and cause too many freepages to be isolated.

So we should also update the cc->nr_migratepages when allocating or
freeing the freepages to avoid isolating excess freepages. And I can see
fewer free pages are scanned and isolated when running thpcompact on my
Arm64 server:

k6.7 k6.7_patched
Ops Compaction pages isolated 120692036.00 118160797.00
Ops Compaction migrate scanned 131210329.00 154093268.00
Ops Compaction free scanned 1090587971.00 1080632536.00
Ops Compact scan efficiency 12.03 14.26

Moreover, I did not see an obvious latency improvements, this is likely
because isolating freepages is not the bottleneck in the thpcompact test
case.

k6.7 k6.7_patched
Amean fault-both-1 1089.76 ( 0.00%) 1080.16 * 0.88%*
Amean fault-both-3 1616.48 ( 0.00%) 1636.65 * -1.25%*
Amean fault-both-5 2266.66 ( 0.00%) 2219.20 * 2.09%*
Amean fault-both-7 2909.84 ( 0.00%) 2801.90 * 3.71%*
Amean fault-both-12 4861.26 ( 0.00%) 4733.25 * 2.63%*
Amean fault-both-18 7351.11 ( 0.00%) 6950.51 * 5.45%*
Amean fault-both-24 9059.30 ( 0.00%) 9159.99 * -1.11%*
Amean fault-both-30 10685.68 ( 0.00%) 11399.02 * -6.68%*

Link: https://lkml.kernel.org/r/6440493f18da82298152b6305d6b41c2962a3ce6.1708409245.git.baolin.wang@linux.alibaba.com
Signed-off-by: Baolin Wang <baolin.wang@linux.alibaba.com>
Acked-by: Mel Gorman <mgorman@techsingularity.net>
Reviewed-by: Vlastimil Babka <vbabka@suse.cz>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Zi Yan <ziy@nvidia.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Baolin Wang and committed by
Andrew Morton
ab755bf4 d1d86ce2

+13 -5
+3 -3
include/trace/events/compaction.h
··· 78 78 #ifdef CONFIG_COMPACTION 79 79 TRACE_EVENT(mm_compaction_migratepages, 80 80 81 - TP_PROTO(struct compact_control *cc, 81 + TP_PROTO(unsigned int nr_migratepages, 82 82 unsigned int nr_succeeded), 83 83 84 - TP_ARGS(cc, nr_succeeded), 84 + TP_ARGS(nr_migratepages, nr_succeeded), 85 85 86 86 TP_STRUCT__entry( 87 87 __field(unsigned long, nr_migrated) ··· 90 90 91 91 TP_fast_assign( 92 92 __entry->nr_migrated = nr_succeeded; 93 - __entry->nr_failed = cc->nr_migratepages - nr_succeeded; 93 + __entry->nr_failed = nr_migratepages - nr_succeeded; 94 94 ), 95 95 96 96 TP_printk("nr_migrated=%lu nr_failed=%lu",
+10 -2
mm/compaction.c
··· 1796 1796 dst = list_entry(cc->freepages.next, struct folio, lru); 1797 1797 list_del(&dst->lru); 1798 1798 cc->nr_freepages--; 1799 + cc->nr_migratepages--; 1799 1800 1800 1801 return dst; 1801 1802 } ··· 1812 1811 1813 1812 list_add(&dst->lru, &cc->freepages); 1814 1813 cc->nr_freepages++; 1814 + cc->nr_migratepages++; 1815 1815 } 1816 1816 1817 1817 /* possible outcome of isolate_migratepages */ ··· 2435 2433 unsigned long last_migrated_pfn; 2436 2434 const bool sync = cc->mode != MIGRATE_ASYNC; 2437 2435 bool update_cached; 2438 - unsigned int nr_succeeded = 0; 2436 + unsigned int nr_succeeded = 0, nr_migratepages; 2439 2437 2440 2438 /* 2441 2439 * These counters track activities during zone compaction. Initialize ··· 2553 2551 pageblock_start_pfn(cc->migrate_pfn - 1)); 2554 2552 } 2555 2553 2554 + /* 2555 + * Record the number of pages to migrate since the 2556 + * compaction_alloc/free() will update cc->nr_migratepages 2557 + * properly. 2558 + */ 2559 + nr_migratepages = cc->nr_migratepages; 2556 2560 err = migrate_pages(&cc->migratepages, compaction_alloc, 2557 2561 compaction_free, (unsigned long)cc, cc->mode, 2558 2562 MR_COMPACTION, &nr_succeeded); 2559 2563 2560 - trace_mm_compaction_migratepages(cc, nr_succeeded); 2564 + trace_mm_compaction_migratepages(nr_migratepages, nr_succeeded); 2561 2565 2562 2566 /* All pages were either migrated or will be released */ 2563 2567 cc->nr_migratepages = 0;