This is the folio equivalent of memcpy_from_page(), but it handles large highmem folios. It may be a little too big to inline on systems that have CONFIG_HIGHMEM enabled but on systems we actually care about almost all the code will be eliminated. Signed-off-by: Matthew Wilcox (Oracle) <willy@xxxxxxxxxxxxx> --- include/linux/highmem.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/include/linux/highmem.h b/include/linux/highmem.h index ec39f544113d..d47f4a09f2fa 100644 --- a/include/linux/highmem.h +++ b/include/linux/highmem.h @@ -536,6 +536,34 @@ static inline void memcpy_to_folio(struct folio *folio, size_t offset, flush_dcache_folio(folio); } +/** + * memcpy_from_folio - Copy a range of bytes from a folio + * @to: The memory to copy to. + * @folio: The folio to read from. + * @offset: The first byte in the folio to read. + * @len: The number of bytes to copy. + */ +static inline void memcpy_from_folio(char *to, struct folio *folio, + size_t offset, size_t len) +{ + size_t n = len; + + VM_BUG_ON(offset + len > folio_size(folio)); + + if (folio_test_highmem(folio)) + n = min(len, PAGE_SIZE - offset_in_page(offset)); + for (;;) { + char *from = kmap_local_folio(folio, offset); + memcpy(to, from, n); + kunmap_local(from); + if (!folio_test_highmem(folio) || n == len) + break; + offset += n; + len -= n; + n = min(len, PAGE_SIZE); + } +} + static inline void put_and_unmap_page(struct page *page, void *addr) { kunmap_local(addr); -- 2.39.2