collapse_file() copies the old folios into the new folio one page at a time with copy_mc_highpage(). Replace it to copy each old folio with a single copy_mc_highpages() call instead. Where copy_mc_to_kernel() is available, the folio is copied with one copy_mc_to_kernel() call, as folio_mc_copy() does. Elsewhere, it is still copied one page at a time, now with cond_resched() before each page. On a machine check, copy_mc_highpages() queues the poisoned source page for memory_failure() and returns -EHWPOISON, as copy_mc_highpage() did, so collapse_file() still rolls back with SCAN_COPY_MC. On an AMD EPYC Zen 6 system, a single thread bound to one CPU runs MADV_COLLAPSE on a 1GB tmpfs file (huge=within_size) whose page cache is made of folios of one size. Time per 2MB collapse (in microseconds, mean ± stddev): folio size base patched 4KB 234.0 ± 2.7 237.0 ± 2.3 (noise) 64KB 95.2 ± 1.8 89.0 ± 0.8 (-6%) 256KB 69.1 ± 1.6 62.7 ± 0.5 (-9%) 1MB 68.2 ± 1.5 60.6 ± 0.3 (-11%) Signed-off-by: Shivank Garg --- mm/khugepaged.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 93d4a6f159e8..06663bb9dbe6 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -2530,7 +2530,7 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr, index = start; dst = folio_page(new_folio, 0); list_for_each_entry(folio, &pagelist, lru) { - int i, nr_pages = folio_nr_pages(folio); + int nr_pages = folio_nr_pages(folio); while (index < folio->index) { clear_highpage(dst); @@ -2538,14 +2538,12 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr, dst++; } - for (i = 0; i < nr_pages; i++) { - if (copy_mc_highpage(dst, folio_page(folio, i)) > 0) { - result = SCAN_COPY_MC; - goto rollback; - } - index++; - dst++; + if (copy_mc_highpages(dst, folio_page(folio, 0), nr_pages)) { + result = SCAN_COPY_MC; + goto rollback; } + index += nr_pages; + dst += nr_pages; } while (index < end) { clear_highpage(dst); -- 2.53.0