2 * Copyright (c) 1996 John S. Dyson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice immediately at the beginning of the file, without modification,
10 * this list of conditions, and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Absolutely no warranty of function or purpose is made by the author
16 * 4. Modifications may be freely made to this file if the above conditions
19 * $FreeBSD: src/sys/kern/sys_pipe.c,v 1.60.2.13 2002/08/05 15:05:15 des Exp $
23 * This file contains a high-performance replacement for the socket-based
24 * pipes scheme originally used in FreeBSD/4.4Lite. It does not support
25 * all features of sockets, but does do everything that pipes normally
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
32 #include <sys/fcntl.h>
34 #include <sys/filedesc.h>
35 #include <sys/filio.h>
36 #include <sys/ttycom.h>
38 #include <sys/signalvar.h>
39 #include <sys/sysproto.h>
41 #include <sys/vnode.h>
43 #include <sys/event.h>
44 #include <sys/globaldata.h>
45 #include <sys/module.h>
46 #include <sys/malloc.h>
47 #include <sys/sysctl.h>
48 #include <sys/socket.h>
49 #include <sys/kern_syscall.h>
52 #include <vm/vm_param.h>
54 #include <vm/vm_object.h>
55 #include <vm/vm_kern.h>
56 #include <vm/vm_extern.h>
58 #include <vm/vm_map.h>
59 #include <vm/vm_page.h>
60 #include <vm/vm_zone.h>
62 #include <sys/file2.h>
63 #include <sys/signal2.h>
65 #include <machine/cpufunc.h>
68 * interfaces to the outside world
70 static int pipe_read (struct file
*fp
, struct uio
*uio
,
71 struct ucred
*cred
, int flags
);
72 static int pipe_write (struct file
*fp
, struct uio
*uio
,
73 struct ucred
*cred
, int flags
);
74 static int pipe_close (struct file
*fp
);
75 static int pipe_shutdown (struct file
*fp
, int how
);
76 static int pipe_kqfilter (struct file
*fp
, struct knote
*kn
);
77 static int pipe_stat (struct file
*fp
, struct stat
*sb
, struct ucred
*cred
);
78 static int pipe_ioctl (struct file
*fp
, u_long cmd
, caddr_t data
,
79 struct ucred
*cred
, struct sysmsg
*msg
);
81 static struct fileops pipeops
= {
83 .fo_write
= pipe_write
,
84 .fo_ioctl
= pipe_ioctl
,
85 .fo_kqfilter
= pipe_kqfilter
,
87 .fo_close
= pipe_close
,
88 .fo_shutdown
= pipe_shutdown
91 static void filt_pipedetach(struct knote
*kn
);
92 static int filt_piperead(struct knote
*kn
, long hint
);
93 static int filt_pipewrite(struct knote
*kn
, long hint
);
95 static struct filterops pipe_rfiltops
=
96 { FILTEROP_ISFD
|FILTEROP_MPSAFE
, NULL
, filt_pipedetach
, filt_piperead
};
97 static struct filterops pipe_wfiltops
=
98 { FILTEROP_ISFD
|FILTEROP_MPSAFE
, NULL
, filt_pipedetach
, filt_pipewrite
};
100 MALLOC_DEFINE(M_PIPE
, "pipe", "pipe structures");
103 * Default pipe buffer size(s), this can be kind-of large now because pipe
104 * space is pageable. The pipe code will try to maintain locality of
105 * reference for performance reasons, so small amounts of outstanding I/O
106 * will not wipe the cache.
108 #define MINPIPESIZE (PIPE_SIZE/3)
109 #define MAXPIPESIZE (2*PIPE_SIZE/3)
112 * Limit the number of "big" pipes
114 #define LIMITBIGPIPES 64
115 #define PIPEQ_MAX_CACHE 16 /* per-cpu pipe structure cache */
117 static int pipe_maxbig
= LIMITBIGPIPES
;
118 static int pipe_maxcache
= PIPEQ_MAX_CACHE
;
119 static int pipe_bigcount
;
120 static int pipe_nbig
;
121 static int pipe_bcache_alloc
;
122 static int pipe_bkmem_alloc
;
123 static int pipe_rblocked_count
;
124 static int pipe_wblocked_count
;
126 SYSCTL_NODE(_kern
, OID_AUTO
, pipe
, CTLFLAG_RW
, 0, "Pipe operation");
127 SYSCTL_INT(_kern_pipe
, OID_AUTO
, nbig
,
128 CTLFLAG_RD
, &pipe_nbig
, 0, "number of big pipes allocated");
129 SYSCTL_INT(_kern_pipe
, OID_AUTO
, bigcount
,
130 CTLFLAG_RW
, &pipe_bigcount
, 0, "number of times pipe expanded");
131 SYSCTL_INT(_kern_pipe
, OID_AUTO
, rblocked
,
132 CTLFLAG_RW
, &pipe_rblocked_count
, 0, "number of times pipe expanded");
133 SYSCTL_INT(_kern_pipe
, OID_AUTO
, wblocked
,
134 CTLFLAG_RW
, &pipe_wblocked_count
, 0, "number of times pipe expanded");
135 SYSCTL_INT(_kern_pipe
, OID_AUTO
, maxcache
,
136 CTLFLAG_RW
, &pipe_maxcache
, 0, "max pipes cached per-cpu");
137 SYSCTL_INT(_kern_pipe
, OID_AUTO
, maxbig
,
138 CTLFLAG_RW
, &pipe_maxbig
, 0, "max number of big pipes");
139 static int pipe_delay
= 5000; /* 5uS default */
140 SYSCTL_INT(_kern_pipe
, OID_AUTO
, delay
,
141 CTLFLAG_RW
, &pipe_delay
, 0, "SMP delay optimization in ns");
142 #if !defined(NO_PIPE_SYSCTL_STATS)
143 SYSCTL_INT(_kern_pipe
, OID_AUTO
, bcache_alloc
,
144 CTLFLAG_RW
, &pipe_bcache_alloc
, 0, "pipe buffer from pcpu cache");
145 SYSCTL_INT(_kern_pipe
, OID_AUTO
, bkmem_alloc
,
146 CTLFLAG_RW
, &pipe_bkmem_alloc
, 0, "pipe buffer from kmem");
150 * Auto-size pipe cache to reduce kmem allocations and frees.
154 pipeinit(void *dummy
)
156 size_t mbytes
= kmem_lim_size();
158 if (pipe_maxbig
== LIMITBIGPIPES
) {
159 if (mbytes
>= 7 * 1024)
161 if (mbytes
>= 15 * 1024)
164 if (pipe_maxcache
== PIPEQ_MAX_CACHE
) {
165 if (mbytes
>= 7 * 1024)
167 if (mbytes
>= 15 * 1024)
171 SYSINIT(kmem
, SI_BOOT2_MACHDEP
, SI_ORDER_ANY
, pipeinit
, NULL
);
173 static void pipeclose (struct pipe
*cpipe
);
174 static void pipe_free_kmem (struct pipe
*cpipe
);
175 static int pipe_create (struct pipe
**cpipep
);
176 static int pipespace (struct pipe
*cpipe
, int size
);
179 pipewakeup(struct pipe
*cpipe
, int dosigio
)
181 if (dosigio
&& (cpipe
->pipe_state
& PIPE_ASYNC
) && cpipe
->pipe_sigio
) {
182 lwkt_gettoken(&sigio_token
);
183 pgsigio(cpipe
->pipe_sigio
, SIGIO
, 0);
184 lwkt_reltoken(&sigio_token
);
186 KNOTE(&cpipe
->pipe_kq
.ki_note
, 0);
190 * These routines are called before and after a UIO. The UIO
191 * may block, causing our held tokens to be lost temporarily.
193 * We use these routines to serialize reads against other reads
194 * and writes against other writes.
196 * The read token is held on entry so *ipp does not race.
199 pipe_start_uio(struct pipe
*cpipe
, int *ipp
)
205 error
= tsleep(ipp
, PCATCH
, "pipexx", 0);
214 pipe_end_uio(struct pipe
*cpipe
, int *ipp
)
226 * The pipe system call for the DTYPE_PIPE type of pipes
228 * pipe_args(int dummy)
233 sys_pipe(struct pipe_args
*uap
)
235 return kern_pipe(uap
->sysmsg_fds
, 0);
239 sys_pipe2(struct pipe2_args
*uap
)
241 return kern_pipe(uap
->sysmsg_fds
, uap
->flags
);
245 kern_pipe(long *fds
, int flags
)
247 struct thread
*td
= curthread
;
248 struct filedesc
*fdp
= td
->td_proc
->p_fd
;
249 struct file
*rf
, *wf
;
250 struct pipe
*rpipe
, *wpipe
;
253 rpipe
= wpipe
= NULL
;
254 if (pipe_create(&rpipe
) || pipe_create(&wpipe
)) {
260 error
= falloc(td
->td_lwp
, &rf
, &fd1
);
269 * Warning: once we've gotten past allocation of the fd for the
270 * read-side, we can only drop the read side via fdrop() in order
271 * to avoid races against processes which manage to dup() the read
272 * side while we are blocked trying to allocate the write side.
274 rf
->f_type
= DTYPE_PIPE
;
275 rf
->f_flag
= FREAD
| FWRITE
;
276 rf
->f_ops
= &pipeops
;
278 if (flags
& O_NONBLOCK
)
279 rf
->f_flag
|= O_NONBLOCK
;
280 if (flags
& O_CLOEXEC
)
281 fdp
->fd_files
[fd1
].fileflags
|= UF_EXCLOSE
;
283 error
= falloc(td
->td_lwp
, &wf
, &fd2
);
285 fsetfd(fdp
, NULL
, fd1
);
287 /* rpipe has been closed by fdrop(). */
291 wf
->f_type
= DTYPE_PIPE
;
292 wf
->f_flag
= FREAD
| FWRITE
;
293 wf
->f_ops
= &pipeops
;
295 if (flags
& O_NONBLOCK
)
296 wf
->f_flag
|= O_NONBLOCK
;
297 if (flags
& O_CLOEXEC
)
298 fdp
->fd_files
[fd2
].fileflags
|= UF_EXCLOSE
;
302 rpipe
->pipe_slock
= kmalloc(sizeof(struct lock
),
303 M_PIPE
, M_WAITOK
|M_ZERO
);
304 wpipe
->pipe_slock
= rpipe
->pipe_slock
;
305 rpipe
->pipe_peer
= wpipe
;
306 wpipe
->pipe_peer
= rpipe
;
307 lockinit(rpipe
->pipe_slock
, "pipecl", 0, 0);
310 * Once activated the peer relationship remains valid until
311 * both sides are closed.
313 fsetfd(fdp
, rf
, fd1
);
314 fsetfd(fdp
, wf
, fd2
);
322 * Allocate kva for pipe circular buffer, the space is pageable
323 * This routine will 'realloc' the size of a pipe safely, if it fails
324 * it will retain the old buffer.
325 * If it fails it will return ENOMEM.
328 pipespace(struct pipe
*cpipe
, int size
)
330 struct vm_object
*object
;
334 npages
= round_page(size
) / PAGE_SIZE
;
335 object
= cpipe
->pipe_buffer
.object
;
338 * [re]create the object if necessary and reserve space for it
339 * in the kernel_map. The object and memory are pageable. On
340 * success, free the old resources before assigning the new
343 if (object
== NULL
|| object
->size
!= npages
) {
344 object
= vm_object_allocate(OBJT_DEFAULT
, npages
);
345 buffer
= (caddr_t
)vm_map_min(&kernel_map
);
347 error
= vm_map_find(&kernel_map
, object
, NULL
,
348 0, (vm_offset_t
*)&buffer
, size
,
350 1, VM_MAPTYPE_NORMAL
,
351 VM_PROT_ALL
, VM_PROT_ALL
, 0);
353 if (error
!= KERN_SUCCESS
) {
354 vm_object_deallocate(object
);
357 pipe_free_kmem(cpipe
);
358 cpipe
->pipe_buffer
.object
= object
;
359 cpipe
->pipe_buffer
.buffer
= buffer
;
360 cpipe
->pipe_buffer
.size
= size
;
365 cpipe
->pipe_buffer
.rindex
= 0;
366 cpipe
->pipe_buffer
.windex
= 0;
371 * Initialize and allocate VM and memory for pipe, pulling the pipe from
372 * our per-cpu cache if possible. For now make sure it is sized for the
373 * smaller PIPE_SIZE default.
376 pipe_create(struct pipe
**cpipep
)
378 globaldata_t gd
= mycpu
;
382 if ((cpipe
= gd
->gd_pipeq
) != NULL
) {
383 gd
->gd_pipeq
= cpipe
->pipe_peer
;
385 cpipe
->pipe_peer
= NULL
;
386 cpipe
->pipe_wantwcnt
= 0;
388 cpipe
= kmalloc(sizeof(struct pipe
), M_PIPE
, M_WAITOK
|M_ZERO
);
391 if ((error
= pipespace(cpipe
, PIPE_SIZE
)) != 0)
393 vfs_timestamp(&cpipe
->pipe_ctime
);
394 cpipe
->pipe_atime
= cpipe
->pipe_ctime
;
395 cpipe
->pipe_mtime
= cpipe
->pipe_ctime
;
396 lwkt_token_init(&cpipe
->pipe_rlock
, "piper");
397 lwkt_token_init(&cpipe
->pipe_wlock
, "pipew");
402 pipe_read(struct file
*fp
, struct uio
*uio
, struct ucred
*cred
, int fflags
)
409 u_int size
; /* total bytes available */
410 u_int nsize
; /* total bytes to read */
411 u_int rindex
; /* contiguous bytes available */
416 atomic_set_int(&curthread
->td_mpflags
, TDF_MP_BATCH_DEMARC
);
418 if (uio
->uio_resid
== 0)
422 * Setup locks, calculate nbio
424 rpipe
= (struct pipe
*)fp
->f_data
;
425 wpipe
= rpipe
->pipe_peer
;
426 lwkt_gettoken(&rpipe
->pipe_rlock
);
428 if (fflags
& O_FBLOCKING
)
430 else if (fflags
& O_FNONBLOCKING
)
432 else if (fp
->f_flag
& O_NONBLOCK
)
438 * Reads are serialized. Note however that pipe_buffer.buffer and
439 * pipe_buffer.size can change out from under us when the number
440 * of bytes in the buffer are zero due to the write-side doing a
443 error
= pipe_start_uio(rpipe
, &rpipe
->pipe_rip
);
445 lwkt_reltoken(&rpipe
->pipe_rlock
);
450 bigread
= (uio
->uio_resid
> 10 * 1024 * 1024);
453 while (uio
->uio_resid
) {
457 if (bigread
&& --bigcount
== 0) {
460 if (CURSIG(curthread
->td_lwp
)) {
466 size
= rpipe
->pipe_buffer
.windex
- rpipe
->pipe_buffer
.rindex
;
469 rindex
= rpipe
->pipe_buffer
.rindex
&
470 (rpipe
->pipe_buffer
.size
- 1);
472 if (nsize
> rpipe
->pipe_buffer
.size
- rindex
)
473 nsize
= rpipe
->pipe_buffer
.size
- rindex
;
474 nsize
= szmin(nsize
, uio
->uio_resid
);
476 error
= uiomove(&rpipe
->pipe_buffer
.buffer
[rindex
],
481 rpipe
->pipe_buffer
.rindex
+= nsize
;
485 * If the FIFO is still over half full just continue
486 * and do not try to notify the writer yet.
488 if (size
- nsize
>= (rpipe
->pipe_buffer
.size
>> 1)) {
494 * When the FIFO is less then half full notify any
495 * waiting writer. WANTW can be checked while
496 * holding just the rlock.
499 if ((rpipe
->pipe_state
& PIPE_WANTW
) == 0)
504 * If the "write-side" was blocked we wake it up. This code
505 * is reached either when the buffer is completely emptied
506 * or if it becomes more then half-empty.
508 * Pipe_state can only be modified if both the rlock and
511 if (rpipe
->pipe_state
& PIPE_WANTW
) {
512 lwkt_gettoken(&rpipe
->pipe_wlock
);
513 if (rpipe
->pipe_state
& PIPE_WANTW
) {
514 rpipe
->pipe_state
&= ~PIPE_WANTW
;
515 lwkt_reltoken(&rpipe
->pipe_wlock
);
518 lwkt_reltoken(&rpipe
->pipe_wlock
);
523 * Pick up our copy loop again if the writer sent data to
524 * us while we were messing around.
526 * On a SMP box poll up to pipe_delay nanoseconds for new
527 * data. Typically a value of 2000 to 4000 is sufficient
528 * to eradicate most IPIs/tsleeps/wakeups when a pipe
529 * is used for synchronous communications with small packets,
530 * and 8000 or so (8uS) will pipeline large buffer xfers
531 * between cpus over a pipe.
533 * For synchronous communications a hit means doing a
534 * full Awrite-Bread-Bwrite-Aread cycle in less then 2uS,
535 * where as miss requiring a tsleep/wakeup sequence
536 * will take 7uS or more.
538 if (rpipe
->pipe_buffer
.windex
!= rpipe
->pipe_buffer
.rindex
)
541 #ifdef _RDTSC_SUPPORTED_
546 tsc_target
= tsc_get_target(pipe_delay
);
547 while (tsc_test_target(tsc_target
) == 0) {
548 if (rpipe
->pipe_buffer
.windex
!=
549 rpipe
->pipe_buffer
.rindex
) {
560 * Detect EOF condition, do not set error.
562 if (rpipe
->pipe_state
& PIPE_REOF
)
566 * Break if some data was read, or if this was a non-blocking
578 * Last chance, interlock with WANTR.
580 lwkt_gettoken(&rpipe
->pipe_wlock
);
581 size
= rpipe
->pipe_buffer
.windex
- rpipe
->pipe_buffer
.rindex
;
583 lwkt_reltoken(&rpipe
->pipe_wlock
);
588 * Retest EOF - acquiring a new token can temporarily release
589 * tokens already held.
591 if (rpipe
->pipe_state
& PIPE_REOF
) {
592 lwkt_reltoken(&rpipe
->pipe_wlock
);
597 * If there is no more to read in the pipe, reset its
598 * pointers to the beginning. This improves cache hit
601 * We need both locks to modify both pointers, and there
602 * must also not be a write in progress or the uiomove()
603 * in the write might block and temporarily release
604 * its wlock, then reacquire and update windex. We are
605 * only serialized against reads, not writes.
607 * XXX should we even bother resetting the indices? It
608 * might actually be more cache efficient not to.
610 if (rpipe
->pipe_buffer
.rindex
== rpipe
->pipe_buffer
.windex
&&
611 rpipe
->pipe_wip
== 0) {
612 rpipe
->pipe_buffer
.rindex
= 0;
613 rpipe
->pipe_buffer
.windex
= 0;
617 * Wait for more data.
619 * Pipe_state can only be set if both the rlock and wlock
622 rpipe
->pipe_state
|= PIPE_WANTR
;
623 tsleep_interlock(rpipe
, PCATCH
);
624 lwkt_reltoken(&rpipe
->pipe_wlock
);
625 error
= tsleep(rpipe
, PCATCH
| PINTERLOCKED
, "piperd", 0);
626 ++pipe_rblocked_count
;
630 pipe_end_uio(rpipe
, &rpipe
->pipe_rip
);
633 * Uptime last access time
635 if (error
== 0 && nread
)
636 vfs_timestamp(&rpipe
->pipe_atime
);
639 * If we drained the FIFO more then half way then handle
640 * write blocking hysteresis.
642 * Note that PIPE_WANTW cannot be set by the writer without
643 * it holding both rlock and wlock, so we can test it
644 * while holding just rlock.
648 * Synchronous blocking is done on the pipe involved
650 if (rpipe
->pipe_state
& PIPE_WANTW
) {
651 lwkt_gettoken(&rpipe
->pipe_wlock
);
652 if (rpipe
->pipe_state
& PIPE_WANTW
) {
653 rpipe
->pipe_state
&= ~PIPE_WANTW
;
654 lwkt_reltoken(&rpipe
->pipe_wlock
);
657 lwkt_reltoken(&rpipe
->pipe_wlock
);
662 * But we may also have to deal with a kqueue which is
663 * stored on the same pipe as its descriptor, so a
664 * EVFILT_WRITE event waiting for our side to drain will
665 * be on the other side.
667 lwkt_gettoken(&wpipe
->pipe_wlock
);
668 pipewakeup(wpipe
, 0);
669 lwkt_reltoken(&wpipe
->pipe_wlock
);
671 /*size = rpipe->pipe_buffer.windex - rpipe->pipe_buffer.rindex;*/
672 lwkt_reltoken(&rpipe
->pipe_rlock
);
678 pipe_write(struct file
*fp
, struct uio
*uio
, struct ucred
*cred
, int fflags
)
692 * Writes go to the peer. The peer will always exist.
694 rpipe
= (struct pipe
*) fp
->f_data
;
695 wpipe
= rpipe
->pipe_peer
;
696 lwkt_gettoken(&wpipe
->pipe_wlock
);
697 if (wpipe
->pipe_state
& PIPE_WEOF
) {
698 lwkt_reltoken(&wpipe
->pipe_wlock
);
703 * Degenerate case (EPIPE takes prec)
705 if (uio
->uio_resid
== 0) {
706 lwkt_reltoken(&wpipe
->pipe_wlock
);
711 * Writes are serialized (start_uio must be called with wlock)
713 error
= pipe_start_uio(wpipe
, &wpipe
->pipe_wip
);
715 lwkt_reltoken(&wpipe
->pipe_wlock
);
719 if (fflags
& O_FBLOCKING
)
721 else if (fflags
& O_FNONBLOCKING
)
723 else if (fp
->f_flag
& O_NONBLOCK
)
729 * If it is advantageous to resize the pipe buffer, do
730 * so. We are write-serialized so we can block safely.
732 if ((wpipe
->pipe_buffer
.size
<= PIPE_SIZE
) &&
733 (pipe_nbig
< pipe_maxbig
) &&
734 wpipe
->pipe_wantwcnt
> 4 &&
735 (wpipe
->pipe_buffer
.rindex
== wpipe
->pipe_buffer
.windex
)) {
737 * Recheck after lock.
739 lwkt_gettoken(&wpipe
->pipe_rlock
);
740 if ((wpipe
->pipe_buffer
.size
<= PIPE_SIZE
) &&
741 (pipe_nbig
< pipe_maxbig
) &&
742 (wpipe
->pipe_buffer
.rindex
== wpipe
->pipe_buffer
.windex
)) {
743 atomic_add_int(&pipe_nbig
, 1);
744 if (pipespace(wpipe
, BIG_PIPE_SIZE
) == 0)
747 atomic_subtract_int(&pipe_nbig
, 1);
749 lwkt_reltoken(&wpipe
->pipe_rlock
);
752 orig_resid
= uio
->uio_resid
;
755 bigwrite
= (uio
->uio_resid
> 10 * 1024 * 1024);
758 while (uio
->uio_resid
) {
759 if (wpipe
->pipe_state
& PIPE_WEOF
) {
767 if (bigwrite
&& --bigcount
== 0) {
770 if (CURSIG(curthread
->td_lwp
)) {
776 windex
= wpipe
->pipe_buffer
.windex
&
777 (wpipe
->pipe_buffer
.size
- 1);
778 space
= wpipe
->pipe_buffer
.size
-
779 (wpipe
->pipe_buffer
.windex
- wpipe
->pipe_buffer
.rindex
);
782 /* Writes of size <= PIPE_BUF must be atomic. */
783 if ((space
< uio
->uio_resid
) && (orig_resid
<= PIPE_BUF
))
787 * Write to fill, read size handles write hysteresis. Also
788 * additional restrictions can cause select-based non-blocking
795 * Transfer size is minimum of uio transfer
796 * and free space in pipe buffer.
798 * Limit each uiocopy to no more then PIPE_SIZE
799 * so we can keep the gravy train going on a
800 * SMP box. This doubles the performance for
801 * write sizes > 16K. Otherwise large writes
802 * wind up doing an inefficient synchronous
805 space
= szmin(space
, uio
->uio_resid
);
806 if (space
> PIPE_SIZE
)
810 * First segment to transfer is minimum of
811 * transfer size and contiguous space in
812 * pipe buffer. If first segment to transfer
813 * is less than the transfer size, we've got
814 * a wraparound in the buffer.
816 segsize
= wpipe
->pipe_buffer
.size
- windex
;
821 * If this is the first loop and the reader is
822 * blocked, do a preemptive wakeup of the reader.
824 * On SMP the IPI latency plus the wlock interlock
825 * on the reader side is the fastest way to get the
826 * reader going. (The scheduler will hard loop on
829 * NOTE: We can't clear WANTR here without acquiring
830 * the rlock, which we don't want to do here!
832 if ((wpipe
->pipe_state
& PIPE_WANTR
))
836 * Transfer segment, which may include a wrap-around.
837 * Update windex to account for both all in one go
838 * so the reader can read() the data atomically.
840 error
= uiomove(&wpipe
->pipe_buffer
.buffer
[windex
],
842 if (error
== 0 && segsize
< space
) {
843 segsize
= space
- segsize
;
844 error
= uiomove(&wpipe
->pipe_buffer
.buffer
[0],
850 wpipe
->pipe_buffer
.windex
+= space
;
856 * We need both the rlock and the wlock to interlock against
857 * the EOF, WANTW, and size checks, and to modify pipe_state.
859 * These are token locks so we do not have to worry about
862 lwkt_gettoken(&wpipe
->pipe_rlock
);
865 * If the "read-side" has been blocked, wake it up now
866 * and yield to let it drain synchronously rather
869 if (wpipe
->pipe_state
& PIPE_WANTR
) {
870 wpipe
->pipe_state
&= ~PIPE_WANTR
;
875 * don't block on non-blocking I/O
878 lwkt_reltoken(&wpipe
->pipe_rlock
);
884 * re-test whether we have to block in the writer after
885 * acquiring both locks, in case the reader opened up
888 space
= wpipe
->pipe_buffer
.size
-
889 (wpipe
->pipe_buffer
.windex
- wpipe
->pipe_buffer
.rindex
);
891 if ((space
< uio
->uio_resid
) && (orig_resid
<= PIPE_BUF
))
895 * Retest EOF - acquiring a new token can temporarily release
896 * tokens already held.
898 if (wpipe
->pipe_state
& PIPE_WEOF
) {
899 lwkt_reltoken(&wpipe
->pipe_rlock
);
905 * We have no more space and have something to offer,
906 * wake up select/poll/kq.
909 wpipe
->pipe_state
|= PIPE_WANTW
;
910 ++wpipe
->pipe_wantwcnt
;
911 pipewakeup(wpipe
, 1);
912 if (wpipe
->pipe_state
& PIPE_WANTW
)
913 error
= tsleep(wpipe
, PCATCH
, "pipewr", 0);
914 ++pipe_wblocked_count
;
916 lwkt_reltoken(&wpipe
->pipe_rlock
);
919 * Break out if we errored or the read side wants us to go
924 if (wpipe
->pipe_state
& PIPE_WEOF
) {
929 pipe_end_uio(wpipe
, &wpipe
->pipe_wip
);
932 * If we have put any characters in the buffer, we wake up
935 * Both rlock and wlock are required to be able to modify pipe_state.
937 if (wpipe
->pipe_buffer
.windex
!= wpipe
->pipe_buffer
.rindex
) {
938 if (wpipe
->pipe_state
& PIPE_WANTR
) {
939 lwkt_gettoken(&wpipe
->pipe_rlock
);
940 if (wpipe
->pipe_state
& PIPE_WANTR
) {
941 wpipe
->pipe_state
&= ~PIPE_WANTR
;
942 lwkt_reltoken(&wpipe
->pipe_rlock
);
945 lwkt_reltoken(&wpipe
->pipe_rlock
);
948 lwkt_gettoken(&wpipe
->pipe_rlock
);
949 pipewakeup(wpipe
, 1);
950 lwkt_reltoken(&wpipe
->pipe_rlock
);
954 * Don't return EPIPE if I/O was successful
956 if ((wpipe
->pipe_buffer
.rindex
== wpipe
->pipe_buffer
.windex
) &&
957 (uio
->uio_resid
== 0) &&
963 vfs_timestamp(&wpipe
->pipe_mtime
);
966 * We have something to offer,
967 * wake up select/poll/kq.
969 /*space = wpipe->pipe_buffer.windex - wpipe->pipe_buffer.rindex;*/
970 lwkt_reltoken(&wpipe
->pipe_wlock
);
975 * we implement a very minimal set of ioctls for compatibility with sockets.
978 pipe_ioctl(struct file
*fp
, u_long cmd
, caddr_t data
,
979 struct ucred
*cred
, struct sysmsg
*msg
)
984 mpipe
= (struct pipe
*)fp
->f_data
;
986 lwkt_gettoken(&mpipe
->pipe_rlock
);
987 lwkt_gettoken(&mpipe
->pipe_wlock
);
992 mpipe
->pipe_state
|= PIPE_ASYNC
;
994 mpipe
->pipe_state
&= ~PIPE_ASYNC
;
999 *(int *)data
= mpipe
->pipe_buffer
.windex
-
1000 mpipe
->pipe_buffer
.rindex
;
1004 error
= fsetown(*(int *)data
, &mpipe
->pipe_sigio
);
1007 *(int *)data
= fgetown(&mpipe
->pipe_sigio
);
1011 /* This is deprecated, FIOSETOWN should be used instead. */
1012 error
= fsetown(-(*(int *)data
), &mpipe
->pipe_sigio
);
1016 /* This is deprecated, FIOGETOWN should be used instead. */
1017 *(int *)data
= -fgetown(&mpipe
->pipe_sigio
);
1024 lwkt_reltoken(&mpipe
->pipe_wlock
);
1025 lwkt_reltoken(&mpipe
->pipe_rlock
);
1034 pipe_stat(struct file
*fp
, struct stat
*ub
, struct ucred
*cred
)
1038 pipe
= (struct pipe
*)fp
->f_data
;
1040 bzero((caddr_t
)ub
, sizeof(*ub
));
1041 ub
->st_mode
= S_IFIFO
;
1042 ub
->st_blksize
= pipe
->pipe_buffer
.size
;
1043 ub
->st_size
= pipe
->pipe_buffer
.windex
- pipe
->pipe_buffer
.rindex
;
1044 ub
->st_blocks
= (ub
->st_size
+ ub
->st_blksize
- 1) / ub
->st_blksize
;
1045 ub
->st_atimespec
= pipe
->pipe_atime
;
1046 ub
->st_mtimespec
= pipe
->pipe_mtime
;
1047 ub
->st_ctimespec
= pipe
->pipe_ctime
;
1049 * Left as 0: st_dev, st_ino, st_nlink, st_uid, st_gid, st_rdev,
1051 * XXX (st_dev, st_ino) should be unique.
1057 pipe_close(struct file
*fp
)
1061 cpipe
= (struct pipe
*)fp
->f_data
;
1062 fp
->f_ops
= &badfileops
;
1064 funsetown(&cpipe
->pipe_sigio
);
1070 * Shutdown one or both directions of a full-duplex pipe.
1073 pipe_shutdown(struct file
*fp
, int how
)
1079 rpipe
= (struct pipe
*)fp
->f_data
;
1080 wpipe
= rpipe
->pipe_peer
;
1083 * We modify pipe_state on both pipes, which means we need
1086 lwkt_gettoken(&rpipe
->pipe_rlock
);
1087 lwkt_gettoken(&rpipe
->pipe_wlock
);
1088 lwkt_gettoken(&wpipe
->pipe_rlock
);
1089 lwkt_gettoken(&wpipe
->pipe_wlock
);
1094 rpipe
->pipe_state
|= PIPE_REOF
; /* my reads */
1095 rpipe
->pipe_state
|= PIPE_WEOF
; /* peer writes */
1096 if (rpipe
->pipe_state
& PIPE_WANTR
) {
1097 rpipe
->pipe_state
&= ~PIPE_WANTR
;
1100 if (rpipe
->pipe_state
& PIPE_WANTW
) {
1101 rpipe
->pipe_state
&= ~PIPE_WANTW
;
1109 wpipe
->pipe_state
|= PIPE_REOF
; /* peer reads */
1110 wpipe
->pipe_state
|= PIPE_WEOF
; /* my writes */
1111 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1112 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1115 if (wpipe
->pipe_state
& PIPE_WANTW
) {
1116 wpipe
->pipe_state
&= ~PIPE_WANTW
;
1122 pipewakeup(rpipe
, 1);
1123 pipewakeup(wpipe
, 1);
1125 lwkt_reltoken(&wpipe
->pipe_wlock
);
1126 lwkt_reltoken(&wpipe
->pipe_rlock
);
1127 lwkt_reltoken(&rpipe
->pipe_wlock
);
1128 lwkt_reltoken(&rpipe
->pipe_rlock
);
1134 pipe_free_kmem(struct pipe
*cpipe
)
1136 if (cpipe
->pipe_buffer
.buffer
!= NULL
) {
1137 if (cpipe
->pipe_buffer
.size
> PIPE_SIZE
)
1138 atomic_subtract_int(&pipe_nbig
, 1);
1139 kmem_free(&kernel_map
,
1140 (vm_offset_t
)cpipe
->pipe_buffer
.buffer
,
1141 cpipe
->pipe_buffer
.size
);
1142 cpipe
->pipe_buffer
.buffer
= NULL
;
1143 cpipe
->pipe_buffer
.object
= NULL
;
1148 * Close the pipe. The slock must be held to interlock against simultanious
1149 * closes. The rlock and wlock must be held to adjust the pipe_state.
1152 pipeclose(struct pipe
*cpipe
)
1161 * The slock may not have been allocated yet (close during
1164 * We need both the read and write tokens to modify pipe_state.
1166 if (cpipe
->pipe_slock
)
1167 lockmgr(cpipe
->pipe_slock
, LK_EXCLUSIVE
);
1168 lwkt_gettoken(&cpipe
->pipe_rlock
);
1169 lwkt_gettoken(&cpipe
->pipe_wlock
);
1172 * Set our state, wakeup anyone waiting in select/poll/kq, and
1173 * wakeup anyone blocked on our pipe.
1175 cpipe
->pipe_state
|= PIPE_CLOSED
| PIPE_REOF
| PIPE_WEOF
;
1176 pipewakeup(cpipe
, 1);
1177 if (cpipe
->pipe_state
& (PIPE_WANTR
| PIPE_WANTW
)) {
1178 cpipe
->pipe_state
&= ~(PIPE_WANTR
| PIPE_WANTW
);
1183 * Disconnect from peer.
1185 if ((ppipe
= cpipe
->pipe_peer
) != NULL
) {
1186 lwkt_gettoken(&ppipe
->pipe_rlock
);
1187 lwkt_gettoken(&ppipe
->pipe_wlock
);
1188 ppipe
->pipe_state
|= PIPE_REOF
| PIPE_WEOF
;
1189 pipewakeup(ppipe
, 1);
1190 if (ppipe
->pipe_state
& (PIPE_WANTR
| PIPE_WANTW
)) {
1191 ppipe
->pipe_state
&= ~(PIPE_WANTR
| PIPE_WANTW
);
1194 if (SLIST_FIRST(&ppipe
->pipe_kq
.ki_note
))
1195 KNOTE(&ppipe
->pipe_kq
.ki_note
, 0);
1196 lwkt_reltoken(&ppipe
->pipe_wlock
);
1197 lwkt_reltoken(&ppipe
->pipe_rlock
);
1201 * If the peer is also closed we can free resources for both
1202 * sides, otherwise we leave our side intact to deal with any
1203 * races (since we only have the slock).
1205 if (ppipe
&& (ppipe
->pipe_state
& PIPE_CLOSED
)) {
1206 cpipe
->pipe_peer
= NULL
;
1207 ppipe
->pipe_peer
= NULL
;
1208 ppipe
->pipe_slock
= NULL
; /* we will free the slock */
1213 lwkt_reltoken(&cpipe
->pipe_wlock
);
1214 lwkt_reltoken(&cpipe
->pipe_rlock
);
1215 if (cpipe
->pipe_slock
)
1216 lockmgr(cpipe
->pipe_slock
, LK_RELEASE
);
1219 * If we disassociated from our peer we can free resources
1221 if (ppipe
== NULL
) {
1223 if (cpipe
->pipe_slock
) {
1224 kfree(cpipe
->pipe_slock
, M_PIPE
);
1225 cpipe
->pipe_slock
= NULL
;
1227 if (gd
->gd_pipeqcount
>= pipe_maxcache
||
1228 cpipe
->pipe_buffer
.size
!= PIPE_SIZE
1230 pipe_free_kmem(cpipe
);
1231 kfree(cpipe
, M_PIPE
);
1233 cpipe
->pipe_state
= 0;
1234 cpipe
->pipe_peer
= gd
->gd_pipeq
;
1235 gd
->gd_pipeq
= cpipe
;
1236 ++gd
->gd_pipeqcount
;
1242 pipe_kqfilter(struct file
*fp
, struct knote
*kn
)
1246 cpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1248 switch (kn
->kn_filter
) {
1250 kn
->kn_fop
= &pipe_rfiltops
;
1253 kn
->kn_fop
= &pipe_wfiltops
;
1254 if (cpipe
->pipe_peer
== NULL
) {
1255 /* other end of pipe has been closed */
1260 return (EOPNOTSUPP
);
1262 kn
->kn_hook
= (caddr_t
)cpipe
;
1264 knote_insert(&cpipe
->pipe_kq
.ki_note
, kn
);
1270 filt_pipedetach(struct knote
*kn
)
1272 struct pipe
*cpipe
= (struct pipe
*)kn
->kn_hook
;
1274 knote_remove(&cpipe
->pipe_kq
.ki_note
, kn
);
1279 filt_piperead(struct knote
*kn
, long hint
)
1281 struct pipe
*rpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1284 lwkt_gettoken(&rpipe
->pipe_rlock
);
1285 lwkt_gettoken(&rpipe
->pipe_wlock
);
1287 kn
->kn_data
= rpipe
->pipe_buffer
.windex
- rpipe
->pipe_buffer
.rindex
;
1289 if (rpipe
->pipe_state
& PIPE_REOF
) {
1291 * Only set NODATA if all data has been exhausted
1293 if (kn
->kn_data
== 0)
1294 kn
->kn_flags
|= EV_NODATA
;
1295 kn
->kn_flags
|= EV_EOF
;
1299 lwkt_reltoken(&rpipe
->pipe_wlock
);
1300 lwkt_reltoken(&rpipe
->pipe_rlock
);
1303 ready
= kn
->kn_data
> 0;
1310 filt_pipewrite(struct knote
*kn
, long hint
)
1312 struct pipe
*rpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1313 struct pipe
*wpipe
= rpipe
->pipe_peer
;
1317 if (wpipe
== NULL
) {
1318 kn
->kn_flags
|= (EV_EOF
| EV_NODATA
);
1322 lwkt_gettoken(&wpipe
->pipe_rlock
);
1323 lwkt_gettoken(&wpipe
->pipe_wlock
);
1325 if (wpipe
->pipe_state
& PIPE_WEOF
) {
1326 kn
->kn_flags
|= (EV_EOF
| EV_NODATA
);
1331 kn
->kn_data
= wpipe
->pipe_buffer
.size
-
1332 (wpipe
->pipe_buffer
.windex
-
1333 wpipe
->pipe_buffer
.rindex
);
1335 lwkt_reltoken(&wpipe
->pipe_wlock
);
1336 lwkt_reltoken(&wpipe
->pipe_rlock
);
1339 ready
= kn
->kn_data
>= PIPE_BUF
;