1 /* $NetBSD: uvm_anon.c,v 1.38 2005/12/11 12:25:29 christos Exp $ */ 2 3 /* 4 * 5 * Copyright (c) 1997 Charles D. Cranor and Washington University. 6 * All rights reserved. 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. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by Charles D. Cranor and 19 * Washington University. 20 * 4. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 /* 36 * uvm_anon.c: uvm anon ops 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: uvm_anon.c,v 1.38 2005/12/11 12:25:29 christos Exp $"); 41 42 #include "opt_uvmhist.h" 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/proc.h> 47 #include <sys/malloc.h> 48 #include <sys/pool.h> 49 #include <sys/kernel.h> 50 51 #include <uvm/uvm.h> 52 #include <uvm/uvm_swap.h> 53 54 static POOL_INIT(uvm_anon_pool, sizeof(struct vm_anon), 0, 0, 0, "anonpl", 55 &pool_allocator_nointr); 56 static struct pool_cache uvm_anon_pool_cache; 57 58 static int uvm_anon_ctor(void *, void *, int); 59 60 /* 61 * allocate anons 62 */ 63 void 64 uvm_anon_init(void) 65 { 66 67 pool_cache_init(&uvm_anon_pool_cache, &uvm_anon_pool, 68 uvm_anon_ctor, NULL, NULL); 69 } 70 71 static int 72 uvm_anon_ctor(void *arg, void *object, int flags) 73 { 74 struct vm_anon *anon = object; 75 76 anon->an_ref = 0; 77 simple_lock_init(&anon->an_lock); 78 anon->an_page = NULL; 79 #if defined(VMSWAP) 80 anon->an_swslot = 0; 81 #endif /* defined(VMSWAP) */ 82 83 return 0; 84 } 85 86 /* 87 * allocate an anon 88 * 89 * => new anon is returned locked! 90 */ 91 struct vm_anon * 92 uvm_analloc(void) 93 { 94 struct vm_anon *anon; 95 96 anon = pool_cache_get(&uvm_anon_pool_cache, PR_NOWAIT); 97 if (anon) { 98 KASSERT(anon->an_ref == 0); 99 LOCK_ASSERT(simple_lock_held(&anon->an_lock) == 0); 100 KASSERT(anon->an_page == NULL); 101 #if defined(VMSWAP) 102 KASSERT(anon->an_swslot == 0); 103 #endif /* defined(VMSWAP) */ 104 anon->an_ref = 1; 105 simple_lock(&anon->an_lock); 106 } 107 return anon; 108 } 109 110 /* 111 * uvm_anfree: free a single anon structure 112 * 113 * => caller must remove anon from its amap before calling (if it was in 114 * an amap). 115 * => anon must be unlocked and have a zero reference count. 116 * => we may lock the pageq's. 117 */ 118 119 void 120 uvm_anfree(struct vm_anon *anon) 121 { 122 struct vm_page *pg; 123 UVMHIST_FUNC("uvm_anfree"); UVMHIST_CALLED(maphist); 124 UVMHIST_LOG(maphist,"(anon=0x%x)", anon, 0,0,0); 125 126 KASSERT(anon->an_ref == 0); 127 LOCK_ASSERT(!simple_lock_held(&anon->an_lock)); 128 129 /* 130 * get page 131 */ 132 133 pg = anon->an_page; 134 135 /* 136 * if there is a resident page and it is loaned, then anon may not 137 * own it. call out to uvm_anon_lockpage() to ensure the real owner 138 * of the page has been identified and locked. 139 */ 140 141 if (pg && pg->loan_count) { 142 simple_lock(&anon->an_lock); 143 pg = uvm_anon_lockloanpg(anon); 144 simple_unlock(&anon->an_lock); 145 } 146 147 /* 148 * if we have a resident page, we must dispose of it before freeing 149 * the anon. 150 */ 151 152 if (pg) { 153 154 /* 155 * if the page is owned by a uobject (now locked), then we must 156 * kill the loan on the page rather than free it. 157 */ 158 159 if (pg->uobject) { 160 uvm_lock_pageq(); 161 KASSERT(pg->loan_count > 0); 162 pg->loan_count--; 163 pg->uanon = NULL; 164 uvm_unlock_pageq(); 165 simple_unlock(&pg->uobject->vmobjlock); 166 } else { 167 168 /* 169 * page has no uobject, so we must be the owner of it. 170 */ 171 172 KASSERT((pg->flags & PG_RELEASED) == 0); 173 simple_lock(&anon->an_lock); 174 pmap_page_protect(pg, VM_PROT_NONE); 175 176 /* 177 * if the page is busy, mark it as PG_RELEASED 178 * so that uvm_anon_release will release it later. 179 */ 180 181 if (pg->flags & PG_BUSY) { 182 pg->flags |= PG_RELEASED; 183 simple_unlock(&anon->an_lock); 184 return; 185 } 186 uvm_lock_pageq(); 187 uvm_pagefree(pg); 188 uvm_unlock_pageq(); 189 simple_unlock(&anon->an_lock); 190 UVMHIST_LOG(maphist, "anon 0x%x, page 0x%x: " 191 "freed now!", anon, pg, 0, 0); 192 } 193 } 194 #if defined(VMSWAP) 195 if (pg == NULL && anon->an_swslot > 0) { 196 /* this page is no longer only in swap. */ 197 simple_lock(&uvm.swap_data_lock); 198 KASSERT(uvmexp.swpgonly > 0); 199 uvmexp.swpgonly--; 200 simple_unlock(&uvm.swap_data_lock); 201 } 202 #endif /* defined(VMSWAP) */ 203 204 /* 205 * free any swap resources. 206 */ 207 208 uvm_anon_dropswap(anon); 209 210 /* 211 * now that we've stripped the data areas from the anon, 212 * free the anon itself. 213 */ 214 215 KASSERT(anon->an_page == NULL); 216 #if defined(VMSWAP) 217 KASSERT(anon->an_swslot == 0); 218 #endif /* defined(VMSWAP) */ 219 220 pool_cache_put(&uvm_anon_pool_cache, anon); 221 UVMHIST_LOG(maphist,"<- done!",0,0,0,0); 222 } 223 224 #if defined(VMSWAP) 225 226 /* 227 * uvm_anon_dropswap: release any swap resources from this anon. 228 * 229 * => anon must be locked or have a reference count of 0. 230 */ 231 void 232 uvm_anon_dropswap(struct vm_anon *anon) 233 { 234 UVMHIST_FUNC("uvm_anon_dropswap"); UVMHIST_CALLED(maphist); 235 236 if (anon->an_swslot == 0) 237 return; 238 239 UVMHIST_LOG(maphist,"freeing swap for anon %p, paged to swslot 0x%x", 240 anon, anon->an_swslot, 0, 0); 241 uvm_swap_free(anon->an_swslot, 1); 242 anon->an_swslot = 0; 243 } 244 245 #endif /* defined(VMSWAP) */ 246 247 /* 248 * uvm_anon_lockloanpg: given a locked anon, lock its resident page 249 * 250 * => anon is locked by caller 251 * => on return: anon is locked 252 * if there is a resident page: 253 * if it has a uobject, it is locked by us 254 * if it is ownerless, we take over as owner 255 * we return the resident page (it can change during 256 * this function) 257 * => note that the only time an anon has an ownerless resident page 258 * is if the page was loaned from a uvm_object and the uvm_object 259 * disowned it 260 * => this only needs to be called when you want to do an operation 261 * on an anon's resident page and that page has a non-zero loan 262 * count. 263 */ 264 struct vm_page * 265 uvm_anon_lockloanpg(struct vm_anon *anon) 266 { 267 struct vm_page *pg; 268 boolean_t locked = FALSE; 269 270 LOCK_ASSERT(simple_lock_held(&anon->an_lock)); 271 272 /* 273 * loop while we have a resident page that has a non-zero loan count. 274 * if we successfully get our lock, we will "break" the loop. 275 * note that the test for pg->loan_count is not protected -- this 276 * may produce false positive results. note that a false positive 277 * result may cause us to do more work than we need to, but it will 278 * not produce an incorrect result. 279 */ 280 281 while (((pg = anon->an_page) != NULL) && pg->loan_count != 0) { 282 283 /* 284 * quickly check to see if the page has an object before 285 * bothering to lock the page queues. this may also produce 286 * a false positive result, but that's ok because we do a real 287 * check after that. 288 */ 289 290 if (pg->uobject) { 291 uvm_lock_pageq(); 292 if (pg->uobject) { 293 locked = 294 simple_lock_try(&pg->uobject->vmobjlock); 295 } else { 296 /* object disowned before we got PQ lock */ 297 locked = TRUE; 298 } 299 uvm_unlock_pageq(); 300 301 /* 302 * if we didn't get a lock (try lock failed), then we 303 * toggle our anon lock and try again 304 */ 305 306 if (!locked) { 307 simple_unlock(&anon->an_lock); 308 309 /* 310 * someone locking the object has a chance to 311 * lock us right now 312 */ 313 314 simple_lock(&anon->an_lock); 315 continue; 316 } 317 } 318 319 /* 320 * if page is un-owned [i.e. the object dropped its ownership], 321 * then we can take over as owner! 322 */ 323 324 if (pg->uobject == NULL && (pg->pqflags & PQ_ANON) == 0) { 325 uvm_lock_pageq(); 326 pg->pqflags |= PQ_ANON; 327 pg->loan_count--; 328 uvm_unlock_pageq(); 329 } 330 break; 331 } 332 return(pg); 333 } 334 335 #if defined(VMSWAP) 336 337 /* 338 * fetch an anon's page. 339 * 340 * => anon must be locked, and is unlocked upon return. 341 * => returns TRUE if pagein was aborted due to lack of memory. 342 */ 343 344 boolean_t 345 uvm_anon_pagein(struct vm_anon *anon) 346 { 347 struct vm_page *pg; 348 struct uvm_object *uobj; 349 int rv; 350 351 /* locked: anon */ 352 LOCK_ASSERT(simple_lock_held(&anon->an_lock)); 353 354 rv = uvmfault_anonget(NULL, NULL, anon); 355 356 /* 357 * if rv == 0, anon is still locked, else anon 358 * is unlocked 359 */ 360 361 switch (rv) { 362 case 0: 363 break; 364 365 case EIO: 366 case ERESTART: 367 368 /* 369 * nothing more to do on errors. 370 * ERESTART can only mean that the anon was freed, 371 * so again there's nothing to do. 372 */ 373 374 return FALSE; 375 376 default: 377 return TRUE; 378 } 379 380 /* 381 * ok, we've got the page now. 382 * mark it as dirty, clear its swslot and un-busy it. 383 */ 384 385 pg = anon->an_page; 386 uobj = pg->uobject; 387 if (anon->an_swslot > 0) 388 uvm_swap_free(anon->an_swslot, 1); 389 anon->an_swslot = 0; 390 pg->flags &= ~(PG_CLEAN); 391 392 /* 393 * deactivate the page (to put it on a page queue) 394 */ 395 396 pmap_clear_reference(pg); 397 uvm_lock_pageq(); 398 if (pg->wire_count == 0) 399 uvm_pagedeactivate(pg); 400 uvm_unlock_pageq(); 401 402 if (pg->flags & PG_WANTED) { 403 wakeup(pg); 404 pg->flags &= ~(PG_WANTED); 405 } 406 407 /* 408 * unlock the anon and we're done. 409 */ 410 411 simple_unlock(&anon->an_lock); 412 if (uobj) { 413 simple_unlock(&uobj->vmobjlock); 414 } 415 return FALSE; 416 } 417 418 #endif /* defined(VMSWAP) */ 419 420 /* 421 * uvm_anon_release: release an anon and its page. 422 * 423 * => caller must lock the anon. 424 */ 425 426 void 427 uvm_anon_release(struct vm_anon *anon) 428 { 429 struct vm_page *pg = anon->an_page; 430 431 LOCK_ASSERT(simple_lock_held(&anon->an_lock)); 432 433 KASSERT(pg != NULL); 434 KASSERT((pg->flags & PG_RELEASED) != 0); 435 KASSERT((pg->flags & PG_BUSY) != 0); 436 KASSERT(pg->uobject == NULL); 437 KASSERT(pg->uanon == anon); 438 KASSERT(pg->loan_count == 0); 439 KASSERT(anon->an_ref == 0); 440 441 uvm_lock_pageq(); 442 uvm_pagefree(pg); 443 uvm_unlock_pageq(); 444 simple_unlock(&anon->an_lock); 445 446 KASSERT(anon->an_page == NULL); 447 448 uvm_anfree(anon); 449 } 450