b24413180f560 (Greg Kroah-Hartman 2017-11-01 15:07:57 +0100 1) // SPDX-License-Identifier: GPL-2.0
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3) * linux/fs/pipe.c
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 5) * Copyright (C) 1991, 1992, 1999 Linus Torvalds
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 6) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 7)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 8) #include <linux/mm.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 9) #include <linux/file.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 10) #include <linux/poll.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 11) #include <linux/slab.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 12) #include <linux/module.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 13) #include <linux/init.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 14) #include <linux/fs.h>
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 15) #include <linux/log2.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 16) #include <linux/mount.h>
4fa7ec5db7e73 (David Howells 2019-03-25 16:38:23 +0000 17) #include <linux/pseudo_fs.h>
b502bd1152472 (Muthu Kumar 2012-03-23 15:01:50 -0700 18) #include <linux/magic.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 19) #include <linux/pipe_fs_i.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 20) #include <linux/uio.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 21) #include <linux/highmem.h>
5274f052e7b3d (Jens Axboe 2006-03-30 15:15:30 +0200 22) #include <linux/pagemap.h>
db3495099d3d5 (Al Viro 2007-02-07 01:48:00 -0500 23) #include <linux/audit.h>
ba719baeabbff (Ulrich Drepper 2008-05-06 20:42:38 -0700 24) #include <linux/syscalls.h>
b492e95be0ae6 (Jens Axboe 2010-05-19 21:03:16 +0200 25) #include <linux/fcntl.h>
d86133bd396f5 (Vladimir Davydov 2016-07-26 15:24:33 -0700 26) #include <linux/memcontrol.h>
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 27) #include <linux/watch_queue.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 28)
7c0f6ba682b9c (Linus Torvalds 2016-12-24 11:46:01 -0800 29) #include <linux/uaccess.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 30) #include <asm/ioctls.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 31)
599a0ac14e065 (Al Viro 2013-03-12 09:58:10 -0400 32) #include "internal.h"
599a0ac14e065 (Al Viro 2013-03-12 09:58:10 -0400 33)
e745e30339991 (Alex Xu (Hello71) 2021-08-05 10:40:47 -0400 34) /*
e745e30339991 (Alex Xu (Hello71) 2021-08-05 10:40:47 -0400 35) * New pipe buffers will be restricted to this size while the user is exceeding
e745e30339991 (Alex Xu (Hello71) 2021-08-05 10:40:47 -0400 36) * their pipe buffer quota. The general pipe use case needs at least two
e745e30339991 (Alex Xu (Hello71) 2021-08-05 10:40:47 -0400 37) * buffers: one for data yet to be read, and one for new data. If this is less
e745e30339991 (Alex Xu (Hello71) 2021-08-05 10:40:47 -0400 38) * than two, then a write to a non-empty pipe may block even if the pipe is not
e745e30339991 (Alex Xu (Hello71) 2021-08-05 10:40:47 -0400 39) * full. This can occur with GNU make jobserver or similar uses of pipes as
e745e30339991 (Alex Xu (Hello71) 2021-08-05 10:40:47 -0400 40) * semaphores: multiple processes may be waiting to write tokens back to the
e745e30339991 (Alex Xu (Hello71) 2021-08-05 10:40:47 -0400 41) * pipe before reading tokens: https://lore.kernel.org/lkml/1628086770.5rn8p04n6j.none@localhost/.
e745e30339991 (Alex Xu (Hello71) 2021-08-05 10:40:47 -0400 42) *
e745e30339991 (Alex Xu (Hello71) 2021-08-05 10:40:47 -0400 43) * Users can reduce their pipe buffers with F_SETPIPE_SZ below this at their
e745e30339991 (Alex Xu (Hello71) 2021-08-05 10:40:47 -0400 44) * own risk, namely: pipe writes to non-full pipes may block until the pipe is
e745e30339991 (Alex Xu (Hello71) 2021-08-05 10:40:47 -0400 45) * emptied.
e745e30339991 (Alex Xu (Hello71) 2021-08-05 10:40:47 -0400 46) */
e745e30339991 (Alex Xu (Hello71) 2021-08-05 10:40:47 -0400 47) #define PIPE_MIN_DEF_BUFFERS 2
e745e30339991 (Alex Xu (Hello71) 2021-08-05 10:40:47 -0400 48)
b492e95be0ae6 (Jens Axboe 2010-05-19 21:03:16 +0200 49) /*
b492e95be0ae6 (Jens Axboe 2010-05-19 21:03:16 +0200 50) * The max size that a non-root user is allowed to grow the pipe. Can
ff9da691c0498 (Jens Axboe 2010-06-03 14:54:39 +0200 51) * be set by root in /proc/sys/fs/pipe-max-size
b492e95be0ae6 (Jens Axboe 2010-05-19 21:03:16 +0200 52) */
ff9da691c0498 (Jens Axboe 2010-06-03 14:54:39 +0200 53) unsigned int pipe_max_size = 1048576;
ff9da691c0498 (Jens Axboe 2010-06-03 14:54:39 +0200 54)
759c01142a5d0 (Willy Tarreau 2016-01-18 16:36:09 +0100 55) /* Maximum allocatable pages per user. Hard limit is unset by default, soft
759c01142a5d0 (Willy Tarreau 2016-01-18 16:36:09 +0100 56) * matches default values.
759c01142a5d0 (Willy Tarreau 2016-01-18 16:36:09 +0100 57) */
759c01142a5d0 (Willy Tarreau 2016-01-18 16:36:09 +0100 58) unsigned long pipe_user_pages_hard;
759c01142a5d0 (Willy Tarreau 2016-01-18 16:36:09 +0100 59) unsigned long pipe_user_pages_soft = PIPE_DEF_BUFFERS * INR_OPEN_CUR;
759c01142a5d0 (Willy Tarreau 2016-01-18 16:36:09 +0100 60)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 61) /*
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 62) * We use head and tail indices that aren't masked off, except at the point of
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 63) * dereference, but rather they're allowed to wrap naturally. This means there
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 64) * isn't a dead spot in the buffer, but the ring has to be a power of two and
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 65) * <= 2^31.
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 66) * -- David Howells 2019-09-23.
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 67) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 68) * Reads with count = 0 should always return 0.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 69) * -- Julian Bradfield 1999-06-07.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 70) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 71) * FIFOs and Pipes now generate SIGIO for both readers and writers.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 72) * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 73) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 74) * pipe_read & write cleanup
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 75) * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 76) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 77)
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 78) static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass)
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 79) {
6447a3cf19da8 (Al Viro 2013-03-21 11:01:38 -0400 80) if (pipe->files)
72b0d9aacb89f (Al Viro 2013-03-21 02:32:24 -0400 81) mutex_lock_nested(&pipe->mutex, subclass);
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 82) }
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 83)
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 84) void pipe_lock(struct pipe_inode_info *pipe)
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 85) {
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 86) /*
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 87) * pipe_lock() nests non-pipe inode locks (for writing to a file)
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 88) */
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 89) pipe_lock_nested(pipe, I_MUTEX_PARENT);
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 90) }
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 91) EXPORT_SYMBOL(pipe_lock);
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 92)
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 93) void pipe_unlock(struct pipe_inode_info *pipe)
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 94) {
6447a3cf19da8 (Al Viro 2013-03-21 11:01:38 -0400 95) if (pipe->files)
72b0d9aacb89f (Al Viro 2013-03-21 02:32:24 -0400 96) mutex_unlock(&pipe->mutex);
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 97) }
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 98) EXPORT_SYMBOL(pipe_unlock);
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 99)
ebec73f4752b7 (Al Viro 2013-03-21 12:24:01 -0400 100) static inline void __pipe_lock(struct pipe_inode_info *pipe)
ebec73f4752b7 (Al Viro 2013-03-21 12:24:01 -0400 101) {
ebec73f4752b7 (Al Viro 2013-03-21 12:24:01 -0400 102) mutex_lock_nested(&pipe->mutex, I_MUTEX_PARENT);
ebec73f4752b7 (Al Viro 2013-03-21 12:24:01 -0400 103) }
ebec73f4752b7 (Al Viro 2013-03-21 12:24:01 -0400 104)
ebec73f4752b7 (Al Viro 2013-03-21 12:24:01 -0400 105) static inline void __pipe_unlock(struct pipe_inode_info *pipe)
ebec73f4752b7 (Al Viro 2013-03-21 12:24:01 -0400 106) {
ebec73f4752b7 (Al Viro 2013-03-21 12:24:01 -0400 107) mutex_unlock(&pipe->mutex);
ebec73f4752b7 (Al Viro 2013-03-21 12:24:01 -0400 108) }
ebec73f4752b7 (Al Viro 2013-03-21 12:24:01 -0400 109)
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 110) void pipe_double_lock(struct pipe_inode_info *pipe1,
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 111) struct pipe_inode_info *pipe2)
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 112) {
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 113) BUG_ON(pipe1 == pipe2);
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 114)
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 115) if (pipe1 < pipe2) {
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 116) pipe_lock_nested(pipe1, I_MUTEX_PARENT);
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 117) pipe_lock_nested(pipe2, I_MUTEX_CHILD);
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 118) } else {
023d43c7b5a23 (Peter Zijlstra 2009-07-21 10:09:23 +0200 119) pipe_lock_nested(pipe2, I_MUTEX_PARENT);
023d43c7b5a23 (Peter Zijlstra 2009-07-21 10:09:23 +0200 120) pipe_lock_nested(pipe1, I_MUTEX_CHILD);
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 121) }
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 122) }
61e0d47c33cc3 (Miklos Szeredi 2009-04-14 19:48:41 +0200 123)
341b446bc5aa3 (Ingo Molnar 2006-04-11 13:57:45 +0200 124) static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
341b446bc5aa3 (Ingo Molnar 2006-04-11 13:57:45 +0200 125) struct pipe_buffer *buf)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 126) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 127) struct page *page = buf->page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 128)
5274f052e7b3d (Jens Axboe 2006-03-30 15:15:30 +0200 129) /*
5274f052e7b3d (Jens Axboe 2006-03-30 15:15:30 +0200 130) * If nobody else uses this page, and we don't already have a
5274f052e7b3d (Jens Axboe 2006-03-30 15:15:30 +0200 131) * temporary page, let's keep track of it as a one-deep
341b446bc5aa3 (Ingo Molnar 2006-04-11 13:57:45 +0200 132) * allocation cache. (Otherwise just release our reference to it)
5274f052e7b3d (Jens Axboe 2006-03-30 15:15:30 +0200 133) */
341b446bc5aa3 (Ingo Molnar 2006-04-11 13:57:45 +0200 134) if (page_count(page) == 1 && !pipe->tmp_page)
923f4f23940d2 (Ingo Molnar 2006-04-11 13:53:33 +0200 135) pipe->tmp_page = page;
341b446bc5aa3 (Ingo Molnar 2006-04-11 13:57:45 +0200 136) else
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 137) put_page(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 138) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 139)
c928f642c29a5 (Christoph Hellwig 2020-05-20 17:58:16 +0200 140) static bool anon_pipe_buf_try_steal(struct pipe_inode_info *pipe,
c928f642c29a5 (Christoph Hellwig 2020-05-20 17:58:16 +0200 141) struct pipe_buffer *buf)
d86133bd396f5 (Vladimir Davydov 2016-07-26 15:24:33 -0700 142) {
d86133bd396f5 (Vladimir Davydov 2016-07-26 15:24:33 -0700 143) struct page *page = buf->page;
d86133bd396f5 (Vladimir Davydov 2016-07-26 15:24:33 -0700 144)
c928f642c29a5 (Christoph Hellwig 2020-05-20 17:58:16 +0200 145) if (page_count(page) != 1)
c928f642c29a5 (Christoph Hellwig 2020-05-20 17:58:16 +0200 146) return false;
c928f642c29a5 (Christoph Hellwig 2020-05-20 17:58:16 +0200 147) memcg_kmem_uncharge_page(page, 0);
c928f642c29a5 (Christoph Hellwig 2020-05-20 17:58:16 +0200 148) __SetPageLocked(page);
c928f642c29a5 (Christoph Hellwig 2020-05-20 17:58:16 +0200 149) return true;
d86133bd396f5 (Vladimir Davydov 2016-07-26 15:24:33 -0700 150) }
d86133bd396f5 (Vladimir Davydov 2016-07-26 15:24:33 -0700 151)
0845718dafea3 (Jens Axboe 2007-06-12 20:51:32 +0200 152) /**
c928f642c29a5 (Christoph Hellwig 2020-05-20 17:58:16 +0200 153) * generic_pipe_buf_try_steal - attempt to take ownership of a &pipe_buffer
0845718dafea3 (Jens Axboe 2007-06-12 20:51:32 +0200 154) * @pipe: the pipe that the buffer belongs to
0845718dafea3 (Jens Axboe 2007-06-12 20:51:32 +0200 155) * @buf: the buffer to attempt to steal
0845718dafea3 (Jens Axboe 2007-06-12 20:51:32 +0200 156) *
0845718dafea3 (Jens Axboe 2007-06-12 20:51:32 +0200 157) * Description:
b51d63c6d3078 (Randy Dunlap 2008-02-13 15:03:22 -0800 158) * This function attempts to steal the &struct page attached to
0845718dafea3 (Jens Axboe 2007-06-12 20:51:32 +0200 159) * @buf. If successful, this function returns 0 and returns with
0845718dafea3 (Jens Axboe 2007-06-12 20:51:32 +0200 160) * the page locked. The caller may then reuse the page for whatever
b51d63c6d3078 (Randy Dunlap 2008-02-13 15:03:22 -0800 161) * he wishes; the typical use is insertion into a different file
0845718dafea3 (Jens Axboe 2007-06-12 20:51:32 +0200 162) * page cache.
0845718dafea3 (Jens Axboe 2007-06-12 20:51:32 +0200 163) */
c928f642c29a5 (Christoph Hellwig 2020-05-20 17:58:16 +0200 164) bool generic_pipe_buf_try_steal(struct pipe_inode_info *pipe,
c928f642c29a5 (Christoph Hellwig 2020-05-20 17:58:16 +0200 165) struct pipe_buffer *buf)
5abc97aa25b2c (Jens Axboe 2006-03-30 15:16:46 +0200 166) {
46e678c96bbd7 (Jens Axboe 2006-04-30 16:36:32 +0200 167) struct page *page = buf->page;
46e678c96bbd7 (Jens Axboe 2006-04-30 16:36:32 +0200 168)
0845718dafea3 (Jens Axboe 2007-06-12 20:51:32 +0200 169) /*
0845718dafea3 (Jens Axboe 2007-06-12 20:51:32 +0200 170) * A reference of one is golden, that means that the owner of this
0845718dafea3 (Jens Axboe 2007-06-12 20:51:32 +0200 171) * page is the only one holding a reference to it. lock the page
0845718dafea3 (Jens Axboe 2007-06-12 20:51:32 +0200 172) * and return OK.
0845718dafea3 (Jens Axboe 2007-06-12 20:51:32 +0200 173) */
46e678c96bbd7 (Jens Axboe 2006-04-30 16:36:32 +0200 174) if (page_count(page) == 1) {
46e678c96bbd7 (Jens Axboe 2006-04-30 16:36:32 +0200 175) lock_page(page);
c928f642c29a5 (Christoph Hellwig 2020-05-20 17:58:16 +0200 176) return true;
46e678c96bbd7 (Jens Axboe 2006-04-30 16:36:32 +0200 177) }
c928f642c29a5 (Christoph Hellwig 2020-05-20 17:58:16 +0200 178) return false;
5abc97aa25b2c (Jens Axboe 2006-03-30 15:16:46 +0200 179) }
c928f642c29a5 (Christoph Hellwig 2020-05-20 17:58:16 +0200 180) EXPORT_SYMBOL(generic_pipe_buf_try_steal);
5abc97aa25b2c (Jens Axboe 2006-03-30 15:16:46 +0200 181)
0845718dafea3 (Jens Axboe 2007-06-12 20:51:32 +0200 182) /**
b51d63c6d3078 (Randy Dunlap 2008-02-13 15:03:22 -0800 183) * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
0845718dafea3 (Jens Axboe 2007-06-12 20:51:32 +0200 184) * @pipe: the pipe that the buffer belongs to
0845718dafea3 (Jens Axboe 2007-06-12 20:51:32 +0200 185) * @buf: the buffer to get a reference to
0845718dafea3 (Jens Axboe 2007-06-12 20:51:32 +0200 186) *
0845718dafea3 (Jens Axboe 2007-06-12 20:51:32 +0200 187) * Description:
0845718dafea3 (Jens Axboe 2007-06-12 20:51:32 +0200 188) * This function grabs an extra reference to @buf. It's used in
3d742d4b6ebb3 (Randy Dunlap 2021-02-24 12:00:48 -0800 189) * the tee() system call, when we duplicate the buffers in one
0845718dafea3 (Jens Axboe 2007-06-12 20:51:32 +0200 190) * pipe into another.
0845718dafea3 (Jens Axboe 2007-06-12 20:51:32 +0200 191) */
15fab63e1e57b (Matthew Wilcox 2019-04-05 14:02:10 -0700 192) bool generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
70524490ee2ea (Jens Axboe 2006-04-11 15:51:17 +0200 193) {
15fab63e1e57b (Matthew Wilcox 2019-04-05 14:02:10 -0700 194) return try_get_page(buf->page);
70524490ee2ea (Jens Axboe 2006-04-11 15:51:17 +0200 195) }
51921cb746f56 (Miklos Szeredi 2010-05-26 08:44:22 +0200 196) EXPORT_SYMBOL(generic_pipe_buf_get);
70524490ee2ea (Jens Axboe 2006-04-11 15:51:17 +0200 197)
6818173bd6584 (Miklos Szeredi 2009-05-07 15:37:36 +0200 198) /**
6818173bd6584 (Miklos Szeredi 2009-05-07 15:37:36 +0200 199) * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
6818173bd6584 (Miklos Szeredi 2009-05-07 15:37:36 +0200 200) * @pipe: the pipe that the buffer belongs to
6818173bd6584 (Miklos Szeredi 2009-05-07 15:37:36 +0200 201) * @buf: the buffer to put a reference to
6818173bd6584 (Miklos Szeredi 2009-05-07 15:37:36 +0200 202) *
6818173bd6584 (Miklos Szeredi 2009-05-07 15:37:36 +0200 203) * Description:
6818173bd6584 (Miklos Szeredi 2009-05-07 15:37:36 +0200 204) * This function releases a reference to @buf.
6818173bd6584 (Miklos Szeredi 2009-05-07 15:37:36 +0200 205) */
6818173bd6584 (Miklos Szeredi 2009-05-07 15:37:36 +0200 206) void generic_pipe_buf_release(struct pipe_inode_info *pipe,
6818173bd6584 (Miklos Szeredi 2009-05-07 15:37:36 +0200 207) struct pipe_buffer *buf)
6818173bd6584 (Miklos Szeredi 2009-05-07 15:37:36 +0200 208) {
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 209) put_page(buf->page);
6818173bd6584 (Miklos Szeredi 2009-05-07 15:37:36 +0200 210) }
51921cb746f56 (Miklos Szeredi 2010-05-26 08:44:22 +0200 211) EXPORT_SYMBOL(generic_pipe_buf_release);
6818173bd6584 (Miklos Szeredi 2009-05-07 15:37:36 +0200 212)
d4c3cca941b64 (Eric Dumazet 2006-12-13 00:34:04 -0800 213) static const struct pipe_buf_operations anon_pipe_buf_ops = {
c928f642c29a5 (Christoph Hellwig 2020-05-20 17:58:16 +0200 214) .release = anon_pipe_buf_release,
c928f642c29a5 (Christoph Hellwig 2020-05-20 17:58:16 +0200 215) .try_steal = anon_pipe_buf_try_steal,
c928f642c29a5 (Christoph Hellwig 2020-05-20 17:58:16 +0200 216) .get = generic_pipe_buf_get,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 217) };
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 218)
85190d15f4ea8 (Linus Torvalds 2019-12-07 13:53:09 -0800 219) /* Done while waiting without holding the pipe lock - thus the READ_ONCE() */
85190d15f4ea8 (Linus Torvalds 2019-12-07 13:53:09 -0800 220) static inline bool pipe_readable(const struct pipe_inode_info *pipe)
85190d15f4ea8 (Linus Torvalds 2019-12-07 13:53:09 -0800 221) {
85190d15f4ea8 (Linus Torvalds 2019-12-07 13:53:09 -0800 222) unsigned int head = READ_ONCE(pipe->head);
85190d15f4ea8 (Linus Torvalds 2019-12-07 13:53:09 -0800 223) unsigned int tail = READ_ONCE(pipe->tail);
85190d15f4ea8 (Linus Torvalds 2019-12-07 13:53:09 -0800 224) unsigned int writers = READ_ONCE(pipe->writers);
85190d15f4ea8 (Linus Torvalds 2019-12-07 13:53:09 -0800 225)
85190d15f4ea8 (Linus Torvalds 2019-12-07 13:53:09 -0800 226) return !pipe_empty(head, tail) || !writers;
85190d15f4ea8 (Linus Torvalds 2019-12-07 13:53:09 -0800 227) }
85190d15f4ea8 (Linus Torvalds 2019-12-07 13:53:09 -0800 228)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 229) static ssize_t
fb9096a344e29 (Al Viro 2014-04-02 19:56:54 -0400 230) pipe_read(struct kiocb *iocb, struct iov_iter *to)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 231) {
fb9096a344e29 (Al Viro 2014-04-02 19:56:54 -0400 232) size_t total_len = iov_iter_count(to);
ee0b3e671baff (Badari Pulavarty 2006-09-30 23:28:47 -0700 233) struct file *filp = iocb->ki_filp;
de32ec4cfeb3b (Al Viro 2013-03-21 11:16:56 -0400 234) struct pipe_inode_info *pipe = filp->private_data;
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 235) bool was_full, wake_next_reader = false;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 236) ssize_t ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 237)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 238) /* Null read succeeds. */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 239) if (unlikely(total_len == 0))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 240) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 241)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 242) ret = 0;
ebec73f4752b7 (Al Viro 2013-03-21 12:24:01 -0400 243) __pipe_lock(pipe);
f467a6a66419a (Linus Torvalds 2019-12-07 12:54:26 -0800 244)
f467a6a66419a (Linus Torvalds 2019-12-07 12:54:26 -0800 245) /*
f467a6a66419a (Linus Torvalds 2019-12-07 12:54:26 -0800 246) * We only wake up writers if the pipe was full when we started
f467a6a66419a (Linus Torvalds 2019-12-07 12:54:26 -0800 247) * reading in order to avoid unnecessary wakeups.
f467a6a66419a (Linus Torvalds 2019-12-07 12:54:26 -0800 248) *
f467a6a66419a (Linus Torvalds 2019-12-07 12:54:26 -0800 249) * But when we do wake up writers, we do so using a sync wakeup
f467a6a66419a (Linus Torvalds 2019-12-07 12:54:26 -0800 250) * (WF_SYNC), because we want them to get going and generate more
f467a6a66419a (Linus Torvalds 2019-12-07 12:54:26 -0800 251) * data for us.
f467a6a66419a (Linus Torvalds 2019-12-07 12:54:26 -0800 252) */
f467a6a66419a (Linus Torvalds 2019-12-07 12:54:26 -0800 253) was_full = pipe_full(pipe->head, pipe->tail, pipe->max_usage);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 254) for (;;) {
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 255) unsigned int head = pipe->head;
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 256) unsigned int tail = pipe->tail;
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 257) unsigned int mask = pipe->ring_size - 1;
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 258)
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 259) #ifdef CONFIG_WATCH_QUEUE
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 260) if (pipe->note_loss) {
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 261) struct watch_notification n;
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 262)
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 263) if (total_len < 8) {
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 264) if (ret == 0)
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 265) ret = -ENOBUFS;
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 266) break;
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 267) }
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 268)
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 269) n.type = WATCH_TYPE_META;
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 270) n.subtype = WATCH_META_LOSS_NOTIFICATION;
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 271) n.info = watch_sizeof(n);
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 272) if (copy_to_iter(&n, sizeof(n), to) != sizeof(n)) {
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 273) if (ret == 0)
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 274) ret = -EFAULT;
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 275) break;
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 276) }
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 277) ret += sizeof(n);
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 278) total_len -= sizeof(n);
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 279) pipe->note_loss = false;
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 280) }
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 281) #endif
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 282)
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 283) if (!pipe_empty(head, tail)) {
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 284) struct pipe_buffer *buf = &pipe->bufs[tail & mask];
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 285) size_t chars = buf->len;
637b58c2887e5 (Al Viro 2014-02-03 19:11:42 -0500 286) size_t written;
637b58c2887e5 (Al Viro 2014-02-03 19:11:42 -0500 287) int error;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 288)
8cfba76383e90 (David Howells 2020-01-14 17:07:11 +0000 289) if (chars > total_len) {
8cfba76383e90 (David Howells 2020-01-14 17:07:11 +0000 290) if (buf->flags & PIPE_BUF_FLAG_WHOLE) {
8cfba76383e90 (David Howells 2020-01-14 17:07:11 +0000 291) if (ret == 0)
8cfba76383e90 (David Howells 2020-01-14 17:07:11 +0000 292) ret = -ENOBUFS;
8cfba76383e90 (David Howells 2020-01-14 17:07:11 +0000 293) break;
8cfba76383e90 (David Howells 2020-01-14 17:07:11 +0000 294) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 295) chars = total_len;
8cfba76383e90 (David Howells 2020-01-14 17:07:11 +0000 296) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 297)
fba597db4218a (Miklos Szeredi 2016-09-27 10:45:12 +0200 298) error = pipe_buf_confirm(pipe, buf);
f84d751994441 (Jens Axboe 2006-05-01 19:59:03 +0200 299) if (error) {
5274f052e7b3d (Jens Axboe 2006-03-30 15:15:30 +0200 300) if (!ret)
e5953cbdff26f (Nicolas Kaiser 2010-10-21 14:56:00 +0200 301) ret = error;
5274f052e7b3d (Jens Axboe 2006-03-30 15:15:30 +0200 302) break;
5274f052e7b3d (Jens Axboe 2006-03-30 15:15:30 +0200 303) }
f84d751994441 (Jens Axboe 2006-05-01 19:59:03 +0200 304)
fb9096a344e29 (Al Viro 2014-04-02 19:56:54 -0400 305) written = copy_page_to_iter(buf->page, buf->offset, chars, to);
637b58c2887e5 (Al Viro 2014-02-03 19:11:42 -0500 306) if (unlikely(written < chars)) {
341b446bc5aa3 (Ingo Molnar 2006-04-11 13:57:45 +0200 307) if (!ret)
637b58c2887e5 (Al Viro 2014-02-03 19:11:42 -0500 308) ret = -EFAULT;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 309) break;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 310) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 311) ret += chars;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 312) buf->offset += chars;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 313) buf->len -= chars;
9883035ae7ede (Linus Torvalds 2012-04-29 13:12:42 -0700 314)
9883035ae7ede (Linus Torvalds 2012-04-29 13:12:42 -0700 315) /* Was it a packet buffer? Clean up and exit */
9883035ae7ede (Linus Torvalds 2012-04-29 13:12:42 -0700 316) if (buf->flags & PIPE_BUF_FLAG_PACKET) {
9883035ae7ede (Linus Torvalds 2012-04-29 13:12:42 -0700 317) total_len = chars;
9883035ae7ede (Linus Torvalds 2012-04-29 13:12:42 -0700 318) buf->len = 0;
9883035ae7ede (Linus Torvalds 2012-04-29 13:12:42 -0700 319) }
9883035ae7ede (Linus Torvalds 2012-04-29 13:12:42 -0700 320)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 321) if (!buf->len) {
a779638cf622f (Miklos Szeredi 2016-09-27 10:45:12 +0200 322) pipe_buf_release(pipe, buf);
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 323) spin_lock_irq(&pipe->rd_wait.lock);
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 324) #ifdef CONFIG_WATCH_QUEUE
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 325) if (buf->flags & PIPE_BUF_FLAG_LOSS)
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 326) pipe->note_loss = true;
e7d553d69cf63 (David Howells 2020-01-14 17:07:12 +0000 327) #endif
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 328) tail++;
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 329) pipe->tail = tail;
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 330) spin_unlock_irq(&pipe->rd_wait.lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 331) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 332) total_len -= chars;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 333) if (!total_len)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 334) break; /* common path: read succeeded */
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 335) if (!pipe_empty(head, tail)) /* More to do? */
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 336) continue;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 337) }
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 338)
923f4f23940d2 (Ingo Molnar 2006-04-11 13:53:33 +0200 339) if (!pipe->writers)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 340) break;
a28c8b9db8a10 (Linus Torvalds 2019-12-07 13:21:01 -0800 341) if (ret)
a28c8b9db8a10 (Linus Torvalds 2019-12-07 13:21:01 -0800 342) break;
a28c8b9db8a10 (Linus Torvalds 2019-12-07 13:21:01 -0800 343) if (filp->f_flags & O_NONBLOCK) {
a28c8b9db8a10 (Linus Torvalds 2019-12-07 13:21:01 -0800 344) ret = -EAGAIN;
a28c8b9db8a10 (Linus Torvalds 2019-12-07 13:21:01 -0800 345) break;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 346) }
85190d15f4ea8 (Linus Torvalds 2019-12-07 13:53:09 -0800 347) __pipe_unlock(pipe);
d1c6a2aa02af0 (Linus Torvalds 2019-12-11 11:46:19 -0800 348)
d1c6a2aa02af0 (Linus Torvalds 2019-12-11 11:46:19 -0800 349) /*
d1c6a2aa02af0 (Linus Torvalds 2019-12-11 11:46:19 -0800 350) * We only get here if we didn't actually read anything.
d1c6a2aa02af0 (Linus Torvalds 2019-12-11 11:46:19 -0800 351) *
d1c6a2aa02af0 (Linus Torvalds 2019-12-11 11:46:19 -0800 352) * However, we could have seen (and removed) a zero-sized
d1c6a2aa02af0 (Linus Torvalds 2019-12-11 11:46:19 -0800 353) * pipe buffer, and might have made space in the buffers
d1c6a2aa02af0 (Linus Torvalds 2019-12-11 11:46:19 -0800 354) * that way.
d1c6a2aa02af0 (Linus Torvalds 2019-12-11 11:46:19 -0800 355) *
d1c6a2aa02af0 (Linus Torvalds 2019-12-11 11:46:19 -0800 356) * You can't make zero-sized pipe buffers by doing an empty
d1c6a2aa02af0 (Linus Torvalds 2019-12-11 11:46:19 -0800 357) * write (not even in packet mode), but they can happen if
d1c6a2aa02af0 (Linus Torvalds 2019-12-11 11:46:19 -0800 358) * the writer gets an EFAULT when trying to fill a buffer
d1c6a2aa02af0 (Linus Torvalds 2019-12-11 11:46:19 -0800 359) * that already got allocated and inserted in the buffer
d1c6a2aa02af0 (Linus Torvalds 2019-12-11 11:46:19 -0800 360) * array.
d1c6a2aa02af0 (Linus Torvalds 2019-12-11 11:46:19 -0800 361) *
d1c6a2aa02af0 (Linus Torvalds 2019-12-11 11:46:19 -0800 362) * So we still need to wake up any pending writers in the
d1c6a2aa02af0 (Linus Torvalds 2019-12-11 11:46:19 -0800 363) * _very_ unlikely case that the pipe was full, but we got
d1c6a2aa02af0 (Linus Torvalds 2019-12-11 11:46:19 -0800 364) * no data.
d1c6a2aa02af0 (Linus Torvalds 2019-12-11 11:46:19 -0800 365) */
6b7533e796aa9 (Linus Torvalds 2021-08-24 10:39:25 -0700 366) if (unlikely(was_full))
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 367) wake_up_interruptible_sync_poll(&pipe->wr_wait, EPOLLOUT | EPOLLWRNORM);
6b7533e796aa9 (Linus Torvalds 2021-08-24 10:39:25 -0700 368) kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
d1c6a2aa02af0 (Linus Torvalds 2019-12-11 11:46:19 -0800 369)
d1c6a2aa02af0 (Linus Torvalds 2019-12-11 11:46:19 -0800 370) /*
d1c6a2aa02af0 (Linus Torvalds 2019-12-11 11:46:19 -0800 371) * But because we didn't read anything, at this point we can
d1c6a2aa02af0 (Linus Torvalds 2019-12-11 11:46:19 -0800 372) * just return directly with -ERESTARTSYS if we're interrupted,
d1c6a2aa02af0 (Linus Torvalds 2019-12-11 11:46:19 -0800 373) * since we've done any required wakeups and there's no need
d1c6a2aa02af0 (Linus Torvalds 2019-12-11 11:46:19 -0800 374) * to mark anything accessed. And we've dropped the lock.
d1c6a2aa02af0 (Linus Torvalds 2019-12-11 11:46:19 -0800 375) */
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 376) if (wait_event_interruptible_exclusive(pipe->rd_wait, pipe_readable(pipe)) < 0)
d1c6a2aa02af0 (Linus Torvalds 2019-12-11 11:46:19 -0800 377) return -ERESTARTSYS;
d1c6a2aa02af0 (Linus Torvalds 2019-12-11 11:46:19 -0800 378)
85190d15f4ea8 (Linus Torvalds 2019-12-07 13:53:09 -0800 379) __pipe_lock(pipe);
f467a6a66419a (Linus Torvalds 2019-12-07 12:54:26 -0800 380) was_full = pipe_full(pipe->head, pipe->tail, pipe->max_usage);
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 381) wake_next_reader = true;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 382) }
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 383) if (pipe_empty(pipe->head, pipe->tail))
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 384) wake_next_reader = false;
ebec73f4752b7 (Al Viro 2013-03-21 12:24:01 -0400 385) __pipe_unlock(pipe);
341b446bc5aa3 (Ingo Molnar 2006-04-11 13:57:45 +0200 386)
6b7533e796aa9 (Linus Torvalds 2021-08-24 10:39:25 -0700 387) if (was_full)
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 388) wake_up_interruptible_sync_poll(&pipe->wr_wait, EPOLLOUT | EPOLLWRNORM);
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 389) if (wake_next_reader)
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 390) wake_up_interruptible_sync_poll(&pipe->rd_wait, EPOLLIN | EPOLLRDNORM);
6b7533e796aa9 (Linus Torvalds 2021-08-24 10:39:25 -0700 391) kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 392) if (ret > 0)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 393) file_accessed(filp);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 394) return ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 395) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 396)
9883035ae7ede (Linus Torvalds 2012-04-29 13:12:42 -0700 397) static inline int is_packetized(struct file *file)
9883035ae7ede (Linus Torvalds 2012-04-29 13:12:42 -0700 398) {
9883035ae7ede (Linus Torvalds 2012-04-29 13:12:42 -0700 399) return (file->f_flags & O_DIRECT) != 0;
9883035ae7ede (Linus Torvalds 2012-04-29 13:12:42 -0700 400) }
9883035ae7ede (Linus Torvalds 2012-04-29 13:12:42 -0700 401)
85190d15f4ea8 (Linus Torvalds 2019-12-07 13:53:09 -0800 402) /* Done while waiting without holding the pipe lock - thus the READ_ONCE() */
85190d15f4ea8 (Linus Torvalds 2019-12-07 13:53:09 -0800 403) static inline bool pipe_writable(const struct pipe_inode_info *pipe)
85190d15f4ea8 (Linus Torvalds 2019-12-07 13:53:09 -0800 404) {
85190d15f4ea8 (Linus Torvalds 2019-12-07 13:53:09 -0800 405) unsigned int head = READ_ONCE(pipe->head);
85190d15f4ea8 (Linus Torvalds 2019-12-07 13:53:09 -0800 406) unsigned int tail = READ_ONCE(pipe->tail);
85190d15f4ea8 (Linus Torvalds 2019-12-07 13:53:09 -0800 407) unsigned int max_usage = READ_ONCE(pipe->max_usage);
85190d15f4ea8 (Linus Torvalds 2019-12-07 13:53:09 -0800 408)
85190d15f4ea8 (Linus Torvalds 2019-12-07 13:53:09 -0800 409) return !pipe_full(head, tail, max_usage) ||
85190d15f4ea8 (Linus Torvalds 2019-12-07 13:53:09 -0800 410) !READ_ONCE(pipe->readers);
85190d15f4ea8 (Linus Torvalds 2019-12-07 13:53:09 -0800 411) }
85190d15f4ea8 (Linus Torvalds 2019-12-07 13:53:09 -0800 412)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 413) static ssize_t
f0d1bec9d58d4 (Al Viro 2014-04-03 15:05:18 -0400 414) pipe_write(struct kiocb *iocb, struct iov_iter *from)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 415) {
ee0b3e671baff (Badari Pulavarty 2006-09-30 23:28:47 -0700 416) struct file *filp = iocb->ki_filp;
de32ec4cfeb3b (Al Viro 2013-03-21 11:16:56 -0400 417) struct pipe_inode_info *pipe = filp->private_data;
8f868d68d335a (David Howells 2019-12-05 22:30:37 +0000 418) unsigned int head;
f0d1bec9d58d4 (Al Viro 2014-04-03 15:05:18 -0400 419) ssize_t ret = 0;
f0d1bec9d58d4 (Al Viro 2014-04-03 15:05:18 -0400 420) size_t total_len = iov_iter_count(from);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 421) ssize_t chars;
1b6b26ae7053e (Linus Torvalds 2019-12-07 12:14:28 -0800 422) bool was_empty = false;
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 423) bool wake_next_writer = false;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 424)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 425) /* Null write succeeds. */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 426) if (unlikely(total_len == 0))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 427) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 428)
ebec73f4752b7 (Al Viro 2013-03-21 12:24:01 -0400 429) __pipe_lock(pipe);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 430)
923f4f23940d2 (Ingo Molnar 2006-04-11 13:53:33 +0200 431) if (!pipe->readers) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 432) send_sig(SIGPIPE, current, 0);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 433) ret = -EPIPE;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 434) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 435) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 436)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 437) #ifdef CONFIG_WATCH_QUEUE
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 438) if (pipe->watch_queue) {
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 439) ret = -EXDEV;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 440) goto out;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 441) }
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 442) #endif
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 443)
1b6b26ae7053e (Linus Torvalds 2019-12-07 12:14:28 -0800 444) /*
1b6b26ae7053e (Linus Torvalds 2019-12-07 12:14:28 -0800 445) * If it wasn't empty we try to merge new data into
1b6b26ae7053e (Linus Torvalds 2019-12-07 12:14:28 -0800 446) * the last buffer.
1b6b26ae7053e (Linus Torvalds 2019-12-07 12:14:28 -0800 447) *
1b6b26ae7053e (Linus Torvalds 2019-12-07 12:14:28 -0800 448) * That naturally merges small writes, but it also
f0aa1bc37e9a9 (Linus Torvalds 2021-07-30 15:42:34 -0700 449) * page-aligns the rest of the writes for large writes
1b6b26ae7053e (Linus Torvalds 2019-12-07 12:14:28 -0800 450) * spanning multiple pages.
1b6b26ae7053e (Linus Torvalds 2019-12-07 12:14:28 -0800 451) */
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 452) head = pipe->head;
4abb1d77321ae (Linus Torvalds 2021-08-05 10:04:43 -0700 453) was_empty = pipe_empty(head, pipe->tail);
1b6b26ae7053e (Linus Torvalds 2019-12-07 12:14:28 -0800 454) chars = total_len & (PAGE_SIZE-1);
4abb1d77321ae (Linus Torvalds 2021-08-05 10:04:43 -0700 455) if (chars && !was_empty) {
8f868d68d335a (David Howells 2019-12-05 22:30:37 +0000 456) unsigned int mask = pipe->ring_size - 1;
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 457) struct pipe_buffer *buf = &pipe->bufs[(head - 1) & mask];
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 458) int offset = buf->offset + buf->len;
341b446bc5aa3 (Ingo Molnar 2006-04-11 13:57:45 +0200 459)
f6dd975583bd8 (Christoph Hellwig 2020-05-20 17:58:12 +0200 460) if ((buf->flags & PIPE_BUF_FLAG_CAN_MERGE) &&
f6dd975583bd8 (Christoph Hellwig 2020-05-20 17:58:12 +0200 461) offset + chars <= PAGE_SIZE) {
fba597db4218a (Miklos Szeredi 2016-09-27 10:45:12 +0200 462) ret = pipe_buf_confirm(pipe, buf);
6ae08069939f1 (Eric Biggers 2015-10-17 16:26:09 -0500 463) if (ret)
5274f052e7b3d (Jens Axboe 2006-03-30 15:15:30 +0200 464) goto out;
f84d751994441 (Jens Axboe 2006-05-01 19:59:03 +0200 465)
f0d1bec9d58d4 (Al Viro 2014-04-03 15:05:18 -0400 466) ret = copy_page_from_iter(buf->page, offset, chars, from);
f0d1bec9d58d4 (Al Viro 2014-04-03 15:05:18 -0400 467) if (unlikely(ret < chars)) {
6ae08069939f1 (Eric Biggers 2015-10-17 16:26:09 -0500 468) ret = -EFAULT;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 469) goto out;
f6762b7ad8edd (Jens Axboe 2006-05-01 20:02:05 +0200 470) }
1b6b26ae7053e (Linus Torvalds 2019-12-07 12:14:28 -0800 471)
6ae08069939f1 (Eric Biggers 2015-10-17 16:26:09 -0500 472) buf->len += ret;
f0d1bec9d58d4 (Al Viro 2014-04-03 15:05:18 -0400 473) if (!iov_iter_count(from))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 474) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 475) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 476) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 477)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 478) for (;;) {
923f4f23940d2 (Ingo Molnar 2006-04-11 13:53:33 +0200 479) if (!pipe->readers) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 480) send_sig(SIGPIPE, current, 0);
341b446bc5aa3 (Ingo Molnar 2006-04-11 13:57:45 +0200 481) if (!ret)
341b446bc5aa3 (Ingo Molnar 2006-04-11 13:57:45 +0200 482) ret = -EPIPE;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 483) break;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 484) }
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 485)
a194dfe6e6f6f (David Howells 2019-09-20 16:32:19 +0100 486) head = pipe->head;
8f868d68d335a (David Howells 2019-12-05 22:30:37 +0000 487) if (!pipe_full(head, pipe->tail, pipe->max_usage)) {
8f868d68d335a (David Howells 2019-12-05 22:30:37 +0000 488) unsigned int mask = pipe->ring_size - 1;
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 489) struct pipe_buffer *buf = &pipe->bufs[head & mask];
923f4f23940d2 (Ingo Molnar 2006-04-11 13:53:33 +0200 490) struct page *page = pipe->tmp_page;
f0d1bec9d58d4 (Al Viro 2014-04-03 15:05:18 -0400 491) int copied;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 492)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 493) if (!page) {
d86133bd396f5 (Vladimir Davydov 2016-07-26 15:24:33 -0700 494) page = alloc_page(GFP_HIGHUSER | __GFP_ACCOUNT);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 495) if (unlikely(!page)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 496) ret = ret ? : -ENOMEM;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 497) break;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 498) }
923f4f23940d2 (Ingo Molnar 2006-04-11 13:53:33 +0200 499) pipe->tmp_page = page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 500) }
a194dfe6e6f6f (David Howells 2019-09-20 16:32:19 +0100 501)
a194dfe6e6f6f (David Howells 2019-09-20 16:32:19 +0100 502) /* Allocate a slot in the ring in advance and attach an
a194dfe6e6f6f (David Howells 2019-09-20 16:32:19 +0100 503) * empty buffer. If we fault or otherwise fail to use
a194dfe6e6f6f (David Howells 2019-09-20 16:32:19 +0100 504) * it, either the reader will consume it or it'll still
a194dfe6e6f6f (David Howells 2019-09-20 16:32:19 +0100 505) * be there for the next write.
a194dfe6e6f6f (David Howells 2019-09-20 16:32:19 +0100 506) */
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 507) spin_lock_irq(&pipe->rd_wait.lock);
a194dfe6e6f6f (David Howells 2019-09-20 16:32:19 +0100 508)
a194dfe6e6f6f (David Howells 2019-09-20 16:32:19 +0100 509) head = pipe->head;
8f868d68d335a (David Howells 2019-12-05 22:30:37 +0000 510) if (pipe_full(head, pipe->tail, pipe->max_usage)) {
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 511) spin_unlock_irq(&pipe->rd_wait.lock);
8df441294dd34 (David Howells 2019-10-07 16:30:51 +0100 512) continue;
8df441294dd34 (David Howells 2019-10-07 16:30:51 +0100 513) }
8df441294dd34 (David Howells 2019-10-07 16:30:51 +0100 514)
a194dfe6e6f6f (David Howells 2019-09-20 16:32:19 +0100 515) pipe->head = head + 1;
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 516) spin_unlock_irq(&pipe->rd_wait.lock);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 517)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 518) /* Insert it into the buffer array */
a194dfe6e6f6f (David Howells 2019-09-20 16:32:19 +0100 519) buf = &pipe->bufs[head & mask];
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 520) buf->page = page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 521) buf->ops = &anon_pipe_buf_ops;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 522) buf->offset = 0;
a194dfe6e6f6f (David Howells 2019-09-20 16:32:19 +0100 523) buf->len = 0;
f6dd975583bd8 (Christoph Hellwig 2020-05-20 17:58:12 +0200 524) if (is_packetized(filp))
9883035ae7ede (Linus Torvalds 2012-04-29 13:12:42 -0700 525) buf->flags = PIPE_BUF_FLAG_PACKET;
f6dd975583bd8 (Christoph Hellwig 2020-05-20 17:58:12 +0200 526) else
f6dd975583bd8 (Christoph Hellwig 2020-05-20 17:58:12 +0200 527) buf->flags = PIPE_BUF_FLAG_CAN_MERGE;
923f4f23940d2 (Ingo Molnar 2006-04-11 13:53:33 +0200 528) pipe->tmp_page = NULL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 529)
a194dfe6e6f6f (David Howells 2019-09-20 16:32:19 +0100 530) copied = copy_page_from_iter(page, 0, PAGE_SIZE, from);
a194dfe6e6f6f (David Howells 2019-09-20 16:32:19 +0100 531) if (unlikely(copied < PAGE_SIZE && iov_iter_count(from))) {
a194dfe6e6f6f (David Howells 2019-09-20 16:32:19 +0100 532) if (!ret)
a194dfe6e6f6f (David Howells 2019-09-20 16:32:19 +0100 533) ret = -EFAULT;
a194dfe6e6f6f (David Howells 2019-09-20 16:32:19 +0100 534) break;
a194dfe6e6f6f (David Howells 2019-09-20 16:32:19 +0100 535) }
a194dfe6e6f6f (David Howells 2019-09-20 16:32:19 +0100 536) ret += copied;
a194dfe6e6f6f (David Howells 2019-09-20 16:32:19 +0100 537) buf->offset = 0;
a194dfe6e6f6f (David Howells 2019-09-20 16:32:19 +0100 538) buf->len = copied;
a194dfe6e6f6f (David Howells 2019-09-20 16:32:19 +0100 539)
f0d1bec9d58d4 (Al Viro 2014-04-03 15:05:18 -0400 540) if (!iov_iter_count(from))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 541) break;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 542) }
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 543)
8f868d68d335a (David Howells 2019-12-05 22:30:37 +0000 544) if (!pipe_full(head, pipe->tail, pipe->max_usage))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 545) continue;
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 546)
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 547) /* Wait for buffer space to become available. */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 548) if (filp->f_flags & O_NONBLOCK) {
341b446bc5aa3 (Ingo Molnar 2006-04-11 13:57:45 +0200 549) if (!ret)
341b446bc5aa3 (Ingo Molnar 2006-04-11 13:57:45 +0200 550) ret = -EAGAIN;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 551) break;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 552) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 553) if (signal_pending(current)) {
341b446bc5aa3 (Ingo Molnar 2006-04-11 13:57:45 +0200 554) if (!ret)
341b446bc5aa3 (Ingo Molnar 2006-04-11 13:57:45 +0200 555) ret = -ERESTARTSYS;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 556) break;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 557) }
1b6b26ae7053e (Linus Torvalds 2019-12-07 12:14:28 -0800 558)
1b6b26ae7053e (Linus Torvalds 2019-12-07 12:14:28 -0800 559) /*
1b6b26ae7053e (Linus Torvalds 2019-12-07 12:14:28 -0800 560) * We're going to release the pipe lock and wait for more
1b6b26ae7053e (Linus Torvalds 2019-12-07 12:14:28 -0800 561) * space. We wake up any readers if necessary, and then
1b6b26ae7053e (Linus Torvalds 2019-12-07 12:14:28 -0800 562) * after waiting we need to re-check whether the pipe
1b6b26ae7053e (Linus Torvalds 2019-12-07 12:14:28 -0800 563) * become empty while we dropped the lock.
1b6b26ae7053e (Linus Torvalds 2019-12-07 12:14:28 -0800 564) */
85190d15f4ea8 (Linus Torvalds 2019-12-07 13:53:09 -0800 565) __pipe_unlock(pipe);
6b7533e796aa9 (Linus Torvalds 2021-08-24 10:39:25 -0700 566) if (was_empty)
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 567) wake_up_interruptible_sync_poll(&pipe->rd_wait, EPOLLIN | EPOLLRDNORM);
6b7533e796aa9 (Linus Torvalds 2021-08-24 10:39:25 -0700 568) kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 569) wait_event_interruptible_exclusive(pipe->wr_wait, pipe_writable(pipe));
85190d15f4ea8 (Linus Torvalds 2019-12-07 13:53:09 -0800 570) __pipe_lock(pipe);
0dd1e3773ae8a (Jan Stancek 2019-12-22 13:33:24 +0100 571) was_empty = pipe_empty(pipe->head, pipe->tail);
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 572) wake_next_writer = true;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 573) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 574) out:
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 575) if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 576) wake_next_writer = false;
ebec73f4752b7 (Al Viro 2013-03-21 12:24:01 -0400 577) __pipe_unlock(pipe);
1b6b26ae7053e (Linus Torvalds 2019-12-07 12:14:28 -0800 578)
1b6b26ae7053e (Linus Torvalds 2019-12-07 12:14:28 -0800 579) /*
1b6b26ae7053e (Linus Torvalds 2019-12-07 12:14:28 -0800 580) * If we do do a wakeup event, we do a 'sync' wakeup, because we
1b6b26ae7053e (Linus Torvalds 2019-12-07 12:14:28 -0800 581) * want the reader to start processing things asap, rather than
1b6b26ae7053e (Linus Torvalds 2019-12-07 12:14:28 -0800 582) * leave the data pending.
1b6b26ae7053e (Linus Torvalds 2019-12-07 12:14:28 -0800 583) *
1b6b26ae7053e (Linus Torvalds 2019-12-07 12:14:28 -0800 584) * This is particularly important for small writes, because of
1b6b26ae7053e (Linus Torvalds 2019-12-07 12:14:28 -0800 585) * how (for example) the GNU make jobserver uses small writes to
1b6b26ae7053e (Linus Torvalds 2019-12-07 12:14:28 -0800 586) * wake up pending jobs
4abb1d77321ae (Linus Torvalds 2021-08-05 10:04:43 -0700 587) *
4abb1d77321ae (Linus Torvalds 2021-08-05 10:04:43 -0700 588) * Epoll nonsensically wants a wakeup whether the pipe
4abb1d77321ae (Linus Torvalds 2021-08-05 10:04:43 -0700 589) * was already empty or not.
1b6b26ae7053e (Linus Torvalds 2019-12-07 12:14:28 -0800 590) */
6b7533e796aa9 (Linus Torvalds 2021-08-24 10:39:25 -0700 591) if (was_empty || pipe->poll_usage)
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 592) wake_up_interruptible_sync_poll(&pipe->rd_wait, EPOLLIN | EPOLLRDNORM);
6b7533e796aa9 (Linus Torvalds 2021-08-24 10:39:25 -0700 593) kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 594) if (wake_next_writer)
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 595) wake_up_interruptible_sync_poll(&pipe->wr_wait, EPOLLOUT | EPOLLWRNORM);
7e775f46a125f (Dmitry Monakhov 2014-01-23 15:55:21 -0800 596) if (ret > 0 && sb_start_write_trylock(file_inode(filp)->i_sb)) {
c3b2da3148344 (Josef Bacik 2012-03-26 09:59:21 -0400 597) int err = file_update_time(filp);
c3b2da3148344 (Josef Bacik 2012-03-26 09:59:21 -0400 598) if (err)
c3b2da3148344 (Josef Bacik 2012-03-26 09:59:21 -0400 599) ret = err;
7e775f46a125f (Dmitry Monakhov 2014-01-23 15:55:21 -0800 600) sb_end_write(file_inode(filp)->i_sb);
c3b2da3148344 (Josef Bacik 2012-03-26 09:59:21 -0400 601) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 602) return ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 603) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 604)
d59d0b1b88c5b (Andi Kleen 2008-02-08 04:21:23 -0800 605) static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 606) {
de32ec4cfeb3b (Al Viro 2013-03-21 11:16:56 -0400 607) struct pipe_inode_info *pipe = filp->private_data;
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 608) int count, head, tail, mask;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 609)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 610) switch (cmd) {
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 611) case FIONREAD:
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 612) __pipe_lock(pipe);
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 613) count = 0;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 614) head = pipe->head;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 615) tail = pipe->tail;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 616) mask = pipe->ring_size - 1;
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 617)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 618) while (tail != head) {
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 619) count += pipe->bufs[tail & mask].len;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 620) tail++;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 621) }
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 622) __pipe_unlock(pipe);
923f4f23940d2 (Ingo Molnar 2006-04-11 13:53:33 +0200 623)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 624) return put_user(count, (int __user *)arg);
923f4f23940d2 (Ingo Molnar 2006-04-11 13:53:33 +0200 625)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 626) #ifdef CONFIG_WATCH_QUEUE
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 627) case IOC_WATCH_QUEUE_SET_SIZE: {
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 628) int ret;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 629) __pipe_lock(pipe);
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 630) ret = watch_queue_set_size(pipe, arg);
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 631) __pipe_unlock(pipe);
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 632) return ret;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 633) }
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 634)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 635) case IOC_WATCH_QUEUE_SET_FILTER:
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 636) return watch_queue_set_filter(
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 637) pipe, (struct watch_notification_filter __user *)arg);
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 638) #endif
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 639)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 640) default:
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 641) return -ENOIOCTLCMD;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 642) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 643) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 644)
dd67081b36a1f (Christoph Hellwig 2017-12-31 16:42:12 +0100 645) /* No kernel lock held - fine */
a11e1d432b51f (Linus Torvalds 2018-06-28 09:43:44 -0700 646) static __poll_t
a11e1d432b51f (Linus Torvalds 2018-06-28 09:43:44 -0700 647) pipe_poll(struct file *filp, poll_table *wait)
dd67081b36a1f (Christoph Hellwig 2017-12-31 16:42:12 +0100 648) {
a11e1d432b51f (Linus Torvalds 2018-06-28 09:43:44 -0700 649) __poll_t mask;
dd67081b36a1f (Christoph Hellwig 2017-12-31 16:42:12 +0100 650) struct pipe_inode_info *pipe = filp->private_data;
ad910e36da4ca (Linus Torvalds 2019-12-07 10:41:17 -0800 651) unsigned int head, tail;
a11e1d432b51f (Linus Torvalds 2018-06-28 09:43:44 -0700 652)
4abb1d77321ae (Linus Torvalds 2021-08-05 10:04:43 -0700 653) /* Epoll has some historical nasty semantics, this enables them */
4abb1d77321ae (Linus Torvalds 2021-08-05 10:04:43 -0700 654) pipe->poll_usage = 1;
4abb1d77321ae (Linus Torvalds 2021-08-05 10:04:43 -0700 655)
ad910e36da4ca (Linus Torvalds 2019-12-07 10:41:17 -0800 656) /*
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 657) * Reading pipe state only -- no need for acquiring the semaphore.
ad910e36da4ca (Linus Torvalds 2019-12-07 10:41:17 -0800 658) *
ad910e36da4ca (Linus Torvalds 2019-12-07 10:41:17 -0800 659) * But because this is racy, the code has to add the
ad910e36da4ca (Linus Torvalds 2019-12-07 10:41:17 -0800 660) * entry to the poll table _first_ ..
ad910e36da4ca (Linus Torvalds 2019-12-07 10:41:17 -0800 661) */
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 662) if (filp->f_mode & FMODE_READ)
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 663) poll_wait(filp, &pipe->rd_wait, wait);
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 664) if (filp->f_mode & FMODE_WRITE)
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 665) poll_wait(filp, &pipe->wr_wait, wait);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 666)
ad910e36da4ca (Linus Torvalds 2019-12-07 10:41:17 -0800 667) /*
ad910e36da4ca (Linus Torvalds 2019-12-07 10:41:17 -0800 668) * .. and only then can you do the racy tests. That way,
ad910e36da4ca (Linus Torvalds 2019-12-07 10:41:17 -0800 669) * if something changes and you got it wrong, the poll
ad910e36da4ca (Linus Torvalds 2019-12-07 10:41:17 -0800 670) * table entry will wake you up and fix it.
ad910e36da4ca (Linus Torvalds 2019-12-07 10:41:17 -0800 671) */
ad910e36da4ca (Linus Torvalds 2019-12-07 10:41:17 -0800 672) head = READ_ONCE(pipe->head);
ad910e36da4ca (Linus Torvalds 2019-12-07 10:41:17 -0800 673) tail = READ_ONCE(pipe->tail);
ad910e36da4ca (Linus Torvalds 2019-12-07 10:41:17 -0800 674)
a11e1d432b51f (Linus Torvalds 2018-06-28 09:43:44 -0700 675) mask = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 676) if (filp->f_mode & FMODE_READ) {
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 677) if (!pipe_empty(head, tail))
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 678) mask |= EPOLLIN | EPOLLRDNORM;
923f4f23940d2 (Ingo Molnar 2006-04-11 13:53:33 +0200 679) if (!pipe->writers && filp->f_version != pipe->w_counter)
a9a08845e9acb (Linus Torvalds 2018-02-11 14:34:03 -0800 680) mask |= EPOLLHUP;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 681) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 682)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 683) if (filp->f_mode & FMODE_WRITE) {
6718b6f855a0b (David Howells 2019-10-16 16:47:32 +0100 684) if (!pipe_full(head, tail, pipe->max_usage))
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 685) mask |= EPOLLOUT | EPOLLWRNORM;
5e5d7a2229261 (Pekka Enberg 2005-09-06 15:17:48 -0700 686) /*
a9a08845e9acb (Linus Torvalds 2018-02-11 14:34:03 -0800 687) * Most Unices do not set EPOLLERR for FIFOs but on Linux they
5e5d7a2229261 (Pekka Enberg 2005-09-06 15:17:48 -0700 688) * behave exactly like pipes for poll().
5e5d7a2229261 (Pekka Enberg 2005-09-06 15:17:48 -0700 689) */
923f4f23940d2 (Ingo Molnar 2006-04-11 13:53:33 +0200 690) if (!pipe->readers)
a9a08845e9acb (Linus Torvalds 2018-02-11 14:34:03 -0800 691) mask |= EPOLLERR;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 692) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 693)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 694) return mask;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 695) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 696)
b0d8d2292160b (Linus Torvalds 2013-12-02 09:44:51 -0800 697) static void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe)
b0d8d2292160b (Linus Torvalds 2013-12-02 09:44:51 -0800 698) {
b0d8d2292160b (Linus Torvalds 2013-12-02 09:44:51 -0800 699) int kill = 0;
b0d8d2292160b (Linus Torvalds 2013-12-02 09:44:51 -0800 700)
b0d8d2292160b (Linus Torvalds 2013-12-02 09:44:51 -0800 701) spin_lock(&inode->i_lock);
b0d8d2292160b (Linus Torvalds 2013-12-02 09:44:51 -0800 702) if (!--pipe->files) {
b0d8d2292160b (Linus Torvalds 2013-12-02 09:44:51 -0800 703) inode->i_pipe = NULL;
b0d8d2292160b (Linus Torvalds 2013-12-02 09:44:51 -0800 704) kill = 1;
b0d8d2292160b (Linus Torvalds 2013-12-02 09:44:51 -0800 705) }
b0d8d2292160b (Linus Torvalds 2013-12-02 09:44:51 -0800 706) spin_unlock(&inode->i_lock);
b0d8d2292160b (Linus Torvalds 2013-12-02 09:44:51 -0800 707)
b0d8d2292160b (Linus Torvalds 2013-12-02 09:44:51 -0800 708) if (kill)
b0d8d2292160b (Linus Torvalds 2013-12-02 09:44:51 -0800 709) free_pipe_info(pipe);
b0d8d2292160b (Linus Torvalds 2013-12-02 09:44:51 -0800 710) }
b0d8d2292160b (Linus Torvalds 2013-12-02 09:44:51 -0800 711)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 712) static int
599a0ac14e065 (Al Viro 2013-03-12 09:58:10 -0400 713) pipe_release(struct inode *inode, struct file *file)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 714) {
b0d8d2292160b (Linus Torvalds 2013-12-02 09:44:51 -0800 715) struct pipe_inode_info *pipe = file->private_data;
923f4f23940d2 (Ingo Molnar 2006-04-11 13:53:33 +0200 716)
ebec73f4752b7 (Al Viro 2013-03-21 12:24:01 -0400 717) __pipe_lock(pipe);
599a0ac14e065 (Al Viro 2013-03-12 09:58:10 -0400 718) if (file->f_mode & FMODE_READ)
599a0ac14e065 (Al Viro 2013-03-12 09:58:10 -0400 719) pipe->readers--;
599a0ac14e065 (Al Viro 2013-03-12 09:58:10 -0400 720) if (file->f_mode & FMODE_WRITE)
599a0ac14e065 (Al Viro 2013-03-12 09:58:10 -0400 721) pipe->writers--;
341b446bc5aa3 (Ingo Molnar 2006-04-11 13:57:45 +0200 722)
6551d5c56eb0d (Linus Torvalds 2020-02-18 10:12:58 -0800 723) /* Was that the last reader or writer, but not the other side? */
6551d5c56eb0d (Linus Torvalds 2020-02-18 10:12:58 -0800 724) if (!pipe->readers != !pipe->writers) {
6551d5c56eb0d (Linus Torvalds 2020-02-18 10:12:58 -0800 725) wake_up_interruptible_all(&pipe->rd_wait);
6551d5c56eb0d (Linus Torvalds 2020-02-18 10:12:58 -0800 726) wake_up_interruptible_all(&pipe->wr_wait);
923f4f23940d2 (Ingo Molnar 2006-04-11 13:53:33 +0200 727) kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
923f4f23940d2 (Ingo Molnar 2006-04-11 13:53:33 +0200 728) kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 729) }
ebec73f4752b7 (Al Viro 2013-03-21 12:24:01 -0400 730) __pipe_unlock(pipe);
ba5bb147330a8 (Al Viro 2013-03-21 02:21:19 -0400 731)
b0d8d2292160b (Linus Torvalds 2013-12-02 09:44:51 -0800 732) put_pipe_info(inode, pipe);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 733) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 734) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 735)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 736) static int
599a0ac14e065 (Al Viro 2013-03-12 09:58:10 -0400 737) pipe_fasync(int fd, struct file *filp, int on)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 738) {
de32ec4cfeb3b (Al Viro 2013-03-21 11:16:56 -0400 739) struct pipe_inode_info *pipe = filp->private_data;
599a0ac14e065 (Al Viro 2013-03-12 09:58:10 -0400 740) int retval = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 741)
ebec73f4752b7 (Al Viro 2013-03-21 12:24:01 -0400 742) __pipe_lock(pipe);
599a0ac14e065 (Al Viro 2013-03-12 09:58:10 -0400 743) if (filp->f_mode & FMODE_READ)
599a0ac14e065 (Al Viro 2013-03-12 09:58:10 -0400 744) retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
599a0ac14e065 (Al Viro 2013-03-12 09:58:10 -0400 745) if ((filp->f_mode & FMODE_WRITE) && retval >= 0) {
341b446bc5aa3 (Ingo Molnar 2006-04-11 13:57:45 +0200 746) retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
599a0ac14e065 (Al Viro 2013-03-12 09:58:10 -0400 747) if (retval < 0 && (filp->f_mode & FMODE_READ))
599a0ac14e065 (Al Viro 2013-03-12 09:58:10 -0400 748) /* this can happen only if on == T */
e5bc49ba7439b (Oleg Nesterov 2009-03-12 14:31:28 -0700 749) fasync_helper(-1, filp, 0, &pipe->fasync_readers);
e5bc49ba7439b (Oleg Nesterov 2009-03-12 14:31:28 -0700 750) }
ebec73f4752b7 (Al Viro 2013-03-21 12:24:01 -0400 751) __pipe_unlock(pipe);
60aa49243d09a (Jonathan Corbet 2009-02-01 14:52:56 -0700 752) return retval;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 753) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 754)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 755) unsigned long account_pipe_buffers(struct user_struct *user,
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 756) unsigned long old, unsigned long new)
759c01142a5d0 (Willy Tarreau 2016-01-18 16:36:09 +0100 757) {
9c87bcf0a31b3 (Michael Kerrisk (man-pages) 2016-10-11 13:53:40 -0700 758) return atomic_long_add_return(new - old, &user->pipe_bufs);
759c01142a5d0 (Willy Tarreau 2016-01-18 16:36:09 +0100 759) }
759c01142a5d0 (Willy Tarreau 2016-01-18 16:36:09 +0100 760)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 761) bool too_many_pipe_buffers_soft(unsigned long user_bufs)
759c01142a5d0 (Willy Tarreau 2016-01-18 16:36:09 +0100 762) {
f7340761812fc (Eric Biggers 2018-02-06 15:42:08 -0800 763) unsigned long soft_limit = READ_ONCE(pipe_user_pages_soft);
f7340761812fc (Eric Biggers 2018-02-06 15:42:08 -0800 764)
f7340761812fc (Eric Biggers 2018-02-06 15:42:08 -0800 765) return soft_limit && user_bufs > soft_limit;
759c01142a5d0 (Willy Tarreau 2016-01-18 16:36:09 +0100 766) }
759c01142a5d0 (Willy Tarreau 2016-01-18 16:36:09 +0100 767)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 768) bool too_many_pipe_buffers_hard(unsigned long user_bufs)
759c01142a5d0 (Willy Tarreau 2016-01-18 16:36:09 +0100 769) {
f7340761812fc (Eric Biggers 2018-02-06 15:42:08 -0800 770) unsigned long hard_limit = READ_ONCE(pipe_user_pages_hard);
f7340761812fc (Eric Biggers 2018-02-06 15:42:08 -0800 771)
f7340761812fc (Eric Biggers 2018-02-06 15:42:08 -0800 772) return hard_limit && user_bufs > hard_limit;
759c01142a5d0 (Willy Tarreau 2016-01-18 16:36:09 +0100 773) }
759c01142a5d0 (Willy Tarreau 2016-01-18 16:36:09 +0100 774)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 775) bool pipe_is_unprivileged_user(void)
85c2dd5473b27 (Eric Biggers 2018-02-06 15:41:53 -0800 776) {
85c2dd5473b27 (Eric Biggers 2018-02-06 15:41:53 -0800 777) return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
85c2dd5473b27 (Eric Biggers 2018-02-06 15:41:53 -0800 778) }
85c2dd5473b27 (Eric Biggers 2018-02-06 15:41:53 -0800 779)
7bee130e222df (Al Viro 2013-03-21 11:04:15 -0400 780) struct pipe_inode_info *alloc_pipe_info(void)
3a326a2ce88e7 (Ingo Molnar 2006-04-10 15:18:35 +0200 781) {
923f4f23940d2 (Ingo Molnar 2006-04-11 13:53:33 +0200 782) struct pipe_inode_info *pipe;
09b4d1990094d (Michael Kerrisk (man-pages) 2016-10-11 13:53:34 -0700 783) unsigned long pipe_bufs = PIPE_DEF_BUFFERS;
09b4d1990094d (Michael Kerrisk (man-pages) 2016-10-11 13:53:34 -0700 784) struct user_struct *user = get_current_user();
9c87bcf0a31b3 (Michael Kerrisk (man-pages) 2016-10-11 13:53:40 -0700 785) unsigned long user_bufs;
f7340761812fc (Eric Biggers 2018-02-06 15:42:08 -0800 786) unsigned int max_size = READ_ONCE(pipe_max_size);
3a326a2ce88e7 (Ingo Molnar 2006-04-10 15:18:35 +0200 787)
d86133bd396f5 (Vladimir Davydov 2016-07-26 15:24:33 -0700 788) pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL_ACCOUNT);
09b4d1990094d (Michael Kerrisk (man-pages) 2016-10-11 13:53:34 -0700 789) if (pipe == NULL)
09b4d1990094d (Michael Kerrisk (man-pages) 2016-10-11 13:53:34 -0700 790) goto out_free_uid;
09b4d1990094d (Michael Kerrisk (man-pages) 2016-10-11 13:53:34 -0700 791)
f7340761812fc (Eric Biggers 2018-02-06 15:42:08 -0800 792) if (pipe_bufs * PAGE_SIZE > max_size && !capable(CAP_SYS_RESOURCE))
f7340761812fc (Eric Biggers 2018-02-06 15:42:08 -0800 793) pipe_bufs = max_size >> PAGE_SHIFT;
086e774a57fba (Michael Kerrisk (man-pages) 2016-10-11 13:53:43 -0700 794)
9c87bcf0a31b3 (Michael Kerrisk (man-pages) 2016-10-11 13:53:40 -0700 795) user_bufs = account_pipe_buffers(user, 0, pipe_bufs);
a005ca0e6813e (Michael Kerrisk (man-pages) 2016-10-11 13:53:37 -0700 796)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 797) if (too_many_pipe_buffers_soft(user_bufs) && pipe_is_unprivileged_user()) {
e745e30339991 (Alex Xu (Hello71) 2021-08-05 10:40:47 -0400 798) user_bufs = account_pipe_buffers(user, pipe_bufs, PIPE_MIN_DEF_BUFFERS);
e745e30339991 (Alex Xu (Hello71) 2021-08-05 10:40:47 -0400 799) pipe_bufs = PIPE_MIN_DEF_BUFFERS;
09b4d1990094d (Michael Kerrisk (man-pages) 2016-10-11 13:53:34 -0700 800) }
759c01142a5d0 (Willy Tarreau 2016-01-18 16:36:09 +0100 801)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 802) if (too_many_pipe_buffers_hard(user_bufs) && pipe_is_unprivileged_user())
a005ca0e6813e (Michael Kerrisk (man-pages) 2016-10-11 13:53:37 -0700 803) goto out_revert_acct;
a005ca0e6813e (Michael Kerrisk (man-pages) 2016-10-11 13:53:37 -0700 804)
a005ca0e6813e (Michael Kerrisk (man-pages) 2016-10-11 13:53:37 -0700 805) pipe->bufs = kcalloc(pipe_bufs, sizeof(struct pipe_buffer),
a005ca0e6813e (Michael Kerrisk (man-pages) 2016-10-11 13:53:37 -0700 806) GFP_KERNEL_ACCOUNT);
a005ca0e6813e (Michael Kerrisk (man-pages) 2016-10-11 13:53:37 -0700 807)
09b4d1990094d (Michael Kerrisk (man-pages) 2016-10-11 13:53:34 -0700 808) if (pipe->bufs) {
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 809) init_waitqueue_head(&pipe->rd_wait);
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 810) init_waitqueue_head(&pipe->wr_wait);
09b4d1990094d (Michael Kerrisk (man-pages) 2016-10-11 13:53:34 -0700 811) pipe->r_counter = pipe->w_counter = 1;
6718b6f855a0b (David Howells 2019-10-16 16:47:32 +0100 812) pipe->max_usage = pipe_bufs;
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 813) pipe->ring_size = pipe_bufs;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 814) pipe->nr_accounted = pipe_bufs;
09b4d1990094d (Michael Kerrisk (man-pages) 2016-10-11 13:53:34 -0700 815) pipe->user = user;
09b4d1990094d (Michael Kerrisk (man-pages) 2016-10-11 13:53:34 -0700 816) mutex_init(&pipe->mutex);
09b4d1990094d (Michael Kerrisk (man-pages) 2016-10-11 13:53:34 -0700 817) return pipe;
3a326a2ce88e7 (Ingo Molnar 2006-04-10 15:18:35 +0200 818) }
3a326a2ce88e7 (Ingo Molnar 2006-04-10 15:18:35 +0200 819)
a005ca0e6813e (Michael Kerrisk (man-pages) 2016-10-11 13:53:37 -0700 820) out_revert_acct:
9c87bcf0a31b3 (Michael Kerrisk (man-pages) 2016-10-11 13:53:40 -0700 821) (void) account_pipe_buffers(user, pipe_bufs, 0);
09b4d1990094d (Michael Kerrisk (man-pages) 2016-10-11 13:53:34 -0700 822) kfree(pipe);
09b4d1990094d (Michael Kerrisk (man-pages) 2016-10-11 13:53:34 -0700 823) out_free_uid:
09b4d1990094d (Michael Kerrisk (man-pages) 2016-10-11 13:53:34 -0700 824) free_uid(user);
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 825) return NULL;
3a326a2ce88e7 (Ingo Molnar 2006-04-10 15:18:35 +0200 826) }
3a326a2ce88e7 (Ingo Molnar 2006-04-10 15:18:35 +0200 827)
4b8a8f1e4f94f (Al Viro 2013-03-21 11:06:46 -0400 828) void free_pipe_info(struct pipe_inode_info *pipe)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 829) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 830) int i;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 831)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 832) #ifdef CONFIG_WATCH_QUEUE
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 833) if (pipe->watch_queue) {
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 834) watch_queue_clear(pipe->watch_queue);
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 835) put_watch_queue(pipe->watch_queue);
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 836) }
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 837) #endif
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 838)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 839) (void) account_pipe_buffers(pipe->user, pipe->nr_accounted, 0);
759c01142a5d0 (Willy Tarreau 2016-01-18 16:36:09 +0100 840) free_uid(pipe->user);
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 841) for (i = 0; i < pipe->ring_size; i++) {
923f4f23940d2 (Ingo Molnar 2006-04-11 13:53:33 +0200 842) struct pipe_buffer *buf = pipe->bufs + i;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 843) if (buf->ops)
a779638cf622f (Miklos Szeredi 2016-09-27 10:45:12 +0200 844) pipe_buf_release(pipe, buf);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 845) }
923f4f23940d2 (Ingo Molnar 2006-04-11 13:53:33 +0200 846) if (pipe->tmp_page)
923f4f23940d2 (Ingo Molnar 2006-04-11 13:53:33 +0200 847) __free_page(pipe->tmp_page);
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 848) kfree(pipe->bufs);
923f4f23940d2 (Ingo Molnar 2006-04-11 13:53:33 +0200 849) kfree(pipe);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 850) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 851)
fa3536cc144c1 (Eric Dumazet 2006-03-26 01:37:24 -0800 852) static struct vfsmount *pipe_mnt __read_mostly;
341b446bc5aa3 (Ingo Molnar 2006-04-11 13:57:45 +0200 853)
c23fbb6bcb3eb (Eric Dumazet 2007-05-08 00:26:18 -0700 854) /*
c23fbb6bcb3eb (Eric Dumazet 2007-05-08 00:26:18 -0700 855) * pipefs_dname() is called from d_path().
c23fbb6bcb3eb (Eric Dumazet 2007-05-08 00:26:18 -0700 856) */
c23fbb6bcb3eb (Eric Dumazet 2007-05-08 00:26:18 -0700 857) static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
c23fbb6bcb3eb (Eric Dumazet 2007-05-08 00:26:18 -0700 858) {
c23fbb6bcb3eb (Eric Dumazet 2007-05-08 00:26:18 -0700 859) return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
75c3cfa855dce (David Howells 2015-03-17 22:26:12 +0000 860) d_inode(dentry)->i_ino);
c23fbb6bcb3eb (Eric Dumazet 2007-05-08 00:26:18 -0700 861) }
c23fbb6bcb3eb (Eric Dumazet 2007-05-08 00:26:18 -0700 862)
3ba13d179e8c2 (Al Viro 2009-02-20 06:02:22 +0000 863) static const struct dentry_operations pipefs_dentry_operations = {
c23fbb6bcb3eb (Eric Dumazet 2007-05-08 00:26:18 -0700 864) .d_dname = pipefs_dname,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 865) };
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 866)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 867) static struct inode * get_pipe_inode(void)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 868) {
a209dfc7b0d94 (Eric Dumazet 2011-07-26 11:36:34 +0200 869) struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
923f4f23940d2 (Ingo Molnar 2006-04-11 13:53:33 +0200 870) struct pipe_inode_info *pipe;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 871)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 872) if (!inode)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 873) goto fail_inode;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 874)
85fe4025c616a (Christoph Hellwig 2010-10-23 11:19:54 -0400 875) inode->i_ino = get_next_ino();
85fe4025c616a (Christoph Hellwig 2010-10-23 11:19:54 -0400 876)
7bee130e222df (Al Viro 2013-03-21 11:04:15 -0400 877) pipe = alloc_pipe_info();
923f4f23940d2 (Ingo Molnar 2006-04-11 13:53:33 +0200 878) if (!pipe)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 879) goto fail_iput;
3a326a2ce88e7 (Ingo Molnar 2006-04-10 15:18:35 +0200 880)
ba5bb147330a8 (Al Viro 2013-03-21 02:21:19 -0400 881) inode->i_pipe = pipe;
ba5bb147330a8 (Al Viro 2013-03-21 02:21:19 -0400 882) pipe->files = 2;
923f4f23940d2 (Ingo Molnar 2006-04-11 13:53:33 +0200 883) pipe->readers = pipe->writers = 1;
599a0ac14e065 (Al Viro 2013-03-12 09:58:10 -0400 884) inode->i_fop = &pipefifo_fops;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 885)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 886) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 887) * Mark the inode dirty from the very beginning,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 888) * that way it will never be moved to the dirty
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 889) * list because "mark_inode_dirty()" will think
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 890) * that it already _is_ on the dirty list.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 891) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 892) inode->i_state = I_DIRTY;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 893) inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
da9592edebcee (David Howells 2008-11-14 10:39:05 +1100 894) inode->i_uid = current_fsuid();
da9592edebcee (David Howells 2008-11-14 10:39:05 +1100 895) inode->i_gid = current_fsgid();
078cd8279e659 (Deepa Dinamani 2016-09-14 07:48:04 -0700 896) inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
923f4f23940d2 (Ingo Molnar 2006-04-11 13:53:33 +0200 897)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 898) return inode;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 899)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 900) fail_iput:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 901) iput(inode);
341b446bc5aa3 (Ingo Molnar 2006-04-11 13:57:45 +0200 902)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 903) fail_inode:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 904) return NULL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 905) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 906)
e4fad8e5d220e (Al Viro 2012-07-21 15:33:25 +0400 907) int create_pipe_files(struct file **res, int flags)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 908) {
e4fad8e5d220e (Al Viro 2012-07-21 15:33:25 +0400 909) struct inode *inode = get_pipe_inode();
d6cbd281d1899 (Andi Kleen 2006-09-30 23:29:26 -0700 910) struct file *f;
8a018eb55e3ac (Qian Cai 2020-10-01 08:50:55 -0400 911) int error;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 912)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 913) if (!inode)
e4fad8e5d220e (Al Viro 2012-07-21 15:33:25 +0400 914) return -ENFILE;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 915)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 916) if (flags & O_NOTIFICATION_PIPE) {
8a018eb55e3ac (Qian Cai 2020-10-01 08:50:55 -0400 917) error = watch_queue_init(inode->i_pipe);
8a018eb55e3ac (Qian Cai 2020-10-01 08:50:55 -0400 918) if (error) {
8a018eb55e3ac (Qian Cai 2020-10-01 08:50:55 -0400 919) free_pipe_info(inode->i_pipe);
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 920) iput(inode);
8a018eb55e3ac (Qian Cai 2020-10-01 08:50:55 -0400 921) return error;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 922) }
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 923) }
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 924)
152b6372c9063 (Al Viro 2018-06-09 10:05:18 -0400 925) f = alloc_file_pseudo(inode, pipe_mnt, "",
152b6372c9063 (Al Viro 2018-06-09 10:05:18 -0400 926) O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT)),
152b6372c9063 (Al Viro 2018-06-09 10:05:18 -0400 927) &pipefifo_fops);
e9bb1f9b12e33 (Eric Biggers 2015-10-17 16:26:08 -0500 928) if (IS_ERR(f)) {
152b6372c9063 (Al Viro 2018-06-09 10:05:18 -0400 929) free_pipe_info(inode->i_pipe);
152b6372c9063 (Al Viro 2018-06-09 10:05:18 -0400 930) iput(inode);
152b6372c9063 (Al Viro 2018-06-09 10:05:18 -0400 931) return PTR_ERR(f);
e9bb1f9b12e33 (Eric Biggers 2015-10-17 16:26:08 -0500 932) }
341b446bc5aa3 (Ingo Molnar 2006-04-11 13:57:45 +0200 933)
de32ec4cfeb3b (Al Viro 2013-03-21 11:16:56 -0400 934) f->private_data = inode->i_pipe;
d6cbd281d1899 (Andi Kleen 2006-09-30 23:29:26 -0700 935)
183266f26f45a (Al Viro 2018-06-17 14:15:10 -0400 936) res[0] = alloc_file_clone(f, O_RDONLY | (flags & O_NONBLOCK),
183266f26f45a (Al Viro 2018-06-17 14:15:10 -0400 937) &pipefifo_fops);
e9bb1f9b12e33 (Eric Biggers 2015-10-17 16:26:08 -0500 938) if (IS_ERR(res[0])) {
b10a4a9f76953 (Al Viro 2018-07-09 02:29:58 -0400 939) put_pipe_info(inode, inode->i_pipe);
b10a4a9f76953 (Al Viro 2018-07-09 02:29:58 -0400 940) fput(f);
b10a4a9f76953 (Al Viro 2018-07-09 02:29:58 -0400 941) return PTR_ERR(res[0]);
e9bb1f9b12e33 (Eric Biggers 2015-10-17 16:26:08 -0500 942) }
de32ec4cfeb3b (Al Viro 2013-03-21 11:16:56 -0400 943) res[0]->private_data = inode->i_pipe;
e4fad8e5d220e (Al Viro 2012-07-21 15:33:25 +0400 944) res[1] = f;
d8e464ecc17b4 (Linus Torvalds 2019-11-17 11:20:48 -0800 945) stream_open(inode, res[0]);
d8e464ecc17b4 (Linus Torvalds 2019-11-17 11:20:48 -0800 946) stream_open(inode, res[1]);
e4fad8e5d220e (Al Viro 2012-07-21 15:33:25 +0400 947) return 0;
d6cbd281d1899 (Andi Kleen 2006-09-30 23:29:26 -0700 948) }
d6cbd281d1899 (Andi Kleen 2006-09-30 23:29:26 -0700 949)
5b249b1b07c42 (Al Viro 2012-08-19 12:17:29 -0400 950) static int __do_pipe_flags(int *fd, struct file **files, int flags)
d6cbd281d1899 (Andi Kleen 2006-09-30 23:29:26 -0700 951) {
d6cbd281d1899 (Andi Kleen 2006-09-30 23:29:26 -0700 952) int error;
d6cbd281d1899 (Andi Kleen 2006-09-30 23:29:26 -0700 953) int fdw, fdr;
d6cbd281d1899 (Andi Kleen 2006-09-30 23:29:26 -0700 954)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 955) if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT | O_NOTIFICATION_PIPE))
ed8cae8ba0134 (Ulrich Drepper 2008-07-23 21:29:30 -0700 956) return -EINVAL;
ed8cae8ba0134 (Ulrich Drepper 2008-07-23 21:29:30 -0700 957)
e4fad8e5d220e (Al Viro 2012-07-21 15:33:25 +0400 958) error = create_pipe_files(files, flags);
e4fad8e5d220e (Al Viro 2012-07-21 15:33:25 +0400 959) if (error)
e4fad8e5d220e (Al Viro 2012-07-21 15:33:25 +0400 960) return error;
d6cbd281d1899 (Andi Kleen 2006-09-30 23:29:26 -0700 961)
ed8cae8ba0134 (Ulrich Drepper 2008-07-23 21:29:30 -0700 962) error = get_unused_fd_flags(flags);
d6cbd281d1899 (Andi Kleen 2006-09-30 23:29:26 -0700 963) if (error < 0)
d6cbd281d1899 (Andi Kleen 2006-09-30 23:29:26 -0700 964) goto err_read_pipe;
d6cbd281d1899 (Andi Kleen 2006-09-30 23:29:26 -0700 965) fdr = error;
d6cbd281d1899 (Andi Kleen 2006-09-30 23:29:26 -0700 966)
ed8cae8ba0134 (Ulrich Drepper 2008-07-23 21:29:30 -0700 967) error = get_unused_fd_flags(flags);
d6cbd281d1899 (Andi Kleen 2006-09-30 23:29:26 -0700 968) if (error < 0)
d6cbd281d1899 (Andi Kleen 2006-09-30 23:29:26 -0700 969) goto err_fdr;
d6cbd281d1899 (Andi Kleen 2006-09-30 23:29:26 -0700 970) fdw = error;
d6cbd281d1899 (Andi Kleen 2006-09-30 23:29:26 -0700 971)
157cf649a735a (Al Viro 2008-12-14 04:57:47 -0500 972) audit_fd_pair(fdr, fdw);
d6cbd281d1899 (Andi Kleen 2006-09-30 23:29:26 -0700 973) fd[0] = fdr;
d6cbd281d1899 (Andi Kleen 2006-09-30 23:29:26 -0700 974) fd[1] = fdw;
d6cbd281d1899 (Andi Kleen 2006-09-30 23:29:26 -0700 975) return 0;
d6cbd281d1899 (Andi Kleen 2006-09-30 23:29:26 -0700 976)
d6cbd281d1899 (Andi Kleen 2006-09-30 23:29:26 -0700 977) err_fdr:
d6cbd281d1899 (Andi Kleen 2006-09-30 23:29:26 -0700 978) put_unused_fd(fdr);
d6cbd281d1899 (Andi Kleen 2006-09-30 23:29:26 -0700 979) err_read_pipe:
e4fad8e5d220e (Al Viro 2012-07-21 15:33:25 +0400 980) fput(files[0]);
e4fad8e5d220e (Al Viro 2012-07-21 15:33:25 +0400 981) fput(files[1]);
d6cbd281d1899 (Andi Kleen 2006-09-30 23:29:26 -0700 982) return error;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 983) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 984)
5b249b1b07c42 (Al Viro 2012-08-19 12:17:29 -0400 985) int do_pipe_flags(int *fd, int flags)
5b249b1b07c42 (Al Viro 2012-08-19 12:17:29 -0400 986) {
5b249b1b07c42 (Al Viro 2012-08-19 12:17:29 -0400 987) struct file *files[2];
5b249b1b07c42 (Al Viro 2012-08-19 12:17:29 -0400 988) int error = __do_pipe_flags(fd, files, flags);
5b249b1b07c42 (Al Viro 2012-08-19 12:17:29 -0400 989) if (!error) {
5b249b1b07c42 (Al Viro 2012-08-19 12:17:29 -0400 990) fd_install(fd[0], files[0]);
5b249b1b07c42 (Al Viro 2012-08-19 12:17:29 -0400 991) fd_install(fd[1], files[1]);
5b249b1b07c42 (Al Viro 2012-08-19 12:17:29 -0400 992) }
5b249b1b07c42 (Al Viro 2012-08-19 12:17:29 -0400 993) return error;
5b249b1b07c42 (Al Viro 2012-08-19 12:17:29 -0400 994) }
5b249b1b07c42 (Al Viro 2012-08-19 12:17:29 -0400 995)
d35c7b0e54a59 (Ulrich Drepper 2008-05-03 15:10:37 -0400 996) /*
d35c7b0e54a59 (Ulrich Drepper 2008-05-03 15:10:37 -0400 997) * sys_pipe() is the normal C calling standard for creating
d35c7b0e54a59 (Ulrich Drepper 2008-05-03 15:10:37 -0400 998) * a pipe. It's not the way Unix traditionally does this, though.
d35c7b0e54a59 (Ulrich Drepper 2008-05-03 15:10:37 -0400 999) */
0a216dd1cfe8a (Dominik Brodowski 2018-03-11 11:34:28 +0100 1000) static int do_pipe2(int __user *fildes, int flags)
d35c7b0e54a59 (Ulrich Drepper 2008-05-03 15:10:37 -0400 1001) {
5b249b1b07c42 (Al Viro 2012-08-19 12:17:29 -0400 1002) struct file *files[2];
d35c7b0e54a59 (Ulrich Drepper 2008-05-03 15:10:37 -0400 1003) int fd[2];
d35c7b0e54a59 (Ulrich Drepper 2008-05-03 15:10:37 -0400 1004) int error;
d35c7b0e54a59 (Ulrich Drepper 2008-05-03 15:10:37 -0400 1005)
5b249b1b07c42 (Al Viro 2012-08-19 12:17:29 -0400 1006) error = __do_pipe_flags(fd, files, flags);
d35c7b0e54a59 (Ulrich Drepper 2008-05-03 15:10:37 -0400 1007) if (!error) {
5b249b1b07c42 (Al Viro 2012-08-19 12:17:29 -0400 1008) if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
5b249b1b07c42 (Al Viro 2012-08-19 12:17:29 -0400 1009) fput(files[0]);
5b249b1b07c42 (Al Viro 2012-08-19 12:17:29 -0400 1010) fput(files[1]);
5b249b1b07c42 (Al Viro 2012-08-19 12:17:29 -0400 1011) put_unused_fd(fd[0]);
5b249b1b07c42 (Al Viro 2012-08-19 12:17:29 -0400 1012) put_unused_fd(fd[1]);
d35c7b0e54a59 (Ulrich Drepper 2008-05-03 15:10:37 -0400 1013) error = -EFAULT;
5b249b1b07c42 (Al Viro 2012-08-19 12:17:29 -0400 1014) } else {
5b249b1b07c42 (Al Viro 2012-08-19 12:17:29 -0400 1015) fd_install(fd[0], files[0]);
5b249b1b07c42 (Al Viro 2012-08-19 12:17:29 -0400 1016) fd_install(fd[1], files[1]);
ba719baeabbff (Ulrich Drepper 2008-05-06 20:42:38 -0700 1017) }
d35c7b0e54a59 (Ulrich Drepper 2008-05-03 15:10:37 -0400 1018) }
d35c7b0e54a59 (Ulrich Drepper 2008-05-03 15:10:37 -0400 1019) return error;
d35c7b0e54a59 (Ulrich Drepper 2008-05-03 15:10:37 -0400 1020) }
d35c7b0e54a59 (Ulrich Drepper 2008-05-03 15:10:37 -0400 1021)
0a216dd1cfe8a (Dominik Brodowski 2018-03-11 11:34:28 +0100 1022) SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
0a216dd1cfe8a (Dominik Brodowski 2018-03-11 11:34:28 +0100 1023) {
0a216dd1cfe8a (Dominik Brodowski 2018-03-11 11:34:28 +0100 1024) return do_pipe2(fildes, flags);
0a216dd1cfe8a (Dominik Brodowski 2018-03-11 11:34:28 +0100 1025) }
0a216dd1cfe8a (Dominik Brodowski 2018-03-11 11:34:28 +0100 1026)
2b66421995d2e (Heiko Carstens 2009-01-14 14:14:35 +0100 1027) SYSCALL_DEFINE1(pipe, int __user *, fildes)
ed8cae8ba0134 (Ulrich Drepper 2008-07-23 21:29:30 -0700 1028) {
0a216dd1cfe8a (Dominik Brodowski 2018-03-11 11:34:28 +0100 1029) return do_pipe2(fildes, 0);
ed8cae8ba0134 (Ulrich Drepper 2008-07-23 21:29:30 -0700 1030) }
ed8cae8ba0134 (Ulrich Drepper 2008-07-23 21:29:30 -0700 1031)
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1032) /*
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1033) * This is the stupid "wait for pipe to be readable or writable"
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1034) * model.
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1035) *
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1036) * See pipe_read/write() for the proper kind of exclusive wait,
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1037) * but that requires that we wake up any other readers/writers
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1038) * if we then do not end up reading everything (ie the whole
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1039) * "wake_next_reader/writer" logic in pipe_read/write()).
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1040) */
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1041) void pipe_wait_readable(struct pipe_inode_info *pipe)
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1042) {
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1043) pipe_unlock(pipe);
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1044) wait_event_interruptible(pipe->rd_wait, pipe_readable(pipe));
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1045) pipe_lock(pipe);
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1046) }
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1047)
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1048) void pipe_wait_writable(struct pipe_inode_info *pipe)
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1049) {
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1050) pipe_unlock(pipe);
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1051) wait_event_interruptible(pipe->wr_wait, pipe_writable(pipe));
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1052) pipe_lock(pipe);
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1053) }
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1054)
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1055) /*
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1056) * This depends on both the wait (here) and the wakeup (wake_up_partner)
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1057) * holding the pipe lock, so "*cnt" is stable and we know a wakeup cannot
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1058) * race with the count check and waitqueue prep.
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1059) *
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1060) * Normally in order to avoid races, you'd do the prepare_to_wait() first,
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1061) * then check the condition you're waiting for, and only then sleep. But
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1062) * because of the pipe lock, we can check the condition before being on
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1063) * the wait queue.
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1064) *
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1065) * We use the 'rd_wait' waitqueue for pipe partner waiting.
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1066) */
fc7478a2bfa9a (Al Viro 2013-03-21 02:07:59 -0400 1067) static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1068) {
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1069) DEFINE_WAIT(rdwait);
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1070) int cur = *cnt;
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1071)
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1072) while (cur == *cnt) {
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1073) prepare_to_wait(&pipe->rd_wait, &rdwait, TASK_INTERRUPTIBLE);
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1074) pipe_unlock(pipe);
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1075) schedule();
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1076) finish_wait(&pipe->rd_wait, &rdwait);
472e5b056f000 (Linus Torvalds 2020-10-01 19:14:36 -0700 1077) pipe_lock(pipe);
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1078) if (signal_pending(current))
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1079) break;
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1080) }
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1081) return cur == *cnt ? -ERESTARTSYS : 0;
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1082) }
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1083)
fc7478a2bfa9a (Al Viro 2013-03-21 02:07:59 -0400 1084) static void wake_up_partner(struct pipe_inode_info *pipe)
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1085) {
6551d5c56eb0d (Linus Torvalds 2020-02-18 10:12:58 -0800 1086) wake_up_interruptible_all(&pipe->rd_wait);
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1087) }
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1088)
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1089) static int fifo_open(struct inode *inode, struct file *filp)
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1090) {
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1091) struct pipe_inode_info *pipe;
599a0ac14e065 (Al Viro 2013-03-12 09:58:10 -0400 1092) bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1093) int ret;
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1094)
ba5bb147330a8 (Al Viro 2013-03-21 02:21:19 -0400 1095) filp->f_version = 0;
ba5bb147330a8 (Al Viro 2013-03-21 02:21:19 -0400 1096)
ba5bb147330a8 (Al Viro 2013-03-21 02:21:19 -0400 1097) spin_lock(&inode->i_lock);
ba5bb147330a8 (Al Viro 2013-03-21 02:21:19 -0400 1098) if (inode->i_pipe) {
ba5bb147330a8 (Al Viro 2013-03-21 02:21:19 -0400 1099) pipe = inode->i_pipe;
ba5bb147330a8 (Al Viro 2013-03-21 02:21:19 -0400 1100) pipe->files++;
ba5bb147330a8 (Al Viro 2013-03-21 02:21:19 -0400 1101) spin_unlock(&inode->i_lock);
ba5bb147330a8 (Al Viro 2013-03-21 02:21:19 -0400 1102) } else {
ba5bb147330a8 (Al Viro 2013-03-21 02:21:19 -0400 1103) spin_unlock(&inode->i_lock);
7bee130e222df (Al Viro 2013-03-21 11:04:15 -0400 1104) pipe = alloc_pipe_info();
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1105) if (!pipe)
ba5bb147330a8 (Al Viro 2013-03-21 02:21:19 -0400 1106) return -ENOMEM;
ba5bb147330a8 (Al Viro 2013-03-21 02:21:19 -0400 1107) pipe->files = 1;
ba5bb147330a8 (Al Viro 2013-03-21 02:21:19 -0400 1108) spin_lock(&inode->i_lock);
ba5bb147330a8 (Al Viro 2013-03-21 02:21:19 -0400 1109) if (unlikely(inode->i_pipe)) {
ba5bb147330a8 (Al Viro 2013-03-21 02:21:19 -0400 1110) inode->i_pipe->files++;
ba5bb147330a8 (Al Viro 2013-03-21 02:21:19 -0400 1111) spin_unlock(&inode->i_lock);
4b8a8f1e4f94f (Al Viro 2013-03-21 11:06:46 -0400 1112) free_pipe_info(pipe);
ba5bb147330a8 (Al Viro 2013-03-21 02:21:19 -0400 1113) pipe = inode->i_pipe;
ba5bb147330a8 (Al Viro 2013-03-21 02:21:19 -0400 1114) } else {
ba5bb147330a8 (Al Viro 2013-03-21 02:21:19 -0400 1115) inode->i_pipe = pipe;
ba5bb147330a8 (Al Viro 2013-03-21 02:21:19 -0400 1116) spin_unlock(&inode->i_lock);
ba5bb147330a8 (Al Viro 2013-03-21 02:21:19 -0400 1117) }
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1118) }
de32ec4cfeb3b (Al Viro 2013-03-21 11:16:56 -0400 1119) filp->private_data = pipe;
ba5bb147330a8 (Al Viro 2013-03-21 02:21:19 -0400 1120) /* OK, we have a pipe and it's pinned down */
ba5bb147330a8 (Al Viro 2013-03-21 02:21:19 -0400 1121)
ebec73f4752b7 (Al Viro 2013-03-21 12:24:01 -0400 1122) __pipe_lock(pipe);
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1123)
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1124) /* We can only do regular read/write on fifos */
d8e464ecc17b4 (Linus Torvalds 2019-11-17 11:20:48 -0800 1125) stream_open(inode, filp);
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1126)
d8e464ecc17b4 (Linus Torvalds 2019-11-17 11:20:48 -0800 1127) switch (filp->f_mode & (FMODE_READ | FMODE_WRITE)) {
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1128) case FMODE_READ:
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1129) /*
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1130) * O_RDONLY
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1131) * POSIX.1 says that O_NONBLOCK means return with the FIFO
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1132) * opened, even when there is no process writing the FIFO.
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1133) */
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1134) pipe->r_counter++;
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1135) if (pipe->readers++ == 0)
fc7478a2bfa9a (Al Viro 2013-03-21 02:07:59 -0400 1136) wake_up_partner(pipe);
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1137)
599a0ac14e065 (Al Viro 2013-03-12 09:58:10 -0400 1138) if (!is_pipe && !pipe->writers) {
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1139) if ((filp->f_flags & O_NONBLOCK)) {
a9a08845e9acb (Linus Torvalds 2018-02-11 14:34:03 -0800 1140) /* suppress EPOLLHUP until we have
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1141) * seen a writer */
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1142) filp->f_version = pipe->w_counter;
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1143) } else {
fc7478a2bfa9a (Al Viro 2013-03-21 02:07:59 -0400 1144) if (wait_for_partner(pipe, &pipe->w_counter))
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1145) goto err_rd;
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1146) }
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1147) }
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1148) break;
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1149)
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1150) case FMODE_WRITE:
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1151) /*
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1152) * O_WRONLY
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1153) * POSIX.1 says that O_NONBLOCK means return -1 with
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1154) * errno=ENXIO when there is no process reading the FIFO.
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1155) */
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1156) ret = -ENXIO;
599a0ac14e065 (Al Viro 2013-03-12 09:58:10 -0400 1157) if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1158) goto err;
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1159)
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1160) pipe->w_counter++;
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1161) if (!pipe->writers++)
fc7478a2bfa9a (Al Viro 2013-03-21 02:07:59 -0400 1162) wake_up_partner(pipe);
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1163)
599a0ac14e065 (Al Viro 2013-03-12 09:58:10 -0400 1164) if (!is_pipe && !pipe->readers) {
fc7478a2bfa9a (Al Viro 2013-03-21 02:07:59 -0400 1165) if (wait_for_partner(pipe, &pipe->r_counter))
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1166) goto err_wr;
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1167) }
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1168) break;
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1169)
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1170) case FMODE_READ | FMODE_WRITE:
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1171) /*
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1172) * O_RDWR
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1173) * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1174) * This implementation will NEVER block on a O_RDWR open, since
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1175) * the process can at least talk to itself.
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1176) */
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1177)
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1178) pipe->readers++;
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1179) pipe->writers++;
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1180) pipe->r_counter++;
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1181) pipe->w_counter++;
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1182) if (pipe->readers == 1 || pipe->writers == 1)
fc7478a2bfa9a (Al Viro 2013-03-21 02:07:59 -0400 1183) wake_up_partner(pipe);
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1184) break;
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1185)
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1186) default:
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1187) ret = -EINVAL;
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1188) goto err;
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1189) }
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1190)
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1191) /* Ok! */
ebec73f4752b7 (Al Viro 2013-03-21 12:24:01 -0400 1192) __pipe_unlock(pipe);
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1193) return 0;
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1194)
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1195) err_rd:
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1196) if (!--pipe->readers)
0ddad21d3e99c (Linus Torvalds 2019-12-09 09:48:27 -0800 1197) wake_up_interruptible(&pipe->wr_wait);
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1198) ret = -ERESTARTSYS;
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1199) goto err;
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1200)
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1201) err_wr:
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1202) if (!--pipe->writers)
6551d5c56eb0d (Linus Torvalds 2020-02-18 10:12:58 -0800 1203) wake_up_interruptible_all(&pipe->rd_wait);
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1204) ret = -ERESTARTSYS;
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1205) goto err;
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1206)
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1207) err:
ebec73f4752b7 (Al Viro 2013-03-21 12:24:01 -0400 1208) __pipe_unlock(pipe);
b0d8d2292160b (Linus Torvalds 2013-12-02 09:44:51 -0800 1209)
b0d8d2292160b (Linus Torvalds 2013-12-02 09:44:51 -0800 1210) put_pipe_info(inode, pipe);
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1211) return ret;
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1212) }
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1213)
599a0ac14e065 (Al Viro 2013-03-12 09:58:10 -0400 1214) const struct file_operations pipefifo_fops = {
599a0ac14e065 (Al Viro 2013-03-12 09:58:10 -0400 1215) .open = fifo_open,
599a0ac14e065 (Al Viro 2013-03-12 09:58:10 -0400 1216) .llseek = no_llseek,
fb9096a344e29 (Al Viro 2014-04-02 19:56:54 -0400 1217) .read_iter = pipe_read,
f0d1bec9d58d4 (Al Viro 2014-04-03 15:05:18 -0400 1218) .write_iter = pipe_write,
a11e1d432b51f (Linus Torvalds 2018-06-28 09:43:44 -0700 1219) .poll = pipe_poll,
599a0ac14e065 (Al Viro 2013-03-12 09:58:10 -0400 1220) .unlocked_ioctl = pipe_ioctl,
599a0ac14e065 (Al Viro 2013-03-12 09:58:10 -0400 1221) .release = pipe_release,
599a0ac14e065 (Al Viro 2013-03-12 09:58:10 -0400 1222) .fasync = pipe_fasync,
f8ad8187c3b53 (Johannes Berg 2021-01-25 10:16:15 +0100 1223) .splice_write = iter_file_splice_write,
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1224) };
f776c738883bc (Al Viro 2013-03-12 09:46:27 -0400 1225)
f491bd71118be (Michael Kerrisk (man-pages) 2016-10-11 13:53:22 -0700 1226) /*
f491bd71118be (Michael Kerrisk (man-pages) 2016-10-11 13:53:22 -0700 1227) * Currently we rely on the pipe array holding a power-of-2 number
d3f14c485867c (Joe Lawrence 2017-11-17 15:29:21 -0800 1228) * of pages. Returns 0 on error.
f491bd71118be (Michael Kerrisk (man-pages) 2016-10-11 13:53:22 -0700 1229) */
96e99be40e4cf (Eric Biggers 2018-02-06 15:42:00 -0800 1230) unsigned int round_pipe_size(unsigned long size)
f491bd71118be (Michael Kerrisk (man-pages) 2016-10-11 13:53:22 -0700 1231) {
c4fed5a91fadc (Eric Biggers 2018-02-06 15:42:05 -0800 1232) if (size > (1U << 31))
96e99be40e4cf (Eric Biggers 2018-02-06 15:42:00 -0800 1233) return 0;
96e99be40e4cf (Eric Biggers 2018-02-06 15:42:00 -0800 1234)
4c2e4befb3cc9 (Eric Biggers 2018-02-06 15:41:45 -0800 1235) /* Minimum pipe size, as required by POSIX */
4c2e4befb3cc9 (Eric Biggers 2018-02-06 15:41:45 -0800 1236) if (size < PAGE_SIZE)
c4fed5a91fadc (Eric Biggers 2018-02-06 15:42:05 -0800 1237) return PAGE_SIZE;
d3f14c485867c (Joe Lawrence 2017-11-17 15:29:21 -0800 1238)
c4fed5a91fadc (Eric Biggers 2018-02-06 15:42:05 -0800 1239) return roundup_pow_of_two(size);
f491bd71118be (Michael Kerrisk (man-pages) 2016-10-11 13:53:22 -0700 1240) }
f491bd71118be (Michael Kerrisk (man-pages) 2016-10-11 13:53:22 -0700 1241)
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1242) /*
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1243) * Resize the pipe ring to a number of slots.
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1244) */
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1245) int pipe_resize_ring(struct pipe_inode_info *pipe, unsigned int nr_slots)
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1246) {
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1247) struct pipe_buffer *bufs;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1248) unsigned int head, tail, mask, n;
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1249)
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1250) /*
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1251) * We can shrink the pipe, if arg is greater than the ring occupancy.
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1252) * Since we don't expect a lot of shrink+grow operations, just free and
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1253) * allocate again like we would do for growing. If the pipe currently
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1254) * contains more buffers than arg, then return busy.
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1255) */
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1256) mask = pipe->ring_size - 1;
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1257) head = pipe->head;
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1258) tail = pipe->tail;
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1259) n = pipe_occupancy(pipe->head, pipe->tail);
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1260) if (nr_slots < n)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1261) return -EBUSY;
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1262)
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1263) bufs = kcalloc(nr_slots, sizeof(*bufs),
d86133bd396f5 (Vladimir Davydov 2016-07-26 15:24:33 -0700 1264) GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1265) if (unlikely(!bufs))
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1266) return -ENOMEM;
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1267)
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1268) /*
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1269) * The pipe array wraps around, so just start the new one at zero
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1270) * and adjust the indices.
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1271) */
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1272) if (n > 0) {
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1273) unsigned int h = head & mask;
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1274) unsigned int t = tail & mask;
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1275) if (h > t) {
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1276) memcpy(bufs, pipe->bufs + t,
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1277) n * sizeof(struct pipe_buffer));
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1278) } else {
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1279) unsigned int tsize = pipe->ring_size - t;
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1280) if (h > 0)
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1281) memcpy(bufs + tsize, pipe->bufs,
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1282) h * sizeof(struct pipe_buffer));
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1283) memcpy(bufs, pipe->bufs + t,
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1284) tsize * sizeof(struct pipe_buffer));
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1285) }
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1286) }
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1287)
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1288) head = n;
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1289) tail = 0;
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1290)
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1291) kfree(pipe->bufs);
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1292) pipe->bufs = bufs;
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1293) pipe->ring_size = nr_slots;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1294) if (pipe->max_usage > nr_slots)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1295) pipe->max_usage = nr_slots;
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1296) pipe->tail = tail;
8cefc107ca54c (David Howells 2019-11-15 13:30:32 +0000 1297) pipe->head = head;
6551d5c56eb0d (Linus Torvalds 2020-02-18 10:12:58 -0800 1298)
6551d5c56eb0d (Linus Torvalds 2020-02-18 10:12:58 -0800 1299) /* This might have made more room for writers */
6551d5c56eb0d (Linus Torvalds 2020-02-18 10:12:58 -0800 1300) wake_up_interruptible(&pipe->wr_wait);
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1301) return 0;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1302) }
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1303)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1304) /*
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1305) * Allocate a new array of pipe buffers and copy the info over. Returns the
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1306) * pipe size if successful, or return -ERROR on error.
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1307) */
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1308) static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long arg)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1309) {
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1310) unsigned long user_bufs;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1311) unsigned int nr_slots, size;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1312) long ret = 0;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1313)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1314) #ifdef CONFIG_WATCH_QUEUE
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1315) if (pipe->watch_queue)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1316) return -EBUSY;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1317) #endif
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1318)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1319) size = round_pipe_size(arg);
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1320) nr_slots = size >> PAGE_SHIFT;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1321)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1322) if (!nr_slots)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1323) return -EINVAL;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1324)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1325) /*
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1326) * If trying to increase the pipe capacity, check that an
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1327) * unprivileged user is not trying to exceed various limits
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1328) * (soft limit check here, hard limit check just below).
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1329) * Decreasing the pipe capacity is always permitted, even
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1330) * if the user is currently over a limit.
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1331) */
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1332) if (nr_slots > pipe->max_usage &&
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1333) size > pipe_max_size && !capable(CAP_SYS_RESOURCE))
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1334) return -EPERM;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1335)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1336) user_bufs = account_pipe_buffers(pipe->user, pipe->nr_accounted, nr_slots);
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1337)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1338) if (nr_slots > pipe->max_usage &&
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1339) (too_many_pipe_buffers_hard(user_bufs) ||
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1340) too_many_pipe_buffers_soft(user_bufs)) &&
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1341) pipe_is_unprivileged_user()) {
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1342) ret = -EPERM;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1343) goto out_revert_acct;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1344) }
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1345)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1346) ret = pipe_resize_ring(pipe, nr_slots);
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1347) if (ret < 0)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1348) goto out_revert_acct;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1349)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1350) pipe->max_usage = nr_slots;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1351) pipe->nr_accounted = nr_slots;
6718b6f855a0b (David Howells 2019-10-16 16:47:32 +0100 1352) return pipe->max_usage * PAGE_SIZE;
b0b91d18e2e97 (Michael Kerrisk (man-pages) 2016-10-11 13:53:31 -0700 1353)
b0b91d18e2e97 (Michael Kerrisk (man-pages) 2016-10-11 13:53:31 -0700 1354) out_revert_acct:
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1355) (void) account_pipe_buffers(pipe->user, nr_slots, pipe->nr_accounted);
b0b91d18e2e97 (Michael Kerrisk (man-pages) 2016-10-11 13:53:31 -0700 1356) return ret;
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1357) }
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1358)
72083646528d4 (Linus Torvalds 2010-11-28 16:27:19 -0800 1359) /*
4e7b5671c6a88 (Christoph Hellwig 2020-11-23 13:38:40 +0100 1360) * Note that i_pipe and i_cdev share the same location, so checking ->i_pipe is
4e7b5671c6a88 (Christoph Hellwig 2020-11-23 13:38:40 +0100 1361) * not enough to verify that this is a pipe.
72083646528d4 (Linus Torvalds 2010-11-28 16:27:19 -0800 1362) */
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1363) struct pipe_inode_info *get_pipe_info(struct file *file, bool for_splice)
72083646528d4 (Linus Torvalds 2010-11-28 16:27:19 -0800 1364) {
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1365) struct pipe_inode_info *pipe = file->private_data;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1366)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1367) if (file->f_op != &pipefifo_fops || !pipe)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1368) return NULL;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1369) #ifdef CONFIG_WATCH_QUEUE
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1370) if (for_splice && pipe->watch_queue)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1371) return NULL;
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1372) #endif
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1373) return pipe;
72083646528d4 (Linus Torvalds 2010-11-28 16:27:19 -0800 1374) }
72083646528d4 (Linus Torvalds 2010-11-28 16:27:19 -0800 1375)
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1376) long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1377) {
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1378) struct pipe_inode_info *pipe;
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1379) long ret;
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1380)
c73be61cede58 (David Howells 2020-01-14 17:07:11 +0000 1381) pipe = get_pipe_info(file, false);
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1382) if (!pipe)
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1383) return -EBADF;
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1384)
ebec73f4752b7 (Al Viro 2013-03-21 12:24:01 -0400 1385) __pipe_lock(pipe);
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1386)
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1387) switch (cmd) {
d37d416664081 (Michael Kerrisk (man-pages) 2016-10-11 13:53:25 -0700 1388) case F_SETPIPE_SZ:
d37d416664081 (Michael Kerrisk (man-pages) 2016-10-11 13:53:25 -0700 1389) ret = pipe_set_size(pipe, arg);
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1390) break;
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1391) case F_GETPIPE_SZ:
6718b6f855a0b (David Howells 2019-10-16 16:47:32 +0100 1392) ret = pipe->max_usage * PAGE_SIZE;
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1393) break;
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1394) default:
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1395) ret = -EINVAL;
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1396) break;
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1397) }
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1398)
ebec73f4752b7 (Al Viro 2013-03-21 12:24:01 -0400 1399) __pipe_unlock(pipe);
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1400) return ret;
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1401) }
35f3d14dbbc58 (Jens Axboe 2010-05-20 10:43:18 +0200 1402)
ff0c7d15f9787 (Nicholas Piggin 2011-01-07 17:49:50 +1100 1403) static const struct super_operations pipefs_ops = {
ff0c7d15f9787 (Nicholas Piggin 2011-01-07 17:49:50 +1100 1404) .destroy_inode = free_inode_nonrcu,
d70ef97baf048 (Pavel Emelyanov 2011-10-31 17:10:04 -0700 1405) .statfs = simple_statfs,
ff0c7d15f9787 (Nicholas Piggin 2011-01-07 17:49:50 +1100 1406) };
ff0c7d15f9787 (Nicholas Piggin 2011-01-07 17:49:50 +1100 1407)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1408) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1409) * pipefs should _never_ be mounted by userland - too much of security hassle,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1410) * no real gain from having the whole whorehouse mounted. So we don't need
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1411) * any operations on the root directory. However, we need a non-trivial
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1412) * d_name - pipe: will go nicely and kill the special-casing in procfs.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1413) */
4fa7ec5db7e73 (David Howells 2019-03-25 16:38:23 +0000 1414)
4fa7ec5db7e73 (David Howells 2019-03-25 16:38:23 +0000 1415) static int pipefs_init_fs_context(struct fs_context *fc)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1416) {
4fa7ec5db7e73 (David Howells 2019-03-25 16:38:23 +0000 1417) struct pseudo_fs_context *ctx = init_pseudo(fc, PIPEFS_MAGIC);
4fa7ec5db7e73 (David Howells 2019-03-25 16:38:23 +0000 1418) if (!ctx)
4fa7ec5db7e73 (David Howells 2019-03-25 16:38:23 +0000 1419) return -ENOMEM;
4fa7ec5db7e73 (David Howells 2019-03-25 16:38:23 +0000 1420) ctx->ops = &pipefs_ops;
4fa7ec5db7e73 (David Howells 2019-03-25 16:38:23 +0000 1421) ctx->dops = &pipefs_dentry_operations;
4fa7ec5db7e73 (David Howells 2019-03-25 16:38:23 +0000 1422) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1423) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1424)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1425) static struct file_system_type pipe_fs_type = {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1426) .name = "pipefs",
4fa7ec5db7e73 (David Howells 2019-03-25 16:38:23 +0000 1427) .init_fs_context = pipefs_init_fs_context,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1428) .kill_sb = kill_anon_super,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1429) };
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1430)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1431) static int __init init_pipe_fs(void)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1432) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1433) int err = register_filesystem(&pipe_fs_type);
341b446bc5aa3 (Ingo Molnar 2006-04-11 13:57:45 +0200 1434)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1435) if (!err) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1436) pipe_mnt = kern_mount(&pipe_fs_type);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1437) if (IS_ERR(pipe_mnt)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1438) err = PTR_ERR(pipe_mnt);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1439) unregister_filesystem(&pipe_fs_type);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1440) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1441) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1442) return err;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1443) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1444)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1445) fs_initcall(init_pipe_fs);