xref: /netbsd-src/sys/uvm/uvm_anon.c (revision 4b896b232495b7a9b8b94a1cf1e21873296d53b8)
1 /*	$NetBSD: uvm_anon.c,v 1.29 2004/05/05 11:54:32 yamt 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.29 2004/05/05 11:54:32 yamt 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(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[lcv].an_lock);
129 		anon[lcv].u.an_nxt = uvm.afree;
130 		uvm.afree = &anon[lcv];
131 	}
132 	simple_unlock(&uvm.afreelock);
133 	return 0;
134 }
135 
136 /*
137  * remove anons from the free pool.
138  */
139 void
140 uvm_anon_remove(count)
141 	int count;
142 {
143 	/*
144 	 * we never actually free any anons, to avoid allocation overhead.
145 	 * XXX someday we might want to try to free anons.
146 	 */
147 
148 	simple_lock(&uvm.afreelock);
149 	uvmexp.nanonneeded -= count;
150 	simple_unlock(&uvm.afreelock);
151 }
152 
153 /*
154  * allocate an anon
155  *
156  * => new anon is returned locked!
157  */
158 struct vm_anon *
159 uvm_analloc()
160 {
161 	struct vm_anon *a;
162 
163 	simple_lock(&uvm.afreelock);
164 	a = uvm.afree;
165 	if (a) {
166 		uvm.afree = a->u.an_nxt;
167 		uvmexp.nfreeanon--;
168 		a->an_ref = 1;
169 		a->an_swslot = 0;
170 		a->u.an_page = NULL;		/* so we can free quickly */
171 		LOCK_ASSERT(simple_lock_held(&a->an_lock) == 0);
172 		simple_lock(&a->an_lock);
173 	}
174 	simple_unlock(&uvm.afreelock);
175 	return(a);
176 }
177 
178 /*
179  * uvm_anfree: free a single anon structure
180  *
181  * => caller must remove anon from its amap before calling (if it was in
182  *	an amap).
183  * => anon must be unlocked and have a zero reference count.
184  * => we may lock the pageq's.
185  */
186 
187 void
188 uvm_anfree(anon)
189 	struct vm_anon *anon;
190 {
191 	struct vm_page *pg;
192 	UVMHIST_FUNC("uvm_anfree"); UVMHIST_CALLED(maphist);
193 	UVMHIST_LOG(maphist,"(anon=0x%x)", anon, 0,0,0);
194 
195 	KASSERT(anon->an_ref == 0);
196 	LOCK_ASSERT(!simple_lock_held(&anon->an_lock));
197 
198 	/*
199 	 * get page
200 	 */
201 
202 	pg = anon->u.an_page;
203 
204 	/*
205 	 * if there is a resident page and it is loaned, then anon may not
206 	 * own it.   call out to uvm_anon_lockpage() to ensure the real owner
207  	 * of the page has been identified and locked.
208 	 */
209 
210 	if (pg && pg->loan_count) {
211 		simple_lock(&anon->an_lock);
212 		pg = uvm_anon_lockloanpg(anon);
213 		simple_unlock(&anon->an_lock);
214 	}
215 
216 	/*
217 	 * if we have a resident page, we must dispose of it before freeing
218 	 * the anon.
219 	 */
220 
221 	if (pg) {
222 
223 		/*
224 		 * if the page is owned by a uobject (now locked), then we must
225 		 * kill the loan on the page rather than free it.
226 		 */
227 
228 		if (pg->uobject) {
229 			uvm_lock_pageq();
230 			KASSERT(pg->loan_count > 0);
231 			pg->loan_count--;
232 			pg->uanon = NULL;
233 			uvm_unlock_pageq();
234 			simple_unlock(&pg->uobject->vmobjlock);
235 		} else {
236 
237 			/*
238 			 * page has no uobject, so we must be the owner of it.
239 			 * if page is busy then we wait until it is not busy,
240 			 * and then free it.
241 			 */
242 
243 			KASSERT((pg->flags & PG_RELEASED) == 0);
244 			simple_lock(&anon->an_lock);
245 			pmap_page_protect(pg, VM_PROT_NONE);
246 
247 			/*
248 			 * if the page is busy, mark it as PG_RELEASED
249 			 * so that uvm_anon_release will release it later.
250 			 */
251 
252 			if (pg->flags & PG_BUSY) {
253 				pg->flags |= PG_RELEASED;
254 				simple_unlock(&anon->an_lock);
255 				return;
256 			}
257 			uvm_lock_pageq();
258 			uvm_pagefree(pg);
259 			uvm_unlock_pageq();
260 			simple_unlock(&anon->an_lock);
261 			UVMHIST_LOG(maphist, "anon 0x%x, page 0x%x: "
262 				    "freed now!", anon, pg, 0, 0);
263 		}
264 	}
265 	if (pg == NULL && anon->an_swslot > 0) {
266 		/* this page is no longer only in swap. */
267 		simple_lock(&uvm.swap_data_lock);
268 		KASSERT(uvmexp.swpgonly > 0);
269 		uvmexp.swpgonly--;
270 		simple_unlock(&uvm.swap_data_lock);
271 	}
272 
273 	/*
274 	 * free any swap resources.
275 	 */
276 
277 	uvm_anon_dropswap(anon);
278 
279 	/*
280 	 * now that we've stripped the data areas from the anon,
281 	 * free the anon itself.
282 	 */
283 
284 	simple_lock(&uvm.afreelock);
285 	anon->u.an_nxt = uvm.afree;
286 	uvm.afree = anon;
287 	uvmexp.nfreeanon++;
288 	simple_unlock(&uvm.afreelock);
289 	UVMHIST_LOG(maphist,"<- done!",0,0,0,0);
290 }
291 
292 /*
293  * uvm_anon_dropswap:  release any swap resources from this anon.
294  *
295  * => anon must be locked or have a reference count of 0.
296  */
297 void
298 uvm_anon_dropswap(anon)
299 	struct vm_anon *anon;
300 {
301 	UVMHIST_FUNC("uvm_anon_dropswap"); UVMHIST_CALLED(maphist);
302 
303 	if (anon->an_swslot == 0)
304 		return;
305 
306 	UVMHIST_LOG(maphist,"freeing swap for anon %p, paged to swslot 0x%x",
307 		    anon, anon->an_swslot, 0, 0);
308 	uvm_swap_free(anon->an_swslot, 1);
309 	anon->an_swslot = 0;
310 }
311 
312 /*
313  * uvm_anon_lockloanpg: given a locked anon, lock its resident page
314  *
315  * => anon is locked by caller
316  * => on return: anon is locked
317  *		 if there is a resident page:
318  *			if it has a uobject, it is locked by us
319  *			if it is ownerless, we take over as owner
320  *		 we return the resident page (it can change during
321  *		 this function)
322  * => note that the only time an anon has an ownerless resident page
323  *	is if the page was loaned from a uvm_object and the uvm_object
324  *	disowned it
325  * => this only needs to be called when you want to do an operation
326  *	on an anon's resident page and that page has a non-zero loan
327  *	count.
328  */
329 struct vm_page *
330 uvm_anon_lockloanpg(anon)
331 	struct vm_anon *anon;
332 {
333 	struct vm_page *pg;
334 	boolean_t locked = FALSE;
335 
336 	LOCK_ASSERT(simple_lock_held(&anon->an_lock));
337 
338 	/*
339 	 * loop while we have a resident page that has a non-zero loan count.
340 	 * if we successfully get our lock, we will "break" the loop.
341 	 * note that the test for pg->loan_count is not protected -- this
342 	 * may produce false positive results.   note that a false positive
343 	 * result may cause us to do more work than we need to, but it will
344 	 * not produce an incorrect result.
345 	 */
346 
347 	while (((pg = anon->u.an_page) != NULL) && pg->loan_count != 0) {
348 
349 		/*
350 		 * quickly check to see if the page has an object before
351 		 * bothering to lock the page queues.   this may also produce
352 		 * a false positive result, but that's ok because we do a real
353 		 * check after that.
354 		 */
355 
356 		if (pg->uobject) {
357 			uvm_lock_pageq();
358 			if (pg->uobject) {
359 				locked =
360 				    simple_lock_try(&pg->uobject->vmobjlock);
361 			} else {
362 				/* object disowned before we got PQ lock */
363 				locked = TRUE;
364 			}
365 			uvm_unlock_pageq();
366 
367 			/*
368 			 * if we didn't get a lock (try lock failed), then we
369 			 * toggle our anon lock and try again
370 			 */
371 
372 			if (!locked) {
373 				simple_unlock(&anon->an_lock);
374 
375 				/*
376 				 * someone locking the object has a chance to
377 				 * lock us right now
378 				 */
379 
380 				simple_lock(&anon->an_lock);
381 				continue;
382 			}
383 		}
384 
385 		/*
386 		 * if page is un-owned [i.e. the object dropped its ownership],
387 		 * then we can take over as owner!
388 		 */
389 
390 		if (pg->uobject == NULL && (pg->pqflags & PQ_ANON) == 0) {
391 			uvm_lock_pageq();
392 			pg->pqflags |= PQ_ANON;
393 			pg->loan_count--;
394 			uvm_unlock_pageq();
395 		}
396 		break;
397 	}
398 	return(pg);
399 }
400 
401 
402 
403 /*
404  * page in every anon that is paged out to a range of swslots.
405  *
406  * swap_syscall_lock should be held (protects anonblock_list).
407  */
408 
409 boolean_t
410 anon_swap_off(startslot, endslot)
411 	int startslot, endslot;
412 {
413 	struct uvm_anonblock *anonblock;
414 
415 	LIST_FOREACH(anonblock, &anonblock_list, list) {
416 		int i;
417 
418 		/*
419 		 * loop thru all the anons in the anonblock,
420 		 * paging in where needed.
421 		 */
422 
423 		for (i = 0; i < anonblock->count; i++) {
424 			struct vm_anon *anon = &anonblock->anons[i];
425 			int slot;
426 
427 			/*
428 			 * lock anon to work on it.
429 			 */
430 
431 			simple_lock(&anon->an_lock);
432 
433 			/*
434 			 * is this anon's swap slot in range?
435 			 */
436 
437 			slot = anon->an_swslot;
438 			if (slot >= startslot && slot < endslot) {
439 				boolean_t rv;
440 
441 				/*
442 				 * yup, page it in.
443 				 */
444 
445 				/* locked: anon */
446 				rv = anon_pagein(anon);
447 				/* unlocked: anon */
448 
449 				if (rv) {
450 					return rv;
451 				}
452 			} else {
453 
454 				/*
455 				 * nope, unlock and proceed.
456 				 */
457 
458 				simple_unlock(&anon->an_lock);
459 			}
460 		}
461 	}
462 	return FALSE;
463 }
464 
465 
466 /*
467  * fetch an anon's page.
468  *
469  * => anon must be locked, and is unlocked upon return.
470  * => returns TRUE if pagein was aborted due to lack of memory.
471  */
472 
473 static boolean_t
474 anon_pagein(anon)
475 	struct vm_anon *anon;
476 {
477 	struct vm_page *pg;
478 	struct uvm_object *uobj;
479 	int rv;
480 
481 	/* locked: anon */
482 	LOCK_ASSERT(simple_lock_held(&anon->an_lock));
483 
484 	rv = uvmfault_anonget(NULL, NULL, anon);
485 
486 	/*
487 	 * if rv == 0, anon is still locked, else anon
488 	 * is unlocked
489 	 */
490 
491 	switch (rv) {
492 	case 0:
493 		break;
494 
495 	case EIO:
496 	case ERESTART:
497 
498 		/*
499 		 * nothing more to do on errors.
500 		 * ERESTART can only mean that the anon was freed,
501 		 * so again there's nothing to do.
502 		 */
503 
504 		return FALSE;
505 
506 	default:
507 		return TRUE;
508 	}
509 
510 	/*
511 	 * ok, we've got the page now.
512 	 * mark it as dirty, clear its swslot and un-busy it.
513 	 */
514 
515 	pg = anon->u.an_page;
516 	uobj = pg->uobject;
517 	if (anon->an_swslot > 0)
518 		uvm_swap_free(anon->an_swslot, 1);
519 	anon->an_swslot = 0;
520 	pg->flags &= ~(PG_CLEAN);
521 
522 	/*
523 	 * deactivate the page (to put it on a page queue)
524 	 */
525 
526 	pmap_clear_reference(pg);
527 	uvm_lock_pageq();
528 	if (pg->wire_count == 0)
529 		uvm_pagedeactivate(pg);
530 	uvm_unlock_pageq();
531 
532 	if (pg->flags & PG_WANTED) {
533 		wakeup(pg);
534 		pg->flags &= ~(PG_WANTED);
535 	}
536 
537 	/*
538 	 * unlock the anon and we're done.
539 	 */
540 
541 	simple_unlock(&anon->an_lock);
542 	if (uobj) {
543 		simple_unlock(&uobj->vmobjlock);
544 	}
545 	return FALSE;
546 }
547 
548 /*
549  * uvm_anon_release: release an anon and its page.
550  *
551  * => caller must lock the anon.
552  */
553 
554 void
555 uvm_anon_release(anon)
556 	struct vm_anon *anon;
557 {
558 	struct vm_page *pg = anon->u.an_page;
559 
560 	LOCK_ASSERT(simple_lock_held(&anon->an_lock));
561 
562 	KASSERT(pg != NULL);
563 	KASSERT((pg->flags & PG_RELEASED) != 0);
564 	KASSERT((pg->flags & PG_BUSY) != 0);
565 	KASSERT(pg->uobject == NULL);
566 	KASSERT(pg->uanon == anon);
567 	KASSERT(pg->loan_count == 0);
568 	KASSERT(anon->an_ref == 0);
569 
570 	uvm_lock_pageq();
571 	uvm_pagefree(pg);
572 	uvm_unlock_pageq();
573 	simple_unlock(&anon->an_lock);
574 
575 	KASSERT(anon->u.an_page == NULL);
576 
577 	uvm_anfree(anon);
578 }
579