Commit e69f37bc authored by David Howells's avatar David Howells Committed by Jens Axboe

splice: Clean up copy_splice_read() a bit

Do a couple of cleanups to copy_splice_read():

 (1) Cast to struct page **, not void *.

 (2) Simplify the calculation of the number of pages to keep/reclaim in
     copy_splice_read().
Suggested-by: default avatarChristoph Hellwig <hch@infradead.org>
Signed-off-by: default avatarDavid Howells <dhowells@redhat.com>
Reviewed-by: default avatarChristoph Hellwig <hch@lst.de>
Reviewed-by: default avatarChristian Brauner <brauner@kernel.org>
cc: Jens Axboe <axboe@kernel.dk>
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/20230522135018.2742245-5-dhowells@redhat.comSigned-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent 69df79a4
...@@ -311,7 +311,7 @@ ssize_t copy_splice_read(struct file *in, loff_t *ppos, ...@@ -311,7 +311,7 @@ ssize_t copy_splice_read(struct file *in, loff_t *ppos,
struct kiocb kiocb; struct kiocb kiocb;
struct page **pages; struct page **pages;
ssize_t ret; ssize_t ret;
size_t used, npages, chunk, remain, reclaim; size_t used, npages, chunk, remain, keep = 0;
int i; int i;
/* Work out how much data we can actually add into the pipe */ /* Work out how much data we can actually add into the pipe */
...@@ -325,7 +325,7 @@ ssize_t copy_splice_read(struct file *in, loff_t *ppos, ...@@ -325,7 +325,7 @@ ssize_t copy_splice_read(struct file *in, loff_t *ppos,
if (!bv) if (!bv)
return -ENOMEM; return -ENOMEM;
pages = (void *)(bv + npages); pages = (struct page **)(bv + npages);
npages = alloc_pages_bulk_array(GFP_USER, npages, pages); npages = alloc_pages_bulk_array(GFP_USER, npages, pages);
if (!npages) { if (!npages) {
kfree(bv); kfree(bv);
...@@ -348,11 +348,8 @@ ssize_t copy_splice_read(struct file *in, loff_t *ppos, ...@@ -348,11 +348,8 @@ ssize_t copy_splice_read(struct file *in, loff_t *ppos,
kiocb.ki_pos = *ppos; kiocb.ki_pos = *ppos;
ret = call_read_iter(in, &kiocb, &to); ret = call_read_iter(in, &kiocb, &to);
reclaim = npages * PAGE_SIZE;
remain = 0;
if (ret > 0) { if (ret > 0) {
reclaim -= ret; keep = DIV_ROUND_UP(ret, PAGE_SIZE);
remain = ret;
*ppos = kiocb.ki_pos; *ppos = kiocb.ki_pos;
file_accessed(in); file_accessed(in);
} else if (ret < 0) { } else if (ret < 0) {
...@@ -365,14 +362,12 @@ ssize_t copy_splice_read(struct file *in, loff_t *ppos, ...@@ -365,14 +362,12 @@ ssize_t copy_splice_read(struct file *in, loff_t *ppos,
} }
/* Free any pages that didn't get touched at all. */ /* Free any pages that didn't get touched at all. */
reclaim /= PAGE_SIZE; if (keep < npages)
if (reclaim) { release_pages(pages + keep, npages - keep);
npages -= reclaim;
release_pages(pages + npages, reclaim);
}
/* Push the remaining pages into the pipe. */ /* Push the remaining pages into the pipe. */
for (i = 0; i < npages; i++) { remain = ret;
for (i = 0; i < keep; i++) {
struct pipe_buffer *buf = pipe_head_buf(pipe); struct pipe_buffer *buf = pipe_head_buf(pipe);
chunk = min_t(size_t, remain, PAGE_SIZE); chunk = min_t(size_t, remain, PAGE_SIZE);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment