sys_pipe.c revision 1.87.2.3 1 /* $NetBSD: sys_pipe.c,v 1.87.2.3 2007/12/15 04:37:18 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2003, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg, and by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1996 John S. Dyson
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice immediately at the beginning of the file, without modification,
48 * this list of conditions, and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. Absolutely no warranty of function or purpose is made by the author
53 * John S. Dyson.
54 * 4. Modifications may be freely made to this file if the above conditions
55 * are met.
56 *
57 * $FreeBSD: src/sys/kern/sys_pipe.c,v 1.95 2002/03/09 22:06:31 alfred Exp $
58 */
59
60 /*
61 * This file contains a high-performance replacement for the socket-based
62 * pipes scheme originally used in FreeBSD/4.4Lite. It does not support
63 * all features of sockets, but does do everything that pipes normally
64 * do.
65 *
66 * Adaption for NetBSD UVM, including uvm_loan() based direct write, was
67 * written by Jaromir Dolecek.
68 */
69
70 /*
71 * This code has two modes of operation, a small write mode and a large
72 * write mode. The small write mode acts like conventional pipes with
73 * a kernel buffer. If the buffer is less than PIPE_MINDIRECT, then the
74 * "normal" pipe buffering is done. If the buffer is between PIPE_MINDIRECT
75 * and PIPE_SIZE in size it is mapped read-only into the kernel address space
76 * using the UVM page loan facility from where the receiving process can copy
77 * the data directly from the pages in the sending process.
78 *
79 * The constant PIPE_MINDIRECT is chosen to make sure that buffering will
80 * happen for small transfers so that the system will not spend all of
81 * its time context switching. PIPE_SIZE is constrained by the
82 * amount of kernel virtual memory.
83 */
84
85 #include <sys/cdefs.h>
86 __KERNEL_RCSID(0, "$NetBSD: sys_pipe.c,v 1.87.2.3 2007/12/15 04:37:18 ad Exp $");
87
88 #include <sys/param.h>
89 #include <sys/systm.h>
90 #include <sys/proc.h>
91 #include <sys/fcntl.h>
92 #include <sys/file.h>
93 #include <sys/filedesc.h>
94 #include <sys/filio.h>
95 #include <sys/kernel.h>
96 #include <sys/ttycom.h>
97 #include <sys/stat.h>
98 #include <sys/malloc.h>
99 #include <sys/poll.h>
100 #include <sys/signalvar.h>
101 #include <sys/vnode.h>
102 #include <sys/uio.h>
103 #include <sys/lock.h>
104 #include <sys/select.h>
105 #include <sys/mount.h>
106 #include <sys/syscallargs.h>
107 #include <sys/sysctl.h>
108 #include <sys/kauth.h>
109 #include <sys/atomic.h>
110 #include <sys/pipe.h>
111
112 #include <uvm/uvm.h>
113
114 /*
115 * Use this define if you want to disable *fancy* VM things. Expect an
116 * approx 30% decrease in transfer rate.
117 */
118 /* #define PIPE_NODIRECT */
119
120 /*
121 * interfaces to the outside world
122 */
123 static int pipe_read(struct file *fp, off_t *offset, struct uio *uio,
124 kauth_cred_t cred, int flags);
125 static int pipe_write(struct file *fp, off_t *offset, struct uio *uio,
126 kauth_cred_t cred, int flags);
127 static int pipe_close(struct file *fp, struct lwp *l);
128 static int pipe_poll(struct file *fp, int events, struct lwp *l);
129 static int pipe_kqfilter(struct file *fp, struct knote *kn);
130 static int pipe_stat(struct file *fp, struct stat *sb, struct lwp *l);
131 static int pipe_ioctl(struct file *fp, u_long cmd, void *data,
132 struct lwp *l);
133
134 static const struct fileops pipeops = {
135 pipe_read, pipe_write, pipe_ioctl, fnullop_fcntl, pipe_poll,
136 pipe_stat, pipe_close, pipe_kqfilter
137 };
138
139 /*
140 * Single mutex shared between both ends of the pipe.
141 */
142
143 struct pipe_mutex {
144 kmutex_t pm_mutex;
145 u_int pm_refcnt;
146 };
147
148 /*
149 * Default pipe buffer size(s), this can be kind-of large now because pipe
150 * space is pageable. The pipe code will try to maintain locality of
151 * reference for performance reasons, so small amounts of outstanding I/O
152 * will not wipe the cache.
153 */
154 #define MINPIPESIZE (PIPE_SIZE/3)
155 #define MAXPIPESIZE (2*PIPE_SIZE/3)
156
157 /*
158 * Maximum amount of kva for pipes -- this is kind-of a soft limit, but
159 * is there so that on large systems, we don't exhaust it.
160 */
161 #define MAXPIPEKVA (8*1024*1024)
162 static u_int maxpipekva = MAXPIPEKVA;
163
164 /*
165 * Limit for direct transfers, we cannot, of course limit
166 * the amount of kva for pipes in general though.
167 */
168 #define LIMITPIPEKVA (16*1024*1024)
169 static u_int limitpipekva = LIMITPIPEKVA;
170
171 /*
172 * Limit the number of "big" pipes
173 */
174 #define LIMITBIGPIPES 32
175 static u_int maxbigpipes = LIMITBIGPIPES;
176 static u_int nbigpipe = 0;
177
178 /*
179 * Amount of KVA consumed by pipe buffers.
180 */
181 static u_int amountpipekva = 0;
182
183 MALLOC_DEFINE(M_PIPE, "pipe", "Pipe structures");
184
185 static void pipeclose(struct file *fp, struct pipe *pipe);
186 static void pipe_free_kmem(struct pipe *pipe);
187 static int pipe_create(struct pipe **pipep, int allockva, struct pipe_mutex *);
188 static int pipelock(struct pipe *pipe, int catch);
189 static inline void pipeunlock(struct pipe *pipe);
190 static void pipeselwakeup(struct pipe *pipe, struct pipe *sigp, int code);
191 #ifndef PIPE_NODIRECT
192 static int pipe_direct_write(struct file *fp, struct pipe *wpipe,
193 struct uio *uio);
194 #endif
195 static int pipespace(struct pipe *pipe, int size);
196
197 #ifndef PIPE_NODIRECT
198 static int pipe_loan_alloc(struct pipe *, int);
199 static void pipe_loan_free(struct pipe *);
200 #endif /* PIPE_NODIRECT */
201
202 static int pipe_mutex_ctor(void *, void *, int);
203 static void pipe_mutex_dtor(void *, void *);
204
205 static pool_cache_t pipe_cache;
206 static pool_cache_t pipe_mutex_cache;
207
208 void
209 pipe_init(void)
210 {
211 size_t size;
212
213 pipe_cache = pool_cache_init(sizeof(struct pipe), 0, 0, 0, "pipepl",
214 NULL, IPL_NONE, NULL, NULL, NULL);
215 KASSERT(pipe_cache != NULL);
216
217 size = (sizeof(struct pipe_mutex) + (CACHE_LINE_SIZE - 1)) &
218 (CACHE_LINE_SIZE - 1);
219 pipe_mutex_cache = pool_cache_init(size, CACHE_LINE_SIZE,
220 0, 0, "pipemtxpl", NULL, IPL_NONE, pipe_mutex_ctor,
221 pipe_mutex_dtor, NULL);
222 KASSERT(pipe_cache != NULL);
223 }
224
225 static int
226 pipe_mutex_ctor(void *arg, void *obj, int flag)
227 {
228 struct pipe_mutex *pm = obj;
229
230 mutex_init(&pm->pm_mutex, MUTEX_DEFAULT, IPL_NONE);
231 pm->pm_refcnt = 0;
232
233 return 0;
234 }
235
236 static void
237 pipe_mutex_dtor(void *arg, void *obj)
238 {
239 struct pipe_mutex *pm = obj;
240
241 KASSERT(pm->pm_refcnt == 0);
242
243 mutex_destroy(&pm->pm_mutex);
244 }
245
246 /*
247 * The pipe system call for the DTYPE_PIPE type of pipes
248 */
249
250 /* ARGSUSED */
251 int
252 sys_pipe(struct lwp *l, void *v, register_t *retval)
253 {
254 struct file *rf, *wf;
255 struct pipe *rpipe, *wpipe;
256 struct pipe_mutex *mutex;
257 int fd, error;
258
259 rpipe = wpipe = NULL;
260 mutex = pool_cache_get(pipe_mutex_cache, PR_WAITOK);
261 if (mutex == NULL)
262 return (ENOMEM);
263 if (pipe_create(&rpipe, 1, mutex) || pipe_create(&wpipe, 0, mutex)) {
264 pipeclose(NULL, rpipe);
265 pipeclose(NULL, wpipe);
266 return (ENFILE);
267 }
268
269 /*
270 * Note: the file structure returned from falloc() is marked
271 * as 'larval' initially. Unless we mark it as 'mature' by
272 * FILE_SET_MATURE(), any attempt to do anything with it would
273 * return EBADF, including e.g. dup(2) or close(2). This avoids
274 * file descriptor races if we block in the second falloc().
275 */
276
277 error = falloc(l, &rf, &fd);
278 if (error)
279 goto free2;
280 retval[0] = fd;
281 rf->f_flag = FREAD;
282 rf->f_type = DTYPE_PIPE;
283 rf->f_data = (void *)rpipe;
284 rf->f_ops = &pipeops;
285
286 error = falloc(l, &wf, &fd);
287 if (error)
288 goto free3;
289 retval[1] = fd;
290 wf->f_flag = FWRITE;
291 wf->f_type = DTYPE_PIPE;
292 wf->f_data = (void *)wpipe;
293 wf->f_ops = &pipeops;
294
295 rpipe->pipe_peer = wpipe;
296 wpipe->pipe_peer = rpipe;
297
298 FILE_SET_MATURE(rf);
299 FILE_SET_MATURE(wf);
300 FILE_UNUSE(rf, l);
301 FILE_UNUSE(wf, l);
302 return (0);
303 free3:
304 FILE_UNUSE(rf, l);
305 ffree(rf);
306 fdremove(l->l_proc->p_fd, retval[0]);
307 free2:
308 pipeclose(NULL, wpipe);
309 pipeclose(NULL, rpipe);
310
311 return (error);
312 }
313
314 /*
315 * Allocate kva for pipe circular buffer, the space is pageable
316 * This routine will 'realloc' the size of a pipe safely, if it fails
317 * it will retain the old buffer.
318 * If it fails it will return ENOMEM.
319 */
320 static int
321 pipespace(struct pipe *pipe, int size)
322 {
323 void *buffer;
324 /*
325 * Allocate pageable virtual address space. Physical memory is
326 * allocated on demand.
327 */
328 buffer = (void *) uvm_km_alloc(kernel_map, round_page(size), 0,
329 UVM_KMF_PAGEABLE);
330 if (buffer == NULL)
331 return (ENOMEM);
332
333 /* free old resources if we're resizing */
334 pipe_free_kmem(pipe);
335 pipe->pipe_buffer.buffer = buffer;
336 pipe->pipe_buffer.size = size;
337 pipe->pipe_buffer.in = 0;
338 pipe->pipe_buffer.out = 0;
339 pipe->pipe_buffer.cnt = 0;
340 atomic_add_int(&amountpipekva, pipe->pipe_buffer.size);
341 return (0);
342 }
343
344 /*
345 * Initialize and allocate VM and memory for pipe.
346 */
347 static int
348 pipe_create(struct pipe **pipep, int allockva, struct pipe_mutex *mutex)
349 {
350 struct pipe *pipe;
351 int error;
352
353 pipe = *pipep = pool_cache_get(pipe_cache, PR_WAITOK);
354 mutex->pm_refcnt++;
355
356 /* Initialize */
357 memset(pipe, 0, sizeof(struct pipe));
358 pipe->pipe_state = PIPE_SIGNALR;
359
360 getmicrotime(&pipe->pipe_ctime);
361 pipe->pipe_atime = pipe->pipe_ctime;
362 pipe->pipe_mtime = pipe->pipe_ctime;
363 pipe->pipe_lock = &mutex->pm_mutex;
364 cv_init(&pipe->pipe_cv, "pipe");
365 cv_init(&pipe->pipe_lkcv, "pipelk");
366 selinit(&pipe->pipe_sel);
367
368 if (allockva && (error = pipespace(pipe, PIPE_SIZE)))
369 return (error);
370
371 return (0);
372 }
373
374
375 /*
376 * Lock a pipe for I/O, blocking other access
377 * Called with pipe spin lock held.
378 * Return with pipe spin lock released on success.
379 */
380 static int
381 pipelock(struct pipe *pipe, int catch)
382 {
383 int error;
384
385 KASSERT(mutex_owned(pipe->pipe_lock));
386
387 while (pipe->pipe_state & PIPE_LOCKFL) {
388 pipe->pipe_state |= PIPE_LWANT;
389 if (catch) {
390 error = cv_wait_sig(&pipe->pipe_lkcv, pipe->pipe_lock);
391 if (error != 0)
392 return error;
393 } else
394 cv_wait(&pipe->pipe_lkcv, pipe->pipe_lock);
395 }
396
397 pipe->pipe_state |= PIPE_LOCKFL;
398 mutex_exit(pipe->pipe_lock);
399
400 return 0;
401 }
402
403 /*
404 * unlock a pipe I/O lock
405 */
406 static inline void
407 pipeunlock(struct pipe *pipe)
408 {
409
410 KASSERT(pipe->pipe_state & PIPE_LOCKFL);
411
412 pipe->pipe_state &= ~PIPE_LOCKFL;
413 if (pipe->pipe_state & PIPE_LWANT) {
414 pipe->pipe_state &= ~PIPE_LWANT;
415 cv_broadcast(&pipe->pipe_lkcv);
416 }
417 }
418
419 /*
420 * Select/poll wakup. This also sends SIGIO to peer connected to
421 * 'sigpipe' side of pipe.
422 */
423 static void
424 pipeselwakeup(struct pipe *selp, struct pipe *sigp, int code)
425 {
426 int band;
427
428 selnotify(&selp->pipe_sel, NOTE_SUBMIT);
429
430 if (sigp == NULL || (sigp->pipe_state & PIPE_ASYNC) == 0)
431 return;
432
433 switch (code) {
434 case POLL_IN:
435 band = POLLIN|POLLRDNORM;
436 break;
437 case POLL_OUT:
438 band = POLLOUT|POLLWRNORM;
439 break;
440 case POLL_HUP:
441 band = POLLHUP;
442 break;
443 #if POLL_HUP != POLL_ERR
444 case POLL_ERR:
445 band = POLLERR;
446 break;
447 #endif
448 default:
449 band = 0;
450 #ifdef DIAGNOSTIC
451 printf("bad siginfo code %d in pipe notification.\n", code);
452 #endif
453 break;
454 }
455
456 fownsignal(sigp->pipe_pgid, SIGIO, code, band, selp);
457 }
458
459 /* ARGSUSED */
460 static int
461 pipe_read(struct file *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
462 int flags)
463 {
464 struct pipe *rpipe = (struct pipe *) fp->f_data;
465 struct pipebuf *bp = &rpipe->pipe_buffer;
466 int error;
467 size_t nread = 0;
468 size_t size;
469 size_t ocnt;
470
471 mutex_enter(rpipe->pipe_lock);
472 ++rpipe->pipe_busy;
473 ocnt = bp->cnt;
474
475 again:
476 error = pipelock(rpipe, 1);
477 if (error)
478 goto unlocked_error;
479
480 while (uio->uio_resid) {
481 /*
482 * normal pipe buffer receive
483 */
484 if (bp->cnt > 0) {
485 size = bp->size - bp->out;
486 if (size > bp->cnt)
487 size = bp->cnt;
488 if (size > uio->uio_resid)
489 size = uio->uio_resid;
490
491 error = uiomove((char *)bp->buffer + bp->out, size, uio);
492 if (error)
493 break;
494
495 bp->out += size;
496 if (bp->out >= bp->size)
497 bp->out = 0;
498
499 bp->cnt -= size;
500
501 /*
502 * If there is no more to read in the pipe, reset
503 * its pointers to the beginning. This improves
504 * cache hit stats.
505 */
506 if (bp->cnt == 0) {
507 bp->in = 0;
508 bp->out = 0;
509 }
510 nread += size;
511 continue;
512 }
513
514 /* Lock to see up-to-date value of pipe_status. */
515 mutex_enter(rpipe->pipe_lock);
516
517 #ifndef PIPE_NODIRECT
518 if ((rpipe->pipe_state & PIPE_DIRECTR) != 0) {
519 /*
520 * Direct copy, bypassing a kernel buffer.
521 */
522 void * va;
523
524 KASSERT(rpipe->pipe_state & PIPE_DIRECTW);
525 mutex_exit(rpipe->pipe_lock);
526
527 size = rpipe->pipe_map.cnt;
528 if (size > uio->uio_resid)
529 size = uio->uio_resid;
530
531 va = (char *)rpipe->pipe_map.kva + rpipe->pipe_map.pos;
532 error = uiomove(va, size, uio);
533 if (error)
534 break;
535 nread += size;
536 rpipe->pipe_map.pos += size;
537 rpipe->pipe_map.cnt -= size;
538 if (rpipe->pipe_map.cnt == 0) {
539 mutex_enter(rpipe->pipe_lock);
540 rpipe->pipe_state &= ~PIPE_DIRECTR;
541 cv_broadcast(&rpipe->pipe_cv);
542 mutex_exit(rpipe->pipe_lock);
543 }
544 continue;
545 }
546 #endif
547 /*
548 * Break if some data was read.
549 */
550 if (nread > 0) {
551 mutex_exit(rpipe->pipe_lock);
552 break;
553 }
554
555 /*
556 * detect EOF condition
557 * read returns 0 on EOF, no need to set error
558 */
559 if (rpipe->pipe_state & PIPE_EOF) {
560 mutex_exit(rpipe->pipe_lock);
561 break;
562 }
563
564 /*
565 * don't block on non-blocking I/O
566 */
567 if (fp->f_flag & FNONBLOCK) {
568 mutex_exit(rpipe->pipe_lock);
569 error = EAGAIN;
570 break;
571 }
572
573 /*
574 * Unlock the pipe buffer for our remaining processing.
575 * We will either break out with an error or we will
576 * sleep and relock to loop.
577 */
578 pipeunlock(rpipe);
579
580 /*
581 * Re-check to see if more direct writes are pending.
582 */
583 if ((rpipe->pipe_state & PIPE_DIRECTR) != 0)
584 goto again;
585
586 /*
587 * We want to read more, wake up select/poll.
588 */
589 pipeselwakeup(rpipe, rpipe->pipe_peer, POLL_IN);
590
591 /*
592 * If the "write-side" is blocked, wake it up now.
593 */
594 if (rpipe->pipe_state & PIPE_WANTW) {
595 rpipe->pipe_state &= ~PIPE_WANTW;
596 cv_broadcast(&rpipe->pipe_cv);
597 }
598
599 /* Now wait until the pipe is filled */
600 rpipe->pipe_state |= PIPE_WANTR;
601 error = cv_wait_sig(&rpipe->pipe_cv, rpipe->pipe_lock);
602 if (error != 0)
603 goto unlocked_error;
604 goto again;
605 }
606
607 if (error == 0)
608 getmicrotime(&rpipe->pipe_atime);
609
610 mutex_enter(rpipe->pipe_lock);
611 pipeunlock(rpipe);
612
613 unlocked_error:
614 --rpipe->pipe_busy;
615
616 /*
617 * PIPE_WANTCLOSE processing only makes sense if pipe_busy is 0.
618 */
619 if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANTCLOSE)) {
620 rpipe->pipe_state &= ~(PIPE_WANTCLOSE|PIPE_WANTW);
621 cv_broadcast(&rpipe->pipe_cv);
622 } else if (bp->cnt < MINPIPESIZE) {
623 /*
624 * Handle write blocking hysteresis.
625 */
626 if (rpipe->pipe_state & PIPE_WANTW) {
627 rpipe->pipe_state &= ~PIPE_WANTW;
628 cv_broadcast(&rpipe->pipe_cv);
629 }
630 }
631
632 /*
633 * If anything was read off the buffer, signal to the writer it's
634 * possible to write more data. Also send signal if we are here for the
635 * first time after last write.
636 */
637 if ((bp->size - bp->cnt) >= PIPE_BUF
638 && (ocnt != bp->cnt || (rpipe->pipe_state & PIPE_SIGNALR))) {
639 pipeselwakeup(rpipe, rpipe->pipe_peer, POLL_OUT);
640 rpipe->pipe_state &= ~PIPE_SIGNALR;
641 }
642
643 mutex_exit(rpipe->pipe_lock);
644 return (error);
645 }
646
647 #ifndef PIPE_NODIRECT
648 /*
649 * Allocate structure for loan transfer.
650 */
651 static int
652 pipe_loan_alloc(struct pipe *wpipe, int npages)
653 {
654 vsize_t len;
655
656 len = (vsize_t)npages << PAGE_SHIFT;
657 wpipe->pipe_map.kva = uvm_km_alloc(kernel_map, len, 0,
658 UVM_KMF_VAONLY | UVM_KMF_WAITVA);
659 if (wpipe->pipe_map.kva == 0)
660 return (ENOMEM);
661
662 atomic_add_int(&amountpipekva, len);
663 wpipe->pipe_map.npages = npages;
664 wpipe->pipe_map.pgs = malloc(npages * sizeof(struct vm_page *), M_PIPE,
665 M_WAITOK);
666 return (0);
667 }
668
669 /*
670 * Free resources allocated for loan transfer.
671 */
672 static void
673 pipe_loan_free(struct pipe *wpipe)
674 {
675 vsize_t len;
676
677 len = (vsize_t)wpipe->pipe_map.npages << PAGE_SHIFT;
678 uvm_km_free(kernel_map, wpipe->pipe_map.kva, len, UVM_KMF_VAONLY);
679 wpipe->pipe_map.kva = 0;
680 atomic_add_int(&amountpipekva, -len);
681 free(wpipe->pipe_map.pgs, M_PIPE);
682 wpipe->pipe_map.pgs = NULL;
683 }
684
685 /*
686 * NetBSD direct write, using uvm_loan() mechanism.
687 * This implements the pipe buffer write mechanism. Note that only
688 * a direct write OR a normal pipe write can be pending at any given time.
689 * If there are any characters in the pipe buffer, the direct write will
690 * be deferred until the receiving process grabs all of the bytes from
691 * the pipe buffer. Then the direct mapping write is set-up.
692 *
693 * Called with the long-term pipe lock held.
694 */
695 static int
696 pipe_direct_write(struct file *fp, struct pipe *wpipe, struct uio *uio)
697 {
698 int error, npages, j;
699 struct vm_page **pgs;
700 vaddr_t bbase, kva, base, bend;
701 vsize_t blen, bcnt;
702 voff_t bpos;
703
704 KASSERT(wpipe->pipe_map.cnt == 0);
705
706 /*
707 * Handle first PIPE_CHUNK_SIZE bytes of buffer. Deal with buffers
708 * not aligned to PAGE_SIZE.
709 */
710 bbase = (vaddr_t)uio->uio_iov->iov_base;
711 base = trunc_page(bbase);
712 bend = round_page(bbase + uio->uio_iov->iov_len);
713 blen = bend - base;
714 bpos = bbase - base;
715
716 if (blen > PIPE_DIRECT_CHUNK) {
717 blen = PIPE_DIRECT_CHUNK;
718 bend = base + blen;
719 bcnt = PIPE_DIRECT_CHUNK - bpos;
720 } else {
721 bcnt = uio->uio_iov->iov_len;
722 }
723 npages = blen >> PAGE_SHIFT;
724
725 /*
726 * Free the old kva if we need more pages than we have
727 * allocated.
728 */
729 if (wpipe->pipe_map.kva != 0 && npages > wpipe->pipe_map.npages)
730 pipe_loan_free(wpipe);
731
732 /* Allocate new kva. */
733 if (wpipe->pipe_map.kva == 0) {
734 error = pipe_loan_alloc(wpipe, npages);
735 if (error)
736 return (error);
737 }
738
739 /* Loan the write buffer memory from writer process */
740 pgs = wpipe->pipe_map.pgs;
741 error = uvm_loan(&uio->uio_vmspace->vm_map, base, blen,
742 pgs, UVM_LOAN_TOPAGE);
743 if (error) {
744 pipe_loan_free(wpipe);
745 return (ENOMEM); /* so that caller fallback to ordinary write */
746 }
747
748 /* Enter the loaned pages to kva */
749 kva = wpipe->pipe_map.kva;
750 for (j = 0; j < npages; j++, kva += PAGE_SIZE) {
751 pmap_kenter_pa(kva, VM_PAGE_TO_PHYS(pgs[j]), VM_PROT_READ);
752 }
753 pmap_update(pmap_kernel());
754
755 /* Now we can put the pipe in direct write mode */
756 wpipe->pipe_map.pos = bpos;
757 wpipe->pipe_map.cnt = bcnt;
758
759 /*
760 * But before we can let someone do a direct read, we
761 * have to wait until the pipe is drained. Release the
762 * pipe lock while we wait.
763 */
764 mutex_enter(wpipe->pipe_lock);
765 wpipe->pipe_state |= PIPE_DIRECTW;
766 pipeunlock(wpipe);
767
768 while (error == 0 && wpipe->pipe_buffer.cnt > 0) {
769 if (wpipe->pipe_state & PIPE_WANTR) {
770 wpipe->pipe_state &= ~PIPE_WANTR;
771 cv_broadcast(&wpipe->pipe_cv);
772 }
773
774 wpipe->pipe_state |= PIPE_WANTW;
775 error = cv_wait_sig(&wpipe->pipe_cv, wpipe->pipe_lock);
776 if (error == 0 && wpipe->pipe_state & PIPE_EOF)
777 error = EPIPE;
778 }
779
780 /* Pipe is drained; next read will off the direct buffer */
781 wpipe->pipe_state |= PIPE_DIRECTR;
782
783 /* Wait until the reader is done */
784 while (error == 0 && (wpipe->pipe_state & PIPE_DIRECTR)) {
785 if (wpipe->pipe_state & PIPE_WANTR) {
786 wpipe->pipe_state &= ~PIPE_WANTR;
787 cv_broadcast(&wpipe->pipe_cv);
788 }
789 pipeselwakeup(wpipe, wpipe, POLL_IN);
790 error = cv_wait_sig(&wpipe->pipe_cv, wpipe->pipe_lock);
791 if (error == 0 && wpipe->pipe_state & PIPE_EOF)
792 error = EPIPE;
793 }
794
795 /* Take pipe out of direct write mode */
796 wpipe->pipe_state &= ~(PIPE_DIRECTW | PIPE_DIRECTR);
797
798 /* Acquire the pipe lock and cleanup */
799 (void)pipelock(wpipe, 0);
800
801 if (pgs != NULL) {
802 pmap_kremove(wpipe->pipe_map.kva, blen);
803 uvm_unloan(pgs, npages, UVM_LOAN_TOPAGE);
804 }
805 if (error || amountpipekva > maxpipekva)
806 pipe_loan_free(wpipe);
807
808 if (error) {
809 pipeselwakeup(wpipe, wpipe, POLL_ERR);
810
811 /*
812 * If nothing was read from what we offered, return error
813 * straight on. Otherwise update uio resid first. Caller
814 * will deal with the error condition, returning short
815 * write, error, or restarting the write(2) as appropriate.
816 */
817 if (wpipe->pipe_map.cnt == bcnt) {
818 wpipe->pipe_map.cnt = 0;
819 cv_broadcast(&wpipe->pipe_cv);
820 return (error);
821 }
822
823 bcnt -= wpipe->pipe_map.cnt;
824 }
825
826 uio->uio_resid -= bcnt;
827 /* uio_offset not updated, not set/used for write(2) */
828 uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + bcnt;
829 uio->uio_iov->iov_len -= bcnt;
830 if (uio->uio_iov->iov_len == 0) {
831 uio->uio_iov++;
832 uio->uio_iovcnt--;
833 }
834
835 wpipe->pipe_map.cnt = 0;
836 return (error);
837 }
838 #endif /* !PIPE_NODIRECT */
839
840 static int
841 pipe_write(struct file *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
842 int flags)
843 {
844 struct pipe *wpipe, *rpipe;
845 struct pipebuf *bp;
846 int error;
847
848 /* We want to write to our peer */
849 rpipe = (struct pipe *) fp->f_data;
850 error = 0;
851
852 mutex_enter(rpipe->pipe_lock);
853 wpipe = rpipe->pipe_peer;
854
855 /*
856 * Detect loss of pipe read side, issue SIGPIPE if lost.
857 */
858 if (wpipe == NULL) {
859 mutex_exit(rpipe->pipe_lock);
860 return EPIPE;
861 } else if ((wpipe->pipe_state & PIPE_EOF) != 0) {
862 mutex_exit(rpipe->pipe_lock);
863 return EPIPE;
864 }
865 ++wpipe->pipe_busy;
866
867 /* Aquire the long-term pipe lock */
868 if ((error = pipelock(wpipe,1)) != 0) {
869 --wpipe->pipe_busy;
870 if (wpipe->pipe_busy == 0
871 && (wpipe->pipe_state & PIPE_WANTCLOSE)) {
872 wpipe->pipe_state &= ~(PIPE_WANTCLOSE | PIPE_WANTR);
873 cv_broadcast(&wpipe->pipe_cv);
874 }
875 mutex_exit(rpipe->pipe_lock);
876 return (error);
877 }
878
879 bp = &wpipe->pipe_buffer;
880
881 /*
882 * If it is advantageous to resize the pipe buffer, do so.
883 */
884 if ((uio->uio_resid > PIPE_SIZE) &&
885 (nbigpipe < maxbigpipes) &&
886 #ifndef PIPE_NODIRECT
887 (wpipe->pipe_state & PIPE_DIRECTW) == 0 &&
888 #endif
889 (bp->size <= PIPE_SIZE) && (bp->cnt == 0)) {
890
891 if (pipespace(wpipe, BIG_PIPE_SIZE) == 0)
892 atomic_inc_uint(&nbigpipe);
893 }
894
895 while (uio->uio_resid) {
896 size_t space;
897
898 #ifndef PIPE_NODIRECT
899 /*
900 * Pipe buffered writes cannot be coincidental with
901 * direct writes. Also, only one direct write can be
902 * in progress at any one time. We wait until the currently
903 * executing direct write is completed before continuing.
904 *
905 * We break out if a signal occurs or the reader goes away.
906 */
907 while (error == 0 && wpipe->pipe_state & PIPE_DIRECTW) {
908 mutex_enter(wpipe->pipe_lock);
909 if (wpipe->pipe_state & PIPE_WANTR) {
910 wpipe->pipe_state &= ~PIPE_WANTR;
911 cv_broadcast(&wpipe->pipe_cv);
912 }
913 pipeunlock(wpipe);
914 error = cv_wait_sig(&wpipe->pipe_cv,
915 wpipe->pipe_lock);
916
917 (void)pipelock(wpipe, 0);
918 if (wpipe->pipe_state & PIPE_EOF)
919 error = EPIPE;
920 }
921 if (error)
922 break;
923
924 /*
925 * If the transfer is large, we can gain performance if
926 * we do process-to-process copies directly.
927 * If the write is non-blocking, we don't use the
928 * direct write mechanism.
929 *
930 * The direct write mechanism will detect the reader going
931 * away on us.
932 */
933 if ((uio->uio_iov->iov_len >= PIPE_MINDIRECT) &&
934 (fp->f_flag & FNONBLOCK) == 0 &&
935 (wpipe->pipe_map.kva || (amountpipekva < limitpipekva))) {
936 error = pipe_direct_write(fp, wpipe, uio);
937
938 /*
939 * Break out if error occurred, unless it's ENOMEM.
940 * ENOMEM means we failed to allocate some resources
941 * for direct write, so we just fallback to ordinary
942 * write. If the direct write was successful,
943 * process rest of data via ordinary write.
944 */
945 if (error == 0)
946 continue;
947
948 if (error != ENOMEM)
949 break;
950 }
951 #endif /* PIPE_NODIRECT */
952
953 space = bp->size - bp->cnt;
954
955 /* Writes of size <= PIPE_BUF must be atomic. */
956 if ((space < uio->uio_resid) && (uio->uio_resid <= PIPE_BUF))
957 space = 0;
958
959 if (space > 0) {
960 int size; /* Transfer size */
961 int segsize; /* first segment to transfer */
962
963 /*
964 * Transfer size is minimum of uio transfer
965 * and free space in pipe buffer.
966 */
967 if (space > uio->uio_resid)
968 size = uio->uio_resid;
969 else
970 size = space;
971 /*
972 * First segment to transfer is minimum of
973 * transfer size and contiguous space in
974 * pipe buffer. If first segment to transfer
975 * is less than the transfer size, we've got
976 * a wraparound in the buffer.
977 */
978 segsize = bp->size - bp->in;
979 if (segsize > size)
980 segsize = size;
981
982 /* Transfer first segment */
983 error = uiomove((char *)bp->buffer + bp->in, segsize,
984 uio);
985
986 if (error == 0 && segsize < size) {
987 /*
988 * Transfer remaining part now, to
989 * support atomic writes. Wraparound
990 * happened.
991 */
992 #ifdef DEBUG
993 if (bp->in + segsize != bp->size)
994 panic("Expected pipe buffer wraparound disappeared");
995 #endif
996
997 error = uiomove(bp->buffer,
998 size - segsize, uio);
999 }
1000 if (error)
1001 break;
1002
1003 bp->in += size;
1004 if (bp->in >= bp->size) {
1005 #ifdef DEBUG
1006 if (bp->in != size - segsize + bp->size)
1007 panic("Expected wraparound bad");
1008 #endif
1009 bp->in = size - segsize;
1010 }
1011
1012 bp->cnt += size;
1013 #ifdef DEBUG
1014 if (bp->cnt > bp->size)
1015 panic("Pipe buffer overflow");
1016 #endif
1017 } else {
1018 /*
1019 * If the "read-side" has been blocked, wake it up now.
1020 */
1021 mutex_enter(wpipe->pipe_lock);
1022 if (wpipe->pipe_state & PIPE_WANTR) {
1023 wpipe->pipe_state &= ~PIPE_WANTR;
1024 cv_broadcast(&wpipe->pipe_cv);
1025 }
1026 mutex_exit(wpipe->pipe_lock);
1027
1028 /*
1029 * don't block on non-blocking I/O
1030 */
1031 if (fp->f_flag & FNONBLOCK) {
1032 error = EAGAIN;
1033 break;
1034 }
1035
1036 /*
1037 * We have no more space and have something to offer,
1038 * wake up select/poll.
1039 */
1040 if (bp->cnt)
1041 pipeselwakeup(wpipe, wpipe, POLL_OUT);
1042
1043 mutex_enter(wpipe->pipe_lock);
1044 pipeunlock(wpipe);
1045 wpipe->pipe_state |= PIPE_WANTW;
1046 error = cv_wait_sig(&wpipe->pipe_cv, wpipe->pipe_lock);
1047 (void)pipelock(wpipe, 0);
1048 if (error != 0)
1049 break;
1050 /*
1051 * If read side wants to go away, we just issue a signal
1052 * to ourselves.
1053 */
1054 if (wpipe->pipe_state & PIPE_EOF) {
1055 error = EPIPE;
1056 break;
1057 }
1058 }
1059 }
1060
1061 mutex_enter(wpipe->pipe_lock);
1062 --wpipe->pipe_busy;
1063 if ((wpipe->pipe_busy == 0) && (wpipe->pipe_state & PIPE_WANTCLOSE)) {
1064 wpipe->pipe_state &= ~(PIPE_WANTCLOSE | PIPE_WANTR);
1065 cv_broadcast(&wpipe->pipe_cv);
1066 } else if (bp->cnt > 0) {
1067 /*
1068 * If we have put any characters in the buffer, we wake up
1069 * the reader.
1070 */
1071 if (wpipe->pipe_state & PIPE_WANTR) {
1072 wpipe->pipe_state &= ~PIPE_WANTR;
1073 cv_broadcast(&wpipe->pipe_cv);
1074 }
1075 }
1076
1077 /*
1078 * Don't return EPIPE if I/O was successful
1079 */
1080 if (error == EPIPE && bp->cnt == 0 && uio->uio_resid == 0)
1081 error = 0;
1082
1083 if (error == 0)
1084 getmicrotime(&wpipe->pipe_mtime);
1085
1086 /*
1087 * We have something to offer, wake up select/poll.
1088 * wpipe->pipe_map.cnt is always 0 in this point (direct write
1089 * is only done synchronously), so check only wpipe->pipe_buffer.cnt
1090 */
1091 if (bp->cnt)
1092 pipeselwakeup(wpipe, wpipe, POLL_OUT);
1093
1094 /*
1095 * Arrange for next read(2) to do a signal.
1096 */
1097 wpipe->pipe_state |= PIPE_SIGNALR;
1098
1099 pipeunlock(wpipe);
1100 mutex_exit(wpipe->pipe_lock);
1101 return (error);
1102 }
1103
1104 /*
1105 * we implement a very minimal set of ioctls for compatibility with sockets.
1106 */
1107 int
1108 pipe_ioctl(struct file *fp, u_long cmd, void *data, struct lwp *l)
1109 {
1110 struct pipe *pipe = (struct pipe *)fp->f_data;
1111 struct proc *p = l->l_proc;
1112
1113 switch (cmd) {
1114
1115 case FIONBIO:
1116 return (0);
1117
1118 case FIOASYNC:
1119 mutex_enter(pipe->pipe_lock);
1120 if (*(int *)data) {
1121 pipe->pipe_state |= PIPE_ASYNC;
1122 } else {
1123 pipe->pipe_state &= ~PIPE_ASYNC;
1124 }
1125 mutex_exit(pipe->pipe_lock);
1126 return (0);
1127
1128 case FIONREAD:
1129 mutex_enter(pipe->pipe_lock);
1130 #ifndef PIPE_NODIRECT
1131 if (pipe->pipe_state & PIPE_DIRECTW)
1132 *(int *)data = pipe->pipe_map.cnt;
1133 else
1134 #endif
1135 *(int *)data = pipe->pipe_buffer.cnt;
1136 mutex_exit(pipe->pipe_lock);
1137 return (0);
1138
1139 case FIONWRITE:
1140 /* Look at other side */
1141 pipe = pipe->pipe_peer;
1142 mutex_enter(pipe->pipe_lock);
1143 #ifndef PIPE_NODIRECT
1144 if (pipe->pipe_state & PIPE_DIRECTW)
1145 *(int *)data = pipe->pipe_map.cnt;
1146 else
1147 #endif
1148 *(int *)data = pipe->pipe_buffer.cnt;
1149 mutex_exit(pipe->pipe_lock);
1150 return (0);
1151
1152 case FIONSPACE:
1153 /* Look at other side */
1154 pipe = pipe->pipe_peer;
1155 mutex_enter(pipe->pipe_lock);
1156 #ifndef PIPE_NODIRECT
1157 /*
1158 * If we're in direct-mode, we don't really have a
1159 * send queue, and any other write will block. Thus
1160 * zero seems like the best answer.
1161 */
1162 if (pipe->pipe_state & PIPE_DIRECTW)
1163 *(int *)data = 0;
1164 else
1165 #endif
1166 *(int *)data = pipe->pipe_buffer.size -
1167 pipe->pipe_buffer.cnt;
1168 mutex_exit(pipe->pipe_lock);
1169 return (0);
1170
1171 case TIOCSPGRP:
1172 case FIOSETOWN:
1173 return fsetown(p, &pipe->pipe_pgid, cmd, data);
1174
1175 case TIOCGPGRP:
1176 case FIOGETOWN:
1177 return fgetown(p, pipe->pipe_pgid, cmd, data);
1178
1179 }
1180 return (EPASSTHROUGH);
1181 }
1182
1183 int
1184 pipe_poll(struct file *fp, int events, struct lwp *l)
1185 {
1186 struct pipe *rpipe = (struct pipe *)fp->f_data;
1187 struct pipe *wpipe;
1188 int eof = 0;
1189 int revents = 0;
1190
1191 mutex_enter(rpipe->pipe_lock);
1192 wpipe = rpipe->pipe_peer;
1193
1194 if (events & (POLLIN | POLLRDNORM))
1195 if ((rpipe->pipe_buffer.cnt > 0) ||
1196 #ifndef PIPE_NODIRECT
1197 (rpipe->pipe_state & PIPE_DIRECTR) ||
1198 #endif
1199 (rpipe->pipe_state & PIPE_EOF))
1200 revents |= events & (POLLIN | POLLRDNORM);
1201
1202 eof |= (rpipe->pipe_state & PIPE_EOF);
1203
1204 if (wpipe == NULL)
1205 revents |= events & (POLLOUT | POLLWRNORM);
1206 else {
1207 if (events & (POLLOUT | POLLWRNORM))
1208 if ((wpipe->pipe_state & PIPE_EOF) || (
1209 #ifndef PIPE_NODIRECT
1210 (wpipe->pipe_state & PIPE_DIRECTW) == 0 &&
1211 #endif
1212 (wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF))
1213 revents |= events & (POLLOUT | POLLWRNORM);
1214
1215 eof |= (wpipe->pipe_state & PIPE_EOF);
1216 }
1217
1218 if (wpipe == NULL || eof)
1219 revents |= POLLHUP;
1220
1221 if (revents == 0) {
1222 if (events & (POLLIN | POLLRDNORM))
1223 selrecord(l, &rpipe->pipe_sel);
1224
1225 if (events & (POLLOUT | POLLWRNORM))
1226 selrecord(l, &wpipe->pipe_sel);
1227 }
1228 mutex_exit(rpipe->pipe_lock);
1229
1230 return (revents);
1231 }
1232
1233 static int
1234 pipe_stat(struct file *fp, struct stat *ub, struct lwp *l)
1235 {
1236 struct pipe *pipe = (struct pipe *)fp->f_data;
1237
1238 memset((void *)ub, 0, sizeof(*ub));
1239 ub->st_mode = S_IFIFO | S_IRUSR | S_IWUSR;
1240 ub->st_blksize = pipe->pipe_buffer.size;
1241 if (ub->st_blksize == 0 && pipe->pipe_peer)
1242 ub->st_blksize = pipe->pipe_peer->pipe_buffer.size;
1243 ub->st_size = pipe->pipe_buffer.cnt;
1244 ub->st_blocks = (ub->st_size) ? 1 : 0;
1245 TIMEVAL_TO_TIMESPEC(&pipe->pipe_atime, &ub->st_atimespec);
1246 TIMEVAL_TO_TIMESPEC(&pipe->pipe_mtime, &ub->st_mtimespec);
1247 TIMEVAL_TO_TIMESPEC(&pipe->pipe_ctime, &ub->st_ctimespec);
1248 ub->st_uid = kauth_cred_geteuid(fp->f_cred);
1249 ub->st_gid = kauth_cred_getegid(fp->f_cred);
1250
1251 /*
1252 * Left as 0: st_dev, st_ino, st_nlink, st_rdev, st_flags, st_gen.
1253 * XXX (st_dev, st_ino) should be unique.
1254 */
1255 return (0);
1256 }
1257
1258 /* ARGSUSED */
1259 static int
1260 pipe_close(struct file *fp, struct lwp *l)
1261 {
1262 struct pipe *pipe = (struct pipe *)fp->f_data;
1263
1264 fp->f_data = NULL;
1265 pipeclose(fp, pipe);
1266 return (0);
1267 }
1268
1269 static void
1270 pipe_free_kmem(struct pipe *pipe)
1271 {
1272
1273 if (pipe->pipe_buffer.buffer != NULL) {
1274 if (pipe->pipe_buffer.size > PIPE_SIZE)
1275 atomic_dec_uint(&nbigpipe);
1276 atomic_add_int(&amountpipekva, -pipe->pipe_buffer.size);
1277 uvm_km_free(kernel_map,
1278 (vaddr_t)pipe->pipe_buffer.buffer,
1279 pipe->pipe_buffer.size, UVM_KMF_PAGEABLE);
1280 pipe->pipe_buffer.buffer = NULL;
1281 }
1282 #ifndef PIPE_NODIRECT
1283 if (pipe->pipe_map.kva != 0) {
1284 pipe_loan_free(pipe);
1285 pipe->pipe_map.cnt = 0;
1286 pipe->pipe_map.kva = 0;
1287 pipe->pipe_map.pos = 0;
1288 pipe->pipe_map.npages = 0;
1289 }
1290 #endif /* !PIPE_NODIRECT */
1291 }
1292
1293 /*
1294 * shutdown the pipe
1295 */
1296 static void
1297 pipeclose(struct file *fp, struct pipe *pipe)
1298 {
1299 struct pipe_mutex *mutex;
1300 struct pipe *ppipe;
1301 u_int refcnt;
1302
1303 if (pipe == NULL)
1304 return;
1305
1306 mutex_enter(pipe->pipe_lock);
1307 pipeselwakeup(pipe, pipe, POLL_HUP);
1308
1309 /*
1310 * If the other side is blocked, wake it up saying that
1311 * we want to close it down.
1312 */
1313 pipe->pipe_state |= PIPE_EOF;
1314 if (pipe->pipe_busy) {
1315 while (pipe->pipe_busy) {
1316 cv_broadcast(&pipe->pipe_cv);
1317 pipe->pipe_state |= PIPE_WANTCLOSE;
1318 cv_wait_sig(&pipe->pipe_cv, pipe->pipe_lock);
1319 }
1320 }
1321
1322 /*
1323 * Disconnect from peer
1324 */
1325 if ((ppipe = pipe->pipe_peer) != NULL) {
1326 pipeselwakeup(ppipe, ppipe, POLL_HUP);
1327 ppipe->pipe_state |= PIPE_EOF;
1328 cv_broadcast(&ppipe->pipe_cv);
1329 ppipe->pipe_peer = NULL;
1330 }
1331
1332 KASSERT((pipe->pipe_state & PIPE_LOCKFL) == 0);
1333
1334 mutex = (struct pipe_mutex *)pipe->pipe_lock;
1335 refcnt = --(mutex->pm_refcnt);
1336 KASSERT(refcnt == 0 || refcnt == 1);
1337 mutex_exit(pipe->pipe_lock);
1338
1339 /*
1340 * free resources
1341 */
1342 pipe_free_kmem(pipe);
1343 cv_destroy(&pipe->pipe_cv);
1344 cv_destroy(&pipe->pipe_lkcv);
1345 seldestroy(&pipe->pipe_sel);
1346 pool_cache_put(pipe_cache, pipe);
1347 if (refcnt == 0)
1348 pool_cache_put(pipe_mutex_cache, mutex);
1349 }
1350
1351 static void
1352 filt_pipedetach(struct knote *kn)
1353 {
1354 struct pipe *pipe = (struct pipe *)kn->kn_fp->f_data;
1355
1356 mutex_enter(pipe->pipe_lock);
1357
1358 switch(kn->kn_filter) {
1359 case EVFILT_WRITE:
1360 /* need the peer structure, not our own */
1361 pipe = pipe->pipe_peer;
1362
1363 /* if reader end already closed, just return */
1364 if (pipe == NULL) {
1365 mutex_exit(pipe->pipe_lock);
1366 return;
1367 }
1368
1369 break;
1370 default:
1371 /* nothing to do */
1372 break;
1373 }
1374
1375 #ifdef DIAGNOSTIC
1376 if (kn->kn_hook != pipe)
1377 panic("filt_pipedetach: inconsistent knote");
1378 #endif
1379
1380 SLIST_REMOVE(&pipe->pipe_sel.sel_klist, kn, knote, kn_selnext);
1381 mutex_exit(pipe->pipe_lock);
1382 }
1383
1384 /*ARGSUSED*/
1385 static int
1386 filt_piperead(struct knote *kn, long hint)
1387 {
1388 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1389 struct pipe *wpipe;
1390
1391 if ((hint & NOTE_SUBMIT) == 0) {
1392 mutex_enter(rpipe->pipe_lock);
1393 }
1394 wpipe = rpipe->pipe_peer;
1395 kn->kn_data = rpipe->pipe_buffer.cnt;
1396
1397 if ((kn->kn_data == 0) && (rpipe->pipe_state & PIPE_DIRECTW))
1398 kn->kn_data = rpipe->pipe_map.cnt;
1399
1400 if ((rpipe->pipe_state & PIPE_EOF) ||
1401 (wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1402 kn->kn_flags |= EV_EOF;
1403 if ((hint & NOTE_SUBMIT) == 0) {
1404 mutex_exit(rpipe->pipe_lock);
1405 }
1406 return (1);
1407 }
1408
1409 if ((hint & NOTE_SUBMIT) == 0) {
1410 mutex_exit(rpipe->pipe_lock);
1411 }
1412 return (kn->kn_data > 0);
1413 }
1414
1415 /*ARGSUSED*/
1416 static int
1417 filt_pipewrite(struct knote *kn, long hint)
1418 {
1419 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1420 struct pipe *wpipe;
1421
1422 if ((hint & NOTE_SUBMIT) == 0) {
1423 mutex_enter(rpipe->pipe_lock);
1424 }
1425 wpipe = rpipe->pipe_peer;
1426
1427 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1428 kn->kn_data = 0;
1429 kn->kn_flags |= EV_EOF;
1430 if ((hint & NOTE_SUBMIT) == 0) {
1431 mutex_exit(rpipe->pipe_lock);
1432 }
1433 return (1);
1434 }
1435 kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
1436 if (wpipe->pipe_state & PIPE_DIRECTW)
1437 kn->kn_data = 0;
1438
1439 if ((hint & NOTE_SUBMIT) == 0) {
1440 mutex_exit(rpipe->pipe_lock);
1441 }
1442 return (kn->kn_data >= PIPE_BUF);
1443 }
1444
1445 static const struct filterops pipe_rfiltops =
1446 { 1, NULL, filt_pipedetach, filt_piperead };
1447 static const struct filterops pipe_wfiltops =
1448 { 1, NULL, filt_pipedetach, filt_pipewrite };
1449
1450 /*ARGSUSED*/
1451 static int
1452 pipe_kqfilter(struct file *fp, struct knote *kn)
1453 {
1454 struct pipe *pipe;
1455
1456 pipe = (struct pipe *)kn->kn_fp->f_data;
1457 mutex_enter(pipe->pipe_lock);
1458
1459 switch (kn->kn_filter) {
1460 case EVFILT_READ:
1461 kn->kn_fop = &pipe_rfiltops;
1462 break;
1463 case EVFILT_WRITE:
1464 kn->kn_fop = &pipe_wfiltops;
1465 pipe = pipe->pipe_peer;
1466 if (pipe == NULL) {
1467 /* other end of pipe has been closed */
1468 mutex_exit(pipe->pipe_lock);
1469 return (EBADF);
1470 }
1471 break;
1472 default:
1473 mutex_exit(pipe->pipe_lock);
1474 return (EINVAL);
1475 }
1476
1477 kn->kn_hook = pipe;
1478 SLIST_INSERT_HEAD(&pipe->pipe_sel.sel_klist, kn, kn_selnext);
1479 mutex_exit(pipe->pipe_lock);
1480
1481 return (0);
1482 }
1483
1484 /*
1485 * Handle pipe sysctls.
1486 */
1487 SYSCTL_SETUP(sysctl_kern_pipe_setup, "sysctl kern.pipe subtree setup")
1488 {
1489
1490 sysctl_createv(clog, 0, NULL, NULL,
1491 CTLFLAG_PERMANENT,
1492 CTLTYPE_NODE, "kern", NULL,
1493 NULL, 0, NULL, 0,
1494 CTL_KERN, CTL_EOL);
1495 sysctl_createv(clog, 0, NULL, NULL,
1496 CTLFLAG_PERMANENT,
1497 CTLTYPE_NODE, "pipe",
1498 SYSCTL_DESCR("Pipe settings"),
1499 NULL, 0, NULL, 0,
1500 CTL_KERN, KERN_PIPE, CTL_EOL);
1501
1502 sysctl_createv(clog, 0, NULL, NULL,
1503 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1504 CTLTYPE_INT, "maxkvasz",
1505 SYSCTL_DESCR("Maximum amount of kernel memory to be "
1506 "used for pipes"),
1507 NULL, 0, &maxpipekva, 0,
1508 CTL_KERN, KERN_PIPE, KERN_PIPE_MAXKVASZ, CTL_EOL);
1509 sysctl_createv(clog, 0, NULL, NULL,
1510 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1511 CTLTYPE_INT, "maxloankvasz",
1512 SYSCTL_DESCR("Limit for direct transfers via page loan"),
1513 NULL, 0, &limitpipekva, 0,
1514 CTL_KERN, KERN_PIPE, KERN_PIPE_LIMITKVA, CTL_EOL);
1515 sysctl_createv(clog, 0, NULL, NULL,
1516 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1517 CTLTYPE_INT, "maxbigpipes",
1518 SYSCTL_DESCR("Maximum number of \"big\" pipes"),
1519 NULL, 0, &maxbigpipes, 0,
1520 CTL_KERN, KERN_PIPE, KERN_PIPE_MAXBIGPIPES, CTL_EOL);
1521 sysctl_createv(clog, 0, NULL, NULL,
1522 CTLFLAG_PERMANENT,
1523 CTLTYPE_INT, "nbigpipes",
1524 SYSCTL_DESCR("Number of \"big\" pipes"),
1525 NULL, 0, &nbigpipe, 0,
1526 CTL_KERN, KERN_PIPE, KERN_PIPE_NBIGPIPES, CTL_EOL);
1527 sysctl_createv(clog, 0, NULL, NULL,
1528 CTLFLAG_PERMANENT,
1529 CTLTYPE_INT, "kvasize",
1530 SYSCTL_DESCR("Amount of kernel memory consumed by pipe "
1531 "buffers"),
1532 NULL, 0, &amountpipekva, 0,
1533 CTL_KERN, KERN_PIPE, KERN_PIPE_KVASIZE, CTL_EOL);
1534 }
1535