From: Hungyu Lin <dennylin0707@gmail.com>
To: hughd@google.com
Cc: david@kernel.org, baolin.wang@linux.alibaba.com,
akpm@linux-foundation.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org
Subject: [PATCH v2] shmem: skip empty unmap ranges when punching holes
Date: Thu, 5 Nov 2026 05:56:19 +0000 [thread overview]
Message-ID: <20261105055619.58289-1-dennylin0707@gmail.com> (raw)
When a hole ends before the first page boundary, shmem_fallocate()
calculates unmap_end as -1. Casting it to u64 makes the range check
succeed even though the hole contains no complete pages.
For example, punching one byte at offset zero calls unmap_mapping_range()
with a zero length, which means unmap to the end of the file. The file
contents are preserved outside the hole, but unrelated mappings are
removed and must fault back in. This can be reproduced with fallocate -p
-o 0 -l 1 on a mapped tmpfs file.
Keep unmap_end as the exclusive rounded end instead of subtracting one,
and adjust shmem_falloc.next and the unmap length accordingly. A hole
ending before the first page boundary then has an end of zero, so the
unsigned range check skips the empty range. Retain the unsigned
comparison so rounding the start near the maximum file offset continues
to be handled correctly.
Fixes: 83e4fa9c16e4 ("tmpfs: support fallocate FALLOC_FL_PUNCH_HOLE")
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
---
Changes in v2:
- Use an exclusive unmap_end throughout, following David's suggestion.
- Add Baolin's Reviewed-by.
v1: https://lore.kernel.org/linux-mm/20261027171753.1485-1-dennylin0707@gmail.com/
mm/shmem.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/mm/shmem.c b/mm/shmem.c
index af21a89abb0f..ce92e74baeb7 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -3845,7 +3845,7 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset,
if (mode & FALLOC_FL_PUNCH_HOLE) {
struct address_space *mapping = file->f_mapping;
loff_t unmap_start = round_up(offset, PAGE_SIZE);
- loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
+ loff_t unmap_end = round_down(offset + len, PAGE_SIZE);
DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
/* protected by i_rwsem */
@@ -3856,14 +3856,14 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset,
shmem_falloc.waitq = &shmem_falloc_waitq;
shmem_falloc.start = (u64)unmap_start >> PAGE_SHIFT;
- shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
+ shmem_falloc.next = unmap_end >> PAGE_SHIFT;
spin_lock(&inode->i_lock);
WRITE_ONCE(inode->i_private, &shmem_falloc);
spin_unlock(&inode->i_lock);
if ((u64)unmap_end > (u64)unmap_start)
unmap_mapping_range(mapping, unmap_start,
- 1 + unmap_end - unmap_start, 0);
+ unmap_end - unmap_start, 0);
shmem_truncate_range(inode, offset, offset + len - 1);
/* No need to unmap again: hole-punching leaves COWed pages */
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
--
2.43.0
next reply other threads:[~2026-11-05 5:56 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-05 5:56 Hungyu Lin [this message]
2026-11-05 9:32 ` Baolin Wang
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=20261105055619.58289-1-dennylin0707@gmail.com \
--to=dennylin0707@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=david@kernel.org \
--cc=hughd@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
/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
all inboxes | Powered by JetHome®