pipe.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  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. struct pipe_inode_info *alloc_pipe_info(void)
  550. {
  551. struct pipe_inode_info *pipe;
  552. unsigned long pipe_bufs = PIPE_DEF_BUFFERS;
  553. struct user_struct *user = get_current_user();
  554. unsigned long user_bufs;
  555. pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL_ACCOUNT);
  556. if (pipe == NULL)
  557. goto out_free_uid;
  558. if (pipe_bufs * PAGE_SIZE > pipe_max_size && !capable(CAP_SYS_RESOURCE))
  559. pipe_bufs = pipe_max_size >> PAGE_SHIFT;
  560. user_bufs = account_pipe_buffers(user, 0, pipe_bufs);
  561. if (too_many_pipe_buffers_soft(user_bufs)) {
  562. user_bufs = account_pipe_buffers(user, pipe_bufs, 1);
  563. pipe_bufs = 1;
  564. }
  565. if (too_many_pipe_buffers_hard(user_bufs))
  566. goto out_revert_acct;
  567. pipe->bufs = kcalloc(pipe_bufs, sizeof(struct pipe_buffer),
  568. GFP_KERNEL_ACCOUNT);
  569. if (pipe->bufs) {
  570. init_waitqueue_head(&pipe->wait);
  571. pipe->r_counter = pipe->w_counter = 1;
  572. pipe->buffers = pipe_bufs;
  573. pipe->user = user;
  574. mutex_init(&pipe->mutex);
  575. return pipe;
  576. }
  577. out_revert_acct:
  578. (void) account_pipe_buffers(user, pipe_bufs, 0);
  579. kfree(pipe);
  580. out_free_uid:
  581. free_uid(user);
  582. return NULL;
  583. }
  584. void free_pipe_info(struct pipe_inode_info *pipe)
  585. {
  586. int i;
  587. (void) account_pipe_buffers(pipe->user, pipe->buffers, 0);
  588. free_uid(pipe->user);
  589. for (i = 0; i < pipe->buffers; i++) {
  590. struct pipe_buffer *buf = pipe->bufs + i;
  591. if (buf->ops)
  592. pipe_buf_release(pipe, buf);
  593. }
  594. if (pipe->tmp_page)
  595. __free_page(pipe->tmp_page);
  596. kfree(pipe->bufs);
  597. kfree(pipe);
  598. }
  599. static struct vfsmount *pipe_mnt __read_mostly;
  600. /*
  601. * pipefs_dname() is called from d_path().
  602. */
  603. static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
  604. {
  605. return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
  606. d_inode(dentry)->i_ino);
  607. }
  608. static const struct dentry_operations pipefs_dentry_operations = {
  609. .d_dname = pipefs_dname,
  610. };
  611. static struct inode * get_pipe_inode(void)
  612. {
  613. struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
  614. struct pipe_inode_info *pipe;
  615. if (!inode)
  616. goto fail_inode;
  617. inode->i_ino = get_next_ino();
  618. pipe = alloc_pipe_info();
  619. if (!pipe)
  620. goto fail_iput;
  621. inode->i_pipe = pipe;
  622. pipe->files = 2;
  623. pipe->readers = pipe->writers = 1;
  624. inode->i_fop = &pipefifo_fops;
  625. /*
  626. * Mark the inode dirty from the very beginning,
  627. * that way it will never be moved to the dirty
  628. * list because "mark_inode_dirty()" will think
  629. * that it already _is_ on the dirty list.
  630. */
  631. inode->i_state = I_DIRTY;
  632. inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
  633. inode->i_uid = current_fsuid();
  634. inode->i_gid = current_fsgid();
  635. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  636. return inode;
  637. fail_iput:
  638. iput(inode);
  639. fail_inode:
  640. return NULL;
  641. }
  642. int create_pipe_files(struct file **res, int flags)
  643. {
  644. int err;
  645. struct inode *inode = get_pipe_inode();
  646. struct file *f;
  647. struct path path;
  648. 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. * Currently we rely on the pipe array holding a power-of-2 number
  884. * of pages.
  885. */
  886. static inline unsigned int round_pipe_size(unsigned int size)
  887. {
  888. unsigned long nr_pages;
  889. nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  890. return roundup_pow_of_two(nr_pages) << PAGE_SHIFT;
  891. }
  892. /*
  893. * Allocate a new array of pipe buffers and copy the info over. Returns the
  894. * pipe size if successful, or return -ERROR on error.
  895. */
  896. static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long arg)
  897. {
  898. struct pipe_buffer *bufs;
  899. unsigned int size, nr_pages;
  900. unsigned long user_bufs;
  901. long ret = 0;
  902. size = round_pipe_size(arg);
  903. nr_pages = size >> PAGE_SHIFT;
  904. if (!nr_pages)
  905. return -EINVAL;
  906. /*
  907. * If trying to increase the pipe capacity, check that an
  908. * unprivileged user is not trying to exceed various limits
  909. * (soft limit check here, hard limit check just below).
  910. * Decreasing the pipe capacity is always permitted, even
  911. * if the user is currently over a limit.
  912. */
  913. if (nr_pages > pipe->buffers &&
  914. size > pipe_max_size && !capable(CAP_SYS_RESOURCE))
  915. return -EPERM;
  916. user_bufs = account_pipe_buffers(pipe->user, pipe->buffers, nr_pages);
  917. if (nr_pages > pipe->buffers &&
  918. (too_many_pipe_buffers_hard(user_bufs) ||
  919. too_many_pipe_buffers_soft(user_bufs)) &&
  920. !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
  921. ret = -EPERM;
  922. goto out_revert_acct;
  923. }
  924. /*
  925. * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
  926. * expect a lot of shrink+grow operations, just free and allocate
  927. * again like we would do for growing. If the pipe currently
  928. * contains more buffers than arg, then return busy.
  929. */
  930. if (nr_pages < pipe->nrbufs) {
  931. ret = -EBUSY;
  932. goto out_revert_acct;
  933. }
  934. bufs = kcalloc(nr_pages, sizeof(*bufs),
  935. GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
  936. if (unlikely(!bufs)) {
  937. ret = -ENOMEM;
  938. goto out_revert_acct;
  939. }
  940. /*
  941. * The pipe array wraps around, so just start the new one at zero
  942. * and adjust the indexes.
  943. */
  944. if (pipe->nrbufs) {
  945. unsigned int tail;
  946. unsigned int head;
  947. tail = pipe->curbuf + pipe->nrbufs;
  948. if (tail < pipe->buffers)
  949. tail = 0;
  950. else
  951. tail &= (pipe->buffers - 1);
  952. head = pipe->nrbufs - tail;
  953. if (head)
  954. memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
  955. if (tail)
  956. memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
  957. }
  958. pipe->curbuf = 0;
  959. kfree(pipe->bufs);
  960. pipe->bufs = bufs;
  961. pipe->buffers = nr_pages;
  962. return nr_pages * PAGE_SIZE;
  963. out_revert_acct:
  964. (void) account_pipe_buffers(pipe->user, nr_pages, pipe->buffers);
  965. return ret;
  966. }
  967. /*
  968. * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax
  969. * will return an error.
  970. */
  971. int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf,
  972. size_t *lenp, loff_t *ppos)
  973. {
  974. int ret;
  975. ret = proc_dointvec_minmax(table, write, buf, lenp, ppos);
  976. if (ret < 0 || !write)
  977. return ret;
  978. pipe_max_size = round_pipe_size(pipe_max_size);
  979. return ret;
  980. }
  981. /*
  982. * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
  983. * location, so checking ->i_pipe is not enough to verify that this is a
  984. * pipe.
  985. */
  986. struct pipe_inode_info *get_pipe_info(struct file *file)
  987. {
  988. return file->f_op == &pipefifo_fops ? file->private_data : NULL;
  989. }
  990. long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
  991. {
  992. struct pipe_inode_info *pipe;
  993. long ret;
  994. pipe = get_pipe_info(file);
  995. if (!pipe)
  996. return -EBADF;
  997. __pipe_lock(pipe);
  998. switch (cmd) {
  999. case F_SETPIPE_SZ:
  1000. ret = pipe_set_size(pipe, arg);
  1001. break;
  1002. case F_GETPIPE_SZ:
  1003. ret = pipe->buffers * PAGE_SIZE;
  1004. break;
  1005. default:
  1006. ret = -EINVAL;
  1007. break;
  1008. }
  1009. __pipe_unlock(pipe);
  1010. return ret;
  1011. }
  1012. static const struct super_operations pipefs_ops = {
  1013. .destroy_inode = free_inode_nonrcu,
  1014. .statfs = simple_statfs,
  1015. };
  1016. /*
  1017. * pipefs should _never_ be mounted by userland - too much of security hassle,
  1018. * no real gain from having the whole whorehouse mounted. So we don't need
  1019. * any operations on the root directory. However, we need a non-trivial
  1020. * d_name - pipe: will go nicely and kill the special-casing in procfs.
  1021. */
  1022. static struct dentry *pipefs_mount(struct file_system_type *fs_type,
  1023. int flags, const char *dev_name, void *data)
  1024. {
  1025. return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
  1026. &pipefs_dentry_operations, PIPEFS_MAGIC);
  1027. }
  1028. static struct file_system_type pipe_fs_type = {
  1029. .name = "pipefs",
  1030. .mount = pipefs_mount,
  1031. .kill_sb = kill_anon_super,
  1032. };
  1033. static int __init init_pipe_fs(void)
  1034. {
  1035. int err = register_filesystem(&pipe_fs_type);
  1036. if (!err) {
  1037. pipe_mnt = kern_mount(&pipe_fs_type);
  1038. if (IS_ERR(pipe_mnt)) {
  1039. err = PTR_ERR(pipe_mnt);
  1040. unregister_filesystem(&pipe_fs_type);
  1041. }
  1042. }
  1043. return err;
  1044. }
  1045. fs_initcall(init_pipe_fs);