xref: /netbsd-src/sys/uvm/uvm_anon.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: uvm_anon.c,v 1.48 2007/11/13 08:48:28 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.48 2007/11/13 08:48:28 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 #include <uvm/uvm_pdpolicy.h>
54 
55 static struct pool_cache uvm_anon_cache;
56 
57 static int uvm_anon_ctor(void *, void *, int);
58 static void uvm_anon_dtor(void *, void *);
59 
60 /*
61  * allocate anons
62  */
63 void
64 uvm_anon_init(void)
65 {
66 
67 	pool_cache_bootstrap(&uvm_anon_cache, sizeof(struct vm_anon), 0, 0, 0,
68 	    "anonpl", NULL, IPL_NONE, uvm_anon_ctor, uvm_anon_dtor, NULL);
69 }
70 
71 static int
72 uvm_anon_ctor(void *arg, void *object, int flags)
73 {
74 	struct vm_anon *anon = object;
75 
76 	anon->an_ref = 0;
77 	simple_lock_init(&anon->an_lock);
78 	anon->an_page = NULL;
79 #if defined(VMSWAP)
80 	anon->an_swslot = 0;
81 #endif /* defined(VMSWAP) */
82 
83 	return 0;
84 }
85 
86 static void
87 uvm_anon_dtor(void *arg, void *object)
88 {
89 
90 	/* nothing yet */
91 }
92 
93 /*
94  * allocate an anon
95  *
96  * => new anon is returned locked!
97  */
98 struct vm_anon *
99 uvm_analloc(void)
100 {
101 	struct vm_anon *anon;
102 
103 	anon = pool_cache_get(&uvm_anon_cache, PR_NOWAIT);
104 	if (anon) {
105 		KASSERT(anon->an_ref == 0);
106 		LOCK_ASSERT(simple_lock_held(&anon->an_lock) == 0);
107 		KASSERT(anon->an_page == NULL);
108 #if defined(VMSWAP)
109 		KASSERT(anon->an_swslot == 0);
110 #endif /* defined(VMSWAP) */
111 		anon->an_ref = 1;
112 		simple_lock(&anon->an_lock);
113 	}
114 	return anon;
115 }
116 
117 /*
118  * uvm_anfree: free a single anon structure
119  *
120  * => caller must remove anon from its amap before calling (if it was in
121  *	an amap).
122  * => anon must be unlocked and have a zero reference count.
123  * => we may lock the pageq's.
124  */
125 
126 void
127 uvm_anfree(struct vm_anon *anon)
128 {
129 	struct vm_page *pg;
130 	UVMHIST_FUNC("uvm_anfree"); UVMHIST_CALLED(maphist);
131 	UVMHIST_LOG(maphist,"(anon=0x%x)", anon, 0,0,0);
132 
133 	KASSERT(anon->an_ref == 0);
134 
135 	/*
136 	 * get page
137 	 */
138 
139 	pg = anon->an_page;
140 
141 	/*
142 	 * if there is a resident page and it is loaned, then anon may not
143 	 * own it.   call out to uvm_anon_lockpage() to ensure the real owner
144  	 * of the page has been identified and locked.
145 	 */
146 
147 	if (pg && pg->loan_count) {
148 		simple_lock(&anon->an_lock);
149 		pg = uvm_anon_lockloanpg(anon);
150 		simple_unlock(&anon->an_lock);
151 	}
152 
153 	/*
154 	 * if we have a resident page, we must dispose of it before freeing
155 	 * the anon.
156 	 */
157 
158 	if (pg) {
159 
160 		/*
161 		 * if the page is owned by a uobject (now locked), then we must
162 		 * kill the loan on the page rather than free it.
163 		 */
164 
165 		if (pg->uobject) {
166 			uvm_lock_pageq();
167 			KASSERT(pg->loan_count > 0);
168 			pg->loan_count--;
169 			pg->uanon = NULL;
170 			uvm_unlock_pageq();
171 			simple_unlock(&pg->uobject->vmobjlock);
172 		} else {
173 
174 			/*
175 			 * page has no uobject, so we must be the owner of it.
176 			 */
177 
178 			KASSERT((pg->flags & PG_RELEASED) == 0);
179 			simple_lock(&anon->an_lock);
180 			pmap_page_protect(pg, VM_PROT_NONE);
181 
182 			/*
183 			 * if the page is busy, mark it as PG_RELEASED
184 			 * so that uvm_anon_release will release it later.
185 			 */
186 
187 			if (pg->flags & PG_BUSY) {
188 				pg->flags |= PG_RELEASED;
189 				simple_unlock(&anon->an_lock);
190 				return;
191 			}
192 			uvm_lock_pageq();
193 			uvm_pagefree(pg);
194 			uvm_unlock_pageq();
195 			simple_unlock(&anon->an_lock);
196 			UVMHIST_LOG(maphist, "anon 0x%x, page 0x%x: "
197 				    "freed now!", anon, pg, 0, 0);
198 		}
199 	}
200 #if defined(VMSWAP)
201 	if (pg == NULL && anon->an_swslot > 0) {
202 		/* this page is no longer only in swap. */
203 		mutex_enter(&uvm_swap_data_lock);
204 		KASSERT(uvmexp.swpgonly > 0);
205 		uvmexp.swpgonly--;
206 		mutex_exit(&uvm_swap_data_lock);
207 	}
208 #endif /* defined(VMSWAP) */
209 
210 	/*
211 	 * free any swap resources.
212 	 */
213 
214 	uvm_anon_dropswap(anon);
215 
216 	/*
217 	 * give a page replacement hint.
218 	 */
219 
220 	uvmpdpol_anfree(anon);
221 
222 	/*
223 	 * now that we've stripped the data areas from the anon,
224 	 * free the anon itself.
225 	 */
226 
227 	KASSERT(anon->an_page == NULL);
228 #if defined(VMSWAP)
229 	KASSERT(anon->an_swslot == 0);
230 #endif /* defined(VMSWAP) */
231 
232 	pool_cache_put(&uvm_anon_cache, anon);
233 	UVMHIST_LOG(maphist,"<- done!",0,0,0,0);
234 }
235 
236 #if defined(VMSWAP)
237 
238 /*
239  * uvm_anon_dropswap:  release any swap resources from this anon.
240  *
241  * => anon must be locked or have a reference count of 0.
242  */
243 void
244 uvm_anon_dropswap(struct vm_anon *anon)
245 {
246 	UVMHIST_FUNC("uvm_anon_dropswap"); UVMHIST_CALLED(maphist);
247 
248 	if (anon->an_swslot == 0)
249 		return;
250 
251 	UVMHIST_LOG(maphist,"freeing swap for anon %p, paged to swslot 0x%x",
252 		    anon, anon->an_swslot, 0, 0);
253 	uvm_swap_free(anon->an_swslot, 1);
254 	anon->an_swslot = 0;
255 }
256 
257 #endif /* defined(VMSWAP) */
258 
259 /*
260  * uvm_anon_lockloanpg: given a locked anon, lock its resident page
261  *
262  * => anon is locked by caller
263  * => on return: anon is locked
264  *		 if there is a resident page:
265  *			if it has a uobject, it is locked by us
266  *			if it is ownerless, we take over as owner
267  *		 we return the resident page (it can change during
268  *		 this function)
269  * => note that the only time an anon has an ownerless resident page
270  *	is if the page was loaned from a uvm_object and the uvm_object
271  *	disowned it
272  * => this only needs to be called when you want to do an operation
273  *	on an anon's resident page and that page has a non-zero loan
274  *	count.
275  */
276 struct vm_page *
277 uvm_anon_lockloanpg(struct vm_anon *anon)
278 {
279 	struct vm_page *pg;
280 	bool locked = false;
281 
282 	LOCK_ASSERT(simple_lock_held(&anon->an_lock));
283 
284 	/*
285 	 * loop while we have a resident page that has a non-zero loan count.
286 	 * if we successfully get our lock, we will "break" the loop.
287 	 * note that the test for pg->loan_count is not protected -- this
288 	 * may produce false positive results.   note that a false positive
289 	 * result may cause us to do more work than we need to, but it will
290 	 * not produce an incorrect result.
291 	 */
292 
293 	while (((pg = anon->an_page) != NULL) && pg->loan_count != 0) {
294 
295 		/*
296 		 * quickly check to see if the page has an object before
297 		 * bothering to lock the page queues.   this may also produce
298 		 * a false positive result, but that's ok because we do a real
299 		 * check after that.
300 		 */
301 
302 		if (pg->uobject) {
303 			uvm_lock_pageq();
304 			if (pg->uobject) {
305 				locked =
306 				    simple_lock_try(&pg->uobject->vmobjlock);
307 			} else {
308 				/* object disowned before we got PQ lock */
309 				locked = true;
310 			}
311 			uvm_unlock_pageq();
312 
313 			/*
314 			 * if we didn't get a lock (try lock failed), then we
315 			 * toggle our anon lock and try again
316 			 */
317 
318 			if (!locked) {
319 				simple_unlock(&anon->an_lock);
320 
321 				/*
322 				 * someone locking the object has a chance to
323 				 * lock us right now
324 				 */
325 
326 				simple_lock(&anon->an_lock);
327 				continue;
328 			}
329 		}
330 
331 		/*
332 		 * if page is un-owned [i.e. the object dropped its ownership],
333 		 * then we can take over as owner!
334 		 */
335 
336 		if (pg->uobject == NULL && (pg->pqflags & PQ_ANON) == 0) {
337 			uvm_lock_pageq();
338 			pg->pqflags |= PQ_ANON;
339 			pg->loan_count--;
340 			uvm_unlock_pageq();
341 		}
342 		break;
343 	}
344 	return(pg);
345 }
346 
347 #if defined(VMSWAP)
348 
349 /*
350  * fetch an anon's page.
351  *
352  * => anon must be locked, and is unlocked upon return.
353  * => returns true if pagein was aborted due to lack of memory.
354  */
355 
356 bool
357 uvm_anon_pagein(struct vm_anon *anon)
358 {
359 	struct vm_page *pg;
360 	struct uvm_object *uobj;
361 	int rv;
362 
363 	/* locked: anon */
364 	LOCK_ASSERT(simple_lock_held(&anon->an_lock));
365 
366 	rv = uvmfault_anonget(NULL, NULL, anon);
367 
368 	/*
369 	 * if rv == 0, anon is still locked, else anon
370 	 * is unlocked
371 	 */
372 
373 	switch (rv) {
374 	case 0:
375 		break;
376 
377 	case EIO:
378 	case ERESTART:
379 
380 		/*
381 		 * nothing more to do on errors.
382 		 * ERESTART can only mean that the anon was freed,
383 		 * so again there's nothing to do.
384 		 */
385 
386 		return false;
387 
388 	default:
389 		return true;
390 	}
391 
392 	/*
393 	 * ok, we've got the page now.
394 	 * mark it as dirty, clear its swslot and un-busy it.
395 	 */
396 
397 	pg = anon->an_page;
398 	uobj = pg->uobject;
399 	if (anon->an_swslot > 0)
400 		uvm_swap_free(anon->an_swslot, 1);
401 	anon->an_swslot = 0;
402 	pg->flags &= ~(PG_CLEAN);
403 
404 	/*
405 	 * deactivate the page (to put it on a page queue)
406 	 */
407 
408 	pmap_clear_reference(pg);
409 	uvm_lock_pageq();
410 	if (pg->wire_count == 0)
411 		uvm_pagedeactivate(pg);
412 	uvm_unlock_pageq();
413 
414 	if (pg->flags & PG_WANTED) {
415 		wakeup(pg);
416 		pg->flags &= ~(PG_WANTED);
417 	}
418 
419 	/*
420 	 * unlock the anon and we're done.
421 	 */
422 
423 	simple_unlock(&anon->an_lock);
424 	if (uobj) {
425 		simple_unlock(&uobj->vmobjlock);
426 	}
427 	return false;
428 }
429 
430 #endif /* defined(VMSWAP) */
431 
432 /*
433  * uvm_anon_release: release an anon and its page.
434  *
435  * => caller must lock the anon.
436  */
437 
438 void
439 uvm_anon_release(struct vm_anon *anon)
440 {
441 	struct vm_page *pg = anon->an_page;
442 
443 	LOCK_ASSERT(simple_lock_held(&anon->an_lock));
444 
445 	KASSERT(pg != NULL);
446 	KASSERT((pg->flags & PG_RELEASED) != 0);
447 	KASSERT((pg->flags & PG_BUSY) != 0);
448 	KASSERT(pg->uobject == NULL);
449 	KASSERT(pg->uanon == anon);
450 	KASSERT(pg->loan_count == 0);
451 	KASSERT(anon->an_ref == 0);
452 
453 	uvm_lock_pageq();
454 	uvm_pagefree(pg);
455 	uvm_unlock_pageq();
456 	simple_unlock(&anon->an_lock);
457 
458 	KASSERT(anon->an_page == NULL);
459 
460 	uvm_anfree(anon);
461 }
462