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