* [PATCH] gfs2: Revalidate the cached writeback mapping after a punch
@ 2026-12-02 14:18 Mark Syms
2026-12-02 14:40 ` sashiko-bot
0 siblings, 1 reply; 2+ 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] 2+ messages in thread
* Re: [PATCH] gfs2: Revalidate the cached writeback mapping after a punch
2026-12-02 14:18 [PATCH] gfs2: Revalidate the cached writeback mapping after a punch Mark Syms
@ 2026-12-02 14:40 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-02 14:40 UTC (permalink / raw)
To: Mark Syms, Andreas Gruenbacher; +Cc: gfs2
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Critical] gfs2: stale cached IOMAP_HOLE reused in
gfs2_writeback_range()
- [Low] gfs2: missing Fixes trailer for writeback iomap caching bug
--
commit febe07e5d07d46ee6a99440c51133ed31590c10c
Author: Mark Syms <mark.syms@citrix.com>
> gfs2: Revalidate the cached writeback mapping after a punch
>
> 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>
[Severity: Low]
This fixes the cached writeback mapping introduced in commit 2164f9b91869.
Should this include:
Fixes: 2164f9b91869 ("gfs2: use iomap for buffered I/O in ordered and writeback mode")
> diff --git a/fs/gfs2/bmap.c b/fs/gfs2/bmap.c
> index 35b0d8d41b98f..9be06b8be98ef 100644
> --- a/fs/gfs2/bmap.c
> +++ b/fs/gfs2/bmap.c
[ ... ]
> @@ -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);
[Severity: Critical]
Can gfs2_writeback_range() still reuse a stale cached IOMAP_HOLE mapping
after new blocks are allocated inside that hole?
When blocksize < PAGE_SIZE, a dirty folio can have an allocated block
followed by hole blocks that are marked dirty (for example, when a short
buffered write on an uptodate folio with folio->private == NULL faults
after the first block and gfs2_iomap_end() punches the rest of the folio).
When writeback reaches the unallocated block in that folio,
gfs2_iomap_get() returns an IOMAP_HOLE mapping spanning up to INT_MAX
bytes and caches it with validity_cookie = seq.
When the retry allocates blocks in that hole via __gfs2_iomap_alloc() and
dirties the following folios, ip->i_dealloc_seq is not incremented. As
writeback advances to those folios, wpc->iomap.validity_cookie still
matches seq, so iomap_add_to_ioend() sees IOMAP_HOLE and skips submitting
I/O while iomap_writeback_folio() clears the dirty state, leaving the
newly allocated blocks unwritten on disk.
Checking wpc->iomap.type != IOMAP_MAPPED (or bumping the sequence counter
on allocation as well) would avoid reusing cached hole mappings.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202141814.668810-1-mark.syms@citrix.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-02 14:40 UTC | newest]
Thread overview: 2+ 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
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.