Skip to contentSkip to conversation

tmpfs: zero post-eof ranges on file extension

PATCH In review Brian Foster 5 participants 11 messages Nov 21, 2025 – Nov 23

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover tmpfs: zero post-eof ranges on file extension

Hi all,

Apologies for the quicker than expected respin here, but during discussion with Baolin on v2 it became apparent that I factored the first two patches in an unnecessarily confusing way, and I wanted to get that resolved more quickly. Otherwise there are a few smaller changes here that I think are incremental improvements.

Baolin, I left off the R-b tag from v2 just out of caution and how I reworked the first two patches. The end result is mostly similar, but I'd appreciate a quick look before adding that back. Otherwise most of the functionality here should be the same as v2.

Thoughts, reviews, flames appreciated.

Brian

v3:
- Reorder changes in patches 1-2 to factor out existing logic first.
- Use local scope variable in shmem_writeout() fallocend block.
- Tweak combined zeroing logic in shmem_writeout().
- Clean up commit logs.
v2: https://lore.kernel.org/linux-mm/[email protected]/
- Rework to zero uptodate post-eof folios on writeout and full range from EOF on size extension.
- Misc. cleanups: variable renames (pos -> end), code relocation, comment cleanups/removal.
- Update commit log to call out POSIX requirement.
v1: https://lore.kernel.org/linux-mm/[email protected]/

Brian Foster (3):
tmpfs: factor out folio zeroing logic at writeout time
tmpfs: zero post-eof folio ranges on swapout
tmpfs: zero post-eof ranges on file extension

 mm/shmem.c | 135 ++++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 114 insertions(+), 21 deletions(-)
Signature · 1 line
2.51.1
#1 lore raw

PATCH v3 1/3 tmpfs: factor out folio zeroing logic at writeout time

tmpfs currently zeroes !uptodate folios at shmem_writeout() time to ensure they are zeroed in swap. We want to expand this behavior to zero post-eof ranges to better abide POSIX file extension requirements. As a first step, split out the existing zeroing code into a separate block. No functional changes in this patch.

---
 mm/shmem.c | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)
mm/shmem.c +16 -15
index 0a25ee095b86..651602460770 100644
@@ -1626,22 +1626,23 @@ int shmem_writeout(struct folio *folio, struct swap_iocb **plug,
 	 * good idea to continue anyway, once we're pushing into swap.  So
 	 * reactivate the folio, and let shmem_fallocate() quit when too many.
 	 */
+	if (!folio_test_uptodate(folio) && inode->i_private) {
+		struct shmem_falloc *shmem_falloc;
+		spin_lock(&inode->i_lock);
+		shmem_falloc = inode->i_private;
+		if (shmem_falloc &&
+		    !shmem_falloc->waitq &&
+		    index >= shmem_falloc->start &&
+		    index < shmem_falloc->next)
+			shmem_falloc->nr_unswapped += nr_pages;
+		else
+			shmem_falloc = NULL;
+		spin_unlock(&inode->i_lock);
+		if (shmem_falloc)
+			goto redirty;
+	}
+
 	if (!folio_test_uptodate(folio)) {
-		if (inode->i_private) {
-			struct shmem_falloc *shmem_falloc;
-			spin_lock(&inode->i_lock);
-			shmem_falloc = inode->i_private;
-			if (shmem_falloc &&
-			    !shmem_falloc->waitq &&
-			    index >= shmem_falloc->start &&
-			    index < shmem_falloc->next)
-				shmem_falloc->nr_unswapped += nr_pages;
-			else
-				shmem_falloc = NULL;
-			spin_unlock(&inode->i_lock);
-			if (shmem_falloc)
-				goto redirty;
-		}
 		folio_zero_range(folio, 0, folio_size(folio));
 		flush_dcache_folio(folio);
 		folio_mark_uptodate(folio);
Signature · 1 line
2.51.1
#2 lore raw

PATCH v3 2/3 tmpfs: zero post-eof folio ranges on swapout

In reply to Brian Foster

Zero post-eof folios at swap out time to help preserve POSIX requirements and facilitate efficient post-eof zeroing during extending operations. This uses an analogous approach to pagecache writeback to ensure post-eof ranges are zeroed on-disk.

This facilitates file extension zeroing by allowing it to skip swapped out folios that might have been preallocated beyond the original i_size.

---
 mm/shmem.c | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)
mm/shmem.c +19 -5
index 651602460770..97ca2b3dd1b9 100644
@@ -1577,6 +1577,8 @@ int shmem_writeout(struct folio *folio, struct swap_iocb **plug,
 	struct inode *inode = mapping->host;
 	struct shmem_inode_info *info = SHMEM_I(inode);
 	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
+	loff_t i_size = i_size_read(inode);
+	pgoff_t end_index = DIV_ROUND_UP(i_size, PAGE_SIZE);
 	pgoff_t index;
 	int nr_pages;
 	bool split = false;
@@ -1596,9 +1598,9 @@ int shmem_writeout(struct folio *folio, struct swap_iocb **plug,
 	 * (unless fallocate has been used to preallocate beyond EOF).
 	 */
 	if (folio_test_large(folio)) {
-		index = shmem_fallocend(inode,
-			DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE));
-		if ((index > folio->index && index < folio_next_index(folio)) ||
+		pgoff_t fallocend = shmem_fallocend(inode, end_index);
+		if ((fallocend > folio->index &&
+		     fallocend < folio_next_index(folio)) ||
 		    !IS_ENABLED(CONFIG_THP_SWAP))
 			split = true;
 	}
@@ -1642,8 +1644,20 @@ int shmem_writeout(struct folio *folio, struct swap_iocb **plug,
 			goto redirty;
 	}
 
-	if (!folio_test_uptodate(folio)) {
-		folio_zero_range(folio, 0, folio_size(folio));
+	/*
+	 * Ranges beyond EOF must be zeroed at writeout time. This mirrors
+	 * traditional writeback behavior and facilitates zeroing on file size
+	 * changes without having to swap back in.
+	 */
+	if (!folio_test_uptodate(folio) ||
+	    folio_next_index(folio) >= end_index) {
+		size_t from = offset_in_folio(folio, i_size);
+
+		if (!folio_test_uptodate(folio) || index >= end_index)
+			folio_zero_segment(folio, 0, folio_size(folio));
+		else if (from)
+			folio_zero_segment(folio, from, folio_size(folio));
+
 		flush_dcache_folio(folio);
 		folio_mark_uptodate(folio);
 	}
Signature · 1 line
2.51.1
#3 lore raw

PATCH v3 3/3 tmpfs: zero post-eof ranges on file extension

In reply to Brian Foster

POSIX requires that "If the file size is increased, the extended area shall appear as if it were zero-filled". It is possible to use mmap to write past EOF and that data will become visible instead of zeroes. This behavior is reproduced by fstests test generic/363.

Most traditional filesystems zero any post-eof portion of a folio at writeback time or when the file size is extended by truncate or extending writes. This ensures that the previously post-eof range of the folio is zeroed before it is exposed to the file.

The tmpfs writeout path has been updated to zero post-eof folio ranges similar to traditional writeback. This ensures post-eof ranges are zeroed "on disk" and allows size extension zeroing to skip over swap entries as they are already appropriately zeroed.

To that end, introduce a new zeroing helper for proper zeroing on file extending operations. This looks up resident folios between the original and new eof and for those that are uptodate, zeroes them before the associated ranges are exposed to the file. This preserves POSIX semantics and allows generic/363 to pass on tmpfs.

---
 mm/shmem.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 79 insertions(+), 1 deletion(-)
mm/shmem.c +79 -1
index 97ca2b3dd1b9..81dd2bfb0444 100644
@@ -1101,6 +1101,78 @@ static struct folio *shmem_get_partial_folio(struct inode *inode, pgoff_t index)
 	return folio;
 }
 
+/*
+ * Zero a post-EOF range about to be exposed by size extension. Zero from the
+ * current i_size through lend, the latter of which typically refers to the
+ * start offset of an extending operation. Skip swap entries because associated
+ * folios were zeroed at swapout time.
+ */
+static void shmem_zero_eof(struct inode *inode, loff_t lend)
+{
+	struct address_space *mapping = inode->i_mapping;
+	loff_t lstart = i_size_read(inode);
+	pgoff_t index = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
+	pgoff_t end = lend >> PAGE_SHIFT;
+	struct folio_batch fbatch;
+	struct folio *folio;
+	int i;
+	bool same_folio = (lstart >> PAGE_SHIFT) == (lend >> PAGE_SHIFT);
+
+	folio = filemap_lock_folio(mapping, lstart >> PAGE_SHIFT);
+	if (!IS_ERR(folio)) {
+		same_folio = lend < folio_next_pos(folio);
+		index = folio_next_index(folio);
+
+		if (folio_test_uptodate(folio)) {
+			size_t from = offset_in_folio(folio, lstart);
+			size_t len = min_t(loff_t, folio_size(folio) - from,
+					   lend - lstart);
+
+			folio_zero_range(folio, from, len);
+		}
+
+		folio_unlock(folio);
+		folio_put(folio);
+	}
+
+	if (!same_folio) {
+		folio = filemap_lock_folio(mapping, lend >> PAGE_SHIFT);
+		if (!IS_ERR(folio)) {
+			end = folio->index;
+
+			if (folio_test_uptodate(folio)) {
+				size_t len = lend - folio_pos(folio);
+				folio_zero_range(folio, 0, len);
+			}
+
+			folio_unlock(folio);
+			folio_put(folio);
+		}
+	}
+
+	/*
+	 * Zero uptodate folios fully within the target range. Uptodate folios
+	 * beyond EOF are generally unexpected, but can exist if a larger
+	 * falloc'd and uptodate EOF folio is split.
+	 */
+	folio_batch_init(&fbatch);
+	while (index < end) {
+		if (!filemap_get_folios(mapping, &index, end - 1, &fbatch))
+			break;
+		for (i = 0; i < folio_batch_count(&fbatch); i++) {
+			folio = fbatch.folios[i];
+
+			folio_lock(folio);
+			if (folio_test_uptodate(folio) &&
+			    folio->mapping == mapping) {
+				folio_zero_segment(folio, 0, folio_size(folio));
+			}
+			folio_unlock(folio);
+		}
+		folio_batch_release(&fbatch);
+	}
+}
+
 /*
  * Remove range of pages and swap entries from page cache, and free them.
  * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
@@ -1331,6 +1403,8 @@ static int shmem_setattr(struct mnt_idmap *idmap,
 					oldsize, newsize);
 			if (error)
 				return error;
+			if (newsize > oldsize)
+				shmem_zero_eof(inode, newsize);
 			i_size_write(inode, newsize);
 			update_mtime = true;
 		} else {
@@ -3514,6 +3588,8 @@ static ssize_t shmem_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
 	ret = file_update_time(file);
 	if (ret)
 		goto unlock;
+	if (iocb->ki_pos > i_size_read(inode))
+		shmem_zero_eof(inode, iocb->ki_pos);
 	ret = generic_perform_write(iocb, from);
 unlock:
 	inode_unlock(inode);
@@ -3846,8 +3922,10 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset,
 		cond_resched();
 	}
 
-	if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
+	if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) {
+		shmem_zero_eof(inode, offset + len);
 		i_size_write(inode, offset + len);
+	}
 undone:
 	spin_lock(&inode->i_lock);
 	inode->i_private = NULL;
Signature · 1 line
2.51.1
reviewer
#4 lore raw
In reply to Brian Foster
Quoted 8 lines · Brian Foster

Hi all,

Apologies for the quicker than expected respin here, but during discussion with Baolin on v2 it became apparent that I factored the first two patches in an unnecessarily confusing way, and I wanted to get that resolved more quickly. Otherwise there are a few smaller changes here that I think are incremental improvements.

Quoted 6 lines

Baolin, I left off the R-b tag from v2 just out of caution and how I reworked the first two patches. The end result is mostly similar, but I'd appreciate a quick look before adding that back. Otherwise most of the functionality here should be the same as v2.

Thoughts, reviews, flames appreciated.

Overall LGTM. And I tried to do some tmpfs read/write tests, and I didn't notice any obvious regressions. Anyway, let's wait and see if Hugh has any input. Thanks for your work.

Quoted 21 lines

v3:
- Reorder changes in patches 1-2 to factor out existing logic first.
- Use local scope variable in shmem_writeout() fallocend block.
- Tweak combined zeroing logic in shmem_writeout().
- Clean up commit logs.
v2: https://lore.kernel.org/linux-mm/[email protected]/
- Rework to zero uptodate post-eof folios on writeout and full range from EOF on size extension.
- Misc. cleanups: variable renames (pos -> end), code relocation, comment cleanups/removal.
- Update commit log to call out POSIX requirement.
v1: https://lore.kernel.org/linux-mm/[email protected]/

Brian Foster (3):
tmpfs: factor out folio zeroing logic at writeout time
tmpfs: zero post-eof folio ranges on swapout
tmpfs: zero post-eof ranges on file extension

  mm/shmem.c | 135 ++++++++++++++++++++++++++++++++++++++++++++---------
  1 file changed, 114 insertions(+), 21 deletions(-)
Tested-by on the v3 series
#5 lore raw
In reply to Brian Foster

Hi Brian,

I ran into this on tmpfs independently, and the series fixes it on v7.4-rc3 (65e4fd942). Patch 1 needed a one-line context fix (the i_private test in shmem_writeout() is now READ_ONCE(inode->i_private)); patches 2 and 3 applied with reduced context.

Test: a 100-byte file, 'A' written at offset 50, 'X' stored at offset 200 through a shared mapping, then the file extended, then bytes 200 and 50 read back (arm64, QEMU):

  extension                     unpatched    patched
  fallocate(0, 150, 100)        'X'          0
  fallocate(0, 1000, 100)       'X'          0
  ftruncate(fd, 250)            'X'          0
  pwrite 1 byte at 1099         'X'          0

Byte 50 reads 'A' in every case, and ext4 gives 0 on both kernels. I did not test the swapout path (patch 2).

Thanks,
Ding Feng

#6 lore raw
Quoted 17 lines · Ding Feng

Hi Brian,

I ran into this on tmpfs independently, and the series fixes it on v7.4-rc3 (65e4fd942). Patch 1 needed a one-line context fix (the i_private test in shmem_writeout() is now READ_ONCE(inode->i_private)); patches 2 and 3 applied with reduced context.

Test: a 100-byte file, 'A' written at offset 50, 'X' stored at offset 200 through a shared mapping, then the file extended, then bytes 200 and 50 read back (arm64, QEMU):

  extension                     unpatched    patched
  fallocate(0, 150, 100)        'X'          0
  fallocate(0, 1000, 100)       'X'          0
  ftruncate(fd, 250)            'X'          0
  pwrite 1 byte at 1099         'X'          0
Quoted 4 lines

Byte 50 reads 'A' in every case, and ext4 gives 0 on both kernels. I did not test the swapout path (patch 2).

The series is from 2025. In order to do anything with that, either the author has to revive it or someone else (you?) must pick that up to resend.

Thanks!

Signature · 3 lines
Cheers,

David
#7 lore raw

Hi David,

Understood, thanks.

Brian, do you plan to revive this? If not, I can rebase it onto current mainline and resend it as v4 with your authorship kept. Is there anything from the v3 review that still needs to be addressed?

Thanks,
Ding Feng

maintainer
#8 lore raw
Quoted 10 lines · Ding Feng

Hi David,

Understood, thanks.

Brian, do you plan to revive this? If not, I can rebase it onto current mainline and resend it as v4 with your authorship kept. Is there anything from the v3 review that still needs to be addressed?

Thanks,
Ding Feng

I never did report back on Brian's v3 - sorry, two days became a year; but it still sits in my home directory, and I do recall that I was not happy with it, but hadn't worked out how to frame a response.

I haven't searched out yet for whether I made notes (quite possibly not), but the two things that stick in my mind are: shmem_zero_eof() seemed over-the-top, just more code than was justified for dealing with such a minor matter (I know I had a better idea, but I'll need some quiet to resurface that, and this is not a quiet time); and IIRC it was applying a new overhead, not just to one folio, but potentially to a large range, IIRC causing fallocate to zero all new folios immediately, instead of leaving them !uptodate (in some ways a good thing, but significant new overhead for fallocate itself, and probably not intended).

So if reviving Brian's v3, please don't just rebase it, please reconsider it carefully.

Thanks,
Hugh

#9 lore raw

Hi Hugh,

Thanks, that's very helpful. I won't just rebase it.

Rereading v3 against current mainline: on the fallocate path the new folios are still !uptodate when shmem_zero_eof() runs, so it skips them rather than zeroing them. It does still look up and lock every folio in the extended range, so a large fallocate pays for a second pass over it.

Here is a smaller direction. A store past EOF through a mapping can only land in the folio that straddles EOF, since shmem may PMD-map that folio across i_size. The stored bytes leave that folio in two ways: a split, where pages up to fallocend survive as separate folios, or swapout. If we zero beyond i_size at both points (v3 2/3 for swapout, plus __folio_freeze_split_file() for the split), the extending paths only have to zero the tail of the EOF folio when it is resident. That costs the same however large the extension is.

I tested it on 7.4-rc3 under qemu (arm64, zram swap, huge=always and huge=never). Each test stores a byte at offsets 200 and 1MiB of a 100-byte file through a shared mapping, then optionally splits the folio or pages it out with MADV_PAGEOUT, then extends the file with ftruncate, pwrite or fallocate. Zeroing only the EOF folio's tail still exposes the stored bytes after a split with a KEEP_SIZE fallocate, and after swapout. With the split and swapout zeroing added, all 30 cases read zero, the same as v3.

Does that match what you had in mind, or would you rather I wait until you've had time to dig out your idea?

Thanks,
Ding Feng

#10 lore raw
In reply to Ding Feng
Quoted 8 lines · Ding Feng

Hi David,

Understood, thanks.

Brian, do you plan to revive this? If not, I can rebase it onto current mainline and resend it as v4 with your authorship kept. Is there anything from the v3 review that still needs to be addressed?

Hi,

Thanks for looking at and testing this! This was still on my radar as well, but just hasn't been high enough priority to get back to quite yet unfortunately. I was hoping to get back to it some day, but if you have time and interest please go for it..

I don't recall any outstanding work from the last post. I think Hugh's response is probably the latest (i.e. he was digesting and framing further feedback). Otherwise I think the latest post passed all my testing at that time, though I do have some notes on an i_size vs. swap issue that I documented as unrelated, and otherwise don't recall details of.

Note that we did add a little test to cover this in xfstests:
generic/363. That runs the internal fsx tool in a mode that intentionally pollutes post-eof folio ranges and detects unexpected non-zero ranges being mapped within eof. You should be able run generic/363 or fsx directly on top of tmpfs to test (along with your direct testing of course). I also found it useful to run this test under various conditions: huge=always, artificially low mem environment to generate swap activity, etc.

I'll try to at least make a pass at the last version I had posted to try and page back in any details and help with review if I can..

Brian

Quoted 3 lines

Thanks,
Ding Feng