sys_pipe.c revision 119811
1/* 2 * Copyright (c) 1996 John S. Dyson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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 15 * John S. Dyson. 16 * 4. Modifications may be freely made to this file if the above conditions 17 * are met. 18 */ 19 20/* 21 * This file contains a high-performance replacement for the socket-based 22 * pipes scheme originally used in FreeBSD/4.4Lite. It does not support 23 * all features of sockets, but does do everything that pipes normally 24 * do. 25 */ 26 27/* 28 * This code has two modes of operation, a small write mode and a large 29 * write mode. The small write mode acts like conventional pipes with 30 * a kernel buffer. If the buffer is less than PIPE_MINDIRECT, then the 31 * "normal" pipe buffering is done. If the buffer is between PIPE_MINDIRECT 32 * and PIPE_SIZE in size, it is fully mapped and wired into the kernel, and 33 * the receiving process can copy it directly from the pages in the sending 34 * process. 35 * 36 * If the sending process receives a signal, it is possible that it will 37 * go away, and certainly its address space can change, because control 38 * is returned back to the user-mode side. In that case, the pipe code 39 * arranges to copy the buffer supplied by the user process, to a pageable 40 * kernel buffer, and the receiving process will grab the data from the 41 * pageable kernel buffer. Since signals don't happen all that often, 42 * the copy operation is normally eliminated. 43 * 44 * The constant PIPE_MINDIRECT is chosen to make sure that buffering will 45 * happen for small transfers so that the system will not spend all of 46 * its time context switching. 47 * 48 * In order to limit the resource use of pipes, two sysctls exist: 49 * 50 * kern.ipc.maxpipekva - This is a hard limit on the amount of pageable 51 * address space available to us in pipe_map. Whenever the amount in use 52 * exceeds half of this value, all new pipes will be created with size 53 * SMALL_PIPE_SIZE, rather than PIPE_SIZE. Big pipe creation will be limited 54 * as well. This value is loader tunable only. 55 * 56 * kern.ipc.maxpipekvawired - This value limits the amount of memory that may 57 * be wired in order to facilitate direct copies using page flipping. 58 * Whenever this value is exceeded, pipes will fall back to using regular 59 * copies. This value is sysctl controllable at all times. 60 * 61 * These values are autotuned in subr_param.c. 62 * 63 * Memory usage may be monitored through the sysctls 64 * kern.ipc.pipes, kern.ipc.pipekva and kern.ipc.pipekvawired. 65 * 66 */ 67 68#include <sys/cdefs.h> 69__FBSDID("$FreeBSD: head/sys/kern/sys_pipe.c 119811 2003-09-06 21:02:10Z alc $"); 70 71#include "opt_mac.h" 72 73#include <sys/param.h> 74#include <sys/systm.h> 75#include <sys/fcntl.h> 76#include <sys/file.h> 77#include <sys/filedesc.h> 78#include <sys/filio.h> 79#include <sys/kernel.h> 80#include <sys/lock.h> 81#include <sys/mac.h> 82#include <sys/mutex.h> 83#include <sys/ttycom.h> 84#include <sys/stat.h> 85#include <sys/malloc.h> 86#include <sys/poll.h> 87#include <sys/selinfo.h> 88#include <sys/signalvar.h> 89#include <sys/sysctl.h> 90#include <sys/sysproto.h> 91#include <sys/pipe.h> 92#include <sys/proc.h> 93#include <sys/vnode.h> 94#include <sys/uio.h> 95#include <sys/event.h> 96 97#include <vm/vm.h> 98#include <vm/vm_param.h> 99#include <vm/vm_object.h> 100#include <vm/vm_kern.h> 101#include <vm/vm_extern.h> 102#include <vm/pmap.h> 103#include <vm/vm_map.h> 104#include <vm/vm_page.h> 105#include <vm/uma.h> 106 107/* 108 * Use this define if you want to disable *fancy* VM things. Expect an 109 * approx 30% decrease in transfer rate. This could be useful for 110 * NetBSD or OpenBSD. 111 */ 112/* #define PIPE_NODIRECT */ 113 114/* 115 * interfaces to the outside world 116 */ 117static fo_rdwr_t pipe_read; 118static fo_rdwr_t pipe_write; 119static fo_ioctl_t pipe_ioctl; 120static fo_poll_t pipe_poll; 121static fo_kqfilter_t pipe_kqfilter; 122static fo_stat_t pipe_stat; 123static fo_close_t pipe_close; 124 125static struct fileops pipeops = { 126 .fo_read = pipe_read, 127 .fo_write = pipe_write, 128 .fo_ioctl = pipe_ioctl, 129 .fo_poll = pipe_poll, 130 .fo_kqfilter = pipe_kqfilter, 131 .fo_stat = pipe_stat, 132 .fo_close = pipe_close, 133 .fo_flags = DFLAG_PASSABLE 134}; 135 136static void filt_pipedetach(struct knote *kn); 137static int filt_piperead(struct knote *kn, long hint); 138static int filt_pipewrite(struct knote *kn, long hint); 139 140static struct filterops pipe_rfiltops = 141 { 1, NULL, filt_pipedetach, filt_piperead }; 142static struct filterops pipe_wfiltops = 143 { 1, NULL, filt_pipedetach, filt_pipewrite }; 144 145#define PIPE_GET_GIANT(pipe) \ 146 do { \ 147 KASSERT(((pipe)->pipe_state & PIPE_LOCKFL) != 0, \ 148 ("%s:%d PIPE_GET_GIANT: line pipe not locked", \ 149 __FILE__, __LINE__)); \ 150 PIPE_UNLOCK(pipe); \ 151 mtx_lock(&Giant); \ 152 } while (0) 153 154#define PIPE_DROP_GIANT(pipe) \ 155 do { \ 156 mtx_unlock(&Giant); \ 157 PIPE_LOCK(pipe); \ 158 } while (0) 159 160/* 161 * Default pipe buffer size(s), this can be kind-of large now because pipe 162 * space is pageable. The pipe code will try to maintain locality of 163 * reference for performance reasons, so small amounts of outstanding I/O 164 * will not wipe the cache. 165 */ 166#define MINPIPESIZE (PIPE_SIZE/3) 167#define MAXPIPESIZE (2*PIPE_SIZE/3) 168 169/* 170 * Limit the number of "big" pipes 171 */ 172#define LIMITBIGPIPES 32 173static int nbigpipe; 174 175static int amountpipes; 176static int amountpipekva; 177static int amountpipekvawired; 178 179SYSCTL_DECL(_kern_ipc); 180 181SYSCTL_INT(_kern_ipc, OID_AUTO, maxpipekva, CTLFLAG_RD, 182 &maxpipekva, 0, "Pipe KVA limit"); 183SYSCTL_INT(_kern_ipc, OID_AUTO, maxpipekvawired, CTLFLAG_RW, 184 &maxpipekvawired, 0, "Pipe KVA wired limit"); 185SYSCTL_INT(_kern_ipc, OID_AUTO, pipes, CTLFLAG_RD, 186 &amountpipes, 0, "Current # of pipes"); 187SYSCTL_INT(_kern_ipc, OID_AUTO, bigpipes, CTLFLAG_RD, 188 &nbigpipe, 0, "Current # of big pipes"); 189SYSCTL_INT(_kern_ipc, OID_AUTO, pipekva, CTLFLAG_RD, 190 &amountpipekva, 0, "Pipe KVA usage"); 191SYSCTL_INT(_kern_ipc, OID_AUTO, pipekvawired, CTLFLAG_RD, 192 &amountpipekvawired, 0, "Pipe wired KVA usage"); 193 194static void pipeinit(void *dummy __unused); 195static void pipeclose(struct pipe *cpipe); 196static void pipe_free_kmem(struct pipe *cpipe); 197static int pipe_create(struct pipe **cpipep); 198static __inline int pipelock(struct pipe *cpipe, int catch); 199static __inline void pipeunlock(struct pipe *cpipe); 200static __inline void pipeselwakeup(struct pipe *cpipe); 201#ifndef PIPE_NODIRECT 202static int pipe_build_write_buffer(struct pipe *wpipe, struct uio *uio); 203static void pipe_destroy_write_buffer(struct pipe *wpipe); 204static int pipe_direct_write(struct pipe *wpipe, struct uio *uio); 205static void pipe_clone_write_buffer(struct pipe *wpipe); 206#endif 207static int pipespace(struct pipe *cpipe, int size); 208 209static uma_zone_t pipe_zone; 210 211SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_ANY, pipeinit, NULL); 212 213static void 214pipeinit(void *dummy __unused) 215{ 216 217 pipe_zone = uma_zcreate("PIPE", sizeof(struct pipe), NULL, 218 NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 219 KASSERT(pipe_zone != NULL, ("pipe_zone not initialized")); 220} 221 222/* 223 * The pipe system call for the DTYPE_PIPE type of pipes 224 */ 225 226/* ARGSUSED */ 227int 228pipe(td, uap) 229 struct thread *td; 230 struct pipe_args /* { 231 int dummy; 232 } */ *uap; 233{ 234 struct filedesc *fdp = td->td_proc->p_fd; 235 struct file *rf, *wf; 236 struct pipe *rpipe, *wpipe; 237 struct mtx *pmtx; 238 int fd, error; 239 240 pmtx = malloc(sizeof(*pmtx), M_TEMP, M_WAITOK | M_ZERO); 241 242 rpipe = wpipe = NULL; 243 if (pipe_create(&rpipe) || pipe_create(&wpipe)) { 244 pipeclose(rpipe); 245 pipeclose(wpipe); 246 free(pmtx, M_TEMP); 247 return (ENFILE); 248 } 249 250 rpipe->pipe_state |= PIPE_DIRECTOK; 251 wpipe->pipe_state |= PIPE_DIRECTOK; 252 253 error = falloc(td, &rf, &fd); 254 if (error) { 255 pipeclose(rpipe); 256 pipeclose(wpipe); 257 free(pmtx, M_TEMP); 258 return (error); 259 } 260 fhold(rf); 261 td->td_retval[0] = fd; 262 263 /* 264 * Warning: once we've gotten past allocation of the fd for the 265 * read-side, we can only drop the read side via fdrop() in order 266 * to avoid races against processes which manage to dup() the read 267 * side while we are blocked trying to allocate the write side. 268 */ 269 FILE_LOCK(rf); 270 rf->f_flag = FREAD | FWRITE; 271 rf->f_type = DTYPE_PIPE; 272 rf->f_data = rpipe; 273 rf->f_ops = &pipeops; 274 FILE_UNLOCK(rf); 275 error = falloc(td, &wf, &fd); 276 if (error) { 277 FILEDESC_LOCK(fdp); 278 if (fdp->fd_ofiles[td->td_retval[0]] == rf) { 279 fdp->fd_ofiles[td->td_retval[0]] = NULL; 280 FILEDESC_UNLOCK(fdp); 281 fdrop(rf, td); 282 } else 283 FILEDESC_UNLOCK(fdp); 284 fdrop(rf, td); 285 /* rpipe has been closed by fdrop(). */ 286 pipeclose(wpipe); 287 free(pmtx, M_TEMP); 288 return (error); 289 } 290 FILE_LOCK(wf); 291 wf->f_flag = FREAD | FWRITE; 292 wf->f_type = DTYPE_PIPE; 293 wf->f_data = wpipe; 294 wf->f_ops = &pipeops; 295 FILE_UNLOCK(wf); 296 td->td_retval[1] = fd; 297 rpipe->pipe_peer = wpipe; 298 wpipe->pipe_peer = rpipe; 299#ifdef MAC 300 /* 301 * struct pipe represents a pipe endpoint. The MAC label is shared 302 * between the connected endpoints. As a result mac_init_pipe() and 303 * mac_create_pipe() should only be called on one of the endpoints 304 * after they have been connected. 305 */ 306 mac_init_pipe(rpipe); 307 mac_create_pipe(td->td_ucred, rpipe); 308#endif 309 mtx_init(pmtx, "pipe mutex", NULL, MTX_DEF | MTX_RECURSE); 310 rpipe->pipe_mtxp = wpipe->pipe_mtxp = pmtx; 311 fdrop(rf, td); 312 313 return (0); 314} 315 316/* 317 * Allocate kva for pipe circular buffer, the space is pageable 318 * This routine will 'realloc' the size of a pipe safely, if it fails 319 * it will retain the old buffer. 320 * If it fails it will return ENOMEM. 321 */ 322static int 323pipespace(cpipe, size) 324 struct pipe *cpipe; 325 int size; 326{ 327 struct vm_object *object; 328 caddr_t buffer; 329 int npages, error; 330 static int curfail = 0; 331 static struct timeval lastfail; 332 333 KASSERT(cpipe->pipe_mtxp == NULL || !mtx_owned(PIPE_MTX(cpipe)), 334 ("pipespace: pipe mutex locked")); 335 336 size = round_page(size); 337 npages = size / PAGE_SIZE; 338 /* 339 * Create an object, I don't like the idea of paging to/from 340 * kernel_object. 341 * XXX -- minor change needed here for NetBSD/OpenBSD VM systems. 342 */ 343 object = vm_object_allocate(OBJT_DEFAULT, npages); 344 buffer = (caddr_t) vm_map_min(pipe_map); 345 346 /* 347 * Insert the object into the kernel map, and allocate kva for it. 348 * The map entry is, by default, pageable. 349 * XXX -- minor change needed here for NetBSD/OpenBSD VM systems. 350 */ 351 error = vm_map_find(pipe_map, object, 0, 352 (vm_offset_t *) &buffer, size, 1, 353 VM_PROT_ALL, VM_PROT_ALL, 0); 354 355 if (error != KERN_SUCCESS) { 356 vm_object_deallocate(object); 357 if (ppsratecheck(&lastfail, &curfail, 1)) 358 printf("kern.maxpipekva exceeded, please see tuning(7).\n"); 359 return (ENOMEM); 360 } 361 362 /* free old resources if we're resizing */ 363 pipe_free_kmem(cpipe); 364 cpipe->pipe_buffer.buffer = buffer; 365 cpipe->pipe_buffer.size = size; 366 cpipe->pipe_buffer.in = 0; 367 cpipe->pipe_buffer.out = 0; 368 cpipe->pipe_buffer.cnt = 0; 369 atomic_add_int(&amountpipes, 1); 370 atomic_add_int(&amountpipekva, cpipe->pipe_buffer.size); 371 return (0); 372} 373 374/* 375 * initialize and allocate VM and memory for pipe 376 */ 377static int 378pipe_create(cpipep) 379 struct pipe **cpipep; 380{ 381 struct pipe *cpipe; 382 int error; 383 384 *cpipep = uma_zalloc(pipe_zone, M_WAITOK); 385 if (*cpipep == NULL) 386 return (ENOMEM); 387 388 cpipe = *cpipep; 389 390 /* 391 * protect so pipeclose() doesn't follow a junk pointer 392 * if pipespace() fails. 393 */ 394 bzero(&cpipe->pipe_sel, sizeof(cpipe->pipe_sel)); 395 cpipe->pipe_state = 0; 396 cpipe->pipe_peer = NULL; 397 cpipe->pipe_busy = 0; 398 399#ifndef PIPE_NODIRECT 400 /* 401 * pipe data structure initializations to support direct pipe I/O 402 */ 403 cpipe->pipe_map.cnt = 0; 404 cpipe->pipe_map.kva = 0; 405 cpipe->pipe_map.pos = 0; 406 cpipe->pipe_map.npages = 0; 407 /* cpipe->pipe_map.ms[] = invalid */ 408#endif 409 410 cpipe->pipe_mtxp = NULL; /* avoid pipespace assertion */ 411 /* 412 * Reduce to 1/4th pipe size if we're over our global max. 413 */ 414 if (amountpipekva > maxpipekva / 2) 415 error = pipespace(cpipe, SMALL_PIPE_SIZE); 416 else 417 error = pipespace(cpipe, PIPE_SIZE); 418 if (error) 419 return (error); 420 421 vfs_timestamp(&cpipe->pipe_ctime); 422 cpipe->pipe_atime = cpipe->pipe_ctime; 423 cpipe->pipe_mtime = cpipe->pipe_ctime; 424 425 return (0); 426} 427 428 429/* 430 * lock a pipe for I/O, blocking other access 431 */ 432static __inline int 433pipelock(cpipe, catch) 434 struct pipe *cpipe; 435 int catch; 436{ 437 int error; 438 439 PIPE_LOCK_ASSERT(cpipe, MA_OWNED); 440 while (cpipe->pipe_state & PIPE_LOCKFL) { 441 cpipe->pipe_state |= PIPE_LWANT; 442 error = msleep(cpipe, PIPE_MTX(cpipe), 443 catch ? (PRIBIO | PCATCH) : PRIBIO, 444 "pipelk", 0); 445 if (error != 0) 446 return (error); 447 } 448 cpipe->pipe_state |= PIPE_LOCKFL; 449 return (0); 450} 451 452/* 453 * unlock a pipe I/O lock 454 */ 455static __inline void 456pipeunlock(cpipe) 457 struct pipe *cpipe; 458{ 459 460 PIPE_LOCK_ASSERT(cpipe, MA_OWNED); 461 cpipe->pipe_state &= ~PIPE_LOCKFL; 462 if (cpipe->pipe_state & PIPE_LWANT) { 463 cpipe->pipe_state &= ~PIPE_LWANT; 464 wakeup(cpipe); 465 } 466} 467 468static __inline void 469pipeselwakeup(cpipe) 470 struct pipe *cpipe; 471{ 472 473 if (cpipe->pipe_state & PIPE_SEL) { 474 cpipe->pipe_state &= ~PIPE_SEL; 475 selwakeup(&cpipe->pipe_sel); 476 } 477 if ((cpipe->pipe_state & PIPE_ASYNC) && cpipe->pipe_sigio) 478 pgsigio(&cpipe->pipe_sigio, SIGIO, 0); 479 KNOTE(&cpipe->pipe_sel.si_note, 0); 480} 481 482/* ARGSUSED */ 483static int 484pipe_read(fp, uio, active_cred, flags, td) 485 struct file *fp; 486 struct uio *uio; 487 struct ucred *active_cred; 488 struct thread *td; 489 int flags; 490{ 491 struct pipe *rpipe = fp->f_data; 492 int error; 493 int nread = 0; 494 u_int size; 495 496 PIPE_LOCK(rpipe); 497 ++rpipe->pipe_busy; 498 error = pipelock(rpipe, 1); 499 if (error) 500 goto unlocked_error; 501 502#ifdef MAC 503 error = mac_check_pipe_read(active_cred, rpipe); 504 if (error) 505 goto locked_error; 506#endif 507 508 while (uio->uio_resid) { 509 /* 510 * normal pipe buffer receive 511 */ 512 if (rpipe->pipe_buffer.cnt > 0) { 513 size = rpipe->pipe_buffer.size - rpipe->pipe_buffer.out; 514 if (size > rpipe->pipe_buffer.cnt) 515 size = rpipe->pipe_buffer.cnt; 516 if (size > (u_int) uio->uio_resid) 517 size = (u_int) uio->uio_resid; 518 519 PIPE_UNLOCK(rpipe); 520 error = uiomove( 521 &rpipe->pipe_buffer.buffer[rpipe->pipe_buffer.out], 522 size, uio); 523 PIPE_LOCK(rpipe); 524 if (error) 525 break; 526 527 rpipe->pipe_buffer.out += size; 528 if (rpipe->pipe_buffer.out >= rpipe->pipe_buffer.size) 529 rpipe->pipe_buffer.out = 0; 530 531 rpipe->pipe_buffer.cnt -= size; 532 533 /* 534 * If there is no more to read in the pipe, reset 535 * its pointers to the beginning. This improves 536 * cache hit stats. 537 */ 538 if (rpipe->pipe_buffer.cnt == 0) { 539 rpipe->pipe_buffer.in = 0; 540 rpipe->pipe_buffer.out = 0; 541 } 542 nread += size; 543#ifndef PIPE_NODIRECT 544 /* 545 * Direct copy, bypassing a kernel buffer. 546 */ 547 } else if ((size = rpipe->pipe_map.cnt) && 548 (rpipe->pipe_state & PIPE_DIRECTW)) { 549 caddr_t va; 550 if (size > (u_int) uio->uio_resid) 551 size = (u_int) uio->uio_resid; 552 553 va = (caddr_t) rpipe->pipe_map.kva + 554 rpipe->pipe_map.pos; 555 PIPE_UNLOCK(rpipe); 556 error = uiomove(va, size, uio); 557 PIPE_LOCK(rpipe); 558 if (error) 559 break; 560 nread += size; 561 rpipe->pipe_map.pos += size; 562 rpipe->pipe_map.cnt -= size; 563 if (rpipe->pipe_map.cnt == 0) { 564 rpipe->pipe_state &= ~PIPE_DIRECTW; 565 wakeup(rpipe); 566 } 567#endif 568 } else { 569 /* 570 * detect EOF condition 571 * read returns 0 on EOF, no need to set error 572 */ 573 if (rpipe->pipe_state & PIPE_EOF) 574 break; 575 576 /* 577 * If the "write-side" has been blocked, wake it up now. 578 */ 579 if (rpipe->pipe_state & PIPE_WANTW) { 580 rpipe->pipe_state &= ~PIPE_WANTW; 581 wakeup(rpipe); 582 } 583 584 /* 585 * Break if some data was read. 586 */ 587 if (nread > 0) 588 break; 589 590 /* 591 * Unlock the pipe buffer for our remaining processing. 592 * We will either break out with an error or we will 593 * sleep and relock to loop. 594 */ 595 pipeunlock(rpipe); 596 597 /* 598 * Handle non-blocking mode operation or 599 * wait for more data. 600 */ 601 if (fp->f_flag & FNONBLOCK) { 602 error = EAGAIN; 603 } else { 604 rpipe->pipe_state |= PIPE_WANTR; 605 if ((error = msleep(rpipe, PIPE_MTX(rpipe), 606 PRIBIO | PCATCH, 607 "piperd", 0)) == 0) 608 error = pipelock(rpipe, 1); 609 } 610 if (error) 611 goto unlocked_error; 612 } 613 } 614#ifdef MAC 615locked_error: 616#endif 617 pipeunlock(rpipe); 618 619 /* XXX: should probably do this before getting any locks. */ 620 if (error == 0) 621 vfs_timestamp(&rpipe->pipe_atime); 622unlocked_error: 623 --rpipe->pipe_busy; 624 625 /* 626 * PIPE_WANT processing only makes sense if pipe_busy is 0. 627 */ 628 if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANT)) { 629 rpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTW); 630 wakeup(rpipe); 631 } else if (rpipe->pipe_buffer.cnt < MINPIPESIZE) { 632 /* 633 * Handle write blocking hysteresis. 634 */ 635 if (rpipe->pipe_state & PIPE_WANTW) { 636 rpipe->pipe_state &= ~PIPE_WANTW; 637 wakeup(rpipe); 638 } 639 } 640 641 if ((rpipe->pipe_buffer.size - rpipe->pipe_buffer.cnt) >= PIPE_BUF) 642 pipeselwakeup(rpipe); 643 644 PIPE_UNLOCK(rpipe); 645 return (error); 646} 647 648#ifndef PIPE_NODIRECT 649/* 650 * Map the sending processes' buffer into kernel space and wire it. 651 * This is similar to a physical write operation. 652 */ 653static int 654pipe_build_write_buffer(wpipe, uio) 655 struct pipe *wpipe; 656 struct uio *uio; 657{ 658 u_int size; 659 int i; 660 vm_offset_t addr, endaddr; 661 vm_paddr_t paddr; 662 663 GIANT_REQUIRED; 664 PIPE_LOCK_ASSERT(wpipe, MA_NOTOWNED); 665 666 size = (u_int) uio->uio_iov->iov_len; 667 if (size > wpipe->pipe_buffer.size) 668 size = wpipe->pipe_buffer.size; 669 670 endaddr = round_page((vm_offset_t)uio->uio_iov->iov_base + size); 671 addr = trunc_page((vm_offset_t)uio->uio_iov->iov_base); 672 for (i = 0; addr < endaddr; addr += PAGE_SIZE, i++) { 673 vm_page_t m; 674 675 /* 676 * vm_fault_quick() can sleep. Consequently, 677 * vm_page_lock_queue() and vm_page_unlock_queue() 678 * should not be performed outside of this loop. 679 */ 680 if (vm_fault_quick((caddr_t)addr, VM_PROT_READ) < 0 || 681 (paddr = pmap_extract(vmspace_pmap(curproc->p_vmspace), 682 addr)) == 0) { 683 int j; 684 685 vm_page_lock_queues(); 686 for (j = 0; j < i; j++) { 687 vm_page_unhold(wpipe->pipe_map.ms[j]); 688 } 689 vm_page_unlock_queues(); 690 return (EFAULT); 691 } 692 693 m = PHYS_TO_VM_PAGE(paddr); 694 vm_page_lock_queues(); 695 vm_page_hold(m); 696 vm_page_unlock_queues(); 697 wpipe->pipe_map.ms[i] = m; 698 } 699 700/* 701 * set up the control block 702 */ 703 wpipe->pipe_map.npages = i; 704 wpipe->pipe_map.pos = 705 ((vm_offset_t) uio->uio_iov->iov_base) & PAGE_MASK; 706 wpipe->pipe_map.cnt = size; 707 708/* 709 * and map the buffer 710 */ 711 if (wpipe->pipe_map.kva == 0) { 712 /* 713 * We need to allocate space for an extra page because the 714 * address range might (will) span pages at times. 715 */ 716 wpipe->pipe_map.kva = kmem_alloc_nofault(kernel_map, 717 wpipe->pipe_buffer.size + PAGE_SIZE); 718 atomic_add_int(&amountpipekvawired, 719 wpipe->pipe_buffer.size + PAGE_SIZE); 720 } 721 pmap_qenter(wpipe->pipe_map.kva, wpipe->pipe_map.ms, 722 wpipe->pipe_map.npages); 723 724/* 725 * and update the uio data 726 */ 727 728 uio->uio_iov->iov_len -= size; 729 uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + size; 730 if (uio->uio_iov->iov_len == 0) 731 uio->uio_iov++; 732 uio->uio_resid -= size; 733 uio->uio_offset += size; 734 return (0); 735} 736 737/* 738 * unmap and unwire the process buffer 739 */ 740static void 741pipe_destroy_write_buffer(wpipe) 742 struct pipe *wpipe; 743{ 744 int i; 745 746 PIPE_LOCK_ASSERT(wpipe, MA_NOTOWNED); 747 if (wpipe->pipe_map.kva) { 748 pmap_qremove(wpipe->pipe_map.kva, wpipe->pipe_map.npages); 749 750 if (amountpipekvawired > maxpipekvawired / 2) { 751 /* Conserve address space */ 752 vm_offset_t kva = wpipe->pipe_map.kva; 753 wpipe->pipe_map.kva = 0; 754 kmem_free(kernel_map, kva, 755 wpipe->pipe_buffer.size + PAGE_SIZE); 756 atomic_subtract_int(&amountpipekvawired, 757 wpipe->pipe_buffer.size + PAGE_SIZE); 758 } 759 } 760 vm_page_lock_queues(); 761 for (i = 0; i < wpipe->pipe_map.npages; i++) { 762 vm_page_unhold(wpipe->pipe_map.ms[i]); 763 } 764 vm_page_unlock_queues(); 765 wpipe->pipe_map.npages = 0; 766} 767 768/* 769 * In the case of a signal, the writing process might go away. This 770 * code copies the data into the circular buffer so that the source 771 * pages can be freed without loss of data. 772 */ 773static void 774pipe_clone_write_buffer(wpipe) 775 struct pipe *wpipe; 776{ 777 int size; 778 int pos; 779 780 PIPE_LOCK_ASSERT(wpipe, MA_OWNED); 781 size = wpipe->pipe_map.cnt; 782 pos = wpipe->pipe_map.pos; 783 784 wpipe->pipe_buffer.in = size; 785 wpipe->pipe_buffer.out = 0; 786 wpipe->pipe_buffer.cnt = size; 787 wpipe->pipe_state &= ~PIPE_DIRECTW; 788 789 PIPE_UNLOCK(wpipe); 790 bcopy((caddr_t) wpipe->pipe_map.kva + pos, 791 wpipe->pipe_buffer.buffer, size); 792 pipe_destroy_write_buffer(wpipe); 793 PIPE_LOCK(wpipe); 794} 795 796/* 797 * This implements the pipe buffer write mechanism. Note that only 798 * a direct write OR a normal pipe write can be pending at any given time. 799 * If there are any characters in the pipe buffer, the direct write will 800 * be deferred until the receiving process grabs all of the bytes from 801 * the pipe buffer. Then the direct mapping write is set-up. 802 */ 803static int 804pipe_direct_write(wpipe, uio) 805 struct pipe *wpipe; 806 struct uio *uio; 807{ 808 int error; 809 810retry: 811 PIPE_LOCK_ASSERT(wpipe, MA_OWNED); 812 while (wpipe->pipe_state & PIPE_DIRECTW) { 813 if (wpipe->pipe_state & PIPE_WANTR) { 814 wpipe->pipe_state &= ~PIPE_WANTR; 815 wakeup(wpipe); 816 } 817 wpipe->pipe_state |= PIPE_WANTW; 818 error = msleep(wpipe, PIPE_MTX(wpipe), 819 PRIBIO | PCATCH, "pipdww", 0); 820 if (error) 821 goto error1; 822 if (wpipe->pipe_state & PIPE_EOF) { 823 error = EPIPE; 824 goto error1; 825 } 826 } 827 wpipe->pipe_map.cnt = 0; /* transfer not ready yet */ 828 if (wpipe->pipe_buffer.cnt > 0) { 829 if (wpipe->pipe_state & PIPE_WANTR) { 830 wpipe->pipe_state &= ~PIPE_WANTR; 831 wakeup(wpipe); 832 } 833 834 wpipe->pipe_state |= PIPE_WANTW; 835 error = msleep(wpipe, PIPE_MTX(wpipe), 836 PRIBIO | PCATCH, "pipdwc", 0); 837 if (error) 838 goto error1; 839 if (wpipe->pipe_state & PIPE_EOF) { 840 error = EPIPE; 841 goto error1; 842 } 843 goto retry; 844 } 845 846 wpipe->pipe_state |= PIPE_DIRECTW; 847 848 pipelock(wpipe, 0); 849 PIPE_GET_GIANT(wpipe); 850 error = pipe_build_write_buffer(wpipe, uio); 851 PIPE_DROP_GIANT(wpipe); 852 pipeunlock(wpipe); 853 if (error) { 854 wpipe->pipe_state &= ~PIPE_DIRECTW; 855 goto error1; 856 } 857 858 error = 0; 859 while (!error && (wpipe->pipe_state & PIPE_DIRECTW)) { 860 if (wpipe->pipe_state & PIPE_EOF) { 861 pipelock(wpipe, 0); 862 PIPE_UNLOCK(wpipe); 863 pipe_destroy_write_buffer(wpipe); 864 PIPE_LOCK(wpipe); 865 pipeselwakeup(wpipe); 866 pipeunlock(wpipe); 867 error = EPIPE; 868 goto error1; 869 } 870 if (wpipe->pipe_state & PIPE_WANTR) { 871 wpipe->pipe_state &= ~PIPE_WANTR; 872 wakeup(wpipe); 873 } 874 pipeselwakeup(wpipe); 875 error = msleep(wpipe, PIPE_MTX(wpipe), PRIBIO | PCATCH, 876 "pipdwt", 0); 877 } 878 879 pipelock(wpipe,0); 880 if (wpipe->pipe_state & PIPE_DIRECTW) { 881 /* 882 * this bit of trickery substitutes a kernel buffer for 883 * the process that might be going away. 884 */ 885 pipe_clone_write_buffer(wpipe); 886 } else { 887 PIPE_UNLOCK(wpipe); 888 pipe_destroy_write_buffer(wpipe); 889 PIPE_LOCK(wpipe); 890 } 891 pipeunlock(wpipe); 892 return (error); 893 894error1: 895 wakeup(wpipe); 896 return (error); 897} 898#endif 899 900static int 901pipe_write(fp, uio, active_cred, flags, td) 902 struct file *fp; 903 struct uio *uio; 904 struct ucred *active_cred; 905 struct thread *td; 906 int flags; 907{ 908 int error = 0; 909 int orig_resid; 910 struct pipe *wpipe, *rpipe; 911 912 rpipe = fp->f_data; 913 wpipe = rpipe->pipe_peer; 914 915 PIPE_LOCK(rpipe); 916 /* 917 * detect loss of pipe read side, issue SIGPIPE if lost. 918 */ 919 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) { 920 PIPE_UNLOCK(rpipe); 921 return (EPIPE); 922 } 923#ifdef MAC 924 error = mac_check_pipe_write(active_cred, wpipe); 925 if (error) { 926 PIPE_UNLOCK(rpipe); 927 return (error); 928 } 929#endif 930 ++wpipe->pipe_busy; 931 932 /* 933 * If it is advantageous to resize the pipe buffer, do 934 * so. 935 */ 936 if ((uio->uio_resid > PIPE_SIZE) && 937 (amountpipekva < maxpipekva / 2) && 938 (nbigpipe < LIMITBIGPIPES) && 939 (wpipe->pipe_state & PIPE_DIRECTW) == 0 && 940 (wpipe->pipe_buffer.size <= PIPE_SIZE) && 941 (wpipe->pipe_buffer.cnt == 0)) { 942 943 if ((error = pipelock(wpipe, 1)) == 0) { 944 PIPE_UNLOCK(wpipe); 945 if (pipespace(wpipe, BIG_PIPE_SIZE) == 0) 946 atomic_add_int(&nbigpipe, 1); 947 PIPE_LOCK(wpipe); 948 pipeunlock(wpipe); 949 } 950 } 951 952 /* 953 * If an early error occured unbusy and return, waking up any pending 954 * readers. 955 */ 956 if (error) { 957 --wpipe->pipe_busy; 958 if ((wpipe->pipe_busy == 0) && 959 (wpipe->pipe_state & PIPE_WANT)) { 960 wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR); 961 wakeup(wpipe); 962 } 963 PIPE_UNLOCK(rpipe); 964 return(error); 965 } 966 967 orig_resid = uio->uio_resid; 968 969 while (uio->uio_resid) { 970 int space; 971 972#ifndef PIPE_NODIRECT 973 /* 974 * If the transfer is large, we can gain performance if 975 * we do process-to-process copies directly. 976 * If the write is non-blocking, we don't use the 977 * direct write mechanism. 978 * 979 * The direct write mechanism will detect the reader going 980 * away on us. 981 */ 982 if ((uio->uio_iov->iov_len >= PIPE_MINDIRECT) && 983 (fp->f_flag & FNONBLOCK) == 0 && 984 amountpipekvawired + uio->uio_resid < maxpipekvawired) { 985 error = pipe_direct_write(wpipe, uio); 986 if (error) 987 break; 988 continue; 989 } 990#endif 991 992 /* 993 * Pipe buffered writes cannot be coincidental with 994 * direct writes. We wait until the currently executing 995 * direct write is completed before we start filling the 996 * pipe buffer. We break out if a signal occurs or the 997 * reader goes away. 998 */ 999 retrywrite: 1000 while (wpipe->pipe_state & PIPE_DIRECTW) { 1001 if (wpipe->pipe_state & PIPE_WANTR) { 1002 wpipe->pipe_state &= ~PIPE_WANTR; 1003 wakeup(wpipe); 1004 } 1005 error = msleep(wpipe, PIPE_MTX(rpipe), PRIBIO | PCATCH, 1006 "pipbww", 0); 1007 if (wpipe->pipe_state & PIPE_EOF) 1008 break; 1009 if (error) 1010 break; 1011 } 1012 if (wpipe->pipe_state & PIPE_EOF) { 1013 error = EPIPE; 1014 break; 1015 } 1016 1017 space = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt; 1018 1019 /* Writes of size <= PIPE_BUF must be atomic. */ 1020 if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF)) 1021 space = 0; 1022 1023 if (space > 0) { 1024 if ((error = pipelock(wpipe,1)) == 0) { 1025 int size; /* Transfer size */ 1026 int segsize; /* first segment to transfer */ 1027 1028 /* 1029 * It is possible for a direct write to 1030 * slip in on us... handle it here... 1031 */ 1032 if (wpipe->pipe_state & PIPE_DIRECTW) { 1033 pipeunlock(wpipe); 1034 goto retrywrite; 1035 } 1036 /* 1037 * If a process blocked in uiomove, our 1038 * value for space might be bad. 1039 * 1040 * XXX will we be ok if the reader has gone 1041 * away here? 1042 */ 1043 if (space > wpipe->pipe_buffer.size - 1044 wpipe->pipe_buffer.cnt) { 1045 pipeunlock(wpipe); 1046 goto retrywrite; 1047 } 1048 1049 /* 1050 * Transfer size is minimum of uio transfer 1051 * and free space in pipe buffer. 1052 */ 1053 if (space > uio->uio_resid) 1054 size = uio->uio_resid; 1055 else 1056 size = space; 1057 /* 1058 * First segment to transfer is minimum of 1059 * transfer size and contiguous space in 1060 * pipe buffer. If first segment to transfer 1061 * is less than the transfer size, we've got 1062 * a wraparound in the buffer. 1063 */ 1064 segsize = wpipe->pipe_buffer.size - 1065 wpipe->pipe_buffer.in; 1066 if (segsize > size) 1067 segsize = size; 1068 1069 /* Transfer first segment */ 1070 1071 PIPE_UNLOCK(rpipe); 1072 error = uiomove(&wpipe->pipe_buffer.buffer[wpipe->pipe_buffer.in], 1073 segsize, uio); 1074 PIPE_LOCK(rpipe); 1075 1076 if (error == 0 && segsize < size) { 1077 /* 1078 * Transfer remaining part now, to 1079 * support atomic writes. Wraparound 1080 * happened. 1081 */ 1082 if (wpipe->pipe_buffer.in + segsize != 1083 wpipe->pipe_buffer.size) 1084 panic("Expected pipe buffer " 1085 "wraparound disappeared"); 1086 1087 PIPE_UNLOCK(rpipe); 1088 error = uiomove( 1089 &wpipe->pipe_buffer.buffer[0], 1090 size - segsize, uio); 1091 PIPE_LOCK(rpipe); 1092 } 1093 if (error == 0) { 1094 wpipe->pipe_buffer.in += size; 1095 if (wpipe->pipe_buffer.in >= 1096 wpipe->pipe_buffer.size) { 1097 if (wpipe->pipe_buffer.in != 1098 size - segsize + 1099 wpipe->pipe_buffer.size) 1100 panic("Expected " 1101 "wraparound bad"); 1102 wpipe->pipe_buffer.in = size - 1103 segsize; 1104 } 1105 1106 wpipe->pipe_buffer.cnt += size; 1107 if (wpipe->pipe_buffer.cnt > 1108 wpipe->pipe_buffer.size) 1109 panic("Pipe buffer overflow"); 1110 1111 } 1112 pipeunlock(wpipe); 1113 } 1114 if (error) 1115 break; 1116 1117 } else { 1118 /* 1119 * If the "read-side" has been blocked, wake it up now. 1120 */ 1121 if (wpipe->pipe_state & PIPE_WANTR) { 1122 wpipe->pipe_state &= ~PIPE_WANTR; 1123 wakeup(wpipe); 1124 } 1125 1126 /* 1127 * don't block on non-blocking I/O 1128 */ 1129 if (fp->f_flag & FNONBLOCK) { 1130 error = EAGAIN; 1131 break; 1132 } 1133 1134 /* 1135 * We have no more space and have something to offer, 1136 * wake up select/poll. 1137 */ 1138 pipeselwakeup(wpipe); 1139 1140 wpipe->pipe_state |= PIPE_WANTW; 1141 error = msleep(wpipe, PIPE_MTX(rpipe), 1142 PRIBIO | PCATCH, "pipewr", 0); 1143 if (error != 0) 1144 break; 1145 /* 1146 * If read side wants to go away, we just issue a signal 1147 * to ourselves. 1148 */ 1149 if (wpipe->pipe_state & PIPE_EOF) { 1150 error = EPIPE; 1151 break; 1152 } 1153 } 1154 } 1155 1156 --wpipe->pipe_busy; 1157 1158 if ((wpipe->pipe_busy == 0) && (wpipe->pipe_state & PIPE_WANT)) { 1159 wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR); 1160 wakeup(wpipe); 1161 } else if (wpipe->pipe_buffer.cnt > 0) { 1162 /* 1163 * If we have put any characters in the buffer, we wake up 1164 * the reader. 1165 */ 1166 if (wpipe->pipe_state & PIPE_WANTR) { 1167 wpipe->pipe_state &= ~PIPE_WANTR; 1168 wakeup(wpipe); 1169 } 1170 } 1171 1172 /* 1173 * Don't return EPIPE if I/O was successful 1174 */ 1175 if ((wpipe->pipe_buffer.cnt == 0) && 1176 (uio->uio_resid == 0) && 1177 (error == EPIPE)) { 1178 error = 0; 1179 } 1180 1181 if (error == 0) 1182 vfs_timestamp(&wpipe->pipe_mtime); 1183 1184 /* 1185 * We have something to offer, 1186 * wake up select/poll. 1187 */ 1188 if (wpipe->pipe_buffer.cnt) 1189 pipeselwakeup(wpipe); 1190 1191 PIPE_UNLOCK(rpipe); 1192 return (error); 1193} 1194 1195/* 1196 * we implement a very minimal set of ioctls for compatibility with sockets. 1197 */ 1198static int 1199pipe_ioctl(fp, cmd, data, active_cred, td) 1200 struct file *fp; 1201 u_long cmd; 1202 void *data; 1203 struct ucred *active_cred; 1204 struct thread *td; 1205{ 1206 struct pipe *mpipe = fp->f_data; 1207#ifdef MAC 1208 int error; 1209#endif 1210 1211 PIPE_LOCK(mpipe); 1212 1213#ifdef MAC 1214 error = mac_check_pipe_ioctl(active_cred, mpipe, cmd, data); 1215 if (error) 1216 return (error); 1217#endif 1218 1219 switch (cmd) { 1220 1221 case FIONBIO: 1222 PIPE_UNLOCK(mpipe); 1223 return (0); 1224 1225 case FIOASYNC: 1226 if (*(int *)data) { 1227 mpipe->pipe_state |= PIPE_ASYNC; 1228 } else { 1229 mpipe->pipe_state &= ~PIPE_ASYNC; 1230 } 1231 PIPE_UNLOCK(mpipe); 1232 return (0); 1233 1234 case FIONREAD: 1235 if (mpipe->pipe_state & PIPE_DIRECTW) 1236 *(int *)data = mpipe->pipe_map.cnt; 1237 else 1238 *(int *)data = mpipe->pipe_buffer.cnt; 1239 PIPE_UNLOCK(mpipe); 1240 return (0); 1241 1242 case FIOSETOWN: 1243 PIPE_UNLOCK(mpipe); 1244 return (fsetown(*(int *)data, &mpipe->pipe_sigio)); 1245 1246 case FIOGETOWN: 1247 PIPE_UNLOCK(mpipe); 1248 *(int *)data = fgetown(&mpipe->pipe_sigio); 1249 return (0); 1250 1251 /* This is deprecated, FIOSETOWN should be used instead. */ 1252 case TIOCSPGRP: 1253 PIPE_UNLOCK(mpipe); 1254 return (fsetown(-(*(int *)data), &mpipe->pipe_sigio)); 1255 1256 /* This is deprecated, FIOGETOWN should be used instead. */ 1257 case TIOCGPGRP: 1258 PIPE_UNLOCK(mpipe); 1259 *(int *)data = -fgetown(&mpipe->pipe_sigio); 1260 return (0); 1261 1262 } 1263 PIPE_UNLOCK(mpipe); 1264 return (ENOTTY); 1265} 1266 1267static int 1268pipe_poll(fp, events, active_cred, td) 1269 struct file *fp; 1270 int events; 1271 struct ucred *active_cred; 1272 struct thread *td; 1273{ 1274 struct pipe *rpipe = fp->f_data; 1275 struct pipe *wpipe; 1276 int revents = 0; 1277#ifdef MAC 1278 int error; 1279#endif 1280 1281 wpipe = rpipe->pipe_peer; 1282 PIPE_LOCK(rpipe); 1283#ifdef MAC 1284 error = mac_check_pipe_poll(active_cred, rpipe); 1285 if (error) 1286 goto locked_error; 1287#endif 1288 if (events & (POLLIN | POLLRDNORM)) 1289 if ((rpipe->pipe_state & PIPE_DIRECTW) || 1290 (rpipe->pipe_buffer.cnt > 0) || 1291 (rpipe->pipe_state & PIPE_EOF)) 1292 revents |= events & (POLLIN | POLLRDNORM); 1293 1294 if (events & (POLLOUT | POLLWRNORM)) 1295 if (wpipe == NULL || (wpipe->pipe_state & PIPE_EOF) || 1296 (((wpipe->pipe_state & PIPE_DIRECTW) == 0) && 1297 (wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF)) 1298 revents |= events & (POLLOUT | POLLWRNORM); 1299 1300 if ((rpipe->pipe_state & PIPE_EOF) || 1301 (wpipe == NULL) || 1302 (wpipe->pipe_state & PIPE_EOF)) 1303 revents |= POLLHUP; 1304 1305 if (revents == 0) { 1306 if (events & (POLLIN | POLLRDNORM)) { 1307 selrecord(td, &rpipe->pipe_sel); 1308 rpipe->pipe_state |= PIPE_SEL; 1309 } 1310 1311 if (events & (POLLOUT | POLLWRNORM)) { 1312 selrecord(td, &wpipe->pipe_sel); 1313 wpipe->pipe_state |= PIPE_SEL; 1314 } 1315 } 1316#ifdef MAC 1317locked_error: 1318#endif 1319 PIPE_UNLOCK(rpipe); 1320 1321 return (revents); 1322} 1323 1324/* 1325 * We shouldn't need locks here as we're doing a read and this should 1326 * be a natural race. 1327 */ 1328static int 1329pipe_stat(fp, ub, active_cred, td) 1330 struct file *fp; 1331 struct stat *ub; 1332 struct ucred *active_cred; 1333 struct thread *td; 1334{ 1335 struct pipe *pipe = fp->f_data; 1336#ifdef MAC 1337 int error; 1338 1339 PIPE_LOCK(pipe); 1340 error = mac_check_pipe_stat(active_cred, pipe); 1341 PIPE_UNLOCK(pipe); 1342 if (error) 1343 return (error); 1344#endif 1345 bzero(ub, sizeof(*ub)); 1346 ub->st_mode = S_IFIFO; 1347 ub->st_blksize = pipe->pipe_buffer.size; 1348 ub->st_size = pipe->pipe_buffer.cnt; 1349 ub->st_blocks = (ub->st_size + ub->st_blksize - 1) / ub->st_blksize; 1350 ub->st_atimespec = pipe->pipe_atime; 1351 ub->st_mtimespec = pipe->pipe_mtime; 1352 ub->st_ctimespec = pipe->pipe_ctime; 1353 ub->st_uid = fp->f_cred->cr_uid; 1354 ub->st_gid = fp->f_cred->cr_gid; 1355 /* 1356 * Left as 0: st_dev, st_ino, st_nlink, st_rdev, st_flags, st_gen. 1357 * XXX (st_dev, st_ino) should be unique. 1358 */ 1359 return (0); 1360} 1361 1362/* ARGSUSED */ 1363static int 1364pipe_close(fp, td) 1365 struct file *fp; 1366 struct thread *td; 1367{ 1368 struct pipe *cpipe = fp->f_data; 1369 1370 fp->f_ops = &badfileops; 1371 fp->f_data = NULL; 1372 funsetown(&cpipe->pipe_sigio); 1373 pipeclose(cpipe); 1374 return (0); 1375} 1376 1377static void 1378pipe_free_kmem(cpipe) 1379 struct pipe *cpipe; 1380{ 1381 1382 KASSERT(cpipe->pipe_mtxp == NULL || !mtx_owned(PIPE_MTX(cpipe)), 1383 ("pipespace: pipe mutex locked")); 1384 1385 if (cpipe->pipe_buffer.buffer != NULL) { 1386 if (cpipe->pipe_buffer.size > PIPE_SIZE) 1387 atomic_subtract_int(&nbigpipe, 1); 1388 atomic_subtract_int(&amountpipekva, cpipe->pipe_buffer.size); 1389 atomic_subtract_int(&amountpipes, 1); 1390 vm_map_remove(pipe_map, 1391 (vm_offset_t)cpipe->pipe_buffer.buffer, 1392 (vm_offset_t)cpipe->pipe_buffer.buffer + cpipe->pipe_buffer.size); 1393 cpipe->pipe_buffer.buffer = NULL; 1394 } 1395#ifndef PIPE_NODIRECT 1396 if (cpipe->pipe_map.kva != 0) { 1397 atomic_subtract_int(&amountpipekvawired, 1398 cpipe->pipe_buffer.size + PAGE_SIZE); 1399 kmem_free(kernel_map, 1400 cpipe->pipe_map.kva, 1401 cpipe->pipe_buffer.size + PAGE_SIZE); 1402 cpipe->pipe_map.cnt = 0; 1403 cpipe->pipe_map.kva = 0; 1404 cpipe->pipe_map.pos = 0; 1405 cpipe->pipe_map.npages = 0; 1406 } 1407#endif 1408} 1409 1410/* 1411 * shutdown the pipe 1412 */ 1413static void 1414pipeclose(cpipe) 1415 struct pipe *cpipe; 1416{ 1417 struct pipe *ppipe; 1418 int hadpeer; 1419 1420 if (cpipe == NULL) 1421 return; 1422 1423 hadpeer = 0; 1424 1425 /* partially created pipes won't have a valid mutex. */ 1426 if (PIPE_MTX(cpipe) != NULL) 1427 PIPE_LOCK(cpipe); 1428 1429 pipeselwakeup(cpipe); 1430 1431 /* 1432 * If the other side is blocked, wake it up saying that 1433 * we want to close it down. 1434 */ 1435 while (cpipe->pipe_busy) { 1436 wakeup(cpipe); 1437 cpipe->pipe_state |= PIPE_WANT | PIPE_EOF; 1438 msleep(cpipe, PIPE_MTX(cpipe), PRIBIO, "pipecl", 0); 1439 } 1440 1441#ifdef MAC 1442 if (cpipe->pipe_label != NULL && cpipe->pipe_peer == NULL) 1443 mac_destroy_pipe(cpipe); 1444#endif 1445 1446 /* 1447 * Disconnect from peer 1448 */ 1449 if ((ppipe = cpipe->pipe_peer) != NULL) { 1450 hadpeer++; 1451 pipeselwakeup(ppipe); 1452 1453 ppipe->pipe_state |= PIPE_EOF; 1454 wakeup(ppipe); 1455 KNOTE(&ppipe->pipe_sel.si_note, 0); 1456 ppipe->pipe_peer = NULL; 1457 } 1458 /* 1459 * free resources 1460 */ 1461 if (PIPE_MTX(cpipe) != NULL) { 1462 PIPE_UNLOCK(cpipe); 1463 if (!hadpeer) { 1464 mtx_destroy(PIPE_MTX(cpipe)); 1465 free(PIPE_MTX(cpipe), M_TEMP); 1466 } 1467 } 1468 pipe_free_kmem(cpipe); 1469 uma_zfree(pipe_zone, cpipe); 1470} 1471 1472/*ARGSUSED*/ 1473static int 1474pipe_kqfilter(struct file *fp, struct knote *kn) 1475{ 1476 struct pipe *cpipe; 1477 1478 cpipe = kn->kn_fp->f_data; 1479 switch (kn->kn_filter) { 1480 case EVFILT_READ: 1481 kn->kn_fop = &pipe_rfiltops; 1482 break; 1483 case EVFILT_WRITE: 1484 kn->kn_fop = &pipe_wfiltops; 1485 cpipe = cpipe->pipe_peer; 1486 if (cpipe == NULL) 1487 /* other end of pipe has been closed */ 1488 return (EPIPE); 1489 break; 1490 default: 1491 return (1); 1492 } 1493 kn->kn_hook = cpipe; 1494 1495 PIPE_LOCK(cpipe); 1496 SLIST_INSERT_HEAD(&cpipe->pipe_sel.si_note, kn, kn_selnext); 1497 PIPE_UNLOCK(cpipe); 1498 return (0); 1499} 1500 1501static void 1502filt_pipedetach(struct knote *kn) 1503{ 1504 struct pipe *cpipe = (struct pipe *)kn->kn_hook; 1505 1506 PIPE_LOCK(cpipe); 1507 SLIST_REMOVE(&cpipe->pipe_sel.si_note, kn, knote, kn_selnext); 1508 PIPE_UNLOCK(cpipe); 1509} 1510 1511/*ARGSUSED*/ 1512static int 1513filt_piperead(struct knote *kn, long hint) 1514{ 1515 struct pipe *rpipe = kn->kn_fp->f_data; 1516 struct pipe *wpipe = rpipe->pipe_peer; 1517 1518 PIPE_LOCK(rpipe); 1519 kn->kn_data = rpipe->pipe_buffer.cnt; 1520 if ((kn->kn_data == 0) && (rpipe->pipe_state & PIPE_DIRECTW)) 1521 kn->kn_data = rpipe->pipe_map.cnt; 1522 1523 if ((rpipe->pipe_state & PIPE_EOF) || 1524 (wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) { 1525 kn->kn_flags |= EV_EOF; 1526 PIPE_UNLOCK(rpipe); 1527 return (1); 1528 } 1529 PIPE_UNLOCK(rpipe); 1530 return (kn->kn_data > 0); 1531} 1532 1533/*ARGSUSED*/ 1534static int 1535filt_pipewrite(struct knote *kn, long hint) 1536{ 1537 struct pipe *rpipe = kn->kn_fp->f_data; 1538 struct pipe *wpipe = rpipe->pipe_peer; 1539 1540 PIPE_LOCK(rpipe); 1541 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) { 1542 kn->kn_data = 0; 1543 kn->kn_flags |= EV_EOF; 1544 PIPE_UNLOCK(rpipe); 1545 return (1); 1546 } 1547 kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt; 1548 if (wpipe->pipe_state & PIPE_DIRECTW) 1549 kn->kn_data = 0; 1550 1551 PIPE_UNLOCK(rpipe); 1552 return (kn->kn_data >= PIPE_BUF); 1553} 1554