2 * Copyright (c) 1996 John S. Dyson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice immediately at the beginning of the file, without modification,
10 * this list of conditions, and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Absolutely no warranty of function or purpose is made by the author
16 * 4. Modifications may be freely made to this file if the above conditions
21 * This file contains a high-performance replacement for the socket-based
22 * pipes scheme originally used in FreeBSD/4.4Lite. It does not support
23 * all features of sockets, but does do everything that pipes normally
28 * This code has two modes of operation, a small write mode and a large
29 * write mode. The small write mode acts like conventional pipes with
30 * a kernel buffer. If the buffer is less than PIPE_MINDIRECT, then the
31 * "normal" pipe buffering is done. If the buffer is between PIPE_MINDIRECT
32 * and PIPE_SIZE in size, it is fully mapped and wired into the kernel, and
33 * the receiving process can copy it directly from the pages in the sending
36 * If the sending process receives a signal, it is possible that it will
37 * go away, and certainly its address space can change, because control
38 * is returned back to the user-mode side. In that case, the pipe code
39 * arranges to copy the buffer supplied by the user process, to a pageable
40 * kernel buffer, and the receiving process will grab the data from the
41 * pageable kernel buffer. Since signals don't happen all that often,
42 * the copy operation is normally eliminated.
44 * The constant PIPE_MINDIRECT is chosen to make sure that buffering will
45 * happen for small transfers so that the system will not spend all of
46 * its time context switching.
48 * In order to limit the resource use of pipes, two sysctls exist:
50 * kern.ipc.maxpipekva - This is a hard limit on the amount of pageable
51 * address space available to us in pipe_map. This value is normally
52 * autotuned, but may also be loader tuned.
54 * kern.ipc.pipekva - This read-only sysctl tracks the current amount of
55 * memory in use by pipes.
57 * Based on how large pipekva is relative to maxpipekva, the following
61 * New pipes are given 16K of memory backing, pipes may dynamically
62 * grow to as large as 64K where needed.
64 * New pipes are given 4K (or PAGE_SIZE) of memory backing,
65 * existing pipes may NOT grow.
67 * New pipes are given 4K (or PAGE_SIZE) of memory backing,
68 * existing pipes will be shrunk down to 4K whenever possible.
70 * Resizing may be disabled by setting kern.ipc.piperesizeallowed=0. If
71 * that is set, the only resize that will occur is the 0 -> SMALL_PIPE_SIZE
72 * resize which MUST occur for reverse-direction pipes when they are
75 * Additional information about the current state of pipes may be obtained
76 * from kern.ipc.pipes, kern.ipc.pipefragretry, kern.ipc.pipeallocfail,
77 * and kern.ipc.piperesizefail.
79 * Locking rules: There are two locks present here: A mutex, used via
80 * PIPE_LOCK, and a flag, used via pipelock(). All locking is done via
81 * the flag, as mutexes can not persist over uiomove. The mutex
82 * exists only to guard access to the flag, and is not in itself a
83 * locking mechanism. Also note that there is only a single mutex for
84 * both directions of a pipe.
86 * As pipelock() may have to sleep before it can acquire the flag, it
87 * is important to reread all data after a call to pipelock(); everything
88 * in the structure may have changed.
91 #include <sys/cdefs.h>
92 __FBSDID("$FreeBSD$");
96 #include <sys/param.h>
97 #include <sys/systm.h>
98 #include <sys/fcntl.h>
100 #include <sys/filedesc.h>
101 #include <sys/filio.h>
102 #include <sys/kernel.h>
103 #include <sys/lock.h>
104 #include <sys/mutex.h>
105 #include <sys/ttycom.h>
106 #include <sys/stat.h>
107 #include <sys/malloc.h>
108 #include <sys/poll.h>
109 #include <sys/selinfo.h>
110 #include <sys/signalvar.h>
111 #include <sys/sysctl.h>
112 #include <sys/sysproto.h>
113 #include <sys/pipe.h>
114 #include <sys/proc.h>
115 #include <sys/vnode.h>
117 #include <sys/event.h>
119 #include <security/mac/mac_framework.h>
122 #include <vm/vm_param.h>
123 #include <vm/vm_object.h>
124 #include <vm/vm_kern.h>
125 #include <vm/vm_extern.h>
127 #include <vm/vm_map.h>
128 #include <vm/vm_page.h>
132 * Use this define if you want to disable *fancy* VM things. Expect an
133 * approx 30% decrease in transfer rate. This could be useful for
136 /* #define PIPE_NODIRECT */
139 * interfaces to the outside world
141 static fo_rdwr_t pipe_read;
142 static fo_rdwr_t pipe_write;
143 static fo_ioctl_t pipe_ioctl;
144 static fo_poll_t pipe_poll;
145 static fo_kqfilter_t pipe_kqfilter;
146 static fo_stat_t pipe_stat;
147 static fo_close_t pipe_close;
149 static struct fileops pipeops = {
150 .fo_read = pipe_read,
151 .fo_write = pipe_write,
152 .fo_ioctl = pipe_ioctl,
153 .fo_poll = pipe_poll,
154 .fo_kqfilter = pipe_kqfilter,
155 .fo_stat = pipe_stat,
156 .fo_close = pipe_close,
157 .fo_flags = DFLAG_PASSABLE
160 static void filt_pipedetach(struct knote *kn);
161 static int filt_piperead(struct knote *kn, long hint);
162 static int filt_pipewrite(struct knote *kn, long hint);
164 static struct filterops pipe_rfiltops =
165 { 1, NULL, filt_pipedetach, filt_piperead };
166 static struct filterops pipe_wfiltops =
167 { 1, NULL, filt_pipedetach, filt_pipewrite };
170 * Default pipe buffer size(s), this can be kind-of large now because pipe
171 * space is pageable. The pipe code will try to maintain locality of
172 * reference for performance reasons, so small amounts of outstanding I/O
173 * will not wipe the cache.
175 #define MINPIPESIZE (PIPE_SIZE/3)
176 #define MAXPIPESIZE (2*PIPE_SIZE/3)
178 static int amountpipekva;
179 static int pipefragretry;
180 static int pipeallocfail;
181 static int piperesizefail;
182 static int piperesizeallowed = 1;
184 SYSCTL_INT(_kern_ipc, OID_AUTO, maxpipekva, CTLFLAG_RDTUN,
185 &maxpipekva, 0, "Pipe KVA limit");
186 SYSCTL_INT(_kern_ipc, OID_AUTO, pipekva, CTLFLAG_RD,
187 &amountpipekva, 0, "Pipe KVA usage");
188 SYSCTL_INT(_kern_ipc, OID_AUTO, pipefragretry, CTLFLAG_RD,
189 &pipefragretry, 0, "Pipe allocation retries due to fragmentation");
190 SYSCTL_INT(_kern_ipc, OID_AUTO, pipeallocfail, CTLFLAG_RD,
191 &pipeallocfail, 0, "Pipe allocation failures");
192 SYSCTL_INT(_kern_ipc, OID_AUTO, piperesizefail, CTLFLAG_RD,
193 &piperesizefail, 0, "Pipe resize failures");
194 SYSCTL_INT(_kern_ipc, OID_AUTO, piperesizeallowed, CTLFLAG_RW,
195 &piperesizeallowed, 0, "Pipe resizing allowed");
197 static void pipeinit(void *dummy __unused);
198 static void pipeclose(struct pipe *cpipe);
199 static void pipe_free_kmem(struct pipe *cpipe);
200 static int pipe_create(struct pipe *pipe, int backing);
201 static __inline int pipelock(struct pipe *cpipe, int catch);
202 static __inline void pipeunlock(struct pipe *cpipe);
203 static __inline void pipeselwakeup(struct pipe *cpipe);
204 #ifndef PIPE_NODIRECT
205 static int pipe_build_write_buffer(struct pipe *wpipe, struct uio *uio);
206 static void pipe_destroy_write_buffer(struct pipe *wpipe);
207 static int pipe_direct_write(struct pipe *wpipe, struct uio *uio);
208 static void pipe_clone_write_buffer(struct pipe *wpipe);
210 static int pipespace(struct pipe *cpipe, int size);
211 static int pipespace_new(struct pipe *cpipe, int size);
213 static int pipe_zone_ctor(void *mem, int size, void *arg, int flags);
214 static int pipe_zone_init(void *mem, int size, int flags);
215 static void pipe_zone_fini(void *mem, int size);
217 static uma_zone_t pipe_zone;
219 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_ANY, pipeinit, NULL);
222 pipeinit(void *dummy __unused)
225 pipe_zone = uma_zcreate("pipe", sizeof(struct pipepair),
226 pipe_zone_ctor, NULL, pipe_zone_init, pipe_zone_fini,
228 KASSERT(pipe_zone != NULL, ("pipe_zone not initialized"));
232 pipe_zone_ctor(void *mem, int size, void *arg, int flags)
235 struct pipe *rpipe, *wpipe;
237 KASSERT(size == sizeof(*pp), ("pipe_zone_ctor: wrong size"));
239 pp = (struct pipepair *)mem;
242 * We zero both pipe endpoints to make sure all the kmem pointers
243 * are NULL, flag fields are zero'd, etc. We timestamp both
244 * endpoints with the same time.
246 rpipe = &pp->pp_rpipe;
247 bzero(rpipe, sizeof(*rpipe));
248 vfs_timestamp(&rpipe->pipe_ctime);
249 rpipe->pipe_atime = rpipe->pipe_mtime = rpipe->pipe_ctime;
251 wpipe = &pp->pp_wpipe;
252 bzero(wpipe, sizeof(*wpipe));
253 wpipe->pipe_ctime = rpipe->pipe_ctime;
254 wpipe->pipe_atime = wpipe->pipe_mtime = rpipe->pipe_ctime;
256 rpipe->pipe_peer = wpipe;
257 rpipe->pipe_pair = pp;
258 wpipe->pipe_peer = rpipe;
259 wpipe->pipe_pair = pp;
262 * Mark both endpoints as present; they will later get free'd
263 * one at a time. When both are free'd, then the whole pair
266 rpipe->pipe_present = PIPE_ACTIVE;
267 wpipe->pipe_present = PIPE_ACTIVE;
270 * Eventually, the MAC Framework may initialize the label
271 * in ctor or init, but for now we do it elswhere to avoid
272 * blocking in ctor or init.
280 pipe_zone_init(void *mem, int size, int flags)
284 KASSERT(size == sizeof(*pp), ("pipe_zone_init: wrong size"));
286 pp = (struct pipepair *)mem;
288 mtx_init(&pp->pp_mtx, "pipe mutex", NULL, MTX_DEF | MTX_RECURSE);
293 pipe_zone_fini(void *mem, int size)
297 KASSERT(size == sizeof(*pp), ("pipe_zone_fini: wrong size"));
299 pp = (struct pipepair *)mem;
301 mtx_destroy(&pp->pp_mtx);
305 * The pipe system call for the DTYPE_PIPE type of pipes. If we fail, let
306 * the zone pick up the pieces via pipeclose().
312 struct pipe_args /* {
316 struct filedesc *fdp = td->td_proc->p_fd;
317 struct file *rf, *wf;
319 struct pipe *rpipe, *wpipe;
322 pp = uma_zalloc(pipe_zone, M_WAITOK);
325 * The MAC label is shared between the connected endpoints. As a
326 * result mac_init_pipe() and mac_create_pipe() are called once
327 * for the pair, and not on the endpoints.
330 mac_create_pipe(td->td_ucred, pp);
332 rpipe = &pp->pp_rpipe;
333 wpipe = &pp->pp_wpipe;
335 knlist_init(&rpipe->pipe_sel.si_note, PIPE_MTX(rpipe), NULL, NULL,
337 knlist_init(&wpipe->pipe_sel.si_note, PIPE_MTX(wpipe), NULL, NULL,
340 /* Only the forward direction pipe is backed by default */
341 if ((error = pipe_create(rpipe, 1)) != 0 ||
342 (error = pipe_create(wpipe, 0)) != 0) {
348 rpipe->pipe_state |= PIPE_DIRECTOK;
349 wpipe->pipe_state |= PIPE_DIRECTOK;
351 error = falloc(td, &rf, &fd);
357 /* An extra reference on `rf' has been held for us by falloc(). */
358 td->td_retval[0] = fd;
361 * Warning: once we've gotten past allocation of the fd for the
362 * read-side, we can only drop the read side via fdrop() in order
363 * to avoid races against processes which manage to dup() the read
364 * side while we are blocked trying to allocate the write side.
367 rf->f_flag = FREAD | FWRITE;
368 rf->f_type = DTYPE_PIPE;
370 rf->f_ops = &pipeops;
372 error = falloc(td, &wf, &fd);
374 fdclose(fdp, rf, td->td_retval[0], td);
376 /* rpipe has been closed by fdrop(). */
380 /* An extra reference on `wf' has been held for us by falloc(). */
382 wf->f_flag = FREAD | FWRITE;
383 wf->f_type = DTYPE_PIPE;
385 wf->f_ops = &pipeops;
388 td->td_retval[1] = fd;
395 * Allocate kva for pipe circular buffer, the space is pageable
396 * This routine will 'realloc' the size of a pipe safely, if it fails
397 * it will retain the old buffer.
398 * If it fails it will return ENOMEM.
401 pipespace_new(cpipe, size)
406 int error, cnt, firstseg;
407 static int curfail = 0;
408 static struct timeval lastfail;
410 KASSERT(!mtx_owned(PIPE_MTX(cpipe)), ("pipespace: pipe mutex locked"));
411 KASSERT(!(cpipe->pipe_state & PIPE_DIRECTW),
412 ("pipespace: resize of direct writes not allowed"));
414 cnt = cpipe->pipe_buffer.cnt;
418 size = round_page(size);
419 buffer = (caddr_t) vm_map_min(pipe_map);
421 error = vm_map_find(pipe_map, NULL, 0,
422 (vm_offset_t *) &buffer, size, 1,
423 VM_PROT_ALL, VM_PROT_ALL, 0);
424 if (error != KERN_SUCCESS) {
425 if ((cpipe->pipe_buffer.buffer == NULL) &&
426 (size > SMALL_PIPE_SIZE)) {
427 size = SMALL_PIPE_SIZE;
431 if (cpipe->pipe_buffer.buffer == NULL) {
433 if (ppsratecheck(&lastfail, &curfail, 1))
434 printf("kern.ipc.maxpipekva exceeded; see tuning(7)\n");
441 /* copy data, then free old resources if we're resizing */
443 if (cpipe->pipe_buffer.in <= cpipe->pipe_buffer.out) {
444 firstseg = cpipe->pipe_buffer.size - cpipe->pipe_buffer.out;
445 bcopy(&cpipe->pipe_buffer.buffer[cpipe->pipe_buffer.out],
447 if ((cnt - firstseg) > 0)
448 bcopy(cpipe->pipe_buffer.buffer, &buffer[firstseg],
449 cpipe->pipe_buffer.in);
451 bcopy(&cpipe->pipe_buffer.buffer[cpipe->pipe_buffer.out],
455 pipe_free_kmem(cpipe);
456 cpipe->pipe_buffer.buffer = buffer;
457 cpipe->pipe_buffer.size = size;
458 cpipe->pipe_buffer.in = cnt;
459 cpipe->pipe_buffer.out = 0;
460 cpipe->pipe_buffer.cnt = cnt;
461 atomic_add_int(&amountpipekva, cpipe->pipe_buffer.size);
466 * Wrapper for pipespace_new() that performs locking assertions.
469 pipespace(cpipe, size)
474 KASSERT(cpipe->pipe_state & PIPE_LOCKFL,
475 ("Unlocked pipe passed to pipespace"));
476 return (pipespace_new(cpipe, size));
480 * lock a pipe for I/O, blocking other access
483 pipelock(cpipe, catch)
489 PIPE_LOCK_ASSERT(cpipe, MA_OWNED);
490 while (cpipe->pipe_state & PIPE_LOCKFL) {
491 cpipe->pipe_state |= PIPE_LWANT;
492 error = msleep(cpipe, PIPE_MTX(cpipe),
493 catch ? (PRIBIO | PCATCH) : PRIBIO,
498 cpipe->pipe_state |= PIPE_LOCKFL;
503 * unlock a pipe I/O lock
510 PIPE_LOCK_ASSERT(cpipe, MA_OWNED);
511 KASSERT(cpipe->pipe_state & PIPE_LOCKFL,
512 ("Unlocked pipe passed to pipeunlock"));
513 cpipe->pipe_state &= ~PIPE_LOCKFL;
514 if (cpipe->pipe_state & PIPE_LWANT) {
515 cpipe->pipe_state &= ~PIPE_LWANT;
525 PIPE_LOCK_ASSERT(cpipe, MA_OWNED);
526 if (cpipe->pipe_state & PIPE_SEL) {
527 cpipe->pipe_state &= ~PIPE_SEL;
528 selwakeuppri(&cpipe->pipe_sel, PSOCK);
530 if ((cpipe->pipe_state & PIPE_ASYNC) && cpipe->pipe_sigio)
531 pgsigio(&cpipe->pipe_sigio, SIGIO, 0);
532 KNOTE_LOCKED(&cpipe->pipe_sel.si_note, 0);
536 * Initialize and allocate VM and memory for pipe. The structure
537 * will start out zero'd from the ctor, so we just manage the kmem.
540 pipe_create(pipe, backing)
547 if (amountpipekva > maxpipekva / 2)
548 error = pipespace_new(pipe, SMALL_PIPE_SIZE);
550 error = pipespace_new(pipe, PIPE_SIZE);
552 /* If we're not backing this pipe, no need to do anything. */
560 pipe_read(fp, uio, active_cred, flags, td)
563 struct ucred *active_cred;
567 struct pipe *rpipe = fp->f_data;
574 error = pipelock(rpipe, 1);
579 error = mac_check_pipe_read(active_cred, rpipe->pipe_pair);
583 if (amountpipekva > (3 * maxpipekva) / 4) {
584 if (!(rpipe->pipe_state & PIPE_DIRECTW) &&
585 (rpipe->pipe_buffer.size > SMALL_PIPE_SIZE) &&
586 (rpipe->pipe_buffer.cnt <= SMALL_PIPE_SIZE) &&
587 (piperesizeallowed == 1)) {
589 pipespace(rpipe, SMALL_PIPE_SIZE);
594 while (uio->uio_resid) {
596 * normal pipe buffer receive
598 if (rpipe->pipe_buffer.cnt > 0) {
599 size = rpipe->pipe_buffer.size - rpipe->pipe_buffer.out;
600 if (size > rpipe->pipe_buffer.cnt)
601 size = rpipe->pipe_buffer.cnt;
602 if (size > (u_int) uio->uio_resid)
603 size = (u_int) uio->uio_resid;
607 &rpipe->pipe_buffer.buffer[rpipe->pipe_buffer.out],
613 rpipe->pipe_buffer.out += size;
614 if (rpipe->pipe_buffer.out >= rpipe->pipe_buffer.size)
615 rpipe->pipe_buffer.out = 0;
617 rpipe->pipe_buffer.cnt -= size;
620 * If there is no more to read in the pipe, reset
621 * its pointers to the beginning. This improves
624 if (rpipe->pipe_buffer.cnt == 0) {
625 rpipe->pipe_buffer.in = 0;
626 rpipe->pipe_buffer.out = 0;
629 #ifndef PIPE_NODIRECT
631 * Direct copy, bypassing a kernel buffer.
633 } else if ((size = rpipe->pipe_map.cnt) &&
634 (rpipe->pipe_state & PIPE_DIRECTW)) {
635 if (size > (u_int) uio->uio_resid)
636 size = (u_int) uio->uio_resid;
639 error = uiomove_fromphys(rpipe->pipe_map.ms,
640 rpipe->pipe_map.pos, size, uio);
645 rpipe->pipe_map.pos += size;
646 rpipe->pipe_map.cnt -= size;
647 if (rpipe->pipe_map.cnt == 0) {
648 rpipe->pipe_state &= ~PIPE_DIRECTW;
654 * detect EOF condition
655 * read returns 0 on EOF, no need to set error
657 if (rpipe->pipe_state & PIPE_EOF)
661 * If the "write-side" has been blocked, wake it up now.
663 if (rpipe->pipe_state & PIPE_WANTW) {
664 rpipe->pipe_state &= ~PIPE_WANTW;
669 * Break if some data was read.
675 * Unlock the pipe buffer for our remaining processing.
676 * We will either break out with an error or we will
677 * sleep and relock to loop.
682 * Handle non-blocking mode operation or
683 * wait for more data.
685 if (fp->f_flag & FNONBLOCK) {
688 rpipe->pipe_state |= PIPE_WANTR;
689 if ((error = msleep(rpipe, PIPE_MTX(rpipe),
692 error = pipelock(rpipe, 1);
703 /* XXX: should probably do this before getting any locks. */
705 vfs_timestamp(&rpipe->pipe_atime);
710 * PIPE_WANT processing only makes sense if pipe_busy is 0.
712 if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANT)) {
713 rpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTW);
715 } else if (rpipe->pipe_buffer.cnt < MINPIPESIZE) {
717 * Handle write blocking hysteresis.
719 if (rpipe->pipe_state & PIPE_WANTW) {
720 rpipe->pipe_state &= ~PIPE_WANTW;
725 if ((rpipe->pipe_buffer.size - rpipe->pipe_buffer.cnt) >= PIPE_BUF)
726 pipeselwakeup(rpipe);
732 #ifndef PIPE_NODIRECT
734 * Map the sending processes' buffer into kernel space and wire it.
735 * This is similar to a physical write operation.
738 pipe_build_write_buffer(wpipe, uio)
745 vm_offset_t addr, endaddr;
747 PIPE_LOCK_ASSERT(wpipe, MA_NOTOWNED);
748 KASSERT(wpipe->pipe_state & PIPE_DIRECTW,
749 ("Clone attempt on non-direct write pipe!"));
751 size = (u_int) uio->uio_iov->iov_len;
752 if (size > wpipe->pipe_buffer.size)
753 size = wpipe->pipe_buffer.size;
755 pmap = vmspace_pmap(curproc->p_vmspace);
756 endaddr = round_page((vm_offset_t)uio->uio_iov->iov_base + size);
757 addr = trunc_page((vm_offset_t)uio->uio_iov->iov_base);
760 for (i = 0; addr < endaddr; addr += PAGE_SIZE, i++) {
762 * vm_fault_quick() can sleep. Consequently,
763 * vm_page_lock_queue() and vm_page_unlock_queue()
764 * should not be performed outside of this loop.
767 if (vm_fault_quick((caddr_t)addr, VM_PROT_READ) < 0) {
768 vm_page_lock_queues();
769 for (j = 0; j < i; j++)
770 vm_page_unhold(wpipe->pipe_map.ms[j]);
771 vm_page_unlock_queues();
774 wpipe->pipe_map.ms[i] = pmap_extract_and_hold(pmap, addr,
776 if (wpipe->pipe_map.ms[i] == NULL)
781 * set up the control block
783 wpipe->pipe_map.npages = i;
784 wpipe->pipe_map.pos =
785 ((vm_offset_t) uio->uio_iov->iov_base) & PAGE_MASK;
786 wpipe->pipe_map.cnt = size;
789 * and update the uio data
792 uio->uio_iov->iov_len -= size;
793 uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + size;
794 if (uio->uio_iov->iov_len == 0)
796 uio->uio_resid -= size;
797 uio->uio_offset += size;
802 * unmap and unwire the process buffer
805 pipe_destroy_write_buffer(wpipe)
810 PIPE_LOCK_ASSERT(wpipe, MA_OWNED);
811 vm_page_lock_queues();
812 for (i = 0; i < wpipe->pipe_map.npages; i++) {
813 vm_page_unhold(wpipe->pipe_map.ms[i]);
815 vm_page_unlock_queues();
816 wpipe->pipe_map.npages = 0;
820 * In the case of a signal, the writing process might go away. This
821 * code copies the data into the circular buffer so that the source
822 * pages can be freed without loss of data.
825 pipe_clone_write_buffer(wpipe)
833 PIPE_LOCK_ASSERT(wpipe, MA_OWNED);
834 size = wpipe->pipe_map.cnt;
835 pos = wpipe->pipe_map.pos;
837 wpipe->pipe_buffer.in = size;
838 wpipe->pipe_buffer.out = 0;
839 wpipe->pipe_buffer.cnt = size;
840 wpipe->pipe_state &= ~PIPE_DIRECTW;
843 iov.iov_base = wpipe->pipe_buffer.buffer;
848 uio.uio_resid = size;
849 uio.uio_segflg = UIO_SYSSPACE;
850 uio.uio_rw = UIO_READ;
851 uio.uio_td = curthread;
852 uiomove_fromphys(wpipe->pipe_map.ms, pos, size, &uio);
854 pipe_destroy_write_buffer(wpipe);
858 * This implements the pipe buffer write mechanism. Note that only
859 * a direct write OR a normal pipe write can be pending at any given time.
860 * If there are any characters in the pipe buffer, the direct write will
861 * be deferred until the receiving process grabs all of the bytes from
862 * the pipe buffer. Then the direct mapping write is set-up.
865 pipe_direct_write(wpipe, uio)
872 PIPE_LOCK_ASSERT(wpipe, MA_OWNED);
873 error = pipelock(wpipe, 1);
874 if (wpipe->pipe_state & PIPE_EOF)
880 while (wpipe->pipe_state & PIPE_DIRECTW) {
881 if (wpipe->pipe_state & PIPE_WANTR) {
882 wpipe->pipe_state &= ~PIPE_WANTR;
885 pipeselwakeup(wpipe);
886 wpipe->pipe_state |= PIPE_WANTW;
888 error = msleep(wpipe, PIPE_MTX(wpipe),
889 PRIBIO | PCATCH, "pipdww", 0);
895 wpipe->pipe_map.cnt = 0; /* transfer not ready yet */
896 if (wpipe->pipe_buffer.cnt > 0) {
897 if (wpipe->pipe_state & PIPE_WANTR) {
898 wpipe->pipe_state &= ~PIPE_WANTR;
901 pipeselwakeup(wpipe);
902 wpipe->pipe_state |= PIPE_WANTW;
904 error = msleep(wpipe, PIPE_MTX(wpipe),
905 PRIBIO | PCATCH, "pipdwc", 0);
912 wpipe->pipe_state |= PIPE_DIRECTW;
915 error = pipe_build_write_buffer(wpipe, uio);
918 wpipe->pipe_state &= ~PIPE_DIRECTW;
924 while (!error && (wpipe->pipe_state & PIPE_DIRECTW)) {
925 if (wpipe->pipe_state & PIPE_EOF) {
926 pipe_destroy_write_buffer(wpipe);
927 pipeselwakeup(wpipe);
932 if (wpipe->pipe_state & PIPE_WANTR) {
933 wpipe->pipe_state &= ~PIPE_WANTR;
936 pipeselwakeup(wpipe);
938 error = msleep(wpipe, PIPE_MTX(wpipe), PRIBIO | PCATCH,
943 if (wpipe->pipe_state & PIPE_EOF)
945 if (wpipe->pipe_state & PIPE_DIRECTW) {
947 * this bit of trickery substitutes a kernel buffer for
948 * the process that might be going away.
950 pipe_clone_write_buffer(wpipe);
952 pipe_destroy_write_buffer(wpipe);
964 pipe_write(fp, uio, active_cred, flags, td)
967 struct ucred *active_cred;
972 int desiredsize, orig_resid;
973 struct pipe *wpipe, *rpipe;
976 wpipe = rpipe->pipe_peer;
979 error = pipelock(wpipe, 1);
985 * detect loss of pipe read side, issue SIGPIPE if lost.
987 if (wpipe->pipe_present != PIPE_ACTIVE ||
988 (wpipe->pipe_state & PIPE_EOF)) {
994 error = mac_check_pipe_write(active_cred, wpipe->pipe_pair);
1003 /* Choose a larger size if it's advantageous */
1004 desiredsize = max(SMALL_PIPE_SIZE, wpipe->pipe_buffer.size);
1005 while (desiredsize < wpipe->pipe_buffer.cnt + uio->uio_resid) {
1006 if (piperesizeallowed != 1)
1008 if (amountpipekva > maxpipekva / 2)
1010 if (desiredsize == BIG_PIPE_SIZE)
1012 desiredsize = desiredsize * 2;
1015 /* Choose a smaller size if we're in a OOM situation */
1016 if ((amountpipekva > (3 * maxpipekva) / 4) &&
1017 (wpipe->pipe_buffer.size > SMALL_PIPE_SIZE) &&
1018 (wpipe->pipe_buffer.cnt <= SMALL_PIPE_SIZE) &&
1019 (piperesizeallowed == 1))
1020 desiredsize = SMALL_PIPE_SIZE;
1022 /* Resize if the above determined that a new size was necessary */
1023 if ((desiredsize != wpipe->pipe_buffer.size) &&
1024 ((wpipe->pipe_state & PIPE_DIRECTW) == 0)) {
1026 pipespace(wpipe, desiredsize);
1029 if (wpipe->pipe_buffer.size == 0) {
1031 * This can only happen for reverse direction use of pipes
1032 * in a complete OOM situation.
1043 orig_resid = uio->uio_resid;
1045 while (uio->uio_resid) {
1049 if (wpipe->pipe_state & PIPE_EOF) {
1054 #ifndef PIPE_NODIRECT
1056 * If the transfer is large, we can gain performance if
1057 * we do process-to-process copies directly.
1058 * If the write is non-blocking, we don't use the
1059 * direct write mechanism.
1061 * The direct write mechanism will detect the reader going
1064 if (uio->uio_segflg == UIO_USERSPACE &&
1065 uio->uio_iov->iov_len >= PIPE_MINDIRECT &&
1066 wpipe->pipe_buffer.size >= PIPE_MINDIRECT &&
1067 (fp->f_flag & FNONBLOCK) == 0) {
1069 error = pipe_direct_write(wpipe, uio);
1077 * Pipe buffered writes cannot be coincidental with
1078 * direct writes. We wait until the currently executing
1079 * direct write is completed before we start filling the
1080 * pipe buffer. We break out if a signal occurs or the
1083 if (wpipe->pipe_state & PIPE_DIRECTW) {
1084 if (wpipe->pipe_state & PIPE_WANTR) {
1085 wpipe->pipe_state &= ~PIPE_WANTR;
1088 pipeselwakeup(wpipe);
1089 wpipe->pipe_state |= PIPE_WANTW;
1091 error = msleep(wpipe, PIPE_MTX(rpipe), PRIBIO | PCATCH,
1099 space = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
1101 /* Writes of size <= PIPE_BUF must be atomic. */
1102 if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF))
1106 int size; /* Transfer size */
1107 int segsize; /* first segment to transfer */
1110 * Transfer size is minimum of uio transfer
1111 * and free space in pipe buffer.
1113 if (space > uio->uio_resid)
1114 size = uio->uio_resid;
1118 * First segment to transfer is minimum of
1119 * transfer size and contiguous space in
1120 * pipe buffer. If first segment to transfer
1121 * is less than the transfer size, we've got
1122 * a wraparound in the buffer.
1124 segsize = wpipe->pipe_buffer.size -
1125 wpipe->pipe_buffer.in;
1129 /* Transfer first segment */
1132 error = uiomove(&wpipe->pipe_buffer.buffer[wpipe->pipe_buffer.in],
1136 if (error == 0 && segsize < size) {
1137 KASSERT(wpipe->pipe_buffer.in + segsize ==
1138 wpipe->pipe_buffer.size,
1139 ("Pipe buffer wraparound disappeared"));
1141 * Transfer remaining part now, to
1142 * support atomic writes. Wraparound
1148 &wpipe->pipe_buffer.buffer[0],
1149 size - segsize, uio);
1153 wpipe->pipe_buffer.in += size;
1154 if (wpipe->pipe_buffer.in >=
1155 wpipe->pipe_buffer.size) {
1156 KASSERT(wpipe->pipe_buffer.in ==
1158 wpipe->pipe_buffer.size,
1159 ("Expected wraparound bad"));
1160 wpipe->pipe_buffer.in = size - segsize;
1163 wpipe->pipe_buffer.cnt += size;
1164 KASSERT(wpipe->pipe_buffer.cnt <=
1165 wpipe->pipe_buffer.size,
1166 ("Pipe buffer overflow"));
1173 * If the "read-side" has been blocked, wake it up now.
1175 if (wpipe->pipe_state & PIPE_WANTR) {
1176 wpipe->pipe_state &= ~PIPE_WANTR;
1181 * don't block on non-blocking I/O
1183 if (fp->f_flag & FNONBLOCK) {
1190 * We have no more space and have something to offer,
1191 * wake up select/poll.
1193 pipeselwakeup(wpipe);
1195 wpipe->pipe_state |= PIPE_WANTW;
1197 error = msleep(wpipe, PIPE_MTX(rpipe),
1198 PRIBIO | PCATCH, "pipewr", 0);
1207 if ((wpipe->pipe_busy == 0) && (wpipe->pipe_state & PIPE_WANT)) {
1208 wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR);
1210 } else if (wpipe->pipe_buffer.cnt > 0) {
1212 * If we have put any characters in the buffer, we wake up
1215 if (wpipe->pipe_state & PIPE_WANTR) {
1216 wpipe->pipe_state &= ~PIPE_WANTR;
1222 * Don't return EPIPE if I/O was successful
1224 if ((wpipe->pipe_buffer.cnt == 0) &&
1225 (uio->uio_resid == 0) &&
1231 vfs_timestamp(&wpipe->pipe_mtime);
1234 * We have something to offer,
1235 * wake up select/poll.
1237 if (wpipe->pipe_buffer.cnt)
1238 pipeselwakeup(wpipe);
1246 * we implement a very minimal set of ioctls for compatibility with sockets.
1249 pipe_ioctl(fp, cmd, data, active_cred, td)
1253 struct ucred *active_cred;
1256 struct pipe *mpipe = fp->f_data;
1262 error = mac_check_pipe_ioctl(active_cred, mpipe->pipe_pair, cmd, data);
1277 mpipe->pipe_state |= PIPE_ASYNC;
1279 mpipe->pipe_state &= ~PIPE_ASYNC;
1284 if (mpipe->pipe_state & PIPE_DIRECTW)
1285 *(int *)data = mpipe->pipe_map.cnt;
1287 *(int *)data = mpipe->pipe_buffer.cnt;
1292 error = fsetown(*(int *)data, &mpipe->pipe_sigio);
1296 *(int *)data = fgetown(&mpipe->pipe_sigio);
1299 /* This is deprecated, FIOSETOWN should be used instead. */
1302 error = fsetown(-(*(int *)data), &mpipe->pipe_sigio);
1305 /* This is deprecated, FIOGETOWN should be used instead. */
1307 *(int *)data = -fgetown(&mpipe->pipe_sigio);
1320 pipe_poll(fp, events, active_cred, td)
1323 struct ucred *active_cred;
1326 struct pipe *rpipe = fp->f_data;
1333 wpipe = rpipe->pipe_peer;
1336 error = mac_check_pipe_poll(active_cred, rpipe->pipe_pair);
1340 if (events & (POLLIN | POLLRDNORM))
1341 if ((rpipe->pipe_state & PIPE_DIRECTW) ||
1342 (rpipe->pipe_buffer.cnt > 0) ||
1343 (rpipe->pipe_state & PIPE_EOF))
1344 revents |= events & (POLLIN | POLLRDNORM);
1346 if (events & (POLLOUT | POLLWRNORM))
1347 if (wpipe->pipe_present != PIPE_ACTIVE ||
1348 (wpipe->pipe_state & PIPE_EOF) ||
1349 (((wpipe->pipe_state & PIPE_DIRECTW) == 0) &&
1350 (wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF))
1351 revents |= events & (POLLOUT | POLLWRNORM);
1353 if ((rpipe->pipe_state & PIPE_EOF) ||
1354 wpipe->pipe_present != PIPE_ACTIVE ||
1355 (wpipe->pipe_state & PIPE_EOF))
1359 if (events & (POLLIN | POLLRDNORM)) {
1360 selrecord(td, &rpipe->pipe_sel);
1361 rpipe->pipe_state |= PIPE_SEL;
1364 if (events & (POLLOUT | POLLWRNORM)) {
1365 selrecord(td, &wpipe->pipe_sel);
1366 wpipe->pipe_state |= PIPE_SEL;
1378 * We shouldn't need locks here as we're doing a read and this should
1379 * be a natural race.
1382 pipe_stat(fp, ub, active_cred, td)
1385 struct ucred *active_cred;
1388 struct pipe *pipe = fp->f_data;
1393 error = mac_check_pipe_stat(active_cred, pipe->pipe_pair);
1398 bzero(ub, sizeof(*ub));
1399 ub->st_mode = S_IFIFO;
1400 ub->st_blksize = PAGE_SIZE;
1401 if (pipe->pipe_state & PIPE_DIRECTW)
1402 ub->st_size = pipe->pipe_map.cnt;
1404 ub->st_size = pipe->pipe_buffer.cnt;
1405 ub->st_blocks = (ub->st_size + ub->st_blksize - 1) / ub->st_blksize;
1406 ub->st_atimespec = pipe->pipe_atime;
1407 ub->st_mtimespec = pipe->pipe_mtime;
1408 ub->st_ctimespec = pipe->pipe_ctime;
1409 ub->st_uid = fp->f_cred->cr_uid;
1410 ub->st_gid = fp->f_cred->cr_gid;
1412 * Left as 0: st_dev, st_ino, st_nlink, st_rdev, st_flags, st_gen.
1413 * XXX (st_dev, st_ino) should be unique.
1424 struct pipe *cpipe = fp->f_data;
1426 fp->f_ops = &badfileops;
1428 funsetown(&cpipe->pipe_sigio);
1434 pipe_free_kmem(cpipe)
1438 KASSERT(!mtx_owned(PIPE_MTX(cpipe)),
1439 ("pipe_free_kmem: pipe mutex locked"));
1441 if (cpipe->pipe_buffer.buffer != NULL) {
1442 atomic_subtract_int(&amountpipekva, cpipe->pipe_buffer.size);
1443 vm_map_remove(pipe_map,
1444 (vm_offset_t)cpipe->pipe_buffer.buffer,
1445 (vm_offset_t)cpipe->pipe_buffer.buffer + cpipe->pipe_buffer.size);
1446 cpipe->pipe_buffer.buffer = NULL;
1448 #ifndef PIPE_NODIRECT
1450 cpipe->pipe_map.cnt = 0;
1451 cpipe->pipe_map.pos = 0;
1452 cpipe->pipe_map.npages = 0;
1464 struct pipepair *pp;
1467 KASSERT(cpipe != NULL, ("pipeclose: cpipe == NULL"));
1471 pp = cpipe->pipe_pair;
1473 pipeselwakeup(cpipe);
1476 * If the other side is blocked, wake it up saying that
1477 * we want to close it down.
1479 cpipe->pipe_state |= PIPE_EOF;
1480 while (cpipe->pipe_busy) {
1482 cpipe->pipe_state |= PIPE_WANT;
1484 msleep(cpipe, PIPE_MTX(cpipe), PRIBIO, "pipecl", 0);
1490 * Disconnect from peer, if any.
1492 ppipe = cpipe->pipe_peer;
1493 if (ppipe->pipe_present == PIPE_ACTIVE) {
1494 pipeselwakeup(ppipe);
1496 ppipe->pipe_state |= PIPE_EOF;
1498 KNOTE_LOCKED(&ppipe->pipe_sel.si_note, 0);
1502 * Mark this endpoint as free. Release kmem resources. We
1503 * don't mark this endpoint as unused until we've finished
1504 * doing that, or the pipe might disappear out from under
1508 pipe_free_kmem(cpipe);
1510 cpipe->pipe_present = PIPE_CLOSING;
1514 * knlist_clear() may sleep dropping the PIPE_MTX. Set the
1515 * PIPE_FINALIZED, that allows other end to free the
1516 * pipe_pair, only after the knotes are completely dismantled.
1518 knlist_clear(&cpipe->pipe_sel.si_note, 1);
1519 cpipe->pipe_present = PIPE_FINALIZED;
1520 knlist_destroy(&cpipe->pipe_sel.si_note);
1523 * If both endpoints are now closed, release the memory for the
1524 * pipe pair. If not, unlock.
1526 if (ppipe->pipe_present == PIPE_FINALIZED) {
1529 mac_destroy_pipe(pp);
1531 uma_zfree(pipe_zone, cpipe->pipe_pair);
1538 pipe_kqfilter(struct file *fp, struct knote *kn)
1542 cpipe = kn->kn_fp->f_data;
1544 switch (kn->kn_filter) {
1546 kn->kn_fop = &pipe_rfiltops;
1549 kn->kn_fop = &pipe_wfiltops;
1550 if (cpipe->pipe_peer->pipe_present != PIPE_ACTIVE) {
1551 /* other end of pipe has been closed */
1555 cpipe = cpipe->pipe_peer;
1562 knlist_add(&cpipe->pipe_sel.si_note, kn, 1);
1568 filt_pipedetach(struct knote *kn)
1570 struct pipe *cpipe = (struct pipe *)kn->kn_fp->f_data;
1573 if (kn->kn_filter == EVFILT_WRITE)
1574 cpipe = cpipe->pipe_peer;
1575 knlist_remove(&cpipe->pipe_sel.si_note, kn, 1);
1581 filt_piperead(struct knote *kn, long hint)
1583 struct pipe *rpipe = kn->kn_fp->f_data;
1584 struct pipe *wpipe = rpipe->pipe_peer;
1588 kn->kn_data = rpipe->pipe_buffer.cnt;
1589 if ((kn->kn_data == 0) && (rpipe->pipe_state & PIPE_DIRECTW))
1590 kn->kn_data = rpipe->pipe_map.cnt;
1592 if ((rpipe->pipe_state & PIPE_EOF) ||
1593 wpipe->pipe_present != PIPE_ACTIVE ||
1594 (wpipe->pipe_state & PIPE_EOF)) {
1595 kn->kn_flags |= EV_EOF;
1599 ret = kn->kn_data > 0;
1606 filt_pipewrite(struct knote *kn, long hint)
1608 struct pipe *rpipe = kn->kn_fp->f_data;
1609 struct pipe *wpipe = rpipe->pipe_peer;
1612 if (wpipe->pipe_present != PIPE_ACTIVE ||
1613 (wpipe->pipe_state & PIPE_EOF)) {
1615 kn->kn_flags |= EV_EOF;
1619 kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
1620 if (wpipe->pipe_state & PIPE_DIRECTW)
1624 return (kn->kn_data >= PIPE_BUF);