FreeBSD/Linux Kernel Cross Reference
sys/kern/uipc_cow.c
1 /*-
2 * Copyright (c) 1997, Duke University
3 * All rights reserved.
4 *
5 * Author:
6 * Andrew Gallatin <gallatin@cs.duke.edu>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of Duke University may not be used to endorse or promote
17 * products derived from this software without specific prior written
18 * permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY DUKE UNIVERSITY ``AS IS'' AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DUKE UNIVERSITY BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITSOR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
28 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * This is a set of routines for enabling and disabling copy on write
35 * protection for data written into sockets.
36 */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD: releng/5.2/sys/kern/uipc_cow.c 122780 2003-11-16 06:11:26Z alc $");
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/proc.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/mbuf.h>
48 #include <sys/sf_buf.h>
49 #include <sys/socketvar.h>
50 #include <sys/uio.h>
51
52 #include <vm/vm.h>
53 #include <vm/vm_param.h>
54 #include <vm/pmap.h>
55 #include <vm/vm_map.h>
56 #include <vm/vm_page.h>
57 #include <vm/vm_object.h>
58
59
60 struct netsend_cow_stats {
61 int attempted;
62 int fail_not_mapped;
63 int fail_wired;
64 int fail_not_anon;
65 int fail_pmap_cow;
66 int fail_pg_error;
67 int fail_kva;
68 int free_post_exit;
69 int success;
70 int iodone;
71 int freed;
72 };
73
74 static struct netsend_cow_stats socow_stats = {0,0,0,0,0,0,0,0,0,0,0};
75
76 static void socow_iodone(void *addr, void *args);
77
78 static void
79 socow_iodone(void *addr, void *args)
80 {
81 int s;
82 struct sf_buf *sf;
83 vm_page_t pp;
84
85 sf = args;
86 pp = sf_buf_page(sf);
87 s = splvm();
88 /* remove COW mapping */
89 vm_page_lock_queues();
90 vm_page_cowclear(pp);
91 vm_page_unlock_queues();
92 splx(s);
93 /* note that sf_buf_free() unwires the page for us*/
94 sf_buf_free(addr, args);
95 socow_stats.iodone++;
96 }
97
98 int
99 socow_setup(struct mbuf *m0, struct uio *uio)
100 {
101 struct sf_buf *sf;
102 vm_page_t pp;
103 vm_paddr_t pa;
104 struct iovec *iov;
105 struct vmspace *vmspace;
106 struct vm_map *map;
107 vm_offset_t uva;
108 int s;
109
110 vmspace = curproc->p_vmspace;
111 map = &vmspace->vm_map;
112 uva = (vm_offset_t) uio->uio_iov->iov_base;
113
114 s = splvm();
115
116 /*
117 * verify page is mapped & not already wired for i/o
118 */
119 socow_stats.attempted++;
120 pa=pmap_extract(map->pmap, uva);
121 if(!pa) {
122 socow_stats.fail_not_mapped++;
123 splx(s);
124 return(0);
125 }
126 pp = PHYS_TO_VM_PAGE(pa);
127
128 /*
129 * set up COW
130 */
131 vm_page_lock_queues();
132 vm_page_cowsetup(pp);
133
134 /*
135 * wire the page for I/O
136 */
137 vm_page_wire(pp);
138 vm_page_unlock_queues();
139
140 /*
141 * Allocate an sf buf
142 */
143 sf = sf_buf_alloc(pp);
144
145 /*
146 * attach to mbuf
147 */
148 m0->m_data = (caddr_t)sf_buf_kva(sf);
149 m0->m_len = PAGE_SIZE;
150 MEXTADD(m0, sf_buf_kva(sf), PAGE_SIZE, socow_iodone, sf, M_RDONLY,
151 EXT_SFBUF);
152 socow_stats.success++;
153
154 iov = uio->uio_iov;
155 iov->iov_base = (char *)iov->iov_base + PAGE_SIZE;
156 iov->iov_len -= PAGE_SIZE;
157 uio->uio_resid -= PAGE_SIZE;
158 uio->uio_offset += PAGE_SIZE;
159 if (iov->iov_len == 0) {
160 uio->uio_iov++;
161 uio->uio_iovcnt--;
162 }
163
164 splx(s);
165 return(1);
166 }
Cache object: 2830f3cec64712636436c2078932e79b
|