blob: 60dbee457143674cc01651e03f213f53776aeaa1 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/pipe.c
4 *
5 * Copyright (C) 1991, 1992, 1999 Linus Torvalds
6 */
7
8#include <linux/mm.h>
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>
14#include <linux/fs.h>
Jens Axboe35f3d142010-05-20 10:43:18 +020015#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/mount.h>
David Howells4fa7ec52019-03-25 16:38:23 +000017#include <linux/pseudo_fs.h>
Muthu Kumarb502bd12012-03-23 15:01:50 -070018#include <linux/magic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/pipe_fs_i.h>
20#include <linux/uio.h>
21#include <linux/highmem.h>
Jens Axboe5274f052006-03-30 15:15:30 +020022#include <linux/pagemap.h>
Al Virodb349502007-02-07 01:48:00 -050023#include <linux/audit.h>
Ulrich Drepperba719ba2008-05-06 20:42:38 -070024#include <linux/syscalls.h>
Jens Axboeb492e952010-05-19 21:03:16 +020025#include <linux/fcntl.h>
Vladimir Davydovd86133b2016-07-26 15:24:33 -070026#include <linux/memcontrol.h>
David Howellsc73be612020-01-14 17:07:11 +000027#include <linux/watch_queue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080029#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/ioctls.h>
31
Al Viro599a0ac2013-03-12 09:58:10 -040032#include "internal.h"
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/*
Jens Axboeb492e952010-05-19 21:03:16 +020035 * The max size that a non-root user is allowed to grow the pipe. Can
Jens Axboeff9da692010-06-03 14:54:39 +020036 * be set by root in /proc/sys/fs/pipe-max-size
Jens Axboeb492e952010-05-19 21:03:16 +020037 */
Jens Axboeff9da692010-06-03 14:54:39 +020038unsigned int pipe_max_size = 1048576;
39
Willy Tarreau759c0112016-01-18 16:36:09 +010040/* Maximum allocatable pages per user. Hard limit is unset by default, soft
41 * matches default values.
42 */
43unsigned long pipe_user_pages_hard;
44unsigned long pipe_user_pages_soft = PIPE_DEF_BUFFERS * INR_OPEN_CUR;
45
Jens Axboeb492e952010-05-19 21:03:16 +020046/*
David Howells8cefc102019-11-15 13:30:32 +000047 * We use head and tail indices that aren't masked off, except at the point of
48 * dereference, but rather they're allowed to wrap naturally. This means there
49 * isn't a dead spot in the buffer, but the ring has to be a power of two and
50 * <= 2^31.
51 * -- David Howells 2019-09-23.
52 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 * Reads with count = 0 should always return 0.
54 * -- Julian Bradfield 1999-06-07.
55 *
56 * FIFOs and Pipes now generate SIGIO for both readers and writers.
57 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
58 *
59 * pipe_read & write cleanup
60 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
61 */
62
Miklos Szeredi61e0d472009-04-14 19:48:41 +020063static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass)
64{
Al Viro6447a3c2013-03-21 11:01:38 -040065 if (pipe->files)
Al Viro72b0d9a2013-03-21 02:32:24 -040066 mutex_lock_nested(&pipe->mutex, subclass);
Miklos Szeredi61e0d472009-04-14 19:48:41 +020067}
68
69void pipe_lock(struct pipe_inode_info *pipe)
70{
71 /*
72 * pipe_lock() nests non-pipe inode locks (for writing to a file)
73 */
74 pipe_lock_nested(pipe, I_MUTEX_PARENT);
75}
76EXPORT_SYMBOL(pipe_lock);
77
78void pipe_unlock(struct pipe_inode_info *pipe)
79{
Al Viro6447a3c2013-03-21 11:01:38 -040080 if (pipe->files)
Al Viro72b0d9a2013-03-21 02:32:24 -040081 mutex_unlock(&pipe->mutex);
Miklos Szeredi61e0d472009-04-14 19:48:41 +020082}
83EXPORT_SYMBOL(pipe_unlock);
84
Al Viroebec73f2013-03-21 12:24:01 -040085static inline void __pipe_lock(struct pipe_inode_info *pipe)
86{
87 mutex_lock_nested(&pipe->mutex, I_MUTEX_PARENT);
88}
89
90static inline void __pipe_unlock(struct pipe_inode_info *pipe)
91{
92 mutex_unlock(&pipe->mutex);
93}
94
Miklos Szeredi61e0d472009-04-14 19:48:41 +020095void pipe_double_lock(struct pipe_inode_info *pipe1,
96 struct pipe_inode_info *pipe2)
97{
98 BUG_ON(pipe1 == pipe2);
99
100 if (pipe1 < pipe2) {
101 pipe_lock_nested(pipe1, I_MUTEX_PARENT);
102 pipe_lock_nested(pipe2, I_MUTEX_CHILD);
103 } else {
Peter Zijlstra023d43c2009-07-21 10:09:23 +0200104 pipe_lock_nested(pipe2, I_MUTEX_PARENT);
105 pipe_lock_nested(pipe1, I_MUTEX_CHILD);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200106 }
107}
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/* Drop the inode semaphore and wait for a pipe event, atomically */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200110void pipe_wait(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Linus Torvalds0ddad212019-12-09 09:48:27 -0800112 DEFINE_WAIT(rdwait);
113 DEFINE_WAIT(wrwait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Ingo Molnard79fc0f2005-09-10 00:26:12 -0700115 /*
116 * Pipes are system-local resources, so sleeping on them
117 * is considered a noninteractive wait:
118 */
Linus Torvalds0ddad212019-12-09 09:48:27 -0800119 prepare_to_wait(&pipe->rd_wait, &rdwait, TASK_INTERRUPTIBLE);
120 prepare_to_wait(&pipe->wr_wait, &wrwait, TASK_INTERRUPTIBLE);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200121 pipe_unlock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 schedule();
Linus Torvalds0ddad212019-12-09 09:48:27 -0800123 finish_wait(&pipe->rd_wait, &rdwait);
124 finish_wait(&pipe->wr_wait, &wrwait);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200125 pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Ingo Molnar341b4462006-04-11 13:57:45 +0200128static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
129 struct pipe_buffer *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 struct page *page = buf->page;
132
Jens Axboe5274f052006-03-30 15:15:30 +0200133 /*
134 * If nobody else uses this page, and we don't already have a
135 * temporary page, let's keep track of it as a one-deep
Ingo Molnar341b4462006-04-11 13:57:45 +0200136 * allocation cache. (Otherwise just release our reference to it)
Jens Axboe5274f052006-03-30 15:15:30 +0200137 */
Ingo Molnar341b4462006-04-11 13:57:45 +0200138 if (page_count(page) == 1 && !pipe->tmp_page)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200139 pipe->tmp_page = page;
Ingo Molnar341b4462006-04-11 13:57:45 +0200140 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300141 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
Christoph Hellwigc928f642020-05-20 17:58:16 +0200144static bool anon_pipe_buf_try_steal(struct pipe_inode_info *pipe,
145 struct pipe_buffer *buf)
Vladimir Davydovd86133b2016-07-26 15:24:33 -0700146{
147 struct page *page = buf->page;
148
Christoph Hellwigc928f642020-05-20 17:58:16 +0200149 if (page_count(page) != 1)
150 return false;
151 memcg_kmem_uncharge_page(page, 0);
152 __SetPageLocked(page);
153 return true;
Vladimir Davydovd86133b2016-07-26 15:24:33 -0700154}
155
Jens Axboe08457182007-06-12 20:51:32 +0200156/**
Christoph Hellwigc928f642020-05-20 17:58:16 +0200157 * generic_pipe_buf_try_steal - attempt to take ownership of a &pipe_buffer
Jens Axboe08457182007-06-12 20:51:32 +0200158 * @pipe: the pipe that the buffer belongs to
159 * @buf: the buffer to attempt to steal
160 *
161 * Description:
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800162 * This function attempts to steal the &struct page attached to
Jens Axboe08457182007-06-12 20:51:32 +0200163 * @buf. If successful, this function returns 0 and returns with
164 * the page locked. The caller may then reuse the page for whatever
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800165 * he wishes; the typical use is insertion into a different file
Jens Axboe08457182007-06-12 20:51:32 +0200166 * page cache.
167 */
Christoph Hellwigc928f642020-05-20 17:58:16 +0200168bool generic_pipe_buf_try_steal(struct pipe_inode_info *pipe,
169 struct pipe_buffer *buf)
Jens Axboe5abc97a2006-03-30 15:16:46 +0200170{
Jens Axboe46e678c2006-04-30 16:36:32 +0200171 struct page *page = buf->page;
172
Jens Axboe08457182007-06-12 20:51:32 +0200173 /*
174 * A reference of one is golden, that means that the owner of this
175 * page is the only one holding a reference to it. lock the page
176 * and return OK.
177 */
Jens Axboe46e678c2006-04-30 16:36:32 +0200178 if (page_count(page) == 1) {
Jens Axboe46e678c2006-04-30 16:36:32 +0200179 lock_page(page);
Christoph Hellwigc928f642020-05-20 17:58:16 +0200180 return true;
Jens Axboe46e678c2006-04-30 16:36:32 +0200181 }
Christoph Hellwigc928f642020-05-20 17:58:16 +0200182 return false;
Jens Axboe5abc97a2006-03-30 15:16:46 +0200183}
Christoph Hellwigc928f642020-05-20 17:58:16 +0200184EXPORT_SYMBOL(generic_pipe_buf_try_steal);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200185
Jens Axboe08457182007-06-12 20:51:32 +0200186/**
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800187 * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
Jens Axboe08457182007-06-12 20:51:32 +0200188 * @pipe: the pipe that the buffer belongs to
189 * @buf: the buffer to get a reference to
190 *
191 * Description:
192 * This function grabs an extra reference to @buf. It's used in
193 * in the tee() system call, when we duplicate the buffers in one
194 * pipe into another.
195 */
Matthew Wilcox15fab632019-04-05 14:02:10 -0700196bool generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
Jens Axboe70524492006-04-11 15:51:17 +0200197{
Matthew Wilcox15fab632019-04-05 14:02:10 -0700198 return try_get_page(buf->page);
Jens Axboe70524492006-04-11 15:51:17 +0200199}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200200EXPORT_SYMBOL(generic_pipe_buf_get);
Jens Axboe70524492006-04-11 15:51:17 +0200201
Jens Axboe08457182007-06-12 20:51:32 +0200202/**
Miklos Szeredi68181732009-05-07 15:37:36 +0200203 * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
204 * @pipe: the pipe that the buffer belongs to
205 * @buf: the buffer to put a reference to
206 *
207 * Description:
208 * This function releases a reference to @buf.
209 */
210void generic_pipe_buf_release(struct pipe_inode_info *pipe,
211 struct pipe_buffer *buf)
212{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300213 put_page(buf->page);
Miklos Szeredi68181732009-05-07 15:37:36 +0200214}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200215EXPORT_SYMBOL(generic_pipe_buf_release);
Miklos Szeredi68181732009-05-07 15:37:36 +0200216
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800217static const struct pipe_buf_operations anon_pipe_buf_ops = {
Christoph Hellwigc928f642020-05-20 17:58:16 +0200218 .release = anon_pipe_buf_release,
219 .try_steal = anon_pipe_buf_try_steal,
220 .get = generic_pipe_buf_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221};
222
Linus Torvalds85190d12019-12-07 13:53:09 -0800223/* Done while waiting without holding the pipe lock - thus the READ_ONCE() */
224static inline bool pipe_readable(const struct pipe_inode_info *pipe)
225{
226 unsigned int head = READ_ONCE(pipe->head);
227 unsigned int tail = READ_ONCE(pipe->tail);
228 unsigned int writers = READ_ONCE(pipe->writers);
229
230 return !pipe_empty(head, tail) || !writers;
231}
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233static ssize_t
Al Virofb9096a2014-04-02 19:56:54 -0400234pipe_read(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Al Virofb9096a2014-04-02 19:56:54 -0400236 size_t total_len = iov_iter_count(to);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700237 struct file *filp = iocb->ki_filp;
Al Virode32ec42013-03-21 11:16:56 -0400238 struct pipe_inode_info *pipe = filp->private_data;
Linus Torvalds0ddad212019-12-09 09:48:27 -0800239 bool was_full, wake_next_reader = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 /* Null read succeeds. */
243 if (unlikely(total_len == 0))
244 return 0;
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 ret = 0;
Al Viroebec73f2013-03-21 12:24:01 -0400247 __pipe_lock(pipe);
Linus Torvaldsf467a6a2019-12-07 12:54:26 -0800248
249 /*
250 * We only wake up writers if the pipe was full when we started
251 * reading in order to avoid unnecessary wakeups.
252 *
253 * But when we do wake up writers, we do so using a sync wakeup
254 * (WF_SYNC), because we want them to get going and generate more
255 * data for us.
256 */
257 was_full = pipe_full(pipe->head, pipe->tail, pipe->max_usage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 for (;;) {
David Howells8cefc102019-11-15 13:30:32 +0000259 unsigned int head = pipe->head;
260 unsigned int tail = pipe->tail;
261 unsigned int mask = pipe->ring_size - 1;
262
David Howellse7d553d2020-01-14 17:07:12 +0000263#ifdef CONFIG_WATCH_QUEUE
264 if (pipe->note_loss) {
265 struct watch_notification n;
266
267 if (total_len < 8) {
268 if (ret == 0)
269 ret = -ENOBUFS;
270 break;
271 }
272
273 n.type = WATCH_TYPE_META;
274 n.subtype = WATCH_META_LOSS_NOTIFICATION;
275 n.info = watch_sizeof(n);
276 if (copy_to_iter(&n, sizeof(n), to) != sizeof(n)) {
277 if (ret == 0)
278 ret = -EFAULT;
279 break;
280 }
281 ret += sizeof(n);
282 total_len -= sizeof(n);
283 pipe->note_loss = false;
284 }
285#endif
286
David Howells8cefc102019-11-15 13:30:32 +0000287 if (!pipe_empty(head, tail)) {
288 struct pipe_buffer *buf = &pipe->bufs[tail & mask];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 size_t chars = buf->len;
Al Viro637b58c2014-02-03 19:11:42 -0500290 size_t written;
291 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
David Howells8cfba762020-01-14 17:07:11 +0000293 if (chars > total_len) {
294 if (buf->flags & PIPE_BUF_FLAG_WHOLE) {
295 if (ret == 0)
296 ret = -ENOBUFS;
297 break;
298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 chars = total_len;
David Howells8cfba762020-01-14 17:07:11 +0000300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Miklos Szeredifba597d2016-09-27 10:45:12 +0200302 error = pipe_buf_confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200303 if (error) {
Jens Axboe5274f052006-03-30 15:15:30 +0200304 if (!ret)
Nicolas Kaisere5953cb2010-10-21 14:56:00 +0200305 ret = error;
Jens Axboe5274f052006-03-30 15:15:30 +0200306 break;
307 }
Jens Axboef84d7512006-05-01 19:59:03 +0200308
Al Virofb9096a2014-04-02 19:56:54 -0400309 written = copy_page_to_iter(buf->page, buf->offset, chars, to);
Al Viro637b58c2014-02-03 19:11:42 -0500310 if (unlikely(written < chars)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200311 if (!ret)
Al Viro637b58c2014-02-03 19:11:42 -0500312 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 break;
314 }
315 ret += chars;
316 buf->offset += chars;
317 buf->len -= chars;
Linus Torvalds98830352012-04-29 13:12:42 -0700318
319 /* Was it a packet buffer? Clean up and exit */
320 if (buf->flags & PIPE_BUF_FLAG_PACKET) {
321 total_len = chars;
322 buf->len = 0;
323 }
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (!buf->len) {
Miklos Szeredia7796382016-09-27 10:45:12 +0200326 pipe_buf_release(pipe, buf);
Linus Torvalds0ddad212019-12-09 09:48:27 -0800327 spin_lock_irq(&pipe->rd_wait.lock);
David Howellse7d553d2020-01-14 17:07:12 +0000328#ifdef CONFIG_WATCH_QUEUE
329 if (buf->flags & PIPE_BUF_FLAG_LOSS)
330 pipe->note_loss = true;
331#endif
David Howells8cefc102019-11-15 13:30:32 +0000332 tail++;
333 pipe->tail = tail;
Linus Torvalds0ddad212019-12-09 09:48:27 -0800334 spin_unlock_irq(&pipe->rd_wait.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336 total_len -= chars;
337 if (!total_len)
338 break; /* common path: read succeeded */
David Howells8cefc102019-11-15 13:30:32 +0000339 if (!pipe_empty(head, tail)) /* More to do? */
340 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
David Howells8cefc102019-11-15 13:30:32 +0000342
Ingo Molnar923f4f22006-04-11 13:53:33 +0200343 if (!pipe->writers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 break;
Linus Torvaldsa28c8b92019-12-07 13:21:01 -0800345 if (ret)
346 break;
347 if (filp->f_flags & O_NONBLOCK) {
348 ret = -EAGAIN;
349 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
Linus Torvalds85190d12019-12-07 13:53:09 -0800351 __pipe_unlock(pipe);
Linus Torvaldsd1c6a2a2019-12-11 11:46:19 -0800352
353 /*
354 * We only get here if we didn't actually read anything.
355 *
356 * However, we could have seen (and removed) a zero-sized
357 * pipe buffer, and might have made space in the buffers
358 * that way.
359 *
360 * You can't make zero-sized pipe buffers by doing an empty
361 * write (not even in packet mode), but they can happen if
362 * the writer gets an EFAULT when trying to fill a buffer
363 * that already got allocated and inserted in the buffer
364 * array.
365 *
366 * So we still need to wake up any pending writers in the
367 * _very_ unlikely case that the pipe was full, but we got
368 * no data.
369 */
370 if (unlikely(was_full)) {
Linus Torvalds0ddad212019-12-09 09:48:27 -0800371 wake_up_interruptible_sync_poll(&pipe->wr_wait, EPOLLOUT | EPOLLWRNORM);
Linus Torvaldsf467a6a2019-12-07 12:54:26 -0800372 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
373 }
Linus Torvaldsd1c6a2a2019-12-11 11:46:19 -0800374
375 /*
376 * But because we didn't read anything, at this point we can
377 * just return directly with -ERESTARTSYS if we're interrupted,
378 * since we've done any required wakeups and there's no need
379 * to mark anything accessed. And we've dropped the lock.
380 */
Linus Torvalds0ddad212019-12-09 09:48:27 -0800381 if (wait_event_interruptible_exclusive(pipe->rd_wait, pipe_readable(pipe)) < 0)
Linus Torvaldsd1c6a2a2019-12-11 11:46:19 -0800382 return -ERESTARTSYS;
383
Linus Torvalds85190d12019-12-07 13:53:09 -0800384 __pipe_lock(pipe);
Linus Torvaldsf467a6a2019-12-07 12:54:26 -0800385 was_full = pipe_full(pipe->head, pipe->tail, pipe->max_usage);
Linus Torvalds0ddad212019-12-09 09:48:27 -0800386 wake_next_reader = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
Linus Torvalds0ddad212019-12-09 09:48:27 -0800388 if (pipe_empty(pipe->head, pipe->tail))
389 wake_next_reader = false;
Al Viroebec73f2013-03-21 12:24:01 -0400390 __pipe_unlock(pipe);
Ingo Molnar341b4462006-04-11 13:57:45 +0200391
Linus Torvaldsf467a6a2019-12-07 12:54:26 -0800392 if (was_full) {
Linus Torvalds0ddad212019-12-09 09:48:27 -0800393 wake_up_interruptible_sync_poll(&pipe->wr_wait, EPOLLOUT | EPOLLWRNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200394 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Linus Torvalds0ddad212019-12-09 09:48:27 -0800396 if (wake_next_reader)
397 wake_up_interruptible_sync_poll(&pipe->rd_wait, EPOLLIN | EPOLLRDNORM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (ret > 0)
399 file_accessed(filp);
400 return ret;
401}
402
Linus Torvalds98830352012-04-29 13:12:42 -0700403static inline int is_packetized(struct file *file)
404{
405 return (file->f_flags & O_DIRECT) != 0;
406}
407
Linus Torvalds85190d12019-12-07 13:53:09 -0800408/* Done while waiting without holding the pipe lock - thus the READ_ONCE() */
409static inline bool pipe_writable(const struct pipe_inode_info *pipe)
410{
411 unsigned int head = READ_ONCE(pipe->head);
412 unsigned int tail = READ_ONCE(pipe->tail);
413 unsigned int max_usage = READ_ONCE(pipe->max_usage);
414
415 return !pipe_full(head, tail, max_usage) ||
416 !READ_ONCE(pipe->readers);
417}
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419static ssize_t
Al Virof0d1bec2014-04-03 15:05:18 -0400420pipe_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700422 struct file *filp = iocb->ki_filp;
Al Virode32ec42013-03-21 11:16:56 -0400423 struct pipe_inode_info *pipe = filp->private_data;
David Howells8f868d62019-12-05 22:30:37 +0000424 unsigned int head;
Al Virof0d1bec2014-04-03 15:05:18 -0400425 ssize_t ret = 0;
Al Virof0d1bec2014-04-03 15:05:18 -0400426 size_t total_len = iov_iter_count(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 ssize_t chars;
Linus Torvalds1b6b26a2019-12-07 12:14:28 -0800428 bool was_empty = false;
Linus Torvalds0ddad212019-12-09 09:48:27 -0800429 bool wake_next_writer = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /* Null write succeeds. */
432 if (unlikely(total_len == 0))
433 return 0;
434
Al Viroebec73f2013-03-21 12:24:01 -0400435 __pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Ingo Molnar923f4f22006-04-11 13:53:33 +0200437 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 send_sig(SIGPIPE, current, 0);
439 ret = -EPIPE;
440 goto out;
441 }
442
David Howellsc73be612020-01-14 17:07:11 +0000443#ifdef CONFIG_WATCH_QUEUE
444 if (pipe->watch_queue) {
445 ret = -EXDEV;
446 goto out;
447 }
448#endif
449
Linus Torvalds1b6b26a2019-12-07 12:14:28 -0800450 /*
451 * Only wake up if the pipe started out empty, since
452 * otherwise there should be no readers waiting.
453 *
454 * If it wasn't empty we try to merge new data into
455 * the last buffer.
456 *
457 * That naturally merges small writes, but it also
458 * page-aligs the rest of the writes for large writes
459 * spanning multiple pages.
460 */
David Howells8cefc102019-11-15 13:30:32 +0000461 head = pipe->head;
Linus Torvalds1b6b26a2019-12-07 12:14:28 -0800462 was_empty = pipe_empty(head, pipe->tail);
463 chars = total_len & (PAGE_SIZE-1);
464 if (chars && !was_empty) {
David Howells8f868d62019-12-05 22:30:37 +0000465 unsigned int mask = pipe->ring_size - 1;
David Howells8cefc102019-11-15 13:30:32 +0000466 struct pipe_buffer *buf = &pipe->bufs[(head - 1) & mask];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 int offset = buf->offset + buf->len;
Ingo Molnar341b4462006-04-11 13:57:45 +0200468
Christoph Hellwigf6dd9752020-05-20 17:58:12 +0200469 if ((buf->flags & PIPE_BUF_FLAG_CAN_MERGE) &&
470 offset + chars <= PAGE_SIZE) {
Miklos Szeredifba597d2016-09-27 10:45:12 +0200471 ret = pipe_buf_confirm(pipe, buf);
Eric Biggers6ae08062015-10-17 16:26:09 -0500472 if (ret)
Jens Axboe5274f052006-03-30 15:15:30 +0200473 goto out;
Jens Axboef84d7512006-05-01 19:59:03 +0200474
Al Virof0d1bec2014-04-03 15:05:18 -0400475 ret = copy_page_from_iter(buf->page, offset, chars, from);
476 if (unlikely(ret < chars)) {
Eric Biggers6ae08062015-10-17 16:26:09 -0500477 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 goto out;
Jens Axboef6762b72006-05-01 20:02:05 +0200479 }
Linus Torvalds1b6b26a2019-12-07 12:14:28 -0800480
Eric Biggers6ae08062015-10-17 16:26:09 -0500481 buf->len += ret;
Al Virof0d1bec2014-04-03 15:05:18 -0400482 if (!iov_iter_count(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 goto out;
484 }
485 }
486
487 for (;;) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200488 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 send_sig(SIGPIPE, current, 0);
Ingo Molnar341b4462006-04-11 13:57:45 +0200490 if (!ret)
491 ret = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 break;
493 }
David Howells8cefc102019-11-15 13:30:32 +0000494
David Howellsa194dfe2019-09-20 16:32:19 +0100495 head = pipe->head;
David Howells8f868d62019-12-05 22:30:37 +0000496 if (!pipe_full(head, pipe->tail, pipe->max_usage)) {
497 unsigned int mask = pipe->ring_size - 1;
David Howells8cefc102019-11-15 13:30:32 +0000498 struct pipe_buffer *buf = &pipe->bufs[head & mask];
Ingo Molnar923f4f22006-04-11 13:53:33 +0200499 struct page *page = pipe->tmp_page;
Al Virof0d1bec2014-04-03 15:05:18 -0400500 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 if (!page) {
Vladimir Davydovd86133b2016-07-26 15:24:33 -0700503 page = alloc_page(GFP_HIGHUSER | __GFP_ACCOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (unlikely(!page)) {
505 ret = ret ? : -ENOMEM;
506 break;
507 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200508 pipe->tmp_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
David Howellsa194dfe2019-09-20 16:32:19 +0100510
511 /* Allocate a slot in the ring in advance and attach an
512 * empty buffer. If we fault or otherwise fail to use
513 * it, either the reader will consume it or it'll still
514 * be there for the next write.
515 */
Linus Torvalds0ddad212019-12-09 09:48:27 -0800516 spin_lock_irq(&pipe->rd_wait.lock);
David Howellsa194dfe2019-09-20 16:32:19 +0100517
518 head = pipe->head;
David Howells8f868d62019-12-05 22:30:37 +0000519 if (pipe_full(head, pipe->tail, pipe->max_usage)) {
Linus Torvalds0ddad212019-12-09 09:48:27 -0800520 spin_unlock_irq(&pipe->rd_wait.lock);
David Howells8df44122019-10-07 16:30:51 +0100521 continue;
522 }
523
David Howellsa194dfe2019-09-20 16:32:19 +0100524 pipe->head = head + 1;
Linus Torvalds0ddad212019-12-09 09:48:27 -0800525 spin_unlock_irq(&pipe->rd_wait.lock);
David Howellsa194dfe2019-09-20 16:32:19 +0100526
527 /* Insert it into the buffer array */
528 buf = &pipe->bufs[head & mask];
529 buf->page = page;
530 buf->ops = &anon_pipe_buf_ops;
531 buf->offset = 0;
532 buf->len = 0;
Christoph Hellwigf6dd9752020-05-20 17:58:12 +0200533 if (is_packetized(filp))
David Howellsa194dfe2019-09-20 16:32:19 +0100534 buf->flags = PIPE_BUF_FLAG_PACKET;
Christoph Hellwigf6dd9752020-05-20 17:58:12 +0200535 else
536 buf->flags = PIPE_BUF_FLAG_CAN_MERGE;
David Howellsa194dfe2019-09-20 16:32:19 +0100537 pipe->tmp_page = NULL;
538
Al Virof0d1bec2014-04-03 15:05:18 -0400539 copied = copy_page_from_iter(page, 0, PAGE_SIZE, from);
540 if (unlikely(copied < PAGE_SIZE && iov_iter_count(from))) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200541 if (!ret)
Al Virof0d1bec2014-04-03 15:05:18 -0400542 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 break;
544 }
Al Virof0d1bec2014-04-03 15:05:18 -0400545 ret += copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 buf->offset = 0;
Al Virof0d1bec2014-04-03 15:05:18 -0400547 buf->len = copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Al Virof0d1bec2014-04-03 15:05:18 -0400549 if (!iov_iter_count(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 break;
551 }
David Howells8cefc102019-11-15 13:30:32 +0000552
David Howells8f868d62019-12-05 22:30:37 +0000553 if (!pipe_full(head, pipe->tail, pipe->max_usage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 continue;
David Howells8cefc102019-11-15 13:30:32 +0000555
556 /* Wait for buffer space to become available. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (filp->f_flags & O_NONBLOCK) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200558 if (!ret)
559 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 break;
561 }
562 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200563 if (!ret)
564 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 break;
566 }
Linus Torvalds1b6b26a2019-12-07 12:14:28 -0800567
568 /*
569 * We're going to release the pipe lock and wait for more
570 * space. We wake up any readers if necessary, and then
571 * after waiting we need to re-check whether the pipe
572 * become empty while we dropped the lock.
573 */
Linus Torvalds85190d12019-12-07 13:53:09 -0800574 __pipe_unlock(pipe);
Linus Torvalds1b6b26a2019-12-07 12:14:28 -0800575 if (was_empty) {
Linus Torvalds0ddad212019-12-09 09:48:27 -0800576 wake_up_interruptible_sync_poll(&pipe->rd_wait, EPOLLIN | EPOLLRDNORM);
Linus Torvalds1b6b26a2019-12-07 12:14:28 -0800577 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
578 }
Linus Torvalds0ddad212019-12-09 09:48:27 -0800579 wait_event_interruptible_exclusive(pipe->wr_wait, pipe_writable(pipe));
Linus Torvalds85190d12019-12-07 13:53:09 -0800580 __pipe_lock(pipe);
Jan Stancek0dd1e372019-12-22 13:33:24 +0100581 was_empty = pipe_empty(pipe->head, pipe->tail);
Linus Torvalds0ddad212019-12-09 09:48:27 -0800582 wake_next_writer = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584out:
Linus Torvalds0ddad212019-12-09 09:48:27 -0800585 if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
586 wake_next_writer = false;
Al Viroebec73f2013-03-21 12:24:01 -0400587 __pipe_unlock(pipe);
Linus Torvalds1b6b26a2019-12-07 12:14:28 -0800588
589 /*
590 * If we do do a wakeup event, we do a 'sync' wakeup, because we
591 * want the reader to start processing things asap, rather than
592 * leave the data pending.
593 *
594 * This is particularly important for small writes, because of
595 * how (for example) the GNU make jobserver uses small writes to
596 * wake up pending jobs
597 */
598 if (was_empty) {
Linus Torvalds0ddad212019-12-09 09:48:27 -0800599 wake_up_interruptible_sync_poll(&pipe->rd_wait, EPOLLIN | EPOLLRDNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200600 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
Linus Torvalds0ddad212019-12-09 09:48:27 -0800602 if (wake_next_writer)
603 wake_up_interruptible_sync_poll(&pipe->wr_wait, EPOLLOUT | EPOLLWRNORM);
Dmitry Monakhov7e775f42014-01-23 15:55:21 -0800604 if (ret > 0 && sb_start_write_trylock(file_inode(filp)->i_sb)) {
Josef Bacikc3b2da32012-03-26 09:59:21 -0400605 int err = file_update_time(filp);
606 if (err)
607 ret = err;
Dmitry Monakhov7e775f42014-01-23 15:55:21 -0800608 sb_end_write(file_inode(filp)->i_sb);
Josef Bacikc3b2da32012-03-26 09:59:21 -0400609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return ret;
611}
612
Andi Kleend59d0b12008-02-08 04:21:23 -0800613static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Al Virode32ec42013-03-21 11:16:56 -0400615 struct pipe_inode_info *pipe = filp->private_data;
David Howells8cefc102019-11-15 13:30:32 +0000616 int count, head, tail, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 switch (cmd) {
David Howellsc73be612020-01-14 17:07:11 +0000619 case FIONREAD:
620 __pipe_lock(pipe);
621 count = 0;
622 head = pipe->head;
623 tail = pipe->tail;
624 mask = pipe->ring_size - 1;
David Howells8cefc102019-11-15 13:30:32 +0000625
David Howellsc73be612020-01-14 17:07:11 +0000626 while (tail != head) {
627 count += pipe->bufs[tail & mask].len;
628 tail++;
629 }
630 __pipe_unlock(pipe);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200631
David Howellsc73be612020-01-14 17:07:11 +0000632 return put_user(count, (int __user *)arg);
633
634#ifdef CONFIG_WATCH_QUEUE
635 case IOC_WATCH_QUEUE_SET_SIZE: {
636 int ret;
637 __pipe_lock(pipe);
638 ret = watch_queue_set_size(pipe, arg);
639 __pipe_unlock(pipe);
640 return ret;
641 }
642
643 case IOC_WATCH_QUEUE_SET_FILTER:
644 return watch_queue_set_filter(
645 pipe, (struct watch_notification_filter __user *)arg);
646#endif
647
648 default:
649 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651}
652
Christoph Hellwigdd670812017-12-31 16:42:12 +0100653/* No kernel lock held - fine */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700654static __poll_t
655pipe_poll(struct file *filp, poll_table *wait)
Christoph Hellwigdd670812017-12-31 16:42:12 +0100656{
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700657 __poll_t mask;
Christoph Hellwigdd670812017-12-31 16:42:12 +0100658 struct pipe_inode_info *pipe = filp->private_data;
Linus Torvaldsad910e32019-12-07 10:41:17 -0800659 unsigned int head, tail;
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700660
Linus Torvaldsad910e32019-12-07 10:41:17 -0800661 /*
Linus Torvalds0ddad212019-12-09 09:48:27 -0800662 * Reading pipe state only -- no need for acquiring the semaphore.
Linus Torvaldsad910e32019-12-07 10:41:17 -0800663 *
664 * But because this is racy, the code has to add the
665 * entry to the poll table _first_ ..
666 */
Linus Torvalds0ddad212019-12-09 09:48:27 -0800667 if (filp->f_mode & FMODE_READ)
668 poll_wait(filp, &pipe->rd_wait, wait);
669 if (filp->f_mode & FMODE_WRITE)
670 poll_wait(filp, &pipe->wr_wait, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Linus Torvaldsad910e32019-12-07 10:41:17 -0800672 /*
673 * .. and only then can you do the racy tests. That way,
674 * if something changes and you got it wrong, the poll
675 * table entry will wake you up and fix it.
676 */
677 head = READ_ONCE(pipe->head);
678 tail = READ_ONCE(pipe->tail);
679
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700680 mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (filp->f_mode & FMODE_READ) {
David Howells8cefc102019-11-15 13:30:32 +0000682 if (!pipe_empty(head, tail))
683 mask |= EPOLLIN | EPOLLRDNORM;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200684 if (!pipe->writers && filp->f_version != pipe->w_counter)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800685 mask |= EPOLLHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687
688 if (filp->f_mode & FMODE_WRITE) {
David Howells6718b6f2019-10-16 16:47:32 +0100689 if (!pipe_full(head, tail, pipe->max_usage))
David Howells8cefc102019-11-15 13:30:32 +0000690 mask |= EPOLLOUT | EPOLLWRNORM;
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700691 /*
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800692 * Most Unices do not set EPOLLERR for FIFOs but on Linux they
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700693 * behave exactly like pipes for poll().
694 */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200695 if (!pipe->readers)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800696 mask |= EPOLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698
699 return mask;
700}
701
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800702static void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe)
703{
704 int kill = 0;
705
706 spin_lock(&inode->i_lock);
707 if (!--pipe->files) {
708 inode->i_pipe = NULL;
709 kill = 1;
710 }
711 spin_unlock(&inode->i_lock);
712
713 if (kill)
714 free_pipe_info(pipe);
715}
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717static int
Al Viro599a0ac2013-03-12 09:58:10 -0400718pipe_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800720 struct pipe_inode_info *pipe = file->private_data;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200721
Al Viroebec73f2013-03-21 12:24:01 -0400722 __pipe_lock(pipe);
Al Viro599a0ac2013-03-12 09:58:10 -0400723 if (file->f_mode & FMODE_READ)
724 pipe->readers--;
725 if (file->f_mode & FMODE_WRITE)
726 pipe->writers--;
Ingo Molnar341b4462006-04-11 13:57:45 +0200727
Linus Torvalds6551d5c2020-02-18 10:12:58 -0800728 /* Was that the last reader or writer, but not the other side? */
729 if (!pipe->readers != !pipe->writers) {
730 wake_up_interruptible_all(&pipe->rd_wait);
731 wake_up_interruptible_all(&pipe->wr_wait);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200732 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
733 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Al Viroebec73f2013-03-21 12:24:01 -0400735 __pipe_unlock(pipe);
Al Viroba5bb142013-03-21 02:21:19 -0400736
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800737 put_pipe_info(inode, pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return 0;
739}
740
741static int
Al Viro599a0ac2013-03-12 09:58:10 -0400742pipe_fasync(int fd, struct file *filp, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Al Virode32ec42013-03-21 11:16:56 -0400744 struct pipe_inode_info *pipe = filp->private_data;
Al Viro599a0ac2013-03-12 09:58:10 -0400745 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Al Viroebec73f2013-03-21 12:24:01 -0400747 __pipe_lock(pipe);
Al Viro599a0ac2013-03-12 09:58:10 -0400748 if (filp->f_mode & FMODE_READ)
749 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
750 if ((filp->f_mode & FMODE_WRITE) && retval >= 0) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200751 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
Al Viro599a0ac2013-03-12 09:58:10 -0400752 if (retval < 0 && (filp->f_mode & FMODE_READ))
753 /* this can happen only if on == T */
Oleg Nesterove5bc49b2009-03-12 14:31:28 -0700754 fasync_helper(-1, filp, 0, &pipe->fasync_readers);
755 }
Al Viroebec73f2013-03-21 12:24:01 -0400756 __pipe_unlock(pipe);
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700757 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
David Howellsc73be612020-01-14 17:07:11 +0000760unsigned long account_pipe_buffers(struct user_struct *user,
761 unsigned long old, unsigned long new)
Willy Tarreau759c0112016-01-18 16:36:09 +0100762{
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700763 return atomic_long_add_return(new - old, &user->pipe_bufs);
Willy Tarreau759c0112016-01-18 16:36:09 +0100764}
765
David Howellsc73be612020-01-14 17:07:11 +0000766bool too_many_pipe_buffers_soft(unsigned long user_bufs)
Willy Tarreau759c0112016-01-18 16:36:09 +0100767{
Eric Biggersf7340762018-02-06 15:42:08 -0800768 unsigned long soft_limit = READ_ONCE(pipe_user_pages_soft);
769
770 return soft_limit && user_bufs > soft_limit;
Willy Tarreau759c0112016-01-18 16:36:09 +0100771}
772
David Howellsc73be612020-01-14 17:07:11 +0000773bool too_many_pipe_buffers_hard(unsigned long user_bufs)
Willy Tarreau759c0112016-01-18 16:36:09 +0100774{
Eric Biggersf7340762018-02-06 15:42:08 -0800775 unsigned long hard_limit = READ_ONCE(pipe_user_pages_hard);
776
777 return hard_limit && user_bufs > hard_limit;
Willy Tarreau759c0112016-01-18 16:36:09 +0100778}
779
David Howellsc73be612020-01-14 17:07:11 +0000780bool pipe_is_unprivileged_user(void)
Eric Biggers85c2dd52018-02-06 15:41:53 -0800781{
782 return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
783}
784
Al Viro7bee1302013-03-21 11:04:15 -0400785struct pipe_inode_info *alloc_pipe_info(void)
Ingo Molnar3a326a22006-04-10 15:18:35 +0200786{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200787 struct pipe_inode_info *pipe;
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700788 unsigned long pipe_bufs = PIPE_DEF_BUFFERS;
789 struct user_struct *user = get_current_user();
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700790 unsigned long user_bufs;
Eric Biggersf7340762018-02-06 15:42:08 -0800791 unsigned int max_size = READ_ONCE(pipe_max_size);
Ingo Molnar3a326a22006-04-10 15:18:35 +0200792
Vladimir Davydovd86133b2016-07-26 15:24:33 -0700793 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL_ACCOUNT);
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700794 if (pipe == NULL)
795 goto out_free_uid;
Willy Tarreau759c0112016-01-18 16:36:09 +0100796
Eric Biggersf7340762018-02-06 15:42:08 -0800797 if (pipe_bufs * PAGE_SIZE > max_size && !capable(CAP_SYS_RESOURCE))
798 pipe_bufs = max_size >> PAGE_SHIFT;
Michael Kerrisk (man-pages)086e7742016-10-11 13:53:43 -0700799
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700800 user_bufs = account_pipe_buffers(user, 0, pipe_bufs);
Michael Kerrisk (man-pages)a005ca02016-10-11 13:53:37 -0700801
David Howellsc73be612020-01-14 17:07:11 +0000802 if (too_many_pipe_buffers_soft(user_bufs) && pipe_is_unprivileged_user()) {
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700803 user_bufs = account_pipe_buffers(user, pipe_bufs, 1);
Michael Kerrisk (man-pages)a005ca02016-10-11 13:53:37 -0700804 pipe_bufs = 1;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200805 }
806
David Howellsc73be612020-01-14 17:07:11 +0000807 if (too_many_pipe_buffers_hard(user_bufs) && pipe_is_unprivileged_user())
Michael Kerrisk (man-pages)a005ca02016-10-11 13:53:37 -0700808 goto out_revert_acct;
809
810 pipe->bufs = kcalloc(pipe_bufs, sizeof(struct pipe_buffer),
811 GFP_KERNEL_ACCOUNT);
812
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700813 if (pipe->bufs) {
Linus Torvalds0ddad212019-12-09 09:48:27 -0800814 init_waitqueue_head(&pipe->rd_wait);
815 init_waitqueue_head(&pipe->wr_wait);
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700816 pipe->r_counter = pipe->w_counter = 1;
David Howells6718b6f2019-10-16 16:47:32 +0100817 pipe->max_usage = pipe_bufs;
David Howells8cefc102019-11-15 13:30:32 +0000818 pipe->ring_size = pipe_bufs;
David Howellsc73be612020-01-14 17:07:11 +0000819 pipe->nr_accounted = pipe_bufs;
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700820 pipe->user = user;
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700821 mutex_init(&pipe->mutex);
822 return pipe;
823 }
824
Michael Kerrisk (man-pages)a005ca02016-10-11 13:53:37 -0700825out_revert_acct:
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700826 (void) account_pipe_buffers(user, pipe_bufs, 0);
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700827 kfree(pipe);
828out_free_uid:
829 free_uid(user);
Jens Axboe35f3d142010-05-20 10:43:18 +0200830 return NULL;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200831}
832
Al Viro4b8a8f12013-03-21 11:06:46 -0400833void free_pipe_info(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
835 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
David Howellsc73be612020-01-14 17:07:11 +0000837#ifdef CONFIG_WATCH_QUEUE
838 if (pipe->watch_queue) {
839 watch_queue_clear(pipe->watch_queue);
840 put_watch_queue(pipe->watch_queue);
841 }
842#endif
843
844 (void) account_pipe_buffers(pipe->user, pipe->nr_accounted, 0);
Willy Tarreau759c0112016-01-18 16:36:09 +0100845 free_uid(pipe->user);
David Howells8cefc102019-11-15 13:30:32 +0000846 for (i = 0; i < pipe->ring_size; i++) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200847 struct pipe_buffer *buf = pipe->bufs + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (buf->ops)
Miklos Szeredia7796382016-09-27 10:45:12 +0200849 pipe_buf_release(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200851 if (pipe->tmp_page)
852 __free_page(pipe->tmp_page);
Jens Axboe35f3d142010-05-20 10:43:18 +0200853 kfree(pipe->bufs);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200854 kfree(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800857static struct vfsmount *pipe_mnt __read_mostly;
Ingo Molnar341b4462006-04-11 13:57:45 +0200858
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700859/*
860 * pipefs_dname() is called from d_path().
861 */
862static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
863{
864 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
David Howells75c3cfa2015-03-17 22:26:12 +0000865 d_inode(dentry)->i_ino);
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700866}
867
Al Viro3ba13d12009-02-20 06:02:22 +0000868static const struct dentry_operations pipefs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700869 .d_dname = pipefs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870};
871
872static struct inode * get_pipe_inode(void)
873{
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200874 struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200875 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 if (!inode)
878 goto fail_inode;
879
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400880 inode->i_ino = get_next_ino();
881
Al Viro7bee1302013-03-21 11:04:15 -0400882 pipe = alloc_pipe_info();
Ingo Molnar923f4f22006-04-11 13:53:33 +0200883 if (!pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 goto fail_iput;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200885
Al Viroba5bb142013-03-21 02:21:19 -0400886 inode->i_pipe = pipe;
887 pipe->files = 2;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200888 pipe->readers = pipe->writers = 1;
Al Viro599a0ac2013-03-12 09:58:10 -0400889 inode->i_fop = &pipefifo_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 /*
892 * Mark the inode dirty from the very beginning,
893 * that way it will never be moved to the dirty
894 * list because "mark_inode_dirty()" will think
895 * that it already _is_ on the dirty list.
896 */
897 inode->i_state = I_DIRTY;
898 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
David Howellsda9592e2008-11-14 10:39:05 +1100899 inode->i_uid = current_fsuid();
900 inode->i_gid = current_fsgid();
Deepa Dinamani078cd822016-09-14 07:48:04 -0700901 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return inode;
904
905fail_iput:
906 iput(inode);
Ingo Molnar341b4462006-04-11 13:57:45 +0200907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908fail_inode:
909 return NULL;
910}
911
Al Viroe4fad8e2012-07-21 15:33:25 +0400912int create_pipe_files(struct file **res, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Al Viroe4fad8e2012-07-21 15:33:25 +0400914 struct inode *inode = get_pipe_inode();
Andi Kleend6cbd282006-09-30 23:29:26 -0700915 struct file *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (!inode)
Al Viroe4fad8e2012-07-21 15:33:25 +0400918 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
David Howellsc73be612020-01-14 17:07:11 +0000920 if (flags & O_NOTIFICATION_PIPE) {
921#ifdef CONFIG_WATCH_QUEUE
922 if (watch_queue_init(inode->i_pipe) < 0) {
923 iput(inode);
924 return -ENOMEM;
925 }
926#else
927 return -ENOPKG;
928#endif
929 }
930
Al Viro152b6372018-06-09 10:05:18 -0400931 f = alloc_file_pseudo(inode, pipe_mnt, "",
932 O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT)),
933 &pipefifo_fops);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500934 if (IS_ERR(f)) {
Al Viro152b6372018-06-09 10:05:18 -0400935 free_pipe_info(inode->i_pipe);
936 iput(inode);
937 return PTR_ERR(f);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Al Virode32ec42013-03-21 11:16:56 -0400940 f->private_data = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Al Viro183266f2018-06-17 14:15:10 -0400942 res[0] = alloc_file_clone(f, O_RDONLY | (flags & O_NONBLOCK),
943 &pipefifo_fops);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500944 if (IS_ERR(res[0])) {
Al Virob10a4a92018-07-09 02:29:58 -0400945 put_pipe_info(inode, inode->i_pipe);
946 fput(f);
947 return PTR_ERR(res[0]);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500948 }
Al Virode32ec42013-03-21 11:16:56 -0400949 res[0]->private_data = inode->i_pipe;
Al Viroe4fad8e2012-07-21 15:33:25 +0400950 res[1] = f;
Linus Torvaldsd8e464e2019-11-17 11:20:48 -0800951 stream_open(inode, res[0]);
952 stream_open(inode, res[1]);
Al Viroe4fad8e2012-07-21 15:33:25 +0400953 return 0;
Andi Kleend6cbd282006-09-30 23:29:26 -0700954}
955
Al Viro5b249b12012-08-19 12:17:29 -0400956static int __do_pipe_flags(int *fd, struct file **files, int flags)
Andi Kleend6cbd282006-09-30 23:29:26 -0700957{
Andi Kleend6cbd282006-09-30 23:29:26 -0700958 int error;
959 int fdw, fdr;
960
David Howellsc73be612020-01-14 17:07:11 +0000961 if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT | O_NOTIFICATION_PIPE))
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700962 return -EINVAL;
963
Al Viroe4fad8e2012-07-21 15:33:25 +0400964 error = create_pipe_files(files, flags);
965 if (error)
966 return error;
Andi Kleend6cbd282006-09-30 23:29:26 -0700967
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700968 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -0700969 if (error < 0)
970 goto err_read_pipe;
971 fdr = error;
972
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700973 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -0700974 if (error < 0)
975 goto err_fdr;
976 fdw = error;
977
Al Viro157cf642008-12-14 04:57:47 -0500978 audit_fd_pair(fdr, fdw);
Andi Kleend6cbd282006-09-30 23:29:26 -0700979 fd[0] = fdr;
980 fd[1] = fdw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return 0;
982
Andi Kleend6cbd282006-09-30 23:29:26 -0700983 err_fdr:
984 put_unused_fd(fdr);
985 err_read_pipe:
Al Viroe4fad8e2012-07-21 15:33:25 +0400986 fput(files[0]);
987 fput(files[1]);
Andi Kleend6cbd282006-09-30 23:29:26 -0700988 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
Al Viro5b249b12012-08-19 12:17:29 -0400991int do_pipe_flags(int *fd, int flags)
992{
993 struct file *files[2];
994 int error = __do_pipe_flags(fd, files, flags);
995 if (!error) {
996 fd_install(fd[0], files[0]);
997 fd_install(fd[1], files[1]);
998 }
999 return error;
1000}
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002/*
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001003 * sys_pipe() is the normal C calling standard for creating
1004 * a pipe. It's not the way Unix traditionally does this, though.
1005 */
Dominik Brodowski0a216dd2018-03-11 11:34:28 +01001006static int do_pipe2(int __user *fildes, int flags)
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001007{
Al Viro5b249b12012-08-19 12:17:29 -04001008 struct file *files[2];
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001009 int fd[2];
1010 int error;
1011
Al Viro5b249b12012-08-19 12:17:29 -04001012 error = __do_pipe_flags(fd, files, flags);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001013 if (!error) {
Al Viro5b249b12012-08-19 12:17:29 -04001014 if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
1015 fput(files[0]);
1016 fput(files[1]);
1017 put_unused_fd(fd[0]);
1018 put_unused_fd(fd[1]);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001019 error = -EFAULT;
Al Viro5b249b12012-08-19 12:17:29 -04001020 } else {
1021 fd_install(fd[0], files[0]);
1022 fd_install(fd[1], files[1]);
Ulrich Drepperba719ba2008-05-06 20:42:38 -07001023 }
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001024 }
1025 return error;
1026}
1027
Dominik Brodowski0a216dd2018-03-11 11:34:28 +01001028SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
1029{
1030 return do_pipe2(fildes, flags);
1031}
1032
Heiko Carstens2b664212009-01-14 14:14:35 +01001033SYSCALL_DEFINE1(pipe, int __user *, fildes)
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001034{
Dominik Brodowski0a216dd2018-03-11 11:34:28 +01001035 return do_pipe2(fildes, 0);
Ulrich Dreppered8cae82008-07-23 21:29:30 -07001036}
1037
Al Virofc7478a2013-03-21 02:07:59 -04001038static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
Al Virof776c732013-03-12 09:46:27 -04001039{
David Howells8cefc102019-11-15 13:30:32 +00001040 int cur = *cnt;
Al Virof776c732013-03-12 09:46:27 -04001041
1042 while (cur == *cnt) {
Al Virofc7478a2013-03-21 02:07:59 -04001043 pipe_wait(pipe);
Al Virof776c732013-03-12 09:46:27 -04001044 if (signal_pending(current))
1045 break;
1046 }
1047 return cur == *cnt ? -ERESTARTSYS : 0;
1048}
1049
Al Virofc7478a2013-03-21 02:07:59 -04001050static void wake_up_partner(struct pipe_inode_info *pipe)
Al Virof776c732013-03-12 09:46:27 -04001051{
Linus Torvalds6551d5c2020-02-18 10:12:58 -08001052 wake_up_interruptible_all(&pipe->rd_wait);
1053 wake_up_interruptible_all(&pipe->wr_wait);
Al Virof776c732013-03-12 09:46:27 -04001054}
1055
1056static int fifo_open(struct inode *inode, struct file *filp)
1057{
1058 struct pipe_inode_info *pipe;
Al Viro599a0ac2013-03-12 09:58:10 -04001059 bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
Al Virof776c732013-03-12 09:46:27 -04001060 int ret;
1061
Al Viroba5bb142013-03-21 02:21:19 -04001062 filp->f_version = 0;
1063
1064 spin_lock(&inode->i_lock);
1065 if (inode->i_pipe) {
1066 pipe = inode->i_pipe;
1067 pipe->files++;
1068 spin_unlock(&inode->i_lock);
1069 } else {
1070 spin_unlock(&inode->i_lock);
Al Viro7bee1302013-03-21 11:04:15 -04001071 pipe = alloc_pipe_info();
Al Virof776c732013-03-12 09:46:27 -04001072 if (!pipe)
Al Viroba5bb142013-03-21 02:21:19 -04001073 return -ENOMEM;
1074 pipe->files = 1;
1075 spin_lock(&inode->i_lock);
1076 if (unlikely(inode->i_pipe)) {
1077 inode->i_pipe->files++;
1078 spin_unlock(&inode->i_lock);
Al Viro4b8a8f12013-03-21 11:06:46 -04001079 free_pipe_info(pipe);
Al Viroba5bb142013-03-21 02:21:19 -04001080 pipe = inode->i_pipe;
1081 } else {
1082 inode->i_pipe = pipe;
1083 spin_unlock(&inode->i_lock);
1084 }
Al Virof776c732013-03-12 09:46:27 -04001085 }
Al Virode32ec42013-03-21 11:16:56 -04001086 filp->private_data = pipe;
Al Viroba5bb142013-03-21 02:21:19 -04001087 /* OK, we have a pipe and it's pinned down */
1088
Al Viroebec73f2013-03-21 12:24:01 -04001089 __pipe_lock(pipe);
Al Virof776c732013-03-12 09:46:27 -04001090
1091 /* We can only do regular read/write on fifos */
Linus Torvaldsd8e464e2019-11-17 11:20:48 -08001092 stream_open(inode, filp);
Al Virof776c732013-03-12 09:46:27 -04001093
Linus Torvaldsd8e464e2019-11-17 11:20:48 -08001094 switch (filp->f_mode & (FMODE_READ | FMODE_WRITE)) {
Al Virof776c732013-03-12 09:46:27 -04001095 case FMODE_READ:
1096 /*
1097 * O_RDONLY
1098 * POSIX.1 says that O_NONBLOCK means return with the FIFO
1099 * opened, even when there is no process writing the FIFO.
1100 */
Al Virof776c732013-03-12 09:46:27 -04001101 pipe->r_counter++;
1102 if (pipe->readers++ == 0)
Al Virofc7478a2013-03-21 02:07:59 -04001103 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -04001104
Al Viro599a0ac2013-03-12 09:58:10 -04001105 if (!is_pipe && !pipe->writers) {
Al Virof776c732013-03-12 09:46:27 -04001106 if ((filp->f_flags & O_NONBLOCK)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001107 /* suppress EPOLLHUP until we have
Al Virof776c732013-03-12 09:46:27 -04001108 * seen a writer */
1109 filp->f_version = pipe->w_counter;
1110 } else {
Al Virofc7478a2013-03-21 02:07:59 -04001111 if (wait_for_partner(pipe, &pipe->w_counter))
Al Virof776c732013-03-12 09:46:27 -04001112 goto err_rd;
1113 }
1114 }
1115 break;
David Howells8cefc102019-11-15 13:30:32 +00001116
Al Virof776c732013-03-12 09:46:27 -04001117 case FMODE_WRITE:
1118 /*
1119 * O_WRONLY
1120 * POSIX.1 says that O_NONBLOCK means return -1 with
1121 * errno=ENXIO when there is no process reading the FIFO.
1122 */
1123 ret = -ENXIO;
Al Viro599a0ac2013-03-12 09:58:10 -04001124 if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
Al Virof776c732013-03-12 09:46:27 -04001125 goto err;
1126
Al Virof776c732013-03-12 09:46:27 -04001127 pipe->w_counter++;
1128 if (!pipe->writers++)
Al Virofc7478a2013-03-21 02:07:59 -04001129 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -04001130
Al Viro599a0ac2013-03-12 09:58:10 -04001131 if (!is_pipe && !pipe->readers) {
Al Virofc7478a2013-03-21 02:07:59 -04001132 if (wait_for_partner(pipe, &pipe->r_counter))
Al Virof776c732013-03-12 09:46:27 -04001133 goto err_wr;
1134 }
1135 break;
David Howells8cefc102019-11-15 13:30:32 +00001136
Al Virof776c732013-03-12 09:46:27 -04001137 case FMODE_READ | FMODE_WRITE:
1138 /*
1139 * O_RDWR
1140 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
1141 * This implementation will NEVER block on a O_RDWR open, since
1142 * the process can at least talk to itself.
1143 */
Al Virof776c732013-03-12 09:46:27 -04001144
1145 pipe->readers++;
1146 pipe->writers++;
1147 pipe->r_counter++;
1148 pipe->w_counter++;
1149 if (pipe->readers == 1 || pipe->writers == 1)
Al Virofc7478a2013-03-21 02:07:59 -04001150 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -04001151 break;
1152
1153 default:
1154 ret = -EINVAL;
1155 goto err;
1156 }
1157
1158 /* Ok! */
Al Viroebec73f2013-03-21 12:24:01 -04001159 __pipe_unlock(pipe);
Al Virof776c732013-03-12 09:46:27 -04001160 return 0;
1161
1162err_rd:
1163 if (!--pipe->readers)
Linus Torvalds0ddad212019-12-09 09:48:27 -08001164 wake_up_interruptible(&pipe->wr_wait);
Al Virof776c732013-03-12 09:46:27 -04001165 ret = -ERESTARTSYS;
1166 goto err;
1167
1168err_wr:
1169 if (!--pipe->writers)
Linus Torvalds6551d5c2020-02-18 10:12:58 -08001170 wake_up_interruptible_all(&pipe->rd_wait);
Al Virof776c732013-03-12 09:46:27 -04001171 ret = -ERESTARTSYS;
1172 goto err;
1173
1174err:
Al Viroebec73f2013-03-21 12:24:01 -04001175 __pipe_unlock(pipe);
Linus Torvaldsb0d8d222013-12-02 09:44:51 -08001176
1177 put_pipe_info(inode, pipe);
Al Virof776c732013-03-12 09:46:27 -04001178 return ret;
1179}
1180
Al Viro599a0ac2013-03-12 09:58:10 -04001181const struct file_operations pipefifo_fops = {
1182 .open = fifo_open,
1183 .llseek = no_llseek,
Al Virofb9096a2014-04-02 19:56:54 -04001184 .read_iter = pipe_read,
Al Virof0d1bec2014-04-03 15:05:18 -04001185 .write_iter = pipe_write,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001186 .poll = pipe_poll,
Al Viro599a0ac2013-03-12 09:58:10 -04001187 .unlocked_ioctl = pipe_ioctl,
1188 .release = pipe_release,
1189 .fasync = pipe_fasync,
Al Virof776c732013-03-12 09:46:27 -04001190};
1191
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001192/*
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001193 * Currently we rely on the pipe array holding a power-of-2 number
Joe Lawrenced3f14c42017-11-17 15:29:21 -08001194 * of pages. Returns 0 on error.
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001195 */
Eric Biggers96e99be402018-02-06 15:42:00 -08001196unsigned int round_pipe_size(unsigned long size)
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001197{
Eric Biggersc4fed5a2018-02-06 15:42:05 -08001198 if (size > (1U << 31))
Eric Biggers96e99be402018-02-06 15:42:00 -08001199 return 0;
1200
Eric Biggers4c2e4be2018-02-06 15:41:45 -08001201 /* Minimum pipe size, as required by POSIX */
1202 if (size < PAGE_SIZE)
Eric Biggersc4fed5a2018-02-06 15:42:05 -08001203 return PAGE_SIZE;
Joe Lawrenced3f14c42017-11-17 15:29:21 -08001204
Eric Biggersc4fed5a2018-02-06 15:42:05 -08001205 return roundup_pow_of_two(size);
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001206}
1207
1208/*
David Howellsc73be612020-01-14 17:07:11 +00001209 * Resize the pipe ring to a number of slots.
Jens Axboe35f3d142010-05-20 10:43:18 +02001210 */
David Howellsc73be612020-01-14 17:07:11 +00001211int pipe_resize_ring(struct pipe_inode_info *pipe, unsigned int nr_slots)
Jens Axboe35f3d142010-05-20 10:43:18 +02001212{
1213 struct pipe_buffer *bufs;
David Howellsc73be612020-01-14 17:07:11 +00001214 unsigned int head, tail, mask, n;
Jens Axboe35f3d142010-05-20 10:43:18 +02001215
1216 /*
David Howells8cefc102019-11-15 13:30:32 +00001217 * We can shrink the pipe, if arg is greater than the ring occupancy.
1218 * Since we don't expect a lot of shrink+grow operations, just free and
1219 * allocate again like we would do for growing. If the pipe currently
Jens Axboe35f3d142010-05-20 10:43:18 +02001220 * contains more buffers than arg, then return busy.
1221 */
David Howells8cefc102019-11-15 13:30:32 +00001222 mask = pipe->ring_size - 1;
1223 head = pipe->head;
1224 tail = pipe->tail;
1225 n = pipe_occupancy(pipe->head, pipe->tail);
David Howellsc73be612020-01-14 17:07:11 +00001226 if (nr_slots < n)
1227 return -EBUSY;
Jens Axboe35f3d142010-05-20 10:43:18 +02001228
David Howells8cefc102019-11-15 13:30:32 +00001229 bufs = kcalloc(nr_slots, sizeof(*bufs),
Vladimir Davydovd86133b2016-07-26 15:24:33 -07001230 GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
David Howellsc73be612020-01-14 17:07:11 +00001231 if (unlikely(!bufs))
1232 return -ENOMEM;
Jens Axboe35f3d142010-05-20 10:43:18 +02001233
1234 /*
1235 * The pipe array wraps around, so just start the new one at zero
David Howells8cefc102019-11-15 13:30:32 +00001236 * and adjust the indices.
Jens Axboe35f3d142010-05-20 10:43:18 +02001237 */
David Howells8cefc102019-11-15 13:30:32 +00001238 if (n > 0) {
1239 unsigned int h = head & mask;
1240 unsigned int t = tail & mask;
1241 if (h > t) {
1242 memcpy(bufs, pipe->bufs + t,
1243 n * sizeof(struct pipe_buffer));
1244 } else {
1245 unsigned int tsize = pipe->ring_size - t;
1246 if (h > 0)
1247 memcpy(bufs + tsize, pipe->bufs,
1248 h * sizeof(struct pipe_buffer));
1249 memcpy(bufs, pipe->bufs + t,
1250 tsize * sizeof(struct pipe_buffer));
1251 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001252 }
1253
David Howells8cefc102019-11-15 13:30:32 +00001254 head = n;
1255 tail = 0;
1256
Jens Axboe35f3d142010-05-20 10:43:18 +02001257 kfree(pipe->bufs);
1258 pipe->bufs = bufs;
David Howells8cefc102019-11-15 13:30:32 +00001259 pipe->ring_size = nr_slots;
David Howellsc73be612020-01-14 17:07:11 +00001260 if (pipe->max_usage > nr_slots)
1261 pipe->max_usage = nr_slots;
David Howells8cefc102019-11-15 13:30:32 +00001262 pipe->tail = tail;
1263 pipe->head = head;
Linus Torvalds6551d5c2020-02-18 10:12:58 -08001264
1265 /* This might have made more room for writers */
1266 wake_up_interruptible(&pipe->wr_wait);
David Howellsc73be612020-01-14 17:07:11 +00001267 return 0;
1268}
1269
1270/*
1271 * Allocate a new array of pipe buffers and copy the info over. Returns the
1272 * pipe size if successful, or return -ERROR on error.
1273 */
1274static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long arg)
1275{
1276 unsigned long user_bufs;
1277 unsigned int nr_slots, size;
1278 long ret = 0;
1279
1280#ifdef CONFIG_WATCH_QUEUE
1281 if (pipe->watch_queue)
1282 return -EBUSY;
1283#endif
1284
1285 size = round_pipe_size(arg);
1286 nr_slots = size >> PAGE_SHIFT;
1287
1288 if (!nr_slots)
1289 return -EINVAL;
1290
1291 /*
1292 * If trying to increase the pipe capacity, check that an
1293 * unprivileged user is not trying to exceed various limits
1294 * (soft limit check here, hard limit check just below).
1295 * Decreasing the pipe capacity is always permitted, even
1296 * if the user is currently over a limit.
1297 */
1298 if (nr_slots > pipe->max_usage &&
1299 size > pipe_max_size && !capable(CAP_SYS_RESOURCE))
1300 return -EPERM;
1301
1302 user_bufs = account_pipe_buffers(pipe->user, pipe->nr_accounted, nr_slots);
1303
1304 if (nr_slots > pipe->max_usage &&
1305 (too_many_pipe_buffers_hard(user_bufs) ||
1306 too_many_pipe_buffers_soft(user_bufs)) &&
1307 pipe_is_unprivileged_user()) {
1308 ret = -EPERM;
1309 goto out_revert_acct;
1310 }
1311
1312 ret = pipe_resize_ring(pipe, nr_slots);
1313 if (ret < 0)
1314 goto out_revert_acct;
1315
1316 pipe->max_usage = nr_slots;
1317 pipe->nr_accounted = nr_slots;
David Howells6718b6f2019-10-16 16:47:32 +01001318 return pipe->max_usage * PAGE_SIZE;
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001319
1320out_revert_acct:
David Howellsc73be612020-01-14 17:07:11 +00001321 (void) account_pipe_buffers(pipe->user, nr_slots, pipe->nr_accounted);
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001322 return ret;
Jens Axboe35f3d142010-05-20 10:43:18 +02001323}
1324
Jens Axboeff9da692010-06-03 14:54:39 +02001325/*
Linus Torvalds72083642010-11-28 16:27:19 -08001326 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1327 * location, so checking ->i_pipe is not enough to verify that this is a
1328 * pipe.
1329 */
David Howellsc73be612020-01-14 17:07:11 +00001330struct pipe_inode_info *get_pipe_info(struct file *file, bool for_splice)
Linus Torvalds72083642010-11-28 16:27:19 -08001331{
David Howellsc73be612020-01-14 17:07:11 +00001332 struct pipe_inode_info *pipe = file->private_data;
1333
1334 if (file->f_op != &pipefifo_fops || !pipe)
1335 return NULL;
1336#ifdef CONFIG_WATCH_QUEUE
1337 if (for_splice && pipe->watch_queue)
1338 return NULL;
1339#endif
1340 return pipe;
Linus Torvalds72083642010-11-28 16:27:19 -08001341}
1342
Jens Axboe35f3d142010-05-20 10:43:18 +02001343long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1344{
1345 struct pipe_inode_info *pipe;
1346 long ret;
1347
David Howellsc73be612020-01-14 17:07:11 +00001348 pipe = get_pipe_info(file, false);
Jens Axboe35f3d142010-05-20 10:43:18 +02001349 if (!pipe)
1350 return -EBADF;
1351
Al Viroebec73f2013-03-21 12:24:01 -04001352 __pipe_lock(pipe);
Jens Axboe35f3d142010-05-20 10:43:18 +02001353
1354 switch (cmd) {
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001355 case F_SETPIPE_SZ:
1356 ret = pipe_set_size(pipe, arg);
Jens Axboe35f3d142010-05-20 10:43:18 +02001357 break;
1358 case F_GETPIPE_SZ:
David Howells6718b6f2019-10-16 16:47:32 +01001359 ret = pipe->max_usage * PAGE_SIZE;
Jens Axboe35f3d142010-05-20 10:43:18 +02001360 break;
1361 default:
1362 ret = -EINVAL;
1363 break;
1364 }
1365
Al Viroebec73f2013-03-21 12:24:01 -04001366 __pipe_unlock(pipe);
Jens Axboe35f3d142010-05-20 10:43:18 +02001367 return ret;
1368}
1369
Nick Pigginff0c7d12011-01-07 17:49:50 +11001370static const struct super_operations pipefs_ops = {
1371 .destroy_inode = free_inode_nonrcu,
Pavel Emelyanovd70ef972011-10-31 17:10:04 -07001372 .statfs = simple_statfs,
Nick Pigginff0c7d12011-01-07 17:49:50 +11001373};
1374
Jens Axboe35f3d142010-05-20 10:43:18 +02001375/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 * pipefs should _never_ be mounted by userland - too much of security hassle,
1377 * no real gain from having the whole whorehouse mounted. So we don't need
1378 * any operations on the root directory. However, we need a non-trivial
1379 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1380 */
David Howells4fa7ec52019-03-25 16:38:23 +00001381
1382static int pipefs_init_fs_context(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
David Howells4fa7ec52019-03-25 16:38:23 +00001384 struct pseudo_fs_context *ctx = init_pseudo(fc, PIPEFS_MAGIC);
1385 if (!ctx)
1386 return -ENOMEM;
1387 ctx->ops = &pipefs_ops;
1388 ctx->dops = &pipefs_dentry_operations;
1389 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
1391
1392static struct file_system_type pipe_fs_type = {
1393 .name = "pipefs",
David Howells4fa7ec52019-03-25 16:38:23 +00001394 .init_fs_context = pipefs_init_fs_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 .kill_sb = kill_anon_super,
1396};
1397
1398static int __init init_pipe_fs(void)
1399{
1400 int err = register_filesystem(&pipe_fs_type);
Ingo Molnar341b4462006-04-11 13:57:45 +02001401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 if (!err) {
1403 pipe_mnt = kern_mount(&pipe_fs_type);
1404 if (IS_ERR(pipe_mnt)) {
1405 err = PTR_ERR(pipe_mnt);
1406 unregister_filesystem(&pipe_fs_type);
1407 }
1408 }
1409 return err;
1410}
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412fs_initcall(init_pipe_fs);