pipe.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  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 <linux/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 <jelson@circlemud.org> 2001-08-16
  51. *
  52. * pipe_read & write cleanup
  53. * -- Manfred Spraul <manfred@colorfullife.com> 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. void generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
  173. {
  174. 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 packet_pipe_buf_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 ssize_t
  221. pipe_read(struct kiocb *iocb, struct iov_iter *to)
  222. {
  223. size_t total_len = iov_iter_count(to);
  224. struct file *filp = iocb->ki_filp;
  225. struct pipe_inode_info *pipe = filp->private_data;
  226. int do_wakeup;
  227. ssize_t ret;
  228. /* Null read succeeds. */
  229. if (unlikely(total_len == 0))
  230. return 0;
  231. do_wakeup = 0;
  232. ret = 0;
  233. __pipe_lock(pipe);
  234. for (;;) {
  235. int bufs = pipe->nrbufs;
  236. if (bufs) {
  237. int curbuf = pipe->curbuf;
  238. struct pipe_buffer *buf = pipe->bufs + curbuf;
  239. size_t chars = buf->len;
  240. size_t written;
  241. int error;
  242. if (chars > total_len)
  243. chars = total_len;
  244. error = pipe_buf_confirm(pipe, buf);
  245. if (error) {
  246. if (!ret)
  247. ret = error;
  248. break;
  249. }
  250. written = copy_page_to_iter(buf->page, buf->offset, chars, to);
  251. if (unlikely(written < chars)) {
  252. if (!ret)
  253. ret = -EFAULT;
  254. break;
  255. }
  256. ret += chars;
  257. buf->offset += chars;
  258. buf->len -= chars;
  259. /* Was it a packet buffer? Clean up and exit */
  260. if (buf->flags & PIPE_BUF_FLAG_PACKET) {
  261. total_len = chars;
  262. buf->len = 0;
  263. }
  264. if (!buf->len) {
  265. pipe_buf_release(pipe, buf);
  266. curbuf = (curbuf + 1) & (pipe->buffers - 1);
  267. pipe->curbuf = curbuf;
  268. pipe->nrbufs = --bufs;
  269. do_wakeup = 1;
  270. }
  271. total_len -= chars;
  272. if (!total_len)
  273. break; /* common path: read succeeded */
  274. }
  275. if (bufs) /* More to do? */
  276. continue;
  277. if (!pipe->writers)
  278. break;
  279. if (!pipe->waiting_writers) {
  280. /* syscall merging: Usually we must not sleep
  281. * if O_NONBLOCK is set, or if we got some data.
  282. * But if a writer sleeps in kernel space, then
  283. * we can wait for that data without violating POSIX.
  284. */
  285. if (ret)
  286. break;
  287. if (filp->f_flags & O_NONBLOCK) {
  288. ret = -EAGAIN;
  289. break;
  290. }
  291. }
  292. if (signal_pending(current)) {
  293. if (!ret)
  294. ret = -ERESTARTSYS;
  295. break;
  296. }
  297. if (do_wakeup) {
  298. wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
  299. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  300. }
  301. pipe_wait(pipe);
  302. }
  303. __pipe_unlock(pipe);
  304. /* Signal writers asynchronously that there is more room. */
  305. if (do_wakeup) {
  306. wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
  307. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  308. }
  309. if (ret > 0)
  310. file_accessed(filp);
  311. return ret;
  312. }
  313. static inline int is_packetized(struct file *file)
  314. {
  315. return (file->f_flags & O_DIRECT) != 0;
  316. }
  317. static ssize_t
  318. pipe_write(struct kiocb *iocb, struct iov_iter *from)
  319. {
  320. struct file *filp = iocb->ki_filp;
  321. struct pipe_inode_info *pipe = filp->private_data;
  322. ssize_t ret = 0;
  323. int do_wakeup = 0;
  324. size_t total_len = iov_iter_count(from);
  325. ssize_t chars;
  326. /* Null write succeeds. */
  327. if (unlikely(total_len == 0))
  328. return 0;
  329. __pipe_lock(pipe);
  330. if (!pipe->readers) {
  331. send_sig(SIGPIPE, current, 0);
  332. ret = -EPIPE;
  333. goto out;
  334. }
  335. /* We try to merge small writes */
  336. chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
  337. if (pipe->nrbufs && chars != 0) {
  338. int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
  339. (pipe->buffers - 1);
  340. struct pipe_buffer *buf = pipe->bufs + lastbuf;
  341. int offset = buf->offset + buf->len;
  342. if (buf->ops->can_merge && offset + chars <= PAGE_SIZE) {
  343. ret = pipe_buf_confirm(pipe, buf);
  344. if (ret)
  345. goto out;
  346. ret = copy_page_from_iter(buf->page, offset, chars, from);
  347. if (unlikely(ret < chars)) {
  348. ret = -EFAULT;
  349. goto out;
  350. }
  351. do_wakeup = 1;
  352. buf->len += ret;
  353. if (!iov_iter_count(from))
  354. goto out;
  355. }
  356. }
  357. for (;;) {
  358. int bufs;
  359. if (!pipe->readers) {
  360. send_sig(SIGPIPE, current, 0);
  361. if (!ret)
  362. ret = -EPIPE;
  363. break;
  364. }
  365. bufs = pipe->nrbufs;
  366. if (bufs < pipe->buffers) {
  367. int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1);
  368. struct pipe_buffer *buf = pipe->bufs + newbuf;
  369. struct page *page = pipe->tmp_page;
  370. int copied;
  371. if (!page) {
  372. page = alloc_page(GFP_HIGHUSER | __GFP_ACCOUNT);
  373. if (unlikely(!page)) {
  374. ret = ret ? : -ENOMEM;
  375. break;
  376. }
  377. pipe->tmp_page = page;
  378. }
  379. /* Always wake up, even if the copy fails. Otherwise
  380. * we lock up (O_NONBLOCK-)readers that sleep due to
  381. * syscall merging.
  382. * FIXME! Is this really true?
  383. */
  384. do_wakeup = 1;
  385. copied = copy_page_from_iter(page, 0, PAGE_SIZE, from);
  386. if (unlikely(copied < PAGE_SIZE && iov_iter_count(from))) {
  387. if (!ret)
  388. ret = -EFAULT;
  389. break;
  390. }
  391. ret += copied;
  392. /* Insert it into the buffer array */
  393. buf->page = page;
  394. buf->ops = &anon_pipe_buf_ops;
  395. buf->offset = 0;
  396. buf->len = copied;
  397. buf->flags = 0;
  398. if (is_packetized(filp)) {
  399. buf->ops = &packet_pipe_buf_ops;
  400. buf->flags = PIPE_BUF_FLAG_PACKET;
  401. }
  402. pipe->nrbufs = ++bufs;
  403. pipe->tmp_page = NULL;
  404. if (!iov_iter_count(from))
  405. break;
  406. }
  407. if (bufs < pipe->buffers)
  408. continue;
  409. if (filp->f_flags & O_NONBLOCK) {
  410. if (!ret)
  411. ret = -EAGAIN;
  412. break;
  413. }
  414. if (signal_pending(current)) {
  415. if (!ret)
  416. ret = -ERESTARTSYS;
  417. break;
  418. }
  419. if (do_wakeup) {
  420. wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
  421. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  422. do_wakeup = 0;
  423. }
  424. pipe->waiting_writers++;
  425. pipe_wait(pipe);
  426. pipe->waiting_writers--;
  427. }
  428. out:
  429. __pipe_unlock(pipe);
  430. if (do_wakeup) {
  431. wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
  432. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  433. }
  434. if (ret > 0 && sb_start_write_trylock(file_inode(filp)->i_sb)) {
  435. int err = file_update_time(filp);
  436. if (err)
  437. ret = err;
  438. sb_end_write(file_inode(filp)->i_sb);
  439. }
  440. return ret;
  441. }
  442. static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  443. {
  444. struct pipe_inode_info *pipe = filp->private_data;
  445. int count, buf, nrbufs;
  446. switch (cmd) {
  447. case FIONREAD:
  448. __pipe_lock(pipe);
  449. count = 0;
  450. buf = pipe->curbuf;
  451. nrbufs = pipe->nrbufs;
  452. while (--nrbufs >= 0) {
  453. count += pipe->bufs[buf].len;
  454. buf = (buf+1) & (pipe->buffers - 1);
  455. }
  456. __pipe_unlock(pipe);
  457. return put_user(count, (int __user *)arg);
  458. default:
  459. return -ENOIOCTLCMD;
  460. }
  461. }
  462. /* No kernel lock held - fine */
  463. static unsigned int
  464. pipe_poll(struct file *filp, poll_table *wait)
  465. {
  466. unsigned int mask;
  467. struct pipe_inode_info *pipe = filp->private_data;
  468. int nrbufs;
  469. poll_wait(filp, &pipe->wait, wait);
  470. /* Reading only -- no need for acquiring the semaphore. */
  471. nrbufs = pipe->nrbufs;
  472. mask = 0;
  473. if (filp->f_mode & FMODE_READ) {
  474. mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
  475. if (!pipe->writers && filp->f_version != pipe->w_counter)
  476. mask |= POLLHUP;
  477. }
  478. if (filp->f_mode & FMODE_WRITE) {
  479. mask |= (nrbufs < pipe->buffers) ? POLLOUT | POLLWRNORM : 0;
  480. /*
  481. * Most Unices do not set POLLERR for FIFOs but on Linux they
  482. * behave exactly like pipes for poll().
  483. */
  484. if (!pipe->readers)
  485. mask |= POLLERR;
  486. }
  487. return mask;
  488. }
  489. static void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe)
  490. {
  491. int kill = 0;
  492. spin_lock(&inode->i_lock);
  493. if (!--pipe->files) {
  494. inode->i_pipe = NULL;
  495. kill = 1;
  496. }
  497. spin_unlock(&inode->i_lock);
  498. if (kill)
  499. free_pipe_info(pipe);
  500. }
  501. static int
  502. pipe_release(struct inode *inode, struct file *file)
  503. {
  504. struct pipe_inode_info *pipe = file->private_data;
  505. __pipe_lock(pipe);
  506. if (file->f_mode & FMODE_READ)
  507. pipe->readers--;
  508. if (file->f_mode & FMODE_WRITE)
  509. pipe->writers--;
  510. if (pipe->readers || pipe->writers) {
  511. wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM | POLLERR | POLLHUP);
  512. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  513. kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
  514. }
  515. __pipe_unlock(pipe);
  516. put_pipe_info(inode, pipe);
  517. return 0;
  518. }
  519. static int
  520. pipe_fasync(int fd, struct file *filp, int on)
  521. {
  522. struct pipe_inode_info *pipe = filp->private_data;
  523. int retval = 0;
  524. __pipe_lock(pipe);
  525. if (filp->f_mode & FMODE_READ)
  526. retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
  527. if ((filp->f_mode & FMODE_WRITE) && retval >= 0) {
  528. retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
  529. if (retval < 0 && (filp->f_mode & FMODE_READ))
  530. /* this can happen only if on == T */
  531. fasync_helper(-1, filp, 0, &pipe->fasync_readers);
  532. }
  533. __pipe_unlock(pipe);
  534. return retval;
  535. }
  536. static unsigned long account_pipe_buffers(struct user_struct *user,
  537. unsigned long old, unsigned long new)
  538. {
  539. return atomic_long_add_return(new - old, &user->pipe_bufs);
  540. }
  541. static bool too_many_pipe_buffers_soft(unsigned long user_bufs)
  542. {
  543. return pipe_user_pages_soft && user_bufs >= pipe_user_pages_soft;
  544. }
  545. static bool too_many_pipe_buffers_hard(unsigned long user_bufs)
  546. {
  547. return pipe_user_pages_hard && user_bufs >= pipe_user_pages_hard;
  548. }
  549. struct pipe_inode_info *alloc_pipe_info(void)
  550. {
  551. struct pipe_inode_info *pipe;
  552. unsigned long pipe_bufs = PIPE_DEF_BUFFERS;
  553. struct user_struct *user = get_current_user();
  554. unsigned long user_bufs;
  555. pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL_ACCOUNT);
  556. if (pipe == NULL)
  557. goto out_free_uid;
  558. if (pipe_bufs * PAGE_SIZE > pipe_max_size && !capable(CAP_SYS_RESOURCE))
  559. pipe_bufs = pipe_max_size >> PAGE_SHIFT;
  560. user_bufs = account_pipe_buffers(user, 0, pipe_bufs);
  561. if (too_many_pipe_buffers_soft(user_bufs)) {
  562. user_bufs = account_pipe_buffers(user, pipe_bufs, 1);
  563. pipe_bufs = 1;
  564. }
  565. if (too_many_pipe_buffers_hard(user_bufs))
  566. goto out_revert_acct;
  567. pipe->bufs = kcalloc(pipe_bufs, sizeof(struct pipe_buffer),
  568. GFP_KERNEL_ACCOUNT);
  569. if (pipe->bufs) {
  570. init_waitqueue_head(&pipe->wait);
  571. pipe->r_counter = pipe->w_counter = 1;
  572. pipe->buffers = pipe_bufs;
  573. pipe->user = user;
  574. mutex_init(&pipe->mutex);
  575. return pipe;
  576. }
  577. out_revert_acct:
  578. (void) account_pipe_buffers(user, pipe_bufs, 0);
  579. kfree(pipe);
  580. out_free_uid:
  581. free_uid(user);
  582. return NULL;
  583. }
  584. void free_pipe_info(struct pipe_inode_info *pipe)
  585. {
  586. int i;
  587. (void) account_pipe_buffers(pipe->user, pipe->buffers, 0);
  588. free_uid(pipe->user);
  589. for (i = 0; i < pipe->buffers; i++) {
  590. struct pipe_buffer *buf = pipe->bufs + i;
  591. if (buf->ops)
  592. pipe_buf_release(pipe, buf);
  593. }
  594. if (pipe->tmp_page)
  595. __free_page(pipe->tmp_page);
  596. kfree(pipe->bufs);
  597. kfree(pipe);
  598. }
  599. static struct vfsmount *pipe_mnt __read_mostly;
  600. /*
  601. * pipefs_dname() is called from d_path().
  602. */
  603. static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
  604. {
  605. return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
  606. d_inode(dentry)->i_ino);
  607. }
  608. static const struct dentry_operations pipefs_dentry_operations = {
  609. .d_dname = pipefs_dname,
  610. };
  611. static struct inode * get_pipe_inode(void)
  612. {
  613. struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
  614. struct pipe_inode_info *pipe;
  615. if (!inode)
  616. goto fail_inode;
  617. inode->i_ino = get_next_ino();
  618. pipe = alloc_pipe_info();
  619. if (!pipe)
  620. goto fail_iput;
  621. inode->i_pipe = pipe;
  622. pipe->files = 2;
  623. pipe->readers = pipe->writers = 1;
  624. inode->i_fop = &pipefifo_fops;
  625. /*
  626. * Mark the inode dirty from the very beginning,
  627. * that way it will never be moved to the dirty
  628. * list because "mark_inode_dirty()" will think
  629. * that it already _is_ on the dirty list.
  630. */
  631. inode->i_state = I_DIRTY;
  632. inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
  633. inode->i_uid = current_fsuid();
  634. inode->i_gid = current_fsgid();
  635. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  636. return inode;
  637. fail_iput:
  638. iput(inode);
  639. fail_inode:
  640. return NULL;
  641. }
  642. int create_pipe_files(struct file **res, int flags)
  643. {
  644. int err;
  645. struct inode *inode = get_pipe_inode();
  646. struct file *f;
  647. struct path path;
  648. if (!inode)
  649. return -ENFILE;
  650. err = -ENOMEM;
  651. path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &empty_name);
  652. if (!path.dentry)
  653. goto err_inode;
  654. path.mnt = mntget(pipe_mnt);
  655. d_instantiate(path.dentry, inode);
  656. f = alloc_file(&path, FMODE_WRITE, &pipefifo_fops);
  657. if (IS_ERR(f)) {
  658. err = PTR_ERR(f);
  659. goto err_dentry;
  660. }
  661. f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
  662. f->private_data = inode->i_pipe;
  663. res[0] = alloc_file(&path, FMODE_READ, &pipefifo_fops);
  664. if (IS_ERR(res[0])) {
  665. err = PTR_ERR(res[0]);
  666. goto err_file;
  667. }
  668. path_get(&path);
  669. res[0]->private_data = inode->i_pipe;
  670. res[0]->f_flags = O_RDONLY | (flags & O_NONBLOCK);
  671. res[1] = f;
  672. return 0;
  673. err_file:
  674. put_filp(f);
  675. err_dentry:
  676. free_pipe_info(inode->i_pipe);
  677. path_put(&path);
  678. return err;
  679. err_inode:
  680. free_pipe_info(inode->i_pipe);
  681. iput(inode);
  682. return err;
  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. SYSCALL_DEFINE2(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_DEFINE1(pipe, int __user *, fildes)
  747. {
  748. return sys_pipe2(fildes, 0);
  749. }
  750. static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
  751. {
  752. int cur = *cnt;
  753. while (cur == *cnt) {
  754. pipe_wait(pipe);
  755. if (signal_pending(current))
  756. break;
  757. }
  758. return cur == *cnt ? -ERESTARTSYS : 0;
  759. }
  760. static void wake_up_partner(struct pipe_inode_info *pipe)
  761. {
  762. wake_up_interruptible(&pipe->wait);
  763. }
  764. static int fifo_open(struct inode *inode, struct file *filp)
  765. {
  766. struct pipe_inode_info *pipe;
  767. bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
  768. int ret;
  769. filp->f_version = 0;
  770. spin_lock(&inode->i_lock);
  771. if (inode->i_pipe) {
  772. pipe = inode->i_pipe;
  773. pipe->files++;
  774. spin_unlock(&inode->i_lock);
  775. } else {
  776. spin_unlock(&inode->i_lock);
  777. pipe = alloc_pipe_info();
  778. if (!pipe)
  779. return -ENOMEM;
  780. pipe->files = 1;
  781. spin_lock(&inode->i_lock);
  782. if (unlikely(inode->i_pipe)) {
  783. inode->i_pipe->files++;
  784. spin_unlock(&inode->i_lock);
  785. free_pipe_info(pipe);
  786. pipe = inode->i_pipe;
  787. } else {
  788. inode->i_pipe = pipe;
  789. spin_unlock(&inode->i_lock);
  790. }
  791. }
  792. filp->private_data = pipe;
  793. /* OK, we have a pipe and it's pinned down */
  794. __pipe_lock(pipe);
  795. /* We can only do regular read/write on fifos */
  796. filp->f_mode &= (FMODE_READ | FMODE_WRITE);
  797. switch (filp->f_mode) {
  798. case FMODE_READ:
  799. /*
  800. * O_RDONLY
  801. * POSIX.1 says that O_NONBLOCK means return with the FIFO
  802. * opened, even when there is no process writing the FIFO.
  803. */
  804. pipe->r_counter++;
  805. if (pipe->readers++ == 0)
  806. wake_up_partner(pipe);
  807. if (!is_pipe && !pipe->writers) {
  808. if ((filp->f_flags & O_NONBLOCK)) {
  809. /* suppress POLLHUP until we have
  810. * seen a writer */
  811. filp->f_version = pipe->w_counter;
  812. } else {
  813. if (wait_for_partner(pipe, &pipe->w_counter))
  814. goto err_rd;
  815. }
  816. }
  817. break;
  818. case FMODE_WRITE:
  819. /*
  820. * O_WRONLY
  821. * POSIX.1 says that O_NONBLOCK means return -1 with
  822. * errno=ENXIO when there is no process reading the FIFO.
  823. */
  824. ret = -ENXIO;
  825. if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
  826. goto err;
  827. pipe->w_counter++;
  828. if (!pipe->writers++)
  829. wake_up_partner(pipe);
  830. if (!is_pipe && !pipe->readers) {
  831. if (wait_for_partner(pipe, &pipe->r_counter))
  832. goto err_wr;
  833. }
  834. break;
  835. case FMODE_READ | FMODE_WRITE:
  836. /*
  837. * O_RDWR
  838. * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
  839. * This implementation will NEVER block on a O_RDWR open, since
  840. * the process can at least talk to itself.
  841. */
  842. pipe->readers++;
  843. pipe->writers++;
  844. pipe->r_counter++;
  845. pipe->w_counter++;
  846. if (pipe->readers == 1 || pipe->writers == 1)
  847. wake_up_partner(pipe);
  848. break;
  849. default:
  850. ret = -EINVAL;
  851. goto err;
  852. }
  853. /* Ok! */
  854. __pipe_unlock(pipe);
  855. return 0;
  856. err_rd:
  857. if (!--pipe->readers)
  858. wake_up_interruptible(&pipe->wait);
  859. ret = -ERESTARTSYS;
  860. goto err;
  861. err_wr:
  862. if (!--pipe->writers)
  863. wake_up_interruptible(&pipe->wait);
  864. ret = -ERESTARTSYS;
  865. goto err;
  866. err:
  867. __pipe_unlock(pipe);
  868. put_pipe_info(inode, pipe);
  869. return ret;
  870. }
  871. const struct file_operations pipefifo_fops = {
  872. .open = fifo_open,
  873. .llseek = no_llseek,
  874. .read_iter = pipe_read,
  875. .write_iter = pipe_write,
  876. .poll = pipe_poll,
  877. .unlocked_ioctl = pipe_ioctl,
  878. .release = pipe_release,
  879. .fasync = pipe_fasync,
  880. };
  881. /*
  882. * Currently we rely on the pipe array holding a power-of-2 number
  883. * of pages.
  884. */
  885. static inline unsigned int round_pipe_size(unsigned int size)
  886. {
  887. unsigned long nr_pages;
  888. nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  889. return roundup_pow_of_two(nr_pages) << PAGE_SHIFT;
  890. }
  891. /*
  892. * Allocate a new array of pipe buffers and copy the info over. Returns the
  893. * pipe size if successful, or return -ERROR on error.
  894. */
  895. static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long arg)
  896. {
  897. struct pipe_buffer *bufs;
  898. unsigned int size, nr_pages;
  899. unsigned long user_bufs;
  900. long ret = 0;
  901. size = round_pipe_size(arg);
  902. nr_pages = size >> PAGE_SHIFT;
  903. if (!nr_pages)
  904. return -EINVAL;
  905. /*
  906. * If trying to increase the pipe capacity, check that an
  907. * unprivileged user is not trying to exceed various limits
  908. * (soft limit check here, hard limit check just below).
  909. * Decreasing the pipe capacity is always permitted, even
  910. * if the user is currently over a limit.
  911. */
  912. if (nr_pages > pipe->buffers &&
  913. size > pipe_max_size && !capable(CAP_SYS_RESOURCE))
  914. return -EPERM;
  915. user_bufs = account_pipe_buffers(pipe->user, pipe->buffers, nr_pages);
  916. if (nr_pages > pipe->buffers &&
  917. (too_many_pipe_buffers_hard(user_bufs) ||
  918. too_many_pipe_buffers_soft(user_bufs)) &&
  919. !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
  920. ret = -EPERM;
  921. goto out_revert_acct;
  922. }
  923. /*
  924. * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
  925. * expect a lot of shrink+grow operations, just free and allocate
  926. * again like we would do for growing. If the pipe currently
  927. * contains more buffers than arg, then return busy.
  928. */
  929. if (nr_pages < pipe->nrbufs) {
  930. ret = -EBUSY;
  931. goto out_revert_acct;
  932. }
  933. bufs = kcalloc(nr_pages, sizeof(*bufs),
  934. GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
  935. if (unlikely(!bufs)) {
  936. ret = -ENOMEM;
  937. goto out_revert_acct;
  938. }
  939. /*
  940. * The pipe array wraps around, so just start the new one at zero
  941. * and adjust the indexes.
  942. */
  943. if (pipe->nrbufs) {
  944. unsigned int tail;
  945. unsigned int head;
  946. tail = pipe->curbuf + pipe->nrbufs;
  947. if (tail < pipe->buffers)
  948. tail = 0;
  949. else
  950. tail &= (pipe->buffers - 1);
  951. head = pipe->nrbufs - tail;
  952. if (head)
  953. memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
  954. if (tail)
  955. memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
  956. }
  957. pipe->curbuf = 0;
  958. kfree(pipe->bufs);
  959. pipe->bufs = bufs;
  960. pipe->buffers = nr_pages;
  961. return nr_pages * PAGE_SIZE;
  962. out_revert_acct:
  963. (void) account_pipe_buffers(pipe->user, nr_pages, pipe->buffers);
  964. return ret;
  965. }
  966. /*
  967. * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax
  968. * will return an error.
  969. */
  970. int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf,
  971. size_t *lenp, loff_t *ppos)
  972. {
  973. int ret;
  974. ret = proc_dointvec_minmax(table, write, buf, lenp, ppos);
  975. if (ret < 0 || !write)
  976. return ret;
  977. pipe_max_size = round_pipe_size(pipe_max_size);
  978. return ret;
  979. }
  980. /*
  981. * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
  982. * location, so checking ->i_pipe is not enough to verify that this is a
  983. * pipe.
  984. */
  985. struct pipe_inode_info *get_pipe_info(struct file *file)
  986. {
  987. return file->f_op == &pipefifo_fops ? file->private_data : NULL;
  988. }
  989. long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
  990. {
  991. struct pipe_inode_info *pipe;
  992. long ret;
  993. pipe = get_pipe_info(file);
  994. if (!pipe)
  995. return -EBADF;
  996. __pipe_lock(pipe);
  997. switch (cmd) {
  998. case F_SETPIPE_SZ:
  999. ret = pipe_set_size(pipe, arg);
  1000. break;
  1001. case F_GETPIPE_SZ:
  1002. ret = pipe->buffers * PAGE_SIZE;
  1003. break;
  1004. default:
  1005. ret = -EINVAL;
  1006. break;
  1007. }
  1008. __pipe_unlock(pipe);
  1009. return ret;
  1010. }
  1011. static const struct super_operations pipefs_ops = {
  1012. .destroy_inode = free_inode_nonrcu,
  1013. .statfs = simple_statfs,
  1014. };
  1015. /*
  1016. * pipefs should _never_ be mounted by userland - too much of security hassle,
  1017. * no real gain from having the whole whorehouse mounted. So we don't need
  1018. * any operations on the root directory. However, we need a non-trivial
  1019. * d_name - pipe: will go nicely and kill the special-casing in procfs.
  1020. */
  1021. static struct dentry *pipefs_mount(struct file_system_type *fs_type,
  1022. int flags, const char *dev_name, void *data)
  1023. {
  1024. return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
  1025. &pipefs_dentry_operations, PIPEFS_MAGIC);
  1026. }
  1027. static struct file_system_type pipe_fs_type = {
  1028. .name = "pipefs",
  1029. .mount = pipefs_mount,
  1030. .kill_sb = kill_anon_super,
  1031. };
  1032. static int __init init_pipe_fs(void)
  1033. {
  1034. int err = register_filesystem(&pipe_fs_type);
  1035. if (!err) {
  1036. pipe_mnt = kern_mount(&pipe_fs_type);
  1037. if (IS_ERR(pipe_mnt)) {
  1038. err = PTR_ERR(pipe_mnt);
  1039. unregister_filesystem(&pipe_fs_type);
  1040. }
  1041. }
  1042. return err;
  1043. }
  1044. fs_initcall(init_pipe_fs);