* [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; 10+ 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] 10+ 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 2026-12-02 15:57 ` [PATCH v2] " Mark Syms 1 sibling, 0 replies; 10+ 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] 10+ messages in thread
* [PATCH v2] 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 @ 2026-12-02 15:57 ` Mark Syms 2026-12-02 20:54 ` Andreas Gruenbacher 2026-12-04 16:08 ` [PATCH v3 0/2] " Mark Syms 1 sibling, 2 replies; 10+ messages in thread From: Mark Syms @ 2026-12-02 15:57 UTC (permalink / raw) To: Andreas Gruenbacher; +Cc: Mark Syms, gfs2, stable 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 ^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH v2] gfs2: Revalidate the cached writeback mapping after a punch 2026-12-02 15:57 ` [PATCH v2] " Mark Syms @ 2026-12-02 20:54 ` Andreas Gruenbacher 2026-12-04 16:08 ` [PATCH v3 0/2] " Mark Syms 1 sibling, 0 replies; 10+ messages in thread From: Andreas Gruenbacher @ 2026-12-02 20:54 UTC (permalink / raw) To: Mark Syms; +Cc: gfs2, stable Mark, On Wed, 2 Dec 2026 at 16:58, Mark Syms <mark.syms@citrix.com> wrote: > 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. good catch, thank you. > 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. I think we should track allocations and deallocations, and if only to keep things more in line with other filesystems. That way, we won't have to make exceptions for holes. > 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) { Can the cookie check go into a gfs2_iomap_valid(inode, iomap) function? While not strictly necessary, can we then wire that function up to .iomap_valid() as well? > 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; This can go in __gfs2_iomap_get(). > } > > 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 */ There is no need for an atomic; see i_es_seq in ext4, for example. > 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 > Again, thank you for this posting. Andreas ^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v3 0/2] gfs2: Revalidate the cached writeback mapping after a punch 2026-12-02 15:57 ` [PATCH v2] " Mark Syms 2026-12-02 20:54 ` Andreas Gruenbacher @ 2026-12-04 16:08 ` Mark Syms 2026-12-04 16:08 ` [PATCH v3 1/2] gfs2: minor punch_hole error path cleanup Mark Syms ` (2 more replies) 1 sibling, 3 replies; 10+ messages in thread From: Mark Syms @ 2026-12-04 16:08 UTC (permalink / raw) To: Andreas Gruenbacher; +Cc: Mark Syms, gfs2, Christoph Hellwig gfs2 writeback reuses its cached mapping across a short-write punch and reallocation, so the retried data goes to the freed blocks and the new blocks keep stale contents. Patch 2 fixes that; patch 1 is Andreas's punch_hole() error-path cleanup, which patch 2 builds on. v3: - Count allocations as well as deallocations in a u64 i_mapping_seq, updated under i_rw_mutex where the inode's mapping changes: on allocation, unstuffing, and at each transaction boundary of punch_hole() (Andreas). - Set the cookie in __gfs2_iomap_get(); check it in writeback only (Andreas). - Add patch 1 (Andreas). v2: - Refuse to reuse a cached IOMAP_HOLE (sashiko); add Fixes. Andreas Gruenbacher (1): gfs2: minor punch_hole error path cleanup Mark Syms (1): gfs2: Revalidate the cached writeback mapping after a punch fs/gfs2/bmap.c | 53 ++++++++++++++++++++++++++++++++++++++++-------- fs/gfs2/incore.h | 1 + fs/gfs2/super.c | 1 + 3 files changed, 46 insertions(+), 9 deletions(-) -- 2.56.0 ^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v3 1/2] gfs2: minor punch_hole error path cleanup 2026-12-04 16:08 ` [PATCH v3 0/2] " Mark Syms @ 2026-12-04 16:08 ` Mark Syms 2026-12-04 16:37 ` sashiko-bot 2026-12-04 16:08 ` [PATCH v3 2/2] gfs2: Revalidate the cached writeback mapping after a punch Mark Syms 2026-12-04 16:26 ` [PATCH v3 0/2] " Andreas Gruenbacher 2 siblings, 1 reply; 10+ messages in thread From: Mark Syms @ 2026-12-04 16:08 UTC (permalink / raw) To: Andreas Gruenbacher; +Cc: gfs2, Christoph Hellwig, stable, Mark Syms From: Andreas Gruenbacher <agruenba@redhat.com> When an error is encountered after deallocating some blocks, do not skip the statfs and quota update. The cleanup code already ends any pending transaction; there is no need to duplicate that code. Cc: stable@vger.kernel.org # prerequisite for "gfs2: Revalidate the cached writeback mapping after a punch" Signed-off-by: Andreas Gruenbacher <agruenba@redhat.com> Signed-off-by: Mark Syms <mark.syms@citrix.com> --- fs/gfs2/bmap.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/fs/gfs2/bmap.c b/fs/gfs2/bmap.c index 35b0d8d41b98..bb9cd9ef8329 100644 --- a/fs/gfs2/bmap.c +++ b/fs/gfs2/bmap.c @@ -1981,12 +1981,18 @@ static int punch_hole(struct gfs2_inode *ip, u64 offset, u64 length) } } +out: if (btotal) { if (current->journal_info == NULL) { - ret = gfs2_trans_begin(sdp, RES_DINODE + RES_STATFS + - RES_QUOTA, 0); - if (ret) - goto out; + int ret2; + + ret2 = gfs2_trans_begin(sdp, RES_DINODE + RES_STATFS + + RES_QUOTA, 0); + if (ret2) { + if (!ret) + ret = ret2; + goto out2; + } down_write(&ip->i_rw_mutex); } gfs2_statfs_change(sdp, 0, +btotal, 0); @@ -1994,11 +2000,9 @@ static int punch_hole(struct gfs2_inode *ip, u64 offset, u64 length) inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); gfs2_trans_add_meta(gl, dibh); gfs2_dinode_out(ip, dibh->b_data); - up_write(&ip->i_rw_mutex); - gfs2_trans_end(sdp); } -out: +out2: if (gfs2_holder_initialized(&rd_gh)) gfs2_glock_dq_uninit(&rd_gh); if (current->journal_info) { -- 2.56.0 ^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH v3 1/2] gfs2: minor punch_hole error path cleanup 2026-12-04 16:08 ` [PATCH v3 1/2] gfs2: minor punch_hole error path cleanup Mark Syms @ 2026-12-04 16:37 ` sashiko-bot 0 siblings, 0 replies; 10+ messages in thread From: sashiko-bot @ 2026-12-04 16:37 UTC (permalink / raw) To: Mark Syms, Andreas Gruenbacher; +Cc: gfs2, Christoph Hellwig Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] gfs2: missing Fixes tag for punch_hole() error-path bugs -- commit 48a4cf4d62ea976d62f135901988d8490776dce2 Author: Andreas Gruenbacher <agruenba@redhat.com> > gfs2: minor punch_hole error path cleanup > > When an error is encountered after deallocating some blocks, do not skip > the statfs and quota update. > > The cleanup code already ends any pending transaction; there is no need > to duplicate that code. > > Cc: stable@vger.kernel.org # prerequisite for "gfs2: Revalidate the cached writeback mapping after a punch" > Signed-off-by: Andreas Gruenbacher <agruenba@redhat.com> > Signed-off-by: Mark Syms <mark.syms@citrix.com> [Severity: Low] This fixes two error-path bugs in punch_hole(): skipping the statfs, quota, and dinode updates after freeing blocks, and clobbering an earlier error in ret when gfs2_trans_begin() succeeds. Both were introduced when trunc_dealloc() (later renamed punch_hole()) was added. Should this include: Fixes: d552a2b9b33e ("GFS2: Non-recursive delete") -- Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796393524.git.mark.syms@citrix.com?part=1 ^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v3 2/2] gfs2: Revalidate the cached writeback mapping after a punch 2026-12-04 16:08 ` [PATCH v3 0/2] " Mark Syms 2026-12-04 16:08 ` [PATCH v3 1/2] gfs2: minor punch_hole error path cleanup Mark Syms @ 2026-12-04 16:08 ` Mark Syms 2026-12-04 17:11 ` sashiko-bot 2026-12-04 16:26 ` [PATCH v3 0/2] " Andreas Gruenbacher 2 siblings, 1 reply; 10+ messages in thread From: Mark Syms @ 2026-12-04 16:08 UTC (permalink / raw) To: Andreas Gruenbacher; +Cc: Mark Syms, gfs2, Christoph Hellwig, stable 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 block allocations and deallocations per inode. In writeback, treat a mapping as valid only while the count is unchanged. Fixes: 2164f9b91869 ("gfs2: use iomap for buffered I/O in ordered and writeback mode") Cc: Christoph Hellwig <hch@lst.de> Cc: stable@vger.kernel.org Co-developed-by: Andreas Gruenbacher <agruenba@redhat.com> Signed-off-by: Andreas Gruenbacher <agruenba@redhat.com> Signed-off-by: Mark Syms <mark.syms@citrix.com> --- fs/gfs2/bmap.c | 35 +++++++++++++++++++++++++++++++++-- fs/gfs2/incore.h | 1 + fs/gfs2/super.c | 1 + 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/fs/gfs2/bmap.c b/fs/gfs2/bmap.c index bb9cd9ef8329..087362bc76c5 100644 --- a/fs/gfs2/bmap.c +++ b/fs/gfs2/bmap.c @@ -88,6 +88,20 @@ static int gfs2_unstuffer_folio(struct gfs2_inode *ip, struct buffer_head *dibh, return 0; } +/* Callers hold i_rw_mutex for write; gfs2_iomap_valid() reads locklessly. */ +static u64 gfs2_update_mapping_seq(struct gfs2_inode *ip) +{ + u64 seq = ip->i_mapping_seq + 1; + + WRITE_ONCE(ip->i_mapping_seq, seq); + return seq; +} + +static bool gfs2_iomap_valid(struct inode *inode, const struct iomap *iomap) +{ + return iomap->validity_cookie == READ_ONCE(GFS2_I(inode)->i_mapping_seq); +} + static int __gfs2_unstuff_inode(struct gfs2_inode *ip, struct folio *folio) { struct inode *inode = &ip->i_inode; @@ -167,6 +181,8 @@ int gfs2_unstuff_dinode(struct gfs2_inode *ip) if (IS_ERR(folio)) goto out; error = __gfs2_unstuff_inode(ip, folio); + if (!error) + gfs2_update_mapping_seq(ip); folio_unlock(folio); folio_put(folio); out: @@ -780,6 +796,7 @@ static int __gfs2_iomap_alloc(struct inode *inode, struct iomap *iomap, ip->i_height = mp->mp_fheight; gfs2_add_inode_blocks(&ip->i_inode, alloced); gfs2_dinode_out(ip, dibh->b_data); + iomap->validity_cookie = gfs2_update_mapping_seq(ip); out: up_write(&ip->i_rw_mutex); return ret; @@ -862,6 +879,7 @@ static int __gfs2_iomap_get(struct inode *inode, loff_t pos, loff_t length, return -EINVAL; down_read(&ip->i_rw_mutex); + iomap->validity_cookie = READ_ONCE(ip->i_mapping_seq); ret = gfs2_meta_inode_buffer(ip, &dibh); if (ret) @@ -1011,9 +1029,18 @@ static void gfs2_iomap_put_folio(struct inode *inode, loff_t pos, } } +/* + * gfs2 holds the inode glock of the affected inode across iomap operations. + * This prevents mappings from being invalidated, so .iomap_valid doesn't need + * to be wired up here. + * + * The one exception is writeback; see gfs2_writeback_range() for that. + */ + const struct iomap_write_ops gfs2_iomap_write_ops = { .get_folio = gfs2_iomap_get_folio, .put_folio = gfs2_iomap_put_folio, + /* .iomap_valid = gfs2_iomap_valid, */ }; static int gfs2_iomap_begin_write(struct inode *inode, loff_t pos, @@ -1627,6 +1654,7 @@ static int sweep_bh_for_rgrps(struct gfs2_inode *ip, struct gfs2_holder *rd_gh, gfs2_trans_add_meta(gl, dibh); gfs2_dinode_out(ip, dibh->b_data); brelse(dibh); + gfs2_update_mapping_seq(ip); up_write(&ip->i_rw_mutex); gfs2_trans_end(sdp); buf_in_tr = false; @@ -1983,7 +2011,9 @@ static int punch_hole(struct gfs2_inode *ip, u64 offset, u64 length) out: if (btotal) { - if (current->journal_info == NULL) { + if (current->journal_info) { + gfs2_update_mapping_seq(ip); + } else { int ret2; ret2 = gfs2_trans_begin(sdp, RES_DINODE + RES_STATFS + @@ -2523,7 +2553,8 @@ static ssize_t gfs2_writeback_range(struct iomap_writepage_ctx *wpc, return -EIO; if (offset < wpc->iomap.offset || - offset >= wpc->iomap.offset + wpc->iomap.length) { + offset >= wpc->iomap.offset + wpc->iomap.length || + !gfs2_iomap_valid(wpc->inode, &wpc->iomap)) { int ret; memset(&wpc->iomap, 0, sizeof(wpc->iomap)); diff --git a/fs/gfs2/incore.h b/fs/gfs2/incore.h index 1ff19f87d3bd..21997b05fdad 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 */ + u64 i_mapping_seq; /* changes when blocks are allocated or freed */ 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..70d1fe5d0964 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; + ip->i_mapping_seq = 0; return &ip->i_inode; } -- 2.56.0 ^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH v3 2/2] gfs2: Revalidate the cached writeback mapping after a punch 2026-12-04 16:08 ` [PATCH v3 2/2] gfs2: Revalidate the cached writeback mapping after a punch Mark Syms @ 2026-12-04 17:11 ` sashiko-bot 0 siblings, 0 replies; 10+ messages in thread From: sashiko-bot @ 2026-12-04 17:11 UTC (permalink / raw) To: Mark Syms, Andreas Gruenbacher; +Cc: gfs2, Christoph Hellwig Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] gfs2: deferred i_mapping_seq update in punch_hole() races with writeback - [Medium] gfs2: i_mapping_seq bump in __gfs2_iomap_alloc() invalidates writeback cache - [Medium] gfs2: inode_go_inval() misses i_mapping_seq update on cluster invalidation -- commit 576cadb6ded78f5b2a77c217a1a22d5cfb731666 Author: Mark Syms <mark.syms@citrix.com> gfs2: Revalidate the cached writeback mapping after a punch This commit adds a per-inode i_mapping_seq counter that is incremented on unstuffing, block allocation, and block deallocation, and checks it via gfs2_iomap_valid() in gfs2_writeback_range() to invalidate cached writeback mappings. > diff --git a/fs/gfs2/bmap.c b/fs/gfs2/bmap.c > index bb9cd9ef8329a..087362bc76c5b 100644 > --- a/fs/gfs2/bmap.c > +++ b/fs/gfs2/bmap.c [ ... ] > @@ -780,6 +796,7 @@ static int __gfs2_iomap_alloc(struct inode *inode, struct iomap *iomap, > ip->i_height = mp->mp_fheight; > gfs2_add_inode_blocks(&ip->i_inode, alloced); > gfs2_dinode_out(ip, dibh->b_data); > + iomap->validity_cookie = gfs2_update_mapping_seq(ip); > out: > up_write(&ip->i_rw_mutex); > return ret; [Severity: Medium] Does updating i_mapping_seq on every allocation in __gfs2_iomap_alloc() unnecessarily thrash writeback's cached mapping during appending writes? GFS2 allocation only fills zero pointers and never alters existing IOMAP_MAPPED extents. When background writeback or sync_file_range() runs concurrently with appending buffered writes, each allocation invalidates wpc->iomap and forces gfs2_writeback_range() back into gfs2_iomap_get(). That contends directly on ip->i_rw_mutex with __gfs2_iomap_alloc(), re-walks the metadata tree, and repeats gfs2_extent_length() scans instead of reusing the cached extent across folios. Since gfs2_writeback_range() is the only caller of gfs2_iomap_valid(), checking wpc->iomap.type != IOMAP_MAPPED there would avoid caching holes without bumping i_mapping_seq on allocation. [ ... ] > @@ -1627,6 +1654,7 @@ static int sweep_bh_for_rgrps(struct gfs2_inode *ip, struct gfs2_holder *rd_gh, > gfs2_trans_add_meta(gl, dibh); > gfs2_dinode_out(ip, dibh->b_data); > brelse(dibh); > + gfs2_update_mapping_seq(ip); > up_write(&ip->i_rw_mutex); > gfs2_trans_end(sdp); > buf_in_tr = false; [ ... ] > @@ -1983,7 +2011,9 @@ static int punch_hole(struct gfs2_inode *ip, u64 offset, u64 length) > > out: > if (btotal) { > - if (current->journal_info == NULL) { > + if (current->journal_info) { > + gfs2_update_mapping_seq(ip); > + } else { > int ret2; > > ret2 = gfs2_trans_begin(sdp, RES_DINODE + RES_STATFS + [Severity: High] Can deferring gfs2_update_mapping_seq() until transaction end leave a window where concurrent writeback queues I/O to blocks already freed by punch_hole()? In sweep_bh_for_rgrps(), block pointers are zeroed and freed via __gfs2_free_blocks() while i_mapping_seq still holds its pre-punch value. When blks_outside_rgrp is zero, punch_hole() keeps the transaction open across further metadata levels (including blocking reads in fillup_metapath()) before updating i_mapping_seq at out:. On filesystems with blocksize < PAGE_SIZE, truncate_pagecache_range() leaves a writable dirty PTE intact on an order-0 boundary folio straddling the punch offset (for example after a short buffered write into gfs2_iomap_end() on a MAP_SHARED file): CPU0 (punch_hole) CPU1 (writeback) gfs2_writeback_range() (folio 0) caches wpc->iomap (seq == S) sweep_bh_for_rgrps() down_write(&ip->i_rw_mutex) *p = 0; __gfs2_free_blocks() /* i_mapping_seq still S */ fillup_metapath() /* blocks */ folio_clear_dirty_for_io() (folio 1) folio_mkclean() -> iomap_dirty_folio() /* re-dirties punched sub-blocks */ gfs2_writeback_range() gfs2_iomap_valid() -> true (S == S) iomap_add_to_ioend() <-- freed blocks gfs2_update_mapping_seq(ip) up_write(&ip->i_rw_mutex) Because gfs2_iomap_valid() reads i_mapping_seq locklessly, writeback skips gfs2_iomap_get() (which would block on i_rw_mutex) and submits I/O to the freed blocks, silently corrupting data once they are reallocated locally or on another cluster node. Updating i_mapping_seq right after taking down_write(&ip->i_rw_mutex) before freeing any blocks, with ordering against lockless readers, would close this window. [ ... ] > @@ -2523,7 +2553,8 @@ static ssize_t gfs2_writeback_range(struct iomap_writepage_ctx *wpc, > return -EIO; > > if (offset < wpc->iomap.offset || > - offset >= wpc->iomap.offset + wpc->iomap.length) { > + offset >= wpc->iomap.offset + wpc->iomap.length || > + !gfs2_iomap_valid(wpc->inode, &wpc->iomap)) { > int ret; > > memset(&wpc->iomap, 0, sizeof(wpc->iomap)); [Severity: Medium] Can gfs2_writeback_range() reuse a stale cached IOMAP_HOLE mapping after another cluster node allocates blocks, silently dropping dirty writes? Neither inode_go_inval() (under DIO_METADATA) nor gfs2_dinode_in() updates i_mapping_seq. Unlike a mapped ioend, an IOMAP_HOLE mapping clears PG_writeback immediately without holding wpc->wb_ctx, so WB_SYNC_NONE writeback can pause at cond_resched() between folio batches while a glock demotion completes. On a cluster with blocksize < PAGE_SIZE, if a full batch of folios over a punched hole is dirtied (e.g. via GUP write completion): Node A (writeback) Node B / Node A (writer) gfs2_writeback_range() caches IOMAP_HOLE (seq == S) writeback_get_folio() folio_batch_release() cond_resched() Node B requests EX glock Node A: inode_go_inval(DIO_METADATA) /* i_mapping_seq still S */ Node B allocates blocks in range Node A: pwrite() -> gfs2_dinode_in() sees IOMAP_MAPPED, dirties folio 31 /* i_mapping_seq still S */ filemap_get_folios_tag() folio_clear_dirty_for_io() gfs2_writeback_range() gfs2_iomap_valid() -> true iomap_add_to_ioend() <-- skips I/O on stale IOMAP_HOLE Because folio_clear_dirty_for_io() has already cleared the dirty state, reusing the stale IOMAP_HOLE drops writeback for folio 31 without submitting I/O, silently losing the write. Updating i_mapping_seq in inode_go_inval() when DIO_METADATA is set (or in gfs2_dinode_in()) would invalidate mappings cached before cluster metadata invalidation. -- Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796393524.git.mark.syms@citrix.com?part=2 ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v3 0/2] gfs2: Revalidate the cached writeback mapping after a punch 2026-12-04 16:08 ` [PATCH v3 0/2] " Mark Syms 2026-12-04 16:08 ` [PATCH v3 1/2] gfs2: minor punch_hole error path cleanup Mark Syms 2026-12-04 16:08 ` [PATCH v3 2/2] gfs2: Revalidate the cached writeback mapping after a punch Mark Syms @ 2026-12-04 16:26 ` Andreas Gruenbacher 2 siblings, 0 replies; 10+ messages in thread From: Andreas Gruenbacher @ 2026-12-04 16:26 UTC (permalink / raw) To: Mark Syms; +Cc: gfs2, Christoph Hellwig On Fri, 4 Dec 2026 at 17:09, Mark Syms <mark.syms@citrix.com> wrote: > gfs2 writeback reuses its cached mapping across a short-write punch and > reallocation, so the retried data goes to the freed blocks and the new > blocks keep stale contents. Patch 2 fixes that; patch 1 is Andreas's > punch_hole() error-path cleanup, which patch 2 builds on. > > v3: > - Count allocations as well as deallocations in a u64 i_mapping_seq, > updated under i_rw_mutex where the inode's mapping changes: on > allocation, unstuffing, and at each transaction boundary of > punch_hole() (Andreas). > - Set the cookie in __gfs2_iomap_get(); check it in writeback only > (Andreas). > - Add patch 1 (Andreas). > v2: > - Refuse to reuse a cached IOMAP_HOLE (sashiko); add Fixes. > > Andreas Gruenbacher (1): > gfs2: minor punch_hole error path cleanup > > Mark Syms (1): > gfs2: Revalidate the cached writeback mapping after a punch > > fs/gfs2/bmap.c | 53 ++++++++++++++++++++++++++++++++++++++++-------- > fs/gfs2/incore.h | 1 + > fs/gfs2/super.c | 1 + > 3 files changed, 46 insertions(+), 9 deletions(-) > > -- > 2.56.0 Thanks Mark, I have pushed this to for-next. Andreas ^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2026-12-04 17:11 UTC | newest] Thread overview: 10+ 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 2026-12-02 20:54 ` Andreas Gruenbacher 2026-12-04 16:08 ` [PATCH v3 0/2] " Mark Syms 2026-12-04 16:08 ` [PATCH v3 1/2] gfs2: minor punch_hole error path cleanup Mark Syms 2026-12-04 16:37 ` sashiko-bot 2026-12-04 16:08 ` [PATCH v3 2/2] gfs2: Revalidate the cached writeback mapping after a punch Mark Syms 2026-12-04 17:11 ` sashiko-bot 2026-12-04 16:26 ` [PATCH v3 0/2] " Andreas Gruenbacher
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.