Lines Matching full:pipe

3  *  linux/fs/pipe.c
36 * New pipe buffers will be restricted to this size while the user is exceeding
37 * their pipe buffer quota. The general pipe use case needs at least two
39 * than two, then a write to a non-empty pipe may block even if the pipe is not
42 * pipe before reading tokens: https://lore.kernel.org/lkml/1628086770.5rn8p04n6j.none@localhost/.
44 * Users can reduce their pipe buffers with F_SETPIPE_SZ below this at their
45 * own risk, namely: pipe writes to non-full pipes may block until the pipe is
51 * The max size that a non-root user is allowed to grow the pipe. Can
52 * be set by root in /proc/sys/fs/pipe-max-size
89 void pipe_lock(struct pipe_inode_info *pipe) in pipe_lock() argument
91 if (pipe->files) in pipe_lock()
92 mutex_lock(&pipe->mutex); in pipe_lock()
96 void pipe_unlock(struct pipe_inode_info *pipe) in pipe_unlock() argument
98 if (pipe->files) in pipe_unlock()
99 mutex_unlock(&pipe->mutex); in pipe_unlock()
115 static void anon_pipe_buf_release(struct pipe_inode_info *pipe, in anon_pipe_buf_release() argument
125 if (page_count(page) == 1 && !pipe->tmp_page) in anon_pipe_buf_release()
126 pipe->tmp_page = page; in anon_pipe_buf_release()
131 static bool anon_pipe_buf_try_steal(struct pipe_inode_info *pipe, in anon_pipe_buf_try_steal() argument
145 * @pipe: the pipe that the buffer belongs to
155 bool generic_pipe_buf_try_steal(struct pipe_inode_info *pipe, in generic_pipe_buf_try_steal() argument
175 * @pipe: the pipe that the buffer belongs to
181 * pipe into another.
183 bool generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf) in generic_pipe_buf_get() argument
191 * @pipe: the pipe that the buffer belongs to
197 void generic_pipe_buf_release(struct pipe_inode_info *pipe, in generic_pipe_buf_release() argument
210 /* Done while waiting without holding the pipe lock - thus the READ_ONCE() */
211 static inline bool pipe_readable(const struct pipe_inode_info *pipe) in pipe_readable() argument
213 unsigned int head = READ_ONCE(pipe->head); in pipe_readable()
214 unsigned int tail = READ_ONCE(pipe->tail); in pipe_readable()
215 unsigned int writers = READ_ONCE(pipe->writers); in pipe_readable()
220 static inline unsigned int pipe_update_tail(struct pipe_inode_info *pipe, in pipe_update_tail() argument
224 pipe_buf_release(pipe, buf); in pipe_update_tail()
227 * If the pipe has a watch_queue, we need additional protection in pipe_update_tail()
231 if (pipe_has_watch_queue(pipe)) { in pipe_update_tail()
232 spin_lock_irq(&pipe->rd_wait.lock); in pipe_update_tail()
235 pipe->note_loss = true; in pipe_update_tail()
237 pipe->tail = ++tail; in pipe_update_tail()
238 spin_unlock_irq(&pipe->rd_wait.lock); in pipe_update_tail()
246 pipe->tail = ++tail; in pipe_update_tail()
255 struct pipe_inode_info *pipe = filp->private_data; in pipe_read() local
264 mutex_lock(&pipe->mutex); in pipe_read()
267 * We only wake up writers if the pipe was full when we started in pipe_read()
274 was_full = pipe_full(pipe->head, pipe->tail, pipe->max_usage); in pipe_read()
277 unsigned int head = smp_load_acquire(&pipe->head); in pipe_read()
278 unsigned int tail = pipe->tail; in pipe_read()
279 unsigned int mask = pipe->ring_size - 1; in pipe_read()
282 if (pipe->note_loss) { in pipe_read()
301 pipe->note_loss = false; in pipe_read()
306 struct pipe_buffer *buf = &pipe->bufs[tail & mask]; in pipe_read()
320 error = pipe_buf_confirm(pipe, buf); in pipe_read()
344 tail = pipe_update_tail(pipe, buf, tail); in pipe_read()
352 if (!pipe->writers) in pipe_read()
361 mutex_unlock(&pipe->mutex); in pipe_read()
367 * pipe buffer, and might have made space in the buffers in pipe_read()
370 * You can't make zero-sized pipe buffers by doing an empty in pipe_read()
377 * _very_ unlikely case that the pipe was full, but we got in pipe_read()
381 wake_up_interruptible_sync_poll(&pipe->wr_wait, EPOLLOUT | EPOLLWRNORM); in pipe_read()
382 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); in pipe_read()
390 if (wait_event_interruptible_exclusive(pipe->rd_wait, pipe_readable(pipe)) < 0) in pipe_read()
393 mutex_lock(&pipe->mutex); in pipe_read()
394 was_full = pipe_full(pipe->head, pipe->tail, pipe->max_usage); in pipe_read()
397 if (pipe_empty(pipe->head, pipe->tail)) in pipe_read()
399 mutex_unlock(&pipe->mutex); in pipe_read()
402 wake_up_interruptible_sync_poll(&pipe->wr_wait, EPOLLOUT | EPOLLWRNORM); in pipe_read()
404 wake_up_interruptible_sync_poll(&pipe->rd_wait, EPOLLIN | EPOLLRDNORM); in pipe_read()
405 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); in pipe_read()
416 /* Done while waiting without holding the pipe lock - thus the READ_ONCE() */
417 static inline bool pipe_writable(const struct pipe_inode_info *pipe) in pipe_writable() argument
419 unsigned int head = READ_ONCE(pipe->head); in pipe_writable()
420 unsigned int tail = READ_ONCE(pipe->tail); in pipe_writable()
421 unsigned int max_usage = READ_ONCE(pipe->max_usage); in pipe_writable()
424 !READ_ONCE(pipe->readers); in pipe_writable()
431 struct pipe_inode_info *pipe = filp->private_data; in pipe_write() local
441 * the pipe. in pipe_write()
443 * pipe locked. in pipe_write()
444 * If we had to support locking a normal pipe and a notification pipe at in pipe_write()
448 if (pipe_has_watch_queue(pipe)) in pipe_write()
455 mutex_lock(&pipe->mutex); in pipe_write()
457 if (!pipe->readers) { in pipe_write()
471 head = pipe->head; in pipe_write()
472 was_empty = pipe_empty(head, pipe->tail); in pipe_write()
475 unsigned int mask = pipe->ring_size - 1; in pipe_write()
476 struct pipe_buffer *buf = &pipe->bufs[(head - 1) & mask]; in pipe_write()
481 ret = pipe_buf_confirm(pipe, buf); in pipe_write()
498 if (!pipe->readers) { in pipe_write()
505 head = pipe->head; in pipe_write()
506 if (!pipe_full(head, pipe->tail, pipe->max_usage)) { in pipe_write()
507 unsigned int mask = pipe->ring_size - 1; in pipe_write()
509 struct page *page = pipe->tmp_page; in pipe_write()
518 pipe->tmp_page = page; in pipe_write()
526 pipe->head = head + 1; in pipe_write()
529 buf = &pipe->bufs[head & mask]; in pipe_write()
538 pipe->tmp_page = NULL; in pipe_write()
553 if (!pipe_full(head, pipe->tail, pipe->max_usage)) in pipe_write()
570 * We're going to release the pipe lock and wait for more in pipe_write()
572 * after waiting we need to re-check whether the pipe in pipe_write()
575 mutex_unlock(&pipe->mutex); in pipe_write()
577 wake_up_interruptible_sync_poll(&pipe->rd_wait, EPOLLIN | EPOLLRDNORM); in pipe_write()
578 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); in pipe_write()
579 wait_event_interruptible_exclusive(pipe->wr_wait, pipe_writable(pipe)); in pipe_write()
580 mutex_lock(&pipe->mutex); in pipe_write()
581 was_empty = pipe_empty(pipe->head, pipe->tail); in pipe_write()
585 if (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) in pipe_write()
587 mutex_unlock(&pipe->mutex); in pipe_write()
598 * Epoll nonsensically wants a wakeup whether the pipe in pipe_write()
601 if (was_empty || pipe->poll_usage) in pipe_write()
602 wake_up_interruptible_sync_poll(&pipe->rd_wait, EPOLLIN | EPOLLRDNORM); in pipe_write()
603 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); in pipe_write()
605 wake_up_interruptible_sync_poll(&pipe->wr_wait, EPOLLOUT | EPOLLWRNORM); in pipe_write()
617 struct pipe_inode_info *pipe = filp->private_data; in pipe_ioctl() local
622 mutex_lock(&pipe->mutex); in pipe_ioctl()
624 head = pipe->head; in pipe_ioctl()
625 tail = pipe->tail; in pipe_ioctl()
626 mask = pipe->ring_size - 1; in pipe_ioctl()
629 count += pipe->bufs[tail & mask].len; in pipe_ioctl()
632 mutex_unlock(&pipe->mutex); in pipe_ioctl()
639 mutex_lock(&pipe->mutex); in pipe_ioctl()
640 ret = watch_queue_set_size(pipe, arg); in pipe_ioctl()
641 mutex_unlock(&pipe->mutex); in pipe_ioctl()
647 pipe, (struct watch_notification_filter __user *)arg); in pipe_ioctl()
660 struct pipe_inode_info *pipe = filp->private_data; in pipe_poll() local
664 WRITE_ONCE(pipe->poll_usage, true); in pipe_poll()
667 * Reading pipe state only -- no need for acquiring the semaphore. in pipe_poll()
673 poll_wait(filp, &pipe->rd_wait, wait); in pipe_poll()
675 poll_wait(filp, &pipe->wr_wait, wait); in pipe_poll()
682 head = READ_ONCE(pipe->head); in pipe_poll()
683 tail = READ_ONCE(pipe->tail); in pipe_poll()
689 if (!pipe->writers && filp->f_pipe != pipe->w_counter) in pipe_poll()
694 if (!pipe_full(head, tail, pipe->max_usage)) in pipe_poll()
700 if (!pipe->readers) in pipe_poll()
707 static void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe) in put_pipe_info() argument
712 if (!--pipe->files) { in put_pipe_info()
719 free_pipe_info(pipe); in put_pipe_info()
725 struct pipe_inode_info *pipe = file->private_data; in pipe_release() local
727 mutex_lock(&pipe->mutex); in pipe_release()
729 pipe->readers--; in pipe_release()
731 pipe->writers--; in pipe_release()
734 if (!pipe->readers != !pipe->writers) { in pipe_release()
735 wake_up_interruptible_all(&pipe->rd_wait); in pipe_release()
736 wake_up_interruptible_all(&pipe->wr_wait); in pipe_release()
737 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); in pipe_release()
738 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); in pipe_release()
740 mutex_unlock(&pipe->mutex); in pipe_release()
742 put_pipe_info(inode, pipe); in pipe_release()
749 struct pipe_inode_info *pipe = filp->private_data; in pipe_fasync() local
752 mutex_lock(&pipe->mutex); in pipe_fasync()
754 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers); in pipe_fasync()
756 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers); in pipe_fasync()
759 fasync_helper(-1, filp, 0, &pipe->fasync_readers); in pipe_fasync()
761 mutex_unlock(&pipe->mutex); in pipe_fasync()
792 struct pipe_inode_info *pipe; in alloc_pipe_info() local
798 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL_ACCOUNT); in alloc_pipe_info()
799 if (pipe == NULL) in alloc_pipe_info()
815 pipe->bufs = kcalloc(pipe_bufs, sizeof(struct pipe_buffer), in alloc_pipe_info()
818 if (pipe->bufs) { in alloc_pipe_info()
819 init_waitqueue_head(&pipe->rd_wait); in alloc_pipe_info()
820 init_waitqueue_head(&pipe->wr_wait); in alloc_pipe_info()
821 pipe->r_counter = pipe->w_counter = 1; in alloc_pipe_info()
822 pipe->max_usage = pipe_bufs; in alloc_pipe_info()
823 pipe->ring_size = pipe_bufs; in alloc_pipe_info()
824 pipe->nr_accounted = pipe_bufs; in alloc_pipe_info()
825 pipe->user = user; in alloc_pipe_info()
826 mutex_init(&pipe->mutex); in alloc_pipe_info()
827 lock_set_cmp_fn(&pipe->mutex, pipe_lock_cmp_fn, NULL); in alloc_pipe_info()
828 return pipe; in alloc_pipe_info()
833 kfree(pipe); in alloc_pipe_info()
839 void free_pipe_info(struct pipe_inode_info *pipe) in free_pipe_info() argument
844 if (pipe->watch_queue) in free_pipe_info()
845 watch_queue_clear(pipe->watch_queue); in free_pipe_info()
848 (void) account_pipe_buffers(pipe->user, pipe->nr_accounted, 0); in free_pipe_info()
849 free_uid(pipe->user); in free_pipe_info()
850 for (i = 0; i < pipe->ring_size; i++) { in free_pipe_info()
851 struct pipe_buffer *buf = pipe->bufs + i; in free_pipe_info()
853 pipe_buf_release(pipe, buf); in free_pipe_info()
856 if (pipe->watch_queue) in free_pipe_info()
857 put_watch_queue(pipe->watch_queue); in free_pipe_info()
859 if (pipe->tmp_page) in free_pipe_info()
860 __free_page(pipe->tmp_page); in free_pipe_info()
861 kfree(pipe->bufs); in free_pipe_info()
862 kfree(pipe); in free_pipe_info()
872 return dynamic_dname(buffer, buflen, "pipe:[%lu]", in pipefs_dname()
883 struct pipe_inode_info *pipe; in get_pipe_inode() local
890 pipe = alloc_pipe_info(); in get_pipe_inode()
891 if (!pipe) in get_pipe_inode()
894 inode->i_pipe = pipe; in get_pipe_inode()
895 pipe->files = 2; in get_pipe_inode()
896 pipe->readers = pipe->writers = 1; in get_pipe_inode()
990 /* pipe groks IOCB_NOWAIT */ in __do_pipe_flags()
1016 * a pipe. It's not the way Unix traditionally does this, though.
1045 SYSCALL_DEFINE1(pipe, int __user *, fildes) in SYSCALL_DEFINE1() argument
1051 * This is the stupid "wait for pipe to be readable or writable"
1059 void pipe_wait_readable(struct pipe_inode_info *pipe) in pipe_wait_readable() argument
1061 pipe_unlock(pipe); in pipe_wait_readable()
1062 wait_event_interruptible(pipe->rd_wait, pipe_readable(pipe)); in pipe_wait_readable()
1063 pipe_lock(pipe); in pipe_wait_readable()
1066 void pipe_wait_writable(struct pipe_inode_info *pipe) in pipe_wait_writable() argument
1068 pipe_unlock(pipe); in pipe_wait_writable()
1069 wait_event_interruptible(pipe->wr_wait, pipe_writable(pipe)); in pipe_wait_writable()
1070 pipe_lock(pipe); in pipe_wait_writable()
1075 * holding the pipe lock, so "*cnt" is stable and we know a wakeup cannot
1080 * because of the pipe lock, we can check the condition before being on
1083 * We use the 'rd_wait' waitqueue for pipe partner waiting.
1085 static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt) in wait_for_partner() argument
1091 prepare_to_wait(&pipe->rd_wait, &rdwait, TASK_INTERRUPTIBLE); in wait_for_partner()
1092 pipe_unlock(pipe); in wait_for_partner()
1094 finish_wait(&pipe->rd_wait, &rdwait); in wait_for_partner()
1095 pipe_lock(pipe); in wait_for_partner()
1102 static void wake_up_partner(struct pipe_inode_info *pipe) in wake_up_partner() argument
1104 wake_up_interruptible_all(&pipe->rd_wait); in wake_up_partner()
1109 struct pipe_inode_info *pipe; in fifo_open() local
1117 pipe = inode->i_pipe; in fifo_open()
1118 pipe->files++; in fifo_open()
1122 pipe = alloc_pipe_info(); in fifo_open()
1123 if (!pipe) in fifo_open()
1125 pipe->files = 1; in fifo_open()
1130 free_pipe_info(pipe); in fifo_open()
1131 pipe = inode->i_pipe; in fifo_open()
1133 inode->i_pipe = pipe; in fifo_open()
1137 filp->private_data = pipe; in fifo_open()
1138 /* OK, we have a pipe and it's pinned down */ in fifo_open()
1140 mutex_lock(&pipe->mutex); in fifo_open()
1152 pipe->r_counter++; in fifo_open()
1153 if (pipe->readers++ == 0) in fifo_open()
1154 wake_up_partner(pipe); in fifo_open()
1156 if (!is_pipe && !pipe->writers) { in fifo_open()
1160 filp->f_pipe = pipe->w_counter; in fifo_open()
1162 if (wait_for_partner(pipe, &pipe->w_counter)) in fifo_open()
1175 if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers) in fifo_open()
1178 pipe->w_counter++; in fifo_open()
1179 if (!pipe->writers++) in fifo_open()
1180 wake_up_partner(pipe); in fifo_open()
1182 if (!is_pipe && !pipe->readers) { in fifo_open()
1183 if (wait_for_partner(pipe, &pipe->r_counter)) in fifo_open()
1196 pipe->readers++; in fifo_open()
1197 pipe->writers++; in fifo_open()
1198 pipe->r_counter++; in fifo_open()
1199 pipe->w_counter++; in fifo_open()
1200 if (pipe->readers == 1 || pipe->writers == 1) in fifo_open()
1201 wake_up_partner(pipe); in fifo_open()
1210 mutex_unlock(&pipe->mutex); in fifo_open()
1214 if (!--pipe->readers) in fifo_open()
1215 wake_up_interruptible(&pipe->wr_wait); in fifo_open()
1220 if (!--pipe->writers) in fifo_open()
1221 wake_up_interruptible_all(&pipe->rd_wait); in fifo_open()
1226 mutex_unlock(&pipe->mutex); in fifo_open()
1228 put_pipe_info(inode, pipe); in fifo_open()
1244 * Currently we rely on the pipe array holding a power-of-2 number
1252 /* Minimum pipe size, as required by POSIX */ in round_pipe_size()
1260 * Resize the pipe ring to a number of slots.
1262 * Note the pipe can be reduced in capacity, but only if the current
1266 int pipe_resize_ring(struct pipe_inode_info *pipe, unsigned int nr_slots) in pipe_resize_ring() argument
1276 spin_lock_irq(&pipe->rd_wait.lock); in pipe_resize_ring()
1277 mask = pipe->ring_size - 1; in pipe_resize_ring()
1278 head = pipe->head; in pipe_resize_ring()
1279 tail = pipe->tail; in pipe_resize_ring()
1283 spin_unlock_irq(&pipe->rd_wait.lock); in pipe_resize_ring()
1289 * The pipe array wraps around, so just start the new one at zero in pipe_resize_ring()
1296 memcpy(bufs, pipe->bufs + t, in pipe_resize_ring()
1299 unsigned int tsize = pipe->ring_size - t; in pipe_resize_ring()
1301 memcpy(bufs + tsize, pipe->bufs, in pipe_resize_ring()
1303 memcpy(bufs, pipe->bufs + t, in pipe_resize_ring()
1311 kfree(pipe->bufs); in pipe_resize_ring()
1312 pipe->bufs = bufs; in pipe_resize_ring()
1313 pipe->ring_size = nr_slots; in pipe_resize_ring()
1314 if (pipe->max_usage > nr_slots) in pipe_resize_ring()
1315 pipe->max_usage = nr_slots; in pipe_resize_ring()
1316 pipe->tail = tail; in pipe_resize_ring()
1317 pipe->head = head; in pipe_resize_ring()
1319 if (!pipe_has_watch_queue(pipe)) { in pipe_resize_ring()
1320 pipe->max_usage = nr_slots; in pipe_resize_ring()
1321 pipe->nr_accounted = nr_slots; in pipe_resize_ring()
1324 spin_unlock_irq(&pipe->rd_wait.lock); in pipe_resize_ring()
1327 wake_up_interruptible(&pipe->wr_wait); in pipe_resize_ring()
1332 * Allocate a new array of pipe buffers and copy the info over. Returns the
1333 * pipe size if successful, or return -ERROR on error.
1335 static long pipe_set_size(struct pipe_inode_info *pipe, unsigned int arg) in pipe_set_size() argument
1341 if (pipe_has_watch_queue(pipe)) in pipe_set_size()
1351 * If trying to increase the pipe capacity, check that an in pipe_set_size()
1354 * Decreasing the pipe capacity is always permitted, even in pipe_set_size()
1357 if (nr_slots > pipe->max_usage && in pipe_set_size()
1361 user_bufs = account_pipe_buffers(pipe->user, pipe->nr_accounted, nr_slots); in pipe_set_size()
1363 if (nr_slots > pipe->max_usage && in pipe_set_size()
1371 ret = pipe_resize_ring(pipe, nr_slots); in pipe_set_size()
1375 return pipe->max_usage * PAGE_SIZE; in pipe_set_size()
1378 (void) account_pipe_buffers(pipe->user, nr_slots, pipe->nr_accounted); in pipe_set_size()
1384 * not enough to verify that this is a pipe.
1388 struct pipe_inode_info *pipe = file->private_data; in get_pipe_info() local
1390 if (file->f_op != &pipefifo_fops || !pipe) in get_pipe_info()
1392 if (for_splice && pipe_has_watch_queue(pipe)) in get_pipe_info()
1394 return pipe; in get_pipe_info()
1399 struct pipe_inode_info *pipe; in pipe_fcntl() local
1402 pipe = get_pipe_info(file, false); in pipe_fcntl()
1403 if (!pipe) in pipe_fcntl()
1406 mutex_lock(&pipe->mutex); in pipe_fcntl()
1410 ret = pipe_set_size(pipe, arg); in pipe_fcntl()
1413 ret = pipe->max_usage * PAGE_SIZE; in pipe_fcntl()
1420 mutex_unlock(&pipe->mutex); in pipe_fcntl()
1433 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1482 .procname = "pipe-max-size",
1489 .procname = "pipe-user-pages-hard",
1496 .procname = "pipe-user-pages-soft",