From: Mark Syms <mark.syms@citrix.com>
To: Andreas Gruenbacher <agruenba@redhat.com>
Cc: Mark Syms <mark.syms@citrix.com>,
gfs2@lists.linux.dev, stable@vger.kernel.org
Subject: [PATCH v2] gfs2: Revalidate the cached writeback mapping after a punch
Date: Wed, 2 Dec 2026 15:57:51 +0000 [thread overview]
Message-ID: <20261202155751.690482-1-mark.syms@citrix.com> (raw)
In-Reply-To: <20261202141814.668810-1-mark.syms@citrix.com>
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. Never reuse a cached hole either: blocks can be
allocated inside it without anything being deallocated.
Cc: stable@vger.kernel.org
Fixes: 2164f9b91869 ("gfs2: use iomap for buffered I/O in ordered and writeback mode")
Signed-off-by: Mark Syms <mark.syms@citrix.com>
---
v2: Also refuse to reuse a cached IOMAP_HOLE, which allocations inside it do
not invalidate (sashiko); add Fixes.
fs/gfs2/bmap.c | 16 +++++++++++++---
fs/gfs2/incore.h | 1 +
fs/gfs2/super.c | 1 +
3 files changed, 15 insertions(+), 3 deletions(-)
diff --git a/fs/gfs2/bmap.c b/fs/gfs2/bmap.c
index 35b0d8d41b98..9832dcd5132f 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,24 @@ 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;
- if (offset < wpc->iomap.offset ||
- offset >= wpc->iomap.offset + wpc->iomap.length) {
+ seq = atomic_read_acquire(&ip->i_dealloc_seq);
+ if (wpc->iomap.type != IOMAP_MAPPED ||
+ offset < wpc->iomap.offset ||
+ 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 1ff19f87d3bd..997c9de9b4de 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 c7777b8565a2..f89e18317f5f 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
prev parent reply other threads:[~2026-12-02 15:57 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` Mark Syms [this message]
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=20261202155751.690482-1-mark.syms@citrix.com \
--to=mark.syms@citrix.com \
--cc=agruenba@redhat.com \
--cc=gfs2@lists.linux.dev \
--cc=stable@vger.kernel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.