pipe.c 27 KB

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