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
20 * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved.
22 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
24 * This file contains Original Code and/or Modifications of Original Code
25 * as defined in and that are subject to the Apple Public Source License
26 * Version 2.0 (the 'License'). You may not use this file except in
27 * compliance with the License. The rights granted to you under the
28 * License may not be used to create, or enable the creation or
29 * redistribution of, unlawful or unlicensed copies of an Apple operating
30 * system, or to circumvent, violate, or enable the circumvention or
31 * violation of, any terms of an Apple operating system software license
34 * Please obtain a copy of the License at
35 * http://www.opensource.apple.com/apsl/ and read it before using this
38 * The Original Code and all software distributed under the License are
39 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
40 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
41 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
42 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
43 * Please see the License for the specific language governing rights and
44 * limitations under the License.
46 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
50 * This file contains a high-performance replacement for the socket-based
51 * pipes scheme originally used in FreeBSD/4.4Lite. It does not support
52 * all features of sockets, but does do everything that pipes normally
57 * This code has two modes of operation, a small write mode and a large
58 * write mode. The small write mode acts like conventional pipes with
59 * a kernel buffer. If the buffer is less than PIPE_MINDIRECT, then the
60 * "normal" pipe buffering is done. If the buffer is between PIPE_MINDIRECT
61 * and PIPE_SIZE in size, it is fully mapped and wired into the kernel, and
62 * the receiving process can copy it directly from the pages in the sending
65 * If the sending process receives a signal, it is possible that it will
66 * go away, and certainly its address space can change, because control
67 * is returned back to the user-mode side. In that case, the pipe code
68 * arranges to copy the buffer supplied by the user process, to a pageable
69 * kernel buffer, and the receiving process will grab the data from the
70 * pageable kernel buffer. Since signals don't happen all that often,
71 * the copy operation is normally eliminated.
73 * The constant PIPE_MINDIRECT is chosen to make sure that buffering will
74 * happen for small transfers so that the system will not spend all of
75 * its time context switching.
77 * In order to limit the resource use of pipes, two sysctls exist:
79 * kern.ipc.maxpipekva - This is a hard limit on the amount of pageable
80 * address space available to us in pipe_map. Whenever the amount in use
81 * exceeds half of this value, all new pipes will be created with size
82 * SMALL_PIPE_SIZE, rather than PIPE_SIZE. Big pipe creation will be limited
83 * as well. This value is loader tunable only.
85 * kern.ipc.maxpipekvawired - This value limits the amount of memory that may
86 * be wired in order to facilitate direct copies using page flipping.
87 * Whenever this value is exceeded, pipes will fall back to using regular
88 * copies. This value is sysctl controllable at all times.
90 * These values are autotuned in subr_param.c.
92 * Memory usage may be monitored through the sysctls
93 * kern.ipc.pipes, kern.ipc.pipekva and kern.ipc.pipekvawired.
97 #include <sys/param.h>
98 #include <sys/systm.h>
99 #include <sys/filedesc.h>
100 #include <sys/kernel.h>
101 #include <sys/vnode.h>
102 #include <sys/proc_internal.h>
103 #include <sys/kauth.h>
104 #include <sys/file_internal.h>
105 #include <sys/stat.h>
106 #include <sys/ioctl.h>
107 #include <sys/fcntl.h>
108 #include <sys/malloc.h>
109 #include <sys/syslog.h>
110 #include <sys/unistd.h>
111 #include <sys/resourcevar.h>
112 #include <sys/aio_kern.h>
113 #include <sys/signalvar.h>
114 #include <sys/pipe.h>
115 #include <sys/sysproto.h>
116 #include <sys/proc_info.h>
118 #include <bsm/audit_kernel.h>
120 #include <sys/kdebug.h>
122 #include <kern/zalloc.h>
123 #include <vm/vm_kern.h>
124 #include <libkern/OSAtomic.h>
126 #define f_flag f_fglob->fg_flag
127 #define f_type f_fglob->fg_type
128 #define f_msgcount f_fglob->fg_msgcount
129 #define f_cred f_fglob->fg_cred
130 #define f_ops f_fglob->fg_ops
131 #define f_offset f_fglob->fg_offset
132 #define f_data f_fglob->fg_data
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 * this needs to be ported to X and the performance measured
139 * before committing to supporting it
141 #define PIPE_NODIRECT 1
143 #ifndef PIPE_NODIRECT
146 #include <vm/vm_param.h>
147 #include <vm/vm_object.h>
148 #include <vm/vm_kern.h>
149 #include <vm/vm_extern.h>
151 #include <vm/vm_map.h>
152 #include <vm/vm_page.h>
159 * interfaces to the outside world
161 static int pipe_read(struct fileproc
*fp
, struct uio
*uio
,
162 kauth_cred_t cred
, int flags
, struct proc
*p
);
164 static int pipe_write(struct fileproc
*fp
, struct uio
*uio
,
165 kauth_cred_t cred
, int flags
, struct proc
*p
);
167 static int pipe_close(struct fileglob
*fg
, struct proc
*p
);
169 static int pipe_select(struct fileproc
*fp
, int which
, void * wql
, struct proc
*p
);
171 static int pipe_kqfilter(struct fileproc
*fp
, struct knote
*kn
, struct proc
*p
);
173 static int pipe_ioctl(struct fileproc
*fp
, u_long cmd
, caddr_t data
, struct proc
*p
);
176 struct fileops pipeops
=
186 static void filt_pipedetach(struct knote
*kn
);
187 static int filt_piperead(struct knote
*kn
, long hint
);
188 static int filt_pipewrite(struct knote
*kn
, long hint
);
190 static struct filterops pipe_rfiltops
=
191 { 1, NULL
, filt_pipedetach
, filt_piperead
};
192 static struct filterops pipe_wfiltops
=
193 { 1, NULL
, filt_pipedetach
, filt_pipewrite
};
196 * Default pipe buffer size(s), this can be kind-of large now because pipe
197 * space is pageable. The pipe code will try to maintain locality of
198 * reference for performance reasons, so small amounts of outstanding I/O
199 * will not wipe the cache.
201 #define MINPIPESIZE (PIPE_SIZE/3)
204 * Limit the number of "big" pipes
206 #define LIMITBIGPIPES 32
209 static int amountpipes
;
210 static int amountpipekva
;
212 #ifndef PIPE_NODIRECT
213 static int amountpipekvawired
;
215 int maxpipekva
= 1024 * 1024 * 16;
218 SYSCTL_DECL(_kern_ipc
);
220 SYSCTL_INT(_kern_ipc
, OID_AUTO
, maxpipekva
, CTLFLAG_RD
,
221 &maxpipekva
, 0, "Pipe KVA limit");
222 SYSCTL_INT(_kern_ipc
, OID_AUTO
, maxpipekvawired
, CTLFLAG_RW
,
223 &maxpipekvawired
, 0, "Pipe KVA wired limit");
224 SYSCTL_INT(_kern_ipc
, OID_AUTO
, pipes
, CTLFLAG_RD
,
225 &amountpipes
, 0, "Current # of pipes");
226 SYSCTL_INT(_kern_ipc
, OID_AUTO
, bigpipes
, CTLFLAG_RD
,
227 &nbigpipe
, 0, "Current # of big pipes");
228 SYSCTL_INT(_kern_ipc
, OID_AUTO
, pipekva
, CTLFLAG_RD
,
229 &amountpipekva
, 0, "Pipe KVA usage");
230 SYSCTL_INT(_kern_ipc
, OID_AUTO
, pipekvawired
, CTLFLAG_RD
,
231 &amountpipekvawired
, 0, "Pipe wired KVA usage");
234 void pipeinit(void *dummy __unused
);
235 static void pipeclose(struct pipe
*cpipe
);
236 static void pipe_free_kmem(struct pipe
*cpipe
);
237 static int pipe_create(struct pipe
**cpipep
);
238 static void pipeselwakeup(struct pipe
*cpipe
, struct pipe
*spipe
);
239 static __inline
int pipelock(struct pipe
*cpipe
, int catch);
240 static __inline
void pipeunlock(struct pipe
*cpipe
);
242 #ifndef PIPE_NODIRECT
243 static int pipe_build_write_buffer(struct pipe
*wpipe
, struct uio
*uio
);
244 static void pipe_destroy_write_buffer(struct pipe
*wpipe
);
245 static int pipe_direct_write(struct pipe
*wpipe
, struct uio
*uio
);
246 static void pipe_clone_write_buffer(struct pipe
*wpipe
);
249 extern int postpipeevent(struct pipe
*, int);
250 extern void evpipefree(struct pipe
*cpipe
);
253 static int pipespace(struct pipe
*cpipe
, int size
);
255 static lck_grp_t
*pipe_mtx_grp
;
256 static lck_attr_t
*pipe_mtx_attr
;
257 static lck_grp_attr_t
*pipe_mtx_grp_attr
;
259 static zone_t pipe_zone
;
261 SYSINIT(vfs
, SI_SUB_VFS
, SI_ORDER_ANY
, pipeinit
, NULL
);
264 pipeinit(void *dummy __unused
)
266 pipe_zone
= (zone_t
)zinit(sizeof(struct pipe
), 8192 * sizeof(struct pipe
), 4096, "pipe zone");
269 * allocate lock group attribute and group for pipe mutexes
271 pipe_mtx_grp_attr
= lck_grp_attr_alloc_init();
272 pipe_mtx_grp
= lck_grp_alloc_init("pipe", pipe_mtx_grp_attr
);
275 * allocate the lock attribute for pipe mutexes
277 pipe_mtx_attr
= lck_attr_alloc_init();
283 * The pipe system call for the DTYPE_PIPE type of pipes
288 pipe(struct proc
*p
, __unused
struct pipe_args
*uap
, register_t
*retval
)
290 struct fileproc
*rf
, *wf
;
291 struct pipe
*rpipe
, *wpipe
;
295 if ((pmtx
= lck_mtx_alloc_init(pipe_mtx_grp
, pipe_mtx_attr
)) == NULL
)
298 rpipe
= wpipe
= NULL
;
299 if (pipe_create(&rpipe
) || pipe_create(&wpipe
)) {
304 * allocate the space for the normal I/O direction up
305 * front... we'll delay the allocation for the other
306 * direction until a write actually occurs (most
307 * likely it won't)...
309 * Reduce to 1/4th pipe size if we're over our global max.
311 if (amountpipekva
> maxpipekva
/ 2)
312 error
= pipespace(rpipe
, SMALL_PIPE_SIZE
);
314 error
= pipespace(rpipe
, PIPE_SIZE
);
318 #ifndef PIPE_NODIRECT
319 rpipe
->pipe_state
|= PIPE_DIRECTOK
;
320 wpipe
->pipe_state
|= PIPE_DIRECTOK
;
322 TAILQ_INIT(&rpipe
->pipe_evlist
);
323 TAILQ_INIT(&wpipe
->pipe_evlist
);
325 error
= falloc(p
, &rf
, &fd
);
332 * for now we'll create half-duplex
333 * pipes... this is what we've always
337 rf
->f_type
= DTYPE_PIPE
;
338 rf
->f_data
= (caddr_t
)rpipe
;
339 rf
->f_ops
= &pipeops
;
341 error
= falloc(p
, &wf
, &fd
);
343 fp_free(p
, retval
[0], rf
);
347 wf
->f_type
= DTYPE_PIPE
;
348 wf
->f_data
= (caddr_t
)wpipe
;
349 wf
->f_ops
= &pipeops
;
354 * XXXXXXXX SHOULD NOT HOLD FILE_LOCK() XXXXXXXXXXXX
356 * struct pipe represents a pipe endpoint. The MAC label is shared
357 * between the connected endpoints. As a result mac_init_pipe() and
358 * mac_create_pipe() should only be called on one of the endpoints
359 * after they have been connected.
361 mac_init_pipe(rpipe
);
362 mac_create_pipe(td
->td_ucred
, rpipe
);
365 *fdflags(p
, retval
[0]) &= ~UF_RESERVED
;
366 *fdflags(p
, retval
[1]) &= ~UF_RESERVED
;
367 fp_drop(p
, retval
[0], rf
, 1);
368 fp_drop(p
, retval
[1], wf
, 1);
371 rpipe
->pipe_peer
= wpipe
;
372 wpipe
->pipe_peer
= rpipe
;
374 rpipe
->pipe_mtxp
= wpipe
->pipe_mtxp
= pmtx
;
381 lck_mtx_free(pmtx
, pipe_mtx_grp
);
388 pipe_stat(struct pipe
*cpipe
, struct stat
*ub
)
399 error
= mac_check_pipe_stat(active_cred
, cpipe
);
404 if (cpipe
->pipe_buffer
.buffer
== 0) {
406 * must be stat'ing the write fd
408 cpipe
= cpipe
->pipe_peer
;
413 bzero(ub
, sizeof(*ub
));
414 ub
->st_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
415 ub
->st_blksize
= cpipe
->pipe_buffer
.size
;
416 ub
->st_size
= cpipe
->pipe_buffer
.cnt
;
417 ub
->st_blocks
= (ub
->st_size
+ ub
->st_blksize
- 1) / ub
->st_blksize
;
420 ub
->st_uid
= kauth_getuid();
421 ub
->st_gid
= kauth_getgid();
424 ub
->st_atimespec
.tv_sec
= now
.tv_sec
;
425 ub
->st_atimespec
.tv_nsec
= now
.tv_usec
* 1000;
427 ub
->st_mtimespec
.tv_sec
= now
.tv_sec
;
428 ub
->st_mtimespec
.tv_nsec
= now
.tv_usec
* 1000;
430 ub
->st_ctimespec
.tv_sec
= now
.tv_sec
;
431 ub
->st_ctimespec
.tv_nsec
= now
.tv_usec
* 1000;
434 * Left as 0: st_dev, st_ino, st_nlink, st_rdev, st_flags, st_gen, st_uid, st_gid.
435 * XXX (st_dev, st_ino) should be unique.
442 * Allocate kva for pipe circular buffer, the space is pageable
443 * This routine will 'realloc' the size of a pipe safely, if it fails
444 * it will retain the old buffer.
445 * If it fails it will return ENOMEM.
448 pipespace(struct pipe
*cpipe
, int size
)
452 size
= round_page(size
);
454 if (kmem_alloc(kernel_map
, &buffer
, size
) != KERN_SUCCESS
)
457 /* free old resources if we're resizing */
458 pipe_free_kmem(cpipe
);
459 cpipe
->pipe_buffer
.buffer
= (caddr_t
)buffer
;
460 cpipe
->pipe_buffer
.size
= size
;
461 cpipe
->pipe_buffer
.in
= 0;
462 cpipe
->pipe_buffer
.out
= 0;
463 cpipe
->pipe_buffer
.cnt
= 0;
465 OSAddAtomic(1, (SInt32
*)&amountpipes
);
466 OSAddAtomic(cpipe
->pipe_buffer
.size
, (SInt32
*)&amountpipekva
);
472 * initialize and allocate VM and memory for pipe
475 pipe_create(struct pipe
**cpipep
)
479 cpipe
= (struct pipe
*)zalloc(pipe_zone
);
481 if ((*cpipep
= cpipe
) == NULL
)
485 * protect so pipespace or pipeclose don't follow a junk pointer
486 * if pipespace() fails.
488 bzero(cpipe
, sizeof *cpipe
);
495 * lock a pipe for I/O, blocking other access
498 pipelock(cpipe
, catch)
504 while (cpipe
->pipe_state
& PIPE_LOCKFL
) {
505 cpipe
->pipe_state
|= PIPE_LWANT
;
507 error
= msleep(cpipe
, PIPE_MTX(cpipe
), catch ? (PRIBIO
| PCATCH
) : PRIBIO
,
512 cpipe
->pipe_state
|= PIPE_LOCKFL
;
518 * unlock a pipe I/O lock
525 cpipe
->pipe_state
&= ~PIPE_LOCKFL
;
527 if (cpipe
->pipe_state
& PIPE_LWANT
) {
528 cpipe
->pipe_state
&= ~PIPE_LWANT
;
534 pipeselwakeup(cpipe
, spipe
)
539 if (cpipe
->pipe_state
& PIPE_SEL
) {
540 cpipe
->pipe_state
&= ~PIPE_SEL
;
541 selwakeup(&cpipe
->pipe_sel
);
543 if (cpipe
->pipe_state
& PIPE_KNOTE
)
544 KNOTE(&cpipe
->pipe_sel
.si_note
, 1);
546 postpipeevent(cpipe
, EV_RWBYTES
);
548 if (spipe
&& (spipe
->pipe_state
& PIPE_ASYNC
) && spipe
->pipe_pgid
) {
551 if (spipe
->pipe_pgid
< 0)
552 gsignal(-spipe
->pipe_pgid
, SIGIO
);
553 else if ((p
= pfind(spipe
->pipe_pgid
)) != (struct proc
*)0)
560 pipe_read(struct fileproc
*fp
, struct uio
*uio
, __unused kauth_cred_t active_cred
, __unused
int flags
, __unused
struct proc
*p
)
562 struct pipe
*rpipe
= (struct pipe
*)fp
->f_data
;
570 error
= pipelock(rpipe
, 1);
575 error
= mac_check_pipe_read(active_cred
, rpipe
);
580 while (uio_resid(uio
)) {
582 * normal pipe buffer receive
584 if (rpipe
->pipe_buffer
.cnt
> 0) {
585 size
= rpipe
->pipe_buffer
.size
- rpipe
->pipe_buffer
.out
;
586 if (size
> rpipe
->pipe_buffer
.cnt
)
587 size
= rpipe
->pipe_buffer
.cnt
;
588 // LP64todo - fix this!
589 if (size
> (u_int
) uio_resid(uio
))
590 size
= (u_int
) uio_resid(uio
);
594 &rpipe
->pipe_buffer
.buffer
[rpipe
->pipe_buffer
.out
],
600 rpipe
->pipe_buffer
.out
+= size
;
601 if (rpipe
->pipe_buffer
.out
>= rpipe
->pipe_buffer
.size
)
602 rpipe
->pipe_buffer
.out
= 0;
604 rpipe
->pipe_buffer
.cnt
-= size
;
607 * If there is no more to read in the pipe, reset
608 * its pointers to the beginning. This improves
611 if (rpipe
->pipe_buffer
.cnt
== 0) {
612 rpipe
->pipe_buffer
.in
= 0;
613 rpipe
->pipe_buffer
.out
= 0;
616 #ifndef PIPE_NODIRECT
618 * Direct copy, bypassing a kernel buffer.
620 } else if ((size
= rpipe
->pipe_map
.cnt
) &&
621 (rpipe
->pipe_state
& PIPE_DIRECTW
)) {
623 // LP64todo - fix this!
624 if (size
> (u_int
) uio_resid(uio
))
625 size
= (u_int
) uio_resid(uio
);
627 va
= (caddr_t
) rpipe
->pipe_map
.kva
+
630 error
= uiomove(va
, size
, uio
);
635 rpipe
->pipe_map
.pos
+= size
;
636 rpipe
->pipe_map
.cnt
-= size
;
637 if (rpipe
->pipe_map
.cnt
== 0) {
638 rpipe
->pipe_state
&= ~PIPE_DIRECTW
;
644 * detect EOF condition
645 * read returns 0 on EOF, no need to set error
647 if (rpipe
->pipe_state
& PIPE_EOF
)
651 * If the "write-side" has been blocked, wake it up now.
653 if (rpipe
->pipe_state
& PIPE_WANTW
) {
654 rpipe
->pipe_state
&= ~PIPE_WANTW
;
659 * Break if some data was read.
665 * Unlock the pipe buffer for our remaining processing.
666 * We will either break out with an error or we will
667 * sleep and relock to loop.
672 * Handle non-blocking mode operation or
673 * wait for more data.
675 if (fp
->f_flag
& FNONBLOCK
) {
678 rpipe
->pipe_state
|= PIPE_WANTR
;
680 error
= msleep(rpipe
, PIPE_MTX(rpipe
), PRIBIO
| PCATCH
, "piperd", 0);
683 error
= pipelock(rpipe
, 1);
698 * PIPE_WANT processing only makes sense if pipe_busy is 0.
700 if ((rpipe
->pipe_busy
== 0) && (rpipe
->pipe_state
& PIPE_WANT
)) {
701 rpipe
->pipe_state
&= ~(PIPE_WANT
|PIPE_WANTW
);
703 } else if (rpipe
->pipe_buffer
.cnt
< MINPIPESIZE
) {
705 * Handle write blocking hysteresis.
707 if (rpipe
->pipe_state
& PIPE_WANTW
) {
708 rpipe
->pipe_state
&= ~PIPE_WANTW
;
713 if ((rpipe
->pipe_buffer
.size
- rpipe
->pipe_buffer
.cnt
) >= PIPE_BUF
)
714 pipeselwakeup(rpipe
, rpipe
->pipe_peer
);
723 #ifndef PIPE_NODIRECT
725 * Map the sending processes' buffer into kernel space and wire it.
726 * This is similar to a physical write operation.
729 pipe_build_write_buffer(wpipe
, uio
)
736 vm_offset_t addr
, endaddr
;
739 size
= (u_int
) uio
->uio_iov
->iov_len
;
740 if (size
> wpipe
->pipe_buffer
.size
)
741 size
= wpipe
->pipe_buffer
.size
;
743 pmap
= vmspace_pmap(curproc
->p_vmspace
);
744 endaddr
= round_page((vm_offset_t
)uio
->uio_iov
->iov_base
+ size
);
745 addr
= trunc_page((vm_offset_t
)uio
->uio_iov
->iov_base
);
746 for (i
= 0; addr
< endaddr
; addr
+= PAGE_SIZE
, i
++) {
748 * vm_fault_quick() can sleep. Consequently,
749 * vm_page_lock_queue() and vm_page_unlock_queue()
750 * should not be performed outside of this loop.
753 if (vm_fault_quick((caddr_t
)addr
, VM_PROT_READ
) < 0) {
754 vm_page_lock_queues();
755 for (j
= 0; j
< i
; j
++)
756 vm_page_unhold(wpipe
->pipe_map
.ms
[j
]);
757 vm_page_unlock_queues();
760 wpipe
->pipe_map
.ms
[i
] = pmap_extract_and_hold(pmap
, addr
,
762 if (wpipe
->pipe_map
.ms
[i
] == NULL
)
767 * set up the control block
769 wpipe
->pipe_map
.npages
= i
;
770 wpipe
->pipe_map
.pos
=
771 ((vm_offset_t
) uio
->uio_iov
->iov_base
) & PAGE_MASK
;
772 wpipe
->pipe_map
.cnt
= size
;
777 if (wpipe
->pipe_map
.kva
== 0) {
779 * We need to allocate space for an extra page because the
780 * address range might (will) span pages at times.
782 wpipe
->pipe_map
.kva
= kmem_alloc_nofault(kernel_map
,
783 wpipe
->pipe_buffer
.size
+ PAGE_SIZE
);
784 atomic_add_int(&amountpipekvawired
,
785 wpipe
->pipe_buffer
.size
+ PAGE_SIZE
);
787 pmap_qenter(wpipe
->pipe_map
.kva
, wpipe
->pipe_map
.ms
,
788 wpipe
->pipe_map
.npages
);
791 * and update the uio data
794 uio
->uio_iov
->iov_len
-= size
;
795 uio
->uio_iov
->iov_base
= (char *)uio
->uio_iov
->iov_base
+ size
;
796 if (uio
->uio_iov
->iov_len
== 0)
798 uio_setresid(uio
, (uio_resid(uio
) - size
));
799 uio
->uio_offset
+= size
;
804 * unmap and unwire the process buffer
807 pipe_destroy_write_buffer(wpipe
)
812 if (wpipe
->pipe_map
.kva
) {
813 pmap_qremove(wpipe
->pipe_map
.kva
, wpipe
->pipe_map
.npages
);
815 if (amountpipekvawired
> maxpipekvawired
/ 2) {
816 /* Conserve address space */
817 vm_offset_t kva
= wpipe
->pipe_map
.kva
;
818 wpipe
->pipe_map
.kva
= 0;
819 kmem_free(kernel_map
, kva
,
820 wpipe
->pipe_buffer
.size
+ PAGE_SIZE
);
821 atomic_subtract_int(&amountpipekvawired
,
822 wpipe
->pipe_buffer
.size
+ PAGE_SIZE
);
825 vm_page_lock_queues();
826 for (i
= 0; i
< wpipe
->pipe_map
.npages
; i
++) {
827 vm_page_unhold(wpipe
->pipe_map
.ms
[i
]);
829 vm_page_unlock_queues();
830 wpipe
->pipe_map
.npages
= 0;
834 * In the case of a signal, the writing process might go away. This
835 * code copies the data into the circular buffer so that the source
836 * pages can be freed without loss of data.
839 pipe_clone_write_buffer(wpipe
)
845 size
= wpipe
->pipe_map
.cnt
;
846 pos
= wpipe
->pipe_map
.pos
;
848 wpipe
->pipe_buffer
.in
= size
;
849 wpipe
->pipe_buffer
.out
= 0;
850 wpipe
->pipe_buffer
.cnt
= size
;
851 wpipe
->pipe_state
&= ~PIPE_DIRECTW
;
854 bcopy((caddr_t
) wpipe
->pipe_map
.kva
+ pos
,
855 wpipe
->pipe_buffer
.buffer
, size
);
856 pipe_destroy_write_buffer(wpipe
);
861 * This implements the pipe buffer write mechanism. Note that only
862 * a direct write OR a normal pipe write can be pending at any given time.
863 * If there are any characters in the pipe buffer, the direct write will
864 * be deferred until the receiving process grabs all of the bytes from
865 * the pipe buffer. Then the direct mapping write is set-up.
868 pipe_direct_write(wpipe
, uio
)
875 while (wpipe
->pipe_state
& PIPE_DIRECTW
) {
876 if (wpipe
->pipe_state
& PIPE_WANTR
) {
877 wpipe
->pipe_state
&= ~PIPE_WANTR
;
880 wpipe
->pipe_state
|= PIPE_WANTW
;
881 error
= msleep(wpipe
, PIPE_MTX(wpipe
),
882 PRIBIO
| PCATCH
, "pipdww", 0);
885 if (wpipe
->pipe_state
& PIPE_EOF
) {
890 wpipe
->pipe_map
.cnt
= 0; /* transfer not ready yet */
891 if (wpipe
->pipe_buffer
.cnt
> 0) {
892 if (wpipe
->pipe_state
& PIPE_WANTR
) {
893 wpipe
->pipe_state
&= ~PIPE_WANTR
;
897 wpipe
->pipe_state
|= PIPE_WANTW
;
898 error
= msleep(wpipe
, PIPE_MTX(wpipe
),
899 PRIBIO
| PCATCH
, "pipdwc", 0);
902 if (wpipe
->pipe_state
& PIPE_EOF
) {
909 wpipe
->pipe_state
|= PIPE_DIRECTW
;
913 error
= pipe_build_write_buffer(wpipe
, uio
);
917 wpipe
->pipe_state
&= ~PIPE_DIRECTW
;
922 while (!error
&& (wpipe
->pipe_state
& PIPE_DIRECTW
)) {
923 if (wpipe
->pipe_state
& PIPE_EOF
) {
926 pipe_destroy_write_buffer(wpipe
);
928 pipeselwakeup(wpipe
, wpipe
);
933 if (wpipe
->pipe_state
& PIPE_WANTR
) {
934 wpipe
->pipe_state
&= ~PIPE_WANTR
;
937 pipeselwakeup(wpipe
, wpipe
);
938 error
= msleep(wpipe
, PIPE_MTX(wpipe
), PRIBIO
| PCATCH
,
943 if (wpipe
->pipe_state
& PIPE_DIRECTW
) {
945 * this bit of trickery substitutes a kernel buffer for
946 * the process that might be going away.
948 pipe_clone_write_buffer(wpipe
);
951 pipe_destroy_write_buffer(wpipe
);
966 pipe_write(struct fileproc
*fp
, struct uio
*uio
, __unused kauth_cred_t active_cred
, __unused
int flags
, __unused
struct proc
*p
)
971 struct pipe
*wpipe
, *rpipe
;
973 rpipe
= (struct pipe
*)fp
->f_data
;
976 wpipe
= rpipe
->pipe_peer
;
979 * detect loss of pipe read side, issue SIGPIPE if lost.
981 if (wpipe
== NULL
|| (wpipe
->pipe_state
& PIPE_EOF
)) {
986 error
= mac_check_pipe_write(active_cred
, wpipe
);
996 if (wpipe
->pipe_buffer
.buffer
== 0) {
998 * need to allocate some storage... we delay the allocation
999 * until the first write on fd[0] to avoid allocating storage for both
1000 * 'pipe ends'... most pipes are half-duplex with the writes targeting
1001 * fd[1], so allocating space for both ends is a waste...
1003 * Reduce to 1/4th pipe size if we're over our global max.
1005 if (amountpipekva
> maxpipekva
/ 2)
1006 pipe_size
= SMALL_PIPE_SIZE
;
1008 pipe_size
= PIPE_SIZE
;
1012 * If it is advantageous to resize the pipe buffer, do
1015 if ((uio_resid(uio
) > PIPE_SIZE
) &&
1016 (wpipe
->pipe_buffer
.size
<= PIPE_SIZE
) &&
1017 (amountpipekva
< maxpipekva
/ 2) &&
1018 (nbigpipe
< LIMITBIGPIPES
) &&
1019 #ifndef PIPE_NODIRECT
1020 (wpipe
->pipe_state
& PIPE_DIRECTW
) == 0 &&
1022 (wpipe
->pipe_buffer
.cnt
== 0)) {
1024 pipe_size
= BIG_PIPE_SIZE
;
1029 * need to do initial allocation or resizing of pipe
1031 if ((error
= pipelock(wpipe
, 1)) == 0) {
1033 if (pipespace(wpipe
, pipe_size
) == 0)
1034 OSAddAtomic(1, (SInt32
*)&nbigpipe
);
1038 if (wpipe
->pipe_buffer
.buffer
== 0) {
1040 * initial allocation failed
1047 * If an error occurred unbusy and return, waking up any pending
1051 if ((wpipe
->pipe_busy
== 0) &&
1052 (wpipe
->pipe_state
& PIPE_WANT
)) {
1053 wpipe
->pipe_state
&= ~(PIPE_WANT
| PIPE_WANTR
);
1060 // LP64todo - fix this!
1061 orig_resid
= uio_resid(uio
);
1063 while (uio_resid(uio
)) {
1066 #ifndef PIPE_NODIRECT
1068 * If the transfer is large, we can gain performance if
1069 * we do process-to-process copies directly.
1070 * If the write is non-blocking, we don't use the
1071 * direct write mechanism.
1073 * The direct write mechanism will detect the reader going
1076 if ((uio
->uio_iov
->iov_len
>= PIPE_MINDIRECT
) &&
1077 (fp
->f_flag
& FNONBLOCK
) == 0 &&
1078 amountpipekvawired
+ uio
->uio_resid
< maxpipekvawired
) {
1079 error
= pipe_direct_write(wpipe
, uio
);
1086 * Pipe buffered writes cannot be coincidental with
1087 * direct writes. We wait until the currently executing
1088 * direct write is completed before we start filling the
1089 * pipe buffer. We break out if a signal occurs or the
1093 while (wpipe
->pipe_state
& PIPE_DIRECTW
) {
1094 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1095 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1098 error
= msleep(wpipe
, PIPE_MTX(wpipe
), PRIBIO
| PCATCH
, "pipbww", 0);
1100 if (wpipe
->pipe_state
& PIPE_EOF
)
1108 space
= wpipe
->pipe_buffer
.size
- wpipe
->pipe_buffer
.cnt
;
1111 * Writes of size <= PIPE_BUF must be atomic.
1113 if ((space
< uio_resid(uio
)) && (orig_resid
<= PIPE_BUF
))
1118 if ((error
= pipelock(wpipe
,1)) == 0) {
1119 int size
; /* Transfer size */
1120 int segsize
; /* first segment to transfer */
1122 if (wpipe
->pipe_state
& PIPE_EOF
) {
1127 #ifndef PIPE_NODIRECT
1129 * It is possible for a direct write to
1130 * slip in on us... handle it here...
1132 if (wpipe
->pipe_state
& PIPE_DIRECTW
) {
1138 * If a process blocked in pipelock, our
1139 * value for space might be bad... the mutex
1140 * is dropped while we're blocked
1142 if (space
> (int)(wpipe
->pipe_buffer
.size
-
1143 wpipe
->pipe_buffer
.cnt
)) {
1149 * Transfer size is minimum of uio transfer
1150 * and free space in pipe buffer.
1152 // LP64todo - fix this!
1153 if (space
> uio_resid(uio
))
1154 size
= uio_resid(uio
);
1158 * First segment to transfer is minimum of
1159 * transfer size and contiguous space in
1160 * pipe buffer. If first segment to transfer
1161 * is less than the transfer size, we've got
1162 * a wraparound in the buffer.
1164 segsize
= wpipe
->pipe_buffer
.size
-
1165 wpipe
->pipe_buffer
.in
;
1169 /* Transfer first segment */
1172 error
= uiomove(&wpipe
->pipe_buffer
.buffer
[wpipe
->pipe_buffer
.in
],
1176 if (error
== 0 && segsize
< size
) {
1178 * Transfer remaining part now, to
1179 * support atomic writes. Wraparound
1182 if (wpipe
->pipe_buffer
.in
+ segsize
!=
1183 wpipe
->pipe_buffer
.size
)
1184 panic("Expected pipe buffer "
1185 "wraparound disappeared");
1189 &wpipe
->pipe_buffer
.buffer
[0],
1190 size
- segsize
, uio
);
1194 wpipe
->pipe_buffer
.in
+= size
;
1195 if (wpipe
->pipe_buffer
.in
>=
1196 wpipe
->pipe_buffer
.size
) {
1197 if (wpipe
->pipe_buffer
.in
!=
1199 wpipe
->pipe_buffer
.size
)
1202 wpipe
->pipe_buffer
.in
= size
-
1206 wpipe
->pipe_buffer
.cnt
+= size
;
1207 if (wpipe
->pipe_buffer
.cnt
>
1208 wpipe
->pipe_buffer
.size
)
1209 panic("Pipe buffer overflow");
1219 * If the "read-side" has been blocked, wake it up now.
1221 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1222 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1226 * don't block on non-blocking I/O
1227 * we'll do the pipeselwakeup on the way out
1229 if (fp
->f_flag
& FNONBLOCK
) {
1234 * We have no more space and have something to offer,
1235 * wake up select/poll.
1237 pipeselwakeup(wpipe
, wpipe
);
1239 wpipe
->pipe_state
|= PIPE_WANTW
;
1241 error
= msleep(wpipe
, PIPE_MTX(wpipe
), PRIBIO
| PCATCH
, "pipewr", 0);
1246 * If read side wants to go away, we just issue a signal
1249 if (wpipe
->pipe_state
& PIPE_EOF
) {
1257 if ((wpipe
->pipe_busy
== 0) && (wpipe
->pipe_state
& PIPE_WANT
)) {
1258 wpipe
->pipe_state
&= ~(PIPE_WANT
| PIPE_WANTR
);
1261 if (wpipe
->pipe_buffer
.cnt
> 0) {
1263 * If there are any characters in the buffer, we wake up
1264 * the reader if it was blocked waiting for data.
1266 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1267 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1271 * wake up thread blocked in select/poll or post the notification
1273 pipeselwakeup(wpipe
, wpipe
);
1281 * we implement a very minimal set of ioctls for compatibility with sockets.
1285 pipe_ioctl(struct fileproc
*fp
, u_long cmd
, caddr_t data
, __unused
struct proc
*p
)
1287 struct pipe
*mpipe
= (struct pipe
*)fp
->f_data
;
1295 error
= mac_check_pipe_ioctl(active_cred
, mpipe
, cmd
, data
);
1311 mpipe
->pipe_state
|= PIPE_ASYNC
;
1313 mpipe
->pipe_state
&= ~PIPE_ASYNC
;
1319 #ifndef PIPE_NODIRECT
1320 if (mpipe
->pipe_state
& PIPE_DIRECTW
)
1321 *(int *)data
= mpipe
->pipe_map
.cnt
;
1324 *(int *)data
= mpipe
->pipe_buffer
.cnt
;
1329 mpipe
->pipe_pgid
= *(int *)data
;
1335 *(int *)data
= mpipe
->pipe_pgid
;
1347 pipe_select(struct fileproc
*fp
, int which
, void *wql
, struct proc
*p
)
1349 struct pipe
*rpipe
= (struct pipe
*)fp
->f_data
;
1353 if (rpipe
== NULL
|| rpipe
== (struct pipe
*)-1)
1358 wpipe
= rpipe
->pipe_peer
;
1363 if ((rpipe
->pipe_state
& PIPE_DIRECTW
) ||
1364 (rpipe
->pipe_buffer
.cnt
> 0) ||
1365 (rpipe
->pipe_state
& PIPE_EOF
)) {
1369 rpipe
->pipe_state
|= PIPE_SEL
;
1370 selrecord(p
, &rpipe
->pipe_sel
, wql
);
1375 if (wpipe
== NULL
|| (wpipe
->pipe_state
& PIPE_EOF
) ||
1376 (((wpipe
->pipe_state
& PIPE_DIRECTW
) == 0) &&
1377 (wpipe
->pipe_buffer
.size
- wpipe
->pipe_buffer
.cnt
) >= PIPE_BUF
)) {
1381 wpipe
->pipe_state
|= PIPE_SEL
;
1382 selrecord(p
, &wpipe
->pipe_sel
, wql
);
1386 rpipe
->pipe_state
|= PIPE_SEL
;
1387 selrecord(p
, &rpipe
->pipe_sel
, wql
);
1398 pipe_close(struct fileglob
*fg
, __unused
struct proc
*p
)
1403 cpipe
= (struct pipe
*)fg
->fg_data
;
1414 pipe_free_kmem(struct pipe
*cpipe
)
1417 if (cpipe
->pipe_buffer
.buffer
!= NULL
) {
1418 if (cpipe
->pipe_buffer
.size
> PIPE_SIZE
)
1419 OSAddAtomic(-1, (SInt32
*)&nbigpipe
);
1420 OSAddAtomic(-(cpipe
->pipe_buffer
.size
), (SInt32
*)&amountpipekva
);
1421 OSAddAtomic(-1, (SInt32
*)&amountpipes
);
1423 kmem_free(kernel_map
, (vm_offset_t
)cpipe
->pipe_buffer
.buffer
,
1424 cpipe
->pipe_buffer
.size
);
1425 cpipe
->pipe_buffer
.buffer
= NULL
;
1427 #ifndef PIPE_NODIRECT
1428 if (cpipe
->pipe_map
.kva
!= 0) {
1429 atomic_subtract_int(&amountpipekvawired
,
1430 cpipe
->pipe_buffer
.size
+ PAGE_SIZE
);
1431 kmem_free(kernel_map
,
1432 cpipe
->pipe_map
.kva
,
1433 cpipe
->pipe_buffer
.size
+ PAGE_SIZE
);
1434 cpipe
->pipe_map
.cnt
= 0;
1435 cpipe
->pipe_map
.kva
= 0;
1436 cpipe
->pipe_map
.pos
= 0;
1437 cpipe
->pipe_map
.npages
= 0;
1446 pipeclose(struct pipe
*cpipe
)
1453 /* partially created pipes won't have a valid mutex. */
1454 if (PIPE_MTX(cpipe
) != NULL
)
1457 pipeselwakeup(cpipe
, cpipe
);
1460 * If the other side is blocked, wake it up saying that
1461 * we want to close it down.
1463 while (cpipe
->pipe_busy
) {
1464 cpipe
->pipe_state
|= PIPE_WANT
| PIPE_EOF
;
1468 msleep(cpipe
, PIPE_MTX(cpipe
), PRIBIO
, "pipecl", 0);
1472 if (cpipe
->pipe_label
!= NULL
&& cpipe
->pipe_peer
== NULL
)
1473 mac_destroy_pipe(cpipe
);
1477 * Disconnect from peer
1479 if ((ppipe
= cpipe
->pipe_peer
) != NULL
) {
1481 ppipe
->pipe_state
|= PIPE_EOF
;
1483 pipeselwakeup(ppipe
, ppipe
);
1486 if (cpipe
->pipe_state
& PIPE_KNOTE
)
1487 KNOTE(&ppipe
->pipe_sel
.si_note
, 1);
1489 postpipeevent(ppipe
, EV_RCLOSED
);
1491 ppipe
->pipe_peer
= NULL
;
1498 if (PIPE_MTX(cpipe
) != NULL
) {
1499 if (ppipe
!= NULL
) {
1501 * since the mutex is shared and the peer is still
1502 * alive, we need to release the mutex, not free it
1507 * peer is gone, so we're the sole party left with
1508 * interest in this mutex... we can just free it
1510 lck_mtx_free(PIPE_MTX(cpipe
), pipe_mtx_grp
);
1513 pipe_free_kmem(cpipe
);
1515 zfree(pipe_zone
, cpipe
);
1521 pipe_kqfilter(__unused
struct fileproc
*fp
, struct knote
*kn
, __unused
struct proc
*p
)
1525 cpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1529 switch (kn
->kn_filter
) {
1531 kn
->kn_fop
= &pipe_rfiltops
;
1534 kn
->kn_fop
= &pipe_wfiltops
;
1536 if (cpipe
->pipe_peer
== NULL
) {
1538 * other end of pipe has been closed
1543 cpipe
= cpipe
->pipe_peer
;
1550 if (KNOTE_ATTACH(&cpipe
->pipe_sel
.si_note
, kn
))
1551 cpipe
->pipe_state
|= PIPE_KNOTE
;
1558 filt_pipedetach(struct knote
*kn
)
1560 struct pipe
*cpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1564 if (kn
->kn_filter
== EVFILT_WRITE
) {
1565 if (cpipe
->pipe_peer
== NULL
) {
1569 cpipe
= cpipe
->pipe_peer
;
1571 if (cpipe
->pipe_state
& PIPE_KNOTE
) {
1572 if (KNOTE_DETACH(&cpipe
->pipe_sel
.si_note
, kn
))
1573 cpipe
->pipe_state
&= ~PIPE_KNOTE
;
1580 filt_piperead(struct knote
*kn
, long hint
)
1582 struct pipe
*rpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1587 * if hint == 0, then we've been called from the kevent
1588 * world directly and do not currently hold the pipe mutex...
1589 * if hint == 1, we're being called back via the KNOTE post
1590 * we made in pipeselwakeup, and we already hold the mutex...
1595 wpipe
= rpipe
->pipe_peer
;
1596 kn
->kn_data
= rpipe
->pipe_buffer
.cnt
;
1598 #ifndef PIPE_NODIRECT
1599 if ((kn
->kn_data
== 0) && (rpipe
->pipe_state
& PIPE_DIRECTW
))
1600 kn
->kn_data
= rpipe
->pipe_map
.cnt
;
1602 if ((rpipe
->pipe_state
& PIPE_EOF
) ||
1603 (wpipe
== NULL
) || (wpipe
->pipe_state
& PIPE_EOF
)) {
1604 kn
->kn_flags
|= EV_EOF
;
1607 retval
= (kn
->kn_sfflags
& NOTE_LOWAT
) ?
1608 (kn
->kn_data
>= kn
->kn_sdata
) : (kn
->kn_data
> 0);
1618 filt_pipewrite(struct knote
*kn
, long hint
)
1620 struct pipe
*rpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1624 * if hint == 0, then we've been called from the kevent
1625 * world directly and do not currently hold the pipe mutex...
1626 * if hint == 1, we're being called back via the KNOTE post
1627 * we made in pipeselwakeup, and we already hold the mutex...
1632 wpipe
= rpipe
->pipe_peer
;
1634 if ((wpipe
== NULL
) || (wpipe
->pipe_state
& PIPE_EOF
)) {
1636 kn
->kn_flags
|= EV_EOF
;
1642 kn
->kn_data
= wpipe
->pipe_buffer
.size
- wpipe
->pipe_buffer
.cnt
;
1644 #ifndef PIPE_NODIRECT
1645 if (wpipe
->pipe_state
& PIPE_DIRECTW
)
1651 return (kn
->kn_data
>= ((kn
->kn_sfflags
& NOTE_LOWAT
) ?
1652 kn
->kn_sdata
: PIPE_BUF
));
1656 fill_pipeinfo(struct pipe
* cpipe
, struct pipe_info
* pinfo
)
1668 error
= mac_check_pipe_stat(active_cred
, cpipe
);
1673 if (cpipe
->pipe_buffer
.buffer
== 0) {
1675 * must be stat'ing the write fd
1677 cpipe
= cpipe
->pipe_peer
;
1683 ub
= &pinfo
->pipe_stat
;
1685 bzero(ub
, sizeof(*ub
));
1686 ub
->st_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
1687 ub
->st_blksize
= cpipe
->pipe_buffer
.size
;
1688 ub
->st_size
= cpipe
->pipe_buffer
.cnt
;
1689 if (ub
->st_blksize
!= 0);
1690 ub
->st_blocks
= (ub
->st_size
+ ub
->st_blksize
- 1) / ub
->st_blksize
;
1693 ub
->st_uid
= kauth_getuid();
1694 ub
->st_gid
= kauth_getgid();
1697 ub
->st_atimespec
.tv_sec
= now
.tv_sec
;
1698 ub
->st_atimespec
.tv_nsec
= now
.tv_usec
* 1000;
1700 ub
->st_mtimespec
.tv_sec
= now
.tv_sec
;
1701 ub
->st_mtimespec
.tv_nsec
= now
.tv_usec
* 1000;
1703 ub
->st_ctimespec
.tv_sec
= now
.tv_sec
;
1704 ub
->st_ctimespec
.tv_nsec
= now
.tv_usec
* 1000;
1707 * Left as 0: st_dev, st_ino, st_nlink, st_rdev, st_flags, st_gen, st_uid, st_gid.
1708 * XXX (st_dev, st_ino) should be unique.
1711 pinfo
->pipe_handle
= (uint64_t)((uintptr_t)cpipe
);
1712 pinfo
->pipe_peerhandle
= (uint64_t)((uintptr_t)(cpipe
->pipe_peer
));
1713 pinfo
->pipe_status
= cpipe
->pipe_state
;