Skip to contentSkip to conversation

splice: Prevent gifting of multipage folios

RFC PATCH Superseded David Howells 2 participants 3 messages Feb 27, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 splice: Prevent gifting of multipage folios

Don't let parts of multipage folios be gifted by (vm)splice into a pipe as the other end may only be expecting single-page gifts (fuse and virtio console for example).

replace_page_cache_folio(), for example, will do the wrong thing if it tries to replace a single paged folio with a multipage folio.

Try to avoid this by making add_to_pipe() remove the gift flag on multipage folios.

---
 fs/splice.c |    2 ++
 1 file changed, 2 insertions(+)
fs/splice.c +2 -0
index 2e76dbb81a8f..33caa28a86e4 100644
@@ -240,6 +240,8 @@ ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
 	} else if (pipe_full(head, tail, pipe->max_usage)) {
 		ret = -EAGAIN;
 	} else {
+		if (folio_nr_pages(page_folio(buf->page)) > 1)
+			buf->flags &= ~PIPE_BUF_FLAG_GIFT;
 		pipe->bufs[head & mask] = *buf;
 		pipe->head = head + 1;
 		return buf->len;
#1 lore raw
Quoted 5 lines · David Howells

Don't let parts of multipage folios be gifted by (vm)splice into a pipe as the other end may only be expecting single-page gifts (fuse and virtio console for example).

Quoted 7 lines

replace_page_cache_folio(), for example, will do the wrong thing if it tries to replace a single paged folio with a multipage folio.

Try to avoid this by making add_to_pipe() remove the gift flag on multipage folios.

What should the Fixes: here be? This was already possible with THPs (both anon and tmpfs backed) long before I introduced folios.

Quoted 15 lines
---
 fs/splice.c |    2 ++
 1 file changed, 2 insertions(+)
fs/splice.c +0 -0
index 2e76dbb81a8f..33caa28a86e4 100644

fs/splice.c ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf) · line 244

Quoted 5 lines
fs/splice.c +2 -0
@@ -240,6 +240,8 @@ ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
 	} else if (pipe_full(head, tail, pipe->max_usage)) {
 		ret = -EAGAIN;
 	} else {
+		if (folio_nr_pages(page_folio(buf->page)) > 1)
+			buf->flags &= ~PIPE_BUF_FLAG_GIFT;
		if (PageCompound(buf->page))
			buf->flags &= ~PIPE_BUF_FLAG_GIFT;

would be simpler and more backportable.

#2 lore raw
Quoted 2 lines · Matthew Wilcox

What should the Fixes: here be? This was already possible with THPs (both anon and tmpfs backed) long before I introduced folios.

Good question. How do I tell when THPs were introduced? It looks like they might predate git, looking at free_compound_page()'s comment.

David