[RFC PATCH 12/39] mm: hugetlb: Move and expose hugetlb_zero_partial_page()

Ackerley Tng
2024-09-10T23:44:53Z

This will used by guest_memfd in a later patch.

Signed-off-by: Ackerley Tng <ackerleytng@google.com>
---
fs/hugetlbfs/inode.c | 33 +++++----------------------------
include/linux/hugetlb.h | 3 +++
mm/hugetlb.c | 21 +++++++++++++++++++++
3 files changed, 29 insertions(+), 28 deletions(-)

diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
index 300a6ef300c1..f76001418672 100644
--- a/fs/hugetlbfs/inode.c
+++ b/fs/hugetlbfs/inode.c
@@ -720,29 +720,6 @@ static void hugetlb_vmtruncate(struct inode *inode, loff_t offset)
remove_inode_hugepages(inode, offset, LLONG_MAX);
}

-static void hugetlbfs_zero_partial_page(struct hstate *h,
- struct address_space *mapping,
- loff_t start,
- loff_t end)
-{
- pgoff_t idx = start >> huge_page_shift(h);
- struct folio *folio;
-
- folio = filemap_lock_hugetlb_folio(h, mapping, idx);
- if (IS_ERR(folio))
- return;
-
- start = start & ~huge_page_mask(h);
- end = end & ~huge_page_mask(h);
- if (!end)
- end = huge_page_size(h);
-
- folio_zero_segment(folio, (size_t)start, (size_t)end);
-
- folio_unlock(folio);
- folio_put(folio);
-}
-
static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
{
struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode);
@@ -768,9 +745,10 @@ static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
i_mmap_lock_write(mapping);

/* If range starts before first full page, zero partial page. */
- if (offset < hole_start)
- hugetlbfs_zero_partial_page(h, mapping,
- offset, min(offset + len, hole_start));
+ if (offset < hole_start) {
+ hugetlb_zero_partial_page(h, mapping, offset,
+ min(offset + len, hole_start));
+ }

/* Unmap users of full pages in the hole. */
if (hole_end > hole_start) {
@@ -782,8 +760,7 @@ static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)

/* If range extends beyond last full page, zero partial page. */
if ((offset + len) > hole_end && (offset + len) > hole_start)
- hugetlbfs_zero_partial_page(h, mapping,
- hole_end, offset + len);
+ hugetlb_zero_partial_page(h, mapping, hole_end, offset + len);

i_mmap_unlock_write(mapping);

diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
index 4d47bf94c211..752062044b0b 100644
--- a/include/linux/hugetlb.h
+++ b/include/linux/hugetlb.h
@@ -124,6 +124,9 @@ long hugepage_subpool_put_pages(struct hugepage_subpool *spool, long delta);

int hugetlb_acct_memory(struct hstate *h, long delta);

+void hugetlb_zero_partial_page(struct hstate *h, struct address_space *mapping,
+ loff_t start, loff_t end);
+
void hugetlb_dup_vma_private(struct vm_area_struct *vma);
void clear_vma_resv_huge_pages(struct vm_area_struct *vma);
int move_hugetlb_page_tables(struct vm_area_struct *vma,
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 5a37b03e1361..372d8294fb2f 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -1989,6 +1989,27 @@ void free_huge_folio(struct folio *folio)
}
}

+void hugetlb_zero_partial_page(struct hstate *h, struct address_space *mapping,
+ loff_t start, loff_t end)
+{
+ pgoff_t idx = start >> huge_page_shift(h);
+ struct folio *folio;
+
+ folio = filemap_lock_hugetlb_folio(h, mapping, idx);
+ if (IS_ERR(folio))
+ return;
+
+ start = start & ~huge_page_mask(h);
+ end = end & ~huge_page_mask(h);
+ if (!end)
+ end = huge_page_size(h);
+
+ folio_zero_segment(folio, (size_t)start, (size_t)end);
+
+ folio_unlock(folio);
+ folio_put(folio);
+}
+
/*
* Must be called with the hugetlb lock held
*/
--
2.46.0.598.g6f2099f65c-goog