pipe.c 28 KB

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