Skip to contentSkip to conversation

NFS: Fix netfs read subrequest range, retry accounting and fallback handling

PATCH New Abhinandan Ekande 1 participant 4 messages Nov 18

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover NFS: Fix netfs read subrequest range, retry accounting and fallback handling

This version fixes an issue that Sashiko's review pointed out.

While addressing an issue where netfs repeatedly reads whole folios when rsize is smaller than the folio size, two additional pre-existing bugs were identified in the NFS netfs read path: subrequest retry byte accounting and legacy read fallback handling.

This series fixes all three issues:
- Patch 1 fixes an accounting bug in nfs_netfs_put(): it assigns the current ->issue_read() call's byte count to sreq->transferred instead of adding to it, discarding progress from an earlier attempt such as a partial cache read, so the subrequest under-reports what it holds. Accumulate instead, with the existing min_t() clamp now bounding the cumulative total.
- Patch 2 ensures nfs_netfs_issue_read() requests only the subrequest's byte range rather than repeatedly submitting the whole folio.
- Patch 3 restricts the legacy nfs_do_read_folio() fallback in nfs_read_folio() to -ENOBUFS (when fscache is absent), avoiding a fallback read on an already unlocked folio when netfs_read_folio() fails with other errors.

Notes on v2 review comments from Sashiko[2]:
1. Fallback path in nfs_read_folio() when netfs_read_folio() fails:
This pre-existing issue is addressed by patch 3, which restricts the nfs_do_read_folio() fallback to -ENOBUFS.
2. Unaligned range / past-EOF zero-fill in patch 2:
- pNFS block/SCSI layout: Neither the sub-page offset BUG_ON() nor the EOF tail overwrite is observable today because pNFS block/SCSI layout with fscache enabled cannot complete any buffered reads. That alignment check can be addressed when block/SCSI layout is updated to support netfs/fscache.
- Past-EOF zero-fill and concurrent i_size growth: netfs has no ordering between an inline NETFS_FILL_WITH_ZEROES subrequest and an async over-read past rreq->i_size; the pre-patch behavior was partial and rsize dependent. Once the client notices the i_size growth, nfs_update_inode() sets NFS_INO_INVALID_DATA, ensuring the next revalidation invalidates both the folio and fscache. Keeping nfs_netfs_issue_read() strictly within the subrequest's byte range avoids violating netfs's subrequest ownership.

Changes in v3:
- Added patch 3 to restrict the legacy read fallback in nfs_read_folio() to -ENOBUFS, following review feedback from Sashiko[2].
- Patch 1 and patch 2 are unchanged from v2.
- Link to v2: https://lore.kernel.org/all/[email protected]/

Changes in v2:
- Added patch 1 to fix sreq->transferred accumulation on completion, following review feedback from Sashiko[1].
- Patch 2 is unchanged from v1.

[1] https://sashiko.dev/#/patchset/20261027053444.32012-1-aekande%40redhat.com
[2] https://sashiko.dev/#/patchset/20261030061138.237833-1-aekande%40redhat.com

Abhinandan Ekande (3):
NFS: Accumulate transferred bytes on netfs read completion
NFS: honour the netfs subrequest range when issuing reads
NFS: only fall back to the legacy read path when fscache is absent

 fs/nfs/fscache.c  | 21 +++++++++++++----
 fs/nfs/fscache.h  |  1 +
 fs/nfs/internal.h |  4 ++++
 fs/nfs/read.c     | 58 ++++++++++++++++++++++++++++++-----------------
 4 files changed, 59 insertions(+), 25 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH v3 1/3 NFS: Accumulate transferred bytes on netfs read completion

When netfs retries a read subrequest that already made progress, it resets subreq->start and subreq->len to the full original extent and leaves subreq->transferred holding the byte count read so far.

nfs_netfs_alloc() is called on every ->issue_read(), including retries, and kzalloc()s netfs->transferred to zero. nfs_netfs_read_completion() only tracks the bytes read during the current ->issue_read() call. nfs_netfs_put() then assigns that value to sreq->transferred rather than adding to it.

The subrequest therefore reports less than it holds. If it hit EOF, nfs_netfs_read_completion() has set NETFS_SREQ_CLEAR_TAIL and the collector calls netfs_clear_unread(), which zeroes data that was already read and then marks the subrequest complete, with no error reported. Otherwise netfs_read_subreq_terminated() sees subreq->transferred < subreq->len with no error and, as NFS never sets NETFS_SREQ_MADE_PROGRESS, fails the read with -ENODATA.

Accumulate instead of assigning. The min_t() clamp is kept and now bounds the cumulative total.

The retry is reached when a NETFS_READ_FROM_CACHE subrequest fails after a partial transfer and netfs reissues it against the server. It is masked today because nfs_netfs_issue_read() reads the whole folio for every subrequest, so the accumulator exceeds sreq->len on its own and the clamp arrives at the right answer by accident. Next patch in this series restricts the read to the subrequest's range and removes that masking.

Fixes ee4cdf7ba857 ("netfs: Speed up buffered reading")
---
 fs/nfs/fscache.h | 1 +
 1 file changed, 1 insertion(+)
fs/nfs/fscache.h +1 -0
index 9d86868f4998..1ab6b8ab6b4f 100644
@@ -73,6 +73,7 @@ static inline void nfs_netfs_put(struct nfs_netfs_io_data *netfs)
 	 * length, and thus avoid netfs's "Subreq overread" warning message.
 	 */
 	netfs->sreq->transferred = min_t(s64, netfs->sreq->len,
+					 netfs->sreq->transferred +
 					 atomic64_read(&netfs->transferred));
 	netfs->sreq->error = netfs->error;
 	netfs_read_subreq_terminated(netfs->sreq);
Signature · 1 line
2.55.0
#2 lore raw

PATCH v3 2/3 NFS: honour the netfs subrequest range when issuing reads

In reply to Abhinandan Ekande

netfs splits the folio into folio_size/rsize subrequests. Each subrequest calls nfs_netfs_issue_read() separately. nfs_netfs_issue_read() discards subreq->start and subreq->len, converts them into a page index range, and hands each folio in that range to nfs_read_add_folio(), which submits the whole folio.

A folio larger than rsize is therefore read once per subrequest. On a mount with rsize=4096, a 128K (order-5) folio is covered by 32 subrequests; each submits the whole folio, which nfs_pageio_add_request() splits into 32 RPCs at pg_bsize. That is 1024 concurrent READs for one folio, with every 4K page targeted by 32 of them, all pointing their receive buffer at the same page.

Under sec=sys every reply carries the same plaintext in the page, so the duplicate writes are byte-identical and only the wasted bandwidth remains.
With sec=krb5p the payload is separately encrypted per reply, so the bytes on the wire differ even though the data is the same, and gss_unwrap_resp_priv() decrypts in place in rq_rcv_buf. One reply's ciphertext lands on top of another's before it is unwrapped, and the integrity check fails with GSS_S_FAILURE. The subrequest completes with -EIO and netfs leaves the folio non-uptodate.

The defect dates to the netfs conversion but stayed latent at the common case of rsize of 4096 or greater and with order-0 folios, folio_size/rsize is 1 and each subrequest's whole folio read happens to cover exactly the range asked for. The 'commit 49b29a573da8 ("nfs:
add support for large folios")' made folios up to order-5, which exposes the same loop at any rsize smaller than folio.

Fix nfs_netfs_issue_read() to intersect each folio with the subrequest's byte range and submit only that. nfs_read_add_folio() is split into a range taking helper, nfs_read_add_folio_range(). nfs_read_add_folio() keeps its existing behaviour, so the two legacy callers, nfs_do_read_folio() and nfs_readahead(), are unaffected.

Once a request can cover only part of a folio, nfs_page_group_set_uptodate() must stop marking the whole folio uptodate. nfs_page_group_sync_on_bit() synchronises across one nfs_page group, not across the folio's other subrequests, so a group that completes cleanly would mark a folio that is only partly filled. Skip the marking when netfs owns the folio.

With the patch, 30 MiB read over sec=krb5p,fsc,rsize=4096 issues exactly 7680 READs, one per page, compared to earlier every page was fetched up to 32 times. Folio sizes and the netfs subrequest shape are unchanged.

Fixes 000dbe0bec05 ("NFS: Convert buffered read paths to use netfs when fscache is enabled")
Assisted-by LLM
---
 fs/nfs/fscache.c  | 21 ++++++++++++++----
 fs/nfs/internal.h |  4 ++++
 fs/nfs/read.c     | 56 ++++++++++++++++++++++++++++++-----------------
 3 files changed, 57 insertions(+), 24 deletions(-)
fs/nfs/fscache.c +17 -4
index 9b7fdad4a920..eb5e54406e58 100644
@@ -303,12 +303,14 @@ static void nfs_netfs_issue_read(struct netfs_io_subrequest *sreq)
 	struct inode *inode = sreq->rreq->inode;
 	struct nfs_open_context *ctx = sreq->rreq->netfs_priv;
 	struct page *page;
+	unsigned long long pos = sreq->start + sreq->transferred;
+	unsigned long long end = sreq->start + sreq->len;
 	unsigned long idx;
 	pgoff_t start, last;
 	int err;
 
-	start = (sreq->start + sreq->transferred) >> PAGE_SHIFT;
-	last = ((sreq->start + sreq->len - sreq->transferred - 1) >> PAGE_SHIFT);
+	start = pos >> PAGE_SHIFT;
+	last = ((end - 1) >> PAGE_SHIFT);
 
 	nfs_pageio_init_read(&pgio, inode, false,
 			     &nfs_async_read_completion_ops);
@@ -322,8 +324,19 @@ static void nfs_netfs_issue_read(struct netfs_io_subrequest *sreq)
 	pgio.pg_netfs = netfs; /* used in completion */
 
 	xa_for_each_range(&sreq->rreq->mapping->i_pages, idx, page, start, last) {
-		/* nfs_read_add_folio() may schedule() due to pNFS layout and other RPCs  */
-		err = nfs_read_add_folio(&pgio, ctx, page_folio(page));
+		struct folio *folio = page_folio(page);
+		unsigned long long fpos = folio_pos(folio);
+		size_t fsize = folio_size(folio);
+		size_t offset, len;
+
+		/* Read only the part of the folio the subrequest asked for. */
+		offset = pos > fpos ? pos - fpos : 0;
+		len = min(end, fpos + fsize) - (fpos + offset);
+		if (!len)
+			continue;
+
+		/* nfs_read_add_folio_range() may schedule() due to pNFS layout and other RPCs  */
+		err = nfs_read_add_folio_range(&pgio, ctx, folio, offset, len);
 		if (err < 0) {
 			netfs->error = err;
 			goto out;
fs/nfs/internal.h +4 -0
index 5af6a032f145..a4fed789e113 100644
@@ -611,6 +611,10 @@ extern bool nfs_read_alloc_scratch(struct nfs_pgio_header *hdr, size_t size);
 extern int nfs_read_add_folio(struct nfs_pageio_descriptor *pgio,
 			       struct nfs_open_context *ctx,
 			       struct folio *folio);
+extern int nfs_read_add_folio_range(struct nfs_pageio_descriptor *pgio,
+				    struct nfs_open_context *ctx,
+				    struct folio *folio,
+				    size_t offset, size_t len);
 extern void nfs_pageio_complete_read(struct nfs_pageio_descriptor *pgio);
 extern void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio);
 
fs/nfs/read.c +36 -20

91 lines not on this page · raw message

Signature · 1 line
2.55.0
#3 lore raw

PATCH v3 3/3 NFS: only fall back to the legacy read path when fscache is absent

In reply to Abhinandan Ekande

nfs_read_folio() tries the netfs read path first and falls back to the legacy path on any non-zero return:

      ret = nfs_netfs_read_folio(file, folio);
      if (ret)
              ret = nfs_do_read_folio(file, folio);

That fallback exists for inodes without an fscache cookie, where nfs_netfs_read_folio() returns -ENOBUFS before doing any work. Every other error also takes it, and that is a problem, because netfs_read_folio() unlocks the folio on all of its error exits, and failures during the read itself are unlocked by netfs_unlock_read_folio() during collection. Either way nfs_do_read_folio() is handed a already unlocked folio.

nfs_do_read_folio() issues its READ RPCs anyway. Its folio_wait_locked_killable() returns immediately because there is no lock to wait on, so it returns 0 to the VFS while those RPCs are still in flight.

Fix it by taking the fallback only for -ENOBUFS, the one case it was written for. nfs_netfs_read_folio() returns -ENOBUFS only from its own test for a missing cookie, before netfs_read_folio() is called, and netfs_read_folio() itself cannot return it: its single -ENOBUFS source, netfs_begin_cache_read(), is left out of the error set that aborts the read and instead falls through to an uncached read. nfs_readahead() needs no equivalent change, as nfs_netfs_readahead() returns only 0 or -ENOBUFS and its fallback is already restricted to the no-cookie case.

Fixes 000dbe0bec05 ("NFS: Convert buffered read paths to use netfs when fscache is enabled")
Assisted-by LLM
---
 fs/nfs/read.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/nfs/read.c +1 -1
index 8d73fbd59d7f..61c803fb683c 100644
@@ -432,7 +432,7 @@ int nfs_read_folio(struct file *file, struct folio *folio)
 		goto out_unlock;
 
 	ret = nfs_netfs_read_folio(file, folio);
-	if (ret)
+	if (ret == -ENOBUFS)
 		ret = nfs_do_read_folio(file, folio);
 out:
 	trace_nfs_aop_readpage_done(inode, pos, len, ret);
Signature · 1 line
2.55.0