blob: e9b361cb093ee29e41acecac155c7a010256e283 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080028#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/ioctls.h>
30
Al Viro599a0ac2013-03-12 09:58:10 -040031#include "internal.h"
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*
Jens Axboeb492e952010-05-19 21:03:16 +020034 * The max size that a non-root user is allowed to grow the pipe. Can
Jens Axboeff9da692010-06-03 14:54:39 +020035 * be set by root in /proc/sys/fs/pipe-max-size
Jens Axboeb492e952010-05-19 21:03:16 +020036 */
Jens Axboeff9da692010-06-03 14:54:39 +020037unsigned int pipe_max_size = 1048576;
38
Willy Tarreau759c0112016-01-18 16:36:09 +010039/* Maximum allocatable pages per user. Hard limit is unset by default, soft
40 * matches default values.
41 */
42unsigned long pipe_user_pages_hard;
43unsigned long pipe_user_pages_soft = PIPE_DEF_BUFFERS * INR_OPEN_CUR;
44
Jens Axboeb492e952010-05-19 21:03:16 +020045/*
David Howells8cefc102019-11-15 13:30:32 +000046 * 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
49 * <= 2^31.
50 * -- David Howells 2019-09-23.
51 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * Reads with count = 0 should always return 0.
53 * -- Julian Bradfield 1999-06-07.
54 *
55 * FIFOs and Pipes now generate SIGIO for both readers and writers.
56 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
57 *
58 * pipe_read & write cleanup
59 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
60 */
61
Miklos Szeredi61e0d472009-04-14 19:48:41 +020062static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass)
63{
Al Viro6447a3c2013-03-21 11:01:38 -040064 if (pipe->files)
Al Viro72b0d9a2013-03-21 02:32:24 -040065 mutex_lock_nested(&pipe->mutex, subclass);
Miklos Szeredi61e0d472009-04-14 19:48:41 +020066}
67
68void pipe_lock(struct pipe_inode_info *pipe)
69{
70 /*
71 * pipe_lock() nests non-pipe inode locks (for writing to a file)
72 */
73 pipe_lock_nested(pipe, I_MUTEX_PARENT);
74}
75EXPORT_SYMBOL(pipe_lock);
76
77void pipe_unlock(struct pipe_inode_info *pipe)
78{
Al Viro6447a3c2013-03-21 11:01:38 -040079 if (pipe->files)
Al Viro72b0d9a2013-03-21 02:32:24 -040080 mutex_unlock(&pipe->mutex);
Miklos Szeredi61e0d472009-04-14 19:48:41 +020081}
82EXPORT_SYMBOL(pipe_unlock);
83
Al Viroebec73f2013-03-21 12:24:01 -040084static inline void __pipe_lock(struct pipe_inode_info *pipe)
85{
86 mutex_lock_nested(&pipe->mutex, I_MUTEX_PARENT);
87}
88
89static inline void __pipe_unlock(struct pipe_inode_info *pipe)
90{
91 mutex_unlock(&pipe->mutex);
92}
93
Miklos Szeredi61e0d472009-04-14 19:48:41 +020094void pipe_double_lock(struct pipe_inode_info *pipe1,
95 struct pipe_inode_info *pipe2)
96{
97 BUG_ON(pipe1 == pipe2);
98
99 if (pipe1 < pipe2) {
100 pipe_lock_nested(pipe1, I_MUTEX_PARENT);
101 pipe_lock_nested(pipe2, I_MUTEX_CHILD);
102 } else {
Peter Zijlstra023d43c2009-07-21 10:09:23 +0200103 pipe_lock_nested(pipe2, I_MUTEX_PARENT);
104 pipe_lock_nested(pipe1, I_MUTEX_CHILD);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200105 }
106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/* Drop the inode semaphore and wait for a pipe event, atomically */
Ingo Molnar3a326a22006-04-10 15:18:35 +0200109void pipe_wait(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 DEFINE_WAIT(wait);
112
Ingo Molnard79fc0f2005-09-10 00:26:12 -0700113 /*
114 * Pipes are system-local resources, so sleeping on them
115 * is considered a noninteractive wait:
116 */
Mike Galbraithaf927232007-10-15 17:00:13 +0200117 prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200118 pipe_unlock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 schedule();
Ingo Molnar3a326a22006-04-10 15:18:35 +0200120 finish_wait(&pipe->wait, &wait);
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200121 pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Ingo Molnar341b4462006-04-11 13:57:45 +0200124static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
125 struct pipe_buffer *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 struct page *page = buf->page;
128
Jens Axboe5274f052006-03-30 15:15:30 +0200129 /*
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
Ingo Molnar341b4462006-04-11 13:57:45 +0200132 * allocation cache. (Otherwise just release our reference to it)
Jens Axboe5274f052006-03-30 15:15:30 +0200133 */
Ingo Molnar341b4462006-04-11 13:57:45 +0200134 if (page_count(page) == 1 && !pipe->tmp_page)
Ingo Molnar923f4f22006-04-11 13:53:33 +0200135 pipe->tmp_page = page;
Ingo Molnar341b4462006-04-11 13:57:45 +0200136 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300137 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
Vladimir Davydovd86133b2016-07-26 15:24:33 -0700140static int anon_pipe_buf_steal(struct pipe_inode_info *pipe,
141 struct pipe_buffer *buf)
142{
143 struct page *page = buf->page;
144
145 if (page_count(page) == 1) {
Shakeel Butt60cd4bc2019-03-05 15:43:13 -0800146 memcg_kmem_uncharge(page, 0);
Vladimir Davydovd86133b2016-07-26 15:24:33 -0700147 __SetPageLocked(page);
148 return 0;
149 }
150 return 1;
151}
152
Jens Axboe08457182007-06-12 20:51:32 +0200153/**
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800154 * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
Jens Axboe08457182007-06-12 20:51:32 +0200155 * @pipe: the pipe that the buffer belongs to
156 * @buf: the buffer to attempt to steal
157 *
158 * Description:
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800159 * This function attempts to steal the &struct page attached to
Jens Axboe08457182007-06-12 20:51:32 +0200160 * @buf. If successful, this function returns 0 and returns with
161 * the page locked. The caller may then reuse the page for whatever
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800162 * he wishes; the typical use is insertion into a different file
Jens Axboe08457182007-06-12 20:51:32 +0200163 * page cache.
164 */
Jens Axboe330ab712006-05-02 15:29:57 +0200165int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
166 struct pipe_buffer *buf)
Jens Axboe5abc97a2006-03-30 15:16:46 +0200167{
Jens Axboe46e678c2006-04-30 16:36:32 +0200168 struct page *page = buf->page;
169
Jens Axboe08457182007-06-12 20:51:32 +0200170 /*
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
173 * and return OK.
174 */
Jens Axboe46e678c2006-04-30 16:36:32 +0200175 if (page_count(page) == 1) {
Jens Axboe46e678c2006-04-30 16:36:32 +0200176 lock_page(page);
177 return 0;
178 }
179
180 return 1;
Jens Axboe5abc97a2006-03-30 15:16:46 +0200181}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200182EXPORT_SYMBOL(generic_pipe_buf_steal);
Jens Axboe5abc97a2006-03-30 15:16:46 +0200183
Jens Axboe08457182007-06-12 20:51:32 +0200184/**
Randy Dunlapb51d63c2008-02-13 15:03:22 -0800185 * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
Jens Axboe08457182007-06-12 20:51:32 +0200186 * @pipe: the pipe that the buffer belongs to
187 * @buf: the buffer to get a reference to
188 *
189 * Description:
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
192 * pipe into another.
193 */
Matthew Wilcox15fab632019-04-05 14:02:10 -0700194bool generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
Jens Axboe70524492006-04-11 15:51:17 +0200195{
Matthew Wilcox15fab632019-04-05 14:02:10 -0700196 return try_get_page(buf->page);
Jens Axboe70524492006-04-11 15:51:17 +0200197}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200198EXPORT_SYMBOL(generic_pipe_buf_get);
Jens Axboe70524492006-04-11 15:51:17 +0200199
Jens Axboe08457182007-06-12 20:51:32 +0200200/**
201 * generic_pipe_buf_confirm - verify contents of the pipe buffer
Randy Dunlap79685b82007-07-27 08:08:51 +0200202 * @info: the pipe that the buffer belongs to
Jens Axboe08457182007-06-12 20:51:32 +0200203 * @buf: the buffer to confirm
204 *
205 * Description:
206 * This function does nothing, because the generic pipe code uses
207 * pages that are always good when inserted into the pipe.
208 */
Jens Axboecac36bb02007-06-14 13:10:48 +0200209int generic_pipe_buf_confirm(struct pipe_inode_info *info,
210 struct pipe_buffer *buf)
Jens Axboef84d7512006-05-01 19:59:03 +0200211{
212 return 0;
213}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200214EXPORT_SYMBOL(generic_pipe_buf_confirm);
Jens Axboef84d7512006-05-01 19:59:03 +0200215
Miklos Szeredi68181732009-05-07 15:37:36 +0200216/**
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
220 *
221 * Description:
222 * This function releases a reference to @buf.
223 */
224void generic_pipe_buf_release(struct pipe_inode_info *pipe,
225 struct pipe_buffer *buf)
226{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300227 put_page(buf->page);
Miklos Szeredi68181732009-05-07 15:37:36 +0200228}
Miklos Szeredi51921cb2010-05-26 08:44:22 +0200229EXPORT_SYMBOL(generic_pipe_buf_release);
Miklos Szeredi68181732009-05-07 15:37:36 +0200230
Jann Horn01e71872019-01-23 15:19:18 +0100231/* New data written to a pipe may be appended to a buffer with this type. */
Eric Dumazetd4c3cca2006-12-13 00:34:04 -0800232static const struct pipe_buf_operations anon_pipe_buf_ops = {
Jens Axboecac36bb02007-06-14 13:10:48 +0200233 .confirm = generic_pipe_buf_confirm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 .release = anon_pipe_buf_release,
Vladimir Davydovd86133b2016-07-26 15:24:33 -0700235 .steal = anon_pipe_buf_steal,
Jens Axboef84d7512006-05-01 19:59:03 +0200236 .get = generic_pipe_buf_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237};
238
Jann Horna0ce2f02019-01-23 15:19:17 +0100239static const struct pipe_buf_operations anon_pipe_buf_nomerge_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 .confirm = generic_pipe_buf_confirm,
241 .release = anon_pipe_buf_release,
242 .steal = anon_pipe_buf_steal,
243 .get = generic_pipe_buf_get,
Ingo Molnar923f4f22006-04-11 13:53:33 +0200244};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Linus Torvalds98830352012-04-29 13:12:42 -0700246static const struct pipe_buf_operations packet_pipe_buf_ops = {
Linus Torvalds98830352012-04-29 13:12:42 -0700247 .confirm = generic_pipe_buf_confirm,
248 .release = anon_pipe_buf_release,
Vladimir Davydovd86133b2016-07-26 15:24:33 -0700249 .steal = anon_pipe_buf_steal,
Linus Torvalds98830352012-04-29 13:12:42 -0700250 .get = generic_pipe_buf_get,
251};
252
Jann Horn01e71872019-01-23 15:19:18 +0100253/**
254 * pipe_buf_mark_unmergeable - mark a &struct pipe_buffer as unmergeable
255 * @buf: the buffer to mark
256 *
257 * Description:
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.
261 */
Jann Horna0ce2f02019-01-23 15:19:17 +0100262void pipe_buf_mark_unmergeable(struct pipe_buffer *buf)
263{
264 if (buf->ops == &anon_pipe_buf_ops)
265 buf->ops = &anon_pipe_buf_nomerge_ops;
266}
267
Jann Horn01e71872019-01-23 15:19:18 +0100268static bool pipe_buf_can_merge(struct pipe_buffer *buf)
269{
270 return buf->ops == &anon_pipe_buf_ops;
271}
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273static ssize_t
Al Virofb9096a2014-04-02 19:56:54 -0400274pipe_read(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Al Virofb9096a2014-04-02 19:56:54 -0400276 size_t total_len = iov_iter_count(to);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700277 struct file *filp = iocb->ki_filp;
Al Virode32ec42013-03-21 11:16:56 -0400278 struct pipe_inode_info *pipe = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 int do_wakeup;
280 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 /* Null read succeeds. */
283 if (unlikely(total_len == 0))
284 return 0;
285
286 do_wakeup = 0;
287 ret = 0;
Al Viroebec73f2013-03-21 12:24:01 -0400288 __pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 for (;;) {
David Howells8cefc102019-11-15 13:30:32 +0000290 unsigned int head = pipe->head;
291 unsigned int tail = pipe->tail;
292 unsigned int mask = pipe->ring_size - 1;
293
294 if (!pipe_empty(head, tail)) {
295 struct pipe_buffer *buf = &pipe->bufs[tail & mask];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 size_t chars = buf->len;
Al Viro637b58c2014-02-03 19:11:42 -0500297 size_t written;
298 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 if (chars > total_len)
301 chars = total_len;
302
Miklos Szeredifba597d2016-09-27 10:45:12 +0200303 error = pipe_buf_confirm(pipe, buf);
Jens Axboef84d7512006-05-01 19:59:03 +0200304 if (error) {
Jens Axboe5274f052006-03-30 15:15:30 +0200305 if (!ret)
Nicolas Kaisere5953cb2010-10-21 14:56:00 +0200306 ret = error;
Jens Axboe5274f052006-03-30 15:15:30 +0200307 break;
308 }
Jens Axboef84d7512006-05-01 19:59:03 +0200309
Al Virofb9096a2014-04-02 19:56:54 -0400310 written = copy_page_to_iter(buf->page, buf->offset, chars, to);
Al Viro637b58c2014-02-03 19:11:42 -0500311 if (unlikely(written < chars)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200312 if (!ret)
Al Viro637b58c2014-02-03 19:11:42 -0500313 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 break;
315 }
316 ret += chars;
317 buf->offset += chars;
318 buf->len -= chars;
Linus Torvalds98830352012-04-29 13:12:42 -0700319
320 /* Was it a packet buffer? Clean up and exit */
321 if (buf->flags & PIPE_BUF_FLAG_PACKET) {
322 total_len = chars;
323 buf->len = 0;
324 }
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (!buf->len) {
Miklos Szeredia7796382016-09-27 10:45:12 +0200327 pipe_buf_release(pipe, buf);
David Howells8cefc102019-11-15 13:30:32 +0000328 tail++;
329 pipe->tail = tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 do_wakeup = 1;
331 }
332 total_len -= chars;
333 if (!total_len)
334 break; /* common path: read succeeded */
David Howells8cefc102019-11-15 13:30:32 +0000335 if (!pipe_empty(head, tail)) /* More to do? */
336 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
David Howells8cefc102019-11-15 13:30:32 +0000338
Ingo Molnar923f4f22006-04-11 13:53:33 +0200339 if (!pipe->writers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 break;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200341 if (!pipe->waiting_writers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 /* 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.
346 */
347 if (ret)
348 break;
349 if (filp->f_flags & O_NONBLOCK) {
350 ret = -EAGAIN;
351 break;
352 }
353 }
354 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200355 if (!ret)
356 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 break;
358 }
359 if (do_wakeup) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800360 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLOUT | EPOLLWRNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200361 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200363 pipe_wait(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
Al Viroebec73f2013-03-21 12:24:01 -0400365 __pipe_unlock(pipe);
Ingo Molnar341b4462006-04-11 13:57:45 +0200366
367 /* Signal writers asynchronously that there is more room. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (do_wakeup) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800369 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLOUT | EPOLLWRNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200370 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372 if (ret > 0)
373 file_accessed(filp);
374 return ret;
375}
376
Linus Torvalds98830352012-04-29 13:12:42 -0700377static inline int is_packetized(struct file *file)
378{
379 return (file->f_flags & O_DIRECT) != 0;
380}
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382static ssize_t
Al Virof0d1bec2014-04-03 15:05:18 -0400383pipe_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700385 struct file *filp = iocb->ki_filp;
Al Virode32ec42013-03-21 11:16:56 -0400386 struct pipe_inode_info *pipe = filp->private_data;
David Howells8cefc102019-11-15 13:30:32 +0000387 unsigned int head, tail, max_usage, mask;
Al Virof0d1bec2014-04-03 15:05:18 -0400388 ssize_t ret = 0;
389 int do_wakeup = 0;
390 size_t total_len = iov_iter_count(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 ssize_t chars;
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /* Null write succeeds. */
394 if (unlikely(total_len == 0))
395 return 0;
396
Al Viroebec73f2013-03-21 12:24:01 -0400397 __pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Ingo Molnar923f4f22006-04-11 13:53:33 +0200399 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 send_sig(SIGPIPE, current, 0);
401 ret = -EPIPE;
402 goto out;
403 }
404
David Howells8cefc102019-11-15 13:30:32 +0000405 tail = pipe->tail;
406 head = pipe->head;
407 max_usage = pipe->ring_size;
408 mask = pipe->ring_size - 1;
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /* We try to merge small writes */
411 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
David Howells8cefc102019-11-15 13:30:32 +0000412 if (!pipe_empty(head, tail) && chars != 0) {
413 struct pipe_buffer *buf = &pipe->bufs[(head - 1) & mask];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 int offset = buf->offset + buf->len;
Ingo Molnar341b4462006-04-11 13:57:45 +0200415
Jann Horn01e71872019-01-23 15:19:18 +0100416 if (pipe_buf_can_merge(buf) && offset + chars <= PAGE_SIZE) {
Miklos Szeredifba597d2016-09-27 10:45:12 +0200417 ret = pipe_buf_confirm(pipe, buf);
Eric Biggers6ae08062015-10-17 16:26:09 -0500418 if (ret)
Jens Axboe5274f052006-03-30 15:15:30 +0200419 goto out;
Jens Axboef84d7512006-05-01 19:59:03 +0200420
Al Virof0d1bec2014-04-03 15:05:18 -0400421 ret = copy_page_from_iter(buf->page, offset, chars, from);
422 if (unlikely(ret < chars)) {
Eric Biggers6ae08062015-10-17 16:26:09 -0500423 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 goto out;
Jens Axboef6762b72006-05-01 20:02:05 +0200425 }
Al Virof0d1bec2014-04-03 15:05:18 -0400426 do_wakeup = 1;
Eric Biggers6ae08062015-10-17 16:26:09 -0500427 buf->len += ret;
Al Virof0d1bec2014-04-03 15:05:18 -0400428 if (!iov_iter_count(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 goto out;
430 }
431 }
432
433 for (;;) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200434 if (!pipe->readers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 send_sig(SIGPIPE, current, 0);
Ingo Molnar341b4462006-04-11 13:57:45 +0200436 if (!ret)
437 ret = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 break;
439 }
David Howells8cefc102019-11-15 13:30:32 +0000440
441 tail = pipe->tail;
442 if (!pipe_full(head, tail, max_usage)) {
443 struct pipe_buffer *buf = &pipe->bufs[head & mask];
Ingo Molnar923f4f22006-04-11 13:53:33 +0200444 struct page *page = pipe->tmp_page;
Al Virof0d1bec2014-04-03 15:05:18 -0400445 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 if (!page) {
Vladimir Davydovd86133b2016-07-26 15:24:33 -0700448 page = alloc_page(GFP_HIGHUSER | __GFP_ACCOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (unlikely(!page)) {
450 ret = ret ? : -ENOMEM;
451 break;
452 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200453 pipe->tmp_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
Ingo Molnar341b4462006-04-11 13:57:45 +0200455 /* Always wake up, even if the copy fails. Otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 * we lock up (O_NONBLOCK-)readers that sleep due to
457 * syscall merging.
458 * FIXME! Is this really true?
459 */
460 do_wakeup = 1;
Al Virof0d1bec2014-04-03 15:05:18 -0400461 copied = copy_page_from_iter(page, 0, PAGE_SIZE, from);
462 if (unlikely(copied < PAGE_SIZE && iov_iter_count(from))) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200463 if (!ret)
Al Virof0d1bec2014-04-03 15:05:18 -0400464 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 break;
466 }
Al Virof0d1bec2014-04-03 15:05:18 -0400467 ret += copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 /* Insert it into the buffer array */
470 buf->page = page;
471 buf->ops = &anon_pipe_buf_ops;
472 buf->offset = 0;
Al Virof0d1bec2014-04-03 15:05:18 -0400473 buf->len = copied;
Linus Torvalds98830352012-04-29 13:12:42 -0700474 buf->flags = 0;
475 if (is_packetized(filp)) {
476 buf->ops = &packet_pipe_buf_ops;
477 buf->flags = PIPE_BUF_FLAG_PACKET;
478 }
David Howells8cefc102019-11-15 13:30:32 +0000479
480 head++;
481 pipe->head = head;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200482 pipe->tmp_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Al Virof0d1bec2014-04-03 15:05:18 -0400484 if (!iov_iter_count(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 break;
486 }
David Howells8cefc102019-11-15 13:30:32 +0000487
488 if (!pipe_full(head, tail, max_usage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 continue;
David Howells8cefc102019-11-15 13:30:32 +0000490
491 /* Wait for buffer space to become available. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (filp->f_flags & O_NONBLOCK) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200493 if (!ret)
494 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 break;
496 }
497 if (signal_pending(current)) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200498 if (!ret)
499 ret = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 break;
501 }
502 if (do_wakeup) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800503 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLRDNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200504 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 do_wakeup = 0;
506 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200507 pipe->waiting_writers++;
508 pipe_wait(pipe);
509 pipe->waiting_writers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511out:
Al Viroebec73f2013-03-21 12:24:01 -0400512 __pipe_unlock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (do_wakeup) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800514 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLRDNORM);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200515 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
Dmitry Monakhov7e775f42014-01-23 15:55:21 -0800517 if (ret > 0 && sb_start_write_trylock(file_inode(filp)->i_sb)) {
Josef Bacikc3b2da32012-03-26 09:59:21 -0400518 int err = file_update_time(filp);
519 if (err)
520 ret = err;
Dmitry Monakhov7e775f42014-01-23 15:55:21 -0800521 sb_end_write(file_inode(filp)->i_sb);
Josef Bacikc3b2da32012-03-26 09:59:21 -0400522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return ret;
524}
525
Andi Kleend59d0b12008-02-08 04:21:23 -0800526static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Al Virode32ec42013-03-21 11:16:56 -0400528 struct pipe_inode_info *pipe = filp->private_data;
David Howells8cefc102019-11-15 13:30:32 +0000529 int count, head, tail, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 switch (cmd) {
532 case FIONREAD:
Al Viroebec73f2013-03-21 12:24:01 -0400533 __pipe_lock(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 count = 0;
David Howells8cefc102019-11-15 13:30:32 +0000535 head = pipe->head;
536 tail = pipe->tail;
537 mask = pipe->ring_size - 1;
538
539 while (tail != head) {
540 count += pipe->bufs[tail & mask].len;
541 tail++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
Al Viroebec73f2013-03-21 12:24:01 -0400543 __pipe_unlock(pipe);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return put_user(count, (int __user *)arg);
546 default:
Will Deacon46ce3412012-05-25 11:39:13 +0100547 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549}
550
Christoph Hellwigdd670812017-12-31 16:42:12 +0100551/* No kernel lock held - fine */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700552static __poll_t
553pipe_poll(struct file *filp, poll_table *wait)
Christoph Hellwigdd670812017-12-31 16:42:12 +0100554{
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700555 __poll_t mask;
Christoph Hellwigdd670812017-12-31 16:42:12 +0100556 struct pipe_inode_info *pipe = filp->private_data;
David Howells8cefc102019-11-15 13:30:32 +0000557 unsigned int head = READ_ONCE(pipe->head);
558 unsigned int tail = READ_ONCE(pipe->tail);
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700559
560 poll_wait(filp, &pipe->wait, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
David Howells8cefc102019-11-15 13:30:32 +0000562 BUG_ON(pipe_occupancy(head, tail) > pipe->ring_size);
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /* Reading only -- no need for acquiring the semaphore. */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700565 mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (filp->f_mode & FMODE_READ) {
David Howells8cefc102019-11-15 13:30:32 +0000567 if (!pipe_empty(head, tail))
568 mask |= EPOLLIN | EPOLLRDNORM;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200569 if (!pipe->writers && filp->f_version != pipe->w_counter)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800570 mask |= EPOLLHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572
573 if (filp->f_mode & FMODE_WRITE) {
David Howells8cefc102019-11-15 13:30:32 +0000574 if (!pipe_full(head, tail, pipe->ring_size))
575 mask |= EPOLLOUT | EPOLLWRNORM;
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700576 /*
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800577 * Most Unices do not set EPOLLERR for FIFOs but on Linux they
Pekka Enberg5e5d7a22005-09-06 15:17:48 -0700578 * behave exactly like pipes for poll().
579 */
Ingo Molnar923f4f22006-04-11 13:53:33 +0200580 if (!pipe->readers)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800581 mask |= EPOLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583
584 return mask;
585}
586
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800587static void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe)
588{
589 int kill = 0;
590
591 spin_lock(&inode->i_lock);
592 if (!--pipe->files) {
593 inode->i_pipe = NULL;
594 kill = 1;
595 }
596 spin_unlock(&inode->i_lock);
597
598 if (kill)
599 free_pipe_info(pipe);
600}
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602static int
Al Viro599a0ac2013-03-12 09:58:10 -0400603pipe_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800605 struct pipe_inode_info *pipe = file->private_data;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200606
Al Viroebec73f2013-03-21 12:24:01 -0400607 __pipe_lock(pipe);
Al Viro599a0ac2013-03-12 09:58:10 -0400608 if (file->f_mode & FMODE_READ)
609 pipe->readers--;
610 if (file->f_mode & FMODE_WRITE)
611 pipe->writers--;
Ingo Molnar341b4462006-04-11 13:57:45 +0200612
Al Viroba5bb142013-03-21 02:21:19 -0400613 if (pipe->readers || pipe->writers) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800614 wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLOUT | EPOLLRDNORM | EPOLLWRNORM | EPOLLERR | EPOLLHUP);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200615 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
616 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
Al Viroebec73f2013-03-21 12:24:01 -0400618 __pipe_unlock(pipe);
Al Viroba5bb142013-03-21 02:21:19 -0400619
Linus Torvaldsb0d8d222013-12-02 09:44:51 -0800620 put_pipe_info(inode, pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return 0;
622}
623
624static int
Al Viro599a0ac2013-03-12 09:58:10 -0400625pipe_fasync(int fd, struct file *filp, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Al Virode32ec42013-03-21 11:16:56 -0400627 struct pipe_inode_info *pipe = filp->private_data;
Al Viro599a0ac2013-03-12 09:58:10 -0400628 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Al Viroebec73f2013-03-21 12:24:01 -0400630 __pipe_lock(pipe);
Al Viro599a0ac2013-03-12 09:58:10 -0400631 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) {
Ingo Molnar341b4462006-04-11 13:57:45 +0200634 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
Al Viro599a0ac2013-03-12 09:58:10 -0400635 if (retval < 0 && (filp->f_mode & FMODE_READ))
636 /* this can happen only if on == T */
Oleg Nesterove5bc49b2009-03-12 14:31:28 -0700637 fasync_helper(-1, filp, 0, &pipe->fasync_readers);
638 }
Al Viroebec73f2013-03-21 12:24:01 -0400639 __pipe_unlock(pipe);
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700640 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700643static unsigned long account_pipe_buffers(struct user_struct *user,
Willy Tarreau759c0112016-01-18 16:36:09 +0100644 unsigned long old, unsigned long new)
645{
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700646 return atomic_long_add_return(new - old, &user->pipe_bufs);
Willy Tarreau759c0112016-01-18 16:36:09 +0100647}
648
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700649static bool too_many_pipe_buffers_soft(unsigned long user_bufs)
Willy Tarreau759c0112016-01-18 16:36:09 +0100650{
Eric Biggersf7340762018-02-06 15:42:08 -0800651 unsigned long soft_limit = READ_ONCE(pipe_user_pages_soft);
652
653 return soft_limit && user_bufs > soft_limit;
Willy Tarreau759c0112016-01-18 16:36:09 +0100654}
655
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700656static bool too_many_pipe_buffers_hard(unsigned long user_bufs)
Willy Tarreau759c0112016-01-18 16:36:09 +0100657{
Eric Biggersf7340762018-02-06 15:42:08 -0800658 unsigned long hard_limit = READ_ONCE(pipe_user_pages_hard);
659
660 return hard_limit && user_bufs > hard_limit;
Willy Tarreau759c0112016-01-18 16:36:09 +0100661}
662
Eric Biggers85c2dd52018-02-06 15:41:53 -0800663static bool is_unprivileged_user(void)
664{
665 return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
666}
667
Al Viro7bee1302013-03-21 11:04:15 -0400668struct pipe_inode_info *alloc_pipe_info(void)
Ingo Molnar3a326a22006-04-10 15:18:35 +0200669{
Ingo Molnar923f4f22006-04-11 13:53:33 +0200670 struct pipe_inode_info *pipe;
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700671 unsigned long pipe_bufs = PIPE_DEF_BUFFERS;
672 struct user_struct *user = get_current_user();
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700673 unsigned long user_bufs;
Eric Biggersf7340762018-02-06 15:42:08 -0800674 unsigned int max_size = READ_ONCE(pipe_max_size);
Ingo Molnar3a326a22006-04-10 15:18:35 +0200675
Vladimir Davydovd86133b2016-07-26 15:24:33 -0700676 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL_ACCOUNT);
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700677 if (pipe == NULL)
678 goto out_free_uid;
Willy Tarreau759c0112016-01-18 16:36:09 +0100679
Eric Biggersf7340762018-02-06 15:42:08 -0800680 if (pipe_bufs * PAGE_SIZE > max_size && !capable(CAP_SYS_RESOURCE))
681 pipe_bufs = max_size >> PAGE_SHIFT;
Michael Kerrisk (man-pages)086e7742016-10-11 13:53:43 -0700682
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700683 user_bufs = account_pipe_buffers(user, 0, pipe_bufs);
Michael Kerrisk (man-pages)a005ca02016-10-11 13:53:37 -0700684
Eric Biggers85c2dd52018-02-06 15:41:53 -0800685 if (too_many_pipe_buffers_soft(user_bufs) && is_unprivileged_user()) {
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700686 user_bufs = account_pipe_buffers(user, pipe_bufs, 1);
Michael Kerrisk (man-pages)a005ca02016-10-11 13:53:37 -0700687 pipe_bufs = 1;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200688 }
689
Eric Biggers85c2dd52018-02-06 15:41:53 -0800690 if (too_many_pipe_buffers_hard(user_bufs) && is_unprivileged_user())
Michael Kerrisk (man-pages)a005ca02016-10-11 13:53:37 -0700691 goto out_revert_acct;
692
693 pipe->bufs = kcalloc(pipe_bufs, sizeof(struct pipe_buffer),
694 GFP_KERNEL_ACCOUNT);
695
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700696 if (pipe->bufs) {
697 init_waitqueue_head(&pipe->wait);
698 pipe->r_counter = pipe->w_counter = 1;
David Howells8cefc102019-11-15 13:30:32 +0000699 pipe->ring_size = pipe_bufs;
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700700 pipe->user = user;
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700701 mutex_init(&pipe->mutex);
702 return pipe;
703 }
704
Michael Kerrisk (man-pages)a005ca02016-10-11 13:53:37 -0700705out_revert_acct:
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -0700706 (void) account_pipe_buffers(user, pipe_bufs, 0);
Michael Kerrisk (man-pages)09b4d192016-10-11 13:53:34 -0700707 kfree(pipe);
708out_free_uid:
709 free_uid(user);
Jens Axboe35f3d142010-05-20 10:43:18 +0200710 return NULL;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200711}
712
Al Viro4b8a8f12013-03-21 11:06:46 -0400713void free_pipe_info(struct pipe_inode_info *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
David Howells8cefc102019-11-15 13:30:32 +0000717 (void) account_pipe_buffers(pipe->user, pipe->ring_size, 0);
Willy Tarreau759c0112016-01-18 16:36:09 +0100718 free_uid(pipe->user);
David Howells8cefc102019-11-15 13:30:32 +0000719 for (i = 0; i < pipe->ring_size; i++) {
Ingo Molnar923f4f22006-04-11 13:53:33 +0200720 struct pipe_buffer *buf = pipe->bufs + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (buf->ops)
Miklos Szeredia7796382016-09-27 10:45:12 +0200722 pipe_buf_release(pipe, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
Ingo Molnar923f4f22006-04-11 13:53:33 +0200724 if (pipe->tmp_page)
725 __free_page(pipe->tmp_page);
Jens Axboe35f3d142010-05-20 10:43:18 +0200726 kfree(pipe->bufs);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200727 kfree(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800730static struct vfsmount *pipe_mnt __read_mostly;
Ingo Molnar341b4462006-04-11 13:57:45 +0200731
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700732/*
733 * pipefs_dname() is called from d_path().
734 */
735static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
736{
737 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
David Howells75c3cfa2015-03-17 22:26:12 +0000738 d_inode(dentry)->i_ino);
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700739}
740
Al Viro3ba13d12009-02-20 06:02:22 +0000741static const struct dentry_operations pipefs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700742 .d_dname = pipefs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743};
744
745static struct inode * get_pipe_inode(void)
746{
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200747 struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200748 struct pipe_inode_info *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 if (!inode)
751 goto fail_inode;
752
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400753 inode->i_ino = get_next_ino();
754
Al Viro7bee1302013-03-21 11:04:15 -0400755 pipe = alloc_pipe_info();
Ingo Molnar923f4f22006-04-11 13:53:33 +0200756 if (!pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 goto fail_iput;
Ingo Molnar3a326a22006-04-10 15:18:35 +0200758
Al Viroba5bb142013-03-21 02:21:19 -0400759 inode->i_pipe = pipe;
760 pipe->files = 2;
Ingo Molnar923f4f22006-04-11 13:53:33 +0200761 pipe->readers = pipe->writers = 1;
Al Viro599a0ac2013-03-12 09:58:10 -0400762 inode->i_fop = &pipefifo_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 /*
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.
769 */
770 inode->i_state = I_DIRTY;
771 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
David Howellsda9592e2008-11-14 10:39:05 +1100772 inode->i_uid = current_fsuid();
773 inode->i_gid = current_fsgid();
Deepa Dinamani078cd822016-09-14 07:48:04 -0700774 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Ingo Molnar923f4f22006-04-11 13:53:33 +0200775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return inode;
777
778fail_iput:
779 iput(inode);
Ingo Molnar341b4462006-04-11 13:57:45 +0200780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781fail_inode:
782 return NULL;
783}
784
Al Viroe4fad8e2012-07-21 15:33:25 +0400785int create_pipe_files(struct file **res, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Al Viroe4fad8e2012-07-21 15:33:25 +0400787 struct inode *inode = get_pipe_inode();
Andi Kleend6cbd282006-09-30 23:29:26 -0700788 struct file *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (!inode)
Al Viroe4fad8e2012-07-21 15:33:25 +0400791 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Al Viro152b6372018-06-09 10:05:18 -0400793 f = alloc_file_pseudo(inode, pipe_mnt, "",
794 O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT)),
795 &pipefifo_fops);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500796 if (IS_ERR(f)) {
Al Viro152b6372018-06-09 10:05:18 -0400797 free_pipe_info(inode->i_pipe);
798 iput(inode);
799 return PTR_ERR(f);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Al Virode32ec42013-03-21 11:16:56 -0400802 f->private_data = inode->i_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Al Viro183266f2018-06-17 14:15:10 -0400804 res[0] = alloc_file_clone(f, O_RDONLY | (flags & O_NONBLOCK),
805 &pipefifo_fops);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500806 if (IS_ERR(res[0])) {
Al Virob10a4a92018-07-09 02:29:58 -0400807 put_pipe_info(inode, inode->i_pipe);
808 fput(f);
809 return PTR_ERR(res[0]);
Eric Biggerse9bb1f92015-10-17 16:26:08 -0500810 }
Al Virode32ec42013-03-21 11:16:56 -0400811 res[0]->private_data = inode->i_pipe;
Al Viroe4fad8e2012-07-21 15:33:25 +0400812 res[1] = f;
813 return 0;
Andi Kleend6cbd282006-09-30 23:29:26 -0700814}
815
Al Viro5b249b12012-08-19 12:17:29 -0400816static int __do_pipe_flags(int *fd, struct file **files, int flags)
Andi Kleend6cbd282006-09-30 23:29:26 -0700817{
Andi Kleend6cbd282006-09-30 23:29:26 -0700818 int error;
819 int fdw, fdr;
820
Linus Torvalds98830352012-04-29 13:12:42 -0700821 if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700822 return -EINVAL;
823
Al Viroe4fad8e2012-07-21 15:33:25 +0400824 error = create_pipe_files(files, flags);
825 if (error)
826 return error;
Andi Kleend6cbd282006-09-30 23:29:26 -0700827
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700828 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -0700829 if (error < 0)
830 goto err_read_pipe;
831 fdr = error;
832
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700833 error = get_unused_fd_flags(flags);
Andi Kleend6cbd282006-09-30 23:29:26 -0700834 if (error < 0)
835 goto err_fdr;
836 fdw = error;
837
Al Viro157cf642008-12-14 04:57:47 -0500838 audit_fd_pair(fdr, fdw);
Andi Kleend6cbd282006-09-30 23:29:26 -0700839 fd[0] = fdr;
840 fd[1] = fdw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return 0;
842
Andi Kleend6cbd282006-09-30 23:29:26 -0700843 err_fdr:
844 put_unused_fd(fdr);
845 err_read_pipe:
Al Viroe4fad8e2012-07-21 15:33:25 +0400846 fput(files[0]);
847 fput(files[1]);
Andi Kleend6cbd282006-09-30 23:29:26 -0700848 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
Al Viro5b249b12012-08-19 12:17:29 -0400851int do_pipe_flags(int *fd, int flags)
852{
853 struct file *files[2];
854 int error = __do_pipe_flags(fd, files, flags);
855 if (!error) {
856 fd_install(fd[0], files[0]);
857 fd_install(fd[1], files[1]);
858 }
859 return error;
860}
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862/*
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400863 * sys_pipe() is the normal C calling standard for creating
864 * a pipe. It's not the way Unix traditionally does this, though.
865 */
Dominik Brodowski0a216dd2018-03-11 11:34:28 +0100866static int do_pipe2(int __user *fildes, int flags)
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400867{
Al Viro5b249b12012-08-19 12:17:29 -0400868 struct file *files[2];
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400869 int fd[2];
870 int error;
871
Al Viro5b249b12012-08-19 12:17:29 -0400872 error = __do_pipe_flags(fd, files, flags);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400873 if (!error) {
Al Viro5b249b12012-08-19 12:17:29 -0400874 if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
875 fput(files[0]);
876 fput(files[1]);
877 put_unused_fd(fd[0]);
878 put_unused_fd(fd[1]);
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400879 error = -EFAULT;
Al Viro5b249b12012-08-19 12:17:29 -0400880 } else {
881 fd_install(fd[0], files[0]);
882 fd_install(fd[1], files[1]);
Ulrich Drepperba719ba2008-05-06 20:42:38 -0700883 }
Ulrich Drepperd35c7b02008-05-03 15:10:37 -0400884 }
885 return error;
886}
887
Dominik Brodowski0a216dd2018-03-11 11:34:28 +0100888SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
889{
890 return do_pipe2(fildes, flags);
891}
892
Heiko Carstens2b664212009-01-14 14:14:35 +0100893SYSCALL_DEFINE1(pipe, int __user *, fildes)
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700894{
Dominik Brodowski0a216dd2018-03-11 11:34:28 +0100895 return do_pipe2(fildes, 0);
Ulrich Dreppered8cae82008-07-23 21:29:30 -0700896}
897
Al Virofc7478a2013-03-21 02:07:59 -0400898static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
Al Virof776c732013-03-12 09:46:27 -0400899{
David Howells8cefc102019-11-15 13:30:32 +0000900 int cur = *cnt;
Al Virof776c732013-03-12 09:46:27 -0400901
902 while (cur == *cnt) {
Al Virofc7478a2013-03-21 02:07:59 -0400903 pipe_wait(pipe);
Al Virof776c732013-03-12 09:46:27 -0400904 if (signal_pending(current))
905 break;
906 }
907 return cur == *cnt ? -ERESTARTSYS : 0;
908}
909
Al Virofc7478a2013-03-21 02:07:59 -0400910static void wake_up_partner(struct pipe_inode_info *pipe)
Al Virof776c732013-03-12 09:46:27 -0400911{
Al Virofc7478a2013-03-21 02:07:59 -0400912 wake_up_interruptible(&pipe->wait);
Al Virof776c732013-03-12 09:46:27 -0400913}
914
915static int fifo_open(struct inode *inode, struct file *filp)
916{
917 struct pipe_inode_info *pipe;
Al Viro599a0ac2013-03-12 09:58:10 -0400918 bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
Al Virof776c732013-03-12 09:46:27 -0400919 int ret;
920
Al Viroba5bb142013-03-21 02:21:19 -0400921 filp->f_version = 0;
922
923 spin_lock(&inode->i_lock);
924 if (inode->i_pipe) {
925 pipe = inode->i_pipe;
926 pipe->files++;
927 spin_unlock(&inode->i_lock);
928 } else {
929 spin_unlock(&inode->i_lock);
Al Viro7bee1302013-03-21 11:04:15 -0400930 pipe = alloc_pipe_info();
Al Virof776c732013-03-12 09:46:27 -0400931 if (!pipe)
Al Viroba5bb142013-03-21 02:21:19 -0400932 return -ENOMEM;
933 pipe->files = 1;
934 spin_lock(&inode->i_lock);
935 if (unlikely(inode->i_pipe)) {
936 inode->i_pipe->files++;
937 spin_unlock(&inode->i_lock);
Al Viro4b8a8f12013-03-21 11:06:46 -0400938 free_pipe_info(pipe);
Al Viroba5bb142013-03-21 02:21:19 -0400939 pipe = inode->i_pipe;
940 } else {
941 inode->i_pipe = pipe;
942 spin_unlock(&inode->i_lock);
943 }
Al Virof776c732013-03-12 09:46:27 -0400944 }
Al Virode32ec42013-03-21 11:16:56 -0400945 filp->private_data = pipe;
Al Viroba5bb142013-03-21 02:21:19 -0400946 /* OK, we have a pipe and it's pinned down */
947
Al Viroebec73f2013-03-21 12:24:01 -0400948 __pipe_lock(pipe);
Al Virof776c732013-03-12 09:46:27 -0400949
950 /* We can only do regular read/write on fifos */
951 filp->f_mode &= (FMODE_READ | FMODE_WRITE);
952
953 switch (filp->f_mode) {
954 case FMODE_READ:
955 /*
956 * O_RDONLY
957 * POSIX.1 says that O_NONBLOCK means return with the FIFO
958 * opened, even when there is no process writing the FIFO.
959 */
Al Virof776c732013-03-12 09:46:27 -0400960 pipe->r_counter++;
961 if (pipe->readers++ == 0)
Al Virofc7478a2013-03-21 02:07:59 -0400962 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400963
Al Viro599a0ac2013-03-12 09:58:10 -0400964 if (!is_pipe && !pipe->writers) {
Al Virof776c732013-03-12 09:46:27 -0400965 if ((filp->f_flags & O_NONBLOCK)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800966 /* suppress EPOLLHUP until we have
Al Virof776c732013-03-12 09:46:27 -0400967 * seen a writer */
968 filp->f_version = pipe->w_counter;
969 } else {
Al Virofc7478a2013-03-21 02:07:59 -0400970 if (wait_for_partner(pipe, &pipe->w_counter))
Al Virof776c732013-03-12 09:46:27 -0400971 goto err_rd;
972 }
973 }
974 break;
David Howells8cefc102019-11-15 13:30:32 +0000975
Al Virof776c732013-03-12 09:46:27 -0400976 case FMODE_WRITE:
977 /*
978 * O_WRONLY
979 * POSIX.1 says that O_NONBLOCK means return -1 with
980 * errno=ENXIO when there is no process reading the FIFO.
981 */
982 ret = -ENXIO;
Al Viro599a0ac2013-03-12 09:58:10 -0400983 if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
Al Virof776c732013-03-12 09:46:27 -0400984 goto err;
985
Al Virof776c732013-03-12 09:46:27 -0400986 pipe->w_counter++;
987 if (!pipe->writers++)
Al Virofc7478a2013-03-21 02:07:59 -0400988 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -0400989
Al Viro599a0ac2013-03-12 09:58:10 -0400990 if (!is_pipe && !pipe->readers) {
Al Virofc7478a2013-03-21 02:07:59 -0400991 if (wait_for_partner(pipe, &pipe->r_counter))
Al Virof776c732013-03-12 09:46:27 -0400992 goto err_wr;
993 }
994 break;
David Howells8cefc102019-11-15 13:30:32 +0000995
Al Virof776c732013-03-12 09:46:27 -0400996 case FMODE_READ | FMODE_WRITE:
997 /*
998 * O_RDWR
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.
1002 */
Al Virof776c732013-03-12 09:46:27 -04001003
1004 pipe->readers++;
1005 pipe->writers++;
1006 pipe->r_counter++;
1007 pipe->w_counter++;
1008 if (pipe->readers == 1 || pipe->writers == 1)
Al Virofc7478a2013-03-21 02:07:59 -04001009 wake_up_partner(pipe);
Al Virof776c732013-03-12 09:46:27 -04001010 break;
1011
1012 default:
1013 ret = -EINVAL;
1014 goto err;
1015 }
1016
1017 /* Ok! */
Al Viroebec73f2013-03-21 12:24:01 -04001018 __pipe_unlock(pipe);
Al Virof776c732013-03-12 09:46:27 -04001019 return 0;
1020
1021err_rd:
1022 if (!--pipe->readers)
1023 wake_up_interruptible(&pipe->wait);
1024 ret = -ERESTARTSYS;
1025 goto err;
1026
1027err_wr:
1028 if (!--pipe->writers)
1029 wake_up_interruptible(&pipe->wait);
1030 ret = -ERESTARTSYS;
1031 goto err;
1032
1033err:
Al Viroebec73f2013-03-21 12:24:01 -04001034 __pipe_unlock(pipe);
Linus Torvaldsb0d8d222013-12-02 09:44:51 -08001035
1036 put_pipe_info(inode, pipe);
Al Virof776c732013-03-12 09:46:27 -04001037 return ret;
1038}
1039
Al Viro599a0ac2013-03-12 09:58:10 -04001040const struct file_operations pipefifo_fops = {
1041 .open = fifo_open,
1042 .llseek = no_llseek,
Al Virofb9096a2014-04-02 19:56:54 -04001043 .read_iter = pipe_read,
Al Virof0d1bec2014-04-03 15:05:18 -04001044 .write_iter = pipe_write,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001045 .poll = pipe_poll,
Al Viro599a0ac2013-03-12 09:58:10 -04001046 .unlocked_ioctl = pipe_ioctl,
1047 .release = pipe_release,
1048 .fasync = pipe_fasync,
Al Virof776c732013-03-12 09:46:27 -04001049};
1050
Ulrich Drepperd35c7b02008-05-03 15:10:37 -04001051/*
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001052 * Currently we rely on the pipe array holding a power-of-2 number
Joe Lawrenced3f14c42017-11-17 15:29:21 -08001053 * of pages. Returns 0 on error.
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001054 */
Eric Biggers96e99be402018-02-06 15:42:00 -08001055unsigned int round_pipe_size(unsigned long size)
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001056{
Eric Biggersc4fed5a2018-02-06 15:42:05 -08001057 if (size > (1U << 31))
Eric Biggers96e99be402018-02-06 15:42:00 -08001058 return 0;
1059
Eric Biggers4c2e4be2018-02-06 15:41:45 -08001060 /* Minimum pipe size, as required by POSIX */
1061 if (size < PAGE_SIZE)
Eric Biggersc4fed5a2018-02-06 15:42:05 -08001062 return PAGE_SIZE;
Joe Lawrenced3f14c42017-11-17 15:29:21 -08001063
Eric Biggersc4fed5a2018-02-06 15:42:05 -08001064 return roundup_pow_of_two(size);
Michael Kerrisk (man-pages)f491bd72016-10-11 13:53:22 -07001065}
1066
1067/*
Jens Axboe35f3d142010-05-20 10:43:18 +02001068 * Allocate a new array of pipe buffers and copy the info over. Returns the
1069 * pipe size if successful, or return -ERROR on error.
1070 */
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001071static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long arg)
Jens Axboe35f3d142010-05-20 10:43:18 +02001072{
1073 struct pipe_buffer *bufs;
David Howells8cefc102019-11-15 13:30:32 +00001074 unsigned int size, nr_slots, head, tail, mask, n;
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -07001075 unsigned long user_bufs;
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001076 long ret = 0;
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001077
1078 size = round_pipe_size(arg);
David Howells8cefc102019-11-15 13:30:32 +00001079 nr_slots = size >> PAGE_SHIFT;
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001080
David Howells8cefc102019-11-15 13:30:32 +00001081 if (!nr_slots)
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001082 return -EINVAL;
1083
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001084 /*
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.
1090 */
David Howells8cefc102019-11-15 13:30:32 +00001091 if (nr_slots > pipe->ring_size &&
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001092 size > pipe_max_size && !capable(CAP_SYS_RESOURCE))
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001093 return -EPERM;
1094
David Howells8cefc102019-11-15 13:30:32 +00001095 user_bufs = account_pipe_buffers(pipe->user, pipe->ring_size, nr_slots);
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001096
David Howells8cefc102019-11-15 13:30:32 +00001097 if (nr_slots > pipe->ring_size &&
Michael Kerrisk (man-pages)9c87bcf2016-10-11 13:53:40 -07001098 (too_many_pipe_buffers_hard(user_bufs) ||
1099 too_many_pipe_buffers_soft(user_bufs)) &&
Eric Biggers85c2dd52018-02-06 15:41:53 -08001100 is_unprivileged_user()) {
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001101 ret = -EPERM;
1102 goto out_revert_acct;
1103 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001104
1105 /*
David Howells8cefc102019-11-15 13:30:32 +00001106 * 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
Jens Axboe35f3d142010-05-20 10:43:18 +02001109 * contains more buffers than arg, then return busy.
1110 */
David Howells8cefc102019-11-15 13:30:32 +00001111 mask = pipe->ring_size - 1;
1112 head = pipe->head;
1113 tail = pipe->tail;
1114 n = pipe_occupancy(pipe->head, pipe->tail);
1115 if (nr_slots < n) {
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001116 ret = -EBUSY;
1117 goto out_revert_acct;
1118 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001119
David Howells8cefc102019-11-15 13:30:32 +00001120 bufs = kcalloc(nr_slots, sizeof(*bufs),
Vladimir Davydovd86133b2016-07-26 15:24:33 -07001121 GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001122 if (unlikely(!bufs)) {
1123 ret = -ENOMEM;
1124 goto out_revert_acct;
1125 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001126
1127 /*
1128 * The pipe array wraps around, so just start the new one at zero
David Howells8cefc102019-11-15 13:30:32 +00001129 * and adjust the indices.
Jens Axboe35f3d142010-05-20 10:43:18 +02001130 */
David Howells8cefc102019-11-15 13:30:32 +00001131 if (n > 0) {
1132 unsigned int h = head & mask;
1133 unsigned int t = tail & mask;
1134 if (h > t) {
1135 memcpy(bufs, pipe->bufs + t,
1136 n * sizeof(struct pipe_buffer));
1137 } else {
1138 unsigned int tsize = pipe->ring_size - t;
1139 if (h > 0)
1140 memcpy(bufs + tsize, pipe->bufs,
1141 h * sizeof(struct pipe_buffer));
1142 memcpy(bufs, pipe->bufs + t,
1143 tsize * sizeof(struct pipe_buffer));
1144 }
Jens Axboe35f3d142010-05-20 10:43:18 +02001145 }
1146
David Howells8cefc102019-11-15 13:30:32 +00001147 head = n;
1148 tail = 0;
1149
Jens Axboe35f3d142010-05-20 10:43:18 +02001150 kfree(pipe->bufs);
1151 pipe->bufs = bufs;
David Howells8cefc102019-11-15 13:30:32 +00001152 pipe->ring_size = nr_slots;
1153 pipe->tail = tail;
1154 pipe->head = head;
1155 return pipe->ring_size * PAGE_SIZE;
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001156
1157out_revert_acct:
David Howells8cefc102019-11-15 13:30:32 +00001158 (void) account_pipe_buffers(pipe->user, nr_slots, pipe->ring_size);
Michael Kerrisk (man-pages)b0b91d12016-10-11 13:53:31 -07001159 return ret;
Jens Axboe35f3d142010-05-20 10:43:18 +02001160}
1161
Jens Axboeff9da692010-06-03 14:54:39 +02001162/*
Linus Torvalds72083642010-11-28 16:27:19 -08001163 * 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
1165 * pipe.
1166 */
1167struct pipe_inode_info *get_pipe_info(struct file *file)
1168{
Al Virode32ec42013-03-21 11:16:56 -04001169 return file->f_op == &pipefifo_fops ? file->private_data : NULL;
Linus Torvalds72083642010-11-28 16:27:19 -08001170}
1171
Jens Axboe35f3d142010-05-20 10:43:18 +02001172long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1173{
1174 struct pipe_inode_info *pipe;
1175 long ret;
1176
Linus Torvaldsc66fb342010-11-28 14:09:57 -08001177 pipe = get_pipe_info(file);
Jens Axboe35f3d142010-05-20 10:43:18 +02001178 if (!pipe)
1179 return -EBADF;
1180
Al Viroebec73f2013-03-21 12:24:01 -04001181 __pipe_lock(pipe);
Jens Axboe35f3d142010-05-20 10:43:18 +02001182
1183 switch (cmd) {
Michael Kerrisk (man-pages)d37d4162016-10-11 13:53:25 -07001184 case F_SETPIPE_SZ:
1185 ret = pipe_set_size(pipe, arg);
Jens Axboe35f3d142010-05-20 10:43:18 +02001186 break;
1187 case F_GETPIPE_SZ:
David Howells8cefc102019-11-15 13:30:32 +00001188 ret = pipe->ring_size * PAGE_SIZE;
Jens Axboe35f3d142010-05-20 10:43:18 +02001189 break;
1190 default:
1191 ret = -EINVAL;
1192 break;
1193 }
1194
Al Viroebec73f2013-03-21 12:24:01 -04001195 __pipe_unlock(pipe);
Jens Axboe35f3d142010-05-20 10:43:18 +02001196 return ret;
1197}
1198
Nick Pigginff0c7d12011-01-07 17:49:50 +11001199static const struct super_operations pipefs_ops = {
1200 .destroy_inode = free_inode_nonrcu,
Pavel Emelyanovd70ef972011-10-31 17:10:04 -07001201 .statfs = simple_statfs,
Nick Pigginff0c7d12011-01-07 17:49:50 +11001202};
1203
Jens Axboe35f3d142010-05-20 10:43:18 +02001204/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 * 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.
1209 */
David Howells4fa7ec52019-03-25 16:38:23 +00001210
1211static int pipefs_init_fs_context(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
David Howells4fa7ec52019-03-25 16:38:23 +00001213 struct pseudo_fs_context *ctx = init_pseudo(fc, PIPEFS_MAGIC);
1214 if (!ctx)
1215 return -ENOMEM;
1216 ctx->ops = &pipefs_ops;
1217 ctx->dops = &pipefs_dentry_operations;
1218 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
1221static struct file_system_type pipe_fs_type = {
1222 .name = "pipefs",
David Howells4fa7ec52019-03-25 16:38:23 +00001223 .init_fs_context = pipefs_init_fs_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 .kill_sb = kill_anon_super,
1225};
1226
1227static int __init init_pipe_fs(void)
1228{
1229 int err = register_filesystem(&pipe_fs_type);
Ingo Molnar341b4462006-04-11 13:57:45 +02001230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (!err) {
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);
1236 }
1237 }
1238 return err;
1239}
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241fs_initcall(init_pipe_fs);