pipe.c 28 KB

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