uipc_cow.c revision 112316
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: head/sys/kern/uipc_cow.c 112316 2003-03-16 07:19:12Z 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#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 64struct 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 78static struct netsend_cow_stats socow_stats = {0,0,0,0,0,0,0,0,0,0,0}; 79 80static void socow_iodone(void *addr, void *args); 81 82static void 83socow_iodone(void *addr, void *args) 84{ 85 int s; 86 struct sf_buf *sf; 87 vm_page_t pp; 88 89 sf = args; 90 pp = sf->m; 91 s = splvm(); 92 /* remove COW mapping */ 93 vm_page_lock_queues(); 94 vm_page_cowclear(pp); 95 vm_page_unlock_queues(); 96 splx(s); 97 /* note that sf_buf_free() unwires the page for us*/ 98 sf_buf_free(addr, args); 99 socow_stats.iodone++; 100} 101 102int 103socow_setup(struct mbuf *m0, struct uio *uio) 104{ 105 struct sf_buf *sf; 106 vm_page_t pp; 107 vm_offset_t pa; 108 struct iovec *iov; 109 struct vmspace *vmspace; 110 struct vm_map *map; 111 vm_offset_t uva; 112 int s; 113 114 vmspace = curproc->p_vmspace;; 115 map = &vmspace->vm_map; 116 uva = (vm_offset_t) uio->uio_iov->iov_base; 117 118 s = splvm(); 119 120 /* 121 * verify page is mapped & not already wired for i/o 122 */ 123 socow_stats.attempted++; 124 pa=pmap_extract(map->pmap, uva); 125 if(!pa) { 126 socow_stats.fail_not_mapped++; 127 splx(s); 128 return(0); 129 } 130 pp = PHYS_TO_VM_PAGE(pa); 131 132 sf = sf_buf_alloc(); 133 sf->m = pp; 134 pmap_qenter(sf->kva, &pp, 1); 135 136 /* 137 * set up COW 138 */ 139 vm_page_lock_queues(); 140 vm_page_cowsetup(pp); 141 142 /* 143 * wire the page for I/O 144 */ 145 vm_page_wire(pp); 146 vm_page_unlock_queues(); 147 148 /* 149 * attach to mbuf 150 */ 151 m0->m_data = (caddr_t)sf->kva; 152 m0->m_len = PAGE_SIZE; 153 MEXTADD(m0, sf->kva, PAGE_SIZE, socow_iodone, sf, 0, EXT_SFBUF); 154 socow_stats.success++; 155 156 iov = uio->uio_iov; 157 iov->iov_base = (char *)iov->iov_base + PAGE_SIZE; 158 iov->iov_len -= PAGE_SIZE; 159 uio->uio_resid -= PAGE_SIZE; 160 uio->uio_offset += PAGE_SIZE; 161 if (iov->iov_len == 0) { 162 uio->uio_iov++; 163 uio->uio_iovcnt--; 164 } 165 166 splx(s); 167 return(1); 168} 169