]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
netfs: Fix netfs_invalidate_folio() to clear dirty bit if all changes gone
authorDavid Howells <dhowells@redhat.com>
Tue, 12 May 2026 12:33:48 +0000 (13:33 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Mon, 1 Jun 2026 15:46:27 +0000 (17:46 +0200)
[ Upstream commit 156ac2ec2ee77c44c4eb7439d6d165247ba12247 ]

If a streaming write is made, this will leave the relevant modified folio
in a not-uptodate, but dirty state with a netfs_folio struct hung off of
folio->private indicating the dirty range.  Subsequently truncating the
file such that the dirty data in the folio is removed, but the first part
of the folio theoretically remains will cause the netfs_folio struct to be
discarded... but will leave the dirty flag set.

If the folio is then read via mmap(), netfs_read_folio() will see that the
page is dirty and jump to netfs_read_gaps() to fill in the missing bits.
netfs_read_gaps(), however, expects there to be a netfs_folio struct
present and can oops because truncate removed it.

Fix this by calling folio_cancel_dirty() in netfs_invalidate_folio() in the
event that all the dirty data in the folio is erased (as nfs does).

Also add some tracepoints to log modifications to a dirty page.

This can be reproduced with something like:

    dd if=/dev/zero of=/xfstest.test/foo bs=1M count=1
    umount /xfstest.test
    mount /xfstest.test
    xfs_io -c "w 0xbbbf 0xf96c" \
           -c "truncate 0xbbbf" \
           -c "mmap -r 0xb000 0x11000" \
           -c "mr 0xb000 0x11000" \
           /xfstest.test/foo

with fscaching disabled (otherwise streaming writes are suppressed) and a
change to netfs_perform_write() to disallow streaming writes if the fd is
open O_RDWR:

if (//(file->f_mode & FMODE_READ) || <--- comment this out
    netfs_is_cache_enabled(ctx)) {

It should be reproducible even without this change, but if prevents the
above trivial xfs_io command from reproducing it.

Note that the initial dd is important: the file must start out sufficiently
large that the zero-point logic doesn't just clear the gaps because it
knows there's nothing in the file to read yet.  Unmounting and mounting is
needed to clear the pagecache (there are other ways to do that that may
also work).

This was initially reproduced with the generic/522 xfstest on some patches
that remove the FMODE_READ restriction.

Fixes: 9ebff83e6481 ("netfs: Prep to use folio->private for write grouping and streaming write")
Reported-by: Marc Dionne <marc.dionne@auristor.com>
Signed-off-by: David Howells <dhowells@redhat.com>
Link: https://patch.msgid.link/20260512123404.719402-12-dhowells@redhat.com
cc: Paulo Alcantara <pc@manguebit.org>
cc: Matthew Wilcox <willy@infradead.org>
cc: netfs@lists.linux.dev
cc: linux-fsdevel@vger.kernel.org
Signed-off-by: Christian Brauner <brauner@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
fs/netfs/misc.c
include/trace/events/netfs.h

index 78fe5796b2b2f7fc31547ae2ae6b2d686cc3a111..488a4b1914300cf63d36c7f74217819d2a49e059 100644 (file)
@@ -268,6 +268,7 @@ void netfs_invalidate_folio(struct folio *folio, size_t offset, size_t length)
                        /* Move the start of the data. */
                        finfo->dirty_len = fend - iend;
                        finfo->dirty_offset = offset;
+                       trace_netfs_folio(folio, netfs_folio_trace_invalidate_front);
                        return;
                }
 
@@ -276,12 +277,14 @@ void netfs_invalidate_folio(struct folio *folio, size_t offset, size_t length)
                 */
                if (iend >= fend) {
                        finfo->dirty_len = offset - fstart;
+                       trace_netfs_folio(folio, netfs_folio_trace_invalidate_tail);
                        return;
                }
 
                /* A partial write was split.  The caller has already zeroed
                 * it, so just absorb the hole.
                 */
+               trace_netfs_folio(folio, netfs_folio_trace_invalidate_middle);
        }
        return;
 
@@ -289,8 +292,9 @@ erase_completely:
        netfs_put_group(netfs_folio_group(folio));
        folio_detach_private(folio);
        folio_clear_uptodate(folio);
+       folio_cancel_dirty(folio);
        kfree(finfo);
-       return;
+       trace_netfs_folio(folio, netfs_folio_trace_invalidate_all);
 }
 EXPORT_SYMBOL(netfs_invalidate_folio);
 
index 69975c9c682393fc3185c283d1229adcff61f759..f3e386c69cc8b200c66b82ba30b36ada4486a266 100644 (file)
        EM(netfs_folio_trace_copy_to_cache,     "mark-copy")    \
        EM(netfs_folio_trace_end_copy,          "end-copy")     \
        EM(netfs_folio_trace_filled_gaps,       "filled-gaps")  \
+       EM(netfs_folio_trace_invalidate_all,    "inval-all")    \
+       EM(netfs_folio_trace_invalidate_front,  "inval-front")  \
+       EM(netfs_folio_trace_invalidate_middle, "inval-mid")    \
+       EM(netfs_folio_trace_invalidate_tail,   "inval-tail")   \
        EM(netfs_folio_trace_kill,              "kill")         \
        EM(netfs_folio_trace_kill_cc,           "kill-cc")      \
        EM(netfs_folio_trace_kill_g,            "kill-g")       \