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