From: Matthew Wilcox <willy@infradead.org>
To: Johnny Hao <johnny_haocn@sina.com>
Cc: gregkh@linuxfoundation.org, stable@vger.kernel.org,
linux-kernel@vger.kernel.org, slava@dubeyko.com,
vishal.moola@gmail.com, Andrew Morton <akpm@linux-foundation.org>
Subject: Re: [PATCH 6.1.y 2/3] highmem: add kernel-doc for memcpy_*_folio()
Date: Mon, 9 Mar 2026 15:37:18 +0000 [thread overview]
Message-ID: <aa7pLpU_-S6quLCR@casper.infradead.org> (raw)
In-Reply-To: <20260309050130.912344-3-johnny_haocn@sina.com>
On Mon, Mar 09, 2026 at 01:01:29PM +0800, Johnny Hao wrote:
> From: "Matthew Wilcox (Oracle)" <willy@infradead.org>
>
> [ Upstream commit 9af47276ed83cc346263e56243756543a2a33c9d ]
what? This patch isn't that commit. That commit does indeed add
kernel-doc. This patch adds the functions themselves. Please be
more careful.
> This was inadvertently skipped when adding the new functions.
>
> Link: https://lkml.kernel.org/r/20240124181217.1761674-1-willy@infradead.org
> Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
> Signed-off-by: Johnny Hao <johnny_haocn@sina.com>
> ---
> include/linux/highmem.h | 164 ++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 164 insertions(+)
>
> diff --git a/include/linux/highmem.h b/include/linux/highmem.h
> index 44242268f53b..a2a0cfbc19a0 100644
> --- a/include/linux/highmem.h
> +++ b/include/linux/highmem.h
> @@ -415,6 +415,170 @@ static inline void memzero_page(struct page *page, size_t offset, size_t len)
> kunmap_local(addr);
> }
>
> +/**
> + * 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)
> +{
> + VM_BUG_ON(offset + len > folio_size(folio));
> +
> + do {
> + const char *from = kmap_local_folio(folio, offset);
> + size_t chunk = len;
> +
> + if (folio_test_highmem(folio) &&
> + chunk > PAGE_SIZE - offset_in_page(offset))
> + chunk = PAGE_SIZE - offset_in_page(offset);
> + memcpy(to, from, chunk);
> + kunmap_local(from);
> +
> + to += chunk;
> + offset += chunk;
> + len -= chunk;
> + } while (len > 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)
> +{
> + VM_BUG_ON(offset + len > folio_size(folio));
> +
> + do {
> + char *to = kmap_local_folio(folio, offset);
> + size_t chunk = len;
> +
> + if (folio_test_highmem(folio) &&
> + chunk > PAGE_SIZE - offset_in_page(offset))
> + chunk = PAGE_SIZE - offset_in_page(offset);
> + memcpy(to, from, chunk);
> + kunmap_local(to);
> +
> + from += chunk;
> + offset += chunk;
> + len -= chunk;
> + } while (len > 0);
> +
> + flush_dcache_folio(folio);
> +}
> +
> +/**
> + * folio_zero_tail - Zero the tail of a folio.
> + * @folio: The folio to zero.
> + * @offset: The byte offset in the folio to start zeroing at.
> + * @kaddr: The address the folio is currently mapped to.
> + *
> + * If you have already used kmap_local_folio() to map a folio, written
> + * some data to it and now need to zero the end of the folio (and flush
> + * the dcache), you can use this function. If you do not have the
> + * folio kmapped (eg the folio has been partially populated by DMA),
> + * use folio_zero_range() or folio_zero_segment() instead.
> + *
> + * Return: An address which can be passed to kunmap_local().
> + */
> +static inline __must_check void *folio_zero_tail(struct folio *folio,
> + size_t offset, void *kaddr)
> +{
> + size_t len = folio_size(folio) - offset;
> +
> + if (folio_test_highmem(folio)) {
> + size_t max = PAGE_SIZE - offset_in_page(offset);
> +
> + while (len > max) {
> + memset(kaddr, 0, max);
> + kunmap_local(kaddr);
> + len -= max;
> + offset += max;
> + max = PAGE_SIZE;
> + kaddr = kmap_local_folio(folio, offset);
> + }
> + }
> +
> + memset(kaddr, 0, len);
> + flush_dcache_folio(folio);
> +
> + return kaddr;
> +}
> +
> +/**
> + * folio_fill_tail - Copy some data to a folio and pad with zeroes.
> + * @folio: The destination folio.
> + * @offset: The offset into @folio at which to start copying.
> + * @from: The data to copy.
> + * @len: How many bytes of data to copy.
> + *
> + * This function is most useful for filesystems which support inline data.
> + * When they want to copy data from the inode into the page cache, this
> + * function does everything for them. It supports large folios even on
> + * HIGHMEM configurations.
> + */
> +static inline void folio_fill_tail(struct folio *folio, size_t offset,
> + const char *from, size_t len)
> +{
> + char *to = kmap_local_folio(folio, offset);
> +
> + VM_BUG_ON(offset + len > folio_size(folio));
> +
> + if (folio_test_highmem(folio)) {
> + size_t max = PAGE_SIZE - offset_in_page(offset);
> +
> + while (len > max) {
> + memcpy(to, from, max);
> + kunmap_local(to);
> + len -= max;
> + from += max;
> + offset += max;
> + max = PAGE_SIZE;
> + to = kmap_local_folio(folio, offset);
> + }
> + }
> +
> + memcpy(to, from, len);
> + to = folio_zero_tail(folio, offset + len, to + len);
> + kunmap_local(to);
> +}
> +
> +/**
> + * memcpy_from_file_folio - Copy some bytes from a file folio.
> + * @to: The destination buffer.
> + * @folio: The folio to copy from.
> + * @pos: The position in the file.
> + * @len: The maximum number of bytes to copy.
> + *
> + * Copy up to @len bytes from this folio. This may be limited by PAGE_SIZE
> + * if the folio comes from HIGHMEM, and by the size of the folio.
> + *
> + * Return: The number of bytes copied from the folio.
> + */
> +static inline size_t memcpy_from_file_folio(char *to, struct folio *folio,
> + loff_t pos, size_t len)
> +{
> + size_t offset = offset_in_folio(folio, pos);
> + char *from = kmap_local_folio(folio, offset);
> +
> + if (folio_test_highmem(folio)) {
> + offset = offset_in_page(offset);
> + len = min_t(size_t, len, PAGE_SIZE - offset);
> + } else
> + len = min(len, folio_size(folio) - offset);
> +
> + memcpy(to, from, len);
> + kunmap_local(from);
> +
> + return len;
> +}
> +
> /**
> * folio_zero_segments() - Zero two byte ranges in a folio.
> * @folio: The folio to write to.
> --
> 2.34.1
>
next prev parent reply other threads:[~2026-03-09 15:37 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-09 5:01 [PATCH 6.1.y 0/3] Fix patch backport review Johnny Hao
2026-03-09 5:01 ` [PATCH 6.1.y 1/3] pagemap: add filemap_grab_folio() Johnny Hao
2026-03-09 5:01 ` [PATCH 6.1.y 2/3] highmem: add kernel-doc for memcpy_*_folio() Johnny Hao
2026-03-09 15:37 ` Matthew Wilcox [this message]
2026-03-09 15:38 ` [PATCH 6.1.y 0/3] Fix patch backport review Matthew Wilcox
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aa7pLpU_-S6quLCR@casper.infradead.org \
--to=willy@infradead.org \
--cc=akpm@linux-foundation.org \
--cc=gregkh@linuxfoundation.org \
--cc=johnny_haocn@sina.com \
--cc=linux-kernel@vger.kernel.org \
--cc=slava@dubeyko.com \
--cc=stable@vger.kernel.org \
--cc=vishal.moola@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox