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(). Signed-off-by: Shivank Garg --- include/linux/highmem.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/include/linux/highmem.h b/include/linux/highmem.h index c3240f70471d..1bb0c1b707e3 100644 --- a/include/linux/highmem.h +++ b/include/linux/highmem.h @@ -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) -- 2.53.0