From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=160.101.131.9 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796227074; b=G9aXbFY4I7t+DlhJcXF+xXO7IU7ZS+kVuaHg0X9G575nHD4LdLOrN8MQys6MdmacuyBY PmMk5ymfrIiDMQz+7LVIW0B/anSBCNNROdrK3z5XDnrmUEV7Jad7h7yBLCqXOPQhcD7ha 5xm2hG0SxMseLRfhQ8zwQ0zF3qtCnlRzuBebHvygkAFRkgTPfNhLanmOoOh8b0uJivaOQ JyH3liFsGbKp8tmg85o42ZjXyt7y2vrR9ByCwF7cvp6PgRzz19LqcFaTymePMwBn31IBs /kor2DZdI44inXW9D7LJqQMqThiGr/2uMY36IbvCZhp4AxhArmm6hqcj6dKNA25XLXg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796227074; h=DMARC-Filter:Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=2YYZGlFS4MzjD8fh/HgGr5lA1je9bhD4CCkPh3Ke0tM=; b=TMruVlCMFiKjdCBuelt11TLu0fwRM2tbLfGC2dcnVdJhsWdblUXzfBNdtZegmEN7V/yB ZoyAlqxavHh+fMn7Ga91U2Aeueq24T/+Pc7dF/iSBY/wCf9i7UV2Kqobozt7sxjSKrTXd QxasRC0iAJ6jEGKWWQ3ue3C2YT96uy+V0nbaLwr1Se/JDcgEQRl8CKeO65G+FReFdxHP4 VVRk4+K023B+fkqGg3tm1q5YT0J7Ve76hCT+QRWMjJxpRJevWMrJNeER97vyvuxdPMieb R0SQHkKWEFK82xQ5izlX3tVmgXtlTViwETarrJd199cvCDXKpTRweWNnKQeyBIE9OSA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=citrix.com; spf=pass smtp.mailfrom=citrix.com; arc=none smtp.remote-ip=160.101.131.9 Received: from na1pdmzitismtp02.tibco.com (na1pdmzitismtp02.tibco.com [160.101.131.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ED0DF606A322 for ; Wed, 2 Dec 2026 15:57:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org ED0DF606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=citrix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=citrix.com Received: from mewpvdipd1070.citrite.net (unknown [10.113.40.46]) by na1pdmzitismtp02.tibco.com (Postfix) with ESMTP id 0BB39854D151; Wed, 2 Dec 2026 10:56:03 -0500 (EST) From: Mark Syms To: Andreas Gruenbacher Cc: Mark Syms , 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 Message-ID: <20261202155751.690482-1-mark.syms@citrix.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261202141814.668810-1-mark.syms@citrix.com> References: <20261202141814.668810-1-mark.syms@citrix.com> X-Mailing-List: gfs2@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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