uvm_anon.c revision 1.33 1 1.33 yamt /* $NetBSD: uvm_anon.c,v 1.33 2005/05/11 13:02:25 yamt 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.21 lukem
39 1.21 lukem #include <sys/cdefs.h>
40 1.33 yamt __KERNEL_RCSID(0, "$NetBSD: uvm_anon.c,v 1.33 2005/05/11 13:02:25 yamt Exp $");
41 1.1 chuck
42 1.1 chuck #include "opt_uvmhist.h"
43 1.1 chuck
44 1.1 chuck #include <sys/param.h>
45 1.1 chuck #include <sys/systm.h>
46 1.1 chuck #include <sys/proc.h>
47 1.1 chuck #include <sys/malloc.h>
48 1.1 chuck #include <sys/pool.h>
49 1.5 chs #include <sys/kernel.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.33 yamt static POOL_INIT(uvm_anon_pool, sizeof(struct vm_anon), 0, 0, 0, "anonpl",
55 1.33 yamt &pool_allocator_nointr);
56 1.33 yamt static struct pool_cache uvm_anon_pool_cache;
57 1.5 chs
58 1.33 yamt static int uvm_anon_ctor(void *, void *, int);
59 1.5 chs
60 1.5 chs /*
61 1.1 chuck * allocate anons
62 1.1 chuck */
63 1.1 chuck void
64 1.1 chuck uvm_anon_init()
65 1.1 chuck {
66 1.5 chs
67 1.33 yamt pool_cache_init(&uvm_anon_pool_cache, &uvm_anon_pool,
68 1.33 yamt uvm_anon_ctor, NULL, NULL);
69 1.1 chuck }
70 1.1 chuck
71 1.33 yamt static int
72 1.33 yamt uvm_anon_ctor(void *arg, void *object, int flags)
73 1.1 chuck {
74 1.33 yamt struct vm_anon *anon = object;
75 1.1 chuck
76 1.33 yamt anon->an_ref = 0;
77 1.33 yamt simple_lock_init(&anon->an_lock);
78 1.33 yamt anon->an_page = NULL;
79 1.33 yamt anon->an_swslot = 0;
80 1.5 chs
81 1.11 chs return 0;
82 1.1 chuck }
83 1.1 chuck
84 1.1 chuck /*
85 1.1 chuck * allocate an anon
86 1.13 thorpej *
87 1.13 thorpej * => new anon is returned locked!
88 1.1 chuck */
89 1.1 chuck struct vm_anon *
90 1.1 chuck uvm_analloc()
91 1.1 chuck {
92 1.33 yamt struct vm_anon *anon;
93 1.1 chuck
94 1.33 yamt anon = pool_cache_get(&uvm_anon_pool_cache, PR_NOWAIT);
95 1.33 yamt if (anon) {
96 1.33 yamt KASSERT(anon->an_ref == 0);
97 1.33 yamt LOCK_ASSERT(simple_lock_held(&anon->an_lock) == 0);
98 1.33 yamt KASSERT(anon->an_page == NULL);
99 1.33 yamt KASSERT(anon->an_swslot == 0);
100 1.33 yamt anon->an_ref = 1;
101 1.33 yamt simple_lock(&anon->an_lock);
102 1.1 chuck }
103 1.33 yamt return anon;
104 1.1 chuck }
105 1.1 chuck
106 1.1 chuck /*
107 1.1 chuck * uvm_anfree: free a single anon structure
108 1.1 chuck *
109 1.1 chuck * => caller must remove anon from its amap before calling (if it was in
110 1.1 chuck * an amap).
111 1.1 chuck * => anon must be unlocked and have a zero reference count.
112 1.1 chuck * => we may lock the pageq's.
113 1.1 chuck */
114 1.20 chs
115 1.1 chuck void
116 1.1 chuck uvm_anfree(anon)
117 1.1 chuck struct vm_anon *anon;
118 1.1 chuck {
119 1.1 chuck struct vm_page *pg;
120 1.1 chuck UVMHIST_FUNC("uvm_anfree"); UVMHIST_CALLED(maphist);
121 1.1 chuck UVMHIST_LOG(maphist,"(anon=0x%x)", anon, 0,0,0);
122 1.1 chuck
123 1.12 thorpej KASSERT(anon->an_ref == 0);
124 1.18 chs LOCK_ASSERT(!simple_lock_held(&anon->an_lock));
125 1.12 thorpej
126 1.1 chuck /*
127 1.1 chuck * get page
128 1.1 chuck */
129 1.1 chuck
130 1.33 yamt pg = anon->an_page;
131 1.1 chuck
132 1.1 chuck /*
133 1.1 chuck * if there is a resident page and it is loaned, then anon may not
134 1.1 chuck * own it. call out to uvm_anon_lockpage() to ensure the real owner
135 1.1 chuck * of the page has been identified and locked.
136 1.1 chuck */
137 1.1 chuck
138 1.22 chs if (pg && pg->loan_count) {
139 1.22 chs simple_lock(&anon->an_lock);
140 1.1 chuck pg = uvm_anon_lockloanpg(anon);
141 1.22 chs simple_unlock(&anon->an_lock);
142 1.22 chs }
143 1.1 chuck
144 1.1 chuck /*
145 1.1 chuck * if we have a resident page, we must dispose of it before freeing
146 1.1 chuck * the anon.
147 1.1 chuck */
148 1.1 chuck
149 1.1 chuck if (pg) {
150 1.1 chuck
151 1.1 chuck /*
152 1.17 chs * if the page is owned by a uobject (now locked), then we must
153 1.1 chuck * kill the loan on the page rather than free it.
154 1.1 chuck */
155 1.1 chuck
156 1.1 chuck if (pg->uobject) {
157 1.1 chuck uvm_lock_pageq();
158 1.10 chs KASSERT(pg->loan_count > 0);
159 1.1 chuck pg->loan_count--;
160 1.1 chuck pg->uanon = NULL;
161 1.1 chuck uvm_unlock_pageq();
162 1.1 chuck simple_unlock(&pg->uobject->vmobjlock);
163 1.1 chuck } else {
164 1.1 chuck
165 1.1 chuck /*
166 1.1 chuck * page has no uobject, so we must be the owner of it.
167 1.1 chuck */
168 1.1 chuck
169 1.18 chs KASSERT((pg->flags & PG_RELEASED) == 0);
170 1.19 chs simple_lock(&anon->an_lock);
171 1.18 chs pmap_page_protect(pg, VM_PROT_NONE);
172 1.29 yamt
173 1.29 yamt /*
174 1.29 yamt * if the page is busy, mark it as PG_RELEASED
175 1.29 yamt * so that uvm_anon_release will release it later.
176 1.29 yamt */
177 1.29 yamt
178 1.29 yamt if (pg->flags & PG_BUSY) {
179 1.29 yamt pg->flags |= PG_RELEASED;
180 1.29 yamt simple_unlock(&anon->an_lock);
181 1.29 yamt return;
182 1.17 chs }
183 1.29 yamt uvm_lock_pageq();
184 1.29 yamt uvm_pagefree(pg);
185 1.29 yamt uvm_unlock_pageq();
186 1.19 chs simple_unlock(&anon->an_lock);
187 1.18 chs UVMHIST_LOG(maphist, "anon 0x%x, page 0x%x: "
188 1.18 chs "freed now!", anon, pg, 0, 0);
189 1.1 chuck }
190 1.1 chuck }
191 1.26 pk if (pg == NULL && anon->an_swslot > 0) {
192 1.20 chs /* this page is no longer only in swap. */
193 1.20 chs simple_lock(&uvm.swap_data_lock);
194 1.20 chs KASSERT(uvmexp.swpgonly > 0);
195 1.20 chs uvmexp.swpgonly--;
196 1.20 chs simple_unlock(&uvm.swap_data_lock);
197 1.20 chs }
198 1.1 chuck
199 1.1 chuck /*
200 1.2 chs * free any swap resources.
201 1.1 chuck */
202 1.18 chs
203 1.2 chs uvm_anon_dropswap(anon);
204 1.1 chuck
205 1.1 chuck /*
206 1.18 chs * now that we've stripped the data areas from the anon,
207 1.18 chs * free the anon itself.
208 1.1 chuck */
209 1.18 chs
210 1.33 yamt KASSERT(anon->an_page == NULL);
211 1.31 yamt KASSERT(anon->an_swslot == 0);
212 1.31 yamt
213 1.33 yamt pool_cache_put(&uvm_anon_pool_cache, anon);
214 1.1 chuck UVMHIST_LOG(maphist,"<- done!",0,0,0,0);
215 1.2 chs }
216 1.2 chs
217 1.2 chs /*
218 1.2 chs * uvm_anon_dropswap: release any swap resources from this anon.
219 1.17 chs *
220 1.2 chs * => anon must be locked or have a reference count of 0.
221 1.2 chs */
222 1.2 chs void
223 1.2 chs uvm_anon_dropswap(anon)
224 1.2 chs struct vm_anon *anon;
225 1.2 chs {
226 1.2 chs UVMHIST_FUNC("uvm_anon_dropswap"); UVMHIST_CALLED(maphist);
227 1.12 thorpej
228 1.12 thorpej if (anon->an_swslot == 0)
229 1.2 chs return;
230 1.2 chs
231 1.2 chs UVMHIST_LOG(maphist,"freeing swap for anon %p, paged to swslot 0x%x",
232 1.2 chs anon, anon->an_swslot, 0, 0);
233 1.2 chs uvm_swap_free(anon->an_swslot, 1);
234 1.2 chs anon->an_swslot = 0;
235 1.1 chuck }
236 1.1 chuck
237 1.1 chuck /*
238 1.1 chuck * uvm_anon_lockloanpg: given a locked anon, lock its resident page
239 1.1 chuck *
240 1.1 chuck * => anon is locked by caller
241 1.1 chuck * => on return: anon is locked
242 1.1 chuck * if there is a resident page:
243 1.1 chuck * if it has a uobject, it is locked by us
244 1.1 chuck * if it is ownerless, we take over as owner
245 1.1 chuck * we return the resident page (it can change during
246 1.1 chuck * this function)
247 1.1 chuck * => note that the only time an anon has an ownerless resident page
248 1.1 chuck * is if the page was loaned from a uvm_object and the uvm_object
249 1.1 chuck * disowned it
250 1.1 chuck * => this only needs to be called when you want to do an operation
251 1.1 chuck * on an anon's resident page and that page has a non-zero loan
252 1.1 chuck * count.
253 1.1 chuck */
254 1.1 chuck struct vm_page *
255 1.1 chuck uvm_anon_lockloanpg(anon)
256 1.1 chuck struct vm_anon *anon;
257 1.1 chuck {
258 1.1 chuck struct vm_page *pg;
259 1.1 chuck boolean_t locked = FALSE;
260 1.1 chuck
261 1.12 thorpej LOCK_ASSERT(simple_lock_held(&anon->an_lock));
262 1.12 thorpej
263 1.1 chuck /*
264 1.1 chuck * loop while we have a resident page that has a non-zero loan count.
265 1.1 chuck * if we successfully get our lock, we will "break" the loop.
266 1.1 chuck * note that the test for pg->loan_count is not protected -- this
267 1.1 chuck * may produce false positive results. note that a false positive
268 1.1 chuck * result may cause us to do more work than we need to, but it will
269 1.1 chuck * not produce an incorrect result.
270 1.1 chuck */
271 1.1 chuck
272 1.33 yamt while (((pg = anon->an_page) != NULL) && pg->loan_count != 0) {
273 1.1 chuck
274 1.1 chuck /*
275 1.1 chuck * quickly check to see if the page has an object before
276 1.1 chuck * bothering to lock the page queues. this may also produce
277 1.1 chuck * a false positive result, but that's ok because we do a real
278 1.1 chuck * check after that.
279 1.1 chuck */
280 1.1 chuck
281 1.1 chuck if (pg->uobject) {
282 1.1 chuck uvm_lock_pageq();
283 1.18 chs if (pg->uobject) {
284 1.1 chuck locked =
285 1.1 chuck simple_lock_try(&pg->uobject->vmobjlock);
286 1.1 chuck } else {
287 1.1 chuck /* object disowned before we got PQ lock */
288 1.1 chuck locked = TRUE;
289 1.1 chuck }
290 1.1 chuck uvm_unlock_pageq();
291 1.1 chuck
292 1.1 chuck /*
293 1.1 chuck * if we didn't get a lock (try lock failed), then we
294 1.1 chuck * toggle our anon lock and try again
295 1.1 chuck */
296 1.1 chuck
297 1.1 chuck if (!locked) {
298 1.1 chuck simple_unlock(&anon->an_lock);
299 1.10 chs
300 1.1 chuck /*
301 1.1 chuck * someone locking the object has a chance to
302 1.1 chuck * lock us right now
303 1.1 chuck */
304 1.10 chs
305 1.1 chuck simple_lock(&anon->an_lock);
306 1.10 chs continue;
307 1.1 chuck }
308 1.1 chuck }
309 1.1 chuck
310 1.1 chuck /*
311 1.1 chuck * if page is un-owned [i.e. the object dropped its ownership],
312 1.1 chuck * then we can take over as owner!
313 1.1 chuck */
314 1.1 chuck
315 1.1 chuck if (pg->uobject == NULL && (pg->pqflags & PQ_ANON) == 0) {
316 1.1 chuck uvm_lock_pageq();
317 1.18 chs pg->pqflags |= PQ_ANON;
318 1.18 chs pg->loan_count--;
319 1.1 chuck uvm_unlock_pageq();
320 1.1 chuck }
321 1.1 chuck break;
322 1.1 chuck }
323 1.1 chuck return(pg);
324 1.5 chs }
325 1.5 chs
326 1.5 chs /*
327 1.5 chs * fetch an anon's page.
328 1.5 chs *
329 1.5 chs * => anon must be locked, and is unlocked upon return.
330 1.5 chs * => returns TRUE if pagein was aborted due to lack of memory.
331 1.5 chs */
332 1.5 chs
333 1.33 yamt boolean_t
334 1.33 yamt uvm_anon_pagein(anon)
335 1.5 chs struct vm_anon *anon;
336 1.5 chs {
337 1.5 chs struct vm_page *pg;
338 1.5 chs struct uvm_object *uobj;
339 1.5 chs int rv;
340 1.8 thorpej
341 1.5 chs /* locked: anon */
342 1.12 thorpej LOCK_ASSERT(simple_lock_held(&anon->an_lock));
343 1.12 thorpej
344 1.5 chs rv = uvmfault_anonget(NULL, NULL, anon);
345 1.12 thorpej
346 1.8 thorpej /*
347 1.16 chs * if rv == 0, anon is still locked, else anon
348 1.8 thorpej * is unlocked
349 1.8 thorpej */
350 1.5 chs
351 1.5 chs switch (rv) {
352 1.16 chs case 0:
353 1.5 chs break;
354 1.5 chs
355 1.16 chs case EIO:
356 1.16 chs case ERESTART:
357 1.5 chs
358 1.5 chs /*
359 1.5 chs * nothing more to do on errors.
360 1.16 chs * ERESTART can only mean that the anon was freed,
361 1.5 chs * so again there's nothing to do.
362 1.5 chs */
363 1.5 chs
364 1.5 chs return FALSE;
365 1.25 pk
366 1.25 pk default:
367 1.25 pk return TRUE;
368 1.5 chs }
369 1.5 chs
370 1.5 chs /*
371 1.5 chs * ok, we've got the page now.
372 1.5 chs * mark it as dirty, clear its swslot and un-busy it.
373 1.5 chs */
374 1.5 chs
375 1.33 yamt pg = anon->an_page;
376 1.5 chs uobj = pg->uobject;
377 1.23 pk if (anon->an_swslot > 0)
378 1.23 pk uvm_swap_free(anon->an_swslot, 1);
379 1.5 chs anon->an_swslot = 0;
380 1.5 chs pg->flags &= ~(PG_CLEAN);
381 1.5 chs
382 1.5 chs /*
383 1.5 chs * deactivate the page (to put it on a page queue)
384 1.5 chs */
385 1.5 chs
386 1.5 chs pmap_clear_reference(pg);
387 1.5 chs uvm_lock_pageq();
388 1.24 pk if (pg->wire_count == 0)
389 1.24 pk uvm_pagedeactivate(pg);
390 1.5 chs uvm_unlock_pageq();
391 1.25 pk
392 1.25 pk if (pg->flags & PG_WANTED) {
393 1.25 pk wakeup(pg);
394 1.25 pk pg->flags &= ~(PG_WANTED);
395 1.25 pk }
396 1.5 chs
397 1.5 chs /*
398 1.5 chs * unlock the anon and we're done.
399 1.5 chs */
400 1.5 chs
401 1.5 chs simple_unlock(&anon->an_lock);
402 1.5 chs if (uobj) {
403 1.5 chs simple_unlock(&uobj->vmobjlock);
404 1.5 chs }
405 1.5 chs return FALSE;
406 1.1 chuck }
407 1.29 yamt
408 1.29 yamt /*
409 1.29 yamt * uvm_anon_release: release an anon and its page.
410 1.29 yamt *
411 1.29 yamt * => caller must lock the anon.
412 1.29 yamt */
413 1.29 yamt
414 1.29 yamt void
415 1.29 yamt uvm_anon_release(anon)
416 1.29 yamt struct vm_anon *anon;
417 1.29 yamt {
418 1.33 yamt struct vm_page *pg = anon->an_page;
419 1.29 yamt
420 1.29 yamt LOCK_ASSERT(simple_lock_held(&anon->an_lock));
421 1.29 yamt
422 1.29 yamt KASSERT(pg != NULL);
423 1.29 yamt KASSERT((pg->flags & PG_RELEASED) != 0);
424 1.29 yamt KASSERT((pg->flags & PG_BUSY) != 0);
425 1.29 yamt KASSERT(pg->uobject == NULL);
426 1.29 yamt KASSERT(pg->uanon == anon);
427 1.29 yamt KASSERT(pg->loan_count == 0);
428 1.29 yamt KASSERT(anon->an_ref == 0);
429 1.29 yamt
430 1.29 yamt uvm_lock_pageq();
431 1.29 yamt uvm_pagefree(pg);
432 1.29 yamt uvm_unlock_pageq();
433 1.29 yamt simple_unlock(&anon->an_lock);
434 1.29 yamt
435 1.33 yamt KASSERT(anon->an_page == NULL);
436 1.29 yamt
437 1.29 yamt uvm_anfree(anon);
438 1.29 yamt }
439