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 * $FreeBSD: releng/5.1/sys/kern/uipc_cow.c 113358 2003-04-11 07:02:36Z alc $
33 */
34 /*
35 * This is a set of routines for enabling and disabling copy on write
36 * protection for data written into sockets.
37 */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/proc.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/mbuf.h>
46 #include <sys/socketvar.h>
47 #include <sys/uio.h>
48
49 #include <vm/vm.h>
50 #include <vm/vm_param.h>
51 #include <vm/pmap.h>
52 #include <vm/vm_map.h>
53 #include <vm/vm_page.h>
54 #include <vm/vm_object.h>
55
56
57 struct netsend_cow_stats {
58 int attempted;
59 int fail_not_mapped;
60 int fail_wired;
61 int fail_not_anon;
62 int fail_pmap_cow;
63 int fail_pg_error;
64 int fail_kva;
65 int free_post_exit;
66 int success;
67 int iodone;
68 int freed;
69 };
70
71 static struct netsend_cow_stats socow_stats = {0,0,0,0,0,0,0,0,0,0,0};
72
73 static void socow_iodone(void *addr, void *args);
74
75 static void
76 socow_iodone(void *addr, void *args)
77 {
78 int s;
79 struct sf_buf *sf;
80 vm_page_t pp;
81
82 sf = args;
83 pp = sf->m;
84 s = splvm();
85 /* remove COW mapping */
86 vm_page_lock_queues();
87 vm_page_cowclear(pp);
88 vm_page_unlock_queues();
89 splx(s);
90 /* note that sf_buf_free() unwires the page for us*/
91 sf_buf_free(addr, args);
92 socow_stats.iodone++;
93 }
94
95 int
96 socow_setup(struct mbuf *m0, struct uio *uio)
97 {
98 struct sf_buf *sf;
99 vm_page_t pp;
100 vm_paddr_t pa;
101 struct iovec *iov;
102 struct vmspace *vmspace;
103 struct vm_map *map;
104 vm_offset_t uva;
105 int s;
106
107 vmspace = curproc->p_vmspace;
108 map = &vmspace->vm_map;
109 uva = (vm_offset_t) uio->uio_iov->iov_base;
110
111 s = splvm();
112
113 /*
114 * verify page is mapped & not already wired for i/o
115 */
116 socow_stats.attempted++;
117 pa=pmap_extract(map->pmap, uva);
118 if(!pa) {
119 socow_stats.fail_not_mapped++;
120 splx(s);
121 return(0);
122 }
123 pp = PHYS_TO_VM_PAGE(pa);
124
125 /*
126 * set up COW
127 */
128 vm_page_lock_queues();
129 vm_page_cowsetup(pp);
130
131 /*
132 * wire the page for I/O
133 */
134 vm_page_wire(pp);
135 vm_page_unlock_queues();
136
137 /*
138 * Allocate an sf buf
139 */
140 sf = sf_buf_alloc(pp);
141
142 /*
143 * attach to mbuf
144 */
145 m0->m_data = (caddr_t)sf->kva;
146 m0->m_len = PAGE_SIZE;
147 MEXTADD(m0, sf->kva, PAGE_SIZE, socow_iodone, sf, M_RDONLY, EXT_SFBUF);
148 socow_stats.success++;
149
150 iov = uio->uio_iov;
151 iov->iov_base = (char *)iov->iov_base + PAGE_SIZE;
152 iov->iov_len -= PAGE_SIZE;
153 uio->uio_resid -= PAGE_SIZE;
154 uio->uio_offset += PAGE_SIZE;
155 if (iov->iov_len == 0) {
156 uio->uio_iov++;
157 uio->uio_iovcnt--;
158 }
159
160 splx(s);
161 return(1);
162 }
Cache object: abbe6e44f9df62eb371f91946112d9e3
|