mm: copy page ranges with one copy_mc_to_kernel() call
Subsystems (3)
v4 cover mm: copy page ranges with one copy_mc_to_kernel() call
On a Zen 6 system with CXL memory, move_pages() gets ~1.4x faster for 2MB THPs and up to ~2.8x faster for 1GB hugetlb pages, and when demoting to CXL, kswapd uses ~31% less CPU per GB demoted. Hugetlb copy on write gets ~1.5x faster for 2MB pages and ~2x faster for 1GB pages, and MADV_COLLAPSE of a tmpfs file made of large folios gets ~11% faster (numbers below).
Copying a folio with machine check handling makes one copy_mc_to_kernel() call per page. This series copies a page range with a single call where possible (x86-64 and ppc64), for folio migration, hugetlb copy on write and khugepaged file collapse.
Test setup: AMD EPYC Zen 6, single socket, NPS2 (nodes 0 and 1 are DRAM, node 2 is CXL), base (mm/core.git for-next) vs the whole series.
move_pages() throughput
-----------------------
A single move_pages() call migrates 1GB of anonymous memory, as 512 2MB THPs or as four 1GB hugetlb pages. Mean ± stddev, GiB/s:
2MB THP:
direction base patched speedup DRAM0 -> DRAM1 19.57 ± 0.17 27.51 ± 0.78 1.41x DRAM1 -> DRAM0 19.44 ± 0.12 27.33 ± 0.27 1.41x DRAM0 -> CXL 18.66 ± 0.18 26.90 ± 0.51 1.44x CXL -> DRAM0 15.38 ± 0.59 16.60 ± 0.24 1.08x
1GB hugetlb:
direction base patched speedup DRAM0 -> DRAM1 20.34 ± 0.13 53.88 ± 1.48 2.65x DRAM1 -> DRAM0 19.10 ± 2.65 53.78 ± 0.55 2.82x DRAM0 -> CXL 19.53 ± 0.08 44.10 ± 0.19 2.26x CXL -> DRAM0 16.41 ± 0.07 20.89 ± 0.06 1.27x
kswapd demotion
---------------
Node 0 is limited to 16GB of free memory and demotion is enabled. A single thread bound to node 0 writes 96GB of anonymous THP memory, so ~80GB has to be demoted to CXL.
base patched kswapd CPU time 4.25 s 3.44 s (-19%) kswapd CPU per GB demoted 63.6 ms 44.1 ms (-31%) demoted by direct reclaim 14.1 GB 0.46 GB allocstall 7427 370
With the patch, kswapd keeps up with the writer, so the writer rarely falls back to direct reclaim.
MADV_COLLAPSE
-------------
The thread 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 of 30 runs over three boots:
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%)
hugetlb copy on write
---------------------
The thread writes to each hugetlb page of a MAP_PRIVATE mapping of a populated hugetlb memfd, so 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
The new copy order alone gives ~15% for 2MB pages and nothing for 1GB pages; the rest comes from the single-call copy.
Changes in v4:
- Convert copy_user_large_folio() to copy page ranges with the new copy_mc_user_highpages(), and drop process_huge_page() and copy_user_gigantic_page() (David).
- Copy each folio with copy_mc_highpages() in collapse_file().
- Move the batching check and the single-call copy into copy_mc_can_batch() and __copy_mc_pages() (for copy_mc_user_highpages cleanups).
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- Copy the whole range with one copy_mc_to_kernel() call only when CONFIG_PREEMPTION is set, otherwise copy page by page with cond_resched(), following clear_contig_highpages() [1] (Pedro, David).
- Handle the machine check first in copy_mc_highpages() (Pedro).
- Rebase onto mm/core.git for-next.
- Link to v2: https://lore.kernel.org/r/[email protected]
[1] https://lore.kernel.org/r/[email protected]
Changes in v2:
- Drop the folio_copy() changes as there is little to gain here. On architectures without copy_mc_to_kernel(), copy_mc_highpages() falls back to the per-page copy_mc_highpage(), which ends up in copy_highpage(). A batched copy_highpages() could help there too, but I haven't tested it on those architectures, so that is left for a follow-up.
- Add COPY_MC_PAGES_NON_PREEMPT_BATCH to limit how much is copied between cond_resched() calls on non-preemptible kernels.
- Rebase onto mm-new (2887e63d2e8e).
- v1: https://lore.kernel.org/all/[email protected]
---
Shivank Garg (6):
mm/highmem: introduce copy_mc_can_batch() and __copy_mc_pages()
mm: introduce copy_mc_highpages() and batch copy in folio_mc_copy() mm/khugepaged: copy each folio with copy_mc_highpages() in collapse_file() mm/highmem: introduce copy_mc_user_highpages()
mm: factor out the locality range computation from folio_zero_user()
mm: copy_user_large_folio(): copy page ranges
include/linux/highmem.h | 105 +++++++++++++++++++++++++++ mm/khugepaged.c | 14 ++-- mm/memory.c | 183 +++++++++++++++++------------------------------- mm/util.c | 14 +--- 4 files changed, 176 insertions(+), 140 deletions(-)
---
base-commit: 89b4b2b15b2063e1bf593e609c3c63313b7ff160
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/mm/core.git for-next
change-id: 20260714-batpagecopy-5a2b45910e5d
Best regards,
Signature · 1 line
Shivank Garg <[email protected]>
PATCH v4 1/6 mm/highmem: introduce copy_mc_can_batch() and __copy_mc_pages()
Copying a page range with machine check handling one page at a time, as folio_mc_copy() does with copy_mc_highpage(), makes one copy_mc_to_kernel() call per page. Each call pays the startup cost of the string instruction ("REP; MOVSB" on x86 CPUs with ERMS).
Add copy_mc_can_batch(), which tells whether a page range can be copied with a single copy_mc_to_kernel() call, and __copy_mc_pages(), which does that copy. On a machine check, __copy_mc_pages() queues the poisoned source page for memory_failure(), as copy_mc_highpage() does, and returns -EHWPOISON.
copy_mc_to_kernel() cannot reschedule, so batching requires CONFIG_PREEMPTION, and the pages must be in the linear map, so it also requires !CONFIG_HIGHMEM. Since commit 7dadeaa6e851 ("sched: Further restrict the preemption modes"), x86 and powerpc, the only architectures that select ARCH_HAS_COPY_MC, only offer the full, lazy and RT preemption models, also with PREEMPT_DYNAMIC, so they can always batch.
The following patches use them for the page range copies copy_mc_highpages() and copy_mc_user_highpages().
--- include/linux/highmem.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
include/linux/highmem.h +26 -0
@@ -490,6 +490,32 @@ static inline int copy_mc_highpage(struct page *to, struct page *from) }#endif +/* +* Return true if a page range can be copied with one copy_mc_to_kernel() call. +* That call does not reschedule, so the kernel must be preemptible, and the +* pages must be in the linear map. +*/ +static inline bool copy_mc_can_batch (void ) +{ +return IS_ENABLED (CONFIG_ARCH_HAS_COPY_MC ) && +IS_ENABLED (CONFIG_PREEMPTION ) && !IS_ENABLED (CONFIG_HIGHMEM ); +} + +/* Only valid if copy_mc_can_batch() returns true. */ +static inline int __copy_mc_pages (struct page *to,struct page *from, +unsigned long nr_pages) +{ +unsigned long len = nr_pages <<PAGE_SHIFT ; +unsigned long ret; + + ret =copy_mc_to_kernel (page_address (to),page_address (from), len); +if (!ret) +return 0 ; + +memory_failure_queue (page_to_pfn (from) + ((len - ret) >>PAGE_SHIFT ),0 ); +return -EHWPOISON ; +} +static inline void memcpy_page (struct page *dst_page,size_t dst_off,struct page *src_page,size_t src_off,size_t len)
Signature · 1 line
2.53.0
PATCH v4 2/6 mm: introduce copy_mc_highpages() and batch copy in folio_mc_copy()
In reply to Shivank Gargfolio_mc_copy() copies a folio one page at a time with copy_mc_highpage(), so a 2MB folio makes 512 separate copy_mc_to_kernel() calls. Each page pays the startup cost of the string instruction ("REP; MOVSB" on x86 CPUs with ERMS) plus a cond_resched().
Introduce copy_mc_highpages(), the page range variant of copy_mc_highpage(). Where copy_mc_can_batch() allows it, copy the whole range with a single __copy_mc_pages() call, so the CPU can optimize for the larger extent. Otherwise, fall back to copy_mc_highpage() one page at a time, with cond_resched() before each page, as clear_contig_highpages() does.
Switch folio_mc_copy() to copy the whole folio with copy_mc_highpages(). On a machine check, it still returns -EHWPOISON.
On an AMD EPYC Zen 6 system (single socket, NPS2; nodes 0 and 1 are DRAM, node 2 is CXL), a single move_pages() call migrates 1GB of anonymous memory, as 512 2MB THPs or as four 1GB hugetlb pages. Throughput in GiB/s, mean ± stddev of 10 runs:
2MB THP:
direction base patched speedup DRAM0 -> DRAM1 19.57 ± 0.17 27.51 ± 0.78 1.41x DRAM1 -> DRAM0 19.44 ± 0.12 27.33 ± 0.27 1.41x DRAM0 -> CXL 18.66 ± 0.18 26.90 ± 0.51 1.44x CXL -> DRAM0 15.38 ± 0.59 16.60 ± 0.24 1.08x
1GB hugetlb:
direction base patched speedup DRAM0 -> DRAM1 20.34 ± 0.13 53.88 ± 1.48 2.65x DRAM1 -> DRAM0 19.10 ± 2.65 53.78 ± 0.55 2.82x DRAM0 -> CXL 19.53 ± 0.08 44.10 ± 0.19 2.26x CXL -> DRAM0 16.41 ± 0.07 20.89 ± 0.06 1.27x
With node 0 limited to 16GB of free memory and demotion enabled, a single thread bound to node 0 writes 96GB of anonymous THP memory, so ~80GB has to be demoted to CXL. Median of 10 runs:
base patched kswapd CPU time 4.25 s 3.44 s (-19%) kswapd CPU per GB demoted 63.6 ms 44.1 ms (-31%) demoted by direct reclaim 14.1 GB 0.46 GB allocstall 7427 370
With the patch, kswapd keeps up with the writer, so the writer rarely falls back to direct reclaim.
--- include/linux/highmem.h | 35 +++++++++++++++++++++++++++++++++++ mm/util.c | 14 ++------------ 2 files changed, 37 insertions(+), 12 deletions(-)
include/linux/highmem.h +35 -0
@@ -516,6 +516,41 @@ static inline int __copy_mc_pages(struct page *to, struct page *from,return -EHWPOISON ; } +/** +* copy_mc_highpages() - copy a page range with machine check handling +* @to: first destination page +* @from: first source page +* @nr_pages: number of pages +* +* Range variant of copy_mc_highpage(). On a machine check, the poisoned +* source page is queued for memory_failure(). +* +* Context: Process context. May sleep. +* Return: 0 on success, -EHWPOISON if a machine check was handled. +*/ +static inline int copy_mc_highpages (struct page *to,struct page *from, +unsigned long nr_pages) +{ +unsigned long i; + +might_sleep (); + +if (copy_mc_can_batch ()) { +if (__copy_mc_pages (to, from, nr_pages)) +return -EHWPOISON ; +for (i =0 ; i < nr_pages; i++) +kmsan_copy_page_meta (to + i, from + i); +return 0 ; + } + +for (i =0 ; i < nr_pages; i++) { +cond_resched (); +if (copy_mc_highpage (to + i, from + i)) +return -EHWPOISON ; + } +return 0 ; +} +static inline void memcpy_page (struct page *dst_page,size_t dst_off,struct page *src_page,size_t src_off,size_t len)
mm/util.c +2 -12
@@ -776,18 +776,8 @@ EXPORT_SYMBOL(folio_copy);int folio_mc_copy (struct folio *dst,struct folio *src) { -long nr =folio_nr_pages (src); -long i =0 ; - -for (;;) { -if (copy_mc_highpage (folio_page (dst, i),folio_page (src, i))) -return -EHWPOISON ; -if (++i == nr) -break ; -cond_resched (); - } - -return 0 ; +return copy_mc_highpages (folio_page (dst,0 ),folio_page (src,0 ), +folio_nr_pages (src)); }EXPORT_SYMBOL (folio_mc_copy);
Signature · 1 line
2.53.0
PATCH v4 3/6 mm/khugepaged: copy each folio with copy_mc_highpages() in collapse_file()
In reply to Shivank Gargcollapse_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%)
--- mm/khugepaged.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-)
mm/khugepaged.c +6 -8
@@ -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);
Signature · 1 line
2.53.0
PATCH v4 4/6 mm/highmem: introduce copy_mc_user_highpages()
In reply to Shivank Gargcopy_user_large_folio() copies a hugetlb folio one page at a time with copy_mc_user_highpage(), in the order given by process_huge_page(). To convert it to copying page ranges, introduce copy_mc_user_highpages(), range variant similar to copy_mc_highpages().
It batches the same way, with copy_mc_can_batch() and __copy_mc_pages(). Where it can batch, copy_mc_user_highpage() uses neither the user address nor the VMA, so one call over the whole range is equivalent.
On a machine check, the batched copy unpoisons none of the range, while copying page by page unpoisons the pages before the poisoned one. This is fine, as all callers of copy_user_large_folio() free the destination folio on error.
--- include/linux/highmem.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+)
include/linux/highmem.h +44 -0
@@ -551,6 +551,50 @@ static inline int copy_mc_highpages(struct page *to, struct page *from,return 0 ; } +/** +* copy_mc_user_highpages() - copy a page range to be mapped to user space, +* with machine check handling +* @to: first destination page +* @from: first source page +* @vaddr: user address the first destination page will be mapped at +* @vma: VMA the destination will be mapped in +* @nr_pages: number of pages +* +* Range variant of copy_mc_user_highpage(). On a machine check, the poisoned +* source page is queued for memory_failure(). +* +* Context: Process context. May sleep. +* Return: 0 on success, -EHWPOISON if a machine check was handled. +*/ +static inline int copy_mc_user_highpages (struct page *to,struct page *from, +unsigned long vaddr,struct vm_area_struct *vma, +unsigned long nr_pages) +{ +unsigned long i; + +might_sleep (); + +/* +* Where we can batch, copy_mc_user_highpage() does not use @vaddr, +* @vma or copy_user_page() either, so a single call is equivalent. +*/ +if (copy_mc_can_batch ()) { +if (__copy_mc_pages (to, from, nr_pages)) +return -EHWPOISON ; +for (i =0 ; i < nr_pages; i++) +kmsan_unpoison_memory (page_address (to + i),PAGE_SIZE ); +return 0 ; + } + +for (i =0 ; i < nr_pages; i++) { +cond_resched (); +if (copy_mc_user_highpage (to + i, from + i, + vaddr + i *PAGE_SIZE , vma)) +return -EHWPOISON ; + } +return 0 ; +} +static inline void memcpy_page (struct page *dst_page,size_t dst_off,struct page *src_page,size_t src_off,size_t len)
Signature · 1 line
2.53.0
PATCH v4 5/6 mm: factor out the locality range computation from folio_zero_user()
In reply to Shivank Gargfolio_zero_user() splits a folio into three page ranges around the faulting page and clears them in order, so that the faulting page and its neighbours are cleared last and stay cache hot. Copying a folio to be mapped at a fault has the same locality requirement, and an upcoming patch converts copy_user_large_folio() to the same scheme.
Move the range computation into folio_user_ranges() so that both can share it. Rename FOLIO_ZERO_LOCALITY_RADIUS to FOLIO_LOCALITY_RADIUS and generalise the comment above it, as it now describes both.
No functional change.
--- mm/memory.c | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-)
mm/memory.c +31 -13
@@ -7767,31 +7767,35 @@ static void clear_contig_highpages(struct page *page, unsigned long addr, }/* -* When zeroing a folio, we want to differentiate between pages in the -* vicinity of the faulting address where we have spatial and temporal +* When zeroing or copying a folio, we want to differentiate between pages in +* the vicinity of the faulting address where we have spatial and temporal * locality, and those far away where we don't. * * Use a radius of 2 for determining the local neighbourhood. */ -#define FOLIO_ZERO_LOCALITY_RADIUS 2 +#define FOLIO_LOCALITY_RADIUS 2 -/** -* folio_zero_user - Zero a folio which will be mapped to userspace. -* @folio: The folio to zero. -* @addr_hint: The address accessed by the user or the base address. +/* +* Split a folio into the page index ranges to process, in order: +* +* r[0]: pages to the right of the faulting page's neighbourhood +* r[1]: pages to the left of the faulting page's neighbourhood +* r[2]: the faulting page and its immediate neighbourhood, to be processed +* last to keep its cachelines hot +* +* A range is empty if its length, range_len(), is 0. */ -void folio_zero_user (struct folio *folio,unsigned long addr_hint) +static void folio_user_ranges (struct folio *folio,unsigned long addr_hint, +struct range r[3 ]) {const unsigned long base_addr =ALIGN_DOWN (addr_hint,folio_size (folio));const long fault_idx = (addr_hint - base_addr) /PAGE_SIZE ;const struct range pg =DEFINE_RANGE (0 ,folio_nr_pages (folio) -1 ); -const long radius =FOLIO_ZERO_LOCALITY_RADIUS ; -struct range r[3 ]; -int i; +const long radius =FOLIO_LOCALITY_RADIUS ;/* -* Faulting page and its immediate neighbourhood. Will be cleared at the -* end to keep its cachelines hot. +* Faulting page and its immediate neighbourhood. Will be processed at +* the end to keep its cachelines hot. */ r[2 ] =DEFINE_RANGE (fault_idx - radius < (long )pg.start ? pg.start : fault_idx - radius, fault_idx + radius > (long )pg.end ? pg.end : fault_idx + radius);
@@ -7802,6 +7806,20 @@ void folio_zero_user(struct folio *folio, unsigned long addr_hint)/* Region to the right of the fault: always valid for the common fault_idx=0 case. */ r[0 ] =DEFINE_RANGE (r[2 ].end +1 , pg.end); +} + +/** +* folio_zero_user - Zero a folio which will be mapped to userspace. +* @folio: The folio to zero. +* @addr_hint: The address accessed by the user or the base address. +*/ +void folio_zero_user (struct folio *folio,unsigned long addr_hint) +{ +const unsigned long base_addr =ALIGN_DOWN (addr_hint,folio_size (folio)); +struct range r[3 ]; +int i; + +folio_user_ranges (folio, addr_hint, r);for (i =0 ; i <ARRAY_SIZE (r); i++) {const unsigned long addr = base_addr + r[i].start *PAGE_SIZE ;
Signature · 1 line
2.53.0
PATCH v4 6/6 mm: copy_user_large_folio(): copy page ranges
In reply to Shivank Gargcopy_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.
--- mm/memory.c | 139 ++++++++++++++---------------------------------------------- 1 file changed, 32 insertions(+), 107 deletions(-)
mm/memory.c +32 -107
@@ -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)
Signature · 1 line
2.53.0