linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Brian Foster <bfoster@redhat.com>
To: linux-mm@kvack.org
Cc: Hugh Dickins <hughd@google.com>,
	Baolin Wang <baolin.wang@linux.alibaba.com>
Subject: [PATCH v3 2/3] tmpfs: zero post-eof folio ranges on swapout
Date: Fri, 21 Nov 2025 10:22:45 -0500	[thread overview]
Message-ID: <20251121152246.1023918-3-bfoster@redhat.com> (raw)
In-Reply-To: <20251121152246.1023918-1-bfoster@redhat.com>

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.

Signed-off-by: Brian Foster <bfoster@redhat.com>
---
 mm/shmem.c | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)

diff --git a/mm/shmem.c b/mm/shmem.c
index 651602460770..97ca2b3dd1b9 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -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);
 	}
-- 
2.51.1



  parent reply	other threads:[~2025-11-21 15:22 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-21 15:22 [PATCH v3 0/3] tmpfs: zero post-eof ranges on file extension Brian Foster
2025-11-21 15:22 ` [PATCH v3 1/3] tmpfs: factor out folio zeroing logic at writeout time Brian Foster
2025-11-21 15:22 ` Brian Foster [this message]
2025-11-21 15:22 ` [PATCH v3 3/3] tmpfs: zero post-eof ranges on file extension Brian Foster
2025-11-25  2:20 ` [PATCH v3 0/3] " 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=20251121152246.1023918-3-bfoster@redhat.com \
    --to=bfoster@redhat.com \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=hughd@google.com \
    --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