Shared mmap() on a growth-sealed hugetlbfs file still reserves huge pages past EOF, pinning pages that can never be faulted in. With F_SEAL_GROW set, mmap() no longer extends the file size, but it still reserves pages up to the end of the mapping. On shared mappings, those reservations beyond EOF stay recorded on the inode across munmap() and remain charged against huge page pools and cgroups until the file is truncated or closed. Cap the shared reservation end offset at the sealed file size so mmap() succeeds without reserving unreachable huge pages. Fixes: ff62a3421044 ("hugetlb: implement memfd sealing") Cc: stable@vger.kernel.org Suggested-by: David Hildenbrand (Arm) Signed-off-by: Dominik Maier --- Based on mm-new (atop "hugetlbfs: do not grow a sealed file in mmap()"). Follows up on: https://lore.kernel.org/linux-mm/603ff6af-104a-4b00-8f66-94d7f79fa540@kernel.org/ Found with an AI-assisted code review tool (ARTIST) and verified in QEMU/KVM across F_SEAL_GROW shared/private mappings and unsealed files. fs/hugetlbfs/inode.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index f1a8208ffc81..3454d51f78e8 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -101,6 +101,7 @@ static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma) { struct inode *inode = file_inode(file); loff_t len, vma_len; + long from, to; int ret; struct hstate *h = hstate_file(file); vma_flags_t vma_flags; @@ -150,10 +151,14 @@ static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma) if (inode->i_flags & S_PRIVATE) vma_flags_set(&vma_flags, VMA_NORESERVE_BIT); - if (hugetlb_reserve_pages(inode, - vma->vm_pgoff >> huge_page_order(h), - len >> huge_page_shift(h), vma, - vma_flags) < 0) + from = vma->vm_pgoff >> huge_page_order(h); + to = len >> huge_page_shift(h); + if (vma_test(vma, VMA_MAYSHARE_BIT) && + (HUGETLBFS_I(inode)->seals & F_SEAL_GROW)) + to = max_t(long, from, + min(len, inode->i_size) >> huge_page_shift(h)); + + if (hugetlb_reserve_pages(inode, from, to, vma, vma_flags) < 0) goto out; ret = 0; base-commit: 0bbbdf85abf48b019cb8dd7a6e87f0a8995fdcd2 -- 2.56.1.911.g883d8bcb20-goog