uvm_anon.c revision 1.57
1/*	$NetBSD: uvm_anon.c,v 1.57 2011/06/24 01:39:22 rmind 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.57 2011/06/24 01:39:22 rmind 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				return;
162			}
163			mutex_enter(&uvm_pageqlock);
164			uvm_pagefree(pg);
165			mutex_exit(&uvm_pageqlock);
166			UVMHIST_LOG(maphist, "anon 0x%x, page 0x%x: "
167				    "freed now!", anon, pg, 0, 0);
168		}
169	}
170
171#if defined(VMSWAP)
172	if (pg == NULL && anon->an_swslot > 0) {
173		/* This page is no longer only in swap. */
174		mutex_enter(&uvm_swap_data_lock);
175		KASSERT(uvmexp.swpgonly > 0);
176		uvmexp.swpgonly--;
177		mutex_exit(&uvm_swap_data_lock);
178	}
179#endif
180
181	/*
182	 * Free any swap resources, leave a page replacement hint, drop
183	 * the reference on lock and finally destroy the anon itself.
184	 */
185
186	uvm_anon_dropswap(anon);
187	uvmpdpol_anfree(anon);
188
189	KASSERT(anon->an_page == NULL);
190#if defined(VMSWAP)
191	KASSERT(anon->an_swslot == 0);
192#endif
193
194	if (anon->an_lock != NULL) {
195		mutex_obj_free(anon->an_lock);
196	}
197	pool_cache_put(&uvm_anon_cache, anon);
198	UVMHIST_LOG(maphist,"<- done!",0,0,0,0);
199}
200
201/*
202 * uvm_anfree: free a linked list of anon structures.
203 */
204void
205uvm_anfree(struct vm_anon *anon)
206{
207	struct vm_anon *next;
208
209	for (; anon != NULL; anon = next) {
210		/* Note: clearing an_link also clears a reference count. */
211		next = anon->an_link;
212		anon->an_link = NULL;
213		uvm_anfree1(anon);
214	}
215}
216
217/*
218 * uvm_anon_lockloanpg: given a locked anon, lock its resident page owner.
219 *
220 * => anon is locked by caller
221 * => on return: anon is locked
222 *		 if there is a resident page:
223 *			if it has a uobject, it is locked by us
224 *			if it is ownerless, we take over as owner
225 *		 we return the resident page (it can change during
226 *		 this function)
227 * => note that the only time an anon has an ownerless resident page
228 *	is if the page was loaned from a uvm_object and the uvm_object
229 *	disowned it
230 * => this only needs to be called when you want to do an operation
231 *	on an anon's resident page and that page has a non-zero loan
232 *	count.
233 */
234struct vm_page *
235uvm_anon_lockloanpg(struct vm_anon *anon)
236{
237	struct vm_page *pg;
238	bool locked = false;
239
240	KASSERT(mutex_owned(anon->an_lock));
241
242	/*
243	 * loop while we have a resident page that has a non-zero loan count.
244	 * if we successfully get our lock, we will "break" the loop.
245	 * note that the test for pg->loan_count is not protected -- this
246	 * may produce false positive results.   note that a false positive
247	 * result may cause us to do more work than we need to, but it will
248	 * not produce an incorrect result.
249	 */
250
251	while (((pg = anon->an_page) != NULL) && pg->loan_count != 0) {
252
253		/*
254		 * quickly check to see if the page has an object before
255		 * bothering to lock the page queues.   this may also produce
256		 * a false positive result, but that's ok because we do a real
257		 * check after that.
258		 */
259
260		if (pg->uobject) {
261			mutex_enter(&uvm_pageqlock);
262			if (pg->uobject) {
263				locked =
264				    mutex_tryenter(pg->uobject->vmobjlock);
265			} else {
266				/* object disowned before we got PQ lock */
267				locked = true;
268			}
269			mutex_exit(&uvm_pageqlock);
270
271			/*
272			 * if we didn't get a lock (try lock failed), then we
273			 * toggle our anon lock and try again
274			 */
275
276			if (!locked) {
277				/*
278				 * someone locking the object has a chance to
279				 * lock us right now
280				 *
281				 * XXX Better than yielding but inadequate.
282				 */
283				kpause("livelock", false, 1, anon->an_lock);
284				continue;
285			}
286		}
287
288		/*
289		 * If page is un-owned i.e. the object dropped its ownership,
290		 * then we have to take the ownership.
291		 */
292
293		if (pg->uobject == NULL && (pg->pqflags & PQ_ANON) == 0) {
294			mutex_enter(&uvm_pageqlock);
295			pg->pqflags |= PQ_ANON;
296			pg->loan_count--;
297			mutex_exit(&uvm_pageqlock);
298		}
299		break;
300	}
301	return pg;
302}
303
304#if defined(VMSWAP)
305
306/*
307 * uvm_anon_pagein: fetch an anon's page.
308 *
309 * => anon must be locked, and is unlocked upon return.
310 * => returns true if pagein was aborted due to lack of memory.
311 */
312
313bool
314uvm_anon_pagein(struct vm_amap *amap, struct vm_anon *anon)
315{
316	struct vm_page *pg;
317	struct uvm_object *uobj;
318
319	KASSERT(mutex_owned(anon->an_lock));
320	KASSERT(anon->an_lock == amap->am_lock);
321
322	/*
323	 * Get the page of the anon.
324	 */
325
326	switch (uvmfault_anonget(NULL, amap, anon)) {
327	case 0:
328		/* Success - we have the page. */
329		KASSERT(mutex_owned(anon->an_lock));
330		break;
331	case EIO:
332	case ERESTART:
333		/*
334		 * Nothing more to do on errors.  ERESTART means that the
335		 * anon was freed.
336		 */
337		return false;
338	default:
339		return true;
340	}
341
342	/*
343	 * Mark the page as dirty, clear its swslot and un-busy it.
344	 */
345
346	pg = anon->an_page;
347	uobj = pg->uobject;
348	if (anon->an_swslot > 0) {
349		uvm_swap_free(anon->an_swslot, 1);
350	}
351	anon->an_swslot = 0;
352	pg->flags &= ~PG_CLEAN;
353
354	/*
355	 * Deactivate the page (to put it on a page queue).
356	 */
357
358	mutex_enter(&uvm_pageqlock);
359	if (pg->wire_count == 0) {
360		uvm_pagedeactivate(pg);
361	}
362	mutex_exit(&uvm_pageqlock);
363
364	if (pg->flags & PG_WANTED) {
365		pg->flags &= ~PG_WANTED;
366		wakeup(pg);
367	}
368
369	mutex_exit(anon->an_lock);
370	if (uobj) {
371		mutex_exit(uobj->vmobjlock);
372	}
373	return false;
374}
375
376/*
377 * uvm_anon_dropswap: release any swap resources from this anon.
378 *
379 * => anon must be locked or have a reference count of 0.
380 */
381void
382uvm_anon_dropswap(struct vm_anon *anon)
383{
384	UVMHIST_FUNC("uvm_anon_dropswap"); UVMHIST_CALLED(maphist);
385
386	if (anon->an_swslot == 0)
387		return;
388
389	UVMHIST_LOG(maphist,"freeing swap for anon %p, paged to swslot 0x%x",
390		    anon, anon->an_swslot, 0, 0);
391	uvm_swap_free(anon->an_swslot, 1);
392	anon->an_swslot = 0;
393}
394
395#endif
396
397/*
398 * uvm_anon_release: release an anon and its page.
399 *
400 * => anon should not have any references.
401 * => anon must be locked.
402 */
403
404void
405uvm_anon_release(struct vm_anon *anon)
406{
407	struct vm_page *pg = anon->an_page;
408	kmutex_t *lock = anon->an_lock;
409
410	KASSERT(mutex_owned(lock));
411	KASSERT(pg != NULL);
412	KASSERT((pg->flags & PG_RELEASED) != 0);
413	KASSERT((pg->flags & PG_BUSY) != 0);
414	KASSERT(pg->uobject == NULL);
415	KASSERT(pg->uanon == anon);
416	KASSERT(pg->loan_count == 0);
417	KASSERT(anon->an_ref == 0);
418
419	mutex_enter(&uvm_pageqlock);
420	uvm_pagefree(pg);
421	mutex_exit(&uvm_pageqlock);
422
423	KASSERT(anon->an_page == NULL);
424
425	mutex_obj_hold(lock);
426	uvm_anfree(anon);
427	mutex_exit(lock);
428	mutex_obj_free(lock);
429}
430