1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1991, 1992, 1999 Linus Torvalds
9 #include <linux/file.h>
10 #include <linux/poll.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
15 #include <linux/log2.h>
16 #include <linux/mount.h>
17 #include <linux/magic.h>
18 #include <linux/pipe_fs_i.h>
19 #include <linux/uio.h>
20 #include <linux/highmem.h>
21 #include <linux/pagemap.h>
22 #include <linux/audit.h>
23 #include <linux/syscalls.h>
24 #include <linux/fcntl.h>
25 #include <linux/memcontrol.h>
27 #include <linux/uaccess.h>
28 #include <asm/ioctls.h>
33 * New pipe buffers will be restricted to this size while the user is exceeding
34 * their pipe buffer quota. The general pipe use case needs at least two
35 * buffers: one for data yet to be read, and one for new data. If this is less
36 * than two, then a write to a non-empty pipe may block even if the pipe is not
37 * full. This can occur with GNU make jobserver or similar uses of pipes as
38 * semaphores: multiple processes may be waiting to write tokens back to the
39 * pipe before reading tokens: https://lore.kernel.org/lkml/1628086770.5rn8p04n6j.none@localhost/.
41 * Users can reduce their pipe buffers with F_SETPIPE_SZ below this at their
42 * own risk, namely: pipe writes to non-full pipes may block until the pipe is
45 #define PIPE_MIN_DEF_BUFFERS 2
48 * The max size that a non-root user is allowed to grow the pipe. Can
49 * be set by root in /proc/sys/fs/pipe-max-size
51 unsigned int pipe_max_size = 1048576;
53 /* Maximum allocatable pages per user. Hard limit is unset by default, soft
54 * matches default values.
56 unsigned long pipe_user_pages_hard;
57 unsigned long pipe_user_pages_soft = PIPE_DEF_BUFFERS * INR_OPEN_CUR;
60 * We use a start+len construction, which provides full use of the
62 * -- Florian Coosmann (FGC)
64 * Reads with count = 0 should always return 0.
65 * -- Julian Bradfield 1999-06-07.
67 * FIFOs and Pipes now generate SIGIO for both readers and writers.
68 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
70 * pipe_read & write cleanup
71 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
74 static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass)
77 mutex_lock_nested(&pipe->mutex, subclass);
80 void pipe_lock(struct pipe_inode_info *pipe)
83 * pipe_lock() nests non-pipe inode locks (for writing to a file)
85 pipe_lock_nested(pipe, I_MUTEX_PARENT);
87 EXPORT_SYMBOL(pipe_lock);
89 void pipe_unlock(struct pipe_inode_info *pipe)
92 mutex_unlock(&pipe->mutex);
94 EXPORT_SYMBOL(pipe_unlock);
96 static inline void __pipe_lock(struct pipe_inode_info *pipe)
98 mutex_lock_nested(&pipe->mutex, I_MUTEX_PARENT);
101 static inline void __pipe_unlock(struct pipe_inode_info *pipe)
103 mutex_unlock(&pipe->mutex);
106 void pipe_double_lock(struct pipe_inode_info *pipe1,
107 struct pipe_inode_info *pipe2)
109 BUG_ON(pipe1 == pipe2);
112 pipe_lock_nested(pipe1, I_MUTEX_PARENT);
113 pipe_lock_nested(pipe2, I_MUTEX_CHILD);
115 pipe_lock_nested(pipe2, I_MUTEX_PARENT);
116 pipe_lock_nested(pipe1, I_MUTEX_CHILD);
120 /* Drop the inode semaphore and wait for a pipe event, atomically */
121 void pipe_wait(struct pipe_inode_info *pipe)
126 * Pipes are system-local resources, so sleeping on them
127 * is considered a noninteractive wait:
129 prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE);
132 finish_wait(&pipe->wait, &wait);
136 static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
137 struct pipe_buffer *buf)
139 struct page *page = buf->page;
142 * If nobody else uses this page, and we don't already have a
143 * temporary page, let's keep track of it as a one-deep
144 * allocation cache. (Otherwise just release our reference to it)
146 if (page_count(page) == 1 && !pipe->tmp_page)
147 pipe->tmp_page = page;
152 static int anon_pipe_buf_steal(struct pipe_inode_info *pipe,
153 struct pipe_buffer *buf)
155 struct page *page = buf->page;
157 if (page_count(page) == 1) {
158 if (memcg_kmem_enabled())
159 memcg_kmem_uncharge(page, 0);
160 __SetPageLocked(page);
167 * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
168 * @pipe: the pipe that the buffer belongs to
169 * @buf: the buffer to attempt to steal
172 * This function attempts to steal the &struct page attached to
173 * @buf. If successful, this function returns 0 and returns with
174 * the page locked. The caller may then reuse the page for whatever
175 * he wishes; the typical use is insertion into a different file
178 int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
179 struct pipe_buffer *buf)
181 struct page *page = buf->page;
184 * A reference of one is golden, that means that the owner of this
185 * page is the only one holding a reference to it. lock the page
188 if (page_count(page) == 1) {
195 EXPORT_SYMBOL(generic_pipe_buf_steal);
198 * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
199 * @pipe: the pipe that the buffer belongs to
200 * @buf: the buffer to get a reference to
203 * This function grabs an extra reference to @buf. It's used in
204 * in the tee() system call, when we duplicate the buffers in one
207 bool generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
209 return try_get_page(buf->page);
211 EXPORT_SYMBOL(generic_pipe_buf_get);
214 * generic_pipe_buf_confirm - verify contents of the pipe buffer
215 * @info: the pipe that the buffer belongs to
216 * @buf: the buffer to confirm
219 * This function does nothing, because the generic pipe code uses
220 * pages that are always good when inserted into the pipe.
222 int generic_pipe_buf_confirm(struct pipe_inode_info *info,
223 struct pipe_buffer *buf)
227 EXPORT_SYMBOL(generic_pipe_buf_confirm);
230 * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
231 * @pipe: the pipe that the buffer belongs to
232 * @buf: the buffer to put a reference to
235 * This function releases a reference to @buf.
237 void generic_pipe_buf_release(struct pipe_inode_info *pipe,
238 struct pipe_buffer *buf)
242 EXPORT_SYMBOL(generic_pipe_buf_release);
244 static const struct pipe_buf_operations anon_pipe_buf_ops = {
246 .confirm = generic_pipe_buf_confirm,
247 .release = anon_pipe_buf_release,
248 .steal = anon_pipe_buf_steal,
249 .get = generic_pipe_buf_get,
252 static const struct pipe_buf_operations anon_pipe_buf_nomerge_ops = {
254 .confirm = generic_pipe_buf_confirm,
255 .release = anon_pipe_buf_release,
256 .steal = anon_pipe_buf_steal,
257 .get = generic_pipe_buf_get,
260 static const struct pipe_buf_operations packet_pipe_buf_ops = {
262 .confirm = generic_pipe_buf_confirm,
263 .release = anon_pipe_buf_release,
264 .steal = anon_pipe_buf_steal,
265 .get = generic_pipe_buf_get,
268 void pipe_buf_mark_unmergeable(struct pipe_buffer *buf)
270 if (buf->ops == &anon_pipe_buf_ops)
271 buf->ops = &anon_pipe_buf_nomerge_ops;
275 pipe_read(struct kiocb *iocb, struct iov_iter *to)
277 size_t total_len = iov_iter_count(to);
278 struct file *filp = iocb->ki_filp;
279 struct pipe_inode_info *pipe = filp->private_data;
283 /* Null read succeeds. */
284 if (unlikely(total_len == 0))
291 int bufs = pipe->nrbufs;
293 int curbuf = pipe->curbuf;
294 struct pipe_buffer *buf = pipe->bufs + curbuf;
295 size_t chars = buf->len;
299 if (chars > total_len)
302 error = pipe_buf_confirm(pipe, buf);
309 written = copy_page_to_iter(buf->page, buf->offset, chars, to);
310 if (unlikely(written < chars)) {
316 buf->offset += chars;
319 /* Was it a packet buffer? Clean up and exit */
320 if (buf->flags & PIPE_BUF_FLAG_PACKET) {
326 pipe_buf_release(pipe, buf);
327 curbuf = (curbuf + 1) & (pipe->buffers - 1);
328 pipe->curbuf = curbuf;
329 pipe->nrbufs = --bufs;
334 break; /* common path: read succeeded */
336 if (bufs) /* More to do? */
340 if (!pipe->waiting_writers) {
341 /* syscall merging: Usually we must not sleep
342 * if O_NONBLOCK is set, or if we got some data.
343 * But if a writer sleeps in kernel space, then
344 * we can wait for that data without violating POSIX.
348 if (filp->f_flags & O_NONBLOCK) {
353 if (signal_pending(current)) {
359 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLOUT | EPOLLWRNORM);
360 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
366 /* Signal writers asynchronously that there is more room. */
368 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLOUT | EPOLLWRNORM);
369 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
376 static inline int is_packetized(struct file *file)
378 return (file->f_flags & O_DIRECT) != 0;
382 pipe_write(struct kiocb *iocb, struct iov_iter *from)
384 struct file *filp = iocb->ki_filp;
385 struct pipe_inode_info *pipe = filp->private_data;
388 size_t total_len = iov_iter_count(from);
391 /* Null write succeeds. */
392 if (unlikely(total_len == 0))
397 if (!pipe->readers) {
398 send_sig(SIGPIPE, current, 0);
403 /* We try to merge small writes */
404 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
405 if (pipe->nrbufs && chars != 0) {
406 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
408 struct pipe_buffer *buf = pipe->bufs + lastbuf;
409 int offset = buf->offset + buf->len;
411 if (buf->ops->can_merge && offset + chars <= PAGE_SIZE) {
412 ret = pipe_buf_confirm(pipe, buf);
416 ret = copy_page_from_iter(buf->page, offset, chars, from);
417 if (unlikely(ret < chars)) {
423 if (!iov_iter_count(from))
431 if (!pipe->readers) {
432 send_sig(SIGPIPE, current, 0);
438 if (bufs < pipe->buffers) {
439 int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1);
440 struct pipe_buffer *buf = pipe->bufs + newbuf;
441 struct page *page = pipe->tmp_page;
445 page = alloc_page(GFP_HIGHUSER | __GFP_ACCOUNT);
446 if (unlikely(!page)) {
447 ret = ret ? : -ENOMEM;
450 pipe->tmp_page = page;
452 /* Always wake up, even if the copy fails. Otherwise
453 * we lock up (O_NONBLOCK-)readers that sleep due to
455 * FIXME! Is this really true?
458 copied = copy_page_from_iter(page, 0, PAGE_SIZE, from);
459 if (unlikely(copied < PAGE_SIZE && iov_iter_count(from))) {
466 /* Insert it into the buffer array */
468 buf->ops = &anon_pipe_buf_ops;
472 if (is_packetized(filp)) {
473 buf->ops = &packet_pipe_buf_ops;
474 buf->flags = PIPE_BUF_FLAG_PACKET;
476 pipe->nrbufs = ++bufs;
477 pipe->tmp_page = NULL;
479 if (!iov_iter_count(from))
482 if (bufs < pipe->buffers)
484 if (filp->f_flags & O_NONBLOCK) {
489 if (signal_pending(current)) {
495 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLRDNORM);
496 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
499 pipe->waiting_writers++;
501 pipe->waiting_writers--;
506 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLRDNORM);
507 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
509 if (ret > 0 && sb_start_write_trylock(file_inode(filp)->i_sb)) {
510 int err = file_update_time(filp);
513 sb_end_write(file_inode(filp)->i_sb);
518 static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
520 struct pipe_inode_info *pipe = filp->private_data;
521 int count, buf, nrbufs;
528 nrbufs = pipe->nrbufs;
529 while (--nrbufs >= 0) {
530 count += pipe->bufs[buf].len;
531 buf = (buf+1) & (pipe->buffers - 1);
535 return put_user(count, (int __user *)arg);
541 /* No kernel lock held - fine */
543 pipe_poll(struct file *filp, poll_table *wait)
546 struct pipe_inode_info *pipe = filp->private_data;
549 poll_wait(filp, &pipe->wait, wait);
551 /* Reading only -- no need for acquiring the semaphore. */
552 nrbufs = pipe->nrbufs;
554 if (filp->f_mode & FMODE_READ) {
555 mask = (nrbufs > 0) ? EPOLLIN | EPOLLRDNORM : 0;
556 if (!pipe->writers && filp->f_version != pipe->w_counter)
560 if (filp->f_mode & FMODE_WRITE) {
561 mask |= (nrbufs < pipe->buffers) ? EPOLLOUT | EPOLLWRNORM : 0;
563 * Most Unices do not set EPOLLERR for FIFOs but on Linux they
564 * behave exactly like pipes for poll().
573 static void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe)
577 spin_lock(&inode->i_lock);
578 if (!--pipe->files) {
579 inode->i_pipe = NULL;
582 spin_unlock(&inode->i_lock);
585 free_pipe_info(pipe);
589 pipe_release(struct inode *inode, struct file *file)
591 struct pipe_inode_info *pipe = file->private_data;
594 if (file->f_mode & FMODE_READ)
596 if (file->f_mode & FMODE_WRITE)
599 if (pipe->readers || pipe->writers) {
600 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLOUT | EPOLLRDNORM | EPOLLWRNORM | EPOLLERR | EPOLLHUP);
601 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
602 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
606 put_pipe_info(inode, pipe);
611 pipe_fasync(int fd, struct file *filp, int on)
613 struct pipe_inode_info *pipe = filp->private_data;
617 if (filp->f_mode & FMODE_READ)
618 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
619 if ((filp->f_mode & FMODE_WRITE) && retval >= 0) {
620 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
621 if (retval < 0 && (filp->f_mode & FMODE_READ))
622 /* this can happen only if on == T */
623 fasync_helper(-1, filp, 0, &pipe->fasync_readers);
629 static unsigned long account_pipe_buffers(struct user_struct *user,
630 unsigned long old, unsigned long new)
632 return atomic_long_add_return(new - old, &user->pipe_bufs);
635 static bool too_many_pipe_buffers_soft(unsigned long user_bufs)
637 unsigned long soft_limit = READ_ONCE(pipe_user_pages_soft);
639 return soft_limit && user_bufs > soft_limit;
642 static bool too_many_pipe_buffers_hard(unsigned long user_bufs)
644 unsigned long hard_limit = READ_ONCE(pipe_user_pages_hard);
646 return hard_limit && user_bufs > hard_limit;
649 static bool is_unprivileged_user(void)
651 return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
654 struct pipe_inode_info *alloc_pipe_info(void)
656 struct pipe_inode_info *pipe;
657 unsigned long pipe_bufs = PIPE_DEF_BUFFERS;
658 struct user_struct *user = get_current_user();
659 unsigned long user_bufs;
660 unsigned int max_size = READ_ONCE(pipe_max_size);
662 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL_ACCOUNT);
666 if (pipe_bufs * PAGE_SIZE > max_size && !capable(CAP_SYS_RESOURCE))
667 pipe_bufs = max_size >> PAGE_SHIFT;
669 user_bufs = account_pipe_buffers(user, 0, pipe_bufs);
671 if (too_many_pipe_buffers_soft(user_bufs) && is_unprivileged_user()) {
672 user_bufs = account_pipe_buffers(user, pipe_bufs, PIPE_MIN_DEF_BUFFERS);
673 pipe_bufs = PIPE_MIN_DEF_BUFFERS;
676 if (too_many_pipe_buffers_hard(user_bufs) && is_unprivileged_user())
677 goto out_revert_acct;
679 pipe->bufs = kcalloc(pipe_bufs, sizeof(struct pipe_buffer),
683 init_waitqueue_head(&pipe->wait);
684 pipe->r_counter = pipe->w_counter = 1;
685 pipe->buffers = pipe_bufs;
687 mutex_init(&pipe->mutex);
692 (void) account_pipe_buffers(user, pipe_bufs, 0);
699 void free_pipe_info(struct pipe_inode_info *pipe)
703 (void) account_pipe_buffers(pipe->user, pipe->buffers, 0);
704 free_uid(pipe->user);
705 for (i = 0; i < pipe->buffers; i++) {
706 struct pipe_buffer *buf = pipe->bufs + i;
708 pipe_buf_release(pipe, buf);
711 __free_page(pipe->tmp_page);
716 static struct vfsmount *pipe_mnt __read_mostly;
719 * pipefs_dname() is called from d_path().
721 static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
723 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
724 d_inode(dentry)->i_ino);
727 static const struct dentry_operations pipefs_dentry_operations = {
728 .d_dname = pipefs_dname,
731 static struct inode * get_pipe_inode(void)
733 struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
734 struct pipe_inode_info *pipe;
739 inode->i_ino = get_next_ino();
741 pipe = alloc_pipe_info();
745 inode->i_pipe = pipe;
747 pipe->readers = pipe->writers = 1;
748 inode->i_fop = &pipefifo_fops;
751 * Mark the inode dirty from the very beginning,
752 * that way it will never be moved to the dirty
753 * list because "mark_inode_dirty()" will think
754 * that it already _is_ on the dirty list.
756 inode->i_state = I_DIRTY;
757 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
758 inode->i_uid = current_fsuid();
759 inode->i_gid = current_fsgid();
760 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
771 int create_pipe_files(struct file **res, int flags)
773 struct inode *inode = get_pipe_inode();
779 f = alloc_file_pseudo(inode, pipe_mnt, "",
780 O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT)),
783 free_pipe_info(inode->i_pipe);
788 f->private_data = inode->i_pipe;
790 res[0] = alloc_file_clone(f, O_RDONLY | (flags & O_NONBLOCK),
792 if (IS_ERR(res[0])) {
793 put_pipe_info(inode, inode->i_pipe);
795 return PTR_ERR(res[0]);
797 res[0]->private_data = inode->i_pipe;
802 static int __do_pipe_flags(int *fd, struct file **files, int flags)
807 if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
810 error = create_pipe_files(files, flags);
814 error = get_unused_fd_flags(flags);
819 error = get_unused_fd_flags(flags);
824 audit_fd_pair(fdr, fdw);
837 int do_pipe_flags(int *fd, int flags)
839 struct file *files[2];
840 int error = __do_pipe_flags(fd, files, flags);
842 fd_install(fd[0], files[0]);
843 fd_install(fd[1], files[1]);
849 * sys_pipe() is the normal C calling standard for creating
850 * a pipe. It's not the way Unix traditionally does this, though.
852 static int do_pipe2(int __user *fildes, int flags)
854 struct file *files[2];
858 error = __do_pipe_flags(fd, files, flags);
860 if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
863 put_unused_fd(fd[0]);
864 put_unused_fd(fd[1]);
867 fd_install(fd[0], files[0]);
868 fd_install(fd[1], files[1]);
874 SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
876 return do_pipe2(fildes, flags);
879 SYSCALL_DEFINE1(pipe, int __user *, fildes)
881 return do_pipe2(fildes, 0);
884 static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
888 while (cur == *cnt) {
890 if (signal_pending(current))
893 return cur == *cnt ? -ERESTARTSYS : 0;
896 static void wake_up_partner(struct pipe_inode_info *pipe)
898 wake_up_interruptible(&pipe->wait);
901 static int fifo_open(struct inode *inode, struct file *filp)
903 struct pipe_inode_info *pipe;
904 bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
909 spin_lock(&inode->i_lock);
911 pipe = inode->i_pipe;
913 spin_unlock(&inode->i_lock);
915 spin_unlock(&inode->i_lock);
916 pipe = alloc_pipe_info();
920 spin_lock(&inode->i_lock);
921 if (unlikely(inode->i_pipe)) {
922 inode->i_pipe->files++;
923 spin_unlock(&inode->i_lock);
924 free_pipe_info(pipe);
925 pipe = inode->i_pipe;
927 inode->i_pipe = pipe;
928 spin_unlock(&inode->i_lock);
931 filp->private_data = pipe;
932 /* OK, we have a pipe and it's pinned down */
936 /* We can only do regular read/write on fifos */
937 filp->f_mode &= (FMODE_READ | FMODE_WRITE);
939 switch (filp->f_mode) {
943 * POSIX.1 says that O_NONBLOCK means return with the FIFO
944 * opened, even when there is no process writing the FIFO.
947 if (pipe->readers++ == 0)
948 wake_up_partner(pipe);
950 if (!is_pipe && !pipe->writers) {
951 if ((filp->f_flags & O_NONBLOCK)) {
952 /* suppress EPOLLHUP until we have
954 filp->f_version = pipe->w_counter;
956 if (wait_for_partner(pipe, &pipe->w_counter))
965 * POSIX.1 says that O_NONBLOCK means return -1 with
966 * errno=ENXIO when there is no process reading the FIFO.
969 if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
973 if (!pipe->writers++)
974 wake_up_partner(pipe);
976 if (!is_pipe && !pipe->readers) {
977 if (wait_for_partner(pipe, &pipe->r_counter))
982 case FMODE_READ | FMODE_WRITE:
985 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
986 * This implementation will NEVER block on a O_RDWR open, since
987 * the process can at least talk to itself.
994 if (pipe->readers == 1 || pipe->writers == 1)
995 wake_up_partner(pipe);
1004 __pipe_unlock(pipe);
1008 if (!--pipe->readers)
1009 wake_up_interruptible(&pipe->wait);
1014 if (!--pipe->writers)
1015 wake_up_interruptible(&pipe->wait);
1020 __pipe_unlock(pipe);
1022 put_pipe_info(inode, pipe);
1026 const struct file_operations pipefifo_fops = {
1028 .llseek = no_llseek,
1029 .read_iter = pipe_read,
1030 .write_iter = pipe_write,
1032 .unlocked_ioctl = pipe_ioctl,
1033 .release = pipe_release,
1034 .fasync = pipe_fasync,
1038 * Currently we rely on the pipe array holding a power-of-2 number
1039 * of pages. Returns 0 on error.
1041 unsigned int round_pipe_size(unsigned long size)
1043 if (size > (1U << 31))
1046 /* Minimum pipe size, as required by POSIX */
1047 if (size < PAGE_SIZE)
1050 return roundup_pow_of_two(size);
1054 * Allocate a new array of pipe buffers and copy the info over. Returns the
1055 * pipe size if successful, or return -ERROR on error.
1057 static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long arg)
1059 struct pipe_buffer *bufs;
1060 unsigned int size, nr_pages;
1061 unsigned long user_bufs;
1064 size = round_pipe_size(arg);
1065 nr_pages = size >> PAGE_SHIFT;
1071 * If trying to increase the pipe capacity, check that an
1072 * unprivileged user is not trying to exceed various limits
1073 * (soft limit check here, hard limit check just below).
1074 * Decreasing the pipe capacity is always permitted, even
1075 * if the user is currently over a limit.
1077 if (nr_pages > pipe->buffers &&
1078 size > pipe_max_size && !capable(CAP_SYS_RESOURCE))
1081 user_bufs = account_pipe_buffers(pipe->user, pipe->buffers, nr_pages);
1083 if (nr_pages > pipe->buffers &&
1084 (too_many_pipe_buffers_hard(user_bufs) ||
1085 too_many_pipe_buffers_soft(user_bufs)) &&
1086 is_unprivileged_user()) {
1088 goto out_revert_acct;
1092 * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
1093 * expect a lot of shrink+grow operations, just free and allocate
1094 * again like we would do for growing. If the pipe currently
1095 * contains more buffers than arg, then return busy.
1097 if (nr_pages < pipe->nrbufs) {
1099 goto out_revert_acct;
1102 bufs = kcalloc(nr_pages, sizeof(*bufs),
1103 GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
1104 if (unlikely(!bufs)) {
1106 goto out_revert_acct;
1110 * The pipe array wraps around, so just start the new one at zero
1111 * and adjust the indexes.
1117 tail = pipe->curbuf + pipe->nrbufs;
1118 if (tail < pipe->buffers)
1121 tail &= (pipe->buffers - 1);
1123 head = pipe->nrbufs - tail;
1125 memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
1127 memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
1133 pipe->buffers = nr_pages;
1134 return nr_pages * PAGE_SIZE;
1137 (void) account_pipe_buffers(pipe->user, nr_pages, pipe->buffers);
1142 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1143 * location, so checking ->i_pipe is not enough to verify that this is a
1146 struct pipe_inode_info *get_pipe_info(struct file *file)
1148 return file->f_op == &pipefifo_fops ? file->private_data : NULL;
1151 long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1153 struct pipe_inode_info *pipe;
1156 pipe = get_pipe_info(file);
1164 ret = pipe_set_size(pipe, arg);
1167 ret = pipe->buffers * PAGE_SIZE;
1174 __pipe_unlock(pipe);
1178 static const struct super_operations pipefs_ops = {
1179 .destroy_inode = free_inode_nonrcu,
1180 .statfs = simple_statfs,
1184 * pipefs should _never_ be mounted by userland - too much of security hassle,
1185 * no real gain from having the whole whorehouse mounted. So we don't need
1186 * any operations on the root directory. However, we need a non-trivial
1187 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1189 static struct dentry *pipefs_mount(struct file_system_type *fs_type,
1190 int flags, const char *dev_name, void *data)
1192 return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
1193 &pipefs_dentry_operations, PIPEFS_MAGIC);
1196 static struct file_system_type pipe_fs_type = {
1198 .mount = pipefs_mount,
1199 .kill_sb = kill_anon_super,
1202 static int __init init_pipe_fs(void)
1204 int err = register_filesystem(&pipe_fs_type);
1207 pipe_mnt = kern_mount(&pipe_fs_type);
1208 if (IS_ERR(pipe_mnt)) {
1209 err = PTR_ERR(pipe_mnt);
1210 unregister_filesystem(&pipe_fs_type);
1216 fs_initcall(init_pipe_fs);