uvm_anon.c revision 1.21 1 /* $NetBSD: uvm_anon.c,v 1.21 2001/11/10 07:36:59 lukem Exp $ */
2
3 /*
4 *
5 * Copyright (c) 1997 Charles D. Cranor and Washington University.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Charles D. Cranor and
19 * Washington University.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /*
36 * uvm_anon.c: uvm anon ops
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: uvm_anon.c,v 1.21 2001/11/10 07:36:59 lukem Exp $");
41
42 #include "opt_uvmhist.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/proc.h>
47 #include <sys/malloc.h>
48 #include <sys/pool.h>
49 #include <sys/kernel.h>
50
51 #include <uvm/uvm.h>
52 #include <uvm/uvm_swap.h>
53
54 /*
55 * anonblock_list: global list of anon blocks,
56 * locked by swap_syscall_lock (since we never remove
57 * anything from this list and we only add to it via swapctl(2)).
58 */
59
60 struct uvm_anonblock {
61 LIST_ENTRY(uvm_anonblock) list;
62 int count;
63 struct vm_anon *anons;
64 };
65 static LIST_HEAD(anonlist, uvm_anonblock) anonblock_list;
66
67
68 static boolean_t anon_pagein __P((struct vm_anon *));
69
70
71 /*
72 * allocate anons
73 */
74 void
75 uvm_anon_init()
76 {
77 int nanon = uvmexp.free - (uvmexp.free / 16); /* XXXCDC ??? */
78
79 simple_lock_init(&uvm.afreelock);
80 LIST_INIT(&anonblock_list);
81
82 /*
83 * Allocate the initial anons.
84 */
85 uvm_anon_add(nanon);
86 }
87
88 /*
89 * add some more anons to the free pool. called when we add
90 * more swap space.
91 *
92 * => swap_syscall_lock should be held (protects anonblock_list).
93 */
94 int
95 uvm_anon_add(count)
96 int count;
97 {
98 struct uvm_anonblock *anonblock;
99 struct vm_anon *anon;
100 int lcv, needed;
101
102 simple_lock(&uvm.afreelock);
103 uvmexp.nanonneeded += count;
104 needed = uvmexp.nanonneeded - uvmexp.nanon;
105 simple_unlock(&uvm.afreelock);
106
107 if (needed <= 0) {
108 return 0;
109 }
110 anon = (void *)uvm_km_alloc(kernel_map, sizeof(*anon) * needed);
111 if (anon == NULL) {
112 simple_lock(&uvm.afreelock);
113 uvmexp.nanonneeded -= count;
114 simple_unlock(&uvm.afreelock);
115 return ENOMEM;
116 }
117 MALLOC(anonblock, void *, sizeof(*anonblock), M_UVMAMAP, M_WAITOK);
118
119 anonblock->count = needed;
120 anonblock->anons = anon;
121 LIST_INSERT_HEAD(&anonblock_list, anonblock, list);
122 memset(anon, 0, sizeof(*anon) * needed);
123
124 simple_lock(&uvm.afreelock);
125 uvmexp.nanon += needed;
126 uvmexp.nfreeanon += needed;
127 for (lcv = 0; lcv < needed; lcv++) {
128 simple_lock_init(&anon->an_lock);
129 anon[lcv].u.an_nxt = uvm.afree;
130 uvm.afree = &anon[lcv];
131 simple_lock_init(&uvm.afree->an_lock);
132 }
133 simple_unlock(&uvm.afreelock);
134 return 0;
135 }
136
137 /*
138 * remove anons from the free pool.
139 */
140 void
141 uvm_anon_remove(count)
142 int count;
143 {
144 /*
145 * we never actually free any anons, to avoid allocation overhead.
146 * XXX someday we might want to try to free anons.
147 */
148
149 simple_lock(&uvm.afreelock);
150 uvmexp.nanonneeded -= count;
151 simple_unlock(&uvm.afreelock);
152 }
153
154 /*
155 * allocate an anon
156 *
157 * => new anon is returned locked!
158 */
159 struct vm_anon *
160 uvm_analloc()
161 {
162 struct vm_anon *a;
163
164 simple_lock(&uvm.afreelock);
165 a = uvm.afree;
166 if (a) {
167 uvm.afree = a->u.an_nxt;
168 uvmexp.nfreeanon--;
169 a->an_ref = 1;
170 a->an_swslot = 0;
171 a->u.an_page = NULL; /* so we can free quickly */
172 LOCK_ASSERT(simple_lock_held(&a->an_lock) == 0);
173 simple_lock(&a->an_lock);
174 }
175 simple_unlock(&uvm.afreelock);
176 return(a);
177 }
178
179 /*
180 * uvm_anfree: free a single anon structure
181 *
182 * => caller must remove anon from its amap before calling (if it was in
183 * an amap).
184 * => anon must be unlocked and have a zero reference count.
185 * => we may lock the pageq's.
186 */
187
188 void
189 uvm_anfree(anon)
190 struct vm_anon *anon;
191 {
192 struct vm_page *pg;
193 UVMHIST_FUNC("uvm_anfree"); UVMHIST_CALLED(maphist);
194 UVMHIST_LOG(maphist,"(anon=0x%x)", anon, 0,0,0);
195
196 KASSERT(anon->an_ref == 0);
197 LOCK_ASSERT(!simple_lock_held(&anon->an_lock));
198
199 /*
200 * get page
201 */
202
203 pg = anon->u.an_page;
204
205 /*
206 * if there is a resident page and it is loaned, then anon may not
207 * own it. call out to uvm_anon_lockpage() to ensure the real owner
208 * of the page has been identified and locked.
209 */
210
211 if (pg && pg->loan_count)
212 pg = uvm_anon_lockloanpg(anon);
213
214 /*
215 * if we have a resident page, we must dispose of it before freeing
216 * the anon.
217 */
218
219 if (pg) {
220
221 /*
222 * if the page is owned by a uobject (now locked), then we must
223 * kill the loan on the page rather than free it.
224 */
225
226 if (pg->uobject) {
227 uvm_lock_pageq();
228 KASSERT(pg->loan_count > 0);
229 pg->loan_count--;
230 pg->uanon = NULL;
231 uvm_unlock_pageq();
232 simple_unlock(&pg->uobject->vmobjlock);
233 } else {
234
235 /*
236 * page has no uobject, so we must be the owner of it.
237 * if page is busy then we wait until it is not busy,
238 * and then free it.
239 */
240
241 KASSERT((pg->flags & PG_RELEASED) == 0);
242 simple_lock(&anon->an_lock);
243 pmap_page_protect(pg, VM_PROT_NONE);
244 while ((pg = anon->u.an_page) &&
245 (pg->flags & PG_BUSY) != 0) {
246 pg->flags |= PG_WANTED;
247 UVM_UNLOCK_AND_WAIT(pg, &anon->an_lock, 0,
248 "anfree", 0);
249 simple_lock(&anon->an_lock);
250 }
251 if (pg) {
252 uvm_lock_pageq();
253 uvm_pagefree(pg);
254 uvm_unlock_pageq();
255 }
256 simple_unlock(&anon->an_lock);
257 UVMHIST_LOG(maphist, "anon 0x%x, page 0x%x: "
258 "freed now!", anon, pg, 0, 0);
259 }
260 }
261 if (pg == NULL && anon->an_swslot != 0) {
262 /* this page is no longer only in swap. */
263 simple_lock(&uvm.swap_data_lock);
264 KASSERT(uvmexp.swpgonly > 0);
265 uvmexp.swpgonly--;
266 simple_unlock(&uvm.swap_data_lock);
267 }
268
269 /*
270 * free any swap resources.
271 */
272
273 uvm_anon_dropswap(anon);
274
275 /*
276 * now that we've stripped the data areas from the anon,
277 * free the anon itself.
278 */
279
280 simple_lock(&uvm.afreelock);
281 anon->u.an_nxt = uvm.afree;
282 uvm.afree = anon;
283 uvmexp.nfreeanon++;
284 simple_unlock(&uvm.afreelock);
285 UVMHIST_LOG(maphist,"<- done!",0,0,0,0);
286 }
287
288 /*
289 * uvm_anon_dropswap: release any swap resources from this anon.
290 *
291 * => anon must be locked or have a reference count of 0.
292 */
293 void
294 uvm_anon_dropswap(anon)
295 struct vm_anon *anon;
296 {
297 UVMHIST_FUNC("uvm_anon_dropswap"); UVMHIST_CALLED(maphist);
298
299 if (anon->an_swslot == 0)
300 return;
301
302 UVMHIST_LOG(maphist,"freeing swap for anon %p, paged to swslot 0x%x",
303 anon, anon->an_swslot, 0, 0);
304 uvm_swap_free(anon->an_swslot, 1);
305 anon->an_swslot = 0;
306 }
307
308 /*
309 * uvm_anon_lockloanpg: given a locked anon, lock its resident page
310 *
311 * => anon is locked by caller
312 * => on return: anon is locked
313 * if there is a resident page:
314 * if it has a uobject, it is locked by us
315 * if it is ownerless, we take over as owner
316 * we return the resident page (it can change during
317 * this function)
318 * => note that the only time an anon has an ownerless resident page
319 * is if the page was loaned from a uvm_object and the uvm_object
320 * disowned it
321 * => this only needs to be called when you want to do an operation
322 * on an anon's resident page and that page has a non-zero loan
323 * count.
324 */
325 struct vm_page *
326 uvm_anon_lockloanpg(anon)
327 struct vm_anon *anon;
328 {
329 struct vm_page *pg;
330 boolean_t locked = FALSE;
331
332 LOCK_ASSERT(simple_lock_held(&anon->an_lock));
333
334 /*
335 * loop while we have a resident page that has a non-zero loan count.
336 * if we successfully get our lock, we will "break" the loop.
337 * note that the test for pg->loan_count is not protected -- this
338 * may produce false positive results. note that a false positive
339 * result may cause us to do more work than we need to, but it will
340 * not produce an incorrect result.
341 */
342
343 while (((pg = anon->u.an_page) != NULL) && pg->loan_count != 0) {
344
345 /*
346 * quickly check to see if the page has an object before
347 * bothering to lock the page queues. this may also produce
348 * a false positive result, but that's ok because we do a real
349 * check after that.
350 */
351
352 if (pg->uobject) {
353 uvm_lock_pageq();
354 if (pg->uobject) {
355 locked =
356 simple_lock_try(&pg->uobject->vmobjlock);
357 } else {
358 /* object disowned before we got PQ lock */
359 locked = TRUE;
360 }
361 uvm_unlock_pageq();
362
363 /*
364 * if we didn't get a lock (try lock failed), then we
365 * toggle our anon lock and try again
366 */
367
368 if (!locked) {
369 simple_unlock(&anon->an_lock);
370
371 /*
372 * someone locking the object has a chance to
373 * lock us right now
374 */
375
376 simple_lock(&anon->an_lock);
377 continue;
378 }
379 }
380
381 /*
382 * if page is un-owned [i.e. the object dropped its ownership],
383 * then we can take over as owner!
384 */
385
386 if (pg->uobject == NULL && (pg->pqflags & PQ_ANON) == 0) {
387 uvm_lock_pageq();
388 pg->pqflags |= PQ_ANON;
389 pg->loan_count--;
390 uvm_unlock_pageq();
391 }
392 break;
393 }
394 return(pg);
395 }
396
397
398
399 /*
400 * page in every anon that is paged out to a range of swslots.
401 *
402 * swap_syscall_lock should be held (protects anonblock_list).
403 */
404
405 boolean_t
406 anon_swap_off(startslot, endslot)
407 int startslot, endslot;
408 {
409 struct uvm_anonblock *anonblock;
410
411 LIST_FOREACH(anonblock, &anonblock_list, list) {
412 int i;
413
414 /*
415 * loop thru all the anons in the anonblock,
416 * paging in where needed.
417 */
418
419 for (i = 0; i < anonblock->count; i++) {
420 struct vm_anon *anon = &anonblock->anons[i];
421 int slot;
422
423 /*
424 * lock anon to work on it.
425 */
426
427 simple_lock(&anon->an_lock);
428
429 /*
430 * is this anon's swap slot in range?
431 */
432
433 slot = anon->an_swslot;
434 if (slot >= startslot && slot < endslot) {
435 boolean_t rv;
436
437 /*
438 * yup, page it in.
439 */
440
441 /* locked: anon */
442 rv = anon_pagein(anon);
443 /* unlocked: anon */
444
445 if (rv) {
446 return rv;
447 }
448 } else {
449
450 /*
451 * nope, unlock and proceed.
452 */
453
454 simple_unlock(&anon->an_lock);
455 }
456 }
457 }
458 return FALSE;
459 }
460
461
462 /*
463 * fetch an anon's page.
464 *
465 * => anon must be locked, and is unlocked upon return.
466 * => returns TRUE if pagein was aborted due to lack of memory.
467 */
468
469 static boolean_t
470 anon_pagein(anon)
471 struct vm_anon *anon;
472 {
473 struct vm_page *pg;
474 struct uvm_object *uobj;
475 int rv;
476
477 /* locked: anon */
478 LOCK_ASSERT(simple_lock_held(&anon->an_lock));
479
480 rv = uvmfault_anonget(NULL, NULL, anon);
481
482 /*
483 * if rv == 0, anon is still locked, else anon
484 * is unlocked
485 */
486
487 switch (rv) {
488 case 0:
489 break;
490
491 case EIO:
492 case ERESTART:
493
494 /*
495 * nothing more to do on errors.
496 * ERESTART can only mean that the anon was freed,
497 * so again there's nothing to do.
498 */
499
500 return FALSE;
501 }
502
503 /*
504 * ok, we've got the page now.
505 * mark it as dirty, clear its swslot and un-busy it.
506 */
507
508 pg = anon->u.an_page;
509 uobj = pg->uobject;
510 uvm_swap_free(anon->an_swslot, 1);
511 anon->an_swslot = 0;
512 pg->flags &= ~(PG_CLEAN);
513
514 /*
515 * deactivate the page (to put it on a page queue)
516 */
517
518 pmap_clear_reference(pg);
519 uvm_lock_pageq();
520 uvm_pagedeactivate(pg);
521 uvm_unlock_pageq();
522
523 /*
524 * unlock the anon and we're done.
525 */
526
527 simple_unlock(&anon->an_lock);
528 if (uobj) {
529 simple_unlock(&uobj->vmobjlock);
530 }
531 return FALSE;
532 }
533