xref: /openbsd/sys/uvm/uvm_anon.c (revision cecf84d4)
1 /*	$OpenBSD: uvm_anon.c,v 1.43 2014/12/23 04:56:47 tedu Exp $	*/
2 /*	$NetBSD: uvm_anon.c,v 1.10 2000/11/25 06:27:59 chs Exp $	*/
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  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * uvm_anon.c: uvm anon ops
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/pool.h>
37 #include <sys/kernel.h>
38 #include <sys/atomic.h>
39 
40 #include <uvm/uvm.h>
41 #include <uvm/uvm_swap.h>
42 
43 struct pool uvm_anon_pool;
44 
45 /*
46  * allocate anons
47  */
48 void
49 uvm_anon_init(void)
50 {
51 	pool_init(&uvm_anon_pool, sizeof(struct vm_anon), 0, 0,
52 	    PR_WAITOK, "anonpl", NULL);
53 	pool_sethiwat(&uvm_anon_pool, uvmexp.free / 16);
54 }
55 
56 /*
57  * allocate an anon
58  */
59 struct vm_anon *
60 uvm_analloc(void)
61 {
62 	struct vm_anon *anon;
63 
64 	anon = pool_get(&uvm_anon_pool, PR_NOWAIT);
65 	if (anon) {
66 		anon->an_ref = 1;
67 		anon->an_page = NULL;
68 		anon->an_swslot = 0;
69 	}
70 	return(anon);
71 }
72 
73 /*
74  * uvm_anfree: free a single anon structure
75  *
76  * => caller must remove anon from its amap before calling (if it was in
77  *	an amap).
78  * => we may lock the pageq's.
79  */
80 void
81 uvm_anfree(struct vm_anon *anon)
82 {
83 	struct vm_page *pg;
84 
85 	/* get page */
86 	pg = anon->an_page;
87 
88 	/*
89 	 * if there is a resident page and it is loaned, then anon may not
90 	 * own it.   call out to uvm_anon_lockpage() to ensure the real owner
91  	 * of the page has been identified and locked.
92 	 */
93 	if (pg && pg->loan_count)
94 		pg = uvm_anon_lockloanpg(anon);
95 
96 	/*
97 	 * if we have a resident page, we must dispose of it before freeing
98 	 * the anon.
99 	 */
100 	if (pg) {
101 		/*
102 		 * if the page is owned by a uobject, then we must
103 		 * kill the loan on the page rather than free it.
104 		 */
105 		if (pg->uobject) {
106 			uvm_lock_pageq();
107 			KASSERT(pg->loan_count > 0);
108 			pg->loan_count--;
109 			pg->uanon = NULL;
110 			uvm_unlock_pageq();
111 		} else {
112 			/*
113 			 * page has no uobject, so we must be the owner of it.
114 			 *
115 			 * if page is busy then we just mark it as released
116 			 * (who ever has it busy must check for this when they
117 			 * wake up).    if the page is not busy then we can
118 			 * free it now.
119 			 */
120 			if ((pg->pg_flags & PG_BUSY) != 0) {
121 				/* tell them to dump it when done */
122 				atomic_setbits_int(&pg->pg_flags, PG_RELEASED);
123 				return;
124 			}
125 			pmap_page_protect(pg, PROT_NONE);
126 			uvm_lock_pageq();	/* lock out pagedaemon */
127 			uvm_pagefree(pg);	/* bye bye */
128 			uvm_unlock_pageq();	/* free the daemon */
129 		}
130 	}
131 	if (pg == NULL && anon->an_swslot != 0) {
132 		/* this page is no longer only in swap. */
133 		KASSERT(uvmexp.swpgonly > 0);
134 		uvmexp.swpgonly--;
135 	}
136 
137 	/* free any swap resources. */
138 	uvm_anon_dropswap(anon);
139 
140 	/*
141 	 * now that we've stripped the data areas from the anon, free the anon
142 	 * itself!
143 	 */
144 	KASSERT(anon->an_page == NULL);
145 	KASSERT(anon->an_swslot == 0);
146 
147 	pool_put(&uvm_anon_pool, anon);
148 }
149 
150 /*
151  * uvm_anon_dropswap:  release any swap resources from this anon.
152  */
153 void
154 uvm_anon_dropswap(struct vm_anon *anon)
155 {
156 
157 	if (anon->an_swslot == 0)
158 		return;
159 
160 	uvm_swap_free(anon->an_swslot, 1);
161 	anon->an_swslot = 0;
162 }
163 
164 /*
165  * uvm_anon_lockloanpg: given a locked anon, lock its resident page
166  *
167  * => on return:
168  *		 if there is a resident page:
169  *			if it is ownerless, we take over as owner
170  *		 we return the resident page (it can change during
171  *		 this function)
172  * => note that the only time an anon has an ownerless resident page
173  *	is if the page was loaned from a uvm_object and the uvm_object
174  *	disowned it
175  * => this only needs to be called when you want to do an operation
176  *	on an anon's resident page and that page has a non-zero loan
177  *	count.
178  */
179 struct vm_page *
180 uvm_anon_lockloanpg(struct vm_anon *anon)
181 {
182 	struct vm_page *pg;
183 
184 	/*
185 	 * loop while we have a resident page that has a non-zero loan count.
186 	 * if we successfully get our lock, we will "break" the loop.
187 	 * note that the test for pg->loan_count is not protected -- this
188 	 * may produce false positive results.   note that a false positive
189 	 * result may cause us to do more work than we need to, but it will
190 	 * not produce an incorrect result.
191 	 */
192 	while (((pg = anon->an_page) != NULL) && pg->loan_count != 0) {
193 		/*
194 		 * if page is un-owned [i.e. the object dropped its ownership],
195 		 * then we can take over as owner!
196 		 */
197 		if (pg->uobject == NULL && (pg->pg_flags & PQ_ANON) == 0) {
198 			uvm_lock_pageq();
199 			atomic_setbits_int(&pg->pg_flags, PQ_ANON);
200 			pg->loan_count--;	/* ... and drop our loan */
201 			uvm_unlock_pageq();
202 		}
203 		break;
204 	}
205 	return(pg);
206 }
207 
208 /*
209  * fetch an anon's page.
210  *
211  * => returns TRUE if pagein was aborted due to lack of memory.
212  */
213 
214 boolean_t
215 uvm_anon_pagein(struct vm_anon *anon)
216 {
217 	struct vm_page *pg;
218 	struct uvm_object *uobj;
219 	int rv;
220 
221 	rv = uvmfault_anonget(NULL, NULL, anon);
222 
223 	switch (rv) {
224 	case VM_PAGER_OK:
225 		break;
226 	case VM_PAGER_ERROR:
227 	case VM_PAGER_REFAULT:
228 		/*
229 		 * nothing more to do on errors.
230 		 * VM_PAGER_REFAULT can only mean that the anon was freed,
231 		 * so again there's nothing to do.
232 		 */
233 		return FALSE;
234 	default:
235 #ifdef DIAGNOSTIC
236 		panic("anon_pagein: uvmfault_anonget -> %d", rv);
237 #else
238 		return FALSE;
239 #endif
240 	}
241 
242 	/*
243 	 * ok, we've got the page now.
244 	 * mark it as dirty, clear its swslot and un-busy it.
245 	 */
246 	pg = anon->an_page;
247 	uobj = pg->uobject;
248 	uvm_swap_free(anon->an_swslot, 1);
249 	anon->an_swslot = 0;
250 	atomic_clearbits_int(&pg->pg_flags, PG_CLEAN);
251 
252 	/* deactivate the page (to put it on a page queue) */
253 	pmap_clear_reference(pg);
254 	pmap_page_protect(pg, PROT_NONE);
255 	uvm_lock_pageq();
256 	uvm_pagedeactivate(pg);
257 	uvm_unlock_pageq();
258 
259 	return FALSE;
260 }
261