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/pseudo_fs.h>
18 #include <linux/magic.h>
19 #include <linux/pipe_fs_i.h>
20 #include <linux/uio.h>
21 #include <linux/highmem.h>
22 #include <linux/pagemap.h>
23 #include <linux/audit.h>
24 #include <linux/syscalls.h>
25 #include <linux/fcntl.h>
26 #include <linux/memcontrol.h>
28 #include <linux/uaccess.h>
29 #include <asm/ioctls.h>
34 * New pipe buffers will be restricted to this size while the user is exceeding
35 * their pipe buffer quota. The general pipe use case needs at least two
36 * buffers: one for data yet to be read, and one for new data. If this is less
37 * than two, then a write to a non-empty pipe may block even if the pipe is not
38 * full. This can occur with GNU make jobserver or similar uses of pipes as
39 * semaphores: multiple processes may be waiting to write tokens back to the
40 * pipe before reading tokens: https://lore.kernel.org/lkml/1628086770.5rn8p04n6j.none@localhost/.
42 * Users can reduce their pipe buffers with F_SETPIPE_SZ below this at their
43 * own risk, namely: pipe writes to non-full pipes may block until the pipe is
46 #define PIPE_MIN_DEF_BUFFERS 2
49 * The max size that a non-root user is allowed to grow the pipe. Can
50 * be set by root in /proc/sys/fs/pipe-max-size
52 unsigned int pipe_max_size = 1048576;
54 /* Maximum allocatable pages per user. Hard limit is unset by default, soft
55 * matches default values.
57 unsigned long pipe_user_pages_hard;
58 unsigned long pipe_user_pages_soft = PIPE_DEF_BUFFERS * INR_OPEN_CUR;
61 * We use a start+len construction, which provides full use of the
63 * -- Florian Coosmann (FGC)
65 * Reads with count = 0 should always return 0.
66 * -- Julian Bradfield 1999-06-07.
68 * FIFOs and Pipes now generate SIGIO for both readers and writers.
69 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
71 * pipe_read & write cleanup
72 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
75 static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass)
78 mutex_lock_nested(&pipe->mutex, subclass);
81 void pipe_lock(struct pipe_inode_info *pipe)
84 * pipe_lock() nests non-pipe inode locks (for writing to a file)
86 pipe_lock_nested(pipe, I_MUTEX_PARENT);
88 EXPORT_SYMBOL(pipe_lock);
90 void pipe_unlock(struct pipe_inode_info *pipe)
93 mutex_unlock(&pipe->mutex);
95 EXPORT_SYMBOL(pipe_unlock);
97 static inline void __pipe_lock(struct pipe_inode_info *pipe)
99 mutex_lock_nested(&pipe->mutex, I_MUTEX_PARENT);
102 static inline void __pipe_unlock(struct pipe_inode_info *pipe)
104 mutex_unlock(&pipe->mutex);
107 void pipe_double_lock(struct pipe_inode_info *pipe1,
108 struct pipe_inode_info *pipe2)
110 BUG_ON(pipe1 == pipe2);
113 pipe_lock_nested(pipe1, I_MUTEX_PARENT);
114 pipe_lock_nested(pipe2, I_MUTEX_CHILD);
116 pipe_lock_nested(pipe2, I_MUTEX_PARENT);
117 pipe_lock_nested(pipe1, I_MUTEX_CHILD);
121 /* Drop the inode semaphore and wait for a pipe event, atomically */
122 void pipe_wait(struct pipe_inode_info *pipe)
127 * Pipes are system-local resources, so sleeping on them
128 * is considered a noninteractive wait:
130 prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE);
133 finish_wait(&pipe->wait, &wait);
137 static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
138 struct pipe_buffer *buf)
140 struct page *page = buf->page;
143 * If nobody else uses this page, and we don't already have a
144 * temporary page, let's keep track of it as a one-deep
145 * allocation cache. (Otherwise just release our reference to it)
147 if (page_count(page) == 1 && !pipe->tmp_page)
148 pipe->tmp_page = page;
153 static int anon_pipe_buf_steal(struct pipe_inode_info *pipe,
154 struct pipe_buffer *buf)
156 struct page *page = buf->page;
158 if (page_count(page) == 1) {
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 /* New data written to a pipe may be appended to a buffer with this type. */
245 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 = {
253 .confirm = generic_pipe_buf_confirm,
254 .release = anon_pipe_buf_release,
255 .steal = anon_pipe_buf_steal,
256 .get = generic_pipe_buf_get,
259 static const struct pipe_buf_operations packet_pipe_buf_ops = {
260 .confirm = generic_pipe_buf_confirm,
261 .release = anon_pipe_buf_release,
262 .steal = anon_pipe_buf_steal,
263 .get = generic_pipe_buf_get,
267 * pipe_buf_mark_unmergeable - mark a &struct pipe_buffer as unmergeable
268 * @buf: the buffer to mark
271 * This function ensures that no future writes will be merged into the
272 * given &struct pipe_buffer. This is necessary when multiple pipe buffers
273 * share the same backing page.
275 void pipe_buf_mark_unmergeable(struct pipe_buffer *buf)
277 if (buf->ops == &anon_pipe_buf_ops)
278 buf->ops = &anon_pipe_buf_nomerge_ops;
281 static bool pipe_buf_can_merge(struct pipe_buffer *buf)
283 return buf->ops == &anon_pipe_buf_ops;
287 pipe_read(struct kiocb *iocb, struct iov_iter *to)
289 size_t total_len = iov_iter_count(to);
290 struct file *filp = iocb->ki_filp;
291 struct pipe_inode_info *pipe = filp->private_data;
295 /* Null read succeeds. */
296 if (unlikely(total_len == 0))
303 int bufs = pipe->nrbufs;
305 int curbuf = pipe->curbuf;
306 struct pipe_buffer *buf = pipe->bufs + curbuf;
307 size_t chars = buf->len;
311 if (chars > total_len)
314 error = pipe_buf_confirm(pipe, buf);
321 written = copy_page_to_iter(buf->page, buf->offset, chars, to);
322 if (unlikely(written < chars)) {
328 buf->offset += chars;
331 /* Was it a packet buffer? Clean up and exit */
332 if (buf->flags & PIPE_BUF_FLAG_PACKET) {
338 pipe_buf_release(pipe, buf);
339 curbuf = (curbuf + 1) & (pipe->buffers - 1);
340 pipe->curbuf = curbuf;
341 pipe->nrbufs = --bufs;
346 break; /* common path: read succeeded */
348 if (bufs) /* More to do? */
352 if (!pipe->waiting_writers) {
353 /* syscall merging: Usually we must not sleep
354 * if O_NONBLOCK is set, or if we got some data.
355 * But if a writer sleeps in kernel space, then
356 * we can wait for that data without violating POSIX.
360 if (filp->f_flags & O_NONBLOCK) {
365 if (signal_pending(current)) {
371 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLOUT | EPOLLWRNORM);
372 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
378 /* Signal writers asynchronously that there is more room. */
380 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLOUT | EPOLLWRNORM);
381 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
388 static inline int is_packetized(struct file *file)
390 return (file->f_flags & O_DIRECT) != 0;
394 pipe_write(struct kiocb *iocb, struct iov_iter *from)
396 struct file *filp = iocb->ki_filp;
397 struct pipe_inode_info *pipe = filp->private_data;
400 size_t total_len = iov_iter_count(from);
403 /* Null write succeeds. */
404 if (unlikely(total_len == 0))
409 if (!pipe->readers) {
410 send_sig(SIGPIPE, current, 0);
415 /* We try to merge small writes */
416 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
417 if (pipe->nrbufs && chars != 0) {
418 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
420 struct pipe_buffer *buf = pipe->bufs + lastbuf;
421 int offset = buf->offset + buf->len;
423 if (pipe_buf_can_merge(buf) && offset + chars <= PAGE_SIZE) {
424 ret = pipe_buf_confirm(pipe, buf);
428 ret = copy_page_from_iter(buf->page, offset, chars, from);
429 if (unlikely(ret < chars)) {
435 if (!iov_iter_count(from))
443 if (!pipe->readers) {
444 send_sig(SIGPIPE, current, 0);
450 if (bufs < pipe->buffers) {
451 int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1);
452 struct pipe_buffer *buf = pipe->bufs + newbuf;
453 struct page *page = pipe->tmp_page;
457 page = alloc_page(GFP_HIGHUSER | __GFP_ACCOUNT);
458 if (unlikely(!page)) {
459 ret = ret ? : -ENOMEM;
462 pipe->tmp_page = page;
464 /* Always wake up, even if the copy fails. Otherwise
465 * we lock up (O_NONBLOCK-)readers that sleep due to
467 * FIXME! Is this really true?
470 copied = copy_page_from_iter(page, 0, PAGE_SIZE, from);
471 if (unlikely(copied < PAGE_SIZE && iov_iter_count(from))) {
478 /* Insert it into the buffer array */
480 buf->ops = &anon_pipe_buf_ops;
484 if (is_packetized(filp)) {
485 buf->ops = &packet_pipe_buf_ops;
486 buf->flags = PIPE_BUF_FLAG_PACKET;
488 pipe->nrbufs = ++bufs;
489 pipe->tmp_page = NULL;
491 if (!iov_iter_count(from))
494 if (bufs < pipe->buffers)
496 if (filp->f_flags & O_NONBLOCK) {
501 if (signal_pending(current)) {
507 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLRDNORM);
508 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
511 pipe->waiting_writers++;
513 pipe->waiting_writers--;
518 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLRDNORM);
519 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
521 if (ret > 0 && sb_start_write_trylock(file_inode(filp)->i_sb)) {
522 int err = file_update_time(filp);
525 sb_end_write(file_inode(filp)->i_sb);
530 static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
532 struct pipe_inode_info *pipe = filp->private_data;
533 int count, buf, nrbufs;
540 nrbufs = pipe->nrbufs;
541 while (--nrbufs >= 0) {
542 count += pipe->bufs[buf].len;
543 buf = (buf+1) & (pipe->buffers - 1);
547 return put_user(count, (int __user *)arg);
553 /* No kernel lock held - fine */
555 pipe_poll(struct file *filp, poll_table *wait)
558 struct pipe_inode_info *pipe = filp->private_data;
561 poll_wait(filp, &pipe->wait, wait);
563 /* Reading only -- no need for acquiring the semaphore. */
564 nrbufs = pipe->nrbufs;
566 if (filp->f_mode & FMODE_READ) {
567 mask = (nrbufs > 0) ? EPOLLIN | EPOLLRDNORM : 0;
568 if (!pipe->writers && filp->f_version != pipe->w_counter)
572 if (filp->f_mode & FMODE_WRITE) {
573 mask |= (nrbufs < pipe->buffers) ? EPOLLOUT | EPOLLWRNORM : 0;
575 * Most Unices do not set EPOLLERR for FIFOs but on Linux they
576 * behave exactly like pipes for poll().
585 static void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe)
589 spin_lock(&inode->i_lock);
590 if (!--pipe->files) {
591 inode->i_pipe = NULL;
594 spin_unlock(&inode->i_lock);
597 free_pipe_info(pipe);
601 pipe_release(struct inode *inode, struct file *file)
603 struct pipe_inode_info *pipe = file->private_data;
606 if (file->f_mode & FMODE_READ)
608 if (file->f_mode & FMODE_WRITE)
611 if (pipe->readers || pipe->writers) {
612 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLOUT | EPOLLRDNORM | EPOLLWRNORM | EPOLLERR | EPOLLHUP);
613 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
614 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
618 put_pipe_info(inode, pipe);
623 pipe_fasync(int fd, struct file *filp, int on)
625 struct pipe_inode_info *pipe = filp->private_data;
629 if (filp->f_mode & FMODE_READ)
630 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
631 if ((filp->f_mode & FMODE_WRITE) && retval >= 0) {
632 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
633 if (retval < 0 && (filp->f_mode & FMODE_READ))
634 /* this can happen only if on == T */
635 fasync_helper(-1, filp, 0, &pipe->fasync_readers);
641 static unsigned long account_pipe_buffers(struct user_struct *user,
642 unsigned long old, unsigned long new)
644 return atomic_long_add_return(new - old, &user->pipe_bufs);
647 static bool too_many_pipe_buffers_soft(unsigned long user_bufs)
649 unsigned long soft_limit = READ_ONCE(pipe_user_pages_soft);
651 return soft_limit && user_bufs > soft_limit;
654 static bool too_many_pipe_buffers_hard(unsigned long user_bufs)
656 unsigned long hard_limit = READ_ONCE(pipe_user_pages_hard);
658 return hard_limit && user_bufs > hard_limit;
661 static bool is_unprivileged_user(void)
663 return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
666 struct pipe_inode_info *alloc_pipe_info(void)
668 struct pipe_inode_info *pipe;
669 unsigned long pipe_bufs = PIPE_DEF_BUFFERS;
670 struct user_struct *user = get_current_user();
671 unsigned long user_bufs;
672 unsigned int max_size = READ_ONCE(pipe_max_size);
674 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL_ACCOUNT);
678 if (pipe_bufs * PAGE_SIZE > max_size && !capable(CAP_SYS_RESOURCE))
679 pipe_bufs = max_size >> PAGE_SHIFT;
681 user_bufs = account_pipe_buffers(user, 0, pipe_bufs);
683 if (too_many_pipe_buffers_soft(user_bufs) && is_unprivileged_user()) {
684 user_bufs = account_pipe_buffers(user, pipe_bufs, PIPE_MIN_DEF_BUFFERS);
685 pipe_bufs = PIPE_MIN_DEF_BUFFERS;
688 if (too_many_pipe_buffers_hard(user_bufs) && is_unprivileged_user())
689 goto out_revert_acct;
691 pipe->bufs = kcalloc(pipe_bufs, sizeof(struct pipe_buffer),
695 init_waitqueue_head(&pipe->wait);
696 pipe->r_counter = pipe->w_counter = 1;
697 pipe->buffers = pipe_bufs;
699 mutex_init(&pipe->mutex);
704 (void) account_pipe_buffers(user, pipe_bufs, 0);
711 void free_pipe_info(struct pipe_inode_info *pipe)
715 (void) account_pipe_buffers(pipe->user, pipe->buffers, 0);
716 free_uid(pipe->user);
717 for (i = 0; i < pipe->buffers; i++) {
718 struct pipe_buffer *buf = pipe->bufs + i;
720 pipe_buf_release(pipe, buf);
723 __free_page(pipe->tmp_page);
728 static struct vfsmount *pipe_mnt __read_mostly;
731 * pipefs_dname() is called from d_path().
733 static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
735 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
736 d_inode(dentry)->i_ino);
739 static const struct dentry_operations pipefs_dentry_operations = {
740 .d_dname = pipefs_dname,
743 static struct inode * get_pipe_inode(void)
745 struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
746 struct pipe_inode_info *pipe;
751 inode->i_ino = get_next_ino();
753 pipe = alloc_pipe_info();
757 inode->i_pipe = pipe;
759 pipe->readers = pipe->writers = 1;
760 inode->i_fop = &pipefifo_fops;
763 * Mark the inode dirty from the very beginning,
764 * that way it will never be moved to the dirty
765 * list because "mark_inode_dirty()" will think
766 * that it already _is_ on the dirty list.
768 inode->i_state = I_DIRTY;
769 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
770 inode->i_uid = current_fsuid();
771 inode->i_gid = current_fsgid();
772 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
783 int create_pipe_files(struct file **res, int flags)
785 struct inode *inode = get_pipe_inode();
791 f = alloc_file_pseudo(inode, pipe_mnt, "",
792 O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT)),
795 free_pipe_info(inode->i_pipe);
800 f->private_data = inode->i_pipe;
802 res[0] = alloc_file_clone(f, O_RDONLY | (flags & O_NONBLOCK),
804 if (IS_ERR(res[0])) {
805 put_pipe_info(inode, inode->i_pipe);
807 return PTR_ERR(res[0]);
809 res[0]->private_data = inode->i_pipe;
814 static int __do_pipe_flags(int *fd, struct file **files, int flags)
819 if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
822 error = create_pipe_files(files, flags);
826 error = get_unused_fd_flags(flags);
831 error = get_unused_fd_flags(flags);
836 audit_fd_pair(fdr, fdw);
849 int do_pipe_flags(int *fd, int flags)
851 struct file *files[2];
852 int error = __do_pipe_flags(fd, files, flags);
854 fd_install(fd[0], files[0]);
855 fd_install(fd[1], files[1]);
861 * sys_pipe() is the normal C calling standard for creating
862 * a pipe. It's not the way Unix traditionally does this, though.
864 static int do_pipe2(int __user *fildes, int flags)
866 struct file *files[2];
870 error = __do_pipe_flags(fd, files, flags);
872 if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
875 put_unused_fd(fd[0]);
876 put_unused_fd(fd[1]);
879 fd_install(fd[0], files[0]);
880 fd_install(fd[1], files[1]);
886 SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
888 return do_pipe2(fildes, flags);
891 SYSCALL_DEFINE1(pipe, int __user *, fildes)
893 return do_pipe2(fildes, 0);
896 static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
900 while (cur == *cnt) {
902 if (signal_pending(current))
905 return cur == *cnt ? -ERESTARTSYS : 0;
908 static void wake_up_partner(struct pipe_inode_info *pipe)
910 wake_up_interruptible(&pipe->wait);
913 static int fifo_open(struct inode *inode, struct file *filp)
915 struct pipe_inode_info *pipe;
916 bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
921 spin_lock(&inode->i_lock);
923 pipe = inode->i_pipe;
925 spin_unlock(&inode->i_lock);
927 spin_unlock(&inode->i_lock);
928 pipe = alloc_pipe_info();
932 spin_lock(&inode->i_lock);
933 if (unlikely(inode->i_pipe)) {
934 inode->i_pipe->files++;
935 spin_unlock(&inode->i_lock);
936 free_pipe_info(pipe);
937 pipe = inode->i_pipe;
939 inode->i_pipe = pipe;
940 spin_unlock(&inode->i_lock);
943 filp->private_data = pipe;
944 /* OK, we have a pipe and it's pinned down */
948 /* We can only do regular read/write on fifos */
949 filp->f_mode &= (FMODE_READ | FMODE_WRITE);
951 switch (filp->f_mode) {
955 * POSIX.1 says that O_NONBLOCK means return with the FIFO
956 * opened, even when there is no process writing the FIFO.
959 if (pipe->readers++ == 0)
960 wake_up_partner(pipe);
962 if (!is_pipe && !pipe->writers) {
963 if ((filp->f_flags & O_NONBLOCK)) {
964 /* suppress EPOLLHUP until we have
966 filp->f_version = pipe->w_counter;
968 if (wait_for_partner(pipe, &pipe->w_counter))
977 * POSIX.1 says that O_NONBLOCK means return -1 with
978 * errno=ENXIO when there is no process reading the FIFO.
981 if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
985 if (!pipe->writers++)
986 wake_up_partner(pipe);
988 if (!is_pipe && !pipe->readers) {
989 if (wait_for_partner(pipe, &pipe->r_counter))
994 case FMODE_READ | FMODE_WRITE:
997 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
998 * This implementation will NEVER block on a O_RDWR open, since
999 * the process can at least talk to itself.
1006 if (pipe->readers == 1 || pipe->writers == 1)
1007 wake_up_partner(pipe);
1016 __pipe_unlock(pipe);
1020 if (!--pipe->readers)
1021 wake_up_interruptible(&pipe->wait);
1026 if (!--pipe->writers)
1027 wake_up_interruptible(&pipe->wait);
1032 __pipe_unlock(pipe);
1034 put_pipe_info(inode, pipe);
1038 const struct file_operations pipefifo_fops = {
1040 .llseek = no_llseek,
1041 .read_iter = pipe_read,
1042 .write_iter = pipe_write,
1044 .unlocked_ioctl = pipe_ioctl,
1045 .release = pipe_release,
1046 .fasync = pipe_fasync,
1050 * Currently we rely on the pipe array holding a power-of-2 number
1051 * of pages. Returns 0 on error.
1053 unsigned int round_pipe_size(unsigned long size)
1055 if (size > (1U << 31))
1058 /* Minimum pipe size, as required by POSIX */
1059 if (size < PAGE_SIZE)
1062 return roundup_pow_of_two(size);
1066 * Allocate a new array of pipe buffers and copy the info over. Returns the
1067 * pipe size if successful, or return -ERROR on error.
1069 static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long arg)
1071 struct pipe_buffer *bufs;
1072 unsigned int size, nr_pages;
1073 unsigned long user_bufs;
1076 size = round_pipe_size(arg);
1077 nr_pages = size >> PAGE_SHIFT;
1083 * If trying to increase the pipe capacity, check that an
1084 * unprivileged user is not trying to exceed various limits
1085 * (soft limit check here, hard limit check just below).
1086 * Decreasing the pipe capacity is always permitted, even
1087 * if the user is currently over a limit.
1089 if (nr_pages > pipe->buffers &&
1090 size > pipe_max_size && !capable(CAP_SYS_RESOURCE))
1093 user_bufs = account_pipe_buffers(pipe->user, pipe->buffers, nr_pages);
1095 if (nr_pages > pipe->buffers &&
1096 (too_many_pipe_buffers_hard(user_bufs) ||
1097 too_many_pipe_buffers_soft(user_bufs)) &&
1098 is_unprivileged_user()) {
1100 goto out_revert_acct;
1104 * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
1105 * expect a lot of shrink+grow operations, just free and allocate
1106 * again like we would do for growing. If the pipe currently
1107 * contains more buffers than arg, then return busy.
1109 if (nr_pages < pipe->nrbufs) {
1111 goto out_revert_acct;
1114 bufs = kcalloc(nr_pages, sizeof(*bufs),
1115 GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
1116 if (unlikely(!bufs)) {
1118 goto out_revert_acct;
1122 * The pipe array wraps around, so just start the new one at zero
1123 * and adjust the indexes.
1129 tail = pipe->curbuf + pipe->nrbufs;
1130 if (tail < pipe->buffers)
1133 tail &= (pipe->buffers - 1);
1135 head = pipe->nrbufs - tail;
1137 memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
1139 memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
1145 pipe->buffers = nr_pages;
1146 return nr_pages * PAGE_SIZE;
1149 (void) account_pipe_buffers(pipe->user, nr_pages, pipe->buffers);
1154 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1155 * location, so checking ->i_pipe is not enough to verify that this is a
1158 struct pipe_inode_info *get_pipe_info(struct file *file)
1160 return file->f_op == &pipefifo_fops ? file->private_data : NULL;
1163 long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1165 struct pipe_inode_info *pipe;
1168 pipe = get_pipe_info(file);
1176 ret = pipe_set_size(pipe, arg);
1179 ret = pipe->buffers * PAGE_SIZE;
1186 __pipe_unlock(pipe);
1190 static const struct super_operations pipefs_ops = {
1191 .destroy_inode = free_inode_nonrcu,
1192 .statfs = simple_statfs,
1196 * pipefs should _never_ be mounted by userland - too much of security hassle,
1197 * no real gain from having the whole whorehouse mounted. So we don't need
1198 * any operations on the root directory. However, we need a non-trivial
1199 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1202 static int pipefs_init_fs_context(struct fs_context *fc)
1204 struct pseudo_fs_context *ctx = init_pseudo(fc, PIPEFS_MAGIC);
1207 ctx->ops = &pipefs_ops;
1208 ctx->dops = &pipefs_dentry_operations;
1212 static struct file_system_type pipe_fs_type = {
1214 .init_fs_context = pipefs_init_fs_context,
1215 .kill_sb = kill_anon_super,
1218 static int __init init_pipe_fs(void)
1220 int err = register_filesystem(&pipe_fs_type);
1223 pipe_mnt = kern_mount(&pipe_fs_type);
1224 if (IS_ERR(pipe_mnt)) {
1225 err = PTR_ERR(pipe_mnt);
1226 unregister_filesystem(&pipe_fs_type);
1232 fs_initcall(init_pipe_fs);