Skip to contentSkip to conversation

nfsd: don't replace page in rq_pages if it's a continuation of last page

PATCH Superseded Jeff Layton 2 participants 10 messages Mar 17, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jeff Layton

author maintainer
#0 lore raw

PATCH 1/2 nfsd: don't replace page in rq_pages if it's a continuation of last page

The splice read calls nfsd_splice_actor to put the pages containing file data into the svc_rqst->rq_pages array. It's possible however to get a splice result that only has a partial page at the end, if (e.g.) the filesystem hands back a short read that doesn't cover the whole page.

nfsd_splice_actor will plop the partial page into its rq_pages array and return. Then later, when nfsd_splice_actor is called again, the remainder of the page may end up being filled out. At this point, nfsd_splice_actor will put the page into the array _again_ corrupting the reply. If this is done enough times, rq_next_page will overrun the array and corrupt the trailing fields -- the rq_respages and rq_next_page pointers themselves.

If we've already added the page to the array in the last pass, don't add it to the array a second time when dealing with a splice continuation. This was originally handled properly in nfsd_splice_actor, but commit 91e23b1c3982 removed the check for it, and started universally replacing pages.

---
 fs/nfsd/vfs.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/nfsd/vfs.c +5 -2
index 502e1b7742db..3709ef57d96e 100644
@@ -941,8 +941,11 @@ nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
 	struct page *last_page;
 
 	last_page = page + (offset + sd->len - 1) / PAGE_SIZE;
-	for (page += offset / PAGE_SIZE; page <= last_page; page++)
-		svc_rqst_replace_page(rqstp, page);
+	for (page += offset / PAGE_SIZE; page <= last_page; page++) {
+		/* Only replace page if we haven't already done so */
+		if (page != *(rqstp->rq_next_page - 1))
+			svc_rqst_replace_page(rqstp, page);
+	}
 	if (rqstp->rq_res.page_len == 0)	// first call
 		rqstp->rq_res.page_base = offset % PAGE_SIZE;
 	rqstp->rq_res.page_len += sd->len;
Signature · 1 line
2.39.2
author maintainer
#1 lore raw

PATCH 2/2 sunrpc: add bounds checking to svc_rqst_replace_page

There's no good way to handle this gracefully, but if rq_next_page ends up pointing outside the array, we can at least crash the box before it scribbles over too much else.

---
 net/sunrpc/svc.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
net/sunrpc/svc.c +10 -0
index fea7ce8fba14..864e62945647 100644
@@ -845,6 +845,16 @@ EXPORT_SYMBOL_GPL(svc_set_num_threads);
  */
 void svc_rqst_replace_page(struct svc_rqst *rqstp, struct page *page)
 {
+	struct page **begin, **end;
+
+	/*
+	 * Bounds check: make sure rq_next_page points into the rq_respages
+	 * part of the array.
+	 */
+	begin = rqstp->rq_pages;
+	end = &rqstp->rq_pages[RPCSVC_MAXPAGES];
+	BUG_ON(rqstp->rq_next_page < begin || rqstp->rq_next_page > end);
+
 	if (*rqstp->rq_next_page) {
 		if (!pagevec_space(&rqstp->rq_pvec))
 			__pagevec_release(&rqstp->rq_pvec);
Signature · 1 line
2.39.2
author maintainer
#5 lore raw
In reply to Jeff Layton
Quoted 33 lines · Jeff Layton

The splice read calls nfsd_splice_actor to put the pages containing file data into the svc_rqst->rq_pages array. It's possible however to get a splice result that only has a partial page at the end, if (e.g.) the filesystem hands back a short read that doesn't cover the whole page.

nfsd_splice_actor will plop the partial page into its rq_pages array and return. Then later, when nfsd_splice_actor is called again, the remainder of the page may end up being filled out. At this point, nfsd_splice_actor will put the page into the array _again_ corrupting the reply. If this is done enough times, rq_next_page will overrun the array and corrupt the trailing fields -- the rq_respages and rq_next_page pointers themselves.

If we've already added the page to the array in the last pass, don't add it to the array a second time when dealing with a splice continuation. This was originally handled properly in nfsd_splice_actor, but commit 91e23b1c3982 removed the check for it, and started universally replacing pages.

---
 fs/nfsd/vfs.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/nfsd/vfs.c +0 -0
index 502e1b7742db..3709ef57d96e 100644

fs/nfsd/vfs.c nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf, · line 945

Quoted 7 lines
fs/nfsd/vfs.c +2 -2
@@ -941,8 +941,11 @@ nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
 	struct page *last_page;
 
 	last_page = page + (offset + sd->len - 1) / PAGE_SIZE;
-	for (page += offset / PAGE_SIZE; page <= last_page; page++)
-		svc_rqst_replace_page(rqstp, page);
+	for (page += offset / PAGE_SIZE; page <= last_page; page++) {
+		/* Only replace page if we haven't already done so */

Note that I think that this was probably the real rationale for the pp[- 1] check that 91e23b1c3982 removed. Given that, maybe we should flesh this comment out a bit more for posterity?

/*
* When we're splicing from a pipe, it's possible that
* we'll get an incomplete page that may be updated on
* a later call. Only splice it into rq_pages once. */

Quoted 6 lines
fs/nfsd/vfs.c +3 -0
+		if (page != *(rqstp->rq_next_page - 1))
+			svc_rqst_replace_page(rqstp, page);
+	}
 	if (rqstp->rq_res.page_len == 0)	// first call
 		rqstp->rq_res.page_base = offset % PAGE_SIZE;
 	rqstp->rq_res.page_len += sd->len;
Signature · 1 line
Jeff Layton <[email protected]>
#2 lore raw

Re: [PATCH 2/2] sunrpc: add bounds checking to svc_rqst_replace_page

In reply to Jeff Layton
Quoted 20 lines · Jeff Layton

There's no good way to handle this gracefully, but if rq_next_page ends up pointing outside the array, we can at least crash the box before it scribbles over too much else.

---
net/sunrpc/svc.c | 10 ++++++++++

1 file changed, 10 insertions(+)
net/sunrpc/svc.c +0 -0
index fea7ce8fba14..864e62945647 100644
@@ -845,6 +845,16 @@ EXPORT_SYMBOL_GPL(svc_set_num_threads);
 */
 void svc_rqst_replace_page(struct svc_rqst *rqstp, struct page *page)

net/sunrpc/svc.c EXPORT_SYMBOL_GPL(svc_set_num_threads); · line 856

Quoted 10 lines
net/sunrpc/svc.c +9 -0
 {
+	struct page **begin, **end;
+
+	/*
+	 * Bounds check: make sure rq_next_page points into the rq_respages
+	 * part of the array.
+	 */
+	begin = rqstp->rq_pages;
+	end = &rqstp->rq_pages[RPCSVC_MAXPAGES];
+	BUG_ON(rqstp->rq_next_page < begin || rqstp->rq_next_page > end);

Linus has stated clearly that he does not want BUG_ON assertions if the system is not actually in danger... and this is clearly the result of a software bug, so a crash will occur anyway.

Can you make this a pr_warn_once() ?

Quoted 7 lines
net/sunrpc/svc.c +1 -0
+
 	if (*rqstp->rq_next_page) {
 		if (!pagevec_space(&rqstp->rq_pvec))
 			__pagevec_release(&rqstp->rq_pvec);
Signature · 1 line
2.39.2

--
Chuck Lever

author maintainer
#3 lore raw

Re: [PATCH 2/2] sunrpc: add bounds checking to svc_rqst_replace_page

Quoted 25 lines · Chuck Lever III
Quoted 25 lines · Jeff Layton

There's no good way to handle this gracefully, but if rq_next_page ends up pointing outside the array, we can at least crash the box before it scribbles over too much else.

---
net/sunrpc/svc.c | 10 ++++++++++

1 file changed, 10 insertions(+)
net/sunrpc/svc.c +5 -0
index fea7ce8fba14..864e62945647 100644
@@ -845,6 +845,16 @@ EXPORT_SYMBOL_GPL(svc_set_num_threads);
 */
 void svc_rqst_replace_page(struct svc_rqst *rqstp, struct page *page)
 {
+	struct page **begin, **end;
+
+	/*
+	 * Bounds check: make sure rq_next_page points into the rq_respages
+	 * part of the array.
Quoted 9 lines

net/sunrpc/svc.c EXPORT_SYMBOL_GPL(svc_set_num_threads); · line 856

Quoted 4 lines
net/sunrpc/svc.c +4 -0
+	 */
+	begin = rqstp->rq_pages;
+	end = &rqstp->rq_pages[RPCSVC_MAXPAGES];
+	BUG_ON(rqstp->rq_next_page < begin || rqstp->rq_next_page > end);

Linus has stated clearly that he does not want BUG_ON assertions if the system is not actually in danger... and this is clearly the result of a software bug, so a crash will occur anyway.

It'll crash, but only after we scribble over some memory.

Actually, it looks like the splice actor can return an error. We could return -EIO here or something without doing anything if we hit this case and then let that bubble back up to the read?

Quoted 15 lines

Can you make this a pr_warn_once() ?

Quoted 7 lines
net/sunrpc/svc.c +1 -0
+
 	if (*rqstp->rq_next_page) {
 		if (!pagevec_space(&rqstp->rq_pvec))
 			__pagevec_release(&rqstp->rq_pvec);
Signature · 1 line
2.39.2

--
Chuck Lever

Signature · 1 line
Jeff Layton <[email protected]>
#4 lore raw

Re: [PATCH 2/2] sunrpc: add bounds checking to svc_rqst_replace_page

Quoted 40 lines · Jeff Layton
Quoted 25 lines · Chuck Lever III
Quoted 25 lines · Jeff Layton

There's no good way to handle this gracefully, but if rq_next_page ends up pointing outside the array, we can at least crash the box before it scribbles over too much else.

---
net/sunrpc/svc.c | 10 ++++++++++

1 file changed, 10 insertions(+)
net/sunrpc/svc.c +5 -0
index fea7ce8fba14..864e62945647 100644
@@ -845,6 +845,16 @@ EXPORT_SYMBOL_GPL(svc_set_num_threads);
 */
 void svc_rqst_replace_page(struct svc_rqst *rqstp, struct page *page)
 {
+	struct page **begin, **end;
+
+	/*
+	 * Bounds check: make sure rq_next_page points into the rq_respages
+	 * part of the array.
Quoted 9 lines

net/sunrpc/svc.c EXPORT_SYMBOL_GPL(svc_set_num_threads); · line 856

Quoted 4 lines
net/sunrpc/svc.c +4 -0
+	 */
+	begin = rqstp->rq_pages;
+	end = &rqstp->rq_pages[RPCSVC_MAXPAGES];
+	BUG_ON(rqstp->rq_next_page < begin || rqstp->rq_next_page > end);

Linus has stated clearly that he does not want BUG_ON assertions if the system is not actually in danger... and this is clearly the result of a software bug, so a crash will occur anyway.

Quoted 5 lines

It'll crash, but only after we scribble over some memory.

Actually, it looks like the splice actor can return an error. We could return -EIO here or something without doing anything if we hit this case and then let that bubble back up to the read?

Yes, if it's possible to fail just the READ operation, that would be best. Maybe a emitting a trace event would be better than a pr_warn.

Quoted 11 lines
Quoted 11 lines

Can you make this a pr_warn_once() ?

Quoted 7 lines
net/sunrpc/svc.c +1 -0
+
 	if (*rqstp->rq_next_page) {
 		if (!pagevec_space(&rqstp->rq_pvec))
 			__pagevec_release(&rqstp->rq_pvec);
Signature · 1 line
2.39.2
Quoted 7 lines
Quoted 4 lines

--
Chuck Lever

Signature · 1 line
Jeff Layton <[email protected]>

--
Chuck Lever

#6 lore raw
In reply to Jeff Layton
Quoted 50 lines · Jeff Layton
Quoted 33 lines · Jeff Layton

The splice read calls nfsd_splice_actor to put the pages containing file data into the svc_rqst->rq_pages array. It's possible however to get a splice result that only has a partial page at the end, if (e.g.) the filesystem hands back a short read that doesn't cover the whole page.

nfsd_splice_actor will plop the partial page into its rq_pages array and return. Then later, when nfsd_splice_actor is called again, the remainder of the page may end up being filled out. At this point, nfsd_splice_actor will put the page into the array _again_ corrupting the reply. If this is done enough times, rq_next_page will overrun the array and corrupt the trailing fields -- the rq_respages and rq_next_page pointers themselves.

If we've already added the page to the array in the last pass, don't add it to the array a second time when dealing with a splice continuation. This was originally handled properly in nfsd_splice_actor, but commit 91e23b1c3982 removed the check for it, and started universally replacing pages.

---
fs/nfsd/vfs.c | 7 +++++--

1 file changed, 5 insertions(+), 2 deletions(-)
fs/nfsd/vfs.c +0 -0
index 502e1b7742db..3709ef57d96e 100644

fs/nfsd/vfs.c nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf, · line 945

Quoted 7 lines
fs/nfsd/vfs.c +2 -2
@@ -941,8 +941,11 @@ nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
 	struct page *last_page;
 
 	last_page = page + (offset + sd->len - 1) / PAGE_SIZE;
-	for (page += offset / PAGE_SIZE; page <= last_page; page++)
-		svc_rqst_replace_page(rqstp, page);
+	for (page += offset / PAGE_SIZE; page <= last_page; page++) {
+		/* Only replace page if we haven't already done so */

Note that I think that this was probably the real rationale for the pp[- 1] check that 91e23b1c3982 removed. Given that, maybe we should flesh this comment out a bit more for posterity?

Quoted 5 lines

/*
* When we're splicing from a pipe, it's possible that
* we'll get an incomplete page that may be updated on
* a later call. Only splice it into rq_pages once. */

The "real" bug here is that the API contract for pipe splicing isn't well defined, so I agree that it's very likely the pp[-1] check was because a splice can call the actor repeatedly for the same page. No one could remember why that check was there.

To be clear, if the passed-in page matches the current page in the rqst, we're "extending the current page" rather than avoiding replacement... maybe:

	/*
	 * Skip page replacement when extending the contents
	 * of the current page.
	 */

In the patch description, would you mention that this case arises if the READ request is not page-aligned?

If you resend this patch, please Cc: viro@ . Thanks for chasing this down!

Quoted 9 lines
Quoted 6 lines
fs/nfsd/vfs.c +3 -0
+		if (page != *(rqstp->rq_next_page - 1))
+			svc_rqst_replace_page(rqstp, page);
+	}
 	if (rqstp->rq_res.page_len == 0)	// first call
 		rqstp->rq_res.page_base = offset % PAGE_SIZE;
 	rqstp->rq_res.page_len += sd->len;
Signature · 1 line
Jeff Layton <[email protected]>

--
Chuck Lever

author maintainer
#7 lore raw
Quoted 55 lines · Chuck Lever III
Quoted 49 lines · Jeff Layton
Quoted 33 lines · Jeff Layton

The splice read calls nfsd_splice_actor to put the pages containing file data into the svc_rqst->rq_pages array. It's possible however to get a splice result that only has a partial page at the end, if (e.g.) the filesystem hands back a short read that doesn't cover the whole page.

nfsd_splice_actor will plop the partial page into its rq_pages array and return. Then later, when nfsd_splice_actor is called again, the remainder of the page may end up being filled out. At this point, nfsd_splice_actor will put the page into the array _again_ corrupting the reply. If this is done enough times, rq_next_page will overrun the array and corrupt the trailing fields -- the rq_respages and rq_next_page pointers themselves.

If we've already added the page to the array in the last pass, don't add it to the array a second time when dealing with a splice continuation. This was originally handled properly in nfsd_splice_actor, but commit 91e23b1c3982 removed the check for it, and started universally replacing pages.

---
fs/nfsd/vfs.c | 7 +++++--

1 file changed, 5 insertions(+), 2 deletions(-)
fs/nfsd/vfs.c +0 -0
index 502e1b7742db..3709ef57d96e 100644

fs/nfsd/vfs.c nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf, · line 945

Quoted 7 lines
fs/nfsd/vfs.c +2 -2
@@ -941,8 +941,11 @@ nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
 	struct page *last_page;
 
 	last_page = page + (offset + sd->len - 1) / PAGE_SIZE;
-	for (page += offset / PAGE_SIZE; page <= last_page; page++)
-		svc_rqst_replace_page(rqstp, page);
+	for (page += offset / PAGE_SIZE; page <= last_page; page++) {
+		/* Only replace page if we haven't already done so */

Note that I think that this was probably the real rationale for the pp[- 1] check that 91e23b1c3982 removed. Given that, maybe we should flesh this comment out a bit more for posterity?

Quoted 5 lines

/*
* When we're splicing from a pipe, it's possible that
* we'll get an incomplete page that may be updated on
* a later call. Only splice it into rq_pages once. */

Quoted 5 lines

The "real" bug here is that the API contract for pipe splicing isn't well defined, so I agree that it's very likely the pp[-1] check was because a splice can call the actor repeatedly for the same page. No one could remember why that check was there.

The whole splice API is a minefield.

Quoted 9 lines

To be clear, if the passed-in page matches the current page in the rqst, we're "extending the current page" rather than avoiding replacement... maybe:

	/*
	 * Skip page replacement when extending the contents
	 * of the current page.
	 */

Sure, sounds good.

Quoted 3 lines

In the patch description, would you mention that this case arises if the READ request is not page-aligned?

Does it though? I'm not sure that page alignment has that much to do with it. I imagine you can hit this even with aligned I/Os.

My guess is the bigger issue is when your storage is doing sub-page-size I/Os under the hood. We end up filling up part of a page from storage and the kernel submits what it has to the pipe and then the next bit comes in and the page is updated for the next actor call.

Quoted 3 lines

If you resend this patch, please Cc: viro@ . Thanks for chasing this down!

Will do.

Quoted 15 lines
Quoted 10 lines
Quoted 7 lines
fs/nfsd/vfs.c +3 -0
+		if (page != *(rqstp->rq_next_page - 1))
+			svc_rqst_replace_page(rqstp, page);
+	}
 	if (rqstp->rq_res.page_len == 0)	// first call
 		rqstp->rq_res.page_base = offset % PAGE_SIZE;
 	rqstp->rq_res.page_len += sd->len;
Signature · 1 line
Jeff Layton <[email protected]>

--
Chuck Lever

Signature · 1 line
Jeff Layton <[email protected]>
#8 lore raw
Quoted 11 lines · Jeff Layton
Quoted 2 lines · Chuck Lever III

In the patch description, would you mention that this case arises if the READ request is not page-aligned?

Does it though? I'm not sure that page alignment has that much to do with it. I imagine you can hit this even with aligned I/Os.

Maybe, but no-one has actually seen that. The vast majority of reports of this problem are with unaligned I/O, which POSIX OS NFS clients (like the Linux NFS client) usually avoid.

I didn't mean to exclude the possibility of hitting this issue in other ways, but simply observing a common way it is hit.

--
Chuck Lever

author maintainer
#9 lore raw
Quoted 11 lines · Chuck Lever III
Quoted 10 lines · Jeff Layton
Quoted 2 lines · Chuck Lever III

In the patch description, would you mention that this case arises if the READ request is not page-aligned?

Does it though? I'm not sure that page alignment has that much to do with it. I imagine you can hit this even with aligned I/Os.

Quoted 7 lines

Maybe, but no-one has actually seen that. The vast majority of reports of this problem are with unaligned I/O, which POSIX OS NFS clients (like the Linux NFS client) usually avoid.

I didn't mean to exclude the possibility of hitting this issue in other ways, but simply observing a common way it is hit.

An unaligned read will consume an extra page, so maybe it just makes it more likely to overrun the array in that case?

Signature · 1 line
Jeff Layton <[email protected]>