1111072Sjake/* $OpenBSD: uvm_anon.c,v 1.58 2024/04/06 10:59:52 mpi Exp $ */ 2111072Sjake/* $NetBSD: uvm_anon.c,v 1.10 2000/11/25 06:27:59 chs Exp $ */ 3111072Sjake 4111072Sjake/* 5111072Sjake * Copyright (c) 1997 Charles D. Cranor and Washington University. 6111072Sjake * All rights reserved. 7111072Sjake * 8111072Sjake * Redistribution and use in source and binary forms, with or without 9111072Sjake * modification, are permitted provided that the following conditions 10111072Sjake * are met: 11111072Sjake * 1. Redistributions of source code must retain the above copyright 12111072Sjake * notice, this list of conditions and the following disclaimer. 13111072Sjake * 2. Redistributions in binary form must reproduce the above copyright 14111072Sjake * notice, this list of conditions and the following disclaimer in the 15111072Sjake * documentation and/or other materials provided with the distribution. 16111072Sjake * 17111072Sjake * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18111072Sjake * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19111072Sjake * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20111072Sjake * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21111072Sjake * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22111072Sjake * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23111072Sjake * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24111072Sjake * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25143825Smarius * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26143825Smarius * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27143825Smarius */ 28143825Smarius 29111072Sjake/* 30143825Smarius * uvm_anon.c: uvm anon ops 31143825Smarius */ 32143825Smarius 33143825Smarius#include <sys/param.h> 34143825Smarius#include <sys/systm.h> 35143825Smarius#include <sys/malloc.h> 36143825Smarius#include <sys/pool.h> 37143825Smarius#include <sys/kernel.h> 38143825Smarius#include <sys/atomic.h> 39143825Smarius 40143825Smarius#include <uvm/uvm.h> 41143825Smarius#include <uvm/uvm_swap.h> 42143825Smarius 43143825Smariusstruct pool uvm_anon_pool; 44143825Smarius 45143825Smariusvoid 46143825Smariusuvm_anon_init(void) 47143825Smarius{ 48143825Smarius pool_init(&uvm_anon_pool, sizeof(struct vm_anon), 0, IPL_MPFLOOR, 49143825Smarius PR_WAITOK, "anonpl", NULL); 50143825Smarius pool_sethiwat(&uvm_anon_pool, uvmexp.free / 16); 51143825Smarius} 52143825Smarius 53111072Sjake/* 54111072Sjake * uvm_analloc: allocate a new anon. 55111072Sjake * 56111072Sjake * => anon will have no lock associated. 57111072Sjake */ 58111072Sjakestruct vm_anon * 59111072Sjakeuvm_analloc(void) 60111072Sjake{ 61111072Sjake struct vm_anon *anon; 62111072Sjake 63111072Sjake anon = pool_get(&uvm_anon_pool, PR_NOWAIT); 64111072Sjake if (anon) { 65111072Sjake anon->an_lock = NULL; 66111072Sjake anon->an_ref = 1; 67111072Sjake anon->an_page = NULL; 68111072Sjake anon->an_swslot = 0; 69111072Sjake } 70111072Sjake return anon; 71143825Smarius} 72143825Smarius 73143825Smarius/* 74143825Smarius * uvm_anfree_list: free a single anon structure 75143825Smarius * 76143825Smarius * => anon must be removed from the amap (if anon was in an amap). 77111123Sjake * => amap must be locked, if anon was owned by amap. 78143825Smarius * => we may lock the pageq's. 79143825Smarius */ 80143825Smariusvoid 81143825Smariusuvm_anfree_list(struct vm_anon *anon, struct pglist *pgl) 82143825Smarius{ 83143825Smarius struct vm_page *pg = anon->an_page; 84143825Smarius 85143825Smarius KASSERT(anon->an_lock == NULL || rw_write_held(anon->an_lock)); 86143825Smarius KASSERT(anon->an_ref == 0); 87143825Smarius 88143825Smarius /* 89143825Smarius * Dispose of the page, if it is resident. 90143825Smarius */ 91143825Smarius if (pg != NULL) { 92143825Smarius KASSERT(anon->an_lock != NULL); 93143825Smarius 94143825Smarius /* 95143825Smarius * If the page is busy, mark it as PG_RELEASED, so 96 * that uvm_anon_release(9) would release it later. 97 */ 98 if ((pg->pg_flags & PG_BUSY) != 0) { 99 atomic_setbits_int(&pg->pg_flags, PG_RELEASED); 100 rw_obj_hold(anon->an_lock); 101 return; 102 } 103 pmap_page_protect(pg, PROT_NONE); 104 if (pgl != NULL) { 105 /* 106 * clean page, and put it on pglist 107 * for later freeing. 108 */ 109 uvm_lock_pageq(); 110 uvm_pageclean(pg); 111 uvm_unlock_pageq(); 112 TAILQ_INSERT_HEAD(pgl, pg, pageq); 113 } else { 114 uvm_lock_pageq(); /* lock out pagedaemon */ 115 uvm_pagefree(pg); /* bye bye */ 116 uvm_unlock_pageq(); /* free the daemon */ 117 } 118 } else { 119 if (anon->an_swslot != 0 && anon->an_swslot != SWSLOT_BAD) { 120 /* This page is no longer only in swap. */ 121 KASSERT(uvmexp.swpgonly > 0); 122 atomic_dec_int(&uvmexp.swpgonly); 123 } 124 } 125 anon->an_lock = NULL; 126 127 /* 128 * Free any swap resources, leave a page replacement hint. 129 */ 130 uvm_anon_dropswap(anon); 131 132 KASSERT(anon->an_page == NULL); 133 KASSERT(anon->an_swslot == 0); 134 135 pool_put(&uvm_anon_pool, anon); 136} 137 138/* 139 * uvm_anwait: wait for memory to become available to allocate an anon. 140 */ 141void 142uvm_anwait(void) 143{ 144 struct vm_anon *anon; 145 146 /* XXX: Want something like pool_wait()? */ 147 anon = pool_get(&uvm_anon_pool, PR_WAITOK); 148 pool_put(&uvm_anon_pool, anon); 149} 150 151/* 152 * uvm_anon_pagein: fetch an anon's page. 153 * 154 * => anon must be locked, and is unlocked upon return. 155 * => returns true if pagein was aborted due to lack of memory. 156 */ 157 158boolean_t 159uvm_anon_pagein(struct vm_amap *amap, struct vm_anon *anon) 160{ 161 struct vm_page *pg; 162 int rv; 163 164 KASSERT(rw_write_held(anon->an_lock)); 165 KASSERT(anon->an_lock == amap->am_lock); 166 167 /* 168 * Get the page of the anon. 169 */ 170 rv = uvmfault_anonget(NULL, amap, anon); 171 172 switch (rv) { 173 case VM_PAGER_OK: 174 KASSERT(rw_write_held(anon->an_lock)); 175 break; 176 177 case VM_PAGER_ERROR: 178 case VM_PAGER_REFAULT: 179 180 /* 181 * Nothing more to do on errors. 182 * VM_PAGER_REFAULT means that the anon was freed. 183 */ 184 185 return FALSE; 186 187 default: 188#ifdef DIAGNOSTIC 189 panic("anon_pagein: uvmfault_anonget -> %d", rv); 190#else 191 return FALSE; 192#endif 193 } 194 195 /* 196 * Mark the page as dirty and clear its swslot. 197 */ 198 pg = anon->an_page; 199 if (anon->an_swslot > 0) { 200 uvm_swap_free(anon->an_swslot, 1); 201 } 202 anon->an_swslot = 0; 203 atomic_clearbits_int(&pg->pg_flags, PG_CLEAN); 204 205 /* 206 * Deactivate the page (to put it on a page queue). 207 */ 208 pmap_clear_reference(pg); 209 pmap_page_protect(pg, PROT_NONE); 210 uvm_lock_pageq(); 211 uvm_pagedeactivate(pg); 212 uvm_unlock_pageq(); 213 rw_exit(anon->an_lock); 214 215 return FALSE; 216} 217 218/* 219 * uvm_anon_dropswap: release any swap resources from this anon. 220 * 221 * => anon must be locked or have a reference count of 0. 222 */ 223void 224uvm_anon_dropswap(struct vm_anon *anon) 225{ 226 KASSERT(anon->an_ref == 0 || rw_lock_held(anon->an_lock)); 227 228 if (anon->an_swslot == 0) 229 return; 230 231 uvm_swap_free(anon->an_swslot, 1); 232 anon->an_swslot = 0; 233} 234 235 236/* 237 * uvm_anon_release: release an anon and its page. 238 * 239 * => anon should not have any references. 240 * => anon must be locked. 241 */ 242 243void 244uvm_anon_release(struct vm_anon *anon) 245{ 246 struct vm_page *pg = anon->an_page; 247 struct rwlock *lock; 248 249 KASSERT(rw_write_held(anon->an_lock)); 250 KASSERT(pg != NULL); 251 KASSERT((pg->pg_flags & PG_RELEASED) != 0); 252 KASSERT((pg->pg_flags & PG_BUSY) != 0); 253 KASSERT(pg->uobject == NULL); 254 KASSERT(pg->uanon == anon); 255 KASSERT(anon->an_ref == 0); 256 257 uvm_lock_pageq(); 258 pmap_page_protect(pg, PROT_NONE); 259 uvm_pagefree(pg); 260 uvm_unlock_pageq(); 261 KASSERT(anon->an_page == NULL); 262 lock = anon->an_lock; 263 uvm_anon_dropswap(anon); 264 pool_put(&uvm_anon_pool, anon); 265 rw_exit(lock); 266 /* Note: extra reference is held for PG_RELEASED case. */ 267 rw_obj_free(lock); 268} 269