1 1.80 chs /* $NetBSD: uvm_anon.c,v 1.80 2020/10/25 00:05:26 chs Exp $ */ 2 1.1 chuck 3 1.1 chuck /* 4 1.1 chuck * Copyright (c) 1997 Charles D. Cranor and Washington University. 5 1.1 chuck * All rights reserved. 6 1.1 chuck * 7 1.1 chuck * Redistribution and use in source and binary forms, with or without 8 1.1 chuck * modification, are permitted provided that the following conditions 9 1.1 chuck * are met: 10 1.1 chuck * 1. Redistributions of source code must retain the above copyright 11 1.1 chuck * notice, this list of conditions and the following disclaimer. 12 1.1 chuck * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 chuck * notice, this list of conditions and the following disclaimer in the 14 1.1 chuck * documentation and/or other materials provided with the distribution. 15 1.1 chuck * 16 1.1 chuck * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.1 chuck * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 1.1 chuck * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 1.1 chuck * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 1.1 chuck * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 1.1 chuck * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 1.1 chuck * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 1.1 chuck * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 1.1 chuck * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 1.1 chuck * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 1.1 chuck */ 27 1.1 chuck 28 1.1 chuck /* 29 1.1 chuck * uvm_anon.c: uvm anon ops 30 1.1 chuck */ 31 1.21 lukem 32 1.21 lukem #include <sys/cdefs.h> 33 1.80 chs __KERNEL_RCSID(0, "$NetBSD: uvm_anon.c,v 1.80 2020/10/25 00:05:26 chs Exp $"); 34 1.1 chuck 35 1.1 chuck #include "opt_uvmhist.h" 36 1.1 chuck 37 1.1 chuck #include <sys/param.h> 38 1.1 chuck #include <sys/systm.h> 39 1.1 chuck #include <sys/pool.h> 40 1.5 chs #include <sys/kernel.h> 41 1.67 uwe #include <sys/atomic.h> 42 1.1 chuck 43 1.1 chuck #include <uvm/uvm.h> 44 1.1 chuck #include <uvm/uvm_swap.h> 45 1.39 yamt #include <uvm/uvm_pdpolicy.h> 46 1.1 chuck 47 1.55 rmind static struct pool_cache uvm_anon_cache; 48 1.5 chs 49 1.55 rmind static int uvm_anon_ctor(void *, void *, int); 50 1.5 chs 51 1.1 chuck void 52 1.34 thorpej uvm_anon_init(void) 53 1.1 chuck { 54 1.5 chs 55 1.49 ad pool_cache_bootstrap(&uvm_anon_cache, sizeof(struct vm_anon), 0, 0, 56 1.49 ad PR_LARGECACHE, "anonpl", NULL, IPL_NONE, uvm_anon_ctor, 57 1.55 rmind NULL, NULL); 58 1.1 chuck } 59 1.1 chuck 60 1.33 yamt static int 61 1.41 yamt uvm_anon_ctor(void *arg, void *object, int flags) 62 1.1 chuck { 63 1.33 yamt struct vm_anon *anon = object; 64 1.1 chuck 65 1.33 yamt anon->an_ref = 0; 66 1.59 rmind anon->an_lock = NULL; 67 1.33 yamt anon->an_page = NULL; 68 1.37 yamt #if defined(VMSWAP) 69 1.33 yamt anon->an_swslot = 0; 70 1.55 rmind #endif 71 1.11 chs return 0; 72 1.1 chuck } 73 1.1 chuck 74 1.1 chuck /* 75 1.55 rmind * uvm_analloc: allocate a new anon. 76 1.13 thorpej * 77 1.55 rmind * => anon will have no lock associated. 78 1.1 chuck */ 79 1.1 chuck struct vm_anon * 80 1.34 thorpej uvm_analloc(void) 81 1.1 chuck { 82 1.33 yamt struct vm_anon *anon; 83 1.1 chuck 84 1.47 ad anon = pool_cache_get(&uvm_anon_cache, PR_NOWAIT); 85 1.33 yamt if (anon) { 86 1.33 yamt KASSERT(anon->an_ref == 0); 87 1.59 rmind KASSERT(anon->an_lock == NULL); 88 1.33 yamt KASSERT(anon->an_page == NULL); 89 1.37 yamt #if defined(VMSWAP) 90 1.33 yamt KASSERT(anon->an_swslot == 0); 91 1.55 rmind #endif 92 1.33 yamt anon->an_ref = 1; 93 1.1 chuck } 94 1.33 yamt return anon; 95 1.1 chuck } 96 1.1 chuck 97 1.1 chuck /* 98 1.76 ad * uvm_anfree: free a single anon structure 99 1.1 chuck * 100 1.55 rmind * => anon must be removed from the amap (if anon was in an amap). 101 1.76 ad * => amap must be locked, if anon was owned by amap. 102 1.76 ad * => we may drop and re-acquire the lock here (to break loans). 103 1.1 chuck */ 104 1.76 ad void 105 1.76 ad uvm_anfree(struct vm_anon *anon) 106 1.1 chuck { 107 1.76 ad struct vm_page *pg = anon->an_page, *pg2 __diagused; 108 1.55 rmind 109 1.79 skrll UVMHIST_FUNC(__func__); 110 1.79 skrll UVMHIST_CALLARGS(maphist,"(anon=%#jx)", (uintptr_t)anon, 0,0,0); 111 1.1 chuck 112 1.76 ad KASSERT(anon->an_lock == NULL || rw_write_held(anon->an_lock)); 113 1.76 ad KASSERT(anon->an_ref == 0); 114 1.12 thorpej 115 1.1 chuck /* 116 1.76 ad * Dispose of the page, if it is resident. 117 1.1 chuck */ 118 1.1 chuck 119 1.76 ad if (__predict_true(pg != NULL)) { 120 1.54 rmind KASSERT(anon->an_lock != NULL); 121 1.1 chuck 122 1.66 ad /* 123 1.66 ad * If there is a resident page and it is loaned, then anon 124 1.66 ad * may not own it. Call out to uvm_anon_lockloanpg() to 125 1.66 ad * identify and lock the real owner of the page. 126 1.66 ad */ 127 1.1 chuck 128 1.76 ad if (__predict_false(pg->loan_count != 0)) { 129 1.76 ad pg2 = uvm_anon_lockloanpg(anon); 130 1.76 ad KASSERT(pg2 == pg); 131 1.66 ad } 132 1.1 chuck 133 1.1 chuck /* 134 1.55 rmind * If the page is owned by a UVM object (now locked), 135 1.55 rmind * then kill the loan on the page rather than free it, 136 1.55 rmind * and release the object lock. 137 1.1 chuck */ 138 1.1 chuck 139 1.76 ad if (__predict_false(pg->uobject != NULL)) { 140 1.69 ad mutex_enter(&pg->interlock); 141 1.10 chs KASSERT(pg->loan_count > 0); 142 1.1 chuck pg->loan_count--; 143 1.1 chuck pg->uanon = NULL; 144 1.69 ad mutex_exit(&pg->interlock); 145 1.72 ad rw_exit(pg->uobject->vmobjlock); 146 1.1 chuck } else { 147 1.1 chuck 148 1.1 chuck /* 149 1.55 rmind * If page has no UVM object, then anon is the owner, 150 1.55 rmind * and it is already locked. 151 1.1 chuck */ 152 1.1 chuck 153 1.18 chs KASSERT((pg->flags & PG_RELEASED) == 0); 154 1.18 chs pmap_page_protect(pg, VM_PROT_NONE); 155 1.29 yamt 156 1.29 yamt /* 157 1.55 rmind * If the page is busy, mark it as PG_RELEASED, so 158 1.55 rmind * that uvm_anon_release(9) would release it later. 159 1.29 yamt */ 160 1.29 yamt 161 1.76 ad if (__predict_false((pg->flags & PG_BUSY) != 0)) { 162 1.29 yamt pg->flags |= PG_RELEASED; 163 1.72 ad rw_obj_hold(anon->an_lock); 164 1.76 ad return; 165 1.17 chs } 166 1.69 ad uvm_pagefree(pg); 167 1.74 rin UVMHIST_LOG(maphist, "anon %#jx, page %#jx: " 168 1.69 ad "freed now!", (uintptr_t)anon, (uintptr_t)pg, 169 1.69 ad 0, 0); 170 1.1 chuck } 171 1.76 ad } else { 172 1.37 yamt #if defined(VMSWAP) 173 1.76 ad if (anon->an_swslot > 0) { 174 1.76 ad /* This page is no longer only in swap. */ 175 1.76 ad KASSERT(uvmexp.swpgonly > 0); 176 1.76 ad atomic_dec_uint(&uvmexp.swpgonly); 177 1.76 ad } 178 1.76 ad #endif 179 1.20 chs } 180 1.76 ad anon->an_lock = NULL; 181 1.1 chuck 182 1.69 ad /* 183 1.69 ad * Free any swap resources, leave a page replacement hint. 184 1.69 ad */ 185 1.69 ad 186 1.69 ad uvm_anon_dropswap(anon); 187 1.69 ad uvmpdpol_anfree(anon); 188 1.59 rmind UVMHIST_LOG(maphist,"<- done!",0,0,0,0); 189 1.47 ad pool_cache_put(&uvm_anon_cache, anon); 190 1.2 chs } 191 1.2 chs 192 1.55 rmind /* 193 1.55 rmind * uvm_anon_lockloanpg: given a locked anon, lock its resident page owner. 194 1.1 chuck * 195 1.1 chuck * => anon is locked by caller 196 1.1 chuck * => on return: anon is locked 197 1.1 chuck * if there is a resident page: 198 1.1 chuck * if it has a uobject, it is locked by us 199 1.1 chuck * if it is ownerless, we take over as owner 200 1.1 chuck * we return the resident page (it can change during 201 1.1 chuck * this function) 202 1.1 chuck * => note that the only time an anon has an ownerless resident page 203 1.1 chuck * is if the page was loaned from a uvm_object and the uvm_object 204 1.1 chuck * disowned it 205 1.1 chuck * => this only needs to be called when you want to do an operation 206 1.1 chuck * on an anon's resident page and that page has a non-zero loan 207 1.1 chuck * count. 208 1.1 chuck */ 209 1.1 chuck struct vm_page * 210 1.34 thorpej uvm_anon_lockloanpg(struct vm_anon *anon) 211 1.1 chuck { 212 1.1 chuck struct vm_page *pg; 213 1.72 ad krw_t op; 214 1.1 chuck 215 1.72 ad KASSERT(rw_lock_held(anon->an_lock)); 216 1.12 thorpej 217 1.1 chuck /* 218 1.1 chuck * loop while we have a resident page that has a non-zero loan count. 219 1.1 chuck * if we successfully get our lock, we will "break" the loop. 220 1.1 chuck * note that the test for pg->loan_count is not protected -- this 221 1.1 chuck * may produce false positive results. note that a false positive 222 1.1 chuck * result may cause us to do more work than we need to, but it will 223 1.1 chuck * not produce an incorrect result. 224 1.1 chuck */ 225 1.1 chuck 226 1.33 yamt while (((pg = anon->an_page) != NULL) && pg->loan_count != 0) { 227 1.69 ad mutex_enter(&pg->interlock); 228 1.1 chuck if (pg->uobject) { 229 1.1 chuck /* 230 1.1 chuck * if we didn't get a lock (try lock failed), then we 231 1.1 chuck * toggle our anon lock and try again 232 1.1 chuck */ 233 1.1 chuck 234 1.72 ad if (!rw_tryenter(pg->uobject->vmobjlock, RW_WRITER)) { 235 1.1 chuck /* 236 1.1 chuck * someone locking the object has a chance to 237 1.1 chuck * lock us right now 238 1.78 skrll * 239 1.54 rmind * XXX Better than yielding but inadequate. 240 1.1 chuck */ 241 1.69 ad mutex_exit(&pg->interlock); 242 1.73 ad op = rw_lock_op(anon->an_lock); 243 1.72 ad rw_exit(anon->an_lock); 244 1.72 ad kpause("lkloanpg", false, 1, NULL); 245 1.72 ad rw_enter(anon->an_lock, op); 246 1.10 chs continue; 247 1.1 chuck } 248 1.1 chuck } 249 1.1 chuck 250 1.1 chuck /* 251 1.55 rmind * If page is un-owned i.e. the object dropped its ownership, 252 1.55 rmind * then we have to take the ownership. 253 1.1 chuck */ 254 1.1 chuck 255 1.69 ad if (pg->uobject == NULL && (pg->flags & PG_ANON) == 0) { 256 1.69 ad pg->flags |= PG_ANON; 257 1.18 chs pg->loan_count--; 258 1.1 chuck } 259 1.69 ad mutex_exit(&pg->interlock); 260 1.1 chuck break; 261 1.1 chuck } 262 1.55 rmind return pg; 263 1.5 chs } 264 1.5 chs 265 1.37 yamt #if defined(VMSWAP) 266 1.37 yamt 267 1.5 chs /* 268 1.55 rmind * uvm_anon_pagein: fetch an anon's page. 269 1.5 chs * 270 1.5 chs * => anon must be locked, and is unlocked upon return. 271 1.43 thorpej * => returns true if pagein was aborted due to lack of memory. 272 1.5 chs */ 273 1.5 chs 274 1.42 thorpej bool 275 1.57 rmind uvm_anon_pagein(struct vm_amap *amap, struct vm_anon *anon) 276 1.5 chs { 277 1.5 chs struct vm_page *pg; 278 1.5 chs struct uvm_object *uobj; 279 1.8 thorpej 280 1.72 ad KASSERT(rw_write_held(anon->an_lock)); 281 1.57 rmind KASSERT(anon->an_lock == amap->am_lock); 282 1.12 thorpej 283 1.8 thorpej /* 284 1.55 rmind * Get the page of the anon. 285 1.8 thorpej */ 286 1.5 chs 287 1.57 rmind switch (uvmfault_anonget(NULL, amap, anon)) { 288 1.16 chs case 0: 289 1.55 rmind /* Success - we have the page. */ 290 1.72 ad KASSERT(rw_write_held(anon->an_lock)); 291 1.5 chs break; 292 1.16 chs case EIO: 293 1.16 chs case ERESTART: 294 1.5 chs /* 295 1.55 rmind * Nothing more to do on errors. ERESTART means that the 296 1.55 rmind * anon was freed. 297 1.5 chs */ 298 1.43 thorpej return false; 299 1.77 ad case ENOLCK: 300 1.77 ad panic("uvm_anon_pagein"); 301 1.25 pk default: 302 1.43 thorpej return true; 303 1.5 chs } 304 1.5 chs 305 1.5 chs /* 306 1.75 ad * Mark the page as dirty and clear its swslot. 307 1.5 chs */ 308 1.5 chs 309 1.33 yamt pg = anon->an_page; 310 1.5 chs uobj = pg->uobject; 311 1.55 rmind if (anon->an_swslot > 0) { 312 1.23 pk uvm_swap_free(anon->an_swslot, 1); 313 1.55 rmind } 314 1.5 chs anon->an_swslot = 0; 315 1.71 ad uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY); 316 1.5 chs 317 1.5 chs /* 318 1.55 rmind * Deactivate the page (to put it on a page queue). 319 1.5 chs */ 320 1.5 chs 321 1.70 ad uvm_pagelock(pg); 322 1.69 ad uvm_pagedeactivate(pg); 323 1.70 ad uvm_pageunlock(pg); 324 1.72 ad rw_exit(anon->an_lock); 325 1.5 chs if (uobj) { 326 1.72 ad rw_exit(uobj->vmobjlock); 327 1.5 chs } 328 1.43 thorpej return false; 329 1.1 chuck } 330 1.29 yamt 331 1.55 rmind /* 332 1.55 rmind * uvm_anon_dropswap: release any swap resources from this anon. 333 1.55 rmind * 334 1.55 rmind * => anon must be locked or have a reference count of 0. 335 1.55 rmind */ 336 1.55 rmind void 337 1.55 rmind uvm_anon_dropswap(struct vm_anon *anon) 338 1.55 rmind { 339 1.79 skrll UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist); 340 1.55 rmind 341 1.55 rmind if (anon->an_swslot == 0) 342 1.55 rmind return; 343 1.55 rmind 344 1.74 rin UVMHIST_LOG(maphist,"freeing swap for anon %#jx, paged to swslot %#jx", 345 1.64 pgoyette (uintptr_t)anon, anon->an_swslot, 0, 0); 346 1.55 rmind uvm_swap_free(anon->an_swslot, 1); 347 1.55 rmind anon->an_swslot = 0; 348 1.55 rmind } 349 1.55 rmind 350 1.55 rmind #endif 351 1.37 yamt 352 1.29 yamt /* 353 1.29 yamt * uvm_anon_release: release an anon and its page. 354 1.29 yamt * 355 1.55 rmind * => anon should not have any references. 356 1.55 rmind * => anon must be locked. 357 1.29 yamt */ 358 1.29 yamt 359 1.29 yamt void 360 1.34 thorpej uvm_anon_release(struct vm_anon *anon) 361 1.29 yamt { 362 1.33 yamt struct vm_page *pg = anon->an_page; 363 1.76 ad krwlock_t *lock; 364 1.29 yamt 365 1.72 ad KASSERT(rw_write_held(anon->an_lock)); 366 1.29 yamt KASSERT(pg != NULL); 367 1.29 yamt KASSERT((pg->flags & PG_RELEASED) != 0); 368 1.29 yamt KASSERT((pg->flags & PG_BUSY) != 0); 369 1.29 yamt KASSERT(pg->uobject == NULL); 370 1.29 yamt KASSERT(pg->uanon == anon); 371 1.29 yamt KASSERT(pg->loan_count == 0); 372 1.29 yamt KASSERT(anon->an_ref == 0); 373 1.29 yamt 374 1.80 chs if ((pg->flags & PG_PAGEOUT) != 0) { 375 1.80 chs pg->flags &= ~PG_PAGEOUT; 376 1.80 chs uvm_pageout_done(1); 377 1.80 chs } 378 1.80 chs 379 1.29 yamt uvm_pagefree(pg); 380 1.62 yamt KASSERT(anon->an_page == NULL); 381 1.76 ad lock = anon->an_lock; 382 1.76 ad uvm_anfree(anon); 383 1.76 ad rw_exit(lock); 384 1.59 rmind /* Note: extra reference is held for PG_RELEASED case. */ 385 1.76 ad rw_obj_free(lock); 386 1.29 yamt } 387