pipe.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  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 <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 void account_pipe_buffers(struct pipe_inode_info *pipe,
  537. unsigned long old, unsigned long new)
  538. {
  539. atomic_long_add(new - old, &pipe->user->pipe_bufs);
  540. }
  541. static bool too_many_pipe_buffers_soft(struct user_struct *user)
  542. {
  543. return pipe_user_pages_soft &&
  544. atomic_long_read(&user->pipe_bufs) >= pipe_user_pages_soft;
  545. }
  546. static bool too_many_pipe_buffers_hard(struct user_struct *user)
  547. {
  548. return pipe_user_pages_hard &&
  549. atomic_long_read(&user->pipe_bufs) >= pipe_user_pages_hard;
  550. }
  551. struct pipe_inode_info *alloc_pipe_info(void)
  552. {
  553. struct pipe_inode_info *pipe;
  554. pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL_ACCOUNT);
  555. if (pipe) {
  556. unsigned long pipe_bufs = PIPE_DEF_BUFFERS;
  557. struct user_struct *user = get_current_user();
  558. if (!too_many_pipe_buffers_hard(user)) {
  559. if (too_many_pipe_buffers_soft(user))
  560. pipe_bufs = 1;
  561. pipe->bufs = kcalloc(pipe_bufs,
  562. sizeof(struct pipe_buffer),
  563. GFP_KERNEL_ACCOUNT);
  564. }
  565. if (pipe->bufs) {
  566. init_waitqueue_head(&pipe->wait);
  567. pipe->r_counter = pipe->w_counter = 1;
  568. pipe->buffers = pipe_bufs;
  569. pipe->user = user;
  570. account_pipe_buffers(pipe, 0, pipe_bufs);
  571. mutex_init(&pipe->mutex);
  572. return pipe;
  573. }
  574. free_uid(user);
  575. kfree(pipe);
  576. }
  577. return NULL;
  578. }
  579. void free_pipe_info(struct pipe_inode_info *pipe)
  580. {
  581. int i;
  582. account_pipe_buffers(pipe, pipe->buffers, 0);
  583. free_uid(pipe->user);
  584. for (i = 0; i < pipe->buffers; i++) {
  585. struct pipe_buffer *buf = pipe->bufs + i;
  586. if (buf->ops)
  587. pipe_buf_release(pipe, buf);
  588. }
  589. if (pipe->tmp_page)
  590. __free_page(pipe->tmp_page);
  591. kfree(pipe->bufs);
  592. kfree(pipe);
  593. }
  594. static struct vfsmount *pipe_mnt __read_mostly;
  595. /*
  596. * pipefs_dname() is called from d_path().
  597. */
  598. static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
  599. {
  600. return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
  601. d_inode(dentry)->i_ino);
  602. }
  603. static const struct dentry_operations pipefs_dentry_operations = {
  604. .d_dname = pipefs_dname,
  605. };
  606. static struct inode * get_pipe_inode(void)
  607. {
  608. struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
  609. struct pipe_inode_info *pipe;
  610. if (!inode)
  611. goto fail_inode;
  612. inode->i_ino = get_next_ino();
  613. pipe = alloc_pipe_info();
  614. if (!pipe)
  615. goto fail_iput;
  616. inode->i_pipe = pipe;
  617. pipe->files = 2;
  618. pipe->readers = pipe->writers = 1;
  619. inode->i_fop = &pipefifo_fops;
  620. /*
  621. * Mark the inode dirty from the very beginning,
  622. * that way it will never be moved to the dirty
  623. * list because "mark_inode_dirty()" will think
  624. * that it already _is_ on the dirty list.
  625. */
  626. inode->i_state = I_DIRTY;
  627. inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
  628. inode->i_uid = current_fsuid();
  629. inode->i_gid = current_fsgid();
  630. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  631. return inode;
  632. fail_iput:
  633. iput(inode);
  634. fail_inode:
  635. return NULL;
  636. }
  637. int create_pipe_files(struct file **res, int flags)
  638. {
  639. int err;
  640. struct inode *inode = get_pipe_inode();
  641. struct file *f;
  642. struct path path;
  643. static struct qstr name = { .name = "" };
  644. if (!inode)
  645. return -ENFILE;
  646. err = -ENOMEM;
  647. path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &name);
  648. if (!path.dentry)
  649. goto err_inode;
  650. path.mnt = mntget(pipe_mnt);
  651. d_instantiate(path.dentry, inode);
  652. f = alloc_file(&path, FMODE_WRITE, &pipefifo_fops);
  653. if (IS_ERR(f)) {
  654. err = PTR_ERR(f);
  655. goto err_dentry;
  656. }
  657. f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
  658. f->private_data = inode->i_pipe;
  659. res[0] = alloc_file(&path, FMODE_READ, &pipefifo_fops);
  660. if (IS_ERR(res[0])) {
  661. err = PTR_ERR(res[0]);
  662. goto err_file;
  663. }
  664. path_get(&path);
  665. res[0]->private_data = inode->i_pipe;
  666. res[0]->f_flags = O_RDONLY | (flags & O_NONBLOCK);
  667. res[1] = f;
  668. return 0;
  669. err_file:
  670. put_filp(f);
  671. err_dentry:
  672. free_pipe_info(inode->i_pipe);
  673. path_put(&path);
  674. return err;
  675. err_inode:
  676. free_pipe_info(inode->i_pipe);
  677. iput(inode);
  678. return err;
  679. }
  680. static int __do_pipe_flags(int *fd, struct file **files, int flags)
  681. {
  682. int error;
  683. int fdw, fdr;
  684. if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
  685. return -EINVAL;
  686. error = create_pipe_files(files, flags);
  687. if (error)
  688. return error;
  689. error = get_unused_fd_flags(flags);
  690. if (error < 0)
  691. goto err_read_pipe;
  692. fdr = error;
  693. error = get_unused_fd_flags(flags);
  694. if (error < 0)
  695. goto err_fdr;
  696. fdw = error;
  697. audit_fd_pair(fdr, fdw);
  698. fd[0] = fdr;
  699. fd[1] = fdw;
  700. return 0;
  701. err_fdr:
  702. put_unused_fd(fdr);
  703. err_read_pipe:
  704. fput(files[0]);
  705. fput(files[1]);
  706. return error;
  707. }
  708. int do_pipe_flags(int *fd, int flags)
  709. {
  710. struct file *files[2];
  711. int error = __do_pipe_flags(fd, files, flags);
  712. if (!error) {
  713. fd_install(fd[0], files[0]);
  714. fd_install(fd[1], files[1]);
  715. }
  716. return error;
  717. }
  718. /*
  719. * sys_pipe() is the normal C calling standard for creating
  720. * a pipe. It's not the way Unix traditionally does this, though.
  721. */
  722. SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
  723. {
  724. struct file *files[2];
  725. int fd[2];
  726. int error;
  727. error = __do_pipe_flags(fd, files, flags);
  728. if (!error) {
  729. if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
  730. fput(files[0]);
  731. fput(files[1]);
  732. put_unused_fd(fd[0]);
  733. put_unused_fd(fd[1]);
  734. error = -EFAULT;
  735. } else {
  736. fd_install(fd[0], files[0]);
  737. fd_install(fd[1], files[1]);
  738. }
  739. }
  740. return error;
  741. }
  742. SYSCALL_DEFINE1(pipe, int __user *, fildes)
  743. {
  744. return sys_pipe2(fildes, 0);
  745. }
  746. static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
  747. {
  748. int cur = *cnt;
  749. while (cur == *cnt) {
  750. pipe_wait(pipe);
  751. if (signal_pending(current))
  752. break;
  753. }
  754. return cur == *cnt ? -ERESTARTSYS : 0;
  755. }
  756. static void wake_up_partner(struct pipe_inode_info *pipe)
  757. {
  758. wake_up_interruptible(&pipe->wait);
  759. }
  760. static int fifo_open(struct inode *inode, struct file *filp)
  761. {
  762. struct pipe_inode_info *pipe;
  763. bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
  764. int ret;
  765. filp->f_version = 0;
  766. spin_lock(&inode->i_lock);
  767. if (inode->i_pipe) {
  768. pipe = inode->i_pipe;
  769. pipe->files++;
  770. spin_unlock(&inode->i_lock);
  771. } else {
  772. spin_unlock(&inode->i_lock);
  773. pipe = alloc_pipe_info();
  774. if (!pipe)
  775. return -ENOMEM;
  776. pipe->files = 1;
  777. spin_lock(&inode->i_lock);
  778. if (unlikely(inode->i_pipe)) {
  779. inode->i_pipe->files++;
  780. spin_unlock(&inode->i_lock);
  781. free_pipe_info(pipe);
  782. pipe = inode->i_pipe;
  783. } else {
  784. inode->i_pipe = pipe;
  785. spin_unlock(&inode->i_lock);
  786. }
  787. }
  788. filp->private_data = pipe;
  789. /* OK, we have a pipe and it's pinned down */
  790. __pipe_lock(pipe);
  791. /* We can only do regular read/write on fifos */
  792. filp->f_mode &= (FMODE_READ | FMODE_WRITE);
  793. switch (filp->f_mode) {
  794. case FMODE_READ:
  795. /*
  796. * O_RDONLY
  797. * POSIX.1 says that O_NONBLOCK means return with the FIFO
  798. * opened, even when there is no process writing the FIFO.
  799. */
  800. pipe->r_counter++;
  801. if (pipe->readers++ == 0)
  802. wake_up_partner(pipe);
  803. if (!is_pipe && !pipe->writers) {
  804. if ((filp->f_flags & O_NONBLOCK)) {
  805. /* suppress POLLHUP until we have
  806. * seen a writer */
  807. filp->f_version = pipe->w_counter;
  808. } else {
  809. if (wait_for_partner(pipe, &pipe->w_counter))
  810. goto err_rd;
  811. }
  812. }
  813. break;
  814. case FMODE_WRITE:
  815. /*
  816. * O_WRONLY
  817. * POSIX.1 says that O_NONBLOCK means return -1 with
  818. * errno=ENXIO when there is no process reading the FIFO.
  819. */
  820. ret = -ENXIO;
  821. if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
  822. goto err;
  823. pipe->w_counter++;
  824. if (!pipe->writers++)
  825. wake_up_partner(pipe);
  826. if (!is_pipe && !pipe->readers) {
  827. if (wait_for_partner(pipe, &pipe->r_counter))
  828. goto err_wr;
  829. }
  830. break;
  831. case FMODE_READ | FMODE_WRITE:
  832. /*
  833. * O_RDWR
  834. * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
  835. * This implementation will NEVER block on a O_RDWR open, since
  836. * the process can at least talk to itself.
  837. */
  838. pipe->readers++;
  839. pipe->writers++;
  840. pipe->r_counter++;
  841. pipe->w_counter++;
  842. if (pipe->readers == 1 || pipe->writers == 1)
  843. wake_up_partner(pipe);
  844. break;
  845. default:
  846. ret = -EINVAL;
  847. goto err;
  848. }
  849. /* Ok! */
  850. __pipe_unlock(pipe);
  851. return 0;
  852. err_rd:
  853. if (!--pipe->readers)
  854. wake_up_interruptible(&pipe->wait);
  855. ret = -ERESTARTSYS;
  856. goto err;
  857. err_wr:
  858. if (!--pipe->writers)
  859. wake_up_interruptible(&pipe->wait);
  860. ret = -ERESTARTSYS;
  861. goto err;
  862. err:
  863. __pipe_unlock(pipe);
  864. put_pipe_info(inode, pipe);
  865. return ret;
  866. }
  867. const struct file_operations pipefifo_fops = {
  868. .open = fifo_open,
  869. .llseek = no_llseek,
  870. .read_iter = pipe_read,
  871. .write_iter = pipe_write,
  872. .poll = pipe_poll,
  873. .unlocked_ioctl = pipe_ioctl,
  874. .release = pipe_release,
  875. .fasync = pipe_fasync,
  876. };
  877. /*
  878. * Allocate a new array of pipe buffers and copy the info over. Returns the
  879. * pipe size if successful, or return -ERROR on error.
  880. */
  881. static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages)
  882. {
  883. struct pipe_buffer *bufs;
  884. /*
  885. * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
  886. * expect a lot of shrink+grow operations, just free and allocate
  887. * again like we would do for growing. If the pipe currently
  888. * contains more buffers than arg, then return busy.
  889. */
  890. if (nr_pages < pipe->nrbufs)
  891. return -EBUSY;
  892. bufs = kcalloc(nr_pages, sizeof(*bufs),
  893. GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
  894. if (unlikely(!bufs))
  895. return -ENOMEM;
  896. /*
  897. * The pipe array wraps around, so just start the new one at zero
  898. * and adjust the indexes.
  899. */
  900. if (pipe->nrbufs) {
  901. unsigned int tail;
  902. unsigned int head;
  903. tail = pipe->curbuf + pipe->nrbufs;
  904. if (tail < pipe->buffers)
  905. tail = 0;
  906. else
  907. tail &= (pipe->buffers - 1);
  908. head = pipe->nrbufs - tail;
  909. if (head)
  910. memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
  911. if (tail)
  912. memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
  913. }
  914. account_pipe_buffers(pipe, pipe->buffers, nr_pages);
  915. pipe->curbuf = 0;
  916. kfree(pipe->bufs);
  917. pipe->bufs = bufs;
  918. pipe->buffers = nr_pages;
  919. return nr_pages * PAGE_SIZE;
  920. }
  921. /*
  922. * Currently we rely on the pipe array holding a power-of-2 number
  923. * of pages.
  924. */
  925. static inline unsigned int round_pipe_size(unsigned int size)
  926. {
  927. unsigned long nr_pages;
  928. nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  929. return roundup_pow_of_two(nr_pages) << PAGE_SHIFT;
  930. }
  931. /*
  932. * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax
  933. * will return an error.
  934. */
  935. int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf,
  936. size_t *lenp, loff_t *ppos)
  937. {
  938. int ret;
  939. ret = proc_dointvec_minmax(table, write, buf, lenp, ppos);
  940. if (ret < 0 || !write)
  941. return ret;
  942. pipe_max_size = round_pipe_size(pipe_max_size);
  943. return ret;
  944. }
  945. /*
  946. * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
  947. * location, so checking ->i_pipe is not enough to verify that this is a
  948. * pipe.
  949. */
  950. struct pipe_inode_info *get_pipe_info(struct file *file)
  951. {
  952. return file->f_op == &pipefifo_fops ? file->private_data : NULL;
  953. }
  954. long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
  955. {
  956. struct pipe_inode_info *pipe;
  957. long ret;
  958. pipe = get_pipe_info(file);
  959. if (!pipe)
  960. return -EBADF;
  961. __pipe_lock(pipe);
  962. switch (cmd) {
  963. case F_SETPIPE_SZ: {
  964. unsigned int size, nr_pages;
  965. size = round_pipe_size(arg);
  966. nr_pages = size >> PAGE_SHIFT;
  967. ret = -EINVAL;
  968. if (!nr_pages)
  969. goto out;
  970. if (!capable(CAP_SYS_RESOURCE) && size > pipe_max_size) {
  971. ret = -EPERM;
  972. goto out;
  973. } else if ((too_many_pipe_buffers_hard(pipe->user) ||
  974. too_many_pipe_buffers_soft(pipe->user)) &&
  975. !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
  976. ret = -EPERM;
  977. goto out;
  978. }
  979. ret = pipe_set_size(pipe, nr_pages);
  980. break;
  981. }
  982. case F_GETPIPE_SZ:
  983. ret = pipe->buffers * PAGE_SIZE;
  984. break;
  985. default:
  986. ret = -EINVAL;
  987. break;
  988. }
  989. out:
  990. __pipe_unlock(pipe);
  991. return ret;
  992. }
  993. static const struct super_operations pipefs_ops = {
  994. .destroy_inode = free_inode_nonrcu,
  995. .statfs = simple_statfs,
  996. };
  997. /*
  998. * pipefs should _never_ be mounted by userland - too much of security hassle,
  999. * no real gain from having the whole whorehouse mounted. So we don't need
  1000. * any operations on the root directory. However, we need a non-trivial
  1001. * d_name - pipe: will go nicely and kill the special-casing in procfs.
  1002. */
  1003. static struct dentry *pipefs_mount(struct file_system_type *fs_type,
  1004. int flags, const char *dev_name, void *data)
  1005. {
  1006. return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
  1007. &pipefs_dentry_operations, PIPEFS_MAGIC);
  1008. }
  1009. static struct file_system_type pipe_fs_type = {
  1010. .name = "pipefs",
  1011. .mount = pipefs_mount,
  1012. .kill_sb = kill_anon_super,
  1013. };
  1014. static int __init init_pipe_fs(void)
  1015. {
  1016. int err = register_filesystem(&pipe_fs_type);
  1017. if (!err) {
  1018. pipe_mnt = kern_mount(&pipe_fs_type);
  1019. if (IS_ERR(pipe_mnt)) {
  1020. err = PTR_ERR(pipe_mnt);
  1021. unregister_filesystem(&pipe_fs_type);
  1022. }
  1023. }
  1024. return err;
  1025. }
  1026. fs_initcall(init_pipe_fs);