1 /*- 2 * Copyright (c) 1996 John S. Dyson 3 * Copyright (c) 2012 Giovanni Trematerra 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice immediately at the beginning of the file, without modification, 11 * this list of conditions, and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Absolutely no warranty of function or purpose is made by the author 16 * John S. Dyson. 17 * 4. Modifications may be freely made to this file if the above conditions 18 * are met. 19 */ 20 21 /* 22 * This file contains a high-performance replacement for the socket-based 23 * pipes scheme originally used in FreeBSD/4.4Lite. It does not support 24 * all features of sockets, but does do everything that pipes normally 25 * do. 26 */ 27 28 /* 29 * This code has two modes of operation, a small write mode and a large 30 * write mode. The small write mode acts like conventional pipes with 31 * a kernel buffer. If the buffer is less than PIPE_MINDIRECT, then the 32 * "normal" pipe buffering is done. If the buffer is between PIPE_MINDIRECT 33 * and PIPE_SIZE in size, the sending process pins the underlying pages in 34 * memory, and the receiving process copies directly from these pinned pages 35 * in the sending process. 36 * 37 * If the sending process receives a signal, it is possible that it will 38 * go away, and certainly its address space can change, because control 39 * is returned back to the user-mode side. In that case, the pipe code 40 * arranges to copy the buffer supplied by the user process, to a pageable 41 * kernel buffer, and the receiving process will grab the data from the 42 * pageable kernel buffer. Since signals don't happen all that often, 43 * the copy operation is normally eliminated. 44 * 45 * The constant PIPE_MINDIRECT is chosen to make sure that buffering will 46 * happen for small transfers so that the system will not spend all of 47 * its time context switching. 48 * 49 * In order to limit the resource use of pipes, two sysctls exist: 50 * 51 * kern.ipc.maxpipekva - This is a hard limit on the amount of pageable 52 * address space available to us in pipe_map. This value is normally 53 * autotuned, but may also be loader tuned. 54 * 55 * kern.ipc.pipekva - This read-only sysctl tracks the current amount of 56 * memory in use by pipes. 57 * 58 * Based on how large pipekva is relative to maxpipekva, the following 59 * will happen: 60 * 61 * 0% - 50%: 62 * New pipes are given 16K of memory backing, pipes may dynamically 63 * grow to as large as 64K where needed. 64 * 50% - 75%: 65 * New pipes are given 4K (or PAGE_SIZE) of memory backing, 66 * existing pipes may NOT grow. 67 * 75% - 100%: 68 * New pipes are given 4K (or PAGE_SIZE) of memory backing, 69 * existing pipes will be shrunk down to 4K whenever possible. 70 * 71 * Resizing may be disabled by setting kern.ipc.piperesizeallowed=0. If 72 * that is set, the only resize that will occur is the 0 -> SMALL_PIPE_SIZE 73 * resize which MUST occur for reverse-direction pipes when they are 74 * first used. 75 * 76 * Additional information about the current state of pipes may be obtained 77 * from kern.ipc.pipes, kern.ipc.pipefragretry, kern.ipc.pipeallocfail, 78 * and kern.ipc.piperesizefail. 79 * 80 * Locking rules: There are two locks present here: A mutex, used via 81 * PIPE_LOCK, and a flag, used via pipelock(). All locking is done via 82 * the flag, as mutexes can not persist over uiomove. The mutex 83 * exists only to guard access to the flag, and is not in itself a 84 * locking mechanism. Also note that there is only a single mutex for 85 * both directions of a pipe. 86 * 87 * As pipelock() may have to sleep before it can acquire the flag, it 88 * is important to reread all data after a call to pipelock(); everything 89 * in the structure may have changed. 90 */ 91 92 #include <sys/param.h> 93 #include <sys/systm.h> 94 #include <sys/conf.h> 95 #include <sys/fcntl.h> 96 #include <sys/file.h> 97 #include <sys/filedesc.h> 98 #include <sys/filio.h> 99 #include <sys/kernel.h> 100 #include <sys/lock.h> 101 #include <sys/mutex.h> 102 #include <sys/ttycom.h> 103 #include <sys/stat.h> 104 #include <sys/malloc.h> 105 #include <sys/poll.h> 106 #include <sys/priv.h> 107 #include <sys/selinfo.h> 108 #include <sys/signalvar.h> 109 #include <sys/syscallsubr.h> 110 #include <sys/sysctl.h> 111 #include <sys/sysproto.h> 112 #include <sys/pipe.h> 113 #include <sys/proc.h> 114 #include <sys/vnode.h> 115 #include <sys/uio.h> 116 #include <sys/user.h> 117 #include <sys/event.h> 118 119 #include <security/mac/mac_framework.h> 120 121 #include <vm/vm.h> 122 #include <vm/vm_param.h> 123 #include <vm/vm_object.h> 124 #include <vm/vm_kern.h> 125 #include <vm/vm_extern.h> 126 #include <vm/pmap.h> 127 #include <vm/vm_map.h> 128 #include <vm/vm_page.h> 129 #include <vm/uma.h> 130 131 /* 132 * Use this define if you want to disable *fancy* VM things. Expect an 133 * approx 30% decrease in transfer rate. This could be useful for 134 * NetBSD or OpenBSD. 135 */ 136 /* #define PIPE_NODIRECT */ 137 138 #define PIPE_PEER(pipe) \ 139 (((pipe)->pipe_type & PIPE_TYPE_NAMED) ? (pipe) : ((pipe)->pipe_peer)) 140 141 /* 142 * interfaces to the outside world 143 */ 144 static fo_rdwr_t pipe_read; 145 static fo_rdwr_t pipe_write; 146 static fo_truncate_t pipe_truncate; 147 static fo_ioctl_t pipe_ioctl; 148 static fo_poll_t pipe_poll; 149 static fo_kqfilter_t pipe_kqfilter; 150 static fo_stat_t pipe_stat; 151 static fo_close_t pipe_close; 152 static fo_chmod_t pipe_chmod; 153 static fo_chown_t pipe_chown; 154 static fo_fill_kinfo_t pipe_fill_kinfo; 155 156 const struct fileops pipeops = { 157 .fo_read = pipe_read, 158 .fo_write = pipe_write, 159 .fo_truncate = pipe_truncate, 160 .fo_ioctl = pipe_ioctl, 161 .fo_poll = pipe_poll, 162 .fo_kqfilter = pipe_kqfilter, 163 .fo_stat = pipe_stat, 164 .fo_close = pipe_close, 165 .fo_chmod = pipe_chmod, 166 .fo_chown = pipe_chown, 167 .fo_sendfile = invfo_sendfile, 168 .fo_fill_kinfo = pipe_fill_kinfo, 169 .fo_cmp = file_kcmp_generic, 170 .fo_flags = DFLAG_PASSABLE 171 }; 172 173 static void filt_pipedetach(struct knote *kn); 174 static void filt_pipedetach_notsup(struct knote *kn); 175 static int filt_pipenotsup(struct knote *kn, long hint); 176 static int filt_piperead(struct knote *kn, long hint); 177 static int filt_pipewrite(struct knote *kn, long hint); 178 static int filt_pipedump(struct proc *p, struct knote *kn, 179 struct kinfo_knote *kin); 180 181 static const struct filterops pipe_nfiltops = { 182 .f_isfd = 1, 183 .f_detach = filt_pipedetach_notsup, 184 .f_event = filt_pipenotsup, 185 /* no userdump */ 186 .f_copy = knote_triv_copy, 187 }; 188 static const struct filterops pipe_rfiltops = { 189 .f_isfd = 1, 190 .f_detach = filt_pipedetach, 191 .f_event = filt_piperead, 192 .f_userdump = filt_pipedump, 193 .f_copy = knote_triv_copy, 194 }; 195 static const struct filterops pipe_wfiltops = { 196 .f_isfd = 1, 197 .f_detach = filt_pipedetach, 198 .f_event = filt_pipewrite, 199 .f_userdump = filt_pipedump, 200 .f_copy = knote_triv_copy, 201 }; 202 203 /* 204 * Default pipe buffer size(s), this can be kind-of large now because pipe 205 * space is pageable. The pipe code will try to maintain locality of 206 * reference for performance reasons, so small amounts of outstanding I/O 207 * will not wipe the cache. 208 */ 209 #define MINPIPESIZE (PIPE_SIZE/3) 210 #define MAXPIPESIZE (2*PIPE_SIZE/3) 211 212 static long amountpipekva; 213 static int pipefragretry; 214 static int pipeallocfail; 215 static int piperesizefail; 216 static int piperesizeallowed = 1; 217 static long pipe_mindirect = PIPE_MINDIRECT; 218 static int pipebuf_reserv = 2; 219 220 SYSCTL_LONG(_kern_ipc, OID_AUTO, maxpipekva, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, 221 &maxpipekva, 0, "Pipe KVA limit"); 222 SYSCTL_LONG(_kern_ipc, OID_AUTO, pipekva, CTLFLAG_RD, 223 &amountpipekva, 0, "Pipe KVA usage"); 224 SYSCTL_INT(_kern_ipc, OID_AUTO, pipefragretry, CTLFLAG_RD, 225 &pipefragretry, 0, "Pipe allocation retries due to fragmentation"); 226 SYSCTL_INT(_kern_ipc, OID_AUTO, pipeallocfail, CTLFLAG_RD, 227 &pipeallocfail, 0, "Pipe allocation failures"); 228 SYSCTL_INT(_kern_ipc, OID_AUTO, piperesizefail, CTLFLAG_RD, 229 &piperesizefail, 0, "Pipe resize failures"); 230 SYSCTL_INT(_kern_ipc, OID_AUTO, piperesizeallowed, CTLFLAG_RW, 231 &piperesizeallowed, 0, "Pipe resizing allowed"); 232 SYSCTL_INT(_kern_ipc, OID_AUTO, pipebuf_reserv, CTLFLAG_RW, 233 &pipebuf_reserv, 0, 234 "Superuser-reserved percentage of the pipe buffers space"); 235 236 static void pipeinit(void *dummy __unused); 237 static void pipeclose(struct pipe *cpipe); 238 static void pipe_free_kmem(struct pipe *cpipe); 239 static int pipe_create(struct pipe *pipe, bool backing); 240 static void pipe_destroy(struct pipe *pipe); 241 static int pipe_paircreate(struct thread *td, struct pipepair **p_pp); 242 static __inline int pipelock(struct pipe *cpipe, bool catch); 243 static __inline void pipeunlock(struct pipe *cpipe); 244 static void pipe_timestamp(struct timespec *tsp); 245 #ifndef PIPE_NODIRECT 246 static int pipe_build_write_buffer(struct pipe *wpipe, struct uio *uio); 247 static void pipe_destroy_write_buffer(struct pipe *wpipe); 248 static int pipe_direct_write(struct pipe *wpipe, struct uio *uio); 249 static void pipe_clone_write_buffer(struct pipe *wpipe); 250 #endif 251 static int pipespace(struct pipe *cpipe, int size); 252 static int pipespace_new(struct pipe *cpipe, int size); 253 254 static int pipe_zone_ctor(void *mem, int size, void *arg, int flags); 255 static int pipe_zone_init(void *mem, int size, int flags); 256 static void pipe_zone_fini(void *mem, int size); 257 258 static uma_zone_t pipe_zone; 259 static struct unrhdr64 pipeino_unr; 260 static dev_t pipedev_ino; 261 262 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_ANY, pipeinit, NULL); 263 264 static void 265 pipeinit(void *dummy __unused) 266 { 267 268 pipe_zone = uma_zcreate("pipe", sizeof(struct pipepair), 269 pipe_zone_ctor, NULL, pipe_zone_init, pipe_zone_fini, 270 UMA_ALIGN_PTR, 0); 271 KASSERT(pipe_zone != NULL, ("pipe_zone not initialized")); 272 new_unrhdr64(&pipeino_unr, 1); 273 pipedev_ino = devfs_alloc_cdp_inode(); 274 KASSERT(pipedev_ino > 0, ("pipe dev inode not initialized")); 275 } 276 277 static int 278 sysctl_handle_pipe_mindirect(SYSCTL_HANDLER_ARGS) 279 { 280 int error = 0; 281 long tmp_pipe_mindirect = pipe_mindirect; 282 283 error = sysctl_handle_long(oidp, &tmp_pipe_mindirect, arg2, req); 284 if (error != 0 || req->newptr == NULL) 285 return (error); 286 287 /* 288 * Don't allow pipe_mindirect to be set so low that we violate 289 * atomicity requirements. 290 */ 291 if (tmp_pipe_mindirect <= PIPE_BUF) 292 return (EINVAL); 293 pipe_mindirect = tmp_pipe_mindirect; 294 return (0); 295 } 296 SYSCTL_OID(_kern_ipc, OID_AUTO, pipe_mindirect, CTLTYPE_LONG | CTLFLAG_RW, 297 &pipe_mindirect, 0, sysctl_handle_pipe_mindirect, "L", 298 "Minimum write size triggering VM optimization"); 299 300 static int 301 pipe_zone_ctor(void *mem, int size, void *arg, int flags) 302 { 303 struct pipepair *pp; 304 struct pipe *rpipe, *wpipe; 305 306 KASSERT(size == sizeof(*pp), ("pipe_zone_ctor: wrong size")); 307 308 pp = (struct pipepair *)mem; 309 310 /* 311 * We zero both pipe endpoints to make sure all the kmem pointers 312 * are NULL, flag fields are zero'd, etc. We timestamp both 313 * endpoints with the same time. 314 */ 315 rpipe = &pp->pp_rpipe; 316 bzero(rpipe, sizeof(*rpipe)); 317 pipe_timestamp(&rpipe->pipe_ctime); 318 rpipe->pipe_atime = rpipe->pipe_mtime = rpipe->pipe_ctime; 319 320 wpipe = &pp->pp_wpipe; 321 bzero(wpipe, sizeof(*wpipe)); 322 wpipe->pipe_ctime = rpipe->pipe_ctime; 323 wpipe->pipe_atime = wpipe->pipe_mtime = rpipe->pipe_ctime; 324 325 rpipe->pipe_peer = wpipe; 326 rpipe->pipe_pair = pp; 327 wpipe->pipe_peer = rpipe; 328 wpipe->pipe_pair = pp; 329 330 /* 331 * Mark both endpoints as present; they will later get free'd 332 * one at a time. When both are free'd, then the whole pair 333 * is released. 334 */ 335 rpipe->pipe_present = PIPE_ACTIVE; 336 wpipe->pipe_present = PIPE_ACTIVE; 337 338 /* 339 * Eventually, the MAC Framework may initialize the label 340 * in ctor or init, but for now we do it elswhere to avoid 341 * blocking in ctor or init. 342 */ 343 pp->pp_label = NULL; 344 345 return (0); 346 } 347 348 static int 349 pipe_zone_init(void *mem, int size, int flags) 350 { 351 struct pipepair *pp; 352 353 KASSERT(size == sizeof(*pp), ("pipe_zone_init: wrong size")); 354 355 pp = (struct pipepair *)mem; 356 357 mtx_init(&pp->pp_mtx, "pipe mutex", NULL, MTX_DEF | MTX_NEW); 358 return (0); 359 } 360 361 static void 362 pipe_zone_fini(void *mem, int size) 363 { 364 struct pipepair *pp; 365 366 KASSERT(size == sizeof(*pp), ("pipe_zone_fini: wrong size")); 367 368 pp = (struct pipepair *)mem; 369 370 mtx_destroy(&pp->pp_mtx); 371 } 372 373 static int 374 pipe_paircreate(struct thread *td, struct pipepair **p_pp) 375 { 376 struct pipepair *pp; 377 struct pipe *rpipe, *wpipe; 378 int error; 379 380 *p_pp = pp = uma_zalloc(pipe_zone, M_WAITOK); 381 #ifdef MAC 382 /* 383 * The MAC label is shared between the connected endpoints. As a 384 * result mac_pipe_init() and mac_pipe_create() are called once 385 * for the pair, and not on the endpoints. 386 */ 387 mac_pipe_init(pp); 388 mac_pipe_create(td->td_ucred, pp); 389 #endif 390 rpipe = &pp->pp_rpipe; 391 wpipe = &pp->pp_wpipe; 392 pp->pp_owner = crhold(td->td_ucred); 393 394 knlist_init_mtx(&rpipe->pipe_sel.si_note, PIPE_MTX(rpipe)); 395 knlist_init_mtx(&wpipe->pipe_sel.si_note, PIPE_MTX(wpipe)); 396 397 /* 398 * Only the forward direction pipe is backed by big buffer by 399 * default. 400 */ 401 error = pipe_create(rpipe, true); 402 if (error != 0) 403 goto fail; 404 error = pipe_create(wpipe, false); 405 if (error != 0) { 406 pipe_destroy(rpipe); 407 goto fail; 408 } 409 410 rpipe->pipe_state |= PIPE_DIRECTOK; 411 wpipe->pipe_state |= PIPE_DIRECTOK; 412 return (0); 413 414 fail: 415 knlist_destroy(&rpipe->pipe_sel.si_note); 416 knlist_destroy(&wpipe->pipe_sel.si_note); 417 crfree(pp->pp_owner); 418 #ifdef MAC 419 mac_pipe_destroy(pp); 420 #endif 421 uma_zfree(pipe_zone, pp); 422 return (error); 423 } 424 425 int 426 pipe_named_ctor(struct pipe **ppipe, struct thread *td) 427 { 428 struct pipepair *pp; 429 int error; 430 431 error = pipe_paircreate(td, &pp); 432 if (error != 0) 433 return (error); 434 pp->pp_rpipe.pipe_type |= PIPE_TYPE_NAMED; 435 *ppipe = &pp->pp_rpipe; 436 return (0); 437 } 438 439 void 440 pipe_dtor(struct pipe *dpipe) 441 { 442 struct pipe *peer; 443 444 peer = (dpipe->pipe_type & PIPE_TYPE_NAMED) != 0 ? dpipe->pipe_peer : NULL; 445 funsetown(&dpipe->pipe_sigio); 446 pipeclose(dpipe); 447 if (peer != NULL) { 448 funsetown(&peer->pipe_sigio); 449 pipeclose(peer); 450 } 451 } 452 453 /* 454 * Get a timestamp. 455 * 456 * This used to be vfs_timestamp but the higher precision is unnecessary and 457 * can very negatively affect performance in virtualized environments (e.g., on 458 * vms running on amd64 when using the rdtscp instruction). 459 */ 460 static void 461 pipe_timestamp(struct timespec *tsp) 462 { 463 464 getnanotime(tsp); 465 } 466 467 /* 468 * The pipe system call for the DTYPE_PIPE type of pipes. If we fail, let 469 * the zone pick up the pieces via pipeclose(). 470 */ 471 int 472 kern_pipe(struct thread *td, int fildes[2], int flags, struct filecaps *fcaps1, 473 struct filecaps *fcaps2) 474 { 475 struct file *rf, *wf; 476 struct pipe *rpipe, *wpipe; 477 struct pipepair *pp; 478 int fd, fflags, error; 479 480 error = pipe_paircreate(td, &pp); 481 if (error != 0) 482 return (error); 483 rpipe = &pp->pp_rpipe; 484 wpipe = &pp->pp_wpipe; 485 error = falloc_caps(td, &rf, &fd, flags, fcaps1); 486 if (error) { 487 pipeclose(rpipe); 488 pipeclose(wpipe); 489 return (error); 490 } 491 /* An extra reference on `rf' has been held for us by falloc_caps(). */ 492 fildes[0] = fd; 493 494 fflags = FREAD | FWRITE; 495 if ((flags & O_NONBLOCK) != 0) 496 fflags |= FNONBLOCK; 497 498 /* 499 * Warning: once we've gotten past allocation of the fd for the 500 * read-side, we can only drop the read side via fdrop() in order 501 * to avoid races against processes which manage to dup() the read 502 * side while we are blocked trying to allocate the write side. 503 */ 504 finit(rf, fflags, DTYPE_PIPE, rpipe, &pipeops); 505 error = falloc_caps(td, &wf, &fd, flags, fcaps2); 506 if (error) { 507 fdclose(td, rf, fildes[0]); 508 fdrop(rf, td); 509 /* rpipe has been closed by fdrop(). */ 510 pipeclose(wpipe); 511 return (error); 512 } 513 /* An extra reference on `wf' has been held for us by falloc_caps(). */ 514 finit(wf, fflags, DTYPE_PIPE, wpipe, &pipeops); 515 fdrop(wf, td); 516 fildes[1] = fd; 517 fdrop(rf, td); 518 519 return (0); 520 } 521 522 #ifdef COMPAT_FREEBSD10 523 /* ARGSUSED */ 524 int 525 freebsd10_pipe(struct thread *td, struct freebsd10_pipe_args *uap __unused) 526 { 527 int error; 528 int fildes[2]; 529 530 error = kern_pipe(td, fildes, 0, NULL, NULL); 531 if (error) 532 return (error); 533 534 td->td_retval[0] = fildes[0]; 535 td->td_retval[1] = fildes[1]; 536 537 return (0); 538 } 539 #endif 540 541 int 542 sys_pipe2(struct thread *td, struct pipe2_args *uap) 543 { 544 int error, fildes[2]; 545 546 if ((uap->flags & ~(O_CLOEXEC | O_CLOFORK | O_NONBLOCK)) != 0) 547 return (EINVAL); 548 error = kern_pipe(td, fildes, uap->flags, NULL, NULL); 549 if (error) 550 return (error); 551 error = copyout(fildes, uap->fildes, 2 * sizeof(int)); 552 if (error) { 553 (void)kern_close(td, fildes[0]); 554 (void)kern_close(td, fildes[1]); 555 } 556 return (error); 557 } 558 559 /* 560 * Allocate kva for pipe circular buffer, the space is pageable 561 * This routine will 'realloc' the size of a pipe safely, if it fails 562 * it will retain the old buffer. 563 * If it fails it will return ENOMEM. 564 */ 565 static int 566 pipespace_new(struct pipe *cpipe, int size) 567 { 568 caddr_t buffer; 569 int error, cnt, firstseg; 570 static int curfail = 0; 571 static struct timeval lastfail; 572 573 PIPE_LOCK_ASSERT(cpipe, MA_NOTOWNED); 574 KASSERT(!(cpipe->pipe_state & PIPE_DIRECTW), 575 ("pipespace: resize of direct writes not allowed")); 576 retry: 577 cnt = cpipe->pipe_buffer.cnt; 578 if (cnt > size) 579 size = cnt; 580 581 size = round_page(size); 582 buffer = (caddr_t) vm_map_min(pipe_map); 583 584 if (!chgpipecnt(cpipe->pipe_pair->pp_owner->cr_ruidinfo, 585 size, lim_cur(curthread, RLIMIT_PIPEBUF))) { 586 if (cpipe->pipe_buffer.buffer == NULL && 587 size > SMALL_PIPE_SIZE) { 588 size = SMALL_PIPE_SIZE; 589 goto retry; 590 } 591 return (ENOMEM); 592 } 593 594 vm_map_lock(pipe_map); 595 if (priv_check(curthread, PRIV_PIPEBUF) != 0 && maxpipekva / 100 * 596 (100 - pipebuf_reserv) < amountpipekva + size) { 597 vm_map_unlock(pipe_map); 598 chgpipecnt(cpipe->pipe_pair->pp_owner->cr_ruidinfo, -size, 0); 599 if (cpipe->pipe_buffer.buffer == NULL && 600 size > SMALL_PIPE_SIZE) { 601 size = SMALL_PIPE_SIZE; 602 pipefragretry++; 603 goto retry; 604 } 605 return (ENOMEM); 606 } 607 error = vm_map_find_locked(pipe_map, NULL, 0, (vm_offset_t *)&buffer, 608 size, 0, VMFS_ANY_SPACE, VM_PROT_RW, VM_PROT_RW, 0); 609 vm_map_unlock(pipe_map); 610 if (error != KERN_SUCCESS) { 611 chgpipecnt(cpipe->pipe_pair->pp_owner->cr_ruidinfo, -size, 0); 612 if (cpipe->pipe_buffer.buffer == NULL && 613 size > SMALL_PIPE_SIZE) { 614 size = SMALL_PIPE_SIZE; 615 pipefragretry++; 616 goto retry; 617 } 618 if (cpipe->pipe_buffer.buffer == NULL) { 619 pipeallocfail++; 620 if (ppsratecheck(&lastfail, &curfail, 1)) 621 printf("kern.ipc.maxpipekva exceeded; see tuning(7)\n"); 622 } else { 623 piperesizefail++; 624 } 625 return (ENOMEM); 626 } 627 628 /* copy data, then free old resources if we're resizing */ 629 if (cnt > 0) { 630 if (cpipe->pipe_buffer.in <= cpipe->pipe_buffer.out) { 631 firstseg = cpipe->pipe_buffer.size - cpipe->pipe_buffer.out; 632 bcopy(&cpipe->pipe_buffer.buffer[cpipe->pipe_buffer.out], 633 buffer, firstseg); 634 if ((cnt - firstseg) > 0) 635 bcopy(cpipe->pipe_buffer.buffer, &buffer[firstseg], 636 cpipe->pipe_buffer.in); 637 } else { 638 bcopy(&cpipe->pipe_buffer.buffer[cpipe->pipe_buffer.out], 639 buffer, cnt); 640 } 641 } 642 pipe_free_kmem(cpipe); 643 cpipe->pipe_buffer.buffer = buffer; 644 cpipe->pipe_buffer.size = size; 645 cpipe->pipe_buffer.in = cnt; 646 cpipe->pipe_buffer.out = 0; 647 cpipe->pipe_buffer.cnt = cnt; 648 atomic_add_long(&amountpipekva, cpipe->pipe_buffer.size); 649 return (0); 650 } 651 652 /* 653 * Wrapper for pipespace_new() that performs locking assertions. 654 */ 655 static int 656 pipespace(struct pipe *cpipe, int size) 657 { 658 659 KASSERT(cpipe->pipe_state & PIPE_LOCKFL, 660 ("Unlocked pipe passed to pipespace")); 661 return (pipespace_new(cpipe, size)); 662 } 663 664 /* 665 * lock a pipe for I/O, blocking other access 666 */ 667 static __inline int 668 pipelock(struct pipe *cpipe, bool catch) 669 { 670 int error, prio; 671 672 PIPE_LOCK_ASSERT(cpipe, MA_OWNED); 673 674 prio = PRIBIO; 675 if (catch) 676 prio |= PCATCH; 677 while (cpipe->pipe_state & PIPE_LOCKFL) { 678 KASSERT(cpipe->pipe_waiters >= 0, 679 ("%s: bad waiter count %d", __func__, 680 cpipe->pipe_waiters)); 681 cpipe->pipe_waiters++; 682 error = msleep(&cpipe->pipe_waiters, PIPE_MTX(cpipe), prio, 683 "pipelk", 0); 684 cpipe->pipe_waiters--; 685 if (error != 0) 686 return (error); 687 } 688 cpipe->pipe_state |= PIPE_LOCKFL; 689 return (0); 690 } 691 692 /* 693 * unlock a pipe I/O lock 694 */ 695 static __inline void 696 pipeunlock(struct pipe *cpipe) 697 { 698 699 PIPE_LOCK_ASSERT(cpipe, MA_OWNED); 700 KASSERT(cpipe->pipe_state & PIPE_LOCKFL, 701 ("Unlocked pipe passed to pipeunlock")); 702 KASSERT(cpipe->pipe_waiters >= 0, 703 ("%s: bad waiter count %d", __func__, 704 cpipe->pipe_waiters)); 705 cpipe->pipe_state &= ~PIPE_LOCKFL; 706 if (cpipe->pipe_waiters > 0) 707 wakeup_one(&cpipe->pipe_waiters); 708 } 709 710 void 711 pipeselwakeup(struct pipe *cpipe) 712 { 713 714 PIPE_LOCK_ASSERT(cpipe, MA_OWNED); 715 if (cpipe->pipe_state & PIPE_SEL) { 716 selwakeuppri(&cpipe->pipe_sel, PSOCK); 717 if (!SEL_WAITING(&cpipe->pipe_sel)) 718 cpipe->pipe_state &= ~PIPE_SEL; 719 } 720 if ((cpipe->pipe_state & PIPE_ASYNC) && cpipe->pipe_sigio) 721 pgsigio(&cpipe->pipe_sigio, SIGIO, 0); 722 KNOTE_LOCKED(&cpipe->pipe_sel.si_note, 0); 723 } 724 725 /* 726 * Initialize and allocate VM and memory for pipe. The structure 727 * will start out zero'd from the ctor, so we just manage the kmem. 728 */ 729 static int 730 pipe_create(struct pipe *pipe, bool large_backing) 731 { 732 int error; 733 734 error = pipespace_new(pipe, !large_backing || amountpipekva > 735 maxpipekva / 2 ? SMALL_PIPE_SIZE : PIPE_SIZE); 736 if (error == 0) 737 pipe->pipe_ino = alloc_unr64(&pipeino_unr); 738 return (error); 739 } 740 741 static void 742 pipe_destroy(struct pipe *pipe) 743 { 744 pipe_free_kmem(pipe); 745 /* 746 * Note: we "leak" pipe_ino -- by design the alloc_unr64 mechanism does 747 * not undo allocations. 748 */ 749 } 750 751 /* ARGSUSED */ 752 static int 753 pipe_read(struct file *fp, struct uio *uio, struct ucred *active_cred, 754 int flags, struct thread *td) 755 { 756 struct pipe *rpipe; 757 int error; 758 int nread = 0; 759 int size; 760 761 rpipe = fp->f_data; 762 763 /* 764 * Try to avoid locking the pipe if we have nothing to do. 765 * 766 * There are programs which share one pipe amongst multiple processes 767 * and perform non-blocking reads in parallel, even if the pipe is 768 * empty. This in particular is the case with BSD make, which when 769 * spawned with a high -j number can find itself with over half of the 770 * calls failing to find anything. 771 */ 772 if ((fp->f_flag & FNONBLOCK) != 0 && !mac_pipe_check_read_enabled()) { 773 if (__predict_false(uio->uio_resid == 0)) 774 return (0); 775 if ((atomic_load_short(&rpipe->pipe_state) & PIPE_EOF) == 0 && 776 atomic_load_int(&rpipe->pipe_buffer.cnt) == 0 && 777 atomic_load_int(&rpipe->pipe_pages.cnt) == 0) 778 return (EAGAIN); 779 } 780 781 PIPE_LOCK(rpipe); 782 ++rpipe->pipe_busy; 783 error = pipelock(rpipe, true); 784 if (error) 785 goto unlocked_error; 786 787 #ifdef MAC 788 error = mac_pipe_check_read(active_cred, rpipe->pipe_pair); 789 if (error) 790 goto locked_error; 791 #endif 792 if (amountpipekva > (3 * maxpipekva) / 4) { 793 if ((rpipe->pipe_state & PIPE_DIRECTW) == 0 && 794 rpipe->pipe_buffer.size > SMALL_PIPE_SIZE && 795 rpipe->pipe_buffer.cnt <= SMALL_PIPE_SIZE && 796 piperesizeallowed == 1) { 797 PIPE_UNLOCK(rpipe); 798 pipespace(rpipe, SMALL_PIPE_SIZE); 799 PIPE_LOCK(rpipe); 800 } 801 } 802 803 while (uio->uio_resid) { 804 /* 805 * normal pipe buffer receive 806 */ 807 if (rpipe->pipe_buffer.cnt > 0) { 808 size = rpipe->pipe_buffer.size - rpipe->pipe_buffer.out; 809 if (size > rpipe->pipe_buffer.cnt) 810 size = rpipe->pipe_buffer.cnt; 811 if (size > uio->uio_resid) 812 size = uio->uio_resid; 813 814 PIPE_UNLOCK(rpipe); 815 error = uiomove( 816 &rpipe->pipe_buffer.buffer[rpipe->pipe_buffer.out], 817 size, uio); 818 PIPE_LOCK(rpipe); 819 if (error) 820 break; 821 822 rpipe->pipe_buffer.out += size; 823 if (rpipe->pipe_buffer.out >= rpipe->pipe_buffer.size) 824 rpipe->pipe_buffer.out = 0; 825 826 rpipe->pipe_buffer.cnt -= size; 827 828 /* 829 * If there is no more to read in the pipe, reset 830 * its pointers to the beginning. This improves 831 * cache hit stats. 832 */ 833 if (rpipe->pipe_buffer.cnt == 0) { 834 rpipe->pipe_buffer.in = 0; 835 rpipe->pipe_buffer.out = 0; 836 } 837 nread += size; 838 #ifndef PIPE_NODIRECT 839 /* 840 * Direct copy, bypassing a kernel buffer. 841 */ 842 } else if ((size = rpipe->pipe_pages.cnt) != 0) { 843 if (size > uio->uio_resid) 844 size = (u_int) uio->uio_resid; 845 PIPE_UNLOCK(rpipe); 846 error = uiomove_fromphys(rpipe->pipe_pages.ms, 847 rpipe->pipe_pages.pos, size, uio); 848 PIPE_LOCK(rpipe); 849 if (error) 850 break; 851 nread += size; 852 rpipe->pipe_pages.pos += size; 853 rpipe->pipe_pages.cnt -= size; 854 if (rpipe->pipe_pages.cnt == 0) { 855 rpipe->pipe_state &= ~PIPE_WANTW; 856 wakeup(rpipe); 857 } 858 #endif 859 } else { 860 /* 861 * detect EOF condition 862 * read returns 0 on EOF, no need to set error 863 */ 864 if (rpipe->pipe_state & PIPE_EOF) 865 break; 866 867 /* 868 * If the "write-side" has been blocked, wake it up now. 869 */ 870 if (rpipe->pipe_state & PIPE_WANTW) { 871 rpipe->pipe_state &= ~PIPE_WANTW; 872 wakeup(rpipe); 873 } 874 875 /* 876 * Break if some data was read. 877 */ 878 if (nread > 0) 879 break; 880 881 /* 882 * Unlock the pipe buffer for our remaining processing. 883 * We will either break out with an error or we will 884 * sleep and relock to loop. 885 */ 886 pipeunlock(rpipe); 887 888 /* 889 * Handle non-blocking mode operation or 890 * wait for more data. 891 */ 892 if (fp->f_flag & FNONBLOCK) { 893 error = EAGAIN; 894 } else { 895 rpipe->pipe_state |= PIPE_WANTR; 896 if ((error = msleep(rpipe, PIPE_MTX(rpipe), 897 PRIBIO | PCATCH, 898 "piperd", 0)) == 0) 899 error = pipelock(rpipe, true); 900 } 901 if (error) 902 goto unlocked_error; 903 } 904 } 905 #ifdef MAC 906 locked_error: 907 #endif 908 pipeunlock(rpipe); 909 910 /* XXX: should probably do this before getting any locks. */ 911 if (error == 0) 912 pipe_timestamp(&rpipe->pipe_atime); 913 unlocked_error: 914 --rpipe->pipe_busy; 915 916 /* 917 * PIPE_WANT processing only makes sense if pipe_busy is 0. 918 */ 919 if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANT)) { 920 rpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTW); 921 wakeup(rpipe); 922 } else if (rpipe->pipe_buffer.cnt < MINPIPESIZE) { 923 /* 924 * Handle write blocking hysteresis. 925 */ 926 if (rpipe->pipe_state & PIPE_WANTW) { 927 rpipe->pipe_state &= ~PIPE_WANTW; 928 wakeup(rpipe); 929 } 930 } 931 932 /* 933 * Only wake up writers if there was actually something read. 934 * Otherwise, when calling read(2) at EOF, a spurious wakeup occurs. 935 */ 936 if (nread > 0 && 937 rpipe->pipe_buffer.size - rpipe->pipe_buffer.cnt >= PIPE_BUF) 938 pipeselwakeup(rpipe); 939 940 PIPE_UNLOCK(rpipe); 941 if (nread > 0) 942 td->td_ru.ru_msgrcv++; 943 return (error); 944 } 945 946 #ifndef PIPE_NODIRECT 947 /* 948 * Map the sending processes' buffer into kernel space and wire it. 949 * This is similar to a physical write operation. 950 */ 951 static int 952 pipe_build_write_buffer(struct pipe *wpipe, struct uio *uio) 953 { 954 u_int size; 955 int i; 956 957 PIPE_LOCK_ASSERT(wpipe, MA_OWNED); 958 KASSERT((wpipe->pipe_state & PIPE_DIRECTW) == 0, 959 ("%s: PIPE_DIRECTW set on %p", __func__, wpipe)); 960 KASSERT(wpipe->pipe_pages.cnt == 0, 961 ("%s: pipe map for %p contains residual data", __func__, wpipe)); 962 963 if (uio->uio_iov->iov_len > wpipe->pipe_buffer.size) 964 size = wpipe->pipe_buffer.size; 965 else 966 size = uio->uio_iov->iov_len; 967 968 wpipe->pipe_state |= PIPE_DIRECTW; 969 PIPE_UNLOCK(wpipe); 970 i = vm_fault_quick_hold_pages(&curproc->p_vmspace->vm_map, 971 (vm_offset_t)uio->uio_iov->iov_base, size, VM_PROT_READ, 972 wpipe->pipe_pages.ms, PIPENPAGES); 973 PIPE_LOCK(wpipe); 974 if (i < 0) { 975 wpipe->pipe_state &= ~PIPE_DIRECTW; 976 return (EFAULT); 977 } 978 979 wpipe->pipe_pages.npages = i; 980 wpipe->pipe_pages.pos = 981 ((vm_offset_t) uio->uio_iov->iov_base) & PAGE_MASK; 982 wpipe->pipe_pages.cnt = size; 983 984 uio->uio_iov->iov_len -= size; 985 uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + size; 986 if (uio->uio_iov->iov_len == 0) { 987 uio->uio_iov++; 988 uio->uio_iovcnt--; 989 } 990 uio->uio_resid -= size; 991 uio->uio_offset += size; 992 return (0); 993 } 994 995 /* 996 * Unwire the process buffer. 997 */ 998 static void 999 pipe_destroy_write_buffer(struct pipe *wpipe) 1000 { 1001 1002 PIPE_LOCK_ASSERT(wpipe, MA_OWNED); 1003 KASSERT((wpipe->pipe_state & PIPE_DIRECTW) != 0, 1004 ("%s: PIPE_DIRECTW not set on %p", __func__, wpipe)); 1005 KASSERT(wpipe->pipe_pages.cnt == 0, 1006 ("%s: pipe map for %p contains residual data", __func__, wpipe)); 1007 1008 wpipe->pipe_state &= ~PIPE_DIRECTW; 1009 vm_page_unhold_pages(wpipe->pipe_pages.ms, wpipe->pipe_pages.npages); 1010 wpipe->pipe_pages.npages = 0; 1011 } 1012 1013 /* 1014 * In the case of a signal, the writing process might go away. This 1015 * code copies the data into the circular buffer so that the source 1016 * pages can be freed without loss of data. 1017 */ 1018 static void 1019 pipe_clone_write_buffer(struct pipe *wpipe) 1020 { 1021 struct uio uio; 1022 struct iovec iov; 1023 int size; 1024 int pos; 1025 1026 PIPE_LOCK_ASSERT(wpipe, MA_OWNED); 1027 KASSERT((wpipe->pipe_state & PIPE_DIRECTW) != 0, 1028 ("%s: PIPE_DIRECTW not set on %p", __func__, wpipe)); 1029 1030 size = wpipe->pipe_pages.cnt; 1031 pos = wpipe->pipe_pages.pos; 1032 wpipe->pipe_pages.cnt = 0; 1033 1034 wpipe->pipe_buffer.in = size; 1035 wpipe->pipe_buffer.out = 0; 1036 wpipe->pipe_buffer.cnt = size; 1037 1038 PIPE_UNLOCK(wpipe); 1039 iov.iov_base = wpipe->pipe_buffer.buffer; 1040 iov.iov_len = size; 1041 uio.uio_iov = &iov; 1042 uio.uio_iovcnt = 1; 1043 uio.uio_offset = 0; 1044 uio.uio_resid = size; 1045 uio.uio_segflg = UIO_SYSSPACE; 1046 uio.uio_rw = UIO_READ; 1047 uio.uio_td = curthread; 1048 uiomove_fromphys(wpipe->pipe_pages.ms, pos, size, &uio); 1049 PIPE_LOCK(wpipe); 1050 pipe_destroy_write_buffer(wpipe); 1051 } 1052 1053 /* 1054 * This implements the pipe buffer write mechanism. Note that only 1055 * a direct write OR a normal pipe write can be pending at any given time. 1056 * If there are any characters in the pipe buffer, the direct write will 1057 * be deferred until the receiving process grabs all of the bytes from 1058 * the pipe buffer. Then the direct mapping write is set-up. 1059 */ 1060 static int 1061 pipe_direct_write(struct pipe *wpipe, struct uio *uio) 1062 { 1063 int error; 1064 1065 retry: 1066 PIPE_LOCK_ASSERT(wpipe, MA_OWNED); 1067 if ((wpipe->pipe_state & PIPE_EOF) != 0) { 1068 error = EPIPE; 1069 goto error1; 1070 } 1071 if (wpipe->pipe_state & PIPE_DIRECTW) { 1072 if (wpipe->pipe_state & PIPE_WANTR) { 1073 wpipe->pipe_state &= ~PIPE_WANTR; 1074 wakeup(wpipe); 1075 } 1076 pipeselwakeup(wpipe); 1077 wpipe->pipe_state |= PIPE_WANTW; 1078 pipeunlock(wpipe); 1079 error = msleep(wpipe, PIPE_MTX(wpipe), 1080 PRIBIO | PCATCH, "pipdww", 0); 1081 pipelock(wpipe, false); 1082 if (error != 0) 1083 goto error1; 1084 goto retry; 1085 } 1086 if (wpipe->pipe_buffer.cnt > 0) { 1087 if (wpipe->pipe_state & PIPE_WANTR) { 1088 wpipe->pipe_state &= ~PIPE_WANTR; 1089 wakeup(wpipe); 1090 } 1091 pipeselwakeup(wpipe); 1092 wpipe->pipe_state |= PIPE_WANTW; 1093 pipeunlock(wpipe); 1094 error = msleep(wpipe, PIPE_MTX(wpipe), 1095 PRIBIO | PCATCH, "pipdwc", 0); 1096 pipelock(wpipe, false); 1097 if (error != 0) 1098 goto error1; 1099 goto retry; 1100 } 1101 1102 error = pipe_build_write_buffer(wpipe, uio); 1103 if (error) { 1104 goto error1; 1105 } 1106 1107 while (wpipe->pipe_pages.cnt != 0 && 1108 (wpipe->pipe_state & PIPE_EOF) == 0) { 1109 if (wpipe->pipe_state & PIPE_WANTR) { 1110 wpipe->pipe_state &= ~PIPE_WANTR; 1111 wakeup(wpipe); 1112 } 1113 pipeselwakeup(wpipe); 1114 wpipe->pipe_state |= PIPE_WANTW; 1115 pipeunlock(wpipe); 1116 error = msleep(wpipe, PIPE_MTX(wpipe), PRIBIO | PCATCH, 1117 "pipdwt", 0); 1118 pipelock(wpipe, false); 1119 if (error != 0) 1120 break; 1121 } 1122 1123 if ((wpipe->pipe_state & PIPE_EOF) != 0) { 1124 wpipe->pipe_pages.cnt = 0; 1125 pipe_destroy_write_buffer(wpipe); 1126 pipeselwakeup(wpipe); 1127 error = EPIPE; 1128 } else if (error == EINTR || error == ERESTART) { 1129 pipe_clone_write_buffer(wpipe); 1130 } else { 1131 pipe_destroy_write_buffer(wpipe); 1132 } 1133 KASSERT((wpipe->pipe_state & PIPE_DIRECTW) == 0, 1134 ("pipe %p leaked PIPE_DIRECTW", wpipe)); 1135 return (error); 1136 1137 error1: 1138 wakeup(wpipe); 1139 return (error); 1140 } 1141 #endif 1142 1143 static int 1144 pipe_write(struct file *fp, struct uio *uio, struct ucred *active_cred, 1145 int flags, struct thread *td) 1146 { 1147 struct pipe *wpipe, *rpipe; 1148 ssize_t orig_resid; 1149 int desiredsize, error; 1150 1151 rpipe = fp->f_data; 1152 wpipe = PIPE_PEER(rpipe); 1153 PIPE_LOCK(rpipe); 1154 error = pipelock(wpipe, true); 1155 if (error) { 1156 PIPE_UNLOCK(rpipe); 1157 return (error); 1158 } 1159 /* 1160 * detect loss of pipe read side, issue SIGPIPE if lost. 1161 */ 1162 if (wpipe->pipe_present != PIPE_ACTIVE || 1163 (wpipe->pipe_state & PIPE_EOF)) { 1164 pipeunlock(wpipe); 1165 PIPE_UNLOCK(rpipe); 1166 return (EPIPE); 1167 } 1168 #ifdef MAC 1169 error = mac_pipe_check_write(active_cred, wpipe->pipe_pair); 1170 if (error) { 1171 pipeunlock(wpipe); 1172 PIPE_UNLOCK(rpipe); 1173 return (error); 1174 } 1175 #endif 1176 ++wpipe->pipe_busy; 1177 1178 /* Choose a larger size if it's advantageous */ 1179 desiredsize = max(SMALL_PIPE_SIZE, wpipe->pipe_buffer.size); 1180 while (desiredsize < wpipe->pipe_buffer.cnt + uio->uio_resid) { 1181 if (piperesizeallowed != 1) 1182 break; 1183 if (amountpipekva > maxpipekva / 2) 1184 break; 1185 if (desiredsize == BIG_PIPE_SIZE) 1186 break; 1187 desiredsize = desiredsize * 2; 1188 } 1189 1190 /* Choose a smaller size if we're in a OOM situation */ 1191 if (amountpipekva > (3 * maxpipekva) / 4 && 1192 wpipe->pipe_buffer.size > SMALL_PIPE_SIZE && 1193 wpipe->pipe_buffer.cnt <= SMALL_PIPE_SIZE && 1194 piperesizeallowed == 1) 1195 desiredsize = SMALL_PIPE_SIZE; 1196 1197 /* Resize if the above determined that a new size was necessary */ 1198 if (desiredsize != wpipe->pipe_buffer.size && 1199 (wpipe->pipe_state & PIPE_DIRECTW) == 0) { 1200 PIPE_UNLOCK(wpipe); 1201 pipespace(wpipe, desiredsize); 1202 PIPE_LOCK(wpipe); 1203 } 1204 MPASS(wpipe->pipe_buffer.size != 0); 1205 1206 orig_resid = uio->uio_resid; 1207 1208 while (uio->uio_resid) { 1209 int space; 1210 1211 if (wpipe->pipe_state & PIPE_EOF) { 1212 error = EPIPE; 1213 break; 1214 } 1215 #ifndef PIPE_NODIRECT 1216 /* 1217 * If the transfer is large, we can gain performance if 1218 * we do process-to-process copies directly. 1219 * If the write is non-blocking, we don't use the 1220 * direct write mechanism. 1221 * 1222 * The direct write mechanism will detect the reader going 1223 * away on us. 1224 */ 1225 if (uio->uio_segflg == UIO_USERSPACE && 1226 uio->uio_iov->iov_len >= pipe_mindirect && 1227 wpipe->pipe_buffer.size >= pipe_mindirect && 1228 (fp->f_flag & FNONBLOCK) == 0) { 1229 error = pipe_direct_write(wpipe, uio); 1230 if (error != 0) 1231 break; 1232 continue; 1233 } 1234 #endif 1235 1236 /* 1237 * Pipe buffered writes cannot be coincidental with 1238 * direct writes. We wait until the currently executing 1239 * direct write is completed before we start filling the 1240 * pipe buffer. We break out if a signal occurs or the 1241 * reader goes away. 1242 */ 1243 if (wpipe->pipe_pages.cnt != 0) { 1244 if (wpipe->pipe_state & PIPE_WANTR) { 1245 wpipe->pipe_state &= ~PIPE_WANTR; 1246 wakeup(wpipe); 1247 } 1248 pipeselwakeup(wpipe); 1249 wpipe->pipe_state |= PIPE_WANTW; 1250 pipeunlock(wpipe); 1251 error = msleep(wpipe, PIPE_MTX(rpipe), PRIBIO | PCATCH, 1252 "pipbww", 0); 1253 pipelock(wpipe, false); 1254 if (error != 0) 1255 break; 1256 continue; 1257 } 1258 1259 space = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt; 1260 1261 /* Writes of size <= PIPE_BUF must be atomic. */ 1262 if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF)) 1263 space = 0; 1264 1265 if (space > 0) { 1266 int size; /* Transfer size */ 1267 int segsize; /* first segment to transfer */ 1268 1269 /* 1270 * Transfer size is minimum of uio transfer 1271 * and free space in pipe buffer. 1272 */ 1273 if (space > uio->uio_resid) 1274 size = uio->uio_resid; 1275 else 1276 size = space; 1277 /* 1278 * First segment to transfer is minimum of 1279 * transfer size and contiguous space in 1280 * pipe buffer. If first segment to transfer 1281 * is less than the transfer size, we've got 1282 * a wraparound in the buffer. 1283 */ 1284 segsize = wpipe->pipe_buffer.size - 1285 wpipe->pipe_buffer.in; 1286 if (segsize > size) 1287 segsize = size; 1288 1289 /* Transfer first segment */ 1290 1291 PIPE_UNLOCK(rpipe); 1292 error = uiomove(&wpipe->pipe_buffer.buffer[wpipe->pipe_buffer.in], 1293 segsize, uio); 1294 PIPE_LOCK(rpipe); 1295 1296 if (error == 0 && segsize < size) { 1297 KASSERT(wpipe->pipe_buffer.in + segsize == 1298 wpipe->pipe_buffer.size, 1299 ("Pipe buffer wraparound disappeared")); 1300 /* 1301 * Transfer remaining part now, to 1302 * support atomic writes. Wraparound 1303 * happened. 1304 */ 1305 1306 PIPE_UNLOCK(rpipe); 1307 error = uiomove( 1308 &wpipe->pipe_buffer.buffer[0], 1309 size - segsize, uio); 1310 PIPE_LOCK(rpipe); 1311 } 1312 if (error == 0) { 1313 wpipe->pipe_buffer.in += size; 1314 if (wpipe->pipe_buffer.in >= 1315 wpipe->pipe_buffer.size) { 1316 KASSERT(wpipe->pipe_buffer.in == 1317 size - segsize + 1318 wpipe->pipe_buffer.size, 1319 ("Expected wraparound bad")); 1320 wpipe->pipe_buffer.in = size - segsize; 1321 } 1322 1323 wpipe->pipe_buffer.cnt += size; 1324 KASSERT(wpipe->pipe_buffer.cnt <= 1325 wpipe->pipe_buffer.size, 1326 ("Pipe buffer overflow")); 1327 } 1328 if (error != 0) 1329 break; 1330 continue; 1331 } else { 1332 /* 1333 * If the "read-side" has been blocked, wake it up now. 1334 */ 1335 if (wpipe->pipe_state & PIPE_WANTR) { 1336 wpipe->pipe_state &= ~PIPE_WANTR; 1337 wakeup(wpipe); 1338 } 1339 1340 /* 1341 * don't block on non-blocking I/O 1342 */ 1343 if (fp->f_flag & FNONBLOCK) { 1344 error = EAGAIN; 1345 break; 1346 } 1347 1348 /* 1349 * We have no more space and have something to offer, 1350 * wake up select/poll. 1351 */ 1352 pipeselwakeup(wpipe); 1353 1354 wpipe->pipe_state |= PIPE_WANTW; 1355 pipeunlock(wpipe); 1356 error = msleep(wpipe, PIPE_MTX(rpipe), 1357 PRIBIO | PCATCH, "pipewr", 0); 1358 pipelock(wpipe, false); 1359 if (error != 0) 1360 break; 1361 continue; 1362 } 1363 } 1364 1365 --wpipe->pipe_busy; 1366 1367 if ((wpipe->pipe_busy == 0) && (wpipe->pipe_state & PIPE_WANT)) { 1368 wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR); 1369 wakeup(wpipe); 1370 } else if (wpipe->pipe_buffer.cnt > 0) { 1371 /* 1372 * If we have put any characters in the buffer, we wake up 1373 * the reader. 1374 */ 1375 if (wpipe->pipe_state & PIPE_WANTR) { 1376 wpipe->pipe_state &= ~PIPE_WANTR; 1377 wakeup(wpipe); 1378 } 1379 } 1380 1381 /* 1382 * Don't return EPIPE if any byte was written. 1383 * EINTR and other interrupts are handled by generic I/O layer. 1384 * Do not pretend that I/O succeeded for obvious user error 1385 * like EFAULT. 1386 */ 1387 if (uio->uio_resid != orig_resid && error == EPIPE) 1388 error = 0; 1389 1390 if (error == 0) 1391 pipe_timestamp(&wpipe->pipe_mtime); 1392 1393 /* 1394 * We have something to offer, 1395 * wake up select/poll. 1396 */ 1397 if (wpipe->pipe_buffer.cnt) 1398 pipeselwakeup(wpipe); 1399 1400 pipeunlock(wpipe); 1401 PIPE_UNLOCK(rpipe); 1402 if (uio->uio_resid != orig_resid) 1403 td->td_ru.ru_msgsnd++; 1404 return (error); 1405 } 1406 1407 /* ARGSUSED */ 1408 static int 1409 pipe_truncate(struct file *fp, off_t length, struct ucred *active_cred, 1410 struct thread *td) 1411 { 1412 struct pipe *cpipe; 1413 int error; 1414 1415 cpipe = fp->f_data; 1416 if (cpipe->pipe_type & PIPE_TYPE_NAMED) 1417 error = vnops.fo_truncate(fp, length, active_cred, td); 1418 else 1419 error = invfo_truncate(fp, length, active_cred, td); 1420 return (error); 1421 } 1422 1423 /* 1424 * we implement a very minimal set of ioctls for compatibility with sockets. 1425 */ 1426 static int 1427 pipe_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *active_cred, 1428 struct thread *td) 1429 { 1430 struct pipe *mpipe = fp->f_data; 1431 int error; 1432 1433 PIPE_LOCK(mpipe); 1434 1435 #ifdef MAC 1436 error = mac_pipe_check_ioctl(active_cred, mpipe->pipe_pair, cmd, data); 1437 if (error) { 1438 PIPE_UNLOCK(mpipe); 1439 return (error); 1440 } 1441 #endif 1442 1443 error = 0; 1444 switch (cmd) { 1445 case FIONBIO: 1446 break; 1447 1448 case FIOASYNC: 1449 if (*(int *)data) { 1450 mpipe->pipe_state |= PIPE_ASYNC; 1451 } else { 1452 mpipe->pipe_state &= ~PIPE_ASYNC; 1453 } 1454 break; 1455 1456 case FIONREAD: 1457 if (!(fp->f_flag & FREAD)) { 1458 *(int *)data = 0; 1459 PIPE_UNLOCK(mpipe); 1460 return (0); 1461 } 1462 if (mpipe->pipe_pages.cnt != 0) 1463 *(int *)data = mpipe->pipe_pages.cnt; 1464 else 1465 *(int *)data = mpipe->pipe_buffer.cnt; 1466 break; 1467 1468 case FIOSETOWN: 1469 PIPE_UNLOCK(mpipe); 1470 error = fsetown(*(int *)data, &mpipe->pipe_sigio); 1471 goto out_unlocked; 1472 1473 case FIOGETOWN: 1474 *(int *)data = fgetown(&mpipe->pipe_sigio); 1475 break; 1476 1477 /* This is deprecated, FIOSETOWN should be used instead. */ 1478 case TIOCSPGRP: 1479 PIPE_UNLOCK(mpipe); 1480 error = fsetown(-(*(int *)data), &mpipe->pipe_sigio); 1481 goto out_unlocked; 1482 1483 /* This is deprecated, FIOGETOWN should be used instead. */ 1484 case TIOCGPGRP: 1485 *(int *)data = -fgetown(&mpipe->pipe_sigio); 1486 break; 1487 1488 default: 1489 error = ENOTTY; 1490 break; 1491 } 1492 PIPE_UNLOCK(mpipe); 1493 out_unlocked: 1494 return (error); 1495 } 1496 1497 static int 1498 pipe_poll(struct file *fp, int events, struct ucred *active_cred, 1499 struct thread *td) 1500 { 1501 struct pipe *rpipe; 1502 struct pipe *wpipe; 1503 int levents, revents; 1504 #ifdef MAC 1505 int error; 1506 #endif 1507 1508 revents = 0; 1509 rpipe = fp->f_data; 1510 wpipe = PIPE_PEER(rpipe); 1511 PIPE_LOCK(rpipe); 1512 #ifdef MAC 1513 error = mac_pipe_check_poll(active_cred, rpipe->pipe_pair); 1514 if (error) 1515 goto locked_error; 1516 #endif 1517 if (fp->f_flag & FREAD && events & (POLLIN | POLLRDNORM)) 1518 if (rpipe->pipe_pages.cnt > 0 || rpipe->pipe_buffer.cnt > 0) 1519 revents |= events & (POLLIN | POLLRDNORM); 1520 1521 if (fp->f_flag & FWRITE && events & (POLLOUT | POLLWRNORM)) 1522 if (wpipe->pipe_present != PIPE_ACTIVE || 1523 (wpipe->pipe_state & PIPE_EOF) || 1524 ((wpipe->pipe_state & PIPE_DIRECTW) == 0 && 1525 ((wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF || 1526 wpipe->pipe_buffer.size == 0))) 1527 revents |= events & (POLLOUT | POLLWRNORM); 1528 1529 levents = events & 1530 (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM | POLLRDBAND); 1531 if (rpipe->pipe_type & PIPE_TYPE_NAMED && fp->f_flag & FREAD && levents && 1532 fp->f_pipegen == rpipe->pipe_wgen) 1533 events |= POLLINIGNEOF; 1534 1535 if ((events & POLLINIGNEOF) == 0) { 1536 if (rpipe->pipe_state & PIPE_EOF) { 1537 if (fp->f_flag & FREAD) 1538 revents |= (events & (POLLIN | POLLRDNORM)); 1539 if (wpipe->pipe_present != PIPE_ACTIVE || 1540 (wpipe->pipe_state & PIPE_EOF)) 1541 revents |= POLLHUP; 1542 } 1543 } 1544 1545 if (revents == 0) { 1546 /* 1547 * Add ourselves regardless of eventmask as we have to return 1548 * POLLHUP even if it was not asked for. 1549 */ 1550 if ((fp->f_flag & FREAD) != 0) { 1551 selrecord(td, &rpipe->pipe_sel); 1552 if (SEL_WAITING(&rpipe->pipe_sel)) 1553 rpipe->pipe_state |= PIPE_SEL; 1554 } 1555 1556 if ((fp->f_flag & FWRITE) != 0 && 1557 wpipe->pipe_present == PIPE_ACTIVE) { 1558 selrecord(td, &wpipe->pipe_sel); 1559 if (SEL_WAITING(&wpipe->pipe_sel)) 1560 wpipe->pipe_state |= PIPE_SEL; 1561 } 1562 } 1563 #ifdef MAC 1564 locked_error: 1565 #endif 1566 PIPE_UNLOCK(rpipe); 1567 1568 return (revents); 1569 } 1570 1571 /* 1572 * We shouldn't need locks here as we're doing a read and this should 1573 * be a natural race. 1574 */ 1575 static int 1576 pipe_stat(struct file *fp, struct stat *ub, struct ucred *active_cred) 1577 { 1578 struct pipe *pipe; 1579 #ifdef MAC 1580 int error; 1581 #endif 1582 1583 pipe = fp->f_data; 1584 #ifdef MAC 1585 if (mac_pipe_check_stat_enabled()) { 1586 PIPE_LOCK(pipe); 1587 error = mac_pipe_check_stat(active_cred, pipe->pipe_pair); 1588 PIPE_UNLOCK(pipe); 1589 if (error) { 1590 return (error); 1591 } 1592 } 1593 #endif 1594 1595 /* For named pipes ask the underlying filesystem. */ 1596 if (pipe->pipe_type & PIPE_TYPE_NAMED) { 1597 return (vnops.fo_stat(fp, ub, active_cred)); 1598 } 1599 1600 bzero(ub, sizeof(*ub)); 1601 ub->st_mode = S_IFIFO; 1602 ub->st_blksize = PAGE_SIZE; 1603 if (pipe->pipe_pages.cnt != 0) 1604 ub->st_size = pipe->pipe_pages.cnt; 1605 else 1606 ub->st_size = pipe->pipe_buffer.cnt; 1607 ub->st_blocks = howmany(ub->st_size, ub->st_blksize); 1608 ub->st_atim = pipe->pipe_atime; 1609 ub->st_mtim = pipe->pipe_mtime; 1610 ub->st_ctim = pipe->pipe_ctime; 1611 ub->st_uid = fp->f_cred->cr_uid; 1612 ub->st_gid = fp->f_cred->cr_gid; 1613 ub->st_dev = pipedev_ino; 1614 ub->st_ino = pipe->pipe_ino; 1615 /* 1616 * Left as 0: st_nlink, st_rdev, st_flags, st_gen. 1617 */ 1618 return (0); 1619 } 1620 1621 /* ARGSUSED */ 1622 static int 1623 pipe_close(struct file *fp, struct thread *td) 1624 { 1625 1626 if (fp->f_vnode != NULL) 1627 return vnops.fo_close(fp, td); 1628 fp->f_ops = &badfileops; 1629 pipe_dtor(fp->f_data); 1630 fp->f_data = NULL; 1631 return (0); 1632 } 1633 1634 static int 1635 pipe_chmod(struct file *fp, mode_t mode, struct ucred *active_cred, struct thread *td) 1636 { 1637 struct pipe *cpipe; 1638 int error; 1639 1640 cpipe = fp->f_data; 1641 if (cpipe->pipe_type & PIPE_TYPE_NAMED) 1642 error = vn_chmod(fp, mode, active_cred, td); 1643 else 1644 error = invfo_chmod(fp, mode, active_cred, td); 1645 return (error); 1646 } 1647 1648 static int 1649 pipe_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred, 1650 struct thread *td) 1651 { 1652 struct pipe *cpipe; 1653 int error; 1654 1655 cpipe = fp->f_data; 1656 if (cpipe->pipe_type & PIPE_TYPE_NAMED) 1657 error = vn_chown(fp, uid, gid, active_cred, td); 1658 else 1659 error = invfo_chown(fp, uid, gid, active_cred, td); 1660 return (error); 1661 } 1662 1663 static int 1664 pipe_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp) 1665 { 1666 struct pipe *pi; 1667 1668 if (fp->f_type == DTYPE_FIFO) 1669 return (vn_fill_kinfo(fp, kif, fdp)); 1670 kif->kf_type = KF_TYPE_PIPE; 1671 pi = fp->f_data; 1672 kif->kf_un.kf_pipe.kf_pipe_addr = (uintptr_t)pi; 1673 kif->kf_un.kf_pipe.kf_pipe_peer = (uintptr_t)pi->pipe_peer; 1674 kif->kf_un.kf_pipe.kf_pipe_buffer_cnt = pi->pipe_buffer.cnt; 1675 kif->kf_un.kf_pipe.kf_pipe_buffer_in = pi->pipe_buffer.in; 1676 kif->kf_un.kf_pipe.kf_pipe_buffer_out = pi->pipe_buffer.out; 1677 kif->kf_un.kf_pipe.kf_pipe_buffer_size = pi->pipe_buffer.size; 1678 return (0); 1679 } 1680 1681 static void 1682 pipe_free_kmem(struct pipe *cpipe) 1683 { 1684 1685 PIPE_LOCK_ASSERT(cpipe, MA_NOTOWNED); 1686 1687 if (cpipe->pipe_buffer.buffer != NULL) { 1688 atomic_subtract_long(&amountpipekva, cpipe->pipe_buffer.size); 1689 chgpipecnt(cpipe->pipe_pair->pp_owner->cr_ruidinfo, 1690 -cpipe->pipe_buffer.size, 0); 1691 vm_map_remove(pipe_map, 1692 (vm_offset_t)cpipe->pipe_buffer.buffer, 1693 (vm_offset_t)cpipe->pipe_buffer.buffer + cpipe->pipe_buffer.size); 1694 cpipe->pipe_buffer.buffer = NULL; 1695 } 1696 #ifndef PIPE_NODIRECT 1697 { 1698 cpipe->pipe_pages.cnt = 0; 1699 cpipe->pipe_pages.pos = 0; 1700 cpipe->pipe_pages.npages = 0; 1701 } 1702 #endif 1703 } 1704 1705 /* 1706 * shutdown the pipe 1707 */ 1708 static void 1709 pipeclose(struct pipe *cpipe) 1710 { 1711 #ifdef MAC 1712 struct pipepair *pp; 1713 #endif 1714 struct pipe *ppipe; 1715 1716 KASSERT(cpipe != NULL, ("pipeclose: cpipe == NULL")); 1717 1718 PIPE_LOCK(cpipe); 1719 pipelock(cpipe, false); 1720 #ifdef MAC 1721 pp = cpipe->pipe_pair; 1722 #endif 1723 1724 /* 1725 * If the other side is blocked, wake it up saying that 1726 * we want to close it down. 1727 */ 1728 cpipe->pipe_state |= PIPE_EOF; 1729 while (cpipe->pipe_busy) { 1730 wakeup(cpipe); 1731 cpipe->pipe_state |= PIPE_WANT; 1732 pipeunlock(cpipe); 1733 msleep(cpipe, PIPE_MTX(cpipe), PRIBIO, "pipecl", 0); 1734 pipelock(cpipe, false); 1735 } 1736 1737 pipeselwakeup(cpipe); 1738 1739 /* 1740 * Disconnect from peer, if any. 1741 */ 1742 ppipe = cpipe->pipe_peer; 1743 if (ppipe->pipe_present == PIPE_ACTIVE) { 1744 ppipe->pipe_state |= PIPE_EOF; 1745 wakeup(ppipe); 1746 pipeselwakeup(ppipe); 1747 } 1748 1749 /* 1750 * Mark this endpoint as free. Release kmem resources. We 1751 * don't mark this endpoint as unused until we've finished 1752 * doing that, or the pipe might disappear out from under 1753 * us. 1754 */ 1755 PIPE_UNLOCK(cpipe); 1756 pipe_free_kmem(cpipe); 1757 PIPE_LOCK(cpipe); 1758 cpipe->pipe_present = PIPE_CLOSING; 1759 pipeunlock(cpipe); 1760 1761 /* 1762 * knlist_clear() may sleep dropping the PIPE_MTX. Set the 1763 * PIPE_FINALIZED, that allows other end to free the 1764 * pipe_pair, only after the knotes are completely dismantled. 1765 */ 1766 knlist_clear(&cpipe->pipe_sel.si_note, 1); 1767 cpipe->pipe_present = PIPE_FINALIZED; 1768 seldrain(&cpipe->pipe_sel); 1769 knlist_destroy(&cpipe->pipe_sel.si_note); 1770 1771 /* 1772 * If both endpoints are now closed, release the memory for the 1773 * pipe pair. If not, unlock. 1774 */ 1775 if (ppipe->pipe_present == PIPE_FINALIZED) { 1776 PIPE_UNLOCK(cpipe); 1777 crfree(cpipe->pipe_pair->pp_owner); 1778 #ifdef MAC 1779 mac_pipe_destroy(pp); 1780 #endif 1781 uma_zfree(pipe_zone, cpipe->pipe_pair); 1782 } else 1783 PIPE_UNLOCK(cpipe); 1784 } 1785 1786 /*ARGSUSED*/ 1787 static int 1788 pipe_kqfilter(struct file *fp, struct knote *kn) 1789 { 1790 struct pipe *cpipe; 1791 1792 /* 1793 * If a filter is requested that is not supported by this file 1794 * descriptor, don't return an error, but also don't ever generate an 1795 * event. 1796 */ 1797 if ((kn->kn_filter == EVFILT_READ) && !(fp->f_flag & FREAD)) { 1798 kn->kn_fop = &pipe_nfiltops; 1799 return (0); 1800 } 1801 if ((kn->kn_filter == EVFILT_WRITE) && !(fp->f_flag & FWRITE)) { 1802 kn->kn_fop = &pipe_nfiltops; 1803 return (0); 1804 } 1805 cpipe = fp->f_data; 1806 PIPE_LOCK(cpipe); 1807 switch (kn->kn_filter) { 1808 case EVFILT_READ: 1809 kn->kn_fop = &pipe_rfiltops; 1810 break; 1811 case EVFILT_WRITE: 1812 kn->kn_fop = &pipe_wfiltops; 1813 if (cpipe->pipe_peer->pipe_present != PIPE_ACTIVE) { 1814 /* other end of pipe has been closed */ 1815 PIPE_UNLOCK(cpipe); 1816 return (EPIPE); 1817 } 1818 cpipe = PIPE_PEER(cpipe); 1819 break; 1820 default: 1821 if ((cpipe->pipe_type & PIPE_TYPE_NAMED) != 0) { 1822 PIPE_UNLOCK(cpipe); 1823 return (vnops.fo_kqfilter(fp, kn)); 1824 } 1825 PIPE_UNLOCK(cpipe); 1826 return (EINVAL); 1827 } 1828 1829 kn->kn_hook = cpipe; 1830 knlist_add(&cpipe->pipe_sel.si_note, kn, 1); 1831 PIPE_UNLOCK(cpipe); 1832 return (0); 1833 } 1834 1835 static void 1836 filt_pipedetach(struct knote *kn) 1837 { 1838 struct pipe *cpipe = kn->kn_hook; 1839 1840 PIPE_LOCK(cpipe); 1841 knlist_remove(&cpipe->pipe_sel.si_note, kn, 1); 1842 PIPE_UNLOCK(cpipe); 1843 } 1844 1845 /*ARGSUSED*/ 1846 static int 1847 filt_piperead(struct knote *kn, long hint) 1848 { 1849 struct file *fp = kn->kn_fp; 1850 struct pipe *rpipe = kn->kn_hook; 1851 1852 PIPE_LOCK_ASSERT(rpipe, MA_OWNED); 1853 kn->kn_data = rpipe->pipe_buffer.cnt; 1854 if (kn->kn_data == 0) 1855 kn->kn_data = rpipe->pipe_pages.cnt; 1856 1857 if ((rpipe->pipe_state & PIPE_EOF) != 0 && 1858 ((rpipe->pipe_type & PIPE_TYPE_NAMED) == 0 || 1859 fp->f_pipegen != rpipe->pipe_wgen)) { 1860 kn->kn_flags |= EV_EOF; 1861 return (1); 1862 } 1863 kn->kn_flags &= ~EV_EOF; 1864 return (kn->kn_data > 0); 1865 } 1866 1867 /*ARGSUSED*/ 1868 static int 1869 filt_pipewrite(struct knote *kn, long hint) 1870 { 1871 struct pipe *wpipe = kn->kn_hook; 1872 1873 /* 1874 * If this end of the pipe is closed, the knote was removed from the 1875 * knlist and the list lock (i.e., the pipe lock) is therefore not held. 1876 */ 1877 if (wpipe->pipe_present == PIPE_ACTIVE || 1878 (wpipe->pipe_type & PIPE_TYPE_NAMED) != 0) { 1879 PIPE_LOCK_ASSERT(wpipe, MA_OWNED); 1880 1881 if (wpipe->pipe_state & PIPE_DIRECTW) { 1882 kn->kn_data = 0; 1883 } else if (wpipe->pipe_buffer.size > 0) { 1884 kn->kn_data = wpipe->pipe_buffer.size - 1885 wpipe->pipe_buffer.cnt; 1886 } else { 1887 kn->kn_data = PIPE_BUF; 1888 } 1889 } 1890 1891 if (wpipe->pipe_present != PIPE_ACTIVE || 1892 (wpipe->pipe_state & PIPE_EOF)) { 1893 kn->kn_flags |= EV_EOF; 1894 return (1); 1895 } 1896 kn->kn_flags &= ~EV_EOF; 1897 return (kn->kn_data >= PIPE_BUF); 1898 } 1899 1900 static void 1901 filt_pipedetach_notsup(struct knote *kn) 1902 { 1903 1904 } 1905 1906 static int 1907 filt_pipenotsup(struct knote *kn, long hint) 1908 { 1909 1910 return (0); 1911 } 1912 1913 static int 1914 filt_pipedump(struct proc *p, struct knote *kn, 1915 struct kinfo_knote *kin) 1916 { 1917 struct pipe *pipe = kn->kn_hook; 1918 1919 kin->knt_extdata = KNOTE_EXTDATA_PIPE; 1920 kin->knt_pipe.knt_pipe_ino = pipe->pipe_ino; 1921 return (0); 1922 } 1923