pipe.c 27 KB

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