1 /* $OpenBSD: uvm_anon.c,v 1.30 2009/03/20 15:19:04 oga Exp $ */ 2 /* $NetBSD: uvm_anon.c,v 1.10 2000/11/25 06:27:59 chs Exp $ */ 3 4 /* 5 * 6 * Copyright (c) 1997 Charles D. Cranor and Washington University. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by Charles D. Cranor and 20 * Washington University. 21 * 4. The name of the author may not be used to endorse or promote products 22 * derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 /* 37 * uvm_anon.c: uvm anon ops 38 */ 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/proc.h> 43 #include <sys/malloc.h> 44 #include <sys/pool.h> 45 #include <sys/kernel.h> 46 47 #include <uvm/uvm.h> 48 #include <uvm/uvm_swap.h> 49 50 struct pool uvm_anon_pool; 51 52 /* 53 * allocate anons 54 */ 55 void 56 uvm_anon_init(void) 57 { 58 pool_init(&uvm_anon_pool, sizeof(struct vm_anon), 0, 0, 0, "anonpl", 59 &pool_allocator_nointr); 60 pool_sethiwat(&uvm_anon_pool, uvmexp.free / 16); 61 } 62 63 /* 64 * allocate an anon 65 */ 66 struct vm_anon * 67 uvm_analloc(void) 68 { 69 struct vm_anon *anon; 70 71 anon = pool_get(&uvm_anon_pool, PR_NOWAIT); 72 if (anon) { 73 simple_lock_init(&anon->an_lock); 74 anon->an_ref = 1; 75 anon->an_page = NULL; 76 anon->an_swslot = 0; 77 simple_lock(&anon->an_lock); 78 } 79 return(anon); 80 } 81 82 /* 83 * uvm_anfree: free a single anon structure 84 * 85 * => caller must remove anon from its amap before calling (if it was in 86 * an amap). 87 * => anon must be unlocked and have a zero reference count. 88 * => we may lock the pageq's. 89 */ 90 void 91 uvm_anfree(struct vm_anon *anon) 92 { 93 struct vm_page *pg; 94 UVMHIST_FUNC("uvm_anfree"); UVMHIST_CALLED(maphist); 95 UVMHIST_LOG(maphist,"(anon=%p)", anon, 0,0,0); 96 97 /* 98 * get page 99 */ 100 101 pg = anon->an_page; 102 103 /* 104 * if there is a resident page and it is loaned, then anon may not 105 * own it. call out to uvm_anon_lockpage() to ensure the real owner 106 * of the page has been identified and locked. 107 */ 108 109 if (pg && pg->loan_count) 110 pg = uvm_anon_lockloanpg(anon); 111 112 /* 113 * if we have a resident page, we must dispose of it before freeing 114 * the anon. 115 */ 116 117 if (pg) { 118 119 /* 120 * if the page is owned by a uobject (now locked), then we must 121 * kill the loan on the page rather than free it. 122 */ 123 124 if (pg->uobject) { 125 uvm_lock_pageq(); 126 KASSERT(pg->loan_count > 0); 127 pg->loan_count--; 128 pg->uanon = NULL; 129 uvm_unlock_pageq(); 130 simple_unlock(&pg->uobject->vmobjlock); 131 } else { 132 133 /* 134 * page has no uobject, so we must be the owner of it. 135 * 136 * if page is busy then we just mark it as released 137 * (who ever has it busy must check for this when they 138 * wake up). if the page is not busy then we can 139 * free it now. 140 */ 141 142 if ((pg->pg_flags & PG_BUSY) != 0) { 143 /* tell them to dump it when done */ 144 atomic_setbits_int(&pg->pg_flags, PG_RELEASED); 145 UVMHIST_LOG(maphist, 146 " anon %p, page %p: BUSY (released!)", 147 anon, pg, 0, 0); 148 return; 149 } 150 pmap_page_protect(pg, VM_PROT_NONE); 151 uvm_lock_pageq(); /* lock out pagedaemon */ 152 uvm_pagefree(pg); /* bye bye */ 153 uvm_unlock_pageq(); /* free the daemon */ 154 UVMHIST_LOG(maphist,"anon %p, page %p: freed now!", 155 anon, pg, 0, 0); 156 } 157 } 158 if (pg == NULL && anon->an_swslot != 0) { 159 /* this page is no longer only in swap. */ 160 simple_lock(&uvm.swap_data_lock); 161 KASSERT(uvmexp.swpgonly > 0); 162 uvmexp.swpgonly--; 163 simple_unlock(&uvm.swap_data_lock); 164 } 165 166 /* 167 * free any swap resources. 168 */ 169 uvm_anon_dropswap(anon); 170 171 /* 172 * now that we've stripped the data areas from the anon, free the anon 173 * itself! 174 */ 175 KASSERT(anon->an_page == NULL); 176 KASSERT(anon->an_swslot == 0); 177 178 pool_put(&uvm_anon_pool, anon); 179 UVMHIST_LOG(maphist,"<- done!",0,0,0,0); 180 } 181 182 /* 183 * uvm_anon_dropswap: release any swap resources from this anon. 184 * 185 * => anon must be locked or have a reference count of 0. 186 */ 187 void 188 uvm_anon_dropswap(struct vm_anon *anon) 189 { 190 UVMHIST_FUNC("uvm_anon_dropswap"); UVMHIST_CALLED(maphist); 191 192 if (anon->an_swslot == 0) 193 return; 194 195 UVMHIST_LOG(maphist,"freeing swap for anon %p, paged to swslot 0x%lx", 196 anon, anon->an_swslot, 0, 0); 197 uvm_swap_free(anon->an_swslot, 1); 198 anon->an_swslot = 0; 199 } 200 201 /* 202 * uvm_anon_lockloanpg: given a locked anon, lock its resident page 203 * 204 * => anon is locked by caller 205 * => on return: anon is locked 206 * if there is a resident page: 207 * if it has a uobject, it is locked by us 208 * if it is ownerless, we take over as owner 209 * we return the resident page (it can change during 210 * this function) 211 * => note that the only time an anon has an ownerless resident page 212 * is if the page was loaned from a uvm_object and the uvm_object 213 * disowned it 214 * => this only needs to be called when you want to do an operation 215 * on an anon's resident page and that page has a non-zero loan 216 * count. 217 */ 218 struct vm_page * 219 uvm_anon_lockloanpg(struct vm_anon *anon) 220 { 221 struct vm_page *pg; 222 boolean_t locked = FALSE; 223 224 /* 225 * loop while we have a resident page that has a non-zero loan count. 226 * if we successfully get our lock, we will "break" the loop. 227 * note that the test for pg->loan_count is not protected -- this 228 * may produce false positive results. note that a false positive 229 * result may cause us to do more work than we need to, but it will 230 * not produce an incorrect result. 231 */ 232 233 while (((pg = anon->an_page) != NULL) && pg->loan_count != 0) { 234 235 /* 236 * quickly check to see if the page has an object before 237 * bothering to lock the page queues. this may also produce 238 * a false positive result, but that's ok because we do a real 239 * check after that. 240 * 241 * XXX: quick check -- worth it? need volatile? 242 */ 243 244 if (pg->uobject) { 245 246 uvm_lock_pageq(); 247 if (pg->uobject) { /* the "real" check */ 248 locked = 249 simple_lock_try(&pg->uobject->vmobjlock); 250 } else { 251 /* object disowned before we got PQ lock */ 252 locked = TRUE; 253 } 254 uvm_unlock_pageq(); 255 256 /* 257 * if we didn't get a lock (try lock failed), then we 258 * toggle our anon lock and try again 259 */ 260 261 if (!locked) { 262 simple_unlock(&anon->an_lock); 263 264 /* 265 * someone locking the object has a chance to 266 * lock us right now 267 */ 268 269 simple_lock(&anon->an_lock); 270 continue; 271 } 272 } 273 274 /* 275 * if page is un-owned [i.e. the object dropped its ownership], 276 * then we can take over as owner! 277 */ 278 279 if (pg->uobject == NULL && (pg->pg_flags & PQ_ANON) == 0) { 280 uvm_lock_pageq(); 281 atomic_setbits_int(&pg->pg_flags, PQ_ANON); 282 pg->loan_count--; /* ... and drop our loan */ 283 uvm_unlock_pageq(); 284 } 285 286 /* 287 * we did it! break the loop 288 */ 289 290 break; 291 } 292 return(pg); 293 } 294 295 /* 296 * fetch an anon's page. 297 * 298 * => anon must be locked, and is unlocked upon return. 299 * => returns TRUE if pagein was aborted due to lack of memory. 300 */ 301 302 boolean_t 303 uvm_anon_pagein(struct vm_anon *anon) 304 { 305 struct vm_page *pg; 306 struct uvm_object *uobj; 307 int rv; 308 309 /* locked: anon */ 310 rv = uvmfault_anonget(NULL, NULL, anon); 311 /* 312 * if rv == VM_PAGER_OK, anon is still locked, else anon 313 * is unlocked 314 */ 315 316 switch (rv) { 317 case VM_PAGER_OK: 318 break; 319 320 case VM_PAGER_ERROR: 321 case VM_PAGER_REFAULT: 322 323 /* 324 * nothing more to do on errors. 325 * VM_PAGER_REFAULT can only mean that the anon was freed, 326 * so again there's nothing to do. 327 */ 328 329 return FALSE; 330 331 default: 332 #ifdef DIAGNOSTIC 333 panic("anon_pagein: uvmfault_anonget -> %d", rv); 334 #else 335 return FALSE; 336 #endif 337 } 338 339 /* 340 * ok, we've got the page now. 341 * mark it as dirty, clear its swslot and un-busy it. 342 */ 343 344 pg = anon->an_page; 345 uobj = pg->uobject; 346 uvm_swap_free(anon->an_swslot, 1); 347 anon->an_swslot = 0; 348 atomic_clearbits_int(&pg->pg_flags, PG_CLEAN); 349 350 /* 351 * deactivate the page (to put it on a page queue) 352 */ 353 354 pmap_clear_reference(pg); 355 pmap_page_protect(pg, VM_PROT_NONE); 356 uvm_lock_pageq(); 357 uvm_pagedeactivate(pg); 358 uvm_unlock_pageq(); 359 360 /* 361 * unlock the anon and we're done. 362 */ 363 364 simple_unlock(&anon->an_lock); 365 if (uobj) { 366 simple_unlock(&uobj->vmobjlock); 367 } 368 return FALSE; 369 } 370