uvm_anon.c revision 1.54 1 1.54 rmind /* $NetBSD: uvm_anon.c,v 1.54 2011/06/12 03:36:02 rmind Exp $ */
2 1.1 chuck
3 1.1 chuck /*
4 1.1 chuck * Copyright (c) 1997 Charles D. Cranor and Washington University.
5 1.1 chuck * All rights reserved.
6 1.1 chuck *
7 1.1 chuck * Redistribution and use in source and binary forms, with or without
8 1.1 chuck * modification, are permitted provided that the following conditions
9 1.1 chuck * are met:
10 1.1 chuck * 1. Redistributions of source code must retain the above copyright
11 1.1 chuck * notice, this list of conditions and the following disclaimer.
12 1.1 chuck * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 chuck * notice, this list of conditions and the following disclaimer in the
14 1.1 chuck * documentation and/or other materials provided with the distribution.
15 1.1 chuck *
16 1.1 chuck * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 chuck * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 chuck * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 chuck * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 chuck * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 chuck * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 chuck * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 chuck * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 chuck * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1 chuck * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 chuck */
27 1.1 chuck
28 1.1 chuck /*
29 1.1 chuck * uvm_anon.c: uvm anon ops
30 1.1 chuck */
31 1.21 lukem
32 1.21 lukem #include <sys/cdefs.h>
33 1.54 rmind __KERNEL_RCSID(0, "$NetBSD: uvm_anon.c,v 1.54 2011/06/12 03:36:02 rmind Exp $");
34 1.1 chuck
35 1.1 chuck #include "opt_uvmhist.h"
36 1.1 chuck
37 1.1 chuck #include <sys/param.h>
38 1.1 chuck #include <sys/systm.h>
39 1.1 chuck #include <sys/pool.h>
40 1.5 chs #include <sys/kernel.h>
41 1.1 chuck
42 1.1 chuck #include <uvm/uvm.h>
43 1.1 chuck #include <uvm/uvm_swap.h>
44 1.39 yamt #include <uvm/uvm_pdpolicy.h>
45 1.1 chuck
46 1.47 ad static struct pool_cache uvm_anon_cache;
47 1.5 chs
48 1.33 yamt static int uvm_anon_ctor(void *, void *, int);
49 1.47 ad static void uvm_anon_dtor(void *, void *);
50 1.5 chs
51 1.5 chs /*
52 1.1 chuck * allocate anons
53 1.1 chuck */
54 1.1 chuck void
55 1.34 thorpej uvm_anon_init(void)
56 1.1 chuck {
57 1.5 chs
58 1.49 ad pool_cache_bootstrap(&uvm_anon_cache, sizeof(struct vm_anon), 0, 0,
59 1.49 ad PR_LARGECACHE, "anonpl", NULL, IPL_NONE, uvm_anon_ctor,
60 1.49 ad uvm_anon_dtor, NULL);
61 1.1 chuck }
62 1.1 chuck
63 1.33 yamt static int
64 1.41 yamt uvm_anon_ctor(void *arg, void *object, int flags)
65 1.1 chuck {
66 1.33 yamt struct vm_anon *anon = object;
67 1.1 chuck
68 1.33 yamt anon->an_ref = 0;
69 1.33 yamt anon->an_page = NULL;
70 1.37 yamt #if defined(VMSWAP)
71 1.33 yamt anon->an_swslot = 0;
72 1.37 yamt #endif /* defined(VMSWAP) */
73 1.5 chs
74 1.11 chs return 0;
75 1.1 chuck }
76 1.1 chuck
77 1.47 ad static void
78 1.47 ad uvm_anon_dtor(void *arg, void *object)
79 1.47 ad {
80 1.47 ad
81 1.47 ad }
82 1.47 ad
83 1.1 chuck /*
84 1.1 chuck * allocate an anon
85 1.13 thorpej *
86 1.13 thorpej * => new anon is returned locked!
87 1.1 chuck */
88 1.1 chuck struct vm_anon *
89 1.34 thorpej uvm_analloc(void)
90 1.1 chuck {
91 1.33 yamt struct vm_anon *anon;
92 1.1 chuck
93 1.47 ad anon = pool_cache_get(&uvm_anon_cache, PR_NOWAIT);
94 1.33 yamt if (anon) {
95 1.33 yamt KASSERT(anon->an_ref == 0);
96 1.33 yamt KASSERT(anon->an_page == NULL);
97 1.37 yamt #if defined(VMSWAP)
98 1.33 yamt KASSERT(anon->an_swslot == 0);
99 1.37 yamt #endif /* defined(VMSWAP) */
100 1.33 yamt anon->an_ref = 1;
101 1.54 rmind anon->an_lock = NULL;
102 1.1 chuck }
103 1.33 yamt return anon;
104 1.1 chuck }
105 1.1 chuck
106 1.1 chuck /*
107 1.54 rmind * uvm_anfree: free a linked list of anon structures
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.54 rmind * => amap must be locked, or anon must not be associated with a lock
112 1.54 rmind * or any other objects.
113 1.1 chuck * => we may lock the pageq's.
114 1.54 rmind * => we may drop and re-acquire amap lock
115 1.1 chuck */
116 1.20 chs
117 1.54 rmind static void
118 1.54 rmind uvm_anfree1(struct vm_anon *anon)
119 1.1 chuck {
120 1.1 chuck struct vm_page *pg;
121 1.1 chuck UVMHIST_FUNC("uvm_anfree"); UVMHIST_CALLED(maphist);
122 1.1 chuck UVMHIST_LOG(maphist,"(anon=0x%x)", anon, 0,0,0);
123 1.1 chuck
124 1.54 rmind KASSERT(anon->an_lock == NULL || mutex_owned(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.54 rmind KASSERT(anon->an_lock != NULL);
140 1.1 chuck pg = uvm_anon_lockloanpg(anon);
141 1.22 chs }
142 1.1 chuck
143 1.1 chuck /*
144 1.1 chuck * if we have a resident page, we must dispose of it before freeing
145 1.1 chuck * the anon.
146 1.1 chuck */
147 1.1 chuck
148 1.1 chuck if (pg) {
149 1.54 rmind KASSERT(anon->an_lock != NULL);
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.50 ad mutex_enter(&uvm_pageqlock);
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.50 ad mutex_exit(&uvm_pageqlock);
162 1.54 rmind mutex_exit(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.18 chs pmap_page_protect(pg, VM_PROT_NONE);
171 1.29 yamt
172 1.29 yamt /*
173 1.29 yamt * if the page is busy, mark it as PG_RELEASED
174 1.29 yamt * so that uvm_anon_release will release it later.
175 1.29 yamt */
176 1.29 yamt
177 1.29 yamt if (pg->flags & PG_BUSY) {
178 1.29 yamt pg->flags |= PG_RELEASED;
179 1.29 yamt return;
180 1.17 chs }
181 1.50 ad mutex_enter(&uvm_pageqlock);
182 1.29 yamt uvm_pagefree(pg);
183 1.50 ad mutex_exit(&uvm_pageqlock);
184 1.18 chs UVMHIST_LOG(maphist, "anon 0x%x, page 0x%x: "
185 1.18 chs "freed now!", anon, pg, 0, 0);
186 1.1 chuck }
187 1.1 chuck }
188 1.37 yamt #if defined(VMSWAP)
189 1.26 pk if (pg == NULL && anon->an_swslot > 0) {
190 1.20 chs /* this page is no longer only in swap. */
191 1.45 ad mutex_enter(&uvm_swap_data_lock);
192 1.20 chs KASSERT(uvmexp.swpgonly > 0);
193 1.20 chs uvmexp.swpgonly--;
194 1.45 ad mutex_exit(&uvm_swap_data_lock);
195 1.20 chs }
196 1.37 yamt #endif /* defined(VMSWAP) */
197 1.1 chuck
198 1.1 chuck /*
199 1.2 chs * free any swap resources.
200 1.1 chuck */
201 1.18 chs
202 1.2 chs uvm_anon_dropswap(anon);
203 1.1 chuck
204 1.1 chuck /*
205 1.39 yamt * give a page replacement hint.
206 1.39 yamt */
207 1.39 yamt
208 1.39 yamt uvmpdpol_anfree(anon);
209 1.39 yamt
210 1.39 yamt /*
211 1.18 chs * now that we've stripped the data areas from the anon,
212 1.18 chs * free the anon itself.
213 1.1 chuck */
214 1.18 chs
215 1.33 yamt KASSERT(anon->an_page == NULL);
216 1.37 yamt #if defined(VMSWAP)
217 1.31 yamt KASSERT(anon->an_swslot == 0);
218 1.37 yamt #endif /* defined(VMSWAP) */
219 1.31 yamt
220 1.54 rmind if (anon->an_lock != NULL) {
221 1.54 rmind mutex_obj_free(anon->an_lock);
222 1.54 rmind }
223 1.47 ad pool_cache_put(&uvm_anon_cache, anon);
224 1.1 chuck UVMHIST_LOG(maphist,"<- done!",0,0,0,0);
225 1.2 chs }
226 1.2 chs
227 1.54 rmind void
228 1.54 rmind uvm_anfree(struct vm_anon *anon)
229 1.54 rmind {
230 1.54 rmind struct vm_anon *next;
231 1.54 rmind
232 1.54 rmind for (; anon != NULL; anon = next) {
233 1.54 rmind next = anon->an_link;
234 1.54 rmind anon->an_link = NULL; /* also clears reference count */
235 1.54 rmind uvm_anfree1(anon);
236 1.54 rmind }
237 1.54 rmind }
238 1.54 rmind
239 1.37 yamt #if defined(VMSWAP)
240 1.37 yamt
241 1.2 chs /*
242 1.2 chs * uvm_anon_dropswap: release any swap resources from this anon.
243 1.17 chs *
244 1.2 chs * => anon must be locked or have a reference count of 0.
245 1.2 chs */
246 1.2 chs void
247 1.34 thorpej uvm_anon_dropswap(struct vm_anon *anon)
248 1.2 chs {
249 1.2 chs UVMHIST_FUNC("uvm_anon_dropswap"); UVMHIST_CALLED(maphist);
250 1.12 thorpej
251 1.12 thorpej if (anon->an_swslot == 0)
252 1.2 chs return;
253 1.2 chs
254 1.2 chs UVMHIST_LOG(maphist,"freeing swap for anon %p, paged to swslot 0x%x",
255 1.2 chs anon, anon->an_swslot, 0, 0);
256 1.2 chs uvm_swap_free(anon->an_swslot, 1);
257 1.2 chs anon->an_swslot = 0;
258 1.1 chuck }
259 1.1 chuck
260 1.37 yamt #endif /* defined(VMSWAP) */
261 1.37 yamt
262 1.1 chuck /*
263 1.1 chuck * uvm_anon_lockloanpg: given a locked anon, lock its resident page
264 1.1 chuck *
265 1.1 chuck * => anon is locked by caller
266 1.1 chuck * => on return: anon is locked
267 1.1 chuck * if there is a resident page:
268 1.1 chuck * if it has a uobject, it is locked by us
269 1.1 chuck * if it is ownerless, we take over as owner
270 1.1 chuck * we return the resident page (it can change during
271 1.1 chuck * this function)
272 1.1 chuck * => note that the only time an anon has an ownerless resident page
273 1.1 chuck * is if the page was loaned from a uvm_object and the uvm_object
274 1.1 chuck * disowned it
275 1.1 chuck * => this only needs to be called when you want to do an operation
276 1.1 chuck * on an anon's resident page and that page has a non-zero loan
277 1.1 chuck * count.
278 1.1 chuck */
279 1.1 chuck struct vm_page *
280 1.34 thorpej uvm_anon_lockloanpg(struct vm_anon *anon)
281 1.1 chuck {
282 1.1 chuck struct vm_page *pg;
283 1.43 thorpej bool locked = false;
284 1.1 chuck
285 1.54 rmind KASSERT(mutex_owned(anon->an_lock));
286 1.12 thorpej
287 1.1 chuck /*
288 1.1 chuck * loop while we have a resident page that has a non-zero loan count.
289 1.1 chuck * if we successfully get our lock, we will "break" the loop.
290 1.1 chuck * note that the test for pg->loan_count is not protected -- this
291 1.1 chuck * may produce false positive results. note that a false positive
292 1.1 chuck * result may cause us to do more work than we need to, but it will
293 1.1 chuck * not produce an incorrect result.
294 1.1 chuck */
295 1.1 chuck
296 1.33 yamt while (((pg = anon->an_page) != NULL) && pg->loan_count != 0) {
297 1.1 chuck
298 1.1 chuck /*
299 1.1 chuck * quickly check to see if the page has an object before
300 1.1 chuck * bothering to lock the page queues. this may also produce
301 1.1 chuck * a false positive result, but that's ok because we do a real
302 1.1 chuck * check after that.
303 1.1 chuck */
304 1.1 chuck
305 1.1 chuck if (pg->uobject) {
306 1.50 ad mutex_enter(&uvm_pageqlock);
307 1.18 chs if (pg->uobject) {
308 1.1 chuck locked =
309 1.54 rmind mutex_tryenter(pg->uobject->vmobjlock);
310 1.1 chuck } else {
311 1.1 chuck /* object disowned before we got PQ lock */
312 1.43 thorpej locked = true;
313 1.1 chuck }
314 1.50 ad mutex_exit(&uvm_pageqlock);
315 1.1 chuck
316 1.1 chuck /*
317 1.1 chuck * if we didn't get a lock (try lock failed), then we
318 1.1 chuck * toggle our anon lock and try again
319 1.1 chuck */
320 1.1 chuck
321 1.1 chuck if (!locked) {
322 1.1 chuck /*
323 1.1 chuck * someone locking the object has a chance to
324 1.1 chuck * lock us right now
325 1.54 rmind *
326 1.54 rmind * XXX Better than yielding but inadequate.
327 1.1 chuck */
328 1.54 rmind kpause("livelock", false, 1, anon->an_lock);
329 1.10 chs continue;
330 1.1 chuck }
331 1.1 chuck }
332 1.1 chuck
333 1.1 chuck /*
334 1.1 chuck * if page is un-owned [i.e. the object dropped its ownership],
335 1.1 chuck * then we can take over as owner!
336 1.1 chuck */
337 1.1 chuck
338 1.1 chuck if (pg->uobject == NULL && (pg->pqflags & PQ_ANON) == 0) {
339 1.50 ad mutex_enter(&uvm_pageqlock);
340 1.18 chs pg->pqflags |= PQ_ANON;
341 1.18 chs pg->loan_count--;
342 1.50 ad mutex_exit(&uvm_pageqlock);
343 1.1 chuck }
344 1.1 chuck break;
345 1.1 chuck }
346 1.1 chuck return(pg);
347 1.5 chs }
348 1.5 chs
349 1.37 yamt #if defined(VMSWAP)
350 1.37 yamt
351 1.5 chs /*
352 1.5 chs * fetch an anon's page.
353 1.5 chs *
354 1.5 chs * => anon must be locked, and is unlocked upon return.
355 1.43 thorpej * => returns true if pagein was aborted due to lack of memory.
356 1.5 chs */
357 1.5 chs
358 1.42 thorpej bool
359 1.34 thorpej uvm_anon_pagein(struct vm_anon *anon)
360 1.5 chs {
361 1.5 chs struct vm_page *pg;
362 1.5 chs struct uvm_object *uobj;
363 1.5 chs int rv;
364 1.8 thorpej
365 1.5 chs /* locked: anon */
366 1.54 rmind KASSERT(mutex_owned(anon->an_lock));
367 1.12 thorpej
368 1.5 chs rv = uvmfault_anonget(NULL, NULL, anon);
369 1.12 thorpej
370 1.8 thorpej /*
371 1.16 chs * if rv == 0, anon is still locked, else anon
372 1.8 thorpej * is unlocked
373 1.8 thorpej */
374 1.5 chs
375 1.5 chs switch (rv) {
376 1.16 chs case 0:
377 1.5 chs break;
378 1.5 chs
379 1.16 chs case EIO:
380 1.16 chs case ERESTART:
381 1.5 chs
382 1.5 chs /*
383 1.5 chs * nothing more to do on errors.
384 1.16 chs * ERESTART can only mean that the anon was freed,
385 1.5 chs * so again there's nothing to do.
386 1.5 chs */
387 1.5 chs
388 1.43 thorpej return false;
389 1.25 pk
390 1.25 pk default:
391 1.43 thorpej return true;
392 1.5 chs }
393 1.5 chs
394 1.5 chs /*
395 1.5 chs * ok, we've got the page now.
396 1.5 chs * mark it as dirty, clear its swslot and un-busy it.
397 1.5 chs */
398 1.5 chs
399 1.33 yamt pg = anon->an_page;
400 1.5 chs uobj = pg->uobject;
401 1.23 pk if (anon->an_swslot > 0)
402 1.23 pk uvm_swap_free(anon->an_swslot, 1);
403 1.5 chs anon->an_swslot = 0;
404 1.5 chs pg->flags &= ~(PG_CLEAN);
405 1.5 chs
406 1.5 chs /*
407 1.5 chs * deactivate the page (to put it on a page queue)
408 1.5 chs */
409 1.5 chs
410 1.50 ad mutex_enter(&uvm_pageqlock);
411 1.24 pk if (pg->wire_count == 0)
412 1.24 pk uvm_pagedeactivate(pg);
413 1.50 ad mutex_exit(&uvm_pageqlock);
414 1.25 pk
415 1.25 pk if (pg->flags & PG_WANTED) {
416 1.25 pk wakeup(pg);
417 1.25 pk pg->flags &= ~(PG_WANTED);
418 1.25 pk }
419 1.5 chs
420 1.5 chs /*
421 1.5 chs * unlock the anon and we're done.
422 1.5 chs */
423 1.5 chs
424 1.54 rmind mutex_exit(anon->an_lock);
425 1.5 chs if (uobj) {
426 1.54 rmind mutex_exit(uobj->vmobjlock);
427 1.5 chs }
428 1.43 thorpej return false;
429 1.1 chuck }
430 1.29 yamt
431 1.37 yamt #endif /* defined(VMSWAP) */
432 1.37 yamt
433 1.29 yamt /*
434 1.29 yamt * uvm_anon_release: release an anon and its page.
435 1.29 yamt *
436 1.29 yamt * => caller must lock the anon.
437 1.29 yamt */
438 1.29 yamt
439 1.29 yamt void
440 1.34 thorpej uvm_anon_release(struct vm_anon *anon)
441 1.29 yamt {
442 1.33 yamt struct vm_page *pg = anon->an_page;
443 1.29 yamt
444 1.54 rmind KASSERT(mutex_owned(anon->an_lock));
445 1.29 yamt KASSERT(pg != NULL);
446 1.29 yamt KASSERT((pg->flags & PG_RELEASED) != 0);
447 1.29 yamt KASSERT((pg->flags & PG_BUSY) != 0);
448 1.29 yamt KASSERT(pg->uobject == NULL);
449 1.29 yamt KASSERT(pg->uanon == anon);
450 1.29 yamt KASSERT(pg->loan_count == 0);
451 1.29 yamt KASSERT(anon->an_ref == 0);
452 1.29 yamt
453 1.50 ad mutex_enter(&uvm_pageqlock);
454 1.29 yamt uvm_pagefree(pg);
455 1.50 ad mutex_exit(&uvm_pageqlock);
456 1.54 rmind mutex_exit(anon->an_lock);
457 1.29 yamt
458 1.33 yamt KASSERT(anon->an_page == NULL);
459 1.29 yamt
460 1.29 yamt uvm_anfree(anon);
461 1.29 yamt }
462