From nobody Thu Apr 9 14:10:41 2026 Received: from mail115-76.sinamail.sina.com.cn (mail115-76.sinamail.sina.com.cn [218.30.115.76]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 41B8426B742 for ; Mon, 9 Mar 2026 05:02:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=218.30.115.76 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773032525; cv=none; b=bhlLlX5tD+b2W3R+FjZwrWwCDTyljrr/UXI60sEsQszn9mOV2tERZyERpygB86nAI8kdekbAtWp3p3/HhijoJHHRQhOsI5DhpleSZwcoM40yKZ6X43q9kd6bl55LgUDsM4IB4Jg1sPoX6eoJvszQ7kopyb6eeDDkkagIdQvgous= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773032525; c=relaxed/simple; bh=xGzdmJDVkagBJ+m8u/7q/0EnpUau8XUMHQU8NXWzjTA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SwBCq5YKtsKpM2RVCIoMhcGM/9YAjepwn49sKEd4NqsZYl6eHamaxLXQWg4bct7i550lLNeWeDy+6oejHwCzDGny9lyM07pm7Dbj+LfppAP8qZWdGAHyzNvMg6bfNhsys8DsVge+2p9A19RhG06PUptc5f2DisoMNjFyFsK1JdA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sina.com; spf=pass smtp.mailfrom=sina.com; dkim=pass (1024-bit key) header.d=sina.com header.i=@sina.com header.b=AjYatDhl; arc=none smtp.client-ip=218.30.115.76 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sina.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sina.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=sina.com header.i=@sina.com header.b="AjYatDhl" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sina.com; s=201208; t=1773032522; bh=oM9kcvMKEwsqRBBNAyTWcihzlsuQwz3LwFf0Tfp+R1M=; h=From:Subject:Date:Message-Id; b=AjYatDhlAgfG+SRX/ttq0q/VHkI8o6OsRnI+NDGITFLyIcykcjsG0QxVZdgwXa+br yBpDACiivgZjYrV4y8J144yq6bzXQpDsK0yFHuXSGL+yMYQwkh+wJHb3Y7P26vBYZV vVoO6qpBavIOSsRnWzZkss3XTnKbVZedvB39p9yo= X-SMAIL-HELO: pek-lpg-core6.wrs.com Received: from unknown (HELO pek-lpg-core6.wrs.com)([60.247.85.88]) by sina.com (10.185.250.22) with ESMTP id 69AE542F00001DDF; Mon, 9 Mar 2026 13:01:54 +0800 (CST) X-Sender: johnny_haocn@sina.com X-Auth-ID: johnny_haocn@sina.com Authentication-Results: sina.com; spf=none smtp.mailfrom=johnny_haocn@sina.com; dkim=none header.i=none; dmarc=none action=none header.from=johnny_haocn@sina.com X-SMAIL-MID: 1196067602751 X-SMAIL-UIID: 1436BB3E5EBA45179967AAC3E78A0F9A-20260309-130154-1 From: Johnny Hao To: gregkh@linuxfoundation.org, stable@vger.kernel.org Cc: linux-kernel@vger.kernel.org, slava@dubeyko.com, willy@infradead.org, vishal.moola@gmail.com, Andrew Morton , Johnny Hao Subject: [PATCH 6.1.y 2/3] highmem: add kernel-doc for memcpy_*_folio() Date: Mon, 9 Mar 2026 13:01:29 +0800 Message-Id: <20260309050130.912344-3-johnny_haocn@sina.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260309050130.912344-1-johnny_haocn@sina.com> References: <20260309050130.912344-1-johnny_haocn@sina.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Matthew Wilcox (Oracle)" [ Upstream commit 9af47276ed83cc346263e56243756543a2a33c9d ] 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) Signed-off-by: Andrew Morton Signed-off-by: Johnny Hao --- 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, si= ze_t offset, size_t len) kunmap_local(addr); } =20 +/** + * 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 =3D kmap_local_folio(folio, offset); + size_t chunk =3D len; + + if (folio_test_highmem(folio) && + chunk > PAGE_SIZE - offset_in_page(offset)) + chunk =3D PAGE_SIZE - offset_in_page(offset); + memcpy(to, from, chunk); + kunmap_local(from); + + to +=3D chunk; + offset +=3D chunk; + len -=3D 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 =3D kmap_local_folio(folio, offset); + size_t chunk =3D len; + + if (folio_test_highmem(folio) && + chunk > PAGE_SIZE - offset_in_page(offset)) + chunk =3D PAGE_SIZE - offset_in_page(offset); + memcpy(to, from, chunk); + kunmap_local(to); + + from +=3D chunk; + offset +=3D chunk; + len -=3D 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 =3D folio_size(folio) - offset; + + if (folio_test_highmem(folio)) { + size_t max =3D PAGE_SIZE - offset_in_page(offset); + + while (len > max) { + memset(kaddr, 0, max); + kunmap_local(kaddr); + len -=3D max; + offset +=3D max; + max =3D PAGE_SIZE; + kaddr =3D 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 =3D kmap_local_folio(folio, offset); + + VM_BUG_ON(offset + len > folio_size(folio)); + + if (folio_test_highmem(folio)) { + size_t max =3D PAGE_SIZE - offset_in_page(offset); + + while (len > max) { + memcpy(to, from, max); + kunmap_local(to); + len -=3D max; + from +=3D max; + offset +=3D max; + max =3D PAGE_SIZE; + to =3D kmap_local_folio(folio, offset); + } + } + + memcpy(to, from, len); + to =3D 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 =3D offset_in_folio(folio, pos); + char *from =3D kmap_local_folio(folio, offset); + + if (folio_test_highmem(folio)) { + offset =3D offset_in_page(offset); + len =3D min_t(size_t, len, PAGE_SIZE - offset); + } else + len =3D 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. --=20 2.34.1