uvm_anon.c revision 1.58
1/*	$NetBSD: uvm_anon.c,v 1.58 2011/07/05 13:47:24 yamt Exp $	*/
2
3/*
4 * Copyright (c) 1997 Charles D. Cranor and Washington University.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28/*
29 * uvm_anon.c: uvm anon ops
30 */
31
32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: uvm_anon.c,v 1.58 2011/07/05 13:47:24 yamt Exp $");
34
35#include "opt_uvmhist.h"
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/pool.h>
40#include <sys/kernel.h>
41
42#include <uvm/uvm.h>
43#include <uvm/uvm_swap.h>
44#include <uvm/uvm_pdpolicy.h>
45
46static struct pool_cache	uvm_anon_cache;
47
48static int			uvm_anon_ctor(void *, void *, int);
49
50void
51uvm_anon_init(void)
52{
53
54	pool_cache_bootstrap(&uvm_anon_cache, sizeof(struct vm_anon), 0, 0,
55	    PR_LARGECACHE, "anonpl", NULL, IPL_NONE, uvm_anon_ctor,
56	    NULL, NULL);
57}
58
59static int
60uvm_anon_ctor(void *arg, void *object, int flags)
61{
62	struct vm_anon *anon = object;
63
64	anon->an_ref = 0;
65	anon->an_page = NULL;
66#if defined(VMSWAP)
67	anon->an_swslot = 0;
68#endif
69	return 0;
70}
71
72/*
73 * uvm_analloc: allocate a new anon.
74 *
75 * => anon will have no lock associated.
76 */
77struct vm_anon *
78uvm_analloc(void)
79{
80	struct vm_anon *anon;
81
82	anon = pool_cache_get(&uvm_anon_cache, PR_NOWAIT);
83	if (anon) {
84		KASSERT(anon->an_ref == 0);
85		KASSERT(anon->an_page == NULL);
86#if defined(VMSWAP)
87		KASSERT(anon->an_swslot == 0);
88#endif
89		anon->an_ref = 1;
90		anon->an_lock = NULL;
91	}
92	return anon;
93}
94
95/*
96 * uvm_anfree1: free a single anon.
97 *
98 * => anon must be removed from the amap (if anon was in an amap).
99 * => amap must be locked or anon must not be associated.
100 * => amap lock may be dropped and re-acquired here.
101 */
102
103static void
104uvm_anfree1(struct vm_anon *anon)
105{
106	struct vm_page *pg = anon->an_page;
107
108	UVMHIST_FUNC("uvm_anfree"); UVMHIST_CALLED(maphist);
109	UVMHIST_LOG(maphist,"(anon=0x%x)", anon, 0,0,0);
110
111	KASSERT(anon->an_lock == NULL || mutex_owned(anon->an_lock));
112
113	/*
114	 * If there is a resident page and it is loaned, then anon may not
115	 * own it.  Call out to uvm_anon_lockloanpg() to identify and lock
116	 * the real owner of the page.
117	 */
118
119	if (pg && pg->loan_count) {
120		KASSERT(anon->an_lock != NULL);
121		pg = uvm_anon_lockloanpg(anon);
122	}
123
124	/*
125	 * Dispose the page, if it is resident.
126	 */
127
128	if (pg) {
129		KASSERT(anon->an_lock != NULL);
130
131		/*
132		 * If the page is owned by a UVM object (now locked),
133		 * then kill the loan on the page rather than free it,
134		 * and release the object lock.
135		 */
136
137		if (pg->uobject) {
138			mutex_enter(&uvm_pageqlock);
139			KASSERT(pg->loan_count > 0);
140			pg->loan_count--;
141			pg->uanon = NULL;
142			mutex_exit(&uvm_pageqlock);
143			mutex_exit(pg->uobject->vmobjlock);
144		} else {
145
146			/*
147			 * If page has no UVM object, then anon is the owner,
148			 * and it is already locked.
149			 */
150
151			KASSERT((pg->flags & PG_RELEASED) == 0);
152			pmap_page_protect(pg, VM_PROT_NONE);
153
154			/*
155			 * If the page is busy, mark it as PG_RELEASED, so
156			 * that uvm_anon_release(9) would release it later.
157			 */
158
159			if (pg->flags & PG_BUSY) {
160				pg->flags |= PG_RELEASED;
161				mutex_obj_hold(anon->an_lock);
162				return;
163			}
164			mutex_enter(&uvm_pageqlock);
165			uvm_pagefree(pg);
166			mutex_exit(&uvm_pageqlock);
167			UVMHIST_LOG(maphist, "anon 0x%x, page 0x%x: "
168				    "freed now!", anon, pg, 0, 0);
169		}
170	}
171
172#if defined(VMSWAP)
173	if (pg == NULL && anon->an_swslot > 0) {
174		/* This page is no longer only in swap. */
175		mutex_enter(&uvm_swap_data_lock);
176		KASSERT(uvmexp.swpgonly > 0);
177		uvmexp.swpgonly--;
178		mutex_exit(&uvm_swap_data_lock);
179	}
180#endif
181
182	/*
183	 * Free any swap resources, leave a page replacement hint, drop
184	 * the reference on lock and finally destroy the anon itself.
185	 */
186
187	uvm_anon_dropswap(anon);
188	uvmpdpol_anfree(anon);
189
190	KASSERT(anon->an_page == NULL);
191#if defined(VMSWAP)
192	KASSERT(anon->an_swslot == 0);
193#endif
194
195	pool_cache_put(&uvm_anon_cache, anon);
196	UVMHIST_LOG(maphist,"<- done!",0,0,0,0);
197}
198
199/*
200 * uvm_anfree: free a linked list of anon structures.
201 */
202void
203uvm_anfree(struct vm_anon *anon)
204{
205	struct vm_anon *next;
206
207	for (; anon != NULL; anon = next) {
208		/* Note: clearing an_link also clears a reference count. */
209		next = anon->an_link;
210		anon->an_link = NULL;
211		uvm_anfree1(anon);
212	}
213}
214
215/*
216 * uvm_anon_lockloanpg: given a locked anon, lock its resident page owner.
217 *
218 * => anon is locked by caller
219 * => on return: anon is locked
220 *		 if there is a resident page:
221 *			if it has a uobject, it is locked by us
222 *			if it is ownerless, we take over as owner
223 *		 we return the resident page (it can change during
224 *		 this function)
225 * => note that the only time an anon has an ownerless resident page
226 *	is if the page was loaned from a uvm_object and the uvm_object
227 *	disowned it
228 * => this only needs to be called when you want to do an operation
229 *	on an anon's resident page and that page has a non-zero loan
230 *	count.
231 */
232struct vm_page *
233uvm_anon_lockloanpg(struct vm_anon *anon)
234{
235	struct vm_page *pg;
236	bool locked = false;
237
238	KASSERT(mutex_owned(anon->an_lock));
239
240	/*
241	 * loop while we have a resident page that has a non-zero loan count.
242	 * if we successfully get our lock, we will "break" the loop.
243	 * note that the test for pg->loan_count is not protected -- this
244	 * may produce false positive results.   note that a false positive
245	 * result may cause us to do more work than we need to, but it will
246	 * not produce an incorrect result.
247	 */
248
249	while (((pg = anon->an_page) != NULL) && pg->loan_count != 0) {
250
251		/*
252		 * quickly check to see if the page has an object before
253		 * bothering to lock the page queues.   this may also produce
254		 * a false positive result, but that's ok because we do a real
255		 * check after that.
256		 */
257
258		if (pg->uobject) {
259			mutex_enter(&uvm_pageqlock);
260			if (pg->uobject) {
261				locked =
262				    mutex_tryenter(pg->uobject->vmobjlock);
263			} else {
264				/* object disowned before we got PQ lock */
265				locked = true;
266			}
267			mutex_exit(&uvm_pageqlock);
268
269			/*
270			 * if we didn't get a lock (try lock failed), then we
271			 * toggle our anon lock and try again
272			 */
273
274			if (!locked) {
275				/*
276				 * someone locking the object has a chance to
277				 * lock us right now
278				 *
279				 * XXX Better than yielding but inadequate.
280				 */
281				kpause("livelock", false, 1, anon->an_lock);
282				continue;
283			}
284		}
285
286		/*
287		 * If page is un-owned i.e. the object dropped its ownership,
288		 * then we have to take the ownership.
289		 */
290
291		if (pg->uobject == NULL && (pg->pqflags & PQ_ANON) == 0) {
292			mutex_enter(&uvm_pageqlock);
293			pg->pqflags |= PQ_ANON;
294			pg->loan_count--;
295			mutex_exit(&uvm_pageqlock);
296		}
297		break;
298	}
299	return pg;
300}
301
302#if defined(VMSWAP)
303
304/*
305 * uvm_anon_pagein: fetch an anon's page.
306 *
307 * => anon must be locked, and is unlocked upon return.
308 * => returns true if pagein was aborted due to lack of memory.
309 */
310
311bool
312uvm_anon_pagein(struct vm_amap *amap, struct vm_anon *anon)
313{
314	struct vm_page *pg;
315	struct uvm_object *uobj;
316
317	KASSERT(mutex_owned(anon->an_lock));
318	KASSERT(anon->an_lock == amap->am_lock);
319
320	/*
321	 * Get the page of the anon.
322	 */
323
324	switch (uvmfault_anonget(NULL, amap, anon)) {
325	case 0:
326		/* Success - we have the page. */
327		KASSERT(mutex_owned(anon->an_lock));
328		break;
329	case EIO:
330	case ERESTART:
331		/*
332		 * Nothing more to do on errors.  ERESTART means that the
333		 * anon was freed.
334		 */
335		return false;
336	default:
337		return true;
338	}
339
340	/*
341	 * Mark the page as dirty, clear its swslot and un-busy it.
342	 */
343
344	pg = anon->an_page;
345	uobj = pg->uobject;
346	if (anon->an_swslot > 0) {
347		uvm_swap_free(anon->an_swslot, 1);
348	}
349	anon->an_swslot = 0;
350	pg->flags &= ~PG_CLEAN;
351
352	/*
353	 * Deactivate the page (to put it on a page queue).
354	 */
355
356	mutex_enter(&uvm_pageqlock);
357	if (pg->wire_count == 0) {
358		uvm_pagedeactivate(pg);
359	}
360	mutex_exit(&uvm_pageqlock);
361
362	if (pg->flags & PG_WANTED) {
363		pg->flags &= ~PG_WANTED;
364		wakeup(pg);
365	}
366
367	mutex_exit(anon->an_lock);
368	if (uobj) {
369		mutex_exit(uobj->vmobjlock);
370	}
371	return false;
372}
373
374/*
375 * uvm_anon_dropswap: release any swap resources from this anon.
376 *
377 * => anon must be locked or have a reference count of 0.
378 */
379void
380uvm_anon_dropswap(struct vm_anon *anon)
381{
382	UVMHIST_FUNC("uvm_anon_dropswap"); UVMHIST_CALLED(maphist);
383
384	if (anon->an_swslot == 0)
385		return;
386
387	UVMHIST_LOG(maphist,"freeing swap for anon %p, paged to swslot 0x%x",
388		    anon, anon->an_swslot, 0, 0);
389	uvm_swap_free(anon->an_swslot, 1);
390	anon->an_swslot = 0;
391}
392
393#endif
394
395/*
396 * uvm_anon_release: release an anon and its page.
397 *
398 * => anon should not have any references.
399 * => anon must be locked.
400 */
401
402void
403uvm_anon_release(struct vm_anon *anon)
404{
405	struct vm_page *pg = anon->an_page;
406	kmutex_t *lock = anon->an_lock;
407
408	KASSERT(mutex_owned(lock));
409	KASSERT(pg != NULL);
410	KASSERT((pg->flags & PG_RELEASED) != 0);
411	KASSERT((pg->flags & PG_BUSY) != 0);
412	KASSERT(pg->uobject == NULL);
413	KASSERT(pg->uanon == anon);
414	KASSERT(pg->loan_count == 0);
415	KASSERT(anon->an_ref == 0);
416
417	mutex_enter(&uvm_pageqlock);
418	uvm_pagefree(pg);
419	mutex_exit(&uvm_pageqlock);
420
421	KASSERT(anon->an_page == NULL);
422
423	uvm_anfree(anon);
424	mutex_exit(lock);
425	mutex_obj_free(lock);
426}
427