2 * Copyright (c) 1996 John S. Dyson
3 * Copyright (c) 2012 Giovanni Trematerra
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice immediately at the beginning of the file, without modification,
11 * this list of conditions, and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Absolutely no warranty of function or purpose is made by the author
17 * 4. Modifications may be freely made to this file if the above conditions
22 * This file contains a high-performance replacement for the socket-based
23 * pipes scheme originally used in FreeBSD/4.4Lite. It does not support
24 * all features of sockets, but does do everything that pipes normally
29 * This code has two modes of operation, a small write mode and a large
30 * write mode. The small write mode acts like conventional pipes with
31 * a kernel buffer. If the buffer is less than PIPE_MINDIRECT, then the
32 * "normal" pipe buffering is done. If the buffer is between PIPE_MINDIRECT
33 * and PIPE_SIZE in size, the sending process pins the underlying pages in
34 * memory, and the receiving process copies directly from these pinned pages
35 * in the sending process.
37 * If the sending process receives a signal, it is possible that it will
38 * go away, and certainly its address space can change, because control
39 * is returned back to the user-mode side. In that case, the pipe code
40 * arranges to copy the buffer supplied by the user process, to a pageable
41 * kernel buffer, and the receiving process will grab the data from the
42 * pageable kernel buffer. Since signals don't happen all that often,
43 * the copy operation is normally eliminated.
45 * The constant PIPE_MINDIRECT is chosen to make sure that buffering will
46 * happen for small transfers so that the system will not spend all of
47 * its time context switching.
49 * In order to limit the resource use of pipes, two sysctls exist:
51 * kern.ipc.maxpipekva - This is a hard limit on the amount of pageable
52 * address space available to us in pipe_map. This value is normally
53 * autotuned, but may also be loader tuned.
55 * kern.ipc.pipekva - This read-only sysctl tracks the current amount of
56 * memory in use by pipes.
58 * Based on how large pipekva is relative to maxpipekva, the following
62 * New pipes are given 16K of memory backing, pipes may dynamically
63 * grow to as large as 64K where needed.
65 * New pipes are given 4K (or PAGE_SIZE) of memory backing,
66 * existing pipes may NOT grow.
68 * New pipes are given 4K (or PAGE_SIZE) of memory backing,
69 * existing pipes will be shrunk down to 4K whenever possible.
71 * Resizing may be disabled by setting kern.ipc.piperesizeallowed=0. If
72 * that is set, the only resize that will occur is the 0 -> SMALL_PIPE_SIZE
73 * resize which MUST occur for reverse-direction pipes when they are
76 * Additional information about the current state of pipes may be obtained
77 * from kern.ipc.pipes, kern.ipc.pipefragretry, kern.ipc.pipeallocfail,
78 * and kern.ipc.piperesizefail.
80 * Locking rules: There are two locks present here: A mutex, used via
81 * PIPE_LOCK, and a flag, used via pipelock(). All locking is done via
82 * the flag, as mutexes can not persist over uiomove. The mutex
83 * exists only to guard access to the flag, and is not in itself a
84 * locking mechanism. Also note that there is only a single mutex for
85 * both directions of a pipe.
87 * As pipelock() may have to sleep before it can acquire the flag, it
88 * is important to reread all data after a call to pipelock(); everything
89 * in the structure may have changed.
92 #include <sys/cdefs.h>
93 __FBSDID("$FreeBSD$");
95 #include <sys/param.h>
96 #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/syscallsubr.h>
112 #include <sys/sysctl.h>
113 #include <sys/sysproto.h>
114 #include <sys/pipe.h>
115 #include <sys/proc.h>
116 #include <sys/vnode.h>
118 #include <sys/user.h>
119 #include <sys/event.h>
121 #include <security/mac/mac_framework.h>
124 #include <vm/vm_param.h>
125 #include <vm/vm_object.h>
126 #include <vm/vm_kern.h>
127 #include <vm/vm_extern.h>
129 #include <vm/vm_map.h>
130 #include <vm/vm_page.h>
134 * Use this define if you want to disable *fancy* VM things. Expect an
135 * approx 30% decrease in transfer rate. This could be useful for
138 /* #define PIPE_NODIRECT */
140 #define PIPE_PEER(pipe) \
141 (((pipe)->pipe_type & PIPE_TYPE_NAMED) ? (pipe) : ((pipe)->pipe_peer))
144 * interfaces to the outside world
146 static fo_rdwr_t pipe_read;
147 static fo_rdwr_t pipe_write;
148 static fo_truncate_t pipe_truncate;
149 static fo_ioctl_t pipe_ioctl;
150 static fo_poll_t pipe_poll;
151 static fo_kqfilter_t pipe_kqfilter;
152 static fo_stat_t pipe_stat;
153 static fo_close_t pipe_close;
154 static fo_chmod_t pipe_chmod;
155 static fo_chown_t pipe_chown;
156 static fo_fill_kinfo_t pipe_fill_kinfo;
158 struct fileops pipeops = {
159 .fo_read = pipe_read,
160 .fo_write = pipe_write,
161 .fo_truncate = pipe_truncate,
162 .fo_ioctl = pipe_ioctl,
163 .fo_poll = pipe_poll,
164 .fo_kqfilter = pipe_kqfilter,
165 .fo_stat = pipe_stat,
166 .fo_close = pipe_close,
167 .fo_chmod = pipe_chmod,
168 .fo_chown = pipe_chown,
169 .fo_sendfile = invfo_sendfile,
170 .fo_fill_kinfo = pipe_fill_kinfo,
171 .fo_flags = DFLAG_PASSABLE
174 static void filt_pipedetach(struct knote *kn);
175 static void filt_pipedetach_notsup(struct knote *kn);
176 static int filt_pipenotsup(struct knote *kn, long hint);
177 static int filt_piperead(struct knote *kn, long hint);
178 static int filt_pipewrite(struct knote *kn, long hint);
180 static struct filterops pipe_nfiltops = {
182 .f_detach = filt_pipedetach_notsup,
183 .f_event = filt_pipenotsup
185 static struct filterops pipe_rfiltops = {
187 .f_detach = filt_pipedetach,
188 .f_event = filt_piperead
190 static struct filterops pipe_wfiltops = {
192 .f_detach = filt_pipedetach,
193 .f_event = filt_pipewrite
197 * Default pipe buffer size(s), this can be kind-of large now because pipe
198 * space is pageable. The pipe code will try to maintain locality of
199 * reference for performance reasons, so small amounts of outstanding I/O
200 * will not wipe the cache.
202 #define MINPIPESIZE (PIPE_SIZE/3)
203 #define MAXPIPESIZE (2*PIPE_SIZE/3)
205 static long amountpipekva;
206 static int pipefragretry;
207 static int pipeallocfail;
208 static int piperesizefail;
209 static int piperesizeallowed = 1;
210 static long pipe_mindirect = PIPE_MINDIRECT;
212 SYSCTL_LONG(_kern_ipc, OID_AUTO, maxpipekva, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
213 &maxpipekva, 0, "Pipe KVA limit");
214 SYSCTL_LONG(_kern_ipc, OID_AUTO, pipekva, CTLFLAG_RD,
215 &amountpipekva, 0, "Pipe KVA usage");
216 SYSCTL_INT(_kern_ipc, OID_AUTO, pipefragretry, CTLFLAG_RD,
217 &pipefragretry, 0, "Pipe allocation retries due to fragmentation");
218 SYSCTL_INT(_kern_ipc, OID_AUTO, pipeallocfail, CTLFLAG_RD,
219 &pipeallocfail, 0, "Pipe allocation failures");
220 SYSCTL_INT(_kern_ipc, OID_AUTO, piperesizefail, CTLFLAG_RD,
221 &piperesizefail, 0, "Pipe resize failures");
222 SYSCTL_INT(_kern_ipc, OID_AUTO, piperesizeallowed, CTLFLAG_RW,
223 &piperesizeallowed, 0, "Pipe resizing allowed");
225 static void pipeinit(void *dummy __unused);
226 static void pipeclose(struct pipe *cpipe);
227 static void pipe_free_kmem(struct pipe *cpipe);
228 static int pipe_create(struct pipe *pipe, bool backing);
229 static int pipe_paircreate(struct thread *td, struct pipepair **p_pp);
230 static __inline int pipelock(struct pipe *cpipe, int catch);
231 static __inline void pipeunlock(struct pipe *cpipe);
232 static void pipe_timestamp(struct timespec *tsp);
233 #ifndef PIPE_NODIRECT
234 static int pipe_build_write_buffer(struct pipe *wpipe, struct uio *uio);
235 static void pipe_destroy_write_buffer(struct pipe *wpipe);
236 static int pipe_direct_write(struct pipe *wpipe, struct uio *uio);
237 static void pipe_clone_write_buffer(struct pipe *wpipe);
239 static int pipespace(struct pipe *cpipe, int size);
240 static int pipespace_new(struct pipe *cpipe, int size);
242 static int pipe_zone_ctor(void *mem, int size, void *arg, int flags);
243 static int pipe_zone_init(void *mem, int size, int flags);
244 static void pipe_zone_fini(void *mem, int size);
246 static uma_zone_t pipe_zone;
247 static struct unrhdr64 pipeino_unr;
248 static dev_t pipedev_ino;
250 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_ANY, pipeinit, NULL);
253 pipeinit(void *dummy __unused)
256 pipe_zone = uma_zcreate("pipe", sizeof(struct pipepair),
257 pipe_zone_ctor, NULL, pipe_zone_init, pipe_zone_fini,
259 KASSERT(pipe_zone != NULL, ("pipe_zone not initialized"));
260 new_unrhdr64(&pipeino_unr, 1);
261 pipedev_ino = devfs_alloc_cdp_inode();
262 KASSERT(pipedev_ino > 0, ("pipe dev inode not initialized"));
266 sysctl_handle_pipe_mindirect(SYSCTL_HANDLER_ARGS)
269 long tmp_pipe_mindirect = pipe_mindirect;
271 error = sysctl_handle_long(oidp, &tmp_pipe_mindirect, arg2, req);
272 if (error != 0 || req->newptr == NULL)
276 * Don't allow pipe_mindirect to be set so low that we violate
277 * atomicity requirements.
279 if (tmp_pipe_mindirect <= PIPE_BUF)
281 pipe_mindirect = tmp_pipe_mindirect;
284 SYSCTL_OID(_kern_ipc, OID_AUTO, pipe_mindirect, CTLTYPE_LONG | CTLFLAG_RW,
285 &pipe_mindirect, 0, sysctl_handle_pipe_mindirect, "L",
286 "Minimum write size triggering VM optimization");
289 pipe_zone_ctor(void *mem, int size, void *arg, int flags)
292 struct pipe *rpipe, *wpipe;
294 KASSERT(size == sizeof(*pp), ("pipe_zone_ctor: wrong size"));
296 pp = (struct pipepair *)mem;
299 * We zero both pipe endpoints to make sure all the kmem pointers
300 * are NULL, flag fields are zero'd, etc. We timestamp both
301 * endpoints with the same time.
303 rpipe = &pp->pp_rpipe;
304 bzero(rpipe, sizeof(*rpipe));
305 pipe_timestamp(&rpipe->pipe_ctime);
306 rpipe->pipe_atime = rpipe->pipe_mtime = rpipe->pipe_ctime;
308 wpipe = &pp->pp_wpipe;
309 bzero(wpipe, sizeof(*wpipe));
310 wpipe->pipe_ctime = rpipe->pipe_ctime;
311 wpipe->pipe_atime = wpipe->pipe_mtime = rpipe->pipe_ctime;
313 rpipe->pipe_peer = wpipe;
314 rpipe->pipe_pair = pp;
315 wpipe->pipe_peer = rpipe;
316 wpipe->pipe_pair = pp;
319 * Mark both endpoints as present; they will later get free'd
320 * one at a time. When both are free'd, then the whole pair
323 rpipe->pipe_present = PIPE_ACTIVE;
324 wpipe->pipe_present = PIPE_ACTIVE;
327 * Eventually, the MAC Framework may initialize the label
328 * in ctor or init, but for now we do it elswhere to avoid
329 * blocking in ctor or init.
337 pipe_zone_init(void *mem, int size, int flags)
341 KASSERT(size == sizeof(*pp), ("pipe_zone_init: wrong size"));
343 pp = (struct pipepair *)mem;
345 mtx_init(&pp->pp_mtx, "pipe mutex", NULL, MTX_DEF | MTX_NEW);
350 pipe_zone_fini(void *mem, int size)
354 KASSERT(size == sizeof(*pp), ("pipe_zone_fini: wrong size"));
356 pp = (struct pipepair *)mem;
358 mtx_destroy(&pp->pp_mtx);
362 pipe_paircreate(struct thread *td, struct pipepair **p_pp)
365 struct pipe *rpipe, *wpipe;
368 *p_pp = pp = uma_zalloc(pipe_zone, M_WAITOK);
371 * The MAC label is shared between the connected endpoints. As a
372 * result mac_pipe_init() and mac_pipe_create() are called once
373 * for the pair, and not on the endpoints.
376 mac_pipe_create(td->td_ucred, pp);
378 rpipe = &pp->pp_rpipe;
379 wpipe = &pp->pp_wpipe;
381 knlist_init_mtx(&rpipe->pipe_sel.si_note, PIPE_MTX(rpipe));
382 knlist_init_mtx(&wpipe->pipe_sel.si_note, PIPE_MTX(wpipe));
385 * Only the forward direction pipe is backed by big buffer by
388 error = pipe_create(rpipe, true);
391 error = pipe_create(wpipe, false);
394 * This cleanup leaves the pipe inode number for rpipe
395 * still allocated, but never used. We do not free
396 * inode numbers for opened pipes, which is required
397 * for correctness because numbers must be unique.
398 * But also it avoids any memory use by the unr
399 * allocator, so stashing away the transient inode
400 * number is reasonable.
402 pipe_free_kmem(rpipe);
406 rpipe->pipe_state |= PIPE_DIRECTOK;
407 wpipe->pipe_state |= PIPE_DIRECTOK;
411 knlist_destroy(&rpipe->pipe_sel.si_note);
412 knlist_destroy(&wpipe->pipe_sel.si_note);
414 mac_pipe_destroy(pp);
416 uma_zfree(pipe_zone, pp);
421 pipe_named_ctor(struct pipe **ppipe, struct thread *td)
426 error = pipe_paircreate(td, &pp);
429 pp->pp_rpipe.pipe_type |= PIPE_TYPE_NAMED;
430 *ppipe = &pp->pp_rpipe;
435 pipe_dtor(struct pipe *dpipe)
439 peer = (dpipe->pipe_type & PIPE_TYPE_NAMED) != 0 ? dpipe->pipe_peer : NULL;
440 funsetown(&dpipe->pipe_sigio);
443 funsetown(&peer->pipe_sigio);
451 * This used to be vfs_timestamp but the higher precision is unnecessary and
452 * can very negatively affect performance in virtualized environments (e.g., on
453 * vms running on amd64 when using the rdtscp instruction).
456 pipe_timestamp(struct timespec *tsp)
463 * The pipe system call for the DTYPE_PIPE type of pipes. If we fail, let
464 * the zone pick up the pieces via pipeclose().
467 kern_pipe(struct thread *td, int fildes[2], int flags, struct filecaps *fcaps1,
468 struct filecaps *fcaps2)
470 struct file *rf, *wf;
471 struct pipe *rpipe, *wpipe;
473 int fd, fflags, error;
475 error = pipe_paircreate(td, &pp);
478 rpipe = &pp->pp_rpipe;
479 wpipe = &pp->pp_wpipe;
480 error = falloc_caps(td, &rf, &fd, flags, fcaps1);
486 /* An extra reference on `rf' has been held for us by falloc_caps(). */
489 fflags = FREAD | FWRITE;
490 if ((flags & O_NONBLOCK) != 0)
494 * Warning: once we've gotten past allocation of the fd for the
495 * read-side, we can only drop the read side via fdrop() in order
496 * to avoid races against processes which manage to dup() the read
497 * side while we are blocked trying to allocate the write side.
499 finit(rf, fflags, DTYPE_PIPE, rpipe, &pipeops);
500 error = falloc_caps(td, &wf, &fd, flags, fcaps2);
502 fdclose(td, rf, fildes[0]);
504 /* rpipe has been closed by fdrop(). */
508 /* An extra reference on `wf' has been held for us by falloc_caps(). */
509 finit(wf, fflags, DTYPE_PIPE, wpipe, &pipeops);
517 #ifdef COMPAT_FREEBSD10
520 freebsd10_pipe(struct thread *td, struct freebsd10_pipe_args *uap __unused)
525 error = kern_pipe(td, fildes, 0, NULL, NULL);
529 td->td_retval[0] = fildes[0];
530 td->td_retval[1] = fildes[1];
537 sys_pipe2(struct thread *td, struct pipe2_args *uap)
539 int error, fildes[2];
541 if (uap->flags & ~(O_CLOEXEC | O_NONBLOCK))
543 error = kern_pipe(td, fildes, uap->flags, NULL, NULL);
546 error = copyout(fildes, uap->fildes, 2 * sizeof(int));
548 (void)kern_close(td, fildes[0]);
549 (void)kern_close(td, fildes[1]);
555 * Allocate kva for pipe circular buffer, the space is pageable
556 * This routine will 'realloc' the size of a pipe safely, if it fails
557 * it will retain the old buffer.
558 * If it fails it will return ENOMEM.
561 pipespace_new(struct pipe *cpipe, int size)
564 int error, cnt, firstseg;
565 static int curfail = 0;
566 static struct timeval lastfail;
568 KASSERT(!mtx_owned(PIPE_MTX(cpipe)), ("pipespace: pipe mutex locked"));
569 KASSERT(!(cpipe->pipe_state & PIPE_DIRECTW),
570 ("pipespace: resize of direct writes not allowed"));
572 cnt = cpipe->pipe_buffer.cnt;
576 size = round_page(size);
577 buffer = (caddr_t) vm_map_min(pipe_map);
579 error = vm_map_find(pipe_map, NULL, 0, (vm_offset_t *)&buffer, size, 0,
580 VMFS_ANY_SPACE, VM_PROT_RW, VM_PROT_RW, 0);
581 if (error != KERN_SUCCESS) {
582 if (cpipe->pipe_buffer.buffer == NULL &&
583 size > SMALL_PIPE_SIZE) {
584 size = SMALL_PIPE_SIZE;
588 if (cpipe->pipe_buffer.buffer == NULL) {
590 if (ppsratecheck(&lastfail, &curfail, 1))
591 printf("kern.ipc.maxpipekva exceeded; see tuning(7)\n");
598 /* copy data, then free old resources if we're resizing */
600 if (cpipe->pipe_buffer.in <= cpipe->pipe_buffer.out) {
601 firstseg = cpipe->pipe_buffer.size - cpipe->pipe_buffer.out;
602 bcopy(&cpipe->pipe_buffer.buffer[cpipe->pipe_buffer.out],
604 if ((cnt - firstseg) > 0)
605 bcopy(cpipe->pipe_buffer.buffer, &buffer[firstseg],
606 cpipe->pipe_buffer.in);
608 bcopy(&cpipe->pipe_buffer.buffer[cpipe->pipe_buffer.out],
612 pipe_free_kmem(cpipe);
613 cpipe->pipe_buffer.buffer = buffer;
614 cpipe->pipe_buffer.size = size;
615 cpipe->pipe_buffer.in = cnt;
616 cpipe->pipe_buffer.out = 0;
617 cpipe->pipe_buffer.cnt = cnt;
618 atomic_add_long(&amountpipekva, cpipe->pipe_buffer.size);
623 * Wrapper for pipespace_new() that performs locking assertions.
626 pipespace(struct pipe *cpipe, int size)
629 KASSERT(cpipe->pipe_state & PIPE_LOCKFL,
630 ("Unlocked pipe passed to pipespace"));
631 return (pipespace_new(cpipe, size));
635 * lock a pipe for I/O, blocking other access
638 pipelock(struct pipe *cpipe, int catch)
642 PIPE_LOCK_ASSERT(cpipe, MA_OWNED);
647 while (cpipe->pipe_state & PIPE_LOCKFL) {
648 KASSERT(cpipe->pipe_waiters >= 0,
649 ("%s: bad waiter count %d", __func__,
650 cpipe->pipe_waiters));
651 cpipe->pipe_waiters++;
652 error = msleep(&cpipe->pipe_waiters, PIPE_MTX(cpipe), prio,
654 cpipe->pipe_waiters--;
658 cpipe->pipe_state |= PIPE_LOCKFL;
663 * unlock a pipe I/O lock
666 pipeunlock(struct pipe *cpipe)
669 PIPE_LOCK_ASSERT(cpipe, MA_OWNED);
670 KASSERT(cpipe->pipe_state & PIPE_LOCKFL,
671 ("Unlocked pipe passed to pipeunlock"));
672 KASSERT(cpipe->pipe_waiters >= 0,
673 ("%s: bad waiter count %d", __func__,
674 cpipe->pipe_waiters));
675 cpipe->pipe_state &= ~PIPE_LOCKFL;
676 if (cpipe->pipe_waiters > 0)
677 wakeup_one(&cpipe->pipe_waiters);
681 pipeselwakeup(struct pipe *cpipe)
684 PIPE_LOCK_ASSERT(cpipe, MA_OWNED);
685 if (cpipe->pipe_state & PIPE_SEL) {
686 selwakeuppri(&cpipe->pipe_sel, PSOCK);
687 if (!SEL_WAITING(&cpipe->pipe_sel))
688 cpipe->pipe_state &= ~PIPE_SEL;
690 if ((cpipe->pipe_state & PIPE_ASYNC) && cpipe->pipe_sigio)
691 pgsigio(&cpipe->pipe_sigio, SIGIO, 0);
692 KNOTE_LOCKED(&cpipe->pipe_sel.si_note, 0);
696 * Initialize and allocate VM and memory for pipe. The structure
697 * will start out zero'd from the ctor, so we just manage the kmem.
700 pipe_create(struct pipe *pipe, bool large_backing)
704 error = pipespace_new(pipe, !large_backing || amountpipekva >
705 maxpipekva / 2 ? SMALL_PIPE_SIZE : PIPE_SIZE);
707 pipe->pipe_ino = alloc_unr64(&pipeino_unr);
713 pipe_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
714 int flags, struct thread *td)
724 * Try to avoid locking the pipe if we have nothing to do.
726 * There are programs which share one pipe amongst multiple processes
727 * and perform non-blocking reads in parallel, even if the pipe is
728 * empty. This in particular is the case with BSD make, which when
729 * spawned with a high -j number can find itself with over half of the
730 * calls failing to find anything.
732 if ((fp->f_flag & FNONBLOCK) != 0 && !mac_pipe_check_read_enabled()) {
733 if (__predict_false(uio->uio_resid == 0))
735 if ((atomic_load_short(&rpipe->pipe_state) & PIPE_EOF) == 0 &&
736 atomic_load_int(&rpipe->pipe_buffer.cnt) == 0 &&
737 atomic_load_int(&rpipe->pipe_pages.cnt) == 0)
743 error = pipelock(rpipe, 1);
748 error = mac_pipe_check_read(active_cred, rpipe->pipe_pair);
752 if (amountpipekva > (3 * maxpipekva) / 4) {
753 if ((rpipe->pipe_state & PIPE_DIRECTW) == 0 &&
754 rpipe->pipe_buffer.size > SMALL_PIPE_SIZE &&
755 rpipe->pipe_buffer.cnt <= SMALL_PIPE_SIZE &&
756 piperesizeallowed == 1) {
758 pipespace(rpipe, SMALL_PIPE_SIZE);
763 while (uio->uio_resid) {
765 * normal pipe buffer receive
767 if (rpipe->pipe_buffer.cnt > 0) {
768 size = rpipe->pipe_buffer.size - rpipe->pipe_buffer.out;
769 if (size > rpipe->pipe_buffer.cnt)
770 size = rpipe->pipe_buffer.cnt;
771 if (size > uio->uio_resid)
772 size = uio->uio_resid;
776 &rpipe->pipe_buffer.buffer[rpipe->pipe_buffer.out],
782 rpipe->pipe_buffer.out += size;
783 if (rpipe->pipe_buffer.out >= rpipe->pipe_buffer.size)
784 rpipe->pipe_buffer.out = 0;
786 rpipe->pipe_buffer.cnt -= size;
789 * If there is no more to read in the pipe, reset
790 * its pointers to the beginning. This improves
793 if (rpipe->pipe_buffer.cnt == 0) {
794 rpipe->pipe_buffer.in = 0;
795 rpipe->pipe_buffer.out = 0;
798 #ifndef PIPE_NODIRECT
800 * Direct copy, bypassing a kernel buffer.
802 } else if ((size = rpipe->pipe_pages.cnt) != 0) {
803 if (size > uio->uio_resid)
804 size = (u_int) uio->uio_resid;
806 error = uiomove_fromphys(rpipe->pipe_pages.ms,
807 rpipe->pipe_pages.pos, size, uio);
812 rpipe->pipe_pages.pos += size;
813 rpipe->pipe_pages.cnt -= size;
814 if (rpipe->pipe_pages.cnt == 0) {
815 rpipe->pipe_state &= ~PIPE_WANTW;
821 * detect EOF condition
822 * read returns 0 on EOF, no need to set error
824 if (rpipe->pipe_state & PIPE_EOF)
828 * If the "write-side" has been blocked, wake it up now.
830 if (rpipe->pipe_state & PIPE_WANTW) {
831 rpipe->pipe_state &= ~PIPE_WANTW;
836 * Break if some data was read.
842 * Unlock the pipe buffer for our remaining processing.
843 * We will either break out with an error or we will
844 * sleep and relock to loop.
849 * Handle non-blocking mode operation or
850 * wait for more data.
852 if (fp->f_flag & FNONBLOCK) {
855 rpipe->pipe_state |= PIPE_WANTR;
856 if ((error = msleep(rpipe, PIPE_MTX(rpipe),
859 error = pipelock(rpipe, 1);
870 /* XXX: should probably do this before getting any locks. */
872 pipe_timestamp(&rpipe->pipe_atime);
877 * PIPE_WANT processing only makes sense if pipe_busy is 0.
879 if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANT)) {
880 rpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTW);
882 } else if (rpipe->pipe_buffer.cnt < MINPIPESIZE) {
884 * Handle write blocking hysteresis.
886 if (rpipe->pipe_state & PIPE_WANTW) {
887 rpipe->pipe_state &= ~PIPE_WANTW;
893 * Only wake up writers if there was actually something read.
894 * Otherwise, when calling read(2) at EOF, a spurious wakeup occurs.
897 rpipe->pipe_buffer.size - rpipe->pipe_buffer.cnt >= PIPE_BUF)
898 pipeselwakeup(rpipe);
902 td->td_ru.ru_msgrcv++;
906 #ifndef PIPE_NODIRECT
908 * Map the sending processes' buffer into kernel space and wire it.
909 * This is similar to a physical write operation.
912 pipe_build_write_buffer(struct pipe *wpipe, struct uio *uio)
917 PIPE_LOCK_ASSERT(wpipe, MA_OWNED);
918 KASSERT((wpipe->pipe_state & PIPE_DIRECTW) == 0,
919 ("%s: PIPE_DIRECTW set on %p", __func__, wpipe));
920 KASSERT(wpipe->pipe_pages.cnt == 0,
921 ("%s: pipe map for %p contains residual data", __func__, wpipe));
923 if (uio->uio_iov->iov_len > wpipe->pipe_buffer.size)
924 size = wpipe->pipe_buffer.size;
926 size = uio->uio_iov->iov_len;
928 wpipe->pipe_state |= PIPE_DIRECTW;
930 i = vm_fault_quick_hold_pages(&curproc->p_vmspace->vm_map,
931 (vm_offset_t)uio->uio_iov->iov_base, size, VM_PROT_READ,
932 wpipe->pipe_pages.ms, PIPENPAGES);
935 wpipe->pipe_state &= ~PIPE_DIRECTW;
939 wpipe->pipe_pages.npages = i;
940 wpipe->pipe_pages.pos =
941 ((vm_offset_t) uio->uio_iov->iov_base) & PAGE_MASK;
942 wpipe->pipe_pages.cnt = size;
944 uio->uio_iov->iov_len -= size;
945 uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + size;
946 if (uio->uio_iov->iov_len == 0)
948 uio->uio_resid -= size;
949 uio->uio_offset += size;
954 * Unwire the process buffer.
957 pipe_destroy_write_buffer(struct pipe *wpipe)
960 PIPE_LOCK_ASSERT(wpipe, MA_OWNED);
961 KASSERT((wpipe->pipe_state & PIPE_DIRECTW) != 0,
962 ("%s: PIPE_DIRECTW not set on %p", __func__, wpipe));
963 KASSERT(wpipe->pipe_pages.cnt == 0,
964 ("%s: pipe map for %p contains residual data", __func__, wpipe));
966 wpipe->pipe_state &= ~PIPE_DIRECTW;
967 vm_page_unhold_pages(wpipe->pipe_pages.ms, wpipe->pipe_pages.npages);
968 wpipe->pipe_pages.npages = 0;
972 * In the case of a signal, the writing process might go away. This
973 * code copies the data into the circular buffer so that the source
974 * pages can be freed without loss of data.
977 pipe_clone_write_buffer(struct pipe *wpipe)
984 PIPE_LOCK_ASSERT(wpipe, MA_OWNED);
985 KASSERT((wpipe->pipe_state & PIPE_DIRECTW) != 0,
986 ("%s: PIPE_DIRECTW not set on %p", __func__, wpipe));
988 size = wpipe->pipe_pages.cnt;
989 pos = wpipe->pipe_pages.pos;
990 wpipe->pipe_pages.cnt = 0;
992 wpipe->pipe_buffer.in = size;
993 wpipe->pipe_buffer.out = 0;
994 wpipe->pipe_buffer.cnt = size;
997 iov.iov_base = wpipe->pipe_buffer.buffer;
1002 uio.uio_resid = size;
1003 uio.uio_segflg = UIO_SYSSPACE;
1004 uio.uio_rw = UIO_READ;
1005 uio.uio_td = curthread;
1006 uiomove_fromphys(wpipe->pipe_pages.ms, pos, size, &uio);
1008 pipe_destroy_write_buffer(wpipe);
1012 * This implements the pipe buffer write mechanism. Note that only
1013 * a direct write OR a normal pipe write can be pending at any given time.
1014 * If there are any characters in the pipe buffer, the direct write will
1015 * be deferred until the receiving process grabs all of the bytes from
1016 * the pipe buffer. Then the direct mapping write is set-up.
1019 pipe_direct_write(struct pipe *wpipe, struct uio *uio)
1024 PIPE_LOCK_ASSERT(wpipe, MA_OWNED);
1025 if ((wpipe->pipe_state & PIPE_EOF) != 0) {
1029 if (wpipe->pipe_state & PIPE_DIRECTW) {
1030 if (wpipe->pipe_state & PIPE_WANTR) {
1031 wpipe->pipe_state &= ~PIPE_WANTR;
1034 pipeselwakeup(wpipe);
1035 wpipe->pipe_state |= PIPE_WANTW;
1037 error = msleep(wpipe, PIPE_MTX(wpipe),
1038 PRIBIO | PCATCH, "pipdww", 0);
1044 if (wpipe->pipe_buffer.cnt > 0) {
1045 if (wpipe->pipe_state & PIPE_WANTR) {
1046 wpipe->pipe_state &= ~PIPE_WANTR;
1049 pipeselwakeup(wpipe);
1050 wpipe->pipe_state |= PIPE_WANTW;
1052 error = msleep(wpipe, PIPE_MTX(wpipe),
1053 PRIBIO | PCATCH, "pipdwc", 0);
1060 error = pipe_build_write_buffer(wpipe, uio);
1065 while (wpipe->pipe_pages.cnt != 0 &&
1066 (wpipe->pipe_state & PIPE_EOF) == 0) {
1067 if (wpipe->pipe_state & PIPE_WANTR) {
1068 wpipe->pipe_state &= ~PIPE_WANTR;
1071 pipeselwakeup(wpipe);
1072 wpipe->pipe_state |= PIPE_WANTW;
1074 error = msleep(wpipe, PIPE_MTX(wpipe), PRIBIO | PCATCH,
1081 if ((wpipe->pipe_state & PIPE_EOF) != 0) {
1082 wpipe->pipe_pages.cnt = 0;
1083 pipe_destroy_write_buffer(wpipe);
1084 pipeselwakeup(wpipe);
1086 } else if (error == EINTR || error == ERESTART) {
1087 pipe_clone_write_buffer(wpipe);
1089 pipe_destroy_write_buffer(wpipe);
1091 KASSERT((wpipe->pipe_state & PIPE_DIRECTW) == 0,
1092 ("pipe %p leaked PIPE_DIRECTW", wpipe));
1102 pipe_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
1103 int flags, struct thread *td)
1105 struct pipe *wpipe, *rpipe;
1107 int desiredsize, error;
1110 wpipe = PIPE_PEER(rpipe);
1112 error = pipelock(wpipe, 1);
1118 * detect loss of pipe read side, issue SIGPIPE if lost.
1120 if (wpipe->pipe_present != PIPE_ACTIVE ||
1121 (wpipe->pipe_state & PIPE_EOF)) {
1127 error = mac_pipe_check_write(active_cred, wpipe->pipe_pair);
1136 /* Choose a larger size if it's advantageous */
1137 desiredsize = max(SMALL_PIPE_SIZE, wpipe->pipe_buffer.size);
1138 while (desiredsize < wpipe->pipe_buffer.cnt + uio->uio_resid) {
1139 if (piperesizeallowed != 1)
1141 if (amountpipekva > maxpipekva / 2)
1143 if (desiredsize == BIG_PIPE_SIZE)
1145 desiredsize = desiredsize * 2;
1148 /* Choose a smaller size if we're in a OOM situation */
1149 if (amountpipekva > (3 * maxpipekva) / 4 &&
1150 wpipe->pipe_buffer.size > SMALL_PIPE_SIZE &&
1151 wpipe->pipe_buffer.cnt <= SMALL_PIPE_SIZE &&
1152 piperesizeallowed == 1)
1153 desiredsize = SMALL_PIPE_SIZE;
1155 /* Resize if the above determined that a new size was necessary */
1156 if (desiredsize != wpipe->pipe_buffer.size &&
1157 (wpipe->pipe_state & PIPE_DIRECTW) == 0) {
1159 pipespace(wpipe, desiredsize);
1162 MPASS(wpipe->pipe_buffer.size != 0);
1164 orig_resid = uio->uio_resid;
1166 while (uio->uio_resid) {
1169 if (wpipe->pipe_state & PIPE_EOF) {
1173 #ifndef PIPE_NODIRECT
1175 * If the transfer is large, we can gain performance if
1176 * we do process-to-process copies directly.
1177 * If the write is non-blocking, we don't use the
1178 * direct write mechanism.
1180 * The direct write mechanism will detect the reader going
1183 if (uio->uio_segflg == UIO_USERSPACE &&
1184 uio->uio_iov->iov_len >= pipe_mindirect &&
1185 wpipe->pipe_buffer.size >= pipe_mindirect &&
1186 (fp->f_flag & FNONBLOCK) == 0) {
1187 error = pipe_direct_write(wpipe, uio);
1195 * Pipe buffered writes cannot be coincidental with
1196 * direct writes. We wait until the currently executing
1197 * direct write is completed before we start filling the
1198 * pipe buffer. We break out if a signal occurs or the
1201 if (wpipe->pipe_pages.cnt != 0) {
1202 if (wpipe->pipe_state & PIPE_WANTR) {
1203 wpipe->pipe_state &= ~PIPE_WANTR;
1206 pipeselwakeup(wpipe);
1207 wpipe->pipe_state |= PIPE_WANTW;
1209 error = msleep(wpipe, PIPE_MTX(rpipe), PRIBIO | PCATCH,
1217 space = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
1219 /* Writes of size <= PIPE_BUF must be atomic. */
1220 if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF))
1224 int size; /* Transfer size */
1225 int segsize; /* first segment to transfer */
1228 * Transfer size is minimum of uio transfer
1229 * and free space in pipe buffer.
1231 if (space > uio->uio_resid)
1232 size = uio->uio_resid;
1236 * First segment to transfer is minimum of
1237 * transfer size and contiguous space in
1238 * pipe buffer. If first segment to transfer
1239 * is less than the transfer size, we've got
1240 * a wraparound in the buffer.
1242 segsize = wpipe->pipe_buffer.size -
1243 wpipe->pipe_buffer.in;
1247 /* Transfer first segment */
1250 error = uiomove(&wpipe->pipe_buffer.buffer[wpipe->pipe_buffer.in],
1254 if (error == 0 && segsize < size) {
1255 KASSERT(wpipe->pipe_buffer.in + segsize ==
1256 wpipe->pipe_buffer.size,
1257 ("Pipe buffer wraparound disappeared"));
1259 * Transfer remaining part now, to
1260 * support atomic writes. Wraparound
1266 &wpipe->pipe_buffer.buffer[0],
1267 size - segsize, uio);
1271 wpipe->pipe_buffer.in += size;
1272 if (wpipe->pipe_buffer.in >=
1273 wpipe->pipe_buffer.size) {
1274 KASSERT(wpipe->pipe_buffer.in ==
1276 wpipe->pipe_buffer.size,
1277 ("Expected wraparound bad"));
1278 wpipe->pipe_buffer.in = size - segsize;
1281 wpipe->pipe_buffer.cnt += size;
1282 KASSERT(wpipe->pipe_buffer.cnt <=
1283 wpipe->pipe_buffer.size,
1284 ("Pipe buffer overflow"));
1291 * If the "read-side" has been blocked, wake it up now.
1293 if (wpipe->pipe_state & PIPE_WANTR) {
1294 wpipe->pipe_state &= ~PIPE_WANTR;
1299 * don't block on non-blocking I/O
1301 if (fp->f_flag & FNONBLOCK) {
1307 * We have no more space and have something to offer,
1308 * wake up select/poll.
1310 pipeselwakeup(wpipe);
1312 wpipe->pipe_state |= PIPE_WANTW;
1314 error = msleep(wpipe, PIPE_MTX(rpipe),
1315 PRIBIO | PCATCH, "pipewr", 0);
1325 if ((wpipe->pipe_busy == 0) && (wpipe->pipe_state & PIPE_WANT)) {
1326 wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR);
1328 } else if (wpipe->pipe_buffer.cnt > 0) {
1330 * If we have put any characters in the buffer, we wake up
1333 if (wpipe->pipe_state & PIPE_WANTR) {
1334 wpipe->pipe_state &= ~PIPE_WANTR;
1340 * Don't return EPIPE if any byte was written.
1341 * EINTR and other interrupts are handled by generic I/O layer.
1342 * Do not pretend that I/O succeeded for obvious user error
1345 if (uio->uio_resid != orig_resid && error == EPIPE)
1349 pipe_timestamp(&wpipe->pipe_mtime);
1352 * We have something to offer,
1353 * wake up select/poll.
1355 if (wpipe->pipe_buffer.cnt)
1356 pipeselwakeup(wpipe);
1360 if (uio->uio_resid != orig_resid)
1361 td->td_ru.ru_msgsnd++;
1367 pipe_truncate(struct file *fp, off_t length, struct ucred *active_cred,
1374 if (cpipe->pipe_type & PIPE_TYPE_NAMED)
1375 error = vnops.fo_truncate(fp, length, active_cred, td);
1377 error = invfo_truncate(fp, length, active_cred, td);
1382 * we implement a very minimal set of ioctls for compatibility with sockets.
1385 pipe_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *active_cred,
1388 struct pipe *mpipe = fp->f_data;
1394 error = mac_pipe_check_ioctl(active_cred, mpipe->pipe_pair, cmd, data);
1408 mpipe->pipe_state |= PIPE_ASYNC;
1410 mpipe->pipe_state &= ~PIPE_ASYNC;
1415 if (!(fp->f_flag & FREAD)) {
1420 if (mpipe->pipe_pages.cnt != 0)
1421 *(int *)data = mpipe->pipe_pages.cnt;
1423 *(int *)data = mpipe->pipe_buffer.cnt;
1428 error = fsetown(*(int *)data, &mpipe->pipe_sigio);
1432 *(int *)data = fgetown(&mpipe->pipe_sigio);
1435 /* This is deprecated, FIOSETOWN should be used instead. */
1438 error = fsetown(-(*(int *)data), &mpipe->pipe_sigio);
1441 /* This is deprecated, FIOGETOWN should be used instead. */
1443 *(int *)data = -fgetown(&mpipe->pipe_sigio);
1456 pipe_poll(struct file *fp, int events, struct ucred *active_cred,
1461 int levents, revents;
1468 wpipe = PIPE_PEER(rpipe);
1471 error = mac_pipe_check_poll(active_cred, rpipe->pipe_pair);
1475 if (fp->f_flag & FREAD && events & (POLLIN | POLLRDNORM))
1476 if (rpipe->pipe_pages.cnt > 0 || rpipe->pipe_buffer.cnt > 0)
1477 revents |= events & (POLLIN | POLLRDNORM);
1479 if (fp->f_flag & FWRITE && events & (POLLOUT | POLLWRNORM))
1480 if (wpipe->pipe_present != PIPE_ACTIVE ||
1481 (wpipe->pipe_state & PIPE_EOF) ||
1482 ((wpipe->pipe_state & PIPE_DIRECTW) == 0 &&
1483 ((wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF ||
1484 wpipe->pipe_buffer.size == 0)))
1485 revents |= events & (POLLOUT | POLLWRNORM);
1488 (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM | POLLRDBAND);
1489 if (rpipe->pipe_type & PIPE_TYPE_NAMED && fp->f_flag & FREAD && levents &&
1490 fp->f_pipegen == rpipe->pipe_wgen)
1491 events |= POLLINIGNEOF;
1493 if ((events & POLLINIGNEOF) == 0) {
1494 if (rpipe->pipe_state & PIPE_EOF) {
1495 if (fp->f_flag & FREAD)
1496 revents |= (events & (POLLIN | POLLRDNORM));
1497 if (wpipe->pipe_present != PIPE_ACTIVE ||
1498 (wpipe->pipe_state & PIPE_EOF))
1505 * Add ourselves regardless of eventmask as we have to return
1506 * POLLHUP even if it was not asked for.
1508 if ((fp->f_flag & FREAD) != 0) {
1509 selrecord(td, &rpipe->pipe_sel);
1510 if (SEL_WAITING(&rpipe->pipe_sel))
1511 rpipe->pipe_state |= PIPE_SEL;
1514 if ((fp->f_flag & FWRITE) != 0 &&
1515 wpipe->pipe_present == PIPE_ACTIVE) {
1516 selrecord(td, &wpipe->pipe_sel);
1517 if (SEL_WAITING(&wpipe->pipe_sel))
1518 wpipe->pipe_state |= PIPE_SEL;
1530 * We shouldn't need locks here as we're doing a read and this should
1531 * be a natural race.
1534 pipe_stat(struct file *fp, struct stat *ub, struct ucred *active_cred)
1543 if (mac_pipe_check_stat_enabled()) {
1545 error = mac_pipe_check_stat(active_cred, pipe->pipe_pair);
1553 /* For named pipes ask the underlying filesystem. */
1554 if (pipe->pipe_type & PIPE_TYPE_NAMED) {
1555 return (vnops.fo_stat(fp, ub, active_cred));
1558 bzero(ub, sizeof(*ub));
1559 ub->st_mode = S_IFIFO;
1560 ub->st_blksize = PAGE_SIZE;
1561 if (pipe->pipe_pages.cnt != 0)
1562 ub->st_size = pipe->pipe_pages.cnt;
1564 ub->st_size = pipe->pipe_buffer.cnt;
1565 ub->st_blocks = howmany(ub->st_size, ub->st_blksize);
1566 ub->st_atim = pipe->pipe_atime;
1567 ub->st_mtim = pipe->pipe_mtime;
1568 ub->st_ctim = pipe->pipe_ctime;
1569 ub->st_uid = fp->f_cred->cr_uid;
1570 ub->st_gid = fp->f_cred->cr_gid;
1571 ub->st_dev = pipedev_ino;
1572 ub->st_ino = pipe->pipe_ino;
1574 * Left as 0: st_nlink, st_rdev, st_flags, st_gen.
1581 pipe_close(struct file *fp, struct thread *td)
1584 if (fp->f_vnode != NULL)
1585 return vnops.fo_close(fp, td);
1586 fp->f_ops = &badfileops;
1587 pipe_dtor(fp->f_data);
1593 pipe_chmod(struct file *fp, mode_t mode, struct ucred *active_cred, struct thread *td)
1599 if (cpipe->pipe_type & PIPE_TYPE_NAMED)
1600 error = vn_chmod(fp, mode, active_cred, td);
1602 error = invfo_chmod(fp, mode, active_cred, td);
1607 pipe_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
1614 if (cpipe->pipe_type & PIPE_TYPE_NAMED)
1615 error = vn_chown(fp, uid, gid, active_cred, td);
1617 error = invfo_chown(fp, uid, gid, active_cred, td);
1622 pipe_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
1626 if (fp->f_type == DTYPE_FIFO)
1627 return (vn_fill_kinfo(fp, kif, fdp));
1628 kif->kf_type = KF_TYPE_PIPE;
1630 kif->kf_un.kf_pipe.kf_pipe_addr = (uintptr_t)pi;
1631 kif->kf_un.kf_pipe.kf_pipe_peer = (uintptr_t)pi->pipe_peer;
1632 kif->kf_un.kf_pipe.kf_pipe_buffer_cnt = pi->pipe_buffer.cnt;
1633 kif->kf_un.kf_pipe.kf_pipe_buffer_in = pi->pipe_buffer.in;
1634 kif->kf_un.kf_pipe.kf_pipe_buffer_out = pi->pipe_buffer.out;
1635 kif->kf_un.kf_pipe.kf_pipe_buffer_size = pi->pipe_buffer.size;
1640 pipe_free_kmem(struct pipe *cpipe)
1643 KASSERT(!mtx_owned(PIPE_MTX(cpipe)),
1644 ("pipe_free_kmem: pipe mutex locked"));
1646 if (cpipe->pipe_buffer.buffer != NULL) {
1647 atomic_subtract_long(&amountpipekva, cpipe->pipe_buffer.size);
1648 vm_map_remove(pipe_map,
1649 (vm_offset_t)cpipe->pipe_buffer.buffer,
1650 (vm_offset_t)cpipe->pipe_buffer.buffer + cpipe->pipe_buffer.size);
1651 cpipe->pipe_buffer.buffer = NULL;
1653 #ifndef PIPE_NODIRECT
1655 cpipe->pipe_pages.cnt = 0;
1656 cpipe->pipe_pages.pos = 0;
1657 cpipe->pipe_pages.npages = 0;
1666 pipeclose(struct pipe *cpipe)
1669 struct pipepair *pp;
1673 KASSERT(cpipe != NULL, ("pipeclose: cpipe == NULL"));
1678 pp = cpipe->pipe_pair;
1682 * If the other side is blocked, wake it up saying that
1683 * we want to close it down.
1685 cpipe->pipe_state |= PIPE_EOF;
1686 while (cpipe->pipe_busy) {
1688 cpipe->pipe_state |= PIPE_WANT;
1690 msleep(cpipe, PIPE_MTX(cpipe), PRIBIO, "pipecl", 0);
1694 pipeselwakeup(cpipe);
1697 * Disconnect from peer, if any.
1699 ppipe = cpipe->pipe_peer;
1700 if (ppipe->pipe_present == PIPE_ACTIVE) {
1701 ppipe->pipe_state |= PIPE_EOF;
1703 pipeselwakeup(ppipe);
1707 * Mark this endpoint as free. Release kmem resources. We
1708 * don't mark this endpoint as unused until we've finished
1709 * doing that, or the pipe might disappear out from under
1713 pipe_free_kmem(cpipe);
1715 cpipe->pipe_present = PIPE_CLOSING;
1719 * knlist_clear() may sleep dropping the PIPE_MTX. Set the
1720 * PIPE_FINALIZED, that allows other end to free the
1721 * pipe_pair, only after the knotes are completely dismantled.
1723 knlist_clear(&cpipe->pipe_sel.si_note, 1);
1724 cpipe->pipe_present = PIPE_FINALIZED;
1725 seldrain(&cpipe->pipe_sel);
1726 knlist_destroy(&cpipe->pipe_sel.si_note);
1729 * If both endpoints are now closed, release the memory for the
1730 * pipe pair. If not, unlock.
1732 if (ppipe->pipe_present == PIPE_FINALIZED) {
1735 mac_pipe_destroy(pp);
1737 uma_zfree(pipe_zone, cpipe->pipe_pair);
1744 pipe_kqfilter(struct file *fp, struct knote *kn)
1749 * If a filter is requested that is not supported by this file
1750 * descriptor, don't return an error, but also don't ever generate an
1753 if ((kn->kn_filter == EVFILT_READ) && !(fp->f_flag & FREAD)) {
1754 kn->kn_fop = &pipe_nfiltops;
1757 if ((kn->kn_filter == EVFILT_WRITE) && !(fp->f_flag & FWRITE)) {
1758 kn->kn_fop = &pipe_nfiltops;
1763 switch (kn->kn_filter) {
1765 kn->kn_fop = &pipe_rfiltops;
1768 kn->kn_fop = &pipe_wfiltops;
1769 if (cpipe->pipe_peer->pipe_present != PIPE_ACTIVE) {
1770 /* other end of pipe has been closed */
1774 cpipe = PIPE_PEER(cpipe);
1777 if ((cpipe->pipe_type & PIPE_TYPE_NAMED) != 0) {
1779 return (vnops.fo_kqfilter(fp, kn));
1785 kn->kn_hook = cpipe;
1786 knlist_add(&cpipe->pipe_sel.si_note, kn, 1);
1792 filt_pipedetach(struct knote *kn)
1794 struct pipe *cpipe = kn->kn_hook;
1797 knlist_remove(&cpipe->pipe_sel.si_note, kn, 1);
1803 filt_piperead(struct knote *kn, long hint)
1805 struct file *fp = kn->kn_fp;
1806 struct pipe *rpipe = kn->kn_hook;
1808 PIPE_LOCK_ASSERT(rpipe, MA_OWNED);
1809 kn->kn_data = rpipe->pipe_buffer.cnt;
1810 if (kn->kn_data == 0)
1811 kn->kn_data = rpipe->pipe_pages.cnt;
1813 if ((rpipe->pipe_state & PIPE_EOF) != 0 &&
1814 ((rpipe->pipe_type & PIPE_TYPE_NAMED) == 0 ||
1815 fp->f_pipegen != rpipe->pipe_wgen)) {
1816 kn->kn_flags |= EV_EOF;
1819 kn->kn_flags &= ~EV_EOF;
1820 return (kn->kn_data > 0);
1825 filt_pipewrite(struct knote *kn, long hint)
1827 struct pipe *wpipe = kn->kn_hook;
1830 * If this end of the pipe is closed, the knote was removed from the
1831 * knlist and the list lock (i.e., the pipe lock) is therefore not held.
1833 if (wpipe->pipe_present == PIPE_ACTIVE ||
1834 (wpipe->pipe_type & PIPE_TYPE_NAMED) != 0) {
1835 PIPE_LOCK_ASSERT(wpipe, MA_OWNED);
1837 if (wpipe->pipe_state & PIPE_DIRECTW) {
1839 } else if (wpipe->pipe_buffer.size > 0) {
1840 kn->kn_data = wpipe->pipe_buffer.size -
1841 wpipe->pipe_buffer.cnt;
1843 kn->kn_data = PIPE_BUF;
1847 if (wpipe->pipe_present != PIPE_ACTIVE ||
1848 (wpipe->pipe_state & PIPE_EOF)) {
1849 kn->kn_flags |= EV_EOF;
1852 kn->kn_flags &= ~EV_EOF;
1853 return (kn->kn_data >= PIPE_BUF);
1857 filt_pipedetach_notsup(struct knote *kn)
1863 filt_pipenotsup(struct knote *kn, long hint)