From: Joanne Koong <joannelkoong@gmail.com>
To: brauner@kernel.org
Cc: willy@infradead.org, djwong@kernel.org, hch@infradead.org,
bfoster@redhat.com, linux-fsdevel@vger.kernel.org
Subject: [PATCH v3 1/1] iomap: fix readahead folio access after folio_end_read()
Date: Fri, 16 Jan 2026 12:04:27 -0800 [thread overview]
Message-ID: <20260116200427.1016177-2-joannelkoong@gmail.com> (raw)
In-Reply-To: <20260116200427.1016177-1-joannelkoong@gmail.com>
If the folio does not have an iomap_folio_state struct attached to it
and the folio gets read in by the filesystem's IO helper,
folio_end_read() may have already been called on the folio.
Fix this by invalidating ctx->cur_folio when a folio without
iomap_folio_state metadata attached to it has been handed to the
filesystem's IO helper.
Fixes: b2f35ac4146d ("iomap: add caller-provided callbacks for read and readahead")
Signed-off-by: Joanne Koong <joannelkoong@gmail.com>
---
fs/iomap/buffered-io.c | 23 +++++++++++++++++++++--
1 file changed, 21 insertions(+), 2 deletions(-)
diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c
index 6beb876658c0..8b7fb33d7212 100644
--- a/fs/iomap/buffered-io.c
+++ b/fs/iomap/buffered-io.c
@@ -502,6 +502,8 @@ static int iomap_read_folio_iter(struct iomap_iter *iter,
loff_t pos = iter->pos;
loff_t length = iomap_length(iter);
struct folio *folio = ctx->cur_folio;
+ size_t folio_len = folio_size(folio);
+ struct iomap_folio_state *ifs;
size_t poff, plen;
loff_t pos_diff;
int ret;
@@ -513,10 +515,10 @@ static int iomap_read_folio_iter(struct iomap_iter *iter,
return iomap_iter_advance(iter, length);
}
- ifs_alloc(iter->inode, folio, iter->flags);
+ ifs = ifs_alloc(iter->inode, folio, iter->flags);
length = min_t(loff_t, length,
- folio_size(folio) - offset_in_folio(folio, pos));
+ folio_len - offset_in_folio(folio, pos));
while (length) {
iomap_adjust_read_range(iter->inode, folio, &pos, length, &poff,
&plen);
@@ -542,7 +544,24 @@ static int iomap_read_folio_iter(struct iomap_iter *iter,
ret = ctx->ops->read_folio_range(iter, ctx, plen);
if (ret)
return ret;
+
*bytes_submitted += plen;
+ /*
+ * If the folio does not have ifs metadata attached,
+ * then after ->read_folio_range(), the folio might have
+ * gotten freed (eg iomap_finish_folio_read() ->
+ * folio_end_read() followed by page cache eviction,
+ * which for readahead folios drops the last refcount).
+ * Invalidate ctx->cur_folio here.
+ *
+ * For folios without ifs metadata attached, the read
+ * should be on the entire folio.
+ */
+ if (!ifs) {
+ ctx->cur_folio = NULL;
+ if (unlikely(plen != folio_len))
+ return -EIO;
+ }
}
ret = iomap_iter_advance(iter, plen);
--
2.47.3
next prev parent reply other threads:[~2026-01-16 20:04 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-16 20:04 [PATCH v3 0/1] iomap: fix readahead folio access after folio_end_read() Joanne Koong
2026-01-16 20:04 ` Joanne Koong [this message]
2026-01-21 7:49 ` [PATCH v3 1/1] " Christoph Hellwig
2026-01-21 23:13 ` Joanne Koong
2026-01-22 6:22 ` Christoph Hellwig
2026-01-22 16:51 ` Matthew Wilcox
2026-01-22 19:50 ` Joanne Koong
2026-01-22 19:56 ` Matthew Wilcox
2026-01-22 23:05 ` Joanne Koong
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=20260116200427.1016177-2-joannelkoong@gmail.com \
--to=joannelkoong@gmail.com \
--cc=bfoster@redhat.com \
--cc=brauner@kernel.org \
--cc=djwong@kernel.org \
--cc=hch@infradead.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=willy@infradead.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox