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