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 * The max size that a non-root user is allowed to grow the pipe. Can
35 * be set by root in /proc/sys/fs/pipe-max-size
37 unsigned int pipe_max_size = 1048576;
39 /* Maximum allocatable pages per user. Hard limit is unset by default, soft
40 * matches default values.
42 unsigned long pipe_user_pages_hard;
43 unsigned long pipe_user_pages_soft = PIPE_DEF_BUFFERS * INR_OPEN_CUR;
46 * We use head and tail indices that aren't masked off, except at the point of
47 * dereference, but rather they're allowed to wrap naturally. This means there
48 * isn't a dead spot in the buffer, but the ring has to be a power of two and
50 * -- David Howells 2019-09-23.
52 * Reads with count = 0 should always return 0.
53 * -- Julian Bradfield 1999-06-07.
55 * FIFOs and Pipes now generate SIGIO for both readers and writers.
56 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
58 * pipe_read & write cleanup
59 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
62 static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass)
65 mutex_lock_nested(&pipe->mutex, subclass);
68 void pipe_lock(struct pipe_inode_info *pipe)
71 * pipe_lock() nests non-pipe inode locks (for writing to a file)
73 pipe_lock_nested(pipe, I_MUTEX_PARENT);
75 EXPORT_SYMBOL(pipe_lock);
77 void pipe_unlock(struct pipe_inode_info *pipe)
80 mutex_unlock(&pipe->mutex);
82 EXPORT_SYMBOL(pipe_unlock);
84 static inline void __pipe_lock(struct pipe_inode_info *pipe)
86 mutex_lock_nested(&pipe->mutex, I_MUTEX_PARENT);
89 static inline void __pipe_unlock(struct pipe_inode_info *pipe)
91 mutex_unlock(&pipe->mutex);
94 void pipe_double_lock(struct pipe_inode_info *pipe1,
95 struct pipe_inode_info *pipe2)
97 BUG_ON(pipe1 == pipe2);
100 pipe_lock_nested(pipe1, I_MUTEX_PARENT);
101 pipe_lock_nested(pipe2, I_MUTEX_CHILD);
103 pipe_lock_nested(pipe2, I_MUTEX_PARENT);
104 pipe_lock_nested(pipe1, I_MUTEX_CHILD);
108 /* Drop the inode semaphore and wait for a pipe event, atomically */
109 void pipe_wait(struct pipe_inode_info *pipe)
114 * Pipes are system-local resources, so sleeping on them
115 * is considered a noninteractive wait:
117 prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE);
120 finish_wait(&pipe->wait, &wait);
124 static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
125 struct pipe_buffer *buf)
127 struct page *page = buf->page;
130 * If nobody else uses this page, and we don't already have a
131 * temporary page, let's keep track of it as a one-deep
132 * allocation cache. (Otherwise just release our reference to it)
134 if (page_count(page) == 1 && !pipe->tmp_page)
135 pipe->tmp_page = page;
140 static int anon_pipe_buf_steal(struct pipe_inode_info *pipe,
141 struct pipe_buffer *buf)
143 struct page *page = buf->page;
145 if (page_count(page) == 1) {
146 memcg_kmem_uncharge(page, 0);
147 __SetPageLocked(page);
154 * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
155 * @pipe: the pipe that the buffer belongs to
156 * @buf: the buffer to attempt to steal
159 * This function attempts to steal the &struct page attached to
160 * @buf. If successful, this function returns 0 and returns with
161 * the page locked. The caller may then reuse the page for whatever
162 * he wishes; the typical use is insertion into a different file
165 int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
166 struct pipe_buffer *buf)
168 struct page *page = buf->page;
171 * A reference of one is golden, that means that the owner of this
172 * page is the only one holding a reference to it. lock the page
175 if (page_count(page) == 1) {
182 EXPORT_SYMBOL(generic_pipe_buf_steal);
185 * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
186 * @pipe: the pipe that the buffer belongs to
187 * @buf: the buffer to get a reference to
190 * This function grabs an extra reference to @buf. It's used in
191 * in the tee() system call, when we duplicate the buffers in one
194 bool generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
196 return try_get_page(buf->page);
198 EXPORT_SYMBOL(generic_pipe_buf_get);
201 * generic_pipe_buf_confirm - verify contents of the pipe buffer
202 * @info: the pipe that the buffer belongs to
203 * @buf: the buffer to confirm
206 * This function does nothing, because the generic pipe code uses
207 * pages that are always good when inserted into the pipe.
209 int generic_pipe_buf_confirm(struct pipe_inode_info *info,
210 struct pipe_buffer *buf)
214 EXPORT_SYMBOL(generic_pipe_buf_confirm);
217 * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
218 * @pipe: the pipe that the buffer belongs to
219 * @buf: the buffer to put a reference to
222 * This function releases a reference to @buf.
224 void generic_pipe_buf_release(struct pipe_inode_info *pipe,
225 struct pipe_buffer *buf)
229 EXPORT_SYMBOL(generic_pipe_buf_release);
231 /* New data written to a pipe may be appended to a buffer with this type. */
232 static const struct pipe_buf_operations anon_pipe_buf_ops = {
233 .confirm = generic_pipe_buf_confirm,
234 .release = anon_pipe_buf_release,
235 .steal = anon_pipe_buf_steal,
236 .get = generic_pipe_buf_get,
239 static const struct pipe_buf_operations anon_pipe_buf_nomerge_ops = {
240 .confirm = generic_pipe_buf_confirm,
241 .release = anon_pipe_buf_release,
242 .steal = anon_pipe_buf_steal,
243 .get = generic_pipe_buf_get,
246 static const struct pipe_buf_operations packet_pipe_buf_ops = {
247 .confirm = generic_pipe_buf_confirm,
248 .release = anon_pipe_buf_release,
249 .steal = anon_pipe_buf_steal,
250 .get = generic_pipe_buf_get,
254 * pipe_buf_mark_unmergeable - mark a &struct pipe_buffer as unmergeable
255 * @buf: the buffer to mark
258 * This function ensures that no future writes will be merged into the
259 * given &struct pipe_buffer. This is necessary when multiple pipe buffers
260 * share the same backing page.
262 void pipe_buf_mark_unmergeable(struct pipe_buffer *buf)
264 if (buf->ops == &anon_pipe_buf_ops)
265 buf->ops = &anon_pipe_buf_nomerge_ops;
268 static bool pipe_buf_can_merge(struct pipe_buffer *buf)
270 return buf->ops == &anon_pipe_buf_ops;
274 pipe_read(struct kiocb *iocb, struct iov_iter *to)
276 size_t total_len = iov_iter_count(to);
277 struct file *filp = iocb->ki_filp;
278 struct pipe_inode_info *pipe = filp->private_data;
282 /* Null read succeeds. */
283 if (unlikely(total_len == 0))
290 unsigned int head = pipe->head;
291 unsigned int tail = pipe->tail;
292 unsigned int mask = pipe->ring_size - 1;
294 if (!pipe_empty(head, tail)) {
295 struct pipe_buffer *buf = &pipe->bufs[tail & mask];
296 size_t chars = buf->len;
300 if (chars > total_len)
303 error = pipe_buf_confirm(pipe, buf);
310 written = copy_page_to_iter(buf->page, buf->offset, chars, to);
311 if (unlikely(written < chars)) {
317 buf->offset += chars;
320 /* Was it a packet buffer? Clean up and exit */
321 if (buf->flags & PIPE_BUF_FLAG_PACKET) {
327 pipe_buf_release(pipe, buf);
334 break; /* common path: read succeeded */
335 if (!pipe_empty(head, tail)) /* More to do? */
341 if (!pipe->waiting_writers) {
342 /* syscall merging: Usually we must not sleep
343 * if O_NONBLOCK is set, or if we got some data.
344 * But if a writer sleeps in kernel space, then
345 * we can wait for that data without violating POSIX.
349 if (filp->f_flags & O_NONBLOCK) {
354 if (signal_pending(current)) {
360 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLOUT | EPOLLWRNORM);
361 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
367 /* Signal writers asynchronously that there is more room. */
369 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLOUT | EPOLLWRNORM);
370 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
377 static inline int is_packetized(struct file *file)
379 return (file->f_flags & O_DIRECT) != 0;
383 pipe_write(struct kiocb *iocb, struct iov_iter *from)
385 struct file *filp = iocb->ki_filp;
386 struct pipe_inode_info *pipe = filp->private_data;
387 unsigned int head, tail, max_usage, mask;
390 size_t total_len = iov_iter_count(from);
393 /* Null write succeeds. */
394 if (unlikely(total_len == 0))
399 if (!pipe->readers) {
400 send_sig(SIGPIPE, current, 0);
407 max_usage = pipe->ring_size;
408 mask = pipe->ring_size - 1;
410 /* We try to merge small writes */
411 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
412 if (!pipe_empty(head, tail) && chars != 0) {
413 struct pipe_buffer *buf = &pipe->bufs[(head - 1) & mask];
414 int offset = buf->offset + buf->len;
416 if (pipe_buf_can_merge(buf) && offset + chars <= PAGE_SIZE) {
417 ret = pipe_buf_confirm(pipe, buf);
421 ret = copy_page_from_iter(buf->page, offset, chars, from);
422 if (unlikely(ret < chars)) {
428 if (!iov_iter_count(from))
434 if (!pipe->readers) {
435 send_sig(SIGPIPE, current, 0);
442 if (!pipe_full(head, tail, max_usage)) {
443 struct pipe_buffer *buf = &pipe->bufs[head & mask];
444 struct page *page = pipe->tmp_page;
448 page = alloc_page(GFP_HIGHUSER | __GFP_ACCOUNT);
449 if (unlikely(!page)) {
450 ret = ret ? : -ENOMEM;
453 pipe->tmp_page = page;
455 /* Always wake up, even if the copy fails. Otherwise
456 * we lock up (O_NONBLOCK-)readers that sleep due to
458 * FIXME! Is this really true?
461 copied = copy_page_from_iter(page, 0, PAGE_SIZE, from);
462 if (unlikely(copied < PAGE_SIZE && iov_iter_count(from))) {
469 /* Insert it into the buffer array */
471 buf->ops = &anon_pipe_buf_ops;
475 if (is_packetized(filp)) {
476 buf->ops = &packet_pipe_buf_ops;
477 buf->flags = PIPE_BUF_FLAG_PACKET;
482 pipe->tmp_page = NULL;
484 if (!iov_iter_count(from))
488 if (!pipe_full(head, tail, max_usage))
491 /* Wait for buffer space to become available. */
492 if (filp->f_flags & O_NONBLOCK) {
497 if (signal_pending(current)) {
503 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLRDNORM);
504 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
507 pipe->waiting_writers++;
509 pipe->waiting_writers--;
514 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLRDNORM);
515 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
517 if (ret > 0 && sb_start_write_trylock(file_inode(filp)->i_sb)) {
518 int err = file_update_time(filp);
521 sb_end_write(file_inode(filp)->i_sb);
526 static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
528 struct pipe_inode_info *pipe = filp->private_data;
529 int count, head, tail, mask;
537 mask = pipe->ring_size - 1;
539 while (tail != head) {
540 count += pipe->bufs[tail & mask].len;
545 return put_user(count, (int __user *)arg);
551 /* No kernel lock held - fine */
553 pipe_poll(struct file *filp, poll_table *wait)
556 struct pipe_inode_info *pipe = filp->private_data;
557 unsigned int head = READ_ONCE(pipe->head);
558 unsigned int tail = READ_ONCE(pipe->tail);
560 poll_wait(filp, &pipe->wait, wait);
562 BUG_ON(pipe_occupancy(head, tail) > pipe->ring_size);
564 /* Reading only -- no need for acquiring the semaphore. */
566 if (filp->f_mode & FMODE_READ) {
567 if (!pipe_empty(head, tail))
568 mask |= EPOLLIN | EPOLLRDNORM;
569 if (!pipe->writers && filp->f_version != pipe->w_counter)
573 if (filp->f_mode & FMODE_WRITE) {
574 if (!pipe_full(head, tail, pipe->ring_size))
575 mask |= EPOLLOUT | EPOLLWRNORM;
577 * Most Unices do not set EPOLLERR for FIFOs but on Linux they
578 * behave exactly like pipes for poll().
587 static void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe)
591 spin_lock(&inode->i_lock);
592 if (!--pipe->files) {
593 inode->i_pipe = NULL;
596 spin_unlock(&inode->i_lock);
599 free_pipe_info(pipe);
603 pipe_release(struct inode *inode, struct file *file)
605 struct pipe_inode_info *pipe = file->private_data;
608 if (file->f_mode & FMODE_READ)
610 if (file->f_mode & FMODE_WRITE)
613 if (pipe->readers || pipe->writers) {
614 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLOUT | EPOLLRDNORM | EPOLLWRNORM | EPOLLERR | EPOLLHUP);
615 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
616 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
620 put_pipe_info(inode, pipe);
625 pipe_fasync(int fd, struct file *filp, int on)
627 struct pipe_inode_info *pipe = filp->private_data;
631 if (filp->f_mode & FMODE_READ)
632 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
633 if ((filp->f_mode & FMODE_WRITE) && retval >= 0) {
634 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
635 if (retval < 0 && (filp->f_mode & FMODE_READ))
636 /* this can happen only if on == T */
637 fasync_helper(-1, filp, 0, &pipe->fasync_readers);
643 static unsigned long account_pipe_buffers(struct user_struct *user,
644 unsigned long old, unsigned long new)
646 return atomic_long_add_return(new - old, &user->pipe_bufs);
649 static bool too_many_pipe_buffers_soft(unsigned long user_bufs)
651 unsigned long soft_limit = READ_ONCE(pipe_user_pages_soft);
653 return soft_limit && user_bufs > soft_limit;
656 static bool too_many_pipe_buffers_hard(unsigned long user_bufs)
658 unsigned long hard_limit = READ_ONCE(pipe_user_pages_hard);
660 return hard_limit && user_bufs > hard_limit;
663 static bool is_unprivileged_user(void)
665 return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
668 struct pipe_inode_info *alloc_pipe_info(void)
670 struct pipe_inode_info *pipe;
671 unsigned long pipe_bufs = PIPE_DEF_BUFFERS;
672 struct user_struct *user = get_current_user();
673 unsigned long user_bufs;
674 unsigned int max_size = READ_ONCE(pipe_max_size);
676 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL_ACCOUNT);
680 if (pipe_bufs * PAGE_SIZE > max_size && !capable(CAP_SYS_RESOURCE))
681 pipe_bufs = max_size >> PAGE_SHIFT;
683 user_bufs = account_pipe_buffers(user, 0, pipe_bufs);
685 if (too_many_pipe_buffers_soft(user_bufs) && is_unprivileged_user()) {
686 user_bufs = account_pipe_buffers(user, pipe_bufs, 1);
690 if (too_many_pipe_buffers_hard(user_bufs) && is_unprivileged_user())
691 goto out_revert_acct;
693 pipe->bufs = kcalloc(pipe_bufs, sizeof(struct pipe_buffer),
697 init_waitqueue_head(&pipe->wait);
698 pipe->r_counter = pipe->w_counter = 1;
699 pipe->ring_size = pipe_bufs;
701 mutex_init(&pipe->mutex);
706 (void) account_pipe_buffers(user, pipe_bufs, 0);
713 void free_pipe_info(struct pipe_inode_info *pipe)
717 (void) account_pipe_buffers(pipe->user, pipe->ring_size, 0);
718 free_uid(pipe->user);
719 for (i = 0; i < pipe->ring_size; i++) {
720 struct pipe_buffer *buf = pipe->bufs + i;
722 pipe_buf_release(pipe, buf);
725 __free_page(pipe->tmp_page);
730 static struct vfsmount *pipe_mnt __read_mostly;
733 * pipefs_dname() is called from d_path().
735 static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
737 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
738 d_inode(dentry)->i_ino);
741 static const struct dentry_operations pipefs_dentry_operations = {
742 .d_dname = pipefs_dname,
745 static struct inode * get_pipe_inode(void)
747 struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
748 struct pipe_inode_info *pipe;
753 inode->i_ino = get_next_ino();
755 pipe = alloc_pipe_info();
759 inode->i_pipe = pipe;
761 pipe->readers = pipe->writers = 1;
762 inode->i_fop = &pipefifo_fops;
765 * Mark the inode dirty from the very beginning,
766 * that way it will never be moved to the dirty
767 * list because "mark_inode_dirty()" will think
768 * that it already _is_ on the dirty list.
770 inode->i_state = I_DIRTY;
771 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
772 inode->i_uid = current_fsuid();
773 inode->i_gid = current_fsgid();
774 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
785 int create_pipe_files(struct file **res, int flags)
787 struct inode *inode = get_pipe_inode();
793 f = alloc_file_pseudo(inode, pipe_mnt, "",
794 O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT)),
797 free_pipe_info(inode->i_pipe);
802 f->private_data = inode->i_pipe;
804 res[0] = alloc_file_clone(f, O_RDONLY | (flags & O_NONBLOCK),
806 if (IS_ERR(res[0])) {
807 put_pipe_info(inode, inode->i_pipe);
809 return PTR_ERR(res[0]);
811 res[0]->private_data = inode->i_pipe;
816 static int __do_pipe_flags(int *fd, struct file **files, int flags)
821 if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
824 error = create_pipe_files(files, flags);
828 error = get_unused_fd_flags(flags);
833 error = get_unused_fd_flags(flags);
838 audit_fd_pair(fdr, fdw);
851 int do_pipe_flags(int *fd, int flags)
853 struct file *files[2];
854 int error = __do_pipe_flags(fd, files, flags);
856 fd_install(fd[0], files[0]);
857 fd_install(fd[1], files[1]);
863 * sys_pipe() is the normal C calling standard for creating
864 * a pipe. It's not the way Unix traditionally does this, though.
866 static int do_pipe2(int __user *fildes, int flags)
868 struct file *files[2];
872 error = __do_pipe_flags(fd, files, flags);
874 if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
877 put_unused_fd(fd[0]);
878 put_unused_fd(fd[1]);
881 fd_install(fd[0], files[0]);
882 fd_install(fd[1], files[1]);
888 SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
890 return do_pipe2(fildes, flags);
893 SYSCALL_DEFINE1(pipe, int __user *, fildes)
895 return do_pipe2(fildes, 0);
898 static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
902 while (cur == *cnt) {
904 if (signal_pending(current))
907 return cur == *cnt ? -ERESTARTSYS : 0;
910 static void wake_up_partner(struct pipe_inode_info *pipe)
912 wake_up_interruptible(&pipe->wait);
915 static int fifo_open(struct inode *inode, struct file *filp)
917 struct pipe_inode_info *pipe;
918 bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
923 spin_lock(&inode->i_lock);
925 pipe = inode->i_pipe;
927 spin_unlock(&inode->i_lock);
929 spin_unlock(&inode->i_lock);
930 pipe = alloc_pipe_info();
934 spin_lock(&inode->i_lock);
935 if (unlikely(inode->i_pipe)) {
936 inode->i_pipe->files++;
937 spin_unlock(&inode->i_lock);
938 free_pipe_info(pipe);
939 pipe = inode->i_pipe;
941 inode->i_pipe = pipe;
942 spin_unlock(&inode->i_lock);
945 filp->private_data = pipe;
946 /* OK, we have a pipe and it's pinned down */
950 /* We can only do regular read/write on fifos */
951 filp->f_mode &= (FMODE_READ | FMODE_WRITE);
953 switch (filp->f_mode) {
957 * POSIX.1 says that O_NONBLOCK means return with the FIFO
958 * opened, even when there is no process writing the FIFO.
961 if (pipe->readers++ == 0)
962 wake_up_partner(pipe);
964 if (!is_pipe && !pipe->writers) {
965 if ((filp->f_flags & O_NONBLOCK)) {
966 /* suppress EPOLLHUP until we have
968 filp->f_version = pipe->w_counter;
970 if (wait_for_partner(pipe, &pipe->w_counter))
979 * POSIX.1 says that O_NONBLOCK means return -1 with
980 * errno=ENXIO when there is no process reading the FIFO.
983 if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
987 if (!pipe->writers++)
988 wake_up_partner(pipe);
990 if (!is_pipe && !pipe->readers) {
991 if (wait_for_partner(pipe, &pipe->r_counter))
996 case FMODE_READ | FMODE_WRITE:
999 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
1000 * This implementation will NEVER block on a O_RDWR open, since
1001 * the process can at least talk to itself.
1008 if (pipe->readers == 1 || pipe->writers == 1)
1009 wake_up_partner(pipe);
1018 __pipe_unlock(pipe);
1022 if (!--pipe->readers)
1023 wake_up_interruptible(&pipe->wait);
1028 if (!--pipe->writers)
1029 wake_up_interruptible(&pipe->wait);
1034 __pipe_unlock(pipe);
1036 put_pipe_info(inode, pipe);
1040 const struct file_operations pipefifo_fops = {
1042 .llseek = no_llseek,
1043 .read_iter = pipe_read,
1044 .write_iter = pipe_write,
1046 .unlocked_ioctl = pipe_ioctl,
1047 .release = pipe_release,
1048 .fasync = pipe_fasync,
1052 * Currently we rely on the pipe array holding a power-of-2 number
1053 * of pages. Returns 0 on error.
1055 unsigned int round_pipe_size(unsigned long size)
1057 if (size > (1U << 31))
1060 /* Minimum pipe size, as required by POSIX */
1061 if (size < PAGE_SIZE)
1064 return roundup_pow_of_two(size);
1068 * Allocate a new array of pipe buffers and copy the info over. Returns the
1069 * pipe size if successful, or return -ERROR on error.
1071 static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long arg)
1073 struct pipe_buffer *bufs;
1074 unsigned int size, nr_slots, head, tail, mask, n;
1075 unsigned long user_bufs;
1078 size = round_pipe_size(arg);
1079 nr_slots = size >> PAGE_SHIFT;
1085 * If trying to increase the pipe capacity, check that an
1086 * unprivileged user is not trying to exceed various limits
1087 * (soft limit check here, hard limit check just below).
1088 * Decreasing the pipe capacity is always permitted, even
1089 * if the user is currently over a limit.
1091 if (nr_slots > pipe->ring_size &&
1092 size > pipe_max_size && !capable(CAP_SYS_RESOURCE))
1095 user_bufs = account_pipe_buffers(pipe->user, pipe->ring_size, nr_slots);
1097 if (nr_slots > pipe->ring_size &&
1098 (too_many_pipe_buffers_hard(user_bufs) ||
1099 too_many_pipe_buffers_soft(user_bufs)) &&
1100 is_unprivileged_user()) {
1102 goto out_revert_acct;
1106 * We can shrink the pipe, if arg is greater than the ring occupancy.
1107 * Since we don't expect a lot of shrink+grow operations, just free and
1108 * allocate again like we would do for growing. If the pipe currently
1109 * contains more buffers than arg, then return busy.
1111 mask = pipe->ring_size - 1;
1114 n = pipe_occupancy(pipe->head, pipe->tail);
1117 goto out_revert_acct;
1120 bufs = kcalloc(nr_slots, sizeof(*bufs),
1121 GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
1122 if (unlikely(!bufs)) {
1124 goto out_revert_acct;
1128 * The pipe array wraps around, so just start the new one at zero
1129 * and adjust the indices.
1132 unsigned int h = head & mask;
1133 unsigned int t = tail & mask;
1135 memcpy(bufs, pipe->bufs + t,
1136 n * sizeof(struct pipe_buffer));
1138 unsigned int tsize = pipe->ring_size - t;
1140 memcpy(bufs + tsize, pipe->bufs,
1141 h * sizeof(struct pipe_buffer));
1142 memcpy(bufs, pipe->bufs + t,
1143 tsize * sizeof(struct pipe_buffer));
1152 pipe->ring_size = nr_slots;
1155 return pipe->ring_size * PAGE_SIZE;
1158 (void) account_pipe_buffers(pipe->user, nr_slots, pipe->ring_size);
1163 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1164 * location, so checking ->i_pipe is not enough to verify that this is a
1167 struct pipe_inode_info *get_pipe_info(struct file *file)
1169 return file->f_op == &pipefifo_fops ? file->private_data : NULL;
1172 long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1174 struct pipe_inode_info *pipe;
1177 pipe = get_pipe_info(file);
1185 ret = pipe_set_size(pipe, arg);
1188 ret = pipe->ring_size * PAGE_SIZE;
1195 __pipe_unlock(pipe);
1199 static const struct super_operations pipefs_ops = {
1200 .destroy_inode = free_inode_nonrcu,
1201 .statfs = simple_statfs,
1205 * pipefs should _never_ be mounted by userland - too much of security hassle,
1206 * no real gain from having the whole whorehouse mounted. So we don't need
1207 * any operations on the root directory. However, we need a non-trivial
1208 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1211 static int pipefs_init_fs_context(struct fs_context *fc)
1213 struct pseudo_fs_context *ctx = init_pseudo(fc, PIPEFS_MAGIC);
1216 ctx->ops = &pipefs_ops;
1217 ctx->dops = &pipefs_dentry_operations;
1221 static struct file_system_type pipe_fs_type = {
1223 .init_fs_context = pipefs_init_fs_context,
1224 .kill_sb = kill_anon_super,
1227 static int __init init_pipe_fs(void)
1229 int err = register_filesystem(&pipe_fs_type);
1232 pipe_mnt = kern_mount(&pipe_fs_type);
1233 if (IS_ERR(pipe_mnt)) {
1234 err = PTR_ERR(pipe_mnt);
1235 unregister_filesystem(&pipe_fs_type);
1241 fs_initcall(init_pipe_fs);