Skip to contentSkip to conversation

NFSD: Remove WARN_ON_ONCE in nfsd_iter_read()

RFC PATCH In review Chuck Lever 3 participants 5 messages Sep 11, 2025 – Sep 14, 2025

Subsystems (2)
lore
Share & tools
lore

Chuck Lever

author maintainer
#0 lore raw

RFC PATCH 1/1 NFSD: Remove WARN_ON_ONCE in nfsd_iter_read()

From: Chuck Lever <[email protected]>

The *count parameter does not appear to be explicitly restricted to being smaller than rsize, so it might be possible to overrun the rq_bvec array.

Rather than overrunning the array (damage done!) and then WARNING once, let's harden the loop so that it terminates before the end of rq_bvec. This should result in a short read, which is OK (clients recover by sending additional READ requests for the remaining unread bytes).

---
 fs/nfsd/vfs.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

There might be a similar issue with rq_next_page in this loop?

Suppose that nfsd4_encode_readv() encounters a second READ operation in a COMPOUND, and the two READ operations together comprise more than "rsize" total bytes of payload. Each rq_bvec is under the page limit, but the total number of pages consumed from rq_pages might exceed rq_maxpages.

fs/nfsd/vfs.c +4 -4
index 714777c221ed..e2f0fe3f82c0 100644
@@ -1120,13 +1120,13 @@ __be32 nfsd_iter_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
 		bvec_set_page(&rqstp->rq_bvec[v], *(rqstp->rq_next_page++),
 			      len, base);
 		total -= len;
-		++v;
 		base = 0;
+		if (++v >= rqstp->rq_maxpages)
+			break;
 	}
-	WARN_ON_ONCE(v > rqstp->rq_maxpages);
 
-	trace_nfsd_read_vector(rqstp, fhp, offset, *count);
-	iov_iter_bvec(&iter, ITER_DEST, rqstp->rq_bvec, v, *count);
+	trace_nfsd_read_vector(rqstp, fhp, offset, *count - total);
+	iov_iter_bvec(&iter, ITER_DEST, rqstp->rq_bvec, v, *count - total);
 	host_err = vfs_iocb_iter_read(file, &kiocb, &iter);
 	return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err);
 }
Signature · 1 line
2.50.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 17 lines · Chuck Lever

From: Chuck Lever <[email protected]>

The *count parameter does not appear to be explicitly restricted to being smaller than rsize, so it might be possible to overrun the rq_bvec array.

Rather than overrunning the array (damage done!) and then WARNING once, let's harden the loop so that it terminates before the end of rq_bvec. This should result in a short read, which is OK (clients recover by sending additional READ requests for the remaining unread bytes).

---
 fs/nfsd/vfs.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
Quoted 7 lines

There might be a similar issue with rq_next_page in this loop?

Suppose that nfsd4_encode_readv() encounters a second READ operation in a COMPOUND, and the two READ operations together comprise more than "rsize" total bytes of payload. Each rq_bvec is under the page limit, but the total number of pages consumed from rq_pages might exceed rq_maxpages.

This concern would appear well-founded; but probably best to deal with it independently.

fs/nfsd/vfs.c __be32 nfsd_iter_read(struct svc_rqst *rqstp, struct svc_fh *fhp, · line 1125

Quoted 12 lines
fs/nfsd/vfs.c +2 -1
index 714777c221ed..e2f0fe3f82c0 100644
@@ -1120,13 +1120,13 @@ __be32 nfsd_iter_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
 		bvec_set_page(&rqstp->rq_bvec[v], *(rqstp->rq_next_page++),
 			      len, base);
 		total -= len;
-		++v;
 		base = 0;
+		if (++v >= rqstp->rq_maxpages)
+			break;

Shouldn't this be == instead of >= ?
Not seeing how it could ever become greater without first being equal.

Other than that, this patch is a welcome obvious improvement:

Quoted 14 lines
fs/nfsd/vfs.c +2 -3
 	}
-	WARN_ON_ONCE(v > rqstp->rq_maxpages);
 
-	trace_nfsd_read_vector(rqstp, fhp, offset, *count);
-	iov_iter_bvec(&iter, ITER_DEST, rqstp->rq_bvec, v, *count);
+	trace_nfsd_read_vector(rqstp, fhp, offset, *count - total);
+	iov_iter_bvec(&iter, ITER_DEST, rqstp->rq_bvec, v, *count - total);
 	host_err = vfs_iocb_iter_read(file, &kiocb, &iter);
 	return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err);
 }
Signature · 1 line
2.50.0
#2 lore raw
In reply to Chuck Lever
Quoted 30 lines · Chuck Lever

From: Chuck Lever <[email protected]>

The *count parameter does not appear to be explicitly restricted to being smaller than rsize, so it might be possible to overrun the rq_bvec array.

Rather than overrunning the array (damage done!) and then WARNING once, let's harden the loop so that it terminates before the end of rq_bvec. This should result in a short read, which is OK (clients recover by sending additional READ requests for the remaining unread bytes).

---
 fs/nfsd/vfs.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

There might be a similar issue with rq_next_page in this loop?

Suppose that nfsd4_encode_readv() encounters a second READ operation in a COMPOUND, and the two READ operations together comprise more than "rsize" total bytes of payload. Each rq_bvec is under the page limit, but the total number of pages consumed from rq_pages might exceed rq_maxpages.

fs/nfsd/vfs.c +0 -0
index 714777c221ed..e2f0fe3f82c0 100644

fs/nfsd/vfs.c __be32 nfsd_iter_read(struct svc_rqst *rqstp, struct svc_fh *fhp, · line 1125

Quoted 7 lines
fs/nfsd/vfs.c +2 -1
@@ -1120,13 +1120,13 @@ __be32 nfsd_iter_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
 		bvec_set_page(&rqstp->rq_bvec[v], *(rqstp->rq_next_page++),
 			      len, base);
 		total -= len;
-		++v;
 		base = 0;
+		if (++v >= rqstp->rq_maxpages)
+			break;

I would have changed the head of the while loop to be

  while (total > 0 && rqstp->rq_next_page < ....)

and then realised that I don't know what to put there.
I don't think that counting up to rq_maxpages is safe when there could be two READ ops in an NFSv4 Compound.

And if we are cleaning up this function I woul put the increments of v and rq_next_page next to each other..
</bikeshed>

The patch is an improvement. I wonder if it is enough.

Thanks,
NeilBrown

Quoted 14 lines
fs/nfsd/vfs.c +2 -3
 	}
-	WARN_ON_ONCE(v > rqstp->rq_maxpages);
 
-	trace_nfsd_read_vector(rqstp, fhp, offset, *count);
-	iov_iter_bvec(&iter, ITER_DEST, rqstp->rq_bvec, v, *count);
+	trace_nfsd_read_vector(rqstp, fhp, offset, *count - total);
+	iov_iter_bvec(&iter, ITER_DEST, rqstp->rq_bvec, v, *count - total);
 	host_err = vfs_iocb_iter_read(file, &kiocb, &iter);
 	return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err);
 }
Signature · 1 line
2.50.0
author maintainer
#3 lore raw
Quoted 20 lines · NeilBrown

fs/nfsd/vfs.c __be32 nfsd_iter_read(struct svc_rqst *rqstp, struct svc_fh *fhp, · line 1125

Quoted 12 lines
fs/nfsd/vfs.c +2 -1
index 714777c221ed..e2f0fe3f82c0 100644
@@ -1120,13 +1120,13 @@ __be32 nfsd_iter_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
 		bvec_set_page(&rqstp->rq_bvec[v], *(rqstp->rq_next_page++),
 			      len, base);
 		total -= len;
-		++v;
 		base = 0;
+		if (++v >= rqstp->rq_maxpages)
+			break;

I would have changed the head of the while loop to be

  while (total > 0 && rqstp->rq_next_page < ....)

and then realised that I don't know what to put there.
I don't think that counting up to rq_maxpages is safe when there could be two READ ops in an NFSv4 Compound.

Quoted 5 lines

And if we are cleaning up this function I woul put the increments of v and rq_next_page next to each other..
</bikeshed>

The patch is an improvement. I wonder if it is enough.

I'm planning a second patch that adds protection against overrunning the rq_pages array. Is there anything else you feel is needed?

Signature · 1 line
Chuck Lever
#4 lore raw
Quoted 21 lines · Chuck Lever
Quoted 20 lines · NeilBrown

fs/nfsd/vfs.c __be32 nfsd_iter_read(struct svc_rqst *rqstp, struct svc_fh *fhp, · line 1125

Quoted 12 lines
fs/nfsd/vfs.c +2 -1
index 714777c221ed..e2f0fe3f82c0 100644
@@ -1120,13 +1120,13 @@ __be32 nfsd_iter_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
 		bvec_set_page(&rqstp->rq_bvec[v], *(rqstp->rq_next_page++),
 			      len, base);
 		total -= len;
-		++v;
 		base = 0;
+		if (++v >= rqstp->rq_maxpages)
+			break;

I would have changed the head of the while loop to be

  while (total > 0 && rqstp->rq_next_page < ....)

and then realised that I don't know what to put there.
I don't think that counting up to rq_maxpages is safe when there could be two READ ops in an NFSv4 Compound.

Quoted 8 lines
Quoted 5 lines

And if we are cleaning up this function I woul put the increments of v and rq_next_page next to each other..
</bikeshed>

The patch is an improvement. I wonder if it is enough.

I'm planning a second patch that adds protection against overrunning the rq_pages array. Is there anything else you feel is needed?

It might be easier to review the two patches together, but if we decide that we aren't worrying about multiple users of the page list I would rather the code looked like:

	while (total > 0 && v < rqstp->rq_maxpages) {
		len = min_t(size_t, total, PAGE_SIZE - base);
		bvec_set_page(&rqstp->rq_bvec[v], *(rqstp->rq_next_page),
			      len, base);
		total -= len;
		v += 1;
		rqstp->rq_nextpage += 1
		base = 0;
	}

So all the tests are together, all the increments are together. I don't mind if you keep "++" but as I personally don't much like it I changed it here.
A possible alternative would be

		bvec_set_page(&rqstp->rq_bvec[v++], *(rqstp->rq_next_page++),
			      len, base);

then again all the increments would be together - I don't mind ++ in this context, I just don't much like it stand-alone.

But if you disagree, leave it as it is. Probably the part I like the least is the "break" and the end of the while(). Having multiple exits from a loop is sometimes needed, but should be avoided when not needed.

Thanks,
NeilBrown