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