Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * linux/fs/pipe.c | |
4 | * | |
5 | * Copyright (C) 1991, 1992, 1999 Linus Torvalds | |
6 | */ | |
7 | ||
8 | #include <linux/mm.h> | |
9 | #include <linux/file.h> | |
10 | #include <linux/poll.h> | |
11 | #include <linux/slab.h> | |
12 | #include <linux/module.h> | |
13 | #include <linux/init.h> | |
14 | #include <linux/fs.h> | |
35f3d14d | 15 | #include <linux/log2.h> |
1da177e4 | 16 | #include <linux/mount.h> |
4fa7ec5d | 17 | #include <linux/pseudo_fs.h> |
b502bd11 | 18 | #include <linux/magic.h> |
1da177e4 LT |
19 | #include <linux/pipe_fs_i.h> |
20 | #include <linux/uio.h> | |
21 | #include <linux/highmem.h> | |
5274f052 | 22 | #include <linux/pagemap.h> |
db349509 | 23 | #include <linux/audit.h> |
ba719bae | 24 | #include <linux/syscalls.h> |
b492e95b | 25 | #include <linux/fcntl.h> |
d86133bd | 26 | #include <linux/memcontrol.h> |
1da177e4 | 27 | |
7c0f6ba6 | 28 | #include <linux/uaccess.h> |
1da177e4 LT |
29 | #include <asm/ioctls.h> |
30 | ||
599a0ac1 AV |
31 | #include "internal.h" |
32 | ||
b492e95b JA |
33 | /* |
34 | * The max size that a non-root user is allowed to grow the pipe. Can | |
ff9da691 | 35 | * be set by root in /proc/sys/fs/pipe-max-size |
b492e95b | 36 | */ |
ff9da691 JA |
37 | unsigned int pipe_max_size = 1048576; |
38 | ||
759c0114 WT |
39 | /* Maximum allocatable pages per user. Hard limit is unset by default, soft |
40 | * matches default values. | |
41 | */ | |
42 | unsigned long pipe_user_pages_hard; | |
43 | unsigned long pipe_user_pages_soft = PIPE_DEF_BUFFERS * INR_OPEN_CUR; | |
44 | ||
1da177e4 | 45 | /* |
8cefc107 DH |
46 | * We use head and tail indices that aren't masked off, except at the point of |
47 | * dereference, but rather they're allowed to wrap naturally. This means there | |
48 | * isn't a dead spot in the buffer, but the ring has to be a power of two and | |
49 | * <= 2^31. | |
50 | * -- David Howells 2019-09-23. | |
51 | * | |
1da177e4 LT |
52 | * Reads with count = 0 should always return 0. |
53 | * -- Julian Bradfield 1999-06-07. | |
54 | * | |
55 | * FIFOs and Pipes now generate SIGIO for both readers and writers. | |
56 | * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16 | |
57 | * | |
58 | * pipe_read & write cleanup | |
59 | * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09 | |
60 | */ | |
61 | ||
61e0d47c MS |
62 | static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass) |
63 | { | |
6447a3cf | 64 | if (pipe->files) |
72b0d9aa | 65 | mutex_lock_nested(&pipe->mutex, subclass); |
61e0d47c MS |
66 | } |
67 | ||
68 | void pipe_lock(struct pipe_inode_info *pipe) | |
69 | { | |
70 | /* | |
71 | * pipe_lock() nests non-pipe inode locks (for writing to a file) | |
72 | */ | |
73 | pipe_lock_nested(pipe, I_MUTEX_PARENT); | |
74 | } | |
75 | EXPORT_SYMBOL(pipe_lock); | |
76 | ||
77 | void pipe_unlock(struct pipe_inode_info *pipe) | |
78 | { | |
6447a3cf | 79 | if (pipe->files) |
72b0d9aa | 80 | mutex_unlock(&pipe->mutex); |
61e0d47c MS |
81 | } |
82 | EXPORT_SYMBOL(pipe_unlock); | |
83 | ||
ebec73f4 AV |
84 | static inline void __pipe_lock(struct pipe_inode_info *pipe) |
85 | { | |
86 | mutex_lock_nested(&pipe->mutex, I_MUTEX_PARENT); | |
87 | } | |
88 | ||
89 | static inline void __pipe_unlock(struct pipe_inode_info *pipe) | |
90 | { | |
91 | mutex_unlock(&pipe->mutex); | |
92 | } | |
93 | ||
61e0d47c MS |
94 | void pipe_double_lock(struct pipe_inode_info *pipe1, |
95 | struct pipe_inode_info *pipe2) | |
96 | { | |
97 | BUG_ON(pipe1 == pipe2); | |
98 | ||
99 | if (pipe1 < pipe2) { | |
100 | pipe_lock_nested(pipe1, I_MUTEX_PARENT); | |
101 | pipe_lock_nested(pipe2, I_MUTEX_CHILD); | |
102 | } else { | |
023d43c7 PZ |
103 | pipe_lock_nested(pipe2, I_MUTEX_PARENT); |
104 | pipe_lock_nested(pipe1, I_MUTEX_CHILD); | |
61e0d47c MS |
105 | } |
106 | } | |
107 | ||
1da177e4 | 108 | /* Drop the inode semaphore and wait for a pipe event, atomically */ |
3a326a2c | 109 | void pipe_wait(struct pipe_inode_info *pipe) |
1da177e4 LT |
110 | { |
111 | DEFINE_WAIT(wait); | |
112 | ||
d79fc0fc IM |
113 | /* |
114 | * Pipes are system-local resources, so sleeping on them | |
115 | * is considered a noninteractive wait: | |
116 | */ | |
af927232 | 117 | prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE); |
61e0d47c | 118 | pipe_unlock(pipe); |
1da177e4 | 119 | schedule(); |
3a326a2c | 120 | finish_wait(&pipe->wait, &wait); |
61e0d47c | 121 | pipe_lock(pipe); |
1da177e4 LT |
122 | } |
123 | ||
341b446b IM |
124 | static void anon_pipe_buf_release(struct pipe_inode_info *pipe, |
125 | struct pipe_buffer *buf) | |
1da177e4 LT |
126 | { |
127 | struct page *page = buf->page; | |
128 | ||
5274f052 JA |
129 | /* |
130 | * If nobody else uses this page, and we don't already have a | |
131 | * temporary page, let's keep track of it as a one-deep | |
341b446b | 132 | * allocation cache. (Otherwise just release our reference to it) |
5274f052 | 133 | */ |
341b446b | 134 | if (page_count(page) == 1 && !pipe->tmp_page) |
923f4f23 | 135 | pipe->tmp_page = page; |
341b446b | 136 | else |
09cbfeaf | 137 | put_page(page); |
1da177e4 LT |
138 | } |
139 | ||
d86133bd VD |
140 | static int anon_pipe_buf_steal(struct pipe_inode_info *pipe, |
141 | struct pipe_buffer *buf) | |
142 | { | |
143 | struct page *page = buf->page; | |
144 | ||
145 | if (page_count(page) == 1) { | |
60cd4bcd | 146 | memcg_kmem_uncharge(page, 0); |
d86133bd VD |
147 | __SetPageLocked(page); |
148 | return 0; | |
149 | } | |
150 | return 1; | |
151 | } | |
152 | ||
0845718d | 153 | /** |
b51d63c6 | 154 | * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer |
0845718d JA |
155 | * @pipe: the pipe that the buffer belongs to |
156 | * @buf: the buffer to attempt to steal | |
157 | * | |
158 | * Description: | |
b51d63c6 | 159 | * This function attempts to steal the &struct page attached to |
0845718d JA |
160 | * @buf. If successful, this function returns 0 and returns with |
161 | * the page locked. The caller may then reuse the page for whatever | |
b51d63c6 | 162 | * he wishes; the typical use is insertion into a different file |
0845718d JA |
163 | * page cache. |
164 | */ | |
330ab716 JA |
165 | int generic_pipe_buf_steal(struct pipe_inode_info *pipe, |
166 | struct pipe_buffer *buf) | |
5abc97aa | 167 | { |
46e678c9 JA |
168 | struct page *page = buf->page; |
169 | ||
0845718d JA |
170 | /* |
171 | * A reference of one is golden, that means that the owner of this | |
172 | * page is the only one holding a reference to it. lock the page | |
173 | * and return OK. | |
174 | */ | |
46e678c9 | 175 | if (page_count(page) == 1) { |
46e678c9 JA |
176 | lock_page(page); |
177 | return 0; | |
178 | } | |
179 | ||
180 | return 1; | |
5abc97aa | 181 | } |
51921cb7 | 182 | EXPORT_SYMBOL(generic_pipe_buf_steal); |
5abc97aa | 183 | |
0845718d | 184 | /** |
b51d63c6 | 185 | * generic_pipe_buf_get - get a reference to a &struct pipe_buffer |
0845718d JA |
186 | * @pipe: the pipe that the buffer belongs to |
187 | * @buf: the buffer to get a reference to | |
188 | * | |
189 | * Description: | |
190 | * This function grabs an extra reference to @buf. It's used in | |
191 | * in the tee() system call, when we duplicate the buffers in one | |
192 | * pipe into another. | |
193 | */ | |
15fab63e | 194 | bool generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf) |
70524490 | 195 | { |
15fab63e | 196 | return try_get_page(buf->page); |
70524490 | 197 | } |
51921cb7 | 198 | EXPORT_SYMBOL(generic_pipe_buf_get); |
70524490 | 199 | |
0845718d JA |
200 | /** |
201 | * generic_pipe_buf_confirm - verify contents of the pipe buffer | |
79685b8d | 202 | * @info: the pipe that the buffer belongs to |
0845718d JA |
203 | * @buf: the buffer to confirm |
204 | * | |
205 | * Description: | |
206 | * This function does nothing, because the generic pipe code uses | |
207 | * pages that are always good when inserted into the pipe. | |
208 | */ | |
cac36bb0 JA |
209 | int generic_pipe_buf_confirm(struct pipe_inode_info *info, |
210 | struct pipe_buffer *buf) | |
f84d7519 JA |
211 | { |
212 | return 0; | |
213 | } | |
51921cb7 | 214 | EXPORT_SYMBOL(generic_pipe_buf_confirm); |
f84d7519 | 215 | |
6818173b MS |
216 | /** |
217 | * generic_pipe_buf_release - put a reference to a &struct pipe_buffer | |
218 | * @pipe: the pipe that the buffer belongs to | |
219 | * @buf: the buffer to put a reference to | |
220 | * | |
221 | * Description: | |
222 | * This function releases a reference to @buf. | |
223 | */ | |
224 | void generic_pipe_buf_release(struct pipe_inode_info *pipe, | |
225 | struct pipe_buffer *buf) | |
226 | { | |
09cbfeaf | 227 | put_page(buf->page); |
6818173b | 228 | } |
51921cb7 | 229 | EXPORT_SYMBOL(generic_pipe_buf_release); |
6818173b | 230 | |
01e7187b | 231 | /* New data written to a pipe may be appended to a buffer with this type. */ |
d4c3cca9 | 232 | static const struct pipe_buf_operations anon_pipe_buf_ops = { |
cac36bb0 | 233 | .confirm = generic_pipe_buf_confirm, |
1da177e4 | 234 | .release = anon_pipe_buf_release, |
d86133bd | 235 | .steal = anon_pipe_buf_steal, |
f84d7519 | 236 | .get = generic_pipe_buf_get, |
1da177e4 LT |
237 | }; |
238 | ||
a0ce2f0a | 239 | static const struct pipe_buf_operations anon_pipe_buf_nomerge_ops = { |
cac36bb0 | 240 | .confirm = generic_pipe_buf_confirm, |
1da177e4 | 241 | .release = anon_pipe_buf_release, |
d86133bd | 242 | .steal = anon_pipe_buf_steal, |
f84d7519 | 243 | .get = generic_pipe_buf_get, |
1da177e4 LT |
244 | }; |
245 | ||
9883035a | 246 | static const struct pipe_buf_operations packet_pipe_buf_ops = { |
9883035a LT |
247 | .confirm = generic_pipe_buf_confirm, |
248 | .release = anon_pipe_buf_release, | |
d86133bd | 249 | .steal = anon_pipe_buf_steal, |
9883035a LT |
250 | .get = generic_pipe_buf_get, |
251 | }; | |
252 | ||
01e7187b JH |
253 | /** |
254 | * pipe_buf_mark_unmergeable - mark a &struct pipe_buffer as unmergeable | |
255 | * @buf: the buffer to mark | |
256 | * | |
257 | * Description: | |
258 | * This function ensures that no future writes will be merged into the | |
259 | * given &struct pipe_buffer. This is necessary when multiple pipe buffers | |
260 | * share the same backing page. | |
261 | */ | |
a0ce2f0a JH |
262 | void pipe_buf_mark_unmergeable(struct pipe_buffer *buf) |
263 | { | |
264 | if (buf->ops == &anon_pipe_buf_ops) | |
265 | buf->ops = &anon_pipe_buf_nomerge_ops; | |
266 | } | |
267 | ||
01e7187b JH |
268 | static bool pipe_buf_can_merge(struct pipe_buffer *buf) |
269 | { | |
270 | return buf->ops == &anon_pipe_buf_ops; | |
271 | } | |
272 | ||
1da177e4 | 273 | static ssize_t |
fb9096a3 | 274 | pipe_read(struct kiocb *iocb, struct iov_iter *to) |
1da177e4 | 275 | { |
fb9096a3 | 276 | size_t total_len = iov_iter_count(to); |
ee0b3e67 | 277 | struct file *filp = iocb->ki_filp; |
de32ec4c | 278 | struct pipe_inode_info *pipe = filp->private_data; |
1da177e4 LT |
279 | int do_wakeup; |
280 | ssize_t ret; | |
1da177e4 | 281 | |
1da177e4 LT |
282 | /* Null read succeeds. */ |
283 | if (unlikely(total_len == 0)) | |
284 | return 0; | |
285 | ||
286 | do_wakeup = 0; | |
287 | ret = 0; | |
ebec73f4 | 288 | __pipe_lock(pipe); |
1da177e4 | 289 | for (;;) { |
8cefc107 DH |
290 | unsigned int head = pipe->head; |
291 | unsigned int tail = pipe->tail; | |
292 | unsigned int mask = pipe->ring_size - 1; | |
293 | ||
294 | if (!pipe_empty(head, tail)) { | |
295 | struct pipe_buffer *buf = &pipe->bufs[tail & mask]; | |
1da177e4 | 296 | size_t chars = buf->len; |
637b58c2 AV |
297 | size_t written; |
298 | int error; | |
1da177e4 LT |
299 | |
300 | if (chars > total_len) | |
301 | chars = total_len; | |
302 | ||
fba597db | 303 | error = pipe_buf_confirm(pipe, buf); |
f84d7519 | 304 | if (error) { |
5274f052 | 305 | if (!ret) |
e5953cbd | 306 | ret = error; |
5274f052 JA |
307 | break; |
308 | } | |
f84d7519 | 309 | |
fb9096a3 | 310 | written = copy_page_to_iter(buf->page, buf->offset, chars, to); |
637b58c2 | 311 | if (unlikely(written < chars)) { |
341b446b | 312 | if (!ret) |
637b58c2 | 313 | ret = -EFAULT; |
1da177e4 LT |
314 | break; |
315 | } | |
316 | ret += chars; | |
317 | buf->offset += chars; | |
318 | buf->len -= chars; | |
9883035a LT |
319 | |
320 | /* Was it a packet buffer? Clean up and exit */ | |
321 | if (buf->flags & PIPE_BUF_FLAG_PACKET) { | |
322 | total_len = chars; | |
323 | buf->len = 0; | |
324 | } | |
325 | ||
1da177e4 | 326 | if (!buf->len) { |
a779638c | 327 | pipe_buf_release(pipe, buf); |
8cefc107 DH |
328 | tail++; |
329 | pipe->tail = tail; | |
1da177e4 LT |
330 | do_wakeup = 1; |
331 | } | |
332 | total_len -= chars; | |
333 | if (!total_len) | |
334 | break; /* common path: read succeeded */ | |
8cefc107 DH |
335 | if (!pipe_empty(head, tail)) /* More to do? */ |
336 | continue; | |
1da177e4 | 337 | } |
8cefc107 | 338 | |
923f4f23 | 339 | if (!pipe->writers) |
1da177e4 | 340 | break; |
923f4f23 | 341 | if (!pipe->waiting_writers) { |
1da177e4 LT |
342 | /* syscall merging: Usually we must not sleep |
343 | * if O_NONBLOCK is set, or if we got some data. | |
344 | * But if a writer sleeps in kernel space, then | |
345 | * we can wait for that data without violating POSIX. | |
346 | */ | |
347 | if (ret) | |
348 | break; | |
349 | if (filp->f_flags & O_NONBLOCK) { | |
350 | ret = -EAGAIN; | |
351 | break; | |
352 | } | |
353 | } | |
354 | if (signal_pending(current)) { | |
341b446b IM |
355 | if (!ret) |
356 | ret = -ERESTARTSYS; | |
1da177e4 LT |
357 | break; |
358 | } | |
359 | if (do_wakeup) { | |
a9a08845 | 360 | wake_up_interruptible_sync_poll(&pipe->wait, EPOLLOUT | EPOLLWRNORM); |
923f4f23 | 361 | kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); |
1da177e4 | 362 | } |
923f4f23 | 363 | pipe_wait(pipe); |
1da177e4 | 364 | } |
ebec73f4 | 365 | __pipe_unlock(pipe); |
341b446b IM |
366 | |
367 | /* Signal writers asynchronously that there is more room. */ | |
1da177e4 | 368 | if (do_wakeup) { |
a9a08845 | 369 | wake_up_interruptible_sync_poll(&pipe->wait, EPOLLOUT | EPOLLWRNORM); |
923f4f23 | 370 | kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); |
1da177e4 LT |
371 | } |
372 | if (ret > 0) | |
373 | file_accessed(filp); | |
374 | return ret; | |
375 | } | |
376 | ||
9883035a LT |
377 | static inline int is_packetized(struct file *file) |
378 | { | |
379 | return (file->f_flags & O_DIRECT) != 0; | |
380 | } | |
381 | ||
1da177e4 | 382 | static ssize_t |
f0d1bec9 | 383 | pipe_write(struct kiocb *iocb, struct iov_iter *from) |
1da177e4 | 384 | { |
ee0b3e67 | 385 | struct file *filp = iocb->ki_filp; |
de32ec4c | 386 | struct pipe_inode_info *pipe = filp->private_data; |
8cefc107 | 387 | unsigned int head, tail, max_usage, mask; |
f0d1bec9 AV |
388 | ssize_t ret = 0; |
389 | int do_wakeup = 0; | |
390 | size_t total_len = iov_iter_count(from); | |
1da177e4 LT |
391 | ssize_t chars; |
392 | ||
1da177e4 LT |
393 | /* Null write succeeds. */ |
394 | if (unlikely(total_len == 0)) | |
395 | return 0; | |
396 | ||
ebec73f4 | 397 | __pipe_lock(pipe); |
1da177e4 | 398 | |
923f4f23 | 399 | if (!pipe->readers) { |
1da177e4 LT |
400 | send_sig(SIGPIPE, current, 0); |
401 | ret = -EPIPE; | |
402 | goto out; | |
403 | } | |
404 | ||
8cefc107 DH |
405 | tail = pipe->tail; |
406 | head = pipe->head; | |
407 | max_usage = pipe->ring_size; | |
408 | mask = pipe->ring_size - 1; | |
409 | ||
1da177e4 LT |
410 | /* We try to merge small writes */ |
411 | chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */ | |
8cefc107 DH |
412 | if (!pipe_empty(head, tail) && chars != 0) { |
413 | struct pipe_buffer *buf = &pipe->bufs[(head - 1) & mask]; | |
1da177e4 | 414 | int offset = buf->offset + buf->len; |
341b446b | 415 | |
01e7187b | 416 | if (pipe_buf_can_merge(buf) && offset + chars <= PAGE_SIZE) { |
fba597db | 417 | ret = pipe_buf_confirm(pipe, buf); |
6ae08069 | 418 | if (ret) |
5274f052 | 419 | goto out; |
f84d7519 | 420 | |
f0d1bec9 AV |
421 | ret = copy_page_from_iter(buf->page, offset, chars, from); |
422 | if (unlikely(ret < chars)) { | |
6ae08069 | 423 | ret = -EFAULT; |
1da177e4 | 424 | goto out; |
f6762b7a | 425 | } |
f0d1bec9 | 426 | do_wakeup = 1; |
6ae08069 | 427 | buf->len += ret; |
f0d1bec9 | 428 | if (!iov_iter_count(from)) |
1da177e4 LT |
429 | goto out; |
430 | } | |
431 | } | |
432 | ||
433 | for (;;) { | |
923f4f23 | 434 | if (!pipe->readers) { |
1da177e4 | 435 | send_sig(SIGPIPE, current, 0); |
341b446b IM |
436 | if (!ret) |
437 | ret = -EPIPE; | |
1da177e4 LT |
438 | break; |
439 | } | |
8cefc107 DH |
440 | |
441 | tail = pipe->tail; | |
442 | if (!pipe_full(head, tail, max_usage)) { | |
443 | struct pipe_buffer *buf = &pipe->bufs[head & mask]; | |
923f4f23 | 444 | struct page *page = pipe->tmp_page; |
f0d1bec9 | 445 | int copied; |
1da177e4 LT |
446 | |
447 | if (!page) { | |
d86133bd | 448 | page = alloc_page(GFP_HIGHUSER | __GFP_ACCOUNT); |
1da177e4 LT |
449 | if (unlikely(!page)) { |
450 | ret = ret ? : -ENOMEM; | |
451 | break; | |
452 | } | |
923f4f23 | 453 | pipe->tmp_page = page; |
1da177e4 | 454 | } |
341b446b | 455 | /* Always wake up, even if the copy fails. Otherwise |
1da177e4 LT |
456 | * we lock up (O_NONBLOCK-)readers that sleep due to |
457 | * syscall merging. | |
458 | * FIXME! Is this really true? | |
459 | */ | |
460 | do_wakeup = 1; | |
f0d1bec9 AV |
461 | copied = copy_page_from_iter(page, 0, PAGE_SIZE, from); |
462 | if (unlikely(copied < PAGE_SIZE && iov_iter_count(from))) { | |
341b446b | 463 | if (!ret) |
f0d1bec9 | 464 | ret = -EFAULT; |
1da177e4 LT |
465 | break; |
466 | } | |
f0d1bec9 | 467 | ret += copied; |
1da177e4 LT |
468 | |
469 | /* Insert it into the buffer array */ | |
470 | buf->page = page; | |
471 | buf->ops = &anon_pipe_buf_ops; | |
472 | buf->offset = 0; | |
f0d1bec9 | 473 | buf->len = copied; |
9883035a LT |
474 | buf->flags = 0; |
475 | if (is_packetized(filp)) { | |
476 | buf->ops = &packet_pipe_buf_ops; | |
477 | buf->flags = PIPE_BUF_FLAG_PACKET; | |
478 | } | |
8cefc107 DH |
479 | |
480 | head++; | |
481 | pipe->head = head; | |
923f4f23 | 482 | pipe->tmp_page = NULL; |
1da177e4 | 483 | |
f0d1bec9 | 484 | if (!iov_iter_count(from)) |
1da177e4 LT |
485 | break; |
486 | } | |
8cefc107 DH |
487 | |
488 | if (!pipe_full(head, tail, max_usage)) | |
1da177e4 | 489 | continue; |
8cefc107 DH |
490 | |
491 | /* Wait for buffer space to become available. */ | |
1da177e4 | 492 | if (filp->f_flags & O_NONBLOCK) { |
341b446b IM |
493 | if (!ret) |
494 | ret = -EAGAIN; | |
1da177e4 LT |
495 | break; |
496 | } | |
497 | if (signal_pending(current)) { | |
341b446b IM |
498 | if (!ret) |
499 | ret = -ERESTARTSYS; | |
1da177e4 LT |
500 | break; |
501 | } | |
502 | if (do_wakeup) { | |
a9a08845 | 503 | wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLRDNORM); |
923f4f23 | 504 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); |
1da177e4 LT |
505 | do_wakeup = 0; |
506 | } | |
923f4f23 IM |
507 | pipe->waiting_writers++; |
508 | pipe_wait(pipe); | |
509 | pipe->waiting_writers--; | |
1da177e4 LT |
510 | } |
511 | out: | |
ebec73f4 | 512 | __pipe_unlock(pipe); |
1da177e4 | 513 | if (do_wakeup) { |
a9a08845 | 514 | wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLRDNORM); |
923f4f23 | 515 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); |
1da177e4 | 516 | } |
7e775f46 | 517 | if (ret > 0 && sb_start_write_trylock(file_inode(filp)->i_sb)) { |
c3b2da31 JB |
518 | int err = file_update_time(filp); |
519 | if (err) | |
520 | ret = err; | |
7e775f46 | 521 | sb_end_write(file_inode(filp)->i_sb); |
c3b2da31 | 522 | } |
1da177e4 LT |
523 | return ret; |
524 | } | |
525 | ||
d59d0b1b | 526 | static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
1da177e4 | 527 | { |
de32ec4c | 528 | struct pipe_inode_info *pipe = filp->private_data; |
8cefc107 | 529 | int count, head, tail, mask; |
1da177e4 LT |
530 | |
531 | switch (cmd) { | |
532 | case FIONREAD: | |
ebec73f4 | 533 | __pipe_lock(pipe); |
1da177e4 | 534 | count = 0; |
8cefc107 DH |
535 | head = pipe->head; |
536 | tail = pipe->tail; | |
537 | mask = pipe->ring_size - 1; | |
538 | ||
539 | while (tail != head) { | |
540 | count += pipe->bufs[tail & mask].len; | |
541 | tail++; | |
1da177e4 | 542 | } |
ebec73f4 | 543 | __pipe_unlock(pipe); |
923f4f23 | 544 | |
1da177e4 LT |
545 | return put_user(count, (int __user *)arg); |
546 | default: | |
46ce341b | 547 | return -ENOIOCTLCMD; |
1da177e4 LT |
548 | } |
549 | } | |
550 | ||
dd67081b | 551 | /* No kernel lock held - fine */ |
a11e1d43 LT |
552 | static __poll_t |
553 | pipe_poll(struct file *filp, poll_table *wait) | |
dd67081b | 554 | { |
a11e1d43 | 555 | __poll_t mask; |
dd67081b | 556 | struct pipe_inode_info *pipe = filp->private_data; |
8cefc107 DH |
557 | unsigned int head = READ_ONCE(pipe->head); |
558 | unsigned int tail = READ_ONCE(pipe->tail); | |
a11e1d43 LT |
559 | |
560 | poll_wait(filp, &pipe->wait, wait); | |
1da177e4 | 561 | |
8cefc107 DH |
562 | BUG_ON(pipe_occupancy(head, tail) > pipe->ring_size); |
563 | ||
1da177e4 | 564 | /* Reading only -- no need for acquiring the semaphore. */ |
a11e1d43 | 565 | mask = 0; |
1da177e4 | 566 | if (filp->f_mode & FMODE_READ) { |
8cefc107 DH |
567 | if (!pipe_empty(head, tail)) |
568 | mask |= EPOLLIN | EPOLLRDNORM; | |
923f4f23 | 569 | if (!pipe->writers && filp->f_version != pipe->w_counter) |
a9a08845 | 570 | mask |= EPOLLHUP; |
1da177e4 LT |
571 | } |
572 | ||
573 | if (filp->f_mode & FMODE_WRITE) { | |
8cefc107 DH |
574 | if (!pipe_full(head, tail, pipe->ring_size)) |
575 | mask |= EPOLLOUT | EPOLLWRNORM; | |
5e5d7a22 | 576 | /* |
a9a08845 | 577 | * Most Unices do not set EPOLLERR for FIFOs but on Linux they |
5e5d7a22 PE |
578 | * behave exactly like pipes for poll(). |
579 | */ | |
923f4f23 | 580 | if (!pipe->readers) |
a9a08845 | 581 | mask |= EPOLLERR; |
1da177e4 LT |
582 | } |
583 | ||
584 | return mask; | |
585 | } | |
586 | ||
b0d8d229 LT |
587 | static void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe) |
588 | { | |
589 | int kill = 0; | |
590 | ||
591 | spin_lock(&inode->i_lock); | |
592 | if (!--pipe->files) { | |
593 | inode->i_pipe = NULL; | |
594 | kill = 1; | |
595 | } | |
596 | spin_unlock(&inode->i_lock); | |
597 | ||
598 | if (kill) | |
599 | free_pipe_info(pipe); | |
600 | } | |
601 | ||
1da177e4 | 602 | static int |
599a0ac1 | 603 | pipe_release(struct inode *inode, struct file *file) |
1da177e4 | 604 | { |
b0d8d229 | 605 | struct pipe_inode_info *pipe = file->private_data; |
923f4f23 | 606 | |
ebec73f4 | 607 | __pipe_lock(pipe); |
599a0ac1 AV |
608 | if (file->f_mode & FMODE_READ) |
609 | pipe->readers--; | |
610 | if (file->f_mode & FMODE_WRITE) | |
611 | pipe->writers--; | |
341b446b | 612 | |
ba5bb147 | 613 | if (pipe->readers || pipe->writers) { |
a9a08845 | 614 | wake_up_interruptible_sync_poll(&pipe->wait, EPOLLIN | EPOLLOUT | EPOLLRDNORM | EPOLLWRNORM | EPOLLERR | EPOLLHUP); |
923f4f23 IM |
615 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); |
616 | kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); | |
1da177e4 | 617 | } |
ebec73f4 | 618 | __pipe_unlock(pipe); |
ba5bb147 | 619 | |
b0d8d229 | 620 | put_pipe_info(inode, pipe); |
1da177e4 LT |
621 | return 0; |
622 | } | |
623 | ||
624 | static int | |
599a0ac1 | 625 | pipe_fasync(int fd, struct file *filp, int on) |
1da177e4 | 626 | { |
de32ec4c | 627 | struct pipe_inode_info *pipe = filp->private_data; |
599a0ac1 | 628 | int retval = 0; |
1da177e4 | 629 | |
ebec73f4 | 630 | __pipe_lock(pipe); |
599a0ac1 AV |
631 | if (filp->f_mode & FMODE_READ) |
632 | retval = fasync_helper(fd, filp, on, &pipe->fasync_readers); | |
633 | if ((filp->f_mode & FMODE_WRITE) && retval >= 0) { | |
341b446b | 634 | retval = fasync_helper(fd, filp, on, &pipe->fasync_writers); |
599a0ac1 AV |
635 | if (retval < 0 && (filp->f_mode & FMODE_READ)) |
636 | /* this can happen only if on == T */ | |
e5bc49ba ON |
637 | fasync_helper(-1, filp, 0, &pipe->fasync_readers); |
638 | } | |
ebec73f4 | 639 | __pipe_unlock(pipe); |
60aa4924 | 640 | return retval; |
1da177e4 LT |
641 | } |
642 | ||
9c87bcf0 | 643 | static unsigned long account_pipe_buffers(struct user_struct *user, |
759c0114 WT |
644 | unsigned long old, unsigned long new) |
645 | { | |
9c87bcf0 | 646 | return atomic_long_add_return(new - old, &user->pipe_bufs); |
759c0114 WT |
647 | } |
648 | ||
9c87bcf0 | 649 | static bool too_many_pipe_buffers_soft(unsigned long user_bufs) |
759c0114 | 650 | { |
f7340761 EB |
651 | unsigned long soft_limit = READ_ONCE(pipe_user_pages_soft); |
652 | ||
653 | return soft_limit && user_bufs > soft_limit; | |
759c0114 WT |
654 | } |
655 | ||
9c87bcf0 | 656 | static bool too_many_pipe_buffers_hard(unsigned long user_bufs) |
759c0114 | 657 | { |
f7340761 EB |
658 | unsigned long hard_limit = READ_ONCE(pipe_user_pages_hard); |
659 | ||
660 | return hard_limit && user_bufs > hard_limit; | |
759c0114 WT |
661 | } |
662 | ||
85c2dd54 EB |
663 | static bool is_unprivileged_user(void) |
664 | { | |
665 | return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN); | |
666 | } | |
667 | ||
7bee130e | 668 | struct pipe_inode_info *alloc_pipe_info(void) |
3a326a2c | 669 | { |
923f4f23 | 670 | struct pipe_inode_info *pipe; |
09b4d199 MK |
671 | unsigned long pipe_bufs = PIPE_DEF_BUFFERS; |
672 | struct user_struct *user = get_current_user(); | |
9c87bcf0 | 673 | unsigned long user_bufs; |
f7340761 | 674 | unsigned int max_size = READ_ONCE(pipe_max_size); |
3a326a2c | 675 | |
d86133bd | 676 | pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL_ACCOUNT); |
09b4d199 MK |
677 | if (pipe == NULL) |
678 | goto out_free_uid; | |
679 | ||
f7340761 EB |
680 | if (pipe_bufs * PAGE_SIZE > max_size && !capable(CAP_SYS_RESOURCE)) |
681 | pipe_bufs = max_size >> PAGE_SHIFT; | |
086e774a | 682 | |
9c87bcf0 | 683 | user_bufs = account_pipe_buffers(user, 0, pipe_bufs); |
a005ca0e | 684 | |
85c2dd54 | 685 | if (too_many_pipe_buffers_soft(user_bufs) && is_unprivileged_user()) { |
9c87bcf0 | 686 | user_bufs = account_pipe_buffers(user, pipe_bufs, 1); |
a005ca0e | 687 | pipe_bufs = 1; |
09b4d199 | 688 | } |
759c0114 | 689 | |
85c2dd54 | 690 | if (too_many_pipe_buffers_hard(user_bufs) && is_unprivileged_user()) |
a005ca0e MK |
691 | goto out_revert_acct; |
692 | ||
693 | pipe->bufs = kcalloc(pipe_bufs, sizeof(struct pipe_buffer), | |
694 | GFP_KERNEL_ACCOUNT); | |
695 | ||
09b4d199 MK |
696 | if (pipe->bufs) { |
697 | init_waitqueue_head(&pipe->wait); | |
698 | pipe->r_counter = pipe->w_counter = 1; | |
8cefc107 | 699 | pipe->ring_size = pipe_bufs; |
09b4d199 | 700 | pipe->user = user; |
09b4d199 MK |
701 | mutex_init(&pipe->mutex); |
702 | return pipe; | |
3a326a2c IM |
703 | } |
704 | ||
a005ca0e | 705 | out_revert_acct: |
9c87bcf0 | 706 | (void) account_pipe_buffers(user, pipe_bufs, 0); |
09b4d199 MK |
707 | kfree(pipe); |
708 | out_free_uid: | |
709 | free_uid(user); | |
35f3d14d | 710 | return NULL; |
3a326a2c IM |
711 | } |
712 | ||
4b8a8f1e | 713 | void free_pipe_info(struct pipe_inode_info *pipe) |
1da177e4 LT |
714 | { |
715 | int i; | |
1da177e4 | 716 | |
8cefc107 | 717 | (void) account_pipe_buffers(pipe->user, pipe->ring_size, 0); |
759c0114 | 718 | free_uid(pipe->user); |
8cefc107 | 719 | for (i = 0; i < pipe->ring_size; i++) { |
923f4f23 | 720 | struct pipe_buffer *buf = pipe->bufs + i; |
1da177e4 | 721 | if (buf->ops) |
a779638c | 722 | pipe_buf_release(pipe, buf); |
1da177e4 | 723 | } |
923f4f23 IM |
724 | if (pipe->tmp_page) |
725 | __free_page(pipe->tmp_page); | |
35f3d14d | 726 | kfree(pipe->bufs); |
923f4f23 | 727 | kfree(pipe); |
1da177e4 LT |
728 | } |
729 | ||
fa3536cc | 730 | static struct vfsmount *pipe_mnt __read_mostly; |
341b446b | 731 | |
c23fbb6b ED |
732 | /* |
733 | * pipefs_dname() is called from d_path(). | |
734 | */ | |
735 | static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen) | |
736 | { | |
737 | return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]", | |
75c3cfa8 | 738 | d_inode(dentry)->i_ino); |
c23fbb6b ED |
739 | } |
740 | ||
3ba13d17 | 741 | static const struct dentry_operations pipefs_dentry_operations = { |
c23fbb6b | 742 | .d_dname = pipefs_dname, |
1da177e4 LT |
743 | }; |
744 | ||
745 | static struct inode * get_pipe_inode(void) | |
746 | { | |
a209dfc7 | 747 | struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb); |
923f4f23 | 748 | struct pipe_inode_info *pipe; |
1da177e4 LT |
749 | |
750 | if (!inode) | |
751 | goto fail_inode; | |
752 | ||
85fe4025 CH |
753 | inode->i_ino = get_next_ino(); |
754 | ||
7bee130e | 755 | pipe = alloc_pipe_info(); |
923f4f23 | 756 | if (!pipe) |
1da177e4 | 757 | goto fail_iput; |
3a326a2c | 758 | |
ba5bb147 AV |
759 | inode->i_pipe = pipe; |
760 | pipe->files = 2; | |
923f4f23 | 761 | pipe->readers = pipe->writers = 1; |
599a0ac1 | 762 | inode->i_fop = &pipefifo_fops; |
1da177e4 LT |
763 | |
764 | /* | |
765 | * Mark the inode dirty from the very beginning, | |
766 | * that way it will never be moved to the dirty | |
767 | * list because "mark_inode_dirty()" will think | |
768 | * that it already _is_ on the dirty list. | |
769 | */ | |
770 | inode->i_state = I_DIRTY; | |
771 | inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR; | |
da9592ed DH |
772 | inode->i_uid = current_fsuid(); |
773 | inode->i_gid = current_fsgid(); | |
078cd827 | 774 | inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); |
923f4f23 | 775 | |
1da177e4 LT |
776 | return inode; |
777 | ||
778 | fail_iput: | |
779 | iput(inode); | |
341b446b | 780 | |
1da177e4 LT |
781 | fail_inode: |
782 | return NULL; | |
783 | } | |
784 | ||
e4fad8e5 | 785 | int create_pipe_files(struct file **res, int flags) |
1da177e4 | 786 | { |
e4fad8e5 | 787 | struct inode *inode = get_pipe_inode(); |
d6cbd281 | 788 | struct file *f; |
1da177e4 | 789 | |
1da177e4 | 790 | if (!inode) |
e4fad8e5 | 791 | return -ENFILE; |
1da177e4 | 792 | |
152b6372 AV |
793 | f = alloc_file_pseudo(inode, pipe_mnt, "", |
794 | O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT)), | |
795 | &pipefifo_fops); | |
e9bb1f9b | 796 | if (IS_ERR(f)) { |
152b6372 AV |
797 | free_pipe_info(inode->i_pipe); |
798 | iput(inode); | |
799 | return PTR_ERR(f); | |
e9bb1f9b | 800 | } |
341b446b | 801 | |
de32ec4c | 802 | f->private_data = inode->i_pipe; |
d6cbd281 | 803 | |
183266f2 AV |
804 | res[0] = alloc_file_clone(f, O_RDONLY | (flags & O_NONBLOCK), |
805 | &pipefifo_fops); | |
e9bb1f9b | 806 | if (IS_ERR(res[0])) { |
b10a4a9f AV |
807 | put_pipe_info(inode, inode->i_pipe); |
808 | fput(f); | |
809 | return PTR_ERR(res[0]); | |
e9bb1f9b | 810 | } |
de32ec4c | 811 | res[0]->private_data = inode->i_pipe; |
e4fad8e5 AV |
812 | res[1] = f; |
813 | return 0; | |
d6cbd281 AK |
814 | } |
815 | ||
5b249b1b | 816 | static int __do_pipe_flags(int *fd, struct file **files, int flags) |
d6cbd281 | 817 | { |
d6cbd281 AK |
818 | int error; |
819 | int fdw, fdr; | |
820 | ||
9883035a | 821 | if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT)) |
ed8cae8b UD |
822 | return -EINVAL; |
823 | ||
e4fad8e5 AV |
824 | error = create_pipe_files(files, flags); |
825 | if (error) | |
826 | return error; | |
d6cbd281 | 827 | |
ed8cae8b | 828 | error = get_unused_fd_flags(flags); |
d6cbd281 AK |
829 | if (error < 0) |
830 | goto err_read_pipe; | |
831 | fdr = error; | |
832 | ||
ed8cae8b | 833 | error = get_unused_fd_flags(flags); |
d6cbd281 AK |
834 | if (error < 0) |
835 | goto err_fdr; | |
836 | fdw = error; | |
837 | ||
157cf649 | 838 | audit_fd_pair(fdr, fdw); |
d6cbd281 AK |
839 | fd[0] = fdr; |
840 | fd[1] = fdw; | |
d6cbd281 AK |
841 | return 0; |
842 | ||
843 | err_fdr: | |
844 | put_unused_fd(fdr); | |
845 | err_read_pipe: | |
e4fad8e5 AV |
846 | fput(files[0]); |
847 | fput(files[1]); | |
d6cbd281 | 848 | return error; |
1da177e4 LT |
849 | } |
850 | ||
5b249b1b AV |
851 | int do_pipe_flags(int *fd, int flags) |
852 | { | |
853 | struct file *files[2]; | |
854 | int error = __do_pipe_flags(fd, files, flags); | |
855 | if (!error) { | |
856 | fd_install(fd[0], files[0]); | |
857 | fd_install(fd[1], files[1]); | |
858 | } | |
859 | return error; | |
860 | } | |
861 | ||
d35c7b0e UD |
862 | /* |
863 | * sys_pipe() is the normal C calling standard for creating | |
864 | * a pipe. It's not the way Unix traditionally does this, though. | |
865 | */ | |
0a216dd1 | 866 | static int do_pipe2(int __user *fildes, int flags) |
d35c7b0e | 867 | { |
5b249b1b | 868 | struct file *files[2]; |
d35c7b0e UD |
869 | int fd[2]; |
870 | int error; | |
871 | ||
5b249b1b | 872 | error = __do_pipe_flags(fd, files, flags); |
d35c7b0e | 873 | if (!error) { |
5b249b1b AV |
874 | if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) { |
875 | fput(files[0]); | |
876 | fput(files[1]); | |
877 | put_unused_fd(fd[0]); | |
878 | put_unused_fd(fd[1]); | |
d35c7b0e | 879 | error = -EFAULT; |
5b249b1b AV |
880 | } else { |
881 | fd_install(fd[0], files[0]); | |
882 | fd_install(fd[1], files[1]); | |
ba719bae | 883 | } |
d35c7b0e UD |
884 | } |
885 | return error; | |
886 | } | |
887 | ||
0a216dd1 DB |
888 | SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags) |
889 | { | |
890 | return do_pipe2(fildes, flags); | |
891 | } | |
892 | ||
2b664219 | 893 | SYSCALL_DEFINE1(pipe, int __user *, fildes) |
ed8cae8b | 894 | { |
0a216dd1 | 895 | return do_pipe2(fildes, 0); |
ed8cae8b UD |
896 | } |
897 | ||
fc7478a2 | 898 | static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt) |
f776c738 | 899 | { |
8cefc107 | 900 | int cur = *cnt; |
f776c738 AV |
901 | |
902 | while (cur == *cnt) { | |
fc7478a2 | 903 | pipe_wait(pipe); |
f776c738 AV |
904 | if (signal_pending(current)) |
905 | break; | |
906 | } | |
907 | return cur == *cnt ? -ERESTARTSYS : 0; | |
908 | } | |
909 | ||
fc7478a2 | 910 | static void wake_up_partner(struct pipe_inode_info *pipe) |
f776c738 | 911 | { |
fc7478a2 | 912 | wake_up_interruptible(&pipe->wait); |
f776c738 AV |
913 | } |
914 | ||
915 | static int fifo_open(struct inode *inode, struct file *filp) | |
916 | { | |
917 | struct pipe_inode_info *pipe; | |
599a0ac1 | 918 | bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC; |
f776c738 AV |
919 | int ret; |
920 | ||
ba5bb147 AV |
921 | filp->f_version = 0; |
922 | ||
923 | spin_lock(&inode->i_lock); | |
924 | if (inode->i_pipe) { | |
925 | pipe = inode->i_pipe; | |
926 | pipe->files++; | |
927 | spin_unlock(&inode->i_lock); | |
928 | } else { | |
929 | spin_unlock(&inode->i_lock); | |
7bee130e | 930 | pipe = alloc_pipe_info(); |
f776c738 | 931 | if (!pipe) |
ba5bb147 AV |
932 | return -ENOMEM; |
933 | pipe->files = 1; | |
934 | spin_lock(&inode->i_lock); | |
935 | if (unlikely(inode->i_pipe)) { | |
936 | inode->i_pipe->files++; | |
937 | spin_unlock(&inode->i_lock); | |
4b8a8f1e | 938 | free_pipe_info(pipe); |
ba5bb147 AV |
939 | pipe = inode->i_pipe; |
940 | } else { | |
941 | inode->i_pipe = pipe; | |
942 | spin_unlock(&inode->i_lock); | |
943 | } | |
f776c738 | 944 | } |
de32ec4c | 945 | filp->private_data = pipe; |
ba5bb147 AV |
946 | /* OK, we have a pipe and it's pinned down */ |
947 | ||
ebec73f4 | 948 | __pipe_lock(pipe); |
f776c738 AV |
949 | |
950 | /* We can only do regular read/write on fifos */ | |
951 | filp->f_mode &= (FMODE_READ | FMODE_WRITE); | |
952 | ||
953 | switch (filp->f_mode) { | |
954 | case FMODE_READ: | |
955 | /* | |
956 | * O_RDONLY | |
957 | * POSIX.1 says that O_NONBLOCK means return with the FIFO | |
958 | * opened, even when there is no process writing the FIFO. | |
959 | */ | |
f776c738 AV |
960 | pipe->r_counter++; |
961 | if (pipe->readers++ == 0) | |
fc7478a2 | 962 | wake_up_partner(pipe); |
f776c738 | 963 | |
599a0ac1 | 964 | if (!is_pipe && !pipe->writers) { |
f776c738 | 965 | if ((filp->f_flags & O_NONBLOCK)) { |
a9a08845 | 966 | /* suppress EPOLLHUP until we have |
f776c738 AV |
967 | * seen a writer */ |
968 | filp->f_version = pipe->w_counter; | |
969 | } else { | |
fc7478a2 | 970 | if (wait_for_partner(pipe, &pipe->w_counter)) |
f776c738 AV |
971 | goto err_rd; |
972 | } | |
973 | } | |
974 | break; | |
8cefc107 | 975 | |
f776c738 AV |
976 | case FMODE_WRITE: |
977 | /* | |
978 | * O_WRONLY | |
979 | * POSIX.1 says that O_NONBLOCK means return -1 with | |
980 | * errno=ENXIO when there is no process reading the FIFO. | |
981 | */ | |
982 | ret = -ENXIO; | |
599a0ac1 | 983 | if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers) |
f776c738 AV |
984 | goto err; |
985 | ||
f776c738 AV |
986 | pipe->w_counter++; |
987 | if (!pipe->writers++) | |
fc7478a2 | 988 | wake_up_partner(pipe); |
f776c738 | 989 | |
599a0ac1 | 990 | if (!is_pipe && !pipe->readers) { |
fc7478a2 | 991 | if (wait_for_partner(pipe, &pipe->r_counter)) |
f776c738 AV |
992 | goto err_wr; |
993 | } | |
994 | break; | |
8cefc107 | 995 | |
f776c738 AV |
996 | case FMODE_READ | FMODE_WRITE: |
997 | /* | |
998 | * O_RDWR | |
999 | * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set. | |
1000 | * This implementation will NEVER block on a O_RDWR open, since | |
1001 | * the process can at least talk to itself. | |
1002 | */ | |
f776c738 AV |
1003 | |
1004 | pipe->readers++; | |
1005 | pipe->writers++; | |
1006 | pipe->r_counter++; | |
1007 | pipe->w_counter++; | |
1008 | if (pipe->readers == 1 || pipe->writers == 1) | |
fc7478a2 | 1009 | wake_up_partner(pipe); |
f776c738 AV |
1010 | break; |
1011 | ||
1012 | default: | |
1013 | ret = -EINVAL; | |
1014 | goto err; | |
1015 | } | |
1016 | ||
1017 | /* Ok! */ | |
ebec73f4 | 1018 | __pipe_unlock(pipe); |
f776c738 AV |
1019 | return 0; |
1020 | ||
1021 | err_rd: | |
1022 | if (!--pipe->readers) | |
1023 | wake_up_interruptible(&pipe->wait); | |
1024 | ret = -ERESTARTSYS; | |
1025 | goto err; | |
1026 | ||
1027 | err_wr: | |
1028 | if (!--pipe->writers) | |
1029 | wake_up_interruptible(&pipe->wait); | |
1030 | ret = -ERESTARTSYS; | |
1031 | goto err; | |
1032 | ||
1033 | err: | |
ebec73f4 | 1034 | __pipe_unlock(pipe); |
b0d8d229 LT |
1035 | |
1036 | put_pipe_info(inode, pipe); | |
f776c738 AV |
1037 | return ret; |
1038 | } | |
1039 | ||
599a0ac1 AV |
1040 | const struct file_operations pipefifo_fops = { |
1041 | .open = fifo_open, | |
1042 | .llseek = no_llseek, | |
fb9096a3 | 1043 | .read_iter = pipe_read, |
f0d1bec9 | 1044 | .write_iter = pipe_write, |
a11e1d43 | 1045 | .poll = pipe_poll, |
599a0ac1 AV |
1046 | .unlocked_ioctl = pipe_ioctl, |
1047 | .release = pipe_release, | |
1048 | .fasync = pipe_fasync, | |
f776c738 AV |
1049 | }; |
1050 | ||
f491bd71 MK |
1051 | /* |
1052 | * Currently we rely on the pipe array holding a power-of-2 number | |
d3f14c48 | 1053 | * of pages. Returns 0 on error. |
f491bd71 | 1054 | */ |
96e99be4 | 1055 | unsigned int round_pipe_size(unsigned long size) |
f491bd71 | 1056 | { |
c4fed5a9 | 1057 | if (size > (1U << 31)) |
96e99be4 EB |
1058 | return 0; |
1059 | ||
4c2e4bef EB |
1060 | /* Minimum pipe size, as required by POSIX */ |
1061 | if (size < PAGE_SIZE) | |
c4fed5a9 | 1062 | return PAGE_SIZE; |
d3f14c48 | 1063 | |
c4fed5a9 | 1064 | return roundup_pow_of_two(size); |
f491bd71 MK |
1065 | } |
1066 | ||
35f3d14d JA |
1067 | /* |
1068 | * Allocate a new array of pipe buffers and copy the info over. Returns the | |
1069 | * pipe size if successful, or return -ERROR on error. | |
1070 | */ | |
d37d4166 | 1071 | static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long arg) |
35f3d14d JA |
1072 | { |
1073 | struct pipe_buffer *bufs; | |
8cefc107 | 1074 | unsigned int size, nr_slots, head, tail, mask, n; |
9c87bcf0 | 1075 | unsigned long user_bufs; |
b0b91d18 | 1076 | long ret = 0; |
d37d4166 MK |
1077 | |
1078 | size = round_pipe_size(arg); | |
8cefc107 | 1079 | nr_slots = size >> PAGE_SHIFT; |
d37d4166 | 1080 | |
8cefc107 | 1081 | if (!nr_slots) |
d37d4166 MK |
1082 | return -EINVAL; |
1083 | ||
b0b91d18 MK |
1084 | /* |
1085 | * If trying to increase the pipe capacity, check that an | |
1086 | * unprivileged user is not trying to exceed various limits | |
1087 | * (soft limit check here, hard limit check just below). | |
1088 | * Decreasing the pipe capacity is always permitted, even | |
1089 | * if the user is currently over a limit. | |
1090 | */ | |
8cefc107 | 1091 | if (nr_slots > pipe->ring_size && |
b0b91d18 | 1092 | size > pipe_max_size && !capable(CAP_SYS_RESOURCE)) |
d37d4166 MK |
1093 | return -EPERM; |
1094 | ||
8cefc107 | 1095 | user_bufs = account_pipe_buffers(pipe->user, pipe->ring_size, nr_slots); |
b0b91d18 | 1096 | |
8cefc107 | 1097 | if (nr_slots > pipe->ring_size && |
9c87bcf0 MK |
1098 | (too_many_pipe_buffers_hard(user_bufs) || |
1099 | too_many_pipe_buffers_soft(user_bufs)) && | |
85c2dd54 | 1100 | is_unprivileged_user()) { |
b0b91d18 MK |
1101 | ret = -EPERM; |
1102 | goto out_revert_acct; | |
1103 | } | |
35f3d14d | 1104 | |
35f3d14d | 1105 | /* |
8cefc107 DH |
1106 | * We can shrink the pipe, if arg is greater than the ring occupancy. |
1107 | * Since we don't expect a lot of shrink+grow operations, just free and | |
1108 | * allocate again like we would do for growing. If the pipe currently | |
35f3d14d JA |
1109 | * contains more buffers than arg, then return busy. |
1110 | */ | |
8cefc107 DH |
1111 | mask = pipe->ring_size - 1; |
1112 | head = pipe->head; | |
1113 | tail = pipe->tail; | |
1114 | n = pipe_occupancy(pipe->head, pipe->tail); | |
1115 | if (nr_slots < n) { | |
b0b91d18 MK |
1116 | ret = -EBUSY; |
1117 | goto out_revert_acct; | |
1118 | } | |
35f3d14d | 1119 | |
8cefc107 | 1120 | bufs = kcalloc(nr_slots, sizeof(*bufs), |
d86133bd | 1121 | GFP_KERNEL_ACCOUNT | __GFP_NOWARN); |
b0b91d18 MK |
1122 | if (unlikely(!bufs)) { |
1123 | ret = -ENOMEM; | |
1124 | goto out_revert_acct; | |
1125 | } | |
35f3d14d JA |
1126 | |
1127 | /* | |
1128 | * The pipe array wraps around, so just start the new one at zero | |
8cefc107 | 1129 | * and adjust the indices. |
35f3d14d | 1130 | */ |
8cefc107 DH |
1131 | if (n > 0) { |
1132 | unsigned int h = head & mask; | |
1133 | unsigned int t = tail & mask; | |
1134 | if (h > t) { | |
1135 | memcpy(bufs, pipe->bufs + t, | |
1136 | n * sizeof(struct pipe_buffer)); | |
1137 | } else { | |
1138 | unsigned int tsize = pipe->ring_size - t; | |
1139 | if (h > 0) | |
1140 | memcpy(bufs + tsize, pipe->bufs, | |
1141 | h * sizeof(struct pipe_buffer)); | |
1142 | memcpy(bufs, pipe->bufs + t, | |
1143 | tsize * sizeof(struct pipe_buffer)); | |
1144 | } | |
35f3d14d JA |
1145 | } |
1146 | ||
8cefc107 DH |
1147 | head = n; |
1148 | tail = 0; | |
1149 | ||
35f3d14d JA |
1150 | kfree(pipe->bufs); |
1151 | pipe->bufs = bufs; | |
8cefc107 DH |
1152 | pipe->ring_size = nr_slots; |
1153 | pipe->tail = tail; | |
1154 | pipe->head = head; | |
1155 | return pipe->ring_size * PAGE_SIZE; | |
b0b91d18 MK |
1156 | |
1157 | out_revert_acct: | |
8cefc107 | 1158 | (void) account_pipe_buffers(pipe->user, nr_slots, pipe->ring_size); |
b0b91d18 | 1159 | return ret; |
35f3d14d JA |
1160 | } |
1161 | ||
72083646 LT |
1162 | /* |
1163 | * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same | |
1164 | * location, so checking ->i_pipe is not enough to verify that this is a | |
1165 | * pipe. | |
1166 | */ | |
1167 | struct pipe_inode_info *get_pipe_info(struct file *file) | |
1168 | { | |
de32ec4c | 1169 | return file->f_op == &pipefifo_fops ? file->private_data : NULL; |
72083646 LT |
1170 | } |
1171 | ||
35f3d14d JA |
1172 | long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg) |
1173 | { | |
1174 | struct pipe_inode_info *pipe; | |
1175 | long ret; | |
1176 | ||
c66fb347 | 1177 | pipe = get_pipe_info(file); |
35f3d14d JA |
1178 | if (!pipe) |
1179 | return -EBADF; | |
1180 | ||
ebec73f4 | 1181 | __pipe_lock(pipe); |
35f3d14d JA |
1182 | |
1183 | switch (cmd) { | |
d37d4166 MK |
1184 | case F_SETPIPE_SZ: |
1185 | ret = pipe_set_size(pipe, arg); | |
35f3d14d JA |
1186 | break; |
1187 | case F_GETPIPE_SZ: | |
8cefc107 | 1188 | ret = pipe->ring_size * PAGE_SIZE; |
35f3d14d JA |
1189 | break; |
1190 | default: | |
1191 | ret = -EINVAL; | |
1192 | break; | |
1193 | } | |
1194 | ||
ebec73f4 | 1195 | __pipe_unlock(pipe); |
35f3d14d JA |
1196 | return ret; |
1197 | } | |
1198 | ||
ff0c7d15 NP |
1199 | static const struct super_operations pipefs_ops = { |
1200 | .destroy_inode = free_inode_nonrcu, | |
d70ef97b | 1201 | .statfs = simple_statfs, |
ff0c7d15 NP |
1202 | }; |
1203 | ||
1da177e4 LT |
1204 | /* |
1205 | * pipefs should _never_ be mounted by userland - too much of security hassle, | |
1206 | * no real gain from having the whole whorehouse mounted. So we don't need | |
1207 | * any operations on the root directory. However, we need a non-trivial | |
1208 | * d_name - pipe: will go nicely and kill the special-casing in procfs. | |
1209 | */ | |
4fa7ec5d DH |
1210 | |
1211 | static int pipefs_init_fs_context(struct fs_context *fc) | |
1da177e4 | 1212 | { |
4fa7ec5d DH |
1213 | struct pseudo_fs_context *ctx = init_pseudo(fc, PIPEFS_MAGIC); |
1214 | if (!ctx) | |
1215 | return -ENOMEM; | |
1216 | ctx->ops = &pipefs_ops; | |
1217 | ctx->dops = &pipefs_dentry_operations; | |
1218 | return 0; | |
1da177e4 LT |
1219 | } |
1220 | ||
1221 | static struct file_system_type pipe_fs_type = { | |
1222 | .name = "pipefs", | |
4fa7ec5d | 1223 | .init_fs_context = pipefs_init_fs_context, |
1da177e4 LT |
1224 | .kill_sb = kill_anon_super, |
1225 | }; | |
1226 | ||
1227 | static int __init init_pipe_fs(void) | |
1228 | { | |
1229 | int err = register_filesystem(&pipe_fs_type); | |
341b446b | 1230 | |
1da177e4 LT |
1231 | if (!err) { |
1232 | pipe_mnt = kern_mount(&pipe_fs_type); | |
1233 | if (IS_ERR(pipe_mnt)) { | |
1234 | err = PTR_ERR(pipe_mnt); | |
1235 | unregister_filesystem(&pipe_fs_type); | |
1236 | } | |
1237 | } | |
1238 | return err; | |
1239 | } | |
1240 | ||
1da177e4 | 1241 | fs_initcall(init_pipe_fs); |