This is the folio equivalent of memcpy_to_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 | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/include/linux/highmem.h b/include/linux/highmem.h index 4de1dbcd3ef6..ec39f544113d 100644 --- a/include/linux/highmem.h +++ b/include/linux/highmem.h @@ -507,6 +507,35 @@ static inline void folio_zero_range(struct folio *folio, zero_user_segments(&folio->page, start, start + length, 0, 0); } +/** + * memcpy_to_folio - Copy a range of bytes to a folio + * @folio: The folio to write to. + * @offset: The first byte in the folio to store to. + * @from: The memory to copy from. + * @len: The number of bytes to copy. + */ +static inline void memcpy_to_folio(struct folio *folio, size_t offset, + const char *from, 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 *to = kmap_local_folio(folio, offset); + memcpy(to, from, n); + kunmap_local(to); + if (!folio_test_highmem(folio) || n == len) + break; + offset += n; + len -= n; + n = min(len, PAGE_SIZE); + } + flush_dcache_folio(folio); +} + static inline void put_and_unmap_page(struct page *page, void *addr) { kunmap_local(addr); -- 2.39.2