uvm_anon.c revision 1.1 1 /* $NetBSD: uvm_anon.c,v 1.1 1999/01/24 23:53:15 chuck 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 "opt_uvmhist.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/proc.h>
44 #include <sys/malloc.h>
45 #include <sys/pool.h>
46
47 #include <vm/vm.h>
48 #include <vm/vm_page.h>
49 #include <vm/vm_kern.h>
50
51 #include <uvm/uvm.h>
52 #include <uvm/uvm_swap.h>
53
54 /*
55 * allocate anons
56 */
57 void
58 uvm_anon_init()
59 {
60 struct vm_anon *anon;
61 int nanon = uvmexp.free - (uvmexp.free / 16); /* XXXCDC ??? */
62 int lcv;
63
64 /*
65 * Allocate the initial anons.
66 */
67 anon = (struct vm_anon *)uvm_km_alloc(kernel_map,
68 sizeof(*anon) * nanon);
69 if (anon == NULL) {
70 printf("uvm_anon_init: can not allocate %d anons\n", nanon);
71 panic("uvm_anon_init");
72 }
73
74 memset(anon, 0, sizeof(*anon) * nanon);
75 uvm.afree = NULL;
76 uvmexp.nanon = uvmexp.nfreeanon = nanon;
77 for (lcv = 0 ; lcv < nanon ; lcv++) {
78 anon[lcv].u.an_nxt = uvm.afree;
79 uvm.afree = &anon[lcv];
80 }
81 simple_lock_init(&uvm.afreelock);
82 }
83
84 /*
85 * add some more anons to the free pool. called when we add
86 * more swap space.
87 */
88 void
89 uvm_anon_add(pages)
90 int pages;
91 {
92 struct vm_anon *anon;
93 int lcv;
94
95 anon = (struct vm_anon *)uvm_km_alloc(kernel_map,
96 sizeof(*anon) * pages);
97
98 /* XXX Should wait for VM to free up. */
99 if (anon == NULL) {
100 printf("uvm_anon_add: can not allocate %d anons\n", pages);
101 panic("uvm_anon_add");
102 }
103
104 simple_lock(&uvm.afreelock);
105 memset(anon, 0, sizeof(*anon) * pages);
106 uvmexp.nanon += pages;
107 uvmexp.nfreeanon += pages;
108 for (lcv = 0; lcv < pages; lcv++) {
109 simple_lock_init(&anon->an_lock);
110 anon[lcv].u.an_nxt = uvm.afree;
111 uvm.afree = &anon[lcv];
112 }
113 simple_unlock(&uvm.afreelock);
114 }
115
116 /*
117 * allocate an anon
118 */
119 struct vm_anon *
120 uvm_analloc()
121 {
122 struct vm_anon *a;
123
124 simple_lock(&uvm.afreelock);
125 a = uvm.afree;
126 if (a) {
127 uvm.afree = a->u.an_nxt;
128 uvmexp.nfreeanon--;
129 a->an_ref = 1;
130 a->an_swslot = 0;
131 a->u.an_page = NULL; /* so we can free quickly */
132 }
133 simple_unlock(&uvm.afreelock);
134 return(a);
135 }
136
137 /*
138 * uvm_anfree: free a single anon structure
139 *
140 * => caller must remove anon from its amap before calling (if it was in
141 * an amap).
142 * => anon must be unlocked and have a zero reference count.
143 * => we may lock the pageq's.
144 */
145 void
146 uvm_anfree(anon)
147 struct vm_anon *anon;
148 {
149 struct vm_page *pg;
150 UVMHIST_FUNC("uvm_anfree"); UVMHIST_CALLED(maphist);
151 UVMHIST_LOG(maphist,"(anon=0x%x)", anon, 0,0,0);
152
153 /*
154 * get page
155 */
156
157 pg = anon->u.an_page;
158
159 /*
160 * if there is a resident page and it is loaned, then anon may not
161 * own it. call out to uvm_anon_lockpage() to ensure the real owner
162 * of the page has been identified and locked.
163 */
164
165 if (pg && pg->loan_count)
166 pg = uvm_anon_lockloanpg(anon);
167
168 /*
169 * if we have a resident page, we must dispose of it before freeing
170 * the anon.
171 */
172
173 if (pg) {
174
175 /*
176 * if the page is owned by a uobject (now locked), then we must
177 * kill the loan on the page rather than free it.
178 */
179
180 if (pg->uobject) {
181
182 /* kill loan */
183 uvm_lock_pageq();
184 #ifdef DIAGNOSTIC
185 if (pg->loan_count < 1)
186 panic("uvm_anfree: obj owned page "
187 "with no loan count");
188 #endif
189 pg->loan_count--;
190 pg->uanon = NULL;
191 uvm_unlock_pageq();
192 simple_unlock(&pg->uobject->vmobjlock);
193
194 } else {
195
196 /*
197 * page has no uobject, so we must be the owner of it.
198 *
199 * if page is busy then we just mark it as released
200 * (who ever has it busy must check for this when they
201 * wake up). if the page is not busy then we can
202 * free it now.
203 */
204
205 if ((pg->flags & PG_BUSY) != 0) {
206 /* tell them to dump it when done */
207 pg->flags |= PG_RELEASED;
208 simple_unlock(&anon->an_lock);
209 UVMHIST_LOG(maphist,
210 " anon 0x%x, page 0x%x: BUSY (released!)",
211 anon, pg, 0, 0);
212 return;
213 }
214
215 pmap_page_protect(PMAP_PGARG(pg), VM_PROT_NONE);
216 uvm_lock_pageq(); /* lock out pagedaemon */
217 uvm_pagefree(pg); /* bye bye */
218 uvm_unlock_pageq(); /* free the daemon */
219
220 UVMHIST_LOG(maphist," anon 0x%x, page 0x%x: freed now!",
221 anon, pg, 0, 0);
222 }
223 }
224
225 /*
226 * are we using any backing store resources? if so, free them.
227 */
228 if (anon->an_swslot) {
229 /*
230 * on backing store: no I/O in progress. sole amap reference
231 * is ours and we've got it locked down. thus we can free,
232 * and be done.
233 */
234 UVMHIST_LOG(maphist," freeing anon 0x%x, paged to swslot 0x%x",
235 anon, anon->an_swslot, 0, 0);
236 uvm_swap_free(anon->an_swslot, 1);
237 anon->an_swslot = 0;
238 }
239
240 /*
241 * now that we've stripped the data areas from the anon, free the anon
242 * itself!
243 */
244 simple_lock(&uvm.afreelock);
245 anon->u.an_nxt = uvm.afree;
246 uvm.afree = anon;
247 uvmexp.nfreeanon++;
248 simple_unlock(&uvm.afreelock);
249 UVMHIST_LOG(maphist,"<- done!",0,0,0,0);
250 }
251
252 /*
253 * uvm_anon_lockloanpg: given a locked anon, lock its resident page
254 *
255 * => anon is locked by caller
256 * => on return: anon is locked
257 * if there is a resident page:
258 * if it has a uobject, it is locked by us
259 * if it is ownerless, we take over as owner
260 * we return the resident page (it can change during
261 * this function)
262 * => note that the only time an anon has an ownerless resident page
263 * is if the page was loaned from a uvm_object and the uvm_object
264 * disowned it
265 * => this only needs to be called when you want to do an operation
266 * on an anon's resident page and that page has a non-zero loan
267 * count.
268 */
269 struct vm_page *
270 uvm_anon_lockloanpg(anon)
271 struct vm_anon *anon;
272 {
273 struct vm_page *pg;
274 boolean_t locked = FALSE;
275
276 /*
277 * loop while we have a resident page that has a non-zero loan count.
278 * if we successfully get our lock, we will "break" the loop.
279 * note that the test for pg->loan_count is not protected -- this
280 * may produce false positive results. note that a false positive
281 * result may cause us to do more work than we need to, but it will
282 * not produce an incorrect result.
283 */
284
285 while (((pg = anon->u.an_page) != NULL) && pg->loan_count != 0) {
286
287 /*
288 * quickly check to see if the page has an object before
289 * bothering to lock the page queues. this may also produce
290 * a false positive result, but that's ok because we do a real
291 * check after that.
292 *
293 * XXX: quick check -- worth it? need volatile?
294 */
295
296 if (pg->uobject) {
297
298 uvm_lock_pageq();
299 if (pg->uobject) { /* the "real" check */
300 locked =
301 simple_lock_try(&pg->uobject->vmobjlock);
302 } else {
303 /* object disowned before we got PQ lock */
304 locked = TRUE;
305 }
306 uvm_unlock_pageq();
307
308 /*
309 * if we didn't get a lock (try lock failed), then we
310 * toggle our anon lock and try again
311 */
312
313 if (!locked) {
314 simple_unlock(&anon->an_lock);
315 /*
316 * someone locking the object has a chance to
317 * lock us right now
318 */
319 simple_lock(&anon->an_lock);
320 continue; /* start over */
321 }
322 }
323
324 /*
325 * if page is un-owned [i.e. the object dropped its ownership],
326 * then we can take over as owner!
327 */
328
329 if (pg->uobject == NULL && (pg->pqflags & PQ_ANON) == 0) {
330 uvm_lock_pageq();
331 pg->pqflags |= PQ_ANON; /* take ownership... */
332 pg->loan_count--; /* ... and drop our loan */
333 uvm_unlock_pageq();
334 }
335
336 /*
337 * we did it! break the loop
338 */
339 break;
340 }
341
342 /*
343 * done!
344 */
345
346 return(pg);
347 }
348