1 // SPDX-License-Identifier: GPL-2.0-only
3 * "splice": joining two ropes together by interweaving their strands.
5 * This is the "extended pipe" functionality, where a pipe is used as
6 * an arbitrary in-memory buffer. Think of a pipe as a small kernel
7 * buffer that you can use to transfer data from one end to the other.
9 * The traditional unix read/write is extended with a "splice()" operation
10 * that transfers data buffers to or from a pipe buffer.
12 * Named by Larry McVoy, original implementation from Linus, extended by
13 * Jens to support splicing to files, network, direct splicing, etc and
14 * fixing lots of bugs.
16 * Copyright (C) 2005-2006 Jens Axboe <axboe@kernel.dk>
17 * Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org>
18 * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu>
21 #include <linux/bvec.h>
23 #include <linux/file.h>
24 #include <linux/pagemap.h>
25 #include <linux/splice.h>
26 #include <linux/memcontrol.h>
27 #include <linux/mm_inline.h>
28 #include <linux/swap.h>
29 #include <linux/writeback.h>
30 #include <linux/export.h>
31 #include <linux/syscalls.h>
32 #include <linux/uio.h>
33 #include <linux/security.h>
34 #include <linux/gfp.h>
35 #include <linux/socket.h>
36 #include <linux/compat.h>
37 #include <linux/sched/signal.h>
42 * Attempt to steal a page from a pipe buffer. This should perhaps go into
43 * a vm helper function, it's already simplified quite a bit by the
44 * addition of remove_mapping(). If success is returned, the caller may
45 * attempt to reuse this page for another destination.
47 static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe,
48 struct pipe_buffer *buf)
50 struct page *page = buf->page;
51 struct address_space *mapping;
55 mapping = page_mapping(page);
57 WARN_ON(!PageUptodate(page));
60 * At least for ext2 with nobh option, we need to wait on
61 * writeback completing on this page, since we'll remove it
62 * from the pagecache. Otherwise truncate wont wait on the
63 * page, allowing the disk blocks to be reused by someone else
64 * before we actually wrote our data to them. fs corruption
67 wait_on_page_writeback(page);
69 if (page_has_private(page) &&
70 !try_to_release_page(page, GFP_KERNEL))
74 * If we succeeded in removing the mapping, set LRU flag
77 if (remove_mapping(mapping, page)) {
78 buf->flags |= PIPE_BUF_FLAG_LRU;
84 * Raced with truncate or failed to remove page from current
85 * address space, unlock and return failure.
92 static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
93 struct pipe_buffer *buf)
96 buf->flags &= ~PIPE_BUF_FLAG_LRU;
100 * Check whether the contents of buf is OK to access. Since the content
101 * is a page cache page, IO may be in flight.
103 static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
104 struct pipe_buffer *buf)
106 struct page *page = buf->page;
109 if (!PageUptodate(page)) {
113 * Page got truncated/unhashed. This will cause a 0-byte
114 * splice, if this is the first page.
116 if (!page->mapping) {
122 * Uh oh, read-error from disk.
124 if (!PageUptodate(page)) {
130 * Page is ok afterall, we are done.
141 const struct pipe_buf_operations page_cache_pipe_buf_ops = {
142 .confirm = page_cache_pipe_buf_confirm,
143 .release = page_cache_pipe_buf_release,
144 .steal = page_cache_pipe_buf_steal,
145 .get = generic_pipe_buf_get,
148 static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
149 struct pipe_buffer *buf)
151 if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
154 buf->flags |= PIPE_BUF_FLAG_LRU;
155 return generic_pipe_buf_steal(pipe, buf);
158 static const struct pipe_buf_operations user_page_pipe_buf_ops = {
159 .confirm = generic_pipe_buf_confirm,
160 .release = page_cache_pipe_buf_release,
161 .steal = user_page_pipe_buf_steal,
162 .get = generic_pipe_buf_get,
165 static void wakeup_pipe_readers(struct pipe_inode_info *pipe)
168 if (waitqueue_active(&pipe->wait))
169 wake_up_interruptible(&pipe->wait);
170 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
174 * splice_to_pipe - fill passed data into a pipe
175 * @pipe: pipe to fill
179 * @spd contains a map of pages and len/offset tuples, along with
180 * the struct pipe_buf_operations associated with these pages. This
181 * function will link that data to the pipe.
184 ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
185 struct splice_pipe_desc *spd)
187 unsigned int spd_pages = spd->nr_pages;
188 unsigned int tail = pipe->tail;
189 unsigned int head = pipe->head;
190 unsigned int mask = pipe->ring_size - 1;
191 int ret = 0, page_nr = 0;
196 if (unlikely(!pipe->readers)) {
197 send_sig(SIGPIPE, current, 0);
202 while (!pipe_full(head, tail, pipe->max_usage)) {
203 struct pipe_buffer *buf = &pipe->bufs[head & mask];
205 buf->page = spd->pages[page_nr];
206 buf->offset = spd->partial[page_nr].offset;
207 buf->len = spd->partial[page_nr].len;
208 buf->private = spd->partial[page_nr].private;
217 if (!--spd->nr_pages)
225 while (page_nr < spd_pages)
226 spd->spd_release(spd, page_nr++);
230 EXPORT_SYMBOL_GPL(splice_to_pipe);
232 ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
234 unsigned int head = pipe->head;
235 unsigned int tail = pipe->tail;
236 unsigned int mask = pipe->ring_size - 1;
239 if (unlikely(!pipe->readers)) {
240 send_sig(SIGPIPE, current, 0);
242 } else if (pipe_full(head, tail, pipe->max_usage)) {
245 pipe->bufs[head & mask] = *buf;
246 pipe->head = head + 1;
249 pipe_buf_release(pipe, buf);
252 EXPORT_SYMBOL(add_to_pipe);
255 * Check if we need to grow the arrays holding pages and partial page
258 int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
260 unsigned int max_usage = READ_ONCE(pipe->max_usage);
262 spd->nr_pages_max = max_usage;
263 if (max_usage <= PIPE_DEF_BUFFERS)
266 spd->pages = kmalloc_array(max_usage, sizeof(struct page *), GFP_KERNEL);
267 spd->partial = kmalloc_array(max_usage, sizeof(struct partial_page),
270 if (spd->pages && spd->partial)
278 void splice_shrink_spd(struct splice_pipe_desc *spd)
280 if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
288 * generic_file_splice_read - splice data from file to a pipe
289 * @in: file to splice from
290 * @ppos: position in @in
291 * @pipe: pipe to splice to
292 * @len: number of bytes to splice
293 * @flags: splice modifier flags
296 * Will read pages from given file and fill them into a pipe. Can be
297 * used as long as it has more or less sane ->read_iter().
300 ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
301 struct pipe_inode_info *pipe, size_t len,
309 iov_iter_pipe(&to, READ, pipe, len);
311 init_sync_kiocb(&kiocb, in);
312 kiocb.ki_pos = *ppos;
313 ret = call_read_iter(in, &kiocb, &to);
315 *ppos = kiocb.ki_pos;
317 } else if (ret < 0) {
320 iov_iter_advance(&to, 0); /* to free what was emitted */
322 * callers of ->splice_read() expect -EAGAIN on
323 * "can't put anything in there", rather than -EFAULT.
331 EXPORT_SYMBOL(generic_file_splice_read);
333 const struct pipe_buf_operations default_pipe_buf_ops = {
334 .confirm = generic_pipe_buf_confirm,
335 .release = generic_pipe_buf_release,
336 .steal = generic_pipe_buf_steal,
337 .get = generic_pipe_buf_get,
340 int generic_pipe_buf_nosteal(struct pipe_inode_info *pipe,
341 struct pipe_buffer *buf)
346 /* Pipe buffer operations for a socket and similar. */
347 const struct pipe_buf_operations nosteal_pipe_buf_ops = {
348 .confirm = generic_pipe_buf_confirm,
349 .release = generic_pipe_buf_release,
350 .steal = generic_pipe_buf_nosteal,
351 .get = generic_pipe_buf_get,
353 EXPORT_SYMBOL(nosteal_pipe_buf_ops);
355 static ssize_t kernel_readv(struct file *file, const struct kvec *vec,
356 unsigned long vlen, loff_t offset)
364 /* The cast to a user pointer is valid due to the set_fs() */
365 res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos, 0);
371 static ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
372 struct pipe_inode_info *pipe, size_t len,
375 struct kvec *vec, __vec[PIPE_DEF_BUFFERS];
378 unsigned int nr_pages;
380 size_t offset, base, copied = 0;
384 if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
388 * Try to keep page boundaries matching to source pagecache ones -
389 * it probably won't be much help, but...
391 offset = *ppos & ~PAGE_MASK;
393 iov_iter_pipe(&to, READ, pipe, len + offset);
395 res = iov_iter_get_pages_alloc(&to, &pages, len + offset, &base);
399 nr_pages = DIV_ROUND_UP(res + base, PAGE_SIZE);
402 if (nr_pages > PIPE_DEF_BUFFERS) {
403 vec = kmalloc_array(nr_pages, sizeof(struct kvec), GFP_KERNEL);
404 if (unlikely(!vec)) {
410 mask = pipe->ring_size - 1;
411 pipe->bufs[to.head & mask].offset = offset;
412 pipe->bufs[to.head & mask].len -= offset;
414 for (i = 0; i < nr_pages; i++) {
415 size_t this_len = min_t(size_t, len, PAGE_SIZE - offset);
416 vec[i].iov_base = page_address(pages[i]) + offset;
417 vec[i].iov_len = this_len;
422 res = kernel_readv(in, vec, nr_pages, *ppos);
431 for (i = 0; i < nr_pages; i++)
434 iov_iter_advance(&to, copied); /* truncates and discards */
439 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
440 * using sendpage(). Return the number of bytes sent.
442 static int pipe_to_sendpage(struct pipe_inode_info *pipe,
443 struct pipe_buffer *buf, struct splice_desc *sd)
445 struct file *file = sd->u.file;
446 loff_t pos = sd->pos;
449 if (!likely(file->f_op->sendpage))
452 more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
454 if (sd->len < sd->total_len &&
455 pipe_occupancy(pipe->head, pipe->tail) > 1)
456 more |= MSG_SENDPAGE_NOTLAST;
458 return file->f_op->sendpage(file, buf->page, buf->offset,
459 sd->len, &pos, more);
462 static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
465 if (waitqueue_active(&pipe->wait))
466 wake_up_interruptible(&pipe->wait);
467 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
471 * splice_from_pipe_feed - feed available data from a pipe to a file
472 * @pipe: pipe to splice from
473 * @sd: information to @actor
474 * @actor: handler that splices the data
477 * This function loops over the pipe and calls @actor to do the
478 * actual moving of a single struct pipe_buffer to the desired
479 * destination. It returns when there's no more buffers left in
480 * the pipe or if the requested number of bytes (@sd->total_len)
481 * have been copied. It returns a positive number (one) if the
482 * pipe needs to be filled with more data, zero if the required
483 * number of bytes have been copied and -errno on error.
485 * This, together with splice_from_pipe_{begin,end,next}, may be
486 * used to implement the functionality of __splice_from_pipe() when
487 * locking is required around copying the pipe buffers to the
490 static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
493 unsigned int head = pipe->head;
494 unsigned int tail = pipe->tail;
495 unsigned int mask = pipe->ring_size - 1;
498 while (!pipe_empty(tail, head)) {
499 struct pipe_buffer *buf = &pipe->bufs[tail & mask];
502 if (sd->len > sd->total_len)
503 sd->len = sd->total_len;
505 ret = pipe_buf_confirm(pipe, buf);
512 ret = actor(pipe, buf, sd);
519 sd->num_spliced += ret;
522 sd->total_len -= ret;
525 pipe_buf_release(pipe, buf);
529 sd->need_wakeup = true;
540 * splice_from_pipe_next - wait for some data to splice from
541 * @pipe: pipe to splice from
542 * @sd: information about the splice operation
545 * This function will wait for some data and return a positive
546 * value (one) if pipe buffers are available. It will return zero
547 * or -errno if no more data needs to be spliced.
549 static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
552 * Check for signal early to make process killable when there are
553 * always buffers available
555 if (signal_pending(current))
558 while (pipe_empty(pipe->head, pipe->tail)) {
562 if (!pipe->waiting_writers && sd->num_spliced)
565 if (sd->flags & SPLICE_F_NONBLOCK)
568 if (signal_pending(current))
571 if (sd->need_wakeup) {
572 wakeup_pipe_writers(pipe);
573 sd->need_wakeup = false;
583 * splice_from_pipe_begin - start splicing from pipe
584 * @sd: information about the splice operation
587 * This function should be called before a loop containing
588 * splice_from_pipe_next() and splice_from_pipe_feed() to
589 * initialize the necessary fields of @sd.
591 static void splice_from_pipe_begin(struct splice_desc *sd)
594 sd->need_wakeup = false;
598 * splice_from_pipe_end - finish splicing from pipe
599 * @pipe: pipe to splice from
600 * @sd: information about the splice operation
603 * This function will wake up pipe writers if necessary. It should
604 * be called after a loop containing splice_from_pipe_next() and
605 * splice_from_pipe_feed().
607 static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
610 wakeup_pipe_writers(pipe);
614 * __splice_from_pipe - splice data from a pipe to given actor
615 * @pipe: pipe to splice from
616 * @sd: information to @actor
617 * @actor: handler that splices the data
620 * This function does little more than loop over the pipe and call
621 * @actor to do the actual moving of a single struct pipe_buffer to
622 * the desired destination. See pipe_to_file, pipe_to_sendpage, or
626 ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
631 splice_from_pipe_begin(sd);
634 ret = splice_from_pipe_next(pipe, sd);
636 ret = splice_from_pipe_feed(pipe, sd, actor);
638 splice_from_pipe_end(pipe, sd);
640 return sd->num_spliced ? sd->num_spliced : ret;
642 EXPORT_SYMBOL(__splice_from_pipe);
645 * splice_from_pipe - splice data from a pipe to a file
646 * @pipe: pipe to splice from
647 * @out: file to splice to
648 * @ppos: position in @out
649 * @len: how many bytes to splice
650 * @flags: splice modifier flags
651 * @actor: handler that splices the data
654 * See __splice_from_pipe. This function locks the pipe inode,
655 * otherwise it's identical to __splice_from_pipe().
658 ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
659 loff_t *ppos, size_t len, unsigned int flags,
663 struct splice_desc sd = {
671 ret = __splice_from_pipe(pipe, &sd, actor);
678 * iter_file_splice_write - splice data from a pipe to a file
680 * @out: file to write to
681 * @ppos: position in @out
682 * @len: number of bytes to splice
683 * @flags: splice modifier flags
686 * Will either move or copy pages (determined by @flags options) from
687 * the given pipe inode to the given file.
688 * This one is ->write_iter-based.
692 iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
693 loff_t *ppos, size_t len, unsigned int flags)
695 struct splice_desc sd = {
701 int nbufs = pipe->max_usage;
702 struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
706 if (unlikely(!array))
711 splice_from_pipe_begin(&sd);
712 while (sd.total_len) {
713 struct iov_iter from;
714 unsigned int head = pipe->head;
715 unsigned int tail = pipe->tail;
716 unsigned int mask = pipe->ring_size - 1;
720 ret = splice_from_pipe_next(pipe, &sd);
724 if (unlikely(nbufs < pipe->max_usage)) {
726 nbufs = pipe->max_usage;
727 array = kcalloc(nbufs, sizeof(struct bio_vec),
735 /* build the vector */
737 for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++, n++) {
738 struct pipe_buffer *buf = &pipe->bufs[tail & mask];
739 size_t this_len = buf->len;
744 ret = pipe_buf_confirm(pipe, buf);
751 array[n].bv_page = buf->page;
752 array[n].bv_len = this_len;
753 array[n].bv_offset = buf->offset;
757 iov_iter_bvec(&from, WRITE, array, n, sd.total_len - left);
758 ret = vfs_iter_write(out, &from, &sd.pos, 0);
762 sd.num_spliced += ret;
766 /* dismiss the fully eaten buffers, adjust the partial one */
769 struct pipe_buffer *buf = &pipe->bufs[tail & mask];
770 if (ret >= buf->len) {
773 pipe_buf_release(pipe, buf);
777 sd.need_wakeup = true;
787 splice_from_pipe_end(pipe, &sd);
792 ret = sd.num_spliced;
797 EXPORT_SYMBOL(iter_file_splice_write);
799 static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
800 struct splice_desc *sd)
804 loff_t tmp = sd->pos;
806 data = kmap(buf->page);
807 ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp);
813 static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
814 struct file *out, loff_t *ppos,
815 size_t len, unsigned int flags)
819 ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
827 * generic_splice_sendpage - splice data from a pipe to a socket
828 * @pipe: pipe to splice from
829 * @out: socket to write to
830 * @ppos: position in @out
831 * @len: number of bytes to splice
832 * @flags: splice modifier flags
835 * Will send @len bytes from the pipe to a network socket. No data copying
839 ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
840 loff_t *ppos, size_t len, unsigned int flags)
842 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
845 EXPORT_SYMBOL(generic_splice_sendpage);
848 * Attempt to initiate a splice from pipe to file.
850 static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
851 loff_t *ppos, size_t len, unsigned int flags)
853 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
854 loff_t *, size_t, unsigned int);
856 if (out->f_op->splice_write)
857 splice_write = out->f_op->splice_write;
859 splice_write = default_file_splice_write;
861 return splice_write(pipe, out, ppos, len, flags);
865 * Attempt to initiate a splice from a file to a pipe.
867 static long do_splice_to(struct file *in, loff_t *ppos,
868 struct pipe_inode_info *pipe, size_t len,
871 ssize_t (*splice_read)(struct file *, loff_t *,
872 struct pipe_inode_info *, size_t, unsigned int);
875 if (unlikely(!(in->f_mode & FMODE_READ)))
878 ret = rw_verify_area(READ, in, ppos, len);
879 if (unlikely(ret < 0))
882 if (unlikely(len > MAX_RW_COUNT))
885 if (in->f_op->splice_read)
886 splice_read = in->f_op->splice_read;
888 splice_read = default_file_splice_read;
890 return splice_read(in, ppos, pipe, len, flags);
894 * splice_direct_to_actor - splices data directly between two non-pipes
895 * @in: file to splice from
896 * @sd: actor information on where to splice to
897 * @actor: handles the data splicing
900 * This is a special case helper to splice directly between two
901 * points, without requiring an explicit pipe. Internally an allocated
902 * pipe is cached in the process, and reused during the lifetime of
906 ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
907 splice_direct_actor *actor)
909 struct pipe_inode_info *pipe;
916 * We require the input being a regular file, as we don't want to
917 * randomly drop data for eg socket -> socket splicing. Use the
918 * piped splicing for that!
920 i_mode = file_inode(in)->i_mode;
921 if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
925 * neither in nor out is a pipe, setup an internal pipe attached to
926 * 'out' and transfer the wanted data from 'in' to 'out' through that
928 pipe = current->splice_pipe;
929 if (unlikely(!pipe)) {
930 pipe = alloc_pipe_info();
935 * We don't have an immediate reader, but we'll read the stuff
936 * out of the pipe right after the splice_to_pipe(). So set
937 * PIPE_READERS appropriately.
941 current->splice_pipe = pipe;
953 * Don't block on output, we have to drain the direct pipe.
955 sd->flags &= ~SPLICE_F_NONBLOCK;
956 more = sd->flags & SPLICE_F_MORE;
958 WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail));
961 unsigned int p_space;
963 loff_t pos = sd->pos, prev_pos = pos;
965 /* Don't try to read more the pipe has space for. */
966 p_space = pipe->max_usage -
967 pipe_occupancy(pipe->head, pipe->tail);
968 read_len = min_t(size_t, len, p_space << PAGE_SHIFT);
969 ret = do_splice_to(in, &pos, pipe, read_len, flags);
970 if (unlikely(ret <= 0))
974 sd->total_len = read_len;
977 * If more data is pending, set SPLICE_F_MORE
978 * If this is the last data and SPLICE_F_MORE was not set
979 * initially, clears it.
982 sd->flags |= SPLICE_F_MORE;
984 sd->flags &= ~SPLICE_F_MORE;
986 * NOTE: nonblocking mode only applies to the input. We
987 * must not do the output in nonblocking mode as then we
988 * could get stuck data in the internal pipe:
990 ret = actor(pipe, sd);
991 if (unlikely(ret <= 0)) {
1000 if (ret < read_len) {
1001 sd->pos = prev_pos + ret;
1007 pipe->tail = pipe->head = 0;
1013 * If we did an incomplete transfer we must release
1014 * the pipe buffers in question:
1016 for (i = 0; i < pipe->ring_size; i++) {
1017 struct pipe_buffer *buf = &pipe->bufs[i];
1020 pipe_buf_release(pipe, buf);
1028 EXPORT_SYMBOL(splice_direct_to_actor);
1030 static int direct_splice_actor(struct pipe_inode_info *pipe,
1031 struct splice_desc *sd)
1033 struct file *file = sd->u.file;
1035 return do_splice_from(pipe, file, sd->opos, sd->total_len,
1040 * do_splice_direct - splices data directly between two files
1041 * @in: file to splice from
1042 * @ppos: input file offset
1043 * @out: file to splice to
1044 * @opos: output file offset
1045 * @len: number of bytes to splice
1046 * @flags: splice modifier flags
1049 * For use by do_sendfile(). splice can easily emulate sendfile, but
1050 * doing it in the application would incur an extra system call
1051 * (splice in + splice out, as compared to just sendfile()). So this helper
1052 * can splice directly through a process-private pipe.
1055 long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
1056 loff_t *opos, size_t len, unsigned int flags)
1058 struct splice_desc sd = {
1068 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1071 if (unlikely(out->f_flags & O_APPEND))
1074 ret = rw_verify_area(WRITE, out, opos, len);
1075 if (unlikely(ret < 0))
1078 ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
1084 EXPORT_SYMBOL(do_splice_direct);
1086 static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
1089 if (unlikely(!pipe->readers)) {
1090 send_sig(SIGPIPE, current, 0);
1093 if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
1095 if (flags & SPLICE_F_NONBLOCK)
1097 if (signal_pending(current))
1098 return -ERESTARTSYS;
1099 pipe->waiting_writers++;
1101 pipe->waiting_writers--;
1105 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1106 struct pipe_inode_info *opipe,
1107 size_t len, unsigned int flags);
1110 * Determine where to splice to/from.
1112 static long do_splice(struct file *in, loff_t __user *off_in,
1113 struct file *out, loff_t __user *off_out,
1114 size_t len, unsigned int flags)
1116 struct pipe_inode_info *ipipe;
1117 struct pipe_inode_info *opipe;
1121 ipipe = get_pipe_info(in);
1122 opipe = get_pipe_info(out);
1124 if (ipipe && opipe) {
1125 if (off_in || off_out)
1128 if (!(in->f_mode & FMODE_READ))
1131 if (!(out->f_mode & FMODE_WRITE))
1134 /* Splicing to self would be fun, but... */
1138 if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1139 flags |= SPLICE_F_NONBLOCK;
1141 return splice_pipe_to_pipe(ipipe, opipe, len, flags);
1148 if (!(out->f_mode & FMODE_PWRITE))
1150 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1153 offset = out->f_pos;
1156 if (unlikely(!(out->f_mode & FMODE_WRITE)))
1159 if (unlikely(out->f_flags & O_APPEND))
1162 ret = rw_verify_area(WRITE, out, &offset, len);
1163 if (unlikely(ret < 0))
1166 if (in->f_flags & O_NONBLOCK)
1167 flags |= SPLICE_F_NONBLOCK;
1169 file_start_write(out);
1170 ret = do_splice_from(ipipe, out, &offset, len, flags);
1171 file_end_write(out);
1174 out->f_pos = offset;
1175 else if (copy_to_user(off_out, &offset, sizeof(loff_t)))
1185 if (!(in->f_mode & FMODE_PREAD))
1187 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1193 if (out->f_flags & O_NONBLOCK)
1194 flags |= SPLICE_F_NONBLOCK;
1197 ret = wait_for_space(opipe, flags);
1199 unsigned int p_space;
1201 /* Don't try to read more the pipe has space for. */
1202 p_space = opipe->max_usage - pipe_occupancy(opipe->head, opipe->tail);
1203 len = min_t(size_t, len, p_space << PAGE_SHIFT);
1205 ret = do_splice_to(in, &offset, opipe, len, flags);
1209 wakeup_pipe_readers(opipe);
1212 else if (copy_to_user(off_in, &offset, sizeof(loff_t)))
1221 static int iter_to_pipe(struct iov_iter *from,
1222 struct pipe_inode_info *pipe,
1225 struct pipe_buffer buf = {
1226 .ops = &user_page_pipe_buf_ops,
1231 bool failed = false;
1233 while (iov_iter_count(from) && !failed) {
1234 struct page *pages[16];
1239 copied = iov_iter_get_pages(from, pages, ~0UL, 16, &start);
1245 for (n = 0; copied; n++, start = 0) {
1246 int size = min_t(int, copied, PAGE_SIZE - start);
1248 buf.page = pages[n];
1251 ret = add_to_pipe(pipe, &buf);
1252 if (unlikely(ret < 0)) {
1255 iov_iter_advance(from, ret);
1264 return total ? total : ret;
1267 static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1268 struct splice_desc *sd)
1270 int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
1271 return n == sd->len ? n : -EFAULT;
1275 * For lack of a better implementation, implement vmsplice() to userspace
1276 * as a simple copy of the pipes pages to the user iov.
1278 static long vmsplice_to_user(struct file *file, struct iov_iter *iter,
1281 struct pipe_inode_info *pipe = get_pipe_info(file);
1282 struct splice_desc sd = {
1283 .total_len = iov_iter_count(iter),
1294 ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
1302 * vmsplice splices a user address range into a pipe. It can be thought of
1303 * as splice-from-memory, where the regular splice is splice-from-file (or
1304 * to file). In both cases the output is a pipe, naturally.
1306 static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter,
1309 struct pipe_inode_info *pipe;
1311 unsigned buf_flag = 0;
1313 if (flags & SPLICE_F_GIFT)
1314 buf_flag = PIPE_BUF_FLAG_GIFT;
1316 pipe = get_pipe_info(file);
1321 ret = wait_for_space(pipe, flags);
1323 ret = iter_to_pipe(iter, pipe, buf_flag);
1326 wakeup_pipe_readers(pipe);
1330 static int vmsplice_type(struct fd f, int *type)
1334 if (f.file->f_mode & FMODE_WRITE) {
1336 } else if (f.file->f_mode & FMODE_READ) {
1346 * Note that vmsplice only really supports true splicing _from_ user memory
1347 * to a pipe, not the other way around. Splicing from user memory is a simple
1348 * operation that can be supported without any funky alignment restrictions
1349 * or nasty vm tricks. We simply map in the user memory and fill them into
1350 * a pipe. The reverse isn't quite as easy, though. There are two possible
1351 * solutions for that:
1353 * - memcpy() the data internally, at which point we might as well just
1354 * do a regular read() on the buffer anyway.
1355 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1356 * has restriction limitations on both ends of the pipe).
1358 * Currently we punt and implement it as a normal copy, see pipe_to_user().
1361 static long do_vmsplice(struct file *f, struct iov_iter *iter, unsigned int flags)
1363 if (unlikely(flags & ~SPLICE_F_ALL))
1366 if (!iov_iter_count(iter))
1369 if (iov_iter_rw(iter) == WRITE)
1370 return vmsplice_to_pipe(f, iter, flags);
1372 return vmsplice_to_user(f, iter, flags);
1375 SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov,
1376 unsigned long, nr_segs, unsigned int, flags)
1378 struct iovec iovstack[UIO_FASTIOV];
1379 struct iovec *iov = iovstack;
1380 struct iov_iter iter;
1386 error = vmsplice_type(f, &type);
1390 error = import_iovec(type, uiov, nr_segs,
1391 ARRAY_SIZE(iovstack), &iov, &iter);
1393 error = do_vmsplice(f.file, &iter, flags);
1400 #ifdef CONFIG_COMPAT
1401 COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32,
1402 unsigned int, nr_segs, unsigned int, flags)
1404 struct iovec iovstack[UIO_FASTIOV];
1405 struct iovec *iov = iovstack;
1406 struct iov_iter iter;
1412 error = vmsplice_type(f, &type);
1416 error = compat_import_iovec(type, iov32, nr_segs,
1417 ARRAY_SIZE(iovstack), &iov, &iter);
1419 error = do_vmsplice(f.file, &iter, flags);
1427 SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1428 int, fd_out, loff_t __user *, off_out,
1429 size_t, len, unsigned int, flags)
1437 if (unlikely(flags & ~SPLICE_F_ALL))
1443 if (in.file->f_mode & FMODE_READ) {
1444 out = fdget(fd_out);
1446 if (out.file->f_mode & FMODE_WRITE)
1447 error = do_splice(in.file, off_in,
1459 * Make sure there's data to read. Wait for input if we can, otherwise
1460 * return an appropriate error.
1462 static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1467 * Check the pipe occupancy without the inode lock first. This function
1468 * is speculative anyways, so missing one is ok.
1470 if (!pipe_empty(pipe->head, pipe->tail))
1476 while (pipe_empty(pipe->head, pipe->tail)) {
1477 if (signal_pending(current)) {
1483 if (!pipe->waiting_writers) {
1484 if (flags & SPLICE_F_NONBLOCK) {
1497 * Make sure there's writeable room. Wait for room if we can, otherwise
1498 * return an appropriate error.
1500 static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1505 * Check pipe occupancy without the inode lock first. This function
1506 * is speculative anyways, so missing one is ok.
1508 if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
1514 while (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) {
1515 if (!pipe->readers) {
1516 send_sig(SIGPIPE, current, 0);
1520 if (flags & SPLICE_F_NONBLOCK) {
1524 if (signal_pending(current)) {
1528 pipe->waiting_writers++;
1530 pipe->waiting_writers--;
1538 * Splice contents of ipipe to opipe.
1540 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1541 struct pipe_inode_info *opipe,
1542 size_t len, unsigned int flags)
1544 struct pipe_buffer *ibuf, *obuf;
1545 unsigned int i_head, o_head;
1546 unsigned int i_tail, o_tail;
1547 unsigned int i_mask, o_mask;
1549 bool input_wakeup = false;
1553 ret = ipipe_prep(ipipe, flags);
1557 ret = opipe_prep(opipe, flags);
1562 * Potential ABBA deadlock, work around it by ordering lock
1563 * grabbing by pipe info address. Otherwise two different processes
1564 * could deadlock (one doing tee from A -> B, the other from B -> A).
1566 pipe_double_lock(ipipe, opipe);
1568 i_tail = ipipe->tail;
1569 i_mask = ipipe->ring_size - 1;
1570 o_head = opipe->head;
1571 o_mask = opipe->ring_size - 1;
1576 if (!opipe->readers) {
1577 send_sig(SIGPIPE, current, 0);
1583 i_head = ipipe->head;
1584 o_tail = opipe->tail;
1586 if (pipe_empty(i_head, i_tail) && !ipipe->writers)
1590 * Cannot make any progress, because either the input
1591 * pipe is empty or the output pipe is full.
1593 if (pipe_empty(i_head, i_tail) ||
1594 pipe_full(o_head, o_tail, opipe->max_usage)) {
1595 /* Already processed some buffers, break */
1599 if (flags & SPLICE_F_NONBLOCK) {
1605 * We raced with another reader/writer and haven't
1606 * managed to process any buffers. A zero return
1607 * value means EOF, so retry instead.
1614 ibuf = &ipipe->bufs[i_tail & i_mask];
1615 obuf = &opipe->bufs[o_head & o_mask];
1617 if (len >= ibuf->len) {
1619 * Simply move the whole buffer from ipipe to opipe
1624 ipipe->tail = i_tail;
1625 input_wakeup = true;
1628 opipe->head = o_head;
1631 * Get a reference to this pipe buffer,
1632 * so we can copy the contents over.
1634 if (!pipe_buf_get(ipipe, ibuf)) {
1642 * Don't inherit the gift flag, we need to
1643 * prevent multiple steals of this page.
1645 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1647 pipe_buf_mark_unmergeable(obuf);
1650 ibuf->offset += len;
1654 opipe->head = o_head;
1664 * If we put data in the output pipe, wakeup any potential readers.
1667 wakeup_pipe_readers(opipe);
1670 wakeup_pipe_writers(ipipe);
1676 * Link contents of ipipe to opipe.
1678 static int link_pipe(struct pipe_inode_info *ipipe,
1679 struct pipe_inode_info *opipe,
1680 size_t len, unsigned int flags)
1682 struct pipe_buffer *ibuf, *obuf;
1683 unsigned int i_head, o_head;
1684 unsigned int i_tail, o_tail;
1685 unsigned int i_mask, o_mask;
1689 * Potential ABBA deadlock, work around it by ordering lock
1690 * grabbing by pipe info address. Otherwise two different processes
1691 * could deadlock (one doing tee from A -> B, the other from B -> A).
1693 pipe_double_lock(ipipe, opipe);
1695 i_tail = ipipe->tail;
1696 i_mask = ipipe->ring_size - 1;
1697 o_head = opipe->head;
1698 o_mask = opipe->ring_size - 1;
1701 if (!opipe->readers) {
1702 send_sig(SIGPIPE, current, 0);
1708 i_head = ipipe->head;
1709 o_tail = opipe->tail;
1712 * If we have iterated all input buffers or run out of
1713 * output room, break.
1715 if (pipe_empty(i_head, i_tail) ||
1716 pipe_full(o_head, o_tail, opipe->max_usage))
1719 ibuf = &ipipe->bufs[i_tail & i_mask];
1720 obuf = &opipe->bufs[o_head & o_mask];
1723 * Get a reference to this pipe buffer,
1724 * so we can copy the contents over.
1726 if (!pipe_buf_get(ipipe, ibuf)) {
1735 * Don't inherit the gift flag, we need to
1736 * prevent multiple steals of this page.
1738 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1740 pipe_buf_mark_unmergeable(obuf);
1742 if (obuf->len > len)
1748 opipe->head = o_head;
1753 * return EAGAIN if we have the potential of some data in the
1754 * future, otherwise just return 0
1756 if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
1763 * If we put data in the output pipe, wakeup any potential readers.
1766 wakeup_pipe_readers(opipe);
1772 * This is a tee(1) implementation that works on pipes. It doesn't copy
1773 * any data, it simply references the 'in' pages on the 'out' pipe.
1774 * The 'flags' used are the SPLICE_F_* variants, currently the only
1775 * applicable one is SPLICE_F_NONBLOCK.
1777 static long do_tee(struct file *in, struct file *out, size_t len,
1780 struct pipe_inode_info *ipipe = get_pipe_info(in);
1781 struct pipe_inode_info *opipe = get_pipe_info(out);
1785 * Duplicate the contents of ipipe to opipe without actually
1788 if (ipipe && opipe && ipipe != opipe) {
1789 if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1790 flags |= SPLICE_F_NONBLOCK;
1793 * Keep going, unless we encounter an error. The ipipe/opipe
1794 * ordering doesn't really matter.
1796 ret = ipipe_prep(ipipe, flags);
1798 ret = opipe_prep(opipe, flags);
1800 ret = link_pipe(ipipe, opipe, len, flags);
1807 SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
1812 if (unlikely(flags & ~SPLICE_F_ALL))
1821 if (in.file->f_mode & FMODE_READ) {
1822 struct fd out = fdget(fdout);
1824 if (out.file->f_mode & FMODE_WRITE)
1825 error = do_tee(in.file, out.file,