Skip to content

Commit e69f37b

Browse files
dhowellsaxboe
authored andcommitted
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: Christoph Hellwig <[email protected]> Signed-off-by: David Howells <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Reviewed-by: Christian Brauner <[email protected]> cc: Jens Axboe <[email protected]> cc: Al Viro <[email protected]> cc: David Hildenbrand <[email protected]> cc: John Hubbard <[email protected]> cc: [email protected] cc: [email protected] cc: [email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 69df79a commit e69f37b

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

fs/splice.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ ssize_t copy_splice_read(struct file *in, loff_t *ppos,
311311
struct kiocb kiocb;
312312
struct page **pages;
313313
ssize_t ret;
314-
size_t used, npages, chunk, remain, reclaim;
314+
size_t used, npages, chunk, remain, keep = 0;
315315
int i;
316316

317317
/* 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,
325325
if (!bv)
326326
return -ENOMEM;
327327

328-
pages = (void *)(bv + npages);
328+
pages = (struct page **)(bv + npages);
329329
npages = alloc_pages_bulk_array(GFP_USER, npages, pages);
330330
if (!npages) {
331331
kfree(bv);
@@ -348,11 +348,8 @@ ssize_t copy_splice_read(struct file *in, loff_t *ppos,
348348
kiocb.ki_pos = *ppos;
349349
ret = call_read_iter(in, &kiocb, &to);
350350

351-
reclaim = npages * PAGE_SIZE;
352-
remain = 0;
353351
if (ret > 0) {
354-
reclaim -= ret;
355-
remain = ret;
352+
keep = DIV_ROUND_UP(ret, PAGE_SIZE);
356353
*ppos = kiocb.ki_pos;
357354
file_accessed(in);
358355
} else if (ret < 0) {
@@ -365,14 +362,12 @@ ssize_t copy_splice_read(struct file *in, loff_t *ppos,
365362
}
366363

367364
/* Free any pages that didn't get touched at all. */
368-
reclaim /= PAGE_SIZE;
369-
if (reclaim) {
370-
npages -= reclaim;
371-
release_pages(pages + npages, reclaim);
372-
}
365+
if (keep < npages)
366+
release_pages(pages + keep, npages - keep);
373367

374368
/* Push the remaining pages into the pipe. */
375-
for (i = 0; i < npages; i++) {
369+
remain = ret;
370+
for (i = 0; i < keep; i++) {
376371
struct pipe_buffer *buf = pipe_head_buf(pipe);
377372

378373
chunk = min_t(size_t, remain, PAGE_SIZE);

0 commit comments

Comments
 (0)