copy_user_large_folio() still copies a folio one page at a time in the order given by process_huge_page(), with a separate loop in copy_user_gigantic_page() for gigantic folios, although folio_zero_user() moved to clearing page ranges. process_huge_page() processes the pages farthest from the faulting page first, converging on it by alternating between left and right. That keeps the faulting page cache hot, but its non-sequential access pattern defeats the hardware prefetcher, which is why clearing was moved away from it in commit 9890ecab6ad9 ("mm: folio_zero_user: clear pages sequentially") Make copy_user_large_folio() mirror folio_zero_user(): split the folio into the same three page ranges with folio_user_ranges() and copy each range with copy_mc_user_highpages(). The range around the faulting page (the faulting page and two pages either side of it) is copied last, as clearing does since commit 93552c9a3350 ("mm: folio_zero_user: cache neighbouring pages"). This keeps the cache locality that process_huge_page() was introduced for in commit c9f4cd713835 ("mm, huge page: copy target sub-page last when copy huge page") process_huge_page() was only used by copy_user_large_folio(), and copy_user_gigantic_page() and struct copy_subpage_arg with its callback only existed to feed those two, so remove them. On an AMD EPYC Zen 6 system, a single thread bound to one CPU writes to each hugetlb page of a MAP_PRIVATE mapping of a populated hugetlb memfd. Each write takes a copy on write fault, at the first or at the middle page of the folio. Copy throughput (GB/s, mean ± stddev): page size fault at base patched speedup 2MB first 22.40 ± 0.23 33.46 ± 0.65 1.49x 2MB middle 22.76 ± 0.31 33.37 ± 0.64 1.47x 1GB first 27.10 ± 0.03 53.25 ± 0.59 1.96x 1GB middle 27.10 ± 0.03 53.35 ± 0.57 1.97x With the single-call copy disabled, the new copy order alone gives 25.87 and 25.80 GB/s (~15%) for 2MB pages. 1GB pages were already copied sequentially and don't change. The rest of the gain comes from copying each range with one call. Signed-off-by: Shivank Garg --- mm/memory.c | 139 ++++++++++++++---------------------------------------------- 1 file changed, 32 insertions(+), 107 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index bd2c276e83aa..329ad8099e6b 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -7678,66 +7678,6 @@ EXPORT_SYMBOL(__might_fault); #endif #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS) -/* - * Process all subpages of the specified huge page with the specified - * operation. The target subpage will be processed last to keep its - * cache lines hot. - */ -static inline int process_huge_page( - unsigned long addr_hint, unsigned int nr_pages, - int (*process_subpage)(unsigned long addr, int idx, void *arg), - void *arg) -{ - int i, n, base, l, ret; - unsigned long addr = addr_hint & - ~(((unsigned long)nr_pages << PAGE_SHIFT) - 1); - - /* Process target subpage last to keep its cache lines hot */ - might_sleep(); - n = (addr_hint - addr) / PAGE_SIZE; - if (2 * n <= nr_pages) { - /* If target subpage in first half of huge page */ - base = 0; - l = n; - /* Process subpages at the end of huge page */ - for (i = nr_pages - 1; i >= 2 * n; i--) { - cond_resched(); - ret = process_subpage(addr + i * PAGE_SIZE, i, arg); - if (ret) - return ret; - } - } else { - /* If target subpage in second half of huge page */ - base = nr_pages - 2 * (nr_pages - n); - l = nr_pages - n; - /* Process subpages at the begin of huge page */ - for (i = 0; i < base; i++) { - cond_resched(); - ret = process_subpage(addr + i * PAGE_SIZE, i, arg); - if (ret) - return ret; - } - } - /* - * Process remaining subpages in left-right-left-right pattern - * towards the target subpage - */ - for (i = 0; i < l; i++) { - int left_idx = base + i; - int right_idx = base + 2 * l - 1 - i; - - cond_resched(); - ret = process_subpage(addr + left_idx * PAGE_SIZE, left_idx, arg); - if (ret) - return ret; - cond_resched(); - ret = process_subpage(addr + right_idx * PAGE_SIZE, right_idx, arg); - if (ret) - return ret; - } - return 0; -} - static void clear_contig_highpages(struct page *page, unsigned long addr, unsigned int nr_pages) { @@ -7831,61 +7771,46 @@ void folio_zero_user(struct folio *folio, unsigned long addr_hint) } } -static int copy_user_gigantic_page(struct folio *dst, struct folio *src, - unsigned long addr_hint, - struct vm_area_struct *vma, - unsigned int nr_pages) +/** + * copy_user_large_folio - Copy a large folio which will be mapped to userspace. + * @dst: The destination folio. + * @src: The source folio. + * @addr_hint: The address accessed by the user or the base address. + * @vma: The VMA @dst will be mapped in. + * + * Copy page ranges, with the one around @addr_hint last to keep its + * cachelines hot, see folio_user_ranges(). On a machine check, the poisoned + * source page is queued for memory_failure() and the remaining pages are + * not copied. + * + * Context: Process context. May sleep. + * Return: 0 on success, -EHWPOISON if a machine check was handled. + */ +int copy_user_large_folio(struct folio *dst, struct folio *src, + unsigned long addr_hint, struct vm_area_struct *vma) { - unsigned long addr = ALIGN_DOWN(addr_hint, folio_size(dst)); - struct page *dst_page; - struct page *src_page; - int i; + const unsigned long base_addr = ALIGN_DOWN(addr_hint, folio_size(dst)); + struct range r[3]; + int i, ret; - for (i = 0; i < nr_pages; i++) { - dst_page = folio_page(dst, i); - src_page = folio_page(src, i); + folio_user_ranges(dst, addr_hint, r); - cond_resched(); - if (copy_mc_user_highpage(dst_page, src_page, - addr + i*PAGE_SIZE, vma)) - return -EHWPOISON; - } - return 0; -} - -struct copy_subpage_arg { - struct folio *dst; - struct folio *src; - struct vm_area_struct *vma; -}; + for (i = 0; i < ARRAY_SIZE(r); i++) { + const unsigned long addr = base_addr + r[i].start * PAGE_SIZE; + const long nr_pages = (long)range_len(&r[i]); -static int copy_subpage(unsigned long addr, int idx, void *arg) -{ - struct copy_subpage_arg *copy_arg = arg; - struct page *dst = folio_page(copy_arg->dst, idx); - struct page *src = folio_page(copy_arg->src, idx); + if (nr_pages <= 0) + continue; - if (copy_mc_user_highpage(dst, src, addr, copy_arg->vma)) - return -EHWPOISON; + ret = copy_mc_user_highpages(folio_page(dst, r[i].start), + folio_page(src, r[i].start), + addr, vma, nr_pages); + if (ret) + return ret; + } return 0; } -int copy_user_large_folio(struct folio *dst, struct folio *src, - unsigned long addr_hint, struct vm_area_struct *vma) -{ - unsigned int nr_pages = folio_nr_pages(dst); - struct copy_subpage_arg arg = { - .dst = dst, - .src = src, - .vma = vma, - }; - - if (unlikely(nr_pages > MAX_ORDER_NR_PAGES)) - return copy_user_gigantic_page(dst, src, addr_hint, vma, nr_pages); - - return process_huge_page(addr_hint, nr_pages, copy_subpage, &arg); -} - long copy_folio_from_user(struct folio *dst_folio, const void __user *usr_src, bool allow_pagefault) -- 2.53.0