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 address_space
*mapping
;
54 mapping
= page_mapping(page
);
56 WARN_ON(!PageUptodate(page
));
59 * At least for ext2 with nobh option, we need to wait on
60 * writeback completing on this page, since we'll remove it
61 * from the pagecache. Otherwise truncate wont wait on the
62 * page, allowing the disk blocks to be reused by someone else
63 * before we actually wrote our data to them. fs corruption
66 wait_on_page_writeback(page
);
68 if (page_has_private(page
) &&
69 !try_to_release_page(page
, GFP_KERNEL
))
73 * If we succeeded in removing the mapping, set LRU flag
76 if (remove_mapping(mapping
, page
)) {
77 buf
->flags
|= PIPE_BUF_FLAG_LRU
;
83 * Raced with truncate or failed to remove page 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
,
307 iov_iter_pipe(&to
, READ
, pipe
, len
);
309 init_sync_kiocb(&kiocb
, in
);
310 kiocb
.ki_pos
= *ppos
;
311 ret
= call_read_iter(in
, &kiocb
, &to
);
313 *ppos
= kiocb
.ki_pos
;
315 } else if (ret
< 0) {
318 iov_iter_advance(&to
, 0); /* to free what was emitted */
320 * callers of ->splice_read() expect -EAGAIN on
321 * "can't put anything in there", rather than -EFAULT.
329 EXPORT_SYMBOL(generic_file_splice_read
);
331 const struct pipe_buf_operations default_pipe_buf_ops
= {
332 .release
= generic_pipe_buf_release
,
333 .try_steal
= generic_pipe_buf_try_steal
,
334 .get
= generic_pipe_buf_get
,
337 /* Pipe buffer operations for a socket and similar. */
338 const struct pipe_buf_operations nosteal_pipe_buf_ops
= {
339 .release
= generic_pipe_buf_release
,
340 .get
= generic_pipe_buf_get
,
342 EXPORT_SYMBOL(nosteal_pipe_buf_ops
);
345 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
346 * using sendpage(). Return the number of bytes sent.
348 static int pipe_to_sendpage(struct pipe_inode_info
*pipe
,
349 struct pipe_buffer
*buf
, struct splice_desc
*sd
)
351 struct file
*file
= sd
->u
.file
;
352 loff_t pos
= sd
->pos
;
355 if (!likely(file
->f_op
->sendpage
))
358 more
= (sd
->flags
& SPLICE_F_MORE
) ? MSG_MORE
: 0;
360 if (sd
->len
< sd
->total_len
&&
361 pipe_occupancy(pipe
->head
, pipe
->tail
) > 1)
362 more
|= MSG_SENDPAGE_NOTLAST
;
364 return file
->f_op
->sendpage(file
, buf
->page
, buf
->offset
,
365 sd
->len
, &pos
, more
);
368 static void wakeup_pipe_writers(struct pipe_inode_info
*pipe
)
371 if (waitqueue_active(&pipe
->wr_wait
))
372 wake_up_interruptible(&pipe
->wr_wait
);
373 kill_fasync(&pipe
->fasync_writers
, SIGIO
, POLL_OUT
);
377 * splice_from_pipe_feed - feed available data from a pipe to a file
378 * @pipe: pipe to splice from
379 * @sd: information to @actor
380 * @actor: handler that splices the data
383 * This function loops over the pipe and calls @actor to do the
384 * actual moving of a single struct pipe_buffer to the desired
385 * destination. It returns when there's no more buffers left in
386 * the pipe or if the requested number of bytes (@sd->total_len)
387 * have been copied. It returns a positive number (one) if the
388 * pipe needs to be filled with more data, zero if the required
389 * number of bytes have been copied and -errno on error.
391 * This, together with splice_from_pipe_{begin,end,next}, may be
392 * used to implement the functionality of __splice_from_pipe() when
393 * locking is required around copying the pipe buffers to the
396 static int splice_from_pipe_feed(struct pipe_inode_info
*pipe
, struct splice_desc
*sd
,
399 unsigned int head
= pipe
->head
;
400 unsigned int tail
= pipe
->tail
;
401 unsigned int mask
= pipe
->ring_size
- 1;
404 while (!pipe_empty(head
, tail
)) {
405 struct pipe_buffer
*buf
= &pipe
->bufs
[tail
& mask
];
408 if (sd
->len
> sd
->total_len
)
409 sd
->len
= sd
->total_len
;
411 ret
= pipe_buf_confirm(pipe
, buf
);
418 ret
= actor(pipe
, buf
, sd
);
425 sd
->num_spliced
+= ret
;
428 sd
->total_len
-= ret
;
431 pipe_buf_release(pipe
, buf
);
435 sd
->need_wakeup
= true;
445 /* We know we have a pipe buffer, but maybe it's empty? */
446 static inline bool eat_empty_buffer(struct pipe_inode_info
*pipe
)
448 unsigned int tail
= pipe
->tail
;
449 unsigned int mask
= pipe
->ring_size
- 1;
450 struct pipe_buffer
*buf
= &pipe
->bufs
[tail
& mask
];
452 if (unlikely(!buf
->len
)) {
453 pipe_buf_release(pipe
, buf
);
462 * splice_from_pipe_next - wait for some data to splice from
463 * @pipe: pipe to splice from
464 * @sd: information about the splice operation
467 * This function will wait for some data and return a positive
468 * value (one) if pipe buffers are available. It will return zero
469 * or -errno if no more data needs to be spliced.
471 static int splice_from_pipe_next(struct pipe_inode_info
*pipe
, struct splice_desc
*sd
)
474 * Check for signal early to make process killable when there are
475 * always buffers available
477 if (signal_pending(current
))
481 while (pipe_empty(pipe
->head
, pipe
->tail
)) {
488 if (sd
->flags
& SPLICE_F_NONBLOCK
)
491 if (signal_pending(current
))
494 if (sd
->need_wakeup
) {
495 wakeup_pipe_writers(pipe
);
496 sd
->need_wakeup
= false;
499 pipe_wait_readable(pipe
);
502 if (eat_empty_buffer(pipe
))
509 * splice_from_pipe_begin - start splicing from pipe
510 * @sd: information about the splice operation
513 * This function should be called before a loop containing
514 * splice_from_pipe_next() and splice_from_pipe_feed() to
515 * initialize the necessary fields of @sd.
517 static void splice_from_pipe_begin(struct splice_desc
*sd
)
520 sd
->need_wakeup
= false;
524 * splice_from_pipe_end - finish splicing from pipe
525 * @pipe: pipe to splice from
526 * @sd: information about the splice operation
529 * This function will wake up pipe writers if necessary. It should
530 * be called after a loop containing splice_from_pipe_next() and
531 * splice_from_pipe_feed().
533 static void splice_from_pipe_end(struct pipe_inode_info
*pipe
, struct splice_desc
*sd
)
536 wakeup_pipe_writers(pipe
);
540 * __splice_from_pipe - splice data from a pipe to given actor
541 * @pipe: pipe to splice from
542 * @sd: information to @actor
543 * @actor: handler that splices the data
546 * This function does little more than loop over the pipe and call
547 * @actor to do the actual moving of a single struct pipe_buffer to
548 * the desired destination. See pipe_to_file, pipe_to_sendpage, or
552 ssize_t
__splice_from_pipe(struct pipe_inode_info
*pipe
, struct splice_desc
*sd
,
557 splice_from_pipe_begin(sd
);
560 ret
= splice_from_pipe_next(pipe
, sd
);
562 ret
= splice_from_pipe_feed(pipe
, sd
, actor
);
564 splice_from_pipe_end(pipe
, sd
);
566 return sd
->num_spliced
? sd
->num_spliced
: ret
;
568 EXPORT_SYMBOL(__splice_from_pipe
);
571 * splice_from_pipe - splice data from a pipe to a file
572 * @pipe: pipe to splice from
573 * @out: file to splice to
574 * @ppos: position in @out
575 * @len: how many bytes to splice
576 * @flags: splice modifier flags
577 * @actor: handler that splices the data
580 * See __splice_from_pipe. This function locks the pipe inode,
581 * otherwise it's identical to __splice_from_pipe().
584 ssize_t
splice_from_pipe(struct pipe_inode_info
*pipe
, struct file
*out
,
585 loff_t
*ppos
, size_t len
, unsigned int flags
,
589 struct splice_desc sd
= {
597 ret
= __splice_from_pipe(pipe
, &sd
, actor
);
604 * iter_file_splice_write - splice data from a pipe to a file
606 * @out: file to write to
607 * @ppos: position in @out
608 * @len: number of bytes to splice
609 * @flags: splice modifier flags
612 * Will either move or copy pages (determined by @flags options) from
613 * the given pipe inode to the given file.
614 * This one is ->write_iter-based.
618 iter_file_splice_write(struct pipe_inode_info
*pipe
, struct file
*out
,
619 loff_t
*ppos
, size_t len
, unsigned int flags
)
621 struct splice_desc sd
= {
627 int nbufs
= pipe
->max_usage
;
628 struct bio_vec
*array
= kcalloc(nbufs
, sizeof(struct bio_vec
),
632 if (unlikely(!array
))
637 splice_from_pipe_begin(&sd
);
638 while (sd
.total_len
) {
639 struct iov_iter from
;
640 unsigned int head
, tail
, mask
;
644 ret
= splice_from_pipe_next(pipe
, &sd
);
648 if (unlikely(nbufs
< pipe
->max_usage
)) {
650 nbufs
= pipe
->max_usage
;
651 array
= kcalloc(nbufs
, sizeof(struct bio_vec
),
661 mask
= pipe
->ring_size
- 1;
663 /* build the vector */
665 for (n
= 0; !pipe_empty(head
, tail
) && left
&& n
< nbufs
; tail
++, n
++) {
666 struct pipe_buffer
*buf
= &pipe
->bufs
[tail
& mask
];
667 size_t this_len
= buf
->len
;
672 ret
= pipe_buf_confirm(pipe
, buf
);
679 array
[n
].bv_page
= buf
->page
;
680 array
[n
].bv_len
= this_len
;
681 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
,
776 if (unlikely(!(in
->f_mode
& FMODE_READ
)))
779 ret
= rw_verify_area(READ
, in
, ppos
, len
);
780 if (unlikely(ret
< 0))
783 if (unlikely(len
> MAX_RW_COUNT
))
786 if (unlikely(!in
->f_op
->splice_read
))
787 return warn_unsupported(in
, "read");
788 return in
->f_op
->splice_read(in
, ppos
, pipe
, len
, flags
);
792 * splice_direct_to_actor - splices data directly between two non-pipes
793 * @in: file to splice from
794 * @sd: actor information on where to splice to
795 * @actor: handles the data splicing
798 * This is a special case helper to splice directly between two
799 * points, without requiring an explicit pipe. Internally an allocated
800 * pipe is cached in the process, and reused during the lifetime of
804 ssize_t
splice_direct_to_actor(struct file
*in
, struct splice_desc
*sd
,
805 splice_direct_actor
*actor
)
807 struct pipe_inode_info
*pipe
;
814 * We require the input being a regular file, as we don't want to
815 * randomly drop data for eg socket -> socket splicing. Use the
816 * piped splicing for that!
818 i_mode
= file_inode(in
)->i_mode
;
819 if (unlikely(!S_ISREG(i_mode
) && !S_ISBLK(i_mode
)))
823 * neither in nor out is a pipe, setup an internal pipe attached to
824 * 'out' and transfer the wanted data from 'in' to 'out' through that
826 pipe
= current
->splice_pipe
;
827 if (unlikely(!pipe
)) {
828 pipe
= alloc_pipe_info();
833 * We don't have an immediate reader, but we'll read the stuff
834 * out of the pipe right after the splice_to_pipe(). So set
835 * PIPE_READERS appropriately.
839 current
->splice_pipe
= pipe
;
851 * Don't block on output, we have to drain the direct pipe.
853 sd
->flags
&= ~SPLICE_F_NONBLOCK
;
854 more
= sd
->flags
& SPLICE_F_MORE
;
856 WARN_ON_ONCE(!pipe_empty(pipe
->head
, pipe
->tail
));
859 unsigned int p_space
;
861 loff_t pos
= sd
->pos
, prev_pos
= pos
;
863 /* Don't try to read more the pipe has space for. */
864 p_space
= pipe
->max_usage
-
865 pipe_occupancy(pipe
->head
, pipe
->tail
);
866 read_len
= min_t(size_t, len
, p_space
<< PAGE_SHIFT
);
867 ret
= do_splice_to(in
, &pos
, pipe
, read_len
, flags
);
868 if (unlikely(ret
<= 0))
872 sd
->total_len
= read_len
;
875 * If more data is pending, set SPLICE_F_MORE
876 * If this is the last data and SPLICE_F_MORE was not set
877 * initially, clears it.
880 sd
->flags
|= SPLICE_F_MORE
;
882 sd
->flags
&= ~SPLICE_F_MORE
;
884 * NOTE: nonblocking mode only applies to the input. We
885 * must not do the output in nonblocking mode as then we
886 * could get stuck data in the internal pipe:
888 ret
= actor(pipe
, sd
);
889 if (unlikely(ret
<= 0)) {
898 if (ret
< read_len
) {
899 sd
->pos
= prev_pos
+ ret
;
905 pipe
->tail
= pipe
->head
= 0;
911 * If we did an incomplete transfer we must release
912 * the pipe buffers in question:
914 for (i
= 0; i
< pipe
->ring_size
; i
++) {
915 struct pipe_buffer
*buf
= &pipe
->bufs
[i
];
918 pipe_buf_release(pipe
, buf
);
926 EXPORT_SYMBOL(splice_direct_to_actor
);
928 static int direct_splice_actor(struct pipe_inode_info
*pipe
,
929 struct splice_desc
*sd
)
931 struct file
*file
= sd
->u
.file
;
933 return do_splice_from(pipe
, file
, sd
->opos
, sd
->total_len
,
938 * do_splice_direct - splices data directly between two files
939 * @in: file to splice from
940 * @ppos: input file offset
941 * @out: file to splice to
942 * @opos: output file offset
943 * @len: number of bytes to splice
944 * @flags: splice modifier flags
947 * For use by do_sendfile(). splice can easily emulate sendfile, but
948 * doing it in the application would incur an extra system call
949 * (splice in + splice out, as compared to just sendfile()). So this helper
950 * can splice directly through a process-private pipe.
953 long do_splice_direct(struct file
*in
, loff_t
*ppos
, struct file
*out
,
954 loff_t
*opos
, size_t len
, unsigned int flags
)
956 struct splice_desc sd
= {
966 if (unlikely(!(out
->f_mode
& FMODE_WRITE
)))
969 if (unlikely(out
->f_flags
& O_APPEND
))
972 ret
= rw_verify_area(WRITE
, out
, opos
, len
);
973 if (unlikely(ret
< 0))
976 ret
= splice_direct_to_actor(in
, &sd
, direct_splice_actor
);
982 EXPORT_SYMBOL(do_splice_direct
);
984 static int wait_for_space(struct pipe_inode_info
*pipe
, unsigned flags
)
987 if (unlikely(!pipe
->readers
)) {
988 send_sig(SIGPIPE
, current
, 0);
991 if (!pipe_full(pipe
->head
, pipe
->tail
, pipe
->max_usage
))
993 if (flags
& SPLICE_F_NONBLOCK
)
995 if (signal_pending(current
))
997 pipe_wait_writable(pipe
);
1001 static int splice_pipe_to_pipe(struct pipe_inode_info
*ipipe
,
1002 struct pipe_inode_info
*opipe
,
1003 size_t len
, unsigned int flags
);
1006 * Determine where to splice to/from.
1008 long do_splice(struct file
*in
, loff_t
*off_in
, struct file
*out
,
1009 loff_t
*off_out
, size_t len
, unsigned int flags
)
1011 struct pipe_inode_info
*ipipe
;
1012 struct pipe_inode_info
*opipe
;
1016 if (unlikely(!(in
->f_mode
& FMODE_READ
) ||
1017 !(out
->f_mode
& FMODE_WRITE
)))
1020 ipipe
= get_pipe_info(in
, true);
1021 opipe
= get_pipe_info(out
, true);
1023 if (ipipe
&& opipe
) {
1024 if (off_in
|| off_out
)
1027 /* Splicing to self would be fun, but... */
1031 if ((in
->f_flags
| out
->f_flags
) & O_NONBLOCK
)
1032 flags
|= SPLICE_F_NONBLOCK
;
1034 return splice_pipe_to_pipe(ipipe
, opipe
, len
, flags
);
1041 if (!(out
->f_mode
& FMODE_PWRITE
))
1045 offset
= out
->f_pos
;
1048 if (unlikely(out
->f_flags
& O_APPEND
))
1051 ret
= rw_verify_area(WRITE
, out
, &offset
, len
);
1052 if (unlikely(ret
< 0))
1055 if (in
->f_flags
& O_NONBLOCK
)
1056 flags
|= SPLICE_F_NONBLOCK
;
1058 file_start_write(out
);
1059 ret
= do_splice_from(ipipe
, out
, &offset
, len
, flags
);
1060 file_end_write(out
);
1063 out
->f_pos
= offset
;
1074 if (!(in
->f_mode
& FMODE_PREAD
))
1081 if (out
->f_flags
& O_NONBLOCK
)
1082 flags
|= SPLICE_F_NONBLOCK
;
1085 ret
= wait_for_space(opipe
, flags
);
1087 unsigned int p_space
;
1089 /* Don't try to read more the pipe has space for. */
1090 p_space
= opipe
->max_usage
- pipe_occupancy(opipe
->head
, opipe
->tail
);
1091 len
= min_t(size_t, len
, p_space
<< PAGE_SHIFT
);
1093 ret
= do_splice_to(in
, &offset
, opipe
, len
, flags
);
1097 wakeup_pipe_readers(opipe
);
1109 static long __do_splice(struct file
*in
, loff_t __user
*off_in
,
1110 struct file
*out
, loff_t __user
*off_out
,
1111 size_t len
, unsigned int flags
)
1113 struct pipe_inode_info
*ipipe
;
1114 struct pipe_inode_info
*opipe
;
1115 loff_t offset
, *__off_in
= NULL
, *__off_out
= NULL
;
1118 ipipe
= get_pipe_info(in
, true);
1119 opipe
= get_pipe_info(out
, true);
1121 if (ipipe
&& off_in
)
1123 if (opipe
&& off_out
)
1127 if (copy_from_user(&offset
, off_out
, sizeof(loff_t
)))
1129 __off_out
= &offset
;
1132 if (copy_from_user(&offset
, off_in
, sizeof(loff_t
)))
1137 ret
= do_splice(in
, __off_in
, out
, __off_out
, len
, flags
);
1141 if (__off_out
&& copy_to_user(off_out
, __off_out
, sizeof(loff_t
)))
1143 if (__off_in
&& copy_to_user(off_in
, __off_in
, sizeof(loff_t
)))
1149 static int iter_to_pipe(struct iov_iter
*from
,
1150 struct pipe_inode_info
*pipe
,
1153 struct pipe_buffer buf
= {
1154 .ops
= &user_page_pipe_buf_ops
,
1159 bool failed
= false;
1161 while (iov_iter_count(from
) && !failed
) {
1162 struct page
*pages
[16];
1167 copied
= iov_iter_get_pages(from
, pages
, ~0UL, 16, &start
);
1173 for (n
= 0; copied
; n
++, start
= 0) {
1174 int size
= min_t(int, copied
, PAGE_SIZE
- start
);
1176 buf
.page
= pages
[n
];
1179 ret
= add_to_pipe(pipe
, &buf
);
1180 if (unlikely(ret
< 0)) {
1183 iov_iter_advance(from
, ret
);
1192 return total
? total
: ret
;
1195 static int pipe_to_user(struct pipe_inode_info
*pipe
, struct pipe_buffer
*buf
,
1196 struct splice_desc
*sd
)
1198 int n
= copy_page_to_iter(buf
->page
, buf
->offset
, sd
->len
, sd
->u
.data
);
1199 return n
== sd
->len
? n
: -EFAULT
;
1203 * For lack of a better implementation, implement vmsplice() to userspace
1204 * as a simple copy of the pipes pages to the user iov.
1206 static long vmsplice_to_user(struct file
*file
, struct iov_iter
*iter
,
1209 struct pipe_inode_info
*pipe
= get_pipe_info(file
, true);
1210 struct splice_desc sd
= {
1211 .total_len
= iov_iter_count(iter
),
1222 ret
= __splice_from_pipe(pipe
, &sd
, pipe_to_user
);
1230 * vmsplice splices a user address range into a pipe. It can be thought of
1231 * as splice-from-memory, where the regular splice is splice-from-file (or
1232 * to file). In both cases the output is a pipe, naturally.
1234 static long vmsplice_to_pipe(struct file
*file
, struct iov_iter
*iter
,
1237 struct pipe_inode_info
*pipe
;
1239 unsigned buf_flag
= 0;
1241 if (flags
& SPLICE_F_GIFT
)
1242 buf_flag
= PIPE_BUF_FLAG_GIFT
;
1244 pipe
= get_pipe_info(file
, true);
1249 ret
= wait_for_space(pipe
, flags
);
1251 ret
= iter_to_pipe(iter
, pipe
, buf_flag
);
1254 wakeup_pipe_readers(pipe
);
1258 static int vmsplice_type(struct fd f
, int *type
)
1262 if (f
.file
->f_mode
& FMODE_WRITE
) {
1264 } else if (f
.file
->f_mode
& FMODE_READ
) {
1274 * Note that vmsplice only really supports true splicing _from_ user memory
1275 * to a pipe, not the other way around. Splicing from user memory is a simple
1276 * operation that can be supported without any funky alignment restrictions
1277 * or nasty vm tricks. We simply map in the user memory and fill them into
1278 * a pipe. The reverse isn't quite as easy, though. There are two possible
1279 * solutions for that:
1281 * - memcpy() the data internally, at which point we might as well just
1282 * do a regular read() on the buffer anyway.
1283 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1284 * has restriction limitations on both ends of the pipe).
1286 * Currently we punt and implement it as a normal copy, see pipe_to_user().
1289 SYSCALL_DEFINE4(vmsplice
, int, fd
, const struct iovec __user
*, uiov
,
1290 unsigned long, nr_segs
, unsigned int, flags
)
1292 struct iovec iovstack
[UIO_FASTIOV
];
1293 struct iovec
*iov
= iovstack
;
1294 struct iov_iter iter
;
1299 if (unlikely(flags
& ~SPLICE_F_ALL
))
1303 error
= vmsplice_type(f
, &type
);
1307 error
= import_iovec(type
, uiov
, nr_segs
,
1308 ARRAY_SIZE(iovstack
), &iov
, &iter
);
1312 if (!iov_iter_count(&iter
))
1314 else if (iov_iter_rw(&iter
) == WRITE
)
1315 error
= vmsplice_to_pipe(f
.file
, &iter
, flags
);
1317 error
= vmsplice_to_user(f
.file
, &iter
, flags
);
1325 SYSCALL_DEFINE6(splice
, int, fd_in
, loff_t __user
*, off_in
,
1326 int, fd_out
, loff_t __user
*, off_out
,
1327 size_t, len
, unsigned int, flags
)
1335 if (unlikely(flags
& ~SPLICE_F_ALL
))
1341 out
= fdget(fd_out
);
1343 error
= __do_splice(in
.file
, off_in
, out
.file
, off_out
,
1353 * Make sure there's data to read. Wait for input if we can, otherwise
1354 * return an appropriate error.
1356 static int ipipe_prep(struct pipe_inode_info
*pipe
, unsigned int flags
)
1361 * Check the pipe occupancy without the inode lock first. This function
1362 * is speculative anyways, so missing one is ok.
1364 if (!pipe_empty(pipe
->head
, pipe
->tail
))
1370 while (pipe_empty(pipe
->head
, pipe
->tail
)) {
1371 if (signal_pending(current
)) {
1377 if (flags
& SPLICE_F_NONBLOCK
) {
1381 pipe_wait_readable(pipe
);
1389 * Make sure there's writeable room. Wait for room if we can, otherwise
1390 * return an appropriate error.
1392 static int opipe_prep(struct pipe_inode_info
*pipe
, unsigned int flags
)
1397 * Check pipe occupancy without the inode lock first. This function
1398 * is speculative anyways, so missing one is ok.
1400 if (!pipe_full(pipe
->head
, pipe
->tail
, pipe
->max_usage
))
1406 while (pipe_full(pipe
->head
, pipe
->tail
, pipe
->max_usage
)) {
1407 if (!pipe
->readers
) {
1408 send_sig(SIGPIPE
, current
, 0);
1412 if (flags
& SPLICE_F_NONBLOCK
) {
1416 if (signal_pending(current
)) {
1420 pipe_wait_writable(pipe
);
1428 * Splice contents of ipipe to opipe.
1430 static int splice_pipe_to_pipe(struct pipe_inode_info
*ipipe
,
1431 struct pipe_inode_info
*opipe
,
1432 size_t len
, unsigned int flags
)
1434 struct pipe_buffer
*ibuf
, *obuf
;
1435 unsigned int i_head
, o_head
;
1436 unsigned int i_tail
, o_tail
;
1437 unsigned int i_mask
, o_mask
;
1439 bool input_wakeup
= false;
1443 ret
= ipipe_prep(ipipe
, flags
);
1447 ret
= opipe_prep(opipe
, flags
);
1452 * Potential ABBA deadlock, work around it by ordering lock
1453 * grabbing by pipe info address. Otherwise two different processes
1454 * could deadlock (one doing tee from A -> B, the other from B -> A).
1456 pipe_double_lock(ipipe
, opipe
);
1458 i_tail
= ipipe
->tail
;
1459 i_mask
= ipipe
->ring_size
- 1;
1460 o_head
= opipe
->head
;
1461 o_mask
= opipe
->ring_size
- 1;
1466 if (!opipe
->readers
) {
1467 send_sig(SIGPIPE
, current
, 0);
1473 i_head
= ipipe
->head
;
1474 o_tail
= opipe
->tail
;
1476 if (pipe_empty(i_head
, i_tail
) && !ipipe
->writers
)
1480 * Cannot make any progress, because either the input
1481 * pipe is empty or the output pipe is full.
1483 if (pipe_empty(i_head
, i_tail
) ||
1484 pipe_full(o_head
, o_tail
, opipe
->max_usage
)) {
1485 /* Already processed some buffers, break */
1489 if (flags
& SPLICE_F_NONBLOCK
) {
1495 * We raced with another reader/writer and haven't
1496 * managed to process any buffers. A zero return
1497 * value means EOF, so retry instead.
1504 ibuf
= &ipipe
->bufs
[i_tail
& i_mask
];
1505 obuf
= &opipe
->bufs
[o_head
& o_mask
];
1507 if (len
>= ibuf
->len
) {
1509 * Simply move the whole buffer from ipipe to opipe
1514 ipipe
->tail
= i_tail
;
1515 input_wakeup
= true;
1518 opipe
->head
= o_head
;
1521 * Get a reference to this pipe buffer,
1522 * so we can copy the contents over.
1524 if (!pipe_buf_get(ipipe
, ibuf
)) {
1532 * Don't inherit the gift and merge flags, we need to
1533 * prevent multiple steals of this page.
1535 obuf
->flags
&= ~PIPE_BUF_FLAG_GIFT
;
1536 obuf
->flags
&= ~PIPE_BUF_FLAG_CAN_MERGE
;
1539 ibuf
->offset
+= len
;
1543 opipe
->head
= o_head
;
1553 * If we put data in the output pipe, wakeup any potential readers.
1556 wakeup_pipe_readers(opipe
);
1559 wakeup_pipe_writers(ipipe
);
1565 * Link contents of ipipe to opipe.
1567 static int link_pipe(struct pipe_inode_info
*ipipe
,
1568 struct pipe_inode_info
*opipe
,
1569 size_t len
, unsigned int flags
)
1571 struct pipe_buffer
*ibuf
, *obuf
;
1572 unsigned int i_head
, o_head
;
1573 unsigned int i_tail
, o_tail
;
1574 unsigned int i_mask
, o_mask
;
1578 * Potential ABBA deadlock, work around it by ordering lock
1579 * grabbing by pipe info address. Otherwise two different processes
1580 * could deadlock (one doing tee from A -> B, the other from B -> A).
1582 pipe_double_lock(ipipe
, opipe
);
1584 i_tail
= ipipe
->tail
;
1585 i_mask
= ipipe
->ring_size
- 1;
1586 o_head
= opipe
->head
;
1587 o_mask
= opipe
->ring_size
- 1;
1590 if (!opipe
->readers
) {
1591 send_sig(SIGPIPE
, current
, 0);
1597 i_head
= ipipe
->head
;
1598 o_tail
= opipe
->tail
;
1601 * If we have iterated all input buffers or run out of
1602 * output room, break.
1604 if (pipe_empty(i_head
, i_tail
) ||
1605 pipe_full(o_head
, o_tail
, opipe
->max_usage
))
1608 ibuf
= &ipipe
->bufs
[i_tail
& i_mask
];
1609 obuf
= &opipe
->bufs
[o_head
& o_mask
];
1612 * Get a reference to this pipe buffer,
1613 * so we can copy the contents over.
1615 if (!pipe_buf_get(ipipe
, ibuf
)) {
1624 * Don't inherit the gift and merge flag, we need to prevent
1625 * multiple steals of this page.
1627 obuf
->flags
&= ~PIPE_BUF_FLAG_GIFT
;
1628 obuf
->flags
&= ~PIPE_BUF_FLAG_CAN_MERGE
;
1630 if (obuf
->len
> len
)
1636 opipe
->head
= o_head
;
1644 * If we put data in the output pipe, wakeup any potential readers.
1647 wakeup_pipe_readers(opipe
);
1653 * This is a tee(1) implementation that works on pipes. It doesn't copy
1654 * any data, it simply references the 'in' pages on the 'out' pipe.
1655 * The 'flags' used are the SPLICE_F_* variants, currently the only
1656 * applicable one is SPLICE_F_NONBLOCK.
1658 long do_tee(struct file
*in
, struct file
*out
, size_t len
, unsigned int flags
)
1660 struct pipe_inode_info
*ipipe
= get_pipe_info(in
, true);
1661 struct pipe_inode_info
*opipe
= get_pipe_info(out
, true);
1664 if (unlikely(!(in
->f_mode
& FMODE_READ
) ||
1665 !(out
->f_mode
& FMODE_WRITE
)))
1669 * Duplicate the contents of ipipe to opipe without actually
1672 if (ipipe
&& opipe
&& ipipe
!= opipe
) {
1673 if ((in
->f_flags
| out
->f_flags
) & O_NONBLOCK
)
1674 flags
|= SPLICE_F_NONBLOCK
;
1677 * Keep going, unless we encounter an error. The ipipe/opipe
1678 * ordering doesn't really matter.
1680 ret
= ipipe_prep(ipipe
, flags
);
1682 ret
= opipe_prep(opipe
, flags
);
1684 ret
= link_pipe(ipipe
, opipe
, len
, flags
);
1691 SYSCALL_DEFINE4(tee
, int, fdin
, int, fdout
, size_t, len
, unsigned int, flags
)
1696 if (unlikely(flags
& ~SPLICE_F_ALL
))
1707 error
= do_tee(in
.file
, out
.file
, len
, flags
);