pipe.c 27 KB

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