From: David Howells <dhowells@redhat.com>
To: Jens Axboe <axboe@kernel.dk>, Al Viro <viro@zeniv.linux.org.uk>,
Christoph Hellwig <hch@infradead.org>
Cc: David Howells <dhowells@redhat.com>,
Matthew Wilcox <willy@infradead.org>, Jan Kara <jack@suse.cz>,
Jeff Layton <jlayton@kernel.org>,
David Hildenbrand <david@redhat.com>,
Jason Gunthorpe <jgg@nvidia.com>,
Logan Gunthorpe <logang@deltatee.com>,
Hillf Danton <hdanton@sina.com>,
linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org,
linux-kernel@vger.kernel.org, linux-mm@kvack.org,
syzbot+a440341a59e3b7142895@syzkaller.appspotmail.com,
Christoph Hellwig <hch@lst.de>,
John Hubbard <jhubbard@nvidia.com>
Subject: [PATCH v12 01/10] vfs, iomap: Fix generic_file_splice_read() to avoid reversion of ITER_PIPE
Date: Tue, 7 Feb 2023 17:12:56 +0000 [thread overview]
Message-ID: <20230207171305.3716974-2-dhowells@redhat.com> (raw)
In-Reply-To: <20230207171305.3716974-1-dhowells@redhat.com>
With the new iov_iter_extract_pages() function (added in a later patch),
pages extracted from a non-user-backed iterator, such as ITER_PIPE, aren't
pinned. __iomap_dio_rw(), however, calls iov_iter_revert() to shorten the
iterator to just the data it is going to use - which causes the pipe
buffers to be freed, even though they're attached to a bio and may get
written to by DMA (thanks to Hillf Danton for spotting this[1]).
This then causes massive memory corruption that is particularly noticable
when the syzbot test[2] is run. The test boils down to:
out = creat(argv[1], 0666);
ftruncate(out, 0x800);
lseek(out, 0x200, SEEK_SET);
in = open(argv[1], O_RDONLY | O_DIRECT | O_NOFOLLOW);
sendfile(out, in, NULL, 0x1dd00);
run repeatedly in parallel. What I think is happening is that ftruncate()
occasionally shortens the DIO read that's about to be made by sendfile's
splice core by reducing i_size.
Fix this by replacing the use of an ITER_PIPE iterator with an ITER_BVEC
iterator for which reversion won't free the buffers. Bulk allocate all the
buffers we think we're going to use in advance, do the read synchronously
and only then trim the buffer down. The pages we did use get pushed into
the pipe.
This is more efficient by virtue of doing a bulk page allocation, but
slightly less efficient by ignoring any partial page in the pipe.
Note that this removes the only user of ITER_PIPE.
Fixes: 920756a3306a ("block: Convert bio_iov_iter_get_pages to use iov_iter_extract_pages")
Reported-by: syzbot+a440341a59e3b7142895@syzkaller.appspotmail.com
Signed-off-by: David Howells <dhowells@redhat.com>
cc: Jens Axboe <axboe@kernel.dk>
cc: Christoph Hellwig <hch@lst.de>
cc: Al Viro <viro@zeniv.linux.org.uk>
cc: David Hildenbrand <david@redhat.com>
cc: John Hubbard <jhubbard@nvidia.com>
cc: linux-mm@kvack.org
cc: linux-block@vger.kernel.org
cc: linux-fsdevel@vger.kernel.org
Link: https://lore.kernel.org/r/20230207094731.1390-1-hdanton@sina.com/ [1]
Link: https://lore.kernel.org/r/000000000000b0b3c005f3a09383@google.com/ [2]
---
fs/splice.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++------
1 file changed, 68 insertions(+), 8 deletions(-)
diff --git a/fs/splice.c b/fs/splice.c
index 5969b7a1d353..51778437f31f 100644
--- a/fs/splice.c
+++ b/fs/splice.c
@@ -295,24 +295,62 @@ void splice_shrink_spd(struct splice_pipe_desc *spd)
* used as long as it has more or less sane ->read_iter().
*
*/
-ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
+ssize_t generic_file_splice_read(struct file *file, loff_t *ppos,
struct pipe_inode_info *pipe, size_t len,
unsigned int flags)
{
+ LIST_HEAD(pages);
struct iov_iter to;
+ struct bio_vec *bv;
struct kiocb kiocb;
- int ret;
+ struct page *page;
+ unsigned int head;
+ ssize_t ret;
+ size_t used, npages, chunk, remain, reclaim;
+ int i;
+
+ /* Work out how much data we can actually add into the pipe */
+ used = pipe_occupancy(pipe->head, pipe->tail);
+ npages = max_t(ssize_t, pipe->max_usage - used, 0);
+ len = min_t(size_t, len, npages * PAGE_SIZE);
+ npages = DIV_ROUND_UP(len, PAGE_SIZE);
+
+ bv = kmalloc(array_size(npages, sizeof(bv[0])), GFP_KERNEL);
+ if (!bv)
+ return -ENOMEM;
+
+ npages = alloc_pages_bulk_list(GFP_USER, npages, &pages);
+ if (!npages) {
+ kfree(bv);
+ return -ENOMEM;
+ }
- iov_iter_pipe(&to, ITER_DEST, pipe, len);
- init_sync_kiocb(&kiocb, in);
+ remain = len = min_t(size_t, len, npages * PAGE_SIZE);
+
+ for (i = 0; i < npages; i++) {
+ chunk = min_t(size_t, PAGE_SIZE, remain);
+ page = list_first_entry(&pages, struct page, lru);
+ list_del_init(&page->lru);
+ bv[i].bv_page = page;
+ bv[i].bv_offset = 0;
+ bv[i].bv_len = chunk;
+ remain -= chunk;
+ }
+
+ /* Do the I/O */
+ iov_iter_bvec(&to, ITER_DEST, bv, npages, len);
+ init_sync_kiocb(&kiocb, file);
kiocb.ki_pos = *ppos;
- ret = call_read_iter(in, &kiocb, &to);
+ ret = call_read_iter(file, &kiocb, &to);
+
+ reclaim = npages * PAGE_SIZE;
+ remain = 0;
if (ret > 0) {
+ reclaim -= ret;
+ remain = ret;
*ppos = kiocb.ki_pos;
- file_accessed(in);
+ file_accessed(file);
} else if (ret < 0) {
- /* free what was emitted */
- pipe_discard_from(pipe, to.start_head);
/*
* callers of ->splice_read() expect -EAGAIN on
* "can't put anything in there", rather than -EFAULT.
@@ -321,6 +359,28 @@ ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
ret = -EAGAIN;
}
+ /* Free any pages that didn't get touched at all. */
+ for (; reclaim >= PAGE_SIZE; reclaim -= PAGE_SIZE)
+ __free_page(bv[--npages].bv_page);
+
+ /* Push the remaining pages into the pipe. */
+ head = pipe->head;
+ for (i = 0; i < npages; i++) {
+ struct pipe_buffer *buf = &pipe->bufs[head & (pipe->ring_size - 1)];
+
+ chunk = min_t(size_t, remain, PAGE_SIZE);
+ *buf = (struct pipe_buffer) {
+ .ops = &default_pipe_buf_ops,
+ .page = bv[i].bv_page,
+ .offset = 0,
+ .len = chunk,
+ };
+ head++;
+ remain -= chunk;
+ }
+ pipe->head = head;
+
+ kfree(bv);
return ret;
}
EXPORT_SYMBOL(generic_file_splice_read);
next prev parent reply other threads:[~2023-02-07 17:14 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-07 17:12 [PATCH v12 00/10] iov_iter: Improve page extraction (pin or just list) David Howells
2023-02-07 17:12 ` David Howells [this message]
2023-02-08 5:26 ` [PATCH v12 01/10] vfs, iomap: Fix generic_file_splice_read() to avoid reversion of ITER_PIPE Christoph Hellwig
2023-02-08 16:09 ` David Howells
2023-02-08 16:19 ` Christoph Hellwig
2023-02-08 17:21 ` Matthew Wilcox
2023-02-08 18:15 ` David Howells
2023-02-09 10:50 ` David Howells
2023-02-07 17:12 ` [PATCH v12 02/10] iov_iter: Kill ITER_PIPE David Howells
2023-02-07 17:12 ` [PATCH v12 03/10] iov_iter: Define flags to qualify page extraction David Howells
2023-02-07 17:12 ` [PATCH v12 04/10] iov_iter: Add a function to extract a page list from an iterator David Howells
2023-02-07 17:13 ` [PATCH v12 05/10] iomap: Don't get an reference on ZERO_PAGE for direct I/O block zeroing David Howells
2023-02-07 17:13 ` [PATCH v12 06/10] block: Fix bio_flagged() so that gcc can better optimise it David Howells
2023-02-07 17:13 ` [PATCH v12 07/10] block: Replace BIO_NO_PAGE_REF with BIO_PAGE_REFFED with inverted logic David Howells
2023-02-07 17:13 ` [PATCH v12 08/10] block: Add BIO_PAGE_PINNED and associated infrastructure David Howells
2023-02-07 17:13 ` [PATCH v12 09/10] block: Convert bio_iov_iter_get_pages to use iov_iter_extract_pages David Howells
2023-02-07 17:13 ` [PATCH v12 10/10] block: convert bio_map_user_iov " David Howells
2023-02-07 18:49 ` [PATCH v12 00/10] iov_iter: Improve page extraction (pin or just list) Jens Axboe
2023-02-07 18:51 ` David Howells
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=20230207171305.3716974-2-dhowells@redhat.com \
--to=dhowells@redhat.com \
--cc=axboe@kernel.dk \
--cc=david@redhat.com \
--cc=hch@infradead.org \
--cc=hch@lst.de \
--cc=hdanton@sina.com \
--cc=jack@suse.cz \
--cc=jgg@nvidia.com \
--cc=jhubbard@nvidia.com \
--cc=jlayton@kernel.org \
--cc=linux-block@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=logang@deltatee.com \
--cc=syzbot+a440341a59e3b7142895@syzkaller.appspotmail.com \
--cc=viro@zeniv.linux.org.uk \
--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;
as well as URLs for NNTP newsgroup(s).