splice
splice is a Linux system call that transfers data between file descriptors, at least one of which is a pipe, without a round trip through userspace.
One of the two descriptors must refer to a pipe, and since Linux 2.6.31 both may. A pipe-side offset must be NULL, while a non-pipe offset pointer leaves the file offset unchanged and advances the supplied value instead. [1] Each transfer is bounded by pipe capacity, which defaults to 16 pages, or 65,536 bytes with 4096-byte pages, and can be changed with F_SETPIPE_SZ. [4]
Pipe buffers can retain reference-counted page references; this does not establish copy avoidance for every operation or direction. In particular, pipe-to-user vmsplice copies bytes, unlike its user-to-pipe path. [7] A retained page reference protects lifetime, not immutability. SPLICE_F_MOVE has been a no-op since Linux 2.6.21, not an exclusive-ownership guarantee, and SPLICE_F_NONBLOCK makes only the pipe operations nonblocking. [1]
The kernel also provides copy_splice_read, which allocates pages, reads file data into them and splices those pages into the pipe, so use of splice alone does not establish that no payload copy occurred. Kernel helpers also route sendfile and some copy_file_range methods through splice. [2]
A failed explicit-offset copyback can follow pipe consumption or delivery. A controlled Linux 6.8.0-147-generic pipe-to-file experiment returned EFAULT after consuming six bytes; repairing permissions and retrying at the stale output offset overwrote that range with later input. An inaccessible-offset control failed before consuming any bytes. These are deliberately invalid caller-buffer tests, not a device-failure rate. [5] [6]
Relations
- related
- sendfile, Page cache, copy_file_range, vmsplice
Advantages
- Moving file data to a socket or another file through a pipe avoids copying between kernel and user address space. [1]
Limitations
- The call fails with EINVAL when the target file is opened in append mode or the filesystem does not support splicing. [1]
- Since Linux 4.5 the default pipe capacity can fall below 16 pages when a per-user soft limit is exceeded. [4]
Citations
- Linux man-pages 6.19: splice(2), pipe buffers and flags
- Linux 6.12 documentation: splice and pipes, copy_splice_read
- Linux man-pages 6.19: pipe(7), pipe capacity
- Hesela: native offset-copyback failures, observed bytes and unsafe retries
- Linux v6.8: transfer and caller-offset copyback
- Linux man-pages 6.19: vmsplice(2), directions, GIFT and NOTES
Further reading
Sources & caveats
- splice is the Linux man-pages 6.19 splice(2) page, re-read for descriptor and offset rules, flags, errors and the pipe-buffer notes.
- pipe7 is the Linux man-pages 6.19 pipe(7) page, read for pipe capacity defaults and limits.
- copy-path is the Linux 6.12 splice and pipes documentation, re-read for copy_splice_read and the sendfile and copy_file_range helpers.
- The rwf-nowait entry links here because SPLICE_F_NONBLOCK, like RWF_NOWAIT, limits blocking only for part of the I/O path.