uvm_anon.c revision 1.55 1 /* $NetBSD: uvm_anon.c,v 1.55 2011/06/17 02:12:35 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.55 2011/06/17 02:12:35 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_page = NULL;
66 #if defined(VMSWAP)
67 anon->an_swslot = 0;
68 #endif
69 return 0;
70 }
71
72 /*
73 * uvm_analloc: allocate a new anon.
74 *
75 * => anon will have no lock associated.
76 */
77 struct vm_anon *
78 uvm_analloc(void)
79 {
80 struct vm_anon *anon;
81
82 anon = pool_cache_get(&uvm_anon_cache, PR_NOWAIT);
83 if (anon) {
84 KASSERT(anon->an_ref == 0);
85 KASSERT(anon->an_page == NULL);
86 #if defined(VMSWAP)
87 KASSERT(anon->an_swslot == 0);
88 #endif
89 anon->an_ref = 1;
90 anon->an_lock = NULL;
91 }
92 return anon;
93 }
94
95 /*
96 * uvm_anfree1: free a single anon.
97 *
98 * => anon must be removed from the amap (if anon was in an amap).
99 * => amap must be locked or anon must not be associated.
100 * => amap lock may be dropped and re-acquired here.
101 */
102
103 static void
104 uvm_anfree1(struct vm_anon *anon)
105 {
106 struct vm_page *pg = anon->an_page;
107
108 UVMHIST_FUNC("uvm_anfree"); UVMHIST_CALLED(maphist);
109 UVMHIST_LOG(maphist,"(anon=0x%x)", anon, 0,0,0);
110
111 KASSERT(anon->an_lock == NULL || 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 return;
162 }
163 mutex_enter(&uvm_pageqlock);
164 uvm_pagefree(pg);
165 mutex_exit(&uvm_pageqlock);
166 UVMHIST_LOG(maphist, "anon 0x%x, page 0x%x: "
167 "freed now!", anon, pg, 0, 0);
168 }
169 }
170
171 #if defined(VMSWAP)
172 if (pg == NULL && anon->an_swslot > 0) {
173 /* This page is no longer only in swap. */
174 mutex_enter(&uvm_swap_data_lock);
175 KASSERT(uvmexp.swpgonly > 0);
176 uvmexp.swpgonly--;
177 mutex_exit(&uvm_swap_data_lock);
178 }
179 #endif
180
181 /*
182 * Free any swap resources, leave a page replacement hint, drop
183 * the reference on lock and finally destroy the anon itself.
184 */
185
186 uvm_anon_dropswap(anon);
187 uvmpdpol_anfree(anon);
188
189 KASSERT(anon->an_page == NULL);
190 #if defined(VMSWAP)
191 KASSERT(anon->an_swslot == 0);
192 #endif
193
194 if (anon->an_lock != NULL) {
195 mutex_obj_free(anon->an_lock);
196 }
197 pool_cache_put(&uvm_anon_cache, anon);
198 UVMHIST_LOG(maphist,"<- done!",0,0,0,0);
199 }
200
201 /*
202 * uvm_anfree: free a linked list of anon structures.
203 */
204 void
205 uvm_anfree(struct vm_anon *anon)
206 {
207 struct vm_anon *next;
208
209 for (; anon != NULL; anon = next) {
210 /* Note: clearing an_link also clears a reference count. */
211 next = anon->an_link;
212 anon->an_link = NULL;
213 uvm_anfree1(anon);
214 }
215 }
216
217 /*
218 * uvm_anon_lockloanpg: given a locked anon, lock its resident page owner.
219 *
220 * => anon is locked by caller
221 * => on return: anon is locked
222 * if there is a resident page:
223 * if it has a uobject, it is locked by us
224 * if it is ownerless, we take over as owner
225 * we return the resident page (it can change during
226 * this function)
227 * => note that the only time an anon has an ownerless resident page
228 * is if the page was loaned from a uvm_object and the uvm_object
229 * disowned it
230 * => this only needs to be called when you want to do an operation
231 * on an anon's resident page and that page has a non-zero loan
232 * count.
233 */
234 struct vm_page *
235 uvm_anon_lockloanpg(struct vm_anon *anon)
236 {
237 struct vm_page *pg;
238 bool locked = false;
239
240 KASSERT(mutex_owned(anon->an_lock));
241
242 /*
243 * loop while we have a resident page that has a non-zero loan count.
244 * if we successfully get our lock, we will "break" the loop.
245 * note that the test for pg->loan_count is not protected -- this
246 * may produce false positive results. note that a false positive
247 * result may cause us to do more work than we need to, but it will
248 * not produce an incorrect result.
249 */
250
251 while (((pg = anon->an_page) != NULL) && pg->loan_count != 0) {
252
253 /*
254 * quickly check to see if the page has an object before
255 * bothering to lock the page queues. this may also produce
256 * a false positive result, but that's ok because we do a real
257 * check after that.
258 */
259
260 if (pg->uobject) {
261 mutex_enter(&uvm_pageqlock);
262 if (pg->uobject) {
263 locked =
264 mutex_tryenter(pg->uobject->vmobjlock);
265 } else {
266 /* object disowned before we got PQ lock */
267 locked = true;
268 }
269 mutex_exit(&uvm_pageqlock);
270
271 /*
272 * if we didn't get a lock (try lock failed), then we
273 * toggle our anon lock and try again
274 */
275
276 if (!locked) {
277 /*
278 * someone locking the object has a chance to
279 * lock us right now
280 *
281 * XXX Better than yielding but inadequate.
282 */
283 kpause("livelock", false, 1, anon->an_lock);
284 continue;
285 }
286 }
287
288 /*
289 * If page is un-owned i.e. the object dropped its ownership,
290 * then we have to take the ownership.
291 */
292
293 if (pg->uobject == NULL && (pg->pqflags & PQ_ANON) == 0) {
294 mutex_enter(&uvm_pageqlock);
295 pg->pqflags |= PQ_ANON;
296 pg->loan_count--;
297 mutex_exit(&uvm_pageqlock);
298 }
299 break;
300 }
301 return pg;
302 }
303
304 #if defined(VMSWAP)
305
306 /*
307 * uvm_anon_pagein: fetch an anon's page.
308 *
309 * => anon must be locked, and is unlocked upon return.
310 * => returns true if pagein was aborted due to lack of memory.
311 */
312
313 bool
314 uvm_anon_pagein(struct vm_anon *anon)
315 {
316 struct vm_page *pg;
317 struct uvm_object *uobj;
318
319 KASSERT(mutex_owned(anon->an_lock));
320
321 /*
322 * Get the page of the anon.
323 */
324
325 switch (uvmfault_anonget(NULL, NULL, anon)) {
326 case 0:
327 /* Success - we have the page. */
328 KASSERT(mutex_owned(anon->an_lock));
329 break;
330 case EIO:
331 case ERESTART:
332 /*
333 * Nothing more to do on errors. ERESTART means that the
334 * anon was freed.
335 */
336 return false;
337 default:
338 return true;
339 }
340
341 /*
342 * Mark the page as dirty, clear its swslot and un-busy it.
343 */
344
345 pg = anon->an_page;
346 uobj = pg->uobject;
347 if (anon->an_swslot > 0) {
348 uvm_swap_free(anon->an_swslot, 1);
349 }
350 anon->an_swslot = 0;
351 pg->flags &= ~PG_CLEAN;
352
353 /*
354 * Deactivate the page (to put it on a page queue).
355 */
356
357 mutex_enter(&uvm_pageqlock);
358 if (pg->wire_count == 0) {
359 uvm_pagedeactivate(pg);
360 }
361 mutex_exit(&uvm_pageqlock);
362
363 if (pg->flags & PG_WANTED) {
364 pg->flags &= ~PG_WANTED;
365 wakeup(pg);
366 }
367
368 mutex_exit(anon->an_lock);
369 if (uobj) {
370 mutex_exit(uobj->vmobjlock);
371 }
372 return false;
373 }
374
375 /*
376 * uvm_anon_dropswap: release any swap resources from this anon.
377 *
378 * => anon must be locked or have a reference count of 0.
379 */
380 void
381 uvm_anon_dropswap(struct vm_anon *anon)
382 {
383 UVMHIST_FUNC("uvm_anon_dropswap"); UVMHIST_CALLED(maphist);
384
385 if (anon->an_swslot == 0)
386 return;
387
388 UVMHIST_LOG(maphist,"freeing swap for anon %p, paged to swslot 0x%x",
389 anon, anon->an_swslot, 0, 0);
390 uvm_swap_free(anon->an_swslot, 1);
391 anon->an_swslot = 0;
392 }
393
394 #endif
395
396 /*
397 * uvm_anon_release: release an anon and its page.
398 *
399 * => anon should not have any references.
400 * => anon must be locked.
401 */
402
403 void
404 uvm_anon_release(struct vm_anon *anon)
405 {
406 struct vm_page *pg = anon->an_page;
407
408 KASSERT(mutex_owned(anon->an_lock));
409 KASSERT(pg != NULL);
410 KASSERT((pg->flags & PG_RELEASED) != 0);
411 KASSERT((pg->flags & PG_BUSY) != 0);
412 KASSERT(pg->uobject == NULL);
413 KASSERT(pg->uanon == anon);
414 KASSERT(pg->loan_count == 0);
415 KASSERT(anon->an_ref == 0);
416
417 mutex_enter(&uvm_pageqlock);
418 uvm_pagefree(pg);
419 mutex_exit(&uvm_pageqlock);
420 mutex_exit(anon->an_lock);
421
422 KASSERT(anon->an_page == NULL);
423
424 uvm_anfree(anon);
425 }
426