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/sched/signal.h>
41 * Attempt to steal a page from a pipe buffer. This should perhaps go into
42 * a vm helper function, it's already simplified quite a bit by the
43 * addition of remove_mapping(). If success is returned, the caller may
44 * attempt to reuse this page for another destination.
46 static bool page_cache_pipe_buf_try_steal(struct pipe_inode_info *pipe,
47 struct pipe_buffer *buf)
49 struct page *page = buf->page;
50 struct folio *folio = page_folio(page);
51 struct address_space *mapping;
55 mapping = folio_mapping(folio);
57 WARN_ON(!folio_test_uptodate(folio));
60 * At least for ext2 with nobh option, we need to wait on
61 * writeback completing on this folio, since we'll remove it
62 * from the pagecache. Otherwise truncate wont wait on the
63 * folio, allowing the disk blocks to be reused by someone else
64 * before we actually wrote our data to them. fs corruption
67 folio_wait_writeback(folio);
69 if (folio_has_private(folio) &&
70 !filemap_release_folio(folio, 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 folio 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 .try_steal = page_cache_pipe_buf_try_steal,
145 .get = generic_pipe_buf_get,
148 static bool user_page_pipe_buf_try_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_try_steal(pipe, buf);
158 static const struct pipe_buf_operations user_page_pipe_buf_ops = {
159 .release = page_cache_pipe_buf_release,
160 .try_steal = user_page_pipe_buf_try_steal,
161 .get = generic_pipe_buf_get,
164 static void wakeup_pipe_readers(struct pipe_inode_info *pipe)
167 if (waitqueue_active(&pipe->rd_wait))
168 wake_up_interruptible(&pipe->rd_wait);
169 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
173 * splice_to_pipe - fill passed data into a pipe
174 * @pipe: pipe to fill
178 * @spd contains a map of pages and len/offset tuples, along with
179 * the struct pipe_buf_operations associated with these pages. This
180 * function will link that data to the pipe.
183 ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
184 struct splice_pipe_desc *spd)
186 unsigned int spd_pages = spd->nr_pages;
187 unsigned int tail = pipe->tail;
188 unsigned int head = pipe->head;
189 unsigned int mask = pipe->ring_size - 1;
190 int ret = 0, page_nr = 0;
195 if (unlikely(!pipe->readers)) {
196 send_sig(SIGPIPE, current, 0);
201 while (!pipe_full(head, tail, pipe->max_usage)) {
202 struct pipe_buffer *buf = &pipe->bufs[head & mask];
204 buf->page = spd->pages[page_nr];
205 buf->offset = spd->partial[page_nr].offset;
206 buf->len = spd->partial[page_nr].len;
207 buf->private = spd->partial[page_nr].private;
216 if (!--spd->nr_pages)
224 while (page_nr < spd_pages)
225 spd->spd_release(spd, page_nr++);
229 EXPORT_SYMBOL_GPL(splice_to_pipe);
231 ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
233 unsigned int head = pipe->head;
234 unsigned int tail = pipe->tail;
235 unsigned int mask = pipe->ring_size - 1;
238 if (unlikely(!pipe->readers)) {
239 send_sig(SIGPIPE, current, 0);
241 } else if (pipe_full(head, tail, pipe->max_usage)) {
244 pipe->bufs[head & mask] = *buf;
245 pipe->head = head + 1;
248 pipe_buf_release(pipe, buf);
251 EXPORT_SYMBOL(add_to_pipe);
254 * Check if we need to grow the arrays holding pages and partial page
257 int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
259 unsigned int max_usage = READ_ONCE(pipe->max_usage);
261 spd->nr_pages_max = max_usage;
262 if (max_usage <= PIPE_DEF_BUFFERS)
265 spd->pages = kmalloc_array(max_usage, sizeof(struct page *), GFP_KERNEL);
266 spd->partial = kmalloc_array(max_usage, sizeof(struct partial_page),
269 if (spd->pages && spd->partial)
277 void splice_shrink_spd(struct splice_pipe_desc *spd)
279 if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
287 * generic_file_splice_read - splice data from file to a pipe
288 * @in: file to splice from
289 * @ppos: position in @in
290 * @pipe: pipe to splice to
291 * @len: number of bytes to splice
292 * @flags: splice modifier flags
295 * Will read pages from given file and fill them into a pipe. Can be
296 * used as long as it has more or less sane ->read_iter().
299 ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
300 struct pipe_inode_info *pipe, size_t len,
308 iov_iter_pipe(&to, READ, pipe, len);
310 init_sync_kiocb(&kiocb, in);
311 kiocb.ki_pos = *ppos;
312 ret = call_read_iter(in, &kiocb, &to);
314 *ppos = kiocb.ki_pos;
316 } else if (ret < 0) {
319 iov_iter_advance(&to, 0); /* to free what was emitted */
321 * callers of ->splice_read() expect -EAGAIN on
322 * "can't put anything in there", rather than -EFAULT.
330 EXPORT_SYMBOL(generic_file_splice_read);
332 const struct pipe_buf_operations default_pipe_buf_ops = {
333 .release = generic_pipe_buf_release,
334 .try_steal = generic_pipe_buf_try_steal,
335 .get = generic_pipe_buf_get,
338 /* Pipe buffer operations for a socket and similar. */
339 const struct pipe_buf_operations nosteal_pipe_buf_ops = {
340 .release = generic_pipe_buf_release,
341 .get = generic_pipe_buf_get,
343 EXPORT_SYMBOL(nosteal_pipe_buf_ops);
346 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
347 * using sendpage(). Return the number of bytes sent.
349 static int pipe_to_sendpage(struct pipe_inode_info *pipe,
350 struct pipe_buffer *buf, struct splice_desc *sd)
352 struct file *file = sd->u.file;
353 loff_t pos = sd->pos;
356 if (!likely(file->f_op->sendpage))
359 more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
361 if (sd->len < sd->total_len &&
362 pipe_occupancy(pipe->head, pipe->tail) > 1)
363 more |= MSG_SENDPAGE_NOTLAST;
365 return file->f_op->sendpage(file, buf->page, buf->offset,
366 sd->len, &pos, more);
369 static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
372 if (waitqueue_active(&pipe->wr_wait))
373 wake_up_interruptible(&pipe->wr_wait);
374 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
378 * splice_from_pipe_feed - feed available data from a pipe to a file
379 * @pipe: pipe to splice from
380 * @sd: information to @actor
381 * @actor: handler that splices the data
384 * This function loops over the pipe and calls @actor to do the
385 * actual moving of a single struct pipe_buffer to the desired
386 * destination. It returns when there's no more buffers left in
387 * the pipe or if the requested number of bytes (@sd->total_len)
388 * have been copied. It returns a positive number (one) if the
389 * pipe needs to be filled with more data, zero if the required
390 * number of bytes have been copied and -errno on error.
392 * This, together with splice_from_pipe_{begin,end,next}, may be
393 * used to implement the functionality of __splice_from_pipe() when
394 * locking is required around copying the pipe buffers to the
397 static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
400 unsigned int head = pipe->head;
401 unsigned int tail = pipe->tail;
402 unsigned int mask = pipe->ring_size - 1;
405 while (!pipe_empty(head, tail)) {
406 struct pipe_buffer *buf = &pipe->bufs[tail & mask];
409 if (sd->len > sd->total_len)
410 sd->len = sd->total_len;
412 ret = pipe_buf_confirm(pipe, buf);
419 ret = actor(pipe, buf, sd);
426 sd->num_spliced += ret;
429 sd->total_len -= ret;
432 pipe_buf_release(pipe, buf);
436 sd->need_wakeup = true;
446 /* We know we have a pipe buffer, but maybe it's empty? */
447 static inline bool eat_empty_buffer(struct pipe_inode_info *pipe)
449 unsigned int tail = pipe->tail;
450 unsigned int mask = pipe->ring_size - 1;
451 struct pipe_buffer *buf = &pipe->bufs[tail & mask];
453 if (unlikely(!buf->len)) {
454 pipe_buf_release(pipe, buf);
463 * splice_from_pipe_next - wait for some data to splice from
464 * @pipe: pipe to splice from
465 * @sd: information about the splice operation
468 * This function will wait for some data and return a positive
469 * value (one) if pipe buffers are available. It will return zero
470 * or -errno if no more data needs to be spliced.
472 static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
475 * Check for signal early to make process killable when there are
476 * always buffers available
478 if (signal_pending(current))
482 while (pipe_empty(pipe->head, pipe->tail)) {
489 if (sd->flags & SPLICE_F_NONBLOCK)
492 if (signal_pending(current))
495 if (sd->need_wakeup) {
496 wakeup_pipe_writers(pipe);
497 sd->need_wakeup = false;
500 pipe_wait_readable(pipe);
503 if (eat_empty_buffer(pipe))
510 * splice_from_pipe_begin - start splicing from pipe
511 * @sd: information about the splice operation
514 * This function should be called before a loop containing
515 * splice_from_pipe_next() and splice_from_pipe_feed() to
516 * initialize the necessary fields of @sd.
518 static void splice_from_pipe_begin(struct splice_desc *sd)
521 sd->need_wakeup = false;
525 * splice_from_pipe_end - finish splicing from pipe
526 * @pipe: pipe to splice from
527 * @sd: information about the splice operation
530 * This function will wake up pipe writers if necessary. It should
531 * be called after a loop containing splice_from_pipe_next() and
532 * splice_from_pipe_feed().
534 static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
537 wakeup_pipe_writers(pipe);
541 * __splice_from_pipe - splice data from a pipe to given actor
542 * @pipe: pipe to splice from
543 * @sd: information to @actor
544 * @actor: handler that splices the data
547 * This function does little more than loop over the pipe and call
548 * @actor to do the actual moving of a single struct pipe_buffer to
549 * the desired destination. See pipe_to_file, pipe_to_sendpage, or
553 ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
558 splice_from_pipe_begin(sd);
561 ret = splice_from_pipe_next(pipe, sd);
563 ret = splice_from_pipe_feed(pipe, sd, actor);
565 splice_from_pipe_end(pipe, sd);
567 return sd->num_spliced ? sd->num_spliced : ret;
569 EXPORT_SYMBOL(__splice_from_pipe);
572 * splice_from_pipe - splice data from a pipe to a file
573 * @pipe: pipe to splice from
574 * @out: file to splice to
575 * @ppos: position in @out
576 * @len: how many bytes to splice
577 * @flags: splice modifier flags
578 * @actor: handler that splices the data
581 * See __splice_from_pipe. This function locks the pipe inode,
582 * otherwise it's identical to __splice_from_pipe().
585 ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
586 loff_t *ppos, size_t len, unsigned int flags,
590 struct splice_desc sd = {
598 ret = __splice_from_pipe(pipe, &sd, actor);
605 * iter_file_splice_write - splice data from a pipe to a file
607 * @out: file to write to
608 * @ppos: position in @out
609 * @len: number of bytes to splice
610 * @flags: splice modifier flags
613 * Will either move or copy pages (determined by @flags options) from
614 * the given pipe inode to the given file.
615 * This one is ->write_iter-based.
619 iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
620 loff_t *ppos, size_t len, unsigned int flags)
622 struct splice_desc sd = {
628 int nbufs = pipe->max_usage;
629 struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
633 if (unlikely(!array))
638 splice_from_pipe_begin(&sd);
639 while (sd.total_len) {
640 struct iov_iter from;
641 unsigned int head, tail, mask;
645 ret = splice_from_pipe_next(pipe, &sd);
649 if (unlikely(nbufs < pipe->max_usage)) {
651 nbufs = pipe->max_usage;
652 array = kcalloc(nbufs, sizeof(struct bio_vec),
662 mask = pipe->ring_size - 1;
664 /* build the vector */
666 for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++) {
667 struct pipe_buffer *buf = &pipe->bufs[tail & mask];
668 size_t this_len = buf->len;
670 /* zero-length bvecs are not supported, skip them */
673 this_len = min(this_len, left);
675 ret = pipe_buf_confirm(pipe, buf);
682 array[n].bv_page = buf->page;
683 array[n].bv_len = this_len;
684 array[n].bv_offset = buf->offset;
689 iov_iter_bvec(&from, WRITE, array, n, sd.total_len - left);
690 ret = vfs_iter_write(out, &from, &sd.pos, 0);
694 sd.num_spliced += ret;
698 /* dismiss the fully eaten buffers, adjust the partial one */
701 struct pipe_buffer *buf = &pipe->bufs[tail & mask];
702 if (ret >= buf->len) {
705 pipe_buf_release(pipe, buf);
709 sd.need_wakeup = true;
719 splice_from_pipe_end(pipe, &sd);
724 ret = sd.num_spliced;
729 EXPORT_SYMBOL(iter_file_splice_write);
732 * generic_splice_sendpage - splice data from a pipe to a socket
733 * @pipe: pipe to splice from
734 * @out: socket to write to
735 * @ppos: position in @out
736 * @len: number of bytes to splice
737 * @flags: splice modifier flags
740 * Will send @len bytes from the pipe to a network socket. No data copying
744 ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
745 loff_t *ppos, size_t len, unsigned int flags)
747 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
750 EXPORT_SYMBOL(generic_splice_sendpage);
752 static int warn_unsupported(struct file *file, const char *op)
754 pr_debug_ratelimited(
755 "splice %s not supported for file %pD4 (pid: %d comm: %.20s)\n",
756 op, file, current->pid, current->comm);
761 * Attempt to initiate a splice from pipe to file.
763 static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
764 loff_t *ppos, size_t len, unsigned int flags)
766 if (unlikely(!out->f_op->splice_write))
767 return warn_unsupported(out, "write");
768 return out->f_op->splice_write(pipe, out, ppos, len, flags);
772 * Attempt to initiate a splice from a file to a pipe.
774 static long do_splice_to(struct file *in, loff_t *ppos,
775 struct pipe_inode_info *pipe, size_t len,
778 unsigned int p_space;
781 if (unlikely(!(in->f_mode & FMODE_READ)))
784 /* Don't try to read more the pipe has space for. */
785 p_space = pipe->max_usage - pipe_occupancy(pipe->head, pipe->tail);
786 len = min_t(size_t, len, p_space << PAGE_SHIFT);
788 ret = rw_verify_area(READ, in, ppos, len);
789 if (unlikely(ret < 0))
792 if (unlikely(len > MAX_RW_COUNT))
795 if (unlikely(!in->f_op->splice_read))
796 return warn_unsupported(in, "read");
797 return in->f_op->splice_read(in, ppos, pipe, len, flags);
801 * splice_direct_to_actor - splices data directly between two non-pipes
802 * @in: file to splice from
803 * @sd: actor information on where to splice to
804 * @actor: handles the data splicing
807 * This is a special case helper to splice directly between two
808 * points, without requiring an explicit pipe. Internally an allocated
809 * pipe is cached in the process, and reused during the lifetime of
813 ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
814 splice_direct_actor *actor)
816 struct pipe_inode_info *pipe;
823 * We require the input being a regular file, as we don't want to
824 * randomly drop data for eg socket -> socket splicing. Use the
825 * piped splicing for that!
827 i_mode = file_inode(in)->i_mode;
828 if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
832 * neither in nor out is a pipe, setup an internal pipe attached to
833 * 'out' and transfer the wanted data from 'in' to 'out' through that
835 pipe = current->splice_pipe;
836 if (unlikely(!pipe)) {
837 pipe = alloc_pipe_info();
842 * We don't have an immediate reader, but we'll read the stuff
843 * out of the pipe right after the splice_to_pipe(). So set
844 * PIPE_READERS appropriately.
848 current->splice_pipe = pipe;
860 * Don't block on output, we have to drain the direct pipe.
862 sd->flags &= ~SPLICE_F_NONBLOCK;
863 more = sd->flags & SPLICE_F_MORE;
865 WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail));
869 loff_t pos = sd->pos, prev_pos = pos;
871 ret = do_splice_to(in, &pos, pipe, len, flags);
872 if (unlikely(ret <= 0))
876 sd->total_len = read_len;
879 * If more data is pending, set SPLICE_F_MORE
880 * If this is the last data and SPLICE_F_MORE was not set
881 * initially, clears it.
884 sd->flags |= SPLICE_F_MORE;
886 sd->flags &= ~SPLICE_F_MORE;
888 * NOTE: nonblocking mode only applies to the input. We
889 * must not do the output in nonblocking mode as then we
890 * could get stuck data in the internal pipe:
892 ret = actor(pipe, sd);
893 if (unlikely(ret <= 0)) {
902 if (ret < read_len) {
903 sd->pos = prev_pos + ret;
909 pipe->tail = pipe->head = 0;
915 * If we did an incomplete transfer we must release
916 * the pipe buffers in question:
918 for (i = 0; i < pipe->ring_size; i++) {
919 struct pipe_buffer *buf = &pipe->bufs[i];
922 pipe_buf_release(pipe, buf);
930 EXPORT_SYMBOL(splice_direct_to_actor);
932 static int direct_splice_actor(struct pipe_inode_info *pipe,
933 struct splice_desc *sd)
935 struct file *file = sd->u.file;
937 return do_splice_from(pipe, file, sd->opos, sd->total_len,
942 * do_splice_direct - splices data directly between two files
943 * @in: file to splice from
944 * @ppos: input file offset
945 * @out: file to splice to
946 * @opos: output file offset
947 * @len: number of bytes to splice
948 * @flags: splice modifier flags
951 * For use by do_sendfile(). splice can easily emulate sendfile, but
952 * doing it in the application would incur an extra system call
953 * (splice in + splice out, as compared to just sendfile()). So this helper
954 * can splice directly through a process-private pipe.
957 long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
958 loff_t *opos, size_t len, unsigned int flags)
960 struct splice_desc sd = {
970 if (unlikely(!(out->f_mode & FMODE_WRITE)))
973 if (unlikely(out->f_flags & O_APPEND))
976 ret = rw_verify_area(WRITE, out, opos, len);
977 if (unlikely(ret < 0))
980 ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
986 EXPORT_SYMBOL(do_splice_direct);
988 static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
991 if (unlikely(!pipe->readers)) {
992 send_sig(SIGPIPE, current, 0);
995 if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
997 if (flags & SPLICE_F_NONBLOCK)
999 if (signal_pending(current))
1000 return -ERESTARTSYS;
1001 pipe_wait_writable(pipe);
1005 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1006 struct pipe_inode_info *opipe,
1007 size_t len, unsigned int flags);
1009 long splice_file_to_pipe(struct file *in,
1010 struct pipe_inode_info *opipe,
1012 size_t len, unsigned int flags)
1017 ret = wait_for_space(opipe, flags);
1019 ret = do_splice_to(in, offset, opipe, len, flags);
1022 wakeup_pipe_readers(opipe);
1027 * Determine where to splice to/from.
1029 long do_splice(struct file *in, loff_t *off_in, struct file *out,
1030 loff_t *off_out, size_t len, unsigned int flags)
1032 struct pipe_inode_info *ipipe;
1033 struct pipe_inode_info *opipe;
1037 if (unlikely(!(in->f_mode & FMODE_READ) ||
1038 !(out->f_mode & FMODE_WRITE)))
1041 ipipe = get_pipe_info(in, true);
1042 opipe = get_pipe_info(out, true);
1044 if (ipipe && opipe) {
1045 if (off_in || off_out)
1048 /* Splicing to self would be fun, but... */
1052 if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1053 flags |= SPLICE_F_NONBLOCK;
1055 return splice_pipe_to_pipe(ipipe, opipe, len, flags);
1062 if (!(out->f_mode & FMODE_PWRITE))
1066 offset = out->f_pos;
1069 if (unlikely(out->f_flags & O_APPEND))
1072 ret = rw_verify_area(WRITE, out, &offset, len);
1073 if (unlikely(ret < 0))
1076 if (in->f_flags & O_NONBLOCK)
1077 flags |= SPLICE_F_NONBLOCK;
1079 file_start_write(out);
1080 ret = do_splice_from(ipipe, out, &offset, len, flags);
1081 file_end_write(out);
1084 out->f_pos = offset;
1095 if (!(in->f_mode & FMODE_PREAD))
1102 if (out->f_flags & O_NONBLOCK)
1103 flags |= SPLICE_F_NONBLOCK;
1105 ret = splice_file_to_pipe(in, opipe, &offset, len, flags);
1117 static long __do_splice(struct file *in, loff_t __user *off_in,
1118 struct file *out, loff_t __user *off_out,
1119 size_t len, unsigned int flags)
1121 struct pipe_inode_info *ipipe;
1122 struct pipe_inode_info *opipe;
1123 loff_t offset, *__off_in = NULL, *__off_out = NULL;
1126 ipipe = get_pipe_info(in, true);
1127 opipe = get_pipe_info(out, true);
1129 if (ipipe && off_in)
1131 if (opipe && off_out)
1135 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1137 __off_out = &offset;
1140 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1145 ret = do_splice(in, __off_in, out, __off_out, len, flags);
1149 if (__off_out && copy_to_user(off_out, __off_out, sizeof(loff_t)))
1151 if (__off_in && copy_to_user(off_in, __off_in, sizeof(loff_t)))
1157 static int iter_to_pipe(struct iov_iter *from,
1158 struct pipe_inode_info *pipe,
1161 struct pipe_buffer buf = {
1162 .ops = &user_page_pipe_buf_ops,
1167 bool failed = false;
1169 while (iov_iter_count(from) && !failed) {
1170 struct page *pages[16];
1175 copied = iov_iter_get_pages(from, pages, ~0UL, 16, &start);
1181 for (n = 0; copied; n++, start = 0) {
1182 int size = min_t(int, copied, PAGE_SIZE - start);
1184 buf.page = pages[n];
1187 ret = add_to_pipe(pipe, &buf);
1188 if (unlikely(ret < 0)) {
1191 iov_iter_advance(from, ret);
1200 return total ? total : ret;
1203 static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1204 struct splice_desc *sd)
1206 int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
1207 return n == sd->len ? n : -EFAULT;
1211 * For lack of a better implementation, implement vmsplice() to userspace
1212 * as a simple copy of the pipes pages to the user iov.
1214 static long vmsplice_to_user(struct file *file, struct iov_iter *iter,
1217 struct pipe_inode_info *pipe = get_pipe_info(file, true);
1218 struct splice_desc sd = {
1219 .total_len = iov_iter_count(iter),
1230 ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
1238 * vmsplice splices a user address range into a pipe. It can be thought of
1239 * as splice-from-memory, where the regular splice is splice-from-file (or
1240 * to file). In both cases the output is a pipe, naturally.
1242 static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter,
1245 struct pipe_inode_info *pipe;
1247 unsigned buf_flag = 0;
1249 if (flags & SPLICE_F_GIFT)
1250 buf_flag = PIPE_BUF_FLAG_GIFT;
1252 pipe = get_pipe_info(file, true);
1257 ret = wait_for_space(pipe, flags);
1259 ret = iter_to_pipe(iter, pipe, buf_flag);
1262 wakeup_pipe_readers(pipe);
1266 static int vmsplice_type(struct fd f, int *type)
1270 if (f.file->f_mode & FMODE_WRITE) {
1272 } else if (f.file->f_mode & FMODE_READ) {
1282 * Note that vmsplice only really supports true splicing _from_ user memory
1283 * to a pipe, not the other way around. Splicing from user memory is a simple
1284 * operation that can be supported without any funky alignment restrictions
1285 * or nasty vm tricks. We simply map in the user memory and fill them into
1286 * a pipe. The reverse isn't quite as easy, though. There are two possible
1287 * solutions for that:
1289 * - memcpy() the data internally, at which point we might as well just
1290 * do a regular read() on the buffer anyway.
1291 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1292 * has restriction limitations on both ends of the pipe).
1294 * Currently we punt and implement it as a normal copy, see pipe_to_user().
1297 SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov,
1298 unsigned long, nr_segs, unsigned int, flags)
1300 struct iovec iovstack[UIO_FASTIOV];
1301 struct iovec *iov = iovstack;
1302 struct iov_iter iter;
1307 if (unlikely(flags & ~SPLICE_F_ALL))
1311 error = vmsplice_type(f, &type);
1315 error = import_iovec(type, uiov, nr_segs,
1316 ARRAY_SIZE(iovstack), &iov, &iter);
1320 if (!iov_iter_count(&iter))
1322 else if (iov_iter_rw(&iter) == WRITE)
1323 error = vmsplice_to_pipe(f.file, &iter, flags);
1325 error = vmsplice_to_user(f.file, &iter, flags);
1333 SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1334 int, fd_out, loff_t __user *, off_out,
1335 size_t, len, unsigned int, flags)
1343 if (unlikely(flags & ~SPLICE_F_ALL))
1349 out = fdget(fd_out);
1351 error = __do_splice(in.file, off_in, out.file, off_out,
1361 * Make sure there's data to read. Wait for input if we can, otherwise
1362 * return an appropriate error.
1364 static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1369 * Check the pipe occupancy without the inode lock first. This function
1370 * is speculative anyways, so missing one is ok.
1372 if (!pipe_empty(pipe->head, pipe->tail))
1378 while (pipe_empty(pipe->head, pipe->tail)) {
1379 if (signal_pending(current)) {
1385 if (flags & SPLICE_F_NONBLOCK) {
1389 pipe_wait_readable(pipe);
1397 * Make sure there's writeable room. Wait for room if we can, otherwise
1398 * return an appropriate error.
1400 static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1405 * Check pipe occupancy without the inode lock first. This function
1406 * is speculative anyways, so missing one is ok.
1408 if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
1414 while (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) {
1415 if (!pipe->readers) {
1416 send_sig(SIGPIPE, current, 0);
1420 if (flags & SPLICE_F_NONBLOCK) {
1424 if (signal_pending(current)) {
1428 pipe_wait_writable(pipe);
1436 * Splice contents of ipipe to opipe.
1438 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1439 struct pipe_inode_info *opipe,
1440 size_t len, unsigned int flags)
1442 struct pipe_buffer *ibuf, *obuf;
1443 unsigned int i_head, o_head;
1444 unsigned int i_tail, o_tail;
1445 unsigned int i_mask, o_mask;
1447 bool input_wakeup = false;
1451 ret = ipipe_prep(ipipe, flags);
1455 ret = opipe_prep(opipe, flags);
1460 * Potential ABBA deadlock, work around it by ordering lock
1461 * grabbing by pipe info address. Otherwise two different processes
1462 * could deadlock (one doing tee from A -> B, the other from B -> A).
1464 pipe_double_lock(ipipe, opipe);
1466 i_tail = ipipe->tail;
1467 i_mask = ipipe->ring_size - 1;
1468 o_head = opipe->head;
1469 o_mask = opipe->ring_size - 1;
1474 if (!opipe->readers) {
1475 send_sig(SIGPIPE, current, 0);
1481 i_head = ipipe->head;
1482 o_tail = opipe->tail;
1484 if (pipe_empty(i_head, i_tail) && !ipipe->writers)
1488 * Cannot make any progress, because either the input
1489 * pipe is empty or the output pipe is full.
1491 if (pipe_empty(i_head, i_tail) ||
1492 pipe_full(o_head, o_tail, opipe->max_usage)) {
1493 /* Already processed some buffers, break */
1497 if (flags & SPLICE_F_NONBLOCK) {
1503 * We raced with another reader/writer and haven't
1504 * managed to process any buffers. A zero return
1505 * value means EOF, so retry instead.
1512 ibuf = &ipipe->bufs[i_tail & i_mask];
1513 obuf = &opipe->bufs[o_head & o_mask];
1515 if (len >= ibuf->len) {
1517 * Simply move the whole buffer from ipipe to opipe
1522 ipipe->tail = i_tail;
1523 input_wakeup = true;
1526 opipe->head = o_head;
1529 * Get a reference to this pipe buffer,
1530 * so we can copy the contents over.
1532 if (!pipe_buf_get(ipipe, ibuf)) {
1540 * Don't inherit the gift and merge flags, we need to
1541 * prevent multiple steals of this page.
1543 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1544 obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
1547 ibuf->offset += len;
1551 opipe->head = o_head;
1561 * If we put data in the output pipe, wakeup any potential readers.
1564 wakeup_pipe_readers(opipe);
1567 wakeup_pipe_writers(ipipe);
1573 * Link contents of ipipe to opipe.
1575 static int link_pipe(struct pipe_inode_info *ipipe,
1576 struct pipe_inode_info *opipe,
1577 size_t len, unsigned int flags)
1579 struct pipe_buffer *ibuf, *obuf;
1580 unsigned int i_head, o_head;
1581 unsigned int i_tail, o_tail;
1582 unsigned int i_mask, o_mask;
1586 * Potential ABBA deadlock, work around it by ordering lock
1587 * grabbing by pipe info address. Otherwise two different processes
1588 * could deadlock (one doing tee from A -> B, the other from B -> A).
1590 pipe_double_lock(ipipe, opipe);
1592 i_tail = ipipe->tail;
1593 i_mask = ipipe->ring_size - 1;
1594 o_head = opipe->head;
1595 o_mask = opipe->ring_size - 1;
1598 if (!opipe->readers) {
1599 send_sig(SIGPIPE, current, 0);
1605 i_head = ipipe->head;
1606 o_tail = opipe->tail;
1609 * If we have iterated all input buffers or run out of
1610 * output room, break.
1612 if (pipe_empty(i_head, i_tail) ||
1613 pipe_full(o_head, o_tail, opipe->max_usage))
1616 ibuf = &ipipe->bufs[i_tail & i_mask];
1617 obuf = &opipe->bufs[o_head & o_mask];
1620 * Get a reference to this pipe buffer,
1621 * so we can copy the contents over.
1623 if (!pipe_buf_get(ipipe, ibuf)) {
1632 * Don't inherit the gift and merge flag, we need to prevent
1633 * multiple steals of this page.
1635 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1636 obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
1638 if (obuf->len > len)
1644 opipe->head = o_head;
1652 * If we put data in the output pipe, wakeup any potential readers.
1655 wakeup_pipe_readers(opipe);
1661 * This is a tee(1) implementation that works on pipes. It doesn't copy
1662 * any data, it simply references the 'in' pages on the 'out' pipe.
1663 * The 'flags' used are the SPLICE_F_* variants, currently the only
1664 * applicable one is SPLICE_F_NONBLOCK.
1666 long do_tee(struct file *in, struct file *out, size_t len, unsigned int flags)
1668 struct pipe_inode_info *ipipe = get_pipe_info(in, true);
1669 struct pipe_inode_info *opipe = get_pipe_info(out, true);
1672 if (unlikely(!(in->f_mode & FMODE_READ) ||
1673 !(out->f_mode & FMODE_WRITE)))
1677 * Duplicate the contents of ipipe to opipe without actually
1680 if (ipipe && opipe && ipipe != opipe) {
1681 if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1682 flags |= SPLICE_F_NONBLOCK;
1685 * Keep going, unless we encounter an error. The ipipe/opipe
1686 * ordering doesn't really matter.
1688 ret = ipipe_prep(ipipe, flags);
1690 ret = opipe_prep(opipe, flags);
1692 ret = link_pipe(ipipe, opipe, len, flags);
1699 SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
1704 if (unlikely(flags & ~SPLICE_F_ALL))
1715 error = do_tee(in.file, out.file, len, flags);