1 /* $NetBSD: uvm_object.c,v 1.6 2008/04/28 20:24:12 martin Exp $ */ 2 3 /* 4 * Copyright (c) 2006 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Mindaugas Rasiukevicius. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * uvm_object.c: operate with memory objects 34 * 35 * TODO: 36 * 1. Support PG_RELEASED-using objects 37 * 38 */ 39 40 #include <sys/cdefs.h> 41 __KERNEL_RCSID(0, "$NetBSD: uvm_object.c,v 1.6 2008/04/28 20:24:12 martin Exp $"); 42 43 #include "opt_uvmhist.h" 44 45 #include <sys/param.h> 46 47 #include <uvm/uvm.h> 48 49 /* We will fetch this page count per step */ 50 #define FETCH_PAGECOUNT 16 51 52 /* 53 * uobj_wirepages: wire the pages of entire uobj 54 * 55 * => NOTE: this function should only be used for types of objects 56 * where PG_RELEASED flag is never set (aobj objects) 57 * => caller must pass page-aligned start and end values 58 */ 59 60 int 61 uobj_wirepages(struct uvm_object *uobj, off_t start, off_t end) 62 { 63 int i, npages, error; 64 struct vm_page *pgs[FETCH_PAGECOUNT], *pg = NULL; 65 off_t offset = start, left; 66 67 left = (end - start) >> PAGE_SHIFT; 68 69 mutex_enter(&uobj->vmobjlock); 70 while (left) { 71 72 npages = MIN(FETCH_PAGECOUNT, left); 73 74 /* Get the pages */ 75 memset(pgs, 0, sizeof(pgs)); 76 error = (*uobj->pgops->pgo_get)(uobj, offset, pgs, &npages, 0, 77 VM_PROT_READ | VM_PROT_WRITE, UVM_ADV_SEQUENTIAL, 78 PGO_ALLPAGES | PGO_SYNCIO); 79 80 if (error) 81 goto error; 82 83 mutex_enter(&uobj->vmobjlock); 84 for (i = 0; i < npages; i++) { 85 86 KASSERT(pgs[i] != NULL); 87 KASSERT(!(pgs[i]->flags & PG_RELEASED)); 88 89 /* 90 * Loan break 91 */ 92 if (pgs[i]->loan_count) { 93 while (pgs[i]->loan_count) { 94 pg = uvm_loanbreak(pgs[i]); 95 if (!pg) { 96 mutex_exit(&uobj->vmobjlock); 97 uvm_wait("uobjwirepg"); 98 mutex_enter(&uobj->vmobjlock); 99 continue; 100 } 101 } 102 pgs[i] = pg; 103 } 104 105 if (pgs[i]->pqflags & PQ_AOBJ) { 106 pgs[i]->flags &= ~(PG_CLEAN); 107 uao_dropswap(uobj, i); 108 } 109 } 110 111 /* Wire the pages */ 112 mutex_enter(&uvm_pageqlock); 113 for (i = 0; i < npages; i++) { 114 uvm_pagewire(pgs[i]); 115 } 116 mutex_exit(&uvm_pageqlock); 117 118 /* Unbusy the pages */ 119 uvm_page_unbusy(pgs, npages); 120 121 left -= npages; 122 offset += npages << PAGE_SHIFT; 123 } 124 mutex_exit(&uobj->vmobjlock); 125 126 return 0; 127 128 error: 129 /* Unwire the pages which has been wired */ 130 uobj_unwirepages(uobj, start, offset); 131 132 return error; 133 } 134 135 /* 136 * uobj_unwirepages: unwire the pages of entire uobj 137 * 138 * => NOTE: this function should only be used for types of objects 139 * where PG_RELEASED flag is never set 140 * => caller must pass page-aligned start and end values 141 */ 142 143 void 144 uobj_unwirepages(struct uvm_object *uobj, off_t start, off_t end) 145 { 146 struct vm_page *pg; 147 off_t offset; 148 149 mutex_enter(&uobj->vmobjlock); 150 mutex_enter(&uvm_pageqlock); 151 for (offset = start; offset < end; offset += PAGE_SIZE) { 152 pg = uvm_pagelookup(uobj, offset); 153 154 KASSERT(pg != NULL); 155 KASSERT(!(pg->flags & PG_RELEASED)); 156 157 uvm_pageunwire(pg); 158 } 159 mutex_exit(&uvm_pageqlock); 160 mutex_exit(&uobj->vmobjlock); 161 } 162