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 folio *folio = page_folio(buf->page);
50 struct address_space *mapping;
54 mapping = folio_mapping(folio);
56 WARN_ON(!folio_test_uptodate(folio));
59 * At least for ext2 with nobh option, we need to wait on
60 * writeback completing on this folio, since we'll remove it
61 * from the pagecache. Otherwise truncate wont wait on the
62 * folio, allowing the disk blocks to be reused by someone else
63 * before we actually wrote our data to them. fs corruption
66 folio_wait_writeback(folio);
68 if (folio_has_private(folio) &&
69 !filemap_release_folio(folio, GFP_KERNEL))
73 * If we succeeded in removing the mapping, set LRU flag
76 if (remove_mapping(mapping, folio)) {
77 buf->flags |= PIPE_BUF_FLAG_LRU;
83 * Raced with truncate or failed to remove folio from current
84 * address space, unlock and return failure.
91 static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
92 struct pipe_buffer *buf)
95 buf->flags &= ~PIPE_BUF_FLAG_LRU;
99 * Check whether the contents of buf is OK to access. Since the content
100 * is a page cache page, IO may be in flight.
102 static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
103 struct pipe_buffer *buf)
105 struct page *page = buf->page;
108 if (!PageUptodate(page)) {
112 * Page got truncated/unhashed. This will cause a 0-byte
113 * splice, if this is the first page.
115 if (!page->mapping) {
121 * Uh oh, read-error from disk.
123 if (!PageUptodate(page)) {
129 * Page is ok afterall, we are done.
140 const struct pipe_buf_operations page_cache_pipe_buf_ops = {
141 .confirm = page_cache_pipe_buf_confirm,
142 .release = page_cache_pipe_buf_release,
143 .try_steal = page_cache_pipe_buf_try_steal,
144 .get = generic_pipe_buf_get,
147 static bool user_page_pipe_buf_try_steal(struct pipe_inode_info *pipe,
148 struct pipe_buffer *buf)
150 if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
153 buf->flags |= PIPE_BUF_FLAG_LRU;
154 return generic_pipe_buf_try_steal(pipe, buf);
157 static const struct pipe_buf_operations user_page_pipe_buf_ops = {
158 .release = page_cache_pipe_buf_release,
159 .try_steal = user_page_pipe_buf_try_steal,
160 .get = generic_pipe_buf_get,
163 static void wakeup_pipe_readers(struct pipe_inode_info *pipe)
166 if (waitqueue_active(&pipe->rd_wait))
167 wake_up_interruptible(&pipe->rd_wait);
168 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
172 * splice_to_pipe - fill passed data into a pipe
173 * @pipe: pipe to fill
177 * @spd contains a map of pages and len/offset tuples, along with
178 * the struct pipe_buf_operations associated with these pages. This
179 * function will link that data to the pipe.
182 ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
183 struct splice_pipe_desc *spd)
185 unsigned int spd_pages = spd->nr_pages;
186 unsigned int tail = pipe->tail;
187 unsigned int head = pipe->head;
188 unsigned int mask = pipe->ring_size - 1;
189 int ret = 0, page_nr = 0;
194 if (unlikely(!pipe->readers)) {
195 send_sig(SIGPIPE, current, 0);
200 while (!pipe_full(head, tail, pipe->max_usage)) {
201 struct pipe_buffer *buf = &pipe->bufs[head & mask];
203 buf->page = spd->pages[page_nr];
204 buf->offset = spd->partial[page_nr].offset;
205 buf->len = spd->partial[page_nr].len;
206 buf->private = spd->partial[page_nr].private;
215 if (!--spd->nr_pages)
223 while (page_nr < spd_pages)
224 spd->spd_release(spd, page_nr++);
228 EXPORT_SYMBOL_GPL(splice_to_pipe);
230 ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
232 unsigned int head = pipe->head;
233 unsigned int tail = pipe->tail;
234 unsigned int mask = pipe->ring_size - 1;
237 if (unlikely(!pipe->readers)) {
238 send_sig(SIGPIPE, current, 0);
240 } else if (pipe_full(head, tail, pipe->max_usage)) {
243 pipe->bufs[head & mask] = *buf;
244 pipe->head = head + 1;
247 pipe_buf_release(pipe, buf);
250 EXPORT_SYMBOL(add_to_pipe);
253 * Check if we need to grow the arrays holding pages and partial page
256 int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
258 unsigned int max_usage = READ_ONCE(pipe->max_usage);
260 spd->nr_pages_max = max_usage;
261 if (max_usage <= PIPE_DEF_BUFFERS)
264 spd->pages = kmalloc_array(max_usage, sizeof(struct page *), GFP_KERNEL);
265 spd->partial = kmalloc_array(max_usage, sizeof(struct partial_page),
268 if (spd->pages && spd->partial)
276 void splice_shrink_spd(struct splice_pipe_desc *spd)
278 if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
286 * generic_file_splice_read - splice data from file to a pipe
287 * @in: file to splice from
288 * @ppos: position in @in
289 * @pipe: pipe to splice to
290 * @len: number of bytes to splice
291 * @flags: splice modifier flags
294 * Will read pages from given file and fill them into a pipe. Can be
295 * used as long as it has more or less sane ->read_iter().
298 ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
299 struct pipe_inode_info *pipe, size_t len,
306 iov_iter_pipe(&to, READ, pipe, len);
307 init_sync_kiocb(&kiocb, in);
308 kiocb.ki_pos = *ppos;
309 ret = call_read_iter(in, &kiocb, &to);
311 *ppos = kiocb.ki_pos;
313 } else if (ret < 0) {
314 /* free what was emitted */
315 pipe_discard_from(pipe, to.start_head);
317 * callers of ->splice_read() expect -EAGAIN on
318 * "can't put anything in there", rather than -EFAULT.
326 EXPORT_SYMBOL(generic_file_splice_read);
328 const struct pipe_buf_operations default_pipe_buf_ops = {
329 .release = generic_pipe_buf_release,
330 .try_steal = generic_pipe_buf_try_steal,
331 .get = generic_pipe_buf_get,
334 /* Pipe buffer operations for a socket and similar. */
335 const struct pipe_buf_operations nosteal_pipe_buf_ops = {
336 .release = generic_pipe_buf_release,
337 .get = generic_pipe_buf_get,
339 EXPORT_SYMBOL(nosteal_pipe_buf_ops);
342 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
343 * using sendpage(). Return the number of bytes sent.
345 static int pipe_to_sendpage(struct pipe_inode_info *pipe,
346 struct pipe_buffer *buf, struct splice_desc *sd)
348 struct file *file = sd->u.file;
349 loff_t pos = sd->pos;
352 if (!likely(file->f_op->sendpage))
355 more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
357 if (sd->len < sd->total_len &&
358 pipe_occupancy(pipe->head, pipe->tail) > 1)
359 more |= MSG_SENDPAGE_NOTLAST;
361 return file->f_op->sendpage(file, buf->page, buf->offset,
362 sd->len, &pos, more);
365 static void wakeup_pipe_writers(struct pipe_inode_info *pipe)
368 if (waitqueue_active(&pipe->wr_wait))
369 wake_up_interruptible(&pipe->wr_wait);
370 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
374 * splice_from_pipe_feed - feed available data from a pipe to a file
375 * @pipe: pipe to splice from
376 * @sd: information to @actor
377 * @actor: handler that splices the data
380 * This function loops over the pipe and calls @actor to do the
381 * actual moving of a single struct pipe_buffer to the desired
382 * destination. It returns when there's no more buffers left in
383 * the pipe or if the requested number of bytes (@sd->total_len)
384 * have been copied. It returns a positive number (one) if the
385 * pipe needs to be filled with more data, zero if the required
386 * number of bytes have been copied and -errno on error.
388 * This, together with splice_from_pipe_{begin,end,next}, may be
389 * used to implement the functionality of __splice_from_pipe() when
390 * locking is required around copying the pipe buffers to the
393 static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
396 unsigned int head = pipe->head;
397 unsigned int tail = pipe->tail;
398 unsigned int mask = pipe->ring_size - 1;
401 while (!pipe_empty(head, tail)) {
402 struct pipe_buffer *buf = &pipe->bufs[tail & mask];
405 if (sd->len > sd->total_len)
406 sd->len = sd->total_len;
408 ret = pipe_buf_confirm(pipe, buf);
415 ret = actor(pipe, buf, sd);
422 sd->num_spliced += ret;
425 sd->total_len -= ret;
428 pipe_buf_release(pipe, buf);
432 sd->need_wakeup = true;
442 /* We know we have a pipe buffer, but maybe it's empty? */
443 static inline bool eat_empty_buffer(struct pipe_inode_info *pipe)
445 unsigned int tail = pipe->tail;
446 unsigned int mask = pipe->ring_size - 1;
447 struct pipe_buffer *buf = &pipe->bufs[tail & mask];
449 if (unlikely(!buf->len)) {
450 pipe_buf_release(pipe, buf);
459 * splice_from_pipe_next - wait for some data to splice from
460 * @pipe: pipe to splice from
461 * @sd: information about the splice operation
464 * This function will wait for some data and return a positive
465 * value (one) if pipe buffers are available. It will return zero
466 * or -errno if no more data needs to be spliced.
468 static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
471 * Check for signal early to make process killable when there are
472 * always buffers available
474 if (signal_pending(current))
478 while (pipe_empty(pipe->head, pipe->tail)) {
485 if (sd->flags & SPLICE_F_NONBLOCK)
488 if (signal_pending(current))
491 if (sd->need_wakeup) {
492 wakeup_pipe_writers(pipe);
493 sd->need_wakeup = false;
496 pipe_wait_readable(pipe);
499 if (eat_empty_buffer(pipe))
506 * splice_from_pipe_begin - start splicing from pipe
507 * @sd: information about the splice operation
510 * This function should be called before a loop containing
511 * splice_from_pipe_next() and splice_from_pipe_feed() to
512 * initialize the necessary fields of @sd.
514 static void splice_from_pipe_begin(struct splice_desc *sd)
517 sd->need_wakeup = false;
521 * splice_from_pipe_end - finish splicing from pipe
522 * @pipe: pipe to splice from
523 * @sd: information about the splice operation
526 * This function will wake up pipe writers if necessary. It should
527 * be called after a loop containing splice_from_pipe_next() and
528 * splice_from_pipe_feed().
530 static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
533 wakeup_pipe_writers(pipe);
537 * __splice_from_pipe - splice data from a pipe to given actor
538 * @pipe: pipe to splice from
539 * @sd: information to @actor
540 * @actor: handler that splices the data
543 * This function does little more than loop over the pipe and call
544 * @actor to do the actual moving of a single struct pipe_buffer to
545 * the desired destination. See pipe_to_file, pipe_to_sendpage, or
549 ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
554 splice_from_pipe_begin(sd);
557 ret = splice_from_pipe_next(pipe, sd);
559 ret = splice_from_pipe_feed(pipe, sd, actor);
561 splice_from_pipe_end(pipe, sd);
563 return sd->num_spliced ? sd->num_spliced : ret;
565 EXPORT_SYMBOL(__splice_from_pipe);
568 * splice_from_pipe - splice data from a pipe to a file
569 * @pipe: pipe to splice from
570 * @out: file to splice to
571 * @ppos: position in @out
572 * @len: how many bytes to splice
573 * @flags: splice modifier flags
574 * @actor: handler that splices the data
577 * See __splice_from_pipe. This function locks the pipe inode,
578 * otherwise it's identical to __splice_from_pipe().
581 ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
582 loff_t *ppos, size_t len, unsigned int flags,
586 struct splice_desc sd = {
594 ret = __splice_from_pipe(pipe, &sd, actor);
601 * iter_file_splice_write - splice data from a pipe to a file
603 * @out: file to write to
604 * @ppos: position in @out
605 * @len: number of bytes to splice
606 * @flags: splice modifier flags
609 * Will either move or copy pages (determined by @flags options) from
610 * the given pipe inode to the given file.
611 * This one is ->write_iter-based.
615 iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
616 loff_t *ppos, size_t len, unsigned int flags)
618 struct splice_desc sd = {
624 int nbufs = pipe->max_usage;
625 struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
629 if (unlikely(!array))
634 splice_from_pipe_begin(&sd);
635 while (sd.total_len) {
636 struct iov_iter from;
637 unsigned int head, tail, mask;
641 ret = splice_from_pipe_next(pipe, &sd);
645 if (unlikely(nbufs < pipe->max_usage)) {
647 nbufs = pipe->max_usage;
648 array = kcalloc(nbufs, sizeof(struct bio_vec),
658 mask = pipe->ring_size - 1;
660 /* build the vector */
662 for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++) {
663 struct pipe_buffer *buf = &pipe->bufs[tail & mask];
664 size_t this_len = buf->len;
666 /* zero-length bvecs are not supported, skip them */
669 this_len = min(this_len, left);
671 ret = pipe_buf_confirm(pipe, buf);
678 array[n].bv_page = buf->page;
679 array[n].bv_len = this_len;
680 array[n].bv_offset = buf->offset;
685 iov_iter_bvec(&from, WRITE, array, n, sd.total_len - left);
686 ret = vfs_iter_write(out, &from, &sd.pos, 0);
690 sd.num_spliced += ret;
694 /* dismiss the fully eaten buffers, adjust the partial one */
697 struct pipe_buffer *buf = &pipe->bufs[tail & mask];
698 if (ret >= buf->len) {
701 pipe_buf_release(pipe, buf);
705 sd.need_wakeup = true;
715 splice_from_pipe_end(pipe, &sd);
720 ret = sd.num_spliced;
725 EXPORT_SYMBOL(iter_file_splice_write);
728 * generic_splice_sendpage - splice data from a pipe to a socket
729 * @pipe: pipe to splice from
730 * @out: socket to write to
731 * @ppos: position in @out
732 * @len: number of bytes to splice
733 * @flags: splice modifier flags
736 * Will send @len bytes from the pipe to a network socket. No data copying
740 ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
741 loff_t *ppos, size_t len, unsigned int flags)
743 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
746 EXPORT_SYMBOL(generic_splice_sendpage);
748 static int warn_unsupported(struct file *file, const char *op)
750 pr_debug_ratelimited(
751 "splice %s not supported for file %pD4 (pid: %d comm: %.20s)\n",
752 op, file, current->pid, current->comm);
757 * Attempt to initiate a splice from pipe to file.
759 static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
760 loff_t *ppos, size_t len, unsigned int flags)
762 if (unlikely(!out->f_op->splice_write))
763 return warn_unsupported(out, "write");
764 return out->f_op->splice_write(pipe, out, ppos, len, flags);
768 * Attempt to initiate a splice from a file to a pipe.
770 static long do_splice_to(struct file *in, loff_t *ppos,
771 struct pipe_inode_info *pipe, size_t len,
774 unsigned int p_space;
777 if (unlikely(!(in->f_mode & FMODE_READ)))
780 /* Don't try to read more the pipe has space for. */
781 p_space = pipe->max_usage - pipe_occupancy(pipe->head, pipe->tail);
782 len = min_t(size_t, len, p_space << PAGE_SHIFT);
784 ret = rw_verify_area(READ, in, ppos, len);
785 if (unlikely(ret < 0))
788 if (unlikely(len > MAX_RW_COUNT))
791 if (unlikely(!in->f_op->splice_read))
792 return warn_unsupported(in, "read");
793 return in->f_op->splice_read(in, ppos, pipe, len, flags);
797 * splice_direct_to_actor - splices data directly between two non-pipes
798 * @in: file to splice from
799 * @sd: actor information on where to splice to
800 * @actor: handles the data splicing
803 * This is a special case helper to splice directly between two
804 * points, without requiring an explicit pipe. Internally an allocated
805 * pipe is cached in the process, and reused during the lifetime of
809 ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
810 splice_direct_actor *actor)
812 struct pipe_inode_info *pipe;
818 * We require the input to be seekable, as we don't want to randomly
819 * drop data for eg socket -> socket splicing. Use the piped splicing
822 if (unlikely(!(in->f_mode & FMODE_LSEEK)))
826 * neither in nor out is a pipe, setup an internal pipe attached to
827 * 'out' and transfer the wanted data from 'in' to 'out' through that
829 pipe = current->splice_pipe;
830 if (unlikely(!pipe)) {
831 pipe = alloc_pipe_info();
836 * We don't have an immediate reader, but we'll read the stuff
837 * out of the pipe right after the splice_to_pipe(). So set
838 * PIPE_READERS appropriately.
842 current->splice_pipe = pipe;
854 * Don't block on output, we have to drain the direct pipe.
856 sd->flags &= ~SPLICE_F_NONBLOCK;
857 more = sd->flags & SPLICE_F_MORE;
859 WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail));
863 loff_t pos = sd->pos, prev_pos = pos;
865 ret = do_splice_to(in, &pos, pipe, len, flags);
866 if (unlikely(ret <= 0))
870 sd->total_len = read_len;
873 * If more data is pending, set SPLICE_F_MORE
874 * If this is the last data and SPLICE_F_MORE was not set
875 * initially, clears it.
878 sd->flags |= SPLICE_F_MORE;
880 sd->flags &= ~SPLICE_F_MORE;
882 * NOTE: nonblocking mode only applies to the input. We
883 * must not do the output in nonblocking mode as then we
884 * could get stuck data in the internal pipe:
886 ret = actor(pipe, sd);
887 if (unlikely(ret <= 0)) {
896 if (ret < read_len) {
897 sd->pos = prev_pos + ret;
903 pipe->tail = pipe->head = 0;
909 * If we did an incomplete transfer we must release
910 * the pipe buffers in question:
912 for (i = 0; i < pipe->ring_size; i++) {
913 struct pipe_buffer *buf = &pipe->bufs[i];
916 pipe_buf_release(pipe, buf);
924 EXPORT_SYMBOL(splice_direct_to_actor);
926 static int direct_splice_actor(struct pipe_inode_info *pipe,
927 struct splice_desc *sd)
929 struct file *file = sd->u.file;
931 return do_splice_from(pipe, file, sd->opos, sd->total_len,
936 * do_splice_direct - splices data directly between two files
937 * @in: file to splice from
938 * @ppos: input file offset
939 * @out: file to splice to
940 * @opos: output file offset
941 * @len: number of bytes to splice
942 * @flags: splice modifier flags
945 * For use by do_sendfile(). splice can easily emulate sendfile, but
946 * doing it in the application would incur an extra system call
947 * (splice in + splice out, as compared to just sendfile()). So this helper
948 * can splice directly through a process-private pipe.
951 long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
952 loff_t *opos, size_t len, unsigned int flags)
954 struct splice_desc sd = {
964 if (unlikely(!(out->f_mode & FMODE_WRITE)))
967 if (unlikely(out->f_flags & O_APPEND))
970 ret = rw_verify_area(WRITE, out, opos, len);
971 if (unlikely(ret < 0))
974 ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
980 EXPORT_SYMBOL(do_splice_direct);
982 static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags)
985 if (unlikely(!pipe->readers)) {
986 send_sig(SIGPIPE, current, 0);
989 if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
991 if (flags & SPLICE_F_NONBLOCK)
993 if (signal_pending(current))
995 pipe_wait_writable(pipe);
999 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1000 struct pipe_inode_info *opipe,
1001 size_t len, unsigned int flags);
1003 long splice_file_to_pipe(struct file *in,
1004 struct pipe_inode_info *opipe,
1006 size_t len, unsigned int flags)
1011 ret = wait_for_space(opipe, flags);
1013 ret = do_splice_to(in, offset, opipe, len, flags);
1016 wakeup_pipe_readers(opipe);
1021 * Determine where to splice to/from.
1023 long do_splice(struct file *in, loff_t *off_in, struct file *out,
1024 loff_t *off_out, size_t len, unsigned int flags)
1026 struct pipe_inode_info *ipipe;
1027 struct pipe_inode_info *opipe;
1031 if (unlikely(!(in->f_mode & FMODE_READ) ||
1032 !(out->f_mode & FMODE_WRITE)))
1035 ipipe = get_pipe_info(in, true);
1036 opipe = get_pipe_info(out, true);
1038 if (ipipe && opipe) {
1039 if (off_in || off_out)
1042 /* Splicing to self would be fun, but... */
1046 if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1047 flags |= SPLICE_F_NONBLOCK;
1049 return splice_pipe_to_pipe(ipipe, opipe, len, flags);
1056 if (!(out->f_mode & FMODE_PWRITE))
1060 offset = out->f_pos;
1063 if (unlikely(out->f_flags & O_APPEND))
1066 ret = rw_verify_area(WRITE, out, &offset, len);
1067 if (unlikely(ret < 0))
1070 if (in->f_flags & O_NONBLOCK)
1071 flags |= SPLICE_F_NONBLOCK;
1073 file_start_write(out);
1074 ret = do_splice_from(ipipe, out, &offset, len, flags);
1075 file_end_write(out);
1078 out->f_pos = offset;
1089 if (!(in->f_mode & FMODE_PREAD))
1096 if (out->f_flags & O_NONBLOCK)
1097 flags |= SPLICE_F_NONBLOCK;
1099 ret = splice_file_to_pipe(in, opipe, &offset, len, flags);
1111 static long __do_splice(struct file *in, loff_t __user *off_in,
1112 struct file *out, loff_t __user *off_out,
1113 size_t len, unsigned int flags)
1115 struct pipe_inode_info *ipipe;
1116 struct pipe_inode_info *opipe;
1117 loff_t offset, *__off_in = NULL, *__off_out = NULL;
1120 ipipe = get_pipe_info(in, true);
1121 opipe = get_pipe_info(out, true);
1123 if (ipipe && off_in)
1125 if (opipe && off_out)
1129 if (copy_from_user(&offset, off_out, sizeof(loff_t)))
1131 __off_out = &offset;
1134 if (copy_from_user(&offset, off_in, sizeof(loff_t)))
1139 ret = do_splice(in, __off_in, out, __off_out, len, flags);
1143 if (__off_out && copy_to_user(off_out, __off_out, sizeof(loff_t)))
1145 if (__off_in && copy_to_user(off_in, __off_in, sizeof(loff_t)))
1151 static int iter_to_pipe(struct iov_iter *from,
1152 struct pipe_inode_info *pipe,
1155 struct pipe_buffer buf = {
1156 .ops = &user_page_pipe_buf_ops,
1162 while (iov_iter_count(from)) {
1163 struct page *pages[16];
1168 left = iov_iter_get_pages2(from, pages, ~0UL, 16, &start);
1174 n = DIV_ROUND_UP(left + start, PAGE_SIZE);
1175 for (i = 0; i < n; i++) {
1176 int size = min_t(int, left, PAGE_SIZE - start);
1178 buf.page = pages[i];
1181 ret = add_to_pipe(pipe, &buf);
1182 if (unlikely(ret < 0)) {
1183 iov_iter_revert(from, left);
1184 // this one got dropped by add_to_pipe()
1195 return total ? total : ret;
1198 static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
1199 struct splice_desc *sd)
1201 int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
1202 return n == sd->len ? n : -EFAULT;
1206 * For lack of a better implementation, implement vmsplice() to userspace
1207 * as a simple copy of the pipes pages to the user iov.
1209 static long vmsplice_to_user(struct file *file, struct iov_iter *iter,
1212 struct pipe_inode_info *pipe = get_pipe_info(file, true);
1213 struct splice_desc sd = {
1214 .total_len = iov_iter_count(iter),
1225 ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
1233 * vmsplice splices a user address range into a pipe. It can be thought of
1234 * as splice-from-memory, where the regular splice is splice-from-file (or
1235 * to file). In both cases the output is a pipe, naturally.
1237 static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter,
1240 struct pipe_inode_info *pipe;
1242 unsigned buf_flag = 0;
1244 if (flags & SPLICE_F_GIFT)
1245 buf_flag = PIPE_BUF_FLAG_GIFT;
1247 pipe = get_pipe_info(file, true);
1252 ret = wait_for_space(pipe, flags);
1254 ret = iter_to_pipe(iter, pipe, buf_flag);
1257 wakeup_pipe_readers(pipe);
1261 static int vmsplice_type(struct fd f, int *type)
1265 if (f.file->f_mode & FMODE_WRITE) {
1267 } else if (f.file->f_mode & FMODE_READ) {
1277 * Note that vmsplice only really supports true splicing _from_ user memory
1278 * to a pipe, not the other way around. Splicing from user memory is a simple
1279 * operation that can be supported without any funky alignment restrictions
1280 * or nasty vm tricks. We simply map in the user memory and fill them into
1281 * a pipe. The reverse isn't quite as easy, though. There are two possible
1282 * solutions for that:
1284 * - memcpy() the data internally, at which point we might as well just
1285 * do a regular read() on the buffer anyway.
1286 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1287 * has restriction limitations on both ends of the pipe).
1289 * Currently we punt and implement it as a normal copy, see pipe_to_user().
1292 SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov,
1293 unsigned long, nr_segs, unsigned int, flags)
1295 struct iovec iovstack[UIO_FASTIOV];
1296 struct iovec *iov = iovstack;
1297 struct iov_iter iter;
1302 if (unlikely(flags & ~SPLICE_F_ALL))
1306 error = vmsplice_type(f, &type);
1310 error = import_iovec(type, uiov, nr_segs,
1311 ARRAY_SIZE(iovstack), &iov, &iter);
1315 if (!iov_iter_count(&iter))
1317 else if (iov_iter_rw(&iter) == WRITE)
1318 error = vmsplice_to_pipe(f.file, &iter, flags);
1320 error = vmsplice_to_user(f.file, &iter, flags);
1328 SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
1329 int, fd_out, loff_t __user *, off_out,
1330 size_t, len, unsigned int, flags)
1338 if (unlikely(flags & ~SPLICE_F_ALL))
1344 out = fdget(fd_out);
1346 error = __do_splice(in.file, off_in, out.file, off_out,
1356 * Make sure there's data to read. Wait for input if we can, otherwise
1357 * return an appropriate error.
1359 static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1364 * Check the pipe occupancy without the inode lock first. This function
1365 * is speculative anyways, so missing one is ok.
1367 if (!pipe_empty(pipe->head, pipe->tail))
1373 while (pipe_empty(pipe->head, pipe->tail)) {
1374 if (signal_pending(current)) {
1380 if (flags & SPLICE_F_NONBLOCK) {
1384 pipe_wait_readable(pipe);
1392 * Make sure there's writeable room. Wait for room if we can, otherwise
1393 * return an appropriate error.
1395 static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
1400 * Check pipe occupancy without the inode lock first. This function
1401 * is speculative anyways, so missing one is ok.
1403 if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
1409 while (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) {
1410 if (!pipe->readers) {
1411 send_sig(SIGPIPE, current, 0);
1415 if (flags & SPLICE_F_NONBLOCK) {
1419 if (signal_pending(current)) {
1423 pipe_wait_writable(pipe);
1431 * Splice contents of ipipe to opipe.
1433 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
1434 struct pipe_inode_info *opipe,
1435 size_t len, unsigned int flags)
1437 struct pipe_buffer *ibuf, *obuf;
1438 unsigned int i_head, o_head;
1439 unsigned int i_tail, o_tail;
1440 unsigned int i_mask, o_mask;
1442 bool input_wakeup = false;
1446 ret = ipipe_prep(ipipe, flags);
1450 ret = opipe_prep(opipe, flags);
1455 * Potential ABBA deadlock, work around it by ordering lock
1456 * grabbing by pipe info address. Otherwise two different processes
1457 * could deadlock (one doing tee from A -> B, the other from B -> A).
1459 pipe_double_lock(ipipe, opipe);
1461 i_tail = ipipe->tail;
1462 i_mask = ipipe->ring_size - 1;
1463 o_head = opipe->head;
1464 o_mask = opipe->ring_size - 1;
1469 if (!opipe->readers) {
1470 send_sig(SIGPIPE, current, 0);
1476 i_head = ipipe->head;
1477 o_tail = opipe->tail;
1479 if (pipe_empty(i_head, i_tail) && !ipipe->writers)
1483 * Cannot make any progress, because either the input
1484 * pipe is empty or the output pipe is full.
1486 if (pipe_empty(i_head, i_tail) ||
1487 pipe_full(o_head, o_tail, opipe->max_usage)) {
1488 /* Already processed some buffers, break */
1492 if (flags & SPLICE_F_NONBLOCK) {
1498 * We raced with another reader/writer and haven't
1499 * managed to process any buffers. A zero return
1500 * value means EOF, so retry instead.
1507 ibuf = &ipipe->bufs[i_tail & i_mask];
1508 obuf = &opipe->bufs[o_head & o_mask];
1510 if (len >= ibuf->len) {
1512 * Simply move the whole buffer from ipipe to opipe
1517 ipipe->tail = i_tail;
1518 input_wakeup = true;
1521 opipe->head = o_head;
1524 * Get a reference to this pipe buffer,
1525 * so we can copy the contents over.
1527 if (!pipe_buf_get(ipipe, ibuf)) {
1535 * Don't inherit the gift and merge flags, we need to
1536 * prevent multiple steals of this page.
1538 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1539 obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
1542 ibuf->offset += len;
1546 opipe->head = o_head;
1556 * If we put data in the output pipe, wakeup any potential readers.
1559 wakeup_pipe_readers(opipe);
1562 wakeup_pipe_writers(ipipe);
1568 * Link contents of ipipe to opipe.
1570 static int link_pipe(struct pipe_inode_info *ipipe,
1571 struct pipe_inode_info *opipe,
1572 size_t len, unsigned int flags)
1574 struct pipe_buffer *ibuf, *obuf;
1575 unsigned int i_head, o_head;
1576 unsigned int i_tail, o_tail;
1577 unsigned int i_mask, o_mask;
1581 * Potential ABBA deadlock, work around it by ordering lock
1582 * grabbing by pipe info address. Otherwise two different processes
1583 * could deadlock (one doing tee from A -> B, the other from B -> A).
1585 pipe_double_lock(ipipe, opipe);
1587 i_tail = ipipe->tail;
1588 i_mask = ipipe->ring_size - 1;
1589 o_head = opipe->head;
1590 o_mask = opipe->ring_size - 1;
1593 if (!opipe->readers) {
1594 send_sig(SIGPIPE, current, 0);
1600 i_head = ipipe->head;
1601 o_tail = opipe->tail;
1604 * If we have iterated all input buffers or run out of
1605 * output room, break.
1607 if (pipe_empty(i_head, i_tail) ||
1608 pipe_full(o_head, o_tail, opipe->max_usage))
1611 ibuf = &ipipe->bufs[i_tail & i_mask];
1612 obuf = &opipe->bufs[o_head & o_mask];
1615 * Get a reference to this pipe buffer,
1616 * so we can copy the contents over.
1618 if (!pipe_buf_get(ipipe, ibuf)) {
1627 * Don't inherit the gift and merge flag, we need to prevent
1628 * multiple steals of this page.
1630 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1631 obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
1633 if (obuf->len > len)
1639 opipe->head = o_head;
1647 * If we put data in the output pipe, wakeup any potential readers.
1650 wakeup_pipe_readers(opipe);
1656 * This is a tee(1) implementation that works on pipes. It doesn't copy
1657 * any data, it simply references the 'in' pages on the 'out' pipe.
1658 * The 'flags' used are the SPLICE_F_* variants, currently the only
1659 * applicable one is SPLICE_F_NONBLOCK.
1661 long do_tee(struct file *in, struct file *out, size_t len, unsigned int flags)
1663 struct pipe_inode_info *ipipe = get_pipe_info(in, true);
1664 struct pipe_inode_info *opipe = get_pipe_info(out, true);
1667 if (unlikely(!(in->f_mode & FMODE_READ) ||
1668 !(out->f_mode & FMODE_WRITE)))
1672 * Duplicate the contents of ipipe to opipe without actually
1675 if (ipipe && opipe && ipipe != opipe) {
1676 if ((in->f_flags | out->f_flags) & O_NONBLOCK)
1677 flags |= SPLICE_F_NONBLOCK;
1680 * Keep going, unless we encounter an error. The ipipe/opipe
1681 * ordering doesn't really matter.
1683 ret = ipipe_prep(ipipe, flags);
1685 ret = opipe_prep(opipe, flags);
1687 ret = link_pipe(ipipe, opipe, len, flags);
1694 SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
1699 if (unlikely(flags & ~SPLICE_F_ALL))
1710 error = do_tee(in.file, out.file, len, flags);