gfs2 filesystem and dlm development
 help / color / mirror / Atom feed
* [PATCH] gfs2: Revalidate the cached writeback mapping after a punch
@ 2026-12-02 14:18 Mark Syms
  2026-12-02 14:40 ` sashiko-bot
  2026-12-02 15:57 ` [PATCH v2] " Mark Syms
  0 siblings, 2 replies; 3+ messages in thread
From: Mark Syms @ 2026-12-02 14:18 UTC (permalink / raw)
  To: Andreas Gruenbacher; +Cc: Mark Syms, gfs2

gfs2_writeback_range() keeps using the mapping cached in wpc->iomap
for every folio it covers, but punch_hole() can free blocks inside
that mapping while writeback is still walking the file.

This happens on an ordinary buffered write. gfs2_file_buffered_write()
runs with page faults disabled, so a source page that is not resident
makes the write come up short, and gfs2_iomap_end() punches the
unwritten blocks it had just allocated. The retry allocates new
blocks, but writeback writes the retried folios to the freed ones
through the stale mapping. The new blocks are never written and keep
whatever they held before, exposing another file's data. On a
cluster the freed blocks may already belong to another node.

Found in XenServer, where suspend images on GFS2 picked up other
guests' memory. A reproducer that writes 512 KiB extents from a buffer
whose tail is repeatedly dropped with MADV_DONTNEED, with
sync_file_range() running alongside, corrupted 11 blocks in 20 rounds
before this change and none after.

Count deallocations per inode and reuse the cached mapping only while
the count is unchanged.

Signed-off-by: Mark Syms <mark.syms@citrix.com>
---
 fs/gfs2/bmap.c   | 13 +++++++++++--
 fs/gfs2/incore.h |  1 +
 fs/gfs2/super.c  |  1 +
 3 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/fs/gfs2/bmap.c b/fs/gfs2/bmap.c
index 35b0d8d41..9be06b8be 100644
--- a/fs/gfs2/bmap.c
+++ b/fs/gfs2/bmap.c
@@ -1999,6 +1999,9 @@ static int punch_hole(struct gfs2_inode *ip, u64 offset, u64 length)
 	}
 
 out:
+	/* Writeback may hold a mapping to the blocks just freed. */
+	smp_mb__before_atomic();
+	atomic_inc(&ip->i_dealloc_seq);
 	if (gfs2_holder_initialized(&rd_gh))
 		gfs2_glock_dq_uninit(&rd_gh);
 	if (current->journal_info) {
@@ -2515,17 +2518,23 @@ int __gfs2_punch_hole(struct file *file, loff_t offset, loff_t length)
 static ssize_t gfs2_writeback_range(struct iomap_writepage_ctx *wpc,
 		struct folio *folio, u64 offset, unsigned int len, u64 end_pos)
 {
-	if (WARN_ON_ONCE(gfs2_is_stuffed(GFS2_I(wpc->inode))))
+	struct gfs2_inode *ip = GFS2_I(wpc->inode);
+	u32 seq;
+
+	if (WARN_ON_ONCE(gfs2_is_stuffed(ip)))
 		return -EIO;
 
+	seq = atomic_read_acquire(&ip->i_dealloc_seq);
 	if (offset < wpc->iomap.offset ||
-	    offset >= wpc->iomap.offset + wpc->iomap.length) {
+	    offset >= wpc->iomap.offset + wpc->iomap.length ||
+	    wpc->iomap.validity_cookie != seq) {
 		int ret;
 
 		memset(&wpc->iomap, 0, sizeof(wpc->iomap));
 		ret = gfs2_iomap_get(wpc->inode, offset, INT_MAX, &wpc->iomap);
 		if (ret)
 			return ret;
+		wpc->iomap.validity_cookie = seq;
 	}
 
 	return iomap_add_to_ioend(wpc, folio, offset, end_pos, len);
diff --git a/fs/gfs2/incore.h b/fs/gfs2/incore.h
index 1ff19f87d..997c9de9b 100644
--- a/fs/gfs2/incore.h
+++ b/fs/gfs2/incore.h
@@ -414,6 +414,7 @@ struct gfs2_inode {
 	struct gfs2_blkreserv i_res; /* rgrp multi-block reservation */
 	u64 i_goal;	/* goal block for allocations */
 	atomic_t i_sizehint;  /* hint of the write size */
+	atomic_t i_dealloc_seq; /* bumped when blocks are deallocated */
 	struct rw_semaphore i_rw_mutex;
 	struct list_head i_ordered;
 	__be64 *i_hash_cache;
diff --git a/fs/gfs2/super.c b/fs/gfs2/super.c
index c7777b856..f89e18317 100644
--- a/fs/gfs2/super.c
+++ b/fs/gfs2/super.c
@@ -1551,6 +1551,7 @@ static struct inode *gfs2_alloc_inode(struct super_block *sb)
 	RB_CLEAR_NODE(&ip->i_res.rs_node);
 	ip->i_diskflags = 0;
 	ip->i_rahead = 0;
+	atomic_set(&ip->i_dealloc_seq, 0);
 	return &ip->i_inode;
 }
 
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-02 15:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 14:18 [PATCH] gfs2: Revalidate the cached writeback mapping after a punch Mark Syms
2026-12-02 14:40 ` sashiko-bot
2026-12-02 15:57 ` [PATCH v2] " Mark Syms

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox