git: 41e054033ed9 - main - tmpfs: fix a hang reachable through hole-punching

From: Kyle Evans <kevans_at_FreeBSD.org>
Date: Tue, 17 Nov 2026 21:11:23 UTC
The branch main has been updated by kevans:

URL: https://cgit.FreeBSD.org/src/commit/?id=41e054033ed91099aaf0dd11c09db4f6752b95b6

commit 41e054033ed91099aaf0dd11c09db4f6752b95b6
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2026-11-17 21:10:38 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2026-11-17 21:11:05 +0000

    tmpfs: fix a hang reachable through hole-punching
    
    Deallocate requests that clip only the start of a single page need to be
    taken into account as well- it misses the first branch because
    startofs == 0, but then it also misses the last branch because the start
    and end are on the same page.
    
    Handling this case in either branch is equally valid, so I've
    arbitrarily chosen the first branch since it already has single-page
    logic.
    
    Reviewed by:    kib
    Differential Revision:  https://reviews.freebsd.org/D61490
---
 sys/fs/tmpfs/tmpfs_subr.c       | 10 +++++++---
 tests/sys/file/fspacectl_test.c | 27 +++++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/sys/fs/tmpfs/tmpfs_subr.c b/sys/fs/tmpfs/tmpfs_subr.c
index cfdd5cd1f973..b29b555dedc2 100644
--- a/sys/fs/tmpfs/tmpfs_subr.c
+++ b/sys/fs/tmpfs/tmpfs_subr.c
@@ -1963,9 +1963,10 @@ tmpfs_reg_punch_hole(struct vnode *vp, off_t *offset, off_t *length)
 
 	/*
 	 * If there is a partial page at the beginning of the hole-punching
-	 * request, fill the partial page with zeroes.
+	 * request or if we're only punching a hole within a single page, fill
+	 * the partial page with zeroes.
 	 */
-	if (startofs != 0) {
+	if (startofs != 0 || pistart == piend) {
 		end = pistart != piend ? PAGE_SIZE : endofs;
 		error = tmpfs_partial_page_invalidate(object, pistart, startofs,
 		    end, FALSE);
@@ -1973,6 +1974,9 @@ tmpfs_reg_punch_hole(struct vnode *vp, off_t *offset, off_t *length)
 			goto out;
 		off += end - startofs;
 		len -= end - startofs;
+
+		if (pistart == piend)
+			goto out;
 	}
 
 	/*
@@ -1988,7 +1992,7 @@ tmpfs_reg_punch_hole(struct vnode *vp, off_t *offset, off_t *length)
 	 * If there is a partial page at the end of the hole-punching request,
 	 * fill the partial page with zeroes.
 	 */
-	if (endofs != 0 && pistart != piend) {
+	if (endofs != 0) {
 		error = tmpfs_partial_page_invalidate(object, piend, 0, endofs,
 		    FALSE);
 		if (error != 0)
diff --git a/tests/sys/file/fspacectl_test.c b/tests/sys/file/fspacectl_test.c
index 714474b76b05..b358e01f13d2 100644
--- a/tests/sys/file/fspacectl_test.c
+++ b/tests/sys/file/fspacectl_test.c
@@ -325,6 +325,32 @@ ATF_TC_BODY(unaligned_dealloc_eof, tc)
 	ATF_REQUIRE(close(fd) == 0);
 }
 
+/*
+ * Check deallocation of an aligned range that ends within its first block.
+ * tmpfs used to make no progress on such a range, leaving the caller
+ * spinning in vn_deallocate_impl() forever.
+ */
+ATF_TC_WITHOUT_HEAD(aligned_dealloc_partial);
+ATF_TC_BODY(aligned_dealloc_partial, tc)
+{
+	struct spacectl_range range;
+	off_t offset, length;
+	blksize_t blocksize;
+	int fd;
+
+	ATF_REQUIRE((blocksize = fd_get_blksize()) != -1);
+	range.r_offset = offset = blocksize;
+	range.r_len = length = blocksize / 2;
+
+	ATF_REQUIRE((fd = open("sys_fspacectl_testfile",
+			 O_CREAT | O_RDWR | O_TRUNC, 0600)) != -1);
+	ATF_REQUIRE(fill(fd, 0, file_max_blocks * blocksize) == 0);
+	ATF_CHECK(fspacectl(fd, SPACECTL_DEALLOC, &range, 0, &range) == 0);
+	ATF_CHECK(check_content_dealloc(fd, offset, length,
+		      file_max_blocks * blocksize) == 0);
+	ATF_REQUIRE(close(fd) == 0);
+}
+
 ATF_TP_ADD_TCS(tp)
 {
 	ATF_TP_ADD_TC(tp, aligned_dealloc);
@@ -333,6 +359,7 @@ ATF_TP_ADD_TCS(tp)
 	ATF_TP_ADD_TC(tp, unaligned_dealloc_eof);
 	ATF_TP_ADD_TC(tp, aligned_dealloc_offmax);
 	ATF_TP_ADD_TC(tp, unaligned_dealloc_offmax);
+	ATF_TP_ADD_TC(tp, aligned_dealloc_partial);
 
 	return atf_no_error();
 }