1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * linux/fs/pipe.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Copyright (C) 1991, 1992, 1999 Linus Torvalds
61da177e4SLinus Torvalds */
71da177e4SLinus Torvalds
81da177e4SLinus Torvalds #include <linux/mm.h>
91da177e4SLinus Torvalds #include <linux/file.h>
101da177e4SLinus Torvalds #include <linux/poll.h>
111da177e4SLinus Torvalds #include <linux/slab.h>
121da177e4SLinus Torvalds #include <linux/module.h>
131da177e4SLinus Torvalds #include <linux/init.h>
141da177e4SLinus Torvalds #include <linux/fs.h>
1535f3d14dSJens Axboe #include <linux/log2.h>
161da177e4SLinus Torvalds #include <linux/mount.h>
174fa7ec5dSDavid Howells #include <linux/pseudo_fs.h>
18b502bd11SMuthu Kumar #include <linux/magic.h>
191da177e4SLinus Torvalds #include <linux/pipe_fs_i.h>
201da177e4SLinus Torvalds #include <linux/uio.h>
211da177e4SLinus Torvalds #include <linux/highmem.h>
225274f052SJens Axboe #include <linux/pagemap.h>
23db349509SAl Viro #include <linux/audit.h>
24ba719baeSUlrich Drepper #include <linux/syscalls.h>
25b492e95bSJens Axboe #include <linux/fcntl.h>
26d86133bdSVladimir Davydov #include <linux/memcontrol.h>
27c73be61cSDavid Howells #include <linux/watch_queue.h>
281998f193SLuis Chamberlain #include <linux/sysctl.h>
291da177e4SLinus Torvalds
307c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
311da177e4SLinus Torvalds #include <asm/ioctls.h>
321da177e4SLinus Torvalds
33599a0ac1SAl Viro #include "internal.h"
34599a0ac1SAl Viro
351da177e4SLinus Torvalds /*
3646c4c9d1SAlex Xu (Hello71) * New pipe buffers will be restricted to this size while the user is exceeding
3746c4c9d1SAlex Xu (Hello71) * their pipe buffer quota. The general pipe use case needs at least two
3846c4c9d1SAlex Xu (Hello71) * buffers: one for data yet to be read, and one for new data. If this is less
3946c4c9d1SAlex Xu (Hello71) * than two, then a write to a non-empty pipe may block even if the pipe is not
4046c4c9d1SAlex Xu (Hello71) * full. This can occur with GNU make jobserver or similar uses of pipes as
4146c4c9d1SAlex Xu (Hello71) * semaphores: multiple processes may be waiting to write tokens back to the
4246c4c9d1SAlex Xu (Hello71) * pipe before reading tokens: https://lore.kernel.org/lkml/1628086770.5rn8p04n6j.none@localhost/.
4346c4c9d1SAlex Xu (Hello71) *
4446c4c9d1SAlex Xu (Hello71) * Users can reduce their pipe buffers with F_SETPIPE_SZ below this at their
4546c4c9d1SAlex Xu (Hello71) * own risk, namely: pipe writes to non-full pipes may block until the pipe is
4646c4c9d1SAlex Xu (Hello71) * emptied.
4746c4c9d1SAlex Xu (Hello71) */
4846c4c9d1SAlex Xu (Hello71) #define PIPE_MIN_DEF_BUFFERS 2
4946c4c9d1SAlex Xu (Hello71)
5046c4c9d1SAlex Xu (Hello71) /*
51b492e95bSJens Axboe * The max size that a non-root user is allowed to grow the pipe. Can
52ff9da691SJens Axboe * be set by root in /proc/sys/fs/pipe-max-size
53b492e95bSJens Axboe */
541998f193SLuis Chamberlain static unsigned int pipe_max_size = 1048576;
55ff9da691SJens Axboe
56759c0114SWilly Tarreau /* Maximum allocatable pages per user. Hard limit is unset by default, soft
57759c0114SWilly Tarreau * matches default values.
58759c0114SWilly Tarreau */
591998f193SLuis Chamberlain static unsigned long pipe_user_pages_hard;
601998f193SLuis Chamberlain static unsigned long pipe_user_pages_soft = PIPE_DEF_BUFFERS * INR_OPEN_CUR;
61759c0114SWilly Tarreau
62b492e95bSJens Axboe /*
638cefc107SDavid Howells * We use head and tail indices that aren't masked off, except at the point of
648cefc107SDavid Howells * dereference, but rather they're allowed to wrap naturally. This means there
658cefc107SDavid Howells * isn't a dead spot in the buffer, but the ring has to be a power of two and
668cefc107SDavid Howells * <= 2^31.
678cefc107SDavid Howells * -- David Howells 2019-09-23.
681da177e4SLinus Torvalds *
691da177e4SLinus Torvalds * Reads with count = 0 should always return 0.
701da177e4SLinus Torvalds * -- Julian Bradfield 1999-06-07.
711da177e4SLinus Torvalds *
721da177e4SLinus Torvalds * FIFOs and Pipes now generate SIGIO for both readers and writers.
731da177e4SLinus Torvalds * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
741da177e4SLinus Torvalds *
751da177e4SLinus Torvalds * pipe_read & write cleanup
761da177e4SLinus Torvalds * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
771da177e4SLinus Torvalds */
781da177e4SLinus Torvalds
pipe_lock_nested(struct pipe_inode_info * pipe,int subclass)7961e0d47cSMiklos Szeredi static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass)
8061e0d47cSMiklos Szeredi {
816447a3cfSAl Viro if (pipe->files)
8272b0d9aaSAl Viro mutex_lock_nested(&pipe->mutex, subclass);
8361e0d47cSMiklos Szeredi }
8461e0d47cSMiklos Szeredi
pipe_lock(struct pipe_inode_info * pipe)8561e0d47cSMiklos Szeredi void pipe_lock(struct pipe_inode_info *pipe)
8661e0d47cSMiklos Szeredi {
8761e0d47cSMiklos Szeredi /*
8861e0d47cSMiklos Szeredi * pipe_lock() nests non-pipe inode locks (for writing to a file)
8961e0d47cSMiklos Szeredi */
9061e0d47cSMiklos Szeredi pipe_lock_nested(pipe, I_MUTEX_PARENT);
9161e0d47cSMiklos Szeredi }
9261e0d47cSMiklos Szeredi EXPORT_SYMBOL(pipe_lock);
9361e0d47cSMiklos Szeredi
pipe_unlock(struct pipe_inode_info * pipe)9461e0d47cSMiklos Szeredi void pipe_unlock(struct pipe_inode_info *pipe)
9561e0d47cSMiklos Szeredi {
966447a3cfSAl Viro if (pipe->files)
9772b0d9aaSAl Viro mutex_unlock(&pipe->mutex);
9861e0d47cSMiklos Szeredi }
9961e0d47cSMiklos Szeredi EXPORT_SYMBOL(pipe_unlock);
10061e0d47cSMiklos Szeredi
__pipe_lock(struct pipe_inode_info * pipe)101ebec73f4SAl Viro static inline void __pipe_lock(struct pipe_inode_info *pipe)
102ebec73f4SAl Viro {
103ebec73f4SAl Viro mutex_lock_nested(&pipe->mutex, I_MUTEX_PARENT);
104ebec73f4SAl Viro }
105ebec73f4SAl Viro
__pipe_unlock(struct pipe_inode_info * pipe)106ebec73f4SAl Viro static inline void __pipe_unlock(struct pipe_inode_info *pipe)
107ebec73f4SAl Viro {
108ebec73f4SAl Viro mutex_unlock(&pipe->mutex);
109ebec73f4SAl Viro }
110ebec73f4SAl Viro
pipe_double_lock(struct pipe_inode_info * pipe1,struct pipe_inode_info * pipe2)11161e0d47cSMiklos Szeredi void pipe_double_lock(struct pipe_inode_info *pipe1,
11261e0d47cSMiklos Szeredi struct pipe_inode_info *pipe2)
11361e0d47cSMiklos Szeredi {
11461e0d47cSMiklos Szeredi BUG_ON(pipe1 == pipe2);
11561e0d47cSMiklos Szeredi
11661e0d47cSMiklos Szeredi if (pipe1 < pipe2) {
11761e0d47cSMiklos Szeredi pipe_lock_nested(pipe1, I_MUTEX_PARENT);
11861e0d47cSMiklos Szeredi pipe_lock_nested(pipe2, I_MUTEX_CHILD);
11961e0d47cSMiklos Szeredi } else {
120023d43c7SPeter Zijlstra pipe_lock_nested(pipe2, I_MUTEX_PARENT);
121023d43c7SPeter Zijlstra pipe_lock_nested(pipe1, I_MUTEX_CHILD);
12261e0d47cSMiklos Szeredi }
12361e0d47cSMiklos Szeredi }
12461e0d47cSMiklos Szeredi
anon_pipe_buf_release(struct pipe_inode_info * pipe,struct pipe_buffer * buf)125341b446bSIngo Molnar static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
126341b446bSIngo Molnar struct pipe_buffer *buf)
1271da177e4SLinus Torvalds {
1281da177e4SLinus Torvalds struct page *page = buf->page;
1291da177e4SLinus Torvalds
1305274f052SJens Axboe /*
1315274f052SJens Axboe * If nobody else uses this page, and we don't already have a
1325274f052SJens Axboe * temporary page, let's keep track of it as a one-deep
133341b446bSIngo Molnar * allocation cache. (Otherwise just release our reference to it)
1345274f052SJens Axboe */
135341b446bSIngo Molnar if (page_count(page) == 1 && !pipe->tmp_page)
136923f4f23SIngo Molnar pipe->tmp_page = page;
137341b446bSIngo Molnar else
13809cbfeafSKirill A. Shutemov put_page(page);
1391da177e4SLinus Torvalds }
1401da177e4SLinus Torvalds
anon_pipe_buf_try_steal(struct pipe_inode_info * pipe,struct pipe_buffer * buf)141c928f642SChristoph Hellwig static bool anon_pipe_buf_try_steal(struct pipe_inode_info *pipe,
142d86133bdSVladimir Davydov struct pipe_buffer *buf)
143d86133bdSVladimir Davydov {
144d86133bdSVladimir Davydov struct page *page = buf->page;
145d86133bdSVladimir Davydov
146c928f642SChristoph Hellwig if (page_count(page) != 1)
147c928f642SChristoph Hellwig return false;
148f4b00eabSRoman Gushchin memcg_kmem_uncharge_page(page, 0);
149d86133bdSVladimir Davydov __SetPageLocked(page);
150c928f642SChristoph Hellwig return true;
151d86133bdSVladimir Davydov }
152d86133bdSVladimir Davydov
1530845718dSJens Axboe /**
154c928f642SChristoph Hellwig * generic_pipe_buf_try_steal - attempt to take ownership of a &pipe_buffer
1550845718dSJens Axboe * @pipe: the pipe that the buffer belongs to
1560845718dSJens Axboe * @buf: the buffer to attempt to steal
1570845718dSJens Axboe *
1580845718dSJens Axboe * Description:
159b51d63c6SRandy Dunlap * This function attempts to steal the &struct page attached to
1600845718dSJens Axboe * @buf. If successful, this function returns 0 and returns with
1610845718dSJens Axboe * the page locked. The caller may then reuse the page for whatever
162b51d63c6SRandy Dunlap * he wishes; the typical use is insertion into a different file
1630845718dSJens Axboe * page cache.
1640845718dSJens Axboe */
generic_pipe_buf_try_steal(struct pipe_inode_info * pipe,struct pipe_buffer * buf)165c928f642SChristoph Hellwig bool generic_pipe_buf_try_steal(struct pipe_inode_info *pipe,
1665abc97aaSJens Axboe struct pipe_buffer *buf)
1675abc97aaSJens Axboe {
16846e678c9SJens Axboe struct page *page = buf->page;
16946e678c9SJens Axboe
1700845718dSJens Axboe /*
1710845718dSJens Axboe * A reference of one is golden, that means that the owner of this
1720845718dSJens Axboe * page is the only one holding a reference to it. lock the page
1730845718dSJens Axboe * and return OK.
1740845718dSJens Axboe */
17546e678c9SJens Axboe if (page_count(page) == 1) {
17646e678c9SJens Axboe lock_page(page);
177c928f642SChristoph Hellwig return true;
1785abc97aaSJens Axboe }
179c928f642SChristoph Hellwig return false;
18046e678c9SJens Axboe }
181c928f642SChristoph Hellwig EXPORT_SYMBOL(generic_pipe_buf_try_steal);
18246e678c9SJens Axboe
1830845718dSJens Axboe /**
184b51d63c6SRandy Dunlap * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
1850845718dSJens Axboe * @pipe: the pipe that the buffer belongs to
1860845718dSJens Axboe * @buf: the buffer to get a reference to
1870845718dSJens Axboe *
1880845718dSJens Axboe * Description:
1890845718dSJens Axboe * This function grabs an extra reference to @buf. It's used in
1903d742d4bSRandy Dunlap * the tee() system call, when we duplicate the buffers in one
1910845718dSJens Axboe * pipe into another.
1920845718dSJens Axboe */
generic_pipe_buf_get(struct pipe_inode_info * pipe,struct pipe_buffer * buf)19315fab63eSMatthew Wilcox bool generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
19470524490SJens Axboe {
195cd1adf1bSLinus Torvalds return try_get_page(buf->page);
19670524490SJens Axboe }
19751921cb7SMiklos Szeredi EXPORT_SYMBOL(generic_pipe_buf_get);
19870524490SJens Axboe
1990845718dSJens Axboe /**
2006818173bSMiklos Szeredi * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
2016818173bSMiklos Szeredi * @pipe: the pipe that the buffer belongs to
2026818173bSMiklos Szeredi * @buf: the buffer to put a reference to
2036818173bSMiklos Szeredi *
2046818173bSMiklos Szeredi * Description:
2056818173bSMiklos Szeredi * This function releases a reference to @buf.
2066818173bSMiklos Szeredi */
generic_pipe_buf_release(struct pipe_inode_info * pipe,struct pipe_buffer * buf)2076818173bSMiklos Szeredi void generic_pipe_buf_release(struct pipe_inode_info *pipe,
2086818173bSMiklos Szeredi struct pipe_buffer *buf)
2096818173bSMiklos Szeredi {
21009cbfeafSKirill A. Shutemov put_page(buf->page);
2116818173bSMiklos Szeredi }
21251921cb7SMiklos Szeredi EXPORT_SYMBOL(generic_pipe_buf_release);
2136818173bSMiklos Szeredi
214d4c3cca9SEric Dumazet static const struct pipe_buf_operations anon_pipe_buf_ops = {
2151da177e4SLinus Torvalds .release = anon_pipe_buf_release,
216c928f642SChristoph Hellwig .try_steal = anon_pipe_buf_try_steal,
217f84d7519SJens Axboe .get = generic_pipe_buf_get,
2181da177e4SLinus Torvalds };
2191da177e4SLinus Torvalds
22085190d15SLinus Torvalds /* Done while waiting without holding the pipe lock - thus the READ_ONCE() */
pipe_readable(const struct pipe_inode_info * pipe)22185190d15SLinus Torvalds static inline bool pipe_readable(const struct pipe_inode_info *pipe)
22285190d15SLinus Torvalds {
22385190d15SLinus Torvalds unsigned int head = READ_ONCE(pipe->head);
22485190d15SLinus Torvalds unsigned int tail = READ_ONCE(pipe->tail);
22585190d15SLinus Torvalds unsigned int writers = READ_ONCE(pipe->writers);
22685190d15SLinus Torvalds
22785190d15SLinus Torvalds return !pipe_empty(head, tail) || !writers;
22885190d15SLinus Torvalds }
22985190d15SLinus Torvalds
2301da177e4SLinus Torvalds static ssize_t
pipe_read(struct kiocb * iocb,struct iov_iter * to)231fb9096a3SAl Viro pipe_read(struct kiocb *iocb, struct iov_iter *to)
2321da177e4SLinus Torvalds {
233fb9096a3SAl Viro size_t total_len = iov_iter_count(to);
234ee0b3e67SBadari Pulavarty struct file *filp = iocb->ki_filp;
235de32ec4cSAl Viro struct pipe_inode_info *pipe = filp->private_data;
2360ddad21dSLinus Torvalds bool was_full, wake_next_reader = false;
2371da177e4SLinus Torvalds ssize_t ret;
2381da177e4SLinus Torvalds
2391da177e4SLinus Torvalds /* Null read succeeds. */
2401da177e4SLinus Torvalds if (unlikely(total_len == 0))
2411da177e4SLinus Torvalds return 0;
2421da177e4SLinus Torvalds
2431da177e4SLinus Torvalds ret = 0;
244ebec73f4SAl Viro __pipe_lock(pipe);
245f467a6a6SLinus Torvalds
246f467a6a6SLinus Torvalds /*
247f467a6a6SLinus Torvalds * We only wake up writers if the pipe was full when we started
248f467a6a6SLinus Torvalds * reading in order to avoid unnecessary wakeups.
249f467a6a6SLinus Torvalds *
250f467a6a6SLinus Torvalds * But when we do wake up writers, we do so using a sync wakeup
251f467a6a6SLinus Torvalds * (WF_SYNC), because we want them to get going and generate more
252f467a6a6SLinus Torvalds * data for us.
253f467a6a6SLinus Torvalds */
254f467a6a6SLinus Torvalds was_full = pipe_full(pipe->head, pipe->tail, pipe->max_usage);
2551da177e4SLinus Torvalds for (;;) {
2562ed147f0SDavid Howells /* Read ->head with a barrier vs post_one_notification() */
2572ed147f0SDavid Howells unsigned int head = smp_load_acquire(&pipe->head);
2588cefc107SDavid Howells unsigned int tail = pipe->tail;
2598cefc107SDavid Howells unsigned int mask = pipe->ring_size - 1;
2608cefc107SDavid Howells
261e7d553d6SDavid Howells #ifdef CONFIG_WATCH_QUEUE
262e7d553d6SDavid Howells if (pipe->note_loss) {
263e7d553d6SDavid Howells struct watch_notification n;
264e7d553d6SDavid Howells
265e7d553d6SDavid Howells if (total_len < 8) {
266e7d553d6SDavid Howells if (ret == 0)
267e7d553d6SDavid Howells ret = -ENOBUFS;
268e7d553d6SDavid Howells break;
269e7d553d6SDavid Howells }
270e7d553d6SDavid Howells
271e7d553d6SDavid Howells n.type = WATCH_TYPE_META;
272e7d553d6SDavid Howells n.subtype = WATCH_META_LOSS_NOTIFICATION;
273e7d553d6SDavid Howells n.info = watch_sizeof(n);
274e7d553d6SDavid Howells if (copy_to_iter(&n, sizeof(n), to) != sizeof(n)) {
275e7d553d6SDavid Howells if (ret == 0)
276e7d553d6SDavid Howells ret = -EFAULT;
277e7d553d6SDavid Howells break;
278e7d553d6SDavid Howells }
279e7d553d6SDavid Howells ret += sizeof(n);
280e7d553d6SDavid Howells total_len -= sizeof(n);
281e7d553d6SDavid Howells pipe->note_loss = false;
282e7d553d6SDavid Howells }
283e7d553d6SDavid Howells #endif
284e7d553d6SDavid Howells
2858cefc107SDavid Howells if (!pipe_empty(head, tail)) {
2868cefc107SDavid Howells struct pipe_buffer *buf = &pipe->bufs[tail & mask];
2871da177e4SLinus Torvalds size_t chars = buf->len;
288637b58c2SAl Viro size_t written;
289637b58c2SAl Viro int error;
2901da177e4SLinus Torvalds
2918cfba763SDavid Howells if (chars > total_len) {
2928cfba763SDavid Howells if (buf->flags & PIPE_BUF_FLAG_WHOLE) {
2938cfba763SDavid Howells if (ret == 0)
2948cfba763SDavid Howells ret = -ENOBUFS;
2958cfba763SDavid Howells break;
2968cfba763SDavid Howells }
2971da177e4SLinus Torvalds chars = total_len;
2988cfba763SDavid Howells }
2991da177e4SLinus Torvalds
300fba597dbSMiklos Szeredi error = pipe_buf_confirm(pipe, buf);
301f84d7519SJens Axboe if (error) {
3025274f052SJens Axboe if (!ret)
303e5953cbdSNicolas Kaiser ret = error;
3045274f052SJens Axboe break;
3055274f052SJens Axboe }
306f84d7519SJens Axboe
307fb9096a3SAl Viro written = copy_page_to_iter(buf->page, buf->offset, chars, to);
308637b58c2SAl Viro if (unlikely(written < chars)) {
309341b446bSIngo Molnar if (!ret)
310637b58c2SAl Viro ret = -EFAULT;
3111da177e4SLinus Torvalds break;
3121da177e4SLinus Torvalds }
3131da177e4SLinus Torvalds ret += chars;
3141da177e4SLinus Torvalds buf->offset += chars;
3151da177e4SLinus Torvalds buf->len -= chars;
3169883035aSLinus Torvalds
3179883035aSLinus Torvalds /* Was it a packet buffer? Clean up and exit */
3189883035aSLinus Torvalds if (buf->flags & PIPE_BUF_FLAG_PACKET) {
3199883035aSLinus Torvalds total_len = chars;
3209883035aSLinus Torvalds buf->len = 0;
3219883035aSLinus Torvalds }
3229883035aSLinus Torvalds
3231da177e4SLinus Torvalds if (!buf->len) {
324a779638cSMiklos Szeredi pipe_buf_release(pipe, buf);
3250ddad21dSLinus Torvalds spin_lock_irq(&pipe->rd_wait.lock);
326e7d553d6SDavid Howells #ifdef CONFIG_WATCH_QUEUE
327e7d553d6SDavid Howells if (buf->flags & PIPE_BUF_FLAG_LOSS)
328e7d553d6SDavid Howells pipe->note_loss = true;
329e7d553d6SDavid Howells #endif
3308cefc107SDavid Howells tail++;
3318cefc107SDavid Howells pipe->tail = tail;
3320ddad21dSLinus Torvalds spin_unlock_irq(&pipe->rd_wait.lock);
3331da177e4SLinus Torvalds }
3341da177e4SLinus Torvalds total_len -= chars;
3351da177e4SLinus Torvalds if (!total_len)
3361da177e4SLinus Torvalds break; /* common path: read succeeded */
3378cefc107SDavid Howells if (!pipe_empty(head, tail)) /* More to do? */
3381da177e4SLinus Torvalds continue;
3398cefc107SDavid Howells }
3408cefc107SDavid Howells
341923f4f23SIngo Molnar if (!pipe->writers)
3421da177e4SLinus Torvalds break;
3431da177e4SLinus Torvalds if (ret)
3441da177e4SLinus Torvalds break;
345c04fe8e3SJens Axboe if ((filp->f_flags & O_NONBLOCK) ||
346c04fe8e3SJens Axboe (iocb->ki_flags & IOCB_NOWAIT)) {
3471da177e4SLinus Torvalds ret = -EAGAIN;
3481da177e4SLinus Torvalds break;
3491da177e4SLinus Torvalds }
35085190d15SLinus Torvalds __pipe_unlock(pipe);
351d1c6a2aaSLinus Torvalds
352d1c6a2aaSLinus Torvalds /*
353d1c6a2aaSLinus Torvalds * We only get here if we didn't actually read anything.
354d1c6a2aaSLinus Torvalds *
355d1c6a2aaSLinus Torvalds * However, we could have seen (and removed) a zero-sized
356d1c6a2aaSLinus Torvalds * pipe buffer, and might have made space in the buffers
357d1c6a2aaSLinus Torvalds * that way.
358d1c6a2aaSLinus Torvalds *
359d1c6a2aaSLinus Torvalds * You can't make zero-sized pipe buffers by doing an empty
360d1c6a2aaSLinus Torvalds * write (not even in packet mode), but they can happen if
361d1c6a2aaSLinus Torvalds * the writer gets an EFAULT when trying to fill a buffer
362d1c6a2aaSLinus Torvalds * that already got allocated and inserted in the buffer
363d1c6a2aaSLinus Torvalds * array.
364d1c6a2aaSLinus Torvalds *
365d1c6a2aaSLinus Torvalds * So we still need to wake up any pending writers in the
366d1c6a2aaSLinus Torvalds * _very_ unlikely case that the pipe was full, but we got
367d1c6a2aaSLinus Torvalds * no data.
368d1c6a2aaSLinus Torvalds */
369fe67f4ddSLinus Torvalds if (unlikely(was_full))
3700ddad21dSLinus Torvalds wake_up_interruptible_sync_poll(&pipe->wr_wait, EPOLLOUT | EPOLLWRNORM);
371f467a6a6SLinus Torvalds kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
372d1c6a2aaSLinus Torvalds
373d1c6a2aaSLinus Torvalds /*
374d1c6a2aaSLinus Torvalds * But because we didn't read anything, at this point we can
375d1c6a2aaSLinus Torvalds * just return directly with -ERESTARTSYS if we're interrupted,
376d1c6a2aaSLinus Torvalds * since we've done any required wakeups and there's no need
377d1c6a2aaSLinus Torvalds * to mark anything accessed. And we've dropped the lock.
378d1c6a2aaSLinus Torvalds */
3790ddad21dSLinus Torvalds if (wait_event_interruptible_exclusive(pipe->rd_wait, pipe_readable(pipe)) < 0)
380d1c6a2aaSLinus Torvalds return -ERESTARTSYS;
381d1c6a2aaSLinus Torvalds
38285190d15SLinus Torvalds __pipe_lock(pipe);
383f467a6a6SLinus Torvalds was_full = pipe_full(pipe->head, pipe->tail, pipe->max_usage);
3840ddad21dSLinus Torvalds wake_next_reader = true;
3851da177e4SLinus Torvalds }
3860ddad21dSLinus Torvalds if (pipe_empty(pipe->head, pipe->tail))
3870ddad21dSLinus Torvalds wake_next_reader = false;
388ebec73f4SAl Viro __pipe_unlock(pipe);
389341b446bSIngo Molnar
390fe67f4ddSLinus Torvalds if (was_full)
3910ddad21dSLinus Torvalds wake_up_interruptible_sync_poll(&pipe->wr_wait, EPOLLOUT | EPOLLWRNORM);
3920ddad21dSLinus Torvalds if (wake_next_reader)
3930ddad21dSLinus Torvalds wake_up_interruptible_sync_poll(&pipe->rd_wait, EPOLLIN | EPOLLRDNORM);
394fe67f4ddSLinus Torvalds kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
3951da177e4SLinus Torvalds if (ret > 0)
3961da177e4SLinus Torvalds file_accessed(filp);
3971da177e4SLinus Torvalds return ret;
3981da177e4SLinus Torvalds }
3991da177e4SLinus Torvalds
is_packetized(struct file * file)4009883035aSLinus Torvalds static inline int is_packetized(struct file *file)
4019883035aSLinus Torvalds {
4029883035aSLinus Torvalds return (file->f_flags & O_DIRECT) != 0;
4039883035aSLinus Torvalds }
4049883035aSLinus Torvalds
40585190d15SLinus Torvalds /* Done while waiting without holding the pipe lock - thus the READ_ONCE() */
pipe_writable(const struct pipe_inode_info * pipe)40685190d15SLinus Torvalds static inline bool pipe_writable(const struct pipe_inode_info *pipe)
40785190d15SLinus Torvalds {
40885190d15SLinus Torvalds unsigned int head = READ_ONCE(pipe->head);
40985190d15SLinus Torvalds unsigned int tail = READ_ONCE(pipe->tail);
41085190d15SLinus Torvalds unsigned int max_usage = READ_ONCE(pipe->max_usage);
41185190d15SLinus Torvalds
41285190d15SLinus Torvalds return !pipe_full(head, tail, max_usage) ||
41385190d15SLinus Torvalds !READ_ONCE(pipe->readers);
41485190d15SLinus Torvalds }
41585190d15SLinus Torvalds
4161da177e4SLinus Torvalds static ssize_t
pipe_write(struct kiocb * iocb,struct iov_iter * from)417f0d1bec9SAl Viro pipe_write(struct kiocb *iocb, struct iov_iter *from)
4181da177e4SLinus Torvalds {
419ee0b3e67SBadari Pulavarty struct file *filp = iocb->ki_filp;
420de32ec4cSAl Viro struct pipe_inode_info *pipe = filp->private_data;
4218f868d68SDavid Howells unsigned int head;
422f0d1bec9SAl Viro ssize_t ret = 0;
423f0d1bec9SAl Viro size_t total_len = iov_iter_count(from);
4241da177e4SLinus Torvalds ssize_t chars;
4251b6b26aeSLinus Torvalds bool was_empty = false;
4260ddad21dSLinus Torvalds bool wake_next_writer = false;
4271da177e4SLinus Torvalds
428*f4a192cdSJann Horn /*
429*f4a192cdSJann Horn * Reject writing to watch queue pipes before the point where we lock
430*f4a192cdSJann Horn * the pipe.
431*f4a192cdSJann Horn * Otherwise, lockdep would be unhappy if the caller already has another
432*f4a192cdSJann Horn * pipe locked.
433*f4a192cdSJann Horn * If we had to support locking a normal pipe and a notification pipe at
434*f4a192cdSJann Horn * the same time, we could set up lockdep annotations for that, but
435*f4a192cdSJann Horn * since we don't actually need that, it's simpler to just bail here.
436*f4a192cdSJann Horn */
437*f4a192cdSJann Horn if (pipe_has_watch_queue(pipe))
438*f4a192cdSJann Horn return -EXDEV;
439*f4a192cdSJann Horn
4401da177e4SLinus Torvalds /* Null write succeeds. */
4411da177e4SLinus Torvalds if (unlikely(total_len == 0))
4421da177e4SLinus Torvalds return 0;
4431da177e4SLinus Torvalds
444ebec73f4SAl Viro __pipe_lock(pipe);
4451da177e4SLinus Torvalds
446923f4f23SIngo Molnar if (!pipe->readers) {
4471da177e4SLinus Torvalds send_sig(SIGPIPE, current, 0);
4481da177e4SLinus Torvalds ret = -EPIPE;
4491da177e4SLinus Torvalds goto out;
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds
4521b6b26aeSLinus Torvalds /*
4531b6b26aeSLinus Torvalds * If it wasn't empty we try to merge new data into
4541b6b26aeSLinus Torvalds * the last buffer.
4551b6b26aeSLinus Torvalds *
4561b6b26aeSLinus Torvalds * That naturally merges small writes, but it also
4573a34b13aSLinus Torvalds * page-aligns the rest of the writes for large writes
4581b6b26aeSLinus Torvalds * spanning multiple pages.
4591b6b26aeSLinus Torvalds */
4608cefc107SDavid Howells head = pipe->head;
4613b844826SLinus Torvalds was_empty = pipe_empty(head, pipe->tail);
4621b6b26aeSLinus Torvalds chars = total_len & (PAGE_SIZE-1);
4633b844826SLinus Torvalds if (chars && !was_empty) {
4648f868d68SDavid Howells unsigned int mask = pipe->ring_size - 1;
4658cefc107SDavid Howells struct pipe_buffer *buf = &pipe->bufs[(head - 1) & mask];
4661da177e4SLinus Torvalds int offset = buf->offset + buf->len;
467341b446bSIngo Molnar
468f6dd9755SChristoph Hellwig if ((buf->flags & PIPE_BUF_FLAG_CAN_MERGE) &&
469f6dd9755SChristoph Hellwig offset + chars <= PAGE_SIZE) {
470fba597dbSMiklos Szeredi ret = pipe_buf_confirm(pipe, buf);
4716ae08069SEric Biggers if (ret)
4725274f052SJens Axboe goto out;
473f84d7519SJens Axboe
474f0d1bec9SAl Viro ret = copy_page_from_iter(buf->page, offset, chars, from);
475f0d1bec9SAl Viro if (unlikely(ret < chars)) {
4766ae08069SEric Biggers ret = -EFAULT;
4771da177e4SLinus Torvalds goto out;
478f6762b7aSJens Axboe }
4791b6b26aeSLinus Torvalds
4806ae08069SEric Biggers buf->len += ret;
481f0d1bec9SAl Viro if (!iov_iter_count(from))
4821da177e4SLinus Torvalds goto out;
4831da177e4SLinus Torvalds }
4841da177e4SLinus Torvalds }
4851da177e4SLinus Torvalds
4861da177e4SLinus Torvalds for (;;) {
487923f4f23SIngo Molnar if (!pipe->readers) {
4881da177e4SLinus Torvalds send_sig(SIGPIPE, current, 0);
489341b446bSIngo Molnar if (!ret)
490341b446bSIngo Molnar ret = -EPIPE;
4911da177e4SLinus Torvalds break;
4921da177e4SLinus Torvalds }
4938cefc107SDavid Howells
494a194dfe6SDavid Howells head = pipe->head;
4958f868d68SDavid Howells if (!pipe_full(head, pipe->tail, pipe->max_usage)) {
4968f868d68SDavid Howells unsigned int mask = pipe->ring_size - 1;
497fbaa530eSColin Ian King struct pipe_buffer *buf;
498923f4f23SIngo Molnar struct page *page = pipe->tmp_page;
499f0d1bec9SAl Viro int copied;
5001da177e4SLinus Torvalds
5011da177e4SLinus Torvalds if (!page) {
502d86133bdSVladimir Davydov page = alloc_page(GFP_HIGHUSER | __GFP_ACCOUNT);
5031da177e4SLinus Torvalds if (unlikely(!page)) {
5041da177e4SLinus Torvalds ret = ret ? : -ENOMEM;
5051da177e4SLinus Torvalds break;
5061da177e4SLinus Torvalds }
507923f4f23SIngo Molnar pipe->tmp_page = page;
5081da177e4SLinus Torvalds }
509a194dfe6SDavid Howells
510a194dfe6SDavid Howells /* Allocate a slot in the ring in advance and attach an
511a194dfe6SDavid Howells * empty buffer. If we fault or otherwise fail to use
512a194dfe6SDavid Howells * it, either the reader will consume it or it'll still
513a194dfe6SDavid Howells * be there for the next write.
514a194dfe6SDavid Howells */
5150ddad21dSLinus Torvalds spin_lock_irq(&pipe->rd_wait.lock);
516a194dfe6SDavid Howells
517a194dfe6SDavid Howells head = pipe->head;
5188f868d68SDavid Howells if (pipe_full(head, pipe->tail, pipe->max_usage)) {
5190ddad21dSLinus Torvalds spin_unlock_irq(&pipe->rd_wait.lock);
5208df44129SDavid Howells continue;
5218df44129SDavid Howells }
5228df44129SDavid Howells
523a194dfe6SDavid Howells pipe->head = head + 1;
5240ddad21dSLinus Torvalds spin_unlock_irq(&pipe->rd_wait.lock);
525a194dfe6SDavid Howells
526a194dfe6SDavid Howells /* Insert it into the buffer array */
527a194dfe6SDavid Howells buf = &pipe->bufs[head & mask];
528a194dfe6SDavid Howells buf->page = page;
529a194dfe6SDavid Howells buf->ops = &anon_pipe_buf_ops;
530a194dfe6SDavid Howells buf->offset = 0;
531a194dfe6SDavid Howells buf->len = 0;
532f6dd9755SChristoph Hellwig if (is_packetized(filp))
533a194dfe6SDavid Howells buf->flags = PIPE_BUF_FLAG_PACKET;
534f6dd9755SChristoph Hellwig else
535f6dd9755SChristoph Hellwig buf->flags = PIPE_BUF_FLAG_CAN_MERGE;
536a194dfe6SDavid Howells pipe->tmp_page = NULL;
537a194dfe6SDavid Howells
538f0d1bec9SAl Viro copied = copy_page_from_iter(page, 0, PAGE_SIZE, from);
539f0d1bec9SAl Viro if (unlikely(copied < PAGE_SIZE && iov_iter_count(from))) {
540341b446bSIngo Molnar if (!ret)
541f0d1bec9SAl Viro ret = -EFAULT;
5421da177e4SLinus Torvalds break;
5431da177e4SLinus Torvalds }
544f0d1bec9SAl Viro ret += copied;
545f0d1bec9SAl Viro buf->len = copied;
5461da177e4SLinus Torvalds
547f0d1bec9SAl Viro if (!iov_iter_count(from))
5481da177e4SLinus Torvalds break;
5491da177e4SLinus Torvalds }
5508cefc107SDavid Howells
5518f868d68SDavid Howells if (!pipe_full(head, pipe->tail, pipe->max_usage))
5521da177e4SLinus Torvalds continue;
5538cefc107SDavid Howells
5548cefc107SDavid Howells /* Wait for buffer space to become available. */
555c04fe8e3SJens Axboe if ((filp->f_flags & O_NONBLOCK) ||
556c04fe8e3SJens Axboe (iocb->ki_flags & IOCB_NOWAIT)) {
557341b446bSIngo Molnar if (!ret)
558341b446bSIngo Molnar ret = -EAGAIN;
5591da177e4SLinus Torvalds break;
5601da177e4SLinus Torvalds }
5611da177e4SLinus Torvalds if (signal_pending(current)) {
562341b446bSIngo Molnar if (!ret)
563341b446bSIngo Molnar ret = -ERESTARTSYS;
5641da177e4SLinus Torvalds break;
5651da177e4SLinus Torvalds }
5661b6b26aeSLinus Torvalds
5671b6b26aeSLinus Torvalds /*
5681b6b26aeSLinus Torvalds * We're going to release the pipe lock and wait for more
5691b6b26aeSLinus Torvalds * space. We wake up any readers if necessary, and then
5701b6b26aeSLinus Torvalds * after waiting we need to re-check whether the pipe
5711b6b26aeSLinus Torvalds * become empty while we dropped the lock.
5721b6b26aeSLinus Torvalds */
57385190d15SLinus Torvalds __pipe_unlock(pipe);
574fe67f4ddSLinus Torvalds if (was_empty)
5750ddad21dSLinus Torvalds wake_up_interruptible_sync_poll(&pipe->rd_wait, EPOLLIN | EPOLLRDNORM);
5761b6b26aeSLinus Torvalds kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
5770ddad21dSLinus Torvalds wait_event_interruptible_exclusive(pipe->wr_wait, pipe_writable(pipe));
57885190d15SLinus Torvalds __pipe_lock(pipe);
5790dd1e377SJan Stancek was_empty = pipe_empty(pipe->head, pipe->tail);
5800ddad21dSLinus Torvalds wake_next_writer = true;
5811da177e4SLinus Torvalds }
5821da177e4SLinus Torvalds out:
5830ddad21dSLinus Torvalds if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
5840ddad21dSLinus Torvalds wake_next_writer = false;
585ebec73f4SAl Viro __pipe_unlock(pipe);
5861b6b26aeSLinus Torvalds
5871b6b26aeSLinus Torvalds /*
5881b6b26aeSLinus Torvalds * If we do do a wakeup event, we do a 'sync' wakeup, because we
5891b6b26aeSLinus Torvalds * want the reader to start processing things asap, rather than
5901b6b26aeSLinus Torvalds * leave the data pending.
5911b6b26aeSLinus Torvalds *
5921b6b26aeSLinus Torvalds * This is particularly important for small writes, because of
5931b6b26aeSLinus Torvalds * how (for example) the GNU make jobserver uses small writes to
5941b6b26aeSLinus Torvalds * wake up pending jobs
5953b844826SLinus Torvalds *
5963b844826SLinus Torvalds * Epoll nonsensically wants a wakeup whether the pipe
5973b844826SLinus Torvalds * was already empty or not.
5981b6b26aeSLinus Torvalds */
599fe67f4ddSLinus Torvalds if (was_empty || pipe->poll_usage)
6000ddad21dSLinus Torvalds wake_up_interruptible_sync_poll(&pipe->rd_wait, EPOLLIN | EPOLLRDNORM);
601923f4f23SIngo Molnar kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
6020ddad21dSLinus Torvalds if (wake_next_writer)
6030ddad21dSLinus Torvalds wake_up_interruptible_sync_poll(&pipe->wr_wait, EPOLLOUT | EPOLLWRNORM);
6047e775f46SDmitry Monakhov if (ret > 0 && sb_start_write_trylock(file_inode(filp)->i_sb)) {
605c3b2da31SJosef Bacik int err = file_update_time(filp);
606c3b2da31SJosef Bacik if (err)
607c3b2da31SJosef Bacik ret = err;
6087e775f46SDmitry Monakhov sb_end_write(file_inode(filp)->i_sb);
609c3b2da31SJosef Bacik }
6101da177e4SLinus Torvalds return ret;
6111da177e4SLinus Torvalds }
6121da177e4SLinus Torvalds
pipe_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)613d59d0b1bSAndi Kleen static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
6141da177e4SLinus Torvalds {
615de32ec4cSAl Viro struct pipe_inode_info *pipe = filp->private_data;
616aeb213cdSAndrei Vagin unsigned int count, head, tail, mask;
6171da177e4SLinus Torvalds
6181da177e4SLinus Torvalds switch (cmd) {
6191da177e4SLinus Torvalds case FIONREAD:
620ebec73f4SAl Viro __pipe_lock(pipe);
6211da177e4SLinus Torvalds count = 0;
6228cefc107SDavid Howells head = pipe->head;
6238cefc107SDavid Howells tail = pipe->tail;
6248cefc107SDavid Howells mask = pipe->ring_size - 1;
6258cefc107SDavid Howells
6268cefc107SDavid Howells while (tail != head) {
6278cefc107SDavid Howells count += pipe->bufs[tail & mask].len;
6288cefc107SDavid Howells tail++;
6291da177e4SLinus Torvalds }
630ebec73f4SAl Viro __pipe_unlock(pipe);
631923f4f23SIngo Molnar
6321da177e4SLinus Torvalds return put_user(count, (int __user *)arg);
633c73be61cSDavid Howells
634c73be61cSDavid Howells #ifdef CONFIG_WATCH_QUEUE
635c73be61cSDavid Howells case IOC_WATCH_QUEUE_SET_SIZE: {
636c73be61cSDavid Howells int ret;
637c73be61cSDavid Howells __pipe_lock(pipe);
638c73be61cSDavid Howells ret = watch_queue_set_size(pipe, arg);
639c73be61cSDavid Howells __pipe_unlock(pipe);
640c73be61cSDavid Howells return ret;
641c73be61cSDavid Howells }
642c73be61cSDavid Howells
643c73be61cSDavid Howells case IOC_WATCH_QUEUE_SET_FILTER:
644c73be61cSDavid Howells return watch_queue_set_filter(
645c73be61cSDavid Howells pipe, (struct watch_notification_filter __user *)arg);
646c73be61cSDavid Howells #endif
647c73be61cSDavid Howells
6481da177e4SLinus Torvalds default:
64946ce341bSWill Deacon return -ENOIOCTLCMD;
6501da177e4SLinus Torvalds }
6511da177e4SLinus Torvalds }
6521da177e4SLinus Torvalds
653dd67081bSChristoph Hellwig /* No kernel lock held - fine */
654a11e1d43SLinus Torvalds static __poll_t
pipe_poll(struct file * filp,poll_table * wait)655a11e1d43SLinus Torvalds pipe_poll(struct file *filp, poll_table *wait)
656dd67081bSChristoph Hellwig {
657a11e1d43SLinus Torvalds __poll_t mask;
658dd67081bSChristoph Hellwig struct pipe_inode_info *pipe = filp->private_data;
659ad910e36SLinus Torvalds unsigned int head, tail;
660a11e1d43SLinus Torvalds
6613b844826SLinus Torvalds /* Epoll has some historical nasty semantics, this enables them */
662f485922dSKuniyuki Iwashima WRITE_ONCE(pipe->poll_usage, true);
6633b844826SLinus Torvalds
664ad910e36SLinus Torvalds /*
6650ddad21dSLinus Torvalds * Reading pipe state only -- no need for acquiring the semaphore.
666ad910e36SLinus Torvalds *
667ad910e36SLinus Torvalds * But because this is racy, the code has to add the
668ad910e36SLinus Torvalds * entry to the poll table _first_ ..
669ad910e36SLinus Torvalds */
6700ddad21dSLinus Torvalds if (filp->f_mode & FMODE_READ)
6710ddad21dSLinus Torvalds poll_wait(filp, &pipe->rd_wait, wait);
6720ddad21dSLinus Torvalds if (filp->f_mode & FMODE_WRITE)
6730ddad21dSLinus Torvalds poll_wait(filp, &pipe->wr_wait, wait);
6741da177e4SLinus Torvalds
675ad910e36SLinus Torvalds /*
676ad910e36SLinus Torvalds * .. and only then can you do the racy tests. That way,
677ad910e36SLinus Torvalds * if something changes and you got it wrong, the poll
678ad910e36SLinus Torvalds * table entry will wake you up and fix it.
679ad910e36SLinus Torvalds */
680ad910e36SLinus Torvalds head = READ_ONCE(pipe->head);
681ad910e36SLinus Torvalds tail = READ_ONCE(pipe->tail);
682ad910e36SLinus Torvalds
683a11e1d43SLinus Torvalds mask = 0;
6841da177e4SLinus Torvalds if (filp->f_mode & FMODE_READ) {
6858cefc107SDavid Howells if (!pipe_empty(head, tail))
6868cefc107SDavid Howells mask |= EPOLLIN | EPOLLRDNORM;
687923f4f23SIngo Molnar if (!pipe->writers && filp->f_version != pipe->w_counter)
688a9a08845SLinus Torvalds mask |= EPOLLHUP;
6891da177e4SLinus Torvalds }
6901da177e4SLinus Torvalds
6911da177e4SLinus Torvalds if (filp->f_mode & FMODE_WRITE) {
6926718b6f8SDavid Howells if (!pipe_full(head, tail, pipe->max_usage))
6938cefc107SDavid Howells mask |= EPOLLOUT | EPOLLWRNORM;
6945e5d7a22SPekka Enberg /*
695a9a08845SLinus Torvalds * Most Unices do not set EPOLLERR for FIFOs but on Linux they
6965e5d7a22SPekka Enberg * behave exactly like pipes for poll().
6975e5d7a22SPekka Enberg */
698923f4f23SIngo Molnar if (!pipe->readers)
699a9a08845SLinus Torvalds mask |= EPOLLERR;
7001da177e4SLinus Torvalds }
7011da177e4SLinus Torvalds
7021da177e4SLinus Torvalds return mask;
7031da177e4SLinus Torvalds }
7041da177e4SLinus Torvalds
put_pipe_info(struct inode * inode,struct pipe_inode_info * pipe)705b0d8d229SLinus Torvalds static void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe)
706b0d8d229SLinus Torvalds {
707b0d8d229SLinus Torvalds int kill = 0;
708b0d8d229SLinus Torvalds
709b0d8d229SLinus Torvalds spin_lock(&inode->i_lock);
710b0d8d229SLinus Torvalds if (!--pipe->files) {
711b0d8d229SLinus Torvalds inode->i_pipe = NULL;
712b0d8d229SLinus Torvalds kill = 1;
713b0d8d229SLinus Torvalds }
714b0d8d229SLinus Torvalds spin_unlock(&inode->i_lock);
715b0d8d229SLinus Torvalds
716b0d8d229SLinus Torvalds if (kill)
717b0d8d229SLinus Torvalds free_pipe_info(pipe);
718b0d8d229SLinus Torvalds }
719b0d8d229SLinus Torvalds
7201da177e4SLinus Torvalds static int
pipe_release(struct inode * inode,struct file * file)721599a0ac1SAl Viro pipe_release(struct inode *inode, struct file *file)
7221da177e4SLinus Torvalds {
723b0d8d229SLinus Torvalds struct pipe_inode_info *pipe = file->private_data;
724923f4f23SIngo Molnar
725ebec73f4SAl Viro __pipe_lock(pipe);
726599a0ac1SAl Viro if (file->f_mode & FMODE_READ)
727599a0ac1SAl Viro pipe->readers--;
728599a0ac1SAl Viro if (file->f_mode & FMODE_WRITE)
729599a0ac1SAl Viro pipe->writers--;
730341b446bSIngo Molnar
7316551d5c5SLinus Torvalds /* Was that the last reader or writer, but not the other side? */
7326551d5c5SLinus Torvalds if (!pipe->readers != !pipe->writers) {
7336551d5c5SLinus Torvalds wake_up_interruptible_all(&pipe->rd_wait);
7346551d5c5SLinus Torvalds wake_up_interruptible_all(&pipe->wr_wait);
735923f4f23SIngo Molnar kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
736923f4f23SIngo Molnar kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
7371da177e4SLinus Torvalds }
738ebec73f4SAl Viro __pipe_unlock(pipe);
739ba5bb147SAl Viro
740b0d8d229SLinus Torvalds put_pipe_info(inode, pipe);
7411da177e4SLinus Torvalds return 0;
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds
7441da177e4SLinus Torvalds static int
pipe_fasync(int fd,struct file * filp,int on)745599a0ac1SAl Viro pipe_fasync(int fd, struct file *filp, int on)
7461da177e4SLinus Torvalds {
747de32ec4cSAl Viro struct pipe_inode_info *pipe = filp->private_data;
748599a0ac1SAl Viro int retval = 0;
7491da177e4SLinus Torvalds
750ebec73f4SAl Viro __pipe_lock(pipe);
751599a0ac1SAl Viro if (filp->f_mode & FMODE_READ)
752341b446bSIngo Molnar retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
753599a0ac1SAl Viro if ((filp->f_mode & FMODE_WRITE) && retval >= 0) {
754341b446bSIngo Molnar retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
755599a0ac1SAl Viro if (retval < 0 && (filp->f_mode & FMODE_READ))
756599a0ac1SAl Viro /* this can happen only if on == T */
757e5bc49baSOleg Nesterov fasync_helper(-1, filp, 0, &pipe->fasync_readers);
758e5bc49baSOleg Nesterov }
759ebec73f4SAl Viro __pipe_unlock(pipe);
7601da177e4SLinus Torvalds return retval;
7611da177e4SLinus Torvalds }
7621da177e4SLinus Torvalds
account_pipe_buffers(struct user_struct * user,unsigned long old,unsigned long new)763c73be61cSDavid Howells unsigned long account_pipe_buffers(struct user_struct *user,
764759c0114SWilly Tarreau unsigned long old, unsigned long new)
765759c0114SWilly Tarreau {
7669c87bcf0SMichael Kerrisk (man-pages) return atomic_long_add_return(new - old, &user->pipe_bufs);
767759c0114SWilly Tarreau }
768759c0114SWilly Tarreau
too_many_pipe_buffers_soft(unsigned long user_bufs)769c73be61cSDavid Howells bool too_many_pipe_buffers_soft(unsigned long user_bufs)
770759c0114SWilly Tarreau {
771f7340761SEric Biggers unsigned long soft_limit = READ_ONCE(pipe_user_pages_soft);
772f7340761SEric Biggers
773f7340761SEric Biggers return soft_limit && user_bufs > soft_limit;
774759c0114SWilly Tarreau }
775759c0114SWilly Tarreau
too_many_pipe_buffers_hard(unsigned long user_bufs)776c73be61cSDavid Howells bool too_many_pipe_buffers_hard(unsigned long user_bufs)
777759c0114SWilly Tarreau {
778f7340761SEric Biggers unsigned long hard_limit = READ_ONCE(pipe_user_pages_hard);
779f7340761SEric Biggers
780f7340761SEric Biggers return hard_limit && user_bufs > hard_limit;
781759c0114SWilly Tarreau }
782759c0114SWilly Tarreau
pipe_is_unprivileged_user(void)783c73be61cSDavid Howells bool pipe_is_unprivileged_user(void)
78485c2dd54SEric Biggers {
78585c2dd54SEric Biggers return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
78685c2dd54SEric Biggers }
78785c2dd54SEric Biggers
alloc_pipe_info(void)7887bee130eSAl Viro struct pipe_inode_info *alloc_pipe_info(void)
7893a326a2cSIngo Molnar {
790923f4f23SIngo Molnar struct pipe_inode_info *pipe;
791759c0114SWilly Tarreau unsigned long pipe_bufs = PIPE_DEF_BUFFERS;
792759c0114SWilly Tarreau struct user_struct *user = get_current_user();
7939c87bcf0SMichael Kerrisk (man-pages) unsigned long user_bufs;
794f7340761SEric Biggers unsigned int max_size = READ_ONCE(pipe_max_size);
795759c0114SWilly Tarreau
79609b4d199SMichael Kerrisk (man-pages) pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL_ACCOUNT);
79709b4d199SMichael Kerrisk (man-pages) if (pipe == NULL)
79809b4d199SMichael Kerrisk (man-pages) goto out_free_uid;
79909b4d199SMichael Kerrisk (man-pages)
800f7340761SEric Biggers if (pipe_bufs * PAGE_SIZE > max_size && !capable(CAP_SYS_RESOURCE))
801f7340761SEric Biggers pipe_bufs = max_size >> PAGE_SHIFT;
802086e774aSMichael Kerrisk (man-pages)
8039c87bcf0SMichael Kerrisk (man-pages) user_bufs = account_pipe_buffers(user, 0, pipe_bufs);
804a005ca0eSMichael Kerrisk (man-pages)
805c73be61cSDavid Howells if (too_many_pipe_buffers_soft(user_bufs) && pipe_is_unprivileged_user()) {
80646c4c9d1SAlex Xu (Hello71) user_bufs = account_pipe_buffers(user, pipe_bufs, PIPE_MIN_DEF_BUFFERS);
80746c4c9d1SAlex Xu (Hello71) pipe_bufs = PIPE_MIN_DEF_BUFFERS;
808759c0114SWilly Tarreau }
809759c0114SWilly Tarreau
810c73be61cSDavid Howells if (too_many_pipe_buffers_hard(user_bufs) && pipe_is_unprivileged_user())
811a005ca0eSMichael Kerrisk (man-pages) goto out_revert_acct;
812a005ca0eSMichael Kerrisk (man-pages)
813906f9040SLinus Torvalds pipe->bufs = kcalloc(pipe_bufs, sizeof(struct pipe_buffer),
814a005ca0eSMichael Kerrisk (man-pages) GFP_KERNEL_ACCOUNT);
815a005ca0eSMichael Kerrisk (man-pages)
81635f3d14dSJens Axboe if (pipe->bufs) {
8170ddad21dSLinus Torvalds init_waitqueue_head(&pipe->rd_wait);
8180ddad21dSLinus Torvalds init_waitqueue_head(&pipe->wr_wait);
819923f4f23SIngo Molnar pipe->r_counter = pipe->w_counter = 1;
8206718b6f8SDavid Howells pipe->max_usage = pipe_bufs;
8218cefc107SDavid Howells pipe->ring_size = pipe_bufs;
822c73be61cSDavid Howells pipe->nr_accounted = pipe_bufs;
823759c0114SWilly Tarreau pipe->user = user;
82472b0d9aaSAl Viro mutex_init(&pipe->mutex);
82535f3d14dSJens Axboe return pipe;
82635f3d14dSJens Axboe }
8273a326a2cSIngo Molnar
828a005ca0eSMichael Kerrisk (man-pages) out_revert_acct:
8299c87bcf0SMichael Kerrisk (man-pages) (void) account_pipe_buffers(user, pipe_bufs, 0);
83009b4d199SMichael Kerrisk (man-pages) kfree(pipe);
83109b4d199SMichael Kerrisk (man-pages) out_free_uid:
83209b4d199SMichael Kerrisk (man-pages) free_uid(user);
83335f3d14dSJens Axboe return NULL;
8343a326a2cSIngo Molnar }
8353a326a2cSIngo Molnar
free_pipe_info(struct pipe_inode_info * pipe)8364b8a8f1eSAl Viro void free_pipe_info(struct pipe_inode_info *pipe)
8371da177e4SLinus Torvalds {
838aeb213cdSAndrei Vagin unsigned int i;
8391da177e4SLinus Torvalds
840c73be61cSDavid Howells #ifdef CONFIG_WATCH_QUEUE
841db8facfcSDavid Howells if (pipe->watch_queue)
842c73be61cSDavid Howells watch_queue_clear(pipe->watch_queue);
843c73be61cSDavid Howells #endif
844c73be61cSDavid Howells
845c73be61cSDavid Howells (void) account_pipe_buffers(pipe->user, pipe->nr_accounted, 0);
846759c0114SWilly Tarreau free_uid(pipe->user);
8478cefc107SDavid Howells for (i = 0; i < pipe->ring_size; i++) {
848923f4f23SIngo Molnar struct pipe_buffer *buf = pipe->bufs + i;
8491da177e4SLinus Torvalds if (buf->ops)
850a779638cSMiklos Szeredi pipe_buf_release(pipe, buf);
8511da177e4SLinus Torvalds }
852db8facfcSDavid Howells #ifdef CONFIG_WATCH_QUEUE
853db8facfcSDavid Howells if (pipe->watch_queue)
854db8facfcSDavid Howells put_watch_queue(pipe->watch_queue);
855db8facfcSDavid Howells #endif
856923f4f23SIngo Molnar if (pipe->tmp_page)
857923f4f23SIngo Molnar __free_page(pipe->tmp_page);
858906f9040SLinus Torvalds kfree(pipe->bufs);
859923f4f23SIngo Molnar kfree(pipe);
8601da177e4SLinus Torvalds }
8611da177e4SLinus Torvalds
862fa3536ccSEric Dumazet static struct vfsmount *pipe_mnt __read_mostly;
863341b446bSIngo Molnar
864c23fbb6bSEric Dumazet /*
865c23fbb6bSEric Dumazet * pipefs_dname() is called from d_path().
866c23fbb6bSEric Dumazet */
pipefs_dname(struct dentry * dentry,char * buffer,int buflen)867c23fbb6bSEric Dumazet static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
868c23fbb6bSEric Dumazet {
8690f60d288SAl Viro return dynamic_dname(buffer, buflen, "pipe:[%lu]",
87075c3cfa8SDavid Howells d_inode(dentry)->i_ino);
871c23fbb6bSEric Dumazet }
872c23fbb6bSEric Dumazet
8733ba13d17SAl Viro static const struct dentry_operations pipefs_dentry_operations = {
874c23fbb6bSEric Dumazet .d_dname = pipefs_dname,
8751da177e4SLinus Torvalds };
8761da177e4SLinus Torvalds
get_pipe_inode(void)8771da177e4SLinus Torvalds static struct inode * get_pipe_inode(void)
8781da177e4SLinus Torvalds {
879a209dfc7SEric Dumazet struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
880923f4f23SIngo Molnar struct pipe_inode_info *pipe;
8811da177e4SLinus Torvalds
8821da177e4SLinus Torvalds if (!inode)
8831da177e4SLinus Torvalds goto fail_inode;
8841da177e4SLinus Torvalds
88585fe4025SChristoph Hellwig inode->i_ino = get_next_ino();
88685fe4025SChristoph Hellwig
8877bee130eSAl Viro pipe = alloc_pipe_info();
888923f4f23SIngo Molnar if (!pipe)
8891da177e4SLinus Torvalds goto fail_iput;
8903a326a2cSIngo Molnar
891ba5bb147SAl Viro inode->i_pipe = pipe;
892ba5bb147SAl Viro pipe->files = 2;
893923f4f23SIngo Molnar pipe->readers = pipe->writers = 1;
894599a0ac1SAl Viro inode->i_fop = &pipefifo_fops;
8951da177e4SLinus Torvalds
8961da177e4SLinus Torvalds /*
8971da177e4SLinus Torvalds * Mark the inode dirty from the very beginning,
8981da177e4SLinus Torvalds * that way it will never be moved to the dirty
8991da177e4SLinus Torvalds * list because "mark_inode_dirty()" will think
9001da177e4SLinus Torvalds * that it already _is_ on the dirty list.
9011da177e4SLinus Torvalds */
9021da177e4SLinus Torvalds inode->i_state = I_DIRTY;
9031da177e4SLinus Torvalds inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
904da9592edSDavid Howells inode->i_uid = current_fsuid();
905da9592edSDavid Howells inode->i_gid = current_fsgid();
9062276e5baSJeff Layton inode->i_atime = inode->i_mtime = inode_set_ctime_current(inode);
907923f4f23SIngo Molnar
9081da177e4SLinus Torvalds return inode;
9091da177e4SLinus Torvalds
9101da177e4SLinus Torvalds fail_iput:
9111da177e4SLinus Torvalds iput(inode);
912341b446bSIngo Molnar
9131da177e4SLinus Torvalds fail_inode:
9141da177e4SLinus Torvalds return NULL;
9151da177e4SLinus Torvalds }
9161da177e4SLinus Torvalds
create_pipe_files(struct file ** res,int flags)917e4fad8e5SAl Viro int create_pipe_files(struct file **res, int flags)
9181da177e4SLinus Torvalds {
919e4fad8e5SAl Viro struct inode *inode = get_pipe_inode();
920d6cbd281SAndi Kleen struct file *f;
9218a018eb5SQian Cai int error;
9221da177e4SLinus Torvalds
9231da177e4SLinus Torvalds if (!inode)
924e4fad8e5SAl Viro return -ENFILE;
9251da177e4SLinus Torvalds
926c73be61cSDavid Howells if (flags & O_NOTIFICATION_PIPE) {
9278a018eb5SQian Cai error = watch_queue_init(inode->i_pipe);
9288a018eb5SQian Cai if (error) {
9298a018eb5SQian Cai free_pipe_info(inode->i_pipe);
930c73be61cSDavid Howells iput(inode);
9318a018eb5SQian Cai return error;
932c73be61cSDavid Howells }
933c73be61cSDavid Howells }
934c73be61cSDavid Howells
935152b6372SAl Viro f = alloc_file_pseudo(inode, pipe_mnt, "",
936152b6372SAl Viro O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT)),
937c9c554f2SAl Viro &pipefifo_fops);
938e9bb1f9bSEric Biggers if (IS_ERR(f)) {
9394b8a8f1eSAl Viro free_pipe_info(inode->i_pipe);
940d6cbd281SAndi Kleen iput(inode);
941152b6372SAl Viro return PTR_ERR(f);
9421da177e4SLinus Torvalds }
9431da177e4SLinus Torvalds
9441da177e4SLinus Torvalds f->private_data = inode->i_pipe;
9451da177e4SLinus Torvalds
946183266f2SAl Viro res[0] = alloc_file_clone(f, O_RDONLY | (flags & O_NONBLOCK),
947c9c554f2SAl Viro &pipefifo_fops);
9481da177e4SLinus Torvalds if (IS_ERR(res[0])) {
949b10a4a9fSAl Viro put_pipe_info(inode, inode->i_pipe);
950b10a4a9fSAl Viro fput(f);
951b10a4a9fSAl Viro return PTR_ERR(res[0]);
9521da177e4SLinus Torvalds }
9531da177e4SLinus Torvalds res[0]->private_data = inode->i_pipe;
9541da177e4SLinus Torvalds res[1] = f;
955d8e464ecSLinus Torvalds stream_open(inode, res[0]);
956d8e464ecSLinus Torvalds stream_open(inode, res[1]);
9571da177e4SLinus Torvalds return 0;
958d6cbd281SAndi Kleen }
959d6cbd281SAndi Kleen
__do_pipe_flags(int * fd,struct file ** files,int flags)9605b249b1bSAl Viro static int __do_pipe_flags(int *fd, struct file **files, int flags)
961d6cbd281SAndi Kleen {
962d6cbd281SAndi Kleen int error;
963d6cbd281SAndi Kleen int fdw, fdr;
964d6cbd281SAndi Kleen
965c73be61cSDavid Howells if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT | O_NOTIFICATION_PIPE))
966ed8cae8bSUlrich Drepper return -EINVAL;
967ed8cae8bSUlrich Drepper
968e4fad8e5SAl Viro error = create_pipe_files(files, flags);
969e4fad8e5SAl Viro if (error)
970e4fad8e5SAl Viro return error;
971d6cbd281SAndi Kleen
972ed8cae8bSUlrich Drepper error = get_unused_fd_flags(flags);
973d6cbd281SAndi Kleen if (error < 0)
974d6cbd281SAndi Kleen goto err_read_pipe;
975d6cbd281SAndi Kleen fdr = error;
976d6cbd281SAndi Kleen
977ed8cae8bSUlrich Drepper error = get_unused_fd_flags(flags);
978d6cbd281SAndi Kleen if (error < 0)
979d6cbd281SAndi Kleen goto err_fdr;
980d6cbd281SAndi Kleen fdw = error;
981d6cbd281SAndi Kleen
982157cf649SAl Viro audit_fd_pair(fdr, fdw);
983d6cbd281SAndi Kleen fd[0] = fdr;
984d6cbd281SAndi Kleen fd[1] = fdw;
985afed6271SJens Axboe /* pipe groks IOCB_NOWAIT */
986afed6271SJens Axboe files[0]->f_mode |= FMODE_NOWAIT;
987afed6271SJens Axboe files[1]->f_mode |= FMODE_NOWAIT;
9881da177e4SLinus Torvalds return 0;
9891da177e4SLinus Torvalds
990d6cbd281SAndi Kleen err_fdr:
991d6cbd281SAndi Kleen put_unused_fd(fdr);
992d6cbd281SAndi Kleen err_read_pipe:
993e4fad8e5SAl Viro fput(files[0]);
994e4fad8e5SAl Viro fput(files[1]);
9951da177e4SLinus Torvalds return error;
9961da177e4SLinus Torvalds }
9971da177e4SLinus Torvalds
do_pipe_flags(int * fd,int flags)9985b249b1bSAl Viro int do_pipe_flags(int *fd, int flags)
9995b249b1bSAl Viro {
10005b249b1bSAl Viro struct file *files[2];
10015b249b1bSAl Viro int error = __do_pipe_flags(fd, files, flags);
10025b249b1bSAl Viro if (!error) {
10035b249b1bSAl Viro fd_install(fd[0], files[0]);
10045b249b1bSAl Viro fd_install(fd[1], files[1]);
10055b249b1bSAl Viro }
10065b249b1bSAl Viro return error;
10075b249b1bSAl Viro }
10085b249b1bSAl Viro
10091da177e4SLinus Torvalds /*
1010d35c7b0eSUlrich Drepper * sys_pipe() is the normal C calling standard for creating
1011d35c7b0eSUlrich Drepper * a pipe. It's not the way Unix traditionally does this, though.
1012d35c7b0eSUlrich Drepper */
do_pipe2(int __user * fildes,int flags)10130a216dd1SDominik Brodowski static int do_pipe2(int __user *fildes, int flags)
1014d35c7b0eSUlrich Drepper {
10155b249b1bSAl Viro struct file *files[2];
1016d35c7b0eSUlrich Drepper int fd[2];
1017d35c7b0eSUlrich Drepper int error;
1018d35c7b0eSUlrich Drepper
10195b249b1bSAl Viro error = __do_pipe_flags(fd, files, flags);
1020d35c7b0eSUlrich Drepper if (!error) {
10215b249b1bSAl Viro if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
10225b249b1bSAl Viro fput(files[0]);
10235b249b1bSAl Viro fput(files[1]);
10245b249b1bSAl Viro put_unused_fd(fd[0]);
10255b249b1bSAl Viro put_unused_fd(fd[1]);
1026d35c7b0eSUlrich Drepper error = -EFAULT;
10275b249b1bSAl Viro } else {
10285b249b1bSAl Viro fd_install(fd[0], files[0]);
10295b249b1bSAl Viro fd_install(fd[1], files[1]);
1030d35c7b0eSUlrich Drepper }
1031ba719baeSUlrich Drepper }
1032d35c7b0eSUlrich Drepper return error;
1033d35c7b0eSUlrich Drepper }
1034d35c7b0eSUlrich Drepper
SYSCALL_DEFINE2(pipe2,int __user *,fildes,int,flags)10350a216dd1SDominik Brodowski SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
10360a216dd1SDominik Brodowski {
10370a216dd1SDominik Brodowski return do_pipe2(fildes, flags);
10380a216dd1SDominik Brodowski }
10390a216dd1SDominik Brodowski
SYSCALL_DEFINE1(pipe,int __user *,fildes)10402b664219SHeiko Carstens SYSCALL_DEFINE1(pipe, int __user *, fildes)
1041ed8cae8bSUlrich Drepper {
10420a216dd1SDominik Brodowski return do_pipe2(fildes, 0);
1043ed8cae8bSUlrich Drepper }
1044ed8cae8bSUlrich Drepper
1045472e5b05SLinus Torvalds /*
1046472e5b05SLinus Torvalds * This is the stupid "wait for pipe to be readable or writable"
1047472e5b05SLinus Torvalds * model.
1048472e5b05SLinus Torvalds *
1049472e5b05SLinus Torvalds * See pipe_read/write() for the proper kind of exclusive wait,
1050472e5b05SLinus Torvalds * but that requires that we wake up any other readers/writers
1051472e5b05SLinus Torvalds * if we then do not end up reading everything (ie the whole
1052472e5b05SLinus Torvalds * "wake_next_reader/writer" logic in pipe_read/write()).
1053472e5b05SLinus Torvalds */
pipe_wait_readable(struct pipe_inode_info * pipe)1054472e5b05SLinus Torvalds void pipe_wait_readable(struct pipe_inode_info *pipe)
1055472e5b05SLinus Torvalds {
1056472e5b05SLinus Torvalds pipe_unlock(pipe);
1057472e5b05SLinus Torvalds wait_event_interruptible(pipe->rd_wait, pipe_readable(pipe));
1058472e5b05SLinus Torvalds pipe_lock(pipe);
1059472e5b05SLinus Torvalds }
1060472e5b05SLinus Torvalds
pipe_wait_writable(struct pipe_inode_info * pipe)1061472e5b05SLinus Torvalds void pipe_wait_writable(struct pipe_inode_info *pipe)
1062472e5b05SLinus Torvalds {
1063472e5b05SLinus Torvalds pipe_unlock(pipe);
1064472e5b05SLinus Torvalds wait_event_interruptible(pipe->wr_wait, pipe_writable(pipe));
1065472e5b05SLinus Torvalds pipe_lock(pipe);
1066472e5b05SLinus Torvalds }
1067472e5b05SLinus Torvalds
1068472e5b05SLinus Torvalds /*
1069472e5b05SLinus Torvalds * This depends on both the wait (here) and the wakeup (wake_up_partner)
1070472e5b05SLinus Torvalds * holding the pipe lock, so "*cnt" is stable and we know a wakeup cannot
1071472e5b05SLinus Torvalds * race with the count check and waitqueue prep.
1072472e5b05SLinus Torvalds *
1073472e5b05SLinus Torvalds * Normally in order to avoid races, you'd do the prepare_to_wait() first,
1074472e5b05SLinus Torvalds * then check the condition you're waiting for, and only then sleep. But
1075472e5b05SLinus Torvalds * because of the pipe lock, we can check the condition before being on
1076472e5b05SLinus Torvalds * the wait queue.
1077472e5b05SLinus Torvalds *
1078472e5b05SLinus Torvalds * We use the 'rd_wait' waitqueue for pipe partner waiting.
1079472e5b05SLinus Torvalds */
wait_for_partner(struct pipe_inode_info * pipe,unsigned int * cnt)1080fc7478a2SAl Viro static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
1081f776c738SAl Viro {
1082472e5b05SLinus Torvalds DEFINE_WAIT(rdwait);
1083f776c738SAl Viro int cur = *cnt;
1084f776c738SAl Viro
1085f776c738SAl Viro while (cur == *cnt) {
1086472e5b05SLinus Torvalds prepare_to_wait(&pipe->rd_wait, &rdwait, TASK_INTERRUPTIBLE);
1087472e5b05SLinus Torvalds pipe_unlock(pipe);
1088472e5b05SLinus Torvalds schedule();
1089472e5b05SLinus Torvalds finish_wait(&pipe->rd_wait, &rdwait);
1090472e5b05SLinus Torvalds pipe_lock(pipe);
1091f776c738SAl Viro if (signal_pending(current))
1092f776c738SAl Viro break;
1093f776c738SAl Viro }
1094f776c738SAl Viro return cur == *cnt ? -ERESTARTSYS : 0;
1095f776c738SAl Viro }
1096f776c738SAl Viro
wake_up_partner(struct pipe_inode_info * pipe)1097fc7478a2SAl Viro static void wake_up_partner(struct pipe_inode_info *pipe)
1098f776c738SAl Viro {
10996551d5c5SLinus Torvalds wake_up_interruptible_all(&pipe->rd_wait);
1100f776c738SAl Viro }
1101f776c738SAl Viro
fifo_open(struct inode * inode,struct file * filp)1102f776c738SAl Viro static int fifo_open(struct inode *inode, struct file *filp)
1103f776c738SAl Viro {
1104f776c738SAl Viro struct pipe_inode_info *pipe;
1105599a0ac1SAl Viro bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
1106f776c738SAl Viro int ret;
1107f776c738SAl Viro
1108ba5bb147SAl Viro filp->f_version = 0;
1109ba5bb147SAl Viro
1110ba5bb147SAl Viro spin_lock(&inode->i_lock);
1111ba5bb147SAl Viro if (inode->i_pipe) {
1112f776c738SAl Viro pipe = inode->i_pipe;
1113ba5bb147SAl Viro pipe->files++;
1114ba5bb147SAl Viro spin_unlock(&inode->i_lock);
1115ba5bb147SAl Viro } else {
1116ba5bb147SAl Viro spin_unlock(&inode->i_lock);
11177bee130eSAl Viro pipe = alloc_pipe_info();
1118f776c738SAl Viro if (!pipe)
1119ba5bb147SAl Viro return -ENOMEM;
1120ba5bb147SAl Viro pipe->files = 1;
1121ba5bb147SAl Viro spin_lock(&inode->i_lock);
1122ba5bb147SAl Viro if (unlikely(inode->i_pipe)) {
1123ba5bb147SAl Viro inode->i_pipe->files++;
1124ba5bb147SAl Viro spin_unlock(&inode->i_lock);
11254b8a8f1eSAl Viro free_pipe_info(pipe);
1126ba5bb147SAl Viro pipe = inode->i_pipe;
1127ba5bb147SAl Viro } else {
1128f776c738SAl Viro inode->i_pipe = pipe;
1129ba5bb147SAl Viro spin_unlock(&inode->i_lock);
1130f776c738SAl Viro }
1131ba5bb147SAl Viro }
1132de32ec4cSAl Viro filp->private_data = pipe;
1133ba5bb147SAl Viro /* OK, we have a pipe and it's pinned down */
1134ba5bb147SAl Viro
1135ebec73f4SAl Viro __pipe_lock(pipe);
1136f776c738SAl Viro
1137f776c738SAl Viro /* We can only do regular read/write on fifos */
1138d8e464ecSLinus Torvalds stream_open(inode, filp);
1139f776c738SAl Viro
1140d8e464ecSLinus Torvalds switch (filp->f_mode & (FMODE_READ | FMODE_WRITE)) {
1141f776c738SAl Viro case FMODE_READ:
1142f776c738SAl Viro /*
1143f776c738SAl Viro * O_RDONLY
1144f776c738SAl Viro * POSIX.1 says that O_NONBLOCK means return with the FIFO
1145f776c738SAl Viro * opened, even when there is no process writing the FIFO.
1146f776c738SAl Viro */
1147f776c738SAl Viro pipe->r_counter++;
1148f776c738SAl Viro if (pipe->readers++ == 0)
1149fc7478a2SAl Viro wake_up_partner(pipe);
1150f776c738SAl Viro
1151599a0ac1SAl Viro if (!is_pipe && !pipe->writers) {
1152f776c738SAl Viro if ((filp->f_flags & O_NONBLOCK)) {
1153a9a08845SLinus Torvalds /* suppress EPOLLHUP until we have
1154f776c738SAl Viro * seen a writer */
1155f776c738SAl Viro filp->f_version = pipe->w_counter;
1156f776c738SAl Viro } else {
1157fc7478a2SAl Viro if (wait_for_partner(pipe, &pipe->w_counter))
1158f776c738SAl Viro goto err_rd;
1159f776c738SAl Viro }
1160f776c738SAl Viro }
1161f776c738SAl Viro break;
1162f776c738SAl Viro
1163f776c738SAl Viro case FMODE_WRITE:
1164f776c738SAl Viro /*
1165f776c738SAl Viro * O_WRONLY
1166f776c738SAl Viro * POSIX.1 says that O_NONBLOCK means return -1 with
1167f776c738SAl Viro * errno=ENXIO when there is no process reading the FIFO.
1168f776c738SAl Viro */
1169f776c738SAl Viro ret = -ENXIO;
1170599a0ac1SAl Viro if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
1171f776c738SAl Viro goto err;
1172f776c738SAl Viro
1173f776c738SAl Viro pipe->w_counter++;
1174f776c738SAl Viro if (!pipe->writers++)
1175fc7478a2SAl Viro wake_up_partner(pipe);
1176f776c738SAl Viro
1177599a0ac1SAl Viro if (!is_pipe && !pipe->readers) {
1178fc7478a2SAl Viro if (wait_for_partner(pipe, &pipe->r_counter))
1179f776c738SAl Viro goto err_wr;
1180f776c738SAl Viro }
1181f776c738SAl Viro break;
1182f776c738SAl Viro
1183f776c738SAl Viro case FMODE_READ | FMODE_WRITE:
1184f776c738SAl Viro /*
1185f776c738SAl Viro * O_RDWR
1186f776c738SAl Viro * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
1187f776c738SAl Viro * This implementation will NEVER block on a O_RDWR open, since
1188f776c738SAl Viro * the process can at least talk to itself.
1189f776c738SAl Viro */
1190f776c738SAl Viro
1191f776c738SAl Viro pipe->readers++;
1192f776c738SAl Viro pipe->writers++;
1193f776c738SAl Viro pipe->r_counter++;
1194f776c738SAl Viro pipe->w_counter++;
1195f776c738SAl Viro if (pipe->readers == 1 || pipe->writers == 1)
1196fc7478a2SAl Viro wake_up_partner(pipe);
1197f776c738SAl Viro break;
1198f776c738SAl Viro
1199f776c738SAl Viro default:
1200f776c738SAl Viro ret = -EINVAL;
1201f776c738SAl Viro goto err;
1202f776c738SAl Viro }
1203f776c738SAl Viro
1204f776c738SAl Viro /* Ok! */
1205ebec73f4SAl Viro __pipe_unlock(pipe);
1206f776c738SAl Viro return 0;
1207f776c738SAl Viro
1208f776c738SAl Viro err_rd:
1209f776c738SAl Viro if (!--pipe->readers)
12100ddad21dSLinus Torvalds wake_up_interruptible(&pipe->wr_wait);
1211f776c738SAl Viro ret = -ERESTARTSYS;
1212f776c738SAl Viro goto err;
1213f776c738SAl Viro
1214f776c738SAl Viro err_wr:
1215f776c738SAl Viro if (!--pipe->writers)
12166551d5c5SLinus Torvalds wake_up_interruptible_all(&pipe->rd_wait);
1217f776c738SAl Viro ret = -ERESTARTSYS;
1218f776c738SAl Viro goto err;
1219f776c738SAl Viro
1220f776c738SAl Viro err:
1221ebec73f4SAl Viro __pipe_unlock(pipe);
1222b0d8d229SLinus Torvalds
1223b0d8d229SLinus Torvalds put_pipe_info(inode, pipe);
1224f776c738SAl Viro return ret;
1225f776c738SAl Viro }
1226f776c738SAl Viro
1227599a0ac1SAl Viro const struct file_operations pipefifo_fops = {
1228599a0ac1SAl Viro .open = fifo_open,
1229599a0ac1SAl Viro .llseek = no_llseek,
1230fb9096a3SAl Viro .read_iter = pipe_read,
1231f0d1bec9SAl Viro .write_iter = pipe_write,
1232a11e1d43SLinus Torvalds .poll = pipe_poll,
1233599a0ac1SAl Viro .unlocked_ioctl = pipe_ioctl,
1234599a0ac1SAl Viro .release = pipe_release,
1235599a0ac1SAl Viro .fasync = pipe_fasync,
1236f8ad8187SJohannes Berg .splice_write = iter_file_splice_write,
1237f776c738SAl Viro };
1238f776c738SAl Viro
1239d35c7b0eSUlrich Drepper /*
1240f491bd71SMichael Kerrisk (man-pages) * Currently we rely on the pipe array holding a power-of-2 number
1241d3f14c48SJoe Lawrence * of pages. Returns 0 on error.
1242f491bd71SMichael Kerrisk (man-pages) */
round_pipe_size(unsigned int size)1243515c5046SLuca Vizzarro unsigned int round_pipe_size(unsigned int size)
1244f491bd71SMichael Kerrisk (man-pages) {
1245c4fed5a9SEric Biggers if (size > (1U << 31))
124696e99be4SEric Biggers return 0;
124796e99be4SEric Biggers
12484c2e4befSEric Biggers /* Minimum pipe size, as required by POSIX */
12494c2e4befSEric Biggers if (size < PAGE_SIZE)
1250c4fed5a9SEric Biggers return PAGE_SIZE;
1251d3f14c48SJoe Lawrence
1252c4fed5a9SEric Biggers return roundup_pow_of_two(size);
1253f491bd71SMichael Kerrisk (man-pages) }
1254f491bd71SMichael Kerrisk (man-pages)
1255f491bd71SMichael Kerrisk (man-pages) /*
1256c73be61cSDavid Howells * Resize the pipe ring to a number of slots.
1257189b0ddcSDavid Howells *
1258189b0ddcSDavid Howells * Note the pipe can be reduced in capacity, but only if the current
1259189b0ddcSDavid Howells * occupancy doesn't exceed nr_slots; if it does, EBUSY will be
1260189b0ddcSDavid Howells * returned instead.
126135f3d14dSJens Axboe */
pipe_resize_ring(struct pipe_inode_info * pipe,unsigned int nr_slots)1262c73be61cSDavid Howells int pipe_resize_ring(struct pipe_inode_info *pipe, unsigned int nr_slots)
126335f3d14dSJens Axboe {
126435f3d14dSJens Axboe struct pipe_buffer *bufs;
1265c73be61cSDavid Howells unsigned int head, tail, mask, n;
126635f3d14dSJens Axboe
1267906f9040SLinus Torvalds bufs = kcalloc(nr_slots, sizeof(*bufs),
1268906f9040SLinus Torvalds GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
1269c73be61cSDavid Howells if (unlikely(!bufs))
1270c73be61cSDavid Howells return -ENOMEM;
127135f3d14dSJens Axboe
1272189b0ddcSDavid Howells spin_lock_irq(&pipe->rd_wait.lock);
1273189b0ddcSDavid Howells mask = pipe->ring_size - 1;
1274189b0ddcSDavid Howells head = pipe->head;
1275189b0ddcSDavid Howells tail = pipe->tail;
1276189b0ddcSDavid Howells
1277189b0ddcSDavid Howells n = pipe_occupancy(head, tail);
1278189b0ddcSDavid Howells if (nr_slots < n) {
1279189b0ddcSDavid Howells spin_unlock_irq(&pipe->rd_wait.lock);
1280189b0ddcSDavid Howells kfree(bufs);
1281189b0ddcSDavid Howells return -EBUSY;
1282189b0ddcSDavid Howells }
1283189b0ddcSDavid Howells
128435f3d14dSJens Axboe /*
128535f3d14dSJens Axboe * The pipe array wraps around, so just start the new one at zero
12868cefc107SDavid Howells * and adjust the indices.
128735f3d14dSJens Axboe */
12888cefc107SDavid Howells if (n > 0) {
12898cefc107SDavid Howells unsigned int h = head & mask;
12908cefc107SDavid Howells unsigned int t = tail & mask;
12918cefc107SDavid Howells if (h > t) {
12928cefc107SDavid Howells memcpy(bufs, pipe->bufs + t,
12938cefc107SDavid Howells n * sizeof(struct pipe_buffer));
12948cefc107SDavid Howells } else {
12958cefc107SDavid Howells unsigned int tsize = pipe->ring_size - t;
12968cefc107SDavid Howells if (h > 0)
12978cefc107SDavid Howells memcpy(bufs + tsize, pipe->bufs,
12988cefc107SDavid Howells h * sizeof(struct pipe_buffer));
12998cefc107SDavid Howells memcpy(bufs, pipe->bufs + t,
13008cefc107SDavid Howells tsize * sizeof(struct pipe_buffer));
13018cefc107SDavid Howells }
130235f3d14dSJens Axboe }
130335f3d14dSJens Axboe
13048cefc107SDavid Howells head = n;
13058cefc107SDavid Howells tail = 0;
13068cefc107SDavid Howells
1307906f9040SLinus Torvalds kfree(pipe->bufs);
130835f3d14dSJens Axboe pipe->bufs = bufs;
13098cefc107SDavid Howells pipe->ring_size = nr_slots;
1310c73be61cSDavid Howells if (pipe->max_usage > nr_slots)
13116718b6f8SDavid Howells pipe->max_usage = nr_slots;
13128cefc107SDavid Howells pipe->tail = tail;
13138cefc107SDavid Howells pipe->head = head;
13146551d5c5SLinus Torvalds
131568e51bdbSLukas Schauer if (!pipe_has_watch_queue(pipe)) {
131668e51bdbSLukas Schauer pipe->max_usage = nr_slots;
131768e51bdbSLukas Schauer pipe->nr_accounted = nr_slots;
131868e51bdbSLukas Schauer }
131968e51bdbSLukas Schauer
1320189b0ddcSDavid Howells spin_unlock_irq(&pipe->rd_wait.lock);
1321189b0ddcSDavid Howells
13226551d5c5SLinus Torvalds /* This might have made more room for writers */
13236551d5c5SLinus Torvalds wake_up_interruptible(&pipe->wr_wait);
1324c73be61cSDavid Howells return 0;
1325c73be61cSDavid Howells }
1326c73be61cSDavid Howells
1327c73be61cSDavid Howells /*
1328c73be61cSDavid Howells * Allocate a new array of pipe buffers and copy the info over. Returns the
1329c73be61cSDavid Howells * pipe size if successful, or return -ERROR on error.
1330c73be61cSDavid Howells */
pipe_set_size(struct pipe_inode_info * pipe,unsigned int arg)1331515c5046SLuca Vizzarro static long pipe_set_size(struct pipe_inode_info *pipe, unsigned int arg)
1332c73be61cSDavid Howells {
1333c73be61cSDavid Howells unsigned long user_bufs;
1334c73be61cSDavid Howells unsigned int nr_slots, size;
1335c73be61cSDavid Howells long ret = 0;
1336c73be61cSDavid Howells
133767f45795SMax Kellermann if (pipe_has_watch_queue(pipe))
1338c73be61cSDavid Howells return -EBUSY;
1339c73be61cSDavid Howells
1340c73be61cSDavid Howells size = round_pipe_size(arg);
1341c73be61cSDavid Howells nr_slots = size >> PAGE_SHIFT;
1342c73be61cSDavid Howells
1343c73be61cSDavid Howells if (!nr_slots)
1344c73be61cSDavid Howells return -EINVAL;
1345c73be61cSDavid Howells
1346c73be61cSDavid Howells /*
1347c73be61cSDavid Howells * If trying to increase the pipe capacity, check that an
1348c73be61cSDavid Howells * unprivileged user is not trying to exceed various limits
1349c73be61cSDavid Howells * (soft limit check here, hard limit check just below).
1350c73be61cSDavid Howells * Decreasing the pipe capacity is always permitted, even
1351c73be61cSDavid Howells * if the user is currently over a limit.
1352c73be61cSDavid Howells */
1353c73be61cSDavid Howells if (nr_slots > pipe->max_usage &&
1354c73be61cSDavid Howells size > pipe_max_size && !capable(CAP_SYS_RESOURCE))
1355c73be61cSDavid Howells return -EPERM;
1356c73be61cSDavid Howells
1357c73be61cSDavid Howells user_bufs = account_pipe_buffers(pipe->user, pipe->nr_accounted, nr_slots);
1358c73be61cSDavid Howells
1359c73be61cSDavid Howells if (nr_slots > pipe->max_usage &&
1360c73be61cSDavid Howells (too_many_pipe_buffers_hard(user_bufs) ||
1361c73be61cSDavid Howells too_many_pipe_buffers_soft(user_bufs)) &&
1362c73be61cSDavid Howells pipe_is_unprivileged_user()) {
1363c73be61cSDavid Howells ret = -EPERM;
1364c73be61cSDavid Howells goto out_revert_acct;
1365c73be61cSDavid Howells }
1366c73be61cSDavid Howells
1367c73be61cSDavid Howells ret = pipe_resize_ring(pipe, nr_slots);
1368c73be61cSDavid Howells if (ret < 0)
1369c73be61cSDavid Howells goto out_revert_acct;
1370c73be61cSDavid Howells
13716718b6f8SDavid Howells return pipe->max_usage * PAGE_SIZE;
1372b0b91d18SMichael Kerrisk (man-pages)
1373b0b91d18SMichael Kerrisk (man-pages) out_revert_acct:
1374c73be61cSDavid Howells (void) account_pipe_buffers(pipe->user, nr_slots, pipe->nr_accounted);
1375b0b91d18SMichael Kerrisk (man-pages) return ret;
137635f3d14dSJens Axboe }
137735f3d14dSJens Axboe
1378ff9da691SJens Axboe /*
13794e7b5671SChristoph Hellwig * Note that i_pipe and i_cdev share the same location, so checking ->i_pipe is
13804e7b5671SChristoph Hellwig * not enough to verify that this is a pipe.
138172083646SLinus Torvalds */
get_pipe_info(struct file * file,bool for_splice)1382c73be61cSDavid Howells struct pipe_inode_info *get_pipe_info(struct file *file, bool for_splice)
138372083646SLinus Torvalds {
1384c73be61cSDavid Howells struct pipe_inode_info *pipe = file->private_data;
1385c73be61cSDavid Howells
1386c73be61cSDavid Howells if (file->f_op != &pipefifo_fops || !pipe)
1387c73be61cSDavid Howells return NULL;
138867f45795SMax Kellermann if (for_splice && pipe_has_watch_queue(pipe))
1389c73be61cSDavid Howells return NULL;
1390c73be61cSDavid Howells return pipe;
139172083646SLinus Torvalds }
139272083646SLinus Torvalds
pipe_fcntl(struct file * file,unsigned int cmd,unsigned int arg)1393515c5046SLuca Vizzarro long pipe_fcntl(struct file *file, unsigned int cmd, unsigned int arg)
139435f3d14dSJens Axboe {
139535f3d14dSJens Axboe struct pipe_inode_info *pipe;
139635f3d14dSJens Axboe long ret;
139735f3d14dSJens Axboe
1398c73be61cSDavid Howells pipe = get_pipe_info(file, false);
139935f3d14dSJens Axboe if (!pipe)
140035f3d14dSJens Axboe return -EBADF;
140135f3d14dSJens Axboe
1402ebec73f4SAl Viro __pipe_lock(pipe);
140335f3d14dSJens Axboe
140435f3d14dSJens Axboe switch (cmd) {
1405d37d4166SMichael Kerrisk (man-pages) case F_SETPIPE_SZ:
1406d37d4166SMichael Kerrisk (man-pages) ret = pipe_set_size(pipe, arg);
140735f3d14dSJens Axboe break;
140835f3d14dSJens Axboe case F_GETPIPE_SZ:
14096718b6f8SDavid Howells ret = pipe->max_usage * PAGE_SIZE;
141035f3d14dSJens Axboe break;
141135f3d14dSJens Axboe default:
141235f3d14dSJens Axboe ret = -EINVAL;
141335f3d14dSJens Axboe break;
141435f3d14dSJens Axboe }
141535f3d14dSJens Axboe
1416ebec73f4SAl Viro __pipe_unlock(pipe);
141735f3d14dSJens Axboe return ret;
141835f3d14dSJens Axboe }
141935f3d14dSJens Axboe
1420ff0c7d15SNick Piggin static const struct super_operations pipefs_ops = {
1421ff0c7d15SNick Piggin .destroy_inode = free_inode_nonrcu,
1422d70ef97bSPavel Emelyanov .statfs = simple_statfs,
1423ff0c7d15SNick Piggin };
1424ff0c7d15SNick Piggin
142535f3d14dSJens Axboe /*
14261da177e4SLinus Torvalds * pipefs should _never_ be mounted by userland - too much of security hassle,
14271da177e4SLinus Torvalds * no real gain from having the whole whorehouse mounted. So we don't need
14281da177e4SLinus Torvalds * any operations on the root directory. However, we need a non-trivial
14291da177e4SLinus Torvalds * d_name - pipe: will go nicely and kill the special-casing in procfs.
14301da177e4SLinus Torvalds */
14314fa7ec5dSDavid Howells
pipefs_init_fs_context(struct fs_context * fc)14324fa7ec5dSDavid Howells static int pipefs_init_fs_context(struct fs_context *fc)
14331da177e4SLinus Torvalds {
14344fa7ec5dSDavid Howells struct pseudo_fs_context *ctx = init_pseudo(fc, PIPEFS_MAGIC);
14354fa7ec5dSDavid Howells if (!ctx)
14364fa7ec5dSDavid Howells return -ENOMEM;
14374fa7ec5dSDavid Howells ctx->ops = &pipefs_ops;
14384fa7ec5dSDavid Howells ctx->dops = &pipefs_dentry_operations;
14394fa7ec5dSDavid Howells return 0;
14401da177e4SLinus Torvalds }
14411da177e4SLinus Torvalds
14421da177e4SLinus Torvalds static struct file_system_type pipe_fs_type = {
14431da177e4SLinus Torvalds .name = "pipefs",
14444fa7ec5dSDavid Howells .init_fs_context = pipefs_init_fs_context,
14451da177e4SLinus Torvalds .kill_sb = kill_anon_super,
14461da177e4SLinus Torvalds };
14471da177e4SLinus Torvalds
14481998f193SLuis Chamberlain #ifdef CONFIG_SYSCTL
do_proc_dopipe_max_size_conv(unsigned long * lvalp,unsigned int * valp,int write,void * data)14491998f193SLuis Chamberlain static int do_proc_dopipe_max_size_conv(unsigned long *lvalp,
14501998f193SLuis Chamberlain unsigned int *valp,
14511998f193SLuis Chamberlain int write, void *data)
14521998f193SLuis Chamberlain {
14531998f193SLuis Chamberlain if (write) {
14541998f193SLuis Chamberlain unsigned int val;
14551998f193SLuis Chamberlain
14561998f193SLuis Chamberlain val = round_pipe_size(*lvalp);
14571998f193SLuis Chamberlain if (val == 0)
14581998f193SLuis Chamberlain return -EINVAL;
14591998f193SLuis Chamberlain
14601998f193SLuis Chamberlain *valp = val;
14611998f193SLuis Chamberlain } else {
14621998f193SLuis Chamberlain unsigned int val = *valp;
14631998f193SLuis Chamberlain *lvalp = (unsigned long) val;
14641998f193SLuis Chamberlain }
14651998f193SLuis Chamberlain
14661998f193SLuis Chamberlain return 0;
14671998f193SLuis Chamberlain }
14681998f193SLuis Chamberlain
proc_dopipe_max_size(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)14691998f193SLuis Chamberlain static int proc_dopipe_max_size(struct ctl_table *table, int write,
14701998f193SLuis Chamberlain void *buffer, size_t *lenp, loff_t *ppos)
14711998f193SLuis Chamberlain {
14721998f193SLuis Chamberlain return do_proc_douintvec(table, write, buffer, lenp, ppos,
14731998f193SLuis Chamberlain do_proc_dopipe_max_size_conv, NULL);
14741998f193SLuis Chamberlain }
14751998f193SLuis Chamberlain
14761998f193SLuis Chamberlain static struct ctl_table fs_pipe_sysctls[] = {
14771998f193SLuis Chamberlain {
14781998f193SLuis Chamberlain .procname = "pipe-max-size",
14791998f193SLuis Chamberlain .data = &pipe_max_size,
14801998f193SLuis Chamberlain .maxlen = sizeof(pipe_max_size),
14811998f193SLuis Chamberlain .mode = 0644,
14821998f193SLuis Chamberlain .proc_handler = proc_dopipe_max_size,
14831998f193SLuis Chamberlain },
14841998f193SLuis Chamberlain {
14851998f193SLuis Chamberlain .procname = "pipe-user-pages-hard",
14861998f193SLuis Chamberlain .data = &pipe_user_pages_hard,
14871998f193SLuis Chamberlain .maxlen = sizeof(pipe_user_pages_hard),
14881998f193SLuis Chamberlain .mode = 0644,
14891998f193SLuis Chamberlain .proc_handler = proc_doulongvec_minmax,
14901998f193SLuis Chamberlain },
14911998f193SLuis Chamberlain {
14921998f193SLuis Chamberlain .procname = "pipe-user-pages-soft",
14931998f193SLuis Chamberlain .data = &pipe_user_pages_soft,
14941998f193SLuis Chamberlain .maxlen = sizeof(pipe_user_pages_soft),
14951998f193SLuis Chamberlain .mode = 0644,
14961998f193SLuis Chamberlain .proc_handler = proc_doulongvec_minmax,
14971998f193SLuis Chamberlain },
14981998f193SLuis Chamberlain { }
14991998f193SLuis Chamberlain };
15001998f193SLuis Chamberlain #endif
15011998f193SLuis Chamberlain
init_pipe_fs(void)15021da177e4SLinus Torvalds static int __init init_pipe_fs(void)
15031da177e4SLinus Torvalds {
15041da177e4SLinus Torvalds int err = register_filesystem(&pipe_fs_type);
1505341b446bSIngo Molnar
15061da177e4SLinus Torvalds if (!err) {
15071da177e4SLinus Torvalds pipe_mnt = kern_mount(&pipe_fs_type);
15081da177e4SLinus Torvalds if (IS_ERR(pipe_mnt)) {
15091da177e4SLinus Torvalds err = PTR_ERR(pipe_mnt);
15101da177e4SLinus Torvalds unregister_filesystem(&pipe_fs_type);
15111da177e4SLinus Torvalds }
15121da177e4SLinus Torvalds }
15131998f193SLuis Chamberlain #ifdef CONFIG_SYSCTL
15141998f193SLuis Chamberlain register_sysctl_init("fs", fs_pipe_sysctls);
15151998f193SLuis Chamberlain #endif
15161da177e4SLinus Torvalds return err;
15171da177e4SLinus Torvalds }
15181da177e4SLinus Torvalds
15191da177e4SLinus Torvalds fs_initcall(init_pipe_fs);
1520