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.0/sys/kern/uipc_cow.c 104908 2002-10-11 14:58:34Z mike $
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 #if 0
56 #include <vm/vm_pager.h>
57 #include <vm/vm_kern.h>
58 #include <vm/vm_extern.h>
59 #include <vm/vm_zone.h>
60 #include <vm/swap_pager.h>
61 #endif
62
63
64 struct netsend_cow_stats {
65 int attempted;
66 int fail_not_mapped;
67 int fail_wired;
68 int fail_not_anon;
69 int fail_pmap_cow;
70 int fail_pg_error;
71 int fail_kva;
72 int free_post_exit;
73 int success;
74 int iodone;
75 int freed;
76 };
77
78 static struct netsend_cow_stats socow_stats = {0,0,0,0,0,0,0,0,0,0,0};
79
80 extern struct sf_buf *sf_bufs;
81 extern vm_offset_t sf_base;
82 #define dtosf(x) (&sf_bufs[((uintptr_t)(x) - (uintptr_t)sf_base) >> PAGE_SHIFT])
83 static void socow_iodone(void *addr, void *args);
84
85 static void
86 socow_iodone(void *addr, void *args)
87 {
88 int s;
89 struct sf_buf *sf;
90
91 vm_offset_t paddr;
92 vm_page_t pp;
93
94 sf = dtosf(addr);
95 paddr = vtophys((vm_offset_t)addr);
96 pp = PHYS_TO_VM_PAGE(paddr);
97 s = splvm();
98 /* remove COW mapping */
99 vm_page_lock_queues();
100 vm_page_cowclear(pp);
101 vm_page_unlock_queues();
102 vm_object_deallocate(pp->object);
103 splx(s);
104 /* note that sf_buf_free() unwires the page for us*/
105 sf_buf_free(addr, NULL);
106 socow_stats.iodone++;
107 }
108
109 int
110 socow_setup(struct mbuf *m0, struct uio *uio)
111 {
112 struct sf_buf *sf;
113 vm_page_t pp;
114 vm_offset_t pa;
115 struct iovec *iov;
116 struct vmspace *vmspace;
117 struct vm_map *map;
118 vm_offset_t uva;
119 int s;
120
121 vmspace = curproc->p_vmspace;;
122 map = &vmspace->vm_map;
123 uva = (vm_offset_t) uio->uio_iov->iov_base;
124
125 s = splvm();
126
127 /*
128 * verify page is mapped & not already wired for i/o
129 */
130 socow_stats.attempted++;
131 pa=pmap_extract(map->pmap, uva);
132 if(!pa) {
133 socow_stats.fail_not_mapped++;
134 splx(s);
135 return(0);
136 }
137 pp = PHYS_TO_VM_PAGE(pa);
138
139 sf = sf_buf_alloc();
140 sf->m = pp;
141 pmap_qenter(sf->kva, &pp, 1);
142
143 /*
144 * set up COW
145 */
146 vm_page_lock_queues();
147 vm_page_cowsetup(pp);
148
149 /*
150 * wire the page for I/O
151 */
152 vm_page_wire(pp);
153 vm_page_unlock_queues();
154
155 /*
156 * prevent the process from exiting on us.
157 */
158 vm_object_reference(pp->object);
159
160 /*
161 * attach to mbuf
162 */
163 m0->m_data = (caddr_t)sf->kva;
164 m0->m_len = PAGE_SIZE;
165 MEXTADD(m0, sf->kva, PAGE_SIZE, socow_iodone, NULL, 0, EXT_SFBUF);
166 socow_stats.success++;
167
168 iov = uio->uio_iov;
169 iov->iov_base = (char *)iov->iov_base + PAGE_SIZE;
170 iov->iov_len -= PAGE_SIZE;
171 uio->uio_resid -= PAGE_SIZE;
172 uio->uio_offset += PAGE_SIZE;
173 if (iov->iov_len == 0) {
174 uio->uio_iov++;
175 uio->uio_iovcnt--;
176 }
177
178 splx(s);
179 return(1);
180 }
Cache object: 0e8698e98e41c8177bb7fc123a47eebf
|