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