Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Dominik Maier <dmnk@google.com>
To: Andrew Morton <akpm@linux-foundation.org>,
	David Hildenbrand <david@kernel.org>,
	 Muchun Song <muchun.song@linux.dev>,
	Oscar Salvador <osalvador@suse.de>
Cc: Lance Yang <lance.yang@linux.dev>,
	linux-mm@kvack.org, dmnk+artist@google.com
Subject: [PATCH] hugetlbfs: cap shared mmap reservation at i_size under F_SEAL_GROW
Date: Mon, 30 Nov 2026 17:04:17 +0100	[thread overview]
Message-ID: <20261130160417.2643255-1-dmnk@google.com> (raw)

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) <david@kernel.org>
Signed-off-by: Dominik Maier <dmnk@google.com>
---
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



                 reply	other threads:[~2026-11-30 16:04 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261130160417.2643255-1-dmnk@google.com \
    --to=dmnk@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=david@kernel.org \
    --cc=dmnk+artist@google.com \
    --cc=lance.yang@linux.dev \
    --cc=linux-mm@kvack.org \
    --cc=muchun.song@linux.dev \
    --cc=osalvador@suse.de \
    /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